Updated

spec/controllers/admin / government_response_controller_spec.rb

F
625 lines of codes
2 methods
768.0 complexity/method
24 churn
1536.04 complexity
894 duplications
require 'rails_helper' RSpec.describe Admin::GovernmentResponseController, type: :controller, admin: true do let!(:petition) { FactoryBot.create(:open_petition) } let(:government_response) { petition.government_response } describe 'not logged in' do
  1. Similar code found in 13 nodes Locations: 0 1 2 3 4 5 6 7 8 9 10 11 12
describe 'GET /show' do it 'redirects to the login page' do get :show, petition_id: petition.id expect(response).to redirect_to('https://moderate.petition.parliament.uk/admin/login') end end describe 'PATCH /update' do it 'redirects to the login page' do patch :update, petition_id: petition.id expect(response).to redirect_to('https://moderate.petition.parliament.uk/admin/login') end end end context 'logged in as moderator user but need to reset password' do
  1. Similar code found in 13 nodes Locations: 0 1 2 3 4 5 6 7 8 9 10 11 12
let(:user) { FactoryBot.create(:moderator_user, force_password_reset: true) } before { login_as(user) } describe 'GET /show' do it 'redirects to edit profile page' do get :show, petition_id: petition.id expect(response).to redirect_to("https://moderate.petition.parliament.uk/admin/profile/#{user.id}/edit") end end describe 'PATCH /update' do it 'redirects to edit profile page' do patch :update, petition_id: petition.id expect(response).to redirect_to("https://moderate.petition.parliament.uk/admin/profile/#{user.id}/edit") end end end describe "logged in as moderator user" do let(:user) { FactoryBot.create(:moderator_user) } before { login_as(user) } describe 'GET /show' do shared_examples_for 'viewing government response for a petition' do it 'fetches the requested petition' do get :show, petition_id: petition.id expect(assigns(:petition)).to eq petition end it 'responds successfully and renders the petitions/show template' do get :show, petition_id: petition.id expect(response).to be_success expect(response).to render_template('petitions/show') end end shared_examples_for 'viewing government response for a petition in the wrong state' do
  1. Similar code found in 6 nodes Locations: 0 1 2 3 4 5
it 'throws a 404' do expect { get :show, petition_id: petition.id }.to raise_error(ActiveRecord::RecordNotFound) end end describe 'for an open petition' do it_behaves_like 'viewing government response for a petition' end describe 'for a pending petition' do before { petition.update_column(:state, Petition::PENDING_STATE) } it_behaves_like 'viewing government response for a petition in the wrong state' end describe 'for a validated petition' do before { petition.update_column(:state, Petition::VALIDATED_STATE) } it_behaves_like 'viewing government response for a petition in the wrong state' end describe 'for a sponsored petition' do before { petition.update_column(:state, Petition::SPONSORED_STATE) } it_behaves_like 'viewing government response for a petition in the wrong state' end describe 'for a rejected petition' do before { petition.update_columns(state: Petition::REJECTED_STATE) } it_behaves_like 'viewing government response for a petition' end describe 'for a hidden petition' do before { petition.update_column(:state, Petition::HIDDEN_STATE) } it_behaves_like 'viewing government response for a petition' end end describe 'PATCH /update' do let(:government_response_attributes) do { responded_on: Date.civil(2018, 6, 23), summary: 'The government agrees', details: 'Your petition is brilliant and we will do our utmost to make it law.' } end context 'when clicking the Email button' do def do_patch(overrides = {})
  1. Similar code found in 4 nodes Locations: 0 1 2 3
login_as(user) params = { petition_id: petition.id, government_response: government_response_attributes, save_and_email: "Email" } patch :update, params.merge(overrides) end describe 'using valid params to add a government response' do it 'redirects to the show page' do do_patch expect(response).to redirect_to "https://moderate.petition.parliament.uk/admin/petitions/#{petition.id}" end it 'tells the moderator that their email will be sent overnight' do do_patch expect(flash[:notice]).to eq 'Email will be sent overnight' end it 'stores the supplied government response in the db' do
  1. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Email button)::describe(using valid params to add a government response)::it#stores the supplied government response in the db has a flog score of 52
