12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258 |
- # -*- coding: utf-8 -*-
- """
- (c) 2015-2017 - Copyright Red Hat Inc
- Authors:
- Pierre-Yves Chibon <pingou@pingoured.fr>
- """
- from __future__ import unicode_literals, absolute_import
- import json
- import unittest
- import shutil
- import sys
- import tempfile
- import time
- import os
- import re
- import pygit2
- import six
- from mock import patch, MagicMock
- from bs4 import BeautifulSoup
- from datetime import datetime, timedelta
- sys.path.insert(
- 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
- )
- import pagure.lib.query
- import pagure.lib.tasks
- import tests
- from pagure.lib.repo import PagureRepo
- def _get_commits(output):
- """ Returns the commits message in the output. All commits must have
- been made by `Alice Author` or `PY C` to be found.
- """
- commits = []
- save = False
- cnt = 0
- for row in output.split("\n"):
- if row.strip() in ["Alice Author", "Alice Äuthòr", "PY C"]:
- save = True
- if save:
- cnt += 1
- if cnt == 7:
- commits.append(row.strip())
- save = False
- cnt = 0
- return commits
- MERGED_PATTERN = (
- re.escape('<span class="text-info font-weight-bold">Merged</span> ')
- + "(just now|seconds ago)\n"
- + re.escape(
- " </span>\n by\n"
- ' <span title="PY C (pingou)">pingou.</span>\n'
- )
- )
- def set_up_git_repo(
- session,
- path,
- new_project=None,
- branch_from="feature",
- mtype="FF",
- prid=1,
- name_from="test",
- ):
- """ Set up the git repo and create the corresponding PullRequest
- object.
- """
- # Create a git repo to play with
- gitrepo = os.path.join(path, "repos", "%s.git" % name_from)
- repo = pygit2.init_repository(gitrepo, bare=True)
- newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
- repopath = os.path.join(newpath, "test")
- clone_repo = pygit2.clone_repository(gitrepo, repopath)
- # Create a file in that git repo
- with open(os.path.join(repopath, "sources"), "w") as stream:
- stream.write("foo\n bar")
- clone_repo.index.add("sources")
- clone_repo.index.write()
- try:
- com = repo.revparse_single("HEAD")
- prev_commit = [com.oid.hex]
- except:
- prev_commit = []
- # Commits the files added
- tree = clone_repo.index.write_tree()
- author = pygit2.Signature("Alice Author", "alice@authors.tld")
- committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
- clone_repo.create_commit(
- "refs/heads/master", # the name of the reference to update
- author,
- committer,
- "Add sources file for testing",
- # binary string representing the tree object ID
- tree,
- # list of binary strings representing parents of the new commit
- prev_commit,
- )
- refname = "refs/heads/master:refs/heads/master"
- ori_remote = clone_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- first_commit = repo.revparse_single("HEAD")
- def compatible_signature(name, email):
- if six.PY2:
- name = name.encode("utf-8")
- email = email.encode("utf-8")
- return pygit2.Signature(name, email)
- if mtype == "merge":
- with open(os.path.join(repopath, ".gitignore"), "w") as stream:
- stream.write("*~")
- clone_repo.index.add(".gitignore")
- clone_repo.index.write()
- # Commits the files added
- tree = clone_repo.index.write_tree()
- author = compatible_signature("Alice Äuthòr", "alice@äuthòrs.tld")
- comitter = compatible_signature(
- "Cecil Cõmmîttër", "cecil@cõmmîttërs.tld"
- )
- clone_repo.create_commit(
- "refs/heads/master",
- author,
- committer,
- "Add .gitignore file for testing",
- # binary string representing the tree object ID
- tree,
- # list of binary strings representing parents of the new commit
- [first_commit.oid.hex],
- )
- refname = "refs/heads/master:refs/heads/master"
- ori_remote = clone_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- if mtype == "conflicts":
- with open(os.path.join(repopath, "sources"), "w") as stream:
- stream.write("foo\n bar\nbaz")
- clone_repo.index.add("sources")
- clone_repo.index.write()
- # Commits the files added
- tree = clone_repo.index.write_tree()
- author = pygit2.Signature("Alice Author", "alice@authors.tld")
- committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
- clone_repo.create_commit(
- "refs/heads/master",
- author,
- committer,
- "Add sources conflicting",
- # binary string representing the tree object ID
- tree,
- # list of binary strings representing parents of the new commit
- [first_commit.oid.hex],
- )
- refname = "refs/heads/master:refs/heads/master"
- ori_remote = clone_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- # Set the second repo
- new_gitrepo = repopath
- if new_project:
- # Create a new git repo to play with
- new_gitrepo = os.path.join(newpath, new_project.fullname)
- if not os.path.exists(new_gitrepo):
- os.makedirs(new_gitrepo)
- new_repo = pygit2.clone_repository(gitrepo, new_gitrepo)
- repo = pygit2.Repository(new_gitrepo)
- if mtype != "nochanges":
- # Edit the sources file again
- with open(os.path.join(new_gitrepo, "sources"), "w") as stream:
- stream.write("foo\n bar\nbaz\n boose")
- repo.index.add("sources")
- repo.index.write()
- # Commits the files added
- tree = repo.index.write_tree()
- author = pygit2.Signature("Alice Author", "alice@authors.tld")
- committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
- repo.create_commit(
- "refs/heads/%s" % branch_from,
- author,
- committer,
- "A commit on branch %s\n\nMore information" % branch_from,
- tree,
- [first_commit.oid.hex],
- )
- refname = "refs/heads/%s" % (branch_from)
- ori_remote = repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- # Create a PR for these changes
- project = pagure.lib.query.get_authorized_project(session, "test")
- req = pagure.lib.query.new_pull_request(
- session=session,
- repo_from=project,
- branch_from=branch_from,
- repo_to=project,
- branch_to="master",
- title="PR from the %s branch" % branch_from,
- user="pingou",
- )
- session.commit()
- assert req.id == prid
- assert req.title == "PR from the %s branch" % branch_from
- shutil.rmtree(newpath)
- class PagureFlaskForktests(tests.Modeltests):
- """ Tests for flask fork controller of pagure """
- def test_request_pull_reference(self):
- """ Test if there is a reference created for a new PR. """
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- self.assertEqual(len(project.requests), 1)
- # View the pull-request
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- gitrepo = os.path.join(self.path, "repos", "test.git")
- repo = pygit2.Repository(gitrepo)
- self.assertEqual(
- list(repo.listall_references()),
- ["refs/heads/feature", "refs/heads/master", "refs/pull/1/head"],
- )
- @patch("pagure.lib.notify.send_email")
- def test_request_pull(self, send_email):
- """ Test the request_pull endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- # Non-existant project
- output = self.app.get("/foobar/pull-request/1")
- self.assertEqual(output.status_code, 404)
- # Project has no PR
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 404)
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- self.assertEqual(len(project.requests), 1)
- # View the pull-request
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- # self.assertIn(
- #'<h3><span class="label label-default">PR#1</span>\n'
- #' PR from the feature branch\n</h3>',
- # output_text)
- self.assertIn(
- 'title="View file as of 2a552b">sources</a>', output_text
- )
- # Test if the `open changed file icon` is displayed.
- self.assertIn(
- 'class="open_changed_file_icon_wrap"><span '
- 'class="fa fa-file-code-o fa-fw" '
- 'alt="Open changed file" title="Open changed file"></span>'
- "</a>",
- output_text,
- )
- self.assertIn(
- '<span class="btn btn-success btn-sm font-weight-bold disabled'
- ' opacity-100">+3</span>',
- output_text,
- )
- self.assertIn(
- '<span class="btn btn-danger btn-sm font-weight-bold disabled '
- 'opacity-100">-1</span>',
- output_text,
- )
- # Test if hunk headline is rendered without line numbers
- self.assertIn(
- '<td class="cell1"></td><td class="prc border-right"></td>\n<td '
- 'class="cell2 stretch-table-column"> <pre class='
- '"text-muted"><code>@@ -1,2 +1,4 @@',
- output_text,
- )
- # Tests if line number 1 is displayed
- self.assertNotIn(
- '<td class="cell1"><a id="_1__1" href="#_1__1" data-line-number="1" data-file-number="1"></a></td>',
- output_text,
- )
- # Test if line number 2 is displayed
- self.assertIn(
- '<td class="cell1"><a id="_1__2" href="#_1__2" data-line-number="2" data-file-number="1"></a></td>',
- output_text,
- )
- @patch("pagure.lib.notify.send_email")
- def test_request_pull_delete_branch_button_no_auth(self, send_email):
- """ Test the request_pull endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- self.assertEqual(len(project.requests), 1)
- # View the pull-request
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- 'title="View file as of 2a552b">sources</a>', output_text
- )
- # Un-authenticated user cannot see this checkbox
- self.assertNotIn(
- '<input id="delete_branch" name="delete_branch" type="checkbox" '
- 'value="y"> <label for="delete_branch">Delete branch after '
- "merging</label>",
- output_text,
- )
- @patch("pagure.lib.notify.send_email")
- def test_request_pull_delete_branch_button(self, send_email):
- """ Test the request_pull endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- self.assertEqual(len(project.requests), 1)
- # View the pull-request
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- 'title="View file as of 2a552b">sources</a>', output_text
- )
- self.assertIn(
- '<input id="delete_branch" name="delete_branch" type="checkbox" '
- 'value="y"> <label for="delete_branch">Delete branch after '
- "merging</label>",
- output_text,
- )
- @patch("pagure.lib.notify.send_email")
- def test_request_pull_delete_branch_button_no_project_from(
- self, send_email
- ):
- """ Test the request_pull endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- self.assertEqual(len(project.requests), 1)
- project.requests[0].project_from = None
- self.session.add(project.requests[0])
- self.session.commit()
- # View the pull-request
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- 'title="View file as of 2a552b">sources</a>', output_text
- )
- self.assertIn(
- '<input id="delete_branch" name="delete_branch" type="checkbox" '
- 'value="y"> <label for="delete_branch">Delete branch after '
- "merging</label>",
- output_text,
- )
- @patch("pagure.lib.notify.send_email")
- def test_request_pull_delete_branch_button_no_project_from_no_acl(
- self, send_email
- ):
- """ Test the request_pull endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- self.assertEqual(len(project.requests), 1)
- project.requests[0].project_from = None
- self.session.add(project.requests[0])
- self.session.commit()
- # View the pull-request
- user = tests.FakeUser()
- user.username = "foo"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- 'title="View file as of 2a552b">sources</a>', output_text
- )
- self.assertNotIn(
- '<input id="delete_branch" name="delete_branch" type="checkbox" '
- 'value="y"> <label for="delete_branch">Delete branch after '
- "merging</label>",
- output_text,
- )
- @patch("pagure.lib.notify.send_email")
- def test_task_update_request_pull(self, send_email):
- """ Test the task update_pull_request endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- self.session = pagure.lib.query.create_session(self.dbpath)
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- self.assertEqual(len(project.requests), 1)
- request = project.requests[0]
- self.assertEqual(len(request.comments), 0)
- start_commit = request.commit_start
- stop_commit = request.commit_stop
- # View the pull-request
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - Pagure</title>",
- output_text,
- )
- self.assertIn(
- 'title="View file as of 2a552b">sources</a>', output_text
- )
- # Add a new commit on the repo from
- newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
- gitrepo = os.path.join(self.path, "repos", "test.git")
- repopath = os.path.join(newpath, "test")
- clone_repo = pygit2.clone_repository(
- gitrepo, repopath, checkout_branch="feature"
- )
- def compatible_signature(name, email):
- if six.PY2:
- name = name.encode("utf-8")
- email = email.encode("utf-8")
- return pygit2.Signature(name, email)
- with open(os.path.join(repopath, ".gitignore"), "w") as stream:
- stream.write("*~")
- clone_repo.index.add(".gitignore")
- clone_repo.index.write()
- com = clone_repo.revparse_single("HEAD")
- prev_commit = [com.oid.hex]
- # Commits the files added
- tree = clone_repo.index.write_tree()
- author = compatible_signature("Alice Äuthòr", "alice@äuthòrs.tld")
- comitter = compatible_signature(
- "Cecil Cõmmîttër", "cecil@cõmmîttërs.tld"
- )
- clone_repo.create_commit(
- "refs/heads/feature",
- author,
- comitter,
- "Add .gitignore file for testing",
- # binary string representing the tree object ID
- tree,
- # list of binary strings representing parents of the new commit
- prev_commit,
- )
- refname = "refs/heads/feature:refs/heads/feature"
- ori_remote = clone_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- shutil.rmtree(newpath)
- pagure.lib.tasks.update_pull_request(request.uid)
- self.session = pagure.lib.query.create_session(self.dbpath)
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- self.assertEqual(len(project.requests), 1)
- request = project.requests[0]
- self.assertEqual(len(request.comments), 1)
- self.assertIsNotNone(request.commit_start)
- self.assertIsNotNone(request.commit_stop)
- self.assertNotEqual(start_commit, request.commit_start)
- self.assertNotEqual(stop_commit, request.commit_stop)
- @patch("pagure.lib.notify.send_email")
- def test_request_pull_ci_dropdown(self, send_email):
- """ Test presence of the "Rerun CI" dropdown with various settings. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- # old-style TRIGGER_CI list - test backwards compatibility
- with patch.dict(
- "pagure.config.config",
- {"TRIGGER_CI": ["old-style-trigger-ci"]},
- ):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertNotIn("Rerun CI", output_text)
- # new-style TRIGGER_CI, but no button to show
- with patch.dict(
- "pagure.config.config", {"TRIGGER_CI": {"no-button": None}}
- ):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertNotIn("Rerun CI", output_text)
- trigger_ci = {
- "foobar-ci": {
- "name": "foobar-ci-name",
- "description": "barfoo",
- },
- "spam-ci": {
- "name": "spam-ci-name",
- "description": "with beans and eggs",
- },
- "no-button-for-me-ci": None,
- }
- # new-style TRIGGER_CI, several buttons to show
- with patch.dict(
- "pagure.config.config", {"TRIGGER_CI": trigger_ci}
- ):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn("Rerun CI", output_text)
- self.assertIn("foobar-ci-name", output_text)
- self.assertIn("spam-ci-name", output_text)
- self.assertNotIn("no-button-for-me-ci", output_text)
- trigger_ci = {
- "foobar-ci": {
- "name": "foobar-ci-name",
- "description": "barfoo",
- "requires_project_hook_attr": (
- "ci_hook",
- "active_pr",
- True,
- ),
- }
- }
- # new-style TRIGGER_CI with requires_project_hook_attr that is
- # not fulfilled by the project
- with patch.dict(
- "pagure.config.config", {"TRIGGER_CI": trigger_ci}
- ):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertNotIn("Rerun CI", output_text)
- # now activate the hook and try again
- data = {
- "active_pr": "y",
- "ci_url": "https://jenkins.fedoraproject.org",
- "ci_job": "ci_job",
- "ci_type": "jenkins",
- "csrf_token": self.get_csrf(),
- }
- output = self.app.post(
- "/test/settings/Pagure CI", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- with patch.dict(
- "pagure.config.config", {"TRIGGER_CI": trigger_ci}
- ):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn("Rerun CI", output_text)
- self.assertIn("foobar-ci-name", output_text)
- # shouldn't show up if user is not logged in
- with patch.dict("pagure.config.config", {"TRIGGER_CI": trigger_ci}):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertNotIn("Rerun CI", output_text)
- @patch("pagure.lib.notify.send_email")
- @patch.dict(
- "pagure.config.config",
- {"TRIGGER_CI": {"CI1": {"name": "CI1", "description": "CI1!"}}},
- )
- def test_request_pull_ci_rerun(self, send_email):
- """ Test rerunning CI using button from the "Rerun CI" dropdown. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- user = tests.FakeUser()
- user.username = "pingou"
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- request = project.requests[0]
- with tests.user_set(self.app.application, user):
- # no csrf token
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output = self.app.post(
- "/test/pull-request/1/trigger-ci", follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- self.assertIn("Invalid input", output.get_data(as_text=True))
- # no such PR
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output = self.app.post(
- "/test/pull-request/2/trigger-ci", follow_redirects=True
- )
- self.assertEqual(output.status_code, 404)
- # wrong comment
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- csrf_token = self.get_csrf(output=output)
- data = {"csrf_token": csrf_token, "comment": "this doesnt exist"}
- output = self.app.post(
- "/test/pull-request/1/trigger-ci",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- self.assertIn("Invalid input", output.get_data(as_text=True))
- # everything ok
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- csrf_token = self.get_csrf(output=output)
- data = {"csrf_token": csrf_token, "comment": "CI1"}
- output = self.app.post(
- "/test/pull-request/1/trigger-ci",
- data=data,
- follow_redirects=True,
- )
- output_text = output.get_data(as_text=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn("<p>CI1</p>", output_text)
- comment = request.comments[0]
- self.assertTrue(comment.notification)
- self.assertEqual(comment.comment, "CI1")
- @patch("pagure.lib.notify.send_email")
- def test_merge_request_pull_FF(self, send_email):
- """ Test the merge_request_pull endpoint with a FF PR. """
- send_email.return_value = True
- self.test_request_pull()
- user = tests.FakeUser()
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- csrf_token = self.get_csrf(output=output)
- # No CSRF
- output = self.app.post(
- "/test/pull-request/1/merge", data={}, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- # self.assertIn(
- #'<h3><span class="label label-default">PR#1</span>\n'
- #' PR from the feature branch\n</h3>',
- # output_text)
- self.assertIn(
- 'title="View file as of 2a552b">sources</a>', output_text
- )
- # Wrong project
- data = {"csrf_token": csrf_token}
- output = self.app.post(
- "/foobar/pull-request/100/merge",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- # Wrong project
- data = {"csrf_token": csrf_token}
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 403)
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- # Wrong request id
- data = {"csrf_token": csrf_token}
- output = self.app.post(
- "/test/pull-request/100/merge",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- # Project w/o pull-request
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings = repo.settings
- settings["pull_requests"] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- # Pull-request disabled
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 404)
- # Project w pull-request but only assignee can merge
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings["pull_requests"] = True
- settings["Only_assignee_can_merge_pull-request"] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertIn(
- "This request must be " "assigned to be merged", output_text
- )
- # PR assigned but not to this user
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- req = repo.requests[0]
- req.assignee_id = 2
- self.session.add(req)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertIn(
- "Only the assignee can merge this request", output_text
- )
- # Project w/ minimal PR score
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings["Only_assignee_can_merge_pull-request"] = False
- settings["Minimum_score_to_merge_pull-request"] = 2
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertIn(
- "This request does not "
- "have the minimum review score necessary to be merged",
- output_text,
- )
- # Merge
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings["Minimum_score_to_merge_pull-request"] = -1
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output = self.app.get("/test/commits")
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Commits - test - Pagure</title>", output_text
- )
- self.assertIn("A commit on branch feature", output_text)
- self.assertNotIn(
- "Merge #1 `PR from the feature branch`", output_text
- )
- # Check if the closing notification was added
- output = self.app.get("/test/pull-request/1")
- self.assertIsNotNone(
- re.search(MERGED_PATTERN, output.get_data(as_text=True))
- )
- @patch("pagure.lib.notify.send_email")
- def test_merge_request_pull_merge(self, send_email):
- """ Test the merge_request_pull endpoint with a merge PR. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=None,
- branch_from="feature",
- mtype="merge",
- )
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- csrf_token = self.get_csrf(output=output)
- data = {"csrf_token": csrf_token}
- # Merge
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - Pagure</title>",
- output.get_data(as_text=True),
- )
- # Check if the closing notification was added
- output = self.app.get("/test/pull-request/1")
- self.assertIsNotNone(
- re.search(MERGED_PATTERN, output.get_data(as_text=True))
- )
- @patch("pagure.lib.notify.send_email")
- def test_merge_request_pull_merge_with_comment(self, send_email):
- """ Test the merge_request_pull endpoint with a merge PR. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=None,
- branch_from="feature",
- mtype="merge",
- )
- self.session = pagure.lib.query.create_session(self.dbpath)
- request = pagure.lib.query.search_pull_requests(
- self.session, project_id=1, requestid=1
- )
- self.assertEqual(len(request.comments), 0)
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- csrf_token = self.get_csrf(output=output)
- data = {
- "csrf_token": csrf_token,
- "comment": "Thanks for the review and the suggestions!",
- }
- # Merge
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - Pagure</title>",
- output.get_data(as_text=True),
- )
- # Check if the closing notification was added
- output = self.app.get("/test/pull-request/1")
- output_text = output.get_data(as_text=True)
- self.assertIsNotNone(re.search(MERGED_PATTERN, output_text))
- self.assertIn(
- "Thanks for the review and the suggestions!", output_text
- )
- self.session = pagure.lib.query.create_session(self.dbpath)
- request = pagure.lib.query.search_pull_requests(
- self.session, project_id=1, requestid=1
- )
- self.assertEqual(len(request.comments), 2)
- @patch("pagure.lib.notify.send_email")
- def test_merge_request_pull_merge_with_delete_branch(self, send_email):
- """ Test the merge_request_pull endpoint with a merge PR and delete source branch. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=None,
- branch_from="feature-branch",
- mtype="merge",
- )
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- data = {
- "csrf_token": self.get_csrf(output=output),
- "delete_branch": True,
- }
- # Merge
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature-branch branch - test\n - Pagure</title>",
- output_text,
- )
- # Check the branch is not mentioned
- self.assertNotIn(
- '<a class="" href="/test/branch/feature-branch"', output_text
- )
- @patch("pagure.lib.notify.send_email")
- def test_merge_request_pull_conflicts(self, send_email):
- """ Test the merge_request_pull endpoint with a conflicting PR. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=None,
- branch_from="feature",
- mtype="conflicts",
- )
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- csrf_token = self.get_csrf(output=output)
- data = {"csrf_token": csrf_token}
- # Merge conflicts
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertIn("Merge conflicts!", output_text)
- @patch("pagure.lib.notify.send_email")
- def test_merge_request_pull_conflicts_with_delete_branch(self, send_email):
- """ Test the merge_request_pull endpoint with a conflicting PR and request deletion of branch. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=None,
- branch_from="feature-branch",
- mtype="conflicts",
- )
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- data = {
- "csrf_token": self.get_csrf(output=output),
- "delete_branch": True,
- }
- # Merge conflicts
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n'
- ' <span class="text-success font-weight-bold">#1</span>\n'
- ' <span class="font-weight-bold">\n'
- " PR from the feature-branch branch\n",
- output_text,
- )
- self.assertIn("Merge conflicts!", output_text)
- # Check the branch still exists
- output = self.app.get("/test/branches")
- self.assertIn("feature-branch", output.get_data(as_text=True))
- @patch("pagure.lib.notify.send_email")
- def test_merge_request_pull_nochange(self, send_email):
- """ Test the merge_request_pull endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=None,
- branch_from="master",
- mtype="nochanges",
- )
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- csrf_token = self.get_csrf(output=output)
- data = {"csrf_token": csrf_token}
- # Nothing to merge
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "Nothing to do, changes were already merged", output_text
- )
- # Check if the closing notification was added
- output = self.app.get("/test/pull-request/1")
- output_text = output.get_data(as_text=True)
- self.assertIsNotNone(re.search(MERGED_PATTERN, output_text))
- @patch("pagure.lib.notify.send_email")
- def test_request_pull_close(self, send_email):
- """ Test the request_pull endpoint with a closed PR. """
- send_email.return_value = True
- self.test_merge_request_pull_FF()
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIsNotNone(re.search(MERGED_PATTERN, output_text))
- self.assertIn(
- 'title="View file as of 2a552b">sources</a>', output_text
- )
- @patch("pagure.lib.notify.send_email")
- def test_request_pull_disabled(self, send_email):
- """ Test the request_pull endpoint with PR disabled. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- # Project w/o pull-request
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- settings = repo.settings
- settings["pull_requests"] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 404)
- @patch("pagure.lib.notify.send_email")
- @patch("pagure.lib.git.update_pull_ref")
- def test_request_pull_empty_repo(self, send_email, update_pull_ref):
- """ Test the request_pull endpoint against an empty repo. """
- # Mock update_pull_ref or the repo won't be empty anymore
- # (the PR will have been pushed to refs/pull)
- send_email.return_value = True
- tests.create_projects(self.session)
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name="test",
- description="test project #1",
- hook_token="aaabbb",
- is_fork=True,
- parent_id=1,
- )
- self.session.add(item)
- self.session.commit()
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- tests.create_projects_git(
- os.path.join(self.path, "repos", "forks", "foo"), bare=True
- )
- # Create a git repo to play with
- gitrepo = os.path.join(self.path, "repos", "test.git")
- self.assertFalse(os.path.exists(gitrepo))
- os.makedirs(gitrepo)
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Create a fork of this repo
- newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
- gitrepo = os.path.join(self.path, "repos", "forks", "foo", "test.git")
- new_repo = pygit2.clone_repository(gitrepo, newpath)
- # Edit the sources file again
- with open(os.path.join(newpath, "sources"), "w") as stream:
- stream.write("foo\n bar\nbaz\n boose")
- new_repo.index.add("sources")
- new_repo.index.write()
- # Commits the files added
- tree = new_repo.index.write_tree()
- author = pygit2.Signature("Alice Author", "alice@authors.tld")
- committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
- new_repo.create_commit(
- "refs/heads/feature",
- author,
- committer,
- "A commit on branch feature",
- tree,
- [],
- )
- refname = "refs/heads/feature:refs/heads/feature"
- ori_remote = new_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- # Create a PR for these changes
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=item,
- branch_from="feature",
- repo_to=project,
- branch_to="master",
- title="PR from the feature branch",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, "PR from the feature branch")
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertTrue(output_text.count('<span class="commitdate"'), 1)
- self.assertTrue(update_pull_ref.called)
- shutil.rmtree(newpath)
- @patch("pagure.lib.notify.send_email")
- def test_request_pull_empty_fork(self, send_email):
- """ Test the request_pull endpoint from an empty fork. """
- send_email.return_value = True
- tests.create_projects(self.session)
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name="test",
- description="test project #1",
- hook_token="aaabbb",
- is_fork=True,
- parent_id=1,
- )
- self.session.add(item)
- self.session.commit()
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- tests.create_projects_git(
- os.path.join(self.path, "repos", "forks", "foo"), bare=True
- )
- # Create a git repo to play with
- gitrepo = os.path.join(self.path, "repos", "test.git")
- self.assertFalse(os.path.exists(gitrepo))
- os.makedirs(gitrepo)
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Create a fork of this repo
- newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
- gitrepo = os.path.join(self.path, "repos", "forks", "foo", "test.git")
- new_repo = pygit2.clone_repository(gitrepo, newpath)
- # Create a PR for these "changes" (there are none, both repos are
- # empty)
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=item,
- branch_from="feature",
- repo_to=project,
- branch_to="master",
- title="PR from the feature branch",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, "PR from the feature branch")
- output = self.app.get("/test/pull-request/1", follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - Pagure</title>",
- output_text,
- )
- self.assertIn(
- "Fork is empty, there are no "
- "commits to create a pull request with",
- output_text,
- )
- shutil.rmtree(newpath)
- @patch("pagure.lib.notify.send_email")
- def test_request_pulls_order(self, send_email):
- """Test the request_pulls
- i.e Make sure that the results are displayed
- in the order required by the user"""
- send_email.return_value = True
- # Initially no project
- output = self.app.get("/test/pull-requests")
- self.assertEqual(output.status_code, 404)
- tests.create_projects(self.session)
- tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- item = pagure.lib.model.Project(
- user_id=2,
- name="test",
- description="test project #1",
- hook_token="aaabbb",
- is_fork=True,
- parent_id=1,
- )
- self.session.add(item)
- self.session.commit()
- # create PR's to play with
- # PR-1
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_to=repo,
- repo_from=item,
- branch_from="feature",
- branch_to="master",
- title="PR from the feature branch",
- user="pingou",
- status="Open",
- )
- self.session.commit()
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, "PR from the feature branch")
- # PR-2
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_to=repo,
- branch_to="master",
- branch_from="feature",
- repo_from=item,
- title="test PR",
- user="pingou",
- status="Open",
- )
- self.session.commit()
- self.assertEqual(req.title, "test PR")
- # PR-3
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_to=repo,
- branch_from="feature",
- branch_to="master",
- repo_from=item,
- title="test Invalid PR",
- user="pingou",
- status="Closed",
- )
- self.session.commit()
- self.assertEqual(req.title, "test Invalid PR")
- # PR-4
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_to=repo,
- branch_from="feature",
- title="test PR for sort",
- repo_from=item,
- user="pingou",
- branch_to="master",
- status="Open",
- )
- self.session.commit()
- self.assertEqual(req.title, "test PR for sort")
- # sort by last_updated
- output = self.app.get("/test/pull-requests?order_key=last_updated")
- output_text = output.get_data(as_text=True)
- tr_elements = re.findall(
- '<div class="request-row list-group-item list-group-item-action ">(.*?)</div><!--end request-row-->',
- output_text,
- re.M | re.S,
- )
- self.assertEqual(output.status_code, 200)
- # Make sure that issue four is first since it was modified last
- self.assertIn('href="/test/pull-request/4"', tr_elements[0])
- self.assertIn('href="/test/pull-request/2"', tr_elements[1])
- self.assertIn('href="/test/pull-request/1"', tr_elements[2])
- pr_one = pagure.lib.query.search_pull_requests(
- self.session, project_id=1, requestid=1
- )
- pr_one.updated_on = datetime.utcnow() + timedelta(seconds=2)
- self.session.add(pr_one)
- self.session.commit()
- # sort by last_updated
- output = self.app.get("/test/pull-requests?order_key=last_updated")
- output_text = output.get_data(as_text=True)
- tr_elements = re.findall(
- '<div class="request-row list-group-item list-group-item-action ">(.*?)</div><!--end request-row-->',
- output_text,
- re.M | re.S,
- )
- self.assertEqual(output.status_code, 200)
- # Make sure that PR four is first since it was modified last
- self.assertIn('href="/test/pull-request/1"', tr_elements[0])
- # Make sure that PR two is second since it was modified second
- self.assertIn('href="/test/pull-request/4"', tr_elements[1])
- # Make sure that PR one is last since it was modified first
- self.assertIn('href="/test/pull-request/2"', tr_elements[2])
- # Now query so that the results are ascending
- output = self.app.get(
- "/test/pull-requests?" "order_key=last_updated&order=asc"
- )
- output_text = output.get_data(as_text=True)
- tr_elements = re.findall(
- '<div class="request-row list-group-item list-group-item-action ">(.*?)</div><!--end request-row-->',
- output_text,
- re.M | re.S,
- )
- self.assertIn('href="/test/pull-request/2"', tr_elements[0])
- self.assertIn('href="/test/pull-request/4"', tr_elements[1])
- self.assertIn('href="/test/pull-request/1"', tr_elements[2])
- # check that search_pattern argument works
- output = self.app.get("/test/pull-requests?search_pattern=feature")
- output_text = output.get_data(as_text=True)
- tr_elements = re.findall(
- '<div class="request-row list-group-item list-group-item-action ">(.*?)</div><!--end request-row-->',
- output_text,
- re.M | re.S,
- )
- self.assertIn('href="/test/pull-request/1"', tr_elements[0])
- self.assertEqual(len(tr_elements), 1)
- output = self.app.get("/test/pull-requests?search_pattern=PR")
- output_text = output.get_data(as_text=True)
- tr_elements = re.findall(
- '<div class="request-row list-group-item list-group-item-action ">(.*?)</div><!--end request-row-->',
- output_text,
- re.M | re.S,
- )
- self.assertIn('href="/test/pull-request/4"', tr_elements[0])
- self.assertIn('href="/test/pull-request/2"', tr_elements[1])
- self.assertIn('href="/test/pull-request/1"', tr_elements[2])
- self.assertEqual(len(tr_elements), 3)
- output = self.app.get("/test/pull-requests?search_pattern=*PR")
- output_text = output.get_data(as_text=True)
- tr_elements = re.findall(
- '<div class="request-row list-group-item list-group-item-action ">(.*?)</div><!--end request-row-->',
- output_text,
- re.M | re.S,
- )
- self.assertEqual(len(tr_elements), 1)
- self.assertIn('href="/test/pull-request/2"', tr_elements[0])
- @patch("pagure.lib.notify.send_email")
- def test_request_pulls(self, send_email):
- """ Test the request_pulls endpoint. """
- send_email.return_value = True
- # No such project
- output = self.app.get("/test/pull-requests")
- self.assertEqual(output.status_code, 404)
- tests.create_projects(self.session)
- tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
- output = self.app.get("/test/pull-requests")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<span class="fa fa-fw fa-arrow-circle-down"></span> 0 Open PRs\n',
- output_text,
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- output = self.app.get("/test/pull-requests")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<span class="fa fa-fw fa-arrow-circle-down"></span> 1 Open PRs\n',
- output_text,
- )
- output = self.app.get("/test/pull-requests?status=1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<span class="fa fa-fw fa-arrow-circle-down"></span> 1 Open PRs\n',
- output_text,
- )
- output = self.app.get("/test/pull-requests?status=true")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<span class="fa fa-fw fa-arrow-circle-down"></span> 1 Open PRs\n',
- output_text,
- )
- output = self.app.get("/test/pull-requests?status=Merged")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<span class="fa fa-fw fa-arrow-circle-down"></span> 0 Merged PRs\n',
- output_text,
- )
- output = self.app.get("/test/pull-requests?status=0")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<span class="fa fa-fw fa-arrow-circle-down"></span> 0 Merged PRs\n',
- output_text,
- )
- output = self.app.get("/test/pull-requests?status=Closed")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<span class="fa fa-fw fa-arrow-circle-down"></span> 0 Cancelled PRs\n',
- output_text,
- )
- # Project w/o pull-request
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- settings = repo.settings
- settings["pull_requests"] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.get("/test/pull-requests")
- self.assertEqual(output.status_code, 404)
- @patch("pagure.lib.notify.send_email")
- def test_request_pulls_filters_tags(self, send_email):
- """Test the requests_pull
- i.e Make sure that the results are filtered properly"""
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- # Create some tags to play with
- pagure.lib.query.new_tag(
- self.session, "tag-1", "tag-1 descripcion", "#ff0000", repo.id
- )
- pagure.lib.query.new_tag(
- self.session, "tag-2", "tag-2 description", "#00ff00", repo.id
- )
- pagure.lib.query.new_tag(
- self.session, "tag-3", "tag-3 description", "#0000ff", repo.id
- )
- fork = pagure.lib.model.Project(
- user_id=2,
- name="test",
- description="test project #1",
- hook_token="aaabbb",
- is_fork=True,
- parent_id=1,
- )
- self.session.add(fork)
- self.session.commit()
- # Create PR's to play with
- # PR-1, tags: tag-1, tag-3
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_to=repo,
- repo_from=fork,
- branch_from="feature",
- branch_to="master",
- title="First PR",
- user="pingou",
- status="Open",
- )
- pagure.lib.query.update_tags(
- self.session, obj=req, tags=["tag-1", "tag-3"], username="pingou"
- )
- self.session.commit()
- # PR-2, tags: tag-2, tag-3
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_to=repo,
- repo_from=fork,
- branch_from="feature",
- branch_to="master",
- title="Second PR",
- user="pingou",
- status="Open",
- )
- pagure.lib.query.update_tags(
- self.session, obj=req, tags=["tag-2", "tag-3"], username="pingou"
- )
- self.session.commit()
- # PR-3 closed, tags: tag-1, tag-3
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_to=repo,
- repo_from=fork,
- branch_from="feature",
- branch_to="master",
- title="Third PR",
- user="pingou",
- status="Closed",
- )
- pagure.lib.query.update_tags(
- self.session, obj=req, tags=["tag-1", "tag-3"], username="pingou"
- )
- self.session.commit()
- # PR-4 closed, tags: tag-1, tag-2
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_to=repo,
- repo_from=fork,
- branch_from="feature",
- branch_to="master",
- title="Fourth PR",
- user="pingou",
- status="Closed",
- )
- pagure.lib.query.update_tags(
- self.session, obj=req, tags=["tag-1", "tag-2"], username="pingou"
- )
- self.session.commit()
- # filter by 'tag-1'
- output = self.app.get("/test/pull-requests?tags=tag-1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- tr_elements = re.findall(
- '<div class="request-row list-group-item list-group-item-action ">(.*?)</div><!--end request-row-->',
- output_text,
- re.M | re.S,
- )
- self.assertEqual(1, len(tr_elements))
- self.assertIn('href="/test/pull-request/1', tr_elements[0])
- # filter by '!tag-1'
- output = self.app.get("/test/pull-requests?tags=!tag-1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- tr_elements = re.findall(
- '<div class="request-row list-group-item list-group-item-action ">(.*?)</div><!--end request-row-->',
- output_text,
- re.M | re.S,
- )
- self.assertEqual(1, len(tr_elements))
- self.assertIn('href="/test/pull-request/2', tr_elements[0])
- # filter by 'tag-2' and 'tag-3'
- output = self.app.get("/test/pull-requests?tags=tag2&tags=tag-3")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- tr_elements = re.findall(
- '<div class="request-row list-group-item list-group-item-action ">(.*?)</div><!--end request-row-->',
- output_text,
- re.M | re.S,
- )
- self.assertEqual(2, len(tr_elements))
- self.assertIn('href="/test/pull-request/2', tr_elements[0])
- self.assertIn('href="/test/pull-request/1', tr_elements[1])
- # filter by '!tag-3'
- output = self.app.get("/test/pull-requests?tags=!tag-3")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- tr_elements = re.findall(
- '<div class="request-row list-group-item list-group-item-action ">(.*?)</div><!--end request-row-->',
- output_text,
- re.M | re.S,
- )
- self.assertEqual(0, len(tr_elements))
- # filter by tag-2 on Closed prs
- output = self.app.get("/test/pull-requests?status=Closed&tags=tag-2")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- tr_elements = re.findall(
- '<div class="request-row list-group-item list-group-item-action ">(.*?)</div><!--end request-row-->',
- output_text,
- re.M | re.S,
- )
- self.assertEqual(1, len(tr_elements))
- self.assertIn('href="/test/pull-request/4', tr_elements[0])
- # filter by !tag-2 on Closed prs
- output = self.app.get("/test/pull-requests?status=Closed&tags=!tag-2")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- tr_elements = re.findall(
- '<div class="request-row list-group-item list-group-item-action ">(.*?)</div><!--end request-row-->',
- output_text,
- re.M | re.S,
- )
- self.assertEqual(1, len(tr_elements))
- self.assertIn('href="/test/pull-request/3', tr_elements[0])
- # filter by tag-2 on all the prs
- output = self.app.get("/test/pull-requests?status=all&tags=tag-2")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- tr_elements = re.findall(
- '<div class="request-row list-group-item list-group-item-action ">(.*?)</div><!--end request-row-->',
- output_text,
- re.M | re.S,
- )
- self.assertEqual(2, len(tr_elements))
- self.assertIn('href="/test/pull-request/4', tr_elements[0])
- self.assertIn('href="/test/pull-request/2', tr_elements[1])
- @patch("pagure.lib.notify.send_email")
- def test_request_pull_patch(self, send_email):
- """ Test the request_pull_patch endpoint. """
- send_email.return_value = True
- output = self.app.get("/test/pull-request/1.patch")
- self.assertEqual(output.status_code, 404)
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=None,
- branch_from="feature",
- mtype="merge",
- )
- output = self.app.get("/test/pull-request/100.patch")
- self.assertEqual(output.status_code, 404)
- output = self.app.get("/test/pull-request/1.patch")
- self.assertEqual(output.status_code, 200)
- npatch = []
- for row in output.get_data(as_text=True).split("\n"):
- if row.startswith("Date:"):
- continue
- if row.startswith("From "):
- row = row.split(" ", 2)[2]
- npatch.append(row)
- exp = r"""Mon Sep 17 00:00:00 2001
- From: Alice Author <alice@authors.tld>
- Subject: A commit on branch feature
- More information
- ---
- diff --git a/.gitignore b/.gitignore
- new file mode 100644
- index 0000000..e4e5f6c
- --- /dev/null
- +++ b/.gitignore
- @@ -0,0 +1 @@
- +*~
- \ No newline at end of file
- diff --git a/sources b/sources
- index 9f44358..2a552bb 100644
- --- a/sources
- +++ b/sources
- @@ -1,2 +1,4 @@
- foo
- - bar
- \ No newline at end of file
- + bar
- +baz
- + boose
- \ No newline at end of file
- """
- patch = "\n".join(npatch)
- # print patch
- self.assertEqual(patch, exp)
- # Project w/o pull-request
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- settings = repo.settings
- settings["pull_requests"] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.get("/test/pull-request/1.patch")
- self.assertEqual(output.status_code, 404)
- @patch("pagure.lib.notify.send_email")
- def test_request_pull_diff(self, send_email):
- """ Test the request_pull_patch endpoint. """
- send_email.return_value = True
- output = self.app.get("/test/pull-request/1.diff")
- self.assertEqual(output.status_code, 404)
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=None,
- branch_from="feature",
- mtype="merge",
- )
- output = self.app.get("/test/pull-request/100.diff")
- self.assertEqual(output.status_code, 404)
- output = self.app.get("/test/pull-request/1.diff")
- self.assertEqual(output.status_code, 200)
- exp = r"""diff --git a/.gitignore b/.gitignore
- new file mode 100644
- index 0000000..e4e5f6c
- --- /dev/null
- +++ b/.gitignore
- @@ -0,0 +1 @@
- +*~
- \ No newline at end of file
- diff --git a/sources b/sources
- index 9f44358..2a552bb 100644
- --- a/sources
- +++ b/sources
- @@ -1,2 +1,4 @@
- foo
- - bar
- \ No newline at end of file
- + bar
- +baz
- + boose
- \ No newline at end of file
- """
- self.assertEqual(output.get_data(as_text=True), exp)
- # Project w/o pull-request
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- settings = repo.settings
- settings["pull_requests"] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.get("/test/pull-request/1.diff")
- self.assertEqual(output.status_code, 404)
- @patch("pagure.lib.notify.send_email")
- def test_request_pull_patch_close(self, send_email):
- """ Test the request_pull_patch endpoint with a closed PR. """
- send_email.return_value = True
- self.test_merge_request_pull_FF()
- output = self.app.get("/test/pull-request/1.patch")
- self.assertEqual(output.status_code, 200)
- npatch = []
- for row in output.get_data(as_text=True).split("\n"):
- if row.startswith("Date:"):
- continue
- if row.startswith("From "):
- row = row.split(" ", 2)[2]
- npatch.append(row)
- exp = r"""Mon Sep 17 00:00:00 2001
- From: Alice Author <alice@authors.tld>
- Subject: A commit on branch feature
- More information
- ---
- diff --git a/sources b/sources
- index 9f44358..2a552bb 100644
- --- a/sources
- +++ b/sources
- @@ -1,2 +1,4 @@
- foo
- - bar
- \ No newline at end of file
- + bar
- +baz
- + boose
- \ No newline at end of file
- """
- patch = "\n".join(npatch)
- # print patch
- self.assertEqual(patch, exp)
- @patch("pagure.lib.notify.send_email")
- @patch("pagure.lib.git.update_pull_ref")
- def test_request_pull_patch_empty_repo(self, send_email, update_pull_ref):
- """ Test the request_pull_patch endpoint against an empty repo. """
- # Mock update_pull_ref or the repo won't be empty anymore
- # (the PR will have been pushed to refs/pull)
- send_email.return_value = True
- tests.create_projects(self.session)
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name="test",
- description="test project #1",
- hook_token="aaabbb",
- is_fork=True,
- parent_id=1,
- )
- self.session.add(item)
- self.session.commit()
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- tests.create_projects_git(
- os.path.join(self.path, "repos", "forks", "foo"), bare=True
- )
- # Create a git repo to play with
- gitrepo = os.path.join(self.path, "repos", "test.git")
- self.assertFalse(os.path.exists(gitrepo))
- os.makedirs(gitrepo)
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Create a fork of this repo
- newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
- gitrepo = os.path.join(self.path, "repos", "forks", "foo", "test.git")
- new_repo = pygit2.clone_repository(gitrepo, newpath)
- # Edit the sources file again
- with open(os.path.join(newpath, "sources"), "w") as stream:
- stream.write("foo\n bar\nbaz\n boose")
- new_repo.index.add("sources")
- new_repo.index.write()
- # Commits the files added
- tree = new_repo.index.write_tree()
- author = pygit2.Signature("Alice Author", "alice@authors.tld")
- committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
- new_repo.create_commit(
- "refs/heads/feature",
- author,
- committer,
- "A commit on branch feature",
- tree,
- [],
- )
- refname = "refs/heads/feature:refs/heads/feature"
- ori_remote = new_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- # Create a PR for these "changes" (there are none, both repos are
- # empty)
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=item,
- branch_from="feature",
- repo_to=project,
- branch_to="master",
- title="PR from the feature branch",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, "PR from the feature branch")
- output = self.app.get(
- "/test/pull-request/1.patch", follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- npatch = []
- for row in output.get_data(as_text=True).split("\n"):
- if row.startswith("Date:"):
- continue
- if row.startswith("From "):
- row = row.split(" ", 2)[2]
- npatch.append(row)
- exp = r"""Mon Sep 17 00:00:00 2001
- From: Alice Author <alice@authors.tld>
- Subject: A commit on branch feature
- ---
- diff --git a/sources b/sources
- new file mode 100644
- index 0000000..2a552bb
- --- /dev/null
- +++ b/sources
- @@ -0,0 +1,4 @@
- +foo
- + bar
- +baz
- + boose
- \ No newline at end of file
- """
- patch = "\n".join(npatch)
- # print patch
- self.assertEqual(patch, exp)
- shutil.rmtree(newpath)
- @patch("pagure.lib.notify.send_email")
- def test_request_pull_patch_empty_fork(self, send_email):
- """ Test the request_pull_patch endpoint from an empty fork. """
- send_email.return_value = True
- tests.create_projects(self.session)
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name="test",
- description="test project #1",
- hook_token="aaabbb",
- is_fork=True,
- parent_id=1,
- )
- self.session.add(item)
- self.session.commit()
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- tests.create_projects_git(
- os.path.join(self.path, "repos", "forks", "foo"), bare=True
- )
- # Create a git repo to play with
- gitrepo = os.path.join(self.path, "repos", "test.git")
- self.assertFalse(os.path.exists(gitrepo))
- os.makedirs(gitrepo)
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Create a fork of this repo
- newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
- gitrepo = os.path.join(self.path, "repos", "forks", "foo", "test.git")
- new_repo = pygit2.clone_repository(gitrepo, newpath)
- # Create a PR for these "changes" (there are none, both repos are
- # empty)
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=item,
- branch_from="feature",
- repo_to=project,
- branch_to="master",
- title="PR from the feature branch",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, "PR from the feature branch")
- output = self.app.get(
- "/test/pull-request/1.patch", follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn("<title>Overview - test - Pagure</title>", output_text)
- self.assertIn(
- "Fork is empty, there are no "
- "commits to create a pull request with",
- output_text,
- )
- shutil.rmtree(newpath)
- @patch("pagure.lib.notify.send_email")
- def test_close_request_pull(self, send_email):
- """ Test the close_request_pull endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=None,
- branch_from="feature",
- mtype="merge",
- )
- user = tests.FakeUser()
- with tests.user_set(self.app.application, user):
- output = self.app.post("/test/pull-request/close/1")
- self.assertEqual(output.status_code, 302)
- output = self.app.post(
- "/test/pull-request/close/1", follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Overview - test - Pagure</title>", output_text
- )
- self.assertIn("Invalid input submitted", output_text)
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- csrf_token = self.get_csrf(output=output)
- data = {"csrf_token": csrf_token}
- # Invalid project
- output = self.app.post(
- "/foo/pull-request/close/1", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 404)
- # Invalid PR id
- output = self.app.post(
- "/test/pull-request/close/100",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- # Invalid user for this project
- output = self.app.post(
- "/test/pull-request/close/1", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 403)
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- # Project w/o pull-request
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings = repo.settings
- settings["pull_requests"] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/close/1", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 404)
- # Project w/ pull-request
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings = repo.settings
- settings["pull_requests"] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/close/1", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Overview - test - Pagure</title>", output_text
- )
- self.assertIn("Pull request closed!", output_text)
- @patch("pagure.lib.notify.send_email")
- def test_reopen_request_pull(self, send_email):
- """ Test the reopen_request_pull endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=None,
- branch_from="feature",
- mtype="merge",
- )
- user = tests.FakeUser()
- with tests.user_set(self.app.application, user):
- output = self.app.post("/test/pull-request/1/reopen")
- self.assertEqual(output.status_code, 302)
- output = self.app.post(
- "/test/pull-request/1/reopen", follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - Pagure</title>",
- output_text,
- )
- self.assertIn(
- #'Pull request reopened!',
- 'return window.confirm("Are you sure you want to reopen this requested pull?")',
- output_text,
- )
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- csrf_token = self.get_csrf(output=output)
- data = {"csrf_token": csrf_token}
- # Invalid project
- output = self.app.post(
- "/foo/pull-request/1/reopen", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 404)
- # Invalid PR id
- output = self.app.post(
- "/test/pull-request/100/reopen",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- # Invalid user for this project
- output = self.app.post(
- "/test/pull-request/1/reopen", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 403)
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- # Project w/o pull-request
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings = repo.settings
- settings["pull_requests"] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/1/reopen", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 404)
- # Project w/ pull-request
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings = repo.settings
- settings["pull_requests"] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/cancel/1", data=data, follow_redirects=True
- )
- output = self.app.post(
- "/test/pull-request/1/reopen", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- 'return window.confirm("Are you sure you want to reopen this requested pull?")',
- output_text,
- )
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_update_pull_requests_assign(self):
- """ Test the update_pull_requests endpoint when assigning a PR.
- """
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- # No such project
- output = self.app.post("/foo/pull-request/1/update")
- self.assertEqual(output.status_code, 404)
- output = self.app.post("/test/pull-request/100/update")
- self.assertEqual(output.status_code, 404)
- # Invalid input
- output = self.app.post(
- "/test/pull-request/1/update", follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertNotIn("Request assigned", output_text)
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- csrf_token = self.get_csrf(output=output)
- data = {"user": "pingou"}
- # No CSRF
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertNotIn("Request assigned", output_text)
- # Invalid assignee
- data = {"csrf_token": csrf_token, "user": "bar"}
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertIn("No user "bar" found", output_text)
- # Assign the PR
- data = {"csrf_token": csrf_token, "user": "pingou"}
- user.username = "foo"
- with tests.user_set(self.app.application, user):
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 403)
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertIn("Request assigned", output_text)
- # Pull-Request closed
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- req = repo.requests[0]
- req.status = "Closed"
- req.closed_by_in = 1
- self.session.add(req)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- # Project w/o pull-request
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings = repo.settings
- settings["pull_requests"] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 404)
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_update_pull_requests_tag(self):
- """ Test the update_pull_requests endpoint when tagging a PR.
- """
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- csrf_token = self.get_csrf(output=output)
- data = {"tag": "black"}
- # No CSRF
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertNotIn("Request assigned", output_text)
- # Tag the PR
- data = {"csrf_token": csrf_token, "tag": "black"}
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertIn("Pull-request tagged with: black", output_text)
- self.assertIn(
- 'title="comma separated list of tags"\n '
- 'value="black" />',
- output_text,
- )
- # Try as another user
- user.username = "foo"
- with tests.user_set(self.app.application, user):
- # Tag the PR
- data = {"csrf_token": csrf_token, "tag": "blue, yellow"}
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 403)
- # Make the PR be from foo
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- req = repo.requests[0]
- req.user_id = 2
- self.session.add(req)
- self.session.commit()
- # Re-try to tag the PR
- data = {"csrf_token": csrf_token, "tag": "blue, yellow"}
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- soup = BeautifulSoup(output.get_data(as_text=True), "html.parser")
- self.assertEqual(
- soup.find("title").string,
- "PR#1: PR from the feature branch - test\n - Pagure",
- )
- self.assertIn(
- "Pull-request **un**tagged with: black",
- output.get_data(as_text=True),
- )
- self.assertIn(
- "Pull-request tagged with: blue, yellow",
- output.get_data(as_text=True),
- )
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- # Pull-Request closed
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- req = repo.requests[0]
- req.status = "Closed"
- req.closed_by_in = 1
- self.session.add(req)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- # Project w/o pull-request
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings = repo.settings
- settings["pull_requests"] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 404)
- @patch("pagure.lib.notify.send_email")
- def test_fork_project(self, send_email):
- """ Test the fork_project endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- for folder in ["docs", "tickets", "requests", "repos"]:
- tests.create_projects_git(
- os.path.join(self.path, folder), bare=True
- )
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.post("/do_fork/test")
- self.assertEqual(output.status_code, 400)
- output = self.app.get("/new/")
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- "<strong>Create new Project</strong>",
- output.get_data(as_text=True),
- )
- csrf_token = self.get_csrf(output=output)
- data = {"csrf_token": csrf_token}
- output = self.app.post(
- "/do_fork/foo", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 404)
- user.username = "foo"
- with tests.user_set(self.app.application, user):
- output = self.app.post("/do_fork/test")
- self.assertEqual(output.status_code, 400)
- data = {"csrf_token": csrf_token}
- output = self.app.post(
- "/do_fork/test", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- @patch("pagure.lib.notify.send_email")
- def test_new_request_pull_branch_space(self, send_email):
- """ Test the new_request_pull endpoint. """
- send_email.return_value = True
- self.test_fork_project()
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- fork = pagure.lib.query.get_authorized_project(
- self.session, "test", user="foo"
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=fork,
- branch_from="feature",
- mtype="FF",
- )
- user = tests.FakeUser(username="pingou")
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/diff/master..foo bar")
- self.assertEqual(output.status_code, 400)
- output_text = output.get_data(as_text=True)
- self.assertIn("<p>Branch foo bar does not exist</p>", output_text)
- @patch("pagure.lib.notify.send_email")
- def test_new_request_pull(self, send_email):
- """ Test the new_request_pull endpoint. """
- send_email.return_value = True
- self.test_fork_project()
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- fork = pagure.lib.query.get_authorized_project(
- self.session, "test", user="foo"
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=fork,
- branch_from="feature",
- mtype="FF",
- )
- user = tests.FakeUser()
- user.username = "foo"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/foo/diff/master..feature")
- self.assertEqual(output.status_code, 404)
- output = self.app.get("/test/diff/master..foo")
- self.assertEqual(output.status_code, 400)
- output = self.app.get("/test/diff/foo..master")
- self.assertEqual(output.status_code, 400)
- output = self.app.get("/test/diff/feature..master")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Diff from master to feature - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<p class="error"> No commits found </p>', output_text
- )
- output = self.app.get("/test/diff/master..feature")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Diff from feature to master - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertNotIn(
- '<input type="submit" class="submit positive button" '
- 'value="Create">',
- output_text,
- )
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/diff/master..feature")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Create new Pull Request for master - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<input type="submit" class="btn btn-primary" value="Create Pull Request">\n',
- output_text,
- )
- # Check that we prefilled the input fields as expected:
- self.assertIn(
- '<input class="form-control" id="title" name="title" '
- 'placeholder="Pull Request Title" required="required" '
- 'type="text" value="A commit on branch feature">',
- output_text,
- )
- self.assertIn(
- """<textarea class="form-control" rows=8 id="initial_comment" name="initial_comment"
- placeholder="Describe your changes" tabindex=1>
- More information</textarea>
- <div id="preview" class="p-1">""",
- output_text,
- )
- self.assertIn(
- '<a class="dropdown-item branch_from_item pointer" '
- 'data-value="master"><span class="fa fa-random">'
- "</span> master</a>",
- output_text,
- )
- csrf_token = self.get_csrf(output=output)
- # Case 1 - Add an initial comment
- data = {
- "csrf_token": csrf_token,
- "title": "foo bar PR",
- "initial_comment": "Test Initial Comment",
- }
- output = self.app.post(
- "/test/diff/master..feature", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#2: foo bar PR - test\n - Pagure</title>",
- output_text,
- )
- self.assertIn("<p>Test Initial Comment</p>", output_text)
- self.assertEqual(output_text.count('title="PY C (pingou)"'), 2)
- # Test if the `open changed file icon` is displayed.
- self.assertIn(
- 'class="open_changed_file_icon_wrap"><span '
- 'class="fa fa-file-code-o fa-fw" '
- 'alt="Open changed file" title="Open changed file"></span>'
- "</a>",
- output_text,
- )
- # Case 2 - Add an empty initial comment
- data = {
- "csrf_token": csrf_token,
- "title": "foo bar PR",
- "initial_comment": "",
- }
- output = self.app.post(
- "/test/diff/master..feature", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#3: foo bar PR - test\n - Pagure</title>",
- output_text,
- )
- self.assertNotIn('<div id="comment-', output_text)
- @patch("pagure.lib.notify.send_email")
- def test_new_request_pull_filename_unicode(self, send_email):
- """ Test the new_request_pull endpoint. """
- send_email.return_value = True
- # Create the main project in the DB
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name="test",
- description="test project #1",
- hook_token="aaabbbccc",
- )
- item.close_status = [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ]
- self.session.add(item)
- self.session.commit()
- # Create the fork
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name="test",
- description="test project #1",
- hook_token="aaabbbcccdd",
- parent_id=1,
- is_fork=True,
- )
- item.close_status = [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ]
- self.session.add(item)
- self.session.commit()
- # Create two git repos, one has 6 commits, the other 4 of which only
- # 1 isn't present in the first repo
- gitrepo = os.path.join(self.path, "repos", "test.git")
- pygit2.init_repository(gitrepo, bare=True)
- gitrepo2 = os.path.join(
- self.path, "repos", "forks", "pingou", "test.git"
- )
- pygit2.init_repository(gitrepo2, bare=True)
- newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
- repopath = os.path.join(newpath, "test")
- clone_repo = pygit2.clone_repository(gitrepo, repopath)
- # Do 2 commits to the main repo
- for i in range(2):
- with open(os.path.join(repopath, "sources"), "w") as stream:
- stream.write("foo%s\n bar%s\n" % (i, i))
- clone_repo.index.add("sources")
- clone_repo.index.write()
- parents = []
- try:
- last_commit = clone_repo.revparse_single("HEAD")
- parents = [last_commit.oid.hex]
- except KeyError:
- pass
- # Commits the files added
- tree = clone_repo.index.write_tree()
- author = pygit2.Signature("Alice Author", "alice@authors.tld")
- committer = pygit2.Signature(
- "Cecil Committer", "cecil@committers.tld"
- )
- clone_repo.create_commit(
- "refs/heads/master", # the name of the reference to update
- author,
- committer,
- "Editing the file sources for testing #%s" % i,
- # binary string representing the tree object ID
- tree,
- # list of binary strings representing parents of the new commit
- parents,
- )
- # Push to the main repo
- refname = "refs/heads/master:refs/heads/master"
- ori_remote = clone_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- # Push to the fork repo
- remote = clone_repo.create_remote("pingou_fork", gitrepo2)
- PagureRepo.push(remote, refname)
- # Add 1 commits to the fork repo
- repopath = os.path.join(newpath, "pingou_test")
- clone_repo = pygit2.clone_repository(gitrepo2, repopath)
- with open(os.path.join(repopath, "soürces"), "w") as stream:
- stream.write("foo\n bar\n")
- clone_repo.index.add("soürces")
- clone_repo.index.write()
- with open(os.path.join(repopath, "fóß"), "w") as stream:
- stream.write("foo\n bar\n")
- clone_repo.index.add("fóß")
- clone_repo.index.write()
- last_commit = clone_repo.revparse_single("HEAD")
- # Commits the files added
- tree = clone_repo.index.write_tree()
- author = pygit2.Signature("Alice Author", "alice@authors.tld")
- committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
- last_commit = clone_repo.create_commit(
- "refs/heads/feature_foo", # the name of the reference to update
- author,
- committer,
- "New edition on side branch of the file sources for testing",
- # binary string representing the tree object ID
- tree,
- # list of binary strings representing parents of the new commit
- [last_commit.oid.hex],
- )
- # Push to the fork repo
- ori_remote = clone_repo.remotes[0]
- refname = "refs/heads/feature_foo:refs/heads/feature_foo"
- PagureRepo.push(ori_remote, refname)
- shutil.rmtree(newpath)
- # Create the PR between the two repos
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- forked_repo = pagure.lib.query.get_authorized_project(
- self.session, "test", user="pingou"
- )
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="feature_foo",
- repo_to=repo,
- branch_to="master",
- title="test pull-request",
- user="pingou",
- )
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, "test pull-request")
- user = tests.FakeUser(username="pingou")
- with tests.user_set(self.app.application, user):
- output = self.app.get("/fork/pingou/test/diff/master..feature_foo")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Create new Pull Request for master - fork/pingou/test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<input type="submit" class="btn btn-primary" value="Create Pull Request">\n',
- output_text,
- )
- # Check that we prefilled the input fields as expected:
- self.assertIn(
- '<input class="form-control" id="title" name="title" '
- 'placeholder="Pull Request Title" required="required" '
- 'type="text" value="New edition on side branch of the file '
- 'sources for testing">',
- output_text,
- )
- self.assertIn(
- '<a class="dropdown-item branch_from_item pointer" '
- 'data-value="master"><span class="fa fa-random">'
- "</span> master</a>",
- output_text,
- )
- csrf_token = self.get_csrf(output=output)
- # Case 1 - Add an initial comment
- data = {
- "csrf_token": csrf_token,
- "title": "foo bar PR",
- "initial_comment": "Test Initial Comment",
- }
- output = self.app.post(
- "/fork/pingou/test/diff/master..feature_foo",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#2: foo bar PR - test\n - Pagure</title>",
- output_text,
- )
- self.assertIn("<p>Test Initial Comment</p>", output_text)
- self.assertEqual(output_text.count('title="PY C (pingou)"'), 2)
- @patch("pagure.lib.notify.send_email")
- def test_new_request_pull_req_sign_off_view(self, send_email):
- """ Test the new_request_pull endpoint. """
- send_email.return_value = True
- self.test_fork_project()
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- fork = pagure.lib.query.get_authorized_project(
- self.session, "test", user="foo"
- )
- # Enforce Signed-of-by in the repo
- settings = repo.settings
- settings["Enforce_signed-off_commits_in_pull-request"] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- set_up_git_repo(
- self.session,
- self.path,
- new_project=fork,
- branch_from="feature",
- mtype="FF",
- )
- user = tests.FakeUser()
- user.username = "foo"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/diff/master..feature")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Diff from feature to master - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- "This project enforces the "
- "Signed-off-by statement on all commits",
- output_text,
- )
- self.assertNotIn(
- '<input type="submit" class="btn btn-primary" value="Create Pull Request">\n',
- output_text,
- )
- self.assertNotIn(
- "This repo enforces that "
- "all commits are signed off by their author.",
- output_text,
- )
- @patch("pagure.lib.notify.send_email")
- def test_new_request_pull_req_sign_off_submit(self, send_email):
- """ Test the new_request_pull endpoint. """
- send_email.return_value = True
- self.test_fork_project()
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- fork = pagure.lib.query.get_authorized_project(
- self.session, "test", user="foo"
- )
- # Enforce Signed-of-by in the repo
- settings = repo.settings
- settings["Enforce_signed-off_commits_in_pull-request"] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- set_up_git_repo(
- self.session,
- self.path,
- new_project=fork,
- branch_from="feature",
- mtype="FF",
- )
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/diff/master..feature")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Create new Pull Request for master - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- "This project enforces the "
- "Signed-off-by statement on all commits",
- output_text,
- )
- self.assertIn(
- '<input type="submit" class="btn btn-primary" value="Create Pull Request">\n',
- output_text,
- )
- csrf_token = self.get_csrf(output=output)
- # Try to create the PR
- data = {
- "csrf_token": csrf_token,
- "title": "foo bar PR",
- "initial_comment": "Test Initial Comment",
- }
- output = self.app.post(
- "/test/diff/master..feature", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Create new Pull Request for master - test\n - "
- "Pagure</title>",
- output_text,
- )
- # Flashed information message
- self.assertIn(
- "This project enforces the "
- "Signed-off-by statement on all commits",
- output_text,
- )
- # Flashed error message
- self.assertIn(
- "This repo enforces that "
- "all commits are signed off by their author.",
- output_text,
- )
- self.assertIn(
- '<input type="submit" class="btn btn-primary" value="Create Pull Request">\n',
- output_text,
- )
- @patch("pagure.lib.notify.send_email")
- def test_request_pull_commit_start_stop(self, send_email):
- """ Test the the commit start and stop of brand new PR. """
- send_email.return_value = True
- self.test_fork_project()
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- fork = pagure.lib.query.get_authorized_project(
- self.session, "test", user="foo"
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=fork,
- branch_from="feature",
- mtype="FF",
- )
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/diff/master..feature")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Create new Pull Request for master - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<input type="submit" class="btn btn-primary" value="Create Pull Request">\n',
- output_text,
- )
- csrf_token = self.get_csrf(output=output)
- # Case 1 - Add an initial comment
- data = {
- "csrf_token": csrf_token,
- "title": "foo bar PR",
- "initial_comment": "Test Initial Comment",
- }
- output = self.app.post(
- "/test/diff/master..feature", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#2: foo bar PR - test\n - Pagure</title>",
- output_text,
- )
- self.assertIn("<p>Test Initial Comment</p>", output_text)
- # Check if commit start and stop have been set for PR#2
- request = pagure.lib.query.search_pull_requests(
- self.session, project_id=1, requestid=2
- )
- self.assertIsNotNone(request.commit_start)
- self.assertIsNotNone(request.commit_stop)
- @patch("pagure.lib.notify.send_email")
- def test_new_request_pull_from_fork_branch(self, send_email):
- """ Test creating a fork to fork PR. """
- send_email.return_value = True
- # Create main repo with some content
- tests.create_projects(self.session)
- tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
- tests.add_content_git_repo(
- os.path.join(self.path, "repos", "test.git")
- )
- # Create fork repo with more content
- tests.create_projects(
- self.session, is_fork=True, hook_token_suffix="fork"
- )
- tests.create_projects_git(
- os.path.join(self.path, "repos", "forks", "pingou"), bare=True
- )
- tests.add_content_git_repo(
- os.path.join(self.path, "repos", "forks", "pingou", "test.git")
- )
- tests.add_readme_git_repo(
- os.path.join(self.path, "repos", "forks", "pingou", "test.git"),
- branch="feature",
- )
- tests.add_readme_git_repo(
- os.path.join(self.path, "repos", "forks", "pingou", "test.git"),
- branch="random_branch",
- )
- user = tests.FakeUser(username="pingou")
- with tests.user_set(self.app.application, user):
- data = {"csrf_token": self.get_csrf()}
- output = self.app.post(
- "/do_fork/test", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- # Check that Ralph's fork do exist
- output = self.app.get("/fork/pingou/test")
- self.assertEqual(output.status_code, 200)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- fork = pagure.lib.query.get_authorized_project(
- self.session, "test", user="ralph"
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=fork,
- branch_from="feature",
- mtype="FF",
- )
- # Try opening a pull-request
- output = self.app.get("/fork/pingou/test/diff/master..feature")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Create new Pull Request for master - "
- "fork/pingou/test\n - Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<input type="submit" class="btn btn-primary" value="Create Pull Request">\n',
- output_text,
- )
- self.assertIn(
- '<a class="dropdown-item branch_from_item pointer" '
- 'data-value="master"><span class="fa fa-random">'
- "</span> master</a>",
- output_text,
- )
- self.assertIn(
- '<a class="dropdown-item branch_from_item pointer" '
- 'data-value="random_branch"><span class="fa fa-random">'
- "</span> random_branch</a>",
- output_text,
- )
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_new_request_pull_from_fork_fixing_ticket(self):
- """ Test creating a fork to fork PR fixing a ticket. """
- # Create main repo with some content
- tests.create_projects(self.session)
- tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
- tests.add_content_git_repo(
- os.path.join(self.path, "repos", "test.git")
- )
- # Create fork repo with more content
- tests.create_projects(
- self.session, is_fork=True, hook_token_suffix="fork"
- )
- tests.create_projects_git(
- os.path.join(self.path, "repos", "forks", "pingou"), bare=True
- )
- tests.add_content_git_repo(
- os.path.join(self.path, "repos", "forks", "pingou", "test.git")
- )
- tests.add_readme_git_repo(
- os.path.join(self.path, "repos", "forks", "pingou", "test.git"),
- branch="feature",
- )
- tests.add_readme_git_repo(
- os.path.join(self.path, "repos", "forks", "pingou", "test.git"),
- branch="random_branch",
- )
- # Check relations before we create the PR
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- self.assertEqual(len(project.requests), 0)
- self.assertEqual(len(project.issues), 0)
- # Create issues to link to
- msg = pagure.lib.query.new_issue(
- session=self.session,
- repo=project,
- title="tést íssüé",
- content="We should work on this",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg.title, "tést íssüé")
- user = tests.FakeUser(username="pingou")
- with tests.user_set(self.app.application, user):
- csrf_token = self.get_csrf()
- data = {"csrf_token": csrf_token}
- output = self.app.post(
- "/do_fork/test", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- # Check that pingou's fork do exist
- output = self.app.get("/fork/pingou/test")
- self.assertEqual(output.status_code, 200)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- fork = pagure.lib.query.get_authorized_project(
- self.session, "test", user="ralph"
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=fork,
- branch_from="feature",
- mtype="FF",
- prid=2,
- )
- # Try opening a pull-request
- output = self.app.get("/fork/pingou/test/diff/master..feature")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Create new Pull Request for master - "
- "fork/pingou/test\n - Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<input type="submit" class="btn btn-primary" value="Create Pull Request">\n',
- output_text,
- )
- self.assertIn(
- '<a class="dropdown-item branch_from_item pointer" '
- 'data-value="master"><span class="fa fa-random">'
- "</span> master</a>",
- output_text,
- )
- self.assertIn(
- '<a class="dropdown-item branch_from_item pointer" '
- 'data-value="random_branch"><span class="fa fa-random">'
- "</span> random_branch</a>",
- output_text,
- )
- data = {
- "csrf_token": csrf_token,
- "title": "foo bar PR",
- "initial_comment": "Test Initial Comment\n\nFixes #1",
- }
- output = self.app.post(
- "/fork/pingou/test/diff/master..feature",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#3: foo bar PR - test\n - Pagure</title>",
- output_text,
- )
- self.assertIn(
- "<p>Test Initial Comment</p>\n<p>Fixes <a href", output_text
- )
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- self.assertEqual(len(project.requests), 2)
- self.assertEqual(len(project.requests[0].related_issues), 0)
- self.assertEqual(len(project.requests[1].related_issues), 1)
- self.assertEqual(len(project.issues), 1)
- self.assertEqual(len(project.issues[0].related_prs), 1)
- @patch("pagure.lib.notify.send_email")
- def test_new_request_pull_fork_to_fork_pr_disabled(self, send_email):
- """ Test creating a fork to fork PR. """
- send_email.return_value = True
- self.test_fork_project()
- # Create a 3rd user
- item = pagure.lib.model.User(
- user="ralph",
- fullname="Ralph bar",
- password="ralph_foo",
- default_email="ralph@bar.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=3, email="ralph@bar.com")
- self.session.add(item)
- self.session.commit()
- user = tests.FakeUser()
- user.username = "ralph"
- with tests.user_set(self.app.application, user):
- # Have Ralph fork, foo's fork of test
- output = self.app.get("/fork/foo/test")
- self.assertEqual(output.status_code, 200)
- output = self.app.post("/do_fork/fork/foo/test")
- self.assertEqual(output.status_code, 400)
- csrf_token = self.get_csrf()
- data = {"csrf_token": csrf_token}
- output = self.app.post(
- "/do_fork/fork/foo/test", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- # Check that Ralph's fork do exist
- output = self.app.get("/fork/ralph/test")
- self.assertEqual(output.status_code, 200)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- fork = pagure.lib.query.get_authorized_project(
- self.session, "test", user="ralph"
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=fork,
- branch_from="feature",
- mtype="FF",
- )
- # Try opening a pull-request
- output = self.app.get("/fork/ralph/test/diff/master..feature")
- self.assertEqual(output.status_code, 404)
- self.assertIn(
- "<p>No pull-request allowed on this project</p>",
- output.get_data(as_text=True),
- )
- @patch("pagure.lib.notify.send_email")
- def test_new_request_pull_fork_to_fork(self, send_email):
- """ Test creating a fork to fork PR. """
- send_email.return_value = True
- self.test_fork_project()
- # Create a 3rd user
- item = pagure.lib.model.User(
- user="ralph",
- fullname="Ralph bar",
- password="ralph_foo",
- default_email="ralph@bar.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=3, email="ralph@bar.com")
- self.session.add(item)
- self.session.commit()
- user = tests.FakeUser()
- user.username = "ralph"
- with tests.user_set(self.app.application, user):
- # Have Ralph fork, foo's fork of test
- output = self.app.get("/fork/foo/test")
- self.assertEqual(output.status_code, 200)
- output = self.app.post("/do_fork/fork/foo/test")
- self.assertEqual(output.status_code, 400)
- csrf_token = self.get_csrf()
- data = {"csrf_token": csrf_token}
- output = self.app.post(
- "/do_fork/fork/foo/test", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- # Check that Ralph's fork do exist
- output = self.app.get("/fork/ralph/test")
- self.assertEqual(output.status_code, 200)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- # Turn on pull-request on the fork
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test", user="foo"
- )
- settings = repo.settings
- settings["pull_requests"] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- # Add some content to the parent
- set_up_git_repo(
- self.session,
- self.path,
- new_project=repo,
- branch_from="master",
- mtype="FF",
- name_from=repo.fullname,
- )
- fork = pagure.lib.query.get_authorized_project(
- self.session, "test", user="ralph"
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=fork,
- branch_from="feature",
- mtype="FF",
- prid=2,
- name_from=fork.fullname,
- )
- # Try opening a pull-request
- output = self.app.get("/fork/ralph/test/diff/master..feature")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Create new Pull Request for master - fork/ralph/test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<input type="submit" class="btn btn-primary" value="Create Pull Request">\n',
- output_text,
- )
- csrf_token = self.get_csrf(output=output)
- # Case 1 - Add an initial comment
- data = {
- "csrf_token": csrf_token,
- "title": "foo bar PR",
- "initial_comment": "Test Initial Comment",
- }
- output = self.app.post(
- "/fork/ralph/test/diff/master..feature",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: foo bar PR - fork/foo/test\n - Pagure</title>",
- output_text,
- )
- self.assertIn("<p>Test Initial Comment</p>", output_text)
- @patch("pagure.lib.notify.send_email")
- def test_new_request_pull_fork_to_other_fork(self, send_email):
- """ Test creating a PR from fork to a fork of the same family. """
- send_email.return_value = True
- self.test_fork_project()
- # Create a 3rd user
- item = pagure.lib.model.User(
- user="ralph",
- fullname="Ralph bar",
- password="ralph_foo",
- default_email="ralph@bar.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=3, email="ralph@bar.com")
- self.session.add(item)
- self.session.commit()
- user = tests.FakeUser()
- user.username = "ralph"
- with tests.user_set(self.app.application, user):
- csrf_token = self.get_csrf()
- data = {"csrf_token": csrf_token}
- output = self.app.post(
- "/do_fork/test", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- # Check that Ralph's fork do exist
- output = self.app.get("/fork/ralph/test")
- self.assertEqual(output.status_code, 200)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- # Turn on pull-request on the fork
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test", user="foo"
- )
- settings = repo.settings
- settings["pull_requests"] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- # Add some content to the parents
- set_up_git_repo(
- self.session,
- self.path,
- new_project=repo,
- branch_from="master",
- mtype="FF",
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=repo,
- branch_from="master",
- mtype="FF",
- name_from=repo.fullname,
- prid=2,
- )
- fork = pagure.lib.query.get_authorized_project(
- self.session, "test", user="ralph"
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=fork,
- branch_from="feature",
- mtype="FF",
- prid=3,
- name_from=fork.fullname,
- )
- # Try opening a pull-request
- output = self.app.get(
- "/fork/ralph/test/diff/master..feature?project_to=fork/foo/test"
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Create new Pull Request for master - fork/ralph/test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<input type="submit" class="btn btn-primary" value="Create Pull Request">\n',
- output_text,
- )
- csrf_token = self.get_csrf(output=output)
- # Case 1 - Opening PR to fork/foo/test
- data = {
- "csrf_token": csrf_token,
- "title": "foo bar PR",
- "initial_comment": "Test Initial Comment",
- }
- output = self.app.post(
- "/fork/ralph/test/diff/master..feature?project_to=fork/foo/test",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: foo bar PR - fork/foo/test\n - Pagure</title>",
- output_text,
- )
- self.assertIn("<p>Test Initial Comment</p>", output_text)
- # Case 1 - Opening PR to parent repo, shows project_to works
- output = self.app.post(
- "/fork/ralph/test/diff/master..feature",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#4: foo bar PR - test\n - Pagure</title>",
- output_text,
- )
- self.assertIn("<p>Test Initial Comment</p>", output_text)
- @patch("pagure.lib.notify.send_email")
- def test_new_request_pull_fork_to_other_unrelated_fork(self, send_email):
- """ Test creating a PR from fork to fork that isn't from the same
- family.
- """
- send_email.return_value = True
- self.test_fork_project()
- # Create a 3rd user
- item = pagure.lib.model.User(
- user="ralph",
- fullname="Ralph bar",
- password="ralph_foo",
- default_email="ralph@bar.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=3, email="ralph@bar.com")
- self.session.add(item)
- self.session.commit()
- user = tests.FakeUser()
- user.username = "ralph"
- with tests.user_set(self.app.application, user):
- csrf_token = self.get_csrf()
- data = {"csrf_token": csrf_token}
- output = self.app.post(
- "/do_fork/test2", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- # Check that Ralph's fork do exist
- output = self.app.get("/fork/ralph/test2")
- self.assertEqual(output.status_code, 200)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- # Turn on pull-request on the fork
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test", user="foo"
- )
- settings = repo.settings
- settings["pull_requests"] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- # Add some content to the parent
- set_up_git_repo(
- self.session,
- self.path,
- new_project=repo,
- branch_from="master",
- mtype="FF",
- name_from=repo.fullname,
- )
- fork = pagure.lib.query.get_authorized_project(
- self.session, "test2", user="ralph"
- )
- set_up_git_repo(
- self.session,
- self.path,
- new_project=fork,
- branch_from="feature",
- mtype="FF",
- prid=2,
- name_from=fork.fullname,
- )
- # Case 1 - Opening PR to fork/foo/test
- data = {
- "csrf_token": csrf_token,
- "title": "foo bar PR",
- "initial_comment": "Test Initial Comment",
- }
- output = self.app.post(
- "/fork/ralph/test2/diff/master..feature?project_to=fork/foo/test",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 400)
- self.assertIn(
- "<p>fork/foo/test is not part of fork/ralph/test2's "
- "family</p>",
- output.get_data(as_text=True),
- )
- @patch("pagure.lib.notify.send_email")
- def test_new_request_pull_empty_repo(self, send_email):
- """ Test the new_request_pull endpoint against an empty repo. """
- send_email.return_value = True
- self.test_fork_project()
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- fork = pagure.lib.query.get_authorized_project(
- self.session, "test", user="foo"
- )
- # Create a git repo to play with
- gitrepo = os.path.join(self.path, "repos", "test.git")
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Create a fork of this repo
- newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
- gitrepo = os.path.join(self.path, "repos", "forks", "foo", "test.git")
- new_repo = pygit2.clone_repository(gitrepo, newpath)
- user = tests.FakeUser()
- user.username = "foo"
- with tests.user_set(self.app.application, user):
- output = self.app.get(
- "/fork/foo/test/diff/master..feature", follow_redirects=True
- )
- self.assertEqual(output.status_code, 400)
- self.assertIn(
- "<p>Fork is empty, there are no commits to create a pull "
- "request with</p>",
- output.get_data(as_text=True),
- )
- output = self.app.get("/test/new_issue")
- csrf_token = self.get_csrf(output=output)
- data = {"csrf_token": csrf_token, "title": "foo bar PR"}
- output = self.app.post(
- "/test/diff/master..feature", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 400)
- self.assertIn(
- "<p>Fork is empty, there are no commits to create a pull "
- "request with</p>",
- output.get_data(as_text=True),
- )
- shutil.rmtree(newpath)
- @patch("pagure.lib.notify.send_email")
- def test_new_request_pull_empty_fork(self, send_email):
- """ Test the new_request_pull endpoint against an empty repo. """
- send_email.return_value = True
- self.test_fork_project()
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- fork = pagure.lib.query.get_authorized_project(
- self.session, "test", user="foo"
- )
- # Create a git repo to play with
- gitrepo = os.path.join(self.path, "repos", "test.git")
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Create a fork of this repo
- newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
- gitrepo = os.path.join(self.path, "repos", "forks", "foo", "test.git")
- new_repo = pygit2.clone_repository(gitrepo, newpath)
- user = tests.FakeUser()
- user.username = "foo"
- with tests.user_set(self.app.application, user):
- output = self.app.get(
- "/fork/foo/test/diff/master..master", follow_redirects=True
- )
- self.assertEqual(output.status_code, 400)
- self.assertIn(
- "<p>Fork is empty, there are no commits to create a pull "
- "request with</p>",
- output.get_data(as_text=True),
- )
- shutil.rmtree(newpath)
- @patch("pagure.lib.notify.send_email")
- def test_pull_request_add_comment(self, send_email):
- """ Test the pull_request_add_comment endpoint. """
- send_email.return_value = True
- self.test_request_pull()
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.post("/foo/pull-request/1/comment")
- self.assertEqual(output.status_code, 404)
- output = self.app.post("/test/pull-request/100/comment")
- self.assertEqual(output.status_code, 404)
- output = self.app.post("/test/pull-request/1/comment")
- self.assertEqual(output.status_code, 200)
- self.assertTrue(
- output.get_data(as_text=True).startswith(
- '\n<section class="add_comment">'
- )
- )
- csrf_token = self.get_csrf(output=output)
- data = {
- "csrf_token": csrf_token,
- "comment": "This look alright but we can do better",
- }
- output = self.app.post(
- "/test/pull-request/1/comment",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn("Comment added", output_text)
- self.assertEqual(output_text.count('title="PY C (pingou)"'), 2)
- # Project w/o pull-request
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings = repo.settings
- settings["pull_requests"] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/1/comment",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- @patch("pagure.lib.notify.send_email")
- def test_pull_request_drop_comment(self, send_email):
- """ Test the pull_request_drop_comment endpoint. """
- send_email.return_value = True
- self.test_pull_request_add_comment()
- # Project w/ pull-request
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- settings = repo.settings
- settings["pull_requests"] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- user = tests.FakeUser()
- user.username = "foo"
- with tests.user_set(self.app.application, user):
- output = self.app.post("/foo/pull-request/1/comment/drop")
- self.assertEqual(output.status_code, 404)
- output = self.app.post("/test/pull-request/100/comment/drop")
- self.assertEqual(output.status_code, 404)
- output = self.app.post(
- "/test/pull-request/1/comment/drop", follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- # self.assertIn('href="#comment-1">¶</a>', output_text)
- self.assertIn(
- "<p>This look alright but we can do better</p>", output_text
- )
- csrf_token = self.get_csrf(output=output)
- # Invalid comment id
- data = {"csrf_token": csrf_token, "drop_comment": "10"}
- output = self.app.post(
- "/test/pull-request/1/comment/drop",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- data["drop_comment"] = "1"
- output = self.app.post(
- "/test/pull-request/1/comment/drop",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 403)
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- # Drop comment
- output = self.app.post(
- "/test/pull-request/1/comment/drop",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertIn("Comment removed", output_text)
- # Project w/o pull-request
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings = repo.settings
- settings["pull_requests"] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/1/comment/drop",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- @patch("pagure.lib.notify.send_email")
- def test_pull_request_edit_comment(self, send_email):
- """ Test the pull request edit comment endpoint """
- send_email.return_value = True
- self.test_request_pull()
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- # Repo 'foo' does not exist so it is verifying that condition
- output = self.app.post("/foo/pull-request/1/comment/1/edit")
- self.assertEqual(output.status_code, 404)
- # Here no comment is present in the PR so its verifying that condition
- output = self.app.post("/test/pull-request/100/comment/100/edit")
- self.assertEqual(output.status_code, 404)
- output = self.app.post("/test/pull-request/1/comment")
- self.assertEqual(output.status_code, 200)
- # Creating comment to play with
- self.assertTrue(
- output.get_data(as_text=True).startswith(
- '\n<section class="add_comment">'
- )
- )
- csrf_token = self.get_csrf(output=output)
- data = {
- "csrf_token": csrf_token,
- "comment": "This look alright but we can do better",
- }
- output = self.app.post(
- "/test/pull-request/1/comment",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertIn("Comment added", output_text)
- # Check if the comment is there
- self.assertIn(
- "<p>This look alright but we can do better</p>", output_text
- )
- output = self.app.get("/test/pull-request/1/comment/1/edit")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn('<section class="edit_comment">', output_text)
- # Checking if the comment is there in the update page
- self.assertIn(
- "This look alright but we can do better</textarea>",
- output_text,
- )
- csrf_token = self.get_csrf(output=output)
- data = {
- "csrf_token": csrf_token,
- "update_comment": "This look alright but we can do better than this.",
- }
- output = self.app.post(
- "/test/pull-request/1/comment/1/edit",
- data=data,
- follow_redirects=True,
- )
- output_text = output.get_data(as_text=True)
- # Checking if the comment is updated in the main page
- self.assertIn(
- "<p>This look alright but we can do better than this.</p>",
- output_text,
- )
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- # Checking if Edited by User is there or not
- pattern = (
- re.escape("<small>Edited ")
- + '<span title="[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:'
- + '[0-9]{2} UTC" data-toggle="tooltip">(just now|seconds ago)</span>'
- + re.escape(" by pingou </small>")
- )
- self.assertIsNotNone(re.search(pattern, output_text))
- self.assertIn("Comment updated", output_text)
- # Project w/o pull-request
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings = repo.settings
- settings["pull_requests"] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- "/test/pull-request/1/comment/edit/1",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- @patch("pagure.lib.notify.send_email")
- def test_merge_request_pull_FF_w_merge_commit(self, send_email):
- """ Test the merge_request_pull endpoint with a FF PR but with a
- merge commit.
- """
- send_email.return_value = True
- self.test_request_pull()
- user = tests.FakeUser()
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- csrf_token = self.get_csrf(output=output)
- # No CSRF
- output = self.app.post(
- "/test/pull-request/1/merge", data={}, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n - "
- "Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<h4 class="ml-1">\n <div>\n '
- '<span class="fa fa-fw text-success fa-arrow-circle-down pt-1"></span>\n '
- '<span class="text-success '
- 'font-weight-bold">#1</span>\n '
- '<span class="font-weight-bold">\n '
- "PR from the feature branch\n",
- output_text,
- )
- self.assertIn(
- 'title="View file as of 2a552b">sources</a>', output_text
- )
- # Wrong project
- data = {"csrf_token": csrf_token}
- output = self.app.post(
- "/foobar/pull-request/100/merge",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- # Wrong project
- data = {"csrf_token": csrf_token}
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 403)
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- # Wrong request id
- data = {"csrf_token": csrf_token}
- output = self.app.post(
- "/test/pull-request/100/merge",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- # Project requiring a merge commit
- repo = pagure.lib.query.get_authorized_project(
- self.session, "test"
- )
- settings = repo.settings
- settings["always_merge"] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- # Merge
- output = self.app.post(
- "/test/pull-request/1/merge", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output = self.app.get("/test/commits")
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Commits - test - Pagure</title>", output_text
- )
- self.assertIn("Merge #1 `PR from the feature branch`", output_text)
- self.assertIn("A commit on branch feature", output_text)
- # Check if the closing notification was added
- output = self.app.get("/test/pull-request/1")
- output_text = output.get_data(as_text=True)
- self.assertIsNotNone(re.search(MERGED_PATTERN, output_text))
- @patch("pagure.lib.notify.send_email")
- def test_internal_endpoint_main_ahead(self, send_email):
- """ Test the new_request_pull endpoint when the main repo is ahead
- of the fork.
- """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- gitrepo = os.path.join(self.path, "repos", "test.git")
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Make the main repo be ahead of the fork
- # First commit
- newpath = tempfile.mkdtemp(prefix="pagure-test")
- repopath = os.path.join(newpath, "test")
- clone_repo = pygit2.clone_repository(gitrepo, repopath)
- # Create a file in that git repo
- with open(os.path.join(repopath, "testfile"), "w") as stream:
- stream.write("foo\n bar")
- clone_repo.index.add("testfile")
- clone_repo.index.write()
- # Commits the files added
- last_commit = clone_repo.revparse_single("HEAD")
- tree = clone_repo.index.write_tree()
- author = pygit2.Signature("Alice Author", "alice@authors.tld")
- committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
- clone_repo.create_commit(
- "refs/heads/master", # the name of the reference to update
- author,
- committer,
- "Add testfile file for testing",
- # binary string representing the tree object ID
- tree,
- # list of binary strings representing parents of the new commit
- [last_commit.oid.hex],
- )
- # Second commit
- with open(os.path.join(repopath, "testfile"), "a") as stream:
- stream.write("\nfoo2\n bar2")
- clone_repo.index.add("testfile")
- clone_repo.index.write()
- # Commits the files added
- last_commit = clone_repo.revparse_single("HEAD")
- tree = clone_repo.index.write_tree()
- author = pygit2.Signature("Alice Author", "alice@authors.tld")
- committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
- clone_repo.create_commit(
- "refs/heads/master", # the name of the reference to update
- author,
- committer,
- "Add a second commit to testfile for testing",
- # binary string representing the tree object ID
- tree,
- # list of binary strings representing parents of the new commit
- [last_commit.oid.hex],
- )
- # Third commit
- with open(os.path.join(repopath, "testfile"), "a") as stream:
- stream.write("\nfoo3\n bar3")
- clone_repo.index.add("testfile")
- clone_repo.index.write()
- # Commits the files added
- last_commit = clone_repo.revparse_single("HEAD")
- tree = clone_repo.index.write_tree()
- author = pygit2.Signature("Alice Author", "alice@authors.tld")
- committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
- clone_repo.create_commit(
- "refs/heads/master", # the name of the reference to update
- author,
- committer,
- "Add a third commit to testfile for testing",
- # binary string representing the tree object ID
- tree,
- # list of binary strings representing parents of the new commit
- [last_commit.oid.hex],
- )
- refname = "refs/heads/master:refs/heads/master"
- ori_remote = clone_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- shutil.rmtree(newpath)
- user = tests.FakeUser()
- user.username = "foo"
- with tests.user_set(self.app.application, user):
- csrf_token = self.get_csrf()
- output = self.app.post(
- "/pv/pull-request/ready",
- data={"repo": "test", "csrf_token": csrf_token},
- )
- self.assertEqual(output.status_code, 200)
- data = json.loads(output.get_data(as_text=True))
- self.assertEqual(sorted(data.keys()), ["code", "task"])
- self.assertEqual(data["code"], "OK")
- @patch("pagure.lib.notify.send_email")
- def test_fork_edit_file(self, send_email):
- """ Test the fork_edit file endpoint. """
- send_email.return_value = True
- # Git repo not found
- output = self.app.post("fork_edit/test/edit/master/f/sources")
- self.assertEqual(output.status_code, 404)
- tests.create_projects(self.session)
- for folder in ["docs", "tickets", "requests", "repos"]:
- tests.create_projects_git(
- os.path.join(self.path, folder), bare=True
- )
- # User not logged in
- output = self.app.post("fork_edit/test/edit/master/f/sources")
- self.assertEqual(output.status_code, 302)
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- # Invalid request
- output = self.app.post("fork_edit/test/edit/master/f/source")
- self.assertEqual(output.status_code, 400)
- output = self.app.get("/new/")
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- "<strong>Create new Project</strong>",
- output.get_data(as_text=True),
- )
- csrf_token = self.get_csrf(output=output)
- data = {"csrf_token": csrf_token}
- # No files can be found since they are not added
- output = self.app.post(
- "fork_edit/test/edit/master/f/sources",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- user = tests.FakeUser()
- user.username = "foo"
- with tests.user_set(self.app.application, user):
- data = {"csrf_token": csrf_token}
- # Invalid request
- output = self.app.post(
- "fork_edit/test/edit/master/f/sources", follow_redirects=True
- )
- self.assertEqual(output.status_code, 400)
- # Add content to the repo
- tests.add_content_git_repo(
- os.path.join(pagure.config.config["GIT_FOLDER"], "test.git")
- )
- tests.add_readme_git_repo(
- os.path.join(pagure.config.config["GIT_FOLDER"], "test.git")
- )
- tests.add_binary_git_repo(
- os.path.join(pagure.config.config["GIT_FOLDER"], "test.git"),
- "test.jpg",
- )
- # Check if button exists
- output = self.app.get("/test/blob/master/f/sources")
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- "Fork and Edit\n </button>\n",
- output.get_data(as_text=True),
- )
- # Check fork-edit doesn't show for binary files
- output = self.app.get("/test/blob/master/f/test.jpg")
- self.assertEqual(output.status_code, 200)
- self.assertNotIn(b"<html", output.data)
- # Check for edit panel
- output = self.app.post(
- "fork_edit/test/edit/master/f/sources",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '<li><a href="/fork/foo/test/tree/master">'
- '<span class="fa fa-random"></span> master</a>'
- '</li><li class="active"><span class="fa fa-file">'
- "</span> sources</li>",
- output_text,
- )
- self.assertIn(
- '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
- output_text,
- )
- # Check for edit panel- Fork already done
- output = self.app.post(
- "fork_edit/test/edit/master/f/sources",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn("<title>Edit - test - Pagure</title>", output_text)
- self.assertIn(
- "You had already forked " "this project", output_text
- )
- self.assertIn(
- '<i class="fa fa-code-fork fa-fw"></i> View Upstream',
- output_text,
- )
- self.assertIn(
- '<li><a href="/fork/foo/test/tree/master">'
- '<span class="fa fa-random"></span> master</a>'
- '</li><li class="active"><span class="fa fa-file">'
- "</span> sources</li>",
- output_text,
- )
- self.assertIn(
- '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
- output_text,
- )
- # View what's supposed to be an image
- output = self.app.post(
- "fork_edit/test/edit/master/f/test.jpg",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 400)
- self.assertIn(
- b"<p>Cannot edit binary files</p>", output.data,
- )
- # Check fork-edit shows when user is not logged in
- output = self.app.get("/test/blob/master/f/sources")
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- "Fork and Edit\n </button>\n",
- output.get_data(as_text=True),
- )
- # Check if fork-edit shows for different user
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- # Check if button exists
- output = self.app.get("/test/blob/master/f/sources")
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- "Edit in your fork\n </button>\n",
- output.get_data(as_text=True),
- )
- # Check fork-edit doesn't show for binary
- output = self.app.get("/test/blob/master/f/test.jpg")
- self.assertEqual(output.status_code, 200)
- self.assertNotIn(b"<html", output.data)
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_fork_edit_file_namespace(self):
- """ Test the fork_edit file endpoint on a namespaced project. """
- tests.create_projects(self.session)
- for folder in ["docs", "tickets", "requests", "repos"]:
- tests.create_projects_git(
- os.path.join(self.path, folder), bare=True
- )
- # User not logged in
- output = self.app.post(
- "fork_edit/somenamespace/test3/edit/master/f/sources"
- )
- self.assertEqual(output.status_code, 302)
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- # Invalid request
- output = self.app.post(
- "fork_edit/somenamespace/test3/edit/master/f/sources"
- )
- self.assertEqual(output.status_code, 400)
- output = self.app.get("/new/")
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- "<strong>Create new Project</strong>",
- output.get_data(as_text=True),
- )
- csrf_token = self.get_csrf(output=output)
- data = {"csrf_token": csrf_token}
- # No files can be found since they are not added
- output = self.app.post(
- "fork_edit/somenamespace/test3/edit/master/f/sources",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- user = tests.FakeUser()
- user.username = "foo"
- with tests.user_set(self.app.application, user):
- data = {"csrf_token": csrf_token}
- # Invalid request
- output = self.app.post(
- "fork_edit/somenamespace/test3/edit/master/f/sources",
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 400)
- # Add content to the repo
- tests.add_content_git_repo(
- os.path.join(
- pagure.config.config["GIT_FOLDER"],
- "somenamespace",
- "test3.git",
- )
- )
- tests.add_readme_git_repo(
- os.path.join(
- pagure.config.config["GIT_FOLDER"],
- "somenamespace",
- "test3.git",
- )
- )
- tests.add_binary_git_repo(
- os.path.join(
- pagure.config.config["GIT_FOLDER"],
- "somenamespace",
- "test3.git",
- ),
- "test.jpg",
- )
- # Check if button exists
- output = self.app.get("/somenamespace/test3/blob/master/f/sources")
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- "Fork and Edit\n </button>\n",
- output.get_data(as_text=True),
- )
- # Check fork-edit doesn't show for binary files
- output = self.app.get(
- "/somenamespace/test3/blob/master/f/test.jpg"
- )
- self.assertEqual(output.status_code, 200)
- self.assertNotIn(b"<html", output.data)
- # Check for edit panel
- output = self.app.post(
- "fork_edit/somenamespace/test3/edit/master/f/sources",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Edit - somenamespace/test3 - Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<i class="fa fa-code-fork fa-fw"></i> View Upstream',
- output_text,
- )
- self.assertIn(
- '<li><a href="/fork/foo/somenamespace/test3/tree/master">'
- '<span class="fa fa-random"></span> master</a>'
- '</li><li class="active"><span class="fa fa-file">'
- "</span> sources</li>",
- output_text,
- )
- self.assertIn(
- '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
- output_text,
- )
- # Check for edit panel - while the project was already forked
- output = self.app.post(
- "fork_edit/somenamespace/test3/edit/master/f/sources",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>Edit - somenamespace/test3 - Pagure</title>",
- output_text,
- )
- self.assertIn(
- "You had already forked " "this project", output_text
- )
- self.assertIn(
- '<i class="fa fa-code-fork fa-fw"></i> View Upstream',
- output_text,
- )
- self.assertIn(
- '<li><a href="/fork/foo/somenamespace/test3/tree/master">'
- '<span class="fa fa-random"></span> master</a>'
- '</li><li class="active"><span class="fa fa-file">'
- "</span> sources</li>",
- output_text,
- )
- self.assertIn(
- '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
- output_text,
- )
- @patch("pagure.lib.notify.send_email")
- def test_fork_without_main_repo(self, send_email):
- """ Test the fork without the main repo. """
- send_email.return_value = True
- tests.create_projects(self.session)
- # Create a fork with no parent i.e parent_id = None
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name="test",
- description="test project #1",
- hook_token="aaabbb",
- is_fork=True,
- parent_id=None,
- )
- self.session.add(item)
- self.session.commit()
- # Get fork project
- project = pagure.lib.query._get_project(self.session, "test", "foo")
- # Pull-requests and issue-trackers are off for forks
- # lib function is not used here so mannually turning them off
- project_settings = project.settings
- project_settings["pull_requests"] = False
- project_settings["issue_tracker"] = False
- project.settings = project_settings
- self.session.add(project)
- self.session.commit()
- tests.create_projects_git(
- os.path.join(self.path, "repos", "forks", "foo"), bare=True
- )
- # Create a git repo to play with
- gitrepo = os.path.join(self.path, "repos", "test.git")
- self.assertFalse(os.path.exists(gitrepo))
- os.makedirs(gitrepo)
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Create a fork of this repo
- newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
- gitrepo = os.path.join(self.path, "repos", "forks", "foo", "test.git")
- new_repo = pygit2.clone_repository(gitrepo, newpath)
- tests.add_content_git_repo(gitrepo)
- # UI test for deleted main
- output = self.app.get("/fork/foo/test")
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- "Forked from a deleted repository", output.get_data(as_text=True)
- )
- # Testing commit endpoint
- output = self.app.get("/fork/foo/test/commits/master")
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- 'Commits <span class="badge badge-secondary"> 2</span>\n',
- output.get_data(as_text=True),
- )
- # Test pull-request endpoint
- output = self.app.get("/fork/foo/test/pull-requests")
- self.assertEqual(output.status_code, 404)
- # Test issue-tracker endpoint
- output = self.app.get("/fork/foo/test/issues")
- self.assertEqual(output.status_code, 404)
- shutil.rmtree(newpath)
- def _set_up_for_reaction_test(self):
- self.session.add(
- pagure.lib.model.User(
- user="jdoe",
- fullname="John Doe",
- password=b"password",
- default_email="jdoe@example.com",
- )
- )
- self.session.commit()
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- pagure.lib.query.get_authorized_project(self.session, "test")
- request = pagure.lib.query.search_pull_requests(
- self.session, requestid=1, project_id=1
- )
- pagure.lib.query.add_pull_request_comment(
- self.session,
- request=request,
- commit=None,
- tree_id=None,
- filename=None,
- row=None,
- comment="Hello",
- user="jdoe",
- )
- self.session.commit()
- @patch("pagure.lib.notify.send_email")
- def test_add_reaction(self, send_email):
- """ Test the request_pull endpoint. """
- send_email.return_value = True
- self._set_up_for_reaction_test()
- user = tests.FakeUser()
- user.username = "pingou"
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- data = {
- "csrf_token": self.get_csrf(output=output),
- "reaction": "Thumbs up",
- }
- output = self.app.post(
- "/test/pull-request/1/comment/1/react",
- data=data,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- # Load the page and check reaction is added.
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- "Thumbs up sent by pingou", output.get_data(as_text=True)
- )
- @patch("pagure.lib.notify.send_email")
- def test_add_reaction_unauthenticated(self, send_email):
- """ Test the request_pull endpoint. """
- send_email.return_value = True
- self._set_up_for_reaction_test()
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- data = {
- "csrf_token": self.get_csrf(output=output),
- "reaction": "Thumbs down",
- }
- output = self.app.post(
- "/test/pull-request/1/comment/1/react",
- data=data,
- follow_redirects=False,
- )
- # Redirect to login page
- self.assertEqual(output.status_code, 302)
- self.assertIn("/login/", output.headers["Location"])
- class TestTicketAccessEditPRMetadata(tests.Modeltests):
- """ Tests that people with ticket access on a project can edit the
- meta-data of a PR """
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(TestTicketAccessEditPRMetadata, self).setUp()
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, "requests"), bare=True
- )
- set_up_git_repo(
- self.session, self.path, new_project=None, branch_from="feature"
- )
- # Add user "foo" to the project "test"
- repo = pagure.lib.query._get_project(self.session, "test")
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=repo,
- new_user="foo",
- user="pingou",
- access="ticket",
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- def test_unauth_cannot_view_edit_metadata_ui(self):
- """ Test that unauthenticated users cannot view the edit the
- metadata fields in the UI. """
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n"
- " - Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<i class="fa fa-fw fa-pencil"></i></a>',
- '<a class="btn btn-outline-primary border-0 btn-sm '
- "issue-metadata-display editmetadatatoggle pointer inline-block"
- '"><i class="fa fa-fw fa-pencil"></i></a>',
- output_text,
- )
- self.assertNotIn(
- '<form method="POST" action="/test/pull-request/1/update">',
- output_text,
- )
- def test_admin_can_view_edit_metadata_ui(self):
- """ Test that admin users can view the edit the metadata fields in
- the UI. """
- user = tests.FakeUser(username="pingou")
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n"
- " - Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<i class="fa fa-fw fa-pencil"></i></a>',
- '<a class="btn btn-outline-primary border-0 btn-sm '
- "issue-metadata-display editmetadatatoggle pointer inline-block"
- '"><i class="fa fa-fw fa-pencil"></i></a>',
- output_text,
- )
- self.assertIn(
- '<form method="POST" action="/test/pull-request/1/update">',
- output_text,
- )
- def test_admin_can_edit_metadata_ui(self):
- """ Test that admin users can edit the metadata in the UI. """
- user = tests.FakeUser(username="pingou")
- with tests.user_set(self.app.application, user):
- data = {"csrf_token": self.get_csrf(), "user": "foo"}
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n"
- " - Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<i class="fa fa-fw fa-pencil"></i></a>',
- '<a class="btn btn-outline-primary border-0 btn-sm '
- "issue-metadata-display editmetadatatoggle pointer inline-block"
- '"><i class="fa fa-fw fa-pencil"></i></a>',
- output_text,
- )
- self.assertIn(
- '<form method="POST" action="/test/pull-request/1/update">',
- output_text,
- )
- self.assertIn(
- '<input value="foo"\n name="user" '
- 'id="assignee" placeholder="username" >',
- output_text,
- )
- def test_ticket_can_view_edit_metadata_ui(self):
- """ Test that users with ticket access can view the edit the
- metadata fields in the UI. """
- user = tests.FakeUser(username="foo")
- with tests.user_set(self.app.application, user):
- output = self.app.get("/test/pull-request/1")
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n"
- " - Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<i class="fa fa-fw fa-pencil"></i></a>',
- '<a class="btn btn-outline-primary border-0 btn-sm '
- "issue-metadata-display editmetadatatoggle pointer inline-block"
- '"><i class="fa fa-fw fa-pencil"></i></a>',
- output_text,
- )
- self.assertIn(
- '<form method="POST" action="/test/pull-request/1/update">',
- output_text,
- )
- def test_ticket_can_edit_metadata_ui(self):
- """ Test that users with ticket access can edit the metadata in the
- UI. """
- user = tests.FakeUser(username="foo")
- with tests.user_set(self.app.application, user):
- data = {"csrf_token": self.get_csrf(), "user": "pingou"}
- output = self.app.post(
- "/test/pull-request/1/update", data=data, follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- "<title>PR#1: PR from the feature branch - test\n"
- " - Pagure</title>",
- output_text,
- )
- self.assertIn(
- '<i class="fa fa-fw fa-pencil"></i></a>',
- '<a class="btn btn-outline-primary border-0 btn-sm '
- "issue-metadata-display editmetadatatoggle pointer inline-block"
- '"><i class="fa fa-fw fa-pencil"></i></a>',
- output_text,
- )
- self.assertIn(
- '<form method="POST" action="/test/pull-request/1/update">',
- output_text,
- )
- self.assertIn(
- '<input value="pingou"\n name="user" '
- 'id="assignee" placeholder="username" >',
- output_text,
- )
- if __name__ == "__main__":
- unittest.main(verbosity=2)
|