Browse Source

CLDC-2537 Remove GOV.UK PaaS code (#2093)

* Remove paas configuration service

* Remove redundant elsif
pull/2112/head
kosiakkatrina 12 months ago committed by GitHub
parent
commit
ea957d38ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      app/helpers/platform_helper.rb
  2. 2
      app/jobs/data_export_xml_job.rb
  3. 5
      app/models/forms/bulk_upload_lettings/upload_your_file.rb
  4. 5
      app/models/forms/bulk_upload_sales/upload_your_file.rb
  5. 5
      app/services/bulk_upload/downloader.rb
  6. 23
      app/services/configuration/paas_configuration_service.rb
  7. 15
      app/services/storage/s3_service.rb
  8. 9
      config/initializers/rack_attack.rb
  9. 4
      config/initializers/sidekiq.rb
  10. 2
      lib/tasks/correct_illness_from_csv.rake
  11. 2
      lib/tasks/data_export.rake
  12. 4
      lib/tasks/import_address_from_csv.rake
  13. 15
      spec/helpers/platform_helper_spec.rb
  14. 4
      spec/jobs/data_export_xml_job_spec.rb
  15. 7
      spec/lib/tasks/correct_address_from_csv_spec.rb
  16. 4
      spec/lib/tasks/correct_illness_from_csv_spec.rb
  17. 6
      spec/lib/tasks/data_export_spec.rb
  18. 149
      spec/services/configuration/paas_configuration_service_spec.rb
  19. 57
      spec/services/storage/s3_service_spec.rb

5
app/helpers/platform_helper.rb

@ -1,5 +0,0 @@
module PlatformHelper
def self.is_paas?
!ENV["VCAP_SERVICES"].nil?
end
end

2
app/jobs/data_export_xml_job.rb

@ -2,7 +2,7 @@ class DataExportXmlJob < ApplicationJob
queue_as :default queue_as :default
def perform(full_update: false) def perform(full_update: false)
storage_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["EXPORT_PAAS_INSTANCE"]) storage_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["EXPORT_PAAS_INSTANCE"])
export_service = Exports::LettingsLogExportService.new(storage_service) export_service = Exports::LettingsLogExportService.new(storage_service)
export_service.export_xml_lettings_logs(full_update:) export_service.export_xml_lettings_logs(full_update:)

5
app/models/forms/bulk_upload_lettings/upload_your_file.rb

@ -56,10 +56,7 @@ module Forms
def storage_service def storage_service
@storage_service ||= if upload_enabled? @storage_service ||= if upload_enabled?
Storage::S3Service.new( Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["CSV_DOWNLOAD_PAAS_INSTANCE"])
PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new,
ENV["CSV_DOWNLOAD_PAAS_INSTANCE"],
)
else else
Storage::LocalDiskService.new Storage::LocalDiskService.new
end end

5
app/models/forms/bulk_upload_sales/upload_your_file.rb

@ -49,10 +49,7 @@ module Forms
def storage_service def storage_service
@storage_service ||= if FeatureToggle.upload_enabled? @storage_service ||= if FeatureToggle.upload_enabled?
Storage::S3Service.new( Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["CSV_DOWNLOAD_PAAS_INSTANCE"])
PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new,
ENV["CSV_DOWNLOAD_PAAS_INSTANCE"],
)
else else
Storage::LocalDiskService.new Storage::LocalDiskService.new
end end

5
app/services/bulk_upload/downloader.rb

@ -37,10 +37,7 @@ private
end end
def s3_storage_service def s3_storage_service
Storage::S3Service.new( Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["CSV_DOWNLOAD_PAAS_INSTANCE"])
PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new,
ENV["CSV_DOWNLOAD_PAAS_INSTANCE"],
)
end end
def local_disk_storage_service def local_disk_storage_service

23
app/services/configuration/paas_configuration_service.rb

@ -1,23 +0,0 @@
module Configuration
class PaasConfigurationService < ConfigurationService
private
def config_present?
!ENV["VCAP_SERVICES"].nil?
end
def read_config
unless config_present?
@logger.warn("Could not find VCAP_SERVICES in the environment variables!")
return {}
end
begin
JSON.parse(ENV["VCAP_SERVICES"], { symbolize_names: true })
rescue StandardError
@logger.warn("Could not parse VCAP_SERVICES!")
{}
end
end
end
end

15
app/services/storage/s3_service.rb

