From 8b7a50ecf2f950b1a672fb5bf699c67108e97c53 Mon Sep 17 00:00:00 2001 From: ariannedee Date: Thu, 1 Oct 2020 12:44:14 -0700 Subject: [PATCH] Add get file test. Delete all documents for a type if file is not in redis --- edivorce/apps/core/models.py | 5 ++- edivorce/apps/core/tests/test_api.py | 46 ++++++++++++++++++++++++++++ edivorce/apps/core/views/api.py | 7 ++++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/edivorce/apps/core/models.py b/edivorce/apps/core/models.py index 39d186f1..145a2c06 100644 --- a/edivorce/apps/core/models.py +++ b/edivorce/apps/core/models.py @@ -179,7 +179,10 @@ class Document(models.Model): @staticmethod def get_file(file_key): - return redis.RedisStorage().open(file_key) + return redis.RedisStorage().get(file_key) + + def file_exists(self): + return redis.RedisStorage().exists(self.file.name) class DontLog: diff --git a/edivorce/apps/core/tests/test_api.py b/edivorce/apps/core/tests/test_api.py index eab9fca6..38ea8e5e 100644 --- a/edivorce/apps/core/tests/test_api.py +++ b/edivorce/apps/core/tests/test_api.py @@ -151,6 +151,25 @@ class APITest(APITestCase): json_response = json.loads(response.content) self.assertEqual(len(json_response), 0) + def test_get_file(self): + document = self._create_document() + self.assertEqual(Document.objects.count(), 1) + url = reverse('document', kwargs={'doc_type': document.doc_type, + 'party_code': document.party_code, + 'filename': document.filename, + 'size': document.size}) + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + + self.client.force_authenticate(self.another_user) + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) + + self.client.force_authenticate(self.user) + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.content, document.file.read()) + def test_delete_document(self): document = self._create_document() self.assertEqual(Document.objects.count(), 1) @@ -198,6 +217,33 @@ class APITest(APITestCase): 'Rotation must be 0, 90, 180, or 270', status_code=status.HTTP_400_BAD_REQUEST) + def test_missing_redis_document_deletes_all_documents(self): + doc_1 = self._create_document() + self._create_document() + self._create_document() + another_doc = self._create_document(party_code=2) + + self.assertEqual(Document.objects.count(), 4) + + url = reverse('documents-meta', kwargs={'doc_type': doc_1.doc_type, 'party_code': doc_1.party_code}) + self.client.force_authenticate(self.user) + + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + json_response = json.loads(response.content) + self.assertEqual(len(json_response), 3) + + # Delete file from Redis + doc_1.file.delete() + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + json_response = json.loads(response.content) + self.assertEqual(len(json_response), 0) + + # All files in for that doc_type/party_code/user were deleted + self.assertEqual(Document.objects.count(), 1) + self.assertEqual(Document.objects.first(), another_doc) + def _create_document(self, doc_type=None, party_code=None): if not doc_type: doc_type = self.default_doc_type diff --git a/edivorce/apps/core/views/api.py b/edivorce/apps/core/views/api.py index 92b48fee..b07854e1 100644 --- a/edivorce/apps/core/views/api.py +++ b/edivorce/apps/core/views/api.py @@ -69,7 +69,12 @@ class DocumentMetaDataView(ListAPIView): def get_queryset(self): doc_type = self.kwargs['doc_type'] party_code = self.kwargs['party_code'] - return Document.objects.filter(doc_type=doc_type, party_code=party_code, bceid_user=self.request.user).order_by('sort_order') + q = Document.objects.filter(doc_type=doc_type, party_code=party_code, bceid_user=self.request.user).order_by('sort_order') + for doc in q: + if not doc.file_exists(): + q.delete() + return Document.objects.none() + return q class DocumentView(RetrieveUpdateDestroyAPIView):