do_patch petition.reload expect(government_response.responded_on).to eq government_response_attributes[:responded_on] expect(government_response.summary).to eq government_response_attributes[:summary] expect(government_response.details).to eq government_response_attributes[:details] end it 'stamps the current time on the government_response_at timestamp in the db' do
  1. Identical code found in 4 nodes Locations: 0 1 2 3
  2. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Email button)::describe(using valid params to add a government response)::it#stamps the current time on the government_response_at timestamp in the db has a flog score of 31
time = 6.days.from_now travel_to time do do_patch petition.reload expect(petition.government_response_at).to be_within(1.second).of time end end it "sets the 'government_response' email requested receipt timestamp" do
  1. Identical code found in 2 nodes Locations: 0 1
  2. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Email button)::describe(using valid params to add a government response)::it#sets the 'government_response' email requested receipt timestamp has a flog score of 31
time = 5.days.from_now travel_to time do do_patch petition.reload expect(petition.get_email_requested_at_for('government_response')).to be_within(1.second).of time end end describe "emails out debate outcomes response" do
  1. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Email button)::describe(using valid params to add a government response)::describe#emails out debate outcomes response has a flog score of 36
before do 3.times do |i| attributes = { name: "Laura #{i}", email: "laura_#{i}@example.com", notify_by_email: true, petition: petition } s = FactoryBot.create(:pending_signature, attributes) s.validate! end 2.times do |i| attributes = { name: "Sarah #{i}", email: "sarah_#{i}@example.com", notify_by_email: false, petition: petition } s = FactoryBot.create(:pending_signature, attributes) s.validate! end 2.times do |i| attributes = { name: "Brian #{i}", email: "brian_#{i}@example.com", notify_by_email: true, petition: petition } FactoryBot.create(:pending_signature, attributes) end petition.reload end it "queues a job to process the emails" do assert_enqueued_jobs 1 do do_patch end end it "stamps the 'government_response' email sent timestamp on each signature when the job runs" do
  1. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Email button)::describe(using valid params to add a government response)::describe(emails out debate outcomes response)::it#stamps the 'government_response' email sent timestamp on each signature when the job runs has a flog score of 42
perform_enqueued_jobs do do_patch petition.reload petition_timestamp = petition.get_email_requested_at_for('government_response') expect(petition_timestamp).not_to be_nil petition.signatures.validated.subscribed.each do |signature| expect(signature.get_email_sent_at_for('government_response')).to eq(petition_timestamp) end end end it "should email out to the validated signees who have opted in when the delayed job runs" do
  1. Identical code found in 2 nodes Locations: 0 1
  2. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Email button)::describe(using valid params to add a government response)::describe(emails out debate outcomes response)::it#should email out to the validated signees who have opted in when the delayed job runs has a flog score of 130
ActionMailer::Base.deliveries.clear perform_enqueued_jobs do do_patch expect(ActionMailer::Base.deliveries.length).to eq 4 expect(ActionMailer::Base.deliveries.map(&:to)).to eq([ [petition.creator.email], ['laura_0@example.com'], ['laura_1@example.com'], ['laura_2@example.com'] ]) expect(ActionMailer::Base.deliveries[0].subject).to match(/Government responded to “#{petition.action}”/) expect(ActionMailer::Base.deliveries[1].subject).to match(/Government responded to “#{petition.action}”/) expect(ActionMailer::Base.deliveries[2].subject).to match(/Government responded to “#{petition.action}”/) expect(ActionMailer::Base.deliveries[3].subject).to match(/Government responded to “#{petition.action}”/) end end end end describe 'using no params to add a government response' do
  1. Similar code found in 4 nodes Locations: 0 1 2 3
before do government_response_attributes[:responded_on] = nil government_response_attributes[:summary] = nil government_response_attributes[:details] = nil end it 'does not tell the moderator that their email will be sent overnight' do do_patch expect(flash[:notice]).to be_blank end it "does not set the 'government_response' email requested receipt timestamp" do
  1. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Email button)::describe(using no params to add a government response)::it#does not set the 'government_response' email requested receipt timestamp has a flog score of 26
