aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tests
diff options
context:
space:
mode:
Diffstat (limited to 'mediagoblin/tests')
-rw-r--r--mediagoblin/tests/test_edit.py84
-rw-r--r--mediagoblin/tests/test_metadata.py78
2 files changed, 159 insertions, 3 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 <http://www.gnu.org/licenses/>.
-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&#39;On the worst day&#39; is not a &#39;date-time&#39;" in
+ response.body)
diff --git a/mediagoblin/tests/test_metadata.py b/mediagoblin/tests/test_metadata.py
new file mode 100644
index 00000000..b4ea646e
--- /dev/null
+++ b/mediagoblin/tests/test_metadata.py
@@ -0,0 +1,78 @@
+# GNU MediaGoblin -- federated, autonomous media hosting
+# 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
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# 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 pytest
+from mediagoblin.tools.metadata import compact_and_validate
+from jsonschema import ValidationError
+
+class TestMetadataFunctionality:
+
+ @pytest.fixture(autouse=True)
+ def _setup(self, test_app):
+ self.test_app = test_app
+
+ def testCompactAndValidate(self):
+ # First, test out a well formatted piece of metadata
+ ######################################################
+ test_metadata = {
+ 'dc:title':'My Pet Bunny',
+ 'dc:description':'A picture displaying how cute my pet bunny is.',
+ 'location':'/home/goblin/Pictures/bunny.png',
+ 'license':'http://www.gnu.org/licenses/gpl.txt'
+ }
+ jsonld_metadata =compact_and_validate(test_metadata)
+ assert jsonld_metadata
+ assert jsonld_metadata.get('dc:title') == 'My Pet Bunny'
+ # Free floating nodes should be removed
+ assert jsonld_metadata.get('location') is None
+ assert jsonld_metadata.get('@context') == \
+ u"http://www.w3.org/2013/json-ld-context/rdfa11"
+
+ # Next, make sure that various badly formatted metadata
+ # will be rejected.
+ #######################################################
+ #,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.
+ # Metadata with a non-URI license should fail :
+ #`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'
+ metadata_fail_1 = {
+ 'dc:title':'My Pet Bunny',
+ 'dc:description':'A picture displaying how cute my pet bunny is.',
+ 'location':'/home/goblin/Pictures/bunny.png',
+ 'license':'All Rights Reserved.'
+ }
+ jsonld_fail_1 = None
+ try:
+ jsonld_fail_1 = compact_and_validate(metadata_fail_1)
+ except ValidationError, e:
+ assert e.message == "'All Rights Reserved.' is not a 'uri'"
+ assert jsonld_fail_1 == None
+ #,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,
+ # Metadata with an ivalid date-time dc:created should fail :
+ #`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`''
+ metadata_fail_2 = {
+ 'dc:title':'My Pet Bunny',
+ 'dc:description':'A picture displaying how cute my pet bunny is.',
+ 'location':'/home/goblin/Pictures/bunny.png',
+ 'license':'http://www.gnu.org/licenses/gpl.txt',
+ 'dc:created':'The other day'
+ }
+ jsonld_fail_2 = None
+ try:
+ jsonld_fail_2 = compact_and_validate(metadata_fail_2)
+ except ValidationError, e:
+ assert e.message == "'The other day' is not a 'date-time'"
+ assert jsonld_fail_2 == None
+