test_pagure_flask_ui_issues.py 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals
  8. __requires__ = ['SQLAlchemy >= 0.8']
  9. import pkg_resources
  10. from unittest.case import SkipTest
  11. import json
  12. import unittest
  13. import shutil
  14. import sys
  15. import os
  16. try:
  17. import pyclamd
  18. except ImportError:
  19. pyclamd = None
  20. import six
  21. import tempfile
  22. import re
  23. from datetime import datetime, timedelta
  24. from six.moves.urllib.parse import urlparse, parse_qs
  25. import pygit2
  26. from bs4 import BeautifulSoup
  27. from mock import patch, MagicMock
  28. sys.path.insert(0, os.path.join(os.path.dirname(
  29. os.path.abspath(__file__)), '..'))
  30. import pagure.lib.query
  31. import tests
  32. class PagureFlaskIssuestests(tests.Modeltests):
  33. """ Tests for flask issues controller of pagure """
  34. @patch('pagure.lib.git.update_git')
  35. @patch('pagure.lib.notify.send_email')
  36. def test_new_issue(self, p_send_email, p_ugt):
  37. """ Test the new_issue endpoint. """
  38. p_send_email.return_value = True
  39. p_ugt.return_value = True
  40. # No Git repo
  41. output = self.app.get('/foo/new_issue')
  42. self.assertEqual(output.status_code, 404)
  43. user = tests.FakeUser()
  44. with tests.user_set(self.app.application, user):
  45. output = self.app.get('/foo/new_issue')
  46. self.assertEqual(output.status_code, 404)
  47. tests.create_projects(self.session)
  48. tests.create_projects_git(
  49. os.path.join(self.path, 'repos'), bare=True)
  50. output = self.app.get('/test/new_issue')
  51. self.assertEqual(output.status_code, 200)
  52. self.assertIn(
  53. 'New Issue',
  54. output.get_data(as_text=True))
  55. csrf_token = self.get_csrf(output=output)
  56. data = {
  57. }
  58. # Insufficient input
  59. output = self.app.post('/test/new_issue', data=data)
  60. self.assertEqual(output.status_code, 200)
  61. output_text = output.get_data(as_text=True)
  62. self.assertIn(
  63. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  64. output_text)
  65. self.assertEqual(output_text.count(
  66. 'This field is required.'), 2)
  67. data['title'] = 'Test issue'
  68. output = self.app.post('/test/new_issue', data=data)
  69. self.assertEqual(output.status_code, 200)
  70. output_text = output.get_data(as_text=True)
  71. self.assertIn(
  72. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  73. output_text)
  74. self.assertEqual(output_text.count(
  75. 'This field is required.'), 1)
  76. data['issue_content'] = 'We really should improve on this issue'
  77. data['status'] = 'Open'
  78. output = self.app.post('/test/new_issue', data=data)
  79. self.assertEqual(output.status_code, 200)
  80. output_text = output.get_data(as_text=True)
  81. self.assertIn(
  82. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  83. output_text)
  84. self.assertEqual(output_text.count(
  85. 'This field is required.'),
  86. 0)
  87. # Invalid user
  88. data['csrf_token'] = csrf_token
  89. output = self.app.post('/test/new_issue', data=data)
  90. self.assertEqual(output.status_code, 404)
  91. self.assertIn(
  92. '<p>No such user found in the database: username</p>',
  93. output.get_data(as_text=True))
  94. # User not logged in
  95. output = self.app.get('/test/new_issue')
  96. self.assertEqual(output.status_code, 302)
  97. user.username = 'pingou'
  98. with tests.user_set(self.app.application, user):
  99. output = self.app.post(
  100. '/test/new_issue', data=data, follow_redirects=True)
  101. self.assertEqual(output.status_code, 200)
  102. output_text = output.get_data(as_text=True)
  103. self.assertIn(
  104. '<title>Issue #1: Test issue - test - Pagure</title>',
  105. output_text)
  106. self.assertIn(
  107. '<a class="btn btn-outline-secondary btn-sm border-0"'
  108. ' href="/test/issue/1/edit" title="Edit this issue">',
  109. output_text)
  110. # Project w/o issue tracker
  111. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  112. repo.settings = {'issue_tracker': False}
  113. self.session.add(repo)
  114. self.session.commit()
  115. user.username = 'pingou'
  116. with tests.user_set(self.app.application, user):
  117. output = self.app.post(
  118. '/test/new_issue', data=data, follow_redirects=True)
  119. self.assertEqual(output.status_code, 404)
  120. @patch('pagure.lib.git.update_git')
  121. @patch('pagure.lib.notify.send_email')
  122. def test_new_issue_w_file(self, p_send_email, p_ugt):
  123. """ Test the new_issue endpoint with a file. """
  124. p_send_email.return_value = True
  125. p_ugt.return_value = True
  126. tests.create_projects(self.session)
  127. tests.create_projects_git(
  128. os.path.join(self.path, 'repos'), bare=True)
  129. tests.create_projects_git(
  130. os.path.join(self.path, 'tickets'), bare=True)
  131. user = tests.FakeUser()
  132. user.username = 'pingou'
  133. with tests.user_set(self.app.application, user):
  134. output = self.app.get('/test/new_issue')
  135. self.assertEqual(output.status_code, 200)
  136. self.assertIn(
  137. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  138. output.get_data(as_text=True))
  139. csrf_token = self.get_csrf()
  140. with open(os.path.join(tests.HERE, 'placebo.png'), 'rb') as stream:
  141. data = {
  142. 'title': 'Test issue',
  143. 'issue_content': 'We really should improve on this issue\n'
  144. '<!!image>',
  145. 'status': 'Open',
  146. 'filestream': stream,
  147. 'enctype': 'multipart/form-data',
  148. 'csrf_token': csrf_token,
  149. }
  150. output = self.app.post(
  151. '/test/new_issue', data=data, follow_redirects=True)
  152. self.assertEqual(output.status_code, 200)
  153. output_text = output.get_data(as_text=True)
  154. self.assertIn(
  155. '<title>Issue #1: Test issue - test - Pagure</title>',
  156. output_text)
  157. self.assertIn(
  158. '<a class="btn btn-outline-secondary btn-sm border-0"'
  159. ' href="/test/issue/1/edit" title="Edit this issue">',
  160. output_text)
  161. # Check the image was uploaded
  162. self.assertIn(
  163. 'href="/test/issue/raw/files/'
  164. '8a06845923010b27bfd8e7e75acff7badc40d1021b4'
  165. '994e01f5e11ca40bc3abe',
  166. output_text)
  167. @patch('pagure.lib.git.update_git')
  168. @patch('pagure.lib.notify.send_email')
  169. def test_new_issue_w_file_no_issue_tracker(self, p_send_email, p_ugt):
  170. """ Test the new_issue endpoint with a file. """
  171. p_send_email.return_value = True
  172. p_ugt.return_value = True
  173. tests.create_projects(self.session)
  174. tests.create_projects_git(
  175. os.path.join(self.path, 'repos'), bare=True)
  176. tests.create_projects_git(
  177. os.path.join(self.path, 'tickets'), bare=True)
  178. # Project w/o issue tracker
  179. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  180. repo.settings = {'issue_tracker': False}
  181. self.session.add(repo)
  182. self.session.commit()
  183. user = tests.FakeUser()
  184. user.username = 'pingou'
  185. with tests.user_set(self.app.application, user):
  186. with open(os.path.join(tests.HERE, 'placebo.png'), 'rb') as stream:
  187. data = {
  188. 'title': 'Test issue',
  189. 'issue_content': 'We really should improve on this issue',
  190. 'status': 'Open',
  191. 'filestream': stream,
  192. 'enctype': 'multipart/form-data',
  193. 'csrf_token': self.get_csrf(),
  194. }
  195. output = self.app.post(
  196. '/test/new_issue', data=data, follow_redirects=True)
  197. self.assertEqual(output.status_code, 404)
  198. @patch('pagure.lib.git.update_git')
  199. @patch('pagure.lib.notify.send_email')
  200. def test_new_issue_w_file_namespace(self, p_send_email, p_ugt):
  201. """ Test the new_issue endpoint with a file. """
  202. p_send_email.return_value = True
  203. p_ugt.return_value = True
  204. tests.create_projects(self.session)
  205. tests.create_projects_git(
  206. os.path.join(self.path, 'repos'), bare=True)
  207. tests.create_projects_git(
  208. os.path.join(self.path, 'tickets'), bare=True)
  209. # Project with a namespace
  210. user = tests.FakeUser()
  211. user.username = 'pingou'
  212. with tests.user_set(self.app.application, user):
  213. output = self.app.get('/somenamespace/test3/new_issue')
  214. self.assertEqual(output.status_code, 200)
  215. self.assertTrue(
  216. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n'
  217. in output.get_data(as_text=True))
  218. csrf_token = self.get_csrf()
  219. with open(os.path.join(tests.HERE, 'placebo.png'), 'rb') as stream:
  220. data = {
  221. 'title': 'Test issue3',
  222. 'issue_content': 'We really should improve on this issue\n'
  223. '<!!image>',
  224. 'status': 'Open',
  225. 'filestream': stream,
  226. 'enctype': 'multipart/form-data',
  227. 'csrf_token': csrf_token,
  228. }
  229. output = self.app.post(
  230. '/somenamespace/test3/new_issue', data=data, follow_redirects=True)
  231. self.assertEqual(output.status_code, 200)
  232. output_text = output.get_data(as_text=True)
  233. self.assertIn(
  234. '<title>Issue #1: Test issue3 - test3 - Pagure</title>',
  235. output_text)
  236. self.assertIn(
  237. '<a class="btn btn-outline-secondary btn-sm border-0" '
  238. 'href="/somenamespace/test3/issue/1/edit" title="Edit this issue">\n',
  239. output_text)
  240. # Check the image was uploaded
  241. self.assertIn(
  242. 'href="/somenamespace/test3/issue/raw/files/'
  243. '8a06845923010b27bfd8e7e75acff7badc40d1021b4'
  244. '994e01f5e11ca40bc3abe',
  245. output_text)
  246. @patch('pagure.lib.git.update_git')
  247. @patch('pagure.lib.notify.send_email')
  248. def test_new_issue_w_files(self, p_send_email, p_ugt):
  249. """ Test the new_issue endpoint with two files. """
  250. p_send_email.return_value = True
  251. p_ugt.return_value = True
  252. tests.create_projects(self.session)
  253. tests.create_projects_git(
  254. os.path.join(self.path, 'repos'), bare=True)
  255. tests.create_projects_git(
  256. os.path.join(self.path, 'tickets'), bare=True)
  257. user = tests.FakeUser()
  258. user.username = 'pingou'
  259. with tests.user_set(self.app.application, user):
  260. output = self.app.get('/test/new_issue')
  261. self.assertEqual(output.status_code, 200)
  262. self.assertTrue(
  263. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n'
  264. in output.get_data(as_text=True))
  265. csrf_token = self.get_csrf()
  266. with open(
  267. os.path.join(tests.HERE, 'placebo.png'), 'rb'
  268. ) as stream:
  269. with open(
  270. os.path.join(tests.HERE, 'pagure.png'), 'rb'
  271. ) as stream2:
  272. data = {
  273. 'title': 'Test issue',
  274. 'issue_content': 'We really should improve on this issue\n'
  275. '<!!image>\n<!!image>',
  276. 'status': 'Open',
  277. 'filestream': [stream, stream2],
  278. 'enctype': 'multipart/form-data',
  279. 'csrf_token': csrf_token,
  280. }
  281. output = self.app.post(
  282. '/test/new_issue', data=data, follow_redirects=True)
  283. self.assertEqual(output.status_code, 200)
  284. output_text = output.get_data(as_text=True)
  285. self.assertIn(
  286. '<title>Issue #1: Test issue - test - Pagure</title>',
  287. output_text)
  288. self.assertIn(
  289. '<a class="btn btn-outline-secondary btn-sm border-0"'
  290. ' href="/test/issue/1/edit" title="Edit this issue">',
  291. output_text)
  292. # Check the image was uploaded
  293. self.assertIn(
  294. 'href="/test/issue/raw/files/'
  295. '8a06845923010b27bfd8e7e75acff7badc40d1021b4'
  296. '994e01f5e11ca40bc3abe',
  297. output_text)
  298. self.assertIn(
  299. 'href="/test/issue/raw/files/'
  300. '6498a2de405546200b6144da56fc25d0a3976ae688d'
  301. 'bfccaca609c8b4480523e',
  302. output_text)
  303. # Check that the files are accessible
  304. _, full_name = output_text.split("/test/issue/raw/files/", 1)
  305. full_name1, full_name2 = full_name.split(
  306. "/test/issue/raw/files/", 1)
  307. full_name1 = full_name1.split(".png", 1)[0]
  308. full_name2 = full_name2.split(".png", 1)[0]
  309. for full_name in [full_name1, full_name2]:
  310. req = self.app.get("/test/issue/raw/files/%s.png" % full_name)
  311. self.assertEqual(req.status_code, 200)
  312. @patch('pagure.lib.git.update_git')
  313. @patch('pagure.lib.notify.send_email')
  314. def test_new_issue_w_files_namespace(self, p_send_email, p_ugt):
  315. """ Test the new_issue endpoint with two files. """
  316. p_send_email.return_value = True
  317. p_ugt.return_value = True
  318. tests.create_projects(self.session)
  319. tests.create_projects_git(
  320. os.path.join(self.path, 'repos'), bare=True)
  321. tests.create_projects_git(
  322. os.path.join(self.path, 'tickets'), bare=True)
  323. # Project with a namespace
  324. user = tests.FakeUser()
  325. user.username = 'pingou'
  326. with tests.user_set(self.app.application, user):
  327. output = self.app.get('/somenamespace/test3/new_issue')
  328. self.assertEqual(output.status_code, 200)
  329. self.assertTrue(
  330. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n'
  331. in output.get_data(as_text=True))
  332. csrf_token = self.get_csrf()
  333. with open(
  334. os.path.join(tests.HERE, 'placebo.png'), 'rb'
  335. ) as stream:
  336. with open(
  337. os.path.join(tests.HERE, 'pagure.png'), 'rb'
  338. ) as stream2:
  339. data = {
  340. 'title': 'Test issue3',
  341. 'issue_content': 'We really should improve on this issue\n'
  342. '<!!image>\n<!!image>',
  343. 'status': 'Open',
  344. 'filestream': [stream, stream2],
  345. 'enctype': 'multipart/form-data',
  346. 'csrf_token': csrf_token,
  347. }
  348. output = self.app.post(
  349. '/somenamespace/test3/new_issue',
  350. data=data, follow_redirects=True)
  351. self.assertEqual(output.status_code, 200)
  352. output_text = output.get_data(as_text=True)
  353. self.assertIn(
  354. '<title>Issue #1: Test issue3 - test3 - Pagure</title>',
  355. output_text)
  356. self.assertIn(
  357. '<a class="btn btn-outline-secondary btn-sm border-0" '
  358. 'href="/somenamespace/test3/issue/1/edit" title="Edit this issue">\n',
  359. output_text)
  360. # Check the image was uploaded
  361. self.assertIn(
  362. 'href="/somenamespace/test3/issue/raw/files/'
  363. '8a06845923010b27bfd8e7e75acff7badc40d1021b4'
  364. '994e01f5e11ca40bc3abe',
  365. output_text)
  366. self.assertIn(
  367. 'href="/somenamespace/test3/issue/raw/files/'
  368. '6498a2de405546200b6144da56fc25d0a3976ae688d'
  369. 'bfccaca609c8b4480523e',
  370. output_text)
  371. def test_new_issue_metadata_user(self):
  372. """ Test the new_issue endpoint when the user has access to the
  373. project. """
  374. tests.create_projects(self.session)
  375. tests.create_projects_git(
  376. os.path.join(self.path, 'repos'), bare=True)
  377. tests.create_projects_git(
  378. os.path.join(self.path, 'tickets'), bare=True)
  379. user = tests.FakeUser()
  380. user.username = 'pingou'
  381. with tests.user_set(self.app.application, user):
  382. output = self.app.get('/test/new_issue')
  383. self.assertEqual(output.status_code, 200)
  384. output_text = output.get_data(as_text=True)
  385. self.assertIn(
  386. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  387. output_text)
  388. self.assertIn(
  389. '<strong>Tags</strong>',
  390. output_text)
  391. self.assertIn(
  392. '<strong>Assignee</strong>',
  393. output_text)
  394. def test_new_issue_metadata_not_user(self):
  395. """ Test the new_issue endpoint when the user does not have access
  396. to the project. """
  397. tests.create_projects(self.session)
  398. tests.create_projects_git(
  399. os.path.join(self.path, 'repos'), bare=True)
  400. tests.create_projects_git(
  401. os.path.join(self.path, 'tickets'), bare=True)
  402. user = tests.FakeUser()
  403. user.username = 'foo'
  404. with tests.user_set(self.app.application, user):
  405. output = self.app.get('/test/new_issue')
  406. self.assertEqual(output.status_code, 200)
  407. output_text = output.get_data(as_text=True)
  408. self.assertIn(
  409. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  410. output_text)
  411. self.assertNotIn(
  412. '<strong>Tags</strong>',
  413. output_text)
  414. self.assertNotIn(
  415. '<strong>Assignee</strong>',
  416. output_text)
  417. @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
  418. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  419. def test_new_issue_with_metadata(self):
  420. """ Test the new_issue endpoint when the user has access to the
  421. project. """
  422. tests.create_projects(self.session)
  423. tests.create_projects_git(
  424. os.path.join(self.path, 'repos'), bare=True)
  425. tests.create_projects_git(
  426. os.path.join(self.path, 'tickets'), bare=True)
  427. # Set some milestone
  428. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  429. repo.milestones = {'v1.0': '', 'v2.0': 'Tomorrow!'}
  430. self.session.add(repo)
  431. self.session.commit()
  432. user = tests.FakeUser()
  433. user.username = 'pingou'
  434. with tests.user_set(self.app.application, user):
  435. output = self.app.get('/test/new_issue')
  436. self.assertEqual(output.status_code, 200)
  437. output_text = output.get_data(as_text=True)
  438. self.assertIn(
  439. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  440. output_text)
  441. self.assertIn(
  442. '<strong>Tags</strong>',
  443. output_text)
  444. self.assertIn(
  445. '<strong>Assignee</strong>',
  446. output_text)
  447. csrf_token = self.get_csrf(output=output)
  448. data = {
  449. 'title': 'Test issue3',
  450. 'issue_content': 'We really should improve on this issue\n',
  451. 'status': 'Open',
  452. 'assignee': 'foo',
  453. 'milestone': 'v2.0',
  454. 'tag': 'tag2',
  455. 'csrf_token': csrf_token,
  456. }
  457. output = self.app.post(
  458. '/test/new_issue', data=data, follow_redirects=True)
  459. self.assertEqual(output.status_code, 200)
  460. output_text = output.get_data(as_text=True)
  461. self.assertIn(
  462. '<title>Issue #1: Test issue3 - test - Pagure</title>',
  463. output_text)
  464. self.assertIn(
  465. '<a class="btn btn-outline-secondary btn-sm border-0" '
  466. 'href="/test/issue/1/edit" title="Edit this issue">\n',
  467. output_text)
  468. # Check the metadata
  469. self.assertIn(
  470. 'title="comma separated list of tags"\n '
  471. 'value="tag2" />', output_text)
  472. self.assertIn(
  473. 'placeholder="username"\n value="foo" />\n',
  474. output_text)
  475. self.assertIn(
  476. 'href="/test/roadmap/v2.0/"',
  477. output_text)
  478. @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
  479. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  480. def test_new_issue_with_metadata_not_user(self):
  481. """ Test the new_issue endpoint when the user does not have access
  482. to the project but still tries to.
  483. """
  484. tests.create_projects(self.session)
  485. tests.create_projects_git(
  486. os.path.join(self.path, 'repos'), bare=True)
  487. tests.create_projects_git(
  488. os.path.join(self.path, 'tickets'), bare=True)
  489. # Set some milestone
  490. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  491. repo.milestones = {'v1.0': '', 'v2.0': 'Tomorrow!'}
  492. self.session.add(repo)
  493. self.session.commit()
  494. user = tests.FakeUser()
  495. user.username = 'foo'
  496. with tests.user_set(self.app.application, user):
  497. output = self.app.get('/test/new_issue')
  498. self.assertEqual(output.status_code, 200)
  499. output_text = output.get_data(as_text=True)
  500. self.assertIn(
  501. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  502. output_text)
  503. self.assertNotIn(
  504. '<strong>Tags</strong>',
  505. output_text)
  506. self.assertNotIn(
  507. '<strong>Assignee</strong>',
  508. output_text)
  509. csrf_token = self.get_csrf(output=output)
  510. data = {
  511. 'title': 'Test issue3',
  512. 'issue_content': 'We really should improve on this issue\n',
  513. 'status': 'Open',
  514. 'assignee': 'foo',
  515. 'milestone': 'v2.0',
  516. 'tag': 'tag2',
  517. 'csrf_token': csrf_token,
  518. }
  519. output = self.app.post(
  520. '/test/new_issue', data=data, follow_redirects=True)
  521. self.assertEqual(output.status_code, 200)
  522. output_text = output.get_data(as_text=True)
  523. self.assertIn(
  524. '<title>Issue #1: Test issue3 - test - Pagure</title>',
  525. output_text)
  526. self.assertIn(
  527. '<a class="btn btn-outline-secondary btn-sm border-0" '
  528. 'href="/test/issue/1/edit" title="Edit this issue">\n',
  529. output_text)
  530. # Check the metadata
  531. self.assertNotIn(
  532. 'title="comma separated list of tags"\n '
  533. 'value="tag2" />', output_text)
  534. self.assertNotIn(
  535. 'placeholder="username"\n value="foo" />\n',
  536. output_text)
  537. self.assertNotIn(
  538. '<div id="milestone_plain">\n <span>'
  539. '\n <a href="/test/roadmap/v2.0/">'
  540. '\n v2.0\n', output_text)
  541. @patch('pagure.lib.git.update_git')
  542. @patch('pagure.lib.notify.send_email')
  543. def test_view_issues(self, p_send_email, p_ugt):
  544. """ Test the view_issues endpoint. """
  545. p_send_email.return_value = True
  546. p_ugt.return_value = True
  547. output = self.app.get('/foo/issues')
  548. self.assertEqual(output.status_code, 404)
  549. tests.create_projects(self.session)
  550. tests.create_projects_git(
  551. os.path.join(self.path, 'repos'), bare=True)
  552. output = self.app.get('/test/issues')
  553. self.assertEqual(output.status_code, 200)
  554. output_text = output.get_data(as_text=True)
  555. self.assertIn(
  556. '<i class="fa fa-calendar-o fa-rotate-270 text-muted"></i></h3>',
  557. output_text)
  558. self.assertIn(
  559. '<a href="/test"><strong>test</strong></a>', output_text)
  560. self.assertIn(
  561. '<span class="fa fa-fw fa-exclamation-circle"></span> 0 Open Issues\n', output_text)
  562. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  563. # Create some custom fields to play with
  564. msg = pagure.lib.query.set_custom_key_fields(
  565. session=self.session,
  566. project=repo,
  567. fields=['test1'],
  568. types=['text'],
  569. data=[None],
  570. notify=[None]
  571. )
  572. self.session.commit()
  573. self.assertEqual(msg, 'List of custom fields updated')
  574. cfield = pagure.lib.query.get_custom_key(
  575. session=self.session,
  576. project=repo,
  577. keyname='test1')
  578. # Create issues to play with
  579. msg = pagure.lib.query.new_issue(
  580. session=self.session,
  581. repo=repo,
  582. title='tést íssüé',
  583. content='We should work on this',
  584. user='pingou',
  585. )
  586. self.session.commit()
  587. self.assertEqual(msg.title, 'tést íssüé')
  588. msg = pagure.lib.query.set_custom_key_value(
  589. session=self.session,
  590. issue=msg,
  591. key=cfield,
  592. value='firstissue')
  593. self.session.commit()
  594. self.assertEqual(msg, 'Custom field test1 adjusted to firstissue')
  595. msg = pagure.lib.query.new_issue(
  596. session=self.session,
  597. repo=repo,
  598. title='Tést íssüé with milestone',
  599. content='Testing search',
  600. user='pingou',
  601. milestone='1.1',
  602. )
  603. self.session.commit()
  604. self.assertEqual(msg.title, 'Tést íssüé with milestone')
  605. # Add a comment to that ticket
  606. pagure.lib.query.add_issue_comment(
  607. session=self.session,
  608. issue=msg,
  609. comment='How about nóã!',
  610. user='foo',
  611. )
  612. self.session.commit()
  613. msg = pagure.lib.query.new_issue(
  614. session=self.session,
  615. repo=repo,
  616. title='Test invalid issue',
  617. content='This really is not related',
  618. user='pingou',
  619. status='Closed',
  620. close_status='Invalid',
  621. )
  622. self.session.commit()
  623. self.assertEqual(msg.title, 'Test invalid issue')
  624. msg = pagure.lib.query.set_custom_key_value(
  625. session=self.session,
  626. issue=msg,
  627. key=cfield,
  628. value='second issue')
  629. self.session.commit()
  630. self.assertEqual(msg, 'Custom field test1 adjusted to second issue')
  631. # Whole list
  632. output = self.app.get('/test/issues')
  633. self.assertEqual(output.status_code, 200)
  634. output_text = output.get_data(as_text=True)
  635. self.assertIn('<title>Issues - test - Pagure</title>',
  636. output_text)
  637. self.assertIn(
  638. '<span class="fa fa-fw fa-exclamation-circle"></span> 2 Open Issues\n', output_text)
  639. self.assertIn(
  640. 'title="2 Open Issues | 1 Closed Issues', output_text)
  641. self.assertIn(
  642. 'bg-success" role="progressbar"\n'
  643. ' style="width:67%"\n', output_text)
  644. # Status = closed (all but open)
  645. output = self.app.get('/test/issues?status=cloSED')
  646. self.assertEqual(output.status_code, 200)
  647. output_text = output.get_data(as_text=True)
  648. self.assertIn('<title>Issues - test - Pagure</title>',
  649. output_text)
  650. self.assertIn(
  651. '<span class="fa fa-fw fa-exclamation-circle"></span> 1 Closed Issues\n', output_text)
  652. self.assertIn(
  653. 'title="2 Open Issues | 1 Closed Issues"', output_text)
  654. self.assertIn(
  655. 'bg-danger" role="progressbar"\n'
  656. ' style="width:33%"\n', output_text)
  657. # Status = fixed
  658. output = self.app.get('/test/issues?status=fixed')
  659. self.assertEqual(output.status_code, 200)
  660. output_text = output.get_data(as_text=True)
  661. self.assertIn('<title>Issues - test - Pagure</title>',
  662. output_text)
  663. self.assertIn(
  664. '<span class="fa fa-fw fa-exclamation-circle"></span> 0 Closed:Fixed Issues\n', output_text)
  665. # Status = Invalid
  666. output = self.app.get('/test/issues?status=Invalid')
  667. self.assertEqual(output.status_code, 200)
  668. output_text = output.get_data(as_text=True)
  669. self.assertIn('<title>Issues - test - Pagure</title>', output_text)
  670. self.assertTrue(
  671. '<span class="fa fa-fw fa-exclamation-circle"></span> 1 Closed:Invalid Issues\n' in output_text)
  672. # All tickets
  673. output = self.app.get('/test/issues?status=all')
  674. self.assertEqual(output.status_code, 200)
  675. output_text = output.get_data(as_text=True)
  676. self.assertIn('<title>Issues - test - Pagure</title>', output_text)
  677. self.assertTrue(
  678. '<span class="fa fa-fw fa-exclamation-circle"></span> 3 Open &amp; Closed Issues\n' in output_text)
  679. # Unicode search pattern
  680. output = self.app.get(
  681. '/test/issues?status=all&search_pattern=گروه')
  682. self.assertEqual(output.status_code, 200)
  683. output_text = output.get_data(as_text=True)
  684. self.assertIn('<title>Issues - test - Pagure</title>', output_text)
  685. self.assertIn('0 Open &amp; Closed Issues', output_text)
  686. # Content search - description
  687. output = self.app.get(
  688. '/test/issues?status=all&search_pattern=content:work')
  689. self.assertEqual(output.status_code, 200)
  690. output_text = output.get_data(as_text=True)
  691. self.assertIn(
  692. '<title>Issues - test - Pagure</title>',
  693. output_text)
  694. self.assertIn('1 Open &amp; Closed Issues', output_text)
  695. # Content search - comment
  696. output = self.app.get(
  697. '/test/issues?status=all&search_pattern=content:nóã')
  698. self.assertEqual(output.status_code, 200)
  699. output_text = output.get_data(as_text=True)
  700. self.assertIn(
  701. '<title>Issues - test - Pagure</title>',
  702. output_text)
  703. self.assertIn('1 Open &amp; Closed Issues', output_text)
  704. # Custom key searching
  705. output = self.app.get(
  706. '/test/issues?status=all&search_pattern=test1:firstissue')
  707. self.assertEqual(output.status_code, 200)
  708. output_text = output.get_data(as_text=True)
  709. self.assertIn(
  710. '<title>Issues - test - Pagure</title>',
  711. output_text)
  712. self.assertIn('1 Open &amp; Closed Issues', output_text)
  713. # Custom key searching with space
  714. output = self.app.get(
  715. '/test/issues?status=all&search_pattern=test1:"second issue"')
  716. self.assertEqual(output.status_code, 200)
  717. output_text = output.get_data(as_text=True)
  718. self.assertIn(
  719. '<title>Issues - test - Pagure</title>',
  720. output_text)
  721. self.assertIn('1 Open &amp; Closed Issues', output_text)
  722. # All tickets - different pagination
  723. before = pagure.config.config['ITEM_PER_PAGE']
  724. pagure.config.config['ITEM_PER_PAGE'] = 1
  725. output = self.app.get('/test/issues?status=all')
  726. self.assertEqual(output.status_code, 200)
  727. output_text = output.get_data(as_text=True)
  728. self.assertIn(
  729. '<title>Issues - test - Pagure</title>',
  730. output_text)
  731. self.assertIn(
  732. '<span class="fa fa-fw fa-exclamation-circle"></span> 3 Open &amp; Closed Issues\n',
  733. output_text)
  734. self.assertIn(
  735. 'page 1 of 3', output_text)
  736. # All tickets - filtered for 1 - checking the pagination
  737. output = self.app.get(
  738. '/test/issues?status=all&search_pattern=invalid')
  739. self.assertEqual(output.status_code, 200)
  740. output_text = output.get_data(as_text=True)
  741. self.assertIn('<title>Issues - test - Pagure</title>', output_text)
  742. self.assertIn('<span class="fa fa-fw fa-exclamation-circle"></span> 1 Open &amp; Closed Issues\n', output_text)
  743. self.assertNotIn(
  744. 'page 1', output_text)
  745. pagure.config.config['ITEM_PER_PAGE'] = before
  746. # Search for issues with no milestone MARK
  747. output = self.app.get(
  748. '/test/issues?milestone=none')
  749. self.assertEqual(output.status_code, 200)
  750. output_text = output.get_data(as_text=True)
  751. self.assertIn(
  752. '<title>Issues - test - Pagure</title>',
  753. output_text)
  754. self.assertIn('<span class="fa fa-fw fa-exclamation-circle"></span> 1 Open Issues\n', output_text)
  755. # Search for issues with no milestone and milestone 1.1
  756. output = self.app.get(
  757. '/test/issues?milestone=none&milestone=1.1')
  758. self.assertEqual(output.status_code, 200)
  759. output_text = output.get_data(as_text=True)
  760. self.assertIn(
  761. '<title>Issues - test - Pagure</title>',
  762. output_text)
  763. self.assertIn('<span class="fa fa-fw fa-exclamation-circle"></span> 2 Open Issues\n', output_text)
  764. # Add another issue to test sorting
  765. msg = pagure.lib.query.new_issue(
  766. session=self.session,
  767. repo=repo,
  768. title='Big problÈm!',
  769. content='I need help ASAP',
  770. user='foo',
  771. )
  772. self.session.commit()
  773. self.assertEqual(msg.title, 'Big problÈm!')
  774. # Sort by last_updated
  775. output = self.app.get('/test/issues?order_key=last_updated')
  776. self.assertEqual(output.status_code, 200)
  777. tr_elements = re.findall(r'<div class="issuerow list-group-item list-group-item-action ">(.*?)</div><!-- end issuerow -->',
  778. output.get_data(as_text=True), re.M | re.S)
  779. # Make sure that issue four is first since it was modified last
  780. self.assertIn('href="/test/issue/4"', tr_elements[0])
  781. # Make sure that issue two is second since it was modified second
  782. self.assertIn('href="/test/issue/2"', tr_elements[1])
  783. # Make sure that issue one is last since it was modified first
  784. self.assertIn('href="/test/issue/1"', tr_elements[2])
  785. # Modify the date of the first issue and try again
  786. issue_one = pagure.lib.query.search_issues(self.session, repo, 1)
  787. issue_one.last_updated = datetime.utcnow() + timedelta(seconds=2)
  788. self.session.add(issue_one)
  789. self.session.commit()
  790. output = self.app.get('/test/issues?order_key=last_updated')
  791. self.assertEqual(output.status_code, 200)
  792. tr_elements = re.findall(r'<div class="issuerow list-group-item list-group-item-action ">(.*?)</div><!-- end issuerow -->',
  793. output.get_data(as_text=True), re.M | re.S)
  794. # Make sure that issue one is first since it was modified last
  795. self.assertIn('href="/test/issue/1"', tr_elements[0])
  796. # Make sure that issue four is second since it was modified before
  797. # last
  798. self.assertIn('href="/test/issue/4"', tr_elements[1])
  799. # Make sure that issue two is last since it was modified before issue
  800. # one and four
  801. self.assertIn('href="/test/issue/2"', tr_elements[2])
  802. # Now query so that the results are ascending
  803. output = self.app.get('/test/issues?order_key=last_updated&order=asc')
  804. tr_elements = re.findall(r'<div class="issuerow list-group-item list-group-item-action ">(.*?)</div><!-- end issuerow -->',
  805. output.get_data(as_text=True), re.M | re.S)
  806. self.assertIn('href="/test/issue/2"', tr_elements[0])
  807. self.assertIn('href="/test/issue/4"', tr_elements[1])
  808. self.assertIn('href="/test/issue/1"', tr_elements[2])
  809. # Sort by title descending
  810. output = self.app.get('/test/issues?order_key=title')
  811. self.assertEqual(output.status_code, 200)
  812. tr_elements = re.findall(r'<div class="issuerow list-group-item list-group-item-action ">(.*?)</div><!-- end issuerow -->',
  813. output.get_data(as_text=True), re.M | re.S)
  814. self.assertIn('href="/test/issue/2"', tr_elements[0])
  815. self.assertIn('href="/test/issue/1"', tr_elements[1])
  816. self.assertIn('href="/test/issue/4"', tr_elements[2])
  817. # Sort by title ascending
  818. output = self.app.get('/test/issues?order_key=title&order=asc')
  819. self.assertEqual(output.status_code, 200)
  820. tr_elements = re.findall(r'<div class="issuerow list-group-item list-group-item-action ">(.*?)</div><!-- end issuerow -->',
  821. output.get_data(as_text=True), re.M | re.S)
  822. self.assertIn('href="/test/issue/4"', tr_elements[0])
  823. self.assertIn('href="/test/issue/1"', tr_elements[1])
  824. self.assertIn('href="/test/issue/2"', tr_elements[2])
  825. # Sort by user (reporter/author) descending
  826. output = self.app.get('/test/issues?order_key=user&order=desc')
  827. self.assertEqual(output.status_code, 200)
  828. tr_elements = re.findall(r'<div class="issuerow list-group-item list-group-item-action ">(.*?)</div><!-- end issuerow -->',
  829. output.get_data(as_text=True), re.M | re.S)
  830. # We check that they are unassigned, otherwise our previous check is
  831. # not specific enough as it can catch an assignee of "pingou"
  832. self.assertNotIn('fa-user-plus', tr_elements[0])
  833. self.assertIn('pingou', tr_elements[1])
  834. self.assertNotIn('fa-user-plus', tr_elements[1])
  835. self.assertIn('foo', tr_elements[2])
  836. self.assertNotIn('fa-user-plus', tr_elements[2])
  837. # Sort by user (reporter/author) ascending
  838. output = self.app.get('/test/issues?order_key=user&order=asc')
  839. self.assertEqual(output.status_code, 200)
  840. tr_elements = re.findall(r'<div class="issuerow list-group-item list-group-item-action ">(.*?)</div><!-- end issuerow -->',
  841. output.get_data(as_text=True), re.M | re.S)
  842. # Check for the name after the avatar
  843. self.assertIn('foo', tr_elements[0])
  844. # We check that they are unassigned, otherwise our previous check is
  845. # not specific enough as it can catch an assignee of "foo"
  846. self.assertNotIn('fa-user-plus', tr_elements[0])
  847. self.assertIn('pingou', tr_elements[1])
  848. self.assertNotIn('fa-user-plus', tr_elements[1])
  849. self.assertIn('pingou', tr_elements[2])
  850. self.assertNotIn('fa-user-plus', tr_elements[2])
  851. # Set some assignees
  852. issues = self.session.query(pagure.lib.model.Issue).filter_by(
  853. status='Open').order_by(pagure.lib.model.Issue.id).all()
  854. issues[0].assignee_id = 1
  855. issues[1].assignee_id = 2
  856. issues[2].assignee_id = 1
  857. self.session.commit()
  858. # This detects the assignee but keying on if a certain link is present
  859. def _check_assignee_link(html, expected_links):
  860. soup = BeautifulSoup(html, "html.parser")
  861. for index, expected_link in enumerate(expected_links):
  862. link = soup.find_all("tr")[index + 1].find(
  863. "a", title="Filter issues by assignee")
  864. self.assertIsNotNone(link, "Link %s was not found" % expected_link)
  865. self.assertURLEqual(link["href"], expected_link)
  866. # Sort by assignee descending
  867. output = self.app.get('/test/issues?order_key=assignee&order=desc')
  868. self.assertEqual(output.status_code, 200)
  869. #tr_elements = re.findall(r'<div class="issuerow list-group-item list-group-item-action ">(.*?)</div><!-- end issuerow -->',
  870. # output.get_data(as_text=True), re.M | re.S)
  871. #arrowed_th = ('Assignee</a>\n <span class="oi" data-glyph='
  872. # '"arrow-thick-bottom"></span>')
  873. # First table row is the header
  874. #self.assertIn(arrowed_th, tr_elements[0])
  875. #_check_assignee_link(output.get_data(as_text=True), [
  876. # '/test/issues?status=Open&assignee=pingou',
  877. # '/test/issues?status=Open&assignee=pingou',
  878. # '/test/issues?status=Open&assignee=foo',
  879. #])
  880. # Sort by assignee ascending
  881. #output = self.app.get('/test/issues?order_key=assignee&order=asc')
  882. #self.assertEqual(output.status_code, 200)
  883. #tr_elements = re.findall(r'<tr>(.*?)</tr>', output.get_data(as_text=True), re.M | re.S)
  884. #arrowed_th = ('Assignee</a>\n <span class="oi" data-glyph='
  885. # '"arrow-thick-top"></span>')
  886. # First table row is the header
  887. #self.assertIn(arrowed_th, tr_elements[0])
  888. #_check_assignee_link(output.get_data(as_text=True), [
  889. # '/test/issues?status=Open&assignee=foo',
  890. # '/test/issues?status=Open&assignee=pingou',
  891. # '/test/issues?status=Open&assignee=pingou',
  892. #])
  893. # New issue button is shown
  894. user = tests.FakeUser()
  895. with tests.user_set(self.app.application, user):
  896. output = self.app.get('/test')
  897. self.assertEqual(output.status_code, 200)
  898. self.assertIn(
  899. 'fa-exclamation-circle fa-fw"></i> New issue</a>',
  900. output.get_data(as_text=True))
  901. output = self.app.get('/test')
  902. self.assertEqual(output.status_code, 200)
  903. self.assertNotIn(
  904. 'fa-exclamation-circle fa-fw"></i> New issue</a>',
  905. output.get_data(as_text=True))
  906. # Project w/o issue tracker
  907. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  908. repo.settings = {'issue_tracker': False}
  909. self.session.add(repo)
  910. self.session.commit()
  911. output = self.app.get('/test/issues')
  912. self.assertEqual(output.status_code, 404)
  913. # New issue button is hidden
  914. user = tests.FakeUser()
  915. with tests.user_set(self.app.application, user):
  916. output = self.app.get('/test')
  917. self.assertEqual(output.status_code, 200)
  918. self.assertNotIn(
  919. 'fa-exclamation-circle fa-fw"></i> New issue</a>',
  920. output.get_data(as_text=True))
  921. @patch('pagure.lib.git.update_git')
  922. @patch('pagure.lib.notify.send_email')
  923. def test_search_issues_unicode(self, p_send_email, p_ugt):
  924. """ Test the view_issues endpoint filtering for an unicode char. """
  925. p_send_email.return_value = True
  926. p_ugt.return_value = True
  927. tests.create_projects(self.session)
  928. tests.create_projects_git(
  929. os.path.join(self.path, 'repos'), bare=True)
  930. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  931. # Create 2 issues to play with
  932. msg = pagure.lib.query.new_issue(
  933. session=self.session,
  934. repo=repo,
  935. title='Test issue ☃',
  936. content='We should work on this ❤',
  937. user='pingou',
  938. )
  939. self.session.commit()
  940. self.assertEqual(msg.title, 'Test issue ☃')
  941. msg = pagure.lib.query.new_issue(
  942. session=self.session,
  943. repo=repo,
  944. title='Test issue with milestone',
  945. content='Testing search',
  946. user='pingou',
  947. milestone='1.1',
  948. )
  949. self.session.commit()
  950. self.assertEqual(msg.title, 'Test issue with milestone')
  951. # Whole list
  952. output = self.app.get('/test/issues')
  953. self.assertEqual(output.status_code, 200)
  954. output_text = output.get_data(as_text=True)
  955. self.assertIn('<title>Issues - test - Pagure</title>', output_text)
  956. self.assertTrue(
  957. '<span class="fa fa-fw fa-exclamation-circle"></span> 2 Open Issues\n' in output_text)
  958. # Unicode search pattern
  959. output = self.app.get(
  960. '/test/issues?status=all&search_pattern=☃')
  961. self.assertEqual(output.status_code, 200)
  962. output_text = output.get_data(as_text=True)
  963. self.assertIn('<title>Issues - test - Pagure</title>', output_text)
  964. self.assertIn('<span class="fa fa-fw fa-exclamation-circle"></span> 1 Open &amp; Closed Issues\n', output_text)
  965. @patch('pagure.lib.git.update_git')
  966. @patch('pagure.lib.notify.send_email')
  967. def test_view_issue_inconsistent_milestone(self, p_send_email, p_ugt):
  968. """ Test the view_issue endpoint when the milestone keys are
  969. inconsistent with the milestones of the project. """
  970. p_send_email.return_value = True
  971. p_ugt.return_value = True
  972. tests.create_projects(self.session)
  973. tests.create_projects_git(
  974. os.path.join(self.path, 'repos'), bare=True)
  975. # Add milestones to the project
  976. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  977. milestones = {
  978. 'v1.0': {'date': None, 'active': True},
  979. 'v2.0': {'date': 'in the future', 'active': True},
  980. }
  981. repo.milestones = milestones
  982. repo.milestones_keys = ['', 'v1.0', 'v2.0']
  983. # Create issues to play with
  984. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  985. msg = pagure.lib.query.new_issue(
  986. session=self.session,
  987. repo=repo,
  988. title='Test issue',
  989. content='We should work on this',
  990. user='pingou',
  991. )
  992. self.session.commit()
  993. self.assertEqual(msg.title, 'Test issue')
  994. output = self.app.get('/test/issue/1')
  995. self.assertEqual(output.status_code, 200)
  996. output_text = output.get_data(as_text=True)
  997. # Not authentified = No edit
  998. self.assertNotIn(
  999. '<a class="btn btn-outline-secondary btn-sm border-0" ''href="/test/issue/1/edit" title="Edit this issue">\n',
  1000. output_text)
  1001. self.assertIn(
  1002. '<a href="/login/?next=http%3A%2F%2Flocalhost%2Ftest%2Fissue%2F1">'
  1003. 'Login</a>\n to comment on this ticket.',
  1004. output_text)
  1005. @patch('pagure.lib.git.update_git')
  1006. @patch('pagure.lib.notify.send_email')
  1007. def test_view_issue(self, p_send_email, p_ugt):
  1008. """ Test the view_issue endpoint. """
  1009. p_send_email.return_value = True
  1010. p_ugt.return_value = True
  1011. output = self.app.get('/foo/issue/1')
  1012. self.assertEqual(output.status_code, 404)
  1013. tests.create_projects(self.session)
  1014. tests.create_projects_git(
  1015. os.path.join(self.path, 'repos'), bare=True)
  1016. output = self.app.get('/test/issue/1')
  1017. self.assertEqual(output.status_code, 404)
  1018. # Create issues to play with
  1019. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1020. msg = pagure.lib.query.new_issue(
  1021. session=self.session,
  1022. repo=repo,
  1023. title='Test issue',
  1024. content='We should work on this',
  1025. user='pingou',
  1026. )
  1027. self.session.commit()
  1028. self.assertEqual(msg.title, 'Test issue')
  1029. output = self.app.get('/test/issue/1')
  1030. self.assertEqual(output.status_code, 200)
  1031. output_text = output.get_data(as_text=True)
  1032. # Not authentified = No edit
  1033. self.assertNotIn(
  1034. '<a class="btn btn-outline-secondary btn-sm border-0" '
  1035. 'href="/test/issue/1/edit" title="Edit this issue">\n',
  1036. output_text)
  1037. self.assertIn(
  1038. '<a href="/login/?next=http%3A%2F%2Flocalhost%2Ftest%2Fissue%2F1">'
  1039. 'Login</a>\n to comment on this ticket.',
  1040. output_text)
  1041. user = tests.FakeUser()
  1042. with tests.user_set(self.app.application, user):
  1043. output = self.app.get('/test/issue/1')
  1044. self.assertEqual(output.status_code, 200)
  1045. output_text = output.get_data(as_text=True)
  1046. # Not author nor admin = No edit
  1047. self.assertNotIn(
  1048. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1049. ' href="/test/issue/1/edit" title="Edit this issue">',
  1050. output_text)
  1051. self.assertNotIn(
  1052. '<a class="dropdown-item text-danger" href="javascript:void(0)" id="closeticket"\n'
  1053. ' title="Delete this ticket">\n',
  1054. output_text)
  1055. self.assertFalse(
  1056. '<a href="/login/">Login</a> to comment on this ticket.'
  1057. in output_text)
  1058. # Not author nor admin = No take
  1059. self.assertNotIn('function take_issue(){', output_text)
  1060. self.assertNotIn('function drop_issue(){', output_text)
  1061. self.assertNotIn(
  1062. '<a href="javascript:void(0)" id="take-btn"\n',
  1063. output_text)
  1064. user.username = 'pingou'
  1065. with tests.user_set(self.app.application, user):
  1066. output = self.app.get('/test/issue/1')
  1067. self.assertEqual(output.status_code, 200)
  1068. output_text = output.get_data(as_text=True)
  1069. self.assertIn(
  1070. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1071. ' href="/test/issue/1/edit" title="Edit this issue">',
  1072. output_text)
  1073. self.assertIn(
  1074. '<a class="dropdown-item text-danger" href="javascript:void(0)" id="closeticket"\n'
  1075. ' title="Delete this ticket">\n',
  1076. output_text)
  1077. csrf_token = self.get_csrf(output=output)
  1078. # Create private issue
  1079. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1080. msg = pagure.lib.query.new_issue(
  1081. session=self.session,
  1082. repo=repo,
  1083. title='Test issue',
  1084. content='We should work on this',
  1085. user='pingou',
  1086. private=True,
  1087. )
  1088. self.session.commit()
  1089. self.assertEqual(msg.title, 'Test issue')
  1090. # Not logged in
  1091. output = self.app.get('/test/issue/2')
  1092. self.assertEqual(output.status_code, 404)
  1093. # Wrong user
  1094. user = tests.FakeUser()
  1095. with tests.user_set(self.app.application, user):
  1096. output = self.app.get('/test/issue/2')
  1097. self.assertEqual(output.status_code, 404)
  1098. # reporter
  1099. user.username = 'pingou'
  1100. with tests.user_set(self.app.application, user):
  1101. output = self.app.get('/test/issue/2')
  1102. self.assertEqual(output.status_code, 200)
  1103. output_text = output.get_data(as_text=True)
  1104. self.assertIn(
  1105. '<title>Issue #2: Test issue - test - Pagure</title>',
  1106. output_text)
  1107. self.assertIn(
  1108. '<span title="Private ticket" class="text-danger fa fa-fw '
  1109. 'fa-lock"></span>', output_text)
  1110. self.assertIn(
  1111. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1112. ' href="/test/issue/2/edit" title="Edit this issue">',
  1113. output_text)
  1114. # Project w/o issue tracker
  1115. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1116. repo.settings = {'issue_tracker': False}
  1117. self.session.add(repo)
  1118. self.session.commit()
  1119. output = self.app.get('/test/issue/1')
  1120. self.assertEqual(output.status_code, 404)
  1121. @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
  1122. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  1123. def test_view_issue_author(self):
  1124. """ Test the view_issue endpoint when you're the author. """
  1125. tests.create_projects(self.session)
  1126. tests.create_projects_git(
  1127. os.path.join(self.path, 'repos'), bare=True)
  1128. # Create issues to play with
  1129. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1130. msg = pagure.lib.query.new_issue(
  1131. session=self.session,
  1132. repo=repo,
  1133. title='Test issue',
  1134. content='We should work on this',
  1135. user='foo',
  1136. )
  1137. self.session.commit()
  1138. self.assertEqual(msg.title, 'Test issue')
  1139. output = self.app.get('/test/issue/1')
  1140. self.assertEqual(output.status_code, 200)
  1141. output_text = output.get_data(as_text=True)
  1142. # Not authentified = No edit & no Close
  1143. self.assertNotIn(
  1144. '<a class="btn btn-outline-secondary btn-sm border-0" '
  1145. 'href="/test/issue/1/edit" title="Edit this issue">\n',
  1146. output_text)
  1147. self.assertNotIn(
  1148. '<form action="/test/issue/1/update" method="post" class="hidden"',
  1149. output_text)
  1150. self.assertNotIn(
  1151. '<input type="hidden" id="statusform_status" name="status" '
  1152. 'value=""/>\n', output_text)
  1153. self.assertNotIn(
  1154. '<input type="hidden" id="statusform_close_status" '
  1155. 'name="close_status" value=""/>', output_text)
  1156. self.assertIn(
  1157. '<a href="/login/?next=http%3A%2F%2Flocalhost%2Ftest%2Fissue%2F1">'
  1158. 'Login</a>\n to comment on this ticket.',
  1159. output_text)
  1160. user = tests.FakeUser(username='foo')
  1161. with tests.user_set(self.app.application, user):
  1162. output = self.app.get('/test/issue/1')
  1163. self.assertEqual(output.status_code, 200)
  1164. output_text = output.get_data(as_text=True)
  1165. # Author = Ability to close ticket
  1166. self.assertIn(
  1167. '<input type="hidden" id="statusform_status" name="status" '
  1168. 'value=""/>', output_text)
  1169. self.assertIn(
  1170. '<input type="hidden" id="statusform_close_status" '
  1171. 'name="close_status" value=""/>', output_text)
  1172. # Author = edit
  1173. self.assertIn(
  1174. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1175. ' href="/test/issue/1/edit" title="Edit this issue">',
  1176. output_text)
  1177. self.assertFalse(
  1178. '<a href="/login/">Login</a> to comment on this ticket.'
  1179. in output_text)
  1180. # author admin = take
  1181. self.assertIn('function take_issue(){', output_text)
  1182. self.assertIn('function drop_issue(){', output_text)
  1183. self.assertIn(
  1184. '<a href="javascript:void(0)" id="take-btn"\n',
  1185. output_text)
  1186. @patch('pagure.lib.git.update_git')
  1187. @patch('pagure.lib.notify.send_email')
  1188. def test_view_issue_user_ticket(self, p_send_email, p_ugt):
  1189. """ Test the view_issue endpoint. """
  1190. p_send_email.return_value = True
  1191. p_ugt.return_value = True
  1192. output = self.app.get('/foo/issue/1')
  1193. self.assertEqual(output.status_code, 404)
  1194. tests.create_projects(self.session)
  1195. tests.create_projects_git(
  1196. os.path.join(self.path, 'repos'), bare=True)
  1197. output = self.app.get('/test/issue/1')
  1198. self.assertEqual(output.status_code, 404)
  1199. # Create issues to play with
  1200. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1201. msg = pagure.lib.query.new_issue(
  1202. session=self.session,
  1203. repo=repo,
  1204. title='Test issue',
  1205. content='We should work on this',
  1206. user='pingou',
  1207. )
  1208. self.session.commit()
  1209. self.assertEqual(msg.title, 'Test issue')
  1210. output = self.app.get('/test/issue/1')
  1211. self.assertEqual(output.status_code, 200)
  1212. output_text = output.get_data(as_text=True)
  1213. # Not authentified = No edit
  1214. self.assertNotIn(
  1215. '<a class="btn btn-outline-secondary btn-sm border-0" '
  1216. 'href="/test/issue/1/edit" title="Edit this issue">\n',
  1217. output_text)
  1218. self.assertTrue(
  1219. '<a href="/login/?next=http%3A%2F%2Flocalhost%2Ftest%2Fissue%2F1">'
  1220. 'Login</a>\n to comment on this ticket.'
  1221. in output_text)
  1222. # Create issues to play with
  1223. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1224. # Add user 'foo' with ticket access on repo
  1225. msg = pagure.lib.query.add_user_to_project(
  1226. self.session,
  1227. repo,
  1228. new_user='foo',
  1229. user='pingou',
  1230. access='ticket',
  1231. )
  1232. self.assertEqual(msg, 'User added')
  1233. self.session.commit()
  1234. user = tests.FakeUser(username='foo')
  1235. with tests.user_set(self.app.application, user):
  1236. output = self.app.get('/test/issue/1')
  1237. self.assertEqual(output.status_code, 200)
  1238. output_text = output.get_data(as_text=True)
  1239. # Not author nor admin = No edit
  1240. self.assertNotIn(
  1241. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1242. ' href="/test/issue/1/edit" title="Edit this issue">',
  1243. output_text)
  1244. self.assertNotIn(
  1245. '<a class="dropdown-item text-danger" href="javascript:void(0)" id="closeticket"\n'
  1246. ' title="Delete this ticket">\n',
  1247. output_text)
  1248. self.assertFalse(
  1249. '<a href="/login/">Login</a> to comment on this ticket.'
  1250. in output_text)
  1251. # user has ticket = take ok
  1252. self.assertIn('function take_issue(){', output_text)
  1253. self.assertIn('function drop_issue(){', output_text)
  1254. self.assertIn(
  1255. '<a href="javascript:void(0)" id="take-btn"\n',
  1256. output_text)
  1257. @patch('pagure.lib.git.update_git')
  1258. @patch('pagure.lib.notify.send_email')
  1259. def test_view_issue_custom_field_user_ticket(self, p_send_email, p_ugt):
  1260. """ Test the view_issue endpoint. """
  1261. p_send_email.return_value = True
  1262. p_ugt.return_value = True
  1263. output = self.app.get('/foo/issue/1')
  1264. self.assertEqual(output.status_code, 404)
  1265. tests.create_projects(self.session)
  1266. tests.create_projects_git(
  1267. os.path.join(self.path, 'repos'), bare=True)
  1268. output = self.app.get('/test/issue/1')
  1269. self.assertEqual(output.status_code, 404)
  1270. # Create issues to play with
  1271. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1272. msg = pagure.lib.query.new_issue(
  1273. session=self.session,
  1274. repo=repo,
  1275. title='Test issue',
  1276. content='We should work on this',
  1277. user='pingou',
  1278. )
  1279. self.session.commit()
  1280. self.assertEqual(msg.title, 'Test issue')
  1281. # Add user 'foo' with ticket access on repo
  1282. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1283. msg = pagure.lib.query.add_user_to_project(
  1284. self.session,
  1285. repo,
  1286. new_user='foo',
  1287. user='pingou',
  1288. access='ticket',
  1289. )
  1290. self.assertEqual(msg, 'User added')
  1291. self.session.commit()
  1292. # Set some custom fields
  1293. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1294. msg = pagure.lib.query.set_custom_key_fields(
  1295. self.session,
  1296. repo,
  1297. ['bugzilla', 'upstream', 'reviewstatus'],
  1298. ['link', 'boolean', 'list'],
  1299. ['unused data for non-list type', '', 'ack, nack , needs review'],
  1300. [None, None, None])
  1301. self.session.commit()
  1302. self.assertEqual(msg, 'List of custom fields updated')
  1303. # User with no rights
  1304. user = tests.FakeUser()
  1305. with tests.user_set(self.app.application, user):
  1306. output = self.app.get('/test/issue/1')
  1307. self.assertEqual(output.status_code, 200)
  1308. output_text = output.get_data(as_text=True)
  1309. self.assertNotIn(
  1310. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1311. ' href="/test/issue/1/edit" title="Edit this issue">',
  1312. output_text)
  1313. self.assertNotIn(
  1314. '<a class="dropdown-item text-danger" href="javascript:void(0)" id="closeticket"\n'
  1315. ' title="Delete this ticket">\n',
  1316. output_text)
  1317. # user no ACLs = no take action/button
  1318. self.assertNotIn('function take_issue(){', output_text)
  1319. self.assertNotIn('function drop_issue(){', output_text)
  1320. self.assertNotIn(
  1321. '<a href="javascript:void(0)" id="take-btn"\n',
  1322. output_text)
  1323. # user no ACLs = no metadata form
  1324. self.assertNotIn(
  1325. '<input class="form-control" '
  1326. 'name="bugzilla" id="bugzilla"/>', output_text)
  1327. self.assertNotIn(
  1328. '<select class="form-control" name="reviewstatus" '
  1329. 'id="reviewstatus>', output_text)
  1330. self.assertNotIn(
  1331. '<input type="checkbox" '
  1332. 'class="form-control" name="upstream" id="upstream"/>',
  1333. output_text)
  1334. user = tests.FakeUser(username='foo')
  1335. with tests.user_set(self.app.application, user):
  1336. output = self.app.get('/test/issue/1')
  1337. self.assertEqual(output.status_code, 200)
  1338. output_text = output.get_data(as_text=True)
  1339. self.assertNotIn(
  1340. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1341. ' href="/test/issue/1/edit" title="Edit this issue">',
  1342. output_text)
  1343. self.assertNotIn(
  1344. '<a class="dropdown-item text-danger" href="javascript:void(0)" id="closeticket"\n'
  1345. ' title="Delete this ticket">\n',
  1346. output_text)
  1347. self.assertNotIn(
  1348. '<a href="/login/">Login</a> to comment on this ticket.',
  1349. output_text)
  1350. # user has ticket = take ok
  1351. self.assertIn('function take_issue(){', output_text)
  1352. self.assertIn('function drop_issue(){', output_text)
  1353. self.assertIn(
  1354. '<a href="javascript:void(0)" id="take-btn"\n',
  1355. output_text)
  1356. # user has ticket == Sees the metadata
  1357. self.assertIn(
  1358. '<input class="form-control" '
  1359. 'name="bugzilla" id="bugzilla"/>', output_text)
  1360. self.assertIn(
  1361. '<select class="form-control"\n'
  1362. ' name="reviewstatus"\n'
  1363. ' id="reviewstatus">\n',
  1364. output_text)
  1365. self.assertIn(
  1366. '<input type="checkbox" '
  1367. 'class="form-control" name="upstream" id="upstream"/>',
  1368. output_text)
  1369. @patch('pagure.lib.git.update_git')
  1370. @patch('pagure.lib.notify.send_email')
  1371. def test_view_issue_non_ascii_milestone(self, p_send_email, p_ugt):
  1372. """ Test the view_issue endpoint with non-ascii milestone. """
  1373. p_send_email.return_value = True
  1374. p_ugt.return_value = True
  1375. output = self.app.get('/foo/issue/1')
  1376. self.assertEqual(output.status_code, 404)
  1377. tests.create_projects(self.session)
  1378. tests.create_projects_git(
  1379. os.path.join(self.path, 'repos'), bare=True)
  1380. output = self.app.get('/test/issue/1')
  1381. self.assertEqual(output.status_code, 404)
  1382. stone = 'käpy'
  1383. # Create issues to play with
  1384. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1385. msg = pagure.lib.query.new_issue(
  1386. session=self.session,
  1387. repo=repo,
  1388. title='Test issue',
  1389. content='We should work on this',
  1390. user='pingou',
  1391. )
  1392. self.session.commit()
  1393. self.assertEqual(msg.title, 'Test issue')
  1394. # Add a non-ascii milestone to the issue but project has no milestone
  1395. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  1396. message = pagure.lib.query.edit_issue(
  1397. self.session,
  1398. issue=issue,
  1399. milestone=stone,
  1400. private=False,
  1401. user='pingou',
  1402. )
  1403. self.assertEqual(
  1404. message,
  1405. [
  1406. 'Issue set to the milestone: k\xe4py'
  1407. ]
  1408. )
  1409. self.session.commit()
  1410. output = self.app.get('/test/issue/1')
  1411. self.assertEqual(output.status_code, 200)
  1412. output_text = output.get_data(as_text=True)
  1413. self.assertIn(
  1414. '<title>Issue #1: Test issue - test - Pagure</title>',
  1415. output_text)
  1416. self.assertNotIn(stone, output_text)
  1417. # Add a non-ascii milestone to the project
  1418. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1419. repo.milestones = {'käpy': None}
  1420. self.session.add(repo)
  1421. self.session.commit()
  1422. # View the issue
  1423. output = self.app.get('/test/issue/1')
  1424. self.assertEqual(output.status_code, 200)
  1425. output_text = output.get_data(as_text=True)
  1426. self.assertIn(
  1427. '<title>Issue #1: Test issue - test - Pagure</title>',
  1428. output_text)
  1429. self.assertIn(stone, output_text)
  1430. @patch('pagure.lib.git.update_git')
  1431. @patch('pagure.lib.notify.send_email')
  1432. def test_view_issue_list_no_data(self, p_send_email, p_ugt):
  1433. """ Test the view_issue endpoint when the issue has a custom field
  1434. of type list with no data attached. """
  1435. p_send_email.return_value = True
  1436. p_ugt.return_value = True
  1437. tests.create_projects(self.session)
  1438. tests.create_projects_git(
  1439. os.path.join(self.path, 'repos'), bare=True)
  1440. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1441. # Add custom fields to the project
  1442. msg = pagure.lib.query.set_custom_key_fields(
  1443. session=self.session,
  1444. project=repo,
  1445. fields=['test1'],
  1446. types=['list'],
  1447. data=[None],
  1448. notify=[None]
  1449. )
  1450. self.session.commit()
  1451. self.assertEqual(msg, 'List of custom fields updated')
  1452. # Create issues to play with
  1453. msg = pagure.lib.query.new_issue(
  1454. session=self.session,
  1455. repo=repo,
  1456. title='Big problÈm!',
  1457. content='We should work on this',
  1458. user='pingou',
  1459. )
  1460. self.session.commit()
  1461. self.assertEqual(msg.title, 'Big problÈm!')
  1462. # Assign a value to the custom key on that ticket
  1463. cfield = pagure.lib.query.get_custom_key(
  1464. session=self.session,
  1465. project=repo,
  1466. keyname='test1')
  1467. msg = pagure.lib.query.set_custom_key_value(
  1468. session=self.session,
  1469. issue=msg,
  1470. key=cfield,
  1471. value='item')
  1472. self.session.commit()
  1473. self.assertEqual(msg, 'Custom field test1 adjusted to item')
  1474. user = tests.FakeUser()
  1475. user.username = 'pingou'
  1476. with tests.user_set(self.app.application, user):
  1477. output = self.app.get('/test/issue/1')
  1478. self.assertEqual(output.status_code, 200)
  1479. @patch('pagure.lib.git.update_git')
  1480. @patch('pagure.lib.notify.send_email')
  1481. def test_update_issue(self, p_send_email, p_ugt):
  1482. """ Test the update_issue endpoint. """
  1483. p_send_email.return_value = True
  1484. p_ugt.return_value = True
  1485. # No Git repo
  1486. output = self.app.get('/foo/issue/1/update')
  1487. self.assertEqual(output.status_code, 404)
  1488. tests.create_projects(self.session)
  1489. tests.create_projects_git(
  1490. os.path.join(self.path, 'repos'), bare=True)
  1491. output = self.app.get('/test/issue/1/update')
  1492. self.assertEqual(output.status_code, 302)
  1493. # Create issues to play with
  1494. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1495. msg = pagure.lib.query.new_issue(
  1496. session=self.session,
  1497. repo=repo,
  1498. title='Test issue',
  1499. content='We should work on this',
  1500. user='pingou',
  1501. )
  1502. self.session.commit()
  1503. self.assertEqual(msg.title, 'Test issue')
  1504. user = tests.FakeUser()
  1505. user.username = 'pingou'
  1506. with tests.user_set(self.app.application, user):
  1507. output = self.app.get('/test/issue/1')
  1508. self.assertEqual(output.status_code, 200)
  1509. output_text = output.get_data(as_text=True)
  1510. self.assertIn(
  1511. '<title>Issue #1: Test issue - test - Pagure</title>',
  1512. output_text)
  1513. self.assertIn(
  1514. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1515. ' href="/test/issue/1/edit" title="Edit this issue">',
  1516. output_text)
  1517. self.assertEqual(output_text.count('title="PY C (pingou)"'), 1)
  1518. csrf_token = self.get_csrf(output=output)
  1519. data = {
  1520. 'status': 'Closed',
  1521. 'close_status': 'fixed'
  1522. }
  1523. # Invalid repo
  1524. output = self.app.post('/bar/issue/1/update', data=data)
  1525. self.assertEqual(output.status_code, 404)
  1526. # Non-existing issue
  1527. output = self.app.post('/test/issue/100/update', data=data)
  1528. self.assertEqual(output.status_code, 404)
  1529. output = self.app.post(
  1530. '/test/issue/1/update', data=data, follow_redirects=True)
  1531. self.assertEqual(output.status_code, 200)
  1532. output_text = output.get_data(as_text=True)
  1533. self.assertIn(
  1534. '<title>Issue #1: Test issue - test - Pagure</title>',
  1535. output_text)
  1536. self.assertIn(
  1537. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1538. ' href="/test/issue/1/edit" title="Edit this issue">',
  1539. output_text)
  1540. self.assertFalse(
  1541. '<option selected value="Fixed">Fixed</option>'
  1542. in output_text)
  1543. # Right status, wrong csrf
  1544. data['close_status'] = 'Fixed'
  1545. output = self.app.post(
  1546. '/test/issue/1/update', data=data, follow_redirects=True)
  1547. self.assertEqual(output.status_code, 200)
  1548. output_text = output.get_data(as_text=True)
  1549. self.assertIn(
  1550. '<title>Issue #1: Test issue - test - Pagure</title>',
  1551. output_text)
  1552. self.assertIn(
  1553. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1554. ' href="/test/issue/1/edit" title="Edit this issue">',
  1555. output_text)
  1556. self.assertFalse(
  1557. '<option selected value="Fixed">Fixed</option>'
  1558. in output_text)
  1559. # working status update
  1560. data['csrf_token'] = csrf_token
  1561. output = self.app.post(
  1562. '/test/issue/1/update', data=data, follow_redirects=True)
  1563. self.assertEqual(output.status_code, 200)
  1564. output_text = output.get_data(as_text=True)
  1565. self.assertIn(
  1566. '<title>Issue #1: Test issue - test - Pagure</title>',
  1567. output_text)
  1568. self.assertIn(
  1569. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1570. ' href="/test/issue/1/edit" title="Edit this issue">',
  1571. output_text)
  1572. self.assertIn(
  1573. ''
  1574. 'Issue close_status updated to: Fixed',
  1575. output_text)
  1576. self.assertIn(
  1577. ''
  1578. 'Issue status updated to: Closed (was: Open)',
  1579. output_text)
  1580. self.assertTrue(
  1581. '<option selected value="Fixed">Fixed</option>'
  1582. in output_text)
  1583. # FIXME: There is likely something going wrong in the html
  1584. # below
  1585. self.assertIn(
  1586. '<span class="font-size-09 autogenerated-comment pl-4">'
  1587. '<p><strong>Metadata Update from <a href="http://localhost.localdomain/user/pingou">'
  1588. '</a><a href="http://localhost.localdomain/user/pingou">@pingou</a></strong>:'
  1589. '<br>\n- Issue close_status updated to: Fixed<br>\n- Issue status updated to:'
  1590. ' Closed (was: Open)</p></span>\n',
  1591. output_text)
  1592. # Add new comment
  1593. data = {
  1594. 'csrf_token': csrf_token,
  1595. 'status': 'Closed',
  1596. 'close_status': 'Fixed',
  1597. 'comment': 'Woohoo a second comment!',
  1598. }
  1599. output = self.app.post(
  1600. '/test/issue/1/update', data=data, follow_redirects=True)
  1601. self.assertEqual(output.status_code, 200)
  1602. output_text = output.get_data(as_text=True)
  1603. self.assertIn(
  1604. '<title>Issue #1: Test issue - test - Pagure</title>',
  1605. output_text)
  1606. self.assertIn(
  1607. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1608. ' href="/test/issue/1/edit" title="Edit this issue">',
  1609. output_text)
  1610. self.assertIn(
  1611. 'Comment added',
  1612. output_text)
  1613. self.assertNotIn(
  1614. 'No changes to edit',
  1615. output_text)
  1616. self.assertIn(
  1617. '<p>Woohoo a second comment!</p>',
  1618. output_text)
  1619. self.assertEqual(
  1620. output_text.count('comment_body">'), 2)
  1621. self.assertTrue(
  1622. '<option selected value="Fixed">Fixed</option>'
  1623. in output_text)
  1624. # 3: one for the original comment, one for the new comment, one for the metadata update
  1625. self.assertEqual(
  1626. output_text.count('title="PY C (pingou)"'),
  1627. 3)
  1628. # Add new tag
  1629. data = {
  1630. 'csrf_token': csrf_token,
  1631. 'status': 'Closed',
  1632. 'close_status': 'Fixed',
  1633. 'tag': 'tag2',
  1634. }
  1635. output = self.app.post(
  1636. '/test/issue/1/update', data=data, follow_redirects=True)
  1637. self.assertEqual(output.status_code, 200)
  1638. output_text = output.get_data(as_text=True)
  1639. self.assertIn(
  1640. '<title>Issue #1: Test issue - test - Pagure</title>',
  1641. output_text)
  1642. self.assertIn(
  1643. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1644. ' href="/test/issue/1/edit" title="Edit this issue">',
  1645. output_text)
  1646. self.assertIn(
  1647. '<p>Woohoo a second comment!</p>',
  1648. output_text)
  1649. self.assertEqual(
  1650. output_text.count('comment_body">'), 2)
  1651. self.assertTrue(
  1652. '<option selected value="Fixed">Fixed</option>'
  1653. in output_text)
  1654. # Assign issue to an non-existent user
  1655. data = {
  1656. 'csrf_token': csrf_token,
  1657. 'status': 'Closed',
  1658. 'close_status': 'Fixed',
  1659. 'assignee': 'ralph',
  1660. }
  1661. output = self.app.post(
  1662. '/test/issue/1/update', data=data, follow_redirects=True)
  1663. self.assertEqual(output.status_code, 200)
  1664. output_text = output.get_data(as_text=True)
  1665. self.assertIn(
  1666. '<title>Issue #1: Test issue - test - Pagure</title>',
  1667. output_text)
  1668. self.assertIn(
  1669. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1670. ' href="/test/issue/1/edit" title="Edit this issue">',
  1671. output_text)
  1672. self.assertIn(
  1673. 'No user &#34;ralph&#34; found',
  1674. output_text)
  1675. self.assertIn(
  1676. '<p>Woohoo a second comment!</p>',
  1677. output_text)
  1678. self.assertEqual(
  1679. output_text.count('comment_body">'), 2)
  1680. self.assertTrue(
  1681. '<option selected value="Fixed">Fixed</option>'
  1682. in output_text)
  1683. # Assign issue properly
  1684. data = {
  1685. 'csrf_token': csrf_token,
  1686. 'status': 'Closed',
  1687. 'close_status': 'Fixed',
  1688. 'assignee': 'pingou',
  1689. }
  1690. output = self.app.post(
  1691. '/test/issue/1/update', data=data, follow_redirects=True)
  1692. self.assertEqual(output.status_code, 200)
  1693. output_text = output.get_data(as_text=True)
  1694. self.assertIn(
  1695. '<title>Issue #1: Test issue - test - Pagure</title>',
  1696. output_text)
  1697. self.assertIn(
  1698. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1699. ' href="/test/issue/1/edit" title="Edit this issue">',
  1700. output_text)
  1701. self.assertIn(
  1702. 'Issue assigned to pingou',
  1703. output_text)
  1704. self.assertIn(
  1705. '<a href="/test/issues?assignee=pingou" title="PY C (pingou)"',
  1706. output_text)
  1707. self.assertIn(
  1708. '<p>Woohoo a second comment!</p>',
  1709. output_text)
  1710. self.assertEqual(
  1711. output_text.count('comment_body">'), 2)
  1712. self.assertTrue(
  1713. '<option selected value="Fixed">Fixed</option>'
  1714. in output_text)
  1715. # Create another issue with a dependency
  1716. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1717. msg = pagure.lib.query.new_issue(
  1718. session=self.session,
  1719. repo=repo,
  1720. title='Test issue',
  1721. content='We should work on this',
  1722. user='pingou',
  1723. )
  1724. self.session.commit()
  1725. self.assertEqual(msg.title, 'Test issue')
  1726. # Reset the status of the first issue
  1727. parent_issue = pagure.lib.query.search_issues(
  1728. self.session, repo, issueid=1)
  1729. parent_issue.status = 'Open'
  1730. self.session.add(parent_issue)
  1731. # Add the dependency relationship
  1732. self.session.add(parent_issue)
  1733. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  1734. issue.parents.append(parent_issue)
  1735. self.session.add(issue)
  1736. self.session.commit()
  1737. with tests.user_set(self.app.application, user):
  1738. data['csrf_token'] = csrf_token
  1739. output = self.app.post(
  1740. '/test/issue/2/update', data=data, follow_redirects=True)
  1741. self.assertEqual(output.status_code, 200)
  1742. output_text = output.get_data(as_text=True)
  1743. self.assertIn(
  1744. '<title>Issue #2: Test issue - test - Pagure</title>',
  1745. output_text)
  1746. self.assertIn(
  1747. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1748. ' href="/test/issue/2/edit" title="Edit this issue">',
  1749. output_text)
  1750. self.assertIn(
  1751. 'You cannot close a ticket '
  1752. 'that has ticket depending that are still open.',
  1753. output_text)
  1754. self.assertTrue(
  1755. '<option selected value="Open">Open</option>'
  1756. in output_text)
  1757. # Create private issue
  1758. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1759. msg = pagure.lib.query.new_issue(
  1760. session=self.session,
  1761. repo=repo,
  1762. title='Test issue',
  1763. content='We should work on this',
  1764. user='pingou',
  1765. private=True,
  1766. )
  1767. self.session.commit()
  1768. self.assertEqual(msg.title, 'Test issue')
  1769. # Wrong user
  1770. user = tests.FakeUser()
  1771. with tests.user_set(self.app.application, user):
  1772. output = self.app.post(
  1773. '/test/issue/3/update', data=data, follow_redirects=True)
  1774. self.assertEqual(output.status_code, 403)
  1775. # Project w/o issue tracker
  1776. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1777. repo.settings = {'issue_tracker': False}
  1778. self.session.add(repo)
  1779. self.session.commit()
  1780. with tests.user_set(self.app.application, user):
  1781. # Repo not set-up for issue tracker
  1782. output = self.app.post('/test/issue/1/update', data=data)
  1783. self.assertEqual(output.status_code, 404)
  1784. @patch('pagure.lib.git.update_git')
  1785. @patch('pagure.lib.notify.send_email')
  1786. def test_update_issue_drop_comment(self, p_send_email, p_ugt):
  1787. """ Test droping comment via the update_issue endpoint. """
  1788. p_send_email.return_value = True
  1789. p_ugt.return_value = True
  1790. tests.create_projects(self.session)
  1791. tests.create_projects_git(
  1792. os.path.join(self.path, 'repos'), bare=True)
  1793. # Create issues to play with
  1794. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1795. msg = pagure.lib.query.new_issue(
  1796. session=self.session,
  1797. repo=repo,
  1798. title='Test issue',
  1799. content='We should work on this',
  1800. user='pingou',
  1801. )
  1802. self.session.commit()
  1803. self.assertEqual(msg.title, 'Test issue')
  1804. user = tests.FakeUser()
  1805. user.username = 'pingou'
  1806. with tests.user_set(self.app.application, user):
  1807. output = self.app.get('/test/issue/1')
  1808. self.assertEqual(output.status_code, 200)
  1809. output_text = output.get_data(as_text=True)
  1810. self.assertIn(
  1811. '<title>Issue #1: Test issue - test - Pagure</title>',
  1812. output_text)
  1813. self.assertIn(
  1814. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1815. ' href="/test/issue/1/edit" title="Edit this issue">',
  1816. output_text)
  1817. csrf_token = self.get_csrf(output=output)
  1818. # Add new comment
  1819. data = {
  1820. 'csrf_token': csrf_token,
  1821. 'comment': 'Woohoo a second comment!',
  1822. }
  1823. output = self.app.post(
  1824. '/test/issue/1/update', data=data, follow_redirects=True)
  1825. self.assertEqual(output.status_code, 200)
  1826. output_text = output.get_data(as_text=True)
  1827. self.assertIn(
  1828. '<title>Issue #1: Test issue - test - Pagure</title>',
  1829. output_text)
  1830. self.assertIn(
  1831. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1832. ' href="/test/issue/1/edit" title="Edit this issue">',
  1833. output_text)
  1834. self.assertIn(
  1835. 'Comment added',
  1836. output_text)
  1837. self.assertIn(
  1838. '<p>Woohoo a second comment!</p>',
  1839. output_text)
  1840. self.assertEqual(
  1841. output_text.count('comment_body">'), 2)
  1842. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1843. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  1844. self.assertEqual(len(issue.comments), 1)
  1845. data = {
  1846. 'csrf_token': csrf_token,
  1847. 'drop_comment': 1,
  1848. }
  1849. user = tests.FakeUser()
  1850. with tests.user_set(self.app.application, user):
  1851. # Wrong issue id
  1852. output = self.app.post(
  1853. '/test/issue/3/update', data=data, follow_redirects=True)
  1854. self.assertEqual(output.status_code, 404)
  1855. # Wrong user
  1856. output = self.app.post(
  1857. '/test/issue/1/update', data=data, follow_redirects=True)
  1858. self.assertEqual(output.status_code, 403)
  1859. user = tests.FakeUser()
  1860. user.username = 'pingou'
  1861. with tests.user_set(self.app.application, user):
  1862. # Drop the new comment
  1863. output = self.app.post(
  1864. '/test/issue/1/update', data=data, follow_redirects=True)
  1865. self.assertEqual(output.status_code, 200)
  1866. output_text = output.get_data(as_text=True)
  1867. self.assertIn(
  1868. '<title>Issue #1: Test issue - test - Pagure</title>',
  1869. output_text)
  1870. self.assertIn(
  1871. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1872. ' href="/test/issue/1/edit" title="Edit this issue">',
  1873. output_text)
  1874. self.assertIn(
  1875. 'Comment removed',
  1876. output_text)
  1877. # Drop non-existant comment
  1878. output = self.app.post(
  1879. '/test/issue/1/update', data=data, follow_redirects=True)
  1880. self.assertEqual(output.status_code, 404)
  1881. self.session.commit()
  1882. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1883. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  1884. self.assertEqual(len(issue.comments), 0)
  1885. @patch('pagure.lib.git.update_git')
  1886. @patch('pagure.lib.notify.send_email')
  1887. def test_update_issue_depend(self, p_send_email, p_ugt):
  1888. """ Test adding dependency via the update_issue endpoint. """
  1889. p_send_email.return_value = True
  1890. p_ugt.return_value = True
  1891. tests.create_projects(self.session)
  1892. tests.create_projects_git(
  1893. os.path.join(self.path, 'repos'), bare=True)
  1894. # Create issues to play with
  1895. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1896. msg = pagure.lib.query.new_issue(
  1897. session=self.session,
  1898. repo=repo,
  1899. title='Test issue',
  1900. content='We should work on this',
  1901. user='pingou',
  1902. )
  1903. self.session.commit()
  1904. self.assertEqual(msg.title, 'Test issue')
  1905. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1906. msg = pagure.lib.query.new_issue(
  1907. session=self.session,
  1908. repo=repo,
  1909. title='Test issue #2',
  1910. content='We should work on this again',
  1911. user='foo',
  1912. )
  1913. self.session.commit()
  1914. self.assertEqual(msg.title, 'Test issue #2')
  1915. user = tests.FakeUser()
  1916. user.username = 'pingou'
  1917. with tests.user_set(self.app.application, user):
  1918. output = self.app.get('/test/issue/1')
  1919. self.assertEqual(output.status_code, 200)
  1920. output_text = output.get_data(as_text=True)
  1921. self.assertIn(
  1922. '<title>Issue #1: Test issue - test - Pagure</title>',
  1923. output_text)
  1924. self.assertIn(
  1925. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1926. ' href="/test/issue/1/edit" title="Edit this issue">',
  1927. output_text)
  1928. csrf_token = self.get_csrf(output=output)
  1929. # Add a dependent ticket
  1930. data = {
  1931. 'csrf_token': csrf_token,
  1932. 'depending': '2',
  1933. }
  1934. output = self.app.post(
  1935. '/test/issue/1/update', data=data, follow_redirects=True)
  1936. self.assertEqual(output.status_code, 200)
  1937. output_text = output.get_data(as_text=True)
  1938. self.assertIn(
  1939. '<title>Issue #1: Test issue - test - Pagure</title>',
  1940. output_text)
  1941. self.assertIn(
  1942. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1943. ' href="/test/issue/1/edit" title="Edit this issue">',
  1944. output_text)
  1945. # Add an invalid dependent ticket
  1946. data = {
  1947. 'csrf_token': csrf_token,
  1948. 'depending': '2,abc',
  1949. }
  1950. output = self.app.post(
  1951. '/test/issue/1/update', data=data, follow_redirects=True)
  1952. self.assertEqual(output.status_code, 200)
  1953. output_text = output.get_data(as_text=True)
  1954. self.assertIn(
  1955. '<title>Issue #1: Test issue - test - Pagure</title>',
  1956. output_text)
  1957. self.assertIn(
  1958. '<a class="btn btn-outline-secondary btn-sm border-0"'
  1959. ' href="/test/issue/1/edit" title="Edit this issue">',
  1960. output_text)
  1961. self.assertNotIn(
  1962. ''
  1963. 'Successfully edited issue #1',
  1964. output_text)
  1965. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1966. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  1967. self.assertEqual(issue.depending_text, [2])
  1968. self.assertEqual(issue.blocking_text, [])
  1969. @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
  1970. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  1971. def test_update_issue_block_closed(self):
  1972. """ Test how blocked issue shows in the UI when the blocking ticket
  1973. is open and closed. """
  1974. tests.create_projects(self.session)
  1975. tests.create_projects_git(
  1976. os.path.join(self.path, 'repos'), bare=True)
  1977. # Create issues to play with
  1978. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1979. msg = pagure.lib.query.new_issue(
  1980. session=self.session,
  1981. repo=repo,
  1982. title='Test issue',
  1983. content='We should work on this',
  1984. user='pingou',
  1985. )
  1986. self.session.commit()
  1987. self.assertEqual(msg.title, 'Test issue')
  1988. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  1989. msg = pagure.lib.query.new_issue(
  1990. session=self.session,
  1991. repo=repo,
  1992. title='Test issue #2',
  1993. content='We should work on this again',
  1994. user='foo',
  1995. )
  1996. self.session.commit()
  1997. self.assertEqual(msg.title, 'Test issue #2')
  1998. user = tests.FakeUser(username='pingou')
  1999. with tests.user_set(self.app.application, user):
  2000. output = self.app.get('/test/issue/1')
  2001. self.assertEqual(output.status_code, 200)
  2002. output_text = output.get_data(as_text=True)
  2003. self.assertIn(
  2004. '<title>Issue #1: Test issue - test - Pagure</title>',
  2005. output_text)
  2006. self.assertIn(
  2007. '<a class="btn btn-outline-secondary btn-sm border-0"'
  2008. ' href="/test/issue/1/edit" title="Edit this issue">',
  2009. output_text)
  2010. csrf_token = self.get_csrf(output=output)
  2011. # Add a dependent ticket - Open
  2012. data = {
  2013. 'csrf_token': csrf_token,
  2014. 'blocking': '2',
  2015. }
  2016. output = self.app.post(
  2017. '/test/issue/1/update', data=data, follow_redirects=True)
  2018. self.assertEqual(output.status_code, 200)
  2019. output_text = output.get_data(as_text=True)
  2020. self.assertIn(
  2021. '<title>Issue #1: Test issue - test - Pagure</title>',
  2022. output_text)
  2023. self.assertIn(
  2024. '<a class="btn btn-outline-secondary btn-sm border-0"'
  2025. ' href="/test/issue/1/edit" title="Edit this issue">',
  2026. output_text)
  2027. self.assertIn(
  2028. '<span class="fa fa-fw text-success fa-exclamation-circle pt-1"></span>',
  2029. output_text)
  2030. self.assertIn(
  2031. '<span class="text-success font-weight-bold">#2</span>',
  2032. output_text)
  2033. # Close ticket #1
  2034. data = {
  2035. 'csrf_token': csrf_token,
  2036. 'status': 'Closed',
  2037. 'blocking': '2',
  2038. }
  2039. output = self.app.post(
  2040. '/test/issue/1/update', data=data, follow_redirects=True)
  2041. self.assertEqual(output.status_code, 200)
  2042. output_text = output.get_data(as_text=True)
  2043. self.assertIn(
  2044. '<title>Issue #1: Test issue - test - Pagure</title>',
  2045. output_text)
  2046. # Now looking at how the dependent ticket looks like:
  2047. output = self.app.get('/test/issue/2')
  2048. self.assertEqual(output.status_code, 200)
  2049. output_text = output.get_data(as_text=True)
  2050. self.assertIn(
  2051. '<title>Issue #2: Test issue #2 - test - Pagure</title>',
  2052. output_text)
  2053. self.assertIn(
  2054. '<a class="btn btn-outline-secondary btn-sm border-0"'
  2055. ' href="/test/issue/2/edit" title="Edit this issue">',
  2056. output_text)
  2057. self.assertIn(
  2058. '<span class="fa fa-fw text-danger fa-exclamation-circle pt-1"></span>',
  2059. output_text)
  2060. self.assertIn(
  2061. '<span class="text-danger font-weight-bold">#1</span>',
  2062. output_text)
  2063. self.session.commit()
  2064. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2065. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2066. self.assertEqual(issue.depending_text, [])
  2067. self.assertEqual(issue.blocking_text, [2])
  2068. @patch('pagure.lib.git.update_git')
  2069. @patch('pagure.lib.notify.send_email')
  2070. def test_update_issue_block(self, p_send_email, p_ugt):
  2071. """ Test adding blocked issue via the update_issue endpoint. """
  2072. p_send_email.return_value = True
  2073. p_ugt.return_value = True
  2074. tests.create_projects(self.session)
  2075. tests.create_projects_git(
  2076. os.path.join(self.path, 'repos'), bare=True)
  2077. # Create issues to play with
  2078. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2079. msg = pagure.lib.query.new_issue(
  2080. session=self.session,
  2081. repo=repo,
  2082. title='Test issue',
  2083. content='We should work on this',
  2084. user='pingou',
  2085. )
  2086. self.session.commit()
  2087. self.assertEqual(msg.title, 'Test issue')
  2088. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2089. msg = pagure.lib.query.new_issue(
  2090. session=self.session,
  2091. repo=repo,
  2092. title='Test issue #2',
  2093. content='We should work on this again',
  2094. user='foo',
  2095. )
  2096. self.session.commit()
  2097. self.assertEqual(msg.title, 'Test issue #2')
  2098. # User is not an admin of the project
  2099. user = tests.FakeUser(username='foo')
  2100. with tests.user_set(self.app.application, user):
  2101. output = self.app.get('/test/issue/1')
  2102. self.assertEqual(output.status_code, 200)
  2103. self.assertIn(
  2104. '<title>Issue #1: Test issue - test - Pagure</title>',
  2105. output.get_data(as_text=True))
  2106. csrf_token = self.get_csrf(output=output)
  2107. # Add a dependent ticket
  2108. data = {
  2109. 'csrf_token': csrf_token,
  2110. 'blocking': '2',
  2111. }
  2112. output = self.app.post(
  2113. '/test/issue/1/update', data=data, follow_redirects=True)
  2114. self.assertEqual(output.status_code, 200)
  2115. self.assertIn(
  2116. '<title>Issue #1: Test issue - test - Pagure</title>',
  2117. output.get_data(as_text=True))
  2118. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2119. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2120. self.assertEqual(issue.depending_text, [])
  2121. self.assertEqual(issue.blocking_text, [])
  2122. user = tests.FakeUser()
  2123. user.username = 'pingou'
  2124. with tests.user_set(self.app.application, user):
  2125. output = self.app.get('/test/issue/1')
  2126. self.assertEqual(output.status_code, 200)
  2127. output_text = output.get_data(as_text=True)
  2128. self.assertIn(
  2129. '<title>Issue #1: Test issue - test - Pagure</title>',
  2130. output_text)
  2131. self.assertIn(
  2132. '<a class="btn btn-outline-secondary btn-sm border-0"'
  2133. ' href="/test/issue/1/edit" title="Edit this issue">',
  2134. output_text)
  2135. csrf_token = self.get_csrf(output=output)
  2136. # Add a dependent ticket
  2137. data = {
  2138. 'csrf_token': csrf_token,
  2139. 'blocking': '2',
  2140. }
  2141. output = self.app.post(
  2142. '/test/issue/1/update', data=data, follow_redirects=True)
  2143. self.assertEqual(output.status_code, 200)
  2144. output_text = output.get_data(as_text=True)
  2145. self.assertIn(
  2146. '<title>Issue #1: Test issue - test - Pagure</title>',
  2147. output_text)
  2148. self.assertIn(
  2149. '<a class="btn btn-outline-secondary btn-sm border-0"'
  2150. ' href="/test/issue/1/edit" title="Edit this issue">',
  2151. output_text)
  2152. # Add an invalid dependent ticket
  2153. data = {
  2154. 'csrf_token': csrf_token,
  2155. 'blocking': '2,abc',
  2156. }
  2157. output = self.app.post(
  2158. '/test/issue/1/update', data=data, follow_redirects=True)
  2159. self.assertEqual(output.status_code, 200)
  2160. output_text = output.get_data(as_text=True)
  2161. self.assertIn(
  2162. '<title>Issue #1: Test issue - test - Pagure</title>',
  2163. output_text)
  2164. self.assertIn(
  2165. '<a class="btn btn-outline-secondary btn-sm border-0"'
  2166. ' href="/test/issue/1/edit" title="Edit this issue">',
  2167. output_text)
  2168. self.assertNotIn(
  2169. ''
  2170. 'Successfully edited issue #1',
  2171. output_text)
  2172. self.session.commit()
  2173. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2174. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2175. self.assertEqual(issue.depending_text, [])
  2176. self.assertEqual(issue.blocking_text, [2])
  2177. @patch('pagure.lib.git.update_git')
  2178. @patch('pagure.lib.notify.send_email')
  2179. def test_upload_issue(self, p_send_email, p_ugt):
  2180. """ Test the upload_issue endpoint. """
  2181. p_send_email.return_value = True
  2182. p_ugt.return_value = True
  2183. tests.create_projects(self.session)
  2184. tests.create_projects_git(
  2185. os.path.join(self.path, 'repos'), bare=True)
  2186. tests.create_projects_git(
  2187. os.path.join(self.path, 'repos', 'tickets'), bare=True)
  2188. # Create issues to play with
  2189. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2190. msg = pagure.lib.query.new_issue(
  2191. session=self.session,
  2192. repo=repo,
  2193. title='Test issue',
  2194. content='We should work on this',
  2195. user='pingou',
  2196. )
  2197. self.session.commit()
  2198. self.assertEqual(msg.title, 'Test issue')
  2199. user = tests.FakeUser()
  2200. user.username = 'pingou'
  2201. with tests.user_set(self.app.application, user):
  2202. output = self.app.get('/test/issue/1')
  2203. self.assertEqual(output.status_code, 200)
  2204. output_text = output.get_data(as_text=True)
  2205. self.assertIn(
  2206. '<title>Issue #1: Test issue - test - Pagure</title>',
  2207. output_text)
  2208. self.assertIn(
  2209. '<a class="btn btn-outline-secondary btn-sm border-0"'
  2210. ' href="/test/issue/1/edit" title="Edit this issue">',
  2211. output_text)
  2212. csrf_token = self.get_csrf(output=output)
  2213. output = self.app.post('/foo/issue/1/upload')
  2214. self.assertEqual(output.status_code, 404)
  2215. output = self.app.post('/test/issue/100/upload')
  2216. self.assertEqual(output.status_code, 404)
  2217. # Invalid upload
  2218. data = {
  2219. 'enctype': 'multipart/form-data',
  2220. }
  2221. output = self.app.post(
  2222. '/test/issue/1/upload', data=data, follow_redirects=True)
  2223. self.assertEqual(output.status_code, 200)
  2224. json_data = json.loads(output.get_data(as_text=True))
  2225. exp = {'output': 'notok'}
  2226. self.assertDictEqual(json_data, exp)
  2227. # Attach a file to a ticket
  2228. with open(os.path.join(tests.HERE, 'placebo.png'), 'rb') as stream:
  2229. data = {
  2230. 'csrf_token': csrf_token,
  2231. 'filestream': stream,
  2232. 'enctype': 'multipart/form-data',
  2233. }
  2234. output = self.app.post(
  2235. '/test/issue/1/upload', data=data, follow_redirects=True)
  2236. self.assertEqual(output.status_code, 200)
  2237. json_data = json.loads(output.get_data(as_text=True))
  2238. folder = os.path.dirname(
  2239. os.path.abspath(__file__))[1:].replace('/', '_')
  2240. exp = {
  2241. 'filelocations': [
  2242. '/test/issue/raw/files/8a06845923010b27bfd8'
  2243. 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a'
  2244. 'be-%s_placebo.png' % folder
  2245. ],
  2246. 'filenames': ['%s_placebo.png' % folder],
  2247. 'output': 'ok'
  2248. }
  2249. self.assertDictEqual(json_data, exp)
  2250. # Project w/o issue tracker
  2251. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2252. repo.settings = {'issue_tracker': False}
  2253. self.session.add(repo)
  2254. self.session.commit()
  2255. with tests.user_set(self.app.application, user):
  2256. output = self.app.post('/test/issue/1/upload')
  2257. self.assertEqual(output.status_code, 404)
  2258. @patch.dict('pagure.config.config', {'PR_ONLY': True})
  2259. @patch('pagure.lib.git.update_git')
  2260. @patch('pagure.lib.notify.send_email')
  2261. def test_upload_issue_virus(self, p_send_email, p_ugt):
  2262. """ Test the upload_issue endpoint. """
  2263. if not pyclamd:
  2264. raise SkipTest()
  2265. p_send_email.return_value = True
  2266. p_ugt.return_value = True
  2267. tests.create_projects(self.session)
  2268. tests.create_projects_git(
  2269. os.path.join(self.path, 'repos'), bare=True)
  2270. tests.create_projects_git(
  2271. os.path.join(self.path, 'tickets'), bare=True)
  2272. # Create issues to play with
  2273. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2274. msg = pagure.lib.query.new_issue(
  2275. session=self.session,
  2276. repo=repo,
  2277. title='Test issue',
  2278. content='We should work on this',
  2279. user='pingou',
  2280. )
  2281. self.session.commit()
  2282. self.assertEqual(msg.title, 'Test issue')
  2283. user = tests.FakeUser()
  2284. user.username = 'pingou'
  2285. with tests.user_set(self.app.application, user):
  2286. csrf_token = self.get_csrf()
  2287. # TODO: Figure a way to enable this test on jenkins
  2288. # Try to attach a virus
  2289. if not os.environ.get('BUILD_ID'):
  2290. with tempfile.NamedTemporaryFile() as eicarfile:
  2291. eicarfile.write(pyclamd.ClamdUnixSocket().EICAR())
  2292. eicarfile.flush()
  2293. with open(eicarfile.name, 'rb') as stream:
  2294. data = {
  2295. 'csrf_token': csrf_token,
  2296. 'filestream': stream,
  2297. 'enctype': 'multipart/form-data',
  2298. }
  2299. output = self.app.post(
  2300. '/test/issue/1/upload', data=data, follow_redirects=True)
  2301. self.assertEqual(output.status_code, 200)
  2302. json_data = json.loads(output.get_data(as_text=True))
  2303. exp = {
  2304. 'output': 'notok',
  2305. }
  2306. self.assertDictEqual(json_data, exp)
  2307. @patch('pagure.lib.git.update_git')
  2308. @patch('pagure.lib.notify.send_email')
  2309. def test_upload_issue_two_files(self, p_send_email, p_ugt):
  2310. """ Test the upload_issue endpoint with two files. """
  2311. p_send_email.return_value = True
  2312. p_ugt.return_value = True
  2313. tests.create_projects(self.session)
  2314. tests.create_projects_git(
  2315. os.path.join(self.path, 'repos'), bare=True)
  2316. tests.create_projects_git(
  2317. os.path.join(self.path, 'tickets'), bare=True)
  2318. # Create issues to play with
  2319. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2320. msg = pagure.lib.query.new_issue(
  2321. session=self.session,
  2322. repo=repo,
  2323. title='Test issue',
  2324. content='We should work on this',
  2325. user='pingou',
  2326. )
  2327. self.session.commit()
  2328. self.assertEqual(msg.title, 'Test issue')
  2329. user = tests.FakeUser()
  2330. user.username = 'pingou'
  2331. with tests.user_set(self.app.application, user):
  2332. csrf_token = self.get_csrf()
  2333. # Attach two files to a ticket
  2334. with open(os.path.join(tests.HERE, 'placebo.png'), 'rb') as stream:
  2335. with open(os.path.join(tests.HERE, 'placebo.png'), 'rb') as stream2:
  2336. data = {
  2337. 'csrf_token': csrf_token,
  2338. 'filestream': [stream, stream2],
  2339. 'enctype': 'multipart/form-data',
  2340. }
  2341. output = self.app.post(
  2342. '/test/issue/1/upload', data=data, follow_redirects=True)
  2343. self.assertEqual(output.status_code, 200)
  2344. json_data = json.loads(output.get_data(as_text=True))
  2345. folder = os.path.dirname(
  2346. os.path.abspath(__file__))[1:].replace('/', '_')
  2347. exp = {
  2348. 'output': 'ok',
  2349. 'filelocations': [
  2350. '/test/issue/raw/files/8a06845923010b27bfd8'
  2351. 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a'
  2352. 'be-%s_placebo.png' % folder,
  2353. '/test/issue/raw/files/8a06845923010b27bfd8'
  2354. 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a'
  2355. 'be-%s_placebo.png' % folder,
  2356. ],
  2357. 'filenames': [
  2358. '%s_placebo.png' % folder,
  2359. '%s_placebo.png' % folder
  2360. ],
  2361. }
  2362. self.assertDictEqual(json_data, exp)
  2363. def test_view_issue_raw_file_empty(self):
  2364. """ Test the view_issue_raw_file endpoint. """
  2365. # Create the project and git repos
  2366. tests.create_projects(self.session)
  2367. tests.create_projects_git(
  2368. os.path.join(self.path, 'tickets'), bare=True)
  2369. # Create issues to play with
  2370. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2371. msg = pagure.lib.query.new_issue(
  2372. session=self.session,
  2373. repo=repo,
  2374. title='Test issue',
  2375. content='We should work on this',
  2376. user='pingou',
  2377. )
  2378. self.session.commit()
  2379. self.assertEqual(msg.title, 'Test issue')
  2380. url = '/issue/raw/8a06845923010b27bfd8'\
  2381. 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a'\
  2382. 'be-home_pierrey_repos_gitrepo_pagure_tests'\
  2383. '_placebo.png'
  2384. output = self.app.get('/foo' + url)
  2385. self.assertEqual(output.status_code, 404)
  2386. output = self.app.get('/test' + url)
  2387. self.assertEqual(output.status_code, 404)
  2388. # Project w/o issue tracker
  2389. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2390. repo.settings = {'issue_tracker': False}
  2391. self.session.add(repo)
  2392. self.session.commit()
  2393. output = self.app.get('/test' + url)
  2394. self.assertEqual(output.status_code, 404)
  2395. def test_view_issue_raw_file(self):
  2396. """ Test the view_issue_raw_file endpoint. """
  2397. # Create the issue and upload to it
  2398. self.test_upload_issue()
  2399. # Project w/ issue tracker
  2400. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2401. repo.settings = {'issue_tracker': True}
  2402. self.session.add(repo)
  2403. self.session.commit()
  2404. url = '/issue/raw/8a06845923010b27bfd8'\
  2405. 'e7e75acff7badc40d1021b4994e01f5e11ca40bc3a'\
  2406. 'be-%s_placebo.png' % os.path.dirname(
  2407. os.path.abspath(__file__))[1:].replace('/', '_')
  2408. output = self.app.get('/foo' + url)
  2409. self.assertEqual(output.status_code, 404)
  2410. output = self.app.get('/test/issue/raw/test.png')
  2411. self.assertEqual(output.status_code, 404)
  2412. # Access file by name
  2413. output = self.app.get('/test' + url)
  2414. self.assertEqual(output.status_code, 200)
  2415. # Project w/o issue tracker
  2416. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2417. repo.settings = {'issue_tracker': False}
  2418. self.session.add(repo)
  2419. self.session.commit()
  2420. output = self.app.get('/test' + url)
  2421. self.assertEqual(output.status_code, 404)
  2422. @patch('pagure.lib.git.update_git')
  2423. @patch('pagure.lib.notify.send_email')
  2424. def test_edit_issue(self, p_send_email, p_ugt):
  2425. """ Test the edit_issue endpoint. """
  2426. p_send_email.return_value = True
  2427. p_ugt.return_value = True
  2428. # No Git repo
  2429. output = self.app.get('/foo/issue/1/edit')
  2430. self.assertEqual(output.status_code, 404)
  2431. user = tests.FakeUser()
  2432. with tests.user_set(self.app.application, user):
  2433. output = self.app.get('/foo/issue/1/edit')
  2434. self.assertEqual(output.status_code, 404)
  2435. tests.create_projects(self.session)
  2436. tests.create_projects_git(
  2437. os.path.join(self.path, 'repos'), bare=True)
  2438. output = self.app.get('/test/issue/1/edit')
  2439. self.assertEqual(output.status_code, 404)
  2440. # User not logged in
  2441. output = self.app.get('/foo/issue/1/edit')
  2442. self.assertEqual(output.status_code, 404)
  2443. user.username = 'pingou'
  2444. with tests.user_set(self.app.application, user):
  2445. output = self.app.get('/test/issue/1/edit')
  2446. self.assertEqual(output.status_code, 404)
  2447. # Create issues to play with
  2448. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2449. msg = pagure.lib.query.new_issue(
  2450. session=self.session,
  2451. repo=repo,
  2452. title='Test issue',
  2453. content='We should work on this',
  2454. user='pingou',
  2455. )
  2456. self.session.commit()
  2457. self.assertEqual(msg.title, 'Test issue')
  2458. user = tests.FakeUser()
  2459. with tests.user_set(self.app.application, user):
  2460. output = self.app.get('/test/issue/1/edit')
  2461. self.assertEqual(output.status_code, 403)
  2462. user.username = 'pingou'
  2463. with tests.user_set(self.app.application, user):
  2464. output = self.app.get('/test/issue/1/edit')
  2465. self.assertEqual(output.status_code, 200)
  2466. self.assertTrue(
  2467. 'Edit Issue' in output.get_data(as_text=True))
  2468. csrf_token = self.get_csrf(output=output)
  2469. data = {
  2470. 'issue_content': 'We should work on this!'
  2471. }
  2472. output = self.app.post('/test/issue/1/edit', data=data)
  2473. self.assertEqual(output.status_code, 200)
  2474. output_text = output.get_data(as_text=True)
  2475. self.assertTrue(
  2476. 'Edit Issue' in output_text)
  2477. self.assertEqual(output_text.count(
  2478. 'This field is required.'), 1)
  2479. data['status'] = 'Open'
  2480. data['title'] = 'Test issue #1'
  2481. output = self.app.post('/test/issue/1/edit', data=data)
  2482. self.assertEqual(output.status_code, 200)
  2483. output_text = output.get_data(as_text=True)
  2484. self.assertTrue(
  2485. 'Edit Issue' in output_text)
  2486. self.assertEqual(output_text.count(
  2487. 'This field is required.'), 0)
  2488. self.assertEqual(output_text.count(
  2489. 'Not a valid choice'), 0)
  2490. data['csrf_token'] = csrf_token
  2491. output = self.app.post(
  2492. '/test/issue/1/edit', data=data, follow_redirects=True)
  2493. self.assertEqual(output.status_code, 200)
  2494. output_text = output.get_data(as_text=True)
  2495. self.assertIn(
  2496. ' <span class="fa fa-fw text-success fa-exclamation-circle pt-1"></span>\n'
  2497. ' <span class="text-success font-weight-bold">#1</span>\n ',
  2498. output_text)
  2499. self.assertEqual(output_text.count(
  2500. '<option selected value="Open">Open</option>'), 1)
  2501. self.assertEqual(output_text.count('comment_body">'), 1)
  2502. self.assertEqual(output_text.count(
  2503. '<p>We should work on this!</p>'), 1)
  2504. # Project w/o issue tracker
  2505. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2506. repo.settings = {'issue_tracker': False}
  2507. self.session.add(repo)
  2508. self.session.commit()
  2509. user.username = 'pingou'
  2510. with tests.user_set(self.app.application, user):
  2511. output = self.app.post('/test/issue/1/edit', data=data)
  2512. self.assertEqual(output.status_code, 404)
  2513. @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
  2514. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  2515. def test_edit_issue_no_change(self):
  2516. """ Test the edit_issue endpoint. """
  2517. tests.create_projects(self.session)
  2518. tests.create_projects_git(
  2519. os.path.join(self.path, 'repos'), bare=True)
  2520. # Create an issue to play with
  2521. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2522. msg = pagure.lib.query.new_issue(
  2523. session=self.session,
  2524. repo=repo,
  2525. title='Test issue',
  2526. content='We should work on this',
  2527. user='pingou',
  2528. )
  2529. self.session.commit()
  2530. self.assertEqual(msg.title, 'Test issue')
  2531. user = tests.FakeUser(username = 'pingou')
  2532. with tests.user_set(self.app.application, user):
  2533. output = self.app.get('/test/issue/1/edit')
  2534. self.assertEqual(output.status_code, 200)
  2535. self.assertTrue(
  2536. 'Edit Issue' in output.get_data(as_text=True))
  2537. csrf_token = self.get_csrf(output=output)
  2538. # Change nothing in the issue
  2539. data = {
  2540. 'issue_content': 'We should work on this',
  2541. 'status': 'Open',
  2542. 'title': 'Test issue',
  2543. 'csrf_token': csrf_token
  2544. }
  2545. output = self.app.post(
  2546. '/test/issue/1/edit', data=data, follow_redirects=True)
  2547. self.assertEqual(output.status_code, 200)
  2548. output_text = output.get_data(as_text=True)
  2549. self.assertIn(
  2550. ' <span class="fa fa-fw text-success fa-exclamation-circle pt-1"></span>\n'
  2551. ' <span class="text-success font-weight-bold">#1</span>\n ',
  2552. output_text)
  2553. self.assertEqual(output_text.count(
  2554. '<option selected value="Open">Open</option>'), 1)
  2555. self.assertEqual(output_text.count('comment_body">'), 1)
  2556. self.assertEqual(output_text.count(
  2557. '<p>We should work on this</p>'), 1)
  2558. @patch('pagure.lib.git.update_git')
  2559. @patch('pagure.lib.notify.send_email')
  2560. def test_edit_tag(self, p_send_email, p_ugt):
  2561. """ Test the edit_tag endpoint. """
  2562. p_send_email.return_value = True
  2563. p_ugt.return_value = True
  2564. # No Git repo
  2565. output = self.app.get('/foo/tag/foo/edit')
  2566. self.assertEqual(output.status_code, 404)
  2567. user = tests.FakeUser()
  2568. with tests.user_set(self.app.application, user):
  2569. output = self.app.get('/foo/tag/foo/edit')
  2570. self.assertEqual(output.status_code, 404)
  2571. tests.create_projects(self.session)
  2572. tests.create_projects_git(os.path.join(self.path, 'repos'))
  2573. output = self.app.get('/test/tag/foo/edit')
  2574. self.assertEqual(output.status_code, 403)
  2575. # User not logged in
  2576. output = self.app.get('/test/tag/foo/edit')
  2577. self.assertEqual(output.status_code, 302)
  2578. # Create issues to play with
  2579. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2580. msg = pagure.lib.query.new_issue(
  2581. session=self.session,
  2582. repo=repo,
  2583. title='Test issue',
  2584. content='We should work on this',
  2585. user='pingou',
  2586. )
  2587. self.session.commit()
  2588. self.assertEqual(msg.title, 'Test issue')
  2589. # Add a tag to the issue
  2590. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2591. msg = pagure.lib.query.add_tag_obj(
  2592. session=self.session,
  2593. obj=issue,
  2594. tags='tag1',
  2595. user='pingou',
  2596. )
  2597. self.session.commit()
  2598. self.assertEqual(msg, 'Issue tagged with: tag1')
  2599. # Before edit, list tags
  2600. tags = pagure.lib.query.get_tags_of_project(self.session, repo)
  2601. self.assertEqual([tag.tag for tag in tags], ['tag1'])
  2602. # Edit tag
  2603. user.username = 'pingou'
  2604. with tests.user_set(self.app.application, user):
  2605. #Edit a tag that doesn't exit
  2606. output = self.app.get('/test/tag/does_not_exist/edit')
  2607. self.assertEqual(output.status_code, 404)
  2608. output = self.app.get('/test/tag/tag1/edit')
  2609. self.assertEqual(output.status_code, 200)
  2610. self.assertTrue('<strong>Edit tag: tag1</strong>' in output.get_data(as_text=True))
  2611. csrf_token = self.get_csrf(output=output)
  2612. data = {'tag': 'tag2',
  2613. 'tag_description': 'lorem ipsum',
  2614. 'tag_color': 'DeepSkyBlue'}
  2615. output = self.app.post('/test/tag/tag1/edit', data=data)
  2616. self.assertEqual(output.status_code, 200)
  2617. self.assertTrue('<strong>Edit tag: tag1</strong>' in output.get_data(as_text=True))
  2618. data['csrf_token'] = csrf_token
  2619. output = self.app.post(
  2620. '/test/tag/tag1/edit', data=data, follow_redirects=True)
  2621. self.assertEqual(output.status_code, 200)
  2622. output_text = output.get_data(as_text=True)
  2623. self.assertIn(
  2624. 'Settings - test - Pagure', output_text)
  2625. self.assertIn(
  2626. ''
  2627. 'Edited tag: tag1()[DeepSkyBlue] to tag2(lorem ipsum)[DeepSkyBlue]',
  2628. output_text)
  2629. # update tag with empty description
  2630. data['tag_description'] = ''
  2631. output = self.app.post(
  2632. '/test/tag/tag2/edit', data=data, follow_redirects=True)
  2633. self.assertEqual(output.status_code, 200)
  2634. output_text = output.get_data(as_text=True)
  2635. self.assertIn(
  2636. 'Settings - test - Pagure', output_text)
  2637. self.assertIn(
  2638. ''
  2639. 'Edited tag: tag2(lorem ipsum)[DeepSkyBlue] to tag2()[DeepSkyBlue]',
  2640. output_text)
  2641. # After edit, list tags
  2642. self.session.commit()
  2643. tags = pagure.lib.query.get_tags_of_project(self.session, repo)
  2644. self.assertEqual([tag.tag for tag in tags], ['tag2'])
  2645. @patch('pagure.lib.git.update_git')
  2646. @patch('pagure.lib.notify.send_email')
  2647. def test_remove_tag(self, p_send_email, p_ugt):
  2648. """ Test the remove_tag endpoint. """
  2649. p_send_email.return_value = True
  2650. p_ugt.return_value = True
  2651. # No Git repo
  2652. output = self.app.post('/foo/droptag/')
  2653. self.assertEqual(output.status_code, 404)
  2654. user = tests.FakeUser()
  2655. with tests.user_set(self.app.application, user):
  2656. output = self.app.post('/foo/droptag/')
  2657. self.assertEqual(output.status_code, 404)
  2658. tests.create_projects(self.session)
  2659. tests.create_projects_git(os.path.join(self.path, 'repos'))
  2660. output = self.app.post('/test/droptag/')
  2661. self.assertEqual(output.status_code, 403)
  2662. # User not logged in
  2663. output = self.app.post('/test/droptag/')
  2664. self.assertEqual(output.status_code, 302)
  2665. # Create issues to play with
  2666. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2667. msg = pagure.lib.query.new_issue(
  2668. session=self.session,
  2669. repo=repo,
  2670. title='Test issue',
  2671. content='We should work on this',
  2672. user='pingou',
  2673. )
  2674. self.session.commit()
  2675. self.assertEqual(msg.title, 'Test issue')
  2676. # Add a tag to the issue
  2677. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2678. msg = pagure.lib.query.add_tag_obj(
  2679. session=self.session,
  2680. obj=issue,
  2681. tags='tag1',
  2682. user='pingou',
  2683. )
  2684. self.session.commit()
  2685. self.assertEqual(msg, 'Issue tagged with: tag1')
  2686. # Before edit, list tags
  2687. tags = pagure.lib.query.get_tags_of_project(self.session, repo)
  2688. self.assertEqual([tag.tag for tag in tags], ['tag1'])
  2689. # Edit tag
  2690. user.username = 'pingou'
  2691. with tests.user_set(self.app.application, user):
  2692. output = self.app.post(
  2693. '/test/droptag/', data={}, follow_redirects=True)
  2694. self.assertEqual(output.status_code, 200)
  2695. output_text = output.get_data(as_text=True)
  2696. self.assertTrue(
  2697. '<title>Settings - test - Pagure</title>' in output_text)
  2698. self.assertIn(
  2699. '<h5 class="pl-2 font-weight-bold text-muted">Project '
  2700. 'Settings</h5>', output_text)
  2701. csrf_token = self.get_csrf(output=output)
  2702. data = {'tag': 'tag1'}
  2703. output = self.app.post(
  2704. '/test/droptag/', data=data, follow_redirects=True)
  2705. self.assertEqual(output.status_code, 200)
  2706. output_text = output.get_data(as_text=True)
  2707. self.assertIn(
  2708. '<h5 class="pl-2 font-weight-bold text-muted">Project '
  2709. 'Settings</h5>', output_text)
  2710. data['csrf_token'] = csrf_token
  2711. output = self.app.post(
  2712. '/test/droptag/', data=data, follow_redirects=True)
  2713. self.assertEqual(output.status_code, 200)
  2714. output_text = output.get_data(as_text=True)
  2715. self.assertIn(
  2716. '<h5 class="pl-2 font-weight-bold text-muted">Project '
  2717. 'Settings</h5>', output_text)
  2718. self.assertIn(
  2719. ''
  2720. 'Tag: tag1 has been deleted', output_text)
  2721. @patch('pagure.lib.git.update_git')
  2722. @patch('pagure.lib.notify.send_email')
  2723. def test_delete_issue(self, p_send_email, p_ugt):
  2724. """ Test the delete_issue endpoint. """
  2725. p_send_email.return_value = True
  2726. p_ugt.return_value = True
  2727. tests.create_projects(self.session)
  2728. tests.create_projects_git(os.path.join(self.path, 'repos'))
  2729. tests.create_projects_git(os.path.join(self.path, 'tickets'))
  2730. # Create issues to play with
  2731. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2732. msg = pagure.lib.query.new_issue(
  2733. session=self.session,
  2734. repo=repo,
  2735. title='Test issue',
  2736. content='We should work on this',
  2737. user='pingou',
  2738. )
  2739. self.session.commit()
  2740. self.assertEqual(msg.title, 'Test issue')
  2741. user = tests.FakeUser()
  2742. with tests.user_set(self.app.application, user):
  2743. output = self.app.post(
  2744. '/foo/issue/1/drop', follow_redirects=True)
  2745. self.assertEqual(output.status_code, 404)
  2746. output = self.app.post(
  2747. '/test/issue/100/drop', follow_redirects=True)
  2748. self.assertEqual(output.status_code, 404)
  2749. output = self.app.post(
  2750. '/test/issue/1/drop', follow_redirects=True)
  2751. self.assertEqual(output.status_code, 403)
  2752. user.username = 'pingou'
  2753. with tests.user_set(self.app.application, user):
  2754. output = self.app.post(
  2755. '/test/issue/1/drop', follow_redirects=True)
  2756. self.assertEqual(output.status_code, 200)
  2757. self.assertIn(
  2758. '<title>Issue #1: Test issue - test - Pagure</title>',
  2759. output.get_data(as_text=True))
  2760. csrf_token = self.get_csrf(output=output)
  2761. data = {
  2762. }
  2763. # No CSRF token
  2764. output = self.app.post(
  2765. '/test/issue/1/drop', data=data, follow_redirects=True)
  2766. self.assertEqual(output.status_code, 200)
  2767. self.assertIn(
  2768. '<title>Issue #1: Test issue - test - Pagure</title>',
  2769. output.get_data(as_text=True))
  2770. data['csrf_token'] = csrf_token
  2771. output = self.app.post(
  2772. '/test/issue/1/drop', data=data, follow_redirects=True)
  2773. self.assertEqual(output.status_code, 200)
  2774. output_text = output.get_data(as_text=True)
  2775. self.assertIn(
  2776. '<title>Issues - test - Pagure</title>', output_text)
  2777. self.assertIn(
  2778. 'Issue deleted',
  2779. output_text)
  2780. # Project w/o issue tracker
  2781. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2782. repo.settings = {'issue_tracker': False}
  2783. self.session.add(repo)
  2784. self.session.commit()
  2785. user.username = 'pingou'
  2786. with tests.user_set(self.app.application, user):
  2787. output = self.app.post('/test/issue/1/drop', data=data)
  2788. self.assertEqual(output.status_code, 404)
  2789. @patch('pagure.lib.git.update_git')
  2790. @patch('pagure.lib.notify.send_email')
  2791. def test_update_issue_edit_comment(self, p_send_email, p_ugt):
  2792. """ Test the issues edit comment endpoint """
  2793. p_send_email.return_value = True
  2794. p_ugt.return_value = True
  2795. tests.create_projects(self.session)
  2796. tests.create_projects_git(
  2797. os.path.join(self.path, 'repos'), bare=True)
  2798. # Create issues to play with
  2799. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2800. msg = pagure.lib.query.new_issue(
  2801. session=self.session,
  2802. repo=repo,
  2803. title='Test issue',
  2804. content='We should work on this',
  2805. user='pingou',
  2806. )
  2807. self.session.commit()
  2808. self.assertEqual(msg.title, 'Test issue')
  2809. user = tests.FakeUser()
  2810. user.username = 'pingou'
  2811. with tests.user_set(self.app.application, user):
  2812. output = self.app.get('/test/issue/1')
  2813. self.assertEqual(output.status_code, 200)
  2814. output_text = output.get_data(as_text=True)
  2815. self.assertIn(
  2816. '<title>Issue #1: Test issue - test - Pagure</title>',
  2817. output_text)
  2818. self.assertIn(
  2819. '<a class="btn btn-outline-secondary btn-sm border-0"'
  2820. ' href="/test/issue/1/edit" title="Edit this issue">\n',
  2821. output_text)
  2822. csrf_token = self.get_csrf(output=output)
  2823. # Add new comment
  2824. data = {
  2825. 'csrf_token': csrf_token,
  2826. 'comment': 'Woohoo a second comment!',
  2827. }
  2828. output = self.app.post(
  2829. '/test/issue/1/update', data=data, follow_redirects=True)
  2830. self.assertEqual(output.status_code, 200)
  2831. output_text = output.get_data(as_text=True)
  2832. self.assertIn(
  2833. '<title>Issue #1: Test issue - test - Pagure</title>',
  2834. output_text)
  2835. self.assertIn(
  2836. '<a class="btn btn-outline-secondary btn-sm border-0"'
  2837. ' href="/test/issue/1/edit" title="Edit this issue">\n',
  2838. output_text)
  2839. self.assertIn(
  2840. 'Comment added',
  2841. output_text)
  2842. self.assertIn(
  2843. '<p>Woohoo a second comment!</p>',
  2844. output_text)
  2845. self.assertEqual(
  2846. output_text.count('comment_body">'), 2)
  2847. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2848. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2849. self.assertEqual(len(issue.comments), 1)
  2850. self.assertEqual(
  2851. issue.comments[0].comment,
  2852. 'Woohoo a second comment!')
  2853. data = {
  2854. 'csrf_token': csrf_token,
  2855. 'edit_comment': 1,
  2856. 'update_comment': 'Updated comment',
  2857. }
  2858. user = tests.FakeUser()
  2859. with tests.user_set(self.app.application, user):
  2860. # Wrong issue id
  2861. output = self.app.post(
  2862. '/test/issue/3/update', data=data, follow_redirects=True)
  2863. self.assertEqual(output.status_code, 404)
  2864. # Wrong user
  2865. output = self.app.post(
  2866. '/test/issue/1/update', data=data, follow_redirects=True)
  2867. self.assertEqual(output.status_code, 403)
  2868. user = tests.FakeUser()
  2869. user.username = 'pingou'
  2870. with tests.user_set(self.app.application, user):
  2871. # Edit comment
  2872. output = self.app.post(
  2873. '/test/issue/1/update', data=data, follow_redirects=True)
  2874. self.assertEqual(output.status_code, 200)
  2875. output_text = output.get_data(as_text=True)
  2876. self.assertIn(
  2877. '<title>Issue #1: Test issue - test - Pagure</title>',
  2878. output_text)
  2879. self.assertIn(
  2880. '<a class="btn btn-outline-secondary btn-sm border-0"'
  2881. ' href="/test/issue/1/edit" title="Edit this issue">',
  2882. output_text)
  2883. self.assertIn(
  2884. 'Comment updated',
  2885. output_text)
  2886. self.session.commit()
  2887. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2888. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2889. self.assertEqual(len(issue.comments), 1)
  2890. self.assertEqual(issue.comments[0].comment, 'Updated comment')
  2891. with tests.user_set(self.app.application, user):
  2892. output = self.app.get('/test/issue/1/comment/1/edit')
  2893. output_text = output.get_data(as_text=True)
  2894. self.assertIn(
  2895. '<title>test - Pagure</title>', output_text)
  2896. self.assertTrue('<div id="edit">' in output_text)
  2897. self.assertTrue('<section class="edit_comment">' in output_text)
  2898. self.assertTrue(
  2899. '<textarea class="form-control" id="update_comment"'
  2900. in output_text)
  2901. csrf_token = self.get_csrf(output=output)
  2902. data['csrf_token'] = csrf_token
  2903. data['update_comment'] = 'Second update'
  2904. # Edit the comment with the other endpoint
  2905. output = self.app.post(
  2906. '/test/issue/1/comment/1/edit',
  2907. data=data,
  2908. follow_redirects=True)
  2909. self.assertEqual(output.status_code, 200)
  2910. output_text = output.get_data(as_text=True)
  2911. self.assertIn(
  2912. '<title>Issue #1: Test issue - test - Pagure</title>',
  2913. output_text)
  2914. self.assertIn(
  2915. '<a class="btn btn-outline-secondary btn-sm border-0"'
  2916. ' href="/test/issue/1/edit" title="Edit this issue">',
  2917. output_text)
  2918. self.assertIn(
  2919. 'Comment updated',
  2920. output_text)
  2921. self.session.commit()
  2922. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2923. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2924. self.assertEqual(len(issue.comments), 1)
  2925. self.assertEqual(issue.comments[0].comment, 'Second update')
  2926. # Create another issue from someone else
  2927. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2928. msg = pagure.lib.query.new_issue(
  2929. session=self.session,
  2930. repo=repo,
  2931. title='Test issue',
  2932. content='We should work on this',
  2933. user='foo',
  2934. )
  2935. self.session.commit()
  2936. self.assertEqual(msg.title, 'Test issue')
  2937. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2938. self.assertEqual(len(issue.comments), 1)
  2939. self.assertEqual(issue.status, 'Open')
  2940. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  2941. self.assertEqual(len(issue.comments), 0)
  2942. self.assertEqual(issue.status, 'Open')
  2943. user = tests.FakeUser(username='foo')
  2944. with tests.user_set(self.app.application, user):
  2945. data = {
  2946. 'csrf_token': csrf_token,
  2947. 'comment': 'Nevermind figured it out',
  2948. 'status': 'Closed',
  2949. 'close_status': 'Invalid'
  2950. }
  2951. # Add a comment and close the ticket #1
  2952. output = self.app.post(
  2953. '/test/issue/1/update', data=data, follow_redirects=True)
  2954. self.assertEqual(output.status_code, 200)
  2955. output_text = output.get_data(as_text=True)
  2956. self.assertNotIn(
  2957. ''
  2958. 'Successfully edited issue #1\n',
  2959. output_text
  2960. )
  2961. self.assertIn(
  2962. 'Comment added',
  2963. output_text
  2964. )
  2965. self.assertNotIn(
  2966. '<a class="btn btn-outline-primary border-0 btn-sm issue-metadata-display'
  2967. ' editmetadatatoggle" href="javascript:void(0)" style="display: inline-block;">'
  2968. '<i class="fa fa-fw fa-pencil">',
  2969. output_text
  2970. )
  2971. data = {
  2972. 'csrf_token': csrf_token,
  2973. 'comment': 'Nevermind figured it out',
  2974. 'status': 'Closed',
  2975. 'close_status': 'Invalid'
  2976. }
  2977. # Add a comment and close the ticket #2
  2978. output = self.app.post(
  2979. '/test/issue/2/update', data=data, follow_redirects=True)
  2980. self.assertEqual(output.status_code, 200)
  2981. output_text = output.get_data(as_text=True)
  2982. self.assertIn(
  2983. ''
  2984. 'Issue close_status updated to: Invalid',
  2985. output_text
  2986. )
  2987. self.assertIn(
  2988. 'Comment added',
  2989. output_text
  2990. )
  2991. self.assertIn(
  2992. ''
  2993. 'Issue status updated to: Closed (was: Open)',
  2994. output_text
  2995. )
  2996. self.assertIn(
  2997. '<a class="btn btn-outline-primary border-0 btn-sm issue-metadata-display'
  2998. ' editmetadatatoggle" href="javascript:void(0)" style="display: inline-block;">'
  2999. '<i class="fa fa-fw fa-pencil">',
  3000. output_text
  3001. )
  3002. # Ticket #1 has one more comment and is still open
  3003. self.session.commit()
  3004. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  3005. self.assertEqual(len(issue.comments), 2)
  3006. self.assertEqual(issue.status, 'Open')
  3007. # Ticket #2 has one less comment and is closed
  3008. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  3009. self.assertEqual(len(issue.comments), 2)
  3010. self.assertEqual(
  3011. issue.comments[0].comment,
  3012. 'Nevermind figured it out')
  3013. self.assertEqual(
  3014. issue.comments[1].comment,
  3015. '**Metadata Update from @foo**:\n'
  3016. '- Issue close_status updated to: Invalid\n'
  3017. '- Issue status updated to: Closed (was: Open)')
  3018. self.assertEqual(issue.status, 'Closed')
  3019. @patch('pagure.lib.git.update_git')
  3020. @patch('pagure.lib.notify.send_email')
  3021. def test_git_urls(self, p_send_email, p_ugt):
  3022. """ Check that the url to the git repo for issues is present/absent when
  3023. it should.
  3024. """
  3025. p_send_email.return_value = True
  3026. p_ugt.return_value = True
  3027. self.test_view_issues()
  3028. user = tests.FakeUser()
  3029. user.username = 'pingou'
  3030. repo = pagure.lib.query._get_project(self.session, 'test')
  3031. pagure.lib.query.update_read_only_mode(self.session, repo, read_only=False)
  3032. pingou = pagure.lib.query.get_user(self.session, 'pingou')
  3033. pagure.lib.query.add_sshkey_to_project_or_user(
  3034. session=self.session,
  3035. user=pingou,
  3036. ssh_key='ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==',
  3037. pushaccess=True,
  3038. creator=pingou,
  3039. )
  3040. self.session.commit()
  3041. with tests.user_set(self.app.application, user):
  3042. # Check that the git issue URL is present
  3043. output = self.app.get('/test')
  3044. self.assertNotIn(
  3045. '<h5><strong>Issues GIT URLs</strong></h5>',
  3046. output.get_data(as_text=True))
  3047. # Project w/o issue tracker
  3048. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  3049. repo.settings = {'issue_tracker': True}
  3050. self.session.add(repo)
  3051. self.session.commit()
  3052. # Check that the git issue URL is gone
  3053. output = self.app.get('/test')
  3054. output_text = output.get_data(as_text=True)
  3055. self.assertIn(
  3056. '<h5><strong>Issues</strong></h5>', output_text)
  3057. self.assertIn(
  3058. 'value="ssh://git@localhost.localdomain/tickets/test.git',
  3059. output_text)
  3060. @patch('pagure.lib.git.update_git')
  3061. @patch('pagure.lib.notify.send_email')
  3062. def test_update_tags(self, p_send_email, p_ugt):
  3063. """ Test the update_tags endpoint. """
  3064. p_send_email.return_value = True
  3065. p_ugt.return_value = True
  3066. # No Git repo
  3067. output = self.app.post('/foo/update/tags')
  3068. self.assertEqual(output.status_code, 404)
  3069. user = tests.FakeUser()
  3070. with tests.user_set(self.app.application, user):
  3071. output = self.app.post('/foo/update/tags')
  3072. self.assertEqual(output.status_code, 404)
  3073. tests.create_projects(self.session)
  3074. tests.create_projects_git(os.path.join(self.path, 'repos'))
  3075. output = self.app.post('/test/update/tags')
  3076. self.assertEqual(output.status_code, 403)
  3077. # User not logged in
  3078. output = self.app.post('/test/update/tags')
  3079. self.assertEqual(output.status_code, 302)
  3080. # Create issues to play with
  3081. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  3082. msg = pagure.lib.query.new_issue(
  3083. session=self.session,
  3084. repo=repo,
  3085. title='Test issue',
  3086. content='We should work on this',
  3087. user='pingou',
  3088. )
  3089. self.session.commit()
  3090. self.assertEqual(msg.title, 'Test issue')
  3091. # Before update, list tags
  3092. tags = pagure.lib.query.get_tags_of_project(self.session, repo)
  3093. self.assertEqual([tag.tag for tag in tags], [])
  3094. user.username = 'pingou'
  3095. with tests.user_set(self.app.application, user):
  3096. # No CSRF
  3097. data = {
  3098. 'tag': 'red',
  3099. 'tag_description': 'lorem ipsum',
  3100. 'tag_color': '#ff0000'
  3101. }
  3102. output = self.app.post(
  3103. '/test/update/tags', data=data, follow_redirects=True)
  3104. self.assertEqual(output.status_code, 200)
  3105. output_text = output.get_data(as_text=True)
  3106. self.assertIn(
  3107. '<title>Settings - test - Pagure</title>', output_text)
  3108. self.assertIn(
  3109. '<h5 class="pl-2 font-weight-bold text-muted">Project '
  3110. 'Settings</h5>', output_text)
  3111. csrf_token = self.get_csrf(output=output)
  3112. # Invalid color
  3113. data = {
  3114. 'tag': 'red',
  3115. 'tag_description': 'lorem ipsum',
  3116. 'tag_color': 'red',
  3117. 'csrf_token': csrf_token,
  3118. }
  3119. output = self.app.post(
  3120. '/test/update/tags', data=data, follow_redirects=True)
  3121. self.assertEqual(output.status_code, 200)
  3122. output_text = output.get_data(as_text=True)
  3123. self.assertIn(
  3124. '<title>Settings - test - Pagure</title>', output_text)
  3125. self.assertIn(
  3126. ''
  3127. 'Color: red does not match the expected pattern',
  3128. output_text)
  3129. self.assertIn(
  3130. '<h5 class="pl-2 font-weight-bold text-muted">Project '
  3131. 'Settings</h5>', output_text)
  3132. # Invalid tag name
  3133. data = {
  3134. 'tag': 'red/green',
  3135. 'tag_description': 'lorem ipsum',
  3136. 'tag_color': '#fff',
  3137. 'csrf_token': csrf_token,
  3138. }
  3139. output = self.app.post(
  3140. '/test/update/tags', data=data, follow_redirects=True)
  3141. self.assertEqual(output.status_code, 200)
  3142. output_text = output.get_data(as_text=True)
  3143. self.assertIn(
  3144. '<title>Settings - test - Pagure</title>', output_text)
  3145. self.assertIn(
  3146. ''
  3147. 'Tag: red/green contains one or more invalid characters',
  3148. output_text)
  3149. self.assertIn(
  3150. '<h5 class="pl-2 font-weight-bold text-muted">Project '
  3151. 'Settings</h5>', output_text)
  3152. # Inconsistent length tags (missing tag field)
  3153. data = {
  3154. 'tag': 'red',
  3155. 'tag_description': ['lorem ipsum', 'foo bar'],
  3156. 'tag_color': ['#ff0000', '#003cff'],
  3157. 'csrf_token': csrf_token,
  3158. }
  3159. output = self.app.post(
  3160. '/test/update/tags', data=data, follow_redirects=True)
  3161. self.assertEqual(output.status_code, 200)
  3162. output_text = output.get_data(as_text=True)
  3163. self.assertIn(
  3164. '<title>Settings - test - Pagure</title>', output_text)
  3165. self.assertIn(
  3166. 'Error: Incomplete request. '
  3167. 'One or more tag fields missing.', output_text)
  3168. self.assertIn(
  3169. '<h5 class="pl-2 font-weight-bold text-muted">Project '
  3170. 'Settings</h5>', output_text)
  3171. # Inconsistent length color
  3172. data = {
  3173. 'tag': ['red', 'blue'],
  3174. 'tag_description': ['lorem ipsum', 'foo bar'],
  3175. 'tag_color': 'red',
  3176. 'csrf_token': csrf_token,
  3177. }
  3178. output = self.app.post(
  3179. '/test/update/tags', data=data, follow_redirects=True)
  3180. self.assertEqual(output.status_code, 200)
  3181. output_text = output.get_data(as_text=True)
  3182. self.assertIn(
  3183. '<title>Settings - test - Pagure</title>', output_text)
  3184. self.assertIn(
  3185. ''
  3186. 'Color: red does not match the expected pattern',
  3187. output_text)
  3188. self.assertIn(
  3189. 'Error: Incomplete request. '
  3190. 'One or more tag color fields missing.', output_text)
  3191. self.assertIn(
  3192. '<h5 class="pl-2 font-weight-bold text-muted">Project '
  3193. 'Settings</h5>', output_text)
  3194. # Inconsistent length description
  3195. data = {
  3196. 'tag': ['red', 'blue'],
  3197. 'tag_description': 'lorem ipsum',
  3198. 'tag_color': ['#ff0000', '#003cff'],
  3199. 'csrf_token': csrf_token,
  3200. }
  3201. output = self.app.post(
  3202. '/test/update/tags', data=data, follow_redirects=True)
  3203. self.assertEqual(output.status_code, 200)
  3204. output_text = output.get_data(as_text=True)
  3205. self.assertIn(
  3206. '<title>Settings - test - Pagure</title>', output_text)
  3207. self.assertIn(
  3208. 'Error: Incomplete request. '
  3209. 'One or more tag description fields missing.', output_text)
  3210. self.assertIn(
  3211. '<h5 class="pl-2 font-weight-bold text-muted">Project '
  3212. 'Settings</h5>', output_text)
  3213. # consistent length, but empty description
  3214. data = {
  3215. 'tag': ['red', 'blue'],
  3216. 'tag_description': ['lorem ipsum', ''],
  3217. 'tag_color': ['#ff0000', '#003cff'],
  3218. 'csrf_token': csrf_token,
  3219. }
  3220. output = self.app.post(
  3221. '/test/update/tags', data=data, follow_redirects=True)
  3222. self.assertEqual(output.status_code, 200)
  3223. output_text = output.get_data(as_text=True)
  3224. self.assertIn(
  3225. '<title>Settings - test - Pagure</title>', output_text)
  3226. self.assertIn(
  3227. '<span class="badge badge-info" '
  3228. 'style="background-color:#003cff">blue</span>\n'
  3229. ' &nbsp;'
  3230. '<span class="text-muted"></span>', output_text)
  3231. self.assertIn(
  3232. '<input type="hidden" value="blue" name="tag" />',
  3233. output_text)
  3234. self.assertIn(
  3235. '<span class="badge badge-info" '
  3236. 'style="background-color:#ff0000">red</span>\n'
  3237. ' &nbsp;'
  3238. '<span class="text-muted">lorem ipsum</span>', output_text)
  3239. self.assertIn(
  3240. '<input type="hidden" value="red" name="tag" />',
  3241. output_text)
  3242. # Valid query
  3243. data = {
  3244. 'tag': ['red1', 'green'],
  3245. 'tag_description': ['lorem ipsum', 'sample description'],
  3246. 'tag_color': ['#ff0000', '#00ff00'],
  3247. 'csrf_token': csrf_token,
  3248. }
  3249. output = self.app.post(
  3250. '/test/update/tags', data=data, follow_redirects=True)
  3251. self.assertEqual(output.status_code, 200)
  3252. output_text = output.get_data(as_text=True)
  3253. self.assertIn(
  3254. '<title>Settings - test - Pagure</title>', output_text)
  3255. self.assertIn(
  3256. '<span class="badge badge-info" '
  3257. 'style="background-color:#00ff00">green</span>\n'
  3258. ' &nbsp;'
  3259. '<span class="text-muted">sample description</span>', output_text)
  3260. self.assertIn(
  3261. '<input type="hidden" value="green" name="tag" />',
  3262. output_text)
  3263. self.assertIn(
  3264. '<span class="badge badge-info" '
  3265. 'style="background-color:#ff0000">red1</span>\n'
  3266. ' &nbsp;'
  3267. '<span class="text-muted">lorem ipsum</span>', output_text)
  3268. self.assertIn(
  3269. '<input type="hidden" value="red" name="tag" />',
  3270. output_text)
  3271. # Valid query - Two tags of the same color
  3272. data = {
  3273. 'tag': ['red2', 'red3'],
  3274. 'tag_color': ['#ff0000', '#ff0000'],
  3275. 'tag_description': ['', ''],
  3276. 'csrf_token': csrf_token,
  3277. }
  3278. output = self.app.post(
  3279. '/test/update/tags', data=data, follow_redirects=True)
  3280. self.assertEqual(output.status_code, 200)
  3281. output_text = output.get_data(as_text=True)
  3282. self.assertIn(
  3283. '<title>Settings - test - Pagure</title>', output_text)
  3284. self.assertIn(
  3285. '<span class="badge badge-info" '
  3286. 'style="background-color:#ff0000">red2</span>\n'
  3287. ' &nbsp;'
  3288. '<span class="text-muted"></span>', output_text)
  3289. self.assertIn(
  3290. '<input type="hidden" value="green" name="tag" />',
  3291. output_text)
  3292. self.assertIn(
  3293. '<span class="badge badge-info" '
  3294. 'style="background-color:#ff0000">red3</span>\n'
  3295. ' &nbsp;'
  3296. '<span class="text-muted"></span>', output_text)
  3297. self.assertIn(
  3298. '<input type="hidden" value="red" name="tag" />',
  3299. output_text)
  3300. # Invalid query - Tag already known
  3301. data = {
  3302. 'tag': ['red2'],
  3303. 'tag_color': ['#000'],
  3304. 'tag_description': [''],
  3305. 'csrf_token': csrf_token,
  3306. }
  3307. output = self.app.post(
  3308. '/test/update/tags', data=data, follow_redirects=True)
  3309. self.assertEqual(output.status_code, 200)
  3310. output_text = output.get_data(as_text=True)
  3311. self.assertIn(
  3312. '<title>Settings - test - Pagure</title>', output_text)
  3313. self.assertIn(
  3314. '<span class="badge badge-info" '
  3315. 'style="background-color:#ff0000">red2</span>\n'
  3316. ' &nbsp;'
  3317. '<span class="text-muted"></span>', output_text)
  3318. self.assertIn(
  3319. '<input type="hidden" value="green" name="tag" />',
  3320. output_text)
  3321. self.assertIn(
  3322. '<span class="badge badge-info" '
  3323. 'style="background-color:#ff0000">red3</span>\n'
  3324. ' &nbsp;'
  3325. '<span class="text-muted"></span>', output_text)
  3326. self.assertIn(
  3327. '<input type="hidden" value="red" name="tag" />',
  3328. output_text)
  3329. self.assertIn(
  3330. 'Duplicated tag: red2',
  3331. output_text)
  3332. # After update, list tags
  3333. tags = pagure.lib.query.get_tags_of_project(self.session, repo)
  3334. self.assertEqual(
  3335. sorted([tag.tag for tag in tags]),
  3336. ['blue', 'green', 'red', 'red1', 'red2', 'red3'])
  3337. @patch('pagure.lib.git.update_git')
  3338. @patch('pagure.lib.notify.send_email')
  3339. def test_update_tags_with_colon(self, p_send_email, p_ugt):
  3340. """ Test the update_tags endpoint with a tag containing a colon. """
  3341. p_send_email.return_value = True
  3342. p_ugt.return_value = True
  3343. tests.create_projects(self.session)
  3344. tests.create_projects_git(os.path.join(self.path, 'repos'))
  3345. # Create issues to play with
  3346. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  3347. msg = pagure.lib.query.new_issue(
  3348. session=self.session,
  3349. repo=repo,
  3350. title='Test issue',
  3351. content='We should work on this',
  3352. user='pingou',
  3353. )
  3354. self.session.commit()
  3355. self.assertEqual(msg.title, 'Test issue')
  3356. # Before update, list tags
  3357. tags = pagure.lib.query.get_tags_of_project(self.session, repo)
  3358. self.assertEqual([tag.tag for tag in tags], [])
  3359. user = tests.FakeUser()
  3360. user.username = 'pingou'
  3361. with tests.user_set(self.app.application, user):
  3362. csrf_token = self.get_csrf()
  3363. # Tag with a colon ':'
  3364. data = {
  3365. 'tag': ['is:red2'],
  3366. 'tag_color': ['#000'],
  3367. 'tag_description': [''],
  3368. 'csrf_token': csrf_token,
  3369. }
  3370. output = self.app.post(
  3371. '/test/update/tags', data=data, follow_redirects=True)
  3372. self.assertEqual(output.status_code, 200)
  3373. output_text = output.get_data(as_text=True)
  3374. self.assertIn(
  3375. '<title>Settings - test - Pagure</title>', output_text)
  3376. self.assertIn(
  3377. '<h5 class="pl-2 font-weight-bold text-muted">Project '
  3378. 'Settings</h5>', output_text)
  3379. self.assertIn(
  3380. '<span class="badge badge-info" '
  3381. 'style="background-color:#000">is:red2</span>\n'
  3382. ' &nbsp;'
  3383. '<span class="text-muted"></span>', output_text)
  3384. self.assertIn(
  3385. '<input type="hidden" value="is:red2" name="tag" />',
  3386. output_text)
  3387. # After update, list tags
  3388. tags = pagure.lib.query.get_tags_of_project(self.session, repo)
  3389. self.assertEqual(sorted([tag.tag for tag in tags]), ['is:red2'])
  3390. @patch('pagure.lib.git.update_git')
  3391. @patch('pagure.lib.notify.send_email')
  3392. def test_view_issue_namespace_comment(self, p_send_email, p_ugt):
  3393. """ Test comment on the view_issue endpoint on namespaced project.
  3394. """
  3395. # Create the project ns/test
  3396. item = pagure.lib.model.Project(
  3397. user_id=1, # pingou
  3398. name='test3',
  3399. namespace='ns',
  3400. description='test project #3',
  3401. hook_token='aaabbbcccdd',
  3402. )
  3403. self.session.add(item)
  3404. self.session.commit()
  3405. self.assertEqual(item.fullname, 'ns/test3')
  3406. pygit2.init_repository(
  3407. os.path.join(self.path, 'repos', 'ns', 'test3.git'),
  3408. bare=True)
  3409. # Create 2 issues
  3410. iss = pagure.lib.query.new_issue(
  3411. issue_id=1,
  3412. session=self.session,
  3413. repo=item,
  3414. title='test issue',
  3415. content='content test issue',
  3416. user='pingou',
  3417. )
  3418. self.session.commit()
  3419. self.assertEqual(iss.id, 1)
  3420. self.assertEqual(iss.title, 'test issue')
  3421. self.assertEqual(iss.project.fullname, 'ns/test3')
  3422. iss = pagure.lib.query.new_issue(
  3423. issue_id=2,
  3424. session=self.session,
  3425. repo=item,
  3426. title='test issue2',
  3427. content='content test issue2',
  3428. user='pingou',
  3429. )
  3430. self.session.commit()
  3431. self.assertEqual(iss.id, 2)
  3432. self.assertEqual(iss.title, 'test issue2')
  3433. self.assertEqual(iss.project.fullname, 'ns/test3')
  3434. # Add a comment on the second issue pointing to the first one
  3435. issue_comment = pagure.lib.model.IssueComment(
  3436. issue_uid=iss.uid,
  3437. comment='foo bar #1 see?',
  3438. user_id=1, # pingou
  3439. notification=False,
  3440. )
  3441. self.session.add(issue_comment)
  3442. self.session.commit()
  3443. output = self.app.get('/ns/test3/issue/2')
  3444. self.assertEqual(output.status_code, 200)
  3445. self.assertIn(
  3446. '<span class="comment_text comment_body">'
  3447. '<p>foo bar <a href="/ns/test3/issue/1" '
  3448. 'title="[Open] test issue">#1</a> see?</p></span>', output.get_data(as_text=True))
  3449. @patch('pagure.lib.git.update_git')
  3450. @patch('pagure.lib.notify.send_email')
  3451. def test_view_issue_forked_namespace_comment(self, p_send_email, p_ugt):
  3452. """ Test comment on the view_issue endpoint on namespaced project.
  3453. """
  3454. # Create the project ns/test
  3455. item = pagure.lib.model.Project(
  3456. user_id=1, # pingou
  3457. name='test3',
  3458. namespace='ns',
  3459. description='test project #3',
  3460. hook_token='aaabbbcccdd',
  3461. )
  3462. self.session.add(item)
  3463. self.session.commit()
  3464. self.assertEqual(item.fullname, 'ns/test3')
  3465. # Fork the project ns/test
  3466. item = pagure.lib.model.Project(
  3467. user_id=1, # pingou
  3468. parent_id=1, # ns/test
  3469. is_fork=True,
  3470. name='test3',
  3471. namespace='ns',
  3472. description='test project #3',
  3473. hook_token='aaabbbcccddff',
  3474. )
  3475. self.session.add(item)
  3476. self.session.commit()
  3477. self.assertEqual(item.fullname, 'forks/pingou/ns/test3')
  3478. pygit2.init_repository(
  3479. os.path.join(
  3480. self.path, 'repos', 'forks', 'pingou', 'ns', 'test3.git'),
  3481. bare=True)
  3482. # Create 2 issues
  3483. iss = pagure.lib.query.new_issue(
  3484. issue_id=1,
  3485. session=self.session,
  3486. repo=item,
  3487. title='test issue',
  3488. content='content test issue',
  3489. user='pingou',
  3490. )
  3491. self.session.commit()
  3492. self.assertEqual(iss.id, 1)
  3493. self.assertEqual(iss.title, 'test issue')
  3494. self.assertEqual(iss.project.fullname, 'forks/pingou/ns/test3')
  3495. iss = pagure.lib.query.new_issue(
  3496. issue_id=2,
  3497. session=self.session,
  3498. repo=item,
  3499. title='test issue2',
  3500. content='content test issue2',
  3501. user='pingou',
  3502. )
  3503. self.session.commit()
  3504. self.assertEqual(iss.id, 2)
  3505. self.assertEqual(iss.title, 'test issue2')
  3506. self.assertEqual(iss.project.fullname, 'forks/pingou/ns/test3')
  3507. # Add a comment on the second issue pointing to the first one
  3508. issue_comment = pagure.lib.model.IssueComment(
  3509. issue_uid=iss.uid,
  3510. comment='foo bar #1 see?',
  3511. user_id=1, # pingou
  3512. notification=False,
  3513. )
  3514. self.session.add(issue_comment)
  3515. self.session.commit()
  3516. output = self.app.get('/fork/pingou/ns/test3/issue/2')
  3517. self.assertEqual(output.status_code, 200)
  3518. self.assertIn(
  3519. '<span class="comment_text comment_body">'
  3520. '<p>foo bar <a href="/fork/pingou/ns/test3/issue/1" '
  3521. 'title="[Open] test issue">#1</a> see?</p></span>', output.get_data(as_text=True))
  3522. @patch('pagure.lib.git.update_git')
  3523. @patch('pagure.lib.notify.send_email')
  3524. def test_view_issue_closed(self, p_send_email, p_ugt):
  3525. """ Test viewing a closed issue. """
  3526. p_send_email.return_value = True
  3527. p_ugt.return_value = True
  3528. tests.create_projects(self.session)
  3529. tests.create_projects_git(
  3530. os.path.join(self.path, 'repos'), bare=True)
  3531. # Create issues to play with
  3532. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  3533. msg = pagure.lib.query.new_issue(
  3534. session=self.session,
  3535. repo=repo,
  3536. title='Test issue',
  3537. content='We should work on this',
  3538. user='pingou',
  3539. )
  3540. self.session.commit()
  3541. self.assertEqual(msg.title, 'Test issue')
  3542. user = tests.FakeUser()
  3543. user.username = 'foo'
  3544. msg = pagure.lib.query.add_user_to_project(self.session, repo, "foo", "pingou")
  3545. self.session.commit()
  3546. with tests.user_set(self.app.application, user):
  3547. output = self.app.get('/test/issue/1')
  3548. self.assertEqual(output.status_code, 200)
  3549. output_text = output.get_data(as_text=True)
  3550. self.assertIn(
  3551. '<title>Issue #1: Test issue - test - Pagure</title>',
  3552. output_text)
  3553. self.assertIn(
  3554. '<a class="btn btn-outline-secondary btn-sm border-0"'
  3555. ' href="/test/issue/1/edit" title="Edit this issue">',
  3556. output_text)
  3557. csrf_token = self.get_csrf(output=output)
  3558. # Add new comment
  3559. data = {
  3560. 'csrf_token': csrf_token,
  3561. 'status': 'Closed',
  3562. 'close_status': 'Fixed',
  3563. 'comment': 'Woohoo a second comment!',
  3564. }
  3565. output = self.app.post(
  3566. '/test/issue/1/update', data=data, follow_redirects=True)
  3567. self.assertEqual(output.status_code, 200)
  3568. output_text = output.get_data(as_text=True)
  3569. self.assertIn(
  3570. '<title>Issue #1: Test issue - test - Pagure</title>',
  3571. output_text)
  3572. self.assertIn(
  3573. '<a class="btn btn-outline-secondary btn-sm border-0"'
  3574. ' href="/test/issue/1/edit" title="Edit this issue">',
  3575. output_text)
  3576. self.assertIn(
  3577. 'Comment added',
  3578. output_text)
  3579. self.assertTrue(
  3580. '<p>Woohoo a second comment!</p>' in output_text)
  3581. self.assertEqual(output_text.count('comment_body">'), 2)
  3582. self.assertTrue(
  3583. '<option selected value="Fixed">Fixed</option>'
  3584. in output_text)
  3585. self.assertIn(
  3586. ' Closed: Fixed\n'
  3587. ' </span> just now\n'
  3588. ' </span>\n'
  3589. ' by\n'
  3590. ' <span title="foo bar (foo)">foo.</span>\n',
  3591. output_text)
  3592. def _set_up_for_reaction_test(self, private=False):
  3593. tests.create_projects(self.session)
  3594. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  3595. self.session.add(pagure.lib.model.User(
  3596. user='naysayer',
  3597. fullname='John Doe',
  3598. password=b'password',
  3599. default_email='jdoe@example.com',
  3600. ))
  3601. self.session.commit()
  3602. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  3603. msg = pagure.lib.query.new_issue(
  3604. session=self.session,
  3605. repo=repo,
  3606. title='Test issue',
  3607. content='Fix me',
  3608. user='pingou',
  3609. private=private,
  3610. )
  3611. pagure.lib.query.add_issue_comment(
  3612. session=self.session,
  3613. issue=msg,
  3614. comment='How about no',
  3615. user='naysayer',
  3616. )
  3617. self.session.commit()
  3618. @patch('pagure.lib.git.update_git')
  3619. @patch('pagure.lib.notify.send_email')
  3620. def test_add_reaction(self, p_send_email, p_ugt):
  3621. ''' Test adding a reaction to an issue comment.'''
  3622. p_send_email.return_value = True
  3623. p_ugt.return_value = True
  3624. self._set_up_for_reaction_test()
  3625. user = tests.FakeUser()
  3626. user.username = 'pingou'
  3627. with tests.user_set(self.app.application, user):
  3628. output = self.app.get('/test/issue/1')
  3629. self.assertEqual(output.status_code, 200)
  3630. data = {
  3631. 'csrf_token': self.get_csrf(output=output),
  3632. 'reaction': 'Thumbs down',
  3633. }
  3634. output = self.app.post(
  3635. '/test/issue/1/comment/1/react',
  3636. data=data,
  3637. follow_redirects=True,
  3638. )
  3639. self.assertEqual(output.status_code, 200)
  3640. # Load the page and check reaction is added.
  3641. output = self.app.get('/test/issue/1')
  3642. self.assertEqual(output.status_code, 200)
  3643. self.assertIn(
  3644. 'Thumbs down sent by pingou',
  3645. output.get_data(as_text=True)
  3646. )
  3647. @patch('pagure.lib.git.update_git')
  3648. @patch('pagure.lib.notify.send_email')
  3649. def test_add_reaction_unauthenticated(self, p_send_email, p_ugt):
  3650. '''
  3651. Test adding a reaction to an issue comment without authentication.
  3652. '''
  3653. p_send_email.return_value = True
  3654. p_ugt.return_value = True
  3655. self._set_up_for_reaction_test()
  3656. output = self.app.get('/test/issue/1')
  3657. self.assertEqual(output.status_code, 200)
  3658. data = {
  3659. 'csrf_token': self.get_csrf(output=output),
  3660. 'reaction': 'Thumbs down',
  3661. }
  3662. output = self.app.post(
  3663. '/test/issue/1/comment/1/react',
  3664. data=data,
  3665. follow_redirects=False,
  3666. )
  3667. # Redirect to login page
  3668. self.assertEqual(output.status_code, 302)
  3669. self.assertIn('/login/', output.headers['Location'])
  3670. @patch('pagure.lib.git.update_git')
  3671. @patch('pagure.lib.notify.send_email')
  3672. def test_add_reaction_private_issue(self, p_send_email, p_ugt):
  3673. '''Test adding a reaction to a private issue comment.'''
  3674. p_send_email.return_value = True
  3675. p_ugt.return_value = True
  3676. self._set_up_for_reaction_test(private=True)
  3677. user = tests.FakeUser()
  3678. user.username = 'naysayer'
  3679. with tests.user_set(self.app.application, user):
  3680. # Steal CSRF token from new issue page
  3681. output = self.app.get('/test/new_issue')
  3682. data = {
  3683. 'csrf_token': self.get_csrf(output=output),
  3684. 'reaction': 'Thumbs down',
  3685. }
  3686. output = self.app.post(
  3687. '/test/issue/1/comment/1/react',
  3688. data=data,
  3689. follow_redirects=True,
  3690. )
  3691. self.assertEqual(output.status_code, 404)
  3692. if __name__ == '__main__':
  3693. unittest.main(verbosity=2)