From 9ec08efc400925130092351d02aef1dd79a4f9c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 4 Nov 2016 13:41:58 +0100 Subject: [PATCH] Don't undelete when unlinking a deleted shot. --- attract/shots/__init__.py | 9 ++++++--- tests/test_shots.py | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/attract/shots/__init__.py b/attract/shots/__init__.py index 030b707..200cd68 100644 --- a/attract/shots/__init__.py +++ b/attract/shots/__init__.py @@ -244,6 +244,12 @@ def patch_shot(node_id, patch): node_setattr(node, key, value) else: # Remaining operations are for marking as 'in use' or 'not in use'. + if node.get('_deleted', False) and op == u'unlink': + # We won't undelete a node in response to an unlink request. + return pillar.api.utils.jsonify({'_deleted': True, + '_etag': node['_etag'], + '_id': node['_id']}) + used_in_edit = { u'unlink': False, u'relink': True, @@ -255,9 +261,6 @@ def patch_shot(node_id, patch): return pillar.api.utils.jsonify(r, status=status) - - - def assert_is_valid_patch(patch): """Raises an exception when the patch isn't valid.""" diff --git a/tests/test_shots.py b/tests/test_shots.py index 0a73ceb..870a29d 100644 --- a/tests/test_shots.py +++ b/tests/test_shots.py @@ -355,6 +355,28 @@ class PatchShotTest(AbstractShotTest): dbnode = self.get(url, auth_token='token').json() self.assertFalse(dbnode['properties']['used_in_edit']) + @responses.activate + def test_patch_unlink_deleted(self): + """Unlinking a deleted shot shouldn't undelete it. + + We implement PATCH by changing then PUTing, which undeletes by default. + """ + + shot = self.create_shot() + self.create_valid_auth_token(ctd.EXAMPLE_PROJECT_OWNER_ID, 'token') + + url = '/api/nodes/%s' % shot._id + + # Delete (and verify deletion) + self.delete(url, auth_token='token', + headers={'If-Match': shot['_etag']}, + expected_status=204) + self.get(url, auth_token='token', expected_status=404) + + patch = {'op': 'unlink'} + self.patch(url, json=patch, auth_token='token') + self.get(url, auth_token='token', expected_status=404) + @responses.activate def test_patch_relink(self): shot = self.create_shot()