Browse Source

Import

pull/412/head
baarkerlounger 3 years ago
parent
commit
07c02edf90
  1. 1
      app/models/user.rb
  2. 36
      app/services/imports/data_protection_confirmation_import_service.rb
  3. 7
      db/migrate/20220323094418_create_data_protection_confirmation.rb
  4. 12
      spec/factories/data_protection_confirmation.rb
  5. 3
      spec/factories/user.rb
  6. 32
      spec/services/imports/data_protection_confirmation_service_spec.rb

1
app/models/user.rb

@ -14,6 +14,7 @@ class User < ApplicationRecord
data_accessor: 0,
data_provider: 1,
data_coordinator: 2,
data_protection_officer: 3
}.freeze
enum role: ROLES

36
app/services/imports/data_protection_confirmation_import_service.rb

@ -0,0 +1,36 @@
module Imports
class DataProtectionConfirmationImportService < ImportService
def create_data_protection_confirmations(folder)
import_from(folder, :create_data_protection_confirmation)
end
private
def create_data_protection_confirmation(xml_document)
org = Organisation.find_by(old_org_id: record_field_value(xml_document, "institution"))
dp_officer = User.find_or_create_by(
name: record_field_value(xml_document, "dp-user"),
organisation: org,
role: "data_protection_officer",
)
dp_officer.encrypted_password = SecureRandom.hex(10)
dp_officer.save(validate: false)
DataProtectionConfirmation.create!(
organisation: org,
confirmed: !!record_field_value(xml_document, "data-protection"),
data_protection_officer: dp_officer,
old_id: record_field_value(xml_document, "id"),
old_org_id: record_field_value(xml_document, "institution"),
)
rescue ActiveRecord::RecordNotUnique
id = record_field_value(xml_document, "id")
dp_officer_name = record_field_value(xml_document, "dp-user")
@logger.warn("Data protection confirmation #{id} created by #{dp_officer_name} for #{org.name} is already present, skipping.")
end
def record_field_value(xml_document, field)
field_value(xml_document, "dataprotect", field)
end
end
end

7
db/migrate/20220323094418_create_data_protection_confirmation.rb

@ -4,8 +4,15 @@ class CreateDataProtectionConfirmation < ActiveRecord::Migration[7.0]
t.belongs_to :organisation
t.belongs_to :data_protection_officer, class_name: "User", index: { name: :dpo_user_id }
t.column :confirmed, :boolean
t.column :old_id, :string
t.column :old_org_id, :string
t.timestamps
end
add_index :data_protection_confirmations,
[:organisation_id, :data_protection_officer_id, :confirmed],
unique: true,
name: "data_protection_confirmations_unique"
end
end

12
spec/factories/data_protection_confirmation.rb

@ -0,0 +1,12 @@
FactoryBot.define do
factory :data_protection_confirmation do
organisation
data_protection_officer { FactoryBot.create(:user, :data_protection_officer) }
confirmed { true }
old_org_id { "7c5bd5fb549c09a2c55d7cb90d7ba84927e64618" }
old_id { "7c5bd5fb549c09a2c55d7cb90d7ba84927e64618" }
created_at { Time.zone.now }
updated_at { Time.zone.now }
end
end

3
spec/factories/user.rb

@ -8,6 +8,9 @@ FactoryBot.define do
trait :data_coordinator do
role { "data_coordinator" }
end
trait :data_protection_officer do
role { "data_protection_officer" }
end
created_at { Time.zone.now }
updated_at { Time.zone.now }
end

32
spec/services/imports/data_protection_confirmation_service_spec.rb

@ -1,22 +1,22 @@
require "rails_helper"
RSpec.describe Imports::UserImportService do
let(:fixture_directory) { "spec/fixtures/softwire_imports/users" }
RSpec.describe Imports::DataProtectionConfirmationImportService do
let(:fixture_directory) { "spec/fixtures/softwire_imports/data_protection_confirmations" }
let(:old_org_id) { "7c5bd5fb549c09a2c55d7cb90d7ba84927e64618" }
let(:old_data_protection_confirmation_id) { old_org_id }
let(:import_file) { File.open("#{fixture_directory}/#{old_data_protection_confirmation_id}.xml") }
let(:old_id) { old_org_id }
let(:import_file) { File.open("#{fixture_directory}/#{old_id}.xml") }
let(:storage_service) { instance_double(StorageService) }
context "when importing users" do
context "when importing data protection confirmations" do
subject(:import_service) { described_class.new(storage_service) }
before do
allow(storage_service)
.to receive(:list_files)
.and_return(["data_protection_directory/#{old_data_protection_confirmation_id}.xml"])
.and_return(["data_protection_directory/#{old_id}.xml"])
allow(storage_service)
.to receive(:get_file_io)
.with("user_directory/#{old_data_protection_confirmation_id}.xml")
.with("data_protection_directory/#{old_id}.xml")
.and_return(import_file)
end
@ -24,8 +24,8 @@ RSpec.describe Imports::UserImportService do
FactoryBot.create(:organisation, old_org_id:)
import_service.create_data_protection_confirmations("data_protection_directory")
confirmation = Organisation.find_by(old_user_id:).data_protection_confirmations.last
expect(confirmation.user.name).to eq("John Doe")
confirmation = Organisation.find_by(old_org_id:).data_protection_confirmations.last
expect(confirmation.data_protection_officer.name).to eq("John Doe")
expect(confirmation.confirmed).to be_truthy
end
@ -35,11 +35,19 @@ RSpec.describe Imports::UserImportService do
end
context "when the data protection record has already been imported previously" do
before do
org = FactoryBot.create(:organisation, old_org_id:)
let(:organisation) { FactoryBot.create(:organisation, old_org_id:) }
let(:data_protection_officer) { FactoryBot.create(:user, :data_protection_officer, name: "John Doe", organisation:) }
let!(:data_protection_confirmation) do
FactoryBot.create(
:data_protection_confirmation,
organisation:,
data_protection_officer:,
old_org_id:,
old_id:
)
end
it "logs that the user already exists" do
it "logs that the record already exists" do
expect(Rails.logger).to receive(:warn)
import_service.create_data_protection_confirmations("data_protection_directory")
end

Loading…
Cancel
Save