Skip to content

Commit

Permalink
- add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marthamareal committed Sep 28, 2022
1 parent 2287695 commit 4798ce5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 1 addition & 2 deletions geonode/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,8 +1741,7 @@ def save_thumbnail(self, filename, image):
url = storage_manager.url(upload_path)
try:
# Optimize the Thumbnail size and resolution
_default_thumb_size = getattr(
settings, 'THUMBNAIL_SIZE', {'width': 500, 'height': 200})
_default_thumb_size = settings.THUMBNAIL_SIZE
im = Image.open(storage_manager.open(actual_name))
im.thumbnail(
(_default_thumb_size['width'], _default_thumb_size['height']),
Expand Down
36 changes: 36 additions & 0 deletions geonode/documents/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from io import BytesIO

from unittest.mock import patch
from urllib.parse import urlparse

from django.urls import reverse
from django.conf import settings
Expand Down Expand Up @@ -95,6 +96,7 @@ def tearDownClass(cls):
def setUp(self):
super().setUp()
create_models('map')
self.project_root = os.path.abspath(os.path.dirname(__file__))
self.imgfile = io.BytesIO(
b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00'
b'\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;')
Expand Down Expand Up @@ -266,6 +268,40 @@ def test_replace_document(self):
# Remove document
d.delete()

def test_non_image_documents_thumbnail(self):
self.client.login(username='admin', password='admin')
try:
with open(os.path.join(f"{self.project_root}", "tests/data/text.txt"), "rb") as f:
data = {
'title': "Non img File Doc",
'doc_file': f,
'extension': 'txt'
}
self.client.post(reverse('document_upload'), data=data)
d = Document.objects.get(title='Non img File Doc')
self.assertIsNone(d.thumbnail_url)
finally:
Document.objects.filter(title='Non img File Doc').delete()

def test_image_documents_thumbnail(self):
self.client.login(username='admin', password='admin')
try:
with open(os.path.join(f"{self.project_root}", "tests/data/img.gif"), "rb") as f:
data = {
'title': "img File Doc",
'doc_file': f,
'extension': 'gif',
}
with self.settings(THUMBNAIL_SIZE={'width': 400, 'height': 200}):
self.client.post(reverse('document_upload'), data=data)
d = Document.objects.get(title='img File Doc')
self.assertIsNotNone(d.thumbnail_url)
thumb = urlparse(d.thumbnail_url).path
file = Image.open(f"geonode/{thumb}")
self.assertEqual(file.size, (400, 200))
finally:
Document.objects.filter(title='img File Doc').delete()

def test_upload_document_form_size_limit(self):
form_data = {
'title': 'GeoNode Map',
Expand Down
1 change: 1 addition & 0 deletions geonode/documents/tests/data/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST

0 comments on commit 4798ce5

Please sign in to comment.