Skip to content

Commit

Permalink
Adding spec coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dmorgan-fa committed May 1, 2024
1 parent 348208b commit 91bfbfe
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 13 deletions.
22 changes: 12 additions & 10 deletions lib/aws_local_config_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ def initialize(config_path: "#{ENV["HOME"]}/.aws/config")
@config = read_config_file
end

def read_config_file
raise AwsLocalConfigParser::NoAwsConfig.new(config_path:) unless File.exist?(config_path)

IniFile.load(config_path)
end

def config_hash
config.to_h
end

def profile(profile_name)
raise NoMatchingProfile.new(profile_name:, config_path:) unless config.has_section?("profile #{profile_name}")

Expand All @@ -45,4 +35,16 @@ def all_profiles
list_of_profiles = config_hash.keys.select { |entry| entry.start_with?('profile') }.sort
list_of_profiles.map { |profile| profile.gsub('profile ', '') }
end

private

def read_config_file
raise AwsLocalConfigParser::NoAwsConfig.new(config_path:) unless File.exist?(config_path)

IniFile.load(config_path)
end

def config_hash
config.to_h
end
end
130 changes: 127 additions & 3 deletions spec/aws_local_config_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,131 @@
expect(AwsLocalConfigParser::VERSION).not_to be nil
end

# it "does something useful" do
# expect(false).to eq(true)
# end
subject(:local_config_parser) { AwsLocalConfigParser.new(config_path:) }
let(:config_path) { './spec/fixtures/aws/config' }

describe '#initialize' do
context 'when a config file exists' do
it 'loads and parses the config' do
expect { local_config_parser }.to_not raise_error
end
end

context 'when a config file does not exist' do
let(:config_path) { 'file/path/does/not/exist' }
it 'raises an error' do
expect { local_config_parser }.to raise_error(
AwsLocalConfigParser::NoAwsConfig,
/No AWS config file at: '#{config_path}'/
)
end
end
end

describe '#profile' do
let(:profile) { local_config_parser.profile(profile_name) }

context 'when the profile exists in the config' do
let(:profile_name) { 'sso-dev-full' }

it 'returns the profile as a ruby object' do
expect(profile.class).to be OpenStruct
end

it 'has an region' do
expect(profile.region).to eq 'dev-profile region'
end

context 'when the profile is an sso profile' do
it 'has a sso_session' do
expect(profile.sso_session).to eq 'sso-dev'
end

it 'has a sso_account_id' do
expect(profile.sso_account_id).to eq 'account_number_1234'
end

it 'has a sso_role_name' do
expect(profile.sso_role_name).to eq 'sso_account_role_name'
end
end

context 'when the profile is not an sso profile' do
let(:profile_name) { 'non-sso-profile' }

it 'has a aws_access_key_id' do
expect(profile.aws_access_key_id).to eq 'aws_access_key_id_value'
end

it 'has a aws_secret_access_key' do
expect(profile.aws_secret_access_key).to eq 'aws_secret_access_key_value'
end
end
end

context 'when the profile does not exists in the config' do
let(:profile_name) { 'profile-does-not-exist' }

it 'raises an error' do
expect { local_config_parser.profile(profile_name) }.to raise_error(
AwsLocalConfigParser::NoMatchingProfile,
/No matching profile, '#{profile_name}', in '#{config_path}'/
)
end
end
end

describe '#sso_session' do
let(:sso_session) { local_config_parser.sso_session(sso_session_name) }

context 'when the sso_session exists in the config' do
let(:sso_session_name) { 'sso-dev' }

it 'returns the profile as a ruby object' do
expect(sso_session.class).to be OpenStruct
end

it 'has a region' do
expect(sso_session.sso_region).to eq 'dev region'
end

it 'has a sso_start_url' do
expect(sso_session.sso_start_url).to eq 'https://sso-dev.awsapps.com/start/'
end

it 'has a sso_registration_scopes' do
expect(sso_session.sso_registration_scopes).to eq 'sso:account:access'
end
end

context 'when the sso_session does not exists in the config' do
let(:sso_session_name) { 'sso-session-does-not-exist' }

it 'raises an error' do
expect { sso_session }.to raise_error(
AwsLocalConfigParser::NoMatchingSsoSession,
/No matching sso-session, '#{sso_session_name}', in '#{config_path}'/
)
end
end
end

describe '#all_sso_sessions' do
it 'reruns a list of the sso_sessions' do
expect(local_config_parser.all_sso_sessions).to match_array(%w[sso-dev sso-prod])
end
end

describe '#all_profiles' do
it 'reruns a list of the profiles' do
expect(local_config_parser.all_profiles).to match_array(
%w[
non-sso-profile
sso-dev-full
sso-sandbox-full
sso-stage-full
]
)
end
end
end
40 changes: 40 additions & 0 deletions spec/fixtures/aws/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# MANAGED SESSION
[sso-session sso-dev]
sso_start_url = https://sso-dev.awsapps.com/start/
sso_region = dev region
sso_registration_scopes = sso:account:access

# MANAGED SESSION
[sso-session sso-prod]
sso_start_url = https://sso-prod.awsapps.com/start/
sso_region = prod region
sso_registration_scopes = sso:account:access

# MANAGED PROFILE
[profile sso-dev-full]
sso_session = sso-dev
sso_account_id = account_number_1234
sso_role_name = sso_account_role_name
region=dev-profile region

[profile non-sso-profile]
aws_access_key_id = aws_access_key_id_value
aws_secret_access_key = aws_secret_access_key_value
region=dev-profile region


## Additional profiles

# MANAGED PROFILE
[profile sso-stage-full]
sso_session = sso-stage
sso_account_id = account_number_1234
sso_role_name = sso_account_role_name
region=stage-profile region

# MANAGED PROFILE
[profile sso-sandbox-full]
sso_session = sso-sandbox
sso_account_id = account_number_1234
sso_role_name = sso_account_role_name
region=sandbox-profile region
11 changes: 11 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'aws_local_config_parser'
require 'debug'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand All @@ -9,6 +10,16 @@
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!

# Support focusing on individual specs, but prevent focus for CI runs
config.filter_run focus: true unless ENV['CI']
config.run_all_when_everything_filtered = true

if ENV['CI']
config.before(:example, :focus) do
raise 'This example was committed with `:focus` and should not have been'
end
end

config.expect_with :rspec do |c|
c.syntax = :expect
end
Expand Down

0 comments on commit 91bfbfe

Please sign in to comment.