1
0

test_pagure_flask_ui_issues_templates.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - 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. import pygit2
  13. from mock import patch, MagicMock
  14. sys.path.insert(
  15. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  16. )
  17. import pagure.lib.query
  18. import tests
  19. def create_templates(repopath):
  20. """ Create a couple of templates at the specified repo.
  21. """
  22. clone_repo = pygit2.Repository(repopath)
  23. # Create the RFE template
  24. os.mkdir(os.path.join(repopath, "templates"))
  25. template = os.path.join(repopath, "templates", "RFE.md")
  26. with open(template, "w") as stream:
  27. stream.write("RFE\n###\n\n* Idea description")
  28. clone_repo.index.add(os.path.join("templates", "RFE.md"))
  29. clone_repo.index.write()
  30. # Commit
  31. tree = clone_repo.index.write_tree()
  32. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  33. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  34. commit = clone_repo.create_commit(
  35. "refs/heads/master", # the name of the reference to update
  36. author,
  37. committer,
  38. "Add a RFE template",
  39. # binary string representing the tree object ID
  40. tree,
  41. # list of binary strings representing parents of the new commit
  42. [],
  43. )
  44. # Create the 2018-bid.md template
  45. template = os.path.join(repopath, "templates", "2018-bid.md")
  46. with open(template, "w") as stream:
  47. stream.write("Bid for 2018\n############\n\n* Location:")
  48. clone_repo.index.add(os.path.join("templates", "2018-bid.md"))
  49. clone_repo.index.write()
  50. # Commit
  51. tree = clone_repo.index.write_tree()
  52. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  53. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  54. commit = clone_repo.create_commit(
  55. "refs/heads/master", # the name of the reference to update
  56. author,
  57. committer,
  58. "Add a RFE template",
  59. # binary string representing the tree object ID
  60. tree,
  61. # list of binary strings representing parents of the new commit
  62. [commit.hex],
  63. )
  64. # Create the default.md template
  65. template = os.path.join(repopath, "templates", "default.md")
  66. with open(template, "w") as stream:
  67. stream.write("Report your issue")
  68. clone_repo.index.add(os.path.join("templates", "default.md"))
  69. clone_repo.index.write()
  70. # Commit
  71. tree = clone_repo.index.write_tree()
  72. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  73. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  74. clone_repo.create_commit(
  75. "refs/heads/master", # the name of the reference to update
  76. author,
  77. committer,
  78. "Add a default template",
  79. # binary string representing the tree object ID
  80. tree,
  81. # list of binary strings representing parents of the new commit
  82. [commit.hex],
  83. )
  84. class PagureFlaskIssuesTemplatetests(tests.Modeltests):
  85. """ Tests for flask issues controller of pagure """
  86. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  87. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  88. def setUp(self):
  89. """ Set up the environnment, run before every tests. """
  90. super(PagureFlaskIssuesTemplatetests, self).setUp()
  91. pagure.config.config["TICKETS_FOLDER"] = os.path.join(
  92. self.path, "tickets"
  93. )
  94. tests.create_projects(self.session)
  95. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  96. tests.create_projects_git(os.path.join(self.path, "tickets"))
  97. # Add a couple of templates to test2
  98. repopath = os.path.join(self.path, "tickets", "test2.git")
  99. create_templates(repopath)
  100. # Add a couple of templates to somenamespace/test3
  101. repopath = os.path.join(
  102. self.path, "tickets", "somenamespace", "test3.git"
  103. )
  104. create_templates(repopath)
  105. def test_new_issue_no_template(self):
  106. """ Test the new_issue endpoint when the project has no templates.
  107. """
  108. user = tests.FakeUser()
  109. with tests.user_set(self.app.application, user):
  110. output = self.app.get("/test/new_issue")
  111. self.assertEqual(output.status_code, 200)
  112. output_text = output.get_data(as_text=True)
  113. self.assertIn(
  114. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  115. output_text,
  116. )
  117. self.assertNotIn("Issue Templates", output_text)
  118. def test_new_issue_w_template(self):
  119. """ Test the new_issue endpoint when the project has templates. """
  120. user = tests.FakeUser()
  121. with tests.user_set(self.app.application, user):
  122. output = self.app.get("/test2/new_issue")
  123. self.assertEqual(output.status_code, 200)
  124. output_text = output.get_data(as_text=True)
  125. self.assertIn(
  126. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  127. output_text,
  128. )
  129. self.assertIn("Issue Templates", output_text)
  130. self.assertIn(
  131. '<a class="issue-template dropdown-item pointer" data-value="RFE">RFE</a>',
  132. output_text,
  133. )
  134. self.assertIn(
  135. '<a class="issue-template dropdown-item pointer" data-value="2018-bid">2018-bid</a>',
  136. output_text,
  137. )
  138. self.assertIn(
  139. '<a class="issue-template dropdown-item pointer" data-value="default">default</a>',
  140. output_text,
  141. )
  142. self.assertIn(
  143. 'placeholder="Enter your comment here" tabindex=2 required>'
  144. "Report your issue</textarea>",
  145. output_text,
  146. )
  147. def test_new_issue_w_specific_template(self):
  148. """ Test the new_issue endpoint when the project has templates. """
  149. user = tests.FakeUser()
  150. with tests.user_set(self.app.application, user):
  151. output = self.app.get("/test2/new_issue?template=2018-bid")
  152. self.assertEqual(output.status_code, 200)
  153. output_text = output.get_data(as_text=True)
  154. self.assertIn(
  155. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  156. output_text,
  157. )
  158. self.assertIn("Issue Templates", output_text)
  159. self.assertIn(
  160. '<a class="issue-template dropdown-item pointer" data-value="RFE">RFE</a>',
  161. output_text,
  162. )
  163. self.assertIn(
  164. '<a class="issue-template dropdown-item pointer" data-value="2018-bid">2018-bid</a>',
  165. output_text,
  166. )
  167. self.assertIn(
  168. '<a class="issue-template dropdown-item pointer" data-value="default">default</a>',
  169. output_text,
  170. )
  171. self.assertIn(
  172. 'placeholder="Enter your comment here" tabindex=2 required>'
  173. "Bid for 2018\n############",
  174. output_text,
  175. )
  176. def test_get_ticket_template_no_csrf(self):
  177. """ Test the get_ticket_template endpoint when the project has no
  178. templates.
  179. """
  180. user = tests.FakeUser()
  181. with tests.user_set(self.app.application, user):
  182. output = self.app.post("/pv/test/issue/template")
  183. self.assertEqual(output.status_code, 400)
  184. data = json.loads(output.get_data(as_text=True))
  185. self.assertEqual(
  186. data, {"code": "ERROR", "message": "Invalid input submitted"}
  187. )
  188. def test_get_ticket_template_no_template_specified(self):
  189. """ Test the get_ticket_template endpoint when not specifying which
  190. template to get.
  191. """
  192. user = tests.FakeUser()
  193. with tests.user_set(self.app.application, user):
  194. csrf = self.get_csrf()
  195. data = {"csrf_token": csrf}
  196. output = self.app.post("/pv/test/issue/template", data=data)
  197. self.assertEqual(output.status_code, 400)
  198. data = json.loads(output.get_data(as_text=True))
  199. self.assertEqual(
  200. data, {"code": "ERROR", "message": "No template provided"}
  201. )
  202. def test_get_ticket_template_no_project(self):
  203. """ Test the get_ticket_template endpoint when the project does not
  204. exist.
  205. """
  206. user = tests.FakeUser()
  207. with tests.user_set(self.app.application, user):
  208. csrf = self.get_csrf()
  209. data = {"csrf_token": csrf}
  210. output = self.app.post("/pv/foobar/issue/template", data=data)
  211. self.assertEqual(output.status_code, 404)
  212. def test_get_ticket_template_no_template(self):
  213. """ Test the get_ticket_template endpoint when the project has no
  214. templates.
  215. """
  216. user = tests.FakeUser()
  217. with tests.user_set(self.app.application, user):
  218. csrf = self.get_csrf()
  219. data = {"csrf_token": csrf}
  220. output = self.app.post(
  221. "/pv/test/issue/template?template=RFE", data=data
  222. )
  223. self.assertEqual(output.status_code, 404)
  224. data = json.loads(output.get_data(as_text=True))
  225. self.assertEqual(
  226. data, {"code": "ERROR", "message": "No such template found"}
  227. )
  228. def test_get_ticket_template_issue_tracker_disabled(self):
  229. """ Test the get_ticket_template endpoint when the project has
  230. disabled its issue tracker.
  231. """
  232. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  233. settings = repo.settings
  234. settings["issue_tracker"] = False
  235. repo.settings = settings
  236. self.session.add(repo)
  237. self.session.commit()
  238. user = tests.FakeUser()
  239. with tests.user_set(self.app.application, user):
  240. csrf = self.get_csrf()
  241. data = {"csrf_token": csrf}
  242. output = self.app.post(
  243. "/pv/test/issue/template?template=RFE", data=data
  244. )
  245. self.assertEqual(output.status_code, 404)
  246. data = json.loads(output.get_data(as_text=True))
  247. self.assertEqual(
  248. data,
  249. {
  250. "code": "ERROR",
  251. "message": "No issue tracker found for this project",
  252. },
  253. )
  254. def test_get_ticket_template_w_template(self):
  255. """ Test the get_ticket_template endpoint when the project has
  256. templates.
  257. """
  258. user = tests.FakeUser()
  259. with tests.user_set(self.app.application, user):
  260. csrf = self.get_csrf()
  261. data = {"csrf_token": csrf}
  262. output = self.app.post(
  263. "/pv/test2/issue/template?template=RFE", data=data
  264. )
  265. self.assertEqual(output.status_code, 200)
  266. data = json.loads(output.get_data(as_text=True))
  267. self.assertEqual(
  268. data,
  269. {"code": "OK", "message": "RFE\n###\n\n* Idea description"},
  270. )
  271. def test_get_ticket_template_w_template_namespace(self):
  272. """ Test the get_ticket_template endpoint when the project has
  273. templates and a namespace.
  274. """
  275. user = tests.FakeUser()
  276. with tests.user_set(self.app.application, user):
  277. csrf = self.get_csrf()
  278. data = {"csrf_token": csrf}
  279. output = self.app.post(
  280. "/pv/somenamespace/test3/issue/template?template=RFE",
  281. data=data,
  282. )
  283. self.assertEqual(output.status_code, 200)
  284. data = json.loads(output.get_data(as_text=True))
  285. self.assertEqual(
  286. data,
  287. {"code": "OK", "message": "RFE\n###\n\n* Idea description"},
  288. )
  289. if __name__ == "__main__":
  290. unittest.main(verbosity=2)