diff options
Diffstat (limited to 'mediagoblin/tests/test_submission.py')
-rw-r--r-- | mediagoblin/tests/test_submission.py | 271 |
1 files changed, 240 insertions, 31 deletions
diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py index b5b13ed3..f51b132c 100644 --- a/mediagoblin/tests/test_submission.py +++ b/mediagoblin/tests/test_submission.py @@ -14,17 +14,46 @@ # 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') +## Optional audio/video stuff + +SKIP_AUDIO = False +SKIP_VIDEO = False + +try: + import gi.repository.Gst + # this gst initialization stuff is really required here + import gi + gi.require_version('Gst', '1.0') + from gi.repository import Gst + Gst.init(None) + from .media_tools import create_av +except ImportError: + SKIP_AUDIO = True + SKIP_VIDEO = True + +try: + import scikits.audiolab +except ImportError: + SKIP_AUDIO = True + +import six + +if six.PY2: # this hack only work in Python 2 + import sys + reload(sys) + sys.setdefaultencoding('utf-8') -import urlparse import os import pytest +import webtest.forms +import pkg_resources -from mediagoblin.tests.tools import fixture_add_user +import six.moves.urllib.parse as urlparse + +from mediagoblin.tests.tools import ( + fixture_add_user, fixture_add_collection, get_app) from mediagoblin import mg_globals -from mediagoblin.db.models import MediaEntry, User +from mediagoblin.db.models import MediaEntry, User, LocalUser, Activity from mediagoblin.db.base import Session from mediagoblin.tools import template from mediagoblin.media_types.image import ImageMediaManager @@ -34,13 +63,46 @@ from .resources import GOOD_JPG, GOOD_PNG, EVIL_FILE, EVIL_JPG, EVIL_PNG, \ BIG_BLUE, GOOD_PDF, GPS_JPG, MED_PNG, BIG_PNG GOOD_TAG_STRING = u'yin,yang' -BAD_TAG_STRING = unicode('rage,' + 'f' * 26 + 'u' * 26) +BAD_TAG_STRING = six.text_type('rage,' + 'f' * 26 + 'u' * 26) FORM_CONTEXT = ['mediagoblin/submit/start.html', 'submit_form'] REQUEST_CONTEXT = ['mediagoblin/user_pages/user.html', 'request'] -class TestSubmission: +@pytest.fixture() +def audio_plugin_app(request): + return get_app( + request, + mgoblin_config=pkg_resources.resource_filename( + 'mediagoblin.tests', + 'test_mgoblin_app_audio.ini')) + +@pytest.fixture() +def video_plugin_app(request): + return get_app( + request, + mgoblin_config=pkg_resources.resource_filename( + 'mediagoblin.tests', + 'test_mgoblin_app_video.ini')) + +@pytest.fixture() +def audio_video_plugin_app(request): + return get_app( + request, + mgoblin_config=pkg_resources.resource_filename( + 'mediagoblin.tests', + 'test_mgoblin_app_audio_video.ini')) + +@pytest.fixture() +def pdf_plugin_app(request): + return get_app( + request, + mgoblin_config=pkg_resources.resource_filename( + 'mediagoblin.tests', + 'test_mgoblin_app_pdf.ini')) + + +class BaseTestSubmission: @pytest.fixture(autouse=True) def setup(self, test_app): self.test_app = test_app @@ -61,7 +123,7 @@ class TestSubmission: #### totally stupid. #### Also if we found a way to make this run it should be a #### property. - return User.query.filter(User.username==u'chris').first() + return LocalUser.query.filter(LocalUser.username==u'chris').first() def login(self): self.test_app.post( @@ -88,19 +150,14 @@ class TestSubmission: return {'upload_files': [('file', filename)]} def check_comments(self, request, media_id, count): - comments = request.db.MediaComment.query.filter_by(media_entry=media_id) - assert count == len(list(comments)) - - def test_missing_fields(self): - # Test blank form - # --------------- - response, form = self.do_post({}, *FORM_CONTEXT) - 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 form.file.errors == [u'You must provide a file.'] + gmr = request.db.GenericModelReference.query.filter_by( + obj_pk=media_id, + model_type=request.db.MediaEntry.__tablename__ + ).first() + if gmr is None and count <= 0: + return # Yerp it's fine. + comments = request.db.Comment.query.filter_by(target_id=gmr.id) + assert count == comments.count() def check_url(self, response, path): assert urlparse.urlsplit(response.location)[2] == path @@ -129,6 +186,19 @@ class TestSubmission: our_user.save() Session.expunge(our_user) + +class TestSubmissionBasics(BaseTestSubmission): + def test_missing_fields(self): + # Test blank form + # --------------- + response, form = self.do_post({}, *FORM_CONTEXT) + 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 form.file.errors == [u'You must provide a file.'] + def test_normal_jpg(self): # User uploaded should be 0 assert self.our_user().uploaded == 0 @@ -142,17 +212,19 @@ class TestSubmission: # Reload user assert self.our_user().uploaded == file_size + def test_public_id_populated(self): + # Upload the image first. + 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) + + # Now check that the public_id attribute is set. + assert media.public_id != None + 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.our_user().username)) - assert 'mediagoblin/user_pages/user.html' in context - def test_default_upload_limits(self): self.user_upload_limits(uploaded=500) @@ -359,7 +431,7 @@ class TestSubmission: 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 media.media_data.gps_latitude == 59.336666666666666 + assert media.get_location.position["latitude"] == 59.336666666666666 def test_processing(self): public_store_dir = mg_globals.global_config[ @@ -382,3 +454,140 @@ class TestSubmission: size = os.stat(filename).st_size assert last_size > size last_size = size + + def test_collection_selection(self): + """Test the ability to choose a collection when submitting media + """ + # Collection option should have been removed if the user has no + # collections. + response = self.test_app.get('/submit/') + assert 'collection' not in response.form.fields + + # Test upload of an image when a user has no collections. + upload = webtest.forms.Upload(os.path.join( + 'mediagoblin', 'static', 'images', 'media_thumbs', 'image.png')) + response.form['file'] = upload + no_collection_title = 'no collection' + response.form['title'] = no_collection_title + response.form.submit() + assert MediaEntry.query.filter_by( + actor=self.our_user().id + ).first().title == no_collection_title + + # Collection option should be present if the user has collections. It + # shouldn't allow other users' collections to be selected. + col = fixture_add_collection(user=self.our_user()) + user = fixture_add_user(username=u'different') + fixture_add_collection(user=user, name=u'different') + response = self.test_app.get('/submit/') + form = response.form + assert 'collection' in form.fields + # Option length is 2, because of the default "--Select--" option + assert len(form['collection'].options) == 2 + assert form['collection'].options[1][2] == col.title + + # Test that if we specify a collection then the media entry is added to + # the specified collection. + form['file'] = upload + title = 'new picture' + form['title'] = title + form['collection'] = form['collection'].options[1][0] + form.submit() + # The title of the first item in our user's first collection should + # match the title of the picture that was just added. + col = self.our_user().collections[0] + assert col.collection_items[0].get_object().title == title + + # Test that an activity was created when the item was added to the + # collection. That should be the last activity. + assert Activity.query.order_by( + Activity.id.desc() + ).first().content == '{0} added new picture to {1}'.format( + self.our_user().username, col.title) + + # Test upload succeeds if the user has collection and no collection is + # chosen. + form['file'] = webtest.forms.Upload(os.path.join( + 'mediagoblin', 'static', 'images', 'media_thumbs', 'image.png')) + title = 'no collection 2' + form['title'] = title + form['collection'] = form['collection'].options[0][0] + form.submit() + # The title of the first item in our user's first collection should + # match the title of the picture that was just added. + assert MediaEntry.query.filter_by( + actor=self.our_user().id + ).count() == 3 + +class TestSubmissionVideo(BaseTestSubmission): + @pytest.fixture(autouse=True) + def setup(self, video_plugin_app): + self.test_app = video_plugin_app + + # TODO: Possibly abstract into a decorator like: + # @as_authenticated_user('chris') + fixture_add_user(privileges=[u'active',u'uploader', u'commenter']) + + self.login() + + @pytest.mark.skipif(SKIP_VIDEO, + reason="Dependencies for video not met") + def test_video(self, video_plugin_app): + with create_av(make_video=True) as path: + self.check_normal_upload('Video', path) + + +class TestSubmissionAudio(BaseTestSubmission): + @pytest.fixture(autouse=True) + def setup(self, audio_plugin_app): + self.test_app = audio_plugin_app + + # TODO: Possibly abstract into a decorator like: + # @as_authenticated_user('chris') + fixture_add_user(privileges=[u'active',u'uploader', u'commenter']) + + self.login() + + @pytest.mark.skipif(SKIP_AUDIO, + reason="Dependencies for audio not met") + def test_audio(self, audio_plugin_app): + with create_av(make_audio=True) as path: + self.check_normal_upload('Audio', path) + + +class TestSubmissionAudioVideo(BaseTestSubmission): + @pytest.fixture(autouse=True) + def setup(self, audio_video_plugin_app): + self.test_app = audio_video_plugin_app + + # TODO: Possibly abstract into a decorator like: + # @as_authenticated_user('chris') + fixture_add_user(privileges=[u'active',u'uploader', u'commenter']) + + self.login() + + @pytest.mark.skipif(SKIP_AUDIO or SKIP_VIDEO, + reason="Dependencies for audio or video not met") + def test_audio_and_video(self): + with create_av(make_audio=True, make_video=True) as path: + self.check_normal_upload('Audio and Video', path) + + +class TestSubmissionPDF(BaseTestSubmission): + @pytest.fixture(autouse=True) + def setup(self, pdf_plugin_app): + self.test_app = pdf_plugin_app + + # TODO: Possibly abstract into a decorator like: + # @as_authenticated_user('chris') + fixture_add_user(privileges=[u'active',u'uploader', u'commenter']) + + self.login() + + @pytest.mark.skipif("not os.path.exists(GOOD_PDF) or 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.our_user().username)) + assert 'mediagoblin/user_pages/user.html' in context |