inspec/test/unit/resources/aws_iam_users_test.rb
Steffanie Freeman 1a31425e81 Issue #46 Lazily load attributes in aws_iam_users (#89)
* Initial Commit

Signed-off-by: sfreeman <Steffanie.Freeman@d2l.com>

* aws_iam_user uses lazy loading

Signed-off-by: sfreeman <Steffanie.Freeman@d2l.com>

* Disassociates convert call from list_users

Signed-off-by: sfreeman <Steffanie.Freeman@d2l.com>

* A real-world working AwsIamUsers (#71)

* Add aws_iam_users

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Adding Filter table and Collect User Details to aws_iam_users.rb

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Adding Filter table and Collect User Details to aws_iam_users.rb

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Adding Filter table and Collect User Details to aws_iam_users.rb

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Get an aws_iam_users integration test to pass

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Fix RuboCop issues and tests

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Improving code based on PR feedback

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* AWS IAM Users unit tests work with new lazy loading feature

Signed-off-by: sfreeman <Steffanie.Freeman@d2l.com>

* Fixes tests

Signed-off-by: aduric <adnan.duric@d2l.com>

* Users should only hold the returned user references, transfering responsibility to each user to fetch any details

Signed-off-by: aduric <adnan.duric@d2l.com>

* Create user details provider class

Signed-off-by: sfreeman <Steffanie.Freeman@d2l.com>

* Using details provider factory to delegate creation of detail providers, and updates tests

Signed-off-by: aduric <adnan.duric@d2l.com>

* Rubocop fixes

Signed-off-by: aduric <adnan.duric@d2l.com>

* Rename user details provider factory to initializer, and remove unnecessary instance variables

Signed-off-by: sfreeman <Steffanie.Freeman@d2l.com>
2017-10-26 15:22:15 -04:00

108 lines
2.9 KiB
Ruby

# author: Adnan Duric
# author: Steffanie Freeman
# author: Simon Varlow
# author: Chris Redekop
require 'aws-sdk'
require 'helper'
require 'aws_iam_users'
class AwsIamUsersTest < Minitest::Test
def setup
@mock_user_factory = Minitest::Mock.new
@mock_user_details_provider_initializer = Minitest::Mock.new
end
def test_users_nil_user_provider_returns_empty_list
cut = AwsIamUsers.new(nil, nil, @mock_user_factory)
assert_equal(cut.users, [])
end
def test_users_empty_list_user_provider_returns_empty_list
cut = AwsIamUsers.new(
create_mock_user_provider,
create_mock_user_details_provider_ini,
@mock_user_factory,
)
assert_equal(cut.users, [])
end
def test_users_returns_true_for_all_users_if_mfa_enabled
cut = AwsIamUsers.new(
create_mock_user_provider(create_mock_users([true, true])),
create_mock_user_details_provider_ini,
@mock_user_factory,
)
cut.users.each do |user|
assert user.has_mfa_enabled?
end
end
[
{
name: 'test_where_returns_no_matching_rows',
user_material: [false],
}, {
name: 'test_where_returns_some_matching_rows',
user_material: [true, false],
}, {
name: 'test_where_returns_all_matching_rows',
user_material: [true],
}
].each do |test_material|
define_method(test_material[:name]) do
cut = create_cut(test_material)
results = cut.where(has_mfa_enabled?: true)
expected_count = test_material[:user_material].count { |x| x }
assert_equal expected_count > 0, results.exists?
assert_equal expected_count, results.entries.length
end
end
def create_cut(user_list = [])
mock_user_details_provider_ini = create_mock_user_details_provider_ini(
user_list[:user_material],
)
AwsIamUsers.new(
create_mock_user_provider(
user_list[:user_material],
),
mock_user_details_provider_ini,
@mock_user_factory,
)
end
def create_mock_user_details_provider_ini(attr_value_list = [])
mock_dets_provider_ini = Minitest::Mock.new
attr_value_list.each do |attr_val|
mock_dets_provider = Minitest::Mock.new
mock_dets_provider.expect :name, nil
mock_dets_provider.expect :has_mfa_enabled?, attr_val
mock_dets_provider.expect :has_console_password?, nil
mock_dets_provider.expect :access_keys, []
mock_dets_provider_ini.expect :create, mock_dets_provider, [Object]
end
mock_dets_provider_ini
end
def create_mock_user_provider(user_list = [])
mock_user_provider = Minitest::Mock.new
mock_user_provider.expect :list_users, user_list
mock_user_provider.expect :nil?, false
mock_user_provider
end
def create_mock_users(has_mfa_enableds = [])
has_mfa_enableds.map { |x| create_mock_user(x) }
end
def create_mock_user(has_mfa_enabled = true)
{ has_mfa_enabled?: has_mfa_enabled }
end
end