Browse Source

Implement storage folder check

pull/821/head
Stéphane Meny 3 years ago
parent
commit
d7c2ff5490
No known key found for this signature in database
GPG Key ID: 9D0AFEA988527923
  1. 22
      app/services/imports/core_import_service.rb
  2. 5
      app/services/storage_service.rb
  3. 67
      spec/services/storage_service_spec.rb

22
app/services/imports/core_import_service.rb

@ -0,0 +1,22 @@
module Imports
class CoreImportService < ImportService
def start_import
end
private
ARCHIVE_FOLDERS = {
organisation: "institution",
scheme: "mgmtgroups",
scheme_location: "schemes",
user: "user",
data_protection_confirmation: "dataprotect",
organisation_rent_periods: "rent-period",
case_log: "logs"
}.freeze
end
end

5
app/services/storage_service.rb

@ -13,6 +13,11 @@ class StorageService
.flat_map { |response| response.contents.map(&:key) }
end
def folder_present?(folder)
response = @client.list_objects_v2(bucket: @configuration.bucket_name, prefix: folder, max_keys: 1)
response.key_count == 1
end
def get_file_io(file_name)
@client.get_object(bucket: @configuration.bucket_name, key: file_name)
.body

67
spec/services/storage_service_spec.rb

@ -99,7 +99,7 @@ RSpec.describe StorageService do
end
end
context "when we create a storage service and list files" do
context "when we create a storage service" do
subject(:storage_service) { described_class.new(PaasConfigurationService.new, instance_name) }
let(:s3_client_stub) { Aws::S3::Client.new(stub_responses: true) }
@ -110,22 +110,59 @@ RSpec.describe StorageService do
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],
},
],
})
context "and we list files based on a prefix" do
let(:expected_filenames) { %w[my_folder/my_file1.xml my_folder/my_file2.xml] }
before do
s3_client_stub.stub_responses(:list_objects_v2, {
contents: [
{
key: expected_filenames[0],
},
{
key: expected_filenames[1],
},
],
})
end
it "returns a list with all present file names in a given folder" do
filenames = storage_service.list_files("my_folder")
expect(filenames).to eq(expected_filenames)
end
end
filenames = storage_service.list_files("my_folder")
context "and we check for an existing folder" do
before do
expected_filenames = %w[my_folder/my_file1.xml]
s3_client_stub.stub_responses(:list_objects_v2, {
key_count: 1,
contents: [
{
key: expected_filenames[0],
},
],
})
end
it "returns true" do
response = storage_service.folder_present?("my_folder")
expect(response).to be_truthy
end
end
expect(filenames).to eq(expected_filenames)
context "and we check for a folder that does not exists" do
before do
s3_client_stub.stub_responses(:list_objects_v2, {
key_count: 0,
contents: [],
})
end
it "returns false" do
response = storage_service.folder_present?("my_folder")
expect(response).to be_falsey
end
end
end
end

Loading…
Cancel
Save