test_pagure_flask_ui_issues_private.py 12 KB

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