diff --git a/app/controllers/admin/parliaments_controller.rb b/app/controllers/admin/parliaments_controller.rb index e74a957d4..4967ad5f0 100644 --- a/app/controllers/admin/parliaments_controller.rb +++ b/app/controllers/admin/parliaments_controller.rb @@ -9,27 +9,20 @@ def show end def update - if @parliament.update(parliament_params) - if send_emails? - @parliament.send_emails! - redirect_to admin_parliament_url(tab: params[:tab]), notice: :creators_emailed - elsif schedule_closure? - @parliament.schedule_closure! - redirect_to admin_parliament_url(tab: params[:tab]), notice: :closure_scheduled - elsif archive_petitions? - @parliament.start_archiving! - redirect_to admin_parliament_url(tab: params[:tab]), notice: :petitions_archiving - elsif anonymize_petitions? - @parliament.start_anonymizing! - redirect_to admin_parliament_url(tab: params[:tab]), notice: :petitions_anonymizing - elsif archive_parliament? - @parliament.archive! - redirect_to admin_parliament_url(tab: params[:tab]), notice: :parliament_archived - else - redirect_to admin_parliament_url(tab: params[:tab]), notice: :parliament_updated + respond_to do |format| + format.html do + @parliament.assign_attributes(parliament_params) + + if @parliament.save(context: parliament_context) + if perform_parliament_action! + redirect_to admin_parliament_url(tab: params[:tab]), notice: parliament_action_notice + else + redirect_to admin_parliament_url(tab: params[:tab]), alert: parliament_action_alert + end + else + render :show, alert: :parliament_not_updated + end end - else - render :show, alert: :parliament_not_updated end end @@ -53,23 +46,75 @@ def parliament_params ) end - def send_emails? - params.key?(:send_emails) && @parliament.dissolution_at? + def perform_parliament_action! + case button_param + when "send_emails" + @parliament.send_emails! + when "schedule_closure" + @parliament.schedule_closure! + when "archive_petitions" + @parliament.start_archiving! + when "anonymize_petitions" + @parliament.start_anonymizing! + when "archive_parliament" + @parliament.archive! + else + true + end end - def schedule_closure? - params.key?(:schedule_closure) && @parliament.dissolution_announced? + def parliament_context + case button_param + when "send_emails" + :send_emails + when "schedule_closure" + :schedule_closure + when "archive_petitions" + :archive_petitions + when "archive_parliament" + :archive_parliament + when "anonymize_petitions" + :anonymize_petitions + else + nil + end end - def archive_petitions? - params.key?(:archive_petitions) && @parliament.can_archive_petitions? + def parliament_action_notice + case button_param + when "send_emails" then + :creators_emailed + when "schedule_closure" + :closure_scheduled + when "archive_petitions" + :petitions_archiving + when "archive_parliament" + :parliament_archived + when "anonymize_petitions" + :petitions_anonymizing + else + :parliament_updated + end end - def anonymize_petitions? - params.key?(:anonymize_petitions) && @parliament.can_anonymize_petitions? + def parliament_action_alert + case button_param + when "send_emails" then + :creators_not_emailed + when "schedule_closure" + :closure_not_scheduled + when "archive_petitions" + :petitions_not_archiving + when "archive_parliament" + :parliament_not_archived + when "anonymize_petitions" + :petitions_not_anonymizing + else + :parliament_not_updated + end end - def archive_parliament? - params.key?(:archive_parliament) && @parliament.can_archive? + def button_param + params.fetch(:button, "save") end end diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb index 308632a09..2150b6116 100644 --- a/app/helpers/admin_helper.rb +++ b/app/helpers/admin_helper.rb @@ -159,6 +159,28 @@ def short_rejection_reason(rejection) t(rejection.code, scope: :"rejection.reasons.short", default: rejection.code.titleize) end + def parliament_tab + if (errors = @parliament.errors.attribute_names).present? + if (errors & %i[government opening_at]).present? + "details" + elsif (errors & %i[dissolution_at dissolution_faq_url show_dissolution_notification dissolution_heading dissolution_message]).present? + "dissolution" + elsif (errors & %i[notification_cutoff_at dissolved_heading dissolved_message]).present? + "dissolved" + elsif (errors & %i[election_date registration_closed_at]).present? + "election" + elsif (errors & %i[government_response_heading government_response_description government_response_status]).present? + "response" + elsif (errors & %i[parliamentary_debate_heading parliamentary_debate_description parliamentary_debate_status]).present? + "debate" + else + "details" + end + else + params.fetch(:tab, "details") + end + end + private def admin_petition_facets diff --git a/app/models/parliament.rb b/app/models/parliament.rb index b3232a1a3..884b511ef 100644 --- a/app/models/parliament.rb +++ b/app/models/parliament.rb @@ -132,8 +132,23 @@ def reload Thread.current[:__parliament__] = nil end + def reset!(attributes = defaults) + destroy_all and reload + create!(attributes) + end + def current_or_create - current.first_or_create(government: "TBC", opening_at: 2.weeks.ago) + current.first_or_create(defaults) + end + + def update!(attributes) + instance.update!(attributes) + end + + def defaults + I18n.t(:defaults, scope: :parliament).transform_values do |value| + value.respond_to?(:call) ? value.call : value + end end end @@ -148,6 +163,43 @@ def current_or_create validates_numericality_of :petition_duration, greater_than_or_equal_to: 1, allow_blank: true validates_numericality_of :petition_duration, less_than_or_equal_to: 12, allow_blank: true + validate on: :send_emails do + errors.add(:dissolution_at, :blank) unless dissolution_at? + errors.add(:dissolution_faq_url, :blank) unless dissolution_faq_url? + errors.add(:show_dissolution_notification, :not_visible) unless show_dissolution_notification? + errors.add(:registration_closed_at, :blank) unless registration_closed_at? + errors.add(:election_date, :blank) unless election_date? + end + + validate on: :schedule_closure do + errors.add(:notification_cutoff_at, :blank) unless notification_cutoff_at? + end + + validate on: :archive_petitions do + errors.add(:dissolution_at, :blank) unless dissolution_at? + + if dissolution_at? + errors.add(:dissolution_at, :too_soon) unless dissolution_at < 2.days.ago + end + end + + validate on: :archive_parliament do + errors.add(:dissolution_at, :blank) unless dissolution_at? + + if dissolution_at? + errors.add(:dissolution_at, :too_soon) unless dissolution_at < 2.days.ago + errors.add(:dissolution_at, :still_archiving) if dissolved? && archiving? + end + end + + validate on: :anonymize_petitions do + errors.add(:opening_at, :blank) unless opening_at? + + if opening_at? + errors.add(:opening_at, :too_soon) unless opening_at < 6.months.ago + end + end + after_save { Site.touch } def name @@ -224,7 +276,7 @@ def start_archiving!(now = Time.current) end def start_anonymizing!(now = Time.current) - unless dissolving? || dissolved? || archiving? + if can_anonymize_petitions? Archived::AnonymizePetitionsJob.set(wait_until: midnight).perform_later(midnight.iso8601) end end diff --git a/app/models/site.rb b/app/models/site.rb index 760e2fb57..5fb76c979 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -126,6 +126,15 @@ def reload Thread.current[:__site__] = nil end + def reset!(attributes = defaults) + destroy_all and reload + create!(attributes) + end + + def update!(attributes) + instance.update!(attributes) + end + def touch(*names) if instance.persisted? instance.touch(*names) diff --git a/app/views/admin/parliaments/_form.html.erb b/app/views/admin/parliaments/_form.html.erb index aef163c6c..b3e032ed4 100644 --- a/app/views/admin/parliaments/_form.html.erb +++ b/app/views/admin/parliaments/_form.html.erb @@ -1,21 +1,21 @@ <%= form_for @parliament, url: admin_parliament_url do |form| %> - <%= render tab, form: form %> - <%= form.submit 'Save', class: 'button' %> + <%= render parliament_tab, form: form %> + <%= form.button 'Save', name: 'button', value: 'save', class: 'button' %> <% if @parliament.dissolved? %> <% if @parliament.can_archive_petitions? %> - <%= form.submit 'Archive petitions', name: 'archive_petitions', class: 'button-secondary', data: { confirm: 'Copy current petitions to archive?' } %> + <%= form.button 'Archive petitions', name: 'button', value: 'archive_petitions', class: 'button-secondary', data: { confirm: 'Copy current petitions to archive?' } %> <% elsif @parliament.can_archive? %> - <%= form.submit 'Archive parliament', name: 'archive_parliament', class: 'button-secondary', data: { confirm: 'Archive this parliament and create a new one?' } %> + <%= form.button 'Archive parliament', name: 'button', value: 'archive_parliament', class: 'button-secondary', data: { confirm: 'Archive this parliament and create a new one?' } %> <% end %> <% elsif @parliament.dissolving? %> <% if !@parliament.dissolution_emails_sent? %> - <%= form.submit 'Send dissolution emails', name: 'send_emails', class: 'button-secondary', data: { confirm: 'Email everyone about dissolution?' } %> + <%= form.button 'Send dissolution emails', name: 'button', value: 'send_emails', class: 'button-secondary', data: { confirm: 'Email everyone about dissolution?' } %> <% elsif !@parliament.closure_scheduled? %> - <%= form.submit 'Schedule closure', name: 'schedule_closure', class: 'button-secondary', data: { confirm: 'Schedule early closure of petitions?' } %> + <%= form.button 'Schedule closure', name: 'button', value: 'schedule_closure', class: 'button-secondary', data: { confirm: 'Schedule early closure of petitions?' } %> <% end %> <% end %> <% if @parliament.can_anonymize_petitions? %> - <%= form.submit 'Anonymize petitions', name: 'anonymize_petitions', class: 'button-secondary', data: { confirm: 'Anonymize all archived petitions?' } %> + <%= form.button 'Anonymize petitions', name: 'button', value: 'anonymize_petitions', class: 'button-secondary', data: { confirm: 'Anonymize all archived petitions?' } %> <% end %> <%= link_to 'Cancel', admin_root_path, class: 'button-secondary' %> <% end %> diff --git a/app/views/admin/parliaments/show.html.erb b/app/views/admin/parliaments/show.html.erb index 90af9bf99..ee54fe156 100644 --- a/app/views/admin/parliaments/show.html.erb +++ b/app/views/admin/parliaments/show.html.erb @@ -6,18 +6,6 @@
- <% if params[:tab] == "dissolution" %> - <%= render "form", tab: "dissolution" %> - <% elsif params[:tab] == "election" %> - <%= render "form", tab: "election" %> - <% elsif params[:tab] == "dissolved" %> - <%= render "form", tab: "dissolved" %> - <% elsif params[:tab] == "response" %> - <%= render "form", tab: "response" %> - <% elsif params[:tab] == "debate" %> - <%= render "form", tab: "debate" %> - <% else %> - <%= render "form", tab: "details" %> - <% end %> + <%= render "form" %>
diff --git a/app/views/admin/shared/_parliament_tabs.html.erb b/app/views/admin/shared/_parliament_tabs.html.erb index 51282e810..4ad56dee2 100644 --- a/app/views/admin/shared/_parliament_tabs.html.erb +++ b/app/views/admin/shared/_parliament_tabs.html.erb @@ -1,33 +1,33 @@

