|
| 1 | +import httpx |
| 2 | +import pytest |
| 3 | +import respx |
| 4 | + |
| 5 | +from mpt_api_client.resources.notifications.contacts import ( |
| 6 | + AsyncContactsService, |
| 7 | + ContactsService, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def contacts_service(http_client): |
| 13 | + return ContactsService(http_client=http_client) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def async_contacts_service(async_http_client): |
| 18 | + return AsyncContactsService(http_client=async_http_client) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize( |
| 22 | + ("action", "input_status"), |
| 23 | + [ |
| 24 | + ("block", {"id": "CON-123", "status": "to_block"}), |
| 25 | + ("unblock", {"id": "CON-123", "status": "to_unblock"}), |
| 26 | + ], |
| 27 | +) |
| 28 | +def test_custom_contact_actions(contacts_service, action, input_status): |
| 29 | + request_expected_content = b'{"id":"CON-123","status":"%s"}' % input_status["status"].encode() |
| 30 | + response_expected_data = {"id": "CON-123", "status": "new_status"} |
| 31 | + with respx.mock: |
| 32 | + mock_route = respx.post( |
| 33 | + f"https://api.example.com/public/v1/notifications/contacts/CON-123/{action}" |
| 34 | + ).mock( |
| 35 | + return_value=httpx.Response( |
| 36 | + status_code=200, |
| 37 | + headers={"content-type": "application/json"}, |
| 38 | + json=response_expected_data, |
| 39 | + ) |
| 40 | + ) |
| 41 | + contact = getattr(contacts_service, action)("CON-123", input_status) |
| 42 | + |
| 43 | + assert mock_route.call_count == 1 |
| 44 | + assert contact.to_dict() == response_expected_data |
| 45 | + request = mock_route.calls[0].request |
| 46 | + assert request.content == request_expected_content |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize( |
| 50 | + ("action", "input_status"), |
| 51 | + [ |
| 52 | + ("block", {"id": "CON-123", "status": "to_block"}), |
| 53 | + ("unblock", {"id": "CON-123", "status": "to_unblock"}), |
| 54 | + ], |
| 55 | +) |
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_async_custom_contact_actions(async_contacts_service, action, input_status): |
| 58 | + request_expected_content = b'{"id":"CON-123","status":"%s"}' % input_status["status"].encode() |
| 59 | + response_expected_data = {"id": "CON-123", "status": "new_status"} |
| 60 | + with respx.mock: |
| 61 | + mock_route = respx.post( |
| 62 | + f"https://api.example.com/public/v1/notifications/contacts/CON-123/{action}" |
| 63 | + ).mock( |
| 64 | + return_value=httpx.Response( |
| 65 | + status_code=200, |
| 66 | + headers={"content-type": "application/json"}, |
| 67 | + json=response_expected_data, |
| 68 | + ) |
| 69 | + ) |
| 70 | + contact = await getattr(async_contacts_service, action)("CON-123", input_status) |
| 71 | + |
| 72 | + assert contact.to_dict() == response_expected_data |
| 73 | + assert mock_route.call_count == 1 |
| 74 | + request = mock_route.calls[0].request |
| 75 | + assert request.content == request_expected_content |
0 commit comments