Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/helpers.py
Show First 20 Lines • Show All 134 Lines • ▼ Show 20 Lines | |||||
def check_api_post_response( | def check_api_post_response( | ||||
api_client: APIClient, | api_client: APIClient, | ||||
url: str, | url: str, | ||||
status_code: int, | status_code: int, | ||||
content_type: str = "*/*", | content_type: str = "*/*", | ||||
data: Optional[Dict[str, Any]] = None, | data: Optional[Dict[str, Any]] = None, | ||||
**headers, | |||||
) -> HttpResponseBase: | ) -> HttpResponseBase: | ||||
"""Helper function to check Web API response for a POST request | """Helper function to check Web API response for a POST request | ||||
for all accepted content types. | for all accepted content types. | ||||
Args: | Args: | ||||
api_client: DRF test client | api_client: DRF test client | ||||
url: Web API URL to check response | url: Web API URL to check response | ||||
status_code: expected HTTP status code | status_code: expected HTTP status code | ||||
Returns: | Returns: | ||||
The HTTP response | The HTTP response | ||||
""" | """ | ||||
return _assert_http_response( | return _assert_http_response( | ||||
response=api_client.post( | response=api_client.post( | ||||
url, | url, | ||||
data=data, | data=data, | ||||
format="json", | format="json", | ||||
HTTP_ACCEPT=content_type, | HTTP_ACCEPT=content_type, | ||||
**headers, | |||||
), | ), | ||||
status_code=status_code, | status_code=status_code, | ||||
content_type=content_type, | content_type=content_type, | ||||
) | ) | ||||
def check_api_post_responses( | def check_api_post_responses( | ||||
api_client: APIClient, | api_client: APIClient, | ||||
url: str, | url: str, | ||||
status_code: int, | status_code: int, | ||||
data: Optional[Dict[str, Any]] = None, | data: Optional[Dict[str, Any]] = None, | ||||
**headers, | |||||
) -> Response: | ) -> Response: | ||||
"""Helper function to check Web API responses for POST requests | """Helper function to check Web API responses for POST requests | ||||
for all accepted content types (JSON, YAML). | for all accepted content types (JSON, YAML). | ||||
Args: | Args: | ||||
api_client: DRF test client | api_client: DRF test client | ||||
url: Web API URL to check responses | url: Web API URL to check responses | ||||
status_code: expected HTTP status code | status_code: expected HTTP status code | ||||
Returns: | Returns: | ||||
The Web API JSON response | The Web API JSON response | ||||
""" | """ | ||||
# check JSON response | # check JSON response | ||||
response_json = check_api_post_response( | response_json = check_api_post_response( | ||||
api_client, url, status_code, content_type="application/json", data=data | api_client, | ||||
url, | |||||
status_code, | |||||
content_type="application/json", | |||||
data=data, | |||||
**headers, | |||||
) | ) | ||||
# check YAML response | # check YAML response | ||||
check_api_post_response( | check_api_post_response( | ||||
api_client, url, status_code, content_type="application/yaml", data=data | api_client, | ||||
url, | |||||
status_code, | |||||
content_type="application/yaml", | |||||
data=data, | |||||
**headers, | |||||
) | ) | ||||
return cast(Response, response_json) | return cast(Response, response_json) | ||||
def check_html_get_response( | def check_html_get_response( | ||||
client: Client, | client: Client, | ||||
url: str, | url: str, | ||||
▲ Show 20 Lines • Show All 55 Lines • Show Last 20 Lines |