Skip to content

Commit e6002aa

Browse files
author
Mat Lord
committed
chore(tests): Add aws client tests
1 parent c752d57 commit e6002aa

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

staxapp/auth.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,23 @@ def sts_from_cognito_identity_pool(self, token, cognito_client=None, **kwargs):
8383
region_name=self.aws_region,
8484
config=BotoConfig(signature_version=UNSIGNED),
8585
)
86-
id = cognito_client.get_id(
87-
IdentityPoolId=self.identity_pool,
88-
Logins={
89-
f"cognito-idp.{self.aws_region}.amazonaws.com/{self.user_pool}": token
90-
},
91-
)
92-
id_creds = cognito_client.get_credentials_for_identity(
93-
IdentityId=id["IdentityId"],
94-
Logins={
95-
f"cognito-idp.{self.aws_region}.amazonaws.com/{self.user_pool}": token
96-
},
97-
)
86+
try:
87+
id = cognito_client.get_id(
88+
IdentityPoolId=self.identity_pool,
89+
Logins={
90+
f"cognito-idp.{self.aws_region}.amazonaws.com/{self.user_pool}": token
91+
},
92+
)
93+
id_creds = cognito_client.get_credentials_for_identity(
94+
IdentityId=id["IdentityId"],
95+
Logins={
96+
f"cognito-idp.{self.aws_region}.amazonaws.com/{self.user_pool}": token
97+
},
98+
)
99+
except ClientError as e:
100+
raise InvalidCredentialsException(
101+
f"Unexpected Client Error. Error details: {e}"
102+
)
98103
return id_creds
99104

100105
def sigv4_signed_auth_headers(self, id_creds):

tests/test_auth.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,20 @@ def testToken(self):
5959
sa = StaxAuth("ApiAuth")
6060
self.stub_aws_srp(sa, "valid_username")
6161
token = sa.id_token_from_cognito(
62-
username="valid_username", password="correct", srp_client=self.aws_srp_client
62+
username="valid_username",
63+
password="correct",
64+
srp_client=self.aws_srp_client,
6365
)
6466
self.assertEqual(token, "valid_token")
6567

68+
def testTokenClient(self):
69+
"""
70+
Test the AWSSRP client is invoked and throws an error
71+
"""
72+
sa = StaxAuth("ApiAuth")
73+
with self.assertRaises(InvalidCredentialsException):
74+
sa.id_token_from_cognito(username="username", password="password")
75+
6676
def testCredentialErrors(self):
6777
"""
6878
Test that boto errors are caught and converted to InvalidCredentialExceptions
@@ -73,7 +83,9 @@ def testCredentialErrors(self):
7383
user_not_found_success = False
7484
try:
7585
sa.id_token_from_cognito(
76-
username="bad_password", password="wrong", srp_client=self.aws_srp_client
86+
username="bad_password",
87+
password="wrong",
88+
srp_client=self.aws_srp_client,
7789
)
7890
except InvalidCredentialsException as e:
7991
self.assertIn("Please check your Secret Key is correct", e.message)
@@ -116,6 +128,16 @@ def testCreds(self):
116128
self.assertIn("Credentials", creds)
117129
self.assertTrue(creds.get("IdentityId").startswith("ap-southeast-2"))
118130

131+
def testCredsClient(self):
132+
"""
133+
Test the cognito client is invoked and throws an error
134+
"""
135+
sa = StaxAuth("ApiAuth")
136+
token = jwt.encode({"sub": "unittest"}, "secret", algorithm="HS256")
137+
jwt_token = jwt.decode(token, verify=False)
138+
with self.assertRaises(InvalidCredentialsException):
139+
sa.sts_from_cognito_identity_pool(jwt_token.get("sub"))
140+
119141
def testAuthErrors(self):
120142
"""
121143
Test that errors are thrown when keys are invalid

0 commit comments

Comments
 (0)