Browse Source

Fix check that imported users are unique (#335)

* Match imported users on id and org

* Rubocop
pull/337/head
baarkerlounger 3 years ago committed by GitHub
parent
commit
78345e7cb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      app/services/imports/user_import_service.rb
  2. 26
      spec/services/imports/user_import_service_spec.rb

26
app/services/imports/user_import_service.rb

@ -12,17 +12,21 @@ module Imports
def create_user(xml_document) def create_user(xml_document)
organisation = Organisation.find_by(old_org_id: user_field_value(xml_document, "institution")) organisation = Organisation.find_by(old_org_id: user_field_value(xml_document, "institution"))
User.create!( old_user_id = user_field_value(xml_document, "id")
email: user_field_value(xml_document, "user-name"), name = user_field_value(xml_document, "full-name")
name: user_field_value(xml_document, "full-name"), if User.find_by(old_user_id:, organisation:)
password: Devise.friendly_token, @logger.warn("User #{name} with old user id #{old_user_id} is already present, skipping.")
phone: user_field_value(xml_document, "telephone-no"), else
old_user_id: user_field_value(xml_document, "id"), User.create!(
organisation:, email: user_field_value(xml_document, "user-name"),
role: PROVIDER_TYPE[user_field_value(xml_document, "user-type")], name:,
) password: Devise.friendly_token,
rescue ActiveRecord::RecordNotUnique phone: user_field_value(xml_document, "telephone-no"),
@logger.warn("User #{name} with old user id #{old_user_id} is already present, skipping.") old_user_id:,
organisation:,
role: PROVIDER_TYPE[user_field_value(xml_document, "user-type")],
)
end
end end
def user_field_value(xml_document, field) def user_field_value(xml_document, field)

26
spec/services/imports/user_import_service_spec.rb

@ -2,7 +2,9 @@ require "rails_helper"
RSpec.describe Imports::UserImportService do RSpec.describe Imports::UserImportService do
let(:fixture_directory) { "spec/fixtures/softwire_imports/users" } let(:fixture_directory) { "spec/fixtures/softwire_imports/users" }
let(:user_file) { File.open("#{fixture_directory}/fc7625a02b24ae16162aa63ae7cb33feeec0c373.xml") } let(:old_user_id) { "fc7625a02b24ae16162aa63ae7cb33feeec0c373" }
let(:old_org_id) { "7c5bd5fb549c09a2c55d7cb90d7ba84927e64618" }
let(:user_file) { File.open("#{fixture_directory}/#{old_user_id}.xml") }
let(:storage_service) { instance_double(StorageService) } let(:storage_service) { instance_double(StorageService) }
context "when importing users" do context "when importing users" do
@ -10,28 +12,40 @@ RSpec.describe Imports::UserImportService do
before do before do
allow(storage_service).to receive(:list_files) allow(storage_service).to receive(:list_files)
.and_return(["user_directory/fc7625a02b24ae16162aa63ae7cb33feeec0c373.xml"]) .and_return(["user_directory/#{old_user_id}.xml"])
allow(storage_service).to receive(:get_file_io) allow(storage_service).to receive(:get_file_io)
.with("user_directory/fc7625a02b24ae16162aa63ae7cb33feeec0c373.xml") .with("user_directory/#{old_user_id}.xml")
.and_return(user_file) .and_return(user_file)
end end
it "successfully create a user with the expected data" do it "successfully create a user with the expected data" do
FactoryBot.create(:organisation, old_org_id: "7c5bd5fb549c09a2c55d7cb90d7ba84927e64618") FactoryBot.create(:organisation, old_org_id:)
import_service.create_users("user_directory") import_service.create_users("user_directory")
user = User.find_by(old_user_id: "fc7625a02b24ae16162aa63ae7cb33feeec0c373") user = User.find_by(old_user_id:)
expect(user.name).to eq("John Doe") expect(user.name).to eq("John Doe")
expect(user.email).to eq("john.doe@gov.uk") expect(user.email).to eq("john.doe@gov.uk")
expect(user.encrypted_password).not_to be_nil expect(user.encrypted_password).not_to be_nil
expect(user.phone).to eq("02012345678") expect(user.phone).to eq("02012345678")
expect(user).to be_data_provider expect(user).to be_data_provider
expect(user.organisation.old_org_id).to eq("7c5bd5fb549c09a2c55d7cb90d7ba84927e64618") expect(user.organisation.old_org_id).to eq(old_org_id)
end end
it "refuses to create a user belonging to a non existing organisation" do it "refuses to create a user belonging to a non existing organisation" do
expect { import_service.create_users("user_directory") } expect { import_service.create_users("user_directory") }
.to raise_error(ActiveRecord::RecordInvalid, /Organisation must exist/) .to raise_error(ActiveRecord::RecordInvalid, /Organisation must exist/)
end end
context "when the user has already been imported previously" do
before do
org = FactoryBot.create(:organisation, old_org_id:)
FactoryBot.create(:user, old_user_id:, organisation: org)
end
it "logs that the user already exists" do
expect(Rails.logger).to receive(:warn)
import_service.create_users("user_directory")
end
end
end end
end end

Loading…
Cancel
Save