test_pagure_flask_ui_issues_private.py 12 KB

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