Skip to content

Commit

Permalink
chore(api): gatekeep timer start at and set test
Browse files Browse the repository at this point in the history
- if submission already has timer started at, we won't allow to re-configure it
- add test cases regarding this API
  • Loading branch information
bivanalhar committed Sep 12, 2024
1 parent c095ff8 commit 181cb3b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ def generate_live_feedback
end

def set_timer_started_at
timer_started_at = Time.zone.now
unless @submission.timer_started_at
@submission.timer_started_at = Time.zone.now

@submission.timer_started_at = timer_started_at
raise ActiveRecord::Rollback unless @submission.save

raise ActiveRecord::Rollback unless @submission.save

Course::Assessment::Submission::ForceSubmitTimedSubmissionJob.
set(wait_until: timer_started_at + @assessment.time_limit.minutes + FORCE_SUBMIT_DELAY).
perform_later(@assessment, @submission_id, @submission.creator)
Course::Assessment::Submission::ForceSubmitTimedSubmissionJob.
set(wait_until: @submission.timer_started_at + @assessment.time_limit.minutes + FORCE_SUBMIT_DELAY).
perform_later(@assessment, @submission_id, @submission.creator)
end

render json: { timerStartedAt: timer_started_at }
render json: { timerStartedAt: @submission.timer_started_at }
end

# Reload the current answer or reset it, depending on parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,55 @@
end
end

describe '#set_timer_started_at' do
let!(:assessment) { create(:assessment, :published, *assessment_traits, course: course, time_limit: 120) }
let!(:assessment2) { create(:assessment, :published, *assessment_traits, course: course, time_limit: 120) }
let!(:submission) { create(:submission, :attempting, assessment: assessment, creator: user) }
let!(:submission2) do
create(:submission, :attempting, assessment: assessment2, creator: user,
timer_started_at: Time.zone.now - 5.seconds)
end

context 'when user first-time attempt the timed assessment' do
subject do
patch :set_timer_started_at, params: {
course_id: course, assessment_id: assessment.id, id: submission.id
}
end

it 'assigns the timer_started_at to current time' do
subject
json_result = JSON.parse(response.body)
expect(json_result['timerStartedAt'].to_datetime.utc).to \
be_within(1.second).of Time.zone.now.utc
expect(submission.reload.timer_started_at.utc).to \
be_within(1.second).of Time.zone.now.utc
end
end

context 'when user has already attempted the timed assessment before' do
subject do
patch :set_timer_started_at, params: {
course_id: course, assessment_id: assessment2.id, id: submission2.id
}
end

it 'assigns the timer_started_at to current time' do
subject
json_result = JSON.parse(response.body)
expect(json_result['timerStartedAt'].to_datetime.utc).not_to \
be_within(1.second).of Time.zone.now.utc
expect(submission2.reload.timer_started_at.utc).not_to \
be_within(1.second).of Time.zone.now.utc

expect(json_result['timerStartedAt'].to_datetime.utc).to \
be_within(1.second).of (Time.zone.now - 5.seconds).utc
expect(submission2.reload.timer_started_at.utc).to \
be_within(1.second).of (Time.zone.now - 5.seconds).utc
end
end
end

describe 'submission_actions' do
let!(:students) { create_list(:course_student, 5, course: course) }
let!(:phantom_student) { create(:course_student, :phantom, course: course) }
Expand Down

0 comments on commit 181cb3b

Please sign in to comment.