def test_token_is_valid(self): api = APIManager("CLIENT_ID", "CLIENT_SECRET", self.server.path, compression_enabled=False, backoff_config=None, token_override=None) self.assertTrue(api.is_access_token_valid())
def test_token_invalid(self): api = APIManager("CLIENT_ID", "CLIENT_SECRET", self.server.path, compression_enabled=False, backoff_config=None, token_override=None) api.access_token['requested_at'] = datetime.datetime.now( ) - datetime.timedelta(hours=2) self.assertFalse(api.is_access_token_valid())
def test_compression_put(self): api = APIManager("CLIENT_ID", "CLIENT_SECRET", self.server.path, True) content = {"some_property": "some_value", "some_boolean": True} r = api.put("compression_enabled", content) self.assertIn('Content-Encoding', r.request.headers) self.assertEquals('gzip', r.request.headers['Content-Encoding']) self.assertIn('Content-Encoding', r.headers) self.assertEquals('gzip', r.headers['Content-Encoding']) # content already decompressed by python-requests self.assertEquals(content, r.json())
def tests_host_non_reachable(self): with self.assertRaises(ValueError) as ctx: APIManager("CLIENT_ID", "CLIENT_SECRET", "http://non-reachable.example.com") self.assertEquals( ctx.exception.message, "Host at {} is not reachable".format( "http://non-reachable.example.com"))
def setUpClass(cls): cls.server = LocalApiServer() cls.server.start() cls.api = APIManager("CLIENT_ID", "CLIENT_SECRET", cls.server.path, compression_enabled=False, backoff_config=None, token_override=None)
def test_refresh_token(self): api = APIManager("CLIENT_ID", "CLIENT_SECRET", self.server.path) api.access_token['requested_at'] = datetime.datetime.now( ) - datetime.timedelta(hours=2) self.assertFalse(api.is_access_token_valid()) api.get("api_manager?parameter") self.assertTrue(api.is_access_token_valid())
def test_fetch_token_at_init(self): api = APIManager("CLIENT_ID", "CLIENT_SECRET", self.server.path, compression_enabled=False, backoff_config=None, token_override=None) self.assertIn("access_token", api.access_token) self.assertIn("expires_in", api.access_token) self.assertIn("requested_at", api.access_token)
def test_client_secret_null(self): with self.assertRaises(ValueError) as ctx: APIManager("CLIENT_ID", "", self.server.path, compression_enabled=False, backoff_config=None, token_override=None) self.assertEqual(ctx.exception.message, "client_secret cannot be null or empty.")
def test_compression_post(self): api = APIManager("CLIENT_ID", "CLIENT_SECRET", self.server.path, compression_enabled=True, backoff_config=None, token_override=None) content = {"some_property": "some_value", "some_boolean": True} r = api.post("/api/v3/compression_enabled", content) self.assertIn('Content-Encoding', r.request.headers) self.assertEqual('gzip', r.request.headers['Content-Encoding']) self.assertIn('Content-Encoding', r.headers) self.assertEqual('gzip', r.headers['Content-Encoding']) # content already decompressed by python-requests self.assertEqual(content, r.json())
def test_fetch_token_at_init_fail(self): with self.assertRaises(HTTPError) as ctx: api = APIManager("CLIENT_ID", "CLIENT_SECRET", self.server.path + '/fail', compression_enabled=False, backoff_config=None, token_override=None) self.assertTrue( ctx.exception.message.startswith( "502 Server Error: Bad Gateway for url: http:"))
def tests_host_non_reachable(self): with self.assertRaises(ValueError) as ctx: APIManager("CLIENT_ID", "CLIENT_SECRET", "http://non-reachable.example.com", compression_enabled=False, backoff_config=None, token_override=None) self.assertEqual( ctx.exception.message, "Host at {} is not reachable".format( "http://non-reachable.example.com"))
def test_authorization_static_token(self): api = APIManager(None, None, self.server.path, compression_enabled=False, backoff_config=None, token_override="static_token") r = api.get("/api/v3/api_manager") self.assertIn('Authorization', r.request.headers) self.assertEqual(r.request.headers['Authorization'], 'Bearer static_token') r = api.post("/api/v3/api_manager", {}) self.assertIn('Authorization', r.request.headers) self.assertEqual(r.request.headers['Authorization'], 'Bearer static_token') r = api.put("/api/v3/api_manager", {}) self.assertIn('Authorization', r.request.headers) self.assertEqual(r.request.headers['Authorization'], 'Bearer static_token') r = api.delete("/api/v3/api_manager") self.assertIn('Authorization', r.request.headers) self.assertEqual(r.request.headers['Authorization'], 'Bearer static_token')
def setUpClass(cls): cls.server = LocalApiServer() cls.server.start() cls.api = APIManager("CLIENT_ID", "CLIENT_SECRET", cls.server.path, False)
def test_client_secret_null(self): with self.assertRaises(ValueError) as ctx: APIManager("CLIENT_ID", "", self.server.path) self.assertEquals(ctx.exception.message, "client_secret cannot be null or empty.")
def test_fetch_token_at_init(self): api = APIManager("CLIENT_ID", "CLIENT_SECRET", self.server.path) self.assertIn("access_token", api.access_token) self.assertIn("expires_in", api.access_token) self.assertIn("requested_at", api.access_token)
def test_token_invalid(self): api = APIManager("CLIENT_ID", "CLIENT_SECRET", self.server.path) api.access_token['requested_at'] = datetime.datetime.now( ) - datetime.timedelta(hours=2) self.assertFalse(api.is_access_token_valid())
def test_token_is_valid(self): api = APIManager("CLIENT_ID", "CLIENT_SECRET", self.server.path) self.assertTrue(api.is_access_token_valid())