forked from descope/django-descope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_admin.py
61 lines (47 loc) · 1.91 KB
/
test_admin.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
import json
import logging
import random
import string
import descope
from descope import REFRESH_SESSION_TOKEN_NAME, SESSION_TOKEN_NAME
from django.test import TestCase
from django_descope import descope_client
from django_descope.authentication import add_tokens_to_request
logger = logging.getLogger(__name__)
def random_string(N: int) -> str:
return "".join(random.choices(string.ascii_lowercase + string.digits, k=N))
class AdminLoginTestCase(TestCase):
delivery_method = descope.DeliveryMethod.EMAIL
login_id = f"test+{random_string(8)}@test.internal"
token: dict
def setUp(self) -> None:
descope_client.mgmt.user.create_test_user(
self.login_id, role_names=["is_staff", "is_superuser"], verified_email=True
)
resp = descope_client.mgmt.user.generate_otp_for_test_user(
self.delivery_method,
self.login_id,
)
self.token = descope_client.otp.verify_code(
self.delivery_method, self.login_id, resp.get("code")
)
session = self.client.session
add_tokens_to_request(
session,
self.token[SESSION_TOKEN_NAME]["jwt"],
self.token[REFRESH_SESSION_TOKEN_NAME]["jwt"],
)
def test_test_user_can_login_to_admin(self):
"""Test that if user has the right roles they can login to admin"""
res = self.client.get("/debug")
self.assertEqual(res.status_code, 200)
debug = json.loads(res.content)
self.assertEqual(debug["user"], self.token["userId"])
self.assertEqual(debug["email"], self.login_id)
self.assertTrue(debug["is_authenticated"])
self.assertTrue(debug["is_staff"])
self.assertTrue(debug["is_superuser"])
res = self.client.get("/admin/")
self.assertEqual(res.status_code, 200, res.headers)
def tearDown(self) -> None:
descope_client.mgmt.user.delete(self.login_id)