| 1 |
from django.urls import reverse |
| 2 |
from rest_framework import status |
| 3 |
from rest_framework.test import APITestCase |
| 4 |
from .models import Book |
| 5 |
from django.test import override_settings |
| 6 |
|
| 7 |
import json |
| 8 |
|
| 9 |
|
| 10 |
@override_settings(ROOT_URLCONF='tests_app.tests.functional._concurrency.conditional_request.urls') |
| 11 |
class BookAPITestCases(APITestCase): |
| 12 |
""" |
| 13 |
Run the conditional requests test cases. |
| 14 |
`tox -- tests_app.tests.functional._concurrency.conditional_request.tests` |
| 15 |
""" |
| 16 |
def setUp(self): |
| 17 |
# create a book |
| 18 |
self.book = Book.objects.create(name='The Summons', |
| 19 |
author='Stephen King', |
| 20 |
issn='9780345531988') |
| 21 |
|
| 22 |
def alter_book_issn(self, issn='0123456789012'): |
| 23 |
"""Mimic alteration of object in the DB.""" |
| 24 |
self.book.issn = issn |
| 25 |
self.book.save() |
| 26 |
return self.book |
| 27 |
|
| 28 |
def test_book_retrieve_cache_hit(self): |
| 29 |
"""Test idempotent retrieve using 'If-None-Match' HTTP header, should result in HTTP 304.""" |
| 30 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 31 |
CONTENT_TYPE='application/json') |
| 32 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 33 |
# memorize the ETag from the response to send with the next request |
| 34 |
etag = book_response['ETag'] |
| 35 |
|
| 36 |
# issue the same request again |
| 37 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 38 |
CONTENT_TYPE='application/json', |
| 39 |
HTTP_IF_NONE_MATCH=etag) |
| 40 |
|
| 41 |
self.assertEqual(book_response.status_code, status.HTTP_304_NOT_MODIFIED, |
| 42 |
'The response status code must be 304!') |
| 43 |
self.assertEqual(book_response['ETag'], etag) |
| 44 |
|
| 45 |
def test_book_retrieve_cache_miss(self): |
| 46 |
"""Test idempotent retrieve using 'If-None-Match' HTTP header, should result in HTTP 200.""" |
| 47 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 48 |
CONTENT_TYPE='application/json') |
| 49 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 50 |
# memorize the ETag from the response to send with the next request |
| 51 |
etag = book_response['ETag'] |
| 52 |
|
| 53 |
# simulate background activity on the book |
| 54 |
self.alter_book_issn() |
| 55 |
|
| 56 |
# issue the same request again |
| 57 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 58 |
CONTENT_TYPE='application/json', |
| 59 |
HTTP_IF_NONE_MATCH=etag) |
| 60 |
|
| 61 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK, |
| 62 |
'The response status code must be 200!') |
| 63 |
self.assertNotEqual(book_response['ETag'], etag) |
| 64 |
|
| 65 |
def test_book_update_unconditional(self): |
| 66 |
"""Test an update without providing the 'ETag' HTTP header, should yield HTTP 200.""" |
| 67 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 68 |
CONTENT_TYPE='application/json') |
| 69 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 70 |
book_json = json.loads(book_response.content.decode()) |
| 71 |
# alter the author |
| 72 |
book_json['author'] = 'John Grisham' |
| 73 |
url = reverse('book_view-unconditional_update', kwargs={'pk': book_json['id']}) |
| 74 |
response = self.client.put(url, |
| 75 |
data=book_json) |
| 76 |
self.assertEqual(response.status_code, status.HTTP_200_OK, 'The response status code must be 200!') |
| 77 |
updated_book_json = json.loads(response.content.decode()) |
| 78 |
self.assertEqual(updated_book_json['author'], book_json['author'], 'Author must be John Grisham!') |
| 79 |
|
| 80 |
def test_book_delete_unconditional(self): |
| 81 |
"""Test delete, should result in HTTP 204.""" |
| 82 |
# retrieve |
| 83 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 84 |
CONTENT_TYPE='application/json') |
| 85 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 86 |
# delete |
| 87 |
response = self.client.delete(reverse('book_view-unconditional_delete', kwargs={'pk': self.book.id})) |
| 88 |
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, 'The response status code must be 204!') |
| 89 |
|
| 90 |
def test_book_conditional_update(self): |
| 91 |
"""Test a conditional update of a book using 'If-Match' HTTP header, should yield HTTP 200.""" |
| 92 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 93 |
CONTENT_TYPE='application/json') |
| 94 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 95 |
self.assertIsNotNone(book_response['ETag']) |
| 96 |
book_json = json.loads(book_response.content.decode()) |
| 97 |
# memorize the ETag from the response to send with the next request |
| 98 |
etag = book_response['ETag'] |
| 99 |
|
| 100 |
# alter the author |
| 101 |
book_json['author'] = 'John Grisham' |
| 102 |
url = reverse('book-detail', kwargs={'pk': book_json['id']}) |
| 103 |
response = self.client.put(url, |
| 104 |
data=book_json, |
| 105 |
HTTP_IF_MATCH=etag) # <-- set the ETag header to trigger the conditional request |
| 106 |
|
| 107 |
# ######################## ######################## ######################## |
| 108 |
# this update must succeed, since the if-match header is the same as the ETag sent from the server! |
| 109 |
self.assertNotEqual(response.status_code, status.HTTP_412_PRECONDITION_FAILED, 'The response status code must ' |
| 110 |
'not be 412!') |
| 111 |
self.assertEqual(response.status_code, status.HTTP_200_OK, 'The response status code must be 200!') |
| 112 |
# ######################## ######################## ######################## |
| 113 |
|
| 114 |
updated_book_json = json.loads(response.content.decode()) |
| 115 |
self.assertEqual(updated_book_json['author'], book_json['author'], 'Author must be John Grisham!') |
| 116 |
|
| 117 |
def test_book_conditional_update_fail(self): |
| 118 |
"""Test a conditional update of a book using 'If-Match' HTTP header, should yield HTTP 412.""" |
| 119 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 120 |
CONTENT_TYPE='application/json') |
| 121 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 122 |
self.assertIsNotNone(book_response['ETag']) |
| 123 |
book_json = json.loads(book_response.content.decode()) |
| 124 |
# memorize the ETag from the response to send with the next request |
| 125 |
etag = book_response['ETag'] |
| 126 |
|
| 127 |
# mimic background activity |
| 128 |
self.alter_book_issn() |
| 129 |
|
| 130 |
# alter the author |
| 131 |
book_json['author'] = 'John Grisham' |
| 132 |
url = reverse('book-detail', kwargs={'pk': book_json['id']}) |
| 133 |
response = self.client.put(url, |
| 134 |
data=book_json, |
| 135 |
HTTP_IF_MATCH=etag) # <-- set the ETag header to trigger the conditional request |
| 136 |
|
| 137 |
self.assertEqual(response.status_code, status.HTTP_412_PRECONDITION_FAILED, 'The response status code must ' |
| 138 |
'be 412!') |
| 139 |
|
| 140 |
def test_book_conditional_update_fail_no_if_match(self): |
| 141 |
"""Test a conditional update of a book using no HTTP 'If-Match' header, should yield HTTP 428.""" |
| 142 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 143 |
CONTENT_TYPE='application/json') |
| 144 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 145 |
self.assertIsNotNone(book_response['ETag']) |
| 146 |
book_json = json.loads(book_response.content.decode()) |
| 147 |
|
| 148 |
# alter the author |
| 149 |
book_json['author'] = 'John Grisham' |
| 150 |
url = reverse('book-detail', kwargs={'pk': book_json['id']}) |
| 151 |
response = self.client.put(url, |
| 152 |
data=book_json) |
| 153 |
|
| 154 |
self.assertEqual(response.status_code, status.HTTP_428_PRECONDITION_REQUIRED, 'The response status code must ' |
| 155 |
'be 428!') |
| 156 |
|
| 157 |
def test_book_conditional_update_fail_first_then_succeed(self): |
| 158 |
"""Test a conditional update of a book using 'If-Match' HTTP header, should yield HTTP 412, then 200.""" |
| 159 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 160 |
CONTENT_TYPE='application/json') |
| 161 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 162 |
self.assertIsNotNone(book_response['ETag']) |
| 163 |
book_json = json.loads(book_response.content.decode()) |
| 164 |
# memorize the ETag from the response to send with the next request |
| 165 |
etag = book_response['ETag'] |
| 166 |
|
| 167 |
# mimic background activity |
| 168 |
self.alter_book_issn() |
| 169 |
|
| 170 |
# try to alter the author |
| 171 |
book_json['author'] = 'John Grisham' |
| 172 |
url = reverse('book-detail', kwargs={'pk': book_json['id']}) |
| 173 |
response = self.client.put(url, |
| 174 |
data=book_json, |
| 175 |
HTTP_IF_MATCH=etag) # <-- set the ETag header to trigger the conditional request |
| 176 |
|
| 177 |
# ######################## ######################## ######################## |
| 178 |
# this update must succeed, since the if-match header is the same as the ETag sent from the server! |
| 179 |
self.assertEqual(response.status_code, status.HTTP_412_PRECONDITION_FAILED, 'The response status code must ' |
| 180 |
'be 412!') |
| 181 |
# ######################## ######################## ######################## |
| 182 |
|
| 183 |
# fetch the instance again and try again |
| 184 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 185 |
CONTENT_TYPE='application/json') |
| 186 |
|
| 187 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 188 |
self.assertIsNotNone(book_response['ETag']) |
| 189 |
book_json = json.loads(book_response.content.decode()) |
| 190 |
# memorize the ETag from the response to send with the next request |
| 191 |
new_etag = book_response['ETag'] |
| 192 |
|
| 193 |
# ...client merges/rejects the local changes... |
| 194 |
|
| 195 |
# try again to update |
| 196 |
response = self.client.put(url, |
| 197 |
data=book_json, |
| 198 |
HTTP_IF_MATCH=new_etag) # <-- set the ETag header to trigger the conditional request |
| 199 |
|
| 200 |
# ######################## ######################## ######################## |
| 201 |
# this update must succeed, since the if-match header is the same as the ETag sent from the server! |
| 202 |
self.assertNotEqual(response.status_code, status.HTTP_412_PRECONDITION_FAILED, 'The response status code must ' |
| 203 |
'not be 412!') |
| 204 |
self.assertEqual(response.status_code, status.HTTP_200_OK, 'The response status code must be 200!') |
| 205 |
# ######################## ######################## ######################## |
| 206 |
|
| 207 |
updated_book_json = json.loads(response.content.decode()) |
| 208 |
self.assertEqual(updated_book_json['author'], book_json['author'], 'Author must be John Grisham!') |
| 209 |
|
| 210 |
def test_book_conditional_delete_default_viewset(self): |
| 211 |
"""Test conditional delete using 'If-Match' HTTP header, should result in HTTP 204.""" |
| 212 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 213 |
CONTENT_TYPE='application/json') |
| 214 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 215 |
# memorize the ETag from the response to send with the next request |
| 216 |
etag = book_response['ETag'] |
| 217 |
|
| 218 |
# delete the instance |
| 219 |
book_response = self.client.delete(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 220 |
HTTP_IF_MATCH=etag) |
| 221 |
|
| 222 |
self.assertEqual(book_response.status_code, status.HTTP_204_NO_CONTENT, 'The response status code must be 204!') |
| 223 |
|
| 224 |
def test_book_conditional_delete_custom_view(self): |
| 225 |
"""Test conditional delete using 'If-Match' HTTP header, should result in HTTP 204.""" |
| 226 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 227 |
CONTENT_TYPE='application/json') |
| 228 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 229 |
# memorize the ETag from the response to send with the next request |
| 230 |
etag = book_response['ETag'] |
| 231 |
|
| 232 |
# delete the instance |
| 233 |
book_response = self.client.delete(reverse('book_view-custom_delete', kwargs={'pk': self.book.id}), |
| 234 |
HTTP_IF_MATCH=etag) |
| 235 |
|
| 236 |
self.assertEqual(book_response.status_code, status.HTTP_204_NO_CONTENT, 'The response status code must be 204!') |
| 237 |
|
| 238 |
def test_book_conditional_delete_fail(self): |
| 239 |
"""Test conditional delete using 'If-Match' HTTP header, should result in HTTP 412.""" |
| 240 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 241 |
CONTENT_TYPE='application/json') |
| 242 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 243 |
# memorize the ETag from the response to send with the next request |
| 244 |
etag = book_response['ETag'] |
| 245 |
|
| 246 |
# alter the book |
| 247 |
self.alter_book_issn() |
| 248 |
|
| 249 |
# delete the instance |
| 250 |
book_response = self.client.delete(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 251 |
HTTP_IF_MATCH=etag) |
| 252 |
|
| 253 |
self.assertEqual(book_response.status_code, status.HTTP_412_PRECONDITION_FAILED, |
| 254 |
'The response status code must be 412!') |
| 255 |
|
| 256 |
def test_book_conditional_delete_fail_no_if_match(self): |
| 257 |
"""Test conditional delete without 'If-Match' HTTP header, should result in HTTP 428.""" |
| 258 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 259 |
CONTENT_TYPE='application/json') |
| 260 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 261 |
|
| 262 |
# delete the instance |
| 263 |
book_response = self.client.delete(reverse('book-detail', kwargs={'pk': self.book.id})) |
| 264 |
self.assertEqual(book_response.status_code, status.HTTP_428_PRECONDITION_REQUIRED, |
| 265 |
'The response status code must be 428!') |
| 266 |
|
| 267 |
def test_book_retrieve_cache_hit_view(self): |
| 268 |
"""Test idempotent retrieve using 'If-None-Match' HTTP header, should result in HTTP 304.""" |
| 269 |
book_response = self.client.get(reverse('book_view-detail', kwargs={'pk': self.book.id}), |
| 270 |
CONTENT_TYPE='application/json') |
| 271 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 272 |
# memorize the ETag from the response to send with the next request |
| 273 |
etag = book_response['ETag'] |
| 274 |
|
| 275 |
# issue the same request again |
| 276 |
book_response = self.client.get(reverse('book_view-detail', kwargs={'pk': self.book.id}), |
| 277 |
CONTENT_TYPE='application/json', |
| 278 |
HTTP_IF_NONE_MATCH=etag) |
| 279 |
|
| 280 |
self.assertEqual(book_response.status_code, status.HTTP_304_NOT_MODIFIED, |
| 281 |
'The response status code must be 304!') |
| 282 |
self.assertEqual(book_response['ETag'], etag) |
| 283 |
|
| 284 |
def test_book_conditional_custom_delete_decorator(self): |
| 285 |
"""Test conditional delete using 'If-Match' HTTP header, should result in HTTP 204.""" |
| 286 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 287 |
CONTENT_TYPE='application/json') |
| 288 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 289 |
# memorize the ETag from the response to send with the next request |
| 290 |
etag = book_response['ETag'] |
| 291 |
|
| 292 |
# delete the instance |
| 293 |
book_response = self.client.delete(reverse('book_view-custom_delete', kwargs={'pk': self.book.id}), |
| 294 |
HTTP_IF_MATCH=etag) |
| 295 |
|
| 296 |
self.assertEqual(book_response.status_code, status.HTTP_204_NO_CONTENT, |
| 297 |
'The response status code must be 204!') |
| 298 |
|
| 299 |
def test_book_conditional_custom_delete_decorator_fail(self): |
| 300 |
"""Test conditional delete using 'If-Match' HTTP header, should result in HTTP 412.""" |
| 301 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 302 |
CONTENT_TYPE='application/json') |
| 303 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 304 |
# memorize the ETag from the response to send with the next request |
| 305 |
etag = book_response['ETag'] |
| 306 |
|
| 307 |
# change the instance in the DB |
| 308 |
self.alter_book_issn() |
| 309 |
|
| 310 |
# delete the instance |
| 311 |
book_response = self.client.delete(reverse('book_view-custom_delete', kwargs={'pk': self.book.id}), |
| 312 |
HTTP_IF_MATCH=etag) |
| 313 |
|
| 314 |
self.assertEqual(book_response.status_code, status.HTTP_412_PRECONDITION_FAILED, |
| 315 |
'The response status code must be 412!') |
| 316 |
|
| 317 |
def test_book_conditional_custom_delete_decorator_fail__not_found(self): |
| 318 |
"""Test conditional delete using 'If-Match' HTTP header, should result in HTTP 412.""" |
| 319 |
book_response = self.client.get(reverse('book-detail', kwargs={'pk': self.book.id}), |
| 320 |
CONTENT_TYPE='application/json') |
| 321 |
self.assertEqual(book_response.status_code, status.HTTP_200_OK) |
| 322 |
book_json = json.loads(book_response.content.decode()) |
| 323 |
# memorize the ETag from the response to send with the next request |
| 324 |
etag = book_response['ETag'] |
| 325 |
|
| 326 |
# delete the instance in the DB |
| 327 |
self.book.delete() |
| 328 |
|
| 329 |
# delete the instance |
| 330 |
book_response = self.client.delete(reverse('book_view-custom_delete', |
| 331 |
kwargs={'pk': book_json['id']}), |
| 332 |
HTTP_IF_MATCH=etag) |
| 333 |
|
| 334 |
self.assertEqual(book_response.status_code, status.HTTP_412_PRECONDITION_FAILED, |
| 335 |
'The response status code must be 412!') |