| 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 | |
| 17 | import pytest |
| 18 | from mediagoblin.tools.metadata import compact_and_validate |
| 19 | from jsonschema import ValidationError |
| 20 | |
| 21 | class TestMetadataFunctionality: |
| 22 | |
| 23 | @pytest.fixture(autouse=True) |
| 24 | def _setup(self, test_app): |
| 25 | self.test_app = test_app |
| 26 | |
| 27 | def testCompactAndValidate(self): |
| 28 | # First, test out a well formatted piece of metadata |
| 29 | ###################################################### |
| 30 | test_metadata = { |
| 31 | 'dc:title':'My Pet Bunny', |
| 32 | 'dc:description':'A picture displaying how cute my pet bunny is.', |
| 33 | 'location':'/home/goblin/Pictures/bunny.png', |
| 34 | 'license':'http://www.gnu.org/licenses/gpl.txt' |
| 35 | } |
| 36 | jsonld_metadata =compact_and_validate(test_metadata) |
| 37 | assert jsonld_metadata |
| 38 | assert jsonld_metadata.get('dc:title') == 'My Pet Bunny' |
| 39 | # Free floating nodes should be removed |
| 40 | assert jsonld_metadata.get('location') is None |
| 41 | assert jsonld_metadata.get('@context') == \ |
| 42 | u"http://www.w3.org/2013/json-ld-context/rdfa11" |
| 43 | |
| 44 | # Next, make sure that various badly formatted metadata |
| 45 | # will be rejected. |
| 46 | ####################################################### |
| 47 | #,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,. |
| 48 | # Metadata with a non-URI license should fail : |
| 49 | #`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`' |
| 50 | metadata_fail_1 = { |
| 51 | 'dc:title':'My Pet Bunny', |
| 52 | 'dc:description':'A picture displaying how cute my pet bunny is.', |
| 53 | 'location':'/home/goblin/Pictures/bunny.png', |
| 54 | 'license':'All Rights Reserved.' |
| 55 | } |
| 56 | jsonld_fail_1 = None |
| 57 | try: |
| 58 | jsonld_fail_1 = compact_and_validate(metadata_fail_1) |
| 59 | except ValidationError as e: |
| 60 | assert e.message == "'All Rights Reserved.' is not a 'uri'" |
| 61 | assert jsonld_fail_1 == None |
| 62 | #,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,., |
| 63 | # Metadata with an ivalid date-time dc:created should fail : |
| 64 | #`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'' |
| 65 | metadata_fail_2 = { |
| 66 | 'dc:title':'My Pet Bunny', |
| 67 | 'dc:description':'A picture displaying how cute my pet bunny is.', |
| 68 | 'location':'/home/goblin/Pictures/bunny.png', |
| 69 | 'license':'http://www.gnu.org/licenses/gpl.txt', |
| 70 | 'dc:created':'The other day' |
| 71 | } |
| 72 | jsonld_fail_2 = None |
| 73 | try: |
| 74 | jsonld_fail_2 = compact_and_validate(metadata_fail_2) |
| 75 | except ValidationError as e: |
| 76 | assert e.message == "'The other day' is not a 'date-time'" |
| 77 | assert jsonld_fail_2 == None |
| 78 | |