コード例 #1
0
    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"})
コード例 #2
0
    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
コード例 #3
0
    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")
コード例 #4
0
    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")
コード例 #5
0
    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
コード例 #6
0
    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")
コード例 #7
0
    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")
コード例 #8
0
    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
コード例 #9
0
    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"))
コード例 #10
0
    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}
コード例 #11
0
    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}
コード例 #12
0
    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"
コード例 #13
0
    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"
コード例 #14
0
    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"))
コード例 #15
0
    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 == {}
コード例 #16
0
    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"))
コード例 #17
0
    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"))
コード例 #18
0
    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())
コード例 #19
0
    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
コード例 #20
0
    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"}
コード例 #21
0
    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
コード例 #22
0
    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
コード例 #23
0
    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
コード例 #24
0
 def test_basic_auth(self):
     c = client.Client("https://instance.com", "user", "pass")
     assert c.auth_header == {"Authorization": b"Basic dXNlcjpwYXNz"}
コード例 #25
0
 def test_valid_host(self, host):
     client.Client(host, "user", "pass")
コード例 #26
0
 def test_invalid_host(self, host):
     with pytest.raises(errors.ServiceNowError,
                        match="Invalid instance host value"):
         client.Client(host, "user", "pass")
コード例 #27
0
    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")