test_pagure_flask_ui_issues_read_only.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 json
  9. import unittest
  10. import sys
  11. import os
  12. from mock import patch, MagicMock
  13. sys.path.insert(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import pagure.lib.query # noqa
  16. import tests # noqa
  17. class PagureFlaskIssuesReadOnlytests(tests.Modeltests):
  18. """ Tests for flask issues controller of pagure with read-only 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(PagureFlaskIssuesReadOnlytests, self).setUp()
  24. tests.create_projects(self.session)
  25. tests.create_projects_git(os.path.join(self.path, 'repos'))
  26. # Make the project's issue tracker read-only
  27. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  28. settings = repo.settings
  29. settings['issue_tracker_read_only'] = True
  30. repo.settings = settings
  31. self.session.add(repo)
  32. self.session.commit()
  33. # Create a couple of issue
  34. msg = pagure.lib.query.new_issue(
  35. session=self.session,
  36. repo=repo,
  37. title='Test issue #1',
  38. content='We should work on this for the second time',
  39. user='foo',
  40. status='Open',
  41. private=True,
  42. )
  43. self.session.commit()
  44. self.assertEqual(msg.title, 'Test issue #1')
  45. msg = pagure.lib.query.new_issue(
  46. session=self.session,
  47. repo=repo,
  48. title='Test issue #2',
  49. content='We should work on this for the second time',
  50. user='foo',
  51. status='Open',
  52. private=False,
  53. )
  54. self.session.commit()
  55. self.assertEqual(msg.title, 'Test issue #2')
  56. def test_issue_list_authenticated_commit(self):
  57. """ Test the list of issues when user is authenticated and has
  58. access to the project.
  59. """
  60. user = tests.FakeUser(username='pingou')
  61. with tests.user_set(self.app.application, user):
  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(
  66. '<title>Issues - test - Pagure</title>', output_text)
  67. self.assertIn(
  68. '<span class="fa fa-fw fa-exclamation-circle"></span>'
  69. ' 2 Open Issues\n', output_text)
  70. def test_field_comment(self):
  71. """ Test if the field commit is present on the issue page.
  72. """
  73. user = tests.FakeUser(username='pingou')
  74. with tests.user_set(self.app.application, user):
  75. output = self.app.get('/test/issue/1')
  76. self.assertEqual(output.status_code, 200)
  77. output_text = output.get_data(as_text=True)
  78. self.assertIn(
  79. '<title>Issue #1: Test issue #1 - test - Pagure</title>',
  80. output_text)
  81. self.assertNotIn(
  82. 'value="Update Issue" title="Comment and Update Metadata" '
  83. 'tabindex=2 />', output_text)
  84. self.assertIn(
  85. 'This issue tracker is read-only.', output_text)
  86. def test_update_ticket(self):
  87. """ Test updating a ticket.
  88. """
  89. user = tests.FakeUser(username='pingou')
  90. with tests.user_set(self.app.application, user):
  91. output = self.app.post(
  92. '/test/issue/1/update', data={}, follow_redirects=True)
  93. self.assertEqual(output.status_code, 401)
  94. output_text = output.get_data(as_text=True)
  95. self.assertIn(
  96. '<title>Unauthorized :\'( - Pagure</title>', output_text)
  97. self.assertIn(
  98. '<p>The issue tracker for this project is read-only</p>',
  99. output_text)
  100. def test_edit_comment(self):
  101. """ Test editing a comment from a ticket.
  102. """
  103. user = tests.FakeUser(username='pingou')
  104. with tests.user_set(self.app.application, user):
  105. output = self.app.post(
  106. '/test/issue/1/comment/1/edit', data={},
  107. follow_redirects=True)
  108. self.assertEqual(output.status_code, 401)
  109. output_text = output.get_data(as_text=True)
  110. self.assertIn(
  111. '<title>Unauthorized :\'( - Pagure</title>', output_text)
  112. self.assertIn(
  113. '<p>The issue tracker for this project is read-only</p>',
  114. output_text)
  115. def test_edit_ticket(self):
  116. """ Test editing a ticket.
  117. """
  118. user = tests.FakeUser(username='pingou')
  119. with tests.user_set(self.app.application, user):
  120. output = self.app.post(
  121. '/test/issue/1/edit', data={}, follow_redirects=True)
  122. self.assertEqual(output.status_code, 401)
  123. output_text = output.get_data(as_text=True)
  124. self.assertIn(
  125. '<title>Unauthorized :\'( - Pagure</title>', output_text)
  126. self.assertIn(
  127. '<p>The issue tracker for this project is read-only</p>',
  128. output_text)
  129. def test_new_issue(self):
  130. """ Test creating a new ticket.
  131. """
  132. user = tests.FakeUser(username='pingou')
  133. with tests.user_set(self.app.application, user):
  134. output = self.app.post('/test/new_issue/', data={})
  135. self.assertEqual(output.status_code, 401)
  136. output_text = output.get_data(as_text=True)
  137. self.assertIn(
  138. '<title>Unauthorized :\'( - Pagure</title>', output_text)
  139. self.assertIn(
  140. '<p>The issue tracker for this project is read-only</p>',
  141. output_text)
  142. def test_deleting_issue(self):
  143. """ Test deleting a new ticket.
  144. """
  145. user = tests.FakeUser(username='pingou')
  146. with tests.user_set(self.app.application, user):
  147. output = self.app.post('/test/issue/1/drop', data={})
  148. self.assertEqual(output.status_code, 401)
  149. output_text = output.get_data(as_text=True)
  150. self.assertIn(
  151. '<title>Unauthorized :\'( - Pagure</title>', output_text)
  152. self.assertIn(
  153. '<p>The issue tracker for this project is read-only</p>',
  154. output_text)
  155. def test_uploading_to_issue(self):
  156. """ Test uploading to a new ticket.
  157. """
  158. user = tests.FakeUser(username='pingou')
  159. with tests.user_set(self.app.application, user):
  160. output = self.app.post('/test/issue/1/upload', data={})
  161. self.assertEqual(output.status_code, 401)
  162. output_text = output.get_data(as_text=True)
  163. self.assertIn(
  164. '<title>Unauthorized :\'( - Pagure</title>', output_text)
  165. self.assertIn(
  166. '<p>The issue tracker for this project is read-only</p>',
  167. output_text)
  168. class PagureFlaskAPIIssuesReadOnlytests(PagureFlaskIssuesReadOnlytests):
  169. """ Tests for flask API issues controller of pagure with read-only tickets
  170. """
  171. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  172. def setUp(self):
  173. """ Set up the environnment, ran before every tests. """
  174. super(PagureFlaskAPIIssuesReadOnlytests, self).setUp()
  175. def test_api_new_issue(self):
  176. """ Test creating a new ticket.
  177. """
  178. user = tests.FakeUser(username='pingou')
  179. with tests.user_set(self.app.application, user):
  180. output = self.app.post('/api/0/test/new_issue', data={})
  181. self.assertEqual(output.status_code, 401)
  182. data = json.loads(output.get_data(as_text=True))
  183. self.assertEqual(
  184. data,
  185. {
  186. u'error': u'The issue tracker of this project is read-only',
  187. u'error_code': u'ETRACKERREADONLY'
  188. }
  189. )
  190. def test_api_change_status_issue(self):
  191. """ Test closing a ticket. """
  192. user = tests.FakeUser(username='pingou')
  193. with tests.user_set(self.app.application, user):
  194. output = self.app.post('/api/0/test/issue/1/status', data={})
  195. self.assertEqual(output.status_code, 401)
  196. data = json.loads(output.get_data(as_text=True))
  197. self.assertEqual(
  198. data,
  199. {
  200. u'error': u'The issue tracker of this project is read-only',
  201. u'error_code': u'ETRACKERREADONLY'
  202. }
  203. )
  204. def test_api_change_milestone_issue(self):
  205. """ Test change the milestone of a ticket. """
  206. user = tests.FakeUser(username='pingou')
  207. with tests.user_set(self.app.application, user):
  208. output = self.app.post('/api/0/test/issue/1/milestone', data={})
  209. self.assertEqual(output.status_code, 401)
  210. data = json.loads(output.get_data(as_text=True))
  211. self.assertEqual(
  212. data,
  213. {
  214. u'error': u'The issue tracker of this project is read-only',
  215. u'error_code': u'ETRACKERREADONLY'
  216. }
  217. )
  218. def test_api_comment_issue(self):
  219. """ Test comment on a ticket. """
  220. user = tests.FakeUser(username='pingou')
  221. with tests.user_set(self.app.application, user):
  222. output = self.app.post('/api/0/test/issue/1/comment', data={})
  223. self.assertEqual(output.status_code, 401)
  224. data = json.loads(output.get_data(as_text=True))
  225. self.assertEqual(
  226. data,
  227. {
  228. u'error': u'The issue tracker of this project is read-only',
  229. u'error_code': u'ETRACKERREADONLY'
  230. }
  231. )
  232. def test_api_assign_issue(self):
  233. """ Test assigning a ticket. """
  234. user = tests.FakeUser(username='pingou')
  235. with tests.user_set(self.app.application, user):
  236. output = self.app.post('/api/0/test/issue/1/assign', data={})
  237. self.assertEqual(output.status_code, 401)
  238. data = json.loads(output.get_data(as_text=True))
  239. self.assertEqual(
  240. data,
  241. {
  242. u'error': u'The issue tracker of this project is read-only',
  243. u'error_code': u'ETRACKERREADONLY'
  244. }
  245. )
  246. def test_api_subscribe_issue(self):
  247. """ Test subscribing to a ticket. """
  248. user = tests.FakeUser(username='pingou')
  249. with tests.user_set(self.app.application, user):
  250. output = self.app.post('/api/0/test/issue/1/subscribe', data={})
  251. self.assertEqual(output.status_code, 401)
  252. data = json.loads(output.get_data(as_text=True))
  253. self.assertEqual(
  254. data,
  255. {
  256. u'error': u'The issue tracker of this project is read-only',
  257. u'error_code': u'ETRACKERREADONLY'
  258. }
  259. )
  260. def test_api_update_custom_field(self):
  261. """ Test updating a specific custom fields on a ticket. """
  262. user = tests.FakeUser(username='pingou')
  263. with tests.user_set(self.app.application, user):
  264. output = self.app.post('/api/0/test/issue/1/custom/foo', data={})
  265. self.assertEqual(output.status_code, 401)
  266. data = json.loads(output.get_data(as_text=True))
  267. self.assertEqual(
  268. data,
  269. {
  270. u'error': u'The issue tracker of this project is read-only',
  271. u'error_code': u'ETRACKERREADONLY'
  272. }
  273. )
  274. def test_api_update_custom_fields(self):
  275. """ Test updating custom fields on a ticket. """
  276. user = tests.FakeUser(username='pingou')
  277. with tests.user_set(self.app.application, user):
  278. output = self.app.post('/api/0/test/issue/1/custom', data={})
  279. self.assertEqual(output.status_code, 401)
  280. data = json.loads(output.get_data(as_text=True))
  281. self.assertEqual(
  282. data,
  283. {
  284. u'error': u'The issue tracker of this project is read-only',
  285. u'error_code': u'ETRACKERREADONLY'
  286. }
  287. )
  288. class PagureFlaskIssuesAndPRDisabledtests(tests.Modeltests):
  289. """ Tests for flask issues controller of pagure with tickets and PRs
  290. disabled.
  291. """
  292. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  293. def setUp(self):
  294. """ Set up the environnment, ran before every tests. """
  295. super(PagureFlaskIssuesAndPRDisabledtests, self).setUp()
  296. tests.create_projects(self.session)
  297. tests.create_projects_git(os.path.join(self.path, 'repos'))
  298. # Make the project's issue tracker read-only
  299. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  300. settings = repo.settings
  301. settings['pull_requests'] = False
  302. settings['issue_tracker_read_only'] = True
  303. repo.settings = settings
  304. self.session.add(repo)
  305. self.session.commit()
  306. # Create a couple of issue
  307. msg = pagure.lib.query.new_issue(
  308. session=self.session,
  309. repo=repo,
  310. title='Test issue #1',
  311. content='We should work on this for the second time',
  312. user='foo',
  313. status='Open',
  314. private=True,
  315. )
  316. self.session.commit()
  317. self.assertEqual(msg.title, 'Test issue #1')
  318. msg = pagure.lib.query.new_issue(
  319. session=self.session,
  320. repo=repo,
  321. title='Test issue #2',
  322. content='We should work on this for the second time',
  323. user='foo',
  324. status='Open',
  325. private=False,
  326. )
  327. self.session.commit()
  328. self.assertEqual(msg.title, 'Test issue #2')
  329. def test_edit_tag(self):
  330. """ Test editing a ticket tag.
  331. """
  332. user = tests.FakeUser(username='pingou')
  333. with tests.user_set(self.app.application, user):
  334. output = self.app.post('/test/tag/tag1/edit', data={})
  335. self.assertEqual(output.status_code, 401)
  336. output_text = output.get_data(as_text=True)
  337. self.assertIn(
  338. '<title>Unauthorized :\'( - Pagure</title>', output_text)
  339. self.assertIn(
  340. '<p>The issue tracker for this project is read-only</p>',
  341. output_text)
  342. def test_drop_tags(self):
  343. """ Test dropping a ticket tag.
  344. """
  345. user = tests.FakeUser(username='pingou')
  346. with tests.user_set(self.app.application, user):
  347. output = self.app.post('/test/droptag/', data={})
  348. self.assertEqual(output.status_code, 401)
  349. output_text = output.get_data(as_text=True)
  350. self.assertIn(
  351. '<title>Unauthorized :\'( - Pagure</title>', output_text)
  352. self.assertIn(
  353. '<p>The issue tracker for this project is read-only</p>',
  354. output_text)
  355. if __name__ == '__main__':
  356. unittest.main(verbosity=2)