test_pagure_flask_ui_issues_private.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. import unittest
  8. import sys
  9. import os
  10. from mock import patch, MagicMock
  11. sys.path.insert(0, os.path.join(os.path.dirname(
  12. os.path.abspath(__file__)), '..'))
  13. import pagure # noqa
  14. import pagure.lib # 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.get_authorized_project(self.session, 'test')
  39. msg = pagure.lib.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. ticketfolder=None
  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. ticketfolder=None
  60. )
  61. self.session.commit()
  62. self.assertEqual(msg.title, 'Test issue #2')
  63. def test_issue_list_anonymous(self):
  64. """ Test the list of issues when user is logged out. """
  65. output = self.app.get('/test/issues')
  66. self.assertEqual(output.status_code, 200)
  67. self.assertIn(
  68. u'div class="projectinfo m-t-1 m-b-1">\ntest project #1'
  69. u' </div>', output.data)
  70. self.assertIn(
  71. u'<h2>\n 1 Open Issues', output.data)
  72. def test_issue_list_admin(self):
  73. """ Test the list of issues when user is an admin of the project.
  74. """
  75. user = tests.FakeUser(username='pingou')
  76. with tests.user_set(self.app.application, user):
  77. output = self.app.get('/test/issues')
  78. self.assertEqual(output.status_code, 200)
  79. self.assertIn(
  80. u'div class="projectinfo m-t-1 m-b-1">\ntest project #1'
  81. u' </div>', output.data)
  82. self.assertIn(
  83. u'<h2>\n 2 Open Issues', output.data)
  84. def test_issue_list_author(self):
  85. """ Test the list of issues when user is an admin of the project.
  86. """
  87. user = tests.FakeUser(username='foo')
  88. with tests.user_set(self.app.application, user):
  89. output = self.app.get('/test/issues')
  90. self.assertEqual(output.status_code, 200)
  91. self.assertIn(
  92. u'div class="projectinfo m-t-1 m-b-1">\ntest project #1'
  93. u' </div>', output.data)
  94. self.assertIn(
  95. u'<h2>\n 2 Open Issues', output.data)
  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. self.assertIn(
  105. u'div class="projectinfo m-t-1 m-b-1">\ntest project #1'
  106. u' </div>', output.data)
  107. self.assertIn(
  108. u'<h2>\n 1 Open Issues', output.data)
  109. def test_issue_list_authenticated_ticket(self):
  110. """ Test the list of issues when user is authenticated but has
  111. ticket level access to the project.
  112. """
  113. repo = pagure.lib._get_project(self.session, 'test')
  114. msg = pagure.lib.add_user_to_project(
  115. session=self.session,
  116. project=repo,
  117. new_user='random',
  118. user='pingou',
  119. access='ticket',
  120. )
  121. self.session.commit()
  122. self.assertEqual(msg, 'User added')
  123. user = tests.FakeUser(username='random')
  124. with tests.user_set(self.app.application, user):
  125. output = self.app.get('/test/issues')
  126. self.assertEqual(output.status_code, 200)
  127. self.assertIn(
  128. u'div class="projectinfo m-t-1 m-b-1">\ntest project #1'
  129. u' </div>', output.data)
  130. self.assertIn(
  131. u'<h2>\n 1 Open Issues', output.data)
  132. def test_issue_list_authenticated_commit(self):
  133. """ Test the list of issues when user is authenticated but has
  134. commit level access to the project.
  135. """
  136. repo = pagure.lib._get_project(self.session, 'test')
  137. msg = pagure.lib.add_user_to_project(
  138. session=self.session,
  139. project=repo,
  140. new_user='random',
  141. user='pingou',
  142. access='commit',
  143. )
  144. self.session.commit()
  145. self.assertEqual(msg, 'User added')
  146. user = tests.FakeUser(username='random')
  147. with tests.user_set(self.app.application, user):
  148. output = self.app.get('/test/issues')
  149. self.assertEqual(output.status_code, 200)
  150. self.assertIn(
  151. u'div class="projectinfo m-t-1 m-b-1">\ntest project #1'
  152. u' </div>', output.data)
  153. self.assertIn(
  154. u'<h2>\n 2 Open Issues', output.data)
  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._get_project(self.session, 'test')
  160. issue = pagure.lib.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. self.assertIn(
  169. u'div class="projectinfo m-t-1 m-b-1">\ntest project #1'
  170. u' </div>', output.data)
  171. self.assertIn(
  172. u'<h2>\n 2 Open Issues', output.data)
  173. def test_view_issue_anonymous(self):
  174. """ Test accessing a private ticket when user is logged out. """
  175. output = self.app.get('/test/issue/1')
  176. self.assertEqual(output.status_code, 404)
  177. def test_view_issue_admin(self):
  178. """ Test accessing a private ticket when user is an admin of the
  179. project.
  180. """
  181. user = tests.FakeUser(username='pingou')
  182. with tests.user_set(self.app.application, user):
  183. output = self.app.get('/test/issue/1')
  184. self.assertEqual(output.status_code, 200)
  185. self.assertIn(
  186. u'div class="projectinfo m-t-1 m-b-1">\ntest project #1'
  187. u' </div>', output.data)
  188. self.assertIn(
  189. u'<title>Issue #1: Test issue #1 - test - Pagure</title>',
  190. output.data)
  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. self.assertIn(
  199. u'div class="projectinfo m-t-1 m-b-1">\ntest project #1'
  200. u' </div>', output.data)
  201. self.assertIn(
  202. u'<title>Issue #1: Test issue #1 - test - Pagure</title>',
  203. output.data)
  204. def test_view_issue_authenticated(self):
  205. """ Test accessing a private ticket when user is authenticated but
  206. has no special access to the project.
  207. """
  208. user = tests.FakeUser(username='random')
  209. with tests.user_set(self.app.application, user):
  210. output = self.app.get('/test/issue/1')
  211. self.assertEqual(output.status_code, 404)
  212. def test_view_issue_authenticated_ticket(self):
  213. """ Test accessing a private ticket when user is authenticated and
  214. has ticket level access to the project.
  215. """
  216. repo = pagure.lib._get_project(self.session, 'test')
  217. msg = pagure.lib.add_user_to_project(
  218. session=self.session,
  219. project=repo,
  220. new_user='random',
  221. user='pingou',
  222. access='ticket',
  223. )
  224. self.session.commit()
  225. self.assertEqual(msg, 'User added')
  226. user = tests.FakeUser(username='random')
  227. with tests.user_set(self.app.application, user):
  228. output = self.app.get('/test/issue/1')
  229. self.assertEqual(output.status_code, 404)
  230. def test_view_issue_authenticated_commit(self):
  231. """ Test accessing a private ticket when user is authenticated and
  232. has commit level access to the project.
  233. """
  234. repo = pagure.lib._get_project(self.session, 'test')
  235. msg = pagure.lib.add_user_to_project(
  236. session=self.session,
  237. project=repo,
  238. new_user='random',
  239. user='pingou',
  240. access='commit',
  241. )
  242. self.session.commit()
  243. self.assertEqual(msg, 'User added')
  244. user = tests.FakeUser(username='random')
  245. with tests.user_set(self.app.application, user):
  246. output = self.app.get('/test/issue/1')
  247. self.assertEqual(output.status_code, 200)
  248. self.assertIn(
  249. u'div class="projectinfo m-t-1 m-b-1">\ntest project #1'
  250. u' </div>', output.data)
  251. self.assertIn(
  252. u'<title>Issue #1: Test issue #1 - test - Pagure</title>',
  253. output.data)
  254. def test_view_issue_authenticated_assigned(self):
  255. """ Test accessing a private ticket when user is authenticated and
  256. is assigned to one of the issue.
  257. """
  258. repo = pagure.lib._get_project(self.session, 'test')
  259. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  260. issue.assignee_id = 3 # random
  261. self.session.add(issue)
  262. self.session.commit()
  263. user = tests.FakeUser(username='random')
  264. with tests.user_set(self.app.application, user):
  265. output = self.app.get('/test/issue/1')
  266. self.assertEqual(output.status_code, 200)
  267. self.assertIn(
  268. u'div class="projectinfo m-t-1 m-b-1">\ntest project #1'
  269. u' </div>', output.data)
  270. self.assertIn(
  271. u'<title>Issue #1: Test issue #1 - test - Pagure</title>',
  272. output.data)
  273. if __name__ == '__main__':
  274. unittest.main(verbosity=2)