Skip to content

Commit

Permalink
Mocked test for create happy path, refs #3
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Nov 3, 2021
1 parent 56a36bc commit 411358a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion s3_credentials/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def log(message):
UserName=username,
)
log("Created access key for user: {}".format(username))
click.echo(response)
click.echo(json.dumps(response, indent=4, default=str))


@cli.command()
Expand Down
27 changes: 27 additions & 0 deletions tests/test_s3_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from s3_credentials.cli import cli
import json
import pytest
from unittest.mock import call, Mock


def test_whoami(mocker):
Expand Down Expand Up @@ -36,3 +37,29 @@ def test_list_users(mocker, option, expected):
result = runner.invoke(cli, ["list-users"] + ([option] if option else []))
assert result.exit_code == 0
assert result.output == expected


def test_create(mocker):
boto3 = mocker.patch("boto3.client")
boto3.return_value = Mock()
boto3.return_value.create_access_key.return_value = {
"Key": "key",
"Secret": "secret",
}
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(cli, ["create", "pytest-bucket-simonw-1", "-c"])
assert result.exit_code == 0
assert result.output == (
"Attached policy s3.read-write.pytest-bucket-simonw-1 to user s3.read-write.pytest-bucket-simonw-1\n"
"Created access key for user: s3.read-write.pytest-bucket-simonw-1\n"
'{\n "Key": "key",\n "Secret": "secret"\n}\n'
)
assert [str(c) for c in boto3.mock_calls] == [
"call('s3')",
"call('iam')",
"call().head_bucket(Bucket='pytest-bucket-simonw-1')",
"call().get_user(UserName='s3.read-write.pytest-bucket-simonw-1')",
'call().put_user_policy(PolicyDocument=\'{"Version": "2012-10-17", "Statement": [{"Sid": "ListObjectsInBucket", "Effect": "Allow", "Action": ["s3:ListBucket"], "Resource": ["arn:aws:s3:::pytest-bucket-simonw-1"]}, {"Sid": "AllObjectActions", "Effect": "Allow", "Action": "s3:*Object", "Resource": ["arn:aws:s3:::pytest-bucket-simonw-1/*"]}]}\', PolicyName=\'s3.read-write.pytest-bucket-simonw-1\', UserName=\'s3.read-write.pytest-bucket-simonw-1\')',
"call().create_access_key(UserName='s3.read-write.pytest-bucket-simonw-1')",
]

0 comments on commit 411358a

Please sign in to comment.