test_pagure_flask_ui_issues_private.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - 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 # noqa
  16. import tests # noqa
  17. class PagureFlaskIssuesPrivatetests(tests.Modeltests):
  18. """ Tests for flask issues controller of pagure with private tickets
  19. """
  20. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  21. def setUp(self):
  22. """ Set up the environnment, ran before every tests. """
  23. super(PagureFlaskIssuesPrivatetests, self).setUp()
  24. # Create a 3rd user
  25. item = pagure.lib.model.User(
  26. user="random",
  27. fullname="Random user",
  28. password="foo",
  29. default_email="random@bar.com",
  30. )
  31. self.session.add(item)
  32. item = pagure.lib.model.UserEmail(user_id=3, email="random@bar.com")
  33. self.session.add(item)
  34. self.session.commit()
  35. tests.create_projects(self.session)
  36. tests.create_projects_git(os.path.join(self.path, "repos"))
  37. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  38. msg = pagure.lib.query.new_issue(
  39. session=self.session,
  40. repo=repo,
  41. title="Test issue #1",
  42. content="We should work on this for the second time",
  43. user="foo",
  44. status="Open",
  45. private=True,
  46. )
  47. self.session.commit()
  48. self.assertEqual(msg.title, "Test issue #1")
  49. msg = pagure.lib.query.new_issue(
  50. session=self.session,
  51. repo=repo,
  52. title="Test issue #2",
  53. content="We should work on this for the second time",
  54. user="foo",
  55. status="Open",
  56. private=False,
  57. )
  58. self.session.commit()
  59. self.assertEqual(msg.title, "Test issue #2")
  60. def test_issue_list_anonymous(self):
  61. """ Test the list of issues when user is logged out. """
  62. output = self.app.get("/test/issues")
  63. self.assertEqual(output.status_code, 200)
  64. output_text = output.get_data(as_text=True)
  65. self.assertIn("<title>Issues - test - Pagure</title>", output_text)
  66. self.assertIn(
  67. '<span class="fa fa-fw fa-exclamation-circle"></span> 1 Open Issues\n',
  68. output_text,
  69. )
  70. def test_issue_list_admin(self):
  71. """ Test the list of issues when user is an admin of the project.
  72. """
  73. user = tests.FakeUser(username="pingou")
  74. with tests.user_set(self.app.application, user):
  75. output = self.app.get("/test/issues")
  76. self.assertEqual(output.status_code, 200)
  77. output_text = output.get_data(as_text=True)
  78. self.assertIn("<title>Issues - test - Pagure</title>", output_text)
  79. self.assertIn(
  80. '<span class="fa fa-fw fa-exclamation-circle"></span> 2 Open Issues\n',
  81. output_text,
  82. )
  83. def test_issue_list_author(self):
  84. """ Test the list of issues when user is an admin of the project.
  85. """
  86. user = tests.FakeUser(username="foo")
  87. with tests.user_set(self.app.application, user):
  88. output = self.app.get("/test/issues")
  89. self.assertEqual(output.status_code, 200)
  90. output_text = output.get_data(as_text=True)
  91. self.assertIn("<title>Issues - test - Pagure</title>", output_text)
  92. self.assertIn(
  93. '<span class="fa fa-fw fa-exclamation-circle"></span> 2 Open Issues\n',
  94. output_text,
  95. )
  96. def test_issue_list_authenticated(self):
  97. """ Test the list of issues when user is authenticated but has no
  98. special access to the project.
  99. """
  100. user = tests.FakeUser(username="random")
  101. with tests.user_set(self.app.application, user):
  102. output = self.app.get("/test/issues")
  103. self.assertEqual(output.status_code, 200)
  104. output_text = output.get_data(as_text=True)
  105. self.assertIn("<title>Issues - test - Pagure</title>", output_text)
  106. self.assertIn(
  107. '<span class="fa fa-fw fa-exclamation-circle"></span> 1 Open Issues\n',
  108. output_text,
  109. )
  110. def test_issue_list_authenticated_ticket(self):
  111. """ Test the list of issues when user is authenticated but has
  112. ticket level access to the project.
  113. """
  114. repo = pagure.lib.query._get_project(self.session, "test")
  115. msg = pagure.lib.query.add_user_to_project(
  116. session=self.session,
  117. project=repo,
  118. new_user="random",
  119. user="pingou",
  120. access="ticket",
  121. )
  122. self.session.commit()
  123. self.assertEqual(msg, "User added")
  124. user = tests.FakeUser(username="random")
  125. with tests.user_set(self.app.application, user):
  126. output = self.app.get("/test/issues")
  127. self.assertEqual(output.status_code, 200)
  128. output_text = output.get_data(as_text=True)
  129. self.assertIn("<title>Issues - test - Pagure</title>", output_text)
  130. self.assertIn(
  131. '<span class="fa fa-fw fa-exclamation-circle"></span> 1 Open Issues\n',
  132. output_text,
  133. )
  134. def test_issue_list_authenticated_commit(self):
  135. """ Test the list of issues when user is authenticated but has
  136. commit level access to the project.
  137. """
  138. repo = pagure.lib.query._get_project(self.session, "test")
  139. msg = pagure.lib.query.add_user_to_project(
  140. session=self.session,
  141. project=repo,
  142. new_user="random",
  143. user="pingou",
  144. access="commit",
  145. )
  146. self.session.commit()
  147. self.assertEqual(msg, "User added")
  148. user = tests.FakeUser(username="random")
  149. with tests.user_set(self.app.application, user):
  150. output = self.app.get("/test/issues")
  151. self.assertEqual(output.status_code, 200)
  152. output_text = output.get_data(as_text=True)
  153. self.assertIn("<title>Issues - test - Pagure</title>", output_text)
  154. self.assertIn(
  155. '<span class="fa fa-fw fa-exclamation-circle"></span> 2 Open Issues\n',
  156. output_text,
  157. )
  158. def test_issue_list_authenticated_assigned(self):
  159. """ Test the list of issues when user is authenticated and is
  160. assigned to one of the issue.
  161. """
  162. repo = pagure.lib.query._get_project(self.session, "test")
  163. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  164. issue.assignee_id = 3 # random
  165. self.session.add(issue)
  166. self.session.commit()
  167. user = tests.FakeUser(username="random")
  168. with tests.user_set(self.app.application, user):
  169. output = self.app.get("/test/issues")
  170. self.assertEqual(output.status_code, 200)
  171. output_text = output.get_data(as_text=True)
  172. self.assertIn("<title>Issues - test - Pagure</title>", output_text)
  173. self.assertIn(
  174. '<span class="fa fa-fw fa-exclamation-circle"></span> 2 Open Issues\n',
  175. output_text,
  176. )
  177. def test_view_issue_anonymous(self):
  178. """ Test accessing a private ticket when user is logged out. """
  179. output = self.app.get("/test/issue/1")
  180. self.assertEqual(output.status_code, 404)
  181. def test_view_issue_admin(self):
  182. """ Test accessing a private ticket when user is an admin of the
  183. project.
  184. """
  185. user = tests.FakeUser(username="pingou")
  186. with tests.user_set(self.app.application, user):
  187. output = self.app.get("/test/issue/1")
  188. self.assertEqual(output.status_code, 200)
  189. output_text = output.get_data(as_text=True)
  190. self.assertIn(
  191. "<title>Issue #1: Test issue #1 - test - Pagure</title>",
  192. output_text,
  193. )
  194. self.assertIn(
  195. '<span class="fa fa-fw text-success fa-exclamation-circle pt-1"></span>\n'
  196. ' <span class="text-success font-weight-bold">#1</span>\n',
  197. output_text,
  198. )
  199. def test_view_issue_author(self):
  200. """ Test accessing a private ticket when user opened the ticket.
  201. """
  202. user = tests.FakeUser(username="foo")
  203. with tests.user_set(self.app.application, user):
  204. output = self.app.get("/test/issue/1")
  205. self.assertEqual(output.status_code, 200)
  206. output_text = output.get_data(as_text=True)
  207. self.assertIn(
  208. "<title>Issue #1: Test issue #1 - test - Pagure</title>",
  209. output_text,
  210. )
  211. self.assertIn(
  212. '<span class="fa fa-fw text-success fa-exclamation-circle pt-1"></span>\n'
  213. ' <span class="text-success font-weight-bold">#1</span>\n',
  214. output_text,
  215. )
  216. def test_view_issue_authenticated(self):
  217. """ Test accessing a private ticket when user is authenticated but
  218. has no special access to the project.
  219. """
  220. user = tests.FakeUser(username="random")
  221. with tests.user_set(self.app.application, user):
  222. output = self.app.get("/test/issue/1")
  223. self.assertEqual(output.status_code, 404)
  224. def test_view_issue_authenticated_ticket(self):
  225. """ Test accessing a private ticket when user is authenticated and
  226. has ticket level access to the project.
  227. """
  228. repo = pagure.lib.query._get_project(self.session, "test")
  229. msg = pagure.lib.query.add_user_to_project(
  230. session=self.session,
  231. project=repo,
  232. new_user="random",
  233. user="pingou",
  234. access="ticket",
  235. )
  236. self.session.commit()
  237. self.assertEqual(msg, "User added")
  238. user = tests.FakeUser(username="random")
  239. with tests.user_set(self.app.application, user):
  240. output = self.app.get("/test/issue/1")
  241. self.assertEqual(output.status_code, 404)
  242. def test_view_issue_authenticated_commit(self):
  243. """ Test accessing a private ticket when user is authenticated and
  244. has commit level access to the project.
  245. """
  246. repo = pagure.lib.query._get_project(self.session, "test")
  247. msg = pagure.lib.query.add_user_to_project(
  248. session=self.session,
  249. project=repo,
  250. new_user="random",
  251. user="pingou",
  252. access="commit",
  253. )
  254. self.session.commit()
  255. self.assertEqual(msg, "User added")
  256. user = tests.FakeUser(username="random")
  257. with tests.user_set(self.app.application, user):
  258. output = self.app.get("/test/issue/1")
  259. self.assertEqual(output.status_code, 200)
  260. output_text = output.get_data(as_text=True)
  261. self.assertIn(
  262. "<title>Issue #1: Test issue #1 - test - Pagure</title>",
  263. output_text,
  264. )
  265. self.assertIn(
  266. '<span class="fa fa-fw text-success fa-exclamation-circle pt-1"></span>\n'
  267. ' <span class="text-success font-weight-bold">#1</span>\n',
  268. output_text,
  269. )
  270. def test_view_issue_authenticated_assigned(self):
  271. """ Test accessing a private ticket when user is authenticated and
  272. is assigned to one of the issue.
  273. """
  274. repo = pagure.lib.query._get_project(self.session, "test")
  275. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  276. issue.assignee_id = 3 # random
  277. self.session.add(issue)
  278. self.session.commit()
  279. user = tests.FakeUser(username="random")
  280. with tests.user_set(self.app.application, user):
  281. output = self.app.get("/test/issue/1")
  282. self.assertEqual(output.status_code, 200)
  283. output_text = output.get_data(as_text=True)
  284. self.assertIn(
  285. "<title>Issue #1: Test issue #1 - test - Pagure</title>",
  286. output_text,
  287. )
  288. self.assertIn(
  289. '<span class="fa fa-fw text-success fa-exclamation-circle pt-1"></span>\n'
  290. ' <span class="text-success font-weight-bold">#1</span>\n',
  291. output_text,
  292. )
  293. if __name__ == "__main__":
  294. unittest.main(verbosity=2)