-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathtest_auth.py
More file actions
397 lines (340 loc) · 14.2 KB
/
test_auth.py
File metadata and controls
397 lines (340 loc) · 14.2 KB
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
from unittest import mock
from unittest.mock import Mock, patch
from nylas.models.auth import (
CodeExchangeResponse,
TokenInfoResponse,
ProviderDetectResponse,
)
from nylas.models.grants import Grant
from nylas.resources.auth import (
_hash_pkce_secret,
_build_query,
_build_query_with_pkce,
_build_query_with_admin_consent,
Auth,
)
class TestAuth:
def test_hash_pkce_secret(self):
assert (
_hash_pkce_secret("nylas")
== "ZTk2YmY2Njg2YTNjMzUxMGU5ZTkyN2RiNzA2OWNiMWNiYTliOTliMDIyZjQ5NDgzYTZjZTMyNzA4MDllNjhhMg"
)
def test_build_query(self):
config = {
"foo": "bar",
"scope": ["email", "calendar"],
}
assert _build_query(config) == {
"foo": "bar",
"response_type": "code",
"access_type": "online",
"scope": "email calendar",
}
def test_build_query_with_pkce(self):
config = {
"foo": "bar",
"scope": ["email", "calendar"],
}
assert _build_query_with_pkce(config, "secret-hash-123") == {
"foo": "bar",
"response_type": "code",
"access_type": "online",
"scope": "email calendar",
"code_challenge": "secret-hash-123",
"code_challenge_method": "s256",
}
def test_build_query_with_admin_consent(self):
config = {
"foo": "bar",
"scope": ["email", "calendar"],
"credential_id": "credential-id-123",
}
assert _build_query_with_admin_consent(config) == {
"foo": "bar",
"response_type": "adminconsent",
"access_type": "online",
"scope": "email calendar",
"credential_id": "credential-id-123",
}
def test_url_auth_builder(self, http_client):
auth = Auth(http_client)
assert (
auth._url_auth_builder({"foo": "bar"})
== "https://test.nylas.com/v3/connect/auth?foo=bar"
)
def test_get_token(self, http_client_token_exchange):
auth = Auth(http_client_token_exchange)
req = {
"redirect_uri": "https://example.com",
"code": "code",
"client_id": "client_id",
"client_secret": "client_secret",
}
res = auth._get_token(req, overrides=None)
http_client_token_exchange._execute.assert_called_once_with(
method="POST", path="/v3/connect/token", request_body=req, overrides=None
)
assert type(res) is CodeExchangeResponse
assert res.access_token == "nylas_access_token"
assert res.expires_in == 3600
assert res.id_token == "jwt_token"
assert res.refresh_token == "nylas_refresh_token"
assert res.scope == "https://www.googleapis.com/auth/gmail.readonly profile"
assert res.token_type == "Bearer"
assert res.grant_id == "grant_123"
assert res.provider == "google"
def test_get_token_info(self, http_client_token_info):
auth = Auth(http_client_token_info)
req = {
"foo": "bar",
}
res = auth._get_token_info(req, overrides=None)
http_client_token_info._execute.assert_called_once_with(
method="GET", path="/v3/connect/tokeninfo", query_params=req, overrides=None
)
assert type(res.data) is TokenInfoResponse
assert res.data.iss == "https://nylas.com"
assert res.data.aud == "http://localhost:3030"
assert res.data.sub == "Jaf84d88-£274-46cc-bbc9-aed7dac061c7"
assert res.data.email == "user@example.com"
assert res.data.iat == 1692094848
assert res.data.exp == 1692095173
def test_url_for_oauth2(self, http_client):
auth = Auth(http_client)
config = {
"client_id": "abc-123",
"redirect_uri": "https://example.com/oauth/callback",
"scope": ["email.read_only", "calendar", "contacts"],
"login_hint": "test@gmail.com",
"provider": "google",
"prompt": "select_provider,detect",
"state": "abc-123-state",
}
url = auth.url_for_oauth2(config)
assert (
url
== "https://test.nylas.com/v3/connect/auth?client_id=abc-123&redirect_uri=https%3A//example.com/oauth/callback&scope=email.read_only%20calendar%20contacts&login_hint=test%40gmail.com&provider=google&prompt=select_provider%2Cdetect&state=abc-123-state&response_type=code&access_type=online"
)
def test_url_for_oauth2_with_credential_id(self, http_client):
auth = Auth(http_client)
config = {
"client_id": "abc-123",
"redirect_uri": "https://example.com/oauth/callback",
"scope": ["Mail.Read", "User.Read"],
"login_hint": "test@outlook.com",
"provider": "microsoft",
"state": "abc-123-state",
"credential_id": "cred-abc-123",
}
url = auth.url_for_oauth2(config)
assert (
url
== "https://test.nylas.com/v3/connect/auth?client_id=abc-123&redirect_uri=https%3A//example.com/oauth/callback&scope=Mail.Read%20User.Read&login_hint=test%40outlook.com&provider=microsoft&state=abc-123-state&credential_id=cred-abc-123&response_type=code&access_type=online"
)
def test_exchange_code_for_token(self, http_client_token_exchange):
auth = Auth(http_client_token_exchange)
config = {
"client_id": "abc-123",
"client_secret": "secret",
"code": "code",
"redirect_uri": "https://example.com/oauth/callback",
}
auth.exchange_code_for_token(config)
http_client_token_exchange._execute.assert_called_once_with(
method="POST",
path="/v3/connect/token",
request_body={
"client_id": "abc-123",
"client_secret": "secret",
"code": "code",
"redirect_uri": "https://example.com/oauth/callback",
"grant_type": "authorization_code",
},
overrides=None,
)
def test_exchange_code_for_token_no_secret(self, http_client_token_exchange):
http_client_token_exchange.api_key = "nylas-api-key"
auth = Auth(http_client_token_exchange)
config = {
"client_id": "abc-123",
"code": "code",
"redirect_uri": "https://example.com/oauth/callback",
}
auth.exchange_code_for_token(config)
http_client_token_exchange._execute.assert_called_once_with(
method="POST",
path="/v3/connect/token",
request_body={
"client_id": "abc-123",
"code": "code",
"redirect_uri": "https://example.com/oauth/callback",
"client_secret": "nylas-api-key",
"grant_type": "authorization_code",
},
overrides=None,
)
def test_custom_authentication(self):
mock_http_client = Mock()
mock_http_client._execute.return_value = ({
"request_id": "abc-123",
"data": {
"id": "e19f8e1a-eb1c-41c0-b6a6-d2e59daf7f47",
"provider": "google",
"grant_status": "valid",
"email": "email@example.com",
"scope": ["Mail.Read", "User.Read", "offline_access"],
"user_agent": "string",
"ip": "string",
"state": "my-state",
"created_at": 1617817109,
"updated_at": 1617817109,
},
}, {"X-Test-Header": "test"})
auth = Auth(mock_http_client)
res = auth.custom_authentication(
{"provider": "google", "settings": {"foo": "bar"}}
)
mock_http_client._execute.assert_called_once_with(
method="POST",
path="/v3/connect/custom",
request_body={"provider": "google", "settings": {"foo": "bar"}},
overrides=None,
)
assert type(res.data) is Grant
assert res.data.id == "e19f8e1a-eb1c-41c0-b6a6-d2e59daf7f47"
assert res.data.provider == "google"
assert res.data.grant_status == "valid"
assert res.data.email == "email@example.com"
assert res.data.scope == ["Mail.Read", "User.Read", "offline_access"]
assert res.data.user_agent == "string"
assert res.data.ip == "string"
assert res.data.state == "my-state"
assert res.data.created_at == 1617817109
assert res.data.updated_at == 1617817109
def test_refresh_access_token(self, http_client_token_exchange):
auth = Auth(http_client_token_exchange)
config = {
"redirect_uri": "https://example.com/oauth/callback",
"refresh_token": "refresh-12345",
"client_id": "abc-123",
"client_secret": "secret",
}
auth.refresh_access_token(config)
http_client_token_exchange._execute.assert_called_once_with(
method="POST",
path="/v3/connect/token",
request_body={
"redirect_uri": "https://example.com/oauth/callback",
"refresh_token": "refresh-12345",
"client_id": "abc-123",
"client_secret": "secret",
"grant_type": "refresh_token",
},
overrides=None,
)
def test_refresh_access_token_no_secret(self, http_client_token_exchange):
http_client_token_exchange.api_key = "nylas-api-key"
auth = Auth(http_client_token_exchange)
config = {
"redirect_uri": "https://example.com/oauth/callback",
"refresh_token": "refresh-12345",
"client_id": "abc-123",
}
auth.refresh_access_token(config)
http_client_token_exchange._execute.assert_called_once_with(
method="POST",
path="/v3/connect/token",
request_body={
"redirect_uri": "https://example.com/oauth/callback",
"refresh_token": "refresh-12345",
"client_id": "abc-123",
"client_secret": "nylas-api-key",
"grant_type": "refresh_token",
},
overrides=None,
)
def test_id_token_info(self, http_client_token_info):
auth = Auth(http_client_token_info)
auth.id_token_info("id-123")
http_client_token_info._execute.assert_called_once_with(
method="GET",
path="/v3/connect/tokeninfo",
query_params={"id_token": "id-123"},
overrides=None,
)
def test_validate_access_token(self, http_client_token_info):
auth = Auth(http_client_token_info)
auth.validate_access_token("id-123")
http_client_token_info._execute.assert_called_once_with(
method="GET",
path="/v3/connect/tokeninfo",
query_params={"access_token": "id-123"},
overrides=None,
)
@mock.patch("uuid.uuid4")
def test_url_for_oauth2_pkce(self, mock_uuid4, http_client):
mock_uuid4.return_value = "nylas"
auth = Auth(http_client)
config = {
"client_id": "abc-123",
"redirect_uri": "https://example.com/oauth/callback",
"scope": ["email.read_only", "calendar", "contacts"],
"login_hint": "test@gmail.com",
"provider": "google",
"prompt": "select_provider,detect",
"state": "abc-123-state",
}
result = auth.url_for_oauth2_pkce(config)
assert (
result.url
== "https://test.nylas.com/v3/connect/auth?client_id=abc-123&redirect_uri=https%3A//example.com/oauth/callback&scope=email.read_only%20calendar%20contacts&login_hint=test%40gmail.com&provider=google&prompt=select_provider%2Cdetect&state=abc-123-state&response_type=code&access_type=online&code_challenge=ZTk2YmY2Njg2YTNjMzUxMGU5ZTkyN2RiNzA2OWNiMWNiYTliOTliMDIyZjQ5NDgzYTZjZTMyNzA4MDllNjhhMg&code_challenge_method=s256"
)
assert result.secret == "nylas"
assert (
result.secret_hash
== "ZTk2YmY2Njg2YTNjMzUxMGU5ZTkyN2RiNzA2OWNiMWNiYTliOTliMDIyZjQ5NDgzYTZjZTMyNzA4MDllNjhhMg"
)
def test_url_for_admin_consent(self, http_client):
auth = Auth(http_client)
config = {
"credential_id": "cred-123",
"client_id": "abc-123",
"redirect_uri": "https://example.com/oauth/callback",
"scope": ["email.read_only", "calendar", "contacts"],
"login_hint": "test@gmail.com",
"prompt": "select_provider,detect",
"state": "abc-123-state",
}
url = auth.url_for_admin_consent(config)
assert (
url
== "https://test.nylas.com/v3/connect/auth?provider=microsoft&credential_id=cred-123&client_id=abc-123&redirect_uri=https%3A//example.com/oauth/callback&scope=email.read_only%20calendar%20contacts&login_hint=test%40gmail.com&prompt=select_provider%2Cdetect&state=abc-123-state&response_type=adminconsent&access_type=online"
)
def test_revoke(self, http_client_response):
auth = Auth(http_client_response)
res = auth.revoke("access_token")
http_client_response._execute.assert_called_once_with(
method="POST",
path="/v3/connect/revoke",
query_params={"token": "access_token"},
overrides=None,
)
assert res is True
def test_detect_provider(self):
mock_http_client = Mock()
mock_http_client._execute.return_value = ({
"request_id": "abc-123",
"data": {
"email_address": "test@gmail.com",
"detected": True,
"provider": "google",
"type": "string",
},
}, {"X-Test-Header": "test"})
auth = Auth(mock_http_client)
req = {"email": "test@gmail.com", "all_provider_types": True}
res = auth.detect_provider(req)
mock_http_client._execute.assert_called_once_with(
method="POST", path="/v3/providers/detect", query_params=req, overrides=None
)
assert type(res.data) == ProviderDetectResponse