Skip to content

Commit

Permalink
adjust tests for session tagging feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mtskillman committed Aug 18, 2023
1 parent 83231af commit da671e1
Showing 1 changed file with 91 additions and 15 deletions.
106 changes: 91 additions & 15 deletions test/unit/awsume/awsumepy/test_default_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,38 @@
from awsume.awsumepy.lib import exceptions, autoawsume


def generate_namespace_with_defaults(
session_tags=None,
region=None,
source_profile=None,
auto_refresh=None,
clean=None,
version=None,
unset_variables=None,
config=None,
kill=None,
with_saml=None,
principal_arn=None,
profile_name=None,
**kwargs
) -> argparse.Namespace:
return argparse.Namespace(
session_tags=session_tags,
region=region,
source_profile=source_profile,
auto_refresh=auto_refresh,
clean=clean,
version=version,
unset_variables=unset_variables,
config=config,
kill=kill,
with_saml=with_saml,
principal_arn=principal_arn,
profile_name=profile_name,
**kwargs
)


def test_custom_duration_argument_type():
assert default_plugins.custom_duration_argument_type('43200') == 43200

Expand Down Expand Up @@ -131,7 +163,7 @@ def test_post_add_arguments_kill(safe_print: MagicMock, kill: MagicMock, stdout:
@patch.object(default_plugins, 'safe_print')
def test_post_add_arguments_role_arn_short(safe_print: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
role_arn='123123123123:myrole',
auto_refresh=False,
version=False,
Expand Down Expand Up @@ -198,7 +230,7 @@ def test_post_add_arguments_role_arn_short_bad_number_parts(safe_print: MagicMoc
@patch.object(default_plugins, 'safe_print')
def test_post_add_arguments_role_arn_explicit(safe_print: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
role_arn='arn:aws:iam::123123123123:role/myrole',
auto_refresh=False,
version=False,
Expand All @@ -218,7 +250,7 @@ def test_post_add_arguments_role_arn_explicit(safe_print: MagicMock):
@patch.object(default_plugins, 'safe_print')
def test_post_add_arguments_set_target_profile_name(safe_print: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
role_arn=None,
auto_refresh=False,
version=False,
Expand All @@ -239,7 +271,7 @@ def test_post_add_arguments_set_target_profile_name(safe_print: MagicMock):
@patch.object(default_plugins, 'safe_print')
def test_post_add_arguments_set_target_profile_name_default(safe_print: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
role_arn=None,
auto_refresh=False,
version=False,
Expand All @@ -260,7 +292,7 @@ def test_post_add_arguments_set_target_profile_name_default(safe_print: MagicMoc
@patch.object(default_plugins, 'safe_print')
def test_post_add_arguments_set_target_profile_name_role_arn(safe_print: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
role_arn='arn:aws:iam::123123123123:role/myrole',
auto_refresh=False,
version=False,
Expand Down Expand Up @@ -376,7 +408,7 @@ def test_post_collect_aws_profiles_no_list(profile_lib: MagicMock):
@patch.object(default_plugins, 'aws_lib')
def test_assume_role_from_cli(aws_lib: MagicMock, profile_lib: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
role_duration=None,
session_name=None,
source_profile=None,
Expand All @@ -392,14 +424,15 @@ def test_assume_role_from_cli(aws_lib: MagicMock, profile_lib: MagicMock):
region=profile_lib.get_region.return_value,
external_id=arguments.external_id,
role_duration=0,
tags=None,
)


@patch.object(default_plugins, 'profile_lib')
@patch.object(default_plugins, 'aws_lib')
def test_assume_role_from_cli_source_profile(aws_lib: MagicMock, profile_lib: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
role_duration=None,
session_name=None,
source_profile='mysource',
Expand Down Expand Up @@ -428,14 +461,15 @@ def test_assume_role_from_cli_source_profile(aws_lib: MagicMock, profile_lib: Ma
region=profile_lib.get_region.return_value,
external_id=arguments.external_id,
role_duration=0,
tags=None
)


@patch.object(default_plugins, 'profile_lib')
@patch.object(default_plugins, 'aws_lib')
def test_assume_role_from_cli_source_profile_role_duration_mfa(aws_lib: MagicMock, profile_lib: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
role_duration='43200',
session_name=None,
source_profile='mysource',
Expand Down Expand Up @@ -466,14 +500,15 @@ def test_assume_role_from_cli_source_profile_role_duration_mfa(aws_lib: MagicMoc
role_duration='43200',
mfa_serial='mymfaserial',
mfa_token='123123',
tags=None,
)


@patch.object(default_plugins, 'profile_lib')
@patch.object(default_plugins, 'aws_lib')
def test_assume_role_from_cli_source_profile_role_duration_no_mfa(aws_lib: MagicMock, profile_lib: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
role_duration='43200',
session_name=None,
source_profile='mysource',
Expand Down Expand Up @@ -501,14 +536,15 @@ def test_assume_role_from_cli_source_profile_role_duration_no_mfa(aws_lib: Magic
region=profile_lib.get_region.return_value,
external_id=arguments.external_id,
role_duration='43200',
tags=None,
)


@patch.object(default_plugins, 'profile_lib')
@patch.object(default_plugins, 'aws_lib')
def test_assume_role_from_cli_source_profile_no_role_duration_mfa(aws_lib: MagicMock, profile_lib: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
role_duration=None,
session_name=None,
source_profile='mysource',
Expand Down Expand Up @@ -544,14 +580,15 @@ def test_assume_role_from_cli_source_profile_no_role_duration_mfa(aws_lib: Magic
region=profile_lib.get_region.return_value,
external_id=arguments.external_id,
role_duration=0,
tags=None,
)


@patch.object(default_plugins, 'profile_lib')
@patch.object(default_plugins, 'aws_lib')
def test_assume_role_from_cli_source_profile_no_role_duration_no_mfa(aws_lib: MagicMock, profile_lib: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
role_duration=None,
session_name=None,
source_profile='mysource',
Expand Down Expand Up @@ -579,6 +616,7 @@ def test_assume_role_from_cli_source_profile_no_role_duration_no_mfa(aws_lib: Ma
region=profile_lib.get_region.return_value,
external_id=arguments.external_id,
role_duration=0,
tags=None,
)


Expand Down Expand Up @@ -610,7 +648,7 @@ def test_assume_role_from_cli_source_profile_not_found(aws_lib: MagicMock, profi
@patch.object(default_plugins, 'aws_lib')
def test_get_credentials(aws_lib: MagicMock, create_autoawsume_profile: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
target_profile_name='role',
external_id='myexternalid',
role_duration=None,
Expand Down Expand Up @@ -650,6 +688,7 @@ def test_get_credentials(aws_lib: MagicMock, create_autoawsume_profile: MagicMoc
region=None,
external_id='myexternalid',
role_duration=0,
tags=None,
)
assert result == aws_lib.assume_role.return_value

Expand All @@ -658,7 +697,7 @@ def test_get_credentials(aws_lib: MagicMock, create_autoawsume_profile: MagicMoc
@patch.object(default_plugins, 'aws_lib')
def test_get_credentials_auto_refresh(aws_lib: MagicMock, create_autoawsume_profile: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
target_profile_name='role',
external_id='myexternalid',
role_duration=None,
Expand Down Expand Up @@ -701,6 +740,7 @@ def test_get_credentials_auto_refresh(aws_lib: MagicMock, create_autoawsume_prof
region=None,
external_id='myexternalid',
role_duration=0,
tags=None
)
assert result == aws_lib.assume_role.return_value

Expand All @@ -709,7 +749,7 @@ def test_get_credentials_auto_refresh(aws_lib: MagicMock, create_autoawsume_prof
@patch.object(default_plugins, 'aws_lib')
def test_get_credentials_role_duration(aws_lib: MagicMock, create_autoawsume_profile: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
target_profile_name='role',
external_id='myexternalid',
role_duration='43200',
Expand Down Expand Up @@ -744,6 +784,7 @@ def test_get_credentials_role_duration(aws_lib: MagicMock, create_autoawsume_pro
role_duration=43200,
mfa_serial='mymfaserial',
mfa_token='123123',
tags=None,
)
assert result == aws_lib.assume_role.return_value

Expand Down Expand Up @@ -823,7 +864,7 @@ def test_get_credentials_user(aws_lib: MagicMock, create_autoawsume_profile: Mag
@patch.object(default_plugins, 'aws_lib')
def test_get_credentials_no_mfa_role(aws_lib: MagicMock, create_autoawsume_profile: MagicMock):
config = {}
arguments = argparse.Namespace(
arguments = generate_namespace_with_defaults(
target_profile_name='role',
external_id='myexternalid',
role_duration=None,
Expand Down Expand Up @@ -855,6 +896,7 @@ def test_get_credentials_no_mfa_role(aws_lib: MagicMock, create_autoawsume_profi
region=None,
external_id='myexternalid',
role_duration=0,
tags=None,
)
assert result == aws_lib.assume_role.return_value

Expand Down Expand Up @@ -928,3 +970,37 @@ def test_get_credentials_role_from_cli(aws_lib: MagicMock, create_autoawsume_pro

result = default_plugins.get_credentials(config, arguments, profiles)
assert result == assume_role_from_cli.return_value


@patch.object(default_plugins, 'aws_lib')
def test_post_add_arguments_session_tags(aws_lib: MagicMock):
config = {}
arguments = generate_namespace_with_defaults(
role_duration='43200',
session_name=None,
role_arn='arn:aws:iam::123123123123:role/myrole',
external_id=None,
mfa_token=None,
force_refresh=False,
session_tags='foo=bar,biz=baz'
)
parser = argparse.ArgumentParser()
default_plugins.post_add_arguments(config, arguments, parser)
default_plugins.get_credentials(config, arguments, {})
aws_lib.assume_role.assert_called_with(
config,
arguments.role_arn,
'awsume-cli-role',
external_id=arguments.external_id,
role_duration='43200',
tags=[
{
'Key': 'foo',
'Value': 'bar',
}, {
'Key': 'biz',
'Value': 'baz',
}
],
region=arguments.region,
)

0 comments on commit da671e1

Please sign in to comment.