def test_initialize__with_untrusted_authenticator(self): authenticator = asyncprawcore.UntrustedAuthenticator(None, None) authorizer = asyncprawcore.Authorizer(authenticator) self.assertIsNone(authorizer.access_token) self.assertIsNone(authorizer.scopes) self.assertIsNone(authorizer.refresh_token) self.assertFalse(authorizer.is_valid())
async def test_authorize__with_invalid_code(self): self.authentication.redirect_uri = REDIRECT_URI authorizer = asyncprawcore.Authorizer(self.authentication) with VCR.use_cassette("Authorizer_authorize__with_invalid_code"): with self.assertRaises(asyncprawcore.OAuthException): await authorizer.authorize("invalid code") self.assertFalse(authorizer.is_valid())
def test_initialize__with_refresh_token(self): authorizer = asyncprawcore.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())
async def test_refresh__with_invalid_token(self): authorizer = asyncprawcore.Authorizer(self.authentication, refresh_token="INVALID_TOKEN") with VCR.use_cassette("Authorizer_refresh__with_invalid_token"): with self.assertRaises(asyncprawcore.ResponseException): await authorizer.refresh() self.assertFalse(authorizer.is_valid())
async def test_refresh(self): authorizer = asyncprawcore.Authorizer(self.authentication, refresh_token=REFRESH_TOKEN) with VCR.use_cassette("Authorizer_refresh"): await authorizer.refresh() self.assertIsNotNone(authorizer.access_token) self.assertIsInstance(authorizer.scopes, set) self.assertTrue(len(authorizer.scopes) > 0) self.assertTrue(authorizer.is_valid())
async def client_authorizer(): requestor = asyncprawcore.requestor.Requestor( "asyncprawcore:test (by /u/Lil_SpazJoekp)") authenticator = asyncprawcore.TrustedAuthenticator(requestor, CLIENT_ID, CLIENT_SECRET) authorizer = asyncprawcore.Authorizer(authenticator, refresh_token=REFRESH_TOKEN) await authorizer.refresh() return authorizer
async def test_authorize__with_temporary_grant(self): self.authentication.redirect_uri = REDIRECT_URI authorizer = asyncprawcore.Authorizer(self.authentication) with VCR.use_cassette("Authorizer_authorize__with_temporary_grant"): await 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())
async def test_revoke__refresh_token_without_access_set(self): authorizer = asyncprawcore.Authorizer(self.authentication, refresh_token=REFRESH_TOKEN) with VCR.use_cassette( "Authorizer_revoke__refresh_token_without_access_set"): await authorizer.revoke() self.assertIsNone(authorizer.access_token) self.assertIsNone(authorizer.refresh_token) self.assertIsNone(authorizer.scopes) self.assertFalse(authorizer.is_valid())
async def test_revoke__access_token_without_refresh_set(self): self.authentication.redirect_uri = REDIRECT_URI authorizer = asyncprawcore.Authorizer(self.authentication) with VCR.use_cassette( "Authorizer_revoke__access_token_without_refresh_set"): await authorizer.authorize(TEMPORARY_GRANT_CODE) await authorizer.revoke() self.assertIsNone(authorizer.access_token) self.assertIsNone(authorizer.refresh_token) self.assertIsNone(authorizer.scopes) self.assertFalse(authorizer.is_valid())
async def test_refresh__post_refresh_callback(self): def callback(authorizer): self.assertNotEqual(REFRESH_TOKEN, authorizer.refresh_token) authorizer.refresh_token = "manually_updated" authorizer = asyncprawcore.Authorizer( self.authentication, post_refresh_callback=callback, refresh_token=REFRESH_TOKEN, ) with VCR.use_cassette("Authorizer_refresh"): await 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())
async def main(): """Provide the program's entry point when directly executed.""" if len(sys.argv) < 2: print(f"Usage: {sys.argv[0]} SCOPE...") return 1 requestor = asyncprawcore.Requestor("asyncprawcore_refresh_token_example") try: authenticator = asyncprawcore.TrustedAuthenticator( requestor, os.environ["ASYNCPRAWCORE_CLIENT_ID"], os.environ["ASYNCPRAWCORE_CLIENT_SECRET"], os.environ["ASYNCPRAWCORE_REDIRECT_URI"], ) state = str(random.randint(0, 65000)) url = authenticator.authorize_url("permanent", sys.argv[1:], state) print(url) client = receive_connection() data = client.recv(1024).decode("utf-8") param_tokens = data.split(" ", 2)[1].split("?", 1)[1].split("&") params = { key: value for (key, value) in [token.split("=") for token in param_tokens] } if state != params["state"]: send_message( client, f"State mismatch. Expected: {state} Received: {params['state']}", ) return 1 elif "error" in params: send_message(client, params["error"]) return 1 authorizer = asyncprawcore.Authorizer(authenticator) await authorizer.authorize(params["code"]) send_message(client, f"Refresh token: {authorizer.refresh_token}") return 0 finally: await requestor.close()
def test_initialize(self): authorizer = asyncprawcore.Authorizer(self.authentication) self.assertIsNone(authorizer.access_token) self.assertIsNone(authorizer.scopes) self.assertIsNone(authorizer.refresh_token) self.assertFalse(authorizer.is_valid())
async def test_authorize__fail_without_redirect_uri(self): authorizer = asyncprawcore.Authorizer(self.authentication) with self.assertRaises(asyncprawcore.InvalidInvocation): await authorizer.authorize("dummy code") self.assertFalse(authorizer.is_valid())
async def test_revoke__without_any_token(self): authorizer = asyncprawcore.Authorizer(self.authentication) with self.assertRaises(asyncprawcore.InvalidInvocation): await authorizer.revoke()
async def test_revoke__without_access_token(self): authorizer = asyncprawcore.Authorizer(self.authentication, refresh_token=REFRESH_TOKEN) with self.assertRaises(asyncprawcore.InvalidInvocation): await authorizer.revoke(only_access=True)
async def test_refresh__without_refresh_token(self): authorizer = asyncprawcore.Authorizer(self.authentication) with self.assertRaises(asyncprawcore.InvalidInvocation): await authorizer.refresh() self.assertFalse(authorizer.is_valid())