- <% if params[:tab] == "dissolution" %> + <% if parliament_tab == "dissolution" %> <%= link_to "Details", admin_parliament_path %> | Dissolution | <%= link_to "Election", admin_parliament_path(tab: 'election') %> | <%= link_to "Dissolved", admin_parliament_path(tab: 'dissolved') %> | <%= link_to "Government Response", admin_parliament_path(tab: 'response') %> | <%= link_to "Parliamentary Debate", admin_parliament_path(tab: 'debate') %> - <% elsif params[:tab] == "election" %> + <% elsif parliament_tab == "election" %> <%= link_to "Details", admin_parliament_path %> | <%= link_to "Dissolution", admin_parliament_path(tab: 'dissolution') %> | Election | <%= link_to "Dissolved", admin_parliament_path(tab: 'dissolved') %> | <%= link_to "Government Response", admin_parliament_path(tab: 'response') %> | <%= link_to "Parliamentary Debate", admin_parliament_path(tab: 'debate') %> - <% elsif params[:tab] == "dissolved" %> + <% elsif parliament_tab == "dissolved" %> <%= link_to "Details", admin_parliament_path %> | <%= link_to "Dissolution", admin_parliament_path(tab: 'dissolution') %> | <%= link_to "Election", admin_parliament_path(tab: 'election') %> | Dissolved | <%= link_to "Government Response", admin_parliament_path(tab: 'response') %> | <%= link_to "Parliamentary Debate", admin_parliament_path(tab: 'debate') %> - <% elsif params[:tab] == "response" %> + <% elsif parliament_tab == "response" %> <%= link_to "Details", admin_parliament_path %> | <%= link_to "Dissolution", admin_parliament_path(tab: 'dissolution') %> | <%= link_to "Election", admin_parliament_path(tab: 'election') %> | <%= link_to "Dissolved", admin_parliament_path(tab: 'dissolved') %> | Government Response | <%= link_to "Parliamentary Debate", admin_parliament_path(tab: 'debate') %> - <% elsif params[:tab] == "debate" %> + <% elsif parliament_tab == "debate" %> <%= link_to "Details", admin_parliament_path %> | <%= link_to "Dissolution", admin_parliament_path(tab: 'dissolution') %> | <%= link_to "Election", admin_parliament_path(tab: 'election') %> | diff --git a/config/locales/activerecord.en-GB.yml b/config/locales/activerecord.en-GB.yml index 6faeb337d..183af4b2c 100644 --- a/config/locales/activerecord.en-GB.yml +++ b/config/locales/activerecord.en-GB.yml @@ -91,6 +91,25 @@ en-GB: aliased_domain: not_found: "The aliased domain was not found - please check the spelling and try again" + parliament: + attributes: + dissolution_at: + blank: Please enter the date and time when parliament dissolves + too_soon: Please wait until two days after dissolution to begin archiving + dissolution_faq_url: + blank: Please enter a URL for the Dissolution FAQ + election_date: + blank: Please enter the election date + notification_cutoff_at: + blank: Please enter a notification cut-off + opening_at: + blank: Please enter the date and time when parliament will reopen + too_soon: Please wait until six months after reopening before anonymizing petitions + registration_closed_at: + blank: Please enter the date and time at which voter registration closes + show_dissolution_notification: + not_visible: Please make the dissolution notification visible + petition: attributes: moderation: diff --git a/config/locales/admin.en-GB.yml b/config/locales/admin.en-GB.yml index 354c1c24a..780a5edc4 100644 --- a/config/locales/admin.en-GB.yml +++ b/config/locales/admin.en-GB.yml @@ -40,7 +40,9 @@ en-GB: admin_required: "You must be logged in as an administrator to view this page" change_password: "Please change your password before continuing" closure_scheduled: "Petitions have been scheduled to close early" + closure_not_scheduled: "Petitions could not be scheduled to close early - please contact support" creators_emailed: "Everyone will be notified of the early closing of their petitions" + creators_not_emailed: "Could not notify everyone of the early closing of their petitions - please contact support" debate_date_updated: "Updated the scheduled debate date successfully" debate_outcome_updated: "Updated debate outcome successfully" department_created: "Department created successfully" @@ -82,10 +84,13 @@ en-GB: missing_tasks: "Please select one or more tasks to execute" password_updated: "Password was successfully updated" petitions_archiving: "Archiving of petitions was successfully started" + petitions_not_archiving: "Archiving of petitions could not be started - please contact support" petitions_anonymizing: "Anonymizing of petitions was successfully started" + petitions_not_anonymizing: "Anonymizing of petitions could not be started - please contact support" parliament_archived: "Parliament archived successfully" - parliament_not_updated: "Parliament could not be updated - please check the form for errors" + parliament_not_archived: "Parliament could not be archived - please contact support" parliament_updated: "Parliament updated successfully" + parliament_not_updated: "Parliament could not be updated - please check the form for errors" petition_email_created: "Created related activity successfully" petition_email_updated: "Updated related activity successfully" petition_email_deleted: "Deleted related activity successfully" diff --git a/config/locales/parliament.en-GB.rb b/config/locales/parliament.en-GB.rb new file mode 100644 index 000000000..105489656 --- /dev/null +++ b/config/locales/parliament.en-GB.rb @@ -0,0 +1,16 @@ +{ + "en-GB": { + "parliament": { + "defaults": { + "government": "TBC", + "opening_at": -> { 2.weeks.from_now.change(hour: 10) }, + "dissolved_heading": "We're waiting for a new Petitions Committee", + "dissolved_message": <<~EOF + Petitions had to stop because of the recent general election. + As soon as a new Petitions Committee is set up by the + House of Commons, petitions will start again. + EOF + } + } + } +} diff --git a/features/admin/anonymize_petitions.feature b/features/admin/anonymize_petitions.feature index 5457e8f16..a5e5e3afd 100644 --- a/features/admin/anonymize_petitions.feature +++ b/features/admin/anonymize_petitions.feature @@ -9,6 +9,7 @@ Feature: An admin anonymizes petitions @javascript Scenario: Admin anonymizes petitions 6 months after parliament closes Given 2 archived petitions exist + And 6 months has passed since parliament opened When I go to the admin parliament page And I press "Anonymize petitions" And I accept the alert diff --git a/features/step_definitions/common_steps.rb b/features/step_definitions/common_steps.rb index f865924c5..7d874485d 100644 --- a/features/step_definitions/common_steps.rb +++ b/features/step_definitions/common_steps.rb @@ -7,11 +7,11 @@ end Given(/^the site is disabled$/) do - Site.instance.update! enabled: false + Site.update! enabled: false end Given(/^the site is protected$/) do - Site.instance.update! protected: true, username: "username", password: "password" + Site.update! protected: true, username: "username", password: "password" end Given(/^signature counting is handled by an external process$/) do @@ -19,7 +19,7 @@ end Given(/^Parliament is dissolving$/) do - Parliament.instance.update! dissolution_at: 2.weeks.from_now, + Parliament.update! dissolution_at: 2.weeks.from_now, dissolution_heading: "Parliament is dissolving", dissolution_message: "This means all petitions will close in 2 weeks", dissolution_faq_url: "https://parliament.example.com/parliament-is-closing", @@ -27,7 +27,7 @@ end Given(/^Parliament is dissolved$/) do - Parliament.instance.update! dissolution_at: 1.day.ago, + Parliament.update! dissolution_at: 1.day.ago, dissolution_heading: "Parliament is dissolving", dissolution_message: "This means all petitions will close in 2 weeks", dissolved_heading: "Parliament has been dissolved", @@ -37,7 +37,11 @@ end Given(/^Parliament is pending$/) do - Parliament.instance.update!(opening_at: 1.month.from_now) + Parliament.update!(opening_at: 1.month.from_now) +end + +Given('{int} months has passed since parliament opened') do |number| + Parliament.update!(opening_at: number.months.ago) end Given(/^the request is not local$/) do diff --git a/features/step_definitions/petition_steps.rb b/features/step_definitions/petition_steps.rb index 87ff0d595..8bed0fa97 100644 --- a/features/step_definitions/petition_steps.rb +++ b/features/step_definitions/petition_steps.rb @@ -111,7 +111,7 @@ open_at = 3.months.ago closed_at = 1.month.ago - Parliament.instance.update! dissolution_at: closed_at, + Parliament.update! dissolution_at: closed_at, dissolution_heading: "Parliament is dissolving", dissolution_message: "This means all petitions will close in 2 weeks", dissolved_heading: "Parliament has been dissolved", @@ -132,7 +132,7 @@ open_at = 3.months.ago closed_at = 1.month.ago - Parliament.instance.update! dissolution_at: closed_at, + Parliament.update! dissolution_at: closed_at, dissolution_heading: "Parliament is dissolving", dissolution_message: "This means all petitions will close in 2 weeks", dissolved_heading: "Parliament has been dissolved", @@ -375,7 +375,7 @@ end Given(/^the threshold for a parliamentary debate is "(.*?)"$/) do |amount| - Site.instance.update!(threshold_for_debate: amount) + Site.update!(threshold_for_debate: amount) end Given(/^there are (\d+) petitions awaiting a government response$/) do |response_count| diff --git a/features/support/hooks.rb b/features/support/hooks.rb index 4cb72bfdf..00e8aa14c 100644 --- a/features/support/hooks.rb +++ b/features/support/hooks.rb @@ -45,9 +45,9 @@ Rails.cache.clear end -After do - Site.reload - Parliament.reload +Before do + Site.reset! + Parliament.reset!(government: "TBC", opening_at: 2.weeks.ago) end After do diff --git a/features/suzie_searches_by_free_text.feature b/features/suzie_searches_by_free_text.feature index c880192c3..ca1c648d1 100644 --- a/features/suzie_searches_by_free_text.feature +++ b/features/suzie_searches_by_free_text.feature @@ -5,6 +5,7 @@ Feature: Suzy Singer searches by free text Background: Given the date is the "21 April 2011 12:00" + And 6 months has passed since parliament opened And a pending petition exists with action: "Wombles are great" And a validated petition exists with action: "The Wombles of Wimbledon" And an open petition exists with action: "Uncle Bulgaria", additional_details: "The Wombles are here", closed_at: "1 minute from now", signature_count: 23 diff --git a/spec/controllers/admin/parliaments_controller_spec.rb b/spec/controllers/admin/parliaments_controller_spec.rb index 0a15417db..c3724b956 100644 --- a/spec/controllers/admin/parliaments_controller_spec.rb +++ b/spec/controllers/admin/parliaments_controller_spec.rb @@ -86,7 +86,7 @@ end context "when clicking the 'Save' button" do - before { patch :update, params: { parliament: params, commit: "Save" } } + before { patch :update, params: { parliament: params, button: "save" } } context "and the params are invalid" do let(:params) { invalid_params } @@ -106,7 +106,7 @@ end context "when clicking the 'Send dissolution emails' button" do - before { patch :update, params: { parliament: params, send_emails: "Send dissolution emails" } } + before { patch :update, params: { parliament: params, button: "send_emails" } } context "and the params are invalid" do let(:params) { invalid_params } @@ -120,12 +120,17 @@ government: "Conservative", opening_at: 2.years.ago.iso8601, dissolution_at: 2.weeks.from_now.iso8601, + registration_closed_at: 3.weeks.from_now.iso8601, + election_date: 3.weeks.from_now.to_date.iso8601, + show_dissolution_notification: true, dissolution_heading: "Parliament is dissolving", dissolution_message: "This means all petitions will close in 2 weeks", dissolution_faq_url: "https://parliament.example.com/parliament-is-closing" } end + it_behaves_like "a valid request" + it "sets the flash notice message" do expect(flash[:notice]).to eq("Everyone will be notified of the early closing of their petitions") end @@ -147,9 +152,7 @@ } end - it "sets the flash notice message" do - expect(flash[:notice]).to eq("Parliament updated successfully") - end + it_behaves_like "an invalid request" it "doesn't enqueue a job to notify creators" do expect(enqueued_jobs).to eq([]) @@ -158,7 +161,7 @@ end context "when clicking the 'Schedule closure' button" do - before { patch :update, params: { parliament: params, schedule_closure: "Schedule closure" } } + before { patch :update, params: { parliament: params, button: "schedule_closure" } } context "and the params are invalid" do let(:params) { invalid_params } @@ -177,7 +180,8 @@ dissolution_heading: "Parliament is dissolving", dissolution_message: "This means all petitions will close in 2 weeks", dissolution_faq_url: "https://parliament.example.com/parliament-is-closing", - show_dissolution_notification: "true" + show_dissolution_notification: "true", + notification_cutoff_at: 3.months.ago.iso8601 } end @@ -199,11 +203,7 @@ context "and the params are valid but parliament isn't dissolving" do let(:params) { valid_params } - it_behaves_like "a valid request" - - it "sets the flash notice message" do - expect(flash[:notice]).to eq("Parliament updated successfully") - end + it_behaves_like "an invalid request" it "doesn't enqueue jobs to close and stop petitions" do expect(enqueued_jobs).to eq([]) @@ -212,7 +212,7 @@ end context "when clicking the 'Archive petitions' button" do - before { patch :update, params: { parliament: params, archive_petitions: "Archive petitions" } } + before { patch :update, params: { parliament: params, button: "archive_petitions" } } context "and the params are invalid" do let(:params) { invalid_params } @@ -263,11 +263,7 @@ } end - it_behaves_like "a valid request" - - it "sets the flash notice message" do - expect(flash[:notice]).to eq("Parliament updated successfully") - end + it_behaves_like "an invalid request" it "doesn't enqueue a job to archive petitions" do expect(enqueued_jobs).to eq([]) @@ -281,11 +277,7 @@ context "and the params are valid but parliament isn't dissolving" do let(:params) { valid_params } - it_behaves_like "a valid request" - - it "sets the flash notice message" do - expect(flash[:notice]).to eq("Parliament updated successfully") - end + it_behaves_like "an invalid request" it "doesn't enqueue a job to archive petitions" do expect(enqueued_jobs).to eq([]) @@ -300,9 +292,9 @@ context "when clicking the 'Archive parliament' button" do before do FactoryBot.create(:closed_petition, archived_at: 1.hour.ago) - FactoryBot.create(:parliament, archiving_started_at: 1.day.ago) + Parliament.update!(archiving_started_at: 1.day.ago) - patch :update, params: { parliament: params, archive_parliament: "Archive parliament" } + patch :update, params: { parliament: params, button: "archive_parliament" } end context "and the params are invalid" do @@ -354,11 +346,7 @@ } end - it_behaves_like "a valid request" - - it "sets the flash notice message" do - expect(flash[:notice]).to eq("Parliament updated successfully") - end + it_behaves_like "an invalid request" it "doesn't enqueue a job to delete petitions" do expect(enqueued_jobs).to eq([]) @@ -372,11 +360,7 @@ context "and the params are valid but parliament isn't dissolving" do let(:params) { valid_params } - it_behaves_like "a valid request" - - it "sets the flash notice message" do - expect(flash[:notice]).to eq("Parliament updated successfully") - end + it_behaves_like "an invalid request" it "doesn't enqueue a job to delete petitions" do expect(enqueued_jobs).to eq([]) @@ -394,7 +378,7 @@ before do FactoryBot.create(:archived_petition, anonymized_at: anonymized_at) - patch :update, params: { parliament: params, anonymize_petitions: "Anonymize petitions" } + patch :update, params: { parliament: params, button: "anonymize_petitions" } end context "and the params are invalid" do @@ -417,6 +401,18 @@ end end + context "and the params are valid, but the site has been reopened for less than six months" do + let :params do + { government: "Conservative", opening_at: 5.months.ago.iso8601 } + end + + it_behaves_like "an invalid request" + + it "doesn't enqueue a job to anonymize petitions" do + expect(enqueued_jobs).to eq([]) + end + end + context "and the params are valid, but there are no unanonymized petitions" do let(:params) { valid_params } let(:anonymized_at) { 2.weeks.ago } @@ -424,7 +420,7 @@ it_behaves_like "a valid request" it "sets the flash notice message" do - expect(flash[:notice]).to eq("Parliament updated successfully") + expect(flash[:alert]).to eq("Anonymizing of petitions could not be started - please contact support") end it "doesn't enqueue a job to anonymize petitions" do @@ -449,7 +445,7 @@ it_behaves_like "a valid request" it "sets the flash notice message" do - expect(flash[:notice]).to eq("Parliament updated successfully") + expect(flash[:alert]).to eq("Anonymizing of petitions could not be started - please contact support") end it "doesn't enqueue a job to anonymize petitions" do @@ -474,7 +470,7 @@ it_behaves_like "a valid request" it "sets the flash notice message" do - expect(flash[:notice]).to eq("Parliament updated successfully") + expect(flash[:alert]).to eq("Anonymizing of petitions could not be started - please contact support") end it "doesn't enqueue a job to anonymize petitions" do diff --git a/spec/support/parliament.rb b/spec/support/parliament.rb index 342a1d029..b7758dbaa 100644 --- a/spec/support/parliament.rb +++ b/spec/support/parliament.rb @@ -1,13 +1,11 @@ RSpec.configure do |config| config.before(:suite) do - Parliament.destroy_all - Parliament.reload + Parliament.reset!(government: "TBC", opening_at: 2.weeks.ago) end config.before(:each) do |example| - Parliament.destroy_all - Parliament.reload + Parliament.reset!(government: "TBC", opening_at: 2.weeks.ago) end end diff --git a/spec/support/site.rb b/spec/support/site.rb index 6f3f3b022..263302d2a 100644 --- a/spec/support/site.rb +++ b/spec/support/site.rb @@ -1,13 +1,11 @@ RSpec.configure do |config| config.before(:suite) do - Site.destroy_all - Site.reload + Site.reset! end config.before(:each) do |example| - Site.destroy_all - Site.reload + Site.reset! end end