time = 5.days.from_now travel_to time do do_patch petition.reload expect(petition.get_email_requested_at_for('government_response')).to be_nil end end it "does not queue a job to process the emails" do assert_enqueued_jobs 0 do do_patch end end it 're-renders the admin/petitions/show template' do do_patch expect(response).to be_success expect(response).to render_template('admin/petitions/show') end end describe 'using invalid params to add a government response' do
  1. Identical code found in 4 nodes Locations: 0 1 2 3
before { government_response_attributes[:summary] = 'a' * 501 } it 're-renders the petitions/show template' do do_patch expect(response).to be_success expect(response).to render_template('petitions/show') end it 'leaves the in-memory instance with errors' do do_patch expect(assigns(:government_response).errors).not_to be_empty end it 'does not store the supplied government response in the db' do do_patch petition.reload expect(government_response).to be_nil end it 'does not stamp the government_response_at timestamp in the db' do do_patch petition.reload expect(petition.government_response_at).to be_nil end it "does not stamp the 'government_response' email requested receipt timestamp" do do_patch petition.reload expect(petition.get_email_requested_at_for('government_response')).to be_nil end it "doest not queue up a job to send the 'government_response' emails" do do_patch assert_enqueued_jobs 0 end end shared_examples_for 'adding a government response to a petition' do
  1. Identical code found in 4 nodes Locations: 0 1 2 3
it 'fetches the requested petition' do do_patch expect(assigns(:petition)).to eq petition end it 'stores the supplied response on the petition in the db' do
  1. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Email button)::shared_examples_for(adding a government response to a petition)::it#stores the supplied response on the petition in the db has a flog score of 52
do_patch petition.reload expect(government_response.responded_on).to eq government_response_attributes[:responded_on] expect(government_response.summary).to eq government_response_attributes[:summary] expect(government_response.details).to eq government_response_attributes[:details] end end shared_examples_for 'adding a government response to a petition in the wrong state' do
  1. Identical code found in 4 nodes Locations: 0 1 2 3
it 'throws a 404' do expect { do_patch }.to raise_error ActiveRecord::RecordNotFound end it 'does not store the supplied response on the petition in the db' do suppress(ActiveRecord::RecordNotFound) { do_patch } petition.reload expect(government_response).to be_nil end end describe 'for an open petition' do it_behaves_like 'adding a government response to a petition' end describe 'for a pending petition' do before { petition.update_column(:state, Petition::PENDING_STATE) } it_behaves_like 'adding a government response to a petition in the wrong state' end describe 'for a validated petition' do before { petition.update_column(:state, Petition::VALIDATED_STATE) } it_behaves_like 'adding a government response to a petition in the wrong state' end describe 'for a sponsored petition' do before { petition.update_column(:state, Petition::SPONSORED_STATE) } it_behaves_like 'adding a government response to a petition in the wrong state' end describe 'for a rejected petition' do before { petition.update_columns(state: Petition::REJECTED_STATE) } it_behaves_like 'adding a government response to a petition' end describe 'for a hidden petition' do before { petition.update_column(:state, Petition::HIDDEN_STATE) } it_behaves_like 'adding a government response to a petition' end end context 'when clicking the Save button' do def do_patch(overrides = {})
  1. Similar code found in 4 nodes Locations: 0 1 2 3
