Fix errors in collection views
[mediagoblin.git] / mediagoblin / tests / test_submission.py
index dec7118b76281ebef4d63b981f19c4c88e33b9bc..fc3d8c835b3739477b7301dc3b660783cf16b5c4 100644 (file)
@@ -1,5 +1,5 @@
 # GNU MediaGoblin -- federated, autonomous media hosting
-# Copyright (C) 2011 MediaGoblin contributors.  See AUTHORS.
+# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import sys
+reload(sys)
+sys.setdefaultencoding('utf-8')
+
 import urlparse
-import pkg_resources
+import os
 
-from nose.tools import assert_equal, assert_true, assert_false
+from nose.tools import assert_equal, assert_true
+from pkg_resources import resource_filename
 
-from mediagoblin.auth import lib as auth_lib
-from mediagoblin.tests.tools import setup_fresh_app, get_test_app
+from mediagoblin.tests.tools import get_app, \
+    fixture_add_user
 from mediagoblin import mg_globals
-from mediagoblin.tools import template, common
+from mediagoblin.db.models import MediaEntry
+from mediagoblin.tools import template
+from mediagoblin.media_types.image import MEDIA_MANAGER as img_MEDIA_MANAGER
+
+def resource(filename):
+    return resource_filename('mediagoblin.tests', 'test_submission/' + filename)
+
 
-GOOD_JPG = pkg_resources.resource_filename(
-  'mediagoblin.tests', 'test_submission/good.jpg')
-GOOD_PNG = pkg_resources.resource_filename(
-  'mediagoblin.tests', 'test_submission/good.png')
-EVIL_FILE = pkg_resources.resource_filename(
-  'mediagoblin.tests', 'test_submission/evil')
-EVIL_JPG = pkg_resources.resource_filename(
-  'mediagoblin.tests', 'test_submission/evil.jpg')
-EVIL_PNG = pkg_resources.resource_filename(
-  'mediagoblin.tests', 'test_submission/evil.png')
+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
 
-GOOD_TAG_STRING = 'yin,yang'
-BAD_TAG_STRING = 'rage,' + 'f' * 26 + 'u' * 26
+GOOD_TAG_STRING = u'yin,yang'
+BAD_TAG_STRING = unicode('rage,' + 'f' * 26 + 'u' * 26)
+
+FORM_CONTEXT = ['mediagoblin/submit/start.html', 'submit_form']
+REQUEST_CONTEXT = ['mediagoblin/user_pages/user.html', 'request']
 
 
 class TestSubmission:
     def setUp(self):
-        self.test_app = get_test_app()
+        self.test_app = get_app(dump_old_app=False)
 
         # TODO: Possibly abstract into a decorator like:
         # @as_authenticated_user('chris')
-        test_user = mg_globals.database.User()
-        test_user['username'] = u'chris'
-        test_user['email'] = u'chris@example.com'
-        test_user['email_verified'] = True
-        test_user['status'] = u'active'
-        test_user['pw_hash'] = auth_lib.bcrypt_gen_password_hash('toast')
-        test_user.save()
+        test_user = fixture_add_user()
 
         self.test_user = test_user
 
+        self.login()
+
+    def login(self):
         self.test_app.post(
             '/auth/login/', {
                 'username': u'chris',
                 'password': 'toast'})
 
-    def test_missing_fields(self):
-        # Test blank form
-        # ---------------
-        template.clear_test_template_context()
-        response = self.test_app.post(
-            '/submit/', {})
-        context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html']
-        form = context['submit_form']
-        assert form.file.errors == [u'You must provide a file.']
+    def logout(self):
+        self.test_app.get('/auth/logout/')
 
-        # Test blank file
-        # ---------------
+    def do_post(self, data, *context_keys, **kwargs):
+        url = kwargs.pop('url', '/submit/')
+        do_follow = kwargs.pop('do_follow', False)
         template.clear_test_template_context()
