inspec/test/unit/resources/aws_iam_users_test.rb
Clinton Wolfe f5251f3c29 Re-work unit tests for user and users (#125)
* Constructor unit tests

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Constructor tests pass, all others gutted

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Basic 'where' test in place, no criteria

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Wired up filter table to backend list users

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Unit testing for has_mfa_enabled and has_console_password

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Simple AWS client implementation for Users

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Rework resource parameters and validation; copy in code from #121

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Add constructor tests

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Add search/recall tests

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Recall unit tests pass

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Failing unit tests for username and has_console_password

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* has_console_password works in unit tests

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* has_mfa_enabled failing unit tests

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* has_mfa_enabled passes unit tests

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Failing unit tests for Access Keys

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* CLean up bad rebase commit

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Access keys property works, as an uncooked AWS response

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* De-linting

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Integration tests work

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Remove provider support libraries

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Integration tests pass for users resource

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* De-lint

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Remove aws connection load from user

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>

* Adapt aws_iam_user to rely on AwsResourceMixin

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
2017-12-08 19:34:09 +01:00

152 lines
4.4 KiB
Ruby

require 'helper'
require 'ostruct'
require 'aws_iam_users'
# Maiusb = Mock AwsIamUsers::Backend
# Abbreviation not used outside of this file
class AwsIamUsersTestConstructor < Minitest::Test
def setup
AwsIamUsers::Backend.select(Maiusb::Empty)
end
def test_users_no_params_does_not_explode
AwsIamUsers.new
end
def test_users_all_params_rejected
assert_raises(ArgumentError) { AwsIamUsers.new(something: 'somevalue') }
end
end
class AwsIamUsersTestFilterCriteria < Minitest::Test
def setup
# Reset to empty, that's harmless
AwsIamUsers::Backend.select(Maiusb::Empty)
end
#------------------------------------------#
# Open Filter
#------------------------------------------#
def test_users_empty_result_when_no_users_no_criteria
users = AwsIamUsers.new.where {}
assert users.entries.empty?
end
def test_users_all_returned_when_some_users_no_criteria
AwsIamUsers::Backend.select(Maiusb::Basic)
users = AwsIamUsers.new.where {}
assert(3, users.entries.count)
end
#------------------------------------------#
# has_mfa_enabled?
#------------------------------------------#
def test_users_criteria_has_mfa_enabled
AwsIamUsers::Backend.select(Maiusb::Basic)
users = AwsIamUsers.new.where { has_mfa_enabled }
assert(1, users.entries.count)
assert_includes users.entries.map{ |u| u[:user_name] }, 'carol'
refute_includes users.entries.map{ |u| u[:user_name] }, 'alice'
end
#------------------------------------------#
# has_console_password?
#------------------------------------------#
def test_users_criteria_has_console_password?
AwsIamUsers::Backend.select(Maiusb::Basic)
users = AwsIamUsers.new.where { has_console_password }
assert(2, users.entries.count)
assert_includes users.entries.map{ |u| u[:user_name] }, 'carol'
refute_includes users.entries.map{ |u| u[:user_name] }, 'alice'
end
end
#=============================================================================#
# Test Fixture Classes
#=============================================================================#
module Maiusb
# --------------------------------
# Empty - No users
# --------------------------------
class Empty < AwsIamUsers::Backend
def list_users
OpenStruct.new({
users: []
})
end
def get_login_profile(criteria)
raise Aws::IAM::Errors::NoSuchEntity.new("No login profile for #{criteria[:user_name]}", 'Nope')
end
def list_mfa_devices(_criteria)
OpenStruct.new({
mfa_devices: []
})
end
end
# --------------------------------
# Basic - 3 Users
# --------------------------------
# Alice has no password or MFA device
# Bob has a password but no MFA device
# Carol has a password and MFA device
class Basic < AwsIamUsers::Backend
# arn, path, user_id omitted
def list_users
OpenStruct.new({
users: [
OpenStruct.new({
user_name: 'alice',
create_date: DateTime.parse('2017-10-10T16:19:30Z'),
# Password last used is absent, never logged in w/ password
}),
OpenStruct.new({
user_name: 'bob',
create_date: DateTime.parse('2017-11-06T16:19:30Z'),
password_last_used: DateTime.parse('2017-11-06T19:19:30Z'),
}),
OpenStruct.new({
user_name: 'carol',
create_date: DateTime.parse('2017-10-10T16:19:30Z'),
password_last_used: DateTime.parse('2017-10-28T19:19:30Z'),
}),
]
})
end
def get_login_profile(criteria)
if ['bob', 'carol'].include?(criteria[:user_name])
OpenStruct.new({
login_profile: OpenStruct.new({
user_name: criteria[:user_name],
created_date: DateTime.parse('2017-10-10T16:19:30Z')
})
})
else
raise Aws::IAM::Errors::NoSuchEntity.new("No login profile for #{criteria[:user_name]}", 'Nope')
end
end
def list_mfa_devices(criteria)
if ['carol'].include?(criteria[:user_name])
OpenStruct.new({
mfa_devices: [
OpenStruct.new({
user_name: criteria[:user_name],
serial_number: '1234567890',
enable_date: DateTime.parse('2017-10-10T16:19:30Z'),
})
]
})
else
OpenStruct.new({
mfa_devices: []
})
end
end
end
end