test_pagure_flask_ui_issues_templates.py 12 KB

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