Skip to content

Commit

Permalink
Merge pull request #5891 from dodona-edu/fix/questions-json-output-empty
Browse files Browse the repository at this point in the history
Added json-output for the questions endpoint
  • Loading branch information
thvmulle authored Oct 28, 2024
2 parents e9ee308 + 0429933 commit 8ef39d5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
12 changes: 11 additions & 1 deletion app/views/courses/questions.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
json.array! @questions
json.unanswered do
json.array! @unanswered || [], partial: 'annotations/annotation', as: :annotation
end

json.in_progress do
json.array! @in_progress || [], partial: 'annotations/annotation', as: :annotation
end

json.answered do
json.array! @answered || [], partial: 'annotations/annotation', as: :annotation
end
73 changes: 71 additions & 2 deletions test/controllers/courses_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ def with_users_signed_in(users)
create :question, question_state: :in_progress, submission: submission
get questions_course_path(@course)

assert :ok, "#{who} should be able to view questions"
assert_response :ok, "#{who} should be able to view questions"
end
end

Expand All @@ -883,7 +883,7 @@ def with_users_signed_in(users)
with_users_signed_in @not_admins do |who|
get questions_course_path(@course)

assert :ok, "#{who} should not be able to view questions"
assert_response :redirect, "#{who} should not be able to view questions"
end
end

Expand All @@ -905,6 +905,75 @@ def with_users_signed_in(users)
end
end

test 'super admins are able to view questions in JSON format' do
add_admins
super_admins = @admins.reject(&:student?)
with_users_signed_in super_admins do |who|
# without delayed jobs, in progress is automatically reset to unanswered
with_delayed_jobs do
# Create some questions so we actually render something
submission = create :submission, course: @course
create :question, question_state: :answered, submission: submission
create :question, question_state: :unanswered, submission: submission
create :question, question_state: :in_progress, submission: submission
get questions_course_path(@course), as: :json

assert_response :ok, "#{who} should be able to view questions in JSON format"
end
end
end

test 'super admins get correct question info in JSON format' do
add_admins
super_admins = @admins.reject(&:student?)
with_users_signed_in super_admins do |_who|
# without delayed jobs, in progress is automatically reset to unanswered
with_delayed_jobs do
submission = create :submission, course: @course
create :question, question_state: :answered, submission: submission
create :question, question_state: :unanswered, submission: submission
create :question, question_state: :in_progress, submission: submission
get questions_course_path(@course), as: :json

json_response = response.parsed_body

assert json_response.key?('unanswered'), "The 'unanswered' key should be present in the JSON response"
assert json_response.key?('in_progress'), "The 'in_progress' key should be present in the JSON response"
assert json_response.key?('answered'), "The 'answered' key should be present in the JSON response"

assert_equal 1, json_response['unanswered'].size, 'There should be 1 unanswered question in the JSON response'
assert_equal 1, json_response['in_progress'].size, 'There should be 1 in_progress question in the JSON response'
assert_equal 1, json_response['answered'].size, 'There should be 1 answered question in the JSON response'
end
run_delayed_jobs
end
end

test 'super admins can view empty question lists' do
add_admins
super_admins = @admins.reject(&:student?)
with_users_signed_in super_admins do |_who|
get questions_course_path(@course), as: :json

assert_response :ok

json_response = response.parsed_body

assert_empty json_response['unanswered'], 'There should be 0 unanswered questions in the JSON response'
assert_empty json_response['in_progress'], 'There should be 0 in_progress questions in the JSON response'
assert_empty json_response['answered'], 'There should be 0 answered questions in the JSON response'
end
end

test 'non admins cannot view questions in JSON format' do
add_not_admins
with_users_signed_in @not_admins do |who|
get questions_course_path(@course), as: :json

assert (response.forbidden? || response.unauthorized?), "#{who} should not be able to view questions in JSON format"
end
end

test 'Icalendar link exports valid and correct ics file' do
time1 = DateTime.now
time2 = DateTime.now + 1.day + 1.hour + 1.second
Expand Down

0 comments on commit 8ef39d5

Please sign in to comment.