Browse Source

Make tests more generic

pull/1684/head
Jack S 3 years ago
parent
commit
5ea21e212e
  1. 2
      spec/controllers/modules/search_filter_spec.rb
  2. 6
      spec/features/lettings_log_spec.rb
  3. 26
      spec/models/form/lettings/questions/created_by_id_spec.rb
  4. 25
      spec/models/form/sales/questions/created_by_id_spec.rb
  5. 66
      spec/models/organisation_spec.rb
  6. 54
      spec/models/user_spec.rb
  7. 20
      spec/requests/users_controller_spec.rb
  8. 10
      spec/services/imports/data_protection_confirmation_import_service_spec.rb

2
spec/controllers/modules/search_filter_spec.rb

@ -49,7 +49,7 @@ RSpec.describe Modules::SearchFilter do
let(:search_term) { nil } let(:search_term) { nil }
it "returns all the users" do it "returns all the users" do
expect(instance.filtered_users(user_list, search_term).count).to eq(7) expect(instance.filtered_users(user_list, search_term).count).to eq(user_list.count)
end end
end end
end end

6
spec/features/lettings_log_spec.rb

@ -85,7 +85,7 @@ RSpec.describe "Lettings Log Features" do
click_link("Set up this lettings log") click_link("Set up this lettings log")
select(support_user.organisation.name, from: "lettings-log-owning-organisation-id-field") select(support_user.organisation.name, from: "lettings-log-owning-organisation-id-field")
click_button("Save and continue") click_button("Save and continue")
select(support_user.name, from: "lettings-log-created-by-id-field") select("#{support_user.name} (#{support_user.email})", from: "lettings-log-created-by-id-field")
click_button("Save and continue") click_button("Save and continue")
log_id = page.current_path.scan(/\d/).join log_id = page.current_path.scan(/\d/).join
visit("lettings-logs/#{log_id}/setup/check-answers") visit("lettings-logs/#{log_id}/setup/check-answers")
@ -95,7 +95,7 @@ RSpec.describe "Lettings Log Features" do
end end
context "when visiting a subsection check answers page" do context "when visiting a subsection check answers page" do
let(:lettings_log) { FactoryBot.create(:lettings_log, :setup_completed) } let(:lettings_log) { create(:lettings_log, :setup_completed) }
it "has the correct breadcrumbs with the correct links" do it "has the correct breadcrumbs with the correct links" do
visit lettings_log_setup_check_answers_path(lettings_log) visit lettings_log_setup_check_answers_path(lettings_log)
@ -108,7 +108,7 @@ RSpec.describe "Lettings Log Features" do
end end
context "when reviewing a complete log" do context "when reviewing a complete log" do
let(:lettings_log) { FactoryBot.create(:lettings_log, :completed) } let(:lettings_log) { create(:lettings_log, :completed) }
it "has the correct breadcrumbs with the correct links" do it "has the correct breadcrumbs with the correct links" do
visit review_lettings_log_path(lettings_log) visit review_lettings_log_path(lettings_log)

26
spec/models/form/lettings/questions/created_by_id_spec.rb

