PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ckanext/example_iauthfunctions/tests/test_example_iauthfunctions.py

https://gitlab.com/iislod/ckan
Python | 261 lines | 190 code | 36 blank | 35 comment | 0 complexity | 3188f88a08d7a6c605a37ee66409a4e6 MD5 | raw file
  1. '''Tests for the ckanext.example_iauthfunctions extension.
  2. '''
  3. import paste.fixture
  4. import pylons.test
  5. import pylons.config as config
  6. import webtest
  7. import ckan.model as model
  8. import ckan.tests.legacy as tests
  9. import ckan.plugins
  10. import ckan.tests.factories as factories
  11. class TestExampleIAuthFunctionsCustomConfigSetting(object):
  12. '''Tests for the plugin_v5_custom_config_setting module.
  13. '''
  14. def _get_app(self, users_can_create_groups):
  15. # Set the custom config option in pylons.config.
  16. config['ckan.iauthfunctions.users_can_create_groups'] = (
  17. users_can_create_groups)
  18. # Return a test app with the custom config.
  19. app = ckan.config.middleware.make_app(config['global_conf'], **config)
  20. app = webtest.TestApp(app)
  21. ckan.plugins.load('example_iauthfunctions_v5_custom_config_setting')
  22. return app
  23. def teardown(self):
  24. # Remove the custom config option from pylons.config.
  25. del config['ckan.iauthfunctions.users_can_create_groups']
  26. # Delete any stuff that's been created in the db, so it doesn't
  27. # interfere with the next test.
  28. model.repo.rebuild_db()
  29. @classmethod
  30. def teardown_class(cls):
  31. ckan.plugins.unload('example_iauthfunctions_v5_custom_config_setting')
  32. def test_sysadmin_can_create_group_when_config_is_False(self):
  33. app = self._get_app(users_can_create_groups=False)
  34. sysadmin = factories.Sysadmin()
  35. tests.call_action_api(app, 'group_create', name='test-group',
  36. apikey=sysadmin['apikey'])
  37. def test_user_cannot_create_group_when_config_is_False(self):
  38. app = self._get_app(users_can_create_groups=False)
  39. user = factories.User()
  40. tests.call_action_api(app, 'group_create', name='test-group',
  41. apikey=user['apikey'], status=403)
  42. def test_visitor_cannot_create_group_when_config_is_False(self):
  43. app = self._get_app(users_can_create_groups=False)
  44. tests.call_action_api(app, 'group_create', name='test-group',
  45. status=403)
  46. def test_sysadmin_can_create_group_when_config_is_True(self):
  47. app = self._get_app(users_can_create_groups=True)
  48. sysadmin = factories.Sysadmin()
  49. tests.call_action_api(app, 'group_create', name='test-group',
  50. apikey=sysadmin['apikey'])
  51. def test_user_can_create_group_when_config_is_True(self):
  52. app = self._get_app(users_can_create_groups=True)
  53. user = factories.User()
  54. tests.call_action_api(app, 'group_create', name='test-group',
  55. apikey=user['apikey'])
  56. def test_visitor_cannot_create_group_when_config_is_True(self):
  57. app = self._get_app(users_can_create_groups=True)
  58. tests.call_action_api(app, 'group_create', name='test-group',
  59. status=403)
  60. class TestExampleIAuthFunctionsPluginV4(object):
  61. '''Tests for the ckanext.example_iauthfunctions.plugin module.
  62. '''
  63. @classmethod
  64. def setup_class(cls):
  65. '''Nose runs this method once to setup our test class.'''
  66. # Make the Paste TestApp that we'll use to simulate HTTP requests to
  67. # CKAN.
  68. cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)
  69. # Test code should use CKAN's plugins.load() function to load plugins
  70. # to be tested.
  71. ckan.plugins.load('example_iauthfunctions_v4')
  72. def teardown(self):
  73. '''Nose runs this method after each test method in our test class.'''
  74. # Rebuild CKAN's database after each test method, so that each test
  75. # method runs with a clean slate.
  76. model.repo.rebuild_db()
  77. @classmethod
  78. def teardown_class(cls):
  79. '''Nose runs this method once after all the test methods in our class
  80. have been run.
  81. '''
  82. # We have to unload the plugin we loaded, so it doesn't affect any
  83. # tests that run after ours.
  84. ckan.plugins.unload('example_iauthfunctions_v4')
  85. def _make_curators_group(self):
  86. '''This is a helper method for test methods to call when they want
  87. the 'curators' group to be created.
  88. '''
  89. sysadmin = factories.Sysadmin()
  90. # Create a user who will *not* be a member of the curators group.
  91. noncurator = factories.User()
  92. # Create a user who will be a member of the curators group.
  93. curator = factories.User()
  94. # Create the curators group, with the 'curator' user as a member.
  95. users = [{'name': curator['name'], 'capacity': 'member'}]
  96. curators_group = tests.call_action_api(self.app, 'group_create',
  97. apikey=sysadmin['apikey'],
  98. name='curators',
  99. users=users)
  100. return (noncurator, curator, curators_group)
  101. def test_group_create_with_no_curators_group(self):
  102. '''Test that group_create doesn't crash when there's no curators group.
  103. '''
  104. sysadmin = factories.Sysadmin()
  105. # Make sure there's no curators group.
  106. assert 'curators' not in tests.call_action_api(self.app, 'group_list')
  107. # Make our sysadmin user create a group. CKAN should not crash.
  108. tests.call_action_api(self.app, 'group_create', name='test-group',
  109. apikey=sysadmin['apikey'])
  110. def test_group_create_with_visitor(self):
  111. '''A visitor (not logged in) should not be able to create a group.
  112. Note: this also tests that the group_create auth function doesn't
  113. crash when the user isn't logged in.
  114. '''
  115. noncurator, curator, curators_group = self._make_curators_group()
  116. result = tests.call_action_api(self.app, 'group_create',
  117. name='this_group_should_not_be_created',
  118. status=403)
  119. assert result['__type'] == 'Authorization Error'
  120. def test_group_create_with_non_curator(self):
  121. '''A user who isn't a member of the curators group should not be able
  122. to create a group.
  123. '''
  124. noncurator, curator, curators_group = self._make_curators_group()
  125. result = tests.call_action_api(self.app, 'group_create',
  126. name='this_group_should_not_be_created',
  127. apikey=noncurator['apikey'],
  128. status=403)
  129. assert result['__type'] == 'Authorization Error'
  130. def test_group_create_with_curator(self):
  131. '''A member of the curators group should be able to create a group.
  132. '''
  133. noncurator, curator, curators_group = self._make_curators_group()
  134. name = 'my-new-group'
  135. result = tests.call_action_api(self.app, 'group_create',
  136. name=name,
  137. apikey=curator['apikey'])
  138. assert result['name'] == name
  139. class TestExampleIAuthFunctionsPluginV3(TestExampleIAuthFunctionsPluginV4):
  140. '''Tests for the ckanext.example_iauthfunctions.plugin_v3 module.
  141. '''
  142. @classmethod
  143. def setup_class(cls):
  144. cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)
  145. ckan.plugins.load('example_iauthfunctions_v3')
  146. @classmethod
  147. def teardown_class(cls):
  148. ckan.plugins.unload('example_iauthfunctions_v3')
  149. def test_group_create_with_no_curators_group(self):
  150. '''Test that group_create returns a 404 when there's no curators group.
  151. With this version of the plugin group_create returns a spurious 404
  152. when a user _is_ logged-in but the site has no curators group.
  153. '''
  154. assert 'curators' not in tests.call_action_api(self.app, 'group_list')
  155. user = factories.User()
  156. response = tests.call_action_api(self.app, 'group_create',
  157. name='test_group',
  158. apikey=user['apikey'], status=404)
  159. assert response == {'__type': 'Not Found Error',
  160. 'message': 'Not found'}
  161. def test_group_create_with_visitor(self):
  162. '''Test that group_create returns 403 when no one is logged in.
  163. Since #1210 non-logged in requests are automatically rejected, unless
  164. the auth function has the appropiate decorator
  165. '''
  166. noncurator, curator, curators_group = self._make_curators_group()
  167. response = tests.call_action_api(self.app, 'group_create',
  168. name='this_group_shouldnt_be_created',
  169. status=403)
  170. assert response['__type'] == 'Authorization Error'
  171. class TestExampleIAuthFunctionsPluginV2(TestExampleIAuthFunctionsPluginV4):
  172. '''Tests for the ckanext.example_iauthfunctions.plugin_v2 module.
  173. '''
  174. @classmethod
  175. def setup_class(cls):
  176. cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)
  177. ckan.plugins.load('example_iauthfunctions_v2')
  178. @classmethod
  179. def teardown_class(cls):
  180. ckan.plugins.unload('example_iauthfunctions_v2')
  181. def test_group_create_with_curator(self):
  182. '''Test that a curator can*not* create a group.
  183. In this version of the plugin, even users who are members of the
  184. curators group cannot create groups.
  185. '''
  186. noncurator, curator, curators_group = self._make_curators_group()
  187. result = tests.call_action_api(self.app, 'group_create',
  188. name='this_group_should_not_be_created',
  189. apikey=curator['apikey'],
  190. status=403)
  191. assert result['__type'] == 'Authorization Error'