test_pagure_flask_ui_issues_read_only.py 15 KB

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