Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Resource.get() parameters persist for all requests #195

Open
dysfungi opened this issue Jul 11, 2023 · 0 comments
Open

Resource.get() parameters persist for all requests #195

dysfungi opened this issue Jul 11, 2023 · 0 comments

Comments

@dysfungi
Copy link

dysfungi commented Jul 11, 2023

Ran into an issue where I noticed URLs including query parameters from previous requests. Tracked it down to a shallow copy of parameters which gets persisted on the resource.

I reproduced with the following test file:

import urllib.parse

import pysnow
import pytest
import requests_mock


@pytest.fixture
def requests_mocker():
    with requests_mock.Mocker(real_http=True) as m:
        yield m


def test_resource_get_parameters_persist(requests_mocker):
    """Identify and track a bug where
    ``pysnow.Resource.parameters.query`` is persisted on the resource
    instance.  A shallow copy of ``pysnow.Resource.parameters`` (see
    [1]) is passed to ``pysnow.request.SnowRequest`` and any parameters
    passed to ``pysnow.Resource.get`` (i.e.,
    ``pysnow.request.SnowRequest.get``; see [2]) will be persisted due
    to ``pysnow.request.SnowRequest.parameters`` being a shallow copy
    of ``pysnow.Resource.parameters``.

    Also see
    ``tests/unit/limpbot/servicenow/test_client_resource.py:test_resource_get_parameters_do_not_persist``.

    [1]: https://github.com/rbw/pysnow/blob/caad5bf/pysnow/resource.py#L82
    [2]: https://github.com/rbw/pysnow/blob/caad5bf/pysnow/request.py#L99
    """
    # GIVEN a ``pysnow.Resource`` instantiation
    instance = "eggspam"
    dino = {"sys_id": "1234", "name": "Dino", "breed": "ACD"}
    requests_mocker.get(
        f"https://{instance}.service-now.com/api/now/table/u_dog",
        json={"result": [dino]},
    )
    client = pysnow.Client(instance=instance, user="foo", password="bar")
    resource = client.resource(api_path="/table/u_dog")

    # WHEN passing a ``query`` paraemeter to ``pysnow.Resource.get``
    query = {"name": "Dino"}
    response = resource.get(query=query)
    assert response._response.status_code == 200
    assert response.one() == dino

    # THEN ``pysnow.Resource.parameters.query`` is persisted
    querystring = urllib.parse.urlencode(query)
    assert resource.parameters.query == querystring
    assert resource.parameters.query != ""
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant