|
1 | 1 | import json
|
2 | 2 |
|
| 3 | +from django.test import RequestFactory |
3 | 4 | from django.utils import timezone
|
4 | 5 | from rest_framework.reverse import reverse
|
5 | 6 |
|
6 | 7 | from rest_framework.test import APITestCase
|
| 8 | +from rest_framework.test import force_authenticate |
7 | 9 |
|
8 | 10 | from rest_framework_json_api.utils import format_relation_name
|
9 | 11 | from example.models import Blog, Entry, Comment, Author
|
10 | 12 |
|
| 13 | +from .. import views |
| 14 | +from . import TestBase |
| 15 | + |
11 | 16 |
|
12 | 17 | class TestRelationshipView(APITestCase):
|
13 | 18 | def setUp(self):
|
@@ -184,3 +189,33 @@ def test_delete_to_many_relationship_with_change(self):
|
184 | 189 | }
|
185 | 190 | response = self.client.delete(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
|
186 | 191 | assert response.status_code == 200, response.content.decode()
|
| 192 | + |
| 193 | + |
| 194 | +class TestValidationErrorResponses(TestBase): |
| 195 | + def test_if_returns_error_on_empty_post(self): |
| 196 | + view = views.BlogViewSet.as_view({'post': 'create'}) |
| 197 | + response = self._get_create_response("{}", view) |
| 198 | + self.assertEqual(400, response.status_code) |
| 199 | + expected = [{'detail': 'Received document does not contain primary data', 'status': '400', 'source': {'pointer': '/data'}}] |
| 200 | + self.assertEqual(expected, response.data) |
| 201 | + |
| 202 | + def test_if_returns_error_on_missing_form_data_post(self): |
| 203 | + view = views.BlogViewSet.as_view({'post': 'create'}) |
| 204 | + response = self._get_create_response('{"data":{"attributes":{},"type":"blogs"}}', view) |
| 205 | + self.assertEqual(400, response.status_code) |
| 206 | + expected = [{'status': '400', 'detail': 'This field is required.', 'source': {'pointer': '/data/attributes/name'}}] |
| 207 | + self.assertEqual(expected, response.data) |
| 208 | + |
| 209 | + def test_if_returns_error_on_bad_endpoint_name(self): |
| 210 | + view = views.BlogViewSet.as_view({'post': 'create'}) |
| 211 | + response = self._get_create_response('{"data":{"attributes":{},"type":"bad"}}', view) |
| 212 | + self.assertEqual(409, response.status_code) |
| 213 | + expected = [{'detail': "The resource object's type (bad) is not the type that constitute the collection represented by the endpoint (blogs).", 'source': {'pointer': '/data'}, 'status': '409'}] |
| 214 | + self.assertEqual(expected, response.data) |
| 215 | + |
| 216 | + def _get_create_response(self, data, view): |
| 217 | + factory = RequestFactory() |
| 218 | + request = factory.post('/', data, content_type='application/vnd.api+json') |
| 219 | + user = self.create_user('user', 'pass') |
| 220 | + force_authenticate(request, user) |
| 221 | + return view(request) |
0 commit comments