Skip to content

Commit

Permalink
Add methods related to Remote Settings plugin (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem authored Nov 22, 2024
1 parent 24853fc commit 07dfd16
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/kinto_http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import mimetypes
import os
import random
import uuid
from collections import OrderedDict
from contextlib import contextmanager
Expand Down Expand Up @@ -940,6 +941,61 @@ def remove_attachment(self, id, bucket=None, collection=None):
resp, _ = self.session.request("delete", endpoint)
return resp

def get_changeset(self, bucket=None, collection=None, bust_cache=False, **kwargs):
kwargs.setdefault(
"_expected", random.randint(999999000000, 999999999999) if bust_cache else 0
)
endpoint = self._get_endpoint("changeset", bucket=bucket, collection=collection)
resp, _ = self.session.request("get", endpoint, params=kwargs)
return resp

def request_review(self, message, id=None, bucket=None, **kwargs):
return self.patch_collection(
id=id,
bucket=bucket,
data={
**kwargs.pop("data", {}),
"status": "to-review",
"last_editor_comment": message,
},
**kwargs,
)

def decline_changes(self, message, id=None, bucket=None, **kwargs):
return self.patch_collection(
id=id,
bucket=bucket,
data={
**kwargs.pop("data", {}),
"status": "work-in-progress",
"last_reviewer_comment": message,
},
**kwargs,
)

def approve_changes(self, id=None, bucket=None, **kwargs):
return self.patch_collection(
id=id,
bucket=bucket,
data={
**kwargs.pop("data", {}),
"status": "to-sign",
},
**kwargs,
)

def rollback_changes(self, message, id=None, bucket=None, **kwargs):
return self.patch_collection(
id=id,
bucket=bucket,
data={
**kwargs.pop("data", {}),
"status": "to-rollback",
"last_editor_comment": message,
},
**kwargs,
)

def __repr__(self) -> str:
if self.collection_name:
endpoint = self._get_endpoint(
Expand Down
1 change: 1 addition & 0 deletions src/kinto_http/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Endpoints(object):
"records": "{root}/buckets/{bucket}/collections/{collection}/records", # NOQA
"record": "{root}/buckets/{bucket}/collections/{collection}/records/{id}", # NOQA
"attachment": "{root}/buckets/{bucket}/collections/{collection}/records/{id}/attachment", # NOQA
"changeset": "{root}/buckets/{bucket}/collections/{collection}/changeset", # NOQA
}

def __init__(self, root=""):
Expand Down
102 changes: 102 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1462,3 +1462,105 @@ def test_get_permissions(client_setup: Client):
"_sort": "id",
},
)


def test_get_changeset_default(client_setup: Client):
client = client_setup
client.collection_name = "foo"
mock_response(client.session)

client.get_changeset()
client.session.request.assert_called_with(
"get", "/buckets/mybucket/collections/foo/changeset", params={"_expected": 0}
)


def test_get_changeset_bust(client_setup: Client, mocker: MockerFixture):
client = client_setup
mock_response(client.session)
mocked_random = mocker.patch("kinto_http.client.random")
mocked_random.randint.return_value = 42

client.get_changeset(collection="bar", bust_cache=True)
client.session.request.assert_called_with(
"get", "/buckets/mybucket/collections/bar/changeset", params={"_expected": 42}
)


def test_get_changeset_params(client_setup: Client, mocker: MockerFixture):
client = client_setup
mock_response(client.session)

client.get_changeset(bucket="foo", collection="bar", _since='"42"')
client.session.request.assert_called_with(
"get", "/buckets/foo/collections/bar/changeset", params={"_expected": 0, "_since": '"42"'}
)


def test_request_review(client_setup: Client, mocker: MockerFixture):
client = client_setup.clone(collection="cid")
mock_response(client.session)

client.request_review("r?")
client.session.request.assert_called_with(
"patch",
"/buckets/mybucket/collections/cid",
headers={"Content-Type": "application/json"},
payload={"data": {"last_editor_comment": "r?", "status": "to-review"}},
)


def test_request_review_advanced(client_setup: Client, mocker: MockerFixture):
client = client_setup
mock_response(client.session)

client.request_review("r?", id="cid", data={"field": "foo"}, if_match='"42"')
client.session.request.assert_called_with(
"patch",
"/buckets/mybucket/collections/cid",
headers={"Content-Type": "application/json", "If-Match": '"42"'},
payload={"data": {"field": "foo", "last_editor_comment": "r?", "status": "to-review"}},
)


def test_approve_changes(client_setup: Client, mocker: MockerFixture):
client = client_setup
mock_response(client.session)

client.approve_changes(id="cid", data={"field": "foo"}, if_match='"42"')
client.session.request.assert_called_with(
"patch",
"/buckets/mybucket/collections/cid",
headers={"Content-Type": "application/json", "If-Match": '"42"'},
payload={"data": {"field": "foo", "status": "to-sign"}},
)


def test_decline_changes(client_setup: Client, mocker: MockerFixture):
client = client_setup
mock_response(client.session)

client.decline_changes(message="r-", id="cid", data={"field": "foo"}, if_match='"42"')
client.session.request.assert_called_with(
"patch",
"/buckets/mybucket/collections/cid",
headers={"Content-Type": "application/json", "If-Match": '"42"'},
payload={
"data": {"field": "foo", "last_reviewer_comment": "r-", "status": "work-in-progress"}
},
)


def test_rollback_changes(client_setup: Client, mocker: MockerFixture):
client = client_setup
mock_response(client.session)

client.rollback_changes(message="cancel", id="cid", data={"field": "foo"}, if_match='"42"')
client.session.request.assert_called_with(
"patch",
"/buckets/mybucket/collections/cid",
headers={"Content-Type": "application/json", "If-Match": '"42"'},
payload={
"data": {"field": "foo", "last_editor_comment": "cancel", "status": "to-rollback"}
},
)

0 comments on commit 07dfd16

Please sign in to comment.