From 63a4e4c8a1da8f2c53e418247a0d44e9c9fcb474 Mon Sep 17 00:00:00 2001 From: bohare Date: Tue, 13 Sep 2016 10:56:14 +0000 Subject: [PATCH] Fixes #669, #670 and #671 --- .eslintrc | 14 ++++++++ cadasta/config/settings/default.py | 3 ++ cadasta/resources/models.py | 16 ++++++--- cadasta/resources/tests/files/invalidgpx.xml | 36 ++++++++++++++++++++ cadasta/resources/tests/test_models.py | 26 +++++++++++--- 5 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 .eslintrc create mode 100644 cadasta/resources/tests/files/invalidgpx.xml diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 000000000..a2cfcd483 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,14 @@ +{ + "extends": "airbnb", + "globals": { + "describe": true, + "it": true, + "expect": true, + "sinon": true, + "assert": true, + "beforeEach": true, + "afterEach": true + } +} + + diff --git a/cadasta/config/settings/default.py b/cadasta/config/settings/default.py index b80ffd1db..918c542ff 100644 --- a/cadasta/config/settings/default.py +++ b/cadasta/config/settings/default.py @@ -260,6 +260,8 @@ MIME_LOOKUPS = { 'application/pdf': 'pdf', 'audio/mpeg3': 'mp3', + 'audio/mpeg': 'mp3', + 'audio/mp3': 'mp3', 'audio/x-mpeg-3': 'mp3', 'video/mpeg': 'mp3', 'video/x-mpeg': 'mp3', @@ -272,6 +274,7 @@ 'application/vnd.openxmlformats-' 'officedocument.spreadsheetml.sheet': 'xlsx', 'text/xml': 'xml', + 'application/xml': 'xml', 'image/jpeg': 'jpg', 'image/png': 'png', 'image/gif': 'gif', diff --git a/cadasta/resources/models.py b/cadasta/resources/models.py index d63af3f30..f5a219380 100644 --- a/cadasta/resources/models.py +++ b/cadasta/resources/models.py @@ -8,6 +8,7 @@ from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.contrib.gis.db.models import GeometryCollectionField +from django.contrib.gis.gdal.error import GDALException from django.contrib.postgres.fields import JSONField from django.db import models from django.dispatch import receiver @@ -25,6 +26,8 @@ content_types = models.Q(app_label='organization', model='project') +GPX_MIME_TYPES = ('application/xml', 'text/xml', 'application/gpx+xml') + @permissioned_model class Resource(RandomIDModel): @@ -145,7 +148,7 @@ def create_thumbnails(sender, instance, created, **kwargs): @receiver(models.signals.post_save, sender=Resource) def create_spatial_resource(sender, instance, created, **kwargs): if created or instance._original_url != instance.file.url: - if 'xml' in instance.mime_type: + if instance.mime_type in GPX_MIME_TYPES: io.ensure_dirs() file_name = instance.file.url.split('/')[-1] write_path = os.path.join(settings.MEDIA_ROOT, @@ -157,9 +160,14 @@ def create_spatial_resource(sender, instance, created, **kwargs): # of gpx mime type is not reliable mime = magic.Magic(mime=True) mime_type = str(mime.from_file(write_path), 'utf-8') - if mime_type in ('application/xml', 'text/xml'): - processor = GPXProcessor(write_path) - layers = processor.get_layers() + if mime_type in GPX_MIME_TYPES: + try: + processor = GPXProcessor(write_path) + layers = processor.get_layers() + except GDALException: + raise InvalidGPXFile( + _('Invalid GPX file') + ) for layer in layers.keys(): if len(layers[layer]) > 0: SpatialResource.objects.create( diff --git a/cadasta/resources/tests/files/invalidgpx.xml b/cadasta/resources/tests/files/invalidgpx.xml new file mode 100644 index 000000000..12548bd4f --- /dev/null +++ b/cadasta/resources/tests/files/invalidgpx.xml @@ -0,0 +1,36 @@ + + + 2016-07-07T16:38:20.310-04 + 2016-07-07T16:39:23.673-04 + 2016-07-07 + 00:bb:3a:44:d0:fb + + <party_type>IN</party_type> + <party_name>Bilbo Baggins</party_name> + <location_geometry>40.6890612 -73.9925067 0.0 0.0;</location_geometry> + <location_type>MI</location_type> + <location_photo>test_image_one.png</location_photo> + <party_photo>test_image_two.png</party_photo> + <tenure_type>LH</tenure_type> + <location_attributes> + <name>Middle Earth</name> + </location_attributes> + <party_attributes_default> + <notes>Party attribute default notes.</notes> + </party_attributes_default> + <party_attributes_individual> + <gender>f</gender> + <homeowner>no</homeowner> + <dob>2016-07-07</dob> + </party_attributes_individual> + <party_relationship_attributes> + <notes>Party relationship notes.</notes> + </party_relationship_attributes> + <tenure_relationship_attributes> + <notes>Tenure relationship notes.</notes> + </tenure_relationship_attributes> + <meta> + <instanceID>uuid:b3f225d3-0fac-4a0b-80c7-60e6db4cc0ad</instanceID> + </meta> + </test_standard_questionnaire> diff --git a/cadasta/resources/tests/test_models.py b/cadasta/resources/tests/test_models.py index aade553de..cecedbb34 100644 --- a/cadasta/resources/tests/test_models.py +++ b/cadasta/resources/tests/test_models.py @@ -1,16 +1,17 @@ import os + import pytest from buckets.test.storage import FakeS3Storage +from core.tests.utils.cases import UserTestCase +from core.tests.utils.files import make_dirs # noqa from django.conf import settings from django.test import TestCase +from django.utils.translation import gettext as _ from ..exceptions import InvalidGPXFile from ..models import (ContentObject, Resource, create_spatial_resource, create_thumbnails) - -from core.tests.utils.cases import UserTestCase -from core.tests.utils.files import make_dirs # noqa from .factories import ResourceFactory, SpatialResourceFactory from .utils import clear_temp # noqa @@ -20,6 +21,7 @@ @pytest.mark.usefixtures('make_dirs') @pytest.mark.usefixtures('clear_temp') class ResourceTest(UserTestCase, TestCase): + def test_file_name_property(self): resource = Resource(file='http://example.com/dir/filename.txt') assert resource.file_name == 'filename.txt' @@ -208,7 +210,23 @@ def test_invalid_gpx_mime_type(self): ) with pytest.raises(InvalidGPXFile) as e: create_spatial_resource(Resource, resource, True) - assert str(e) == 'Invalid GPX mime type: audio/mpeg' + + assert str(e.value) == _('Invalid GPX mime type: audio/mpeg') + + def test_invalid_gpx_file(self): + storage = FakeS3Storage() + file = open(path + + '/resources/tests/files/invalidgpx.xml', 'rb').read() + file_name = storage.save('resources/invalidgpx.xml', file) + resource = ResourceFactory.build( + file=file_name, mime_type='application/xml') + assert os.path.isfile(os.path.join( + settings.MEDIA_ROOT, 's3/uploads/resources/invalidgpx.xml') + ) + with pytest.raises(InvalidGPXFile)as e: + create_spatial_resource(Resource, resource, True) + assert str(e.value) == _('Invalid GPX file') + assert Resource.objects.all().count() == 0 class SpatialResourceTest(UserTestCase, TestCase):