| 1 |
"""Test for prawcore.auth.Authorizer classes.""" |
| 2 |
import unittest |
| 3 |
|
| 4 |
from betamax import Betamax |
| 5 |
|
| 6 |
import prawcore |
| 7 |
|
| 8 |
from .conftest import ( |
| 9 |
CLIENT_ID, |
| 10 |
CLIENT_SECRET, |
| 11 |
PASSWORD, |
| 12 |
PERMANENT_GRANT_CODE, |
| 13 |
REDIRECT_URI, |
| 14 |
REFRESH_TOKEN, |
| 15 |
REQUESTOR, |
| 16 |
TEMPORARY_GRANT_CODE, |
| 17 |
two_factor_callback, |
| 18 |
USERNAME, |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
class AuthorizerTestBase(unittest.TestCase): |
| 23 |
def setUp(self): |
| 24 |
self.authentication = prawcore.TrustedAuthenticator( |
| 25 |
REQUESTOR, CLIENT_ID, CLIENT_SECRET |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
class AuthorizerTest(AuthorizerTestBase): |
| 30 |
def test_authorize__with_permanent_grant(self): |
| 31 |
self.authentication.redirect_uri = REDIRECT_URI |
| 32 |
authorizer = prawcore.Authorizer(self.authentication) |
| 33 |
with Betamax(REQUESTOR).use_cassette( |
| 34 |
"Authorizer_authorize__with_permanent_grant" |
| 35 |
): |
| 36 |
authorizer.authorize(PERMANENT_GRANT_CODE) |
| 37 |
|
| 38 |
self.assertIsNotNone(authorizer.access_token) |
| 39 |
self.assertIsNotNone(authorizer.refresh_token) |
| 40 |
self.assertIsInstance(authorizer.scopes, set) |
| 41 |
self.assertTrue(len(authorizer.scopes) > 0) |
| 42 |
self.assertTrue(authorizer.is_valid()) |
| 43 |
|
| 44 |
def test_authorize__with_temporary_grant(self): |
| 45 |
self.authentication.redirect_uri = REDIRECT_URI |
| 46 |
authorizer = prawcore.Authorizer(self.authentication) |
| 47 |
with Betamax(REQUESTOR).use_cassette( |
| 48 |
"Authorizer_authorize__with_temporary_grant" |
| 49 |
): |
| 50 |
authorizer.authorize(TEMPORARY_GRANT_CODE) |
| 51 |
|
| 52 |
self.assertIsNotNone(authorizer.access_token) |
| 53 |
self.assertIsNone(authorizer.refresh_token) |
| 54 |
self.assertIsInstance(authorizer.scopes, set) |
| 55 |
self.assertTrue(len(authorizer.scopes) > 0) |
| 56 |
self.assertTrue(authorizer.is_valid()) |
| 57 |
|
| 58 |
def test_authorize__with_invalid_code(self): |
| 59 |
self.authentication.redirect_uri = REDIRECT_URI |
| 60 |
authorizer = prawcore.Authorizer(self.authentication) |
| 61 |
with Betamax(REQUESTOR).use_cassette( |
| 62 |
"Authorizer_authorize__with_invalid_code" |
| 63 |
): |
| 64 |
self.assertRaises( |
| 65 |
prawcore.OAuthException, authorizer.authorize, "invalid code" |
| 66 |
) |
| 67 |
self.assertFalse(authorizer.is_valid()) |
| 68 |
|
| 69 |
def test_authorize__fail_without_redirect_uri(self): |
| 70 |
authorizer = prawcore.Authorizer(self.authentication) |
| 71 |
self.assertRaises( |
| 72 |
prawcore.InvalidInvocation, authorizer.authorize, "dummy code" |
| 73 |
) |
| 74 |
self.assertFalse(authorizer.is_valid()) |
| 75 |
|
| 76 |
def test_initialize(self): |
| 77 |
authorizer = prawcore.Authorizer(self.authentication) |
| 78 |
self.assertIsNone(authorizer.access_token) |
| 79 |
self.assertIsNone(authorizer.scopes) |
| 80 |
self.assertIsNone(authorizer.refresh_token) |
| 81 |
self.assertFalse(authorizer.is_valid()) |
| 82 |
|
| 83 |
def test_initialize__with_refresh_token(self): |
| 84 |
authorizer = prawcore.Authorizer( |
| 85 |
self.authentication, refresh_token=REFRESH_TOKEN |
| 86 |
) |
| 87 |
self.assertIsNone(authorizer.access_token) |
| 88 |
self.assertIsNone(authorizer.scopes) |
| 89 |
self.assertEqual(REFRESH_TOKEN, authorizer.refresh_token) |
| 90 |
self.assertFalse(authorizer.is_valid()) |
| 91 |
|
| 92 |
def test_initialize__with_untrusted_authenticator(self): |
| 93 |
authenticator = prawcore.UntrustedAuthenticator(None, None) |
| 94 |
authorizer = prawcore.Authorizer(authenticator) |
| 95 |
self.assertIsNone(authorizer.access_token) |
| 96 |
self.assertIsNone(authorizer.scopes) |
| 97 |
self.assertIsNone(authorizer.refresh_token) |
| 98 |
self.assertFalse(authorizer.is_valid()) |
| 99 |
|
| 100 |
def test_refresh(self): |
| 101 |
authorizer = prawcore.Authorizer( |
| 102 |
self.authentication, refresh_token=REFRESH_TOKEN |
| 103 |
) |
| 104 |
with Betamax(REQUESTOR).use_cassette("Authorizer_refresh"): |
| 105 |
authorizer.refresh() |
| 106 |
|
| 107 |
self.assertIsNotNone(authorizer.access_token) |
| 108 |
self.assertIsInstance(authorizer.scopes, set) |
| 109 |
self.assertTrue(len(authorizer.scopes) > 0) |
| 110 |
self.assertTrue(authorizer.is_valid()) |
| 111 |
|
| 112 |
def test_refresh__post_refresh_callback(self): |
| 113 |
def callback(authorizer): |
| 114 |
self.assertNotEqual(REFRESH_TOKEN, authorizer.refresh_token) |
| 115 |
authorizer.refresh_token = "manually_updated" |
| 116 |
|
| 117 |
authorizer = prawcore.Authorizer( |
| 118 |
self.authentication, |
| 119 |
post_refresh_callback=callback, |
| 120 |
refresh_token=REFRESH_TOKEN, |
| 121 |
) |
| 122 |
with Betamax(REQUESTOR).use_cassette("Authorizer_refresh"): |
| 123 |
authorizer.refresh() |
| 124 |
|
| 125 |
self.assertIsNotNone(authorizer.access_token) |
| 126 |
self.assertEqual("manually_updated", authorizer.refresh_token) |
| 127 |
self.assertIsInstance(authorizer.scopes, set) |
| 128 |
self.assertTrue(len(authorizer.scopes) > 0) |
| 129 |
self.assertTrue(authorizer.is_valid()) |
| 130 |
|
| 131 |
def test_refresh__pre_refresh_callback(self): |
| 132 |
def callback(authorizer): |
| 133 |
self.assertIsNone(authorizer.refresh_token) |
| 134 |
authorizer.refresh_token = REFRESH_TOKEN |
| 135 |
|
| 136 |
authorizer = prawcore.Authorizer( |
| 137 |
self.authentication, pre_refresh_callback=callback |
| 138 |
) |
| 139 |
with Betamax(REQUESTOR).use_cassette("Authorizer_refresh"): |
| 140 |
authorizer.refresh() |
| 141 |
|
| 142 |
self.assertIsNotNone(authorizer.access_token) |
| 143 |
self.assertIsInstance(authorizer.scopes, set) |
| 144 |
self.assertTrue(len(authorizer.scopes) > 0) |
| 145 |
self.assertTrue(authorizer.is_valid()) |
| 146 |
|
| 147 |
def test_refresh__with_invalid_token(self): |
| 148 |
authorizer = prawcore.Authorizer( |
| 149 |
self.authentication, refresh_token="INVALID_TOKEN" |
| 150 |
) |
| 151 |
with Betamax(REQUESTOR).use_cassette( |
| 152 |
"Authorizer_refresh__with_invalid_token" |
| 153 |
): |
| 154 |
self.assertRaises(prawcore.ResponseException, authorizer.refresh) |
| 155 |
self.assertFalse(authorizer.is_valid()) |
| 156 |
|
| 157 |
def test_refresh__without_refresh_token(self): |
| 158 |
authorizer = prawcore.Authorizer(self.authentication) |
| 159 |
self.assertRaises(prawcore.InvalidInvocation, authorizer.refresh) |
| 160 |
self.assertFalse(authorizer.is_valid()) |
| 161 |
|
| 162 |
def test_revoke__access_token_with_refresh_set(self): |
| 163 |
authorizer = prawcore.Authorizer( |
| 164 |
self.authentication, refresh_token=REFRESH_TOKEN |
| 165 |
) |
| 166 |
with Betamax(REQUESTOR).use_cassette( |
| 167 |
"Authorizer_revoke__access_token_with_refresh_set" |
| 168 |
): |
| 169 |
authorizer.refresh() |
| 170 |
authorizer.revoke(only_access=True) |
| 171 |
|
| 172 |
self.assertIsNone(authorizer.access_token) |
| 173 |
self.assertIsNotNone(authorizer.refresh_token) |
| 174 |
self.assertIsNone(authorizer.scopes) |
| 175 |
self.assertFalse(authorizer.is_valid()) |
| 176 |
|
| 177 |
authorizer.refresh() |
| 178 |
|
| 179 |
self.assertTrue(authorizer.is_valid()) |
| 180 |
|
| 181 |
def test_revoke__access_token_without_refresh_set(self): |
| 182 |
self.authentication.redirect_uri = REDIRECT_URI |
| 183 |
authorizer = prawcore.Authorizer(self.authentication) |
| 184 |
with Betamax(REQUESTOR).use_cassette( |
| 185 |
"Authorizer_revoke__access_token_without_refresh_set" |
| 186 |
): |
| 187 |
authorizer.authorize(TEMPORARY_GRANT_CODE) |
| 188 |
authorizer.revoke() |
| 189 |
|
| 190 |
self.assertIsNone(authorizer.access_token) |
| 191 |
self.assertIsNone(authorizer.refresh_token) |
| 192 |
self.assertIsNone(authorizer.scopes) |
| 193 |
self.assertFalse(authorizer.is_valid()) |
| 194 |
|
| 195 |
def test_revoke__refresh_token_with_access_set(self): |
| 196 |
authorizer = prawcore.Authorizer( |
| 197 |
self.authentication, refresh_token=REFRESH_TOKEN |
| 198 |
) |
| 199 |
with Betamax(REQUESTOR).use_cassette( |
| 200 |
"Authorizer_revoke__refresh_token_with_access_set" |
| 201 |
): |
| 202 |
authorizer.refresh() |
| 203 |
authorizer.revoke() |
| 204 |
|
| 205 |
self.assertIsNone(authorizer.access_token) |
| 206 |
self.assertIsNone(authorizer.refresh_token) |
| 207 |
self.assertIsNone(authorizer.scopes) |
| 208 |
self.assertFalse(authorizer.is_valid()) |
| 209 |
|
| 210 |
def test_revoke__refresh_token_without_access_set(self): |
| 211 |
authorizer = prawcore.Authorizer( |
| 212 |
self.authentication, refresh_token=REFRESH_TOKEN |
| 213 |
) |
| 214 |
with Betamax(REQUESTOR).use_cassette( |
| 215 |
"Authorizer_revoke__refresh_token_without_access_set" |
| 216 |
): |
| 217 |
authorizer.revoke() |
| 218 |
|
| 219 |
self.assertIsNone(authorizer.access_token) |
| 220 |
self.assertIsNone(authorizer.refresh_token) |
| 221 |
self.assertIsNone(authorizer.scopes) |
| 222 |
self.assertFalse(authorizer.is_valid()) |
| 223 |
|
| 224 |
def test_revoke__without_access_token(self): |
| 225 |
authorizer = prawcore.Authorizer( |
| 226 |
self.authentication, refresh_token=REFRESH_TOKEN |
| 227 |
) |
| 228 |
self.assertRaises( |
| 229 |
prawcore.InvalidInvocation, authorizer.revoke, only_access=True |
| 230 |
) |
| 231 |
|
| 232 |
def test_revoke__without_any_token(self): |
| 233 |
authorizer = prawcore.Authorizer(self.authentication) |
| 234 |
self.assertRaises(prawcore.InvalidInvocation, authorizer.revoke) |
| 235 |
|
| 236 |
|
| 237 |
class DeviceIDAuthorizerTest(AuthorizerTestBase): |
| 238 |
def setUp(self): |
| 239 |
self.authentication = prawcore.UntrustedAuthenticator( |
| 240 |
REQUESTOR, CLIENT_ID |
| 241 |
) |
| 242 |
|
| 243 |
def test_initialize(self): |
| 244 |
authorizer = prawcore.DeviceIDAuthorizer(self.authentication) |
| 245 |
self.assertIsNone(authorizer.access_token) |
| 246 |
self.assertIsNone(authorizer.scopes) |
| 247 |
self.assertFalse(authorizer.is_valid()) |
| 248 |
|
| 249 |
def test_initialize__with_trusted_authenticator(self): |
| 250 |
authenticator = prawcore.TrustedAuthenticator(None, None, None) |
| 251 |
self.assertRaises( |
| 252 |
prawcore.InvalidInvocation, |
| 253 |
prawcore.DeviceIDAuthorizer, |
| 254 |
authenticator, |
| 255 |
) |
| 256 |
|
| 257 |
def test_refresh(self): |
| 258 |
authorizer = prawcore.DeviceIDAuthorizer(self.authentication) |
| 259 |
with Betamax(REQUESTOR).use_cassette("DeviceIDAuthorizer_refresh"): |
| 260 |
authorizer.refresh() |
| 261 |
|
| 262 |
self.assertIsNotNone(authorizer.access_token) |
| 263 |
self.assertEqual(set(["*"]), authorizer.scopes) |
| 264 |
self.assertTrue(authorizer.is_valid()) |
| 265 |
|
| 266 |
def test_refresh__with_scopes(self): |
| 267 |
scope_list = ["adsedit", "adsread", "creddits", "history"] |
| 268 |
authorizer = prawcore.DeviceIDAuthorizer( |
| 269 |
self.authentication, |
| 270 |
scopes=scope_list, |
| 271 |
) |
| 272 |
with Betamax(REQUESTOR).use_cassette( |
| 273 |
"DeviceIDAuthorizer_refresh__with_scopes" |
| 274 |
): |
| 275 |
authorizer.refresh() |
| 276 |
|
| 277 |
self.assertIsNotNone(authorizer.access_token) |
| 278 |
self.assertEqual(set(scope_list), authorizer.scopes) |
| 279 |
self.assertTrue(authorizer.is_valid()) |
| 280 |
|
| 281 |
def test_refresh__with_short_device_id(self): |
| 282 |
authorizer = prawcore.DeviceIDAuthorizer(self.authentication, "a" * 19) |
| 283 |
with Betamax(REQUESTOR).use_cassette( |
| 284 |
"DeviceIDAuthorizer_refresh__with_short_device_id" |
| 285 |
): |
| 286 |
self.assertRaises(prawcore.OAuthException, authorizer.refresh) |
| 287 |
|
| 288 |
|
| 289 |
class ImplicitAuthorizerTest(AuthorizerTestBase): |
| 290 |
def test_initialize(self): |
| 291 |
authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID) |
| 292 |
authorizer = prawcore.ImplicitAuthorizer( |
| 293 |
authenticator, "fake token", 1, "modposts read" |
| 294 |
) |
| 295 |
self.assertEqual("fake token", authorizer.access_token) |
| 296 |
self.assertEqual({"modposts", "read"}, authorizer.scopes) |
| 297 |
self.assertTrue(authorizer.is_valid()) |
| 298 |
|
| 299 |
def test_initialize__with_trusted_authenticator(self): |
| 300 |
self.assertRaises( |
| 301 |
prawcore.InvalidInvocation, |
| 302 |
prawcore.ImplicitAuthorizer, |
| 303 |
self.authentication, |
| 304 |
None, |
| 305 |
None, |
| 306 |
None, |
| 307 |
) |
| 308 |
|
| 309 |
|
| 310 |
class ReadOnlyAuthorizerTest(AuthorizerTestBase): |
| 311 |
def test_initialize__with_untrusted_authenticator(self): |
| 312 |
authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID) |
| 313 |
self.assertRaises( |
| 314 |
prawcore.InvalidInvocation, |
| 315 |
prawcore.ReadOnlyAuthorizer, |
| 316 |
authenticator, |
| 317 |
) |
| 318 |
|
| 319 |
def test_refresh(self): |
| 320 |
authorizer = prawcore.ReadOnlyAuthorizer(self.authentication) |
| 321 |
self.assertIsNone(authorizer.access_token) |
| 322 |
self.assertIsNone(authorizer.scopes) |
| 323 |
self.assertFalse(authorizer.is_valid()) |
| 324 |
|
| 325 |
with Betamax(REQUESTOR).use_cassette("ReadOnlyAuthorizer_refresh"): |
| 326 |
authorizer.refresh() |
| 327 |
|
| 328 |
self.assertIsNotNone(authorizer.access_token) |
| 329 |
self.assertEqual(set(["*"]), authorizer.scopes) |
| 330 |
self.assertTrue(authorizer.is_valid()) |
| 331 |
|
| 332 |
def test_refresh__with_scopes(self): |
| 333 |
scope_list = ["adsedit", "adsread", "creddits", "history"] |
| 334 |
authorizer = prawcore.ReadOnlyAuthorizer( |
| 335 |
self.authentication, scopes=scope_list |
| 336 |
) |
| 337 |
self.assertIsNone(authorizer.access_token) |
| 338 |
self.assertIsNone(authorizer.scopes) |
| 339 |
self.assertFalse(authorizer.is_valid()) |
| 340 |
|
| 341 |
with Betamax(REQUESTOR).use_cassette( |
| 342 |
"ReadOnlyAuthorizer_refresh__with_scopes" |
| 343 |
): |
| 344 |
authorizer.refresh() |
| 345 |
|
| 346 |
self.assertIsNotNone(authorizer.access_token) |
| 347 |
self.assertEqual(set(scope_list), authorizer.scopes) |
| 348 |
self.assertTrue(authorizer.is_valid()) |
| 349 |
|
| 350 |
|
| 351 |
class ScriptAuthorizerTest(AuthorizerTestBase): |
| 352 |
def test_initialize__with_untrusted_authenticator(self): |
| 353 |
authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID) |
| 354 |
self.assertRaises( |
| 355 |
prawcore.InvalidInvocation, |
| 356 |
prawcore.ScriptAuthorizer, |
| 357 |
authenticator, |
| 358 |
None, |
| 359 |
None, |
| 360 |
) |
| 361 |
|
| 362 |
def test_refresh(self): |
| 363 |
authorizer = prawcore.ScriptAuthorizer( |
| 364 |
self.authentication, USERNAME, PASSWORD |
| 365 |
) |
| 366 |
self.assertIsNone(authorizer.access_token) |
| 367 |
self.assertIsNone(authorizer.scopes) |
| 368 |
self.assertFalse(authorizer.is_valid()) |
| 369 |
|
| 370 |
with Betamax(REQUESTOR).use_cassette("ScriptAuthorizer_refresh"): |
| 371 |
authorizer.refresh() |
| 372 |
|
| 373 |
self.assertIsNotNone(authorizer.access_token) |
| 374 |
self.assertEqual(set(["*"]), authorizer.scopes) |
| 375 |
self.assertTrue(authorizer.is_valid()) |
| 376 |
|
| 377 |
def test_refresh__with_invalid_otp(self): |
| 378 |
authorizer = prawcore.ScriptAuthorizer( |
| 379 |
self.authentication, |
| 380 |
USERNAME, |
| 381 |
PASSWORD, |
| 382 |
lambda: "fake", |
| 383 |
) |
| 384 |
|
| 385 |
with Betamax(REQUESTOR).use_cassette( |
| 386 |
"ScriptAuthorizer_refresh__with_invalid_otp" |
| 387 |
): |
| 388 |
self.assertRaises(prawcore.OAuthException, authorizer.refresh) |
| 389 |
self.assertFalse(authorizer.is_valid()) |
| 390 |
|
| 391 |
def test_refresh__with_invalid_username_or_password(self): |
| 392 |
authorizer = prawcore.ScriptAuthorizer( |
| 393 |
self.authentication, USERNAME, "invalidpassword" |
| 394 |
) |
| 395 |
with Betamax(REQUESTOR).use_cassette( |
| 396 |
"ScriptAuthorizer_refresh__with_invalid_username_or_password" |
| 397 |
): |
| 398 |
self.assertRaises(prawcore.OAuthException, authorizer.refresh) |
| 399 |
self.assertFalse(authorizer.is_valid()) |
| 400 |
|
| 401 |
def test_refresh__with_scopes(self): |
| 402 |
scope_list = ["adsedit", "adsread", "creddits", "history"] |
| 403 |
authorizer = prawcore.ScriptAuthorizer( |
| 404 |
self.authentication, USERNAME, PASSWORD, scopes=scope_list |
| 405 |
) |
| 406 |
with Betamax(REQUESTOR).use_cassette( |
| 407 |
"ScriptAuthorizer_refresh__with_scopes" |
| 408 |
): |
| 409 |
authorizer.refresh() |
| 410 |
|
| 411 |
self.assertIsNotNone(authorizer.access_token) |
| 412 |
self.assertEqual(set(scope_list), authorizer.scopes) |
| 413 |
self.assertTrue(authorizer.is_valid()) |
| 414 |
|
| 415 |
def test_refresh__with_valid_otp(self): |
| 416 |
authorizer = prawcore.ScriptAuthorizer( |
| 417 |
self.authentication, |
| 418 |
USERNAME, |
| 419 |
PASSWORD, |
| 420 |
lambda: "000000", |
| 421 |
) |
| 422 |
self.assertIsNone(authorizer.access_token) |
| 423 |
self.assertIsNone(authorizer.scopes) |
| 424 |
self.assertFalse(authorizer.is_valid()) |
| 425 |
|
| 426 |
with Betamax(REQUESTOR).use_cassette( |
| 427 |
"ScriptAuthorizer_refresh__with_valid_otp" |
| 428 |
): |
| 429 |
authorizer.refresh() |
| 430 |
|
| 431 |
self.assertIsNotNone(authorizer.access_token) |
| 432 |
self.assertEqual(set(["*"]), authorizer.scopes) |
| 433 |
self.assertTrue(authorizer.is_valid()) |