def test_json_error_response(self): url = "http://%s/" % factory.make_string() api = SeaMicroAPIV09(url) data = {"error": {"code": 401}} response = FakeResponse(200, data, is_json=True) self.assertRaises(SeaMicroAPIV09Error, api.parse_response, url, response)
def test_invalid_reponse_code(self): url = "http://%s/" % factory.make_string() api = SeaMicroAPIV09(url) response = FakeResponse(401, "Unauthorized") self.assertRaises( SeaMicroAPIV09Error, api.parse_response, url, response )
def test_invalid_json_response(self): url = "http://%s/" % factory.make_string() api = SeaMicroAPIV09(url) response = FakeResponse(200, factory.make_string()) self.assertRaises( SeaMicroAPIV09Error, api.parse_response, url, response )
def configure_put_server_power(self, token=None): result = {"serverId": {0: "0/0"}} self.configure_get_result(result) mock = self.patch(SeaMicroAPIV09, "put") url = "http://%s/" % factory.make_string() api = SeaMicroAPIV09(url) api.token = token return mock, api
def test_json_valid_response(self): url = "http://%s/" % factory.make_string() api = SeaMicroAPIV09(url) output = factory.make_string() data = {"error": {"code": 200}, "result": {"data": output}} response = FakeResponse(200, data, is_json=True) result = api.parse_response(url, response) self.assertEqual(output, result["result"]["data"])
def test_get_server_index(self): result = {"serverId": {0: "0/0", 1: "1/0", 2: "2/0"}} self.configure_get_result(result) url = "http://%s/" % factory.make_string() api = SeaMicroAPIV09(url) self.assertEqual(0, api.server_index("0/0")) self.assertEqual(1, api.server_index("1/0")) self.assertEqual(2, api.server_index("2/0")) self.assertIsNone(api.server_index("3/0"))
def test_login_and_logout(self): token = factory.make_string() self.configure_get_result(token) url = "http://%s/" % factory.make_string() api = SeaMicroAPIV09(url) api.login("username", "password") self.assertEqual(token, api.token) api.logout() self.assertIsNone(api.token)
def test_build_url(self): url = factory.make_string() api = SeaMicroAPIV09("http://%s/" % url) location = factory.make_string() params = [factory.make_string() for _ in range(3)] output = api.build_url(location, params) parsed = urllib.parse.urlparse(output) self.assertEqual(url, parsed.netloc) self.assertEqual(location, parsed.path.split("/")[1]) self.assertEqual(params, parsed.query.split("&"))
def configure_put_server_power(self, token=None): result = { 'serverId': { 0: '0/0', } } self.configure_get_result(result) mock = self.patch(SeaMicroAPIV09, 'put') url = 'http://%s/' % factory.make_string() api = SeaMicroAPIV09(url) api.token = token return mock, api
def test_get_server_index(self): result = { 'serverId': { 0: '0/0', 1: '1/0', 2: '2/0', } } self.configure_get_result(result) url = 'http://%s/' % factory.make_string() api = SeaMicroAPIV09(url) self.assertEqual(0, api.server_index('0/0')) self.assertEqual(1, api.server_index('1/0')) self.assertEqual(2, api.server_index('2/0')) self.assertIsNone(api.server_index('3/0'))
def test_json_valid_response(self): url = 'http://%s/' % factory.make_string() api = SeaMicroAPIV09(url) output = factory.make_string() data = { 'error': { 'code': 200 }, 'result': { 'data': output }, } response = FakeResponse(200, data, is_json=True) result = api.parse_response(url, response) self.assertEqual(output, result['result']['data'])