Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Fix PUT not using create() method in storage backend when tombstone e…
Browse files Browse the repository at this point in the history
…xists (fixes #530)
  • Loading branch information
leplatrem committed Oct 30, 2015
1 parent 920d55c commit 31654bd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ This document describes changes between each past release.

- Fix PostgreSQL error when deleting an empty collection in a protected
resource (fixes #528)
- Fix PUT not using ``create()`` method in storage backend when tombstone exists
(fixes #530)

**Internal changes**

Expand Down
13 changes: 7 additions & 6 deletions cliquet/resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,16 @@ def put(self):
self._raise_400_if_invalid_id(self.record_id)
id_field = self.model.id_field
existing = None
tombstones = None
try:
existing = self._get_record_or_404(self.record_id)
except HTTPNotFound:
# Look if this record used to exist (for preconditions check).
deleted = Filter(id_field, self.record_id, COMPARISON.EQ)
result, _ = self.model.get_records(filters=[deleted],
include_deleted=True)
if len(result) > 0:
existing = result[0]
filter_by_id = Filter(id_field, self.record_id, COMPARISON.EQ)
tombstones, _ = self.model.get_records(filters=[filter_by_id],
include_deleted=True)
if len(tombstones) > 0:
existing = tombstones[0]
finally:
if existing:
self._raise_412_if_modified(existing)
Expand All @@ -366,7 +367,7 @@ def put(self):

try:
unique = self.mapping.get_option('unique_fields')
if existing:
if existing and not tombstones:
record = self.model.update_record(new_record,
unique_fields=unique)
else:
Expand Down
10 changes: 10 additions & 0 deletions cliquet/tests/resource/test_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ def test_relies_on_collection_create(self):
self.resource.put()
self.assertEqual(patched.call_count, 1)

def test_relies_on_collection_create_even_when_previously_deleted(self):
record = self.model.create_record({'field': 'value'})
self.resource.record_id = record['id']
self.resource.delete()['data']

self.resource.request.validated = {'data': {'field': 'new'}}
with mock.patch.object(self.model, 'create_record') as patched:
self.resource.put()
self.assertEqual(patched.call_count, 1)

def test_replace_record_returns_updated_fields(self):
self.resource.request.validated = {'data': {'field': 'new'}}
result = self.resource.put()['data']
Expand Down
2 changes: 1 addition & 1 deletion cliquet/tests/resource/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_record_operations_are_authorized_if_authenticated(self):
self.app.get(record_url, headers=self.headers, status=200)
self.app.patch_json(record_url, body, headers=self.headers, status=200)
self.app.delete(record_url, headers=self.headers, status=200)
self.app.put_json(record_url, body, headers=self.headers, status=200)
self.app.put_json(record_url, body, headers=self.headers, status=201)
self.app.put_json(unknown_url, body, headers=self.headers, status=201)


Expand Down

0 comments on commit 31654bd

Please sign in to comment.