Skip to content

Commit

Permalink
feat: update user's response to an event
Browse files Browse the repository at this point in the history
  • Loading branch information
maiminhp committed May 23, 2024
1 parent d73eb9b commit 776628f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ Either specify an existing `chat_id`, or both `user` and `group_uid` for a new c
### get_export()
Get excel report for an event attendance, 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).
Expand Down
24 changes: 24 additions & 0 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,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()
39 changes: 39 additions & 0 deletions tests/test_spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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

0 comments on commit 776628f

Please sign in to comment.