test_metadata.py 3.4 KB

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