From 4a559ed3cdbbabee85a7d383ef6a089a202e69f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Mart=C3=ADn?= Date: Sun, 1 Jul 2018 20:15:46 +0200 Subject: [PATCH 1/4] Avoid date changes during results tests. As explained by @iagirre with pinpoint accuracy [1]: "If the officer_assignments are created at 23:59:59 and the rest of the test is executed after 00:00:00, the dates for the objects and the `Date.current` (used to check if there are any shifts today) won't be the same, because the shift will be for, lets say, 07/03/2018 and `Date.current` will be 08/03/2018, so, there are no shifts." Freezing time avoids this issue. Related to issues #2520 and #2521. [1] https://github.com/AyuntamientoMadrid/consul/pull/1342 --- spec/features/officing/results_spec.rb | 6 ++++++ spec/time_helper.rb | 8 ++++++++ 2 files changed, 14 insertions(+) create mode 100644 spec/time_helper.rb diff --git a/spec/features/officing/results_spec.rb b/spec/features/officing/results_spec.rb index 91a194eec..b0b4d66be 100644 --- a/spec/features/officing/results_spec.rb +++ b/spec/features/officing/results_spec.rb @@ -1,8 +1,10 @@ require 'rails_helper' +require 'time_helper' feature 'Officing Results' do background do + freeze_time @poll_officer = create(:poll_officer) @officer_assignment = create(:poll_officer_assignment, :final, officer: @poll_officer) @poll = @officer_assignment.booth_assignment.poll @@ -17,6 +19,10 @@ 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/time_helper.rb b/spec/time_helper.rb new file mode 100644 index 000000000..547f3772c --- /dev/null +++ b/spec/time_helper.rb @@ -0,0 +1,8 @@ +module ActiveSupport::Testing::TimeHelpers + # Copied from Rails 5.2. TODO: remove after migrating to Rails 5. + def freeze_time(&block) + travel_to Time.now, &block + end +end + +include ActiveSupport::Testing::TimeHelpers From de0afe1621ca5bf864930b3d05b17e1d800dc95d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Mart=C3=ADn?= Date: Mon, 2 Jul 2018 18:26:54 +0200 Subject: [PATCH 2/4] Don't monkey patch ActiveSupport. It could have side effects (for example, a conflict after upgrading to Rails 5). Thanks @aitbw for the suggestion! --- spec/features/officing/results_spec.rb | 3 +-- spec/spec_helper.rb | 1 + spec/time_helper.rb | 8 -------- 3 files changed, 2 insertions(+), 10 deletions(-) delete mode 100644 spec/time_helper.rb diff --git a/spec/features/officing/results_spec.rb b/spec/features/officing/results_spec.rb index b0b4d66be..821e4b001 100644 --- a/spec/features/officing/results_spec.rb +++ b/spec/features/officing/results_spec.rb @@ -1,10 +1,9 @@ require 'rails_helper' -require 'time_helper' feature 'Officing Results' do background do - freeze_time + 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2598f085e..1f4c703b7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,6 +18,7 @@ RSpec.configure do |config| config.include(EmailSpec::Helpers) config.include(EmailSpec::Matchers) config.include(CommonActions) + config.include(ActiveSupport::Testing::TimeHelpers) config.before(:suite) do DatabaseCleaner.clean_with :truncation end diff --git a/spec/time_helper.rb b/spec/time_helper.rb deleted file mode 100644 index 547f3772c..000000000 --- a/spec/time_helper.rb +++ /dev/null @@ -1,8 +0,0 @@ -module ActiveSupport::Testing::TimeHelpers - # Copied from Rails 5.2. TODO: remove after migrating to Rails 5. - def freeze_time(&block) - travel_to Time.now, &block - end -end - -include ActiveSupport::Testing::TimeHelpers From 0cf799add6d4259628471c943e38a3f0b75bbff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Mart=C3=ADn?= Date: Sat, 7 Jul 2018 00:08:20 +0200 Subject: [PATCH 3/4] Use dynamic times and dates in factories. The tests depending on the date changing were still failing because Date.current was being stubbed after loading the factories. The following lines affected these specific tests: factory :poll_officer_assignment, class: 'Poll::OfficerAssignment' do (...) date Date.current end So if the tests were executed right before midnight, the sequence was: 1. The factories file was loaded, assigning Date.current to the date of every Poll::OfficerAssignment to be created. 2. Time passed, so now it was after midnight. 3. The `travel_to` method freezed time, after midnight. 4. A Poll::OfficerAssignment factory was created, using the date it was before midnight. Using dynamic fixtures solves the problem: factory :poll_officer_assignment, class: 'Poll::OfficerAssignment' do (...) date { Date.current } end Now the sequence is: 1. The factories file is loaded, and since it finds a block, doesn't assign a static value to every Poll::OfficerAssignment to be created. 2. Time passes, so now it's after midnight. 3. The `travel_to` method freezes time, after midnight. 4. A Poll::OfficerAssignment factory was created, and in executes the block, using the current date, that is, after midnight. --- spec/factories.rb | 124 +++++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 62 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 50dd0dec8..dacde8385 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -24,7 +24,7 @@ FactoryBot.define do end trait :level_two do - residence_verified_at Time.current + residence_verified_at { Time.current } unconfirmed_phone "611111111" confirmed_phone "611111111" sms_confirmation_code "1234" @@ -36,28 +36,28 @@ FactoryBot.define do end trait :level_three do - verified_at Time.current + verified_at { Time.current } document_type "1" document_number end trait :hidden do - hidden_at Time.current + hidden_at { Time.current } end trait :with_confirmed_hide do - confirmed_hide_at Time.current + confirmed_hide_at { Time.current } end trait :verified do - residence_verified_at Time.current - verified_at Time.current + residence_verified_at { Time.current } + verified_at { Time.current } end trait :in_census do document_number "12345678Z" document_type "1" - verified_at Time.current + verified_at { Time.current } end end @@ -112,7 +112,7 @@ FactoryBot.define do factory :lock do user tries 0 - locked_until Time.current + locked_until { Time.current } end factory :verified_user do @@ -127,15 +127,15 @@ FactoryBot.define do association :author, factory: :user 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 :with_confirmed_hide do - confirmed_hide_at Time.current + confirmed_hide_at { Time.current } end trait :flagged do @@ -173,15 +173,15 @@ FactoryBot.define do association :author, factory: :user 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 :with_confirmed_hide do - confirmed_hide_at Time.current + confirmed_hide_at { Time.current } end trait :flagged do @@ -369,8 +369,8 @@ FactoryBot.define do kind :balloting summary Faker::Lorem.sentence(3) description Faker::Lorem.sentence(10) - starts_at Date.yesterday - ends_at Date.tomorrow + starts_at { Date.yesterday } + ends_at { Date.tomorrow } enabled true end @@ -414,7 +414,7 @@ FactoryBot.define do association :status, factory: :budget_investment_status sequence(:title) { |n| "Budget investment milestone #{n} title" } description 'Milestone description' - publication_date Date.current + publication_date { Date.current } end factory :vote do @@ -467,15 +467,15 @@ FactoryBot.define do sequence(:body) { |n| "Comment body #{n}" } 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 :with_confirmed_hide do - confirmed_hide_at Time.current + confirmed_hide_at { Time.current } end trait :flagged do @@ -601,7 +601,7 @@ FactoryBot.define do factory :poll_officer_assignment, class: 'Poll::OfficerAssignment' do association :officer, factory: :poll_officer association :booth_assignment, factory: :poll_booth_assignment - date Date.current + date { Date.current } trait :final do final true @@ -611,7 +611,7 @@ FactoryBot.define do factory :poll_shift, class: 'Poll::Shift' do association :booth, factory: :poll_booth association :officer, factory: :poll_officer - date Date.current + date { Date.current } trait :vote_collection_task do task 0 @@ -669,7 +669,7 @@ FactoryBot.define do year_of_birth "1980" trait :invalid do - year_of_birth Time.current.year + year_of_birth { Time.current.year } end end @@ -679,11 +679,11 @@ FactoryBot.define do sequence(:name) { |n| "org#{n}" } trait :verified do - verified_at Time.current + verified_at { Time.current } end trait :rejected do - rejected_at Time.current + rejected_at { Time.current } end end @@ -702,13 +702,13 @@ FactoryBot.define do factory :ahoy_event, class: Ahoy::Event do id { SecureRandom.uuid } - time DateTime.current + time { DateTime.current } sequence(:name) {|n| "Event #{n} type"} end factory :visit do id { SecureRandom.uuid } - started_at DateTime.current + started_at { DateTime.current } end factory :campaign do @@ -721,7 +721,7 @@ FactoryBot.define do association :notifiable, factory: :proposal trait :read do - read_at Time.current + read_at { Time.current } end end @@ -741,8 +741,8 @@ FactoryBot.define do style {["banner-style-one", "banner-style-two", "banner-style-three"].sample} image {["banner.banner-img-one", "banner.banner-img-two", "banner.banner-img-three"].sample} target_url {["/proposals", "/debates" ].sample} - post_started_at Time.current - 7.days - post_ended_at Time.current + 7.days + post_started_at { Time.current - 7.days } + post_ended_at { Time.current + 7.days } end factory :proposal_notification do @@ -790,14 +790,14 @@ FactoryBot.define do title "A collaborative legislation process" description "Description of the process" summary "Summary of the process" - start_date Date.current - 5.days - end_date Date.current + 5.days - debate_start_date Date.current - 5.days - debate_end_date Date.current + 2.days - draft_publication_date Date.current - 1.day - allegations_start_date Date.current - allegations_end_date Date.current + 3.days - result_publication_date Date.current + 5.days + start_date { Date.current - 5.days } + end_date { Date.current + 5.days } + debate_start_date { Date.current - 5.days } + debate_end_date { Date.current + 2.days } + draft_publication_date { Date.current - 1.day } + allegations_start_date { Date.current } + allegations_end_date { Date.current + 3.days } + result_publication_date { Date.current + 5.days } debate_phase_enabled true allegations_phase_enabled true draft_publication_enabled true @@ -805,36 +805,36 @@ FactoryBot.define do published true trait :next do - start_date Date.current + 2.days - end_date Date.current + 8.days - debate_start_date Date.current + 2.days - debate_end_date Date.current + 4.days - draft_publication_date Date.current + 5.days - allegations_start_date Date.current + 5.days - allegations_end_date Date.current + 7.days - result_publication_date Date.current + 8.days + start_date { Date.current + 2.days } + end_date { Date.current + 8.days } + debate_start_date { Date.current + 2.days } + debate_end_date { Date.current + 4.days } + draft_publication_date { Date.current + 5.days } + allegations_start_date { Date.current + 5.days } + allegations_end_date { Date.current + 7.days } + result_publication_date { Date.current + 8.days } end trait :past do - start_date Date.current - 12.days - end_date Date.current - 2.days - debate_start_date Date.current - 12.days - debate_end_date Date.current - 9.days - draft_publication_date Date.current - 8.days - allegations_start_date Date.current - 8.days - allegations_end_date Date.current - 4.days - result_publication_date Date.current - 2.days + start_date { Date.current - 12.days } + end_date { Date.current - 2.days } + debate_start_date { Date.current - 12.days } + debate_end_date { Date.current - 9.days } + draft_publication_date { Date.current - 8.days } + allegations_start_date { Date.current - 8.days } + allegations_end_date { Date.current - 4.days } + result_publication_date { Date.current - 2.days } end trait :in_debate_phase do - start_date Date.current - 5.days - end_date Date.current + 5.days - debate_start_date Date.current - 5.days - debate_end_date Date.current + 1.day - draft_publication_date Date.current + 1.day - allegations_start_date Date.current + 2.days - allegations_end_date Date.current + 3.days - result_publication_date Date.current + 5.days + start_date { Date.current - 5.days } + end_date { Date.current + 5.days } + debate_start_date { Date.current - 5.days } + debate_end_date { Date.current + 1.day } + draft_publication_date { Date.current + 1.day } + allegations_start_date { Date.current + 2.days } + allegations_end_date { Date.current + 3.days } + result_publication_date { Date.current + 5.days } end trait :published do From 735eab8e712330a1f920c3f0f24abe81e4f751e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Mart=C3=ADn?= Date: Mon, 9 Jul 2018 00:04:12 +0200 Subject: [PATCH 4/4] Avoid date changes during residence tests. As it happened with results (commit 4a559ed), these tests failed if `Date.current` changes between the moment records are created and the moment the rest of the test is executed. --- spec/features/officing/residence_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/features/officing/residence_spec.rb b/spec/features/officing/residence_spec.rb index 473a3a979..e5f060366 100644 --- a/spec/features/officing/residence_spec.rb +++ b/spec/features/officing/residence_spec.rb @@ -3,6 +3,14 @@ require 'rails_helper' feature 'Residence' 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