login_as(user) params = { petition_id: petition.id, government_response: government_response_attributes, save: "Save" } patch :update, params.merge(overrides) end describe 'using valid params to add a government response' do it 'redirects to the show page' do do_patch expect(response).to redirect_to "https://moderate.petition.parliament.uk/admin/petitions/#{petition.id}" end it 'tells the moderator that their changes were saved' do do_patch expect(flash[:notice]).to eq 'Updated government response successfully' end it 'stores the supplied government response in the db' do
  1. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Save button)::describe(using valid params to add a government response)::it#stores the supplied government response in the db has a flog score of 52
do_patch petition.reload expect(government_response.responded_on).to eq government_response_attributes[:responded_on] expect(government_response.summary).to eq government_response_attributes[:summary] expect(government_response.details).to eq government_response_attributes[:details] end it 'stamps the current time on the government_response_at timestamp in the db' do
  1. Identical code found in 4 nodes Locations: 0 1 2 3
  2. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Save button)::describe(using valid params to add a government response)::it#stamps the current time on the government_response_at timestamp in the db has a flog score of 31
time = 6.days.from_now travel_to time do do_patch petition.reload expect(petition.government_response_at).to be_within(1.second).of time end end it "does not set the 'government_response' email requested receipt timestamp" do
  1. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Save button)::describe(using valid params to add a government response)::it#does not set the 'government_response' email requested receipt timestamp has a flog score of 26
time = 5.days.from_now travel_to time do do_patch petition.reload expect(petition.get_email_requested_at_for('government_response')).to be_nil end end describe "does not email out debate outcomes response" do
  1. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Save button)::describe(using valid params to add a government response)::describe#does not email out debate outcomes response has a flog score of 36
before do 3.times do |i| attributes = { name: "Laura #{i}", email: "laura_#{i}@example.com", notify_by_email: true, petition: petition } s = FactoryBot.create(:pending_signature, attributes) s.validate! end 2.times do |i| attributes = { name: "Sarah #{i}", email: "sarah_#{i}@example.com", notify_by_email: false, petition: petition } s = FactoryBot.create(:pending_signature, attributes) s.validate! end 2.times do |i| attributes = { name: "Brian #{i}", email: "brian_#{i}@example.com", notify_by_email: true, petition: petition } FactoryBot.create(:pending_signature, attributes) end petition.reload end it "does not queue a job to process the emails" do assert_enqueued_jobs 0 do do_patch end end end end describe 'using no params to add a government response' do
  1. Similar code found in 4 nodes Locations: 0 1 2 3
before do government_response_attributes[:responded_on] = nil government_response_attributes[:summary] = nil government_response_attributes[:details] = nil end it 'does not tell the moderator that their changes were saved' do do_patch expect(flash[:notice]).to be_blank end it "does not set the 'government_response' email requested receipt timestamp" do
  1. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Save button)::describe(using no params to add a government response)::it#does not set the 'government_response' email requested receipt timestamp has a flog score of 26
time = 5.days.from_now travel_to time do do_patch petition.reload expect(petition.get_email_requested_at_for('government_response')).to be_nil end end it "does not queue a job to process the emails" do assert_enqueued_jobs 0 do do_patch end end it 're-renders the admin/petitions/show template' do do_patch expect(response).to be_success expect(response).to render_template('admin/petitions/show') end end describe 'using invalid params to add a government response' do
  1. Identical code found in 4 nodes Locations: 0 1 2 3
before { government_response_attributes[:summary] = 'a' * 501 } it 're-renders the petitions/show template' do do_patch expect(response).to be_success expect(response).to render_template('petitions/show') end it 'leaves the in-memory instance with errors' do do_patch expect(assigns(:government_response).errors).not_to be_empty end it 'does not store the supplied government response in the db' do do_patch petition.reload expect(government_response).to be_nil end it 'does not stamp the government_response_at timestamp in the db' do do_patch petition.reload expect(petition.government_response_at).to be_nil end it "does not stamp the 'government_response' email requested receipt timestamp" do do_patch petition.reload expect(petition.get_email_requested_at_for('government_response')).to be_nil end it "doest not queue up a job to send the 'government_response' emails" do do_patch assert_enqueued_jobs 0 end end shared_examples_for 'adding a government response to a petition' do
  1. Identical code found in 4 nodes Locations: 0 1 2 3
