2017-02-01 21:27:10 +00:00
|
|
|
# author: Simon Varlow
|
|
|
|
require 'aws-sdk'
|
|
|
|
require 'helper'
|
|
|
|
require 'aws_iam_user'
|
|
|
|
|
|
|
|
class AwsIamUserTest < Minitest::Test
|
2017-07-05 20:31:27 +00:00
|
|
|
Username = 'test'.freeze
|
|
|
|
|
2017-02-01 21:27:10 +00:00
|
|
|
def setup
|
2017-03-29 21:09:05 +00:00
|
|
|
@mock_user_provider = Minitest::Mock.new
|
2017-02-01 21:27:10 +00:00
|
|
|
end
|
|
|
|
|
2017-07-05 20:31:27 +00:00
|
|
|
def test_mfa_enabled_returns_true_if_mfa_enabled
|
|
|
|
@mock_user_provider.expect(
|
|
|
|
:user,
|
|
|
|
{ has_mfa_enabled?: true },
|
|
|
|
[Username],
|
|
|
|
)
|
2017-08-08 13:50:35 +00:00
|
|
|
assert(
|
|
|
|
AwsIamUser.new(
|
|
|
|
{ name: Username },
|
|
|
|
@mock_user_provider,
|
|
|
|
).has_mfa_enabled?,
|
|
|
|
)
|
2017-02-01 21:27:10 +00:00
|
|
|
end
|
|
|
|
|
2017-07-05 20:31:27 +00:00
|
|
|
def test_mfa_enabled_returns_false_if_mfa_is_not_enabled
|
|
|
|
@mock_user_provider.expect(
|
|
|
|
:user,
|
|
|
|
{ has_mfa_enabled?: false },
|
|
|
|
[Username],
|
|
|
|
)
|
2017-08-08 13:50:35 +00:00
|
|
|
refute(
|
|
|
|
AwsIamUser.new(
|
|
|
|
{ name: Username },
|
|
|
|
@mock_user_provider,
|
|
|
|
).has_mfa_enabled?,
|
|
|
|
)
|
2017-02-01 21:27:10 +00:00
|
|
|
end
|
|
|
|
|
2017-07-05 20:31:27 +00:00
|
|
|
def test_console_password_returns_true_if_console_password_has_been_set
|
|
|
|
@mock_user_provider.expect(
|
|
|
|
:user,
|
|
|
|
{ has_console_password?: true },
|
|
|
|
[Username],
|
|
|
|
)
|
2017-08-08 13:50:35 +00:00
|
|
|
assert(
|
|
|
|
AwsIamUser.new(
|
|
|
|
{ name: Username },
|
|
|
|
@mock_user_provider,
|
|
|
|
).has_console_password?,
|
|
|
|
)
|
2017-02-01 21:27:10 +00:00
|
|
|
end
|
|
|
|
|
2017-07-05 20:31:27 +00:00
|
|
|
def test_console_password_returns_false_if_console_password_has_not_been_set
|
|
|
|
@mock_user_provider.expect(
|
|
|
|
:user,
|
|
|
|
{ has_console_password?: false },
|
|
|
|
[Username],
|
|
|
|
)
|
2017-08-08 13:50:35 +00:00
|
|
|
refute(
|
|
|
|
AwsIamUser.new(
|
|
|
|
{ name: Username },
|
|
|
|
@mock_user_provider,
|
|
|
|
).has_console_password?,
|
|
|
|
)
|
2017-02-01 21:27:10 +00:00
|
|
|
end
|
2017-06-13 05:36:43 +00:00
|
|
|
|
|
|
|
def test_that_access_keys_returns_aws_iam_access_key_resources
|
|
|
|
stub_aws_access_key = Object.new
|
|
|
|
stub_access_key_resource = Object.new
|
|
|
|
mock_access_key_factory = Minitest::Mock.new
|
|
|
|
|
2017-07-05 20:31:27 +00:00
|
|
|
@mock_user_provider.expect(
|
|
|
|
:user,
|
|
|
|
{ access_keys: [stub_aws_access_key] },
|
|
|
|
[Username],
|
|
|
|
)
|
|
|
|
mock_access_key_factory.expect(
|
|
|
|
:create_access_key,
|
|
|
|
stub_access_key_resource,
|
|
|
|
[stub_aws_access_key],
|
|
|
|
)
|
2017-06-13 05:36:43 +00:00
|
|
|
|
2017-07-05 20:31:27 +00:00
|
|
|
assert_equal(
|
|
|
|
stub_access_key_resource,
|
|
|
|
AwsIamUser.new(
|
2017-08-08 13:50:35 +00:00
|
|
|
{ name: Username },
|
2017-07-05 20:31:27 +00:00
|
|
|
@mock_user_provider,
|
|
|
|
mock_access_key_factory,
|
|
|
|
).access_keys[0],
|
|
|
|
)
|
2017-06-13 05:36:43 +00:00
|
|
|
|
|
|
|
mock_access_key_factory.verify
|
|
|
|
end
|
2017-06-28 12:46:59 +00:00
|
|
|
|
|
|
|
def test_to_s
|
2017-08-08 13:50:35 +00:00
|
|
|
@mock_user_provider.expect(
|
|
|
|
:user,
|
|
|
|
{ name: Username, has_mfa_enabled?: true },
|
|
|
|
[Username],
|
|
|
|
)
|
|
|
|
expected = "IAM User #{Username}"
|
|
|
|
test = AwsIamUser.new({ name: Username }, @mock_user_provider).to_s
|
2017-06-28 12:46:59 +00:00
|
|
|
assert_equal expected, test
|
|
|
|
end
|
2017-06-13 05:36:43 +00:00
|
|
|
end
|