test_pagure_lib_add_user_to_project.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals
  8. import unittest
  9. import sys
  10. import os
  11. from mock import patch, MagicMock
  12. sys.path.insert(0, os.path.join(os.path.dirname(
  13. os.path.abspath(__file__)), '..'))
  14. import pagure.lib.query
  15. import tests
  16. class PagureLibAddUserToProjecttests(tests.Modeltests):
  17. """ Tests for pagure.lib.query.add_user_to_project """
  18. def setUp(self):
  19. """ Set up the environnment, ran before every tests. """
  20. super(PagureLibAddUserToProjecttests, self).setUp()
  21. tests.create_projects(self.session)
  22. item = pagure.lib.model.User(
  23. user='bar',
  24. fullname='bar baz',
  25. password='foo',
  26. default_email='bar@bar.com',
  27. )
  28. self.session.add(item)
  29. item = pagure.lib.model.UserEmail(
  30. user_id=3,
  31. email='bar@bar.com')
  32. self.session.add(item)
  33. self.session.commit()
  34. # Before
  35. repo = pagure.lib.query._get_project(self.session, 'test')
  36. self.assertEqual(len(repo.users), 0)
  37. msg = pagure.lib.query.add_user_to_project(
  38. session=self.session,
  39. project=repo,
  40. new_user='foo',
  41. user='pingou',
  42. )
  43. self.session.commit()
  44. self.assertEqual(msg, 'User added')
  45. # After
  46. repo = pagure.lib.query._get_project(self.session, 'test')
  47. self.assertEqual(len(repo.users), 1)
  48. self.assertEqual(repo.users[0].user, 'foo')
  49. self.assertEqual(repo.admins[0].user, 'foo')
  50. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  51. def test_re_add_user_to_project_default(self):
  52. """ Update an existing user but to the same access level. """
  53. repo = pagure.lib.query._get_project(self.session, 'test')
  54. # Try adding the same user with the same access
  55. self.assertRaises(
  56. pagure.exceptions.PagureException,
  57. pagure.lib.query.add_user_to_project,
  58. session=self.session,
  59. project=repo,
  60. new_user='foo',
  61. user='pingou',
  62. access='admin'
  63. )
  64. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  65. def test_update_user_to_project_default(self):
  66. """ Update an existing user without any required group membership.
  67. """
  68. repo = pagure.lib.query._get_project(self.session, 'test')
  69. # Update the access of the user
  70. msg = pagure.lib.query.add_user_to_project(
  71. session=self.session,
  72. project=repo,
  73. new_user='foo',
  74. user='pingou',
  75. access='commit'
  76. )
  77. self.session.commit()
  78. self.assertEqual(msg, 'User access updated')
  79. self.assertEqual(len(repo.users), 1)
  80. self.assertEqual(repo.users[0].user, 'foo')
  81. self.assertEqual(repo.committers[0].user, 'foo')
  82. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  83. def test_update_user_to_project_require_packager_on_all(self):
  84. """
  85. Update an existing user but required group membership on all
  86. projects.
  87. """
  88. repo = pagure.lib.query._get_project(self.session, 'test')
  89. config = {
  90. '*': ['packager']
  91. }
  92. # Update the access of the user
  93. self.assertRaises(
  94. pagure.exceptions.PagureException,
  95. pagure.lib.query.add_user_to_project,
  96. session=self.session,
  97. project=repo,
  98. new_user='foo',
  99. user='pingou',
  100. access='admin',
  101. required_groups=config
  102. )
  103. self.session.commit()
  104. self.assertEqual(len(repo.users), 1)
  105. self.assertEqual(repo.users[0].user, 'foo')
  106. self.assertEqual(repo.committers[0].user, 'foo')
  107. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  108. def test_update_user_to_project_require_packager_on_st(self):
  109. """
  110. Update an existing user but required group membership on all
  111. projects match *st.
  112. """
  113. repo = pagure.lib.query._get_project(self.session, 'test')
  114. config = {
  115. '*st': ['packager']
  116. }
  117. # Update the access of the user
  118. self.assertRaises(
  119. pagure.exceptions.PagureException,
  120. pagure.lib.query.add_user_to_project,
  121. session=self.session,
  122. project=repo,
  123. new_user='foo',
  124. user='pingou',
  125. access='admin',
  126. required_groups=config
  127. )
  128. self.session.commit()
  129. self.assertEqual(len(repo.users), 1)
  130. self.assertEqual(repo.users[0].user, 'foo')
  131. self.assertEqual(repo.committers[0].user, 'foo')
  132. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  133. def test_update_user_to_project_require_packager_on_te(self):
  134. """
  135. Update an existing user but required group membership on all
  136. projects match te*.
  137. """
  138. repo = pagure.lib.query._get_project(self.session, 'test')
  139. config = {
  140. 'te*': ['packager']
  141. }
  142. # Update the access of the user
  143. self.assertRaises(
  144. pagure.exceptions.PagureException,
  145. pagure.lib.query.add_user_to_project,
  146. session=self.session,
  147. project=repo,
  148. new_user='foo',
  149. user='pingou',
  150. access='admin',
  151. required_groups=config
  152. )
  153. self.session.commit()
  154. self.assertEqual(len(repo.users), 1)
  155. self.assertEqual(repo.users[0].user, 'foo')
  156. self.assertEqual(repo.committers[0].user, 'foo')
  157. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  158. def test_update_user_to_project_require_packager_on_test(self):
  159. """
  160. Update an existing user but required group membership on a specific
  161. project: test.
  162. """
  163. repo = pagure.lib.query._get_project(self.session, 'test')
  164. config = {
  165. 'test': ['packager']
  166. }
  167. # Update the access of the user
  168. self.assertRaises(
  169. pagure.exceptions.PagureException,
  170. pagure.lib.query.add_user_to_project,
  171. session=self.session,
  172. project=repo,
  173. new_user='foo',
  174. user='pingou',
  175. access='admin',
  176. required_groups=config
  177. )
  178. self.session.commit()
  179. self.assertEqual(len(repo.users), 1)
  180. self.assertEqual(repo.users[0].user, 'foo')
  181. self.assertEqual(repo.committers[0].user, 'foo')
  182. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  183. def test_add_user_to_test2_require_packager_on_test(self):
  184. """
  185. Add user to project test2 while the configuration requires group
  186. membership on the project test.
  187. """
  188. repo = pagure.lib.query._get_project(self.session, 'test2')
  189. self.assertEqual(len(repo.users), 0)
  190. config = {
  191. 'test': ['packager']
  192. }
  193. # Add the user
  194. pagure.lib.query.add_user_to_project(
  195. session=self.session,
  196. project=repo,
  197. new_user='foo',
  198. user='pingou',
  199. access='admin',
  200. required_groups=config
  201. )
  202. self.session.commit()
  203. self.assertEqual(len(repo.users), 1)
  204. self.assertEqual(repo.users[0].user, 'foo')
  205. self.assertEqual(repo.committers[0].user, 'foo')
  206. class PagureLibAddUserToProjectWithGrouptests(
  207. PagureLibAddUserToProjecttests):
  208. """ Tests for pagure.lib.query.add_user_to_project """
  209. def setUp(self):
  210. """ Set up the environnment, ran before every tests. """
  211. super(PagureLibAddUserToProjectWithGrouptests, self).setUp()
  212. # Create group
  213. msg = pagure.lib.query.add_group(
  214. self.session,
  215. group_name='packager',
  216. display_name='packager',
  217. description='The Fedora packager groups',
  218. group_type='user',
  219. user='pingou',
  220. is_admin=False,
  221. blacklist=[])
  222. self.session.commit()
  223. self.assertEqual(msg, 'User `pingou` added to the group `packager`.')
  224. # Add user to group
  225. group = pagure.lib.query.search_groups(self.session, group_name='packager')
  226. msg = pagure.lib.query.add_user_to_group(
  227. self.session,
  228. username='bar',
  229. group=group,
  230. user='pingou',
  231. is_admin=True)
  232. self.session.commit()
  233. self.assertEqual(msg, 'User `bar` added to the group `packager`.')
  234. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  235. def test_add_user_to_test_require_packager_on_test(self):
  236. """
  237. Add user to project test while the configuration requires group
  238. membership on the project test.
  239. """
  240. repo = pagure.lib.query._get_project(self.session, 'test')
  241. self.assertEqual(len(repo.users), 1)
  242. config = {
  243. 'test': ['packager']
  244. }
  245. # Add the user to the project
  246. pagure.lib.query.add_user_to_project(
  247. session=self.session,
  248. project=repo,
  249. new_user='bar',
  250. user='pingou',
  251. access='commit',
  252. required_groups=config
  253. )
  254. self.session.commit()
  255. repo = pagure.lib.query._get_project(self.session, 'test')
  256. self.assertEqual(len(repo.users), 2)
  257. self.assertEqual(repo.users[0].user, 'foo')
  258. self.assertEqual(repo.committers[0].user, 'foo')
  259. self.assertEqual(repo.users[1].user, 'bar')
  260. self.assertEqual(repo.committers[1].user, 'bar')
  261. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  262. def test_add_user_to_test_require_packager(self):
  263. """
  264. Add user to project test while the configuration requires group
  265. membership on all the projects.
  266. """
  267. repo = pagure.lib.query._get_project(self.session, 'test')
  268. self.assertEqual(len(repo.users), 1)
  269. config = {
  270. '*': ['packager']
  271. }
  272. # Add the user to the project
  273. pagure.lib.query.add_user_to_project(
  274. session=self.session,
  275. project=repo,
  276. new_user='bar',
  277. user='pingou',
  278. access='commit',
  279. required_groups=config
  280. )
  281. self.session.commit()
  282. repo = pagure.lib.query._get_project(self.session, 'test')
  283. self.assertEqual(len(repo.users), 2)
  284. self.assertEqual(repo.users[0].user, 'foo')
  285. self.assertEqual(repo.committers[0].user, 'foo')
  286. self.assertEqual(repo.users[1].user, 'bar')
  287. self.assertEqual(repo.committers[1].user, 'bar')
  288. if __name__ == '__main__':
  289. unittest.main(verbosity=2)