1616from botocore .stub import Stubber , ANY
1717from datetime import datetime , timedelta , timezone
1818
19+ from staxapp .api import Api
1920from staxapp .auth import StaxAuth , ApiTokenAuth , RootAuth
2021from staxapp .config import Config
2122from staxapp .exceptions import InvalidCredentialsException
@@ -59,10 +60,20 @@ def testToken(self):
5960 sa = StaxAuth ("ApiAuth" )
6061 self .stub_aws_srp (sa , "valid_username" )
6162 token = sa .id_token_from_cognito (
62- username = "valid_username" , password = "correct" , srp_client = self .aws_srp_client
63+ username = "valid_username" ,
64+ password = "correct" ,
65+ srp_client = self .aws_srp_client ,
6366 )
6467 self .assertEqual (token , "valid_token" )
6568
69+ def testTokenClient (self ):
70+ """
71+ Test the AWSSRP client is invoked and throws an error
72+ """
73+ sa = StaxAuth ("ApiAuth" )
74+ with self .assertRaises (InvalidCredentialsException ):
75+ sa .id_token_from_cognito (username = "username" , password = "password" )
76+
6677 def testCredentialErrors (self ):
6778 """
6879 Test that boto errors are caught and converted to InvalidCredentialExceptions
@@ -73,10 +84,12 @@ def testCredentialErrors(self):
7384 user_not_found_success = False
7485 try :
7586 sa .id_token_from_cognito (
76- username = "bad_password" , password = "wrong" , srp_client = self .aws_srp_client
87+ username = "bad_password" ,
88+ password = "wrong" ,
89+ srp_client = self .aws_srp_client ,
7790 )
7891 except InvalidCredentialsException as e :
79- self .assertIn ("Please check your Secret Key is correct" , e . message )
92+ self .assertIn ("Please check your Secret Key is correct" , str ( e ) )
8093 user_not_found_success = True
8194 self .assertTrue (user_not_found_success )
8295
@@ -90,7 +103,7 @@ def testCredentialErrors(self):
90103 except InvalidCredentialsException as e :
91104 self .assertIn (
92105 "Please check your Access Key, that you have created your Api Token and that you are using the right STAX REGION" ,
93- e . message ,
106+ str ( e ) ,
94107 )
95108 no_access_success = True
96109 self .assertTrue (no_access_success )
@@ -116,6 +129,16 @@ def testCreds(self):
116129 self .assertIn ("Credentials" , creds )
117130 self .assertTrue (creds .get ("IdentityId" ).startswith ("ap-southeast-2" ))
118131
132+ def testCredsClient (self ):
133+ """
134+ Test the cognito client is invoked and throws an error
135+ """
136+ sa = StaxAuth ("ApiAuth" )
137+ token = jwt .encode ({"sub" : "unittest" }, "secret" , algorithm = "HS256" )
138+ jwt_token = jwt .decode (token , verify = False )
139+ with self .assertRaises (InvalidCredentialsException ):
140+ sa .sts_from_cognito_identity_pool (jwt_token .get ("sub" ))
141+
119142 def testAuthErrors (self ):
120143 """
121144 Test that errors are thrown when keys are invalid
@@ -289,6 +312,28 @@ def testRootAuth(self):
289312 )
290313 self .assertIsNotNone (StaxConfig .auth )
291314
315+ def testApiAuth (self ):
316+ """
317+ Test auth through the Api class
318+ """
319+ sa = StaxAuth ("ApiAuth" )
320+ StaxConfig = Config
321+ StaxConfig .expiration = None
322+ StaxConfig .access_key = "username"
323+ StaxConfig .secret_key = "password"
324+
325+ token = jwt .encode ({"sub" : "valid_token" }, "secret" , algorithm = "HS256" )
326+ jwt_token = jwt .decode (token , verify = False )
327+
328+ self .stub_cognito_creds (sa , jwt_token .get ("sub" ))
329+ self .stub_aws_srp (sa , "username" )
330+
331+ Api ._requests_auth = None
332+ Api ._auth (
333+ srp_client = self .aws_srp_client , cognito_client = self .cognito_client ,
334+ )
335+ self .assertIsNotNone (Api ._requests_auth )
336+
292337
293338if __name__ == "__main__" :
294339 unittest .main ()
0 commit comments