1212
1313from botocore import UNSIGNED
1414from botocore .client import Config as BotoConfig
15- from botocore .stub import Stubber
15+ from botocore .stub import Stubber , ANY
1616
1717from staxapp .auth import StaxAuth
1818from staxapp .exceptions import InvalidCredentialsException
1919
20+
2021class StaxAuthTests (unittest .TestCase ):
2122 """
2223 Inherited class to run all unit tests for this module
@@ -30,8 +31,16 @@ def setUp(self):
3031 )
3132 self .cognito_stub = Stubber (self .cognito_client )
3233
34+ self .aws_srp_client = botocore .session .get_session ().create_client (
35+ "cognito-idp" ,
36+ region_name = "ap-southeast-2" ,
37+ config = BotoConfig (signature_version = UNSIGNED ),
38+ )
39+ self .aws_srp_stubber = Stubber (self .aws_srp_client )
40+
3341 def tearDown (self ):
3442 self .cognito_stub .deactivate ()
43+ self .aws_srp_stubber .deactivate ()
3544
3645 def testStaxAuthInit (self ):
3746 """
@@ -45,16 +54,58 @@ def testToken(self):
4554 Test valid JWT is returned
4655 """
4756 sa = StaxAuth ("ApiAuth" )
48- token = sa .id_token_from_cognito ()
49- jwt_token = jwt .decode (token , verify = False )
50- self .assertIn ("sub" , jwt_token )
57+ self .stub_aws_srp (sa , "valid_username" )
58+ token = sa .id_token_from_cognito (
59+ username = "valid_username" , password = "correct" , client = self .aws_srp_client
60+ )
61+ self .assertEqual (token , "valid_token" )
62+
63+ def testCredentialErrors (self ):
64+ """
65+ Test that boto errors are caught and converted to InvalidCredentialExceptions
66+ """
67+
68+ sa = StaxAuth ("ApiAuth" )
69+ # Test with invalid username password
70+ self .stub_aws_srp (sa , "bad_password" , "UserNotFoundException" )
71+ user_not_found_success = False
72+ try :
73+ sa .id_token_from_cognito (
74+ username = "bad_password" , password = "wrong" , client = self .aws_srp_client
75+ )
76+ except InvalidCredentialsException as e :
77+ self .assertIn ("Please check your Secret Key is correct" , e .message )
78+ user_not_found_success = True
79+ self .assertTrue (user_not_found_success )
80+
81+ # Test with no access
82+ self .stub_aws_srp (sa , "no_access" , "NotAuthorizedException" )
83+ no_access_success = False
84+ try :
85+ sa .id_token_from_cognito (
86+ username = "no_access" , password = "wrong" , client = self .aws_srp_client
87+ )
88+ except InvalidCredentialsException as e :
89+ self .assertIn (
90+ "Please check your Access Key, that you have created your Api Token and that you are using the right STAX REGION" ,
91+ e .message ,
92+ )
93+ no_access_success = True
94+ self .assertTrue (no_access_success )
95+
96+ # Test Unknown Error
97+ self .stub_aws_srp (sa , "Unknown" , "UnitTesting" )
98+ with self .assertRaises (InvalidCredentialsException ):
99+ sa .id_token_from_cognito (
100+ username = "Unknown" , password = "wrong" , client = self .aws_srp_client
101+ )
51102
52103 def testCreds (self ):
53104 """
54105 Test valid credentials are returned
55106 """
56107 sa = StaxAuth ("ApiAuth" )
57- token = sa . id_token_from_cognito ( )
108+ token = jwt . encode ({ "sub" : "unittest" }, "secret" , algorithm = "HS256" )
58109 jwt_token = jwt .decode (token , verify = False )
59110 self .stub_cognito_creds (jwt_token .get ("sub" ))
60111 creds = sa .sts_from_cognito_identity_pool (
@@ -63,19 +114,55 @@ def testCreds(self):
63114 self .assertIn ("Credentials" , creds )
64115 self .assertTrue (creds .get ("IdentityId" ).startswith ("ap-southeast-2" ))
65116
66- def testCredentialErrors (self ):
67- """
117+ def testAuthErrors (self ):
118+ """
68119 Test that errors are thrown when keys are invalid
69120 """
70- sa = StaxAuth ("ApiAuth" )
71- # Test with no username
72- with self .assertRaises (InvalidCredentialsException ):
73- sa .requests_auth (username = None , password = 'valid' )
74- # Test with no username
75- with self .assertRaises (InvalidCredentialsException ):
76- sa .requests_auth (username = 'valid' , password = None )
77- # Test with invalid username password
78- # Todo
121+ sa = StaxAuth ("ApiAuth" )
122+ # Test with no username
123+ with self .assertRaises (InvalidCredentialsException ):
124+ sa .requests_auth (username = None , password = "valid" )
125+
126+ # Test with no username
127+ with self .assertRaises (InvalidCredentialsException ):
128+ sa .requests_auth (username = "valid" , password = None )
129+
130+ def stub_aws_srp (self , stax_auth , username , error_code = None ):
131+ expected_parameters = {
132+ "AuthFlow" : "USER_SRP_AUTH" ,
133+ "AuthParameters" : {"SRP_A" : ANY , "USERNAME" : username },
134+ "ClientId" : stax_auth .client_id ,
135+ }
136+ if error_code :
137+ self .aws_srp_stubber .add_client_error (
138+ "initiate_auth" ,
139+ service_error_code = error_code ,
140+ expected_params = expected_parameters ,
141+ )
142+ else :
143+ self .aws_srp_stubber .add_response (
144+ "initiate_auth" ,
145+ {
146+ "ChallengeParameters" : {
147+ "USER_ID_FOR_SRP" : "user" ,
148+ "SALT" : "4" ,
149+ "SRP_B" : "5" ,
150+ "SECRET_BLOCK" : "secblock" ,
151+ },
152+ "ChallengeName" : "PASSWORD_VERIFIER" ,
153+ },
154+ expected_parameters ,
155+ )
156+ self .aws_srp_stubber .add_response (
157+ "respond_to_auth_challenge" ,
158+ {"AuthenticationResult" : {"IdToken" : "valid_token" },},
159+ {
160+ "ClientId" : stax_auth .client_id ,
161+ "ChallengeName" : ANY ,
162+ "ChallengeResponses" : ANY ,
163+ },
164+ )
165+ self .aws_srp_stubber .activate ()
79166
80167 def stub_cognito_creds (self , token : str ):
81168 sa = StaxAuth ("ApiAuth" )
0 commit comments