diff --git a/spond/spond.py b/spond/spond.py index 110374b..00055b6 100644 --- a/spond/spond.py +++ b/spond/spond.py @@ -383,3 +383,27 @@ async def get_export(self, uid: str) -> bytes: output_data = await r.read() return output_data + @SpondBase.require_authentication + async def change_response(self, uid: str, user: str, payload: dict) -> dict: + """change a user's response for an event + + Parameters + ---------- + uid : str + UID of the event. + + user : str + UID of the user + + payload : dict + user response to event, e.g. {"accepted": "true"} + + Returns + ---------- + json: event["responses"] with updated info + """ + url = f"{self.api_url}sponds/{uid}/responses/{user}" + async with self.clientsession.put( + url, headers=self.auth_headers, json=payload + ) as r: + return await r.json() diff --git a/tests/test_spond.py b/tests/test_spond.py index c5c22bd..e6f0703 100644 --- a/tests/test_spond.py +++ b/tests/test_spond.py @@ -7,6 +7,7 @@ MOCK_USERNAME, MOCK_PASSWORD = "MOCK_USERNAME", "MOCK_PASSWORD" MOCK_TOKEN = "MOCK_TOKEN" +MOCK_PAYLOAD = {"accepted": "false", "declineMessage": "sick cannot make it"} # Mock the `require_authentication` decorator to bypass authentication @@ -55,6 +56,11 @@ def mock_token(): return MOCK_TOKEN +@pytest.fixture +def mock_payload(): + return MOCK_PAYLOAD + + @pytest.mark.asyncio async def test_get_event__happy_path(mock_events, mock_token): """Test that a valid `id` returns the matching event.""" @@ -156,3 +162,36 @@ async def test_get_export(mock_get, mock_token): }, ) assert data == mock_binary + + +@pytest.mark.asyncio +@patch("aiohttp.ClientSession.put") +async def test_change_response(mock_put, mock_payload, mock_token): + s = Spond(MOCK_USERNAME, MOCK_PASSWORD) + s.token = mock_token + + mock_response_data = { + "acceptedIds": ["PID1", "PID2"], + "declinedIds": ["PID3"], + "unansweredIds": [], + "waitinglistIds": [], + "unconfirmedIds": [], + "declineMessages": {"PID3": "sick cannot make it"}, + } + mock_put.return_value.__aenter__.return_value.status = 200 + mock_put.return_value.__aenter__.return_value.json = AsyncMock( + return_value=mock_response_data + ) + + response = await s.change_response(uid="ID1", user="PID3", payload=mock_payload) + + mock_url = "https://api.spond.com/core/v1/sponds/ID1/responses/PID3" + mock_put.assert_called_once_with( + mock_url, + headers={ + "content-type": "application/json", + "Authorization": f"Bearer {mock_token}", + }, + json=mock_payload, + ) + assert response == mock_response_data