test_pagure_flask_ui_issues.py 227 KB

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