test_pagure_flask_ui_priorities.py 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016-2017 - 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, MagicMock
  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. @patch('pagure.lib.git.update_git')
  27. @patch('pagure.lib.notify.send_email')
  28. def test_ticket_with_no_priority(self, p_send_email, p_ugt):
  29. """ Test creating a ticket without priority. """
  30. p_send_email.return_value = True
  31. p_ugt.return_value = True
  32. tests.create_projects(self.session)
  33. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  34. user = tests.FakeUser()
  35. user.username = 'pingou'
  36. with tests.user_set(self.app.application, user):
  37. # Get the CSRF token
  38. output = self.app.get('/test/new_issue')
  39. self.assertEqual(output.status_code, 200)
  40. self.assertTrue(
  41. '<div class="card-header">\n New issue'
  42. in output.data)
  43. csrf_token = output.data.split(
  44. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  45. data = {
  46. 'title': 'Test issue',
  47. 'issue_content': 'We really should improve on this issue',
  48. 'status': 'Open',
  49. 'csrf_token': csrf_token,
  50. }
  51. # Create the issue
  52. output = self.app.post(
  53. '/test/new_issue', data=data, follow_redirects=True)
  54. self.assertEqual(output.status_code, 200)
  55. self.assertIn(
  56. '<title>Issue #1: Test issue - test - Pagure</title>',
  57. output.data)
  58. self.assertIn(
  59. '<a class="btn btn-primary btn-sm" '
  60. 'href="/test/issue/1/edit" title="Edit this issue">',
  61. output.data)
  62. self.assertNotIn('<div id="priority_plain">', output.data)
  63. self.assertNotIn('<option value="1">High</option>', output.data)
  64. @patch('pagure.lib.git.update_git')
  65. @patch('pagure.lib.notify.send_email')
  66. def test_ticket_with_priorities(self, p_send_email, p_ugt):
  67. """ Test creating a ticket with priorities. """
  68. p_send_email.return_value = True
  69. p_ugt.return_value = True
  70. tests.create_projects(self.session)
  71. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  72. # Set some priorities
  73. repo = pagure.lib.get_authorized_project(self.session, 'test')
  74. repo.priorities = {'1': 'High', '2': 'Normal'}
  75. self.session.add(repo)
  76. self.session.commit()
  77. user = tests.FakeUser()
  78. user.username = 'pingou'
  79. with tests.user_set(self.app.application, user):
  80. # Get the CSRF token
  81. output = self.app.get('/test/new_issue')
  82. self.assertEqual(output.status_code, 200)
  83. self.assertTrue(
  84. '<div class="card-header">\n New issue'
  85. in output.data)
  86. csrf_token = output.data.split(
  87. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  88. data = {
  89. 'title': 'Test issue',
  90. 'issue_content': 'We really should improve on this issue',
  91. 'status': 'Open',
  92. 'csrf_token': csrf_token,
  93. }
  94. # Create the issue
  95. output = self.app.post(
  96. '/test/new_issue', data=data, follow_redirects=True)
  97. self.assertEqual(output.status_code, 200)
  98. self.assertIn(
  99. '<title>Issue #1: Test issue - test - Pagure</title>',
  100. output.data)
  101. self.assertIn(
  102. '<a class="btn btn-primary btn-sm" '
  103. 'href="/test/issue/1/edit" title="Edit this issue">',
  104. output.data)
  105. self.assertIn('<div id="priority_plain">', output.data)
  106. self.assertIn('<option value="1">High</option>', output.data)
  107. def test_update_priorities(self):
  108. """ Test updating priorities of a repo. """
  109. tests.create_projects(self.session)
  110. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  111. # Set some priorities
  112. repo = pagure.lib.get_authorized_project(self.session, 'test')
  113. self.assertEqual(repo.priorities, {})
  114. user = tests.FakeUser()
  115. user.username = 'pingou'
  116. with tests.user_set(self.app.application, user):
  117. # Get the CSRF token
  118. output = self.app.get('/test/settings')
  119. self.assertEqual(output.status_code, 200)
  120. self.assertIn(
  121. '<title>Settings - test - Pagure</title>', output.data)
  122. self.assertIn('<h3>Settings for test</h3>', output.data)
  123. csrf_token = output.data.split(
  124. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  125. data = {
  126. 'priority_weigth': 1,
  127. 'priority_title': 'High',
  128. }
  129. output = self.app.post(
  130. '/test/update/priorities', data=data, follow_redirects=True)
  131. self.assertEqual(output.status_code, 200)
  132. # Check the redirect
  133. self.assertIn(
  134. '<title>Settings - test - Pagure</title>', output.data)
  135. self.assertIn('<h3>Settings for test</h3>', output.data)
  136. # Check the result of the action -- None, no CSRF
  137. repo = pagure.lib.get_authorized_project(self.session, 'test')
  138. self.assertEqual(repo.priorities, {})
  139. data = {
  140. 'priority_weigth': 1,
  141. 'priority_title': 'High',
  142. 'csrf_token': csrf_token,
  143. }
  144. output = self.app.post(
  145. '/test/update/priorities', data=data, follow_redirects=True)
  146. self.assertEqual(output.status_code, 200)
  147. # Check the redirect
  148. self.assertIn(
  149. '<title>Settings - test - Pagure</title>', output.data)
  150. self.assertIn('<h3>Settings for test</h3>', output.data)
  151. # Check the result of the action -- Priority recorded
  152. self.session.commit()
  153. repo = pagure.lib.get_authorized_project(self.session, 'test')
  154. self.assertEqual(repo.priorities, {u'': u'', u'1': u'High'})
  155. data = {
  156. 'priority_weigth': [1, 2, 3],
  157. 'priority_title': ['High', 'Normal', 'Low'],
  158. 'csrf_token': csrf_token,
  159. }
  160. output = self.app.post(
  161. '/test/update/priorities', data=data, follow_redirects=True)
  162. self.assertEqual(output.status_code, 200)
  163. # Check the redirect
  164. self.assertIn(
  165. '<title>Settings - test - Pagure</title>', output.data)
  166. self.assertIn('<h3>Settings for test</h3>', output.data)
  167. # Check the ordering
  168. self.assertTrue(
  169. output.data.find('High') < output.data.find('Normal'))
  170. self.assertTrue(
  171. output.data.find('Normal') < output.data.find('Low'))
  172. # Check the result of the action -- Priority recorded
  173. self.session.commit()
  174. repo = pagure.lib.get_authorized_project(self.session, 'test')
  175. self.assertEqual(
  176. repo.priorities,
  177. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  178. )
  179. # Check error - less weigths than titles
  180. data = {
  181. 'priority_weigth': [1, 2],
  182. 'priority_title': ['High', 'Normal', 'Low'],
  183. 'csrf_token': csrf_token,
  184. }
  185. output = self.app.post(
  186. '/test/update/priorities', data=data, follow_redirects=True)
  187. self.assertEqual(output.status_code, 200)
  188. # Check the redirect
  189. self.assertIn(
  190. '<title>Settings - test - Pagure</title>', output.data)
  191. self.assertIn('<h3>Settings for test</h3>', output.data)
  192. self.assertIn(
  193. '</button>\n'
  194. ' Priorities weights and titles are '
  195. 'not of the same length', output.data)
  196. # Check the result of the action -- Priorities un-changed
  197. self.session.commit()
  198. repo = pagure.lib.get_authorized_project(self.session, 'test')
  199. self.assertEqual(
  200. repo.priorities,
  201. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  202. )
  203. # Check error - weigths must be integer
  204. data = {
  205. 'priority_weigth': [1, 2, 'c'],
  206. 'priority_title': ['High', 'Normal', 'Low'],
  207. 'csrf_token': csrf_token,
  208. }
  209. output = self.app.post(
  210. '/test/update/priorities', data=data, follow_redirects=True)
  211. self.assertEqual(output.status_code, 200)
  212. # Check the redirect
  213. self.assertIn(
  214. '<title>Settings - test - Pagure</title>', output.data)
  215. self.assertIn('<h3>Settings for test</h3>', output.data)
  216. self.assertIn(
  217. '</button>\n'
  218. ' Priorities weights must be numbers',
  219. output.data)
  220. # Check the result of the action -- Priorities un-changed
  221. self.session.commit()
  222. repo = pagure.lib.get_authorized_project(self.session, 'test')
  223. self.assertEqual(
  224. repo.priorities,
  225. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  226. )
  227. # Check error - Twice the same priority weigth
  228. data = {
  229. 'priority_weigth': [1, 2, 2],
  230. 'priority_title': ['High', 'Normal', 'Low'],
  231. 'csrf_token': csrf_token,
  232. }
  233. output = self.app.post(
  234. '/test/update/priorities', data=data, follow_redirects=True)
  235. self.assertEqual(output.status_code, 200)
  236. # Check the redirect
  237. self.assertIn(
  238. '<title>Settings - test - Pagure</title>', output.data)
  239. self.assertIn('<h3>Settings for test</h3>', output.data)
  240. self.assertIn(
  241. '</button>\n'
  242. ' Priority weight 2 is present 2 times',
  243. output.data)
  244. # Check the result of the action -- Priorities un-changed
  245. self.session.commit()
  246. repo = pagure.lib.get_authorized_project(self.session, 'test')
  247. self.assertEqual(
  248. repo.priorities,
  249. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  250. )
  251. # Check error - Twice the same priority title
  252. data = {
  253. 'priority_weigth': [1, 2, 3],
  254. 'priority_title': ['High', 'Normal', 'Normal'],
  255. 'csrf_token': csrf_token,
  256. }
  257. output = self.app.post(
  258. '/test/update/priorities', data=data, follow_redirects=True)
  259. self.assertEqual(output.status_code, 200)
  260. # Check the redirect
  261. self.assertIn(
  262. '<title>Settings - test - Pagure</title>', output.data)
  263. self.assertIn('<h3>Settings for test</h3>', output.data)
  264. self.assertIn(
  265. '</button>\n'
  266. ' Priority Normal is present 2 times',
  267. output.data)
  268. # Check the result of the action -- Priorities un-changed
  269. self.session.commit()
  270. repo = pagure.lib.get_authorized_project(self.session, 'test')
  271. self.assertEqual(
  272. repo.priorities,
  273. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  274. )
  275. # Check the behavior if the project disabled the issue tracker
  276. settings = repo.settings
  277. settings['issue_tracker'] = False
  278. repo.settings = settings
  279. self.session.add(repo)
  280. self.session.commit()
  281. output = self.app.post(
  282. '/test/update/priorities', data=data)
  283. self.assertEqual(output.status_code, 404)
  284. # Check for an invalid project
  285. output = self.app.post(
  286. '/foo/update/priorities', data=data)
  287. self.assertEqual(output.status_code, 404)
  288. # Check for a non-admin user
  289. settings = repo.settings
  290. settings['issue_tracker'] = True
  291. repo.settings = settings
  292. self.session.add(repo)
  293. self.session.commit()
  294. user.username = 'ralph'
  295. with tests.user_set(self.app.application, user):
  296. output = self.app.post(
  297. '/test/update/priorities', data=data)
  298. self.assertEqual(output.status_code, 403)
  299. @patch('pagure.lib.git.update_git')
  300. @patch('pagure.lib.notify.send_email')
  301. def test_reset_priorities(self, p_send_email, p_ugt):
  302. """ Test resetting the priorities of a repo. """
  303. p_send_email.return_value = True
  304. p_ugt.return_value = True
  305. tests.create_projects(self.session)
  306. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  307. # Start from scrach on priorities
  308. repo = pagure.lib._get_project(self.session, 'test')
  309. self.assertEqual(repo.priorities, {})
  310. user = tests.FakeUser()
  311. user.username = 'pingou'
  312. with tests.user_set(self.app.application, user):
  313. # Get the CSRF token
  314. output = self.app.get('/test/settings')
  315. self.assertEqual(output.status_code, 200)
  316. self.assertIn(
  317. '<title>Settings - test - Pagure</title>', output.data)
  318. self.assertIn('<h3>Settings for test</h3>', output.data)
  319. csrf_token = output.data.split(
  320. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  321. # Set some priorities
  322. data = {
  323. 'priority_weigth': [1, 2, 3],
  324. 'priority_title': ['High', 'Normal', 'Low'],
  325. 'csrf_token': csrf_token,
  326. }
  327. output = self.app.post(
  328. '/test/update/priorities', data=data, follow_redirects=True)
  329. self.assertEqual(output.status_code, 200)
  330. self.assertIn(
  331. '<title>Settings - test - Pagure</title>', output.data)
  332. self.assertIn('<h3>Settings for test</h3>', output.data)
  333. # Check the result of the action -- Priority recorded
  334. self.session.commit()
  335. repo = pagure.lib._get_project(self.session, 'test')
  336. self.assertEqual(
  337. repo.priorities,
  338. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  339. )
  340. # Create an issue
  341. data = {
  342. 'title': 'Test issue',
  343. 'issue_content': 'We really should improve on this issue',
  344. 'status': 'Open',
  345. 'csrf_token': csrf_token,
  346. }
  347. output = self.app.post(
  348. '/test/new_issue', data=data, follow_redirects=True)
  349. self.assertEqual(output.status_code, 200)
  350. self.assertIn(
  351. '<title>Issue #1: Test issue - test - Pagure</title>',
  352. output.data)
  353. self.assertIn(
  354. '<a class="btn btn-primary btn-sm" '
  355. 'href="/test/issue/1/edit" title="Edit this issue">',
  356. output.data)
  357. self.assertIn('<div id="priority_plain">', output.data)
  358. self.assertIn('<option value="1">High</option>', output.data)
  359. # Check that the ticket *does* have priorities
  360. output = self.app.get('/test/issue/1')
  361. self.assertEqual(output.status_code, 200)
  362. self.assertIn('<div id="priority_plain">', output.data)
  363. self.assertIn('<option value="1">High</option>', output.data)
  364. # Reset the priorities
  365. data = {
  366. 'csrf_token': csrf_token,
  367. }
  368. output = self.app.post(
  369. '/test/update/priorities', data=data, follow_redirects=True)
  370. self.assertEqual(output.status_code, 200)
  371. self.assertIn(
  372. '<title>Settings - test - Pagure</title>', output.data)
  373. self.assertIn('<h3>Settings for test</h3>', output.data)
  374. # Check that the issue list renders fine
  375. output = self.app.get('/test/issues')
  376. self.assertEqual(output.status_code, 200)
  377. # Check that the ticket *does not* have priorities
  378. output = self.app.get('/test/issue/1')
  379. self.assertEqual(output.status_code, 200)
  380. self.assertNotIn('<div id="priority_plain">', output.data)
  381. self.assertNotIn('<option value="1">High</option>', output.data)
  382. # Check the result of the action -- Priority reset
  383. self.session.commit()
  384. repo = pagure.lib._get_project(self.session, 'test')
  385. self.assertEqual(repo.priorities, {})
  386. @patch('pagure.lib.git.update_git')
  387. @patch('pagure.lib.notify.send_email')
  388. def test_reset_priorities_None(self, p_send_email, p_ugt):
  389. """ Test resetting the priorities of a repo. """
  390. p_send_email.return_value = True
  391. p_ugt.return_value = True
  392. tests.create_projects(self.session)
  393. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  394. # Start from scrach on priorities
  395. repo = pagure.lib._get_project(self.session, 'test')
  396. self.assertEqual(repo.priorities, {})
  397. user = tests.FakeUser()
  398. user.username = 'pingou'
  399. with tests.user_set(self.app.application, user):
  400. # Get the CSRF token
  401. output = self.app.get('/test/settings')
  402. self.assertEqual(output.status_code, 200)
  403. self.assertIn(
  404. '<title>Settings - test - Pagure</title>', output.data)
  405. self.assertIn('<h3>Settings for test</h3>', output.data)
  406. csrf_token = output.data.split(
  407. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  408. # Set some priorities
  409. data = {
  410. 'priority_weigth': [1, 2, 3],
  411. 'priority_title': ['High', 'Normal', 'Low'],
  412. 'csrf_token': csrf_token,
  413. }
  414. output = self.app.post(
  415. '/test/update/priorities', data=data, follow_redirects=True)
  416. self.assertEqual(output.status_code, 200)
  417. self.assertIn(
  418. '<title>Settings - test - Pagure</title>', output.data)
  419. self.assertIn('<h3>Settings for test</h3>', output.data)
  420. # Check the result of the action -- Priority recorded
  421. self.session.commit()
  422. repo = pagure.lib._get_project(self.session, 'test')
  423. self.assertEqual(
  424. repo.priorities,
  425. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  426. )
  427. # Create an issue
  428. data = {
  429. 'title': 'Test issue',
  430. 'issue_content': 'We really should improve on this issue',
  431. 'status': 'Open',
  432. 'csrf_token': csrf_token,
  433. }
  434. output = self.app.post(
  435. '/test/new_issue', data=data, follow_redirects=True)
  436. self.assertEqual(output.status_code, 200)
  437. self.assertIn(
  438. '<title>Issue #1: Test issue - test - Pagure</title>',
  439. output.data)
  440. self.assertIn(
  441. '<a class="btn btn-primary btn-sm" '
  442. 'href="/test/issue/1/edit" title="Edit this issue">',
  443. output.data)
  444. self.assertIn('<div id="priority_plain">', output.data)
  445. self.assertIn('<option value="1">High</option>', output.data)
  446. # Check that the ticket *does* have priorities
  447. output = self.app.get('/test/issue/1')
  448. self.assertEqual(output.status_code, 200)
  449. self.assertIn('<div id="priority_plain">', output.data)
  450. self.assertIn('<option value="1">High</option>', output.data)
  451. # Reset the priorities
  452. data = {
  453. 'priority': None,
  454. 'csrf_token': csrf_token,
  455. }
  456. output = self.app.post(
  457. '/test/update/priorities', data=data, follow_redirects=True)
  458. self.assertEqual(output.status_code, 200)
  459. self.assertIn(
  460. '<title>Settings - test - Pagure</title>', output.data)
  461. self.assertIn('<h3>Settings for test</h3>', output.data)
  462. # Check that the issue list renders fine
  463. output = self.app.get('/test/issues')
  464. self.assertEqual(output.status_code, 200)
  465. # Check that the ticket *does not* have priorities
  466. output = self.app.get('/test/issue/1')
  467. self.assertEqual(output.status_code, 200)
  468. self.assertNotIn('<div id="priority_plain">', output.data)
  469. self.assertNotIn('<option value="1">High</option>', output.data)
  470. # Check the result of the action -- Priority recorded
  471. self.session.commit()
  472. repo = pagure.lib._get_project(self.session, 'test')
  473. self.assertEqual(repo.priorities, {})
  474. @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
  475. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  476. def test_set_priority_1_and_back(self):
  477. """ Test setting the priority of a ticket to 1. """
  478. tests.create_projects(self.session)
  479. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  480. # Start from scrach on priorities
  481. repo = pagure.lib._get_project(self.session, 'test')
  482. self.assertEqual(repo.priorities, {})
  483. user = tests.FakeUser()
  484. user.username = 'pingou'
  485. with tests.user_set(self.app.application, user):
  486. # Get the CSRF token
  487. output = self.app.get('/test/settings')
  488. self.assertEqual(output.status_code, 200)
  489. self.assertIn(
  490. '<title>Settings - test - Pagure</title>', output.data)
  491. self.assertIn('<h3>Settings for test</h3>', output.data)
  492. csrf_token = output.data.split(
  493. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  494. # Set some priorities
  495. data = {
  496. 'priority_weigth': [-1, 0, 1, 2, 3],
  497. 'priority_title': [
  498. 'Sky Falling', 'Urgent', 'High', 'Normal', 'Low'],
  499. 'csrf_token': csrf_token,
  500. }
  501. output = self.app.post(
  502. '/test/update/priorities', data=data, follow_redirects=True)
  503. self.assertEqual(output.status_code, 200)
  504. self.assertIn(
  505. '<title>Settings - test - Pagure</title>', output.data)
  506. self.assertIn('<h3>Settings for test</h3>', output.data)
  507. # Check the result of the action -- Priority recorded
  508. self.session.commit()
  509. repo = pagure.lib._get_project(self.session, 'test')
  510. self.assertEqual(
  511. repo.priorities,
  512. {u'': u'', u'-1': u'Sky Falling', u'0': u'Urgent',
  513. u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  514. )
  515. # Create an issue
  516. data = {
  517. 'title': 'Test issue',
  518. 'issue_content': 'We really should improve on this issue',
  519. 'status': 'Open',
  520. 'csrf_token': csrf_token,
  521. }
  522. output = self.app.post(
  523. '/test/new_issue', data=data, follow_redirects=True)
  524. self.assertEqual(output.status_code, 200)
  525. self.assertIn(
  526. '<title>Issue #1: Test issue - test - Pagure</title>',
  527. output.data)
  528. self.assertIn(
  529. '<a class="btn btn-primary btn-sm" '
  530. 'href="/test/issue/1/edit" title="Edit this issue">',
  531. output.data)
  532. self.assertIn('<div id="priority_plain">', output.data)
  533. self.assertIn('<option value="1">High</option>', output.data)
  534. # Check that the ticket *does* have priorities
  535. output = self.app.get('/test/issue/1')
  536. self.assertEqual(output.status_code, 200)
  537. self.assertIn('<div id="priority_plain">', output.data)
  538. self.assertIn(
  539. '<option value="-1">Sky Falling</option>', output.data)
  540. self.assertIn('<option value="0">Urgent</option>', output.data)
  541. self.assertIn('<option value="1">High</option>', output.data)
  542. # Set the priority to High
  543. data = {
  544. 'priority': '1',
  545. 'csrf_token': csrf_token,
  546. }
  547. output = self.app.post(
  548. '/test/issue/1/update', data=data, follow_redirects=True)
  549. self.assertEqual(output.status_code, 200)
  550. self.assertIn(
  551. '<title>Issue #1: Test issue - test - Pagure</title>',
  552. output.data)
  553. self.assertIn(
  554. '<a class="btn btn-primary btn-sm" '
  555. 'href="/test/issue/1/edit" title="Edit this issue">',
  556. output.data)
  557. self.assertIn('<div id="priority_plain">', output.data)
  558. self.assertIn(
  559. '<option value="-1">Sky Falling</option>', output.data)
  560. self.assertIn('<option value="0">Urgent</option>', output.data)
  561. self.assertIn(
  562. '<option selected value="1">High</option>', output.data)
  563. # Reset the priority
  564. data = {
  565. 'priority': '',
  566. 'csrf_token': csrf_token,
  567. }
  568. output = self.app.post(
  569. '/test/issue/1/update', data=data, follow_redirects=True)
  570. self.assertEqual(output.status_code, 200)
  571. self.assertIn(
  572. '<title>Issue #1: Test issue - test - Pagure</title>',
  573. output.data)
  574. self.assertIn(
  575. '<a class="btn btn-primary btn-sm" '
  576. 'href="/test/issue/1/edit" title="Edit this issue">',
  577. output.data)
  578. self.assertIn('<div id="priority_plain">', output.data)
  579. self.assertIn(
  580. '<option value="-1">Sky Falling</option>', output.data)
  581. self.assertIn('<option value="0">Urgent</option>', output.data)
  582. self.assertIn('<option value="1">High</option>', output.data)
  583. @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
  584. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  585. def test_set_priority_0(self):
  586. """ Test setting the priority of a ticket to 0. """
  587. tests.create_projects(self.session)
  588. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  589. # Start from scrach on priorities
  590. repo = pagure.lib._get_project(self.session, 'test')
  591. self.assertEqual(repo.priorities, {})
  592. user = tests.FakeUser()
  593. user.username = 'pingou'
  594. with tests.user_set(self.app.application, user):
  595. # Get the CSRF token
  596. output = self.app.get('/test/settings')
  597. self.assertEqual(output.status_code, 200)
  598. self.assertIn(
  599. '<title>Settings - test - Pagure</title>', output.data)
  600. self.assertIn('<h3>Settings for test</h3>', output.data)
  601. csrf_token = output.data.split(
  602. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  603. # Set some priorities
  604. data = {
  605. 'priority_weigth': [-1, 0, 1, 2, 3],
  606. 'priority_title': [
  607. 'Sky Falling', 'Urgent', 'High', 'Normal', 'Low'],
  608. 'csrf_token': csrf_token,
  609. }
  610. output = self.app.post(
  611. '/test/update/priorities', data=data, follow_redirects=True)
  612. self.assertEqual(output.status_code, 200)
  613. self.assertIn(
  614. '<title>Settings - test - Pagure</title>', output.data)
  615. self.assertIn('<h3>Settings for test</h3>', output.data)
  616. # Check the result of the action -- Priority recorded
  617. self.session.commit()
  618. repo = pagure.lib._get_project(self.session, 'test')
  619. self.assertEqual(
  620. repo.priorities,
  621. {u'': u'', u'-1': u'Sky Falling', u'0': u'Urgent',
  622. u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  623. )
  624. # Create an issue
  625. data = {
  626. 'title': 'Test issue',
  627. 'issue_content': 'We really should improve on this issue',
  628. 'status': 'Open',
  629. 'csrf_token': csrf_token,
  630. }
  631. output = self.app.post(
  632. '/test/new_issue', data=data, follow_redirects=True)
  633. self.assertEqual(output.status_code, 200)
  634. self.assertIn(
  635. '<title>Issue #1: Test issue - test - Pagure</title>',
  636. output.data)
  637. self.assertIn(
  638. '<a class="btn btn-primary btn-sm" '
  639. 'href="/test/issue/1/edit" title="Edit this issue">',
  640. output.data)
  641. self.assertIn('<div id="priority_plain">', output.data)
  642. self.assertIn('<option value="1">High</option>', output.data)
  643. # Check that the ticket *does* have priorities
  644. output = self.app.get('/test/issue/1')
  645. self.assertEqual(output.status_code, 200)
  646. self.assertIn('<div id="priority_plain">', output.data)
  647. self.assertIn(
  648. '<option value="-1">Sky Falling</option>', output.data)
  649. self.assertIn('<option value="0">Urgent</option>', output.data)
  650. self.assertIn('<option value="1">High</option>', output.data)
  651. # Set the priority to Urgent
  652. data = {
  653. 'priority': '0',
  654. 'csrf_token': csrf_token,
  655. }
  656. output = self.app.post(
  657. '/test/issue/1/update', data=data, follow_redirects=True)
  658. self.assertEqual(output.status_code, 200)
  659. self.assertIn(
  660. '<title>Issue #1: Test issue - test - Pagure</title>',
  661. output.data)
  662. self.assertIn(
  663. '<a class="btn btn-primary btn-sm" '
  664. 'href="/test/issue/1/edit" title="Edit this issue">',
  665. output.data)
  666. self.assertIn('<div id="priority_plain">', output.data)
  667. self.assertIn(
  668. '<option value="-1">Sky Falling</option>', output.data)
  669. self.assertIn(
  670. '<option selected value="0">Urgent</option>',
  671. output.data)
  672. self.assertIn('<option value="1">High</option>', output.data)
  673. @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
  674. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  675. def test_set_priority_minus1(self):
  676. """ Test setting the priority of a ticket to -1. """
  677. tests.create_projects(self.session)
  678. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  679. # Start from scrach on priorities
  680. repo = pagure.lib._get_project(self.session, 'test')
  681. self.assertEqual(repo.priorities, {})
  682. user = tests.FakeUser()
  683. user.username = 'pingou'
  684. with tests.user_set(self.app.application, user):
  685. # Get the CSRF token
  686. output = self.app.get('/test/settings')
  687. self.assertEqual(output.status_code, 200)
  688. self.assertIn(
  689. '<title>Settings - test - Pagure</title>', output.data)
  690. self.assertIn('<h3>Settings for test</h3>', output.data)
  691. csrf_token = output.data.split(
  692. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  693. # Set some priorities
  694. data = {
  695. 'priority_weigth': [-1, 0, 1, 2, 3],
  696. 'priority_title': [
  697. 'Sky Falling', 'Urgent', 'High', 'Normal', 'Low'],
  698. 'csrf_token': csrf_token,
  699. }
  700. output = self.app.post(
  701. '/test/update/priorities', data=data, follow_redirects=True)
  702. self.assertEqual(output.status_code, 200)
  703. self.assertIn(
  704. '<title>Settings - test - Pagure</title>', output.data)
  705. self.assertIn('<h3>Settings for test</h3>', output.data)
  706. # Check the result of the action -- Priority recorded
  707. self.session.commit()
  708. repo = pagure.lib._get_project(self.session, 'test')
  709. self.assertEqual(
  710. repo.priorities,
  711. {u'': u'', u'-1': u'Sky Falling', u'0': u'Urgent',
  712. u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  713. )
  714. # Create an issue
  715. data = {
  716. 'title': 'Test issue',
  717. 'issue_content': 'We really should improve on this issue',
  718. 'status': 'Open',
  719. 'csrf_token': csrf_token,
  720. }
  721. output = self.app.post(
  722. '/test/new_issue', data=data, follow_redirects=True)
  723. self.assertEqual(output.status_code, 200)
  724. self.assertIn(
  725. '<title>Issue #1: Test issue - test - Pagure</title>',
  726. output.data)
  727. self.assertIn(
  728. '<a class="btn btn-primary btn-sm" '
  729. 'href="/test/issue/1/edit" title="Edit this issue">',
  730. output.data)
  731. self.assertIn('<div id="priority_plain">', output.data)
  732. self.assertIn('<option value="1">High</option>', output.data)
  733. # Check that the ticket *does* have priorities
  734. output = self.app.get('/test/issue/1')
  735. self.assertEqual(output.status_code, 200)
  736. self.assertIn('<div id="priority_plain">', output.data)
  737. self.assertIn(
  738. '<option value="-1">Sky Falling</option>', output.data)
  739. self.assertIn('<option value="0">Urgent</option>', output.data)
  740. self.assertIn('<option value="1">High</option>', output.data)
  741. # Set the priority to Sky Falling
  742. data = {
  743. 'priority': '-1',
  744. 'csrf_token': csrf_token,
  745. }
  746. output = self.app.post(
  747. '/test/issue/1/update', data=data, follow_redirects=True)
  748. self.assertEqual(output.status_code, 200)
  749. self.assertIn(
  750. '<title>Issue #1: Test issue - test - Pagure</title>',
  751. output.data)
  752. self.assertIn(
  753. '<a class="btn btn-primary btn-sm" '
  754. 'href="/test/issue/1/edit" title="Edit this issue">',
  755. output.data)
  756. self.assertIn('<div id="priority_plain">', output.data)
  757. self.assertIn(
  758. '<option selected value="-1">Sky Falling</option>',
  759. output.data)
  760. self.assertIn('<option value="0">Urgent</option>', output.data)
  761. self.assertIn('<option value="1">High</option>', output.data)
  762. @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
  763. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  764. def test_default_priority(self):
  765. """ Test updating the default priority of a repo. """
  766. tests.create_projects(self.session)
  767. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  768. # Check the default priorities
  769. repo = pagure.lib.get_authorized_project(self.session, 'test')
  770. self.assertEqual(repo.priorities, {})
  771. self.assertEqual(repo.default_priority, None)
  772. user = tests.FakeUser()
  773. user.username = 'pingou'
  774. with tests.user_set(self.app.application, user):
  775. csrf_token = self.get_csrf()
  776. # Set some priorities
  777. data = {
  778. 'priority_weigth': [1, 2, 3],
  779. 'priority_title': ['High', 'Normal', 'Low'],
  780. 'csrf_token': csrf_token,
  781. }
  782. output = self.app.post(
  783. '/test/update/priorities', data=data, follow_redirects=True)
  784. self.assertEqual(output.status_code, 200)
  785. # Check the redirect
  786. self.assertIn(
  787. '<title>Settings - test - Pagure</title>', output.data)
  788. self.assertIn('<h3>Settings for test</h3>', output.data)
  789. # Check the ordering
  790. self.assertTrue(
  791. output.data.find('High') < output.data.find('Normal'))
  792. self.assertTrue(
  793. output.data.find('Normal') < output.data.find('Low'))
  794. # Check the result of the action -- Priority recorded
  795. self.session.commit()
  796. repo = pagure.lib.get_authorized_project(self.session, 'test')
  797. self.assertEqual(
  798. repo.priorities,
  799. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  800. )
  801. # Try setting the default priority -- no csrf
  802. data = {'priority': 'High'}
  803. output = self.app.post(
  804. '/test/update/default_priority', data=data,
  805. follow_redirects=True)
  806. self.assertEqual(output.status_code, 200)
  807. # Check the redirect
  808. self.assertIn(
  809. '<title>Settings - test - Pagure</title>', output.data)
  810. self.assertIn('<h3>Settings for test</h3>', output.data)
  811. # Check the result of the action -- default_priority no change
  812. self.session.commit()
  813. repo = pagure.lib.get_authorized_project(self.session, 'test')
  814. self.assertEqual(repo.default_priority, None)
  815. # Try setting the default priority
  816. data = {'priority': 'High', 'csrf_token': csrf_token}
  817. output = self.app.post(
  818. '/test/update/default_priority', data=data,
  819. follow_redirects=True)
  820. self.assertEqual(output.status_code, 200)
  821. # Check the redirect
  822. self.assertIn(
  823. '<title>Settings - test - Pagure</title>', output.data)
  824. self.assertIn('<h3>Settings for test</h3>', output.data)
  825. self.assertIn(
  826. '</button>\n Default priority set '
  827. 'to High', output.data)
  828. # Check the result of the action -- default_priority no change
  829. self.session.commit()
  830. repo = pagure.lib.get_authorized_project(self.session, 'test')
  831. self.assertEqual(repo.default_priority, 'High')
  832. # Try setting a wrong default priority
  833. data = {'priority': 'Smooth', 'csrf_token': csrf_token}
  834. output = self.app.post(
  835. '/test/update/default_priority', data=data,
  836. follow_redirects=True)
  837. self.assertEqual(output.status_code, 200)
  838. # Check the redirect
  839. self.assertIn(
  840. '<title>Settings - test - Pagure</title>', output.data)
  841. self.assertIn('<h3>Settings for test</h3>', output.data)
  842. # Check the result of the action -- default_priority no change
  843. self.session.commit()
  844. repo = pagure.lib.get_authorized_project(self.session, 'test')
  845. self.assertEqual(repo.default_priority, 'High')
  846. # reset the default priority
  847. data = {'csrf_token': csrf_token, 'priority': ''}
  848. output = self.app.post(
  849. '/test/update/default_priority', data=data,
  850. follow_redirects=True)
  851. self.assertEqual(output.status_code, 200)
  852. # Check the redirect
  853. self.assertIn(
  854. '<title>Settings - test - Pagure</title>', output.data)
  855. self.assertIn('<h3>Settings for test</h3>', output.data)
  856. self.assertIn(
  857. '</button>\n Default priority reset',
  858. output.data)
  859. # Check the result of the action -- default_priority no change
  860. self.session.commit()
  861. repo = pagure.lib.get_authorized_project(self.session, 'test')
  862. self.assertEqual(repo.default_priority, None)
  863. # Check the behavior if the project disabled the issue tracker
  864. settings = repo.settings
  865. settings['issue_tracker'] = False
  866. repo.settings = settings
  867. self.session.add(repo)
  868. self.session.commit()
  869. output = self.app.post(
  870. '/test/update/default_priority', data=data)
  871. self.assertEqual(output.status_code, 404)
  872. # Check for an invalid project
  873. output = self.app.post(
  874. '/foo/update/default_priority', data=data)
  875. self.assertEqual(output.status_code, 404)
  876. # Check for a non-admin user
  877. settings = repo.settings
  878. settings['issue_tracker'] = True
  879. repo.settings = settings
  880. self.session.add(repo)
  881. self.session.commit()
  882. user.username = 'ralph'
  883. with tests.user_set(self.app.application, user):
  884. output = self.app.post(
  885. '/test/update/default_priority', data=data)
  886. self.assertEqual(output.status_code, 403)
  887. @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
  888. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  889. def test_default_priority_reset_when_updating_priorities(self):
  890. """ Test updating the default priority of a repo when updating the
  891. priorities.
  892. """
  893. tests.create_projects(self.session)
  894. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  895. # Check the default priorities
  896. repo = pagure.lib.get_authorized_project(self.session, 'test')
  897. self.assertEqual(repo.priorities, {})
  898. self.assertEqual(repo.default_priority, None)
  899. user = tests.FakeUser()
  900. user.username = 'pingou'
  901. with tests.user_set(self.app.application, user):
  902. csrf_token = self.get_csrf()
  903. # Set some priorities
  904. data = {
  905. 'priority_weigth': [1, 2, 3],
  906. 'priority_title': ['High', 'Normal', 'Low'],
  907. 'csrf_token': csrf_token,
  908. }
  909. output = self.app.post(
  910. '/test/update/priorities', data=data, follow_redirects=True)
  911. self.assertEqual(output.status_code, 200)
  912. # Check the redirect
  913. self.assertIn(
  914. '<title>Settings - test - Pagure</title>', output.data)
  915. self.assertIn('<h3>Settings for test</h3>', output.data)
  916. # Check the ordering
  917. self.assertTrue(
  918. output.data.find('High') < output.data.find('Normal'))
  919. self.assertTrue(
  920. output.data.find('Normal') < output.data.find('Low'))
  921. # Check the result of the action -- Priority recorded
  922. self.session.commit()
  923. repo = pagure.lib.get_authorized_project(self.session, 'test')
  924. self.assertEqual(
  925. repo.priorities,
  926. {u'': u'', u'1': u'High', u'2': u'Normal', u'3': u'Low'}
  927. )
  928. # Try setting the default priority
  929. data = {'priority': 'High', 'csrf_token': csrf_token}
  930. output = self.app.post(
  931. '/test/update/default_priority', data=data,
  932. follow_redirects=True)
  933. self.assertEqual(output.status_code, 200)
  934. # Check the redirect
  935. self.assertIn(
  936. '<title>Settings - test - Pagure</title>', output.data)
  937. self.assertIn('<h3>Settings for test</h3>', output.data)
  938. self.assertIn(
  939. '</button>\n Default priority set '
  940. 'to High', output.data)
  941. # Check the result of the action -- default_priority no change
  942. self.session.commit()
  943. repo = pagure.lib.get_authorized_project(self.session, 'test')
  944. self.assertEqual(repo.default_priority, 'High')
  945. # Remove the Hight priority
  946. data = {
  947. 'priority_weigth': [1, 2],
  948. 'priority_title': ['Normal', 'Low'],
  949. 'csrf_token': csrf_token,
  950. }
  951. output = self.app.post(
  952. '/test/update/priorities', data=data, follow_redirects=True)
  953. self.assertEqual(output.status_code, 200)
  954. # Check the redirect
  955. self.assertIn(
  956. '<title>Settings - test - Pagure</title>', output.data)
  957. self.assertIn('<h3>Settings for test</h3>', output.data)
  958. self.assertIn(
  959. '</button>\n Priorities updated',
  960. output.data)
  961. self.assertIn(
  962. '</button>\n Default priority reset '
  963. 'as it is no longer one of set priorities.',
  964. output.data)
  965. # Check the ordering
  966. self.assertTrue(
  967. output.data.find('Normal') < output.data.find('Low'))
  968. # Check the result of the action -- Priority recorded
  969. self.session.commit()
  970. repo = pagure.lib.get_authorized_project(self.session, 'test')
  971. self.assertEqual(
  972. repo.priorities,
  973. {u'': u'', u'1': u'Normal', u'2': u'Low'}
  974. )
  975. # Default priority is now None
  976. self.assertIsNone(repo.default_priority)
  977. @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
  978. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  979. def test_default_priority_on_new_ticket(self):
  980. """ Test updating the default priority of a repo. """
  981. tests.create_projects(self.session)
  982. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  983. # Set some priority and the default one
  984. repo = pagure.lib.get_authorized_project(self.session, 'test')
  985. repo.priorities = {'1': 'High', '2': 'Normal'}
  986. repo.default_priority = 'Normal'
  987. self.session.add(repo)
  988. self.session.commit()
  989. # Check the default priorities
  990. repo = pagure.lib.get_authorized_project(self.session, 'test')
  991. self.assertEqual(repo.priorities, {u'1': u'High', u'2': u'Normal'})
  992. self.assertEqual(repo.default_priority, 'Normal')
  993. user = tests.FakeUser()
  994. user.username = 'pingou'
  995. with tests.user_set(self.app.application, user):
  996. csrf_token = self.get_csrf()
  997. data = {
  998. 'title': 'Test issue',
  999. 'issue_content': 'We really should improve on this issue',
  1000. 'status': 'Open',
  1001. 'csrf_token': csrf_token,
  1002. }
  1003. output = self.app.post(
  1004. '/test/new_issue', data=data, follow_redirects=True)
  1005. self.assertEqual(output.status_code, 200)
  1006. self.assertIn(
  1007. '<title>Issue #1: Test issue - test - Pagure</title>',
  1008. output.data)
  1009. self.assertIn(
  1010. '<a class="btn btn-primary btn-sm" '
  1011. 'href="/test/issue/1/edit" title="Edit this issue">',
  1012. output.data)
  1013. repo = pagure.lib.get_authorized_project(self.session, 'test')
  1014. self.assertEqual(len(repo.issues), 1)
  1015. self.assertEqual(repo.issues[0].priority, 2)
  1016. if __name__ == '__main__':
  1017. unittest.main(verbosity=2)