aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tests/test_metadata.py
blob: 71346bb4e78c07ab77e2dd8584c09438a6b6c8ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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