Browse Source

Refactor sign_in user in tests

pull/2288/head
Kat 2 years ago
parent
commit
8b4ae03b25
  1. 105
      spec/requests/users_controller_spec.rb

105
spec/requests/users_controller_spec.rb

@ -120,10 +120,13 @@ RSpec.describe UsersController, type: :request do
end end
context "when user is signed in as a data provider" do context "when user is signed in as a data provider" do
before do
sign_in user
end
describe "#show" do describe "#show" do
context "when the current user matches the user ID" do context "when the current user matches the user ID" do
before do before do
sign_in user
get "/users/#{user.id}", headers:, params: {} get "/users/#{user.id}", headers:, params: {}
end end
@ -169,7 +172,6 @@ RSpec.describe UsersController, type: :request do
let(:user) { create(:user, role: nil) } let(:user) { create(:user, role: nil) }
before do before do
sign_in user
get "/users/#{user.id}", headers:, params: {} get "/users/#{user.id}", headers:, params: {}
end end
@ -180,7 +182,6 @@ RSpec.describe UsersController, type: :request do
context "when the current user does not match the user ID" do context "when the current user does not match the user ID" do
before do before do
sign_in user
get "/users/#{other_user.id}", headers:, params: {} get "/users/#{other_user.id}", headers:, params: {}
end end
@ -237,7 +238,6 @@ RSpec.describe UsersController, type: :request do
describe "#edit" do describe "#edit" do
context "when the current user matches the user ID" do context "when the current user matches the user ID" do
before do before do
sign_in user
get "/users/#{user.id}/edit", headers:, params: {} get "/users/#{user.id}/edit", headers:, params: {}
end end
@ -256,7 +256,6 @@ RSpec.describe UsersController, type: :request do
context "when the current user does not match the user ID" do context "when the current user does not match the user ID" do
before do before do
sign_in user
get "/users/#{other_user.id}/edit", headers:, params: {} get "/users/#{other_user.id}/edit", headers:, params: {}
end end
@ -269,7 +268,6 @@ RSpec.describe UsersController, type: :request do
describe "#edit_password" do describe "#edit_password" do
context "when the current user matches the user ID" do context "when the current user matches the user ID" do
before do before do
sign_in user
get "/account/edit/password", headers:, params: {} get "/account/edit/password", headers:, params: {}
end end
@ -284,7 +282,6 @@ RSpec.describe UsersController, type: :request do
context "when the current user does not match the user ID" do context "when the current user does not match the user ID" do
before do before do
sign_in user
get "/users/#{other_user.id}/edit", headers:, params: {} get "/users/#{other_user.id}/edit", headers:, params: {}
end end
@ -297,7 +294,6 @@ RSpec.describe UsersController, type: :request do
describe "#update" do describe "#update" do
context "when the current user matches the user ID" do context "when the current user matches the user ID" do
before do before do
sign_in user
patch "/users/#{user.id}", headers:, params: patch "/users/#{user.id}", headers:, params:
end end
@ -327,7 +323,6 @@ RSpec.describe UsersController, type: :request do
context "when the update fails to persist" do context "when the update fails to persist" do
before do before do
sign_in user
allow(User).to receive(:find_by).and_return(user) allow(User).to receive(:find_by).and_return(user)
allow(user).to receive(:update).and_return(false) allow(user).to receive(:update).and_return(false)
patch "/users/#{user.id}", headers:, params: patch "/users/#{user.id}", headers:, params:
@ -342,7 +337,6 @@ RSpec.describe UsersController, type: :request do
let(:params) { { id: other_user.id, user: { name: new_name } } } let(:params) { { id: other_user.id, user: { name: new_name } } }
before do before do
sign_in user
patch "/users/#{other_user.id}", headers:, params: patch "/users/#{other_user.id}", headers:, params:
end end
@ -359,7 +353,6 @@ RSpec.describe UsersController, type: :request do
end end
before do before do
sign_in user
patch "/users/#{user.id}", headers:, params: patch "/users/#{user.id}", headers:, params:
end end
@ -382,10 +375,6 @@ RSpec.describe UsersController, type: :request do
end end
let(:request) { post "/users/", headers:, params: } let(:request) { post "/users/", headers:, params: }
before do
sign_in user
end
it "does not invite a new user" do it "does not invite a new user" do
expect { request }.not_to change(User, :count) expect { request }.not_to change(User, :count)
end end
@ -398,8 +387,6 @@ RSpec.describe UsersController, type: :request do
describe "#delete-confirmation" do describe "#delete-confirmation" do
before do before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
get "/users/#{user.id}/delete-confirmation" get "/users/#{user.id}/delete-confirmation"
end end
@ -410,8 +397,6 @@ RSpec.describe UsersController, type: :request do
describe "#delete" do describe "#delete" do
before do before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
delete "/users/#{user.id}/delete" delete "/users/#{user.id}/delete"
end end
@ -425,11 +410,11 @@ RSpec.describe UsersController, type: :request do
let(:user) { create(:user, :data_coordinator, email: "coordinator@example.com", organisation: create(:organisation, :without_dpc)) } let(:user) { create(:user, :data_coordinator, email: "coordinator@example.com", organisation: create(:organisation, :without_dpc)) }
let!(:other_user) { create(:user, organisation: user.organisation, name: "filter name", email: "filter@example.com") } let!(:other_user) { create(:user, organisation: user.organisation, name: "filter name", email: "filter@example.com") }
describe "#index" do before do
before do sign_in user
sign_in user end
end
describe "#index" do
context "when there are no url params" do context "when there are no url params" do
before do before do
get "/users", headers:, params: {} get "/users", headers:, params: {}
@ -570,7 +555,6 @@ RSpec.describe UsersController, type: :request do
let(:user) { create(:user) } let(:user) { create(:user) }
before do before do
sign_in user
get "/users", headers:, params: {} get "/users", headers:, params: {}
end end
@ -582,7 +566,6 @@ RSpec.describe UsersController, type: :request do
describe "#show" do describe "#show" do
context "when the current user matches the user ID" do context "when the current user matches the user ID" do
before do before do
sign_in user
get "/users/#{user.id}", headers:, params: {} get "/users/#{user.id}", headers:, params: {}
end end
@ -622,7 +605,6 @@ RSpec.describe UsersController, type: :request do
context "when the current user does not match the user ID" do context "when the current user does not match the user ID" do
before do before do
sign_in user
get "/users/#{other_user.id}", headers:, params: {} get "/users/#{other_user.id}", headers:, params: {}
end end
@ -690,7 +672,6 @@ RSpec.describe UsersController, type: :request do
describe "#edit" do describe "#edit" do
context "when the current user matches the user ID" do context "when the current user matches the user ID" do
before do before do
sign_in user
get "/users/#{user.id}/edit", headers:, params: {} get "/users/#{user.id}/edit", headers:, params: {}
end end
@ -711,7 +692,6 @@ RSpec.describe UsersController, type: :request do
context "when the current user does not match the user ID" do context "when the current user does not match the user ID" do
before do before do
sign_in user
get "/users/#{other_user.id}/edit", headers:, params: {} get "/users/#{other_user.id}/edit", headers:, params: {}
end end
@ -744,7 +724,6 @@ RSpec.describe UsersController, type: :request do
describe "#edit_password" do describe "#edit_password" do
context "when the current user matches the user ID" do context "when the current user matches the user ID" do
before do before do
sign_in user
get "/account/edit/password", headers:, params: {} get "/account/edit/password", headers:, params: {}
end end
@ -758,10 +737,6 @@ RSpec.describe UsersController, type: :request do
end end
context "when the current user does not match the user ID" do context "when the current user does not match the user ID" do
before do
sign_in user
end
it "there is no route" do it "there is no route" do
expect { expect {
get "/users/#{other_user.id}/password/edit", headers:, params: {} get "/users/#{other_user.id}/password/edit", headers:, params: {}
@ -773,7 +748,6 @@ RSpec.describe UsersController, type: :request do
describe "#update" do describe "#update" do
context "when the current user matches the user ID" do context "when the current user matches the user ID" do
before do before do
sign_in user
patch "/users/#{user.id}", headers:, params: patch "/users/#{user.id}", headers:, params:
end end
@ -808,7 +782,6 @@ RSpec.describe UsersController, type: :request do
end end
before do before do
sign_in user
patch "/users/#{user.id}", headers:, params: patch "/users/#{user.id}", headers:, params:
end end
@ -820,10 +793,6 @@ RSpec.describe UsersController, type: :request do
end end
context "when the current user does not match the user ID" do context "when the current user does not match the user ID" do
before do
sign_in user
end
context "when the user is part of the same organisation as the current user" do context "when the user is part of the same organisation as the current user" do
it "updates the user" do it "updates the user" do
expect { patch "/users/#{other_user.id}", headers:, params: } expect { patch "/users/#{other_user.id}", headers:, params: }
@ -909,7 +878,6 @@ RSpec.describe UsersController, type: :request do
let(:params) { { id: other_user.id, user: { name: new_name } } } let(:params) { { id: other_user.id, user: { name: new_name } } }
before do before do
sign_in user
patch "/users/#{other_user.id}", headers:, params: patch "/users/#{other_user.id}", headers:, params:
end end
@ -922,7 +890,6 @@ RSpec.describe UsersController, type: :request do
context "when the update fails to persist" do context "when the update fails to persist" do
before do before do
sign_in user
allow(User).to receive(:find_by).and_return(user) allow(User).to receive(:find_by).and_return(user)
allow(user).to receive(:update).and_return(false) allow(user).to receive(:update).and_return(false)
patch "/users/#{user.id}", headers:, params: patch "/users/#{user.id}", headers:, params:
@ -943,7 +910,6 @@ RSpec.describe UsersController, type: :request do
end end
before do before do
sign_in user
patch "/users/#{user.id}", headers:, params: patch "/users/#{user.id}", headers:, params:
end end
@ -1015,10 +981,6 @@ RSpec.describe UsersController, type: :request do
end end
let(:request) { post "/users/", headers:, params: } let(:request) { post "/users/", headers:, params: }
before do
sign_in user
end
it "invites a new user" do it "invites a new user" do
expect { request }.to change(User, :count).by(1) expect { request }.to change(User, :count).by(1)
end end
@ -1140,10 +1102,6 @@ RSpec.describe UsersController, type: :request do
end end
describe "#new" do describe "#new" do
before do
sign_in user
end
it "cannot assign support role to the new user" do it "cannot assign support role to the new user" do
get "/users/new" get "/users/new"
expect(page).not_to have_field("user-role-support-field") expect(page).not_to have_field("user-role-support-field")
@ -1151,10 +1109,6 @@ RSpec.describe UsersController, type: :request do
end end
describe "#deactivate" do describe "#deactivate" do
before do
sign_in user
end
context "when the current user matches the user ID" do context "when the current user matches the user ID" do
before do before do
get "/users/#{user.id}/deactivate", headers:, params: {} get "/users/#{user.id}/deactivate", headers:, params: {}
@ -1181,10 +1135,6 @@ RSpec.describe UsersController, type: :request do
end end
describe "#reactivate" do describe "#reactivate" do
before do
sign_in user
end
context "when the current user does not match the user ID" do context "when the current user does not match the user ID" do
before do before do
other_user.update!(active: false) other_user.update!(active: false)
@ -1203,8 +1153,6 @@ RSpec.describe UsersController, type: :request do
describe "#delete-confirmation" do describe "#delete-confirmation" do
before do before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
get "/users/#{user.id}/delete-confirmation" get "/users/#{user.id}/delete-confirmation"
end end
@ -1215,8 +1163,6 @@ RSpec.describe UsersController, type: :request do
describe "#delete" do describe "#delete" do
before do before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
delete "/users/#{user.id}/delete" delete "/users/#{user.id}/delete"
end end
@ -1232,6 +1178,7 @@ RSpec.describe UsersController, type: :request do
before do before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false) allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
end end
describe "#index" do describe "#index" do
@ -1240,7 +1187,6 @@ RSpec.describe UsersController, type: :request do
let!(:other_org_user) { create(:user, name: "User 4", email: "otherorg@otherexample.com", organisation: create(:organisation, :without_dpc)) } let!(:other_org_user) { create(:user, name: "User 4", email: "otherorg@otherexample.com", organisation: create(:organisation, :without_dpc)) }
before do before do
sign_in user
get "/users", headers:, params: {} get "/users", headers:, params: {}
end end
@ -1388,7 +1334,6 @@ RSpec.describe UsersController, type: :request do
before do before do
create_list(:user, 25) create_list(:user, 25)
sign_in user
end end
context "when there is no search param" do context "when there is no search param" do
@ -1433,7 +1378,6 @@ RSpec.describe UsersController, type: :request do
describe "#show" do describe "#show" do
context "when the current user matches the user ID" do context "when the current user matches the user ID" do
before do before do
sign_in user
get "/users/#{user.id}", headers:, params: {} get "/users/#{user.id}", headers:, params: {}
end end
@ -1458,7 +1402,6 @@ RSpec.describe UsersController, type: :request do
context "when the current user does not match the user ID" do context "when the current user does not match the user ID" do
before do before do
sign_in user
get "/users/#{other_user.id}", headers:, params: {} get "/users/#{other_user.id}", headers:, params: {}
end end
@ -1565,7 +1508,6 @@ RSpec.describe UsersController, type: :request do
describe "#edit" do describe "#edit" do
context "when the current user matches the user ID" do context "when the current user matches the user ID" do
before do before do
sign_in user
get "/users/#{user.id}/edit", headers:, params: {} get "/users/#{user.id}/edit", headers:, params: {}
end end
@ -1587,7 +1529,6 @@ RSpec.describe UsersController, type: :request do
context "when the current user does not match the user ID" do context "when the current user does not match the user ID" do
before do before do
sign_in user
get "/users/#{other_user.id}/edit", headers:, params: {} get "/users/#{other_user.id}/edit", headers:, params: {}
end end
@ -1643,7 +1584,6 @@ RSpec.describe UsersController, type: :request do
describe "#edit_password" do describe "#edit_password" do
context "when the current user matches the user ID" do context "when the current user matches the user ID" do
before do before do
sign_in user
get "/account/edit/password", headers:, params: {} get "/account/edit/password", headers:, params: {}
end end
@ -1657,10 +1597,6 @@ RSpec.describe UsersController, type: :request do
end end
context "when the current user does not match the user ID" do context "when the current user does not match the user ID" do
before do
sign_in user
end
it "there is no route" do it "there is no route" do
expect { expect {
get "/users/#{other_user.id}/password/edit", headers:, params: {} get "/users/#{other_user.id}/password/edit", headers:, params: {}
@ -1673,10 +1609,6 @@ RSpec.describe UsersController, type: :request do
context "when the current user matches the user ID" do context "when the current user matches the user ID" do
let(:request) { patch "/users/#{user.id}", headers:, params: } let(:request) { patch "/users/#{user.id}", headers:, params: }
before do
sign_in user
end
it "updates the user" do it "updates the user" do
request request
user.reload user.reload
@ -1788,7 +1720,6 @@ RSpec.describe UsersController, type: :request do
end end
before do before do
sign_in user
patch "/users/#{user.id}", headers:, params: patch "/users/#{user.id}", headers:, params:
end end
@ -1800,10 +1731,6 @@ RSpec.describe UsersController, type: :request do
end end
context "when the current user does not match the user ID" do context "when the current user does not match the user ID" do
before do
sign_in user
end
context "when the user is part of the same organisation as the current user" do context "when the user is part of the same organisation as the current user" do
it "updates the user" do it "updates the user" do
expect { patch "/users/#{other_user.id}", headers:, params: } expect { patch "/users/#{other_user.id}", headers:, params: }
@ -1858,10 +1785,6 @@ RSpec.describe UsersController, type: :request do
let(:other_user) { create(:user) } let(:other_user) { create(:user) }
let(:params) { { id: other_user.id, user: { name: new_name } } } let(:params) { { id: other_user.id, user: { name: new_name } } }
before do
sign_in user
end
it "updates the user" do it "updates the user" do
expect { patch "/users/#{other_user.id}", headers:, params: } expect { patch "/users/#{other_user.id}", headers:, params: }
.to change { other_user.reload.name }.from(other_user.name).to(new_name) .to change { other_user.reload.name }.from(other_user.name).to(new_name)
@ -1948,7 +1871,6 @@ RSpec.describe UsersController, type: :request do
context "when the update fails to persist" do context "when the update fails to persist" do
before do before do
sign_in user
allow(User).to receive(:find_by).and_return(user) allow(User).to receive(:find_by).and_return(user)
allow(user).to receive(:update).and_return(false) allow(user).to receive(:update).and_return(false)
patch "/users/#{user.id}", headers:, params: patch "/users/#{user.id}", headers:, params:
@ -1976,10 +1898,6 @@ RSpec.describe UsersController, type: :request do
end end
let(:request) { post "/users/", headers:, params: } let(:request) { post "/users/", headers:, params: }
before do
sign_in user
end
it "invites a new user" do it "invites a new user" do
expect { request }.to change(User, :count).by(1) expect { request }.to change(User, :count).by(1)
end end
@ -2052,7 +1970,6 @@ RSpec.describe UsersController, type: :request do
describe "#new" do describe "#new" do
before do before do
sign_in user
create(:organisation, name: "other org") create(:organisation, name: "other org")
end end
@ -2083,8 +2000,6 @@ RSpec.describe UsersController, type: :request do
describe "#delete-confirmation" do describe "#delete-confirmation" do
before do before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
get "/users/#{other_user.id}/delete-confirmation" get "/users/#{other_user.id}/delete-confirmation"
end end
@ -2120,8 +2035,6 @@ RSpec.describe UsersController, type: :request do
let(:other_user) { create(:user, name: "User to be deleted") } let(:other_user) { create(:user, name: "User to be deleted") }
before do before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
delete "/users/#{other_user.id}/delete" delete "/users/#{other_user.id}/delete"
end end

Loading…
Cancel
Save