-        response = self.test_app.post(
-            '/submit/', {
-                'title': 'test title'})
-        context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html']
-        form = context['submit_form']
-        assert form.file.errors == [u'You must provide a file.']
+        response = self.test_app.post(url, data, **kwargs)
+        if do_follow:
+            response.follow()
+        context_data = template.TEMPLATE_TEST_CONTEXT
+        for key in context_keys:
+            context_data = context_data[key]
+        return response, context_data
 
+    def upload_data(self, filename):
+        return {'upload_files': [('file', filename)]}
 
-    def test_normal_uploads(self):
-        # Test JPG
-        # --------
-        template.clear_test_template_context()
-        response = self.test_app.post(
-            '/submit/', {
-                'title': 'Normal upload 1'
-                }, upload_files=[(
-                    'file', GOOD_JPG)])
-
-        # User should be redirected
-        response.follow()
-        assert_equal(
-            urlparse.urlsplit(response.location)[2],
-            '/u/chris/')
-        assert template.TEMPLATE_TEST_CONTEXT.has_key(
-            'mediagoblin/user_pages/user.html')
+    def check_comments(self, request, media_id, count):
+        comments = request.db.MediaComment.find({'media_entry': media_id})
+        assert_equal(count, len(list(comments)))
 
-        # Test PNG
-        # --------
-        template.clear_test_template_context()
-        response = self.test_app.post(
-            '/submit/', {
-                'title': 'Normal upload 2'
-                }, upload_files=[(
-                    'file', GOOD_PNG)])
+    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.'])
 
-        response.follow()
-        assert_equal(
-            urlparse.urlsplit(response.location)[2],
-            '/u/chris/')
-        assert template.TEMPLATE_TEST_CONTEXT.has_key(
-            'mediagoblin/user_pages/user.html')
+        # 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.'])
+
+    def check_url(self, response, path):
+        assert_equal(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)
+        # Make sure the media view is at least reachable, logged in...
+        url = '/u/{0}/m/{1}/'.format(self.test_user.username,
+                                     title.lower().replace(' ', '-'))
+        self.test_app.get(url)
+        # ... and logged out too.
+        self.logout()
+        self.test_app.get(url)
+
+    def test_normal_jpg(self):
+        self.check_normal_upload(u'Normal upload 1', GOOD_JPG)
+
+    def test_normal_png(self):
+        self.check_normal_upload(u'Normal upload 2', GOOD_PNG)
+
+    def check_media(self, request, find_data, count=None):
+        media = MediaEntry.find(find_data)
+        if count is not None:
+            assert_equal(media.count(), count)
+            if count == 0:
+                return
+        return media[0]
 
     def test_tags(self):
         # Good tag string
         # --------
-        template.clear_test_template_context()
-        response = self.test_app.post(
-            '/submit/', {
-                'title': 'Balanced Goblin',
-                'tags': GOOD_TAG_STRING
-                }, upload_files=[(
-                    'file', GOOD_JPG)])
+        response, request = self.do_post({'title': u'Balanced Goblin 2',
+                                          'tags': GOOD_TAG_STRING},
+                                         *REQUEST_CONTEXT, do_follow=True,
+                                         **self.upload_data(GOOD_JPG))
+        media = self.check_media(request, {'title': u'Balanced Goblin 2'}, 1)
+        assert media.tags[0]['name'] == u'yin'
+        assert media.tags[0]['slug'] == u'yin'
 
-        # New media entry with correct tags should be created
-        response.follow()
-        context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/user_pages/user.html']
-        request = context['request']
-        media = request.db.MediaEntry.find({'title': 'Balanced Goblin'})[0]
-        assert_equal(media['tags'],
-                     [{'name': u'yin', 'slug': u'yin'},
-                                            {'name': u'yang', 'slug': u'yang'}])
+        assert media.tags[1]['name'] == u'yang'
+        assert media.tags[1]['slug'] == u'yang'
 
         # Test tags that are too long
         # ---------------
