From 414c682fb4a5b684cf259743a77e6e569395c16d Mon Sep 17 00:00:00 2001 From: tilly-Q Date: Wed, 14 May 2014 12:16:03 -0400 Subject: [PATCH] Added some tests for metadata. --- mediagoblin/tests/test_edit.py | 84 ++++++++++++++++++++++++++++-- mediagoblin/tests/test_metadata.py | 25 --------- 2 files changed, 81 insertions(+), 28 deletions(-) diff --git a/mediagoblin/tests/test_edit.py b/mediagoblin/tests/test_edit.py index 4f44e0b9..dc9c422f 100644 --- a/mediagoblin/tests/test_edit.py +++ b/mediagoblin/tests/test_edit.py @@ -14,11 +14,11 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import urlparse +import urlparse, os, pytest from mediagoblin import mg_globals -from mediagoblin.db.models import User -from mediagoblin.tests.tools import fixture_add_user +from mediagoblin.db.models import User, MediaEntry +from mediagoblin.tests.tools import fixture_add_user, fixture_media_entry from mediagoblin import auth from mediagoblin.tools import template, mail @@ -174,3 +174,81 @@ class TestUserEdit(object): email = User.query.filter_by(username='chris').first().email assert email == 'new@example.com' # test changing the url inproperly + +class TestMetaDataEdit: + @pytest.fixture(autouse=True) + def setup(self, test_app): + # set up new user + self.user_password = u'toast' + self.user = fixture_add_user(password = self.user_password, + privileges=[u'active',u'admin']) + self.test_app = test_app + + def login(self, test_app): + test_app.post( + '/auth/login/', { + 'username': self.user.username, + 'password': self.user_password}) + + 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(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 test_edit_metadata(self, test_app): + media_entry = fixture_media_entry(uploader=self.user.id, + state=u'processed') + media_slug = "/u/{username}/m/{media_id}/metadata/".format( + username = str(self.user.username), + media_id = str(media_entry.id)) + + self.login(test_app) + response = test_app.get(media_slug) + assert response.status == '200 OK' + assert media_entry.media_metadata == {} + # First test adding in metadata + ################################ + response, context = self.do_post({ + "media_metadata-0-identifier":"dc:title", + "media_metadata-0-value":"Some title", + "media_metadata-1-identifier":"dc:creator", + "media_metadata-1-value":"Me"},url=media_slug) + + media_entry = MediaEntry.query.first() + new_metadata = media_entry.media_metadata + assert new_metadata != {} + assert new_metadata.get("dc:title") == "Some title" + assert new_metadata.get("dc:creator") == "Me" + # Now test removing the metadata + ################################ + response, context = self.do_post({ + "media_metadata-0-identifier":"dc:title", + "media_metadata-0-value":"Some title"},url=media_slug) + + media_entry = MediaEntry.query.first() + new_metadata = media_entry.media_metadata + assert new_metadata.get("dc:title") == "Some title" + assert new_metadata.get("dc:creator") is None + # Now test adding bad metadata + ############################### + response, context = self.do_post({ + "media_metadata-0-identifier":"dc:title", + "media_metadata-0-value":"Some title", + "media_metadata-1-identifier":"dc:creator", + "media_metadata-1-value":"Me", + "media_metadata-2-identifier":"dc:created", + "media_metadata-2-value":"On the worst day"},url=media_slug) + + media_entry = MediaEntry.query.first() + old_metadata = new_metadata + new_metadata = media_entry.media_metadata + assert new_metadata == old_metadata + assert ("u'On the worst day' is not a 'date-time'" in + response.body) diff --git a/mediagoblin/tests/test_metadata.py b/mediagoblin/tests/test_metadata.py index 71346bb4..b4ea646e 100644 --- a/mediagoblin/tests/test_metadata.py +++ b/mediagoblin/tests/test_metadata.py @@ -15,40 +15,15 @@ # along with this program. If not, see . import pytest -from mediagoblin.tools import template from mediagoblin.tools.metadata import compact_and_validate -from webtest import AppError from jsonschema import ValidationError -from .resources import GOOD_JPG - class TestMetadataFunctionality: @pytest.fixture(autouse=True) def _setup(self, test_app): self.test_app = test_app - def login(self, username): - self.test_app.post( - '/auth/login/', { - 'username': username, - 'password': 'toast'}) - - def logout(self): - self.test_app.get('/auth/logout/') - - 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(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 testCompactAndValidate(self): # First, test out a well formatted piece of metadata ###################################################### -- 2.25.1