@ -41,6 +41,12 @@ RSpec.describe Form::Lettings::Questions::CreatedById, type: :model do
expect(question.derived?).to be true expect(question.derived?).to be true
end end
def expected_option_for_users(users)
users.each_with_object({ "" => "Select an option" }) do |user, obj|
obj[user.id] = "#{user.name} (#{user.email})"
end
end
context "when the current user is support" do context "when the current user is support" do
let(:owning_org_user) { create(:user) } let(:owning_org_user) { create(:user) }
let(:managing_org_user) { create(:user) } let(:managing_org_user) { create(:user) }
@ -51,17 +57,8 @@ RSpec.describe Form::Lettings::Questions::CreatedById, type: :model do
create(:lettings_log, created_by: support_user, owning_organisation: owning_org_user.organisation, managing_organisation: managing_org_user.organisation) create(:lettings_log, created_by: support_user, owning_organisation: owning_org_user.organisation, managing_organisation: managing_org_user.organisation)
end end
let(:expected_answer_options) do
{
"" => "Select an option",
managing_org_user.id => "#{managing_org_user.name} (#{managing_org_user.email})",
owning_org_user.id => "#{owning_org_user.name} (#{owning_org_user.email})",
support_user.id => "#{support_user.name} (#{support_user.email})",
}
end
it "only displays users that belong to owning and managing organisations" do it "only displays users that belong to owning and managing organisations" do
expect(question.displayed_answer_options(lettings_log, support_user)).to eq(expected_answer_options) expect(question.displayed_answer_options(lettings_log, support_user)).to eq(expected_option_for_users(managing_org_user.organisation.users + owning_org_user.organisation.users))
end end
end end
end end
@ -77,15 +74,8 @@ RSpec.describe Form::Lettings::Questions::CreatedById, type: :model do
let(:user_in_same_org) { create(:user, organisation: data_coordinator.organisation) } let(:user_in_same_org) { create(:user, organisation: data_coordinator.organisation) }
let(:expected_answer_options) do
{
"" => "Select an option",
data_coordinator.id => "#{data_coordinator.name} (#{data_coordinator.email})",
}
end
it "only displays users that belong user's org" do it "only displays users that belong user's org" do
expect(question.displayed_answer_options(lettings_log, data_coordinator)).to eq(expected_answer_options) expect(question.displayed_answer_options(lettings_log, data_coordinator)).to eq(expected_option_for_users(data_coordinator.organisation.users))
end end
end end
end end

25
spec/models/form/sales/questions/created_by_id_spec.rb

@ -37,6 +37,12 @@ RSpec.describe Form::Sales::Questions::CreatedById, type: :model do
expect(question.derived?).to be true expect(question.derived?).to be true
end end
def expected_option_for_users(users)
users.each_with_object({ "" => "Select an option" }) do |user, obj|
obj[user.id] = "#{user.name} (#{user.email})"
end
end
context "when the current user is support" do context "when the current user is support" do
let(:support_user) { build(:user, :support) } let(:support_user) { build(:user, :support) }
@ -47,15 +53,9 @@ RSpec.describe Form::Sales::Questions::CreatedById, type: :model do
describe "#displayed_answer_options" do describe "#displayed_answer_options" do
let(:owning_org_user) { create(:user) } let(:owning_org_user) { create(:user) }
let(:sales_log) { create(:sales_log, owning_organisation: owning_org_user.organisation) } let(:sales_log) { create(:sales_log, owning_organisation: owning_org_user.organisation) }
let(:expected_answer_options) do
{
"" => "Select an option",
owning_org_user.id => "#{owning_org_user.name} (#{owning_org_user.email})",
}
end
it "only displays users that belong to the owning organisation" do it "only displays users that belong to the owning organisation" do
expect(question.displayed_answer_options(sales_log, support_user)).to eq(expected_answer_options) expect(question.displayed_answer_options(sales_log, support_user)).to eq(expected_option_for_users(owning_org_user.organisation.users))
end end
end end
end end
@ -70,18 +70,13 @@ RSpec.describe Form::Sales::Questions::CreatedById, type: :model do
describe "#displayed_answer_options" do describe "#displayed_answer_options" do
let(:owning_org_user) { create(:user) } let(:owning_org_user) { create(:user) }
let(:sales_log) { create(:sales_log, owning_organisation: owning_org_user.organisation) } let(:sales_log) { create(:sales_log, owning_organisation: owning_org_user.organisation) }
let!(:user_in_same_org) { create(:user, organisation: data_coordinator.organisation) }
let(:expected_answer_options) do before do
{ create(:user, organisation: data_coordinator.organisation)
"" => "Select an option",
user_in_same_org.id => "#{user_in_same_org.name} (#{user_in_same_org.email})",
data_coordinator.id => "#{data_coordinator.name} (#{data_coordinator.email})",
}
end end
it "only displays users that belong user's org" do it "only displays users that belong user's org" do
expect(question.displayed_answer_options(sales_log, data_coordinator)).to eq(expected_answer_options) expect(question.displayed_answer_options(sales_log, data_coordinator)).to eq(expected_option_for_users(data_coordinator.organisation.users))
end end
end end
end end

66
spec/models/organisation_spec.rb

