def test_error(self, mocker): c = client.Client("https://instance.com", "user", "pass") request_mock = mocker.patch.object(c, "request") request_mock.return_value = client.Response(400, "bad request") with pytest.raises(errors.UnexpectedAPIResponse, match="bad request"): c.put("api/now/table/incident/1", {"some": "data"})
def test_request_without_data_binary_success(self, mocker): c = client.Client("https://instance.com", "user", "pass") mock_response = client.Response(200, "data", headers=[("Content-type", "image/apng") ]) request_mock = mocker.patch.object(c, "_request") request_mock.return_value = mock_response resp = c.request( "GET", "api/now/some/path", headers={ "Accept": "image/apng", "Content-type": "text/plain" }, ) request_mock.assert_called_once_with( "GET", "https://instance.com/api/now/some/path", data=None, headers=dict({ "Accept": "image/apng", "Content-type": "text/plain" }, **c.auth_header), ) assert resp == mock_response
def test_error(self, mocker): c = client.Client("https://instance.com", "user", "pass") request_mock = mocker.patch.object(c, "request") request_mock.return_value = client.Response(404, "not found") with pytest.raises(errors.UnexpectedAPIResponse, match="not found"): c.delete("api/now/table/resource/1")
def test_error(self, mocker): c = client.Client("instance.com", "user", "pass") request_mock = mocker.patch.object(c, "request") request_mock.return_value = client.Response(403, "forbidden") with pytest.raises(errors.UnexpectedAPIResponse, match="forbidden"): c.get("table/incident/1")
def test_request_with_data_binary_success(self, mocker): c = client.Client("https://instance.com", "user", "pass") mock_response = client.Response(200, "some_data", headers=[("Content-type", "text/plain") ]) request_mock = mocker.patch.object(c, "_request") request_mock.return_value = mock_response resp = c.request( "PUT", "api/now/some/path", headers={ "Accept": "text/plain", "Content-type": "text/plain" }, bytes="some_data", ) request_mock.assert_called_once_with( "PUT", "https://instance.com/api/now/some/path", data="some_data", headers={ "Accept": "text/plain", "Content-type": "text/plain", "Authorization": c.auth_header["Authorization"], }, ) assert resp == mock_response
def test_url_error(self, mocker): request_mock = mocker.patch.object(client, "Request").return_value request_mock.open.side_effect = URLError("some error") c = client.Client("https://instance.com", "user", "pass") with pytest.raises(errors.ServiceNowError, match="some error"): c.request("GET", "api/now/some/path")
def test_auth_error(self, mocker): request_mock = mocker.patch.object(client, "Request").return_value request_mock.open.side_effect = HTTPError("", 401, "Unauthorized", {}, None) c = client.Client("https://instance.com", "user", "pass") with pytest.raises(errors.AuthError): c.request("GET", "api/now/some/path")
def test_ok_missing(self, mocker): c = client.Client("https://instance.com", "user", "pass") mock_response = client.Response(404, "Not Found", None) request_mock = mocker.patch.object(c, "request") request_mock.return_value = mock_response resp = c.get("api/now/table/incident/1") assert resp == mock_response
def test_query(self, mocker): c = client.Client("https://instance.com", "user", "pass") request_mock = mocker.patch.object(c, "request") request_mock.return_value = client.Response(204, {}) c.delete("api/now/table/resource/1", query=dict(x="y")) request_mock.assert_called_with("DELETE", "api/now/table/resource/1", query=dict(x="y"))
def test_ok(self, mocker): c = client.Client("https://instance.com", "user", "pass") mock_response = client.Response(200, '{"incident": 1}') request_mock = mocker.patch.object(c, "request") request_mock.return_value = mock_response resp = c.put("api/now/table/incident/1", {"some": "data"}) assert resp == mock_response assert resp.json == {"incident": 1}
def test_ok(self, mocker): c = client.Client("instance.com", "user", "pass") mock_response = client.Response(200, '{"incident": 1}', None) request_mock = mocker.patch.object(c, "request") request_mock.return_value = mock_response resp = c.get("table/incident/1") assert resp == mock_response assert resp.json == {"incident": 1}
def test_path_escaping(self, mocker): request_mock = mocker.patch.object(client, "Request").return_value raw_request = mocker.MagicMock(status=200) raw_request.read.return_value = "{}" c = client.Client("instance.com", "user", "pass") c.request("GET", "some path") request_mock.open.assert_called_once() path_arg = request_mock.open.call_args.args[1] assert path_arg == "instance.com/api/now/some%20path"
def test_path_without_query(self, mocker, query): request_mock = mocker.patch.object(client, "Request").return_value raw_request = mocker.MagicMock(status=200) raw_request.read.return_value = "{}" c = client.Client("https://instance.com", "user", "pass") c.request("GET", "api/now/some/path", query=query) request_mock.open.assert_called_once() path_arg = request_mock.open.call_args.args[1] assert path_arg == "https://instance.com/api/now/some/path"
def test_query(self, mocker): c = client.Client("instance.com", "user", "pass") request_mock = mocker.patch.object(c, "request") request_mock.return_value = client.Response(200, '{"incident": 1}') c.patch("table/incident/1", {"some": "data"}, query={"g": "f"}) request_mock.assert_called_with("PATCH", "table/incident/1", data=dict(some="data"), query=dict(g="f"))
def test_http_error(self, mocker): request_mock = mocker.patch.object(client, "Request").return_value request_mock.open.side_effect = HTTPError( "", 404, "Not Found", {}, io.StringIO(to_text("My Error"))) c = client.Client("https://instance.com", "user", "pass") resp = c.request("GET", "api/now/some/path") assert resp.status == 404 assert resp.data == "My Error" assert resp.headers == {}
def test_query(self, mocker): c = client.Client("https://instance.com", "user", "pass") request_mock = mocker.patch.object(c, "request") request_mock.return_value = client.Response(200, '{"incident": 1}') c.put("api/now/table/incident/1", {"some": "data"}, query={"j": "i"}) request_mock.assert_called_with("PUT", "api/now/table/incident/1", data=dict(some="data"), query=dict(j="i"))
def test_query(self, mocker): c = client.Client("https://instance.com", "user", "pass") request_mock = mocker.patch.object(c, "request") request_mock.return_value = client.Response(200, '{"incident": 1}', None) c.get("api/now/table/incident/1", query=dict(a="1")) request_mock.assert_called_with("GET", "api/now/table/incident/1", query=dict(a="1"))
def test_path_with_query(self, mocker, query): request_mock = mocker.patch.object(client, "Request").return_value raw_request = mocker.MagicMock(status=200) raw_request.read.return_value = "{}" c = client.Client("https://instance.com", "user", "pass") c.request("GET", "api/now/some/path", query=query) request_mock.open.assert_called_once() path_arg = request_mock.open.call_args.args[1] parsed_query = parse_qs(urlparse(path_arg).query) assert parsed_query == dict((k, [str(v)]) for k, v in query.items())
def test_oauth_failure(self, mocker): request_mock = mocker.patch.object(client, "Request").return_value request_mock.open.side_effect = HTTPError( "", 403, "Forbidden", {}, io.StringIO(to_text("Error message"))) c = client.Client("instance.com", "user", "pass", client_id="id", client_secret="secret") with pytest.raises(errors.UnexpectedAPIResponse, match="Error message"): c.auth_header
def test_oauth(self, mocker): resp_mock = mocker.MagicMock() resp_mock.status = 200 # Used when testing on Python 3 resp_mock.getcode.return_value = 200 # Used when testing on Python 2 resp_mock.read.return_value = '{"access_token": "token"}' request_mock = mocker.patch.object(client, "Request").return_value request_mock.open.return_value = resp_mock c = client.Client("instance.com", "user", "pass", client_id="id", client_secret="secret") assert c.auth_header == {"Authorization": "Bearer token"}
def test_request_without_data_success(self, mocker): c = client.Client("https://instance.com", "user", "pass") mock_response = client.Response(200, '{"returned": "data"}', headers=[("Content-type", "application/json")]) request_mock = mocker.patch.object(c, "_request") request_mock.return_value = mock_response resp = c.request("GET", "api/now/some/path") request_mock.assert_called_once_with( "GET", "https://instance.com/api/now/some/path", data=None, headers=dict(Accept="application/json", **c.auth_header), ) assert resp == mock_response
def test_header_is_cached(self, mocker): raw_resp_mock = mocker.MagicMock() raw_resp_mock.status = 200 # Used when testing on Python 3 raw_resp_mock.getcode.return_value = 200 # Used when testing on Python 2 raw_resp_mock.read.return_value = '{"access_token": "token"}' request_mock = mocker.patch.object(client, "Request").return_value request_mock.open.return_value = raw_resp_mock c = client.Client("instance.com", "user", "pass", client_id="id", client_secret="secret") c.auth_header c.auth_header assert request_mock.open.call_count == 1
def test_request_with_data_success(self, mocker): c = client.Client("https://instance.com", "user", "pass") mock_response = client.Response(200, '{"returned": "data"}', headers=[("Content-type", "application/json")]) request_mock = mocker.patch.object(c, "_request") request_mock.return_value = mock_response resp = c.request("PUT", "api/now/some/path", data={"some": "data"}) request_mock.assert_called_once_with( "PUT", "https://instance.com/api/now/some/path", data='{"some":"data"}', headers={ "Accept": "application/json", "Content-type": "application/json", "Authorization": c.auth_header["Authorization"], }, ) assert resp == mock_response
def test_basic_auth(self): c = client.Client("https://instance.com", "user", "pass") assert c.auth_header == {"Authorization": b"Basic dXNlcjpwYXNz"}
def test_valid_host(self, host): client.Client(host, "user", "pass")
def test_invalid_host(self, host): with pytest.raises(errors.ServiceNowError, match="Invalid instance host value"): client.Client(host, "user", "pass")
def test_ok(self, mocker): c = client.Client("https://instance.com", "user", "pass") request_mock = mocker.patch.object(c, "request") request_mock.return_value = client.Response(204, {}) c.delete("api/now/table/resource/1")