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_refresh__with_invalid_token(self): authorizer = prawcore.Authorizer(self.authentication, "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_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 client_authorizer(): authenticator = prawcore.TrustedAuthenticator( REQUESTOR, CLIENT_ID, CLIENT_SECRET ) authorizer = prawcore.Authorizer(authenticator, REFRESH_TOKEN) authorizer.refresh() return authorizer
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_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_refresh(self): authorizer = prawcore.Authorizer(self.authentication, 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_revoke__refresh_token_without_access_set(self): authorizer = prawcore.Authorizer(self.authentication, 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_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_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__access_token_with_refresh_set(self): authorizer = prawcore.Authorizer(self.authentication, 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_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 main(): """Provide the program's entry point when directly executed.""" if len(sys.argv) < 2: print("Usage: {} SCOPE...".format(sys.argv[0])) return 1 authenticator = prawcore.TrustedAuthenticator( prawcore.Requestor("prawcore_refresh_token_example"), os.environ["PRAWCORE_CLIENT_ID"], os.environ["PRAWCORE_CLIENT_SECRET"], os.environ["PRAWCORE_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, "State mismatch. Expected: {} Received: {}".format( state, params["state"] ), ) return 1 elif "error" in params: send_message(client, params["error"]) return 1 authorizer = prawcore.Authorizer(authenticator) authorizer.authorize(params["code"]) send_message(client, "Refresh token: {}".format(authorizer.refresh_token)) return 0
def main(): """Provide the program's entry point when directly executed.""" if len(sys.argv) < 2: print('Usage: {} SCOPE...'.format(sys.argv[0])) return 1 authenticator = prawcore.TrustedAuthenticator( prawcore.Requestor('prawcore_refresh_token_example'), os.environ['PRAWCORE_CLIENT_ID'], os.environ['PRAWCORE_CLIENT_SECRET'], os.environ['PRAWCORE_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, 'State mismatch. Expected: {} Received: {}'.format( state, params['state'])) return 1 elif 'error' in params: send_message(client, params['error']) return 1 authorizer = prawcore.Authorizer(authenticator) authorizer.authorize(params['code']) send_message(client, 'Refresh token: {}'.format(authorizer.refresh_token)) return 0
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_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_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_revoke__without_any_token(self): authorizer = prawcore.Authorizer(self.authentication) self.assertRaises(prawcore.InvalidInvocation, authorizer.revoke)