@ -2,10 +2,10 @@ module Storage
class S3Service < StorageService class S3Service < StorageService
attr_reader :configuration attr_reader :configuration
def initialize(config_service, paas_instance_name) def initialize(config_service, instance_name)
super() super()
@config_service = config_service @config_service = config_service
@instance_name = (paas_instance_name || "").to_sym @instance_name = (instance_name || "").to_sym
@configuration = create_configuration @configuration = create_configuration
@client = create_client @client = create_client
end end
@ -43,7 +43,7 @@ module Storage
def create_configuration def create_configuration
unless @config_service.s3_config_present? unless @config_service.s3_config_present?
raise "No S3 bucket is present in the PaaS configuration" raise "No S3 bucket is present in the configuration"
end end
unless @config_service.s3_buckets.key?(@instance_name) unless @config_service.s3_buckets.key?(@instance_name)
raise "#{@instance_name} instance name could not be found" raise "#{@instance_name} instance name could not be found"
@ -54,14 +54,7 @@ module Storage
end end
def create_client def create_client
credentials = if PlatformHelper.is_paas? credentials = Aws::ECSCredentials.new
Aws::Credentials.new(
@configuration.access_key_id,
@configuration.secret_access_key,
)
else
Aws::ECSCredentials.new
end
Aws::S3::Client.new( Aws::S3::Client.new(
region: @configuration.region, region: @configuration.region,
credentials:, credentials:,

9
config/initializers/rack_attack.rb

@ -1,18 +1,13 @@
require "configuration/configuration_service" require "configuration/configuration_service"
require "configuration/paas_configuration_service"
require "configuration/env_configuration_service" require "configuration/env_configuration_service"
require Rails.root.join("app/helpers/platform_helper")
configuration_service = PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new configuration_service = Configuration::EnvConfigurationService.new
if Rails.env.development? || Rails.env.test? if Rails.env.development? || Rails.env.test?
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
Rack::Attack.enabled = false Rack::Attack.enabled = false
elsif Rails.env.review?
redis_url = configuration_service.redis_uris.to_a[0][1]
Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new(url: redis_url)
else else
redis_url = PlatformHelper.is_paas? ? configuration_service.redis_uris[:"dluhc-core-#{Rails.env}-redis"] : configuration_service.redis_uris.to_a[0][1] redis_url = configuration_service.redis_uris.to_a[0][1]
Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new(url: redis_url) Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new(url: redis_url)
end end

4
config/initializers/sidekiq.rb

@ -1,10 +1,10 @@
require "sidekiq/web" require "sidekiq/web"
require "sidekiq/cron/web" require "sidekiq/cron/web"
configuration_service = PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new configuration_service = Configuration::EnvConfigurationService.new
if Rails.env.staging? || Rails.env.production? if Rails.env.staging? || Rails.env.production?
redis_url = PlatformHelper.is_paas? ? configuration_service.redis_uris[:"dluhc-core-#{Rails.env}-redis"] : configuration_service.redis_uris.to_a[0][1] redis_url = configuration_service.redis_uris.to_a[0][1]
Sidekiq.configure_server do |config| Sidekiq.configure_server do |config|
config.redis = { url: redis_url } config.redis = { url: redis_url }

2
lib/tasks/correct_illness_from_csv.rake

@ -19,7 +19,7 @@ namespace :correct_illness do
raise "Usage: rake correct_illness:correct_illness_from_csv['csv_file_name']" if file_name.blank? raise "Usage: rake correct_illness:correct_illness_from_csv['csv_file_name']" if file_name.blank?
s3_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) s3_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"])
file_io = s3_service.get_file_io(file_name) file_io = s3_service.get_file_io(file_name)
file_io.set_encoding_by_bom file_io.set_encoding_by_bom
illness_csv = CSV.parse(file_io, headers: false) illness_csv = CSV.parse(file_io, headers: false)

2
lib/tasks/data_export.rake

@ -9,7 +9,7 @@ namespace :core do
desc "Export all data XMLs for import into Central Data System (CDS)" desc "Export all data XMLs for import into Central Data System (CDS)"
task :full_data_export_xml, %i[year] => :environment do |_task, args| task :full_data_export_xml, %i[year] => :environment do |_task, args|
collection_year = args[:year].present? ? args[:year].to_i : nil collection_year = args[:year].present? ? args[:year].to_i : nil
storage_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["EXPORT_PAAS_INSTANCE"]) storage_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["EXPORT_PAAS_INSTANCE"])
export_service = Exports::LettingsLogExportService.new(storage_service) export_service = Exports::LettingsLogExportService.new(storage_service)
export_service.export_xml_lettings_logs(full_update: true, collection_year:) export_service.export_xml_lettings_logs(full_update: true, collection_year:)

