test_pagure_flask_ui_issues.py 184 KB

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