def test_pagination(self, client):
        client.get.side_effect = (
            Response(200, '{"result": [{"a": 3, "b": "sys_id"}]}',
                     {"X-Total-Count": "2"}),
            Response(200, '{"result": [{"a": 2, "b": "sys_ie"}]}',
                     {"X-Total-Count": "2"}),
        )
        t = table.TableClient(client, batch_size=1)

        records = t.list_records("my_table")

        assert [dict(a=3, b="sys_id"), dict(a=2, b="sys_ie")] == records
        assert 2 == len(client.get.mock_calls)
        client.get.assert_any_call(
            "api/now/table/my_table",
            query=dict(sysparm_exclude_reference_link="true",
                       sysparm_limit=1,
                       sysparm_offset=0),
        )
        client.get.assert_any_call(
            "api/now/table/my_table",
            query=dict(sysparm_exclude_reference_link="true",
                       sysparm_limit=1,
                       sysparm_offset=1),
        )
    def test_normal_mode(self, client, tmp_path):
        client.request.side_effect = [
            Response(201, '{"result": {"a": 3, "b": "sys_id"}}'),
            Response(201, '{"result": {"a": 4, "b": "sys_idn"}}'),
        ]
        a = attachment.AttachmentClient(client)

        path1 = tmp_path / "name1.txt"
        path1.write_text(u"file_content1")
        path2 = tmp_path / "name2.txt"
        path2.write_text(u"file_content2")

        record = a.upload_records(
            "table",
            "1234",
            {
                "attachment_name.txt": {
                    "path": str(path1),
                    "type": "text/markdown",
                    "hash": "hash",
                },
                "another_file_name.txt": {
                    "path": str(path2),
                    "type": "text/plain",
                    "hash": "hash",
                },
            },
            check_mode=False,
        )

        assert [dict(a=3, b="sys_id"), dict(a=4, b="sys_idn")] == record
        assert 2 == client.request.call_count
        client.request.assert_any_call(
            "POST",
            "api/now/attachment/file",
            query={
                "table_name": "table",
                "table_sys_id": "1234",
                "file_name": "attachment_name.txt",
                "content_type": "text/markdown",
                "hash": "hash",
            },
            headers={"Accept": "application/json", "Content-type": "text/markdown"},
            bytes=b"file_content1",
        )
        client.request.assert_any_call(
            "POST",
            "api/now/attachment/file",
            query={
                "table_name": "table",
                "table_sys_id": "1234",
                "file_name": "another_file_name.txt",
                "content_type": "text/plain",
                "hash": "hash",
            },
            headers={"Accept": "application/json", "Content-type": "text/plain"},
            bytes=b"file_content2",
        )
    def test_changed_check_mode(self, client, tmp_path):
        client.request.side_effect = [
            Response(201, '{"result": {"a": 1, "sys_id": "a"}}'),
            Response(201, '{"result": {"a": 2, "sys_id": "b"}}'),
        ]
        client.delete.return_value = Response(204, "")
        a = attachment.AttachmentClient(client)

        path1 = tmp_path / "name1.txt"
        path1.write_text(u"file_content1")
        path2 = tmp_path / "name2.txt"
        path2.write_text(u"file_content2")

        record = a.update_records(
            "table",
            "1234",
            {
                "attachment_name.txt": {
                    "path": str(path1),
                    "type": "text/markdown",
                    "hash": "hash",
                },
                "another_file_name.txt": {
                    "path": str(path2),
                    "type": "text/plain",
                    "hash": "hash",
                },
            },
            [
                {"hash": "oldhash", "sys_id": "1", "file_name": "attachment_name.txt"},
                {
                    "hash": "oldhash",
                    "sys_id": "2",
                    "file_name": "another_file_name.txt",
                },
            ],
            True,
        )

        assert [
            {
                "table_name": "table",
                "table_sys_id": "1234",
                "content_type": "text/plain",
                "hash": "hash",
                "file_name": "another_file_name.txt",
            },
            {
                "table_name": "table",
                "table_sys_id": "1234",
                "content_type": "text/markdown",
                "hash": "hash",
                "file_name": "attachment_name.txt",
            },
        ] == sorted(record, key=lambda k: k["file_name"])
    def test_check_mode(self, client):
        client.get.return_value = Response(
            200,
            '{"result": [{"a": 3, "sys_id": "1234"}, {"a": 4, "sys_id": "4321"}]}',
            {"X-Total-Count": "2"},
        )
        client.delete.return_value = Response(204, "")
        a = attachment.AttachmentClient(client)

        a.delete_attached_records("table", 5555, True)
        client.delete.assert_not_called()
    def test_normal_mode(self, client):
        client.get.return_value = Response(
            200,
            '{"result": [{"a": 3, "sys_id": "1234"}, {"a": 4, "sys_id": "4321"}]}',
            {"X-Total-Count": "2"},
        )
        client.delete.return_value = Response(204, "")
        a = attachment.AttachmentClient(client)

        a.delete_attached_records("table", 5555, False)

        assert client.delete.call_count == 2
        client.delete.assert_any_call("api/now/attachment/1234")
        client.delete.assert_any_call("api/now/attachment/4321")
    def test_normal_mode(self, client, tmp_path):
        client.request.return_value = Response(
            201, '{"result": {"a": 3, "b": "sys_id"}}'
        )
        a = attachment.AttachmentClient(client)

        path = tmp_path / "name.txt"
        path.write_text(u"file_content")

        record = a.upload_record(
            "table",
            "1234",
            {
                "path": str(path),
                "name": "attachment_name.txt",
                "type": "text/markdown",
                "hash": "hash",
            },
            check_mode=False,
        )

        assert dict(a=3, b="sys_id") == record
        client.request.assert_called_with(
            "POST",
            "api/now/attachment/file",
            query={
                "table_name": "table",
                "table_sys_id": "1234",
                "file_name": "attachment_name.txt",
                "content_type": "text/markdown",
                "hash": "hash",
            },
            headers={"Accept": "application/json", "Content-type": "text/markdown"},
            bytes=b"file_content",
        )
    def test_check_mode(self, client, tmp_path):
        client.request.return_value = Response(
            201, '{"result": {"a": 3, "b": "sys_id"}}'
        )
        a = attachment.AttachmentClient(client)

        path = tmp_path / "name.txt"
        path.write_text(u"file_contents")

        record = a.upload_record(
            "table",
            "1234",
            {
                "path": str(path),
                "name": "attachment_name.txt",
                "type": "text/markdown",
                "hash": "hash",
            },
            check_mode=True,
        )

        assert {
            "table_name": "table",
            "table_sys_id": "1234",
            "file_name": "attachment_name.txt",
            "content_type": "text/markdown",
            "hash": "hash",
        } == record
        client.request.assert_not_called()
    def test_check_mode(self, client):
        client.delete.return_value = Response(204, "")
        t = table.TableClient(client)

        t.delete_record("my_table", dict(sys_id="id"), True)

        client.delete.assert_not_called()
    def test_normal_mode(self, client):
        client.delete.return_value = Response(204, "")
        t = table.TableClient(client)

        t.delete_record("my_table", dict(sys_id="id"), False)

        client.delete.assert_called_with("api/now/table/my_table/id")
    def test_zero_matches_fail(self, client):
        client.get.return_value = Response(200, '{"result": []}',
                                           {"X-Total-Count": "0"})
        t = table.TableClient(client)

        with pytest.raises(errors.ServiceNowError, match="No"):
            t.get_record("my_table", dict(our="query"), must_exist=True)
