-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
test_authorizer.py
351 lines (293 loc) · 12.8 KB
/
test_authorizer.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
"""Test for prawcore.auth.Authorizer classes."""
import unittest
from betamax import Betamax
import prawcore
from .conftest import (
CLIENT_ID,
CLIENT_SECRET,
PASSWORD,
PERMANENT_GRANT_CODE,
REDIRECT_URI,
REFRESH_TOKEN,
REQUESTOR,
TEMPORARY_GRANT_CODE,
USERNAME,
)
class AuthorizerTestBase(unittest.TestCase):
def setUp(self):
self.authentication = prawcore.TrustedAuthenticator(
REQUESTOR, CLIENT_ID, CLIENT_SECRET
)
class AuthorizerTest(AuthorizerTestBase):
def test_authorize__with_permanent_grant(self):
self.authentication.redirect_uri = REDIRECT_URI
authorizer = prawcore.Authorizer(self.authentication)
with Betamax(REQUESTOR).use_cassette(
"Authorizer_authorize__with_permanent_grant"
):
authorizer.authorize(PERMANENT_GRANT_CODE)
self.assertIsNotNone(authorizer.access_token)
self.assertIsNotNone(authorizer.refresh_token)
self.assertIsInstance(authorizer.scopes, set)
self.assertTrue(len(authorizer.scopes) > 0)
self.assertTrue(authorizer.is_valid())
def test_authorize__with_temporary_grant(self):
self.authentication.redirect_uri = REDIRECT_URI
authorizer = prawcore.Authorizer(self.authentication)
with Betamax(REQUESTOR).use_cassette(
"Authorizer_authorize__with_temporary_grant"
):
authorizer.authorize(TEMPORARY_GRANT_CODE)
self.assertIsNotNone(authorizer.access_token)
self.assertIsNone(authorizer.refresh_token)
self.assertIsInstance(authorizer.scopes, set)
self.assertTrue(len(authorizer.scopes) > 0)
self.assertTrue(authorizer.is_valid())
def test_authorize__with_invalid_code(self):
self.authentication.redirect_uri = REDIRECT_URI
authorizer = prawcore.Authorizer(self.authentication)
with Betamax(REQUESTOR).use_cassette(
"Authorizer_authorize__with_invalid_code"
):
self.assertRaises(
prawcore.OAuthException, authorizer.authorize, "invalid code"
)
self.assertFalse(authorizer.is_valid())
def test_authorize__fail_without_redirect_uri(self):
authorizer = prawcore.Authorizer(self.authentication)
self.assertRaises(
prawcore.InvalidInvocation, authorizer.authorize, "dummy code"
)
self.assertFalse(authorizer.is_valid())
def test_initialize(self):
authorizer = prawcore.Authorizer(self.authentication)
self.assertIsNone(authorizer.access_token)
self.assertIsNone(authorizer.scopes)
self.assertIsNone(authorizer.refresh_token)
self.assertFalse(authorizer.is_valid())
def test_initialize__with_refresh_token(self):
authorizer = prawcore.Authorizer(
self.authentication, refresh_token=REFRESH_TOKEN
)
self.assertIsNone(authorizer.access_token)
self.assertIsNone(authorizer.scopes)
self.assertEqual(REFRESH_TOKEN, authorizer.refresh_token)
self.assertFalse(authorizer.is_valid())
def test_initialize__with_untrusted_authenticator(self):
authenticator = prawcore.UntrustedAuthenticator(None, None)
authorizer = prawcore.Authorizer(authenticator)
self.assertIsNone(authorizer.access_token)
self.assertIsNone(authorizer.scopes)
self.assertIsNone(authorizer.refresh_token)
self.assertFalse(authorizer.is_valid())
def test_refresh(self):
authorizer = prawcore.Authorizer(
self.authentication, refresh_token=REFRESH_TOKEN
)
with Betamax(REQUESTOR).use_cassette("Authorizer_refresh"):
authorizer.refresh()
self.assertIsNotNone(authorizer.access_token)
self.assertIsInstance(authorizer.scopes, set)
self.assertTrue(len(authorizer.scopes) > 0)
self.assertTrue(authorizer.is_valid())
def test_refresh__post_refresh_callback(self):
def callback(authorizer):
self.assertNotEqual(REFRESH_TOKEN, authorizer.refresh_token)
authorizer.refresh_token = "manually_updated"
authorizer = prawcore.Authorizer(
self.authentication,
post_refresh_callback=callback,
refresh_token=REFRESH_TOKEN,
)
with Betamax(REQUESTOR).use_cassette("Authorizer_refresh"):
authorizer.refresh()
self.assertIsNotNone(authorizer.access_token)
self.assertEqual("manually_updated", authorizer.refresh_token)
self.assertIsInstance(authorizer.scopes, set)
self.assertTrue(len(authorizer.scopes) > 0)
self.assertTrue(authorizer.is_valid())
def test_refresh__pre_refresh_callback(self):
def callback(authorizer):
self.assertIsNone(authorizer.refresh_token)
authorizer.refresh_token = REFRESH_TOKEN
authorizer = prawcore.Authorizer(
self.authentication, pre_refresh_callback=callback
)
with Betamax(REQUESTOR).use_cassette("Authorizer_refresh"):
authorizer.refresh()
self.assertIsNotNone(authorizer.access_token)
self.assertIsInstance(authorizer.scopes, set)
self.assertTrue(len(authorizer.scopes) > 0)
self.assertTrue(authorizer.is_valid())
def test_refresh__with_invalid_token(self):
authorizer = prawcore.Authorizer(
self.authentication, refresh_token="INVALID_TOKEN"
)
with Betamax(REQUESTOR).use_cassette(
"Authorizer_refresh__with_invalid_token"
):
self.assertRaises(prawcore.ResponseException, authorizer.refresh)
self.assertFalse(authorizer.is_valid())
def test_refresh__without_refresh_token(self):
authorizer = prawcore.Authorizer(self.authentication)
self.assertRaises(prawcore.InvalidInvocation, authorizer.refresh)
self.assertFalse(authorizer.is_valid())
def test_revoke__access_token_with_refresh_set(self):
authorizer = prawcore.Authorizer(
self.authentication, refresh_token=REFRESH_TOKEN
)
with Betamax(REQUESTOR).use_cassette(
"Authorizer_revoke__access_token_with_refresh_set"
):
authorizer.refresh()
authorizer.revoke(only_access=True)
self.assertIsNone(authorizer.access_token)
self.assertIsNotNone(authorizer.refresh_token)
self.assertIsNone(authorizer.scopes)
self.assertFalse(authorizer.is_valid())
authorizer.refresh()
self.assertTrue(authorizer.is_valid())
def test_revoke__access_token_without_refresh_set(self):
self.authentication.redirect_uri = REDIRECT_URI
authorizer = prawcore.Authorizer(self.authentication)
with Betamax(REQUESTOR).use_cassette(
"Authorizer_revoke__access_token_without_refresh_set"
):
authorizer.authorize(TEMPORARY_GRANT_CODE)
authorizer.revoke()
self.assertIsNone(authorizer.access_token)
self.assertIsNone(authorizer.refresh_token)
self.assertIsNone(authorizer.scopes)
self.assertFalse(authorizer.is_valid())
def test_revoke__refresh_token_with_access_set(self):
authorizer = prawcore.Authorizer(
self.authentication, refresh_token=REFRESH_TOKEN
)
with Betamax(REQUESTOR).use_cassette(
"Authorizer_revoke__refresh_token_with_access_set"
):
authorizer.refresh()
authorizer.revoke()
self.assertIsNone(authorizer.access_token)
self.assertIsNone(authorizer.refresh_token)
self.assertIsNone(authorizer.scopes)
self.assertFalse(authorizer.is_valid())
def test_revoke__refresh_token_without_access_set(self):
authorizer = prawcore.Authorizer(
self.authentication, refresh_token=REFRESH_TOKEN
)
with Betamax(REQUESTOR).use_cassette(
"Authorizer_revoke__refresh_token_without_access_set"
):
authorizer.revoke()
self.assertIsNone(authorizer.access_token)
self.assertIsNone(authorizer.refresh_token)
self.assertIsNone(authorizer.scopes)
self.assertFalse(authorizer.is_valid())
def test_revoke__without_access_token(self):
authorizer = prawcore.Authorizer(
self.authentication, refresh_token=REFRESH_TOKEN
)
self.assertRaises(
prawcore.InvalidInvocation, authorizer.revoke, only_access=True
)
def test_revoke__without_any_token(self):
authorizer = prawcore.Authorizer(self.authentication)
self.assertRaises(prawcore.InvalidInvocation, authorizer.revoke)
class DeviceIDAuthorizerTest(AuthorizerTestBase):
def setUp(self):
self.authentication = prawcore.UntrustedAuthenticator(
REQUESTOR, CLIENT_ID
)
def test_initialize(self):
authorizer = prawcore.DeviceIDAuthorizer(self.authentication)
self.assertIsNone(authorizer.access_token)
self.assertIsNone(authorizer.scopes)
self.assertFalse(authorizer.is_valid())
def test_initialize__with_trusted_authenticator(self):
authenticator = prawcore.TrustedAuthenticator(None, None, None)
self.assertRaises(
prawcore.InvalidInvocation,
prawcore.DeviceIDAuthorizer,
authenticator,
)
def test_refresh(self):
authorizer = prawcore.DeviceIDAuthorizer(self.authentication)
with Betamax(REQUESTOR).use_cassette("DeviceIDAuthorizer_refresh"):
authorizer.refresh()
self.assertIsNotNone(authorizer.access_token)
self.assertEqual(set(["*"]), authorizer.scopes)
self.assertTrue(authorizer.is_valid())
def test_refresh__with_short_device_id(self):
authorizer = prawcore.DeviceIDAuthorizer(self.authentication, "a" * 19)
with Betamax(REQUESTOR).use_cassette(
"DeviceIDAuthorizer_refresh__with_short_device_id"
):
self.assertRaises(prawcore.OAuthException, authorizer.refresh)
class ImplicitAuthorizerTest(AuthorizerTestBase):
def test_initialize(self):
authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID)
authorizer = prawcore.ImplicitAuthorizer(
authenticator, "fake token", 1, "modposts read"
)
self.assertEqual("fake token", authorizer.access_token)
self.assertEqual({"modposts", "read"}, authorizer.scopes)
self.assertTrue(authorizer.is_valid())
def test_initialize__with_trusted_authenticator(self):
self.assertRaises(
prawcore.InvalidInvocation,
prawcore.ImplicitAuthorizer,
self.authentication,
None,
None,
None,
)
class ReadOnlyAuthorizerTest(AuthorizerTestBase):
def test_initialize__with_untrusted_authenticator(self):
authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID)
self.assertRaises(
prawcore.InvalidInvocation,
prawcore.ReadOnlyAuthorizer,
authenticator,
)
def test_refresh(self):
authorizer = prawcore.ReadOnlyAuthorizer(self.authentication)
self.assertIsNone(authorizer.access_token)
self.assertIsNone(authorizer.scopes)
self.assertFalse(authorizer.is_valid())
with Betamax(REQUESTOR).use_cassette("ReadOnlyAuthorizer_refresh"):
authorizer.refresh()
self.assertIsNotNone(authorizer.access_token)
self.assertEqual(set(["*"]), authorizer.scopes)
self.assertTrue(authorizer.is_valid())
class ScriptAuthorizerTest(AuthorizerTestBase):
def test_initialize__with_untrusted_authenticator(self):
authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID)
self.assertRaises(
prawcore.InvalidInvocation,
prawcore.ScriptAuthorizer,
authenticator,
None,
None,
)
def test_refresh(self):
authorizer = prawcore.ScriptAuthorizer(
self.authentication, USERNAME, PASSWORD
)
self.assertIsNone(authorizer.access_token)
self.assertIsNone(authorizer.scopes)
self.assertFalse(authorizer.is_valid())
with Betamax(REQUESTOR).use_cassette("ScriptAuthorizer_refresh"):
authorizer.refresh()
self.assertIsNotNone(authorizer.access_token)
self.assertEqual(set(["*"]), authorizer.scopes)
self.assertTrue(authorizer.is_valid())
def test_refresh__with_invalid_username_or_password(self):
authorizer = prawcore.ScriptAuthorizer(
self.authentication, USERNAME, "invalidpassword"
)
with Betamax(REQUESTOR).use_cassette(
"ScriptAuthorizer_refresh__with_invalid_username_or_password"
):
self.assertRaises(prawcore.OAuthException, authorizer.refresh)
self.assertFalse(authorizer.is_valid())