test_pagure_flask_ui_issues_templates.py 12 KB

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