Skip to content

Commit

Permalink
Tests for finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSerth committed Nov 22, 2023
1 parent 1f2b6bf commit 3b9399d
Showing 1 changed file with 41 additions and 83 deletions.
124 changes: 41 additions & 83 deletions spec/controllers/submissions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
render_views

let(:exercise) { create(:math) }
let(:submission) { create(:submission, exercise:, contributor:) }
let(:cause) { 'save' }
let(:submission) { create(:submission, exercise:, contributor:, cause:) }

shared_examples 'a regular user' do |record_not_found_status_code|
describe 'POST #create' do
Expand Down Expand Up @@ -248,117 +249,80 @@
expect_assigns(file: :file)
expect_http_status(204)
end
end

shared_examples 'an internal user' do
describe 'GET #finalize' do
context 'when a community solution is offered' do
let!(:community_solution) { CommunitySolution.create(exercise:) }
let(:perform_request) { proc { get :finalize, params: {id: submission.id} } }
let(:cause) { 'assess' }

before do
allow(Java21Study).to receive(:allow_redirect_to_community_solution?).and_return(true)
get :finalize, params: {id: submission.id}
end
context 'when the request is performed' do
before { perform_request.call }

expect_redirect { edit_community_solution_path(community_solution, lock_id: CommunitySolutionLock.last) }
expect_assigns(submission: :submission)
expect_redirect
end

context 'when a community solution is not offered' do
before do
get :finalize, params: {id: submission.id}
end

it 'updates cause to submit' do
submission.reload
expect(submission.cause).to eq('submit')
end

expect_redirect { lti_return_path(submission_id: submission.id) }
it 'updates cause to submit' do
expect { perform_request.call && submission.reload }.to change(submission, :cause).from('assess').to('submit')
end
end
end

shared_examples 'an external user' do
describe 'GET #finalize' do
context 'when a community solution is offered' do
context 'when contributing to a community solution is possible' do
let!(:community_solution) { CommunitySolution.create(exercise:) }

before do
allow(Java21Study).to receive(:allow_redirect_to_community_solution?).and_return(true)
get :finalize, params: {id: submission.id}
perform_request.call
end

expect_redirect { edit_community_solution_path(community_solution, lock_id: CommunitySolutionLock.last) }
end

context 'when an external user is selected for feedback' do
context 'when sharing exercise feedback is desired' do
before do
uef&.save!
allow_any_instance_of(Submission).to receive(:redirect_to_feedback?).and_return(true)
get :finalize, params: {id: submission.id}
perform_request.call
end

it 'updates cause to submit' do
submission.reload
expect(submission.cause).to eq('submit')
context 'without any previous feedback' do
let(:uef) { nil }

expect_redirect { new_user_exercise_feedback_path(user_exercise_feedback: {exercise_id: submission.exercise.id}) }
end

context 'when a user feedback already exists' do
let!(:uef) { UserExerciseFeedback.create!(exercise_id: submission.exercise.id, user: current_user) }
context 'with a previous feedback for the same exercise' do
let(:uef) { UserExerciseFeedback.create(exercise:, user: current_user) }

expect_redirect { edit_user_exercise_feedback_path(uef) }
end

context 'when user feedback does not exist' do
expect_redirect { new_user_exercise_feedback_path(user_exercise_feedback: {exercise_id: submission.exercise.id}) }
end
end

context 'when user has an own unsolved rfc' do
context 'with an RfC' do
before do
allow(submission).to receive(:redirect_to_feedback?).and_return(false)
get :finalize, params: {id: submission.id}
rfc.save!
allow_any_instance_of(Submission).to receive(:redirect_to_feedback?).and_return(false)
perform_request.call
end

let(:rfc) { create(:rfc) }
context 'when an own RfC is unsolved' do
let(:rfc) { create(:rfc, user: current_user, exercise:, submission:) }
let(:flash_message) { I18n.t('exercises.editor.exercise_finished_redirect_to_own_rfc') }

it 'updates cause to submit' do
submission.reload
expect(submission.cause).to eq('submit')
expect_flash_message(:notice, I18n.t('exercises.editor.exercise_finished_redirect_to_own_rfc'))
expect_redirect { request_for_comment_url(rfc) }
end

expect_flash_message(:notice, I18n.t('exercises.editor.exercise_finished_redirect_to_own_rfc'))
expect_redirect { request_for_comment_url(rfc) }
end

context 'when unsolved rfc exists' do
let!(:rfc_peer) do
rfc = create(:rfc)
rfc.user.update(consumer: current_user.consumer)
rfc.submission.update(study_group: current_user.study_groups.first)
rfc
end
context 'when another RfC is unsolved' do
let(:rfc) { create(:rfc, exercise:) }

before do
get :finalize, params: {id: submission.id}
expect_flash_message(:notice, I18n.t('exercises.editor.exercise_finished_redirect_to_rfc'))
expect_redirect { request_for_comment_url(rfc) }
end

it 'updates cause to submit' do
submission.reload
expect(submission.cause).to eq('submit')
end

expect_flash_message(:notice, I18n.t('exercises.editor.exercise_finished_redirect_to_rfc'))
expect_redirect { request_for_comment_url(rfc_peer) }
end

context 'when unsolved rfc does not exist' do
context 'when neither a community solution, feedback nor RfC is available' do
before do
get :finalize, params: {id: submission.id}
end

it 'updates cause to submit' do
submission.reload
expect(submission.cause).to eq('submit')
allow_any_instance_of(Submission).to receive(:redirect_to_feedback?).and_return(false)
perform_request.call
end

expect_redirect { lti_return_path(submission_id: submission.id) }
Expand All @@ -379,8 +343,9 @@

context 'with an admin user' do
let(:contributor) { create(:admin) }
let(:current_user) { contributor }

before { allow(controller).to receive(:current_user).and_return(contributor) }
before { allow(controller).to receive_messages(current_user:) }

describe 'GET #index' do
before do
Expand All @@ -394,7 +359,6 @@
end

it_behaves_like 'a regular user', :not_found
it_behaves_like 'an internal user'
end

context 'with a programming group' do
Expand All @@ -403,25 +367,19 @@
let(:contributor) { create(:programming_group, exercise:, users: [group_author, other_group_author]) }
let(:current_user) { group_author }

before do
allow(controller).to receive_messages(current_contributor: contributor, current_user: group_author)
end
before { allow(controller).to receive_messages(current_contributor: contributor, current_user:) }

it_behaves_like 'a regular user', :unauthorized
it_behaves_like 'denies access for regular, non-admin users'
it_behaves_like 'an external user'
end

context 'with a learner' do
let(:contributor) { create(:external_user) }
let(:current_user) { contributor }

before do
allow(controller).to receive_messages(current_user: contributor)
end
before { allow(controller).to receive_messages(current_user:) }

it_behaves_like 'a regular user', :unauthorized
it_behaves_like 'denies access for regular, non-admin users'
it_behaves_like 'an external user'
end
end

0 comments on commit 3b9399d

Please sign in to comment.