diff --git a/app/helpers/polls_helper.rb b/app/helpers/polls_helper.rb index 0d5b0a605..0cb875dff 100644 --- a/app/helpers/polls_helper.rb +++ b/app/helpers/polls_helper.rb @@ -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 diff --git a/spec/factories/budgets.rb b/spec/factories/budgets.rb index e509b2cad..d713302fd 100644 --- a/spec/factories/budgets.rb +++ b/spec/factories/budgets.rb @@ -149,11 +149,11 @@ FactoryBot.define do end trait :hidden do - hidden_at Time.current + hidden_at { Time.current } end trait :with_ignored_flag do - ignored_flag_at Time.current + ignored_flag_at { Time.current } end trait :flagged do @@ -163,7 +163,7 @@ FactoryBot.define do end trait :with_confirmed_hide do - confirmed_hide_at Time.current + confirmed_hide_at { Time.current } end end diff --git a/spec/factories/notifications.rb b/spec/factories/notifications.rb index 08f4edb86..b43aa7660 100644 --- a/spec/factories/notifications.rb +++ b/spec/factories/notifications.rb @@ -18,7 +18,7 @@ FactoryBot.define do trait :sent do recipients_count 1 - sent_at Time.current + sent_at { Time.current } end end end diff --git a/spec/factories/proposals.rb b/spec/factories/proposals.rb index 01928cdb1..358411d79 100644 --- a/spec/factories/proposals.rb +++ b/spec/factories/proposals.rb @@ -64,15 +64,15 @@ FactoryBot.define do end trait :ignored do - ignored_at Date.current + ignored_at { Date.current } end trait :hidden do - hidden_at Date.current + hidden_at { Date.current } end trait :with_confirmed_hide do - confirmed_hide_at Time.current + confirmed_hide_at { Time.current } end end diff --git a/spec/features/admin/admin_notifications_spec.rb b/spec/features/admin/admin_notifications_spec.rb index f3706cbdc..d72b8529f 100644 --- a/spec/features/admin/admin_notifications_spec.rb +++ b/spec/features/admin/admin_notifications_spec.rb @@ -39,7 +39,7 @@ feature "Admin Notifications" do end context "Index" do - scenario "Valid Admin Notifications" do + scenario "Valid Admin Notifications", :with_frozen_time do draft = create(:admin_notification, segment_recipient: :all_users, title: 'Not yet sent') sent = create(:admin_notification, :sent, segment_recipient: :administrators, title: 'Sent one') diff --git a/spec/features/legislation/processes_spec.rb b/spec/features/legislation/processes_spec.rb index 0f233a577..f8bd9f09c 100644 --- a/spec/features/legislation/processes_spec.rb +++ b/spec/features/legislation/processes_spec.rb @@ -144,7 +144,7 @@ feature 'Legislation' do end context 'debate phase' do - scenario 'not open' do + scenario 'not open', :with_frozen_time do process = create(:legislation_process, debate_start_date: Date.current + 1.day, debate_end_date: Date.current + 2.days) visit legislation_process_path(process) @@ -179,7 +179,7 @@ feature 'Legislation' do end context 'draft publication phase' do - scenario 'not open' do + scenario 'not open', :with_frozen_time do process = create(:legislation_process, draft_publication_date: Date.current + 1.day) visit draft_publication_legislation_process_path(process) @@ -199,7 +199,7 @@ feature 'Legislation' do end context 'allegations phase' do - scenario 'not open' do + scenario 'not open', :with_frozen_time do process = create(:legislation_process, allegations_start_date: Date.current + 1.day, allegations_end_date: Date.current + 2.days) visit allegations_legislation_process_path(process) @@ -219,7 +219,7 @@ feature 'Legislation' do end context 'final version publication phase' do - scenario 'not open' do + scenario 'not open', :with_frozen_time do process = create(:legislation_process, result_publication_date: Date.current + 1.day) visit result_publication_legislation_process_path(process) diff --git a/spec/features/officing/residence_spec.rb b/spec/features/officing/residence_spec.rb index e5f060366..034d47420 100644 --- a/spec/features/officing/residence_spec.rb +++ b/spec/features/officing/residence_spec.rb @@ -1,16 +1,8 @@ require 'rails_helper' -feature 'Residence' do +feature 'Residence', :with_frozen_time do let(:officer) { create(:poll_officer) } - background do - travel_to Time.now # TODO: use `freeze_time` after migrating to Rails 5. - end - - after do - travel_back - end - feature "Officers without assignments" do scenario "Can not access residence verification" do diff --git a/spec/features/officing/results_spec.rb b/spec/features/officing/results_spec.rb index 821e4b001..376629314 100644 --- a/spec/features/officing/results_spec.rb +++ b/spec/features/officing/results_spec.rb @@ -1,9 +1,8 @@ require 'rails_helper' -feature 'Officing Results' do +feature 'Officing Results', :with_frozen_time do background do - travel_to Time.now # TODO: use `freeze_time` after migrating to Rails 5. @poll_officer = create(:poll_officer) @officer_assignment = create(:poll_officer_assignment, :final, officer: @poll_officer) @poll = @officer_assignment.booth_assignment.poll @@ -18,10 +17,6 @@ feature 'Officing Results' do login_as(@poll_officer.user) end - after do - travel_back - end - scenario 'Only polls where user is officer for results are accessible' do regular_officer_assignment_1 = create(:poll_officer_assignment, officer: @poll_officer) regular_officer_assignment_2 = create(:poll_officer_assignment, officer: @poll_officer) diff --git a/spec/features/officing_spec.rb b/spec/features/officing_spec.rb index b2ead66f2..d5e410afb 100644 --- a/spec/features/officing_spec.rb +++ b/spec/features/officing_spec.rb @@ -122,7 +122,7 @@ feature 'Poll Officing' do expect(page).not_to have_css('#moderation_menu') end - scenario 'Officing dashboard available for multiple sessions', :js do + scenario 'Officing dashboard available for multiple sessions', :js, :with_frozen_time do poll = create(:poll) booth = create(:poll_booth) booth_assignment = create(:poll_booth_assignment, poll: poll, booth: booth) diff --git a/spec/features/polls/polls_spec.rb b/spec/features/polls/polls_spec.rb index a2e009f77..b95bb8129 100644 --- a/spec/features/polls/polls_spec.rb +++ b/spec/features/polls/polls_spec.rb @@ -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) diff --git a/spec/features/polls/voter_spec.rb b/spec/features/polls/voter_spec.rb index 5ba89fc3c..f7f1b58c8 100644 --- a/spec/features/polls/voter_spec.rb +++ b/spec/features/polls/voter_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 044697bef..f14ee4041 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -82,6 +82,14 @@ RSpec.configure do |config| Bullet.end_request end + config.before(:each, :with_frozen_time) do + travel_to Time.now # TODO: use `freeze_time` after migrating to Rails 5. + end + + config.after(:each, :with_frozen_time) do + travel_back + end + # Allows RSpec to persist some state between runs in order to support # the `--only-failures` and `--next-failure` CLI options. config.example_status_persistence_file_path = "spec/examples.txt"