Wrote a test for the compact_and_validate metadata function.
authortilly-Q <nattilypigeonfowl@gmail.com>
Tue, 13 May 2014 21:45:29 +0000 (17:45 -0400)
committertilly-Q <nattilypigeonfowl@gmail.com>
Tue, 13 May 2014 21:45:29 +0000 (17:45 -0400)
mediagoblin/tests/test_metadata.py [new file with mode: 0644]

diff --git a/mediagoblin/tests/test_metadata.py b/mediagoblin/tests/test_metadata.py
new file mode 100644 (file)
index 0000000..71346bb
--- /dev/null
@@ -0,0 +1,103 @@
+# 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 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
+        ######################################################
+        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
+