4
lib/tasks/import_address_from_csv.rake

@ -5,7 +5,7 @@ namespace :data_import do
raise "Usage: rake data_import:import_lettings_addresses_from_csv['csv_file_name']" if file_name.blank? raise "Usage: rake data_import:import_lettings_addresses_from_csv['csv_file_name']" if file_name.blank?
s3_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) s3_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"])
file_io = s3_service.get_file_io(file_name) file_io = s3_service.get_file_io(file_name)
file_io.set_encoding_by_bom file_io.set_encoding_by_bom
addresses_csv = CSV.parse(file_io, headers: true) addresses_csv = CSV.parse(file_io, headers: true)
@ -69,7 +69,7 @@ namespace :data_import do
raise "Usage: rake data_import:import_sales_addresses_from_csv['csv_file_name']" if file_name.blank? raise "Usage: rake data_import:import_sales_addresses_from_csv['csv_file_name']" if file_name.blank?
s3_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) s3_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"])
file_io = s3_service.get_file_io(file_name) file_io = s3_service.get_file_io(file_name)
file_io.set_encoding_by_bom file_io.set_encoding_by_bom
addresses_csv = CSV.parse(file_io, headers: true) addresses_csv = CSV.parse(file_io, headers: true)

15
spec/helpers/platform_helper_spec.rb

@ -1,15 +0,0 @@
require "rails_helper"
RSpec.describe PlatformHelper do
describe "is_paas?" do
it "returns true if the VCAP_SERVICES environment variable exists" do
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return("dummy")
expect(described_class.is_paas?).to eq(true)
end
it "returns false if the VCAP_SERVICES environment variable doesn't exist" do
allow(ENV).to receive(:[]).with("VCAP_SERVICES")
expect(described_class.is_paas?).to eq(false)
end
end
end

4
spec/jobs/data_export_xml_job_spec.rb

@ -2,12 +2,12 @@ require "rails_helper"
describe DataExportXmlJob do describe DataExportXmlJob do
let(:storage_service) { instance_double(Storage::S3Service) } let(:storage_service) { instance_double(Storage::S3Service) }
let(:paas_config_service) { instance_double(Configuration::PaasConfigurationService) } let(:env_config_service) { instance_double(Configuration::EnvConfigurationService) }
let(:export_service) { instance_double(Exports::LettingsLogExportService) } let(:export_service) { instance_double(Exports::LettingsLogExportService) }
before do before do
allow(Storage::S3Service).to receive(:new).and_return(storage_service) allow(Storage::S3Service).to receive(:new).and_return(storage_service)
allow(Configuration::PaasConfigurationService).to receive(:new).and_return(paas_config_service) allow(Configuration::EnvConfigurationService).to receive(:new).and_return(env_config_service)
allow(Exports::LettingsLogExportService).to receive(:new).and_return(export_service) allow(Exports::LettingsLogExportService).to receive(:new).and_return(export_service)
end end

7
spec/lib/tasks/correct_address_from_csv_spec.rb

