Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using botocore.stub.Stubber to mock AWS #16

Merged
merged 1 commit into from
Nov 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 94 additions & 17 deletions tests/test_s3_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,115 @@
import json
import pytest
from unittest.mock import call, Mock
from botocore.stub import Stubber


def test_whoami(mocker):
boto3 = mocker.patch("boto3.client")
boto3().get_user.return_value = {"User": {"username": "name"}}
@pytest.fixture
def stub_client(mocker):
client = botocore.session.get_session().create_client("iam")
stubber = Stubber(client)
stubber.activate()
mocker.patch("s3_credentials.cli.make_client", return_value=client)
return stubber


def test_whoami(mocker, stub_client):
stub_client.add_response(
"get_user",
{
"User": {
"Path": "/",
"UserName": "Name",
"UserId": "AID000000000000000000",
"Arn": "arn:aws:iam::000000000000:user/Name",
"CreateDate": "2020-01-01 00:00:00+00:00",
}
},
)

runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(cli, ["whoami"])
assert result.exit_code == 0
assert json.loads(result.output) == {"username": "name"}
assert json.loads(result.output) == {
"Path": "/",
"UserName": "Name",
"UserId": "AID000000000000000000",
"Arn": "arn:aws:iam::000000000000:user/Name",
"CreateDate": "2020-01-01 00:00:00+00:00",
}


@pytest.mark.parametrize(
"option,expected",
(
("", '{\n "name": "one"\n}\n{\n "name": "two"\n}\n'),
(
"",
"{\n"
' "Path": "/",\n'
' "UserName": "NameA",\n'
' "UserId": "AID000000000000000001",\n'
' "Arn": "arn:aws:iam::000000000000:user/NameB",\n'
' "CreateDate": "2020-01-01 00:00:00+00:00"\n'
"}\n"
"{\n"
' "Path": "/",\n'
' "UserName": "NameA",\n'
' "UserId": "AID000000000000000000",\n'
' "Arn": "arn:aws:iam::000000000000:user/NameB",\n'
' "CreateDate": "2020-01-01 00:00:00+00:00"\n'
"}\n",
),
(
"--array",
'[\n {\n "name": "one"\n },\n'
' {\n "name": "two"\n }\n]\n',
"[\n"
" {\n"
' "Path": "/",\n'
' "UserName": "NameA",\n'
' "UserId": "AID000000000000000001",\n'
' "Arn": "arn:aws:iam::000000000000:user/NameB",\n'
' "CreateDate": "2020-01-01 00:00:00+00:00"\n'
" },\n"
" {\n"
' "Path": "/",\n'
' "UserName": "NameA",\n'
' "UserId": "AID000000000000000000",\n'
' "Arn": "arn:aws:iam::000000000000:user/NameB",\n'
' "CreateDate": "2020-01-01 00:00:00+00:00"\n'
" }\n"
"]\n"
"",
),
(
"--nl",
'{"Path": "/", "UserName": "NameA", "UserId": "AID000000000000000001", "Arn": "arn:aws:iam::000000000000:user/NameB", "CreateDate": "2020-01-01 00:00:00+00:00"}\n'
'{"Path": "/", "UserName": "NameA", "UserId": "AID000000000000000000", "Arn": "arn:aws:iam::000000000000:user/NameB", "CreateDate": "2020-01-01 00:00:00+00:00"}\n',
),
("--nl", '{"name": "one"}\n{"name": "two"}\n'),
),
)
def test_list_users(mocker, option, expected):
boto3 = mocker.patch("boto3.client")
boto3().get_paginator().paginate.return_value = [
{"Users": [{"name": "one"}, {"name": "two"}]}
]
def test_list_users(mocker, option, expected, stub_client):
stub_client.add_response(
"list_users",
{
"Users": [
{
"Path": "/",
"UserName": "NameA",
"UserId": "AID000000000000000001",
"Arn": "arn:aws:iam::000000000000:user/NameB",
"CreateDate": "2020-01-01 00:00:00+00:00",
},
{
"Path": "/",
"UserName": "NameA",
"UserId": "AID000000000000000000",
"Arn": "arn:aws:iam::000000000000:user/NameB",
"CreateDate": "2020-01-01 00:00:00+00:00",
},
]
},
)

runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(cli, ["list-users"] + ([option] if option else []))
Expand Down Expand Up @@ -85,10 +165,7 @@ def test_create(
boto3 = mocker.patch("boto3.client")
boto3.return_value = Mock()
boto3.return_value.create_access_key.return_value = {
"AccessKey": {
"AccessKeyId": "access",
"SecretAccessKey": "secret",
}
"AccessKey": {"AccessKeyId": "access", "SecretAccessKey": "secret"}
}
runner = CliRunner()
with runner.isolated_filesystem():
Expand Down