-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_mfa.py
257 lines (228 loc) · 9.54 KB
/
test_mfa.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
from workos.mfa import Mfa
import pytest
class TestMfa(object):
@pytest.fixture(autouse=True)
def setup(self, sync_http_client_for_test):
self.http_client = sync_http_client_for_test
self.mfa = Mfa(http_client=self.http_client)
@pytest.fixture
def mock_enroll_factor_no_type(self):
return None
@pytest.fixture
def mock_enroll_factor_incorrect_type(self):
return "dinosaur"
@pytest.fixture
def mock_enroll_factor_totp_payload(self):
return [
"totp",
"workos",
"stanley@yelnats.com",
]
@pytest.fixture
def mock_enroll_factor_sms_payload(self):
return ["sms", "7208675309"]
@pytest.fixture
def mock_challenge_factor_payload(self):
return ["auth_factor_01FWRSPQ2XXXQKBCY5AAW5T59W", "Your code is {{code}}"]
@pytest.fixture
def mock_verify_challenge_payload(self):
return ["auth_challenge_01FWRY5H0XXXX0JGGVTY6QFX7X", "626592"]
@pytest.fixture
def mock_enroll_factor_response_sms(self):
return {
"object": "authentication_factor",
"id": "auth_factor_01FVYZ5QM8N98T9ME5BCB2BBMJ",
"created_at": "2022-02-15T15:14:19.392Z",
"updated_at": "2022-02-15T15:14:19.392Z",
"type": "sms",
"sms": {"phone_number": "+19204703484"},
"user_id": None,
}
@pytest.fixture
def mock_enroll_factor_response_totp(self):
return {
"object": "authentication_factor",
"id": "auth_factor_01FVYZ5QM8N98T9ME5BCB2BBMJ",
"created_at": "2022-02-15T15:14:19.392Z",
"updated_at": "2022-02-15T15:14:19.392Z",
"type": "totp",
"totp": {
"issuer": "FooCorp",
"user": "test@example.com",
"qr_code": "data:image/png;base64,{base64EncodedPng}",
"secret": "NAGCCFS3EYRB422HNAKAKY3XDUORMSRF",
"uri": "otpauth://totp/FooCorp:alan.turing@foo-corp.com?secret=NAGCCFS3EYRB422HNAKAKY3XDUORMSRF&issuer=FooCorp",
},
"user_id": None,
}
@pytest.fixture
def mock_get_factor_response_totp(self):
return {
"object": "authentication_factor",
"id": "auth_factor_01FVYZ5QM8N98T9ME5BCB2BBMJ",
"created_at": "2022-02-15T15:14:19.392Z",
"updated_at": "2022-02-15T15:14:19.392Z",
"type": "totp",
"totp": {
"issuer": "FooCorp",
"user": "test@example.com",
},
"user_id": None,
}
@pytest.fixture
def mock_challenge_factor_response(self):
return {
"object": "authentication_challenge",
"id": "auth_challenge_01FVYZWQTZQ5VB6BC5MPG2EYC5",
"created_at": "2022-02-15T15:26:53.274Z",
"updated_at": "2022-02-15T15:26:53.274Z",
"expires_at": "2022-02-15T15:36:53.279Z",
"authentication_factor_id": "auth_factor_01FVYZ5QM8N98T9ME5BCB2BBMJ",
"code": None,
}
@pytest.fixture
def mock_verify_challenge_response(self):
return {
"challenge": {
"object": "authentication_challenge",
"id": "auth_challenge_01FVYZWQTZQ5VB6BC5MPG2EYC5",
"created_at": "2022-02-15T15:26:53.274Z",
"updated_at": "2022-02-15T15:26:53.274Z",
"expires_at": "2022-02-15T15:36:53.279Z",
"authentication_factor_id": "auth_factor_01FVYZ5QM8N98T9ME5BCB2BBMJ",
"code": None,
},
"valid": True,
}
def test_enroll_factor_totp_no_issuer(self, mock_enroll_factor_totp_payload):
with pytest.raises(ValueError) as err:
self.mfa.enroll_factor(
type=mock_enroll_factor_totp_payload[0],
totp_issuer=None,
totp_user=mock_enroll_factor_totp_payload[2],
)
assert (
"Incomplete arguments. Need to specify both totp_issuer and totp_user when type is totp"
in str(err.value)
)
def test_enroll_factor_totp_no_user(self, mock_enroll_factor_totp_payload):
with pytest.raises(ValueError) as err:
self.mfa.enroll_factor(
type=mock_enroll_factor_totp_payload[0],
totp_issuer=mock_enroll_factor_totp_payload[1],
totp_user=None,
)
assert (
"Incomplete arguments. Need to specify both totp_issuer and totp_user when type is totp"
in str(err.value)
)
def test_enroll_factor_sms_no_phone_number(self, mock_enroll_factor_sms_payload):
with pytest.raises(ValueError) as err:
self.mfa.enroll_factor(
type=mock_enroll_factor_sms_payload[0], phone_number=None
)
assert (
"Incomplete arguments. Need to specify phone_number when type is sms"
in str(err.value)
)
def test_enroll_factor_sms_success(
self, mock_enroll_factor_response_sms, capture_and_mock_http_client_request
):
request_kwargs = capture_and_mock_http_client_request(
self.http_client, mock_enroll_factor_response_sms, 200
)
enroll_factor = self.mfa.enroll_factor(type="sms", phone_number="9204448888")
assert request_kwargs["url"].endswith("/auth/factors/enroll")
assert request_kwargs["method"] == "post"
assert request_kwargs["json"] == {
"type": "sms",
"phone_number": "9204448888",
}
assert enroll_factor.dict() == mock_enroll_factor_response_sms
def test_enroll_factor_totp_success(
self, mock_enroll_factor_response_totp, capture_and_mock_http_client_request
):
request_kwargs = capture_and_mock_http_client_request(
self.http_client, mock_enroll_factor_response_totp, 200
)
enroll_factor = self.mfa.enroll_factor(
type="totp", totp_issuer="testissuer", totp_user="testuser"
)
assert request_kwargs["url"].endswith("/auth/factors/enroll")
assert request_kwargs["method"] == "post"
assert request_kwargs["json"] == {
"type": "totp",
"totp_issuer": "testissuer",
"totp_user": "testuser",
}
assert enroll_factor.dict() == mock_enroll_factor_response_totp
def test_get_factor_totp_success(
self, mock_get_factor_response_totp, capture_and_mock_http_client_request
):
request_kwargs = capture_and_mock_http_client_request(
self.http_client, mock_get_factor_response_totp, 200
)
authentication_factor_id = mock_get_factor_response_totp["id"]
response = self.mfa.get_factor(
authentication_factor_id=authentication_factor_id
)
assert request_kwargs["url"].endswith(
f"/auth/factors/{authentication_factor_id}"
)
assert request_kwargs["method"] == "get"
assert response.dict() == mock_get_factor_response_totp
def test_get_factor_sms_success(
self, mock_enroll_factor_response_sms, capture_and_mock_http_client_request
):
request_kwargs = capture_and_mock_http_client_request(
self.http_client, mock_enroll_factor_response_sms, 200
)
authentication_factor_id = mock_enroll_factor_response_sms["id"]
response = self.mfa.get_factor(
authentication_factor_id=authentication_factor_id
)
assert request_kwargs["url"].endswith(
f"/auth/factors/{authentication_factor_id}"
)
assert request_kwargs["method"] == "get"
assert response.dict() == mock_enroll_factor_response_sms
def test_delete_factor_success(self, capture_and_mock_http_client_request):
request_kwargs = capture_and_mock_http_client_request(
self.http_client, None, 200
)
response = self.mfa.delete_factor("auth_factor_01FZ4TS14D1PHFNZ9GF6YD8M1F")
assert request_kwargs["url"].endswith(
"/auth/factors/auth_factor_01FZ4TS14D1PHFNZ9GF6YD8M1F"
)
assert request_kwargs["method"] == "delete"
assert response is None
def test_challenge_success(
self, mock_challenge_factor_response, capture_and_mock_http_client_request
):
request_kwargs = capture_and_mock_http_client_request(
self.http_client, mock_challenge_factor_response, 200
)
challenge_factor = self.mfa.challenge_factor(
authentication_factor_id="auth_factor_01FXNWW32G7F3MG8MYK5D1HJJM"
)
assert request_kwargs["url"].endswith(
"/auth/factors/auth_factor_01FXNWW32G7F3MG8MYK5D1HJJM/challenge"
)
assert request_kwargs["method"] == "post"
assert challenge_factor.dict() == mock_challenge_factor_response
def test_verify_success(
self, mock_verify_challenge_response, capture_and_mock_http_client_request
):
request_kwargs = capture_and_mock_http_client_request(
self.http_client, mock_verify_challenge_response, 200
)
verify_challenge = self.mfa.verify_challenge(
authentication_challenge_id="auth_challenge_01FXNXH8Y2K3YVWJ10P139A6DT",
code="093647",
)
assert request_kwargs["url"].endswith(
"/auth/challenges/auth_challenge_01FXNXH8Y2K3YVWJ10P139A6DT/verify"
)
assert request_kwargs["method"] == "post"
assert request_kwargs["json"] == {"code": "093647"}
assert verify_challenge.dict() == mock_verify_challenge_response