示例#1
0
 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())
示例#2
0
 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())
示例#3
0
    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())
示例#4
0
 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"))
示例#5
0
 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)
示例#6
0
    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())
示例#7
0
 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)
示例#8
0
 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.")
示例#9
0
    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())
示例#10
0
 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:"))
示例#11
0
 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"))
示例#12
0
    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')
示例#13
0
 def setUpClass(cls):
     cls.server = LocalApiServer()
     cls.server.start()
     cls.api = APIManager("CLIENT_ID", "CLIENT_SECRET", cls.server.path,
                          False)
示例#14
0
 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.")
示例#15
0
 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)
示例#16
0
 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())
示例#17
0
 def test_token_is_valid(self):
     api = APIManager("CLIENT_ID", "CLIENT_SECRET", self.server.path)
     self.assertTrue(api.is_access_token_valid())