@ -12,7 +12,6 @@ RSpec.describe "data_import" do
before do before do
allow(Storage::S3Service).to receive(:new).and_return(storage_service) allow(Storage::S3Service).to receive(:new).and_return(storage_service)
allow(Configuration::EnvConfigurationService).to receive(:new).and_return(env_config_service) allow(Configuration::EnvConfigurationService).to receive(:new).and_return(env_config_service)
allow(Configuration::PaasConfigurationService).to receive(:new).and_return(paas_config_service)
allow(ENV).to receive(:[]) allow(ENV).to receive(:[])
allow(ENV).to receive(:[]).with("IMPORT_PAAS_INSTANCE").and_return(instance_name) allow(ENV).to receive(:[]).with("IMPORT_PAAS_INSTANCE").and_return(instance_name)
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return("dummy") allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return("dummy")
@ -26,10 +25,9 @@ RSpec.describe "data_import" do
describe ":import_lettings_addresses_from_csv", type: :task do describe ":import_lettings_addresses_from_csv", type: :task do
subject(:task) { Rake::Task["data_import:import_lettings_addresses_from_csv"] } subject(:task) { Rake::Task["data_import:import_lettings_addresses_from_csv"] }
let(:instance_name) { "paas_import_instance" } let(:instance_name) { "import_instance" }
let(:storage_service) { instance_double(Storage::S3Service) } let(:storage_service) { instance_double(Storage::S3Service) }
let(:env_config_service) { instance_double(Configuration::EnvConfigurationService) } let(:env_config_service) { instance_double(Configuration::EnvConfigurationService) }
let(:paas_config_service) { instance_double(Configuration::PaasConfigurationService) }
before do before do
Rake.application.rake_require("tasks/import_address_from_csv") Rake.application.rake_require("tasks/import_address_from_csv")
@ -282,10 +280,9 @@ RSpec.describe "data_import" do
describe ":import_sales_addresses_from_csv", type: :task do describe ":import_sales_addresses_from_csv", type: :task do
subject(:task) { Rake::Task["data_import:import_sales_addresses_from_csv"] } subject(:task) { Rake::Task["data_import:import_sales_addresses_from_csv"] }
let(:instance_name) { "paas_import_instance" } let(:instance_name) { "import_instance" }
let(:storage_service) { instance_double(Storage::S3Service) } let(:storage_service) { instance_double(Storage::S3Service) }
let(:env_config_service) { instance_double(Configuration::EnvConfigurationService) } let(:env_config_service) { instance_double(Configuration::EnvConfigurationService) }
let(:paas_config_service) { instance_double(Configuration::PaasConfigurationService) }
before do before do
Rake.application.rake_require("tasks/import_address_from_csv") Rake.application.rake_require("tasks/import_address_from_csv")

4
spec/lib/tasks/correct_illness_from_csv_spec.rb

@ -50,15 +50,13 @@ RSpec.describe "correct_illness" do
subject(:task) { Rake::Task["correct_illness:correct_illness_from_csv"] } subject(:task) { Rake::Task["correct_illness:correct_illness_from_csv"] }
let(:instance_name) { "paas_import_instance" } let(:instance_name) { "import_instance" }
let(:storage_service) { instance_double(Storage::S3Service) } let(:storage_service) { instance_double(Storage::S3Service) }
let(:env_config_service) { instance_double(Configuration::EnvConfigurationService) } let(:env_config_service) { instance_double(Configuration::EnvConfigurationService) }
let(:paas_config_service) { instance_double(Configuration::PaasConfigurationService) }
before do before do
allow(Storage::S3Service).to receive(:new).and_return(storage_service) allow(Storage::S3Service).to receive(:new).and_return(storage_service)
allow(Configuration::EnvConfigurationService).to receive(:new).and_return(env_config_service) allow(Configuration::EnvConfigurationService).to receive(:new).and_return(env_config_service)
allow(Configuration::PaasConfigurationService).to receive(:new).and_return(paas_config_service)
allow(ENV).to receive(:[]) allow(ENV).to receive(:[])
allow(ENV).to receive(:[]).with("IMPORT_PAAS_INSTANCE").and_return(instance_name) allow(ENV).to receive(:[]).with("IMPORT_PAAS_INSTANCE").and_return(instance_name)
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return("dummy") allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return("dummy")

6
spec/lib/tasks/data_export_spec.rb

@ -2,9 +2,8 @@ require "rails_helper"
require "rake" require "rake"
describe "rake core:data_export", type: task do describe "rake core:data_export", type: task do
let(:paas_instance) { "paas_export_instance" } let(:export_instance) { "export_instance" }
let(:storage_service) { instance_double(Storage::S3Service) } let(:storage_service) { instance_double(Storage::S3Service) }
let(:paas_config_service) { instance_double(Configuration::PaasConfigurationService) }
let(:export_service) { instance_double(Exports::LettingsLogExportService) } let(:export_service) { instance_double(Exports::LettingsLogExportService) }
before do before do
@ -13,10 +12,9 @@ describe "rake core:data_export", type: task do
task.reenable task.reenable
allow(Storage::S3Service).to receive(:new).and_return(storage_service) allow(Storage::S3Service).to receive(:new).and_return(storage_service)
allow(Configuration::PaasConfigurationService).to receive(:new).and_return(paas_config_service)
allow(Exports::LettingsLogExportService).to receive(:new).and_return(export_service) allow(Exports::LettingsLogExportService).to receive(:new).and_return(export_service)
allow(ENV).to receive(:[]) allow(ENV).to receive(:[])
allow(ENV).to receive(:[]).with("EXPORT_PAAS_INSTANCE").and_return(paas_instance) allow(ENV).to receive(:[]).with("EXPORT_PAAS_INSTANCE").and_return(export_instance)
end end
context "when exporting lettings logs with no parameters" do context "when exporting lettings logs with no parameters" do