@ -2,39 +2,35 @@ require "rails_helper"
RSpec.describe Organisation, type: :model do RSpec.describe Organisation, type: :model do
describe "#new" do describe "#new" do
let(:user) { FactoryBot.create(:user) } let(:user) { create(:user) }
let!(:organisation) { user.organisation } let!(:organisation) { user.organisation }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: organisation) } let!(:scheme) { create(:scheme, owning_organisation: organisation) }
it "has expected fields" do it "has expected fields" do
expect(organisation.attribute_names).to include("name", "phone", "provider_type") expect(organisation.attribute_names).to include("name", "phone", "provider_type")
end end
it "has users" do
expect(organisation.users.first).to eq(user)
end
it "has owned_schemes" do it "has owned_schemes" do
expect(organisation.owned_schemes.first).to eq(scheme) expect(organisation.owned_schemes.first).to eq(scheme)
end end
it "validates provider_type presence" do it "validates provider_type presence" do
expect { FactoryBot.create(:organisation, provider_type: nil) } expect { create(:organisation, provider_type: nil) }
.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Provider type #{I18n.t('validations.organisation.provider_type_missing')}") .to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Provider type #{I18n.t('validations.organisation.provider_type_missing')}")
end end
context "with parent/child associations", :aggregate_failures do context "with parent/child associations", :aggregate_failures do
let!(:child_organisation) { FactoryBot.create(:organisation, name: "DLUHC Child") } let!(:child_organisation) { create(:organisation, name: "DLUHC Child") }
let!(:grandchild_organisation) { FactoryBot.create(:organisation, name: "DLUHC Grandchild") } let!(:grandchild_organisation) { create(:organisation, name: "DLUHC Grandchild") }
before do before do
FactoryBot.create( create(
:organisation_relationship, :organisation_relationship,
child_organisation:, child_organisation:,
parent_organisation: organisation, parent_organisation: organisation,
) )
FactoryBot.create( create(
:organisation_relationship, :organisation_relationship,
child_organisation: grandchild_organisation, child_organisation: grandchild_organisation,
parent_organisation: child_organisation, parent_organisation: child_organisation,
@ -53,17 +49,17 @@ RSpec.describe Organisation, type: :model do
end end
context "with owning association", :aggregate_failures do context "with owning association", :aggregate_failures do
let!(:child_organisation) { FactoryBot.create(:organisation, name: "DLUHC Child") } let!(:child_organisation) { create(:organisation, name: "DLUHC Child") }
let!(:grandchild_organisation) { FactoryBot.create(:organisation, name: "DLUHC Grandchild") } let!(:grandchild_organisation) { create(:organisation, name: "DLUHC Grandchild") }
before do before do
FactoryBot.create( create(
:organisation_relationship, :organisation_relationship,
child_organisation:, child_organisation:,
parent_organisation: organisation, parent_organisation: organisation,
) )
FactoryBot.create( create(
:organisation_relationship, :organisation_relationship,
child_organisation: grandchild_organisation, child_organisation: grandchild_organisation,
parent_organisation: child_organisation, parent_organisation: child_organisation,
@ -77,17 +73,17 @@ RSpec.describe Organisation, type: :model do
end end
context "with managing association", :aggregate_failures do context "with managing association", :aggregate_failures do
let!(:child_organisation) { FactoryBot.create(:organisation, name: "DLUHC Child") } let!(:child_organisation) { create(:organisation, name: "DLUHC Child") }
let!(:grandchild_organisation) { FactoryBot.create(:organisation, name: "DLUHC Grandchild") } let!(:grandchild_organisation) { create(:organisation, name: "DLUHC Grandchild") }
before do before do
FactoryBot.create( create(
:organisation_relationship, :organisation_relationship,
child_organisation:, child_organisation:,
parent_organisation: organisation, parent_organisation: organisation,
) )
FactoryBot.create( create(
:organisation_relationship, :organisation_relationship,
child_organisation: grandchild_organisation, child_organisation: grandchild_organisation,
parent_organisation: child_organisation, parent_organisation: child_organisation,
@ -103,8 +99,8 @@ RSpec.describe Organisation, type: :model do
context "with data protection confirmations" do context "with data protection confirmations" do
before do before do
FactoryBot.create(:data_protection_confirmation, organisation:, confirmed: false, created_at: Time.utc(2018, 0o6, 0o5, 10, 36, 49)) create(:data_protection_confirmation, organisation:, confirmed: false, created_at: Time.utc(2018, 0o6, 0o5, 10, 36, 49))
FactoryBot.create(:data_protection_confirmation, organisation:, created_at: Time.utc(2019, 0o6, 0o5, 10, 36, 49)) create(:data_protection_confirmation, organisation:, created_at: Time.utc(2019, 0o6, 0o5, 10, 36, 49))
end end
it "takes the most recently created" do it "takes the most recently created" do
@ -118,11 +114,11 @@ RSpec.describe Organisation, type: :model do
end end
before do before do
FactoryBot.create(:organisation_rent_period, organisation:, rent_period: 2) create(:organisation_rent_period, organisation:, rent_period: 2)
FactoryBot.create(:organisation_rent_period, organisation:, rent_period: 3) create(:organisation_rent_period, organisation:, rent_period: 3)
# Unmapped and ignored by `rent_period_labels` # Unmapped and ignored by `rent_period_labels`
FactoryBot.create(:organisation_rent_period, organisation:, rent_period: 10) create(:organisation_rent_period, organisation:, rent_period: 10)
allow(RentPeriod).to receive(:rent_period_mappings).and_return(rent_period_mappings) allow(RentPeriod).to receive(:rent_period_mappings).and_return(rent_period_mappings)
end end
@ -142,9 +138,9 @@ RSpec.describe Organisation, type: :model do
end end
context "with lettings logs" do context "with lettings logs" do
let(:other_organisation) { FactoryBot.create(:organisation) } let(:other_organisation) { create(:organisation) }
let!(:owned_lettings_log) do let!(:owned_lettings_log) do
FactoryBot.create( create(
:lettings_log, :lettings_log,
:completed, :completed,
managing_organisation: other_organisation, managing_organisation: other_organisation,
@ -152,7 +148,7 @@ RSpec.describe Organisation, type: :model do
) )
end end
let!(:managed_lettings_log) do let!(:managed_lettings_log) do
FactoryBot.create( create(
:lettings_log, :lettings_log,
created_by: user, created_by: user,
) )
@ -173,7 +169,7 @@ RSpec.describe Organisation, type: :model do
end end
describe "paper trail" do describe "paper trail" do
let(:organisation) { FactoryBot.create(:organisation) } let(:organisation) { create(:organisation) }
it "creates a record of changes to a log" do it "creates a record of changes to a log" do
expect { organisation.update!(name: "new test name") }.to change(organisation.versions, :count).by(1) expect { organisation.update!(name: "new test name") }.to change(organisation.versions, :count).by(1)
@ -187,11 +183,11 @@ RSpec.describe Organisation, type: :model do
describe "delete cascade" do describe "delete cascade" do
context "when the organisation is deleted" do context "when the organisation is deleted" do
let!(:organisation) { FactoryBot.create(:organisation) } let!(:organisation) { create(:organisation) }
let!(:user) { FactoryBot.create(:user, :support, last_sign_in_at: Time.zone.now, organisation:) } let!(:user) { create(:user, :support, last_sign_in_at: Time.zone.now, organisation:) }
let!(:scheme_to_delete) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } let!(:scheme_to_delete) { create(:scheme, owning_organisation: user.organisation) }
let!(:log_to_delete) { FactoryBot.create(:lettings_log, owning_organisation: user.organisation) } let!(:log_to_delete) { create(:lettings_log, owning_organisation: user.organisation) }
let!(:sales_log_to_delete) { FactoryBot.create(:sales_log, owning_organisation: user.organisation) } let!(:sales_log_to_delete) { create(:sales_log, owning_organisation: user.organisation) }
context "when organisation is deleted" do context "when organisation is deleted" do
it "child relationships ie logs, schemes and users are deleted too - application" do it "child relationships ie logs, schemes and users are deleted too - application" do
@ -216,8 +212,8 @@ RSpec.describe Organisation, type: :model do
describe "scopes" do describe "scopes" do
before do before do
FactoryBot.create(:organisation, name: "Joe Bloggs") create(:organisation, name: "Joe Bloggs")
FactoryBot.create(:organisation, name: "Tom Smith") create(:organisation, name: "Tom Smith")
end end
context "when searching by name" do context "when searching by name" do

54
spec/models/user_spec.rb

@ -2,10 +2,10 @@ require "rails_helper"
RSpec.describe User, type: :model do RSpec.describe User, type: :model do
describe "#new" do describe "#new" do
let(:user) { FactoryBot.create(:user, old_user_id: "3") } let(:user) { create(:user, old_user_id: "3") }
let(:other_organisation) { FactoryBot.create(:organisation) } let(:other_organisation) { create(:organisation) }
let!(:owned_lettings_log) do let!(:owned_lettings_log) do
FactoryBot.create( create(
:lettings_log, :lettings_log,
:completed, :completed,
managing_organisation: other_organisation, managing_organisation: other_organisation,
@ -13,7 +13,7 @@ RSpec.describe User, type: :model do
) )
end end
let!(:managed_lettings_log) do let!(:managed_lettings_log) do
FactoryBot.create( create(
:lettings_log, :lettings_log,
created_by: user, created_by: user,
owning_organisation: other_organisation, owning_organisation: other_organisation,
@ -103,7 +103,7 @@ RSpec.describe User, type: :model do
end end
context "when the user is a data coordinator" do context "when the user is a data coordinator" do
let(:user) { FactoryBot.create(:user, :data_coordinator) } let(:user) { create(:user, :data_coordinator) }
it "can assign all roles except support" do it "can assign all roles except support" do
expect(user.assignable_roles).to eq({ expect(user.assignable_roles).to eq({
@ -124,7 +124,7 @@ RSpec.describe User, type: :model do
context "and their organisation has managing agents" do context "and their organisation has managing agents" do
before do before do
FactoryBot.create(:organisation_relationship, parent_organisation: user.organisation) create(:organisation_relationship, parent_organisation: user.organisation)
end end
it "can filter lettings logs by user, year, status and organisation" do it "can filter lettings logs by user, year, status and organisation" do
@ -134,8 +134,8 @@ RSpec.describe User, type: :model do
end end
context "when the user is a Customer Support person" do context "when the user is a Customer Support person" do
let(:user) { FactoryBot.create(:user, :support) } let(:user) { create(:user, :support) }
let!(:other_orgs_log) { FactoryBot.create(:lettings_log) } let!(:other_orgs_log) { create(:lettings_log) }
it "has access to logs from all organisations" do it "has access to logs from all organisations" do
expect(user.lettings_logs.to_a).to match_array([owned_lettings_log, managed_lettings_log, other_orgs_log]) expect(user.lettings_logs.to_a).to match_array([owned_lettings_log, managed_lettings_log, other_orgs_log])
@ -159,7 +159,7 @@ RSpec.describe User, type: :model do
end end
context "when the user is in development environment" do context "when the user is in development environment" do
let(:user) { FactoryBot.create(:user, :support) } let(:user) { create(:user, :support) }
before do before do
allow(Rails.env).to receive(:development?).and_return(true) allow(Rails.env).to receive(:development?).and_return(true)
@ -171,7 +171,7 @@ RSpec.describe User, type: :model do
end end
context "when the user is in review environment" do context "when the user is in review environment" do
let(:user) { FactoryBot.create(:user, :support) } let(:user) { create(:user, :support) }
before do before do
allow(Rails.env).to receive(:development?).and_return(false) allow(Rails.env).to receive(:development?).and_return(false)
@ -185,7 +185,7 @@ RSpec.describe User, type: :model do
end end
describe "paper trail" do describe "paper trail" do
let(:user) { FactoryBot.create(:user) } let(:user) { create(:user) }
it "creates a record of changes to a log" do it "creates a record of changes to a log" do
expect { user.update!(name: "new test name") }.to change(user.versions, :count).by(1) expect { user.update!(name: "new test name") }.to change(user.versions, :count).by(1)
@ -217,13 +217,13 @@ RSpec.describe User, type: :model do
end end
describe "scopes" do describe "scopes" do
let(:organisation_1) { FactoryBot.create(:organisation, name: "A") } let(:organisation_1) { create(:organisation, :without_dpc, name: "A") }
let(:organisation_2) { FactoryBot.create(:organisation, name: "B") } let(:organisation_2) { create(:organisation, :without_dpc, name: "B") }
let!(:user_1) { FactoryBot.create(:user, name: "Joe Bloggs", email: "joe@example.com", organisation: organisation_1, role: "support") } let!(:user_1) { create(:user, name: "Joe Bloggs", email: "joe@example.com", organisation: organisation_1, role: "support") }
let!(:user_3) { FactoryBot.create(:user, name: "Tom Smith", email: "tom@example.com", organisation: organisation_1, role: "data_provider") } let!(:user_3) { create(:user, name: "Tom Smith", email: "tom@example.com", organisation: organisation_1, role: "data_provider") }
let!(:user_2) { FactoryBot.create(:user, name: "Jenny Ford", email: "jenny@smith.com", organisation: organisation_1, role: "data_coordinator") } let!(:user_2) { create(:user, name: "Jenny Ford", email: "jenny@smith.com", organisation: organisation_1, role: "data_coordinator") }
let!(:user_4) { FactoryBot.create(:user, name: "Greg Thomas", email: "greg@org2.com", organisation: organisation_2, role: "data_coordinator") } let!(:user_4) { create(:user, name: "Greg Thomas", email: "greg@org2.com", organisation: organisation_2, role: "data_coordinator") }
let!(:user_5) { FactoryBot.create(:user, name: "Adam Thomas", email: "adam@org2.com", organisation: organisation_2, role: "data_coordinator") } let!(:user_5) { create(:user, name: "Adam Thomas", email: "adam@org2.com", organisation: organisation_2, role: "data_coordinator") }
context "when searching by name" do context "when searching by name" do
it "returns case insensitive matching records" do it "returns case insensitive matching records" do
@ -271,7 +271,7 @@ RSpec.describe User, type: :model do
let(:error_message) { "Validation failed: Password #{I18n.t('activerecord.errors.models.user.attributes.password.too_short', count: 8)}" } let(:error_message) { "Validation failed: Password #{I18n.t('activerecord.errors.models.user.attributes.password.too_short', count: 8)}" }
it "validates password length" do it "validates password length" do
expect { FactoryBot.create(:user, password:) } expect { create(:user, password:) }
.to raise_error(ActiveRecord::RecordInvalid, error_message) .to raise_error(ActiveRecord::RecordInvalid, error_message)
end end
end end
@ -281,27 +281,27 @@ RSpec.describe User, type: :model do
let(:error_message) { "Validation failed: email #{I18n.t('activerecord.errors.models.user.attributes.email.invalid')}" } let(:error_message) { "Validation failed: email #{I18n.t('activerecord.errors.models.user.attributes.email.invalid')}" }
it "validates email format" do it "validates email format" do
expect { FactoryBot.create(:user, email: invalid_email) } expect { create(:user, email: invalid_email) }
.to raise_error(ActiveRecord::RecordInvalid, error_message) .to raise_error(ActiveRecord::RecordInvalid, error_message)
end end
end end
context "when the email entered has already been used" do context "when the email entered has already been used" do
let(:user) { FactoryBot.create(:user) } let(:user) { create(:user) }
let(:error_message) { "Validation failed: email #{I18n.t('activerecord.errors.models.user.attributes.email.taken')}" } let(:error_message) { "Validation failed: email #{I18n.t('activerecord.errors.models.user.attributes.email.taken')}" }
it "validates email uniqueness" do it "validates email uniqueness" do
expect { FactoryBot.create(:user, email: user.email) } expect { create(:user, email: user.email) }
.to raise_error(ActiveRecord::RecordInvalid, error_message) .to raise_error(ActiveRecord::RecordInvalid, error_message)
end end
end end
end end
describe "delete" do describe "delete" do
let(:user) { FactoryBot.create(:user) } let(:user) { create(:user) }
before do before do
FactoryBot.create( create(
:lettings_log, :lettings_log,
:completed, :completed,
owning_organisation: user.organisation, owning_organisation: user.organisation,
@ -309,7 +309,7 @@ RSpec.describe User, type: :model do
created_by: user, created_by: user,
) )
FactoryBot.create( create(
:sales_log, :sales_log,
owning_organisation: user.organisation, owning_organisation: user.organisation,
created_by: user, created_by: user,
@ -319,13 +319,13 @@ RSpec.describe User, type: :model do
context "when the user is deleted" do context "when the user is deleted" do
it "owned lettings logs are not deleted as a result" do it "owned lettings logs are not deleted as a result" do
expect { user.destroy! } expect { user.destroy! }
.to change(described_class, :count).from(1).to(0) .to change(described_class, :count).by(-1)
.and change(LettingsLog, :count).by(0) .and change(LettingsLog, :count).by(0)
end end
it "owned sales logs are not deleted as a result" do it "owned sales logs are not deleted as a result" do
expect { user.destroy! } expect { user.destroy! }
.to change(described_class, :count).from(1).to(0) .to change(described_class, :count).by(-1)
.and change(SalesLog, :count).by(0) .and change(SalesLog, :count).by(0)
end end
end end

20
spec/requests/users_controller_spec.rb

@ -363,7 +363,7 @@ RSpec.describe UsersController, type: :request do
end end
context "when user is signed in as a data coordinator" do context "when user is signed in as a data coordinator" do
let(:user) { FactoryBot.create(:user, :data_coordinator, email: "coordinator@example.com") } let(:user) { FactoryBot.create(:user, :data_coordinator, email: "coordinator@example.com", organisation: create(:organisation, :without_dpc)) }
let!(:other_user) { FactoryBot.create(:user, organisation: user.organisation, name: "filter name", email: "filter@example.com") } let!(:other_user) { FactoryBot.create(:user, organisation: user.organisation, name: "filter name", email: "filter@example.com") }
describe "#index" do describe "#index" do
@ -969,7 +969,7 @@ RSpec.describe UsersController, type: :request do
end end
context "when user is signed in as a support user" do context "when user is signed in as a support user" do
let(:user) { FactoryBot.create(:user, :support) } let(:user) { FactoryBot.create(:user, :support, organisation: create(:organisation, :without_dpc)) }
let(:other_user) { FactoryBot.create(:user, organisation: user.organisation) } let(:other_user) { FactoryBot.create(:user, organisation: user.organisation) }
before do before do
@ -979,7 +979,7 @@ RSpec.describe UsersController, type: :request do
describe "#index" do describe "#index" do
let!(:other_user) { FactoryBot.create(:user, organisation: user.organisation, name: "User 2", email: "other@example.com") } let!(:other_user) { FactoryBot.create(:user, organisation: user.organisation, name: "User 2", email: "other@example.com") }
let!(:inactive_user) { FactoryBot.create(:user, organisation: user.organisation, active: false, name: "User 3", email: "inactive@example.com") } let!(:inactive_user) { FactoryBot.create(:user, organisation: user.organisation, active: false, name: "User 3", email: "inactive@example.com") }
let!(:other_org_user) { FactoryBot.create(:user, name: "User 4", email: "otherorg@otherexample.com") } let!(:other_org_user) { FactoryBot.create(:user, name: "User 4", email: "otherorg@otherexample.com", organisation: create(:organisation, :without_dpc)) }
before do before do
sign_in user sign_in user
@ -1058,7 +1058,7 @@ RSpec.describe UsersController, type: :request do
context "when our search term matches an email and a name" do context "when our search term matches an email and a name" do
let!(:other_user) { FactoryBot.create(:user, organisation: user.organisation, name: "joe", email: "other@example.com") } let!(:other_user) { FactoryBot.create(:user, organisation: user.organisation, name: "joe", email: "other@example.com") }
let!(:other_org_user) { FactoryBot.create(:user, name: "User 4", email: "joe@otherexample.com") } let!(:other_org_user) { FactoryBot.create(:user, name: "User 4", email: "joe@otherexample.com", organisation: create(:organisation, :without_dpc)) }
let(:search_param) { "joe" } let(:search_param) { "joe" }
it "returns any results including joe" do it "returns any results including joe" do
@ -1090,15 +1090,19 @@ RSpec.describe UsersController, type: :request do
get "/users", headers:, params: {} get "/users", headers:, params: {}
end end
let(:byte_order_mark) { "\uFEFF" }
it "downloads a CSV file with headers" do it "downloads a CSV file with headers" do
csv = CSV.parse(response.body) csv = CSV.parse(response.body)
expect(csv.first.second).to eq("email")
expect(csv.second.first).to eq(user.id.to_s) expect(csv.first.to_csv).to eq(
"#{byte_order_mark}id,email,name,organisation_name,role,old_user_id,is_dpo,is_key_contact,active,sign_in_count,last_sign_in_at\n",
)
end end
it "downloads all users" do it "downloads all users" do
csv = CSV.parse(response.body) csv = CSV.parse(response.body)
expect(csv.count).to eq(27) expect(csv.count).to eq(User.all.count + 1) # +1 for the headers
end end
it "downloads organisation names rather than ids" do it "downloads organisation names rather than ids" do
@ -1517,7 +1521,7 @@ RSpec.describe UsersController, type: :request do
end end
describe "#create" do describe "#create" do
let(:organisation) { FactoryBot.create(:organisation) } let(:organisation) { FactoryBot.create(:organisation, :without_dpc) }
let(:email) { "new_user@example.com" } let(:email) { "new_user@example.com" }
let(:params) do let(:params) do
{ {

10
spec/services/imports/data_protection_confirmation_import_service_spec.rb

@ -29,7 +29,7 @@ RSpec.describe Imports::DataProtectionConfirmationImportService do
end end
context "when the organisation does exist" do context "when the organisation does exist" do
let!(:organisation) { FactoryBot.create(:organisation, old_org_id:) } let!(:organisation) { create(:organisation, :without_dpc, old_org_id:) }
context "when a data protection officer with matching name does not exists for the organisation" do context "when a data protection officer with matching name does not exists for the organisation" do
it "creates a data protection officer without sign in credentials" do it "creates a data protection officer without sign in credentials" do
@ -41,7 +41,7 @@ RSpec.describe Imports::DataProtectionConfirmationImportService do
it "successfully create a data protection confirmation record with the expected data" do it "successfully create a data protection confirmation record with the expected data" do
import_service.create_data_protection_confirmations("data_protection_directory") import_service.create_data_protection_confirmations("data_protection_directory")
confirmation = Organisation.find_by(old_org_id:).data_protection_confirmations.last confirmation = Organisation.find_by(old_org_id:).data_protection_confirmation
expect(confirmation.data_protection_officer.name).to eq("John Doe") expect(confirmation.data_protection_officer.name).to eq("John Doe")
expect(confirmation.confirmed).to be_truthy expect(confirmation.confirmed).to be_truthy
expect(Time.zone.local_to_utc(confirmation.created_at)).to eq(Time.utc(2018, 0o6, 0o5, 10, 36, 49)) expect(Time.zone.local_to_utc(confirmation.created_at)).to eq(Time.utc(2018, 0o6, 0o5, 10, 36, 49))
@ -50,13 +50,13 @@ RSpec.describe Imports::DataProtectionConfirmationImportService do
context "when a data protection officer with matching name already exists for the organisation" do context "when a data protection officer with matching name already exists for the organisation" do
let!(:data_protection_officer) do let!(:data_protection_officer) do
FactoryBot.create(:user, :data_protection_officer, name: "John Doe", organisation:) create(:user, :data_protection_officer, name: "John Doe", organisation:)
end end
it "successfully creates a data protection confirmation record with the expected data" do it "successfully creates a data protection confirmation record with the expected data" do
import_service.create_data_protection_confirmations("data_protection_directory") import_service.create_data_protection_confirmations("data_protection_directory")
confirmation = Organisation.find_by(old_org_id:).data_protection_confirmations.last confirmation = Organisation.find_by(old_org_id:).data_protection_confirmation
expect(confirmation.data_protection_officer.id).to eq(data_protection_officer.id) expect(confirmation.data_protection_officer.id).to eq(data_protection_officer.id)
expect(confirmation.confirmed).to be_truthy expect(confirmation.confirmed).to be_truthy
expect(Time.zone.local_to_utc(confirmation.created_at)).to eq(Time.utc(2018, 0o6, 0o5, 10, 36, 49)) expect(Time.zone.local_to_utc(confirmation.created_at)).to eq(Time.utc(2018, 0o6, 0o5, 10, 36, 49))
@ -64,7 +64,7 @@ RSpec.describe Imports::DataProtectionConfirmationImportService do
context "when the data protection record has already been imported previously" do context "when the data protection record has already been imported previously" do
before do before do
FactoryBot.create( create(
:data_protection_confirmation, :data_protection_confirmation,
organisation:, organisation:,
data_protection_officer:, data_protection_officer:,

Loading…
Cancel
Save