Beispiel #11
0
    def test_multiple_matches(self, client):
        client.get.return_value = Response(200,
                                           '{"result": [{"a": 3}, {"b": 4}]}')
        t = table.TableClient(client)

        with pytest.raises(errors.ServiceNowError, match="2"):
            t.get_record("my_table", dict(our="query"))
Beispiel #12
0
    def test_non_empty_response(self, client):
        client.get.return_value = Response(
            200, '{"result": [{"a": 3, "b": "sys_id"}]}')
        t = table.TableClient(client)

        records = t.list_records("my_table")

        assert [dict(a=3, b="sys_id")] == records
    def test_check_mode(self, client):
        client.post.return_value = Response(
            201, '{"result": {"a": 3, "b": "sys_id"}}')
        t = table.TableClient(client)

        record = t.create_record("my_table", dict(a=4), True)

        assert dict(a=4) == record
        client.post.assert_not_called()
Beispiel #14
0
    def test_query_passing(self, client):
        client.get.return_value = Response(200, '{"result": []}')
        t = table.TableClient(client)

        t.list_records("my_table", dict(a="b"))

        client.get.assert_called_with(
            "table/my_table",
            query=dict(sysparm_exclude_reference_link="true", a="b"))
    def test_non_empty_response_meta(self, client):
        client.get.return_value = Response(
            200, '{"result": [{"a": 3, "b": "sys_id"}]}', {"X-Total-Count": "1"}
        )
        a = attachment.AttachmentClient(client)

        records = a.list_records()

        assert [dict(a=3, b="sys_id")] == records
