test_pagure_flask_ui_issues_read_only.py 15 KB

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