it 'fetches the requested petition' do do_patch expect(assigns(:petition)).to eq petition end it 'stores the supplied response on the petition in the db' do
  1. describe(logged in as moderator user)::describe(PATCH /update)::context(when clicking the Save button)::shared_examples_for(adding a government response to a petition)::it#stores the supplied response on the petition in the db has a flog score of 52
do_patch petition.reload expect(government_response.responded_on).to eq government_response_attributes[:responded_on] expect(government_response.summary).to eq government_response_attributes[:summary] expect(government_response.details).to eq government_response_attributes[:details] end end shared_examples_for 'adding a government response to a petition in the wrong state' do
  1. Identical code found in 4 nodes Locations: 0 1 2 3
it 'throws a 404' do expect { do_patch }.to raise_error ActiveRecord::RecordNotFound end it 'does not store the supplied response on the petition in the db' do suppress(ActiveRecord::RecordNotFound) { do_patch } petition.reload expect(government_response).to be_nil end end describe 'for an open petition' do it_behaves_like 'adding a government response to a petition' end describe 'for a pending petition' do before { petition.update_column(:state, Petition::PENDING_STATE) } it_behaves_like 'adding a government response to a petition in the wrong state' end describe 'for a validated petition' do before { petition.update_column(:state, Petition::VALIDATED_STATE) } it_behaves_like 'adding a government response to a petition in the wrong state' end describe 'for a sponsored petition' do before { petition.update_column(:state, Petition::SPONSORED_STATE) } it_behaves_like 'adding a government response to a petition in the wrong state' end describe 'for a rejected petition' do before { petition.update_columns(state: Petition::REJECTED_STATE) } it_behaves_like 'adding a government response to a petition' end describe 'for a hidden petition' do before { petition.update_column(:state, Petition::HIDDEN_STATE) } it_behaves_like 'adding a government response to a petition' end end context "when two moderators update the response for the first time simultaneously" do
  1. describe(logged in as moderator user)::describe(PATCH /update)::context#when two moderators update the response for the first time simultaneously has a flog score of 33
let(:government_response) do FactoryBot.build(:government_response, responded_on: "", summary: "", details: "", petition: petition) end before do
  1. Similar code found in 2 nodes Locations: 0 1
moderated = double(:scope) allow(Petition).to receive(:moderated).and_return(moderated) allow(moderated).to receive(:find).with(petition.id.to_s).and_return(petition) end it "doesn't raise an ActiveRecord::RecordNotUnique error" do
  1. Similar code found in 2 nodes Locations: 0 1
  2. describe(logged in as moderator user)::describe(PATCH /update)::context(when two moderators update the response for the first time simultaneously)::itdoesn't raise an ActiveRecord::RecordNotUnique error has a flog score of 97
expect { expect(petition.government_response).to be_nil response_attributes = { responded_on: Date.civil(2018, 6, 23), summary: "summmary 1", details: "details 1" } patch :update, petition_id: petition.id, government_response: response_attributes, save: "Save" expect(petition.government_response.summary).to eq("summmary 1") allow(petition).to receive(:government_response).and_return(nil, petition.government_response) allow(petition).to receive(:build_government_response).and_return(government_response) response_attributes = { responded_on: Date.civil(2018, 6, 23), summary: "summmary 2", details: "details 2" } patch :update, petition_id: petition.id, government_response: response_attributes, save: "Save" expect(petition.government_response(true).summary).to eq("summmary 2") }.not_to raise_error end end end end end