-        template.clear_test_template_context()
-        response = self.test_app.post(
-            '/submit/', {
-                'title': 'Balanced Goblin',
-                'tags': BAD_TAG_STRING
-                }, upload_files=[(
-                    'file', GOOD_JPG)])
-
-        # Too long error should be raised
-        context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html']
-        form = context['submit_form']
-        assert form.tags.errors == [
-            u'Tags must be shorter than 50 characters.  Tags that are too long'\
-             ': ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu']
+        response, form = self.do_post({'title': u'Balanced Goblin 2',
+                                       'tags': BAD_TAG_STRING},
+                                      *FORM_CONTEXT,
+                                      **self.upload_data(GOOD_JPG))
+        assert_equal(form.tags.errors, [
+                u'Tags must be shorter than 50 characters.  ' \
+                    'Tags that are too long: ' \
+                    'ffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuu'])
 
     def test_delete(self):
-        template.clear_test_template_context()
-        response = self.test_app.post(
-            '/submit/', {
-                'title': 'Balanced Goblin',
-                }, upload_files=[(
-                    'file', GOOD_JPG)])
-
-        # Post image
-        response.follow()
-
-        request = template.TEMPLATE_TEST_CONTEXT[
-            'mediagoblin/user_pages/user.html']['request']
-
-        media = request.db.MediaEntry.find({'title': 'Balanced Goblin'})[0]
-
-        # Does media entry exist?
-        assert_true(media)
+        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)
+        media_id = media.id
+
+        # render and post to the edit page.
+        edit_url = request.urlgen(
+            'mediagoblin.edit.edit_media',
+            user=self.test_user.username, media_id=media_id)
+        self.test_app.get(edit_url)
+        self.test_app.post(edit_url,
+            {'title': u'Balanced Goblin',
+             'slug': u"Balanced=Goblin",
+             'tags': u''})
+        media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
+        assert_equal(media.slug, u"balanced-goblin")
+
+        # Add a comment, so we can test for its deletion later.
+        self.check_comments(request, media_id, 0)
+        comment_url = request.urlgen(
+            'mediagoblin.user_pages.media_post_comment',
+            user=self.test_user.username, media_id=media_id)
+        response = self.do_post({'comment_content': 'i love this test'},
+                                url=comment_url, do_follow=True)[0]
+        self.check_comments(request, media_id, 1)
 
         # Do not confirm deletion
         # ---------------------------------------------------
-        response = self.test_app.post(
-            request.urlgen('mediagoblin.user_pages.media_confirm_delete',
-                           # No work: user=media.uploader().username,
-                           user=self.test_user['username'],
-                           media=media._id),
-            # no value means no confirm
-            {})
-
-        response.follow()
-
-        request = template.TEMPLATE_TEST_CONTEXT[
-            'mediagoblin/user_pages/user.html']['request']
-
-        media = request.db.MediaEntry.find({'title': 'Balanced Goblin'})[0]
-
-        # Does media entry still exist?
-        assert_true(media)
+        delete_url = request.urlgen(
+            'mediagoblin.user_pages.media_confirm_delete',
+            user=self.test_user.username, media_id=media_id)
+        # Empty data means don't confirm
+        response = self.do_post({}, do_follow=True, url=delete_url)[0]
+        media = self.check_media(request, {'title': u'Balanced Goblin'}, 1)
+        media_id = media.id
 
         # Confirm deletion
         # ---------------------------------------------------
-        response = self.test_app.post(
-            request.urlgen('mediagoblin.user_pages.media_confirm_delete',
-                           # No work: user=media.uploader().username,
-                           user=self.test_user['username'],
-                           media=media._id),
-            {'confirm': 'y'})
-
-        response.follow()
+        response, request = self.do_post({'confirm': 'y'}, *REQUEST_CONTEXT,
+                                         do_follow=True, url=delete_url)
+        self.check_media(request, {'id': media_id}, 0)
+        self.check_comments(request, media_id, 0)
 
