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 15, 2024
1 parent 97fdee1 commit 672571a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
24 changes: 24 additions & 0 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
39 changes: 39 additions & 0 deletions tests/test_spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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

0 comments on commit 672571a

Please sign in to comment.