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
|
"""Test for subclasses of prawcore.auth.BaseAuthenticator class."""
import unittest
from betamax import Betamax
import prawcore
from .conftest import CLIENT_ID, CLIENT_SECRET, REDIRECT_URI, REQUESTOR
class TrustedAuthenticatorTest(unittest.TestCase):
def test_authorize_url(self):
authenticator = prawcore.TrustedAuthenticator(
REQUESTOR, CLIENT_ID, CLIENT_SECRET, REDIRECT_URI
)
url = authenticator.authorize_url(
"permanent", ["identity", "read"], "a_state"
)
self.assertIn(f"client_id={CLIENT_ID}", url)
self.assertIn("duration=permanent", url)
self.assertIn("response_type=code", url)
self.assertIn("scope=identity+read", url)
self.assertIn("state=a_state", url)
def test_authorize_url__fail_with_implicit(self):
authenticator = prawcore.TrustedAuthenticator(
REQUESTOR, CLIENT_ID, CLIENT_SECRET, REDIRECT_URI
)
self.assertRaises(
prawcore.InvalidInvocation,
authenticator.authorize_url,
"temporary",
["identity", "read"],
"a_state",
implicit=True,
)
def test_authorize_url__fail_without_redirect_uri(self):
authenticator = prawcore.TrustedAuthenticator(
REQUESTOR, CLIENT_ID, CLIENT_SECRET
)
self.assertRaises(
prawcore.InvalidInvocation,
authenticator.authorize_url,
"permanent",
["identity"],
"...",
)
def test_revoke_token(self):
authenticator = prawcore.TrustedAuthenticator(
REQUESTOR, CLIENT_ID, CLIENT_SECRET
)
with Betamax(REQUESTOR).use_cassette(
"TrustedAuthenticator_revoke_token"
):
authenticator.revoke_token("dummy token")
def test_revoke_token__with_access_token_hint(self):
authenticator = prawcore.TrustedAuthenticator(
REQUESTOR, CLIENT_ID, CLIENT_SECRET
)
with Betamax(REQUESTOR).use_cassette(
"TrustedAuthenticator_revoke_token__with_access_token_hint"
):
authenticator.revoke_token("dummy token", "access_token")
def test_revoke_token__with_refresh_token_hint(self):
authenticator = prawcore.TrustedAuthenticator(
REQUESTOR, CLIENT_ID, CLIENT_SECRET
)
with Betamax(REQUESTOR).use_cassette(
"TrustedAuthenticator_revoke_token__with_refresh_token_hint"
):
authenticator.revoke_token("dummy token", "refresh_token")
class UntrustedAuthenticatorTest(unittest.TestCase):
def test_authorize_url__code(self):
authenticator = prawcore.UntrustedAuthenticator(
REQUESTOR, CLIENT_ID, REDIRECT_URI
)
url = authenticator.authorize_url(
"permanent", ["identity", "read"], "a_state"
)
self.assertIn(f"client_id={CLIENT_ID}", url)
self.assertIn("duration=permanent", url)
self.assertIn("response_type=code", url)
self.assertIn("scope=identity+read", url)
self.assertIn("state=a_state", url)
def test_authorize_url__token(self):
authenticator = prawcore.UntrustedAuthenticator(
REQUESTOR, CLIENT_ID, REDIRECT_URI
)
url = authenticator.authorize_url(
"temporary", ["identity", "read"], "a_state", implicit=True
)
self.assertIn(f"client_id={CLIENT_ID}", url)
self.assertIn("duration=temporary", url)
self.assertIn("response_type=token", url)
self.assertIn("scope=identity+read", url)
self.assertIn("state=a_state", url)
def test_authorize_url__fail_with_token_and_permanent(self):
authenticator = prawcore.UntrustedAuthenticator(
REQUESTOR, CLIENT_ID, REDIRECT_URI
)
self.assertRaises(
prawcore.InvalidInvocation,
authenticator.authorize_url,
"permanent",
["identity", "read"],
"a_state",
implicit=True,
)
def test_authorize_url__fail_without_redirect_uri(self):
authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID)
self.assertRaises(
prawcore.InvalidInvocation,
authenticator.authorize_url,
"temporary",
["identity"],
"...",
)
def test_revoke_token(self):
authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID)
with Betamax(REQUESTOR).use_cassette(
"UntrustedAuthenticator_revoke_token"
):
authenticator.revoke_token("dummy token")
|