test_pagure_flask_ui_issues_private.py 12 KB

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