diff --git a/README.md b/README.md index 8386cce..d27bc33 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,9 @@ Either specify an existing `chat_id`, or both `user` and `group_uid` for a new c ### get_export() Get Excel attendance report for a single event, available via the web client. +### change_response() +Change a member's response for an event (e.g. accept/decline) + ## Example scripts The following scripts are included as examples. Some of the scripts might require additional packages to be installed (csv, ical etc). diff --git a/spond/spond.py b/spond/spond.py index ca4f165..dba9202 100644 --- a/spond/spond.py +++ b/spond/spond.py @@ -397,3 +397,27 @@ async def get_event_attendance_xlsx(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 e212447..0ffdaee 100644 --- a/tests/test_spond.py +++ b/tests/test_spond.py @@ -9,6 +9,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 @@ -57,6 +58,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.""" @@ -158,3 +164,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