149
spec/services/configuration/paas_configuration_service_spec.rb

@ -1,149 +0,0 @@
require "rails_helper"
RSpec.describe Configuration::PaasConfigurationService do
subject(:config_service) { described_class.new(logger) }
let(:logger) { instance_double(ActiveSupport::LogSubscriber) }
context "when the paas configuration is unavailable" do
before { allow(logger).to receive(:warn) }
it "returns the S3 configuration as not present" do
expect(config_service.s3_config_present?).to be(false)
end
it "returns the redis configuration as not present" do
expect(config_service.redis_config_present?).to be(false)
end
it "does not retrieve any S3 bucket configuration" do
expect(config_service.s3_buckets).to be_a(Hash)
expect(config_service.s3_buckets).to be_empty
end
it "does not retrieve any redis configuration" do
expect(config_service.redis_uris).to be_a(Hash)
expect(config_service.redis_uris).to be_empty
end
end
context "when the paas configuration is present but empty" do
before do
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return("{}")
end
it "returns the S3 configuration as not present" do
expect(config_service.s3_config_present?).to be(false)
end
it "returns the redis configuration as not present" do
expect(config_service.redis_config_present?).to be(false)
end
it "does not retrieve any S3 bucket configuration" do
expect(config_service.s3_buckets).to be_a(Hash)
expect(config_service.s3_buckets).to be_empty
end
it "does not retrieve any redis configuration" do
expect(config_service.redis_uris).to be_a(Hash)
expect(config_service.redis_uris).to be_empty
end
end
context "when the paas configuration is present but invalid" do
let(:vcap_services) { "random text" }
before do
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services)
allow(logger).to receive(:warn)
end
it "logs an error when checking if the S3 config is present" do
expect(logger).to receive(:warn).with("Could not parse VCAP_SERVICES!")
config_service.s3_config_present?
end
it "logs an error when checking if the Redis config is present" do
expect(logger).to receive(:warn).with("Could not parse VCAP_SERVICES!")
config_service.redis_config_present?
end
end
context "when the paas configuration is present with S3 configured" do
let(:vcap_services) do
<<~JSON
{"aws-s3-bucket":
[{
"instance_name": "bucket_1",
"credentials": {
"aws_access_key_id": "123",
"aws_secret_access_key": "456",
"aws_region": "eu-west-1",
"bucket_name": "my-bucket"
}
},
{
"instance_name": "bucket_2",
"credentials": {
"aws_access_key_id": "789",
"aws_secret_access_key": "012",
"aws_region": "eu-west-2",
"bucket_name": "my-bucket2"
}
}]
}
JSON
end
before do
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services)
end
it "returns the S3 configuration as present" do
expect(config_service.s3_config_present?).to be(true)
end
it "returns the redis configuration as not present" do
expect(config_service.redis_config_present?).to be(false)
end
it "does retrieve the S3 bucket configurations" do
s3_buckets = config_service.s3_buckets
expect(s3_buckets).not_to be_empty
expect(s3_buckets.count).to be(2)
expect(s3_buckets).to have_key(:bucket_1)
expect(s3_buckets).to have_key(:bucket_2)
end
end
context "when the paas configuration is present with redis configured" do
let(:vcap_services) do
<<-JSON
{"redis": [{"instance_name": "redis_1", "credentials": {"uri": "redis_uri" }}]}
JSON
end
before do
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services)
end
it "returns the redis configuration as present" do
expect(config_service.redis_config_present?).to be(true)
end
it "returns the S3 configuration as not present" do
expect(config_service.s3_config_present?).to be(false)
end
it "does retrieve the redis configurations" do
redis_uris = config_service.redis_uris
expect(redis_uris).not_to be_empty
expect(redis_uris.count).to be(1)
expect(redis_uris).to have_key(:redis_1)
expect(redis_uris[:redis_1]).to eq("redis_uri")
end
end
end

