class TestClientAuthorize(unittest.TestCase): def setUp(self): with patch('batfish.client.read_token_from_conf', return_value=None): self.cli = Client() @responses.activate def test_authorize_error(self): url = "https://api.digitalocean.com/v2/actions" responses.add(responses.GET, url, body='{"error": "something"}', status=500, content_type="application/json") with self.assertRaises(HTTPError): self.cli.authorize("test_token") @responses.activate def test_authorize_unauthorized(self): url = "https://api.digitalocean.com/v2/kura" body = {'id': "unauthorized", 'message': "Unable to authenticate you."} responses.add(responses.GET, url, body=json.dumps(body), status=401, content_type="application/json") self.cli.authorize("test_token") self.assertEquals(responses.calls[0].response.status_code, 401) @responses.activate def test_authorize_unauthorized(self): url = "https://api.digitalocean.com/v2/actions" responses.add(responses.GET, url, body='{"error": "something"}', status=200, content_type="application/json") auth = self.cli.authorize("test_token") self.assertEquals(auth, "OK") self.assertEquals(responses.calls[0].response.status_code, 200)
class TestClientPost(unittest.TestCase): def setUp(self): with patch('batfish.client.read_token_from_conf', return_value="test_token"): self.cli = Client() self.base_payload = {} @responses.activate def test_post_errors(self): url = "https://api.digitalocean.com/v2/kura" responses.add(responses.POST, url, body='{"error": "something"}', status=500, content_type="application/json") with self.assertRaises(HTTPError): self.cli.post('kura', self.base_payload) @responses.activate def test_post_json(self): url = "https://api.digitalocean.com/v2/kura" responses.add(responses.POST, url, body='{"message": "something"}', status=200, content_type="application/json") j = self.cli.post('kura', self.base_payload) self.assertEquals(j['message'], "something") @responses.activate def test_post_headers(self): url = "https://api.digitalocean.com/v2/kura" responses.add(responses.POST, url, body='{"message": "something"}', status=200, content_type="application/json") self.cli.post('kura', self.base_payload) h = {'Accept': "*/*", 'Accept-Encoding': "gzip, deflate", 'Authorization': "Bearer test_token", 'Connection': "keep-alive", 'User-Agent': "Batfish ({0})".format(__version__), 'Content-Length': '2', 'Content-Type': "application/json"} th = collections.OrderedDict(sorted(h.items())) rh = collections.OrderedDict(sorted(responses.calls[0].request.headers.items())) self.assertEquals(rh, th)
class TestClientDelete(unittest.TestCase): def setUp(self): with patch('batfish.client.read_token_from_conf', return_value="test_token"): self.cli = Client() @responses.activate def test_delete_errors(self): url = "https://api.digitalocean.com/v2/kura" responses.add(responses.DELETE, url, body='{"error": "something"}', status=500, content_type="application/json") with self.assertRaises(HTTPError): self.cli.delete('kura') @responses.activate def test_delete_no_error(self): url = "https://api.digitalocean.com/v2/kura" responses.add(responses.DELETE, url, body='{"message": "something"}', status=204, content_type="application/json") self.cli.delete('kura') self.assertEquals(responses.calls[0].response.status_code, 204) @responses.activate def test_delete_headers(self): url = "https://api.digitalocean.com/v2/kura" responses.add(responses.DELETE, url, body='{"message": "something"}', status=204, content_type="application/json") self.cli.delete('kura') h = {'Accept': "*/*", 'Accept-Encoding': "gzip, deflate", 'Authorization': "Bearer test_token", 'Connection': "keep-alive", 'User-Agent': "Batfish ({0})".format(__version__), 'Content-Length': '0'} th = collections.OrderedDict(sorted(h.items())) rh = collections.OrderedDict(sorted(responses.calls[0].request.headers.items())) self.assertEquals(rh, th)
def setUp(self): with patch('batfish.client.read_token_from_conf', return_value="test_token"): self.cli = Client()
class TestClientGet(unittest.TestCase): def setUp(self): with patch('batfish.client.read_token_from_conf', return_value="test_token"): self.cli = Client() @responses.activate def test_get_errors(self): url = "https://api.digitalocean.com/v2/kura" responses.add(responses.GET, url, body='{"error": "something"}', status=500, content_type="application/json") with self.assertRaises(HTTPError): self.cli.get('kura') @responses.activate def test_get_json(self): url = "https://api.digitalocean.com/v2/kura" responses.add(responses.GET, url, body='{"message": "something"}', status=200, content_type="application/json") j = self.cli.get('kura') self.assertEquals(j['message'], "something") @responses.activate def test_get_default_headers(self): url = "https://api.digitalocean.com/v2/kura" responses.add(responses.GET, url, body='{"message": "something"}', status=200, content_type="application/json") self.cli.get('kura') h = {'Accept': "*/*", 'Accept-Encoding': "gzip, deflate", 'Authorization': "Bearer test_token", 'Connection': "keep-alive", 'User-Agent': "Batfish ({0})".format(__version__)} th = collections.OrderedDict(sorted(h.items())) rh = collections.OrderedDict(sorted(responses.calls[0].request.headers.items())) self.assertEquals(rh, th) @responses.activate def test_get_headers_modified_ua(self): url = "https://api.digitalocean.com/v2/kura" responses.add(responses.GET, url, body='{"message": "something"}', status=200, content_type="application/json") self.cli.ua = "Kura" self.cli.get('kura') h = {'Accept': "*/*", 'Accept-Encoding': "gzip, deflate", 'Authorization': "Bearer test_token", 'Connection': "keep-alive", 'User-Agent': "Kura"} th = collections.OrderedDict(sorted(h.items())) rh = collections.OrderedDict(sorted(responses.calls[0].request.headers.items())) self.assertEquals(rh, th) @responses.activate def test_get_headers_custom(self): url = "https://api.digitalocean.com/v2/kura" responses.add(responses.GET, url, body='{"message": "something"}', status=200, content_type="application/json") h = {'Authorization': "Bearer test_token2", 'X-Random-Header': 'test'} self.cli.get('kura', headers=h) h = {'Accept': "*/*", 'Accept-Encoding': "gzip, deflate", 'Authorization': "Bearer test_token2", 'X-Random-Header': "test", 'User-Agent': 'Batfish ({0})'.format(__version__), 'Connection': "keep-alive",} self.assertEquals(responses.calls[0].request.headers, h)