Beispiel #16
0
    def test_empty_response(self, client):
        client.get.return_value = Response(200, '{"result": []}')
        t = table.TableClient(client)

        records = t.list_records("my_table")

        assert [] == records
        client.get.assert_called_with(
            "table/my_table",
            query=dict(sysparm_exclude_reference_link="true"))
Beispiel #17
0
    def test_single_match(self, client):
        client.get.return_value = Response(
            200, '{"result": [{"a": 3, "b": "sys_id"}]}')
        t = table.TableClient(client)

        record = t.get_record("my_table", dict(our="query"))

        assert dict(a=3, b="sys_id") == record
        client.get.assert_called_with(
            "table/my_table",
            query=dict(sysparm_exclude_reference_link="true", our="query"),
        )
    def test_normal_mode(self, client):
        client.post.return_value = Response(
            201, '{"result": {"a": 3, "b": "sys_id"}}')
        t = table.TableClient(client)

        record = t.create_record("my_table", dict(a=4), False)

        assert dict(a=3, b="sys_id") == record
        client.post.assert_called_with(
            "api/now/table/my_table",
            dict(a=4),
            query=dict(sysparm_exclude_reference_link="true"),
        )
    def test_normal_mode_missing(self, client):
        client.get.return_value = Response(
            200,
            '{"result": [{"a": 3, "sys_id": "1234"}, {"a": 4, "sys_id": "4321"}]}',
            {"X-Total-Count": "2"},
        )
        client.delete.side_effect = errors.UnexpectedAPIResponse(
            404, "Record not found"
        )
        a = attachment.AttachmentClient(client)

        with pytest.raises(errors.UnexpectedAPIResponse, match="not found"):
            a.delete_attached_records("table", 5555, False)
    def test_pagination_meta(self, client):
        client.get.side_effect = (
            Response(
                200, '{"result": [{"a": 3, "b": "sys_id"}]}', {"X-Total-Count": "2"}
            ),
            Response(
                200, '{"result": [{"a": 2, "b": "sys_ie"}]}', {"X-Total-Count": "2"}
            ),
        )
        a = attachment.AttachmentClient(client, batch_size=1)

        records = a.list_records()

        assert [dict(a=3, b="sys_id"), dict(a=2, b="sys_ie")] == records
        assert 2 == len(client.get.mock_calls)
        client.get.assert_any_call(
            "api/now/attachment",
            query=dict(sysparm_limit=1, sysparm_offset=0),
        )
        client.get.assert_any_call(
            "api/now/attachment",
            query=dict(sysparm_limit=1, sysparm_offset=1),
        )
    def test_check_mode(self, client):
        client.request.return_value = Response(
            201, '{"result": {"a": 3, "b": "sys_id"}}'
        )
        a = attachment.AttachmentClient(client)

        record = a.create_record(
            dict(some="property"),
            "file_content",
            "text/plain",
            True,
        )

        assert dict(some="property") == record
        client.request.assert_not_called()
    def test_query_passing(self, client):
        client.get.return_value = Response(200, '{"result": []}',
                                           {"X-Total-Count": "0"})
        t = table.TableClient(client)

        t.list_records("my_table", dict(a="b"))

        client.get.assert_called_once_with(
            "api/now/table/my_table",
            query=dict(
                sysparm_exclude_reference_link="true",
                a="b",
                sysparm_limit=1000,
                sysparm_offset=0,
            ),
        )
    def test_query_passing_meta(self, client):
        client.get.return_value = Response(
            200, '{"result": []}', {"X-Total-Count": "0"}
        )
        a = attachment.AttachmentClient(client)

        a.list_records(dict(a="b"))

        client.get.assert_called_once_with(
            "api/now/attachment",
            query=dict(
                a="b",
                sysparm_limit=10000,
                sysparm_offset=0,
            ),
        )
    def test_empty_response_meta(self, client):
        client.get.return_value = Response(
            200, '{"result": []}', {"X-Total-Count": "0"}
        )
        a = attachment.AttachmentClient(client)

        records = a.list_records()

        assert [] == records
        client.get.assert_called_once_with(
            "api/now/attachment",
            query=dict(
                sysparm_limit=10000,
                sysparm_offset=0,
            ),
        )
    def test_check_mode(self, client, tmp_path):
        client.request.return_value = Response(
            201, '{"result": {"a": 1, "sys_id": "1"}}'
        )
        a = attachment.AttachmentClient(client)

        path1 = tmp_path / "name1.txt"
        path1.write_text(u"file_content1")
        path2 = tmp_path / "name2.txt"
        path2.write_text(u"file_content2")

        record = a.upload_records(
            "table",
            "1234",
            {
                "attachment_name.txt": {
                    "path": str(path1),
                    "type": "text/markdown",
                    "hash": "hash",
                },
                "another_file_name.txt": {
                    "path": str(path2),
                    "type": "text/plain",
                    "hash": "hash",
                },
            },
            check_mode=True,
        )

        assert [
            {
                "table_name": "table",
                "table_sys_id": "1234",
                "file_name": "another_file_name.txt",
                "content_type": "text/plain",
                "hash": "hash",
            },
            {
                "table_name": "table",
                "table_sys_id": "1234",
                "file_name": "attachment_name.txt",
                "content_type": "text/markdown",
                "hash": "hash",
            },
        ] == sorted(record, key=lambda k: k["file_name"])
        client.request.assert_not_called()
    def test_single_match(self, client):
        client.get.return_value = Response(
            200, '{"result": [{"a": 3, "b": "sys_id"}]}',
            {"X-Total-Count": "1"})
        t = table.TableClient(client)

        record = t.get_record("my_table", dict(our="query"))

        assert dict(a=3, b="sys_id") == record
        client.get.assert_called_with(
            "api/now/table/my_table",
            query=dict(
                sysparm_exclude_reference_link="true",
                our="query",
                sysparm_limit=1000,
                sysparm_offset=0,
            ),
        )
    def test_normal_mode(self, client):
        client.request.return_value = Response(
            201, '{"result": {"a": 3, "b": "sys_id"}}'
        )
        a = attachment.AttachmentClient(client)

        record = a.create_record(
            dict(some="property"),
            "file_content",
            "text/plain",
            False,
        )

        assert dict(a=3, b="sys_id") == record
        client.request.assert_called_with(
            "POST",
            "api/now/attachment/file",
            query={"some": "property"},
            headers={"Accept": "application/json", "Content-type": "text/plain"},
            bytes="file_content",
        )
    def test_zero_matches(self, client):
        client.get.return_value = Response(200, '{"result": []}',
                                           {"X-Total-Count": "0"})
        t = table.TableClient(client)

        assert t.get_record("my_table", dict(our="query")) is None
    def test_check_mode(self, client):
        client.delete.return_value = Response(204, "")
        a = attachment.AttachmentClient(client)
        a.delete_record(dict(sys_id="id"), True)

        client.delete.assert_not_called()
    def test_normal_mode(self, client):
        client.delete.return_value = Response(204, "")
        a = attachment.AttachmentClient(client)
        a.delete_record(dict(sys_id="1234"), False)

        client.delete.assert_called_with("api/now/attachment/1234")