test_pagure_flask_ui_issues.py 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import json
  10. import unittest
  11. import shutil
  12. import sys
  13. import os
  14. import pygit2
  15. from mock import patch
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure.lib
  19. import tests
  20. class PagureFlaskIssuestests(tests.Modeltests):
  21. """ Tests for flask issues controller of pagure """
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskIssuestests, self).setUp()
  25. pagure.APP.config['TESTING'] = True
  26. pagure.SESSION = self.session
  27. pagure.ui.SESSION = self.session
  28. pagure.ui.app.SESSION = self.session
  29. pagure.ui.issues.SESSION = self.session
  30. pagure.ui.repo.SESSION = self.session
  31. pagure.ui.filters.SESSION = self.session
  32. pagure.APP.config['GIT_FOLDER'] = tests.HERE
  33. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  34. tests.HERE, 'forks')
  35. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  36. tests.HERE, 'tickets')
  37. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  38. tests.HERE, 'docs')
  39. self.app = pagure.APP.test_client()
  40. @patch('pagure.lib.git.update_git')
  41. @patch('pagure.lib.notify.send_email')
  42. def test_new_issue(self, p_send_email, p_ugt):
  43. """ Test the new_issue endpoint. """
  44. p_send_email.return_value = True
  45. p_ugt.return_value = True
  46. output = self.app.get('/foo/new_issue')
  47. self.assertEqual(output.status_code, 302)
  48. user = tests.FakeUser()
  49. with tests.user_set(pagure.APP, user):
  50. output = self.app.get('/foo/new_issue')
  51. self.assertEqual(output.status_code, 404)
  52. tests.create_projects(self.session)
  53. tests.create_projects_git(
  54. os.path.join(tests.HERE), bare=True)
  55. output = self.app.get('/test/new_issue')
  56. self.assertEqual(output.status_code, 200)
  57. self.assertTrue(
  58. '<div class="card-header">\n New issue'
  59. in output.data)
  60. csrf_token = output.data.split(
  61. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  62. data = {
  63. }
  64. # Insufficient input
  65. output = self.app.post('/test/new_issue', data=data)
  66. self.assertEqual(output.status_code, 200)
  67. self.assertTrue(
  68. '<div class="card-header">\n New issue'
  69. in output.data)
  70. self.assertEqual(output.data.count(
  71. 'This field is required.'), 2)
  72. data['title'] = 'Test issue'
  73. output = self.app.post('/test/new_issue', data=data)
  74. self.assertEqual(output.status_code, 200)
  75. self.assertTrue(
  76. '<div class="card-header">\n New issue'
  77. in output.data)
  78. self.assertEqual(output.data.count(
  79. 'This field is required.'), 1)
  80. data['issue_content'] = 'We really should improve on this issue'
  81. data['status'] = 'Open'
  82. output = self.app.post('/test/new_issue', data=data)
  83. self.assertEqual(output.status_code, 200)
  84. self.assertTrue(
  85. '<div class="card-header">\n New issue'
  86. in output.data)
  87. self.assertEqual(output.data.count(
  88. '</button>\n This field is required.'),
  89. 0)
  90. # Invalid user
  91. data['csrf_token'] = csrf_token
  92. output = self.app.post('/test/new_issue', data=data)
  93. self.assertEqual(output.status_code, 200)
  94. self.assertTrue(
  95. '<div class="card-header">\n New issue'
  96. in output.data)
  97. self.assertEqual(output.data.count(
  98. '<td class="errors">This field is required.</td>'), 0)
  99. self.assertTrue(
  100. '</button>\n No user &#34;username&#34; found'
  101. in output.data)
  102. user.username = 'pingou'
  103. with tests.user_set(pagure.APP, user):
  104. output = self.app.post(
  105. '/test/new_issue', data=data, follow_redirects=True)
  106. self.assertEqual(output.status_code, 200)
  107. self.assertIn(
  108. '<title>Issue #1: Test issue - test - Pagure</title>',
  109. output.data)
  110. self.assertIn(
  111. '<a class="btn btn-primary btn-sm" '
  112. 'href="/test/issue/1/edit" title="Edit this issue">',
  113. output.data)
  114. # Project w/o issue tracker
  115. repo = pagure.lib.get_project(self.session, 'test')
  116. repo.settings = {'issue_tracker': False}
  117. self.session.add(repo)
  118. self.session.commit()
  119. user.username = 'pingou'
  120. with tests.user_set(pagure.APP, user):
  121. output = self.app.post(
  122. '/test/new_issue', data=data, follow_redirects=True)
  123. self.assertEqual(output.status_code, 404)
  124. @patch('pagure.lib.git.update_git')
  125. @patch('pagure.lib.notify.send_email')
  126. def test_new_issue_w_file(self, p_send_email, p_ugt):
  127. """ Test the new_issue endpoint with a file. """
  128. p_send_email.return_value = True
  129. p_ugt.return_value = True
  130. tests.create_projects(self.session)
  131. tests.create_projects_git(
  132. os.path.join(tests.HERE), bare=True)
  133. tests.create_projects_git(
  134. os.path.join(tests.HERE, 'tickets'), bare=True)
  135. user = tests.FakeUser()
  136. user.username = 'pingou'
  137. with tests.user_set(pagure.APP, user):
  138. output = self.app.get('/test/new_issue')
  139. self.assertEqual(output.status_code, 200)
  140. self.assertTrue(
  141. '<div class="card-header">\n New issue'
  142. in output.data)
  143. csrf_token = output.data.split(
  144. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  145. stream = open(os.path.join(tests.HERE, 'placebo.png'), 'r')
  146. data = {
  147. 'title': 'Test issue',
  148. 'issue_content': 'We really should improve on this issue\n'
  149. '<!!image>',
  150. 'status': 'Open',
  151. 'filestream': stream,
  152. 'enctype': 'multipart/form-data',
  153. 'csrf_token': csrf_token,
  154. }
  155. output = self.app.post(
  156. '/test/new_issue', data=data, follow_redirects=True)
  157. stream.close()
  158. self.assertEqual(output.status_code, 200)
  159. self.assertIn(
  160. '<title>Issue #1: Test issue - test - Pagure</title>',
  161. output.data)
  162. self.assertIn(
  163. '<a class="btn btn-primary btn-sm" '
  164. 'href="/test/issue/1/edit" title="Edit this issue">',
  165. output.data)
  166. # Project w/o issue tracker
  167. repo = pagure.lib.get_project(self.session, 'test')
  168. repo.settings = {'issue_tracker': False}
  169. self.session.add(repo)
  170. self.session.commit()
  171. user.username = 'pingou'
  172. with tests.user_set(pagure.APP, user):
  173. stream = open(os.path.join(tests.HERE, 'placebo.png'), 'r')
  174. data = {
  175. 'title': 'Test issue',
  176. 'issue_content': 'We really should improve on this issue',
  177. 'status': 'Open',
  178. 'filestream': stream,
  179. 'enctype': 'multipart/form-data',
  180. 'csrf_token': csrf_token,
  181. }
  182. output = self.app.post(
  183. '/test/new_issue', data=data, follow_redirects=True)
  184. self.assertEqual(output.status_code, 404)
  185. @patch('pagure.lib.git.update_git')
  186. @patch('pagure.lib.notify.send_email')
  187. def test_view_issues(self, p_send_email, p_ugt):
  188. """ Test the view_issues endpoint. """
  189. p_send_email.return_value = True
  190. p_ugt.return_value = True
  191. output = self.app.get('/foo/issues')
  192. self.assertEqual(output.status_code, 404)
  193. tests.create_projects(self.session)
  194. tests.create_projects_git(
  195. os.path.join(tests.HERE), bare=True)
  196. output = self.app.get('/test/issues')
  197. self.assertEqual(output.status_code, 200)
  198. self.assertIn(
  199. 'div class="projectinfo m-t-1 m-b-1">\ntest project #1 '
  200. '</div>', output.data)
  201. self.assertTrue(
  202. '<h2>\n 0 Open Issues' in output.data)
  203. # Create issues to play with
  204. repo = pagure.lib.get_project(self.session, 'test')
  205. msg = pagure.lib.new_issue(
  206. session=self.session,
  207. repo=repo,
  208. title='Test issue',
  209. content='We should work on this',
  210. user='pingou',
  211. ticketfolder=None
  212. )
  213. self.session.commit()
  214. self.assertEqual(msg.title, 'Test issue')
  215. # Whole list
  216. output = self.app.get('/test/issues')
  217. self.assertEqual(output.status_code, 200)
  218. self.assertIn('<title>Issues - test - Pagure</title>', output.data)
  219. self.assertTrue(
  220. '<h2>\n 1 Open Issues' in output.data)
  221. # Status = closed
  222. output = self.app.get('/test/issues?status=cloSED')
  223. self.assertEqual(output.status_code, 200)
  224. self.assertIn('<title>Issues - test - Pagure</title>', output.data)
  225. self.assertTrue(
  226. '<h2>\n 0 Closed Issues' in output.data)
  227. # Status = fixed
  228. output = self.app.get('/test/issues?status=fixed')
  229. self.assertEqual(output.status_code, 200)
  230. self.assertIn('<title>Issues - test - Pagure</title>', output.data)
  231. self.assertTrue(
  232. '<h2>\n 0 Closed Issues' in output.data)
  233. # Project w/o issue tracker
  234. repo = pagure.lib.get_project(self.session, 'test')
  235. repo.settings = {'issue_tracker': False}
  236. self.session.add(repo)
  237. self.session.commit()
  238. output = self.app.get('/test/issues')
  239. self.assertEqual(output.status_code, 404)
  240. @patch('pagure.lib.git.update_git')
  241. @patch('pagure.lib.notify.send_email')
  242. def test_view_issue(self, p_send_email, p_ugt):
  243. """ Test the view_issue endpoint. """
  244. p_send_email.return_value = True
  245. p_ugt.return_value = True
  246. output = self.app.get('/foo/issue/1')
  247. self.assertEqual(output.status_code, 404)
  248. tests.create_projects(self.session)
  249. tests.create_projects_git(
  250. os.path.join(tests.HERE), bare=True)
  251. output = self.app.get('/test/issue/1')
  252. self.assertEqual(output.status_code, 404)
  253. # Create issues to play with
  254. repo = pagure.lib.get_project(self.session, 'test')
  255. msg = pagure.lib.new_issue(
  256. session=self.session,
  257. repo=repo,
  258. title='Test issue',
  259. content='We should work on this',
  260. user='pingou',
  261. ticketfolder=None
  262. )
  263. self.session.commit()
  264. self.assertEqual(msg.title, 'Test issue')
  265. output = self.app.get('/test/issue/1')
  266. self.assertEqual(output.status_code, 200)
  267. # Not authentified = No edit
  268. self.assertNotIn(
  269. '<a class="btn btn-primary btn-sm" href="/test/issue/1/edit" '
  270. 'title="Edit this issue">',
  271. output.data)
  272. self.assertTrue(
  273. '<a href="/login/?next=http%3A%2F%2Flocalhost%2Ftest%2Fissue%2F1">'
  274. 'Login</a>\n to comment on this ticket.'
  275. in output.data)
  276. user = tests.FakeUser()
  277. with tests.user_set(pagure.APP, user):
  278. output = self.app.get('/test/issue/1')
  279. self.assertEqual(output.status_code, 200)
  280. # Not author nor admin = No edit
  281. self.assertNotIn(
  282. '<a class="btn btn-primary btn-sm" '
  283. 'href="/test/issue/1/edit" title="Edit this issue">',
  284. output.data)
  285. self.assertNotIn(
  286. '<button class="btn btn-danger btn-sm" type="submit"',
  287. output.data)
  288. self.assertNotIn('title="Delete this ticket">', output.data)
  289. self.assertFalse(
  290. '<a href="/login/">Login</a> to comment on this ticket.'
  291. in output.data)
  292. user.username = 'pingou'
  293. with tests.user_set(pagure.APP, user):
  294. output = self.app.get('/test/issue/1')
  295. self.assertEqual(output.status_code, 200)
  296. self.assertIn(
  297. '<a class="btn btn-primary btn-sm" '
  298. 'href="/test/issue/1/edit" title="Edit this issue">',
  299. output.data)
  300. self.assertIn(
  301. '<button class="btn btn-danger btn-sm" type="submit"',
  302. output.data)
  303. self.assertIn('title="Delete this ticket">', output.data)
  304. csrf_token = output.data.split(
  305. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  306. # Create private issue
  307. repo = pagure.lib.get_project(self.session, 'test')
  308. msg = pagure.lib.new_issue(
  309. session=self.session,
  310. repo=repo,
  311. title='Test issue',
  312. content='We should work on this',
  313. user='pingou',
  314. ticketfolder=None,
  315. private=True,
  316. )
  317. self.session.commit()
  318. self.assertEqual(msg.title, 'Test issue')
  319. # Not logged in
  320. output = self.app.get('/test/issue/2')
  321. self.assertEqual(output.status_code, 403)
  322. # Wrong user
  323. user = tests.FakeUser()
  324. with tests.user_set(pagure.APP, user):
  325. output = self.app.get('/test/issue/2')
  326. self.assertEqual(output.status_code, 403)
  327. # reporter
  328. user.username = 'pingou'
  329. with tests.user_set(pagure.APP, user):
  330. output = self.app.get('/test/issue/2')
  331. self.assertEqual(output.status_code, 200)
  332. self.assertIn(
  333. '<title>Issue #2: Test issue - test - Pagure</title>',
  334. output.data)
  335. self.assertIn(
  336. '<span class="oi red-icon" data-glyph="lock-locked" '
  337. 'title="Private issue"></span>', output.data)
  338. self.assertIn(
  339. '<a class="btn btn-primary btn-sm" '
  340. 'href="/test/issue/2/edit" title="Edit this issue">',
  341. output.data)
  342. # Project w/o issue tracker
  343. repo = pagure.lib.get_project(self.session, 'test')
  344. repo.settings = {'issue_tracker': False}
  345. self.session.add(repo)
  346. self.session.commit()
  347. output = self.app.get('/test/issue/1')
  348. self.assertEqual(output.status_code, 404)
  349. @patch('pagure.lib.git.update_git')
  350. @patch('pagure.lib.notify.send_email')
  351. def test_update_issue(self, p_send_email, p_ugt):
  352. """ Test the update_issue endpoint. """
  353. p_send_email.return_value = True
  354. p_ugt.return_value = True
  355. output = self.app.get('/foo/issue/1/update')
  356. self.assertEqual(output.status_code, 302)
  357. tests.create_projects(self.session)
  358. tests.create_projects_git(
  359. os.path.join(tests.HERE), bare=True)
  360. output = self.app.get('/test/issue/1/update')
  361. self.assertEqual(output.status_code, 302)
  362. # Create issues to play with
  363. repo = pagure.lib.get_project(self.session, 'test')
  364. msg = pagure.lib.new_issue(
  365. session=self.session,
  366. repo=repo,
  367. title='Test issue',
  368. content='We should work on this',
  369. user='pingou',
  370. ticketfolder=None
  371. )
  372. self.session.commit()
  373. self.assertEqual(msg.title, 'Test issue')
  374. user = tests.FakeUser()
  375. user.username = 'pingou'
  376. with tests.user_set(pagure.APP, user):
  377. output = self.app.get('/test/issue/1')
  378. self.assertEqual(output.status_code, 200)
  379. self.assertIn(
  380. '<title>Issue #1: Test issue - test - Pagure</title>',
  381. output.data)
  382. self.assertIn(
  383. '<a class="btn btn-primary btn-sm" '
  384. 'href="/test/issue/1/edit" title="Edit this issue">',
  385. output.data)
  386. csrf_token = output.data.split(
  387. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  388. data = {
  389. 'status': 'fixed'
  390. }
  391. # Invalid repo
  392. output = self.app.post('/bar/issue/1/update', data=data)
  393. self.assertEqual(output.status_code, 404)
  394. # Non-existing issue
  395. output = self.app.post('/test/issue/100/update', data=data)
  396. self.assertEqual(output.status_code, 404)
  397. output = self.app.post(
  398. '/test/issue/1/update', data=data, follow_redirects=True)
  399. self.assertEqual(output.status_code, 200)
  400. self.assertIn(
  401. '<title>Issue #1: Test issue - test - Pagure</title>',
  402. output.data)
  403. self.assertIn(
  404. '<a class="btn btn-primary btn-sm" '
  405. 'href="/test/issue/1/edit" title="Edit this issue">',
  406. output.data)
  407. self.assertFalse(
  408. '<option selected value="Fixed">Fixed</option>'
  409. in output.data)
  410. data['status'] = 'Fixed'
  411. output = self.app.post(
  412. '/test/issue/1/update', data=data, follow_redirects=True)
  413. self.assertEqual(output.status_code, 200)
  414. self.assertIn(
  415. '<title>Issue #1: Test issue - test - Pagure</title>',
  416. output.data)
  417. self.assertIn(
  418. '<a class="btn btn-primary btn-sm" '
  419. 'href="/test/issue/1/edit" title="Edit this issue">',
  420. output.data)
  421. self.assertFalse(
  422. '<option selected value="Fixed">Fixed</option>'
  423. in output.data)
  424. data['csrf_token'] = csrf_token
  425. output = self.app.post(
  426. '/test/issue/1/update', data=data, follow_redirects=True)
  427. self.assertEqual(output.status_code, 200)
  428. self.assertIn(
  429. '<title>Issue #1: Test issue - test - Pagure</title>',
  430. output.data)
  431. self.assertIn(
  432. '<a class="btn btn-primary btn-sm" '
  433. 'href="/test/issue/1/edit" title="Edit this issue">',
  434. output.data)
  435. self.assertIn(
  436. '</button>\n Successfully edited issue #1',
  437. output.data)
  438. self.assertTrue(
  439. '<option selected value="Fixed">Fixed</option>'
  440. in output.data)
  441. # Add new comment
  442. data = {
  443. 'csrf_token': csrf_token,
  444. 'status': 'Fixed',
  445. 'comment': 'Woohoo a second comment !',
  446. }
  447. output = self.app.post(
  448. '/test/issue/1/update', data=data, follow_redirects=True)
  449. self.assertEqual(output.status_code, 200)
  450. self.assertIn(
  451. '<title>Issue #1: Test issue - test - Pagure</title>',
  452. output.data)
  453. self.assertIn(
  454. '<a class="btn btn-primary btn-sm" '
  455. 'href="/test/issue/1/edit" title="Edit this issue">',
  456. output.data)
  457. self.assertIn(
  458. '</button>\n Comment added',
  459. output.data)
  460. self.assertNotIn(
  461. '</button>\n No changes to edit',
  462. output.data)
  463. self.assertTrue(
  464. '<p>Woohoo a second comment !</p>' in output.data)
  465. self.assertEqual(output.data.count('comment_body">'), 2)
  466. self.assertTrue(
  467. '<option selected value="Fixed">Fixed</option>'
  468. in output.data)
  469. # Add new tag
  470. data = {
  471. 'csrf_token': csrf_token,
  472. 'status': 'Fixed',
  473. 'tag': 'tag2',
  474. }
  475. output = self.app.post(
  476. '/test/issue/1/update', data=data, follow_redirects=True)
  477. self.assertEqual(output.status_code, 200)
  478. self.assertIn(
  479. '<title>Issue #1: Test issue - test - Pagure</title>',
  480. output.data)
  481. self.assertIn(
  482. '<a class="btn btn-primary btn-sm" '
  483. 'href="/test/issue/1/edit" title="Edit this issue">',
  484. output.data)
  485. self.assertIn(
  486. '</button>\n Tag added: tag2',
  487. output.data)
  488. self.assertNotIn(
  489. '</button>\n No changes to edit',
  490. output.data)
  491. self.assertTrue(
  492. '<p>Woohoo a second comment !</p>' in output.data)
  493. self.assertEqual(output.data.count('comment_body">'), 2)
  494. self.assertTrue(
  495. '<option selected value="Fixed">Fixed</option>'
  496. in output.data)
  497. # Assign issue to an non-existent user
  498. data = {
  499. 'csrf_token': csrf_token,
  500. 'status': 'Fixed',
  501. 'assignee': 'ralph',
  502. }
  503. output = self.app.post(
  504. '/test/issue/1/update', data=data, follow_redirects=True)
  505. self.assertEqual(output.status_code, 200)
  506. self.assertIn(
  507. '<title>Issue #1: Test issue - test - Pagure</title>',
  508. output.data)
  509. self.assertIn(
  510. '<a class="btn btn-primary btn-sm" '
  511. 'href="/test/issue/1/edit" title="Edit this issue">',
  512. output.data)
  513. self.assertIn(
  514. '</button>\n No user &#34;ralph&#34; found',
  515. output.data)
  516. self.assertTrue(
  517. '<p>Woohoo a second comment !</p>' in output.data)
  518. self.assertEqual(output.data.count('comment_body">'), 2)
  519. self.assertTrue(
  520. '<option selected value="Fixed">Fixed</option>'
  521. in output.data)
  522. # Assign issue properly
  523. data = {
  524. 'csrf_token': csrf_token,
  525. 'status': 'Fixed',
  526. 'assignee': 'pingou',
  527. }
  528. output = self.app.post(
  529. '/test/issue/1/update', data=data, follow_redirects=True)
  530. self.assertEqual(output.status_code, 200)
  531. self.assertIn(
  532. '<title>Issue #1: Test issue - test - Pagure</title>',
  533. output.data)
  534. self.assertIn(
  535. '<a class="btn btn-primary btn-sm" '
  536. 'href="/test/issue/1/edit" title="Edit this issue">',
  537. output.data)
  538. self.assertIn(
  539. '</button>\n Issue assigned',
  540. output.data)
  541. self.assertTrue(
  542. '<a href="/test/issues?assignee=pingou">' in output.data)
  543. self.assertTrue(
  544. '<p>Woohoo a second comment !</p>' in output.data)
  545. self.assertEqual(output.data.count('comment_body">'), 2)
  546. self.assertTrue(
  547. '<option selected value="Fixed">Fixed</option>'
  548. in output.data)
  549. # Create another issue with a dependency
  550. repo = pagure.lib.get_project(self.session, 'test')
  551. msg = pagure.lib.new_issue(
  552. session=self.session,
  553. repo=repo,
  554. title='Test issue',
  555. content='We should work on this',
  556. user='pingou',
  557. ticketfolder=None
  558. )
  559. self.session.commit()
  560. self.assertEqual(msg.title, 'Test issue')
  561. # Reset the status of the first issue
  562. parent_issue = pagure.lib.search_issues(
  563. self.session, repo, issueid=2)
  564. parent_issue.status = 'Open'
  565. # Add the dependency relationship
  566. self.session.add(parent_issue)
  567. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  568. issue.parents.append(parent_issue)
  569. self.session.add(issue)
  570. self.session.commit()
  571. with tests.user_set(pagure.APP, user):
  572. data['csrf_token'] = csrf_token
  573. output = self.app.post(
  574. '/test/issue/2/update', data=data, follow_redirects=True)
  575. self.assertEqual(output.status_code, 200)
  576. self.assertIn(
  577. '<title>Issue #2: Test issue - test - Pagure</title>',
  578. output.data)
  579. self.assertIn(
  580. '<a class="btn btn-primary btn-sm" '
  581. 'href="/test/issue/2/edit" title="Edit this issue">',
  582. output.data)
  583. self.assertIn(
  584. '</button>\n You cannot close a ticket '
  585. 'that has ticket depending that are still open.',
  586. output.data)
  587. self.assertTrue(
  588. '<option selected value="Open">Open</option>'
  589. in output.data)
  590. # Create private issue
  591. repo = pagure.lib.get_project(self.session, 'test')
  592. msg = pagure.lib.new_issue(
  593. session=self.session,
  594. repo=repo,
  595. title='Test issue',
  596. content='We should work on this',
  597. user='pingou',
  598. ticketfolder=None,
  599. private=True,
  600. )
  601. self.session.commit()
  602. self.assertEqual(msg.title, 'Test issue')
  603. # Wrong user
  604. user = tests.FakeUser()
  605. with tests.user_set(pagure.APP, user):
  606. output = self.app.post(
  607. '/test/issue/3/update', data=data, follow_redirects=True)
  608. self.assertEqual(output.status_code, 403)
  609. # Project w/o issue tracker
  610. repo = pagure.lib.get_project(self.session, 'test')
  611. repo.settings = {'issue_tracker': False}
  612. self.session.add(repo)
  613. self.session.commit()
  614. with tests.user_set(pagure.APP, user):
  615. output = self.app.get('/test/issue/1/update')
  616. self.assertEqual(output.status_code, 302)
  617. # Repo not set-up for issue tracker
  618. output = self.app.post('/test/issue/1/update', data=data)
  619. self.assertEqual(output.status_code, 404)
  620. @patch('pagure.lib.git.update_git')
  621. @patch('pagure.lib.notify.send_email')
  622. def test_update_issue_drop_comment(self, p_send_email, p_ugt):
  623. """ Test droping comment via the update_issue endpoint. """
  624. p_send_email.return_value = True
  625. p_ugt.return_value = True
  626. tests.create_projects(self.session)
  627. tests.create_projects_git(
  628. os.path.join(tests.HERE), bare=True)
  629. # Create issues to play with
  630. repo = pagure.lib.get_project(self.session, 'test')
  631. msg = pagure.lib.new_issue(
  632. session=self.session,
  633. repo=repo,
  634. title='Test issue',
  635. content='We should work on this',
  636. user='pingou',
  637. ticketfolder=None
  638. )
  639. self.session.commit()
  640. self.assertEqual(msg.title, 'Test issue')
  641. user = tests.FakeUser()
  642. user.username = 'pingou'
  643. with tests.user_set(pagure.APP, user):
  644. output = self.app.get('/test/issue/1')
  645. self.assertEqual(output.status_code, 200)
  646. self.assertIn(
  647. '<title>Issue #1: Test issue - test - Pagure</title>',
  648. output.data)
  649. self.assertIn(
  650. '<a class="btn btn-primary btn-sm" '
  651. 'href="/test/issue/1/edit" title="Edit this issue">',
  652. output.data)
  653. csrf_token = output.data.split(
  654. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  655. # Add new comment
  656. data = {
  657. 'csrf_token': csrf_token,
  658. 'comment': 'Woohoo a second comment !',
  659. }
  660. output = self.app.post(
  661. '/test/issue/1/update', data=data, follow_redirects=True)
  662. self.assertEqual(output.status_code, 200)
  663. self.assertIn(
  664. '<title>Issue #1: Test issue - test - Pagure</title>',
  665. output.data)
  666. self.assertIn(
  667. '<a class="btn btn-primary btn-sm" '
  668. 'href="/test/issue/1/edit" title="Edit this issue">',
  669. output.data)
  670. self.assertIn(
  671. '</button>\n Comment added',
  672. output.data)
  673. self.assertTrue(
  674. '<p>Woohoo a second comment !</p>' in output.data)
  675. self.assertEqual(output.data.count('comment_body">'), 2)
  676. repo = pagure.lib.get_project(self.session, 'test')
  677. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  678. self.assertEqual(len(issue.comments), 1)
  679. data = {
  680. 'csrf_token': csrf_token,
  681. 'drop_comment': 1,
  682. }
  683. user = tests.FakeUser()
  684. with tests.user_set(pagure.APP, user):
  685. # Wrong issue id
  686. output = self.app.post(
  687. '/test/issue/3/update', data=data, follow_redirects=True)
  688. self.assertEqual(output.status_code, 404)
  689. # Wrong user
  690. output = self.app.post(
  691. '/test/issue/1/update', data=data, follow_redirects=True)
  692. self.assertEqual(output.status_code, 403)
  693. user = tests.FakeUser()
  694. user.username = 'pingou'
  695. with tests.user_set(pagure.APP, user):
  696. # Drop the new comment
  697. output = self.app.post(
  698. '/test/issue/1/update', data=data, follow_redirects=True)
  699. self.assertEqual(output.status_code, 200)
  700. self.assertIn(
  701. '<title>Issue #1: Test issue - test - Pagure</title>',
  702. output.data)
  703. self.assertIn(
  704. '<a class="btn btn-primary btn-sm" '
  705. 'href="/test/issue/1/edit" title="Edit this issue">',
  706. output.data)
  707. self.assertIn(
  708. '</button>\n Comment removed',
  709. output.data)
  710. # Drop non-existant comment
  711. output = self.app.post(
  712. '/test/issue/1/update', data=data, follow_redirects=True)
  713. self.assertEqual(output.status_code, 404)
  714. repo = pagure.lib.get_project(self.session, 'test')
  715. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  716. self.assertEqual(len(issue.comments), 0)
  717. @patch('pagure.lib.git.update_git')
  718. @patch('pagure.lib.notify.send_email')
  719. def test_update_issue_depend(self, p_send_email, p_ugt):
  720. """ Test adding dependency via the update_issue endpoint. """
  721. p_send_email.return_value = True
  722. p_ugt.return_value = True
  723. tests.create_projects(self.session)
  724. tests.create_projects_git(
  725. os.path.join(tests.HERE), bare=True)
  726. # Create issues to play with
  727. repo = pagure.lib.get_project(self.session, 'test')
  728. msg = pagure.lib.new_issue(
  729. session=self.session,
  730. repo=repo,
  731. title='Test issue',
  732. content='We should work on this',
  733. user='pingou',
  734. ticketfolder=None
  735. )
  736. self.session.commit()
  737. self.assertEqual(msg.title, 'Test issue')
  738. repo = pagure.lib.get_project(self.session, 'test')
  739. msg = pagure.lib.new_issue(
  740. session=self.session,
  741. repo=repo,
  742. title='Test issue #2',
  743. content='We should work on this again',
  744. user='foo',
  745. ticketfolder=None
  746. )
  747. self.session.commit()
  748. self.assertEqual(msg.title, 'Test issue #2')
  749. user = tests.FakeUser()
  750. user.username = 'pingou'
  751. with tests.user_set(pagure.APP, user):
  752. output = self.app.get('/test/issue/1')
  753. self.assertEqual(output.status_code, 200)
  754. self.assertIn(
  755. '<title>Issue #1: Test issue - test - Pagure</title>',
  756. output.data)
  757. self.assertIn(
  758. '<a class="btn btn-primary btn-sm" '
  759. 'href="/test/issue/1/edit" title="Edit this issue">',
  760. output.data)
  761. csrf_token = output.data.split(
  762. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  763. # Add a dependent ticket
  764. data = {
  765. 'csrf_token': csrf_token,
  766. 'depends': '2',
  767. }
  768. output = self.app.post(
  769. '/test/issue/1/update', data=data, follow_redirects=True)
  770. self.assertEqual(output.status_code, 200)
  771. self.assertIn(
  772. '<title>Issue #1: Test issue - test - Pagure</title>',
  773. output.data)
  774. self.assertIn(
  775. '<a class="btn btn-primary btn-sm" '
  776. 'href="/test/issue/1/edit" title="Edit this issue">',
  777. output.data)
  778. self.assertIn(
  779. '</button>\n Dependency added',
  780. output.data)
  781. # Add an invalid dependent ticket
  782. data = {
  783. 'csrf_token': csrf_token,
  784. 'depends': '2,abc',
  785. }
  786. output = self.app.post(
  787. '/test/issue/1/update', data=data, follow_redirects=True)
  788. self.assertEqual(output.status_code, 200)
  789. self.assertIn(
  790. '<title>Issue #1: Test issue - test - Pagure</title>',
  791. output.data)
  792. self.assertIn(
  793. '<a class="btn btn-primary btn-sm" '
  794. 'href="/test/issue/1/edit" title="Edit this issue">',
  795. output.data)
  796. self.assertNotIn(
  797. '</button>\n Dependency added',
  798. output.data)
  799. repo = pagure.lib.get_project(self.session, 'test')
  800. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  801. self.assertEqual(issue.depends_text, [2])
  802. self.assertEqual(issue.blocks_text, [])
  803. @patch('pagure.lib.git.update_git')
  804. @patch('pagure.lib.notify.send_email')
  805. def test_update_issue_block(self, p_send_email, p_ugt):
  806. """ Test adding blocked issue via the update_issue endpoint. """
  807. p_send_email.return_value = True
  808. p_ugt.return_value = True
  809. tests.create_projects(self.session)
  810. tests.create_projects_git(
  811. os.path.join(tests.HERE), bare=True)
  812. # Create issues to play with
  813. repo = pagure.lib.get_project(self.session, 'test')
  814. msg = pagure.lib.new_issue(
  815. session=self.session,
  816. repo=repo,
  817. title='Test issue',
  818. content='We should work on this',
  819. user='pingou',
  820. ticketfolder=None
  821. )
  822. self.session.commit()
  823. self.assertEqual(msg.title, 'Test issue')
  824. repo = pagure.lib.get_project(self.session, 'test')
  825. msg = pagure.lib.new_issue(
  826. session=self.session,
  827. repo=repo,
  828. title='Test issue #2',
  829. content='We should work on this again',
  830. user='foo',
  831. ticketfolder=None
  832. )
  833. self.session.commit()
  834. self.assertEqual(msg.title, 'Test issue #2')
  835. user = tests.FakeUser()
  836. user.username = 'pingou'
  837. with tests.user_set(pagure.APP, user):
  838. output = self.app.get('/test/issue/1')
  839. self.assertEqual(output.status_code, 200)
  840. self.assertIn(
  841. '<title>Issue #1: Test issue - test - Pagure</title>',
  842. output.data)
  843. self.assertIn(
  844. '<a class="btn btn-primary btn-sm" '
  845. 'href="/test/issue/1/edit" title="Edit this issue">',
  846. output.data)
  847. csrf_token = output.data.split(
  848. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  849. # Add a dependent ticket
  850. data = {
  851. 'csrf_token': csrf_token,
  852. 'blocks': '2',
  853. }
  854. output = self.app.post(
  855. '/test/issue/1/update', data=data, follow_redirects=True)
  856. self.assertEqual(output.status_code, 200)
  857. self.assertIn(
  858. '<title>Issue #1: Test issue - test - Pagure</title>',
  859. output.data)
  860. self.assertIn(
  861. '<a class="btn btn-primary btn-sm" '
  862. 'href="/test/issue/1/edit" title="Edit this issue">',
  863. output.data)
  864. self.assertIn(
  865. '</button>\n Dependency added',
  866. output.data)
  867. # Add an invalid dependent ticket
  868. data = {
  869. 'csrf_token': csrf_token,
  870. 'blocks': '2,abc',
  871. }
  872. output = self.app.post(
  873. '/test/issue/1/update', data=data, follow_redirects=True)
  874. self.assertEqual(output.status_code, 200)
  875. self.assertIn(
  876. '<title>Issue #1: Test issue - test - Pagure</title>',
  877. output.data)
  878. self.assertIn(
  879. '<a class="btn btn-primary btn-sm" '
  880. 'href="/test/issue/1/edit" title="Edit this issue">',
  881. output.data)
  882. self.assertNotIn(
  883. '</button>\n Dependency added',
  884. output.data)
  885. repo = pagure.lib.get_project(self.session, 'test')
  886. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  887. self.assertEqual(issue.depends_text, [])
  888. self.assertEqual(issue.blocks_text, [2])
  889. @patch('pagure.lib.git.update_git')
  890. @patch('pagure.lib.notify.send_email')
  891. def test_upload_issue(self, p_send_email, p_ugt):
  892. """ Test the upload_issue endpoint. """
  893. p_send_email.return_value = True
  894. p_ugt.return_value = True
  895. tests.create_projects(self.session)
  896. tests.create_projects_git(
  897. os.path.join(tests.HERE), bare=True)
  898. tests.create_projects_git(
  899. os.path.join(tests.HERE, 'tickets'), bare=True)
  900. # Create issues to play with
  901. repo = pagure.lib.get_project(self.session, 'test')
  902. msg = pagure.lib.new_issue(
  903. session=self.session,
  904. repo=repo,
  905. title='Test issue',
  906. content='We should work on this',
  907. user='pingou',
  908. ticketfolder=None
  909. )
  910. self.session.commit()
  911. self.assertEqual(msg.title, 'Test issue')
  912. user = tests.FakeUser()
  913. user.username = 'pingou'
  914. with tests.user_set(pagure.APP, user):
  915. output = self.app.get('/test/issue/1')
  916. self.assertEqual(output.status_code, 200)
  917. self.assertIn(
  918. '<title>Issue #1: Test issue - test - Pagure</title>',
  919. output.data)
  920. self.assertIn(
  921. '<a class="btn btn-primary btn-sm" '
  922. 'href="/test/issue/1/edit" title="Edit this issue">',
  923. output.data)
  924. csrf_token = output.data.split(
  925. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  926. output = self.app.post('/foo/issue/1/upload')
  927. self.assertEqual(output.status_code, 404)
  928. output = self.app.post('/test/issue/100/upload')
  929. self.assertEqual(output.status_code, 404)
  930. # Invalid upload
  931. data = {
  932. 'enctype': 'multipart/form-data',
  933. }
  934. output = self.app.post(
  935. '/test/issue/1/upload', data=data, follow_redirects=True)
  936. self.assertEqual(output.status_code, 200)
  937. json_data = json.loads(output.data)
  938. exp = {'output': 'notok'}
  939. self.assertDictEqual(json_data, exp)
  940. # Attach a file to a ticket
  941. stream = open(os.path.join(tests.HERE, 'placebo.png'), 'rb')
  942. data = {
  943. 'csrf_token': csrf_token,
  944. 'filestream': stream,
  945. 'enctype': 'multipart/form-data',
  946. }
  947. output = self.app.post(
  948. '/test/issue/1/upload', data=data, follow_redirects=True)
  949. stream.close()
  950. self.assertEqual(output.status_code, 200)
  951. json_data = json.loads(output.data)
  952. folder = os.path.dirname(
  953. os.path.abspath(__file__))[1:].replace('/', '_')
  954. exp = {
  955. 'output': 'ok',
  956. 'filelocation': '/test/issue/raw/files/8a06845923010b27bfd8'
  957. 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a'
  958. 'be-%s_placebo.png' % folder,
  959. 'filename': '%s_placebo.png' % folder,
  960. }
  961. self.assertDictEqual(json_data, exp)
  962. # Project w/o issue tracker
  963. repo = pagure.lib.get_project(self.session, 'test')
  964. repo.settings = {'issue_tracker': False}
  965. self.session.add(repo)
  966. self.session.commit()
  967. with tests.user_set(pagure.APP, user):
  968. output = self.app.post('/test/issue/1/upload')
  969. self.assertEqual(output.status_code, 404)
  970. def test_view_issue_raw_file_empty(self):
  971. """ Test the view_issue_raw_file endpoint. """
  972. # Create the project and git repos
  973. tests.create_projects(self.session)
  974. tests.create_projects_git(
  975. os.path.join(tests.HERE, 'tickets'), bare=True)
  976. # Create issues to play with
  977. repo = pagure.lib.get_project(self.session, 'test')
  978. msg = pagure.lib.new_issue(
  979. session=self.session,
  980. repo=repo,
  981. title='Test issue',
  982. content='We should work on this',
  983. user='pingou',
  984. ticketfolder=None
  985. )
  986. self.session.commit()
  987. self.assertEqual(msg.title, 'Test issue')
  988. url = '/issue/raw/files/8a06845923010b27bfd8'\
  989. 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a'\
  990. 'be-home_pierrey_repos_gitrepo_pagure_tests'\
  991. '_placebo.png'
  992. output = self.app.get('/foo' + url)
  993. self.assertEqual(output.status_code, 404)
  994. output = self.app.get('/test' + url)
  995. self.assertEqual(output.status_code, 404)
  996. # Project w/o issue tracker
  997. repo = pagure.lib.get_project(self.session, 'test')
  998. repo.settings = {'issue_tracker': False}
  999. self.session.add(repo)
  1000. self.session.commit()
  1001. output = self.app.get('/test' + url)
  1002. self.assertEqual(output.status_code, 404)
  1003. def test_view_issue_raw_file(self):
  1004. """ Test the view_issue_raw_file endpoint. """
  1005. # Create the issue and upload to it
  1006. self.test_upload_issue()
  1007. # Project w/ issue tracker
  1008. repo = pagure.lib.get_project(self.session, 'test')
  1009. repo.settings = {'issue_tracker': True}
  1010. self.session.add(repo)
  1011. self.session.commit()
  1012. url = '/issue/raw/files/8a06845923010b27bfd8'\
  1013. 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a'\
  1014. 'be-%s_placebo.png' % os.path.dirname(
  1015. os.path.abspath(__file__))[1:].replace('/', '_')
  1016. output = self.app.get('/foo' + url)
  1017. self.assertEqual(output.status_code, 404)
  1018. output = self.app.get('/test/issue/raw/files/test.png')
  1019. self.assertEqual(output.status_code, 404)
  1020. # Access file by name
  1021. output = self.app.get('/test' + url)
  1022. self.assertEqual(output.status_code, 200)
  1023. # Project w/o issue tracker
  1024. repo = pagure.lib.get_project(self.session, 'test')
  1025. repo.settings = {'issue_tracker': False}
  1026. self.session.add(repo)
  1027. self.session.commit()
  1028. output = self.app.get('/test' + url)
  1029. self.assertEqual(output.status_code, 404)
  1030. @patch('pagure.lib.git.update_git')
  1031. @patch('pagure.lib.notify.send_email')
  1032. def test_edit_issue(self, p_send_email, p_ugt):
  1033. """ Test the edit_issue endpoint. """
  1034. p_send_email.return_value = True
  1035. p_ugt.return_value = True
  1036. output = self.app.get('/foo/issue/1/edit')
  1037. self.assertEqual(output.status_code, 302)
  1038. user = tests.FakeUser()
  1039. with tests.user_set(pagure.APP, user):
  1040. output = self.app.get('/foo/issue/1/edit')
  1041. self.assertEqual(output.status_code, 404)
  1042. tests.create_projects(self.session)
  1043. tests.create_projects_git(
  1044. os.path.join(tests.HERE), bare=True)
  1045. output = self.app.get('/test/issue/1/edit')
  1046. self.assertEqual(output.status_code, 404)
  1047. user.username = 'pingou'
  1048. with tests.user_set(pagure.APP, user):
  1049. output = self.app.get('/test/issue/1/edit')
  1050. self.assertEqual(output.status_code, 404)
  1051. # Create issues to play with
  1052. repo = pagure.lib.get_project(self.session, 'test')
  1053. msg = pagure.lib.new_issue(
  1054. session=self.session,
  1055. repo=repo,
  1056. title='Test issue',
  1057. content='We should work on this',
  1058. user='pingou',
  1059. ticketfolder=None
  1060. )
  1061. self.session.commit()
  1062. self.assertEqual(msg.title, 'Test issue')
  1063. user = tests.FakeUser()
  1064. with tests.user_set(pagure.APP, user):
  1065. output = self.app.get('/test/issue/1/edit')
  1066. self.assertEqual(output.status_code, 403)
  1067. user.username = 'pingou'
  1068. with tests.user_set(pagure.APP, user):
  1069. output = self.app.get('/test/issue/1/edit')
  1070. self.assertEqual(output.status_code, 200)
  1071. self.assertTrue(
  1072. '<div class="card-header">\n Edit '
  1073. 'issue #1\n </div>' in output.data)
  1074. csrf_token = output.data.split(
  1075. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1076. data = {
  1077. 'issue_content': 'We should work on this!'
  1078. }
  1079. output = self.app.post('/test/issue/1/edit', data=data)
  1080. self.assertEqual(output.status_code, 200)
  1081. self.assertTrue(
  1082. '<div class="card-header">\n Edit '
  1083. 'issue #1\n </div>' in output.data)
  1084. self.assertEqual(output.data.count(
  1085. '<small>\n This field is required.&nbsp;\n'
  1086. ' </small>'), 1)
  1087. self.assertEqual(output.data.count(
  1088. '<small>\n Not a valid choice&nbsp;'
  1089. '\n </small>'), 1)
  1090. data['status'] = 'Open'
  1091. data['title'] = 'Test issue #1'
  1092. output = self.app.post('/test/issue/1/edit', data=data)
  1093. self.assertEqual(output.status_code, 200)
  1094. self.assertTrue(
  1095. '<div class="card-header">\n Edit '
  1096. 'issue #1\n </div>' in output.data)
  1097. self.assertEqual(output.data.count(
  1098. '<small>\n This field is required.&nbsp;\n'
  1099. ' </small>'), 0)
  1100. self.assertEqual(output.data.count(
  1101. '<small>\n Not a valid choice&nbsp;'
  1102. '\n </small>'), 0)
  1103. data['csrf_token'] = csrf_token
  1104. output = self.app.post(
  1105. '/test/issue/1/edit', data=data, follow_redirects=True)
  1106. self.assertEqual(output.status_code, 200)
  1107. self.assertIn(
  1108. '</button>\n Successfully edited issue #1',
  1109. output.data)
  1110. self.assertIn(
  1111. '<span class="issueid label label-default">#1</span>\n'
  1112. ' <span id="issuetitle">Test issue #1</span>',
  1113. output.data)
  1114. self.assertEqual(output.data.count(
  1115. '<option selected value="Open">Open</option>'), 1)
  1116. self.assertEqual(output.data.count('comment_body">'), 1)
  1117. self.assertEqual(output.data.count(
  1118. '<p>We should work on this!</p>'), 1)
  1119. # Project w/o issue tracker
  1120. repo = pagure.lib.get_project(self.session, 'test')
  1121. repo.settings = {'issue_tracker': False}
  1122. self.session.add(repo)
  1123. self.session.commit()
  1124. user.username = 'pingou'
  1125. with tests.user_set(pagure.APP, user):
  1126. output = self.app.post('/test/issue/1/edit', data=data)
  1127. self.assertEqual(output.status_code, 404)
  1128. @patch('pagure.lib.git.update_git')
  1129. @patch('pagure.lib.notify.send_email')
  1130. def test_edit_tag(self, p_send_email, p_ugt):
  1131. """ Test the edit_tag endpoint. """
  1132. p_send_email.return_value = True
  1133. p_ugt.return_value = True
  1134. output = self.app.get('/foo/tag/foo/edit')
  1135. self.assertEqual(output.status_code, 302)
  1136. user = tests.FakeUser()
  1137. with tests.user_set(pagure.APP, user):
  1138. output = self.app.get('/foo/tag/foo/edit')
  1139. self.assertEqual(output.status_code, 404)
  1140. tests.create_projects(self.session)
  1141. output = self.app.get('/test/tag/foo/edit')
  1142. self.assertEqual(output.status_code, 403)
  1143. # Create issues to play with
  1144. repo = pagure.lib.get_project(self.session, 'test')
  1145. msg = pagure.lib.new_issue(
  1146. session=self.session,
  1147. repo=repo,
  1148. title='Test issue',
  1149. content='We should work on this',
  1150. user='pingou',
  1151. ticketfolder=None
  1152. )
  1153. self.session.commit()
  1154. self.assertEqual(msg.title, 'Test issue')
  1155. # Add a tag to the issue
  1156. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1157. msg = pagure.lib.add_tag_obj(
  1158. session=self.session,
  1159. obj=issue,
  1160. tags='tag1',
  1161. user='pingou',
  1162. ticketfolder=None)
  1163. self.session.commit()
  1164. self.assertEqual(msg, 'Tag added: tag1')
  1165. # Before edit, list tags
  1166. tags = pagure.lib.get_tags_of_project(self.session, repo)
  1167. self.assertEqual([tag.tag for tag in tags], ['tag1'])
  1168. # Edit tag
  1169. user.username = 'pingou'
  1170. with tests.user_set(pagure.APP, user):
  1171. #Edit a tag that doesn't exit
  1172. output = self.app.get('/test/tag/does_not_exist/edit')
  1173. self.assertEqual(output.status_code, 404)
  1174. output = self.app.get('/test/tag/tag1/edit')
  1175. self.assertEqual(output.status_code, 200)
  1176. self.assertTrue('<h2>Edit tag: tag1</h2>' in output.data)
  1177. self.assertTrue(
  1178. '<p>Enter in the field below the new name for the tag: '
  1179. '"tag1"</p>' in output.data)
  1180. csrf_token = output.data.split(
  1181. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1182. data = {'tag': 'tag2'}
  1183. output = self.app.post('/test/tag/tag1/edit', data=data)
  1184. self.assertEqual(output.status_code, 200)
  1185. self.assertTrue('<h2>Edit tag: tag1</h2>' in output.data)
  1186. self.assertTrue(
  1187. '<p>Enter in the field below the new name for the tag: '
  1188. '"tag1"</p>' in output.data)
  1189. data['csrf_token'] = csrf_token
  1190. tests.create_projects_git(tests.HERE)
  1191. output = self.app.post(
  1192. '/test/tag/tag1/edit', data=data, follow_redirects=True)
  1193. self.assertEqual(output.status_code, 200)
  1194. self.assertIn(
  1195. '<title>Settings - test - Pagure</title>', output.data)
  1196. self.assertIn(
  1197. '</button>\n Edited tag: tag1 to tag2',
  1198. output.data)
  1199. # After edit, list tags
  1200. tags = pagure.lib.get_tags_of_project(self.session, repo)
  1201. self.assertEqual([tag.tag for tag in tags], ['tag2'])
  1202. @patch('pagure.lib.git.update_git')
  1203. @patch('pagure.lib.notify.send_email')
  1204. def test_remove_tag(self, p_send_email, p_ugt):
  1205. """ Test the remove_tag endpoint. """
  1206. p_send_email.return_value = True
  1207. p_ugt.return_value = True
  1208. output = self.app.post('/foo/droptag/')
  1209. self.assertEqual(output.status_code, 302)
  1210. user = tests.FakeUser()
  1211. with tests.user_set(pagure.APP, user):
  1212. output = self.app.post('/foo/droptag/')
  1213. self.assertEqual(output.status_code, 404)
  1214. tests.create_projects(self.session)
  1215. output = self.app.post('/test/droptag/')
  1216. self.assertEqual(output.status_code, 403)
  1217. # Create issues to play with
  1218. repo = pagure.lib.get_project(self.session, 'test')
  1219. msg = pagure.lib.new_issue(
  1220. session=self.session,
  1221. repo=repo,
  1222. title='Test issue',
  1223. content='We should work on this',
  1224. user='pingou',
  1225. ticketfolder=None
  1226. )
  1227. self.session.commit()
  1228. self.assertEqual(msg.title, 'Test issue')
  1229. # Add a tag to the issue
  1230. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1231. msg = pagure.lib.add_tag_obj(
  1232. session=self.session,
  1233. obj=issue,
  1234. tags='tag1',
  1235. user='pingou',
  1236. ticketfolder=None)
  1237. self.session.commit()
  1238. self.assertEqual(msg, 'Tag added: tag1')
  1239. # Before edit, list tags
  1240. tags = pagure.lib.get_tags_of_project(self.session, repo)
  1241. self.assertEqual([tag.tag for tag in tags], ['tag1'])
  1242. # Edit tag
  1243. user.username = 'pingou'
  1244. with tests.user_set(pagure.APP, user):
  1245. tests.create_projects_git(tests.HERE)
  1246. output = self.app.post(
  1247. '/test/droptag/', data={}, follow_redirects=True)
  1248. self.assertEqual(output.status_code, 200)
  1249. self.assertTrue(
  1250. '<title>Settings - test - Pagure</title>' in output.data)
  1251. self.assertTrue("<h3>Settings for test</h3>" in output.data)
  1252. csrf_token = output.data.split(
  1253. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1254. data = {'tag': 'tag1'}
  1255. output = self.app.post(
  1256. '/test/droptag/', data=data, follow_redirects=True)
  1257. self.assertEqual(output.status_code, 200)
  1258. self.assertTrue("<h3>Settings for test</h3>" in output.data)
  1259. data['csrf_token'] = csrf_token
  1260. output = self.app.post(
  1261. '/test/droptag/', data=data, follow_redirects=True)
  1262. self.assertEqual(output.status_code, 200)
  1263. self.assertTrue("<h3>Settings for test</h3>" in output.data)
  1264. self.assertIn(
  1265. '</button>\n Removed tag: tag1',
  1266. output.data)
  1267. @patch('pagure.lib.git.update_git')
  1268. @patch('pagure.lib.notify.send_email')
  1269. def test_delete_issue(self, p_send_email, p_ugt):
  1270. """ Test the delete_issue endpoint. """
  1271. p_send_email.return_value = True
  1272. p_ugt.return_value = True
  1273. tests.create_projects(self.session)
  1274. tests.create_projects_git(tests.HERE)
  1275. tests.create_projects_git(os.path.join(tests.HERE, 'tickets'))
  1276. # Create issues to play with
  1277. repo = pagure.lib.get_project(self.session, 'test')
  1278. msg = pagure.lib.new_issue(
  1279. session=self.session,
  1280. repo=repo,
  1281. title='Test issue',
  1282. content='We should work on this',
  1283. user='pingou',
  1284. ticketfolder=None
  1285. )
  1286. self.session.commit()
  1287. self.assertEqual(msg.title, 'Test issue')
  1288. user = tests.FakeUser()
  1289. with tests.user_set(pagure.APP, user):
  1290. output = self.app.post(
  1291. '/foo/issue/1/drop', follow_redirects=True)
  1292. self.assertEqual(output.status_code, 404)
  1293. output = self.app.post(
  1294. '/test/issue/100/drop', follow_redirects=True)
  1295. self.assertEqual(output.status_code, 404)
  1296. output = self.app.post(
  1297. '/test/issue/1/drop', follow_redirects=True)
  1298. self.assertEqual(output.status_code, 403)
  1299. user.username = 'pingou'
  1300. with tests.user_set(pagure.APP, user):
  1301. output = self.app.post(
  1302. '/test/issue/1/drop', follow_redirects=True)
  1303. self.assertEqual(output.status_code, 200)
  1304. self.assertIn(
  1305. '<title>Issue #1: Test issue - test - Pagure</title>',
  1306. output.data)
  1307. csrf_token = output.data.split(
  1308. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1309. data = {
  1310. }
  1311. # No CSRF token
  1312. output = self.app.post(
  1313. '/test/issue/1/drop', data=data, follow_redirects=True)
  1314. self.assertEqual(output.status_code, 200)
  1315. self.assertIn(
  1316. '<title>Issue #1: Test issue - test - Pagure</title>',
  1317. output.data)
  1318. data['csrf_token'] = csrf_token
  1319. output = self.app.post(
  1320. '/test/issue/1/drop', data=data, follow_redirects=True)
  1321. self.assertEqual(output.status_code, 200)
  1322. self.assertIn(
  1323. '<title>Issues - test - Pagure</title>', output.data)
  1324. self.assertIn(
  1325. '</button>\n Issue deleted',
  1326. output.data)
  1327. # Project w/o issue tracker
  1328. repo = pagure.lib.get_project(self.session, 'test')
  1329. repo.settings = {'issue_tracker': False}
  1330. self.session.add(repo)
  1331. self.session.commit()
  1332. user.username = 'pingou'
  1333. with tests.user_set(pagure.APP, user):
  1334. output = self.app.post('/test/issue/1/drop', data=data)
  1335. self.assertEqual(output.status_code, 404)
  1336. @patch('pagure.lib.git.update_git')
  1337. @patch('pagure.lib.notify.send_email')
  1338. def test_update_issue_edit_comment(self, p_send_email, p_ugt):
  1339. """ Test the issues edit comment endpoint """
  1340. p_send_email.return_value = True
  1341. p_ugt.return_value = True
  1342. tests.create_projects(self.session)
  1343. tests.create_projects_git(
  1344. os.path.join(tests.HERE), bare=True)
  1345. # Create issues to play with
  1346. repo = pagure.lib.get_project(self.session, 'test')
  1347. msg = pagure.lib.new_issue(
  1348. session=self.session,
  1349. repo=repo,
  1350. title='Test issue',
  1351. content='We should work on this',
  1352. user='pingou',
  1353. ticketfolder=None
  1354. )
  1355. self.session.commit()
  1356. self.assertEqual(msg.title, 'Test issue')
  1357. user = tests.FakeUser()
  1358. user.username = 'pingou'
  1359. with tests.user_set(pagure.APP, user):
  1360. output = self.app.get('/test/issue/1')
  1361. self.assertEqual(output.status_code, 200)
  1362. self.assertIn(
  1363. '<title>Issue #1: Test issue - test - Pagure</title>',
  1364. output.data)
  1365. self.assertIn(
  1366. '<a class="btn btn-primary btn-sm" '
  1367. 'href="/test/issue/1/edit" title="Edit this issue">',
  1368. output.data)
  1369. csrf_token = output.data.split(
  1370. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1371. # Add new comment
  1372. data = {
  1373. 'csrf_token': csrf_token,
  1374. 'comment': 'Woohoo a second comment !',
  1375. }
  1376. output = self.app.post(
  1377. '/test/issue/1/update', data=data, follow_redirects=True)
  1378. self.assertEqual(output.status_code, 200)
  1379. self.assertIn(
  1380. '<title>Issue #1: Test issue - test - Pagure</title>',
  1381. output.data)
  1382. self.assertIn(
  1383. '<a class="btn btn-primary btn-sm" '
  1384. 'href="/test/issue/1/edit" title="Edit this issue">',
  1385. output.data)
  1386. self.assertIn(
  1387. '</button>\n Comment added',
  1388. output.data)
  1389. self.assertTrue(
  1390. '<p>Woohoo a second comment !</p>' in output.data)
  1391. self.assertEqual(output.data.count('comment_body">'), 2)
  1392. repo = pagure.lib.get_project(self.session, 'test')
  1393. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1394. self.assertEqual(len(issue.comments), 1)
  1395. self.assertEqual(
  1396. issue.comments[0].comment,
  1397. 'Woohoo a second comment !')
  1398. data = {
  1399. 'csrf_token': csrf_token,
  1400. 'edit_comment': 1,
  1401. 'update_comment': 'Updated comment',
  1402. }
  1403. user = tests.FakeUser()
  1404. with tests.user_set(pagure.APP, user):
  1405. # Wrong issue id
  1406. output = self.app.post(
  1407. '/test/issue/3/update', data=data, follow_redirects=True)
  1408. self.assertEqual(output.status_code, 404)
  1409. # Wrong user
  1410. output = self.app.post(
  1411. '/test/issue/1/update', data=data, follow_redirects=True)
  1412. self.assertEqual(output.status_code, 403)
  1413. user = tests.FakeUser()
  1414. user.username = 'pingou'
  1415. with tests.user_set(pagure.APP, user):
  1416. # Edit comment
  1417. output = self.app.post(
  1418. '/test/issue/1/update', data=data, follow_redirects=True)
  1419. self.assertEqual(output.status_code, 200)
  1420. self.assertIn(
  1421. '<title>Issue #1: Test issue - test - Pagure</title>',
  1422. output.data)
  1423. self.assertIn(
  1424. '<a class="btn btn-primary btn-sm" '
  1425. 'href="/test/issue/1/edit" title="Edit this issue">',
  1426. output.data)
  1427. self.assertIn(
  1428. '</button>\n Comment updated',
  1429. output.data)
  1430. repo = pagure.lib.get_project(self.session, 'test')
  1431. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1432. self.assertEqual(len(issue.comments), 1)
  1433. self.assertEqual(issue.comments[0].comment, 'Updated comment')
  1434. with tests.user_set(pagure.APP, user):
  1435. output = self.app.get('/test/issue/1/comment/1/edit')
  1436. self.assertIn(
  1437. '<title>test - Pagure</title>', output.data)
  1438. self.assertTrue('<div id="edit">' in output.data)
  1439. self.assertTrue('<section class="edit_comment">' in output.data)
  1440. self.assertTrue(
  1441. '<textarea class="form-control" id="update_comment"'
  1442. in output.data)
  1443. csrf_token = output.data.split(
  1444. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1445. data['csrf_token'] = csrf_token
  1446. data['update_comment'] = 'Second update'
  1447. # Edit the comment with the other endpoint
  1448. output = self.app.post(
  1449. '/test/issue/1/comment/1/edit',
  1450. data=data,
  1451. follow_redirects=True)
  1452. self.assertEqual(output.status_code, 200)
  1453. self.assertIn(
  1454. '<title>Issue #1: Test issue - test - Pagure</title>',
  1455. output.data)
  1456. self.assertIn(
  1457. '<a class="btn btn-primary btn-sm" '
  1458. 'href="/test/issue/1/edit" title="Edit this issue">',
  1459. output.data)
  1460. self.assertIn(
  1461. '</button>\n Comment updated',
  1462. output.data)
  1463. repo = pagure.lib.get_project(self.session, 'test')
  1464. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1465. self.assertEqual(len(issue.comments), 1)
  1466. self.assertEqual(issue.comments[0].comment, 'Second update')
  1467. @patch('pagure.lib.git.update_git')
  1468. @patch('pagure.lib.notify.send_email')
  1469. def test_git_urls(self, p_send_email, p_ugt):
  1470. """ Check that the url to the git repo for issues is present/absent when
  1471. it should.
  1472. """
  1473. p_send_email.return_value = True
  1474. p_ugt.return_value = True
  1475. self.test_view_issues()
  1476. user = tests.FakeUser()
  1477. user.username = 'pingou'
  1478. with tests.user_set(pagure.APP, user):
  1479. # Check that the git issue URL is present
  1480. output = self.app.get('/test')
  1481. self.assertNotIn(
  1482. '<h5><strong>Issues GIT URLs</strong></h5>', output.data)
  1483. # Project w/o issue tracker
  1484. repo = pagure.lib.get_project(self.session, 'test')
  1485. repo.settings = {'issue_tracker': True}
  1486. self.session.add(repo)
  1487. self.session.commit()
  1488. # Check that the git issue URL is gone
  1489. output = self.app.get('/test')
  1490. self.assertIn(
  1491. '<h5><strong>Issues GIT URLs</strong></h5>', output.data)
  1492. if __name__ == '__main__':
  1493. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskIssuestests)
  1494. unittest.TextTestRunner(verbosity=2).run(SUITE)