You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.5 KiB
150 lines
4.5 KiB
3 years ago
|
require "rails_helper"
|
||
|
|
||
2 years ago
|
RSpec.describe Configuration::PaasConfigurationService do
|
||
3 years ago
|
subject(:config_service) { described_class.new(logger) }
|
||
|
|
||
|
let(:logger) { instance_double(ActiveSupport::LogSubscriber) }
|
||
3 years ago
|
|
||
3 years ago
|
context "when the paas configuration is unavailable" do
|
||
3 years ago
|
before { allow(logger).to receive(:warn) }
|
||
|
|
||
2 years ago
|
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("{}")
|
||
3 years ago
|
end
|
||
|
|
||
|
it "returns the S3 configuration as not present" do
|
||
3 years ago
|
expect(config_service.s3_config_present?).to be(false)
|
||
3 years ago
|
end
|
||
|
|
||
2 years ago
|
it "returns the redis configuration as not present" do
|
||
|
expect(config_service.redis_config_present?).to be(false)
|
||
|
end
|
||
|
|
||
3 years ago
|
it "does not retrieve any S3 bucket configuration" do
|
||
3 years ago
|
expect(config_service.s3_buckets).to be_a(Hash)
|
||
3 years ago
|
expect(config_service.s3_buckets).to be_empty
|
||
3 years ago
|
end
|
||
3 years ago
|
|
||
|
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
|
||
3 years ago
|
end
|
||
|
|
||
2 years ago
|
context "when the paas configuration is present but invalid" do
|
||
3 years ago
|
let(:vcap_services) { "random text" }
|
||
3 years ago
|
|
||
|
before do
|
||
|
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services)
|
||
|
allow(logger).to receive(:warn)
|
||
|
end
|
||
|
|
||
2 years ago
|
it "logs an error when checking if the S3 config is present" do
|
||
3 years ago
|
expect(logger).to receive(:warn).with("Could not parse VCAP_SERVICES!")
|
||
|
config_service.s3_config_present?
|
||
|
end
|
||
2 years ago
|
|
||
|
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
|
||
3 years ago
|
end
|
||
|
|
||
2 years ago
|
context "when the paas configuration is present with S3 configured" do
|
||
3 years ago
|
let(:vcap_services) do
|
||
2 years ago
|
<<~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"
|
||
|
}
|
||
|
}]
|
||
|
}
|
||
3 years ago
|
JSON
|
||
|
end
|
||
|
|
||
|
before do
|
||
|
allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services)
|
||
|
end
|
||
|
|
||
|
it "returns the S3 configuration as present" do
|
||
3 years ago
|
expect(config_service.s3_config_present?).to be(true)
|
||
3 years ago
|
end
|
||
|
|
||
2 years ago
|
it "returns the redis configuration as not present" do
|
||
|
expect(config_service.redis_config_present?).to be(false)
|
||
|
end
|
||
|
|
||
3 years ago
|
it "does retrieve the S3 bucket configurations" do
|
||
3 years ago
|
s3_buckets = config_service.s3_buckets
|
||
3 years ago
|
|
||
3 years ago
|
expect(s3_buckets).not_to be_empty
|
||
3 years ago
|
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
|
||
|
|
||
2 years ago
|
context "when the paas configuration is present with redis configured" do
|
||
3 years ago
|
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
|
||
|
|
||
2 years ago
|
it "returns the S3 configuration as not present" do
|
||
|
expect(config_service.s3_config_present?).to be(false)
|
||
|
end
|
||
|
|
||
3 years ago
|
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
|
||
3 years ago
|
end
|