diff --git a/Changelog.md b/Changelog.md index f714503d38..fae105630c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -29,6 +29,7 @@ - Fixed `(hidden)` assignment labeling for assignments with `visible_on` and/or `visible_until` set (#7944) ### 🔧 Internal changes +- Added tests for `MarksGradersController` to achieve full test coverage for `randomly_assign` (#7947) - Refactored `AuthenticationHelper#sign_in` to set session values directly instead of going through `MainController#login` (#7962) - Updated `MainController` specs to dispatch `post :login` directly in tests that assert on login's response, instead of relying on `sign_in`'s internal request (#7962) - Added variable to enable simplecov in `spec_helper.rb` if and only if COVERAGE=true (#7960) diff --git a/doc/markus-contributors.txt b/doc/markus-contributors.txt index 73733d1ed0..a56bc1ebe3 100644 --- a/doc/markus-contributors.txt +++ b/doc/markus-contributors.txt @@ -58,6 +58,7 @@ Clément Delafargue Clément Schiano Danesh Dadachanji Daniel Dervishi +Daniel Rafailov Daniel St. Jules Daniyal Liaqat Daryn Lam diff --git a/spec/controllers/marks_graders_controller_spec.rb b/spec/controllers/marks_graders_controller_spec.rb index 2a590ec2b5..6aa07884b3 100644 --- a/spec/controllers/marks_graders_controller_spec.rb +++ b/spec/controllers/marks_graders_controller_spec.rb @@ -376,4 +376,115 @@ end end end + + describe '#randomly_assign' do + context 'does not require initial setup' do + context 'when students and graders are selected' do + it 'calls GradeEntryStudent `randomly_assign_tas` and ensures that every student gets a TA assigned to them' do + tas = create_list(:ta, 3) + ta_ids = tas.map(&:id) + + students = create_list(:student, 10, course: course) + student_ids = students.map(&:id) + + post_as instructor, :randomly_assign, params: { + course_id: course.id, + grade_entry_form_id: grade_entry_form.id, + students: student_ids, + graders: ta_ids + } + + expect(response).to have_http_status(:ok) + + students.each do |student| + ges = grade_entry_form.grade_entry_students.find_by!(role: student.id) + expect(ges.tas.count).to eq(1) + end + end + end + end + + context 'requires initial setup' do + before do + allow(GradeEntryStudent).to receive(:randomly_assign_tas) + end + + context 'when students and graders are selected' do + it 'calls GradeEntryStudent `randomly_assign_tas` and returns a success response' do + student = create(:student) + grade_entry_student = grade_entry_form.grade_entry_students.find_by(role: student) + grade_entry_student_ta = create(:grade_entry_student_ta, grade_entry_student: grade_entry_student) + + expect(GradeEntryStudent).to receive(:randomly_assign_tas).with( + [grade_entry_student.id.to_s], + [grade_entry_student_ta.id.to_s], + grade_entry_form + ) + + post_as instructor, :randomly_assign, params: { + course_id: course.id, + grade_entry_form_id: grade_entry_form.id, + students: [grade_entry_student.id], + graders: [grade_entry_student_ta.id] + } + + expect(response).to have_http_status(:ok) + end + end + + context 'when students are not selected' do + it 'returns bad request and sets a flash error' do + post_as instructor, :randomly_assign, params: { + course_id: course.id, + grade_entry_form_id: grade_entry_form.id, + students: [], + graders: [1] + } + + expect(response).to have_http_status(:bad_request) + expect(flash[:error]).to have_message(I18n.t('groups.select_a_student')) + end + end + + context 'when graders are not selected' do + it 'returns bad request and sets flash error' do + post_as instructor, :randomly_assign, params: { + course_id: course.id, + grade_entry_form_id: grade_entry_form.id, + students: [1], + graders: [] + } + + expect(response).to have_http_status(:bad_request) + expect(flash[:error]).to have_message(I18n.t('graders.select_a_grader')) + end + end + + context 'when students parameter is missing' do + it 'returns bad request and sets flash error' do + post_as instructor, :randomly_assign, params: { + course_id: course.id, + grade_entry_form_id: grade_entry_form.id, + graders: [1] + } + + expect(response).to have_http_status(:bad_request) + expect(flash[:error]).to have_message(I18n.t('groups.select_a_student')) + end + end + + context 'when graders parameter is missing' do + it 'returns bad request and sets flash error' do + post_as instructor, :randomly_assign, params: { + course_id: course.id, + grade_entry_form_id: grade_entry_form.id, + students: [1] + } + + expect(response).to have_http_status(:bad_request) + expect(flash[:error]).to have_message(I18n.t('graders.select_a_grader')) + end + end + end + end end