Updated

spec/controllers/admin/archived / notes_controller_spec.rb

F
149 lines of codes
1 methods
351.9 complexity/method
10 churn
351.85 complexity
303 duplications
require 'rails_helper' RSpec.describe Admin::Archived::NotesController, type: :controller, admin: true do let!(:petition) { FactoryBot.create(:archived_petition) } let!(:creator) { FactoryBot.create(:archived_signature, :validated, creator: true, petition: petition) } 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
  1. Similar code found in 3 nodes Locations: 0 1 2
shared_examples_for 'viewing notes 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 describe 'for an open petition' do it_behaves_like 'viewing notes for a petition' end describe 'for a rejected petition' do let!(:petition) { FactoryBot.create(:archived_petition, :rejected) } it_behaves_like 'viewing notes for a petition' end describe 'for a hidden petition' do let!(:petition) { FactoryBot.create(:archived_petition, :hidden) } it_behaves_like 'viewing notes for a petition' end end describe 'PATCH /update' do let(:notes_attributes) do { details: 'This seems fine, just need to get legal to give it the once over before letting it through.' } end def do_patch(overrides = {})
  1. Similar code found in 2 nodes Locations: 0 1
params = { petition_id: petition.id, archived_note: notes_attributes } patch :update, params.merge(overrides) end shared_examples_for 'updating notes for a petition' do
  1. Similar code found in 2 nodes Locations: 0 1
it 'fetches the requested petition' do do_patch expect(assigns(:petition)).to eq petition end context 'with valid params' do it 'redirects to the petition show page' do do_patch expect(response).to redirect_to "https://moderate.petition.parliament.uk/admin/archived/petitions/#{petition.id}" end it 'stores the supplied notes in the db' do
  1. describe(logged in as moderator user)::describe(PATCH /update)::shared_examples_for(updating notes for a petition)::context(with valid params)::it#stores the supplied notes in the db has a flog score of 25
do_patch petition.reload expect(petition.note.details).to eq notes_attributes[:details] end end end describe 'for a published petition' do it_behaves_like 'updating notes for a petition' end describe 'for a rejected petition' do let!(:petition) { FactoryBot.create(:archived_petition, :rejected) } it_behaves_like 'updating notes for a petition' end describe 'for a hidden petition' do let!(:petition) { FactoryBot.create(:archived_petition, :hidden) } it_behaves_like 'updating notes for a petition' end context "when two moderators update the notes for the first time simultaneously" do let(:note) { FactoryBot.build(:archived_note, details: "", petition: petition) } before do allow(Archived::Petition).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 notes for the first time simultaneously)::itdoesn't raise an ActiveRecord::RecordNotUnique error has a flog score of 90
expect { expect(petition.note).to be_nil patch :update, petition_id: petition.id, archived_note: { details: "update 1" } expect(petition.note.details).to eq("update 1") allow(petition).to receive(:note).and_return(nil, petition.note) allow(petition).to receive(:build_note).and_return(note) patch :update, petition_id: petition.id, archived_note: { details: "update 2" } expect(petition.note(true).details).to eq("update 2") }.not_to raise_error end end end end end