/plone/resource/tests/test_zcml.py

https://github.com/plone/plone.resource
Python | 111 lines | 87 code | 20 blank | 4 comment | 2 complexity | d12da95a904195376df5b83594cadd2f MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. import os.path
  3. import unittest
  4. from plone.resource.interfaces import IResourceDirectory
  5. from six import StringIO
  6. from zope.component import getUtility
  7. from zope.component.testing import tearDown
  8. from zope.configuration.exceptions import ConfigurationError
  9. from zope.configuration.xmlconfig import XMLConfig, xmlconfig
  10. def clearZCML(test=None):
  11. # Copy from ``zope.component.tests.examples``
  12. from zope.configuration.xmlconfig import XMLConfig
  13. import zope.component
  14. from zope.component.testing import setUp
  15. from zope.component.testing import tearDown
  16. tearDown()
  17. setUp()
  18. XMLConfig('meta.zcml', zope.component)()
  19. def runSnippet(snippet, dist='plone.resource.tests'):
  20. template = """\
  21. <configure xmlns="http://namespaces.zope.org/zope"
  22. xmlns:plone="http://namespaces.plone.org/plone"
  23. i18n_domain="plone"
  24. %s>
  25. %s
  26. </configure>"""
  27. dist = 'package="%s"' % dist if dist else ''
  28. xmlconfig(StringIO(template % (dist, snippet)))
  29. class ZCMLTestCase(unittest.TestCase):
  30. def setUp(self):
  31. clearZCML()
  32. import plone.resource
  33. XMLConfig('meta.zcml', plone.resource)()
  34. def tearDown(self):
  35. tearDown()
  36. def test_dist_with_name_and_type(self):
  37. runSnippet("""
  38. <plone:static
  39. name="foo"
  40. type="theme"
  41. directory="resources"
  42. />
  43. """)
  44. res = getUtility(IResourceDirectory, name='++theme++foo')
  45. self.assertTrue(res.directory.endswith(os.path.join('plone', 'resource', 'tests', 'resources')))
  46. def test_dist_rejects_with_missing_type(self):
  47. # resource directories in distributions must be registered with a type
  48. self.assertRaises(ConfigurationError,
  49. runSnippet,
  50. """<plone:static
  51. name="foo"
  52. directory="resources"
  53. />"""
  54. )
  55. def test_dist_with_type_only(self):
  56. runSnippet("""
  57. <plone:static
  58. type="theme"
  59. directory="resources"
  60. />
  61. """)
  62. getUtility(IResourceDirectory, name='++theme++plone.resource.tests')
  63. def test_dist_rejects_absolute_directory(self):
  64. self.assertRaises(ConfigurationError,
  65. runSnippet,
  66. """<plone:static directory="/" />"""
  67. )
  68. def test_global(self):
  69. runSnippet("""
  70. <plone:static
  71. directory="/"
  72. />
  73. """, dist=None)
  74. res = getUtility(IResourceDirectory)
  75. self.assertEqual('/', res.directory)
  76. def test_global_rejects_relative_directory(self):
  77. self.assertRaises(ConfigurationError,
  78. runSnippet,
  79. """<plone:static directory="foobar" />""",
  80. dist=None
  81. )
  82. def test_missing_directory(self):
  83. self.assertRaises(ConfigurationError,
  84. runSnippet,
  85. """<plone:static directory="foobar" />"""
  86. )
  87. def test_rejects_parent_directory_traversal(self):
  88. self.assertRaises(ConfigurationError,
  89. runSnippet,
  90. """<plone:static directory="../tests" />"""
  91. )