test_pagure_lib_add_user_to_project.py 10 KB

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