|
| 1 | +import pytest |
| 2 | +from source_amazon_seller_partner import SourceAmazonSellerPartner |
| 3 | +from source_amazon_seller_partner import ConnectorConfig |
| 4 | +from airbyte_cdk.models import ConnectorSpecification |
| 5 | +from airbyte_cdk.sources.streams import Stream |
| 6 | +from unittest.mock import MagicMock |
| 7 | +from source_amazon_seller_partner.source import boto3 |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def connector_source(): |
| 12 | + return SourceAmazonSellerPartner() |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def connector_config(): |
| 17 | + return ConnectorConfig( |
| 18 | + replication_start_date="2017-01-25T00:00:00Z", |
| 19 | + refresh_token="Atzr|IwEBIP-abc123", |
| 20 | + lwa_app_id="amzn1.application-oa2-client.abc123", |
| 21 | + lwa_client_secret="abc123", |
| 22 | + aws_access_key="aws_access_key", |
| 23 | + aws_secret_key="aws_secret_key", |
| 24 | + role_arn="arn:aws:iam::123456789098:role/some-role", |
| 25 | + aws_environment="SANDBOX", |
| 26 | + region="US" |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def sts_credentials(): |
| 32 | + return { |
| 33 | + "Credentials": { |
| 34 | + "AccessKeyId": "foo", |
| 35 | + "SecretAccessKey": "bar", |
| 36 | + "SessionToken": "foobar", |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def mock_boto_client(mocker, sts_credentials): |
| 43 | + boto_client = MagicMock() |
| 44 | + mocker.patch.object(boto3, "client", return_value=boto_client) |
| 45 | + boto_client.assume_role.return_value = sts_credentials |
| 46 | + boto_client.get_session_token.return_value = sts_credentials |
| 47 | + return boto_client |
| 48 | + |
| 49 | + |
| 50 | +def test_spec(connector_source): |
| 51 | + assert isinstance(connector_source.spec(), ConnectorSpecification) |
| 52 | + |
| 53 | + |
| 54 | +def test_streams(connector_source, connector_config, mock_boto_client): |
| 55 | + for stream in connector_source.streams(connector_config): |
| 56 | + assert isinstance(stream, Stream) |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize( |
| 60 | + "arn", |
| 61 | + ("arn:aws:iam::123456789098:user/some-user", "arn:aws:iam::123456789098:role/some-role") |
| 62 | +) |
| 63 | +def test_stream_with_good_iam_arn_value(mock_boto_client, connector_source, connector_config, arn): |
| 64 | + connector_config.role_arn = arn |
| 65 | + result = connector_source.get_sts_credentials(connector_config) |
| 66 | + assert "Credentials" in result |
| 67 | + if "user" in arn: |
| 68 | + mock_boto_client.get_session_token.assert_called_once() |
| 69 | + if "role" in arn: |
| 70 | + mock_boto_client.assume_role.assert_called_once_with(RoleArn=arn, RoleSessionName="guid") |
| 71 | + |
| 72 | + |
| 73 | +def test_stream_with_bad_iam_arn_value(connector_source, connector_config, mock_boto_client): |
| 74 | + connector_config.role_arn = "bad-arn" |
| 75 | + with pytest.raises(ValueError) as e: |
| 76 | + connector_source.get_sts_credentials(connector_config) |
| 77 | + assert "Invalid" in e.message |
0 commit comments