async def test_user_agent(mock_responses): server_url = "http://fake.local/v1" mock_responses.get(server_url + "/", payload={}) client = KintoClient(server_url=server_url) await client.server_info() user_agent = mock_responses.calls[0].request.headers["User-Agent"] assert "telescope" in user_agent assert "kinto_http" in user_agent
async def test_client_extra_headers(mock_responses): server_url = "http://fake.local/v1" mock_responses.get(server_url + "/", payload={}) with mock.patch.dict(config.DEFAULT_REQUEST_HEADERS, {"Extra": "header"}): client = KintoClient(server_url=server_url) await client.server_info() sent_request = mock_responses.calls[0].request assert "Extra" in sent_request.headers
async def test_get_monitor_changes(mock_responses): server_url = "http://fake.local/v1" monitor_url = f"{server_url}/buckets/monitor/collections/changes/records" mock_responses.get(monitor_url, payload={}) client = KintoClient(server_url=server_url) await client.get_monitor_changes() assert mock_responses.calls[0].request.params == {} await client.get_monitor_changes(bust_cache=True) assert "_expected" in mock_responses.calls[1].request.params await client.get_monitor_changes(_expected="bim") assert mock_responses.calls[2].request.params["_expected"] == "bim" with pytest.raises(ValueError): await client.get_monitor_changes(bust_cache=True, _expected="boom")
async def test_get_signature_age_hours(mock_responses): server_url = "http://fake.local/v1" collection_url = server_url + COLLECTION_URL.format("bid", "cid") mock_responses.get( collection_url, payload={ "data": { "id": "cid", "last_signature_date": "2019-09-08T15:11:09.142054+00:00", } }, ) client = KintoClient(server_url=server_url) real_hours = await get_signature_age_hours(client, "bid", "cid") fake_now = datetime.datetime(2019, 9, 9, 14, 57, 38, 297837).replace( tzinfo=datetime.timezone.utc ) with mock.patch(f"{MODULE}.utcnow", return_value=fake_now): hours = await get_signature_age_hours(client, "bid", "cid") assert hours == 23 assert real_hours > 280 # age at the time this test was written.
def test_kinto_auth(): client = KintoClient(server_url="http://server/v1", auth="Bearer token") assert client._client.session.auth.type == "Bearer" assert client._client.session.auth.token == "token"
async def test_get_latest_approvals(mock_responses): server_url = "http://fake.local/v1" history_url = server_url + HISTORY_URL.format("bid") query_params = ( "?resource_name=collection&target.data.id=cid" "&target.data.status=to-sign&_sort=-last_modified&_since=42&_limit=3") mock_responses.get( history_url + query_params, payload={ "data": [{ "id": "0fdeba9f-d83c-4ab2-99f9-d852d6f22cae", "last_modified": 1567790095111, "uri": "/buckets/bid/collections/cid", "date": "2019-09-06T17:14:55.106994", "action": "update", "target": { "data": { "id": "cid", "status": "to-sign", "last_edit_by": "ldap:[email protected]", "last_modified": 1567790094461, "last_edit_date": "2019-09-05T07:15:03.950868+00:00", "last_review_by": "ldap:[email protected]", "last_review_date": "2019-04-16T19:28:10.065088+00:00", "last_signature_by": "account:cloudservices_kinto_prod", "last_editor_comment": "add layout property for existing and new save_login message", "last_signature_date": "2019-09-04T19:41:57.068721+00:00", "last_reviewer_comment": "", "last_review_request_by": "ldap:[email protected]", "last_review_request_date": "2019-09-05T19:12:32.910831+00:00", } }, "user_id": "ldap:[email protected]", "collection_id": "cid", "resource_name": "collection", }] }, ) query_params = ( "?resource_name=record&collection_id=cid" ">_target.data.last_modified=0<_target.data.last_modified=1567790095111" ) mock_responses.get( history_url + query_params, payload={"data": [{ "id": "r1" }, { "id": "r2" }, { "id": "r3" }]}, ) client = KintoClient(server_url=server_url) infos = await get_latest_approvals(client, "bid", "cid", max_approvals=2, min_timestamp=42) assert infos == INFOS