microapi.tests¶
- class microapi.tests.ApiTestCase(methodName='runTest')¶
A lightly customized TestCase that provide convenience methods for making API requests & checking API responses.
This does not do anything automatically (beyond creating a self.factory for making requests).
- assertAccepted(resp)¶
Checks the response for an HTTP 202 Accepted.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 202.
- assertAppError(resp)¶
Checks the response for an HTTP 500 Application Error.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 500.
- assertBadRequest(resp)¶
Checks the response for an HTTP 400 Bad Request.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 400.
- assertCreated(resp)¶
Checks the response for an HTTP 201 Created.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 201.
- assertForbidden(resp)¶
Checks the response for an HTTP 403 Forbidden.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 403.
- assertNoContent(resp)¶
Checks the response for an HTTP 204 No Content.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 204.
- assertNotAllowed(resp)¶
Checks the response for an HTTP 405 Not Allowed.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 405.
- assertNotFound(resp)¶
Checks the response for an HTTP 404 Not Found.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 404.
- assertOK(resp)¶
Checks the response for an HTTP 200 OK.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 200.
- assertResponseEquals(resp, data)¶
Checks for a valid response & asserts the response body matches the expected data.
This checks for: * A valid Content-Type header * Loads the JSON body * Asserts the response data equals the expected data
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
data (dict or list) – The expected data.
- Raises:
AssertionError – If the response is invalid or doesn’t match the data.
ValueError – If a body is present, but is not valid JSON.
- assertStatusCode(resp, status_code)¶
Checks the response for a specific status code.
There are many specialized variants included with this class, so this is really only needed when you need to support a rarer HTTP status code.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
status_code (int) – The desired HTTP status code.
- Raises:
AssertionError – If the expected status code does not match the response.
- assertUnauthorized(resp)¶
Checks the response for an HTTP 401 Unauthorized.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 401.
- create_request(url, method='GET', headers=None, data=None, user=None)¶
Creates a Request object.
- Parameters:
url (str) – The URL as entered by the user.
method (str) – The HTTP method used. Case-insensitive. Default is GET.
headers (dict) – The HTTP headers on the request. Default is None, which turns into basic JSON headers.
data (dict) – The JSON data to send. Default is None.
user (User) – (Optional) Logged in user. Default is None.
- Returns:
The received response object from calling the view.
- Return type:
Response
- make_request(view_class, request, *args, **kwargs)¶
Simulates the request/response cycle against an ApiView.
- Parameters:
view_class (ApiView) – The class to test against.
request (Request) – The request to be sent.
*args (list) – (Optional) Any positional URLconf arguments.
**kwargs (dict) – (Optional) Any keyword URLconf arguments.
- Returns:
The received response.
- Return type:
HttpResponse
- setUp()¶
Hook method for setting up the test fixture before exercising it.
- microapi.tests.assert_accepted(resp)¶
Checks the response for an HTTP 202 Accepted.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 202.
- microapi.tests.assert_app_error(resp)¶
Checks the response for an HTTP 500 Application Error.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 500.
- microapi.tests.assert_bad_request(resp)¶
Checks the response for an HTTP 400 Bad Request.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 400.
- microapi.tests.assert_created(resp)¶
Checks the response for an HTTP 201 Created.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 201.
- microapi.tests.assert_forbidden(resp)¶
Checks the response for an HTTP 403 Forbidden.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 403.
- microapi.tests.assert_no_content(resp)¶
Checks the response for an HTTP 204 No Content.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 204.
- microapi.tests.assert_not_allowed(resp)¶
Checks the response for an HTTP 405 Not Allowed.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 405.
- microapi.tests.assert_not_found(resp)¶
Checks the response for an HTTP 404 Not Found.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 404.
- microapi.tests.assert_ok(resp)¶
Checks the response for an HTTP 200 OK.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 200.
- microapi.tests.assert_status_code(resp, status_code)¶
Checks the response for a specific status code.
There are many specialized variants included with this library, so this is really only needed when you need to support a rarer HTTP status code.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
status_code (int) – The desired HTTP status code.
- Raises:
AssertionError – If the expected status code does not match the response.
- microapi.tests.assert_unauthorized(resp)¶
Checks the response for an HTTP 401 Unauthorized.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Raises:
AssertionError – If the response’s status code is not a 401.
- microapi.tests.check_response(resp)¶
Checks for a valid response & returns the decoded JSON data.
This checks for: * A valid Content-Type header * Loads the JSON body
If no body is present, this returns an empty dict.
- Parameters:
resp (django.http.HttpResponse) – The response from the view.
- Returns:
The loaded data, if any.
- Return type:
dict
- Raises:
AssertionError – If the Content-Type header doesn’t contain the JSON header.
ValueError – If a body is present, but is not valid JSON.
- microapi.tests.create_request(url, method='GET', headers=None, data=None, user=None, factory=None)¶
Creates a Request object (via a django.test.RequestFactory).
- Parameters:
url (str) – The URL as entered by the user.
method (str) – The HTTP method used. Case-insensitive. Default is GET.
headers (dict) – The HTTP headers on the request. Default is None, which turns into basic JSON headers.
data (dict) – The JSON data to send. Default is None.
user (User) – (Optional) Logged in user. Default is None.
factory (RequestFactory) – (Optional) Allows for providing a different RequestFactory. Default is django.test.RequestFactory.
- Returns:
The built request object.
- Return type:
Request