-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathtest_user.py
105 lines (97 loc) · 2.88 KB
/
test_user.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
import calendar
import datetime
from typing import Dict
import pytest
from fastapi_azure_auth.user import User
from fastapi_azure_auth.utils import is_guest
@pytest.mark.parametrize(
'claims, expected',
(
[
{ # v1 appreg
'iss': 'https://sts.windows.net/9b5ff18e-53c0-45a2-8bc2-9c0c8f60b2c6/',
'idp': 'https://sts.windows.net/9b5ff18e-53c0-45a2-8bc2-9c0c8f60b2c6/',
'ver': '1.0',
},
False,
],
[
{ # v2 appreg
'iss': 'https://login.microsoftonline.com/9b5ff18e-53c0-45a2-8bc2-9c0c8f60b2c6/v2.0',
'ver': '2.0',
},
False,
],
[
{ # v1 guest user
'iss': 'https://sts.windows.net/9b5ff18e-53c0-45a2-8bc2-9c0c8f60b2c6/',
'idp': 'https://sts.windows.net/e49ee8b0-4ec8-486f-93f3-bedaa281a154/',
'ver': '1.0',
},
True,
],
[
{ # v2 guest user
'iss': 'https://login.microsoftonline.com/9b5ff18e-53c0-45a2-8bc2-9c0c8f60b2c6/v2.0',
'idp': 'https://sts.windows.net/e49ee8b0-4ec8-486f-93f3-bedaa281a154/',
'ver': '2.0',
},
True,
],
[
{ # v1 tenant member user
'iss': 'https://sts.windows.net/9b5ff18e-53c0-45a2-8bc2-9c0c8f60b2c6/',
'ver': '1.0',
},
False,
],
[
{ # v2 tenant member user
'iss': 'https://login.microsoftonline.com/9b5ff18e-53c0-45a2-8bc2-9c0c8f60b2c6/v2.0',
'ver': '2.0',
},
False,
],
[
{ # acct claim
'acct': 1, # 1 == guest
},
True,
],
[
{ # acct claim
'acct': 0, # 1 == tenant member
},
False,
],
),
ids=[
'v1 appreg',
'v2 appreg',
'v1 guest user',
'v2 guest user',
'v1 tenant member user',
'v2 tenant member user',
'acct guest',
'acct tenant member',
],
)
def test_guest_user(claims: Dict[str, str], expected: bool):
assert is_guest(claims=claims) == expected
def get_utc_now_as_unix_timestamp() -> int:
date = datetime.datetime.now(datetime.timezone.utc)
return calendar.timegm(date.utctimetuple())
def test_user_missing_optionals():
user = User(
aud='Dummy',
access_token='Dummy',
claims={'oid': 'Dummy oid'},
iss='https://dummy-platform.dummylogin.com/dummy-uid/v2.0/',
iat=get_utc_now_as_unix_timestamp(),
nbf=get_utc_now_as_unix_timestamp(),
exp=get_utc_now_as_unix_timestamp(),
sub='dummy-sub',
ver='1.0',
scp='AccessAsUser',
)
assert user is not None