Committing present MediaGoblin translations before pushing extracted messages
[mediagoblin.git] / mediagoblin / tests / test_submission.py
index ddb8cefd51e03fdcbedd1fe59b2b4c7a5ad9c10a..162b2d195f09eb762124507b380950ca294b76d1 100644 (file)
@@ -20,27 +20,17 @@ sys.setdefaultencoding('utf-8')
 
 import urlparse
 import os
-
-from nose.tools import assert_equal, assert_true
-from pkg_resources import resource_filename
+import pytest
 
 from mediagoblin.tests.tools import fixture_add_user
 from mediagoblin import mg_globals
 from mediagoblin.db.models import MediaEntry
 from mediagoblin.tools import template
 from mediagoblin.media_types.image import MEDIA_MANAGER as img_MEDIA_MANAGER
+from mediagoblin.media_types.pdf.processing import check_prerequisites as pdf_check_prerequisites
 
-def resource(filename):
-    return resource_filename('mediagoblin.tests', 'test_submission/' + filename)
-
-
-GOOD_JPG = resource('good.jpg')
-GOOD_PNG = resource('good.png')
-EVIL_FILE = resource('evil')
-EVIL_JPG = resource('evil.jpg')
-EVIL_PNG = resource('evil.png')
-BIG_BLUE = resource('bigblue.png')
-from .test_exif import GPS_JPG
+from .resources import GOOD_JPG, GOOD_PNG, EVIL_FILE, EVIL_JPG, EVIL_PNG, \
+    BIG_BLUE, GOOD_PDF, GPS_JPG
 
 GOOD_TAG_STRING = u'yin,yang'
 BAD_TAG_STRING = unicode('rage,' + 'f' * 26 + 'u' * 26)
@@ -50,7 +40,8 @@ REQUEST_CONTEXT = ['mediagoblin/user_pages/user.html', 'request']
 
 
 class TestSubmission:
-    def _setup(self, test_app):
+    @pytest.fixture(autouse=True)
+    def setup(self, test_app):
         self.test_app = test_app
 
         # TODO: Possibly abstract into a decorator like:
@@ -87,29 +78,27 @@ class TestSubmission:
 
     def check_comments(self, request, media_id, count):
         comments = request.db.MediaComment.find({'media_entry': media_id})
-        assert_equal(count, len(list(comments)))
-
-    def test_missing_fields(self, test_app):
-        self._setup(test_app)
+        assert count == len(list(comments))
 
+    def test_missing_fields(self):
         # Test blank form
         # ---------------
         response, form = self.do_post({}, *FORM_CONTEXT)
-        assert_equal(form.file.errors, [u'You must provide a file.'])
+        assert form.file.errors == [u'You must provide a file.']
 
         # Test blank file
         # ---------------
         response, form = self.do_post({'title': u'test title'}, *FORM_CONTEXT)
-        assert_equal(form.file.errors, [u'You must provide a file.'])
+        assert form.file.errors == [u'You must provide a file.']
 
     def check_url(self, response, path):
-        assert_equal(urlparse.urlsplit(response.location)[2], path)
+        assert urlparse.urlsplit(response.location)[2] == path
 
     def check_normal_upload(self, title, filename):
         response, context = self.do_post({'title': title}, do_follow=True,
                                          **self.upload_data(filename))
         self.check_url(response, '/u/{0}/'.format(self.test_user.username))
-        assert_true('mediagoblin/user_pages/user.html' in context)
+        assert 'mediagoblin/user_pages/user.html' in context
         # Make sure the media view is at least reachable, logged in...
         url = '/u/{0}/m/{1}/'.format(self.test_user.username,
                                      title.lower().replace(' ', '-'))
@@ -118,25 +107,29 @@ class TestSubmission:
         self.logout()
         self.test_app.get(url)
 
-    def test_normal_jpg(self, test_app):
-        self._setup(test_app)
+    def test_normal_jpg(self):
         self.check_normal_upload(u'Normal upload 1', GOOD_JPG)
 
-    def test_normal_png(self, test_app):
-        self._setup(test_app)
+    def test_normal_png(self):
         self.check_normal_upload(u'Normal upload 2', GOOD_PNG)
 
+    @pytest.mark.skipif("not pdf_check_prerequisites()")
+    def test_normal_pdf(self):
+        response, context = self.do_post({'title': u'Normal upload 3 (pdf)'},
+                                         do_follow=True,
+                                         **self.upload_data(GOOD_PDF))
+        self.check_url(response, '/u/{0}/'.format(self.test_user.username))
+        assert 'mediagoblin/user_pages/user.html' in context
+
     def check_media(self, request, find_data, count=None):
         media = MediaEntry.find(find_data)
         if count is not None:
-            assert_equal(media.count(), count)
+            assert media.count() == count
             if count == 0:
                 return
         return media[0]
 
