2017-02-01 21:27:10 +00:00
|
|
|
# author: Simon Varlow
|
|
|
|
require 'helper'
|
|
|
|
require 'aws_iam_user'
|
|
|
|
|
2017-11-22 13:30:18 +00:00
|
|
|
# rubocop:disable Metrics/ClassLength
|
2017-02-01 21:27:10 +00:00
|
|
|
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-10-26 19:22:15 +00:00
|
|
|
@mock_dets_provider = Minitest::Mock.new
|
|
|
|
@mock_dets_prov_ini = Minitest::Mock.new
|
|
|
|
@mock_user = { name: Username }
|
2017-02-01 21:27:10 +00:00
|
|
|
end
|
|
|
|
|
2017-11-22 13:30:18 +00:00
|
|
|
def test_that_exists_returns_true_if_user_exists
|
|
|
|
@mock_user_provider.expect :user, @mock_user, [Username]
|
|
|
|
@mock_dets_provider.expect :exists?, true
|
|
|
|
@mock_dets_prov_ini.expect :create, @mock_dets_provider, [@mock_user]
|
|
|
|
assert AwsIamUser.new(
|
|
|
|
@mock_user,
|
|
|
|
@mock_user_provider,
|
|
|
|
@mock_dets_prov_ini,
|
|
|
|
).exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_that_exists_returns_false_if_user_does_not_exist
|
|
|
|
@mock_user_provider.expect :user, @mock_user, [Username]
|
|
|
|
@mock_dets_provider.expect :exists?, false
|
|
|
|
@mock_dets_prov_ini.expect :create, @mock_dets_provider, [@mock_user]
|
|
|
|
refute AwsIamUser.new(
|
|
|
|
@mock_user,
|
|
|
|
@mock_user_provider,
|
|
|
|
@mock_dets_prov_ini,
|
|
|
|
).exists?
|
|
|
|
end
|
|
|
|
|
2017-10-26 19:22:15 +00:00
|
|
|
def test_that_mfa_enable_returns_true_if_mfa_enabled
|
|
|
|
@mock_user_provider.expect :user, @mock_user, [Username]
|
|
|
|
@mock_dets_provider.expect :has_mfa_enabled?, true
|
|
|
|
@mock_dets_prov_ini.expect :create, @mock_dets_provider, [@mock_user]
|
|
|
|
assert AwsIamUser.new(
|
|
|
|
@mock_user,
|
|
|
|
@mock_user_provider,
|
|
|
|
@mock_dets_prov_ini,
|
|
|
|
).has_mfa_enabled?
|
2017-02-01 21:27:10 +00:00
|
|
|
end
|
|
|
|
|
2017-10-26 19:22:15 +00:00
|
|
|
def test_that_mfa_enable_returns_false_if_mfa_is_not_enabled
|
|
|
|
@mock_user_provider.expect :user, @mock_user, [Username]
|
|
|
|
@mock_dets_provider.expect :has_mfa_enabled?, false
|
|
|
|
@mock_dets_prov_ini.expect :create, @mock_dets_provider, [@mock_user]
|
|
|
|
refute AwsIamUser.new(
|
|
|
|
@mock_user,
|
|
|
|
@mock_user_provider,
|
|
|
|
@mock_dets_prov_ini,
|
|
|
|
).has_mfa_enabled?
|
2017-02-01 21:27:10 +00:00
|
|
|
end
|
|
|
|
|
2017-10-26 19:22:15 +00:00
|
|
|
def test_that_console_password_returns_true_if_console_password_set
|
|
|
|
@mock_user_provider.expect :user, @mock_user, [Username]
|
|
|
|
@mock_dets_provider.expect :has_console_password?, true
|
|
|
|
@mock_dets_prov_ini.expect :create, @mock_dets_provider, [@mock_user]
|
|
|
|
assert AwsIamUser.new(
|
|
|
|
@mock_user,
|
|
|
|
@mock_user_provider,
|
|
|
|
@mock_dets_prov_ini,
|
|
|
|
).has_console_password?
|
2017-02-01 21:27:10 +00:00
|
|
|
end
|
|
|
|
|
2017-10-26 19:22:15 +00:00
|
|
|
def test_that_console_password_returns_false_if_console_password_not_set
|
|
|
|
@mock_user_provider.expect :user, @mock_user, [Username]
|
|
|
|
@mock_dets_provider.expect :has_console_password?, false
|
|
|
|
@mock_dets_prov_ini.expect :create, @mock_dets_provider, [@mock_user]
|
|
|
|
refute AwsIamUser.new(
|
|
|
|
@mock_user,
|
|
|
|
@mock_user_provider,
|
|
|
|
@mock_dets_prov_ini,
|
|
|
|
).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-10-26 19:22:15 +00:00
|
|
|
@mock_user_provider.expect :user, @mock_user, [Username]
|
|
|
|
@mock_dets_provider.expect :access_keys, [stub_aws_access_key]
|
|
|
|
@mock_dets_prov_ini.expect :create, @mock_dets_provider, [@mock_user]
|
2017-07-05 20:31:27 +00:00
|
|
|
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-10-26 19:22:15 +00:00
|
|
|
@mock_user,
|
2017-07-05 20:31:27 +00:00
|
|
|
@mock_user_provider,
|
2017-10-26 19:22:15 +00:00
|
|
|
@mock_dets_prov_ini,
|
2017-07-05 20:31:27 +00:00
|
|
|
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-10-26 19:22:15 +00:00
|
|
|
test_user = { name: Username, has_mfa_enabled?: true }
|
|
|
|
@mock_user_provider.expect :user, test_user, [Username]
|
|
|
|
@mock_dets_provider.expect :name, Username
|
|
|
|
@mock_dets_prov_ini.expect :create, @mock_dets_provider, [test_user]
|
2017-08-08 13:50:35 +00:00
|
|
|
expected = "IAM User #{Username}"
|
2017-10-26 19:22:15 +00:00
|
|
|
test = AwsIamUser.new(
|
|
|
|
{ name: Username },
|
|
|
|
@mock_user_provider,
|
|
|
|
@mock_dets_prov_ini,
|
|
|
|
).to_s
|
2017-06-28 12:46:59 +00:00
|
|
|
assert_equal expected, test
|
|
|
|
end
|
2017-06-13 05:36:43 +00:00
|
|
|
end
|