Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolving Issue 2191 - log_submit now validates for date #2251

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions app/controllers/assessment/handin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
105 changes: 105 additions & 0 deletions spec/controllers/assessments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,109 @@
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)
ogometz marked this conversation as resolved.
Show resolved Hide resolved
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
Loading