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 test_initialize(self): authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID) authorizer = prawcore.ImplicitAuthorizer(authenticator, "fake token", 1, "modposts read") self.assertEqual("fake token", authorizer.access_token) self.assertEqual({"modposts", "read"}, authorizer.scopes) self.assertTrue(authorizer.is_valid())
def test_initialize__with_untrusted_authenticator(self): authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID) self.assertRaises( prawcore.InvalidInvocation, prawcore.ReadOnlyAuthorizer, authenticator, )
def test_initialize(self): authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID) authorizer = prawcore.ImplicitAuthorizer(authenticator, 'fake token', 1, 'modposts read') self.assertEqual('fake token', authorizer.access_token) self.assertEqual({'modposts', 'read'}, authorizer.scopes) self.assertTrue(authorizer.is_valid())
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_request__with_invalid_access_token(self): authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID) authorizer = prawcore.ImplicitAuthorizer(authenticator, None, 0, "") session = prawcore.Session(authorizer) with Betamax(REQUESTOR).use_cassette( "Session_request__with_invalid_access_token"): session._authorizer.access_token = "invalid" self.assertRaises(prawcore.InvalidToken, session.request, "get", "/")
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('client_id={}'.format(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__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__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__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__token(self): authenticator = prawcore.UntrustedAuthenticator( REQUESTOR, CLIENT_ID, REDIRECT_URI) url = authenticator.authorize_url('temporary', ['identity', 'read'], 'a_state', implicit=True) self.assertIn('client_id={}'.format(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 main(): """Provide the program's entry point when directly executed.""" if len(sys.argv) != 2: print('Usage: {} USERNAME'.format(sys.argv[0])) return 1 authenticator = prawcore.UntrustedAuthenticator( prawcore.Requestor('prawcore_device_id_auth_example'), os.environ['PRAWCORE_CLIENT_ID']) authorizer = prawcore.DeviceIDAuthorizer(authenticator) authorizer.refresh() user = sys.argv[1] with prawcore.session(authorizer) as session: data = session.request('GET', '/api/v1/user/{}/trophies'.format(user)) for trophy in data['data']['trophies']: description = trophy['data']['description'] print(trophy['data']['name'] + (' ({})'.format(description) if description else '')) return 0
def main(): """Provide the program's entry point when directly executed.""" if len(sys.argv) != 2: print("Usage: {} USERNAME".format(sys.argv[0])) return 1 authenticator = prawcore.UntrustedAuthenticator( prawcore.Requestor("prawcore_device_id_auth_example"), os.environ["PRAWCORE_CLIENT_ID"], ) authorizer = prawcore.DeviceIDAuthorizer(authenticator) authorizer.refresh() user = sys.argv[1] with prawcore.session(authorizer) as session: data = session.request("GET", "/api/v1/user/{}/trophies".format(user)) for trophy in data["data"]["trophies"]: description = trophy["data"]["description"] print(trophy["data"]["name"] + (" ({})".format(description) if description else "")) return 0
def test_init__with_implicit_authorizer(self): authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID) authorizer = prawcore.ImplicitAuthorizer(authenticator, None, 0, "") prawcore.Session(authorizer)
def test_init__with_device_id_authorizer(self): authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID) authorizer = prawcore.DeviceIDAuthorizer(authenticator) prawcore.Session(authorizer)
def test_revoke_token(self): authenticator = prawcore.UntrustedAuthenticator(REQUESTOR, CLIENT_ID) with Betamax(REQUESTOR).use_cassette( 'UntrustedAuthenticator_revoke_token'): authenticator.revoke_token('dummy token')
def setUp(self): self.authentication = prawcore.UntrustedAuthenticator( REQUESTOR, CLIENT_ID )