test_pagure_lib_add_user_to_project.py 11 KB

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