Use the same system to freeze time in all specs

This required changing the `voted_before_sign_in` slightly in order to
change what the method returns if the user signed in and voted at the
exact same microsecond.

It doesn't affect production code because it would be impossible for the
user to do both things at the same time.

As a side effect, the method now returns what the method name suggests.
Before this change, the correct method name would have been
`voted_before_or_at_the_same_time_of_sign_in`.

As a less desirable side effect, in the tests now we need to make sure
at least one second passes between the moment a user votes and the
moment a user signs in again. One microsecond wouldn't work because
the method `travel_to` automatically sets microseconds to zero in order
to avoid rounding issues.
This commit is contained in:
Javi Martín
2018-08-30 22:01:33 +02:00
parent 02b8bc6f69
commit b787e33883
3 changed files with 15 additions and 15 deletions

View File

@@ -46,7 +46,7 @@ module PollsHelper
end
def voted_before_sign_in(question)
question.answers.where(author: current_user).any? { |vote| current_user.current_sign_in_at >= vote.updated_at }
question.answers.where(author: current_user).any? { |vote| current_user.current_sign_in_at > vote.updated_at }
end
end

View File

@@ -387,18 +387,12 @@ feature 'Polls' do
end
end
context 'Booth & Website' do
context 'Booth & Website', :with_frozen_time do
let(:poll) { create(:poll, summary: "Summary", description: "Description", starts_at: '2017-12-01', ends_at: '2018-02-01') }
let(:poll) { create(:poll, summary: "Summary", description: "Description") }
let(:booth) { create(:poll_booth) }
let(:officer) { create(:poll_officer) }
before do
allow(Date).to receive(:current).and_return Date.new(2018,1,1)
allow(Date).to receive(:today).and_return Date.new(2018,1,1)
allow(Time).to receive(:current).and_return Time.zone.parse("2018-01-01 12:00:00")
end
scenario 'Already voted on booth cannot vote on website', :js do
create(:poll_shift, officer: officer, booth: booth, date: Date.current, task: :vote_collection)

View File

@@ -2,7 +2,7 @@ require 'rails_helper'
feature "Voter" do
context "Origin" do
context "Origin", :with_frozen_time do
let(:poll) { create(:poll, :current) }
let(:question) { create(:poll_question, poll: poll) }
@@ -125,12 +125,18 @@ feature "Voter" do
click_link "Sign out"
login_as user
visit poll_path(poll)
# Time needs to pass between the moment we vote and the moment
# we log in; otherwise the link to vote won't be available.
# It's safe to advance one second because this test isn't
# affected by possible date changes.
travel 1.second do
login_as user
visit poll_path(poll)
within("#poll_question_#{question.id}_answers") do
expect(page).to have_link(answer_yes.title)
expect(page).to have_link(answer_no.title)
within("#poll_question_#{question.id}_answers") do
expect(page).to have_link(answer_yes.title)
expect(page).to have_link(answer_no.title)
end
end
end
end