Browse Source

Merge pull request #132 from bcgov/DIV-1168

DIV-1168: Only one PDF can be uploaded per form
pull/172/head
Arianne 5 years ago
committed by GitHub
parent
commit
b8824c5485
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 13 deletions
  1. +5
    -1
      edivorce/apps/core/models.py
  2. +11
    -4
      edivorce/apps/core/serializer.py
  3. +62
    -8
      edivorce/apps/core/tests/test_api.py

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

@ -213,7 +213,11 @@ class Document(models.Model):
content_type = content_types.get(extension)
if not content_type:
return "application/unknown"
return content_type
return content_type
@property
def is_pdf(self):
return self.filename.split('.')[-1].lower() == 'pdf'
class DontLog:


+ 11
- 4
edivorce/apps/core/serializer.py View File

@ -42,13 +42,20 @@ class CreateDocumentSerializer(serializers.ModelSerializer):
filename = validated_data['file'].name
size = validated_data['file'].size
user = self.context['request'].user
order = Document.objects.filter(bceid_user=user, doc_type=validated_data['doc_type'], party_code=validated_data['party_code']).count() + 1
response = Document(bceid_user=user, filename=filename, size=size, sort_order=order, **validated_data)
existing_docs = Document.objects.filter(bceid_user=user, doc_type=validated_data['doc_type'], party_code=validated_data['party_code'])
for other_doc in existing_docs:
if other_doc.is_pdf:
raise ValidationError("PDF documents cannot be combined with images. Only a single PDF or multiple images can be uploaded into one form.")
sort_order = existing_docs.count() + 1
document = Document(bceid_user=user, filename=filename, size=size, sort_order=sort_order, **validated_data)
if document.is_pdf and existing_docs.count() > 0:
raise ValidationError("Only one PDF is allowed per form, and PDF documents cannot be combined with images.")
try:
response.save()
document.save()
except IntegrityError:
raise ValidationError("This file appears to have already been uploaded for this document. Duplicate filename: " + filename)
return response
return document
class DocumentMetadataSerializer(serializers.ModelSerializer):


+ 62
- 8
edivorce/apps/core/tests/test_api.py View File

@ -40,7 +40,8 @@ class MockRedis:
@mock.patch.object(Redis, 'get', MockRedis.get)
@mock.patch.object(Redis, 'delete', MockRedis.delete)
@mock.patch.object(Redis, 'exists', MockRedis.exists)
@modify_settings(MIDDLEWARE={'remove': 'edivorce.apps.core.middleware.bceid_middleware.BceidMiddleware', })
@modify_settings(MIDDLEWARE={'remove': 'edivorce.apps.core.middleware.bceid_middleware.BceidMiddleware'})
@override_settings(CLAMAV_ENABLED=False)
class APITest(APITestCase):
def setUp(self):
self.user = BceidUser.objects.create(user_guid='1234')
@ -73,8 +74,7 @@ class APITest(APITestCase):
}
self.client.force_authenticate(self.user)
with self.settings(CLAMAV_ENABLED=False):
response = self.client.post(url, data)
response = self.client.post(url, data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
@ -101,21 +101,75 @@ class APITest(APITestCase):
}
self.client.force_authenticate(self.user)
with self.settings(CLAMAV_ENABLED=False):
response = self.client.post(url, data)
response = self.client.post(url, data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Document.objects.count(), 1)
file.seek(0) #
file.seek(0)
with self.settings(CLAMAV_ENABLED=False):
response = self.client.post(url, data)
response = self.client.post(url, data)
self.assertContains(response,
'This file appears to have already been uploaded for this document.',
status_code=status.HTTP_400_BAD_REQUEST)
def test_post_only_one_pdf(self):
url = reverse('documents')
file = _create_file()
data = {
'file': file,
'doc_type': 'AAI',
'party_code': 1
}
self.client.force_authenticate(self.user)
response = self.client.post(url, data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Document.objects.count(), 1)
file = _create_file(extension='pdf')
data = {
'file': file,
'doc_type': 'AAI',
'party_code': 1
}
response = self.client.post(url, data)
self.assertContains(response,
"Only one PDF is allowed per form, and PDF documents cannot be combined with images.",
status_code=status.HTTP_400_BAD_REQUEST)
self.assertEqual(Document.objects.count(), 1)
def test_post_no_existing_pdfs(self):
url = reverse('documents')
file = _create_file(extension='pdf')
data = {
'file': file,
'doc_type': 'AAI',
'party_code': 1
}
self.client.force_authenticate(self.user)
response = self.client.post(url, data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Document.objects.count(), 1)
file = _create_file()
data = {
'file': file,
'doc_type': 'AAI',
'party_code': 1
}
response = self.client.post(url, data)
self.assertContains(response,
"PDF documents cannot be combined with images. Only a single PDF or multiple images can be uploaded into one form.",
status_code=status.HTTP_400_BAD_REQUEST)
self.assertEqual(Document.objects.count(), 1)
def test_post_field_validation(self):
url = reverse('documents')


Loading…
Cancel
Save