|
| 1 | +import httpx |
| 2 | +import pytest |
| 3 | +import respx |
| 4 | + |
| 5 | +from mpt_api_client.resources.helpdesk.forms import AsyncFormsService, Form, FormsService |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def forms_service(http_client): |
| 10 | + return FormsService(http_client=http_client) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def async_forms_service(async_http_client): |
| 15 | + return AsyncFormsService(http_client=async_http_client) |
| 16 | + |
| 17 | + |
| 18 | +def test_endpoint(forms_service): |
| 19 | + result = forms_service.path == "/public/v1/helpdesk/forms" |
| 20 | + |
| 21 | + assert result is True |
| 22 | + |
| 23 | + |
| 24 | +def test_async_endpoint(async_forms_service): |
| 25 | + result = async_forms_service.path == "/public/v1/helpdesk/forms" |
| 26 | + |
| 27 | + assert result is True |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize( |
| 31 | + "method", |
| 32 | + ["get", "create", "update", "delete", "fetch_page", "iterate", "publish", "unpublish"], |
| 33 | +) |
| 34 | +def test_methods_present(forms_service, method): |
| 35 | + result = hasattr(forms_service, method) |
| 36 | + |
| 37 | + assert result is True |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize( |
| 41 | + "method", |
| 42 | + ["get", "create", "update", "delete", "fetch_page", "iterate", "publish", "unpublish"], |
| 43 | +) |
| 44 | +def test_async_methods_present(async_forms_service, method): |
| 45 | + result = hasattr(async_forms_service, method) |
| 46 | + |
| 47 | + assert result is True |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("action", ["publish", "unpublish"]) |
| 51 | +def test_custom_resource_actions(forms_service, action): |
| 52 | + response_expected_data = {"id": "FRM-1234-5678", "status": "Updated"} |
| 53 | + with respx.mock: |
| 54 | + mock_route = respx.post( |
| 55 | + f"https://api.example.com/public/v1/helpdesk/forms/FRM-1234-5678/{action}" |
| 56 | + ).mock( |
| 57 | + return_value=httpx.Response( |
| 58 | + status_code=httpx.codes.OK, |
| 59 | + headers={"content-type": "application/json"}, |
| 60 | + json=response_expected_data, |
| 61 | + ) |
| 62 | + ) |
| 63 | + |
| 64 | + result = getattr(forms_service, action)("FRM-1234-5678") |
| 65 | + |
| 66 | + assert mock_route.call_count == 1 |
| 67 | + request = mock_route.calls[0].request |
| 68 | + assert request.content == b"" |
| 69 | + assert result.to_dict() == response_expected_data |
| 70 | + assert isinstance(result, Form) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize("action", ["publish", "unpublish"]) |
| 74 | +async def test_async_custom_resource_actions(async_forms_service, action): |
| 75 | + response_expected_data = {"id": "FRM-1234-5678", "status": "Updated"} |
| 76 | + with respx.mock: |
| 77 | + mock_route = respx.post( |
| 78 | + f"https://api.example.com/public/v1/helpdesk/forms/FRM-1234-5678/{action}" |
| 79 | + ).mock( |
| 80 | + return_value=httpx.Response( |
| 81 | + status_code=httpx.codes.OK, |
| 82 | + headers={"content-type": "application/json"}, |
| 83 | + json=response_expected_data, |
| 84 | + ) |
| 85 | + ) |
| 86 | + |
| 87 | + result = await getattr(async_forms_service, action)("FRM-1234-5678") |
| 88 | + |
| 89 | + assert mock_route.call_count == 1 |
| 90 | + request = mock_route.calls[0].request |
| 91 | + assert request.content == b"" |
| 92 | + assert result.to_dict() == response_expected_data |
| 93 | + assert isinstance(result, Form) |
0 commit comments