This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
forked from NabuCasa/pycognito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
494 lines (419 loc) · 17.3 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import datetime
import unittest
import os.path
import uuid
import freezegun
import moto
import moto.cognitoidp
import boto3
from botocore.exceptions import ParamValidationError
from botocore.stub import Stubber
from mock import patch
from envs import env
import requests
import requests_mock
from pycognito import Cognito, UserObj, GroupObj, TokenVerificationException
from pycognito.aws_srp import AWSSRP
from pycognito.utils import RequestsSrpAuth
def _mock_authenticate_user(_, client=None, client_metadata=None):
return {
"AuthenticationResult": {
"TokenType": "admin",
"IdToken": "dummy_token",
"AccessToken": "dummy_token",
"RefreshToken": "dummy_token",
}
}
def _mock_get_params(_):
return {"USERNAME": "bob", "SRP_A": "srp"}
def _mock_verify_tokens(self, token, id_name, token_use):
if "wrong" in token:
raise TokenVerificationException
setattr(self, id_name, token)
class UserObjTestCase(unittest.TestCase):
def setUp(self):
if env("USE_CLIENT_SECRET", "False") == "True":
self.app_id = env("COGNITO_APP_WITH_SECRET_ID")
else:
self.app_id = env("COGNITO_APP_ID")
self.cognito_user_pool_id = env("COGNITO_USER_POOL_ID", "us-east-1_123456789")
self.username = env("COGNITO_TEST_USERNAME")
self.user = Cognito(
user_pool_id=self.cognito_user_pool_id,
client_id=self.app_id,
username=self.username,
)
self.user_metadata = {
"user_status": "CONFIRMED",
"username": "bjones",
}
self.user_info = [
{"Name": "name", "Value": "Brian Jones"},
{"Name": "given_name", "Value": "Brian"},
{"Name": "birthdate", "Value": "12/7/1980"},
]
def test_init(self):
user = UserObj("bjones", self.user_info, self.user, self.user_metadata)
self.assertEqual(user.username, self.user_metadata.get("username"))
self.assertEqual(user.name, self.user_info[0].get("Value"))
self.assertEqual(user.user_status, self.user_metadata.get("user_status"))
class GroupObjTestCase(unittest.TestCase):
def setUp(self):
if env("USE_CLIENT_SECRET", "False") == "True":
self.app_id = env("COGNITO_APP_WITH_SECRET_ID")
else:
self.app_id = env("COGNITO_APP_ID")
self.cognito_user_pool_id = env("COGNITO_USER_POOL_ID", "us-east-1_123456789")
self.group_data = {"GroupName": "test_group", "Precedence": 1}
self.cognito_obj = Cognito(
user_pool_id=self.cognito_user_pool_id, client_id=self.app_id
)
def test_init(self):
group = GroupObj(group_data=self.group_data, cognito_obj=self.cognito_obj)
self.assertEqual(group.group_name, "test_group")
self.assertEqual(group.precedence, 1)
class CognitoAuthTestCase(unittest.TestCase):
def setUp(self):
if env("USE_CLIENT_SECRET") == "True":
self.app_id = env("COGNITO_APP_WITH_SECRET_ID", "app")
self.client_secret = env("COGNITO_CLIENT_SECRET")
else:
self.app_id = env("COGNITO_APP_ID", "app")
self.client_secret = None
self.cognito_user_pool_id = env("COGNITO_USER_POOL_ID", "us-east-1_123456789")
self.username = env("COGNITO_TEST_USERNAME", "bob")
self.password = env("COGNITO_TEST_PASSWORD", "bobpassword")
self.user = Cognito(
self.cognito_user_pool_id,
self.app_id,
username=self.username,
client_secret=self.client_secret,
)
@patch("pycognito.aws_srp.AWSSRP.authenticate_user", _mock_authenticate_user)
@patch("pycognito.Cognito.verify_token", _mock_verify_tokens)
def test_authenticate(self):
self.user.authenticate(self.password)
self.assertNotEqual(self.user.access_token, None)
self.assertNotEqual(self.user.id_token, None)
self.assertNotEqual(self.user.refresh_token, None)
@patch("pycognito.aws_srp.AWSSRP.authenticate_user", _mock_authenticate_user)
@patch("pycognito.Cognito.verify_token", _mock_verify_tokens)
def test_verify_token(self):
self.user.authenticate(self.password)
bad_access_token = "{}wrong".format(self.user.access_token)
with self.assertRaises(TokenVerificationException):
self.user.verify_token(bad_access_token, "access_token", "access")
@patch("pycognito.Cognito", autospec=True)
def test_register(self, cognito_user):
user = cognito_user(
self.cognito_user_pool_id, self.app_id, username=self.username
)
base_attr = dict(
given_name="Brian",
family_name="Jones",
name="Brian Jones",
email="[email protected]",
phone_number="+19194894555",
gender="Male",
preferred_username="billyocean",
)
user.set_base_attributes(**base_attr)
user.register("sampleuser", "sample4#Password")
@patch("pycognito.aws_srp.AWSSRP.authenticate_user", _mock_authenticate_user)
@patch("pycognito.Cognito.verify_token", _mock_verify_tokens)
@patch("pycognito.Cognito._add_secret_hash", return_value=None)
def test_renew_tokens(self, _):
stub = Stubber(self.user.client)
# By the stubber nature, we need to add the sequence
# of calls for the AWS SRP auth to test the whole process
stub.add_response(
method="initiate_auth",
service_response={
"AuthenticationResult": {
"TokenType": "admin",
"IdToken": "dummy_token",
"AccessToken": "dummy_token",
"RefreshToken": "dummy_token",
},
"ResponseMetadata": {"HTTPStatusCode": 200},
},
expected_params={
"ClientId": self.app_id,
"AuthFlow": "REFRESH_TOKEN_AUTH",
"AuthParameters": {"REFRESH_TOKEN": "dummy_token"},
},
)
with stub:
self.user.authenticate(self.password)
self.user.renew_access_token()
stub.assert_no_pending_responses()
@patch("pycognito.Cognito", autospec=True)
def test_update_profile(self, cognito_user):
user = cognito_user(
self.cognito_user_pool_id, self.app_id, username=self.username
)
user.authenticate(self.password)
user.update_profile({"given_name": "Jenkins"})
def test_admin_get_user(self):
stub = Stubber(self.user.client)
stub.add_response(
method="admin_get_user",
service_response={
"Enabled": True,
"UserStatus": "CONFIRMED",
"Username": self.username,
"UserAttributes": [],
},
expected_params={
"UserPoolId": self.cognito_user_pool_id,
"Username": self.username,
},
)
with stub:
u = self.user.admin_get_user()
self.assertEqual(u.username, self.username)
stub.assert_no_pending_responses()
def test_check_token(self):
# This is a sample JWT with an expiration time set to January, 1st, 3000
self.user.access_token = (
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG"
"9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjMyNTAzNjgwMDAwfQ.C-1gPxrhUsiWeCvMvaZuuQYarkDNAc"
"pEGJPIqu_SrKQ"
)
try:
valid = self.user.check_token()
except OverflowError:
self.skipTest("This test requires 64-bit time_t")
else:
self.assertFalse(valid)
@patch("pycognito.Cognito", autospec=True)
def test_validate_verification(self, cognito_user):
u = cognito_user(self.cognito_user_pool_id, self.app_id, username=self.username)
u.validate_verification("4321")
@patch("pycognito.Cognito", autospec=True)
def test_confirm_forgot_password(self, cognito_user):
u = cognito_user(self.cognito_user_pool_id, self.app_id, username=self.username)
u.confirm_forgot_password("4553", "samplepassword")
with self.assertRaises(TypeError):
u.confirm_forgot_password(self.password)
@patch("pycognito.aws_srp.AWSSRP.authenticate_user", _mock_authenticate_user)
@patch("pycognito.Cognito.verify_token", _mock_verify_tokens)
@patch("pycognito.Cognito.check_token", return_value=True)
def test_change_password(self, _):
# u = cognito_user(self.cognito_user_pool_id, self.app_id,
# username=self.username)
self.user.authenticate(self.password)
stub = Stubber(self.user.client)
stub.add_response(
method="change_password",
service_response={"ResponseMetadata": {"HTTPStatusCode": 200}},
expected_params={
"PreviousPassword": self.password,
"ProposedPassword": "crazypassword$45DOG",
"AccessToken": self.user.access_token,
},
)
with stub:
self.user.change_password(self.password, "crazypassword$45DOG")
stub.assert_no_pending_responses()
with self.assertRaises(ParamValidationError):
self.user.change_password(self.password, None)
def test_set_attributes(self):
user = Cognito(self.cognito_user_pool_id, self.app_id)
user._set_attributes(
{"ResponseMetadata": {"HTTPStatusCode": 200}}, {"somerandom": "attribute"}
)
self.assertEqual(user.somerandom, "attribute")
@patch("pycognito.Cognito.verify_token", _mock_verify_tokens)
def test_admin_authenticate(self):
stub = Stubber(self.user.client)
# By the stubber nature, we need to add the sequence
# of calls for the AWS SRP auth to test the whole process
stub.add_response(
method="admin_initiate_auth",
service_response={
"AuthenticationResult": {
"TokenType": "admin",
"IdToken": "dummy_token",
"AccessToken": "dummy_token",
"RefreshToken": "dummy_token",
}
},
expected_params={
"UserPoolId": self.cognito_user_pool_id,
"ClientId": self.app_id,
"AuthFlow": "ADMIN_NO_SRP_AUTH",
"AuthParameters": {
"USERNAME": self.username,
"PASSWORD": self.password,
},
},
)
with stub:
self.user.admin_authenticate(self.password)
self.assertNotEqual(self.user.access_token, None)
self.assertNotEqual(self.user.id_token, None)
self.assertNotEqual(self.user.refresh_token, None)
stub.assert_no_pending_responses()
class AWSSRPTestCase(unittest.TestCase):
def setUp(self):
if env("USE_CLIENT_SECRET") == "True":
self.client_secret = env("COGNITO_CLIENT_SECRET")
self.app_id = env("COGNITO_APP_WITH_SECRET_ID", "app")
else:
self.app_id = env("COGNITO_APP_ID", "app")
self.client_secret = None
self.cognito_user_pool_id = env("COGNITO_USER_POOL_ID", "us-east-1_123456789")
self.username = env("COGNITO_TEST_USERNAME")
self.password = env("COGNITO_TEST_PASSWORD")
self.aws = AWSSRP(
username=self.username,
password=self.password,
pool_region="us-east-1",
pool_id=self.cognito_user_pool_id,
client_id=self.app_id,
client_secret=self.client_secret,
)
def tearDown(self):
del self.aws
@patch("pycognito.aws_srp.AWSSRP.get_auth_params", _mock_get_params)
@patch("pycognito.aws_srp.AWSSRP.process_challenge", return_value={})
def test_authenticate_user(self, _):
stub = Stubber(self.aws.client)
# By the stubber nature, we need to add the sequence
# of calls for the AWS SRP auth to test the whole process
stub.add_response(
method="initiate_auth",
service_response={
"ChallengeName": "PASSWORD_VERIFIER",
"ChallengeParameters": {},
},
expected_params={
"AuthFlow": "USER_SRP_AUTH",
"AuthParameters": _mock_get_params(None),
"ClientId": self.app_id,
},
)
stub.add_response(
method="respond_to_auth_challenge",
service_response={
"AuthenticationResult": {
"IdToken": "dummy_token",
"AccessToken": "dummy_token",
"RefreshToken": "dummy_token",
}
},
expected_params={
"ClientId": self.app_id,
"ChallengeName": "PASSWORD_VERIFIER",
"ChallengeResponses": {},
},
)
with stub:
tokens = self.aws.authenticate_user()
self.assertTrue("IdToken" in tokens["AuthenticationResult"])
self.assertTrue("AccessToken" in tokens["AuthenticationResult"])
self.assertTrue("RefreshToken" in tokens["AuthenticationResult"])
stub.assert_no_pending_responses()
def test_cognito_formatted_timestamp(self):
self.assertEqual(
self.aws.get_cognito_formatted_timestamp(
datetime.datetime(2022, 1, 1, 0, 0, 0)
),
"Sat Jan 1 00:00:00 UTC 2022",
)
self.assertEqual(
self.aws.get_cognito_formatted_timestamp(
datetime.datetime(2022, 1, 2, 12, 0, 0)
),
"Sun Jan 2 12:00:00 UTC 2022",
)
self.assertEqual(
self.aws.get_cognito_formatted_timestamp(
datetime.datetime(2022, 1, 3, 9, 0, 0)
),
"Mon Jan 3 09:00:00 UTC 2022",
)
self.assertEqual(
self.aws.get_cognito_formatted_timestamp(
datetime.datetime(2022, 12, 31, 23, 59, 59)
),
"Sat Dec 31 23:59:59 UTC 2022",
)
@moto.mock_cognitoidp
class UtilsTestCase(unittest.TestCase):
username = "[email protected]"
password = "Test1234"
def setUp(self) -> None:
cognitoidp_client = boto3.client("cognito-idp", region_name="us-east-1")
user_pool = cognitoidp_client.create_user_pool(
PoolName="pycognito-test-pool",
AliasAttributes=[
"email",
],
UsernameAttributes=[
"email",
],
)
self.user_pool_id = user_pool["UserPool"]["Id"]
user_pool_client = cognitoidp_client.create_user_pool_client(
UserPoolId=self.user_pool_id,
ClientName="test-client",
RefreshTokenValidity=1,
AccessTokenValidity=1,
IdTokenValidity=1,
TokenValidityUnits={
"AccessToken": "hour",
"IdToken": "hour",
"RefreshToken": "days",
},
)
self.client_id = user_pool_client["UserPoolClient"]["ClientId"]
cognitoidp_client.admin_create_user(
UserPoolId=self.user_pool_id,
Username=self.username,
TemporaryPassword=self.password,
MessageAction="SUPPRESS",
)
@requests_mock.Mocker()
def test_srp_requests_http_auth(self, m):
# Get Moto's static public jwks
jwks_public_key_filename = os.path.join(
os.path.dirname(moto.cognitoidp.__file__), "resources/jwks-public.json"
)
jwks_public_key_f = open(jwks_public_key_filename, "rb")
# Create some test data
test_data = str(uuid.uuid4())
# Mock a test endpoint. We pretend this endpoint would require an Authorization header
m.get("http://test.com", text=test_data)
# Pycognito will automatically verify the token it receives. Mock the proper endpoint and return the static
# key from above
m.get(
f"https://cognito-idp.us-east-1.amazonaws.com/{self.user_pool_id}/.well-known/jwks.json",
body=jwks_public_key_f,
)
now = datetime.datetime.now()
# Standup the actual Requests plugin
srp_auth = RequestsSrpAuth(
username=self.username,
password=self.password,
user_pool_id=self.user_pool_id,
user_pool_region="us-east-1",
client_id=self.client_id,
)
# Make the actual request
req = requests.get("http://test.com", auth=srp_auth)
req.raise_for_status()
# Ensure the data returns matches the mocked endpoint
self.assertEqual(test_data, req.text)
# Get the access token used
access_token_orig = srp_auth.cognito_client.access_token
# Make a second request with a time 2 hours in the future
with freezegun.freeze_time(now + datetime.timedelta(hours=2)):
req = requests.get("http://test.com", auth=srp_auth)
req.raise_for_status()
access_token_new = srp_auth.cognito_client.access_token
# Check that the access token was refreshed to a new one
self.assertNotEqual(access_token_orig, access_token_new)
if __name__ == "__main__":
unittest.main()