Browse Source

Add get file test. Delete all documents for a type if file is not in redis

pull/170/head
ariannedee 5 years ago
parent
commit
8b7a50ecf2
3 changed files with 56 additions and 2 deletions
  1. +4
    -1
      edivorce/apps/core/models.py
  2. +46
    -0
      edivorce/apps/core/tests/test_api.py
  3. +6
    -1
      edivorce/apps/core/views/api.py

+ 4
- 1
edivorce/apps/core/models.py View File

@ -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:


+ 46
- 0
edivorce/apps/core/tests/test_api.py View File

@ -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


+ 6
- 1
edivorce/apps/core/views/api.py View File

@ -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):


Loading…
Cancel
Save