PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/python2.6/site-packages/repoze.what-1.0.9-py2.6.egg/repoze/what/adapters/testutil.py

https://github.com/rudyvallejos/GestionItems
Python | 303 lines | 256 code | 24 blank | 23 comment | 11 complexity | af916326dccdc68f8aee9dcb1f92c6d7 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (c) 2007, Agendaless Consulting and Contributors.
  5. # Copyright (c) 2008, Florent Aide <florent.aide@gmail.com>.
  6. # Copyright (c) 2008-2009, Gustavo Narea <me@gustavonarea.net>.
  7. # All Rights Reserved.
  8. #
  9. # This software is subject to the provisions of the BSD-like license at
  10. # http://www.repoze.org/LICENSE.txt. A copy of the license should accompany
  11. # this distribution. THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL
  12. # EXPRESS OR IMPLIED WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO,
  13. # THE IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND
  14. # FITNESS FOR A PARTICULAR PURPOSE.
  15. #
  16. ##############################################################################
  17. """Utilities to test source adapters."""
  18. from repoze.what.adapters import SourceError, ExistingSectionError, \
  19. NonExistingSectionError, \
  20. ItemPresentError, ItemNotPresentError
  21. __all__ = ['GroupsAdapterTester', 'PermissionsAdapterTester',
  22. 'ReadOnlyGroupsAdapterTester', 'ReadOnlyPermissionsAdapterTester']
  23. class _ReadOnlyBaseAdapterTester(object):
  24. """Base test case for read-only adapters"""
  25. def _get_all_items(self):
  26. all_items = set()
  27. for items in self.all_sections.values():
  28. all_items |= items
  29. return all_items
  30. def _get_item_sections(self, item):
  31. return set([n for (n, s) in self.all_sections.items() if item in s])
  32. def test_retrieving_all_sections(self):
  33. self.assertEqual(self.adapter._get_all_sections(), self.all_sections)
  34. def test_getting_section_items(self):
  35. for section_name, items in self.all_sections.items():
  36. self.assertEqual(self.adapter._get_section_items(section_name),
  37. items)
  38. def test_checking_existing_section(self):
  39. for section_name in self.all_sections.keys():
  40. assert self.adapter._section_exists(section_name), \
  41. 'Section "%s" does NOT exist' % section_name
  42. def test_checking_non_existing_section(self):
  43. section_name = u'i_dont_exist'
  44. assert not self.adapter._section_exists(section_name), \
  45. 'Section "%s" DOES exist' % section_name
  46. def test_checking_item_inclusion(self):
  47. for section_name, items in self.all_sections.items():
  48. for item in self.adapter._get_section_items(section_name):
  49. assert self.adapter._item_is_included(section_name, item), \
  50. 'Item "%s" must be included in section "%s"' % \
  51. (item, section_name)
  52. def test_checking_excluded_item_inclusion(self):
  53. excluded_item = self.new_items.pop()
  54. for section_name, items in self.all_sections.items():
  55. assert not self.adapter._item_is_included(section_name,
  56. excluded_item), \
  57. 'Item "%s" must not included in section "%s"' % \
  58. (item, section_name)
  59. def test_checking_section_existence(self):
  60. for section_name in self.all_sections.keys():
  61. assert self.adapter._section_exists(section_name), \
  62. 'Section "%s" must exist' % section_name
  63. def test_checking_non_existing_section_existence(self):
  64. invalid_section = u'designers'
  65. assert not self.adapter._section_exists(invalid_section), \
  66. 'Section "%s" must not exist' % invalid_section
  67. def test_sets_if_it_is_writable(self):
  68. assert hasattr(self.adapter, 'is_writable'), \
  69. "The adapter doesn't have the 'is_writable' attribute; " \
  70. "please call its parent's constructor too"
  71. class _BaseAdapterTester(_ReadOnlyBaseAdapterTester):
  72. """Base test case for read & write adapters"""
  73. def test_adding_many_items_to_section(self):
  74. for section_name, items in self.all_sections.items():
  75. self.adapter._include_items(section_name, self.new_items)
  76. final_items = items | self.new_items
  77. assert self.adapter._get_section_items(section_name)==final_items, \
  78. '"%s" does not include %s' % (section_name, self.new_items)
  79. def test_creating_section(self):
  80. section = u'cool-section'
  81. self.adapter._create_section(section)
  82. assert section in self.adapter._get_all_sections().keys(), \
  83. 'Section "%s" could not be added' % section
  84. def test_editing_section(self):
  85. old_section = self.all_sections.keys()[0]
  86. new_section = u'cool-section'
  87. self.adapter._edit_section(old_section, new_section)
  88. assert new_section in self.adapter._get_all_sections().keys() and \
  89. old_section not in self.adapter._get_all_sections().keys(), \
  90. 'Section "%s" was not renamed to "%s"' % (old_section,
  91. new_section)
  92. def test_deleting_section(self):
  93. section = self.all_sections.keys()[0]
  94. self.adapter._delete_section(section)
  95. assert section not in self.adapter._get_all_sections().keys(), \
  96. 'Section "%s" was not deleted' % section
  97. class ReadOnlyGroupsAdapterTester(_ReadOnlyBaseAdapterTester):
  98. """
  99. Test case for read-only groups source adapters.
  100. The groups source used for the tests must only contain the following
  101. groups (aka "sections") and their relevant users (aka "items"; if any):
  102. * admins
  103. * rms
  104. * developers
  105. * rms
  106. * linus
  107. * trolls
  108. * sballmer
  109. * python
  110. * php
  111. .. attribute:: adapter
  112. An instance of the :term:`group adapter` to be tested.
  113. For example, a test case for the mock group adapter
  114. ``FakeReadOnlyGroupSourceAdapter`` may look like this::
  115. from repoze.what.adapters.testutil import ReadOnlyGroupsAdapterTester
  116. class TestReadOnlyGroupsAdapterTester(ReadOnlyGroupsAdapterTester,
  117. unittest.TestCase):
  118. def setUp(self):
  119. super(TestReadOnlyGroupsAdapterTester, self).setUp()
  120. self.adapter = FakeReadOnlyGroupSourceAdapter()
  121. .. note::
  122. :class:`GroupsAdapterTester` extends this test case to check write
  123. operations.
  124. """
  125. new_items = set((u'guido', u'rasmus'))
  126. def setUp(self):
  127. self.all_sections = {
  128. u'admins': set((u'rms', )),
  129. u'developers': set((u'rms', u'linus')),
  130. u'trolls': set((u'sballmer', )),
  131. u'python': set(),
  132. u'php': set()
  133. }
  134. def _make_credentials(self, userid):
  135. """
  136. Return a fake :mod:`repoze.what` ``credentials`` dictionary based on
  137. the ``userid``.
  138. Overwrite this method if its generated ``credentials`` dictionaries
  139. are not suitable for your adapter.
  140. """
  141. return {'repoze.what.userid': userid}
  142. def test_finding_groups_of_authenticated_user(self):
  143. for userid in self._get_all_items():
  144. credentials = self._make_credentials(userid)
  145. self.assertEqual(self.adapter._find_sections(credentials),
  146. self._get_item_sections(userid))
  147. def test_finding_groups_of_non_existing_user(self):
  148. credentials = self._make_credentials(u'gustavo')
  149. self.assertEqual(self.adapter._find_sections(credentials), set())
  150. class GroupsAdapterTester(ReadOnlyGroupsAdapterTester, _BaseAdapterTester):
  151. """
  152. Test case for groups source adapters.
  153. This test case extends :class:`ReadOnlyGroupsAdapterTester` to test
  154. write operations in read & write adapters and it should be set up the same
  155. way as its parent. For example, a test case for the mock group adapter
  156. ``FakeGroupSourceAdapter`` may look like this::
  157. from repoze.what.adapters.testutil import GroupsAdapterTester
  158. class TestGroupsAdapterTester(GroupsAdapterTester, unittest.TestCase):
  159. def setUp(self):
  160. super(TestGroupsAdapterTester, self).setUp()
  161. self.adapter = FakeGroupSourceAdapter()
  162. """
  163. def test_removing_many_users_from_group(self):
  164. group = u'developers'
  165. users = (u'rms', u'linus')
  166. self.adapter._exclude_items(group, users)
  167. assert self.adapter._get_section_items(group)==set(), \
  168. '"%s" still includes %s' % (group, users)
  169. class ReadOnlyPermissionsAdapterTester(_ReadOnlyBaseAdapterTester):
  170. """
  171. Test case for read-only permissions source adapters.
  172. The permissions source used for the tests must only contain the following
  173. permissions (aka "sections") and their relevant groups (aka "items"; if
  174. any):
  175. * see-site
  176. * trolls
  177. * edit-site
  178. * admins
  179. * developers
  180. * commit
  181. * developers
  182. .. attribute:: adapter
  183. An instance of the :term:`permission adapter` to be tested.
  184. For example, a test case for the mock permission adapter defined above
  185. (``FakeReadOnlyPermissionSourceAdapter``) may look like this::
  186. from repoze.what.adapters.testutil import ReadOnlyPermissionsAdapterTester
  187. class TestReadOnlyPermissionsAdapterTester(ReadOnlyPermissionsAdapterTester,
  188. unittest.TestCase):
  189. def setUp(self):
  190. super(TestReadOnlyPermissionsAdapterTester, self).setUp()
  191. self.adapter = FakeReadOnlyPermissionSourceAdapter()
  192. .. note::
  193. :class:`PermissionsAdapterTester` extends this test case to check write
  194. operations.
  195. """
  196. new_items = set((u'python', u'php'))
  197. def setUp(self):
  198. self.all_sections = {
  199. u'see-site': set((u'trolls', )),
  200. u'edit-site': set((u'admins', u'developers')),
  201. u'commit': set((u'developers', ))
  202. }
  203. def test_finding_permissions(self):
  204. for group in self._get_all_items():
  205. self.assertEqual(self.adapter._find_sections(group),
  206. self._get_item_sections(group))
  207. def test_finding_permissions_of_non_existing_group(self):
  208. self.assertEqual(self.adapter._find_sections(u'designers'), set())
  209. class PermissionsAdapterTester(ReadOnlyPermissionsAdapterTester,
  210. _BaseAdapterTester):
  211. """
  212. Test case for permissions source adapters.
  213. This test case extends :class:`ReadOnlyPermissionsAdapterTester` to test
  214. write operations in read & write adapters and it should be set up the same
  215. way as its parent. For example, a test case for the mock group adapter
  216. ``FakePermissionSourceAdapter`` may look like this:
  217. For example, a test case for the mock permission adapter defined above
  218. (``FakePermissionSourceAdapter``) may look like this::
  219. from repoze.what.adapters.testutil import PermissionsAdapterTester
  220. class TestPermissionsAdapterTester(PermissionsAdapterTester,
  221. unittest.TestCase):
  222. def setUp(self):
  223. super(TestPermissionsAdapterTester, self).setUp()
  224. self.adapter = FakePermissionSourceAdapter()
  225. """
  226. def test_deying_permisssion_to_many_groups(self):
  227. permission = u'edit-site'
  228. groups = (u'admins', u'developers')
  229. self.adapter._exclude_items(permission, groups)
  230. assert self.adapter._get_section_items(permission)==set(), \
  231. '"%s" still includes %s' % (permission, groups)