57
spec/services/storage/s3_service_spec.rb

@ -3,41 +3,37 @@ require "rails_helper"
RSpec.describe Storage::S3Service do RSpec.describe Storage::S3Service do
let(:instance_name) { "instance_1" } let(:instance_name) { "instance_1" }
let(:bucket_name) { "bucket_1" } let(:bucket_name) { "bucket_1" }
let(:vcap_services) do let(:env_config_service) { instance_double(Configuration::EnvConfigurationService) }
<<-JSON let(:aws_credentials) { instance_double(Aws::ECSCredentials) }
{"aws-s3-bucket": [
{ before do
"instance_name": "#{instance_name}", allow(env_config_service).to receive(:s3_config_present?).and_return(true)
"credentials": { allow(Aws::ECSCredentials).to receive(:new).and_return(aws_credentials)
allow(env_config_service).to receive(:s3_buckets).and_return({ "instance_1": { "credentials": {
"aws_access_key_id": "key_id", "aws_access_key_id": "key_id",
"aws_region": "eu-west-2", "aws_region": "eu-west-2",
"aws_secret_access_key": "secret", "aws_secret_access_key": "secret",
"bucket_name": "#{bucket_name}" "bucket_name": bucket_name.to_s,
} } } })
}
]}
JSON
end end
context "when we create a storage service with no PaaS Configuration present" do context "when we create a storage service with no Configuration present" do
subject(:storage_service) { described_class.new(Configuration::PaasConfigurationService.new, "random_instance") } subject(:storage_service) { described_class.new(env_config_service, "random_instance") }
before do
allow(env_config_service).to receive(:s3_config_present?).and_return(false)
end
it "raises an exception" do it "raises an exception" do
expect { storage_service }.to raise_error(RuntimeError, "No S3 bucket is present in the PaaS configuration") expect { storage_service }.to raise_error(RuntimeError, "No S3 bucket is present in the configuration")
end end
end end
context "when we create a storage service and the S3 instance name is not found in the PaaS configuration" do context "when we create a storage service and the S3 instance name is not found in the env configuration" do
subject(:storage_service) { described_class.new(Configuration::PaasConfigurationService.new, "random_instance") } subject(:storage_service) { described_class.new(env_config_service, "random_instance") }
let(:vcap_services) do
<<-JSON
{"aws-s3-bucket": []}
JSON
end
before do before do
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services) allow(env_config_service).to receive(:s3_buckets).and_return({ "aws-s3-bucket": [] })
end end
it "raises an exception" do it "raises an exception" do
@ -46,12 +42,7 @@ RSpec.describe Storage::S3Service do
end end
context "when we create a 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(Configuration::PaasConfigurationService.new, instance_name) } subject(:storage_service) { described_class.new(env_config_service, instance_name) }
before do
allow(ENV).to receive(:[])
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services)
end
it "creates a Storage Configuration" do it "creates a Storage Configuration" do
expect(storage_service.configuration).to be_an(Storage::StorageConfiguration) expect(storage_service.configuration).to be_an(Storage::StorageConfiguration)
@ -71,15 +62,13 @@ RSpec.describe Storage::S3Service do
end end
context "when we create a 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(Configuration::PaasConfigurationService.new, instance_name) } subject(:storage_service) { described_class.new(env_config_service, instance_name) }
let(:filename) { "my_file" } let(:filename) { "my_file" }
let(:content) { "content" } let(:content) { "content" }
let(:s3_client_stub) { Aws::S3::Client.new(stub_responses: true) } let(:s3_client_stub) { Aws::S3::Client.new(stub_responses: true) }
before do 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) allow(Aws::S3::Client).to receive(:new).and_return(s3_client_stub)
end end
@ -106,13 +95,11 @@ RSpec.describe Storage::S3Service do
end end
context "when we create a storage service" do context "when we create a storage service" do
subject(:storage_service) { described_class.new(Configuration::PaasConfigurationService.new, instance_name) } subject(:storage_service) { described_class.new(env_config_service, instance_name) }
let(:s3_client_stub) { Aws::S3::Client.new(stub_responses: true) } let(:s3_client_stub) { Aws::S3::Client.new(stub_responses: true) }
before do 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) allow(Aws::S3::Client).to receive(:new).and_return(s3_client_stub)
end end

Loading…
Cancel
Save