test_pagure_flask_ui_priorities.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import datetime
  10. import json
  11. import unittest
  12. import shutil
  13. import sys
  14. import tempfile
  15. import os
  16. import pygit2
  17. from mock import patch
  18. sys.path.insert(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. import pagure.lib
  21. import tests
  22. from pagure.lib.repo import PagureRepo
  23. class PagureFlaskPrioritiestests(tests.Modeltests):
  24. """ Tests for the behavior of priorities in pagure """
  25. def setUp(self):
  26. """ Set up the environnment, ran before every tests. """
  27. super(PagureFlaskPrioritiestests, self).setUp()
  28. pagure.APP.config['TESTING'] = True
  29. pagure.SESSION = self.session
  30. pagure.ui.SESSION = self.session
  31. pagure.ui.app.SESSION = self.session
  32. pagure.ui.filters.SESSION = self.session
  33. pagure.ui.repo.SESSION = self.session
  34. pagure.ui.issues.SESSION = self.session
  35. pagure.APP.config['GIT_FOLDER'] = tests.HERE
  36. pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
  37. tests.HERE, 'requests')
  38. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  39. tests.HERE, 'tickets')
  40. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  41. tests.HERE, 'docs')
  42. self.app = pagure.APP.test_client()
  43. @patch('pagure.lib.git.update_git')
  44. @patch('pagure.lib.notify.send_email')
  45. def test_ticket_with_no_priority(self, p_send_email, p_ugt):
  46. """ Test creating a ticket without priority. """
  47. p_send_email.return_value = True
  48. p_ugt.return_value = True
  49. tests.create_projects(self.session)
  50. tests.create_projects_git(os.path.join(tests.HERE), bare=True)
  51. user = tests.FakeUser()
  52. user.username = 'pingou'
  53. with tests.user_set(pagure.APP, user):
  54. # Get the CSRF token
  55. output = self.app.get('/test/new_issue')
  56. self.assertEqual(output.status_code, 200)
  57. self.assertTrue(
  58. '<div class="card-header">\n New issue'
  59. in output.data)
  60. csrf_token = output.data.split(
  61. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  62. data = {
  63. 'title': 'Test issue',
  64. 'issue_content': 'We really should improve on this issue',
  65. 'status': 'Open',
  66. 'csrf_token': csrf_token,
  67. }
  68. # Create the issue
  69. output = self.app.post(
  70. '/test/new_issue', data=data, follow_redirects=True)
  71. self.assertEqual(output.status_code, 200)
  72. self.assertIn(
  73. '<title>Issue #1: Test issue - test - Pagure</title>',
  74. output.data)
  75. self.assertIn(
  76. '<a class="btn btn-primary btn-sm" '
  77. 'href="/test/issue/1/edit" title="Edit this issue">',
  78. output.data)
  79. self.assertNotIn('<div id="priority_plain">', output.data)
  80. self.assertNotIn('<option value="1">High</option>', output.data)
  81. @patch('pagure.lib.git.update_git')
  82. @patch('pagure.lib.notify.send_email')
  83. def test_ticket_with_priorities(self, p_send_email, p_ugt):
  84. """ Test creating a ticket with priorities. """
  85. p_send_email.return_value = True
  86. p_ugt.return_value = True
  87. tests.create_projects(self.session)
  88. tests.create_projects_git(os.path.join(tests.HERE), bare=True)
  89. # Set some priorities
  90. repo = pagure.lib.get_project(self.session, 'test')
  91. repo.priorities = {'1': 'High', '2': 'Normal'}
  92. self.session.add(repo)
  93. self.session.commit()
  94. user = tests.FakeUser()
  95. user.username = 'pingou'
  96. with tests.user_set(pagure.APP, user):
  97. # Get the CSRF token
  98. output = self.app.get('/test/new_issue')
  99. self.assertEqual(output.status_code, 200)
  100. self.assertTrue(
  101. '<div class="card-header">\n New issue'
  102. in output.data)
  103. csrf_token = output.data.split(
  104. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  105. data = {
  106. 'title': 'Test issue',
  107. 'issue_content': 'We really should improve on this issue',
  108. 'status': 'Open',
  109. 'csrf_token': csrf_token,
  110. }
  111. # Create the issue
  112. output = self.app.post(
  113. '/test/new_issue', data=data, follow_redirects=True)
  114. self.assertEqual(output.status_code, 200)
  115. self.assertIn(
  116. '<title>Issue #1: Test issue - test - Pagure</title>',
  117. output.data)
  118. self.assertIn(
  119. '<a class="btn btn-primary btn-sm" '
  120. 'href="/test/issue/1/edit" title="Edit this issue">',
  121. output.data)
  122. self.assertIn('<div id="priority_plain">', output.data)
  123. self.assertIn('<option value="1">High</option>', output.data)
  124. def test_update_priorities(self):
  125. """ Test updating priorities of a repo. """
  126. tests.create_projects(self.session)
  127. tests.create_projects_git(os.path.join(tests.HERE), bare=True)
  128. # Set some priorities
  129. repo = pagure.lib.get_project(self.session, 'test')
  130. self.assertEqual(repo.priorities, {})
  131. user = tests.FakeUser()
  132. user.username = 'pingou'
  133. with tests.user_set(pagure.APP, user):
  134. # Get the CSRF token
  135. output = self.app.get('/test/settings')
  136. self.assertEqual(output.status_code, 200)
  137. self.assertIn(
  138. '<title>Settings - test - Pagure</title>', output.data)
  139. self.assertIn('<h3>Settings for test</h3>', output.data)
  140. csrf_token = output.data.split(
  141. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  142. data = {
  143. 'priority_weigth': 1,
  144. 'priority_title': 'High',
  145. }
  146. output = self.app.post(
  147. '/test/update/priorities', data=data, follow_redirects=True)
  148. self.assertEqual(output.status_code, 200)
  149. # Check the redirect
  150. self.assertIn(
  151. '<title>Settings - test - Pagure</title>', output.data)
  152. self.assertIn('<h3>Settings for test</h3>', output.data)
  153. # Check the result of the action -- None, no CSRF
  154. repo = pagure.lib.get_project(self.session, 'test')
  155. self.assertEqual(repo.priorities, {})
  156. data = {
  157. 'priority_weigth': 1,
  158. 'priority_title': 'High',
  159. 'csrf_token': csrf_token,
  160. }
  161. output = self.app.post(
  162. '/test/update/priorities', data=data, follow_redirects=True)
  163. self.assertEqual(output.status_code, 200)
  164. # Check the redirect
  165. self.assertIn(
  166. '<title>Settings - test - Pagure</title>', output.data)
  167. self.assertIn('<h3>Settings for test</h3>', output.data)
  168. # Check the result of the action -- Priority recorded
  169. repo = pagure.lib.get_project(self.session, 'test')
  170. self.assertEqual(repo.priorities, {u'': u'', u'1': u'High'})
  171. data = {
  172. 'priority_weigth': [1, 2, 3],
  173. 'priority_title': ['High', 'Normal', 'Low'],
  174. 'csrf_token': csrf_token,
  175. }
  176. output = self.app.post(
  177. '/test/update/priorities', data=data, follow_redirects=True)
  178. self.assertEqual(output.status_code, 200)
  179. # Check the redirect
  180. self.assertIn(
  181. '<title>Settings - test - Pagure</title>', output.data)
  182. self.assertIn('<h3>Settings for test</h3>', output.data)
  183. # Check the result of the action -- Priority recorded
  184. repo = pagure.lib.get_project(self.session, 'test')
  185. self.assertEqual(
  186. repo.priorities,
  187. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  188. )
  189. # Check error - less weigths than titles
  190. data = {
  191. 'priority_weigth': [1, 2],
  192. 'priority_title': ['High', 'Normal', 'Low'],
  193. 'csrf_token': csrf_token,
  194. }
  195. output = self.app.post(
  196. '/test/update/priorities', data=data, follow_redirects=True)
  197. self.assertEqual(output.status_code, 200)
  198. # Check the redirect
  199. self.assertIn(
  200. '<title>Settings - test - Pagure</title>', output.data)
  201. self.assertIn('<h3>Settings for test</h3>', output.data)
  202. self.assertIn(
  203. '</button>\n'
  204. ' Priorities weights and titles are '
  205. 'not of the same length', output.data) # Check the result of the action -- Priorities un-changed
  206. repo = pagure.lib.get_project(self.session, 'test')
  207. self.assertEqual(
  208. repo.priorities,
  209. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  210. )
  211. # Check error - weigths must be integer
  212. data = {
  213. 'priority_weigth': [1, 2, 'c'],
  214. 'priority_title': ['High', 'Normal', 'Low'],
  215. 'csrf_token': csrf_token,
  216. }
  217. output = self.app.post(
  218. '/test/update/priorities', data=data, follow_redirects=True)
  219. self.assertEqual(output.status_code, 200)
  220. # Check the redirect
  221. self.assertIn(
  222. '<title>Settings - test - Pagure</title>', output.data)
  223. self.assertIn('<h3>Settings for test</h3>', output.data)
  224. self.assertIn(
  225. '</button>\n'
  226. ' Priorities weights must be numbers',
  227. output.data)
  228. # Check the result of the action -- Priorities un-changed
  229. repo = pagure.lib.get_project(self.session, 'test')
  230. self.assertEqual(
  231. repo.priorities,
  232. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  233. )
  234. # Check error - Twice the same priority weigth
  235. data = {
  236. 'priority_weigth': [1, 2, 2],
  237. 'priority_title': ['High', 'Normal', 'Low'],
  238. 'csrf_token': csrf_token,
  239. }
  240. output = self.app.post(
  241. '/test/update/priorities', data=data, follow_redirects=True)
  242. self.assertEqual(output.status_code, 200)
  243. # Check the redirect
  244. self.assertIn(
  245. '<title>Settings - test - Pagure</title>', output.data)
  246. self.assertIn('<h3>Settings for test</h3>', output.data)
  247. self.assertIn(
  248. '</button>\n'
  249. ' Priority weight 2 is present 2 times',
  250. output.data)
  251. # Check the result of the action -- Priorities un-changed
  252. repo = pagure.lib.get_project(self.session, 'test')
  253. self.assertEqual(
  254. repo.priorities,
  255. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  256. )
  257. # Check error - Twice the same priority title
  258. data = {
  259. 'priority_weigth': [1, 2, 3],
  260. 'priority_title': ['High', 'Normal', 'Normal'],
  261. 'csrf_token': csrf_token,
  262. }
  263. output = self.app.post(
  264. '/test/update/priorities', data=data, follow_redirects=True)
  265. self.assertEqual(output.status_code, 200)
  266. # Check the redirect
  267. self.assertIn(
  268. '<title>Settings - test - Pagure</title>', output.data)
  269. self.assertIn('<h3>Settings for test</h3>', output.data)
  270. self.assertIn(
  271. '</button>\n'
  272. ' Priority Normal is present 2 times',
  273. output.data)
  274. # Check the result of the action -- Priorities un-changed
  275. repo = pagure.lib.get_project(self.session, 'test')
  276. self.assertEqual(
  277. repo.priorities,
  278. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  279. )
  280. # Check the behavior if the project disabled the issue tracker
  281. settings = repo.settings
  282. settings['issue_tracker'] = False
  283. repo.settings = settings
  284. self.session.add(repo)
  285. self.session.commit()
  286. output = self.app.post(
  287. '/test/update/priorities', data=data)
  288. self.assertEqual(output.status_code, 404)
  289. # Check for an invalid project
  290. output = self.app.post(
  291. '/foo/update/priorities', data=data)
  292. self.assertEqual(output.status_code, 404)
  293. # Check for a non-admin user
  294. settings = repo.settings
  295. settings['issue_tracker'] = True
  296. repo.settings = settings
  297. self.session.add(repo)
  298. self.session.commit()
  299. user.username = 'ralph'
  300. with tests.user_set(pagure.APP, user):
  301. output = self.app.post(
  302. '/test/update/priorities', data=data)
  303. self.assertEqual(output.status_code, 403)
  304. if __name__ == '__main__':
  305. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  306. PagureFlaskPrioritiestests)
  307. unittest.TextTestRunner(verbosity=2).run(SUITE)