| 1 | """Test for subclasses of prawcore.auth.BaseAuthenticator class.""" |
| 2 | import unittest |
| 3 | |
| 4 | from betamax import Betamax |
| 5 | |
| 6 | import prawcore |
| 7 | |
| 8 | from .conftest import CLIENT_ID, CLIENT_SECRET, REDIRECT_URI, REQUESTOR |
| 9 | |
| 10 | |
| 11 | class TrustedAuthenticatorTest(unittest.TestCase): |
| 12 | def test_authorize_url(self): |
| 13 | authenticator = prawcore.TrustedAuthenticator( |
| 14 | REQUESTOR, CLIENT_ID, CLIENT_SECRET, REDIRECT_URI |
| 15 | ) |
| 16 | url = authenticator.authorize_url( |
| 17 | "permanent", ["identity", "read"], "a_state" |
| 18 | ) |
| 19 | self.assertIn(f"client_id={CLIENT_ID}", url) |
| 20 | self.assertIn("duration=permanent", url) |
| 21 | self.assertIn("response_type=code", url) |
| 22 | self.assertIn("scope=identity+read", url) |
| 23 | self.assertIn("state=a_state", url) |
| 24 | |
| 25 | def test_authorize_url__fail_with_implicit(self): |
| 26 | authenticator = prawcore.TrustedAuthenticator( |
| 27 | REQUESTOR, CLIENT_ID, CLIENT_SECRET, REDIRECT_URI |
| 28 | ) |
| 29 | self.assertRaises( |
| 30 | prawcore.InvalidInvocation, |
| 31 | authenticator.authorize_url, |
| 32 | "temporary", |
| 33 | ["identity", "read"], |
| 34 | "a_state", |
| 35 | implicit=True, |
| 36 | ) |
| 37 | |
| 38 | def test_authorize_url__fail_without_redirect_uri(self): |
| 39 | authenticator = prawcore.TrustedAuthenticator( |
| 40 | REQUESTOR, CLIENT_ID, CLIENT_SECRET |
| 41 | ) |
| 42 | self.assertRaises( |
| 43 | prawcore.InvalidInvocation, |
| 44 | authenticator.authorize_url, |
| 45 | "permanent", |
| 46 | ["identity"], |
| 47 | "...", |
| 48 | ) |
| 49 | |
| 50 | def test_revoke_token(self): |
| 51 | authenticator = prawcore.TrustedAuthenticator( |
| 52 | REQUESTOR, CLIENT_ID, CLIENT_SECRET |
| 53 | ) |
| 54 | with Betamax(REQUESTOR).use_cassette( |
| 55 | "TrustedAuthenticator_revoke_token" |
| 56 | ): |
| 57 | authenticator.revoke_token("dummy token") |
| 58 | |
| 59 | def test_revoke_token__with_access_token_hint(self): |
| 60 | authenticator = prawcore.TrustedAuthenticator( |
| 61 | REQUESTOR, CLIENT_ID, CLIENT_SECRET |
| 62 | ) |
| 63 | with Betamax(REQUESTOR).use_cassette( |
| 64 | "TrustedAuthenticator_revoke_token__with_access_token_hint" |
| 65 | ): |
| 66 | authenticator.revoke_token("dummy token", "access_token") |
| 67 | |
| 68 | def test_revoke_token__with_refresh_token_hint(self): |
| 69 | authenticator = prawcore.TrustedAuthenticator( |
| 70 | REQUESTOR, CLIENT_ID, CLIENT_SECRET |
| 71 | ) |
| 72 | with Betamax(REQUESTOR).use_cassette( |
| 73 | "TrustedAuthenticator_revoke_token__with_refresh_token_hint" |
| 74 | ): |
| 75 | authenticator.revoke_token("dummy token", "refresh_token") |
| 76 | |
| 77 | |
| 78 | class UntrustedAuthenticatorTest(unittest.TestCase): |
| 79 | def test_authorize_url__code(self): |
| 80 | authenticator = prawcore.UntrustedAuthenticator( |
| 81 | REQUESTOR, CLIENT_ID, REDIRECT_URI |
| 82 | ) |
| 83 | url = authenticator.authorize_url( |
| 84 | "permanent", ["identity", "read"], "a_state" |
| 85 | ) |
| 86 | self.assertIn(f"client_id={CLIENT_ID}", url) |
| 87 | self.assertIn("duration=permanent", url) |
| 88 | self.assertIn("response_type=code", url) |
| 89 | self.assertIn("scope=identity+read", url) |
| 90 | self.assertIn("state=a_state", url) |
| 91 | |
| 92 | def test_authorize_url__token(self): |
| 93 | authenticator = prawcore.UntrustedAuthenticator( |
| 94 | REQUESTOR, CLIENT_ID, REDIRECT_URI |
| 95 | ) |
| 96 | url = authenticator.authorize_url( |
| 97 | "temporary", ["identity", "read"], "a_state", implicit=True |
| 98 | ) |
| 99 | self.assertIn(f"client_id={CLIENT_ID}", url) |
| 100 | self.assertIn("duration=temporary", url) |
| 101 | self.assertIn("response_type=token", url) |
| 102 | self.assertIn("scope=identity+read", url) |
| 103 | self.assertIn("state=a_state", url) |
| 104 | |
| 105 | def test_authorize_url__fail_with_token_and_permanent(self): |
| 106 | authenticator = prawcore.UntrustedAuthenticator( |
| 107 | REQUESTOR, CLIENT_ID, REDIRECT_URI |
| 108 | ) |
| 109 | self.assertRaises( |
| 110 | prawcore.InvalidInvocation, |
| 111 | authenticator.authorize_url, |
| 112 | "permanent", |
| 113 | ["identity", "read"], |
| 114 | "a_state", |
| 115 | implicit=True, |
| 116 | ) |
| 117 | |
| 118 | def test_authorize_url__fail_without_redirect_uri(self): |
| 119 | authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID) |
| 120 | self.assertRaises( |
| 121 | prawcore.InvalidInvocation, |
| 122 | authenticator.authorize_url, |
| 123 | "temporary", |
| 124 | ["identity"], |
| 125 | "...", |
| 126 | ) |
| 127 | |
| 128 | def test_revoke_token(self): |
| 129 | authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID) |
| 130 | with Betamax(REQUESTOR).use_cassette( |
| 131 | "UntrustedAuthenticator_revoke_token" |
| 132 | ): |
| 133 | authenticator.revoke_token("dummy token") |