Wrote a test for the compact_and_validate metadata function.
[mediagoblin.git] / mediagoblin / tests / test_metadata.py
CommitLineData
375db9c9 1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17import pytest
18from mediagoblin.tools import template
19from mediagoblin.tools.metadata import compact_and_validate
20from webtest import AppError
21from jsonschema import ValidationError
22
23from .resources import GOOD_JPG
24
25class TestMetadataFunctionality:
26
27 @pytest.fixture(autouse=True)
28 def _setup(self, test_app):
29 self.test_app = test_app
30
31 def login(self, username):
32 self.test_app.post(
33 '/auth/login/', {
34 'username': username,
35 'password': 'toast'})
36
37 def logout(self):
38 self.test_app.get('/auth/logout/')
39
40 def do_post(self, data, *context_keys, **kwargs):
41 url = kwargs.pop('url', '/submit/')
42 do_follow = kwargs.pop('do_follow', False)
43 template.clear_test_template_context()
44 response = self.test_app.post(url, data, **kwargs)
45 if do_follow:
46 response.follow()
47 context_data = template.TEMPLATE_TEST_CONTEXT
48 for key in context_keys:
49 context_data = context_data[key]
50 return response, context_data
51
52 def testCompactAndValidate(self):
53 # First, test out a well formatted piece of metadata
54 ######################################################
55 test_metadata = {
56 'dc:title':'My Pet Bunny',
57 'dc:description':'A picture displaying how cute my pet bunny is.',
58 'location':'/home/goblin/Pictures/bunny.png',
59 'license':'http://www.gnu.org/licenses/gpl.txt'
60 }
61 jsonld_metadata =compact_and_validate(test_metadata)
62 assert jsonld_metadata
63 assert jsonld_metadata.get('dc:title') == 'My Pet Bunny'
64 # Free floating nodes should be removed
65 assert jsonld_metadata.get('location') is None
66 assert jsonld_metadata.get('@context') == \
67 u"http://www.w3.org/2013/json-ld-context/rdfa11"
68
69 # Next, make sure that various badly formatted metadata
70 # will be rejected.
71 #######################################################
72 #,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.
73 # Metadata with a non-URI license should fail :
74 #`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'
75 metadata_fail_1 = {
76 'dc:title':'My Pet Bunny',
77 'dc:description':'A picture displaying how cute my pet bunny is.',
78 'location':'/home/goblin/Pictures/bunny.png',
79 'license':'All Rights Reserved.'
80 }
81 jsonld_fail_1 = None
82 try:
83 jsonld_fail_1 = compact_and_validate(metadata_fail_1)
84 except ValidationError, e:
85 assert e.message == "'All Rights Reserved.' is not a 'uri'"
86 assert jsonld_fail_1 == None
87 #,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,
88 # Metadata with an ivalid date-time dc:created should fail :
89 #`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`''
90 metadata_fail_2 = {
91 'dc:title':'My Pet Bunny',
92 'dc:description':'A picture displaying how cute my pet bunny is.',
93 'location':'/home/goblin/Pictures/bunny.png',
94 'license':'http://www.gnu.org/licenses/gpl.txt',
95 'dc:created':'The other day'
96 }
97 jsonld_fail_2 = None
98 try:
99 jsonld_fail_2 = compact_and_validate(metadata_fail_2)
100 except ValidationError, e:
101 assert e.message == "'The other day' is not a 'date-time'"
102 assert jsonld_fail_2 == None
103