test_pagure_flask_ui_priorities.py 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import json
  10. import unittest
  11. import shutil
  12. import sys
  13. import tempfile
  14. import os
  15. import pygit2
  16. from mock import patch, MagicMock
  17. sys.path.insert(
  18. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  19. )
  20. import pagure.lib.query
  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. @patch("pagure.lib.git.update_git")
  26. @patch("pagure.lib.notify.send_email")
  27. def test_ticket_with_no_priority(self, p_send_email, p_ugt):
  28. """ Test creating a ticket without priority. """
  29. p_send_email.return_value = True
  30. p_ugt.return_value = True
  31. tests.create_projects(self.session)
  32. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  33. user = tests.FakeUser()
  34. user.username = "pingou"
  35. with tests.user_set(self.app.application, user):
  36. # Get the CSRF token
  37. output = self.app.get("/test/new_issue")
  38. self.assertEqual(output.status_code, 200)
  39. output_text = output.get_data(as_text=True)
  40. self.assertTrue(
  41. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n'
  42. in output_text
  43. )
  44. csrf_token = output_text.split(
  45. 'name="csrf_token" type="hidden" value="'
  46. )[1].split('">')[0]
  47. data = {
  48. "title": "Test issue",
  49. "issue_content": "We really should improve on this issue",
  50. "status": "Open",
  51. "csrf_token": csrf_token,
  52. }
  53. # Create the issue
  54. output = self.app.post(
  55. "/test/new_issue", data=data, follow_redirects=True
  56. )
  57. self.assertEqual(output.status_code, 200)
  58. output_text = output.get_data(as_text=True)
  59. self.assertIn(
  60. "<title>Issue #1: Test issue - test - Pagure</title>",
  61. output_text,
  62. )
  63. self.assertIn(
  64. '<a class="btn btn-outline-secondary btn-sm border-0" '
  65. 'href="/test/issue/1/edit" title="Edit this issue">',
  66. output_text,
  67. )
  68. self.assertNotIn(
  69. '<div class="ml-2" id="priority_plain">', output_text
  70. )
  71. self.assertNotIn('<option value="1">High</option>', output_text)
  72. @patch("pagure.lib.git.update_git")
  73. @patch("pagure.lib.notify.send_email")
  74. def test_ticket_with_priorities(self, p_send_email, p_ugt):
  75. """ Test creating a ticket with priorities. """
  76. p_send_email.return_value = True
  77. p_ugt.return_value = True
  78. tests.create_projects(self.session)
  79. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  80. # Set some priorities
  81. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  82. repo.priorities = {"1": "High", "2": "Normal"}
  83. self.session.add(repo)
  84. self.session.commit()
  85. user = tests.FakeUser()
  86. user.username = "pingou"
  87. with tests.user_set(self.app.application, user):
  88. # Get the CSRF token
  89. output = self.app.get("/test/new_issue")
  90. self.assertEqual(output.status_code, 200)
  91. output_text = output.get_data(as_text=True)
  92. self.assertTrue(
  93. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n'
  94. in output_text
  95. )
  96. csrf_token = output_text.split(
  97. 'name="csrf_token" type="hidden" value="'
  98. )[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. )
  109. self.assertEqual(output.status_code, 200)
  110. output_text = output.get_data(as_text=True)
  111. self.assertIn(
  112. "<title>Issue #1: Test issue - test - Pagure</title>",
  113. output_text,
  114. )
  115. self.assertIn(
  116. '<a class="btn btn-outline-secondary btn-sm border-0" '
  117. 'href="/test/issue/1/edit" title="Edit this issue">',
  118. output_text,
  119. )
  120. self.assertIn(
  121. '<div class="ml-2" id="priority_plain">', output_text
  122. )
  123. self.assertIn('<option value="1">High</option>', output_text)
  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(self.path, "repos"), bare=True)
  128. # Set some priorities
  129. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  130. self.assertEqual(repo.priorities, {})
  131. user = tests.FakeUser()
  132. user.username = "pingou"
  133. with tests.user_set(self.app.application, user):
  134. # Get the CSRF token
  135. output = self.app.get("/test/settings")
  136. self.assertEqual(output.status_code, 200)
  137. output_text = output.get_data(as_text=True)
  138. self.assertIn(
  139. "<title>Settings - test - Pagure</title>", output_text
  140. )
  141. self.assertIn(
  142. '<h5 class="pl-2 font-weight-bold text-muted">'
  143. "Project Settings</h5>\n",
  144. output_text,
  145. )
  146. csrf_token = output_text.split(
  147. 'name="csrf_token" type="hidden" value="'
  148. )[1].split('">')[0]
  149. data = {"priority_weigth": 1, "priority_title": "High"}
  150. output = self.app.post(
  151. "/test/update/priorities", data=data, follow_redirects=True
  152. )
  153. self.assertEqual(output.status_code, 200)
  154. output_text = output.get_data(as_text=True)
  155. # Check the redirect
  156. self.assertIn(
  157. "<title>Settings - test - Pagure</title>", output_text
  158. )
  159. self.assertIn(
  160. '<h5 class="pl-2 font-weight-bold text-muted">'
  161. "Project Settings</h5>\n",
  162. output_text,
  163. )
  164. # Check the result of the action -- None, no CSRF
  165. repo = pagure.lib.query.get_authorized_project(
  166. self.session, "test"
  167. )
  168. self.assertEqual(repo.priorities, {})
  169. data = {
  170. "priority_weigth": 1,
  171. "priority_title": "High",
  172. "csrf_token": csrf_token,
  173. }
  174. output = self.app.post(
  175. "/test/update/priorities", data=data, follow_redirects=True
  176. )
  177. self.assertEqual(output.status_code, 200)
  178. output_text = output.get_data(as_text=True)
  179. # Check the redirect
  180. self.assertIn(
  181. "<title>Settings - test - Pagure</title>", output_text
  182. )
  183. self.assertIn(
  184. '<h5 class="pl-2 font-weight-bold text-muted">'
  185. "Project Settings</h5>\n",
  186. output_text,
  187. )
  188. # Check the result of the action -- Priority recorded
  189. self.session.commit()
  190. repo = pagure.lib.query.get_authorized_project(
  191. self.session, "test"
  192. )
  193. self.assertEqual(repo.priorities, {"": "", "1": "High"})
  194. data = {
  195. "priority_weigth": [1, 2, 3],
  196. "priority_title": ["High", "Normal", "Low"],
  197. "csrf_token": csrf_token,
  198. }
  199. output = self.app.post(
  200. "/test/update/priorities", data=data, follow_redirects=True
  201. )
  202. self.assertEqual(output.status_code, 200)
  203. output_text = output.get_data(as_text=True)
  204. # Check the redirect
  205. self.assertIn(
  206. "<title>Settings - test - Pagure</title>", output_text
  207. )
  208. self.assertIn(
  209. '<h5 class="pl-2 font-weight-bold text-muted">'
  210. "Project Settings</h5>\n",
  211. output_text,
  212. )
  213. # Check the ordering
  214. self.assertTrue(
  215. output_text.find("High") < output_text.find("Normal")
  216. )
  217. self.assertTrue(
  218. output_text.find("Normal") < output_text.find("Low")
  219. )
  220. # Check the result of the action -- Priority recorded
  221. self.session.commit()
  222. repo = pagure.lib.query.get_authorized_project(
  223. self.session, "test"
  224. )
  225. self.assertEqual(
  226. repo.priorities,
  227. {"": "", "1": "High", "2": "Normal", "3": "Low"},
  228. )
  229. # Check error - less weigths than titles
  230. data = {
  231. "priority_weigth": [1, 2],
  232. "priority_title": ["High", "Normal", "Low"],
  233. "csrf_token": csrf_token,
  234. }
  235. output = self.app.post(
  236. "/test/update/priorities", data=data, follow_redirects=True
  237. )
  238. self.assertEqual(output.status_code, 200)
  239. output_text = output.get_data(as_text=True)
  240. # Check the redirect
  241. self.assertIn(
  242. "<title>Settings - test - Pagure</title>", output_text
  243. )
  244. self.assertIn(
  245. '<h5 class="pl-2 font-weight-bold text-muted">'
  246. "Project Settings</h5>\n",
  247. output_text,
  248. )
  249. self.assertIn(
  250. "Priorities weights and titles are " "not of the same length",
  251. output_text,
  252. )
  253. # Check the result of the action -- Priorities un-changed
  254. self.session.commit()
  255. repo = pagure.lib.query.get_authorized_project(
  256. self.session, "test"
  257. )
  258. self.assertEqual(
  259. repo.priorities,
  260. {"": "", "1": "High", "2": "Normal", "3": "Low"},
  261. )
  262. # Check error - weigths must be integer
  263. data = {
  264. "priority_weigth": [1, 2, "c"],
  265. "priority_title": ["High", "Normal", "Low"],
  266. "csrf_token": csrf_token,
  267. }
  268. output = self.app.post(
  269. "/test/update/priorities", data=data, follow_redirects=True
  270. )
  271. self.assertEqual(output.status_code, 200)
  272. output_text = output.get_data(as_text=True)
  273. # Check the redirect
  274. self.assertIn(
  275. "<title>Settings - test - Pagure</title>", output_text
  276. )
  277. self.assertIn(
  278. '<h5 class="pl-2 font-weight-bold text-muted">'
  279. "Project Settings</h5>\n",
  280. output_text,
  281. )
  282. self.assertIn("Priorities weights must be numbers", output_text)
  283. # Check the result of the action -- Priorities un-changed
  284. self.session.commit()
  285. repo = pagure.lib.query.get_authorized_project(
  286. self.session, "test"
  287. )
  288. self.assertEqual(
  289. repo.priorities,
  290. {"": "", "1": "High", "2": "Normal", "3": "Low"},
  291. )
  292. # Check error - Twice the same priority weigth
  293. data = {
  294. "priority_weigth": [1, 2, 2],
  295. "priority_title": ["High", "Normal", "Low"],
  296. "csrf_token": csrf_token,
  297. }
  298. output = self.app.post(
  299. "/test/update/priorities", data=data, follow_redirects=True
  300. )
  301. self.assertEqual(output.status_code, 200)
  302. output_text = output.get_data(as_text=True)
  303. # Check the redirect
  304. self.assertIn(
  305. "<title>Settings - test - Pagure</title>", output_text
  306. )
  307. self.assertIn(
  308. '<h5 class="pl-2 font-weight-bold text-muted">'
  309. "Project Settings</h5>\n",
  310. output_text,
  311. )
  312. self.assertIn("Priority weight 2 is present 2 times", output_text)
  313. # Check the result of the action -- Priorities un-changed
  314. self.session.commit()
  315. repo = pagure.lib.query.get_authorized_project(
  316. self.session, "test"
  317. )
  318. self.assertEqual(
  319. repo.priorities,
  320. {"": "", "1": "High", "2": "Normal", "3": "Low"},
  321. )
  322. # Check error - Twice the same priority title
  323. data = {
  324. "priority_weigth": [1, 2, 3],
  325. "priority_title": ["High", "Normal", "Normal"],
  326. "csrf_token": csrf_token,
  327. }
  328. output = self.app.post(
  329. "/test/update/priorities", data=data, follow_redirects=True
  330. )
  331. self.assertEqual(output.status_code, 200)
  332. output_text = output.get_data(as_text=True)
  333. # Check the redirect
  334. self.assertIn(
  335. "<title>Settings - test - Pagure</title>", output_text
  336. )
  337. self.assertIn(
  338. '<h5 class="pl-2 font-weight-bold text-muted">'
  339. "Project Settings</h5>\n",
  340. output_text,
  341. )
  342. self.assertIn("Priority Normal is present 2 times", output_text)
  343. # Check the result of the action -- Priorities un-changed
  344. self.session.commit()
  345. repo = pagure.lib.query.get_authorized_project(
  346. self.session, "test"
  347. )
  348. self.assertEqual(
  349. repo.priorities,
  350. {"": "", "1": "High", "2": "Normal", "3": "Low"},
  351. )
  352. # Check the behavior if the project disabled the issue tracker
  353. settings = repo.settings
  354. settings["issue_tracker"] = False
  355. repo.settings = settings
  356. self.session.add(repo)
  357. self.session.commit()
  358. output = self.app.post("/test/update/priorities", data=data)
  359. self.assertEqual(output.status_code, 404)
  360. # Check for an invalid project
  361. output = self.app.post("/foo/update/priorities", data=data)
  362. self.assertEqual(output.status_code, 404)
  363. # Check for a non-admin user
  364. settings = repo.settings
  365. settings["issue_tracker"] = True
  366. repo.settings = settings
  367. self.session.add(repo)
  368. self.session.commit()
  369. user.username = "ralph"
  370. with tests.user_set(self.app.application, user):
  371. output = self.app.post("/test/update/priorities", data=data)
  372. self.assertEqual(output.status_code, 403)
  373. @patch("pagure.lib.git.update_git")
  374. @patch("pagure.lib.notify.send_email")
  375. def test_reset_priorities(self, p_send_email, p_ugt):
  376. """ Test resetting the priorities of a repo. """
  377. p_send_email.return_value = True
  378. p_ugt.return_value = True
  379. tests.create_projects(self.session)
  380. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  381. # Start from scrach on priorities
  382. repo = pagure.lib.query._get_project(self.session, "test")
  383. self.assertEqual(repo.priorities, {})
  384. user = tests.FakeUser()
  385. user.username = "pingou"
  386. with tests.user_set(self.app.application, user):
  387. # Get the CSRF token
  388. output = self.app.get("/test/settings")
  389. self.assertEqual(output.status_code, 200)
  390. output_text = output.get_data(as_text=True)
  391. self.assertIn(
  392. "<title>Settings - test - Pagure</title>", output_text
  393. )
  394. self.assertIn(
  395. '<h5 class="pl-2 font-weight-bold text-muted">'
  396. "Project Settings</h5>\n",
  397. output_text,
  398. )
  399. csrf_token = output_text.split(
  400. 'name="csrf_token" type="hidden" value="'
  401. )[1].split('">')[0]
  402. # Set some priorities
  403. data = {
  404. "priority_weigth": [1, 2, 3],
  405. "priority_title": ["High", "Normal", "Low"],
  406. "csrf_token": csrf_token,
  407. }
  408. output = self.app.post(
  409. "/test/update/priorities", data=data, follow_redirects=True
  410. )
  411. self.assertEqual(output.status_code, 200)
  412. output_text = output.get_data(as_text=True)
  413. self.assertIn(
  414. "<title>Settings - test - Pagure</title>", output_text
  415. )
  416. self.assertIn(
  417. '<h5 class="pl-2 font-weight-bold text-muted">'
  418. "Project Settings</h5>\n",
  419. output_text,
  420. )
  421. # Check the result of the action -- Priority recorded
  422. self.session.commit()
  423. repo = pagure.lib.query._get_project(self.session, "test")
  424. self.assertEqual(
  425. repo.priorities,
  426. {"": "", "1": "High", "2": "Normal", "3": "Low"},
  427. )
  428. # Create an issue
  429. data = {
  430. "title": "Test issue",
  431. "issue_content": "We really should improve on this issue",
  432. "status": "Open",
  433. "csrf_token": csrf_token,
  434. }
  435. output = self.app.post(
  436. "/test/new_issue", data=data, follow_redirects=True
  437. )
  438. self.assertEqual(output.status_code, 200)
  439. output_text = output.get_data(as_text=True)
  440. self.assertIn(
  441. "<title>Issue #1: Test issue - test - Pagure</title>",
  442. output_text,
  443. )
  444. self.assertIn(
  445. '<a class="btn btn-outline-secondary btn-sm border-0" '
  446. 'href="/test/issue/1/edit" title="Edit this issue">',
  447. output_text,
  448. )
  449. self.assertIn(
  450. '<div class="ml-2" id="priority_plain">', output_text
  451. )
  452. self.assertIn('<option value="1">High</option>', output_text)
  453. # Check that the ticket *does* have priorities
  454. output = self.app.get("/test/issue/1")
  455. self.assertEqual(output.status_code, 200)
  456. output_text = output.get_data(as_text=True)
  457. self.assertIn(
  458. '<div class="ml-2" id="priority_plain">', output_text
  459. )
  460. self.assertIn('<option value="1">High</option>', output_text)
  461. # Reset the priorities
  462. data = {"csrf_token": csrf_token}
  463. output = self.app.post(
  464. "/test/update/priorities", data=data, follow_redirects=True
  465. )
  466. self.assertEqual(output.status_code, 200)
  467. output_text = output.get_data(as_text=True)
  468. self.assertIn(
  469. "<title>Settings - test - Pagure</title>", output_text
  470. )
  471. self.assertIn(
  472. '<h5 class="pl-2 font-weight-bold text-muted">'
  473. "Project Settings</h5>\n",
  474. output_text,
  475. )
  476. # Check that the issue list renders fine
  477. output = self.app.get("/test/issues")
  478. self.assertEqual(output.status_code, 200)
  479. output_text = output.get_data(as_text=True)
  480. # Check that the ticket *does not* have priorities
  481. output = self.app.get("/test/issue/1")
  482. self.assertEqual(output.status_code, 200)
  483. output_text = output.get_data(as_text=True)
  484. self.assertNotIn(
  485. '<div class="ml-2" id="priority_plain">', output_text
  486. )
  487. self.assertNotIn('<option value="1">High</option>', output_text)
  488. # Check the result of the action -- Priority reset
  489. self.session.commit()
  490. repo = pagure.lib.query._get_project(self.session, "test")
  491. self.assertEqual(repo.priorities, {})
  492. @patch("pagure.lib.git.update_git")
  493. @patch("pagure.lib.notify.send_email")
  494. def test_reset_priorities_None(self, p_send_email, p_ugt):
  495. """ Test resetting the priorities of a repo. """
  496. p_send_email.return_value = True
  497. p_ugt.return_value = True
  498. tests.create_projects(self.session)
  499. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  500. # Start from scrach on priorities
  501. repo = pagure.lib.query._get_project(self.session, "test")
  502. self.assertEqual(repo.priorities, {})
  503. user = tests.FakeUser()
  504. user.username = "pingou"
  505. with tests.user_set(self.app.application, user):
  506. # Get the CSRF token
  507. output = self.app.get("/test/settings")
  508. self.assertEqual(output.status_code, 200)
  509. output_text = output.get_data(as_text=True)
  510. self.assertIn(
  511. "<title>Settings - test - Pagure</title>", output_text
  512. )
  513. self.assertIn(
  514. '<h5 class="pl-2 font-weight-bold text-muted">'
  515. "Project Settings</h5>\n",
  516. output_text,
  517. )
  518. csrf_token = output_text.split(
  519. 'name="csrf_token" type="hidden" value="'
  520. )[1].split('">')[0]
  521. # Set some priorities
  522. data = {
  523. "priority_weigth": [1, 2, 3],
  524. "priority_title": ["High", "Normal", "Low"],
  525. "csrf_token": csrf_token,
  526. }
  527. output = self.app.post(
  528. "/test/update/priorities", data=data, follow_redirects=True
  529. )
  530. self.assertEqual(output.status_code, 200)
  531. output_text = output.get_data(as_text=True)
  532. self.assertIn(
  533. "<title>Settings - test - Pagure</title>", output_text
  534. )
  535. self.assertIn(
  536. '<h5 class="pl-2 font-weight-bold text-muted">'
  537. "Project Settings</h5>\n",
  538. output_text,
  539. )
  540. # Check the result of the action -- Priority recorded
  541. self.session.commit()
  542. repo = pagure.lib.query._get_project(self.session, "test")
  543. self.assertEqual(
  544. repo.priorities,
  545. {"": "", "1": "High", "2": "Normal", "3": "Low"},
  546. )
  547. # Create an issue
  548. data = {
  549. "title": "Test issue",
  550. "issue_content": "We really should improve on this issue",
  551. "status": "Open",
  552. "csrf_token": csrf_token,
  553. }
  554. output = self.app.post(
  555. "/test/new_issue", data=data, follow_redirects=True
  556. )
  557. self.assertEqual(output.status_code, 200)
  558. output_text = output.get_data(as_text=True)
  559. self.assertIn(
  560. "<title>Issue #1: Test issue - test - Pagure</title>",
  561. output_text,
  562. )
  563. self.assertIn(
  564. '<a class="btn btn-outline-secondary btn-sm border-0" '
  565. 'href="/test/issue/1/edit" title="Edit this issue">',
  566. output_text,
  567. )
  568. self.assertIn(
  569. '<div class="ml-2" id="priority_plain">', output_text
  570. )
  571. self.assertIn('<option value="1">High</option>', output_text)
  572. # Check that the ticket *does* have priorities
  573. output = self.app.get("/test/issue/1")
  574. self.assertEqual(output.status_code, 200)
  575. output_text = output.get_data(as_text=True)
  576. self.assertIn(
  577. '<div class="ml-2" id="priority_plain">', output_text
  578. )
  579. self.assertIn('<option value="1">High</option>', output_text)
  580. # Reset the priorities
  581. data = {"priority": None, "csrf_token": csrf_token}
  582. output = self.app.post(
  583. "/test/update/priorities", data=data, follow_redirects=True
  584. )
  585. self.assertEqual(output.status_code, 200)
  586. output_text = output.get_data(as_text=True)
  587. self.assertIn(
  588. "<title>Settings - test - Pagure</title>", output_text
  589. )
  590. self.assertIn(
  591. '<h5 class="pl-2 font-weight-bold text-muted">'
  592. "Project Settings</h5>\n",
  593. output_text,
  594. )
  595. # Check that the issue list renders fine
  596. output = self.app.get("/test/issues")
  597. self.assertEqual(output.status_code, 200)
  598. output_text = output.get_data(as_text=True)
  599. # Check that the ticket *does not* have priorities
  600. output = self.app.get("/test/issue/1")
  601. self.assertEqual(output.status_code, 200)
  602. output_text = output.get_data(as_text=True)
  603. self.assertNotIn(
  604. '<div class="ml-2" id="priority_plain">', output_text
  605. )
  606. self.assertNotIn('<option value="1">High</option>', output_text)
  607. # Check the result of the action -- Priority recorded
  608. self.session.commit()
  609. repo = pagure.lib.query._get_project(self.session, "test")
  610. self.assertEqual(repo.priorities, {})
  611. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  612. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  613. def test_set_priority_1_and_back(self):
  614. """ Test setting the priority of a ticket to 1. """
  615. tests.create_projects(self.session)
  616. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  617. # Start from scrach on priorities
  618. repo = pagure.lib.query._get_project(self.session, "test")
  619. self.assertEqual(repo.priorities, {})
  620. user = tests.FakeUser()
  621. user.username = "pingou"
  622. with tests.user_set(self.app.application, user):
  623. # Get the CSRF token
  624. output = self.app.get("/test/settings")
  625. self.assertEqual(output.status_code, 200)
  626. output_text = output.get_data(as_text=True)
  627. self.assertIn(
  628. "<title>Settings - test - Pagure</title>", output_text
  629. )
  630. self.assertIn(
  631. '<h5 class="pl-2 font-weight-bold text-muted">'
  632. "Project Settings</h5>\n",
  633. output_text,
  634. )
  635. csrf_token = output_text.split(
  636. 'name="csrf_token" type="hidden" value="'
  637. )[1].split('">')[0]
  638. # Set some priorities
  639. data = {
  640. "priority_weigth": [-1, 0, 1, 2, 3],
  641. "priority_title": [
  642. "Sky Falling",
  643. "Urgent",
  644. "High",
  645. "Normal",
  646. "Low",
  647. ],
  648. "csrf_token": csrf_token,
  649. }
  650. output = self.app.post(
  651. "/test/update/priorities", data=data, follow_redirects=True
  652. )
  653. self.assertEqual(output.status_code, 200)
  654. output_text = output.get_data(as_text=True)
  655. self.assertIn(
  656. "<title>Settings - test - Pagure</title>", output_text
  657. )
  658. self.assertIn(
  659. '<h5 class="pl-2 font-weight-bold text-muted">'
  660. "Project Settings</h5>\n",
  661. output_text,
  662. )
  663. # Check the result of the action -- Priority recorded
  664. self.session.commit()
  665. repo = pagure.lib.query._get_project(self.session, "test")
  666. self.assertEqual(
  667. repo.priorities,
  668. {
  669. "": "",
  670. "-1": "Sky Falling",
  671. "0": "Urgent",
  672. "1": "High",
  673. "2": "Normal",
  674. "3": "Low",
  675. },
  676. )
  677. # Create an issue
  678. data = {
  679. "title": "Test issue",
  680. "issue_content": "We really should improve on this issue",
  681. "status": "Open",
  682. "csrf_token": csrf_token,
  683. }
  684. output = self.app.post(
  685. "/test/new_issue", data=data, follow_redirects=True
  686. )
  687. self.assertEqual(output.status_code, 200)
  688. output_text = output.get_data(as_text=True)
  689. self.assertIn(
  690. "<title>Issue #1: Test issue - test - Pagure</title>",
  691. output_text,
  692. )
  693. self.assertIn(
  694. '<a class="btn btn-outline-secondary btn-sm border-0" '
  695. 'href="/test/issue/1/edit" title="Edit this issue">',
  696. output_text,
  697. )
  698. self.assertIn(
  699. '<div class="ml-2" id="priority_plain">', output_text
  700. )
  701. self.assertIn('<option value="1">High</option>', output_text)
  702. # Check that the ticket *does* have priorities
  703. output = self.app.get("/test/issue/1")
  704. self.assertEqual(output.status_code, 200)
  705. output_text = output.get_data(as_text=True)
  706. self.assertIn(
  707. '<div class="ml-2" id="priority_plain">', output_text
  708. )
  709. self.assertIn(
  710. '<option value="-1">Sky Falling</option>', output_text
  711. )
  712. self.assertIn('<option value="0">Urgent</option>', output_text)
  713. self.assertIn('<option value="1">High</option>', output_text)
  714. # Set the priority to High
  715. data = {"priority": "1", "csrf_token": csrf_token}
  716. output = self.app.post(
  717. "/test/issue/1/update", data=data, follow_redirects=True
  718. )
  719. self.assertEqual(output.status_code, 200)
  720. output_text = output.get_data(as_text=True)
  721. self.assertIn(
  722. "<title>Issue #1: Test issue - test - Pagure</title>",
  723. output_text,
  724. )
  725. self.assertIn(
  726. '<a class="btn btn-outline-secondary btn-sm border-0" '
  727. 'href="/test/issue/1/edit" title="Edit this issue">',
  728. output_text,
  729. )
  730. self.assertIn(
  731. '<div class="ml-2" id="priority_plain">', output_text
  732. )
  733. self.assertIn(
  734. '<option value="-1">Sky Falling</option>', output_text
  735. )
  736. self.assertIn('<option value="0">Urgent</option>', output_text)
  737. self.assertIn(
  738. '<option selected value="1">High</option>', output_text
  739. )
  740. # Reset the priority
  741. data = {"priority": "", "csrf_token": csrf_token}
  742. output = self.app.post(
  743. "/test/issue/1/update", data=data, follow_redirects=True
  744. )
  745. self.assertEqual(output.status_code, 200)
  746. output_text = output.get_data(as_text=True)
  747. self.assertIn(
  748. "<title>Issue #1: Test issue - test - Pagure</title>",
  749. output_text,
  750. )
  751. self.assertIn(
  752. '<a class="btn btn-outline-secondary btn-sm border-0" '
  753. 'href="/test/issue/1/edit" title="Edit this issue">',
  754. output_text,
  755. )
  756. self.assertIn(
  757. '<div class="ml-2" id="priority_plain">', output_text
  758. )
  759. self.assertIn(
  760. '<option value="-1">Sky Falling</option>', output_text
  761. )
  762. self.assertIn('<option value="0">Urgent</option>', output_text)
  763. self.assertIn('<option value="1">High</option>', output_text)
  764. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  765. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  766. def test_set_priority_0(self):
  767. """ Test setting the priority of a ticket to 0. """
  768. tests.create_projects(self.session)
  769. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  770. # Start from scrach on priorities
  771. repo = pagure.lib.query._get_project(self.session, "test")
  772. self.assertEqual(repo.priorities, {})
  773. user = tests.FakeUser()
  774. user.username = "pingou"
  775. with tests.user_set(self.app.application, user):
  776. # Get the CSRF token
  777. output = self.app.get("/test/settings")
  778. self.assertEqual(output.status_code, 200)
  779. output_text = output.get_data(as_text=True)
  780. self.assertIn(
  781. "<title>Settings - test - Pagure</title>", output_text
  782. )
  783. self.assertIn(
  784. '<h5 class="pl-2 font-weight-bold text-muted">'
  785. "Project Settings</h5>\n",
  786. output_text,
  787. )
  788. csrf_token = output_text.split(
  789. 'name="csrf_token" type="hidden" value="'
  790. )[1].split('">')[0]
  791. # Set some priorities
  792. data = {
  793. "priority_weigth": [-1, 0, 1, 2, 3],
  794. "priority_title": [
  795. "Sky Falling",
  796. "Urgent",
  797. "High",
  798. "Normal",
  799. "Low",
  800. ],
  801. "csrf_token": csrf_token,
  802. }
  803. output = self.app.post(
  804. "/test/update/priorities", data=data, follow_redirects=True
  805. )
  806. self.assertEqual(output.status_code, 200)
  807. output_text = output.get_data(as_text=True)
  808. self.assertIn(
  809. "<title>Settings - test - Pagure</title>", output_text
  810. )
  811. self.assertIn(
  812. '<h5 class="pl-2 font-weight-bold text-muted">'
  813. "Project Settings</h5>\n",
  814. output_text,
  815. )
  816. # Check the result of the action -- Priority recorded
  817. self.session.commit()
  818. repo = pagure.lib.query._get_project(self.session, "test")
  819. self.assertEqual(
  820. repo.priorities,
  821. {
  822. "": "",
  823. "-1": "Sky Falling",
  824. "0": "Urgent",
  825. "1": "High",
  826. "2": "Normal",
  827. "3": "Low",
  828. },
  829. )
  830. # Create an issue
  831. data = {
  832. "title": "Test issue",
  833. "issue_content": "We really should improve on this issue",
  834. "status": "Open",
  835. "csrf_token": csrf_token,
  836. }
  837. output = self.app.post(
  838. "/test/new_issue", data=data, follow_redirects=True
  839. )
  840. self.assertEqual(output.status_code, 200)
  841. output_text = output.get_data(as_text=True)
  842. self.assertIn(
  843. "<title>Issue #1: Test issue - test - Pagure</title>",
  844. output_text,
  845. )
  846. self.assertIn(
  847. '<a class="btn btn-outline-secondary btn-sm border-0" '
  848. 'href="/test/issue/1/edit" title="Edit this issue">',
  849. output_text,
  850. )
  851. self.assertIn(
  852. '<div class="ml-2" id="priority_plain">', output_text
  853. )
  854. self.assertIn('<option value="1">High</option>', output_text)
  855. # Check that the ticket *does* have priorities
  856. output = self.app.get("/test/issue/1")
  857. self.assertEqual(output.status_code, 200)
  858. output_text = output.get_data(as_text=True)
  859. self.assertIn(
  860. '<div class="ml-2" id="priority_plain">', output_text
  861. )
  862. self.assertIn(
  863. '<option value="-1">Sky Falling</option>', output_text
  864. )
  865. self.assertIn('<option value="0">Urgent</option>', output_text)
  866. self.assertIn('<option value="1">High</option>', output_text)
  867. # Set the priority to Urgent
  868. data = {"priority": "0", "csrf_token": csrf_token}
  869. output = self.app.post(
  870. "/test/issue/1/update", data=data, follow_redirects=True
  871. )
  872. self.assertEqual(output.status_code, 200)
  873. output_text = output.get_data(as_text=True)
  874. self.assertIn(
  875. "<title>Issue #1: Test issue - test - Pagure</title>",
  876. output_text,
  877. )
  878. self.assertIn(
  879. '<a class="btn btn-outline-secondary btn-sm border-0" '
  880. 'href="/test/issue/1/edit" title="Edit this issue">',
  881. output_text,
  882. )
  883. self.assertIn(
  884. '<div class="ml-2" id="priority_plain">', output_text
  885. )
  886. self.assertIn(
  887. '<option value="-1">Sky Falling</option>', output_text
  888. )
  889. self.assertIn(
  890. '<option selected value="0">Urgent</option>', output_text
  891. )
  892. self.assertIn('<option value="1">High</option>', output_text)
  893. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  894. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  895. def test_set_priority_minus1(self):
  896. """ Test setting the priority of a ticket to -1. """
  897. tests.create_projects(self.session)
  898. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  899. # Start from scrach on priorities
  900. repo = pagure.lib.query._get_project(self.session, "test")
  901. self.assertEqual(repo.priorities, {})
  902. user = tests.FakeUser()
  903. user.username = "pingou"
  904. with tests.user_set(self.app.application, user):
  905. # Get the CSRF token
  906. output = self.app.get("/test/settings")
  907. self.assertEqual(output.status_code, 200)
  908. output_text = output.get_data(as_text=True)
  909. self.assertIn(
  910. "<title>Settings - test - Pagure</title>", output_text
  911. )
  912. self.assertIn(
  913. '<h5 class="pl-2 font-weight-bold text-muted">'
  914. "Project Settings</h5>\n",
  915. output_text,
  916. )
  917. csrf_token = output_text.split(
  918. 'name="csrf_token" type="hidden" value="'
  919. )[1].split('">')[0]
  920. # Set some priorities
  921. data = {
  922. "priority_weigth": [-1, 0, 1, 2, 3],
  923. "priority_title": [
  924. "Sky Falling",
  925. "Urgent",
  926. "High",
  927. "Normal",
  928. "Low",
  929. ],
  930. "csrf_token": csrf_token,
  931. }
  932. output = self.app.post(
  933. "/test/update/priorities", data=data, follow_redirects=True
  934. )
  935. self.assertEqual(output.status_code, 200)
  936. output_text = output.get_data(as_text=True)
  937. self.assertIn(
  938. "<title>Settings - test - Pagure</title>", output_text
  939. )
  940. self.assertIn(
  941. '<h5 class="pl-2 font-weight-bold text-muted">'
  942. "Project Settings</h5>\n",
  943. output_text,
  944. )
  945. # Check the result of the action -- Priority recorded
  946. self.session.commit()
  947. repo = pagure.lib.query._get_project(self.session, "test")
  948. self.assertEqual(
  949. repo.priorities,
  950. {
  951. "": "",
  952. "-1": "Sky Falling",
  953. "0": "Urgent",
  954. "1": "High",
  955. "2": "Normal",
  956. "3": "Low",
  957. },
  958. )
  959. # Create an issue
  960. data = {
  961. "title": "Test issue",
  962. "issue_content": "We really should improve on this issue",
  963. "status": "Open",
  964. "csrf_token": csrf_token,
  965. }
  966. output = self.app.post(
  967. "/test/new_issue", data=data, follow_redirects=True
  968. )
  969. self.assertEqual(output.status_code, 200)
  970. output_text = output.get_data(as_text=True)
  971. self.assertIn(
  972. "<title>Issue #1: Test issue - test - Pagure</title>",
  973. output_text,
  974. )
  975. self.assertIn(
  976. '<a class="btn btn-outline-secondary btn-sm border-0" '
  977. 'href="/test/issue/1/edit" title="Edit this issue">',
  978. output_text,
  979. )
  980. self.assertIn(
  981. '<div class="ml-2" id="priority_plain">', output_text
  982. )
  983. self.assertIn('<option value="1">High</option>', output_text)
  984. # Check that the ticket *does* have priorities
  985. output = self.app.get("/test/issue/1")
  986. self.assertEqual(output.status_code, 200)
  987. output_text = output.get_data(as_text=True)
  988. self.assertIn(
  989. '<div class="ml-2" id="priority_plain">', output_text
  990. )
  991. self.assertIn(
  992. '<option value="-1">Sky Falling</option>', output_text
  993. )
  994. self.assertIn('<option value="0">Urgent</option>', output_text)
  995. self.assertIn('<option value="1">High</option>', output_text)
  996. # Set the priority to Sky Falling
  997. data = {"priority": "-1", "csrf_token": csrf_token}
  998. output = self.app.post(
  999. "/test/issue/1/update", data=data, follow_redirects=True
  1000. )
  1001. self.assertEqual(output.status_code, 200)
  1002. output_text = output.get_data(as_text=True)
  1003. self.assertIn(
  1004. "<title>Issue #1: Test issue - test - Pagure</title>",
  1005. output_text,
  1006. )
  1007. self.assertIn(
  1008. '<a class="btn btn-outline-secondary btn-sm border-0" '
  1009. 'href="/test/issue/1/edit" title="Edit this issue">',
  1010. output_text,
  1011. )
  1012. self.assertIn(
  1013. '<div class="ml-2" id="priority_plain">', output_text
  1014. )
  1015. self.assertIn(
  1016. '<option selected value="-1">Sky Falling</option>', output_text
  1017. )
  1018. self.assertIn('<option value="0">Urgent</option>', output_text)
  1019. self.assertIn('<option value="1">High</option>', output_text)
  1020. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  1021. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1022. def test_default_priority(self):
  1023. """ Test updating the default priority of a repo. """
  1024. tests.create_projects(self.session)
  1025. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1026. # Check the default priorities
  1027. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1028. self.assertEqual(repo.priorities, {})
  1029. self.assertEqual(repo.default_priority, None)
  1030. user = tests.FakeUser()
  1031. user.username = "pingou"
  1032. with tests.user_set(self.app.application, user):
  1033. csrf_token = self.get_csrf()
  1034. # Set some priorities
  1035. data = {
  1036. "priority_weigth": [1, 2, 3],
  1037. "priority_title": ["High", "Normal", "Low"],
  1038. "csrf_token": csrf_token,
  1039. }
  1040. output = self.app.post(
  1041. "/test/update/priorities", data=data, follow_redirects=True
  1042. )
  1043. self.assertEqual(output.status_code, 200)
  1044. output_text = output.get_data(as_text=True)
  1045. # Check the redirect
  1046. self.assertIn(
  1047. "<title>Settings - test - Pagure</title>", output_text
  1048. )
  1049. self.assertIn(
  1050. '<h5 class="pl-2 font-weight-bold text-muted">'
  1051. "Project Settings</h5>\n",
  1052. output_text,
  1053. )
  1054. # Check the ordering
  1055. self.assertTrue(
  1056. output_text.find("High") < output_text.find("Normal")
  1057. )
  1058. self.assertTrue(
  1059. output_text.find("Normal") < output_text.find("Low")
  1060. )
  1061. # Check the result of the action -- Priority recorded
  1062. self.session.commit()
  1063. repo = pagure.lib.query.get_authorized_project(
  1064. self.session, "test"
  1065. )
  1066. self.assertEqual(
  1067. repo.priorities,
  1068. {"": "", "1": "High", "2": "Normal", "3": "Low"},
  1069. )
  1070. # Try setting the default priority -- no csrf
  1071. data = {"priority": "High"}
  1072. output = self.app.post(
  1073. "/test/update/default_priority",
  1074. data=data,
  1075. follow_redirects=True,
  1076. )
  1077. self.assertEqual(output.status_code, 200)
  1078. output_text = output.get_data(as_text=True)
  1079. # Check the redirect
  1080. self.assertIn(
  1081. "<title>Settings - test - Pagure</title>", output_text
  1082. )
  1083. self.assertIn(
  1084. '<h5 class="pl-2 font-weight-bold text-muted">'
  1085. "Project Settings</h5>\n",
  1086. output_text,
  1087. )
  1088. # Check the result of the action -- default_priority no change
  1089. self.session.commit()
  1090. repo = pagure.lib.query.get_authorized_project(
  1091. self.session, "test"
  1092. )
  1093. self.assertEqual(repo.default_priority, None)
  1094. # Try setting the default priority
  1095. data = {"priority": "High", "csrf_token": csrf_token}
  1096. output = self.app.post(
  1097. "/test/update/default_priority",
  1098. data=data,
  1099. follow_redirects=True,
  1100. )
  1101. self.assertEqual(output.status_code, 200)
  1102. output_text = output.get_data(as_text=True)
  1103. # Check the redirect
  1104. self.assertIn(
  1105. "<title>Settings - test - Pagure</title>", output_text
  1106. )
  1107. self.assertIn(
  1108. '<h5 class="pl-2 font-weight-bold text-muted">'
  1109. "Project Settings</h5>\n",
  1110. output_text,
  1111. )
  1112. self.assertIn("Default priority set " "to High", output_text)
  1113. # Check the result of the action -- default_priority no change
  1114. self.session.commit()
  1115. repo = pagure.lib.query.get_authorized_project(
  1116. self.session, "test"
  1117. )
  1118. self.assertEqual(repo.default_priority, "High")
  1119. # Try setting a wrong default priority
  1120. data = {"priority": "Smooth", "csrf_token": csrf_token}
  1121. output = self.app.post(
  1122. "/test/update/default_priority",
  1123. data=data,
  1124. follow_redirects=True,
  1125. )
  1126. self.assertEqual(output.status_code, 200)
  1127. output_text = output.get_data(as_text=True)
  1128. # Check the redirect
  1129. self.assertIn(
  1130. "<title>Settings - test - Pagure</title>", output_text
  1131. )
  1132. self.assertIn(
  1133. '<h5 class="pl-2 font-weight-bold text-muted">'
  1134. "Project Settings</h5>\n",
  1135. output_text,
  1136. )
  1137. # Check the result of the action -- default_priority no change
  1138. self.session.commit()
  1139. repo = pagure.lib.query.get_authorized_project(
  1140. self.session, "test"
  1141. )
  1142. self.assertEqual(repo.default_priority, "High")
  1143. # reset the default priority
  1144. data = {"csrf_token": csrf_token, "priority": ""}
  1145. output = self.app.post(
  1146. "/test/update/default_priority",
  1147. data=data,
  1148. follow_redirects=True,
  1149. )
  1150. self.assertEqual(output.status_code, 200)
  1151. output_text = output.get_data(as_text=True)
  1152. # Check the redirect
  1153. self.assertIn(
  1154. "<title>Settings - test - Pagure</title>", output_text
  1155. )
  1156. self.assertIn(
  1157. '<h5 class="pl-2 font-weight-bold text-muted">'
  1158. "Project Settings</h5>\n",
  1159. output_text,
  1160. )
  1161. self.assertIn("Default priority reset", output_text)
  1162. # Check the result of the action -- default_priority no change
  1163. self.session.commit()
  1164. repo = pagure.lib.query.get_authorized_project(
  1165. self.session, "test"
  1166. )
  1167. self.assertEqual(repo.default_priority, None)
  1168. # Check the behavior if the project disabled the issue tracker
  1169. settings = repo.settings
  1170. settings["issue_tracker"] = False
  1171. repo.settings = settings
  1172. self.session.add(repo)
  1173. self.session.commit()
  1174. output = self.app.post("/test/update/default_priority", data=data)
  1175. self.assertEqual(output.status_code, 404)
  1176. # Check for an invalid project
  1177. output = self.app.post("/foo/update/default_priority", data=data)
  1178. self.assertEqual(output.status_code, 404)
  1179. # Check for a non-admin user
  1180. settings = repo.settings
  1181. settings["issue_tracker"] = True
  1182. repo.settings = settings
  1183. self.session.add(repo)
  1184. self.session.commit()
  1185. user.username = "ralph"
  1186. with tests.user_set(self.app.application, user):
  1187. output = self.app.post("/test/update/default_priority", data=data)
  1188. self.assertEqual(output.status_code, 403)
  1189. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  1190. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1191. def test_default_priority_reset_when_updating_priorities(self):
  1192. """ Test updating the default priority of a repo when updating the
  1193. priorities.
  1194. """
  1195. tests.create_projects(self.session)
  1196. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1197. # Check the default priorities
  1198. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1199. self.assertEqual(repo.priorities, {})
  1200. self.assertEqual(repo.default_priority, None)
  1201. user = tests.FakeUser()
  1202. user.username = "pingou"
  1203. with tests.user_set(self.app.application, user):
  1204. csrf_token = self.get_csrf()
  1205. # Set some priorities
  1206. data = {
  1207. "priority_weigth": [1, 2, 3],
  1208. "priority_title": ["High", "Normal", "Low"],
  1209. "csrf_token": csrf_token,
  1210. }
  1211. output = self.app.post(
  1212. "/test/update/priorities", data=data, follow_redirects=True
  1213. )
  1214. self.assertEqual(output.status_code, 200)
  1215. output_text = output.get_data(as_text=True)
  1216. # Check the redirect
  1217. self.assertIn(
  1218. "<title>Settings - test - Pagure</title>", output_text
  1219. )
  1220. self.assertIn(
  1221. '<h5 class="pl-2 font-weight-bold text-muted">'
  1222. "Project Settings</h5>\n",
  1223. output_text,
  1224. )
  1225. # Check the ordering
  1226. self.assertTrue(
  1227. output_text.find("High") < output_text.find("Normal")
  1228. )
  1229. self.assertTrue(
  1230. output_text.find("Normal") < output_text.find("Low")
  1231. )
  1232. # Check the result of the action -- Priority recorded
  1233. self.session.commit()
  1234. repo = pagure.lib.query.get_authorized_project(
  1235. self.session, "test"
  1236. )
  1237. self.assertEqual(
  1238. repo.priorities,
  1239. {"": "", "1": "High", "2": "Normal", "3": "Low"},
  1240. )
  1241. # Try setting the default priority
  1242. data = {"priority": "High", "csrf_token": csrf_token}
  1243. output = self.app.post(
  1244. "/test/update/default_priority",
  1245. data=data,
  1246. follow_redirects=True,
  1247. )
  1248. self.assertEqual(output.status_code, 200)
  1249. output_text = output.get_data(as_text=True)
  1250. # Check the redirect
  1251. self.assertIn(
  1252. "<title>Settings - test - Pagure</title>", output_text
  1253. )
  1254. self.assertIn(
  1255. '<h5 class="pl-2 font-weight-bold text-muted">'
  1256. "Project Settings</h5>\n",
  1257. output_text,
  1258. )
  1259. self.assertIn("Default priority set " "to High", output_text)
  1260. # Check the result of the action -- default_priority no change
  1261. self.session.commit()
  1262. repo = pagure.lib.query.get_authorized_project(
  1263. self.session, "test"
  1264. )
  1265. self.assertEqual(repo.default_priority, "High")
  1266. # Remove the Hight priority
  1267. data = {
  1268. "priority_weigth": [1, 2],
  1269. "priority_title": ["Normal", "Low"],
  1270. "csrf_token": csrf_token,
  1271. }
  1272. output = self.app.post(
  1273. "/test/update/priorities", data=data, follow_redirects=True
  1274. )
  1275. self.assertEqual(output.status_code, 200)
  1276. output_text = output.get_data(as_text=True)
  1277. # Check the redirect
  1278. self.assertIn(
  1279. "<title>Settings - test - Pagure</title>", output_text
  1280. )
  1281. self.assertIn(
  1282. '<h5 class="pl-2 font-weight-bold text-muted">'
  1283. "Project Settings</h5>\n",
  1284. output_text,
  1285. )
  1286. self.assertIn("Priorities updated", output_text)
  1287. self.assertIn(
  1288. "Default priority reset "
  1289. "as it is no longer one of set priorities.",
  1290. output_text,
  1291. )
  1292. # Check the ordering
  1293. self.assertTrue(
  1294. output_text.find("Normal") < output_text.find("Low")
  1295. )
  1296. # Check the result of the action -- Priority recorded
  1297. self.session.commit()
  1298. repo = pagure.lib.query.get_authorized_project(
  1299. self.session, "test"
  1300. )
  1301. self.assertEqual(
  1302. repo.priorities, {"": "", "1": "Normal", "2": "Low"}
  1303. )
  1304. # Default priority is now None
  1305. self.assertIsNone(repo.default_priority)
  1306. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  1307. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1308. def test_default_priority_on_new_ticket(self):
  1309. """ Test updating the default priority of a repo. """
  1310. tests.create_projects(self.session)
  1311. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1312. # Set some priority and the default one
  1313. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1314. repo.priorities = {"1": "High", "2": "Normal"}
  1315. repo.default_priority = "Normal"
  1316. self.session.add(repo)
  1317. self.session.commit()
  1318. # Check the default priorities
  1319. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1320. self.assertEqual(repo.priorities, {"1": "High", "2": "Normal"})
  1321. self.assertEqual(repo.default_priority, "Normal")
  1322. user = tests.FakeUser()
  1323. user.username = "pingou"
  1324. with tests.user_set(self.app.application, user):
  1325. csrf_token = self.get_csrf()
  1326. data = {
  1327. "title": "Test issue",
  1328. "issue_content": "We really should improve on this issue",
  1329. "status": "Open",
  1330. "csrf_token": csrf_token,
  1331. }
  1332. output = self.app.post(
  1333. "/test/new_issue", data=data, follow_redirects=True
  1334. )
  1335. self.assertEqual(output.status_code, 200)
  1336. output_text = output.get_data(as_text=True)
  1337. self.assertIn(
  1338. "<title>Issue #1: Test issue - test - Pagure</title>",
  1339. output_text,
  1340. )
  1341. self.assertIn(
  1342. '<a class="btn btn-outline-secondary btn-sm border-0" '
  1343. 'href="/test/issue/1/edit" title="Edit this issue">',
  1344. output_text,
  1345. )
  1346. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1347. self.assertEqual(len(repo.issues), 1)
  1348. self.assertEqual(repo.issues[0].priority, 2)
  1349. if __name__ == "__main__":
  1350. unittest.main(verbosity=2)