Browse Source

Organisation import first draft

pull/267/head
Stéphane Meny 3 years ago
parent
commit
ba93400e6b
No known key found for this signature in database
GPG Key ID: 9D0AFEA988527923
  1. 5
      app/services/import_service.rb
  2. 8
      app/services/storage_service.rb
  3. 17
      spec/services/import_service_spec.rb
  4. 38
      spec/services/storage_service_spec.rb

5
app/services/import_service.rb

@ -0,0 +1,5 @@
class ImportService
def initialize(storage_service)
@storage_service = storage_service
end
end

8
app/services/storage_service.rb

@ -8,6 +8,14 @@ class StorageService
@client = create_client
end
def list_files(folder)
file_list = []
@client.list_objects_v2(bucket: @configuration.bucket_name, prefix: folder).each do |response|
file_list += response.contents.map(&:key)
end
file_list
end
def get_file_io(file_name)
file_response =
@client.get_object(bucket: @configuration.bucket_name, key: file_name)

17
spec/services/import_service_spec.rb

@ -0,0 +1,17 @@
require "rspec"
describe ImportService do
let(:storage_service) { instance_double(StorageService) }
context "when importing organisations" do
subject(:import_service) { described_class.new(storage_service) }
it "successfully create a new organisation if it does not exist" do
import_service.update_organisations()
# 1. call import with folder name
# 2. check that it calls storage lists files
# 3. check that it calls read file on each files
# 4. create a temporary organisation object
# 5. update/insert (upsert) organisation (settings?)
end
end
end

38
spec/services/storage_service_spec.rb

@ -19,7 +19,7 @@ RSpec.describe StorageService do
JSON
end
context "when we create an S3 Service with no PaaS Configuration present" do
context "when we create a storage service with no PaaS Configuration present" do
subject(:storage_service) { described_class.new(PaasConfigurationService.new, "random_instance") }
it "raises an exception" do
@ -27,7 +27,7 @@ RSpec.describe StorageService do
end
end
context "when we create an S3 Service with an unknown instance name" do
context "when we create a storage service with an unknown instance name" do
subject(:storage_service) { described_class.new(PaasConfigurationService.new, "random_instance") }
before do
@ -39,7 +39,7 @@ RSpec.describe StorageService do
end
end
context "when we create an storage service with a valid instance name" do
context "when we create a storage service with a valid instance name" do
subject(:storage_service) { described_class.new(PaasConfigurationService.new, instance_name) }
before do
@ -64,7 +64,7 @@ RSpec.describe StorageService do
end
end
context "when we create an storage service and write a stubbed object" do
context "when we create a storage service and write a stubbed object" do
subject(:storage_service) { described_class.new(PaasConfigurationService.new, instance_name) }
let(:filename) { "my_file" }
@ -98,4 +98,34 @@ RSpec.describe StorageService do
storage_service.write_file(filename, content)
end
end
context "when we create a storage service and list files" do
subject(:storage_service) { described_class.new(PaasConfigurationService.new, instance_name) }
let(:s3_client_stub) { Aws::S3::Client.new(stub_responses: true) }
before do
allow(ENV).to receive(:[])
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services)
allow(Aws::S3::Client).to receive(:new).and_return(s3_client_stub)
end
it "returns a list with all present file names in a given folder" do
expected_filenames = %w[my_folder/my_file1.xml my_folder/my_file2.xml]
s3_client_stub.stub_responses(:list_objects_v2, {
contents: [
{
key: expected_filenames[0],
},
{
key: expected_filenames[1],
},
],
})
filenames = storage_service.list_files("my_folder")
expect(filenames).to eq(expected_filenames)
end
end
end

Loading…
Cancel
Save