Skip to content

Commit 07dfd16

Browse files
authored
Add methods related to Remote Settings plugin (#379)
1 parent 24853fc commit 07dfd16

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

src/kinto_http/client.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import mimetypes
77
import os
8+
import random
89
import uuid
910
from collections import OrderedDict
1011
from contextlib import contextmanager
@@ -940,6 +941,61 @@ def remove_attachment(self, id, bucket=None, collection=None):
940941
resp, _ = self.session.request("delete", endpoint)
941942
return resp
942943

944+
def get_changeset(self, bucket=None, collection=None, bust_cache=False, **kwargs):
945+
kwargs.setdefault(
946+
"_expected", random.randint(999999000000, 999999999999) if bust_cache else 0
947+
)
948+
endpoint = self._get_endpoint("changeset", bucket=bucket, collection=collection)
949+
resp, _ = self.session.request("get", endpoint, params=kwargs)
950+
return resp
951+
952+
def request_review(self, message, id=None, bucket=None, **kwargs):
953+
return self.patch_collection(
954+
id=id,
955+
bucket=bucket,
956+
data={
957+
**kwargs.pop("data", {}),
958+
"status": "to-review",
959+
"last_editor_comment": message,
960+
},
961+
**kwargs,
962+
)
963+
964+
def decline_changes(self, message, id=None, bucket=None, **kwargs):
965+
return self.patch_collection(
966+
id=id,
967+
bucket=bucket,
968+
data={
969+
**kwargs.pop("data", {}),
970+
"status": "work-in-progress",
971+
"last_reviewer_comment": message,
972+
},
973+
**kwargs,
974+
)
975+
976+
def approve_changes(self, id=None, bucket=None, **kwargs):
977+
return self.patch_collection(
978+
id=id,
979+
bucket=bucket,
980+
data={
981+
**kwargs.pop("data", {}),
982+
"status": "to-sign",
983+
},
984+
**kwargs,
985+
)
986+
987+
def rollback_changes(self, message, id=None, bucket=None, **kwargs):
988+
return self.patch_collection(
989+
id=id,
990+
bucket=bucket,
991+
data={
992+
**kwargs.pop("data", {}),
993+
"status": "to-rollback",
994+
"last_editor_comment": message,
995+
},
996+
**kwargs,
997+
)
998+
943999
def __repr__(self) -> str:
9441000
if self.collection_name:
9451001
endpoint = self._get_endpoint(

src/kinto_http/endpoints.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Endpoints(object):
2222
"records": "{root}/buckets/{bucket}/collections/{collection}/records", # NOQA
2323
"record": "{root}/buckets/{bucket}/collections/{collection}/records/{id}", # NOQA
2424
"attachment": "{root}/buckets/{bucket}/collections/{collection}/records/{id}/attachment", # NOQA
25+
"changeset": "{root}/buckets/{bucket}/collections/{collection}/changeset", # NOQA
2526
}
2627

2728
def __init__(self, root=""):

tests/test_client.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,3 +1462,105 @@ def test_get_permissions(client_setup: Client):
14621462
"_sort": "id",
14631463
},
14641464
)
1465+
1466+
1467+
def test_get_changeset_default(client_setup: Client):
1468+
client = client_setup
1469+
client.collection_name = "foo"
1470+
mock_response(client.session)
1471+
1472+
client.get_changeset()
1473+
client.session.request.assert_called_with(
1474+
"get", "/buckets/mybucket/collections/foo/changeset", params={"_expected": 0}
1475+
)
1476+
1477+
1478+
def test_get_changeset_bust(client_setup: Client, mocker: MockerFixture):
1479+
client = client_setup
1480+
mock_response(client.session)
1481+
mocked_random = mocker.patch("kinto_http.client.random")
1482+
mocked_random.randint.return_value = 42
1483+
1484+
client.get_changeset(collection="bar", bust_cache=True)
1485+
client.session.request.assert_called_with(
1486+
"get", "/buckets/mybucket/collections/bar/changeset", params={"_expected": 42}
1487+
)
1488+
1489+
1490+
def test_get_changeset_params(client_setup: Client, mocker: MockerFixture):
1491+
client = client_setup
1492+
mock_response(client.session)
1493+
1494+
client.get_changeset(bucket="foo", collection="bar", _since='"42"')
1495+
client.session.request.assert_called_with(
1496+
"get", "/buckets/foo/collections/bar/changeset", params={"_expected": 0, "_since": '"42"'}
1497+
)
1498+
1499+
1500+
def test_request_review(client_setup: Client, mocker: MockerFixture):
1501+
client = client_setup.clone(collection="cid")
1502+
mock_response(client.session)
1503+
1504+
client.request_review("r?")
1505+
client.session.request.assert_called_with(
1506+
"patch",
1507+
"/buckets/mybucket/collections/cid",
1508+
headers={"Content-Type": "application/json"},
1509+
payload={"data": {"last_editor_comment": "r?", "status": "to-review"}},
1510+
)
1511+
1512+
1513+
def test_request_review_advanced(client_setup: Client, mocker: MockerFixture):
1514+
client = client_setup
1515+
mock_response(client.session)
1516+
1517+
client.request_review("r?", id="cid", data={"field": "foo"}, if_match='"42"')
1518+
client.session.request.assert_called_with(
1519+
"patch",
1520+
"/buckets/mybucket/collections/cid",
1521+
headers={"Content-Type": "application/json", "If-Match": '"42"'},
1522+
payload={"data": {"field": "foo", "last_editor_comment": "r?", "status": "to-review"}},
1523+
)
1524+
1525+
1526+
def test_approve_changes(client_setup: Client, mocker: MockerFixture):
1527+
client = client_setup
1528+
mock_response(client.session)
1529+
1530+
client.approve_changes(id="cid", data={"field": "foo"}, if_match='"42"')
1531+
client.session.request.assert_called_with(
1532+
"patch",
1533+
"/buckets/mybucket/collections/cid",
1534+
headers={"Content-Type": "application/json", "If-Match": '"42"'},
1535+
payload={"data": {"field": "foo", "status": "to-sign"}},
1536+
)
1537+
1538+
1539+
def test_decline_changes(client_setup: Client, mocker: MockerFixture):
1540+
client = client_setup
1541+
mock_response(client.session)
1542+
1543+
client.decline_changes(message="r-", id="cid", data={"field": "foo"}, if_match='"42"')
1544+
client.session.request.assert_called_with(
1545+
"patch",
1546+
"/buckets/mybucket/collections/cid",
1547+
headers={"Content-Type": "application/json", "If-Match": '"42"'},
1548+
payload={
1549+
"data": {"field": "foo", "last_reviewer_comment": "r-", "status": "work-in-progress"}
1550+
},
1551+
)
1552+
1553+
1554+
def test_rollback_changes(client_setup: Client, mocker: MockerFixture):
1555+
client = client_setup
1556+
mock_response(client.session)
1557+
1558+
client.rollback_changes(message="cancel", id="cid", data={"field": "foo"}, if_match='"42"')
1559+
client.session.request.assert_called_with(
1560+
"patch",
1561+
"/buckets/mybucket/collections/cid",
1562+
headers={"Content-Type": "application/json", "If-Match": '"42"'},
1563+
payload={
1564+
"data": {"field": "foo", "last_editor_comment": "cancel", "status": "to-rollback"}
1565+
},
1566+
)

0 commit comments

Comments
 (0)