-        request = template.TEMPLATE_TEST_CONTEXT[
-            'mediagoblin/user_pages/user.html']['request']
-
-        # Does media entry still exist?
-        assert_false(
-            request.db.MediaEntry.find(
-                {'_id': media._id}).count())
-
-    def test_malicious_uploads(self):
+    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 'Sorry, I don\'t support that file type :(' == \
+                str(form.file.errors[0])
+
+
+    def test_get_media_manager(self):
+        """Test if the get_media_manger function returns sensible things
+        """
+        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)
+
+
+    def test_sniffing(self):
+        '''
+        Test sniffing mechanism to assert that regular uploads work as intended
+        '''
         template.clear_test_template_context()
         response = self.test_app.post(
             '/submit/', {
-                'title': 'Malicious Upload 1'
+                'title': u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE'
                 }, upload_files=[(
-                    'file', EVIL_FILE)])
+                    'file', GOOD_JPG)])
 
-        context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/submit/start.html']
-        form = context['submit_form']
-        assert form.file.errors == ['The file doesn\'t seem to be an image!']
+        response.follow()
+
+        context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/user_pages/user.html']
+
+        request = context['request']
 
+        media = request.db.MediaEntry.find_one({
+            u'title': u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE'})
+
+        assert media.media_type == 'mediagoblin.media_types.image'
+
+    def check_false_image(self, title, filename):
         # NOTE: The following 2 tests will ultimately fail, but they
         #   *will* pass the initial form submission step.  Instead,
         #   they'll be caught as failures during the processing step.
-
+        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))
+        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 non-supported file with .jpg extension
         # -------------------------------------------
-        template.clear_test_template_context()
-        response = self.test_app.post(
-           '/submit/', {
-               'title': 'Malicious Upload 2'
-               }, upload_files=[(
-                   'file', EVIL_JPG)])
-        response.follow()
-        assert_equal(
-            urlparse.urlsplit(response.location)[2],
-            '/u/chris/')
-
-        entry = mg_globals.database.MediaEntry.find_one(
-            {'title': 'Malicious Upload 2'})
-        assert_equal(entry['state'], 'failed')
-        assert_equal(
-            entry['fail_error'],
-            u'mediagoblin.process_media.errors:BadMediaFail')
+        self.check_false_image(u'Malicious Upload 2', EVIL_JPG)
 
+    def test_evil_png(self):
         # Test non-supported file with .png extension
         # -------------------------------------------
-        template.clear_test_template_context()
-        response = self.test_app.post(
-           '/submit/', {
-               'title': 'Malicious Upload 3'
-               }, upload_files=[(
-                   'file', EVIL_PNG)])
-        response.follow()
-        assert_equal(
-            urlparse.urlsplit(response.location)[2],
-            '/u/chris/')
-
-        entry = mg_globals.database.MediaEntry.find_one(
-            {'title': 'Malicious Upload 3'})
-        assert_equal(entry['state'], 'failed')
-        assert_equal(
-            entry['fail_error'],
-            u'mediagoblin.process_media.errors:BadMediaFail')
+        self.check_false_image(u'Malicious Upload 3', EVIL_PNG)
+
+    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):
+        data = {'title': u'Big Blue'}
+        response, request = self.do_post(data, *REQUEST_CONTEXT, do_follow=True,
+                                         **self.upload_data(BIG_BLUE))
+        media = self.check_media(request, data, 1)
+        last_size = 1024 ** 3  # Needs to be larger than bigblue.png
+        for key, basename in (('original', 'bigblue.png'),
+                              ('medium', 'bigblue.medium.png'),
+                              ('thumb', 'bigblue.thumbnail.png')):
+            # Does the processed image have a good filename?
+            filename = resource_filename(
+                'mediagoblin.tests',
+                os.path.join('test_user_dev/media/public',
+                             *media.media_files.get(key, [])))
+            assert_true(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)
+            last_size = size