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())
def test_initialize__with_untrusted_authenticator(self): authenticator = asyncprawcore.UntrustedAuthenticator( self.requestor, CLIENT_ID) self.assertRaises( asyncprawcore.InvalidInvocation, asyncprawcore.ReadOnlyAuthorizer, authenticator, )
def test_initialize(self): authenticator = asyncprawcore.UntrustedAuthenticator( self.requestor, CLIENT_ID) authorizer = asyncprawcore.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())
async def test_authorize_url__code(self): authenticator = asyncprawcore.UntrustedAuthenticator( self.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_without_redirect_uri(self): authenticator = asyncprawcore.UntrustedAuthenticator( self.requestor, CLIENT_ID) self.assertRaises( asyncprawcore.InvalidInvocation, authenticator.authorize_url, "temporary", ["identity"], "...", )
async def test_authorize_url__fail_with_token_and_permanent(self): authenticator = asyncprawcore.UntrustedAuthenticator( self.requestor, CLIENT_ID, REDIRECT_URI) with self.assertRaises(asyncprawcore.InvalidInvocation): authenticator.authorize_url( "permanent", ["identity", "read"], "a_state", implicit=True, )
async def test_request__with_invalid_access_token(self): self.authenticator = asyncprawcore.UntrustedAuthenticator( self.requestor, CLIENT_ID) self.authorizer = asyncprawcore.ImplicitAuthorizer( self.authenticator, None, 0, "") self.session = asyncprawcore.Session(self.authorizer) with VCR.use_cassette("Session_request__with_invalid_access_token"): self.session._authorizer.access_token = "invalid" with self.assertRaises(asyncprawcore.InvalidToken): await self.session.request("get", "/")
async def test_authorize_url__token(self): authenticator = asyncprawcore.UntrustedAuthenticator( self.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)
async def test_request__unavailable_for_legal_reasons(self): with VCR.use_cassette( "Session_request__unavailable_for_legal_reasons"): authenticator = asyncprawcore.UntrustedAuthenticator( self.requestor, CLIENT_ID) authorizer = asyncprawcore.ImplicitAuthorizer( authenticator, None, 0, "") self.session = asyncprawcore.Session(authorizer) exception_class = asyncprawcore.UnavailableForLegalReasons with self.assertRaises(exception_class) as context_manager: await self.session.request("GET", "/") self.assertEqual(451, context_manager.exception.response.status)
async def main(): """Provide the program's entry point when directly executed.""" if len(sys.argv) != 2: print(f"Usage: {sys.argv[0]} USERNAME") return 1 authenticator = asyncprawcore.UntrustedAuthenticator( asyncprawcore.Requestor("asyncprawcore_device_id_auth_example"), os.environ["asyncprawcore_CLIENT_ID"], ) authorizer = asyncprawcore.DeviceIDAuthorizer(authenticator) await authorizer.refresh() user = sys.argv[1] async with asyncprawcore.session(authorizer) as session: data = await session.request("GET", f"/api/v1/user/{user}/trophies") for trophy in data["data"]["trophies"]: description = trophy["data"]["description"] print(trophy["data"]["name"] + (f" ({description})" if description else "")) return 0
async def setUp(self): self.requestor = Requestor("asyncprawcore:test (by /u/Lil_SpazJoekp)") self.authentication = asyncprawcore.UntrustedAuthenticator( self.requestor, CLIENT_ID)
def test_init__with_device_id_authorizer(self): authenticator = asyncprawcore.UntrustedAuthenticator( self.requestor, CLIENT_ID) authorizer = asyncprawcore.DeviceIDAuthorizer(authenticator) asyncprawcore.Session(authorizer)
def test_init__with_implicit_authorizer(self): authenticator = asyncprawcore.UntrustedAuthenticator( self.requestor, CLIENT_ID) authorizer = asyncprawcore.ImplicitAuthorizer(authenticator, None, 0, "") asyncprawcore.Session(authorizer)
async def test_revoke_token(self): authenticator = asyncprawcore.UntrustedAuthenticator( self.requestor, CLIENT_ID) with VCR.use_cassette("UntrustedAuthenticator_revoke_token"): await authenticator.revoke_token("dummy token")