-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI args for aws account id and aws principals (#178)
* Add command line options for aws_account_id and aws_principals * Update AWS Account IDs in a test to be 12-digit numbers * Update cli.md docs with aws-account-id and aws-principals args
- Loading branch information
Showing
6 changed files
with
201 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from unittest.mock import MagicMock, patch | ||
|
||
import click | ||
import pytest | ||
from click.testing import CliRunner | ||
|
||
import cfripper.cli as undertest | ||
from tests.utils import FIXTURE_ROOT_PATH | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"aws_account_id_arg, validation_result", [(None, None), ("", None), ("123456789012", "123456789012")], | ||
) | ||
def test_validate_aws_account_id( | ||
aws_account_id_arg, validation_result, | ||
): | ||
fake_command = click.Command("fake_command") | ||
fake_context = click.Context(fake_command) | ||
fake_param = "fake_param" | ||
assert undertest.validate_aws_account_id(fake_context, fake_param, aws_account_id_arg) == validation_result | ||
|
||
|
||
def test_validate_aws_account_id_with_malformed_arg(): | ||
fake_command = click.Command("fake_command") | ||
fake_context = click.Context(fake_command) | ||
fake_param = "fake_param" | ||
|
||
with pytest.raises(click.BadParameter): | ||
undertest.validate_aws_account_id(fake_context, fake_param, "malformed aws account id") | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"aws_principals_arg, validation_result", | ||
[ | ||
(None, None), | ||
("", None), | ||
("123456789012", ["123456789012"]), | ||
( | ||
"arn:aws:iam::123456789012:root,234567890123,arn:aws:iam::111222333444:user/user-name", | ||
["arn:aws:iam::123456789012:root", "234567890123", "arn:aws:iam::111222333444:user/user-name"], | ||
), | ||
], | ||
) | ||
def test_validate_aws_principals( | ||
aws_principals_arg, validation_result, | ||
): | ||
fake_command = click.Command("fake_command") | ||
fake_context = click.Context(fake_command) | ||
fake_param = "fake_param" | ||
assert undertest.validate_aws_principals(fake_context, fake_param, aws_principals_arg) == validation_result | ||
|
||
|
||
@patch("cfripper.cli.process_template") | ||
def test_aws_account_id_cli_option(patched_process_template: MagicMock): | ||
patched_process_template.return_value = True | ||
test_template_path = str(FIXTURE_ROOT_PATH) + "/others/iam_role.json" | ||
fake_aws_account_id = "123456789012" | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(undertest.cli, ["--aws-account-id", fake_aws_account_id, test_template_path]) | ||
assert patched_process_template.call_count == 1 | ||
assert patched_process_template.call_args[1]["aws_account_id"] == fake_aws_account_id | ||
assert result.exit_code == 0 | ||
|
||
|
||
@patch("cfripper.cli.process_template") | ||
def test_aws_principles_cli_option(patched_process_template: MagicMock): | ||
patched_process_template.return_value = True | ||
test_template_path = str(FIXTURE_ROOT_PATH) + "/others/iam_role.json" | ||
fake_aws_principals = ["123456789012", "234567890123"] | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(undertest.cli, ["--aws-principals", ",".join(fake_aws_principals), test_template_path]) | ||
assert patched_process_template.call_count == 1 | ||
assert patched_process_template.call_args[1]["aws_principals"] == fake_aws_principals | ||
assert result.exit_code == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters