| 1 |
import json |
| 2 |
|
| 3 |
import unittest |
| 4 |
|
| 5 |
import django |
| 6 |
from django.test import override_settings |
| 7 |
|
| 8 |
from rest_framework.test import APITestCase |
| 9 |
from rest_framework_extensions.settings import extensions_api_settings |
| 10 |
from rest_framework_extensions import utils |
| 11 |
|
| 12 |
from .models import ( |
| 13 |
CommentForListUpdateModelMixin as Comment, |
| 14 |
UserForListUpdateModelMixin as User |
| 15 |
) |
| 16 |
from tests_app.testutils import override_extensions_api_settings |
| 17 |
|
| 18 |
|
| 19 |
@override_settings(ROOT_URLCONF='tests_app.tests.functional.mixins.list_update_model_mixin.urls') |
| 20 |
class ListUpdateModelMixinTest(APITestCase): |
| 21 |
|
| 22 |
def setUp(self): |
| 23 |
self.comments = [ |
| 24 |
Comment.objects.create( |
| 25 |
id=1, |
| 26 |
email='[email protected]' |
| 27 |
), |
| 28 |
Comment.objects.create( |
| 29 |
id=2, |
| 30 |
email='[email protected]' |
| 31 |
) |
| 32 |
] |
| 33 |
self.protection_headers = { |
| 34 |
utils.prepare_header_name(extensions_api_settings.DEFAULT_BULK_OPERATION_HEADER_NAME): 'true' |
| 35 |
} |
| 36 |
self.patch_data = { |
| 37 |
'email': '[email protected]' |
| 38 |
} |
| 39 |
|
| 40 |
def test_simple_response(self): |
| 41 |
resp = self.client.get('/comments/') |
| 42 |
expected = [ |
| 43 |
{ |
| 44 |
'id': 1, |
| 45 |
'email': '[email protected]' |
| 46 |
}, |
| 47 |
{ |
| 48 |
'id': 2, |
| 49 |
'email': '[email protected]' |
| 50 |
} |
| 51 |
] |
| 52 |
self.assertEqual(resp.data, expected) |
| 53 |
|
| 54 |
def test_filter_works(self): |
| 55 |
resp = self.client.get('/comments/?id=1') |
| 56 |
expected = [ |
| 57 |
{ |
| 58 |
'id': 1, |
| 59 |
'email': '[email protected]' |
| 60 |
} |
| 61 |
] |
| 62 |
self.assertEqual(resp.data, expected) |
| 63 |
|
| 64 |
def test_update_instance(self): |
| 65 |
data = { |
| 66 |
'id': 1, |
| 67 |
'email': '[email protected]' |
| 68 |
} |
| 69 |
resp = self.client.put('/comments/1/', data=json.dumps(data), content_type='application/json') |
| 70 |
self.assertEqual(resp.status_code, 200) |
| 71 |
self.assertEqual(Comment.objects.get(pk=1).email, '[email protected]') |
| 72 |
|
| 73 |
def test_partial_update_instance(self): |
| 74 |
data = { |
| 75 |
'id': 1, |
| 76 |
'email': '[email protected]' |
| 77 |
} |
| 78 |
resp = self.client.patch('/comments/1/', data=json.dumps(data), content_type='application/json') |
| 79 |
self.assertEqual(resp.status_code, 200) |
| 80 |
self.assertEqual(Comment.objects.get(pk=1).email, '[email protected]') |
| 81 |
|
| 82 |
def test_bulk_partial_update__without_protection_header(self): |
| 83 |
resp = self.client.patch('/comments/', data=json.dumps(self.patch_data), content_type='application/json') |
| 84 |
self.assertEqual(resp.status_code, 400) |
| 85 |
expected_message = { |
| 86 |
'detail': 'Header \'{0}\' should be provided for bulk operation.'.format( |
| 87 |
extensions_api_settings.DEFAULT_BULK_OPERATION_HEADER_NAME |
| 88 |
) |
| 89 |
} |
| 90 |
self.assertEqual(resp.data, expected_message) |
| 91 |
|
| 92 |
def test_bulk_partial_update__with_protection_header(self): |
| 93 |
resp = self.client.patch('/comments/', data=json.dumps(self.patch_data), content_type='application/json', **self.protection_headers) |
| 94 |
self.assertEqual(resp.status_code, 204) |
| 95 |
for comment in Comment.objects.all(): |
| 96 |
self.assertEqual(comment.email, self.patch_data['email']) |
| 97 |
|
| 98 |
@override_extensions_api_settings(DEFAULT_BULK_OPERATION_HEADER_NAME=None) |
| 99 |
def test_bulk_partial_update__without_protection_header__and_with_turned_off_protection_header(self): |
| 100 |
resp = self.client.patch('/comments/', data=json.dumps(self.patch_data), content_type='application/json', **self.protection_headers) |
| 101 |
self.assertEqual(resp.status_code, 204) |
| 102 |
for comment in Comment.objects.all(): |
| 103 |
self.assertEqual(comment.email, self.patch_data['email']) |
| 104 |
|
| 105 |
def test_bulk_partial_update__should_update_filtered_queryset(self): |
| 106 |
resp = self.client.patch('/comments/?id=1', data=json.dumps(self.patch_data), content_type='application/json', **self.protection_headers) |
| 107 |
self.assertEqual(resp.status_code, 204) |
| 108 |
self.assertEqual(Comment.objects.get(pk=1).email, self.patch_data['email']) |
| 109 |
self.assertEqual(Comment.objects.get(pk=2).email, self.comments[1].email) |
| 110 |
|
| 111 |
def test_bulk_partial_update__should_not_update_if_client_has_no_permissions(self): |
| 112 |
resp = self.client.patch('/comments-with-permission/', data=json.dumps(self.patch_data), content_type='application/json', **self.protection_headers) |
| 113 |
self.assertEqual(resp.status_code, 404) |
| 114 |
for i, comment in enumerate(Comment.objects.all()): |
| 115 |
self.assertEqual(comment.email, self.comments[i].email) |
| 116 |
|
| 117 |
|
| 118 |
@override_settings(ROOT_URLCONF='tests_app.tests.functional.mixins.list_update_model_mixin.urls') |
| 119 |
class ListUpdateModelMixinTestBehaviour__serializer_fields(APITestCase): |
| 120 |
|
| 121 |
def setUp(self): |
| 122 |
self.user = User.objects.create( |
| 123 |
id=1, |
| 124 |
name='Gennady', |
| 125 |
age=24, |
| 126 |
last_name='Chibisov', |
| 127 |
email='[email protected]', |
| 128 |
password='somepassword' |
| 129 |
) |
| 130 |
self.headers = { |
| 131 |
utils.prepare_header_name(extensions_api_settings.DEFAULT_BULK_OPERATION_HEADER_NAME): 'true' |
| 132 |
} |
| 133 |
|
| 134 |
def get_fresh_user(self): |
| 135 |
return User.objects.get(pk=self.user.pk) |
| 136 |
|
| 137 |
def test_simple_response(self): |
| 138 |
resp = self.client.get('/users/') |
| 139 |
expected = [ |
| 140 |
{ |
| 141 |
'id': 1, |
| 142 |
'age': 24, |
| 143 |
'name': 'Gennady', |
| 144 |
'surname': 'Chibisov' |
| 145 |
} |
| 146 |
] |
| 147 |
self.assertEqual(resp.data, expected) |
| 148 |
|
| 149 |
def test_invalid_for_db_data(self): |
| 150 |
data = { |
| 151 |
'age': 'Not integer value' |
| 152 |
} |
| 153 |
try: |
| 154 |
resp = self.client.patch('/users/', data=json.dumps(data), content_type='application/json', **self.headers) |
| 155 |
except ValueError: |
| 156 |
self.fail('Errors with invalid for DB data should be caught') |
| 157 |
else: |
| 158 |
self.assertEqual(resp.status_code, 400) |
| 159 |
if django.VERSION < (3, 0, 0): |
| 160 |
expected_message = { |
| 161 |
'detail': "invalid literal for int() with base 10: 'Not integer value'" |
| 162 |
} |
| 163 |
else: |
| 164 |
expected_message = { |
| 165 |
'detail': "Field 'age' expected a number but got 'Not integer value'." |
| 166 |
} |
| 167 |
self.assertEqual(resp.data, expected_message) |
| 168 |
|
| 169 |
def test_should_use_source_if_it_set_in_serializer(self): |
| 170 |
data = { |
| 171 |
'surname': 'Ivanov' |
| 172 |
} |
| 173 |
resp = self.client.patch('/users/', data=json.dumps(data), content_type='application/json', **self.headers) |
| 174 |
self.assertEqual(resp.status_code, 204) |
| 175 |
self.assertEqual(self.get_fresh_user().last_name, data['surname']) |
| 176 |
|
| 177 |
def test_should_update_write_only_fields(self): |
| 178 |
data = { |
| 179 |
'password': '123' |
| 180 |
} |
| 181 |
resp = self.client.patch('/users/', data=json.dumps(data), content_type='application/json', **self.headers) |
| 182 |
self.assertEqual(resp.status_code, 204) |
| 183 |
self.assertEqual(self.get_fresh_user().password, data['password']) |
| 184 |
|
| 185 |
def test_should_not_update_read_only_fields(self): |
| 186 |
data = { |
| 187 |
'name': 'Ivan' |
| 188 |
} |
| 189 |
resp = self.client.patch('/users/', data=json.dumps(data), content_type='application/json', **self.headers) |
| 190 |
self.assertEqual(resp.status_code, 204) |
| 191 |
self.assertEqual(self.get_fresh_user().name, self.user.name) |
| 192 |
|
| 193 |
def test_should_not_update_hidden_fields(self): |
| 194 |
data = { |
| 195 |
'email': '[email protected]' |
| 196 |
} |
| 197 |
resp = self.client.patch('/users/', data=json.dumps(data), content_type='application/json', **self.headers) |
| 198 |
self.assertEqual(resp.status_code, 204) |
| 199 |
self.assertEqual(self.get_fresh_user().email, self.user.email) |