test_pagure_flask_ui_priorities.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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
  21. import pagure.lib
  22. import tests
  23. from pagure.lib.repo import PagureRepo
  24. class PagureFlaskPrioritiestests(tests.Modeltests):
  25. """ Tests for the behavior of priorities in pagure """
  26. def setUp(self):
  27. """ Set up the environnment, ran before every tests. """
  28. super(PagureFlaskPrioritiestests, self).setUp()
  29. pagure.APP.config['TESTING'] = True
  30. pagure.SESSION = self.session
  31. pagure.ui.SESSION = self.session
  32. pagure.ui.app.SESSION = self.session
  33. pagure.ui.filters.SESSION = self.session
  34. pagure.ui.repo.SESSION = self.session
  35. pagure.ui.issues.SESSION = self.session
  36. self.app = pagure.APP.test_client()
  37. @patch('pagure.lib.git.update_git')
  38. @patch('pagure.lib.notify.send_email')
  39. def test_ticket_with_no_priority(self, p_send_email, p_ugt):
  40. """ Test creating a ticket without priority. """
  41. p_send_email.return_value = True
  42. p_ugt.return_value = True
  43. tests.create_projects(self.session)
  44. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  45. user = tests.FakeUser()
  46. user.username = 'pingou'
  47. with tests.user_set(pagure.APP, user):
  48. # Get the CSRF token
  49. output = self.app.get('/test/new_issue')
  50. self.assertEqual(output.status_code, 200)
  51. self.assertTrue(
  52. '<div class="card-header">\n New issue'
  53. in output.data)
  54. csrf_token = output.data.split(
  55. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  56. data = {
  57. 'title': 'Test issue',
  58. 'issue_content': 'We really should improve on this issue',
  59. 'status': 'Open',
  60. 'csrf_token': csrf_token,
  61. }
  62. # Create the issue
  63. output = self.app.post(
  64. '/test/new_issue', data=data, follow_redirects=True)
  65. self.assertEqual(output.status_code, 200)
  66. self.assertIn(
  67. '<title>Issue #1: Test issue - test - Pagure</title>',
  68. output.data)
  69. self.assertIn(
  70. '<a class="btn btn-primary btn-sm" '
  71. 'href="/test/issue/1/edit" title="Edit this issue">',
  72. output.data)
  73. self.assertNotIn('<div id="priority_plain">', output.data)
  74. self.assertNotIn('<option value="1">High</option>', output.data)
  75. @patch('pagure.lib.git.update_git')
  76. @patch('pagure.lib.notify.send_email')
  77. def test_ticket_with_priorities(self, p_send_email, p_ugt):
  78. """ Test creating a ticket with priorities. """
  79. p_send_email.return_value = True
  80. p_ugt.return_value = True
  81. tests.create_projects(self.session)
  82. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  83. # Set some priorities
  84. repo = pagure.get_authorized_project(self.session, 'test')
  85. repo.priorities = {'1': 'High', '2': 'Normal'}
  86. self.session.add(repo)
  87. self.session.commit()
  88. user = tests.FakeUser()
  89. user.username = 'pingou'
  90. with tests.user_set(pagure.APP, user):
  91. # Get the CSRF token
  92. output = self.app.get('/test/new_issue')
  93. self.assertEqual(output.status_code, 200)
  94. self.assertTrue(
  95. '<div class="card-header">\n New issue'
  96. in output.data)
  97. csrf_token = output.data.split(
  98. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  99. data = {
  100. 'title': 'Test issue',
  101. 'issue_content': 'We really should improve on this issue',
  102. 'status': 'Open',
  103. 'csrf_token': csrf_token,
  104. }
  105. # Create the issue
  106. output = self.app.post(
  107. '/test/new_issue', data=data, follow_redirects=True)
  108. self.assertEqual(output.status_code, 200)
  109. self.assertIn(
  110. '<title>Issue #1: Test issue - test - Pagure</title>',
  111. output.data)
  112. self.assertIn(
  113. '<a class="btn btn-primary btn-sm" '
  114. 'href="/test/issue/1/edit" title="Edit this issue">',
  115. output.data)
  116. self.assertIn('<div id="priority_plain">', output.data)
  117. self.assertIn('<option value="1">High</option>', output.data)
  118. def test_update_priorities(self):
  119. """ Test updating priorities of a repo. """
  120. tests.create_projects(self.session)
  121. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  122. # Set some priorities
  123. repo = pagure.get_authorized_project(self.session, 'test')
  124. self.assertEqual(repo.priorities, {})
  125. user = tests.FakeUser()
  126. user.username = 'pingou'
  127. with tests.user_set(pagure.APP, user):
  128. # Get the CSRF token
  129. output = self.app.get('/test/settings')
  130. self.assertEqual(output.status_code, 200)
  131. self.assertIn(
  132. '<title>Settings - test - Pagure</title>', output.data)
  133. self.assertIn('<h3>Settings for test</h3>', output.data)
  134. csrf_token = output.data.split(
  135. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  136. data = {
  137. 'priority_weigth': 1,
  138. 'priority_title': 'High',
  139. }
  140. output = self.app.post(
  141. '/test/update/priorities', data=data, follow_redirects=True)
  142. self.assertEqual(output.status_code, 200)
  143. # Check the redirect
  144. self.assertIn(
  145. '<title>Settings - test - Pagure</title>', output.data)
  146. self.assertIn('<h3>Settings for test</h3>', output.data)
  147. # Check the result of the action -- None, no CSRF
  148. repo = pagure.get_authorized_project(self.session, 'test')
  149. self.assertEqual(repo.priorities, {})
  150. data = {
  151. 'priority_weigth': 1,
  152. 'priority_title': 'High',
  153. 'csrf_token': csrf_token,
  154. }
  155. output = self.app.post(
  156. '/test/update/priorities', data=data, follow_redirects=True)
  157. self.assertEqual(output.status_code, 200)
  158. # Check the redirect
  159. self.assertIn(
  160. '<title>Settings - test - Pagure</title>', output.data)
  161. self.assertIn('<h3>Settings for test</h3>', output.data)
  162. # Check the result of the action -- Priority recorded
  163. repo = pagure.get_authorized_project(self.session, 'test')
  164. self.assertEqual(repo.priorities, {u'': u'', u'1': u'High'})
  165. data = {
  166. 'priority_weigth': [1, 2, 3],
  167. 'priority_title': ['High', 'Normal', 'Low'],
  168. 'csrf_token': csrf_token,
  169. }
  170. output = self.app.post(
  171. '/test/update/priorities', data=data, follow_redirects=True)
  172. self.assertEqual(output.status_code, 200)
  173. # Check the redirect
  174. self.assertIn(
  175. '<title>Settings - test - Pagure</title>', output.data)
  176. self.assertIn('<h3>Settings for test</h3>', output.data)
  177. # Check the ordering
  178. self.assertTrue(
  179. output.data.find('High') < output.data.find('Normal'))
  180. self.assertTrue(
  181. output.data.find('Normal') < output.data.find('Low'))
  182. # Check the result of the action -- Priority recorded
  183. repo = pagure.get_authorized_project(self.session, 'test')
  184. self.assertEqual(
  185. repo.priorities,
  186. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  187. )
  188. # Check error - less weigths than titles
  189. data = {
  190. 'priority_weigth': [1, 2],
  191. 'priority_title': ['High', 'Normal', 'Low'],
  192. 'csrf_token': csrf_token,
  193. }
  194. output = self.app.post(
  195. '/test/update/priorities', data=data, follow_redirects=True)
  196. self.assertEqual(output.status_code, 200)
  197. # Check the redirect
  198. self.assertIn(
  199. '<title>Settings - test - Pagure</title>', output.data)
  200. self.assertIn('<h3>Settings for test</h3>', output.data)
  201. self.assertIn(
  202. '</button>\n'
  203. ' Priorities weights and titles are '
  204. 'not of the same length', output.data) # Check the result of the action -- Priorities un-changed
  205. repo = pagure.get_authorized_project(self.session, 'test')
  206. self.assertEqual(
  207. repo.priorities,
  208. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  209. )
  210. # Check error - weigths must be integer
  211. data = {
  212. 'priority_weigth': [1, 2, 'c'],
  213. 'priority_title': ['High', 'Normal', 'Low'],
  214. 'csrf_token': csrf_token,
  215. }
  216. output = self.app.post(
  217. '/test/update/priorities', data=data, follow_redirects=True)
  218. self.assertEqual(output.status_code, 200)
  219. # Check the redirect
  220. self.assertIn(
  221. '<title>Settings - test - Pagure</title>', output.data)
  222. self.assertIn('<h3>Settings for test</h3>', output.data)
  223. self.assertIn(
  224. '</button>\n'
  225. ' Priorities weights must be numbers',
  226. output.data)
  227. # Check the result of the action -- Priorities un-changed
  228. repo = pagure.get_authorized_project(self.session, 'test')
  229. self.assertEqual(
  230. repo.priorities,
  231. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  232. )
  233. # Check error - Twice the same priority weigth
  234. data = {
  235. 'priority_weigth': [1, 2, 2],
  236. 'priority_title': ['High', 'Normal', 'Low'],
  237. 'csrf_token': csrf_token,
  238. }
  239. output = self.app.post(
  240. '/test/update/priorities', data=data, follow_redirects=True)
  241. self.assertEqual(output.status_code, 200)
  242. # Check the redirect
  243. self.assertIn(
  244. '<title>Settings - test - Pagure</title>', output.data)
  245. self.assertIn('<h3>Settings for test</h3>', output.data)
  246. self.assertIn(
  247. '</button>\n'
  248. ' Priority weight 2 is present 2 times',
  249. output.data)
  250. # Check the result of the action -- Priorities un-changed
  251. repo = pagure.get_authorized_project(self.session, 'test')
  252. self.assertEqual(
  253. repo.priorities,
  254. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  255. )
  256. # Check error - Twice the same priority title
  257. data = {
  258. 'priority_weigth': [1, 2, 3],
  259. 'priority_title': ['High', 'Normal', 'Normal'],
  260. 'csrf_token': csrf_token,
  261. }
  262. output = self.app.post(
  263. '/test/update/priorities', data=data, follow_redirects=True)
  264. self.assertEqual(output.status_code, 200)
  265. # Check the redirect
  266. self.assertIn(
  267. '<title>Settings - test - Pagure</title>', output.data)
  268. self.assertIn('<h3>Settings for test</h3>', output.data)
  269. self.assertIn(
  270. '</button>\n'
  271. ' Priority Normal is present 2 times',
  272. output.data)
  273. # Check the result of the action -- Priorities un-changed
  274. repo = pagure.get_authorized_project(self.session, 'test')
  275. self.assertEqual(
  276. repo.priorities,
  277. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  278. )
  279. # Check the behavior if the project disabled the issue tracker
  280. settings = repo.settings
  281. settings['issue_tracker'] = False
  282. repo.settings = settings
  283. self.session.add(repo)
  284. self.session.commit()
  285. output = self.app.post(
  286. '/test/update/priorities', data=data)
  287. self.assertEqual(output.status_code, 404)
  288. # Check for an invalid project
  289. output = self.app.post(
  290. '/foo/update/priorities', data=data)
  291. self.assertEqual(output.status_code, 404)
  292. # Check for a non-admin user
  293. settings = repo.settings
  294. settings['issue_tracker'] = True
  295. repo.settings = settings
  296. self.session.add(repo)
  297. self.session.commit()
  298. user.username = 'ralph'
  299. with tests.user_set(pagure.APP, user):
  300. output = self.app.post(
  301. '/test/update/priorities', data=data)
  302. self.assertEqual(output.status_code, 403)
  303. @patch('pagure.lib.git.update_git')
  304. @patch('pagure.lib.notify.send_email')
  305. def test_reset_priorities(self, p_send_email, p_ugt):
  306. """ Test resetting the priorities of a repo. """
  307. p_send_email.return_value = True
  308. p_ugt.return_value = True
  309. tests.create_projects(self.session)
  310. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  311. # Start from scrach on priorities
  312. repo = pagure.lib._get_project(self.session, 'test')
  313. self.assertEqual(repo.priorities, {})
  314. user = tests.FakeUser()
  315. user.username = 'pingou'
  316. with tests.user_set(pagure.APP, user):
  317. # Get the CSRF token
  318. output = self.app.get('/test/settings')
  319. self.assertEqual(output.status_code, 200)
  320. self.assertIn(
  321. '<title>Settings - test - Pagure</title>', output.data)
  322. self.assertIn('<h3>Settings for test</h3>', output.data)
  323. csrf_token = output.data.split(
  324. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  325. # Set some priorities
  326. data = {
  327. 'priority_weigth': [1, 2, 3],
  328. 'priority_title': ['High', 'Normal', 'Low'],
  329. 'csrf_token': csrf_token,
  330. }
  331. output = self.app.post(
  332. '/test/update/priorities', data=data, follow_redirects=True)
  333. self.assertEqual(output.status_code, 200)
  334. self.assertIn(
  335. '<title>Settings - test - Pagure</title>', output.data)
  336. self.assertIn('<h3>Settings for test</h3>', output.data)
  337. # Check the result of the action -- Priority recorded
  338. repo = pagure.lib._get_project(self.session, 'test')
  339. self.assertEqual(
  340. repo.priorities,
  341. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  342. )
  343. # Create an issue
  344. data = {
  345. 'title': 'Test issue',
  346. 'issue_content': 'We really should improve on this issue',
  347. 'status': 'Open',
  348. 'csrf_token': csrf_token,
  349. }
  350. output = self.app.post(
  351. '/test/new_issue', data=data, follow_redirects=True)
  352. self.assertEqual(output.status_code, 200)
  353. self.assertIn(
  354. '<title>Issue #1: Test issue - test - Pagure</title>',
  355. output.data)
  356. self.assertIn(
  357. '<a class="btn btn-primary btn-sm" '
  358. 'href="/test/issue/1/edit" title="Edit this issue">',
  359. output.data)
  360. self.assertIn('<div id="priority_plain">', output.data)
  361. self.assertIn('<option value="1">High</option>', output.data)
  362. # Check that the ticket *does* have priorities
  363. output = self.app.get('/test/issue/1')
  364. self.assertEqual(output.status_code, 200)
  365. self.assertIn('<div id="priority_plain">', output.data)
  366. self.assertIn('<option value="1">High</option>', output.data)
  367. # Reset the priorities
  368. data = {
  369. 'csrf_token': csrf_token,
  370. }
  371. output = self.app.post(
  372. '/test/update/priorities', data=data, follow_redirects=True)
  373. self.assertEqual(output.status_code, 200)
  374. self.assertIn(
  375. '<title>Settings - test - Pagure</title>', output.data)
  376. self.assertIn('<h3>Settings for test</h3>', output.data)
  377. # Check that the issue list renders fine
  378. output = self.app.get('/test/issues')
  379. self.assertEqual(output.status_code, 200)
  380. # Check that the ticket *does not* have priorities
  381. output = self.app.get('/test/issue/1')
  382. self.assertEqual(output.status_code, 200)
  383. self.assertNotIn('<div id="priority_plain">', output.data)
  384. self.assertNotIn('<option value="1">High</option>', output.data)
  385. # Check the result of the action -- Priority recorded
  386. repo = pagure.lib._get_project(self.session, 'test')
  387. self.assertEqual(repo.priorities, {})
  388. if __name__ == '__main__':
  389. unittest.main(verbosity=2)