-    def test_tags(self, test_app):
-        self._setup(test_app)
-
+    def test_tags(self):
         # Good tag string
         # --------
         response, request = self.do_post({'title': u'Balanced Goblin 2',
@@ -156,14 +149,12 @@ class TestSubmission:
                                        'tags': BAD_TAG_STRING},
                                       *FORM_CONTEXT,
                                       **self.upload_data(GOOD_JPG))
-        assert_equal(form.tags.errors, [
+        assert form.tags.errors == [
                 u'Tags must be shorter than 50 characters.  ' \
                     'Tags that are too long: ' \
-                    'ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu'])
-
-    def test_delete(self, test_app):
-        self._setup(test_app)
+                    'ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu']
 
+    def test_delete(self):
         response, request = self.do_post({'title': u'Balanced Goblin'},
                                          *REQUEST_CONTEXT, do_follow=True,
                                          **self.upload_data(GOOD_JPG))
@@ -180,7 +171,7 @@ class TestSubmission:
              'slug': u"Balanced=Goblin",
              'tags': u''})
         media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
-        assert_equal(media.slug, u"balanced-goblin")
+        assert media.slug == u"balanced-goblin"
 
         # Add a comment, so we can test for its deletion later.
         self.check_comments(request, media_id, 0)
@@ -208,39 +199,34 @@ class TestSubmission:
         self.check_media(request, {'id': media_id}, 0)
         self.check_comments(request, media_id, 0)
 
-    def test_evil_file(self, test_app):
-        self._setup(test_app)
-
+    def test_evil_file(self):
         # Test non-suppoerted file with non-supported extension
         # -----------------------------------------------------
         response, form = self.do_post({'title': u'Malicious Upload 1'},
                                       *FORM_CONTEXT,
                                       **self.upload_data(EVIL_FILE))
-        assert_equal(len(form.file.errors), 1)
+        assert len(form.file.errors) == 1
         assert 'Sorry, I don\'t support that file type :(' == \
                 str(form.file.errors[0])
 
 
-    def test_get_media_manager(self, test_app):
+    def test_get_media_manager(self):
         """Test if the get_media_manger function returns sensible things
         """
-        self._setup(test_app)
-
         response, request = self.do_post({'title': u'Balanced Goblin'},
                                          *REQUEST_CONTEXT, do_follow=True,
                                          **self.upload_data(GOOD_JPG))
         media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
 
-        assert_equal(media.media_type, u'mediagoblin.media_types.image')
-        assert_equal(media.media_manager, img_MEDIA_MANAGER)
+        assert media.media_type == u'mediagoblin.media_types.image'
+        assert isinstance(media.media_manager, img_MEDIA_MANAGER)
+        assert media.media_manager.entry == media
 
 
-    def test_sniffing(self, test_app):
+    def test_sniffing(self):
         '''
         Test sniffing mechanism to assert that regular uploads work as intended
         '''
-        self._setup(test_app)
-
         template.clear_test_template_context()
         response = self.test_app.post(
             '/submit/', {
@@ -267,33 +253,25 @@ class TestSubmission:
                                          **self.upload_data(filename))
         self.check_url(response, '/u/{0}/'.format(self.test_user.username))
         entry = mg_globals.database.MediaEntry.find_one({'title': title})
-        assert_equal(entry.state, 'failed')
-        assert_equal(entry.fail_error, u'mediagoblin.processing:BadMediaFail')
-
-    def test_evil_jpg(self, test_app):
-        self._setup(test_app)
+        assert entry.state == 'failed'
+        assert entry.fail_error == u'mediagoblin.processing:BadMediaFail'
 
+    def test_evil_jpg(self):
         # Test non-supported file with .jpg extension
         # -------------------------------------------
         self.check_false_image(u'Malicious Upload 2', EVIL_JPG)
 
-    def test_evil_png(self, test_app):
-        self._setup(test_app)
-
+    def test_evil_png(self):
         # Test non-supported file with .png extension
         # -------------------------------------------
         self.check_false_image(u'Malicious Upload 3', EVIL_PNG)
 
-    def test_media_data(self, test_app):
-        self._setup(test_app)
-
+    def test_media_data(self):
         self.check_normal_upload(u"With GPS data", GPS_JPG)
         media = self.check_media(None, {"title": u"With GPS data"}, 1)
-        assert_equal(media.media_data.gps_latitude, 59.336666666666666)
-
-    def test_processing(self, test_app):
-        self._setup(test_app)
+        assert media.media_data.gps_latitude == 59.336666666666666
 
+    def test_processing(self):
         public_store_dir = mg_globals.global_config[
             'storage:publicstore']['base_dir']
 
@@ -308,9 +286,9 @@ class TestSubmission:
             # Does the processed image have a good filename?
             filename = os.path.join(
                 public_store_dir,
-                *media.media_files.get(key, []))
-            assert_true(filename.endswith('_' + basename))
+                *media.media_files[key])
+            assert filename.endswith('_' + basename)
             # Is it smaller than the last processed image we looked at?
             size = os.stat(filename).st_size
-            assert_true(last_size > size)
+            assert last_size > size
             last_size = size