From 3bdfd126f8d46ea865f5c4741ae14a59e27635e2 Mon Sep 17 00:00:00 2001 From: Owen Gometz Date: Sun, 8 Dec 2024 14:58:34 -0500 Subject: [PATCH 1/5] Resolving Issue 2191 - log_submit now validates for date --- app/controllers/assessment/handin.rb | 7 ++ .../assessments_controller_spec.rb | 97 ++++++++++++++++++- 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/app/controllers/assessment/handin.rb b/app/controllers/assessment/handin.rb index d78886969..22b9839e2 100755 --- a/app/controllers/assessment/handin.rb +++ b/app/controllers/assessment/handin.rb @@ -270,6 +270,13 @@ def log_submit render(plain: err, status: :bad_request) && return end + # Validate assessment dates + current_time = Time.current + if current_time < @assessment.start_at || current_time > @assessment.end_at + err = "ERROR: Submissions are not allowed outside the assessment period" + render(plain: err, status: :bad_request) && return + end + @result = params[:result] render(plain: "ERROR: No result!", status: :bad_request) && return unless @result diff --git a/spec/controllers/assessments_controller_spec.rb b/spec/controllers/assessments_controller_spec.rb index 7f2b4ce03..c5537a73c 100644 --- a/spec/controllers/assessments_controller_spec.rb +++ b/spec/controllers/assessments_controller_spec.rb @@ -212,4 +212,99 @@ end end end -end + + describe "log_submit" do + context "when the date is inside of the assessment period" do + let!(:course_hash) { create_course_with_users_as_hash } + let!(:valid_assessment) do + assessment_path = Rails.root.join("courses/#{course_hash[:course].name}/assessment_valid") + FileUtils.mkdir_p(assessment_path) + FileUtils.mkdir_p("#{assessment_path}/handin") + assessment = FactoryBot.create(:assessment, + name: "assessment_valid", + course: course_hash[:course], + start_at: 2.days.ago, + due_at: 1.day.from_now, + end_at: 2.days.from_now, + allow_unofficial: true) + assessment.save! + assessment + end + + it "allows a valid log submission" do + user = course_hash[:students_cud].first + params = { + course_name: course_hash[:course].name, + name: valid_assessment.name, + user: user.email, + result: "test result" + } + post :log_submit, params: params + expect(response).to have_http_status(:ok) + expect(response.body).to eq("OK") + end + end + + context "when the submission is before the start date" do + let!(:course_hash) { create_course_with_users_as_hash } + let!(:invalid_assessment) do + assessment_path = Rails.root.join("courses/#{course_hash[:course].name}/assessment_invalid_date") + FileUtils.mkdir_p(assessment_path) + FileUtils.mkdir_p("#{assessment_path}/handin") + assessment = FactoryBot.create(:assessment, + name: "assessment_invalid_date", + course: course_hash[:course], + start_at: 1.day.from_now, + due_at: 2.days.from_now, + end_at: 3.days.from_now, + allow_unofficial: true) + assessment.save! + assessment + end + + it "rejects a log submission for having invalid dates" do + user = course_hash[:students_cud].first + params = { + course_name: course_hash[:course].name, + name: invalid_assessment.name, + user: user.email, + result: "test result" + } + post :log_submit, params: params + expect(response).to have_http_status(:bad_request) + expect(response.body).to eq("ERROR: Submissions are not allowed outside the assessment period") + end + end + + context "when the submission is after the end date" do + let!(:course_hash) { create_course_with_users_as_hash } + let!(:expired_assessment) do + assessment_path = Rails.root.join("courses/#{course_hash[:course].name}/assessment_expired") + FileUtils.mkdir_p(assessment_path) + FileUtils.mkdir_p("#{assessment_path}/handin") + assessment = FactoryBot.create(:assessment, + name: "assessment_expired", + course: course_hash[:course], + start_at: 3.days.ago, + due_at: 2.days.ago, + end_at: 1.day.ago, + allow_unofficial: true) + assessment.save! + assessment + end + + it "rejects a log submission for having invalid dates" do + user = course_hash[:students_cud].first + params = { + course_name: course_hash[:course].name, + name: expired_assessment.name, + user: user.email, + result: "test result" + } + post :log_submit, params: params + expect(response).to have_http_status(:bad_request) + expect(response.body).to eq("ERROR: Submissions are not allowed outside the assessment period") + end + end + end +end \ No newline at end of file From 2ffdfa272ef5b04ae8b4ba4da2031fa626079bc0 Mon Sep 17 00:00:00 2001 From: Owen Gometz Date: Sun, 8 Dec 2024 16:41:16 -0500 Subject: [PATCH 2/5] fixing linter issues --- .../assessments_controller_spec.rb | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/spec/controllers/assessments_controller_spec.rb b/spec/controllers/assessments_controller_spec.rb index c5537a73c..03773d373 100644 --- a/spec/controllers/assessments_controller_spec.rb +++ b/spec/controllers/assessments_controller_spec.rb @@ -230,7 +230,7 @@ assessment.save! assessment end - + it "allows a valid log submission" do user = course_hash[:students_cud].first params = { @@ -239,16 +239,18 @@ user: user.email, result: "test result" } - post :log_submit, params: params + post :log_submit, params expect(response).to have_http_status(:ok) expect(response.body).to eq("OK") end end - + context "when the submission is before the start date" do let!(:course_hash) { create_course_with_users_as_hash } let!(:invalid_assessment) do - assessment_path = Rails.root.join("courses/#{course_hash[:course].name}/assessment_invalid_date") + assessment_path = Rails.root.join( + "courses/#{course_hash[:course].name}/assessment_invalid_date" + ) FileUtils.mkdir_p(assessment_path) FileUtils.mkdir_p("#{assessment_path}/handin") assessment = FactoryBot.create(:assessment, @@ -261,7 +263,7 @@ assessment.save! assessment end - + it "rejects a log submission for having invalid dates" do user = course_hash[:students_cud].first params = { @@ -270,16 +272,19 @@ user: user.email, result: "test result" } - post :log_submit, params: params + post :log_submit, params expect(response).to have_http_status(:bad_request) - expect(response.body).to eq("ERROR: Submissions are not allowed outside the assessment period") + expect(response.body).to eq( + "ERROR: Submissions are not allowed outside the assessment period" + ) end end context "when the submission is after the end date" do let!(:course_hash) { create_course_with_users_as_hash } let!(:expired_assessment) do - assessment_path = Rails.root.join("courses/#{course_hash[:course].name}/assessment_expired") + assessment_path = Rails.root.join("courses/#{course_hash[:course].name} + /assessment_expired") FileUtils.mkdir_p(assessment_path) FileUtils.mkdir_p("#{assessment_path}/handin") assessment = FactoryBot.create(:assessment, @@ -292,7 +297,7 @@ assessment.save! assessment end - + it "rejects a log submission for having invalid dates" do user = course_hash[:students_cud].first params = { @@ -301,10 +306,12 @@ user: user.email, result: "test result" } - post :log_submit, params: params + post :log_submit, params expect(response).to have_http_status(:bad_request) - expect(response.body).to eq("ERROR: Submissions are not allowed outside the assessment period") + expect(response.body).to eq( + "ERROR: Submissions are not allowed outside the assessment period" + ) end end end -end \ No newline at end of file +end From 3492b9394acc0a3940f692072b02c76a21a64fa1 Mon Sep 17 00:00:00 2001 From: Owen Gometz Date: Sat, 14 Dec 2024 21:14:30 -0500 Subject: [PATCH 3/5] linter fixes being addressed --- .../assessments_controller_spec.rb | 55 ++++++++++--------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/spec/controllers/assessments_controller_spec.rb b/spec/controllers/assessments_controller_spec.rb index 03773d373..e1629b52a 100644 --- a/spec/controllers/assessments_controller_spec.rb +++ b/spec/controllers/assessments_controller_spec.rb @@ -220,13 +220,14 @@ assessment_path = Rails.root.join("courses/#{course_hash[:course].name}/assessment_valid") FileUtils.mkdir_p(assessment_path) FileUtils.mkdir_p("#{assessment_path}/handin") - assessment = FactoryBot.create(:assessment, - name: "assessment_valid", - course: course_hash[:course], - start_at: 2.days.ago, - due_at: 1.day.from_now, - end_at: 2.days.from_now, - allow_unofficial: true) + assessment = FactoryBot.create( + :assessment, + name: "assessment_valid", + course: course_hash[:course], + start_at: 2.days.ago, + due_at: 1.day.from_now, + end_at: 2.days.from_now, + allow_unofficial: true) assessment.save! assessment end @@ -239,7 +240,7 @@ user: user.email, result: "test result" } - post :log_submit, params + post :log_submit, params: params expect(response).to have_http_status(:ok) expect(response.body).to eq("OK") end @@ -250,16 +251,17 @@ let!(:invalid_assessment) do assessment_path = Rails.root.join( "courses/#{course_hash[:course].name}/assessment_invalid_date" - ) + ) FileUtils.mkdir_p(assessment_path) FileUtils.mkdir_p("#{assessment_path}/handin") - assessment = FactoryBot.create(:assessment, - name: "assessment_invalid_date", - course: course_hash[:course], - start_at: 1.day.from_now, - due_at: 2.days.from_now, - end_at: 3.days.from_now, - allow_unofficial: true) + assessment = FactoryBot.create( + :assessment, + name: "assessment_invalid_date", + course: course_hash[:course], + start_at: 1.day.from_now, + due_at: 2.days.from_now, + end_at: 3.days.from_now, + allow_unofficial: true) assessment.save! assessment end @@ -272,11 +274,11 @@ user: user.email, result: "test result" } - post :log_submit, params + post :log_submit, params: params expect(response).to have_http_status(:bad_request) expect(response.body).to eq( "ERROR: Submissions are not allowed outside the assessment period" - ) + ) end end @@ -287,13 +289,14 @@ /assessment_expired") FileUtils.mkdir_p(assessment_path) FileUtils.mkdir_p("#{assessment_path}/handin") - assessment = FactoryBot.create(:assessment, - name: "assessment_expired", - course: course_hash[:course], - start_at: 3.days.ago, - due_at: 2.days.ago, - end_at: 1.day.ago, - allow_unofficial: true) + assessment = FactoryBot.create( + :assessment, + name: "assessment_expired", + course: course_hash[:course], + start_at: 3.days.ago, + due_at: 2.days.ago, + end_at: 1.day.ago, + allow_unofficial: true) assessment.save! assessment end @@ -306,7 +309,7 @@ user: user.email, result: "test result" } - post :log_submit, params + post :log_submit, params: params expect(response).to have_http_status(:bad_request) expect(response.body).to eq( "ERROR: Submissions are not allowed outside the assessment period" From 7d73c17d3c80a02d34b461615587b02709c02e09 Mon Sep 17 00:00:00 2001 From: Owen Gometz Date: Sat, 14 Dec 2024 21:34:59 -0500 Subject: [PATCH 4/5] fixing linter issues --- spec/controllers/assessments_controller_spec.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spec/controllers/assessments_controller_spec.rb b/spec/controllers/assessments_controller_spec.rb index e1629b52a..6a00f6694 100644 --- a/spec/controllers/assessments_controller_spec.rb +++ b/spec/controllers/assessments_controller_spec.rb @@ -285,8 +285,9 @@ context "when the submission is after the end date" do let!(:course_hash) { create_course_with_users_as_hash } let!(:expired_assessment) do - assessment_path = Rails.root.join("courses/#{course_hash[:course].name} - /assessment_expired") + assessment_path = Rails.root.join( + "courses/#{course_hash[:course].name}/assessment_expired" + ) FileUtils.mkdir_p(assessment_path) FileUtils.mkdir_p("#{assessment_path}/handin") assessment = FactoryBot.create( From 27e813ffd00449dd39e70bde8ceeaf1073d104d7 Mon Sep 17 00:00:00 2001 From: Owen Gometz Date: Mon, 16 Dec 2024 16:41:12 -0500 Subject: [PATCH 5/5] final linter fixes --- spec/controllers/assessments_controller_spec.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/spec/controllers/assessments_controller_spec.rb b/spec/controllers/assessments_controller_spec.rb index 6a00f6694..addef635a 100644 --- a/spec/controllers/assessments_controller_spec.rb +++ b/spec/controllers/assessments_controller_spec.rb @@ -227,7 +227,8 @@ start_at: 2.days.ago, due_at: 1.day.from_now, end_at: 2.days.from_now, - allow_unofficial: true) + allow_unofficial: true + ) assessment.save! assessment end @@ -240,7 +241,7 @@ user: user.email, result: "test result" } - post :log_submit, params: params + post(:log_submit, params:) expect(response).to have_http_status(:ok) expect(response.body).to eq("OK") end @@ -261,7 +262,8 @@ start_at: 1.day.from_now, due_at: 2.days.from_now, end_at: 3.days.from_now, - allow_unofficial: true) + allow_unofficial: true + ) assessment.save! assessment end @@ -274,7 +276,7 @@ user: user.email, result: "test result" } - post :log_submit, params: params + post(:log_submit, params:) expect(response).to have_http_status(:bad_request) expect(response.body).to eq( "ERROR: Submissions are not allowed outside the assessment period" @@ -297,7 +299,8 @@ start_at: 3.days.ago, due_at: 2.days.ago, end_at: 1.day.ago, - allow_unofficial: true) + allow_unofficial: true + ) assessment.save! assessment end @@ -310,7 +313,7 @@ user: user.email, result: "test result" } - post :log_submit, params: params + post(:log_submit, params:) expect(response).to have_http_status(:bad_request) expect(response.body).to eq( "ERROR: Submissions are not allowed outside the assessment period"