|
| 1 | +import pytest |
| 2 | +import responses |
| 3 | + |
| 4 | +from posit.connect import Client |
| 5 | +from posit.connect.external.aws import get_aws_credentials |
| 6 | + |
| 7 | +aws_creds = { |
| 8 | + "accessKeyId": "abc123", |
| 9 | + "secretAccessKey": "def456", |
| 10 | + "sessionToken": "ghi789", |
| 11 | + "expiration": "2025-01-01T00:00:00Z", |
| 12 | +} |
| 13 | + |
| 14 | +encoded_aws_creds = "eyJhY2Nlc3NLZXlJZCI6ICJhYmMxMjMiLCAic2VjcmV0QWNjZXNzS2V5IjogImRlZjQ1NiIsICJzZXNzaW9uVG9rZW4iOiAiZ2hpNzg5IiwgImV4cGlyYXRpb24iOiAiMjAyNS0wMS0wMVQwMDowMDowMFoifQ==" |
| 15 | + |
| 16 | + |
| 17 | +class TestAWS: |
| 18 | + @responses.activate |
| 19 | + def test_get_aws_credentials(self): |
| 20 | + responses.post( |
| 21 | + "https://connect.example/__api__/v1/oauth/integrations/credentials", |
| 22 | + match=[ |
| 23 | + responses.matchers.urlencoded_params_matcher( |
| 24 | + { |
| 25 | + "grant_type": "urn:ietf:params:oauth:grant-type:token-exchange", |
| 26 | + "subject_token_type": "urn:posit:connect:user-session-token", |
| 27 | + "subject_token": "cit", |
| 28 | + "requested_token_type": "urn:ietf:params:aws:token-type:credentials", |
| 29 | + } |
| 30 | + ) |
| 31 | + ], |
| 32 | + json={ |
| 33 | + "access_token": encoded_aws_creds, |
| 34 | + "issued_token_type": "urn:ietf:params:aws:token-type:credentials", |
| 35 | + "token_type": "aws_credentials", |
| 36 | + }, |
| 37 | + ) |
| 38 | + |
| 39 | + c = Client(api_key="12345", url="https://connect.example/") |
| 40 | + c._ctx.version = None |
| 41 | + response = get_aws_credentials(c, "cit") |
| 42 | + |
| 43 | + assert response == aws_creds |
| 44 | + |
| 45 | + @responses.activate |
| 46 | + def test_get_aws_credentials_no_token(self): |
| 47 | + responses.post( |
| 48 | + "https://connect.example/__api__/v1/oauth/integrations/credentials", |
| 49 | + match=[ |
| 50 | + responses.matchers.urlencoded_params_matcher( |
| 51 | + { |
| 52 | + "grant_type": "urn:ietf:params:oauth:grant-type:token-exchange", |
| 53 | + "subject_token_type": "urn:posit:connect:user-session-token", |
| 54 | + "subject_token": "cit", |
| 55 | + "requested_token_type": "urn:ietf:params:aws:token-type:credentials", |
| 56 | + } |
| 57 | + ) |
| 58 | + ], |
| 59 | + json={}, |
| 60 | + ) |
| 61 | + |
| 62 | + c = Client(api_key="12345", url="https://connect.example/") |
| 63 | + c._ctx.version = None |
| 64 | + |
| 65 | + with pytest.raises(ValueError) as e: |
| 66 | + get_aws_credentials(c, "cit") |
| 67 | + |
| 68 | + assert e.match("No access token found in credentials") |
0 commit comments