test_pagure_flask_api_fork.py 145 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import unittest
  10. import shutil
  11. import sys
  12. import os
  13. import json
  14. from mock import patch, MagicMock
  15. sys.path.insert(
  16. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  17. )
  18. import pagure.lib.query
  19. import pagure.default_config
  20. import tests
  21. class PagureFlaskApiForktests(tests.Modeltests):
  22. """ Tests for the flask API of pagure for issue """
  23. maxDiff = None
  24. def setUp(self):
  25. """ Set up the environnment, ran before every tests. """
  26. super(PagureFlaskApiForktests, self).setUp()
  27. pagure.config.config["REQUESTS_FOLDER"] = None
  28. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  29. def test_api_pull_request_views_pr_disabled(self):
  30. """Test the api_pull_request_views method of the flask api when PR
  31. are disabled."""
  32. tests.create_projects(self.session)
  33. tests.create_tokens(self.session)
  34. tests.create_tokens_acl(self.session)
  35. # Create a pull-request
  36. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  37. forked_repo = pagure.lib.query.get_authorized_project(
  38. self.session, "test"
  39. )
  40. req = pagure.lib.query.new_pull_request(
  41. session=self.session,
  42. repo_from=forked_repo,
  43. branch_from="master",
  44. repo_to=repo,
  45. branch_to="master",
  46. title="test pull-request",
  47. user="pingou",
  48. )
  49. self.session.commit()
  50. self.assertEqual(req.id, 1)
  51. self.assertEqual(req.title, "test pull-request")
  52. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  53. settings = repo.settings
  54. settings["pull_requests"] = False
  55. repo.settings = settings
  56. self.session.add(repo)
  57. self.session.commit()
  58. output = self.app.get("/api/0/test/pull-requests")
  59. self.assertEqual(output.status_code, 404)
  60. data = json.loads(output.get_data(as_text=True))
  61. self.assertDictEqual(
  62. data,
  63. {
  64. "error": "Pull-Request have been deactivated for this project",
  65. "error_code": "EPULLREQUESTSDISABLED",
  66. },
  67. )
  68. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  69. def test_api_pull_request_views_pr_closed(self):
  70. """Test the api_pull_request_views method of the flask api to list
  71. the closed PRs."""
  72. tests.create_projects(self.session)
  73. tests.create_tokens(self.session)
  74. tests.create_tokens_acl(self.session)
  75. # Create a pull-request
  76. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  77. forked_repo = pagure.lib.query.get_authorized_project(
  78. self.session, "test"
  79. )
  80. req = pagure.lib.query.new_pull_request(
  81. session=self.session,
  82. repo_from=forked_repo,
  83. branch_from="master",
  84. repo_to=repo,
  85. branch_to="master",
  86. title="test pull-request",
  87. user="pingou",
  88. )
  89. self.session.commit()
  90. self.assertEqual(req.id, 1)
  91. self.assertEqual(req.title, "test pull-request")
  92. output = self.app.get("/api/0/test/pull-requests?status=closed")
  93. self.assertEqual(output.status_code, 200)
  94. data = json.loads(output.get_data(as_text=True))
  95. for k in ["first", "last"]:
  96. self.assertIsNotNone(data["pagination"][k])
  97. data["pagination"][k] = "http://localhost..."
  98. self.assertDictEqual(
  99. data,
  100. {
  101. "args": {
  102. "assignee": None,
  103. "author": None,
  104. "tags": [],
  105. "page": 1,
  106. "per_page": 20,
  107. "status": "closed",
  108. },
  109. "pagination": {
  110. "first": "http://localhost...",
  111. "last": "http://localhost...",
  112. "next": None,
  113. "page": 1,
  114. "pages": 0,
  115. "per_page": 20,
  116. "prev": None,
  117. },
  118. "requests": [],
  119. "total_requests": 0,
  120. },
  121. )
  122. # Close the PR and try again
  123. pagure.lib.query.close_pull_request(
  124. self.session, request=req, user="pingou", merged=False
  125. )
  126. output = self.app.get("/api/0/test/pull-requests?status=closed")
  127. self.assertEqual(output.status_code, 200)
  128. data = json.loads(output.get_data(as_text=True))
  129. self.assertEqual(
  130. sorted(data.keys()),
  131. ["args", "pagination", "requests", "total_requests"],
  132. )
  133. self.assertDictEqual(
  134. data["args"],
  135. {
  136. "assignee": None,
  137. "author": None,
  138. "tags": [],
  139. "page": 1,
  140. "per_page": 20,
  141. "status": "closed",
  142. },
  143. )
  144. self.assertEqual(data["total_requests"], 1)
  145. # Create two closed pull-requests
  146. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  147. forked_repo = pagure.lib.query.get_authorized_project(
  148. self.session, "test"
  149. )
  150. pagure.lib.query.new_pull_request(
  151. session=self.session,
  152. repo_from=forked_repo,
  153. branch_from="master",
  154. repo_to=repo,
  155. branch_to="master",
  156. title="closed pullrequest by user foo on repo test",
  157. user="foo",
  158. status="Closed",
  159. )
  160. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  161. forked_repo = pagure.lib.query.get_authorized_project(
  162. self.session, "test"
  163. )
  164. pagure.lib.query.new_pull_request(
  165. session=self.session,
  166. repo_from=forked_repo,
  167. branch_from="master",
  168. repo_to=repo,
  169. branch_to="master",
  170. title="closed pullrequest by user pingou on repo test",
  171. user="pingou",
  172. status="Closed",
  173. )
  174. self.session.commit()
  175. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  176. forked_repo = pagure.lib.query.get_authorized_project(
  177. self.session, "test"
  178. )
  179. pagure.lib.query.new_pull_request(
  180. session=self.session,
  181. repo_from=forked_repo,
  182. branch_from="master",
  183. repo_to=repo,
  184. branch_to="master",
  185. title="merged pullrequest by user pingou on repo test",
  186. user="pingou",
  187. status="Merged",
  188. )
  189. self.session.commit()
  190. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  191. forked_repo = pagure.lib.query.get_authorized_project(
  192. self.session, "test"
  193. )
  194. pagure.lib.query.new_pull_request(
  195. session=self.session,
  196. repo_from=forked_repo,
  197. branch_from="master",
  198. repo_to=repo,
  199. branch_to="master",
  200. title="merged pullrequest by user foo on repo test",
  201. user="foo",
  202. status="Merged",
  203. )
  204. self.session.commit()
  205. # Test the API view of closed pull-requests
  206. output = self.app.get("/api/0/test/pull-requests?status=closed")
  207. self.assertEqual(output.status_code, 200)
  208. data = json.loads(output.get_data(as_text=True))
  209. self.assertEqual(len(data["requests"]), 3)
  210. self.assertEqual(
  211. sorted(data.keys()),
  212. ["args", "pagination", "requests", "total_requests"],
  213. )
  214. for req in data["requests"]:
  215. self.assertEqual(req["status"], "Closed")
  216. self.assertEqual(data["args"]["status"], "closed")
  217. self.assertEqual(data["args"]["page"], 1)
  218. self.assertEqual(data["total_requests"], 3)
  219. # Test the API view of merged pull-requests
  220. output = self.app.get("/api/0/test/pull-requests?status=merged")
  221. self.assertEqual(output.status_code, 200)
  222. data = json.loads(output.get_data(as_text=True))
  223. self.assertEqual(len(data["requests"]), 2)
  224. self.assertEqual(
  225. sorted(data.keys()),
  226. ["args", "pagination", "requests", "total_requests"],
  227. )
  228. for req in data["requests"]:
  229. self.assertEqual(req["status"], "Merged")
  230. self.assertEqual(data["args"]["status"], "merged")
  231. self.assertEqual(data["args"]["page"], 1)
  232. self.assertEqual(data["total_requests"], 2)
  233. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  234. def test_api_pull_request_views_all_pr(self):
  235. """Test the api_pull_request_views method of the flask api to list
  236. all PRs."""
  237. tests.create_projects(self.session)
  238. tests.create_tokens(self.session)
  239. tests.create_tokens_acl(self.session)
  240. # Create a pull-request
  241. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  242. forked_repo = pagure.lib.query.get_authorized_project(
  243. self.session, "test"
  244. )
  245. req = pagure.lib.query.new_pull_request(
  246. session=self.session,
  247. repo_from=forked_repo,
  248. branch_from="master",
  249. repo_to=repo,
  250. branch_to="master",
  251. title="test pull-request",
  252. user="pingou",
  253. )
  254. self.session.commit()
  255. self.assertEqual(req.id, 1)
  256. self.assertEqual(req.title, "test pull-request")
  257. output = self.app.get("/api/0/test/pull-requests?status=all")
  258. self.assertEqual(output.status_code, 200)
  259. data = json.loads(output.get_data(as_text=True))
  260. self.assertEqual(
  261. sorted(data.keys()),
  262. ["args", "pagination", "requests", "total_requests"],
  263. )
  264. self.assertDictEqual(
  265. data["args"],
  266. {
  267. "assignee": None,
  268. "author": None,
  269. "tags": [],
  270. "page": 1,
  271. "per_page": 20,
  272. "status": "all",
  273. },
  274. )
  275. self.assertEqual(data["total_requests"], 1)
  276. # Close the PR and try again
  277. pagure.lib.query.close_pull_request(
  278. self.session, request=req, user="pingou", merged=False
  279. )
  280. output = self.app.get("/api/0/test/pull-requests?status=all")
  281. self.assertEqual(output.status_code, 200)
  282. data = json.loads(output.get_data(as_text=True))
  283. self.assertEqual(
  284. sorted(data.keys()),
  285. ["args", "pagination", "requests", "total_requests"],
  286. )
  287. self.assertDictEqual(
  288. data["args"],
  289. {
  290. "assignee": None,
  291. "author": None,
  292. "tags": [],
  293. "page": 1,
  294. "per_page": 20,
  295. "status": "all",
  296. },
  297. )
  298. self.assertEqual(data["total_requests"], 1)
  299. @patch("pagure.lib.notify.send_email")
  300. def test_api_pull_request_views(self, send_email):
  301. """ Test the api_pull_request_views method of the flask api. """
  302. send_email.return_value = True
  303. tests.create_projects(self.session)
  304. tests.create_tokens(self.session)
  305. tests.create_tokens_acl(self.session)
  306. # Create a pull-request
  307. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  308. forked_repo = pagure.lib.query.get_authorized_project(
  309. self.session, "test"
  310. )
  311. req = pagure.lib.query.new_pull_request(
  312. session=self.session,
  313. repo_from=forked_repo,
  314. branch_from="master",
  315. repo_to=repo,
  316. branch_to="master",
  317. title="test pull-request",
  318. user="pingou",
  319. )
  320. self.session.commit()
  321. self.assertEqual(req.id, 1)
  322. self.assertEqual(req.title, "test pull-request")
  323. # Invalid repo
  324. output = self.app.get("/api/0/foo/pull-requests")
  325. self.assertEqual(output.status_code, 404)
  326. data = json.loads(output.get_data(as_text=True))
  327. self.assertDictEqual(
  328. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  329. )
  330. # List pull-requests
  331. output = self.app.get("/api/0/test/pull-requests")
  332. self.assertEqual(output.status_code, 200)
  333. data = json.loads(output.get_data(as_text=True))
  334. data["requests"][0]["date_created"] = "1431414800"
  335. data["requests"][0]["updated_on"] = "1431414800"
  336. data["requests"][0]["project"]["date_created"] = "1431414800"
  337. data["requests"][0]["project"]["date_modified"] = "1431414800"
  338. data["requests"][0]["repo_from"]["date_created"] = "1431414800"
  339. data["requests"][0]["repo_from"]["date_modified"] = "1431414800"
  340. data["requests"][0]["uid"] = "1431414800"
  341. data["requests"][0]["last_updated"] = "1431414800"
  342. for k in ["first", "last"]:
  343. self.assertIsNotNone(data["pagination"][k])
  344. data["pagination"][k] = "http://localhost..."
  345. expected_data = {
  346. "args": {
  347. "assignee": None,
  348. "author": None,
  349. "tags": [],
  350. "page": 1,
  351. "per_page": 20,
  352. "status": True,
  353. },
  354. "pagination": {
  355. "first": "http://localhost...",
  356. "last": "http://localhost...",
  357. "next": None,
  358. "page": 1,
  359. "pages": 1,
  360. "per_page": 20,
  361. "prev": None,
  362. },
  363. "requests": [
  364. {
  365. "assignee": None,
  366. "branch": "master",
  367. "branch_from": "master",
  368. "cached_merge_status": "unknown",
  369. "closed_at": None,
  370. "closed_by": None,
  371. "comments": [],
  372. "commit_start": None,
  373. "commit_stop": None,
  374. "date_created": "1431414800",
  375. "full_url": "http://localhost.localdomain/test/pull-request/1",
  376. "id": 1,
  377. "initial_comment": None,
  378. "last_updated": "1431414800",
  379. "project": {
  380. "access_groups": {
  381. "admin": [],
  382. "collaborator": [],
  383. "commit": [],
  384. "ticket": [],
  385. },
  386. "access_users": {
  387. "admin": [],
  388. "collaborator": [],
  389. "commit": [],
  390. "owner": ["pingou"],
  391. "ticket": [],
  392. },
  393. "close_status": [
  394. "Invalid",
  395. "Insufficient data",
  396. "Fixed",
  397. "Duplicate",
  398. ],
  399. "custom_keys": [],
  400. "date_created": "1431414800",
  401. "date_modified": "1431414800",
  402. "description": "test project #1",
  403. "full_url": "http://localhost.localdomain/test",
  404. "fullname": "test",
  405. "url_path": "test",
  406. "id": 1,
  407. "milestones": {},
  408. "name": "test",
  409. "namespace": None,
  410. "parent": None,
  411. "priorities": {},
  412. "tags": [],
  413. "user": {
  414. "full_url": "http://localhost.localdomain/user/pingou",
  415. "fullname": "PY C",
  416. "name": "pingou",
  417. "url_path": "user/pingou",
  418. },
  419. },
  420. "remote_git": None,
  421. "repo_from": {
  422. "access_groups": {
  423. "admin": [],
  424. "collaborator": [],
  425. "commit": [],
  426. "ticket": [],
  427. },
  428. "access_users": {
  429. "admin": [],
  430. "collaborator": [],
  431. "commit": [],
  432. "owner": ["pingou"],
  433. "ticket": [],
  434. },
  435. "close_status": [
  436. "Invalid",
  437. "Insufficient data",
  438. "Fixed",
  439. "Duplicate",
  440. ],
  441. "custom_keys": [],
  442. "date_created": "1431414800",
  443. "date_modified": "1431414800",
  444. "description": "test project #1",
  445. "full_url": "http://localhost.localdomain/test",
  446. "fullname": "test",
  447. "url_path": "test",
  448. "id": 1,
  449. "milestones": {},
  450. "name": "test",
  451. "namespace": None,
  452. "parent": None,
  453. "priorities": {},
  454. "tags": [],
  455. "user": {
  456. "full_url": "http://localhost.localdomain/user/pingou",
  457. "fullname": "PY C",
  458. "name": "pingou",
  459. "url_path": "user/pingou",
  460. },
  461. },
  462. "status": "Open",
  463. "tags": [],
  464. "threshold_reached": None,
  465. "title": "test pull-request",
  466. "uid": "1431414800",
  467. "updated_on": "1431414800",
  468. "user": {
  469. "full_url": "http://localhost.localdomain/user/pingou",
  470. "fullname": "PY C",
  471. "name": "pingou",
  472. "url_path": "user/pingou",
  473. },
  474. }
  475. ],
  476. "total_requests": 1,
  477. }
  478. self.assertDictEqual(data, expected_data)
  479. headers = {"Authorization": "token aaabbbcccddd"}
  480. # Access Pull-Request authenticated
  481. output = self.app.get("/api/0/test/pull-requests", headers=headers)
  482. self.assertEqual(output.status_code, 200)
  483. data2 = json.loads(output.get_data(as_text=True))
  484. data2["requests"][0]["date_created"] = "1431414800"
  485. data2["requests"][0]["updated_on"] = "1431414800"
  486. data2["requests"][0]["project"]["date_created"] = "1431414800"
  487. data2["requests"][0]["project"]["date_modified"] = "1431414800"
  488. data2["requests"][0]["repo_from"]["date_created"] = "1431414800"
  489. data2["requests"][0]["repo_from"]["date_modified"] = "1431414800"
  490. data2["requests"][0]["uid"] = "1431414800"
  491. data2["requests"][0]["last_updated"] = "1431414800"
  492. for k in ["first", "last"]:
  493. self.assertIsNotNone(data["pagination"][k])
  494. data2["pagination"][k] = "http://localhost..."
  495. self.assertDictEqual(data, data2)
  496. @patch("pagure.lib.notify.send_email")
  497. def test_api_pull_request_view_tag_filtered(self, send_email):
  498. """Test the api_pull_request_view method of the flask api to list
  499. tag filtered open PRs."""
  500. send_email.return_value = True
  501. tests.create_projects(self.session)
  502. tests.create_tokens(self.session)
  503. tests.create_tokens_acl(self.session)
  504. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  505. # Add a tag
  506. pagure.lib.query.new_tag(
  507. self.session, "tag-1", "tag-1 description", "#ff0000", repo.id
  508. )
  509. # Create a pull-request
  510. forked_repo = pagure.lib.query.get_authorized_project(
  511. self.session, "test"
  512. )
  513. req = pagure.lib.query.new_pull_request(
  514. session=self.session,
  515. repo_from=forked_repo,
  516. branch_from="master",
  517. repo_to=repo,
  518. branch_to="master",
  519. title="test pull-request",
  520. user="pingou",
  521. )
  522. self.session.commit()
  523. self.assertEqual(req.id, 1)
  524. self.assertEqual(req.title, "test pull-request")
  525. output = self.app.get("/api/0/test/pull-requests?tags=tag-1")
  526. self.assertEqual(output.status_code, 200)
  527. data = json.loads(output.get_data(as_text=True))
  528. for k in ["first", "last"]:
  529. self.assertIsNotNone(data["pagination"][k])
  530. data["pagination"][k] = "http://localhost..."
  531. self.assertDictEqual(
  532. data,
  533. {
  534. "args": {
  535. "assignee": None,
  536. "author": None,
  537. "tags": ["tag-1"],
  538. "page": 1,
  539. "per_page": 20,
  540. "status": True,
  541. },
  542. "pagination": {
  543. "first": "http://localhost...",
  544. "last": "http://localhost...",
  545. "next": None,
  546. "page": 1,
  547. "pages": 0,
  548. "per_page": 20,
  549. "prev": None,
  550. },
  551. "requests": [],
  552. "total_requests": 0,
  553. },
  554. )
  555. # Tag the PR and try again
  556. pagure.lib.query.update_tags(
  557. self.session, obj=req, tags=["tag-1"], username="pingou"
  558. )
  559. self.session.commit()
  560. output = self.app.get("/api/0/test/pull-requests?tags=tag-1")
  561. self.assertEqual(output.status_code, 200)
  562. data = json.loads(output.get_data(as_text=True))
  563. self.assertEqual(
  564. sorted(data.keys()),
  565. ["args", "pagination", "requests", "total_requests"],
  566. )
  567. self.assertDictEqual(
  568. data["args"],
  569. {
  570. "assignee": None,
  571. "author": None,
  572. "tags": ["tag-1"],
  573. "page": 1,
  574. "per_page": 20,
  575. "status": True,
  576. },
  577. )
  578. self.assertEqual(data["total_requests"], 1)
  579. # Try negative filtering
  580. output = self.app.get("/api/0/test/pull-requests?tags=!tag-1")
  581. self.assertEqual(output.status_code, 200)
  582. data = json.loads(output.get_data(as_text=True))
  583. for k in ["first", "last"]:
  584. self.assertIsNotNone(data["pagination"][k])
  585. data["pagination"][k] = "http://localhost..."
  586. self.assertDictEqual(
  587. data,
  588. {
  589. "args": {
  590. "assignee": None,
  591. "author": None,
  592. "tags": ["!tag-1"],
  593. "page": 1,
  594. "per_page": 20,
  595. "status": True,
  596. },
  597. "pagination": {
  598. "first": "http://localhost...",
  599. "last": "http://localhost...",
  600. "next": None,
  601. "page": 1,
  602. "pages": 0,
  603. "per_page": 20,
  604. "prev": None,
  605. },
  606. "requests": [],
  607. "total_requests": 0,
  608. },
  609. )
  610. @patch("pagure.lib.notify.send_email")
  611. def test_api_pull_request_view_pr_disabled(self, send_email):
  612. """ Test the api_pull_request_view method of the flask api. """
  613. send_email.return_value = True
  614. tests.create_projects(self.session)
  615. tests.create_tokens(self.session)
  616. tests.create_tokens_acl(self.session)
  617. # Create a pull-request
  618. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  619. forked_repo = pagure.lib.query.get_authorized_project(
  620. self.session, "test"
  621. )
  622. req = pagure.lib.query.new_pull_request(
  623. session=self.session,
  624. repo_from=forked_repo,
  625. branch_from="master",
  626. repo_to=repo,
  627. branch_to="master",
  628. title="test pull-request",
  629. user="pingou",
  630. )
  631. self.session.commit()
  632. self.assertEqual(req.id, 1)
  633. self.assertEqual(req.title, "test pull-request")
  634. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  635. settings = repo.settings
  636. settings["pull_requests"] = False
  637. repo.settings = settings
  638. self.session.add(repo)
  639. self.session.commit()
  640. output = self.app.get("/api/0/test/pull-request/1")
  641. self.assertEqual(output.status_code, 404)
  642. data = json.loads(output.get_data(as_text=True))
  643. self.assertDictEqual(
  644. data,
  645. {
  646. "error": "Pull-Request have been deactivated for this project",
  647. "error_code": "EPULLREQUESTSDISABLED",
  648. },
  649. )
  650. @patch("pagure.lib.notify.send_email")
  651. def test_api_pull_request_view(self, send_email):
  652. """ Test the api_pull_request_view method of the flask api. """
  653. send_email.return_value = True
  654. tests.create_projects(self.session)
  655. tests.create_tokens(self.session)
  656. tests.create_tokens_acl(self.session)
  657. # Create a pull-request
  658. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  659. forked_repo = pagure.lib.query.get_authorized_project(
  660. self.session, "test"
  661. )
  662. req = pagure.lib.query.new_pull_request(
  663. session=self.session,
  664. repo_from=forked_repo,
  665. branch_from="master",
  666. repo_to=repo,
  667. branch_to="master",
  668. title="test pull-request",
  669. user="pingou",
  670. )
  671. self.session.commit()
  672. self.assertEqual(req.id, 1)
  673. self.assertEqual(req.title, "test pull-request")
  674. # Invalid repo
  675. output = self.app.get("/api/0/foo/pull-request/1")
  676. self.assertEqual(output.status_code, 404)
  677. data = json.loads(output.get_data(as_text=True))
  678. self.assertDictEqual(
  679. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  680. )
  681. # Invalid issue for this repo
  682. output = self.app.get("/api/0/test2/pull-request/1")
  683. self.assertEqual(output.status_code, 404)
  684. data = json.loads(output.get_data(as_text=True))
  685. self.assertDictEqual(
  686. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  687. )
  688. # Valid issue
  689. output = self.app.get("/api/0/test/pull-request/1")
  690. self.assertEqual(output.status_code, 200)
  691. data = json.loads(output.get_data(as_text=True))
  692. data["date_created"] = "1431414800"
  693. data["updated_on"] = "1431414800"
  694. data["project"]["date_created"] = "1431414800"
  695. data["project"]["date_modified"] = "1431414800"
  696. data["repo_from"]["date_created"] = "1431414800"
  697. data["repo_from"]["date_modified"] = "1431414800"
  698. data["uid"] = "1431414800"
  699. data["last_updated"] = "1431414800"
  700. expected_data = {
  701. "assignee": None,
  702. "branch": "master",
  703. "branch_from": "master",
  704. "cached_merge_status": "unknown",
  705. "closed_at": None,
  706. "closed_by": None,
  707. "comments": [],
  708. "commit_start": None,
  709. "commit_stop": None,
  710. "date_created": "1431414800",
  711. "full_url": "http://localhost.localdomain/test/pull-request/1",
  712. "id": 1,
  713. "initial_comment": None,
  714. "last_updated": "1431414800",
  715. "project": {
  716. "access_groups": {
  717. "admin": [],
  718. "collaborator": [],
  719. "commit": [],
  720. "ticket": [],
  721. },
  722. "access_users": {
  723. "admin": [],
  724. "collaborator": [],
  725. "commit": [],
  726. "owner": ["pingou"],
  727. "ticket": [],
  728. },
  729. "close_status": [
  730. "Invalid",
  731. "Insufficient data",
  732. "Fixed",
  733. "Duplicate",
  734. ],
  735. "custom_keys": [],
  736. "date_created": "1431414800",
  737. "date_modified": "1431414800",
  738. "description": "test project #1",
  739. "full_url": "http://localhost.localdomain/test",
  740. "fullname": "test",
  741. "url_path": "test",
  742. "id": 1,
  743. "milestones": {},
  744. "name": "test",
  745. "namespace": None,
  746. "parent": None,
  747. "priorities": {},
  748. "tags": [],
  749. "user": {
  750. "full_url": "http://localhost.localdomain/user/pingou",
  751. "fullname": "PY C",
  752. "name": "pingou",
  753. "url_path": "user/pingou",
  754. },
  755. },
  756. "remote_git": None,
  757. "repo_from": {
  758. "access_groups": {
  759. "admin": [],
  760. "collaborator": [],
  761. "commit": [],
  762. "ticket": [],
  763. },
  764. "access_users": {
  765. "admin": [],
  766. "collaborator": [],
  767. "commit": [],
  768. "owner": ["pingou"],
  769. "ticket": [],
  770. },
  771. "close_status": [
  772. "Invalid",
  773. "Insufficient data",
  774. "Fixed",
  775. "Duplicate",
  776. ],
  777. "custom_keys": [],
  778. "date_created": "1431414800",
  779. "date_modified": "1431414800",
  780. "description": "test project #1",
  781. "full_url": "http://localhost.localdomain/test",
  782. "fullname": "test",
  783. "url_path": "test",
  784. "id": 1,
  785. "milestones": {},
  786. "name": "test",
  787. "namespace": None,
  788. "parent": None,
  789. "priorities": {},
  790. "tags": [],
  791. "user": {
  792. "full_url": "http://localhost.localdomain/user/pingou",
  793. "fullname": "PY C",
  794. "name": "pingou",
  795. "url_path": "user/pingou",
  796. },
  797. },
  798. "status": "Open",
  799. "tags": [],
  800. "threshold_reached": None,
  801. "title": "test pull-request",
  802. "uid": "1431414800",
  803. "updated_on": "1431414800",
  804. "user": {
  805. "full_url": "http://localhost.localdomain/user/pingou",
  806. "fullname": "PY C",
  807. "name": "pingou",
  808. "url_path": "user/pingou",
  809. },
  810. }
  811. self.assertDictEqual(data, expected_data)
  812. headers = {"Authorization": "token aaabbbcccddd"}
  813. # Access Pull-Request authenticated
  814. output = self.app.get("/api/0/test/pull-request/1", headers=headers)
  815. self.assertEqual(output.status_code, 200)
  816. data2 = json.loads(output.get_data(as_text=True))
  817. data2["date_created"] = "1431414800"
  818. data2["project"]["date_created"] = "1431414800"
  819. data2["project"]["date_modified"] = "1431414800"
  820. data2["repo_from"]["date_created"] = "1431414800"
  821. data2["repo_from"]["date_modified"] = "1431414800"
  822. data2["uid"] = "1431414800"
  823. data2["date_created"] = "1431414800"
  824. data2["updated_on"] = "1431414800"
  825. data2["last_updated"] = "1431414800"
  826. self.assertDictEqual(data, data2)
  827. @patch("pagure.lib.notify.send_email")
  828. def test_api_pull_request_by_uid_view(self, send_email):
  829. """ Test the api_pull_request_by_uid_view method of the flask api. """
  830. send_email.return_value = True
  831. tests.create_projects(self.session)
  832. tests.create_tokens(self.session)
  833. tests.create_tokens_acl(self.session)
  834. # Create a pull-request
  835. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  836. forked_repo = pagure.lib.query.get_authorized_project(
  837. self.session, "test"
  838. )
  839. req = pagure.lib.query.new_pull_request(
  840. session=self.session,
  841. repo_from=forked_repo,
  842. branch_from="master",
  843. repo_to=repo,
  844. branch_to="master",
  845. title="test pull-request",
  846. user="pingou",
  847. )
  848. self.session.commit()
  849. self.assertEqual(req.id, 1)
  850. self.assertEqual(req.title, "test pull-request")
  851. uid = req.uid
  852. # Invalid request
  853. output = self.app.get("/api/0/pull-requests/{}".format(uid + "aaa"))
  854. self.assertEqual(output.status_code, 404)
  855. data = json.loads(output.get_data(as_text=True))
  856. self.assertDictEqual(
  857. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  858. )
  859. # Valid issue
  860. output = self.app.get("/api/0/pull-requests/{}".format(uid))
  861. self.assertEqual(output.status_code, 200)
  862. data = json.loads(output.get_data(as_text=True))
  863. data["date_created"] = "1431414800"
  864. data["updated_on"] = "1431414800"
  865. data["project"]["date_created"] = "1431414800"
  866. data["project"]["date_modified"] = "1431414800"
  867. data["repo_from"]["date_created"] = "1431414800"
  868. data["repo_from"]["date_modified"] = "1431414800"
  869. data["last_updated"] = "1431414800"
  870. expected_data = {
  871. "assignee": None,
  872. "branch": "master",
  873. "branch_from": "master",
  874. "cached_merge_status": "unknown",
  875. "closed_at": None,
  876. "closed_by": None,
  877. "comments": [],
  878. "commit_start": None,
  879. "commit_stop": None,
  880. "date_created": "1431414800",
  881. "full_url": "http://localhost.localdomain/test/pull-request/1",
  882. "id": 1,
  883. "initial_comment": None,
  884. "last_updated": "1431414800",
  885. "project": {
  886. "access_groups": {
  887. "admin": [],
  888. "collaborator": [],
  889. "commit": [],
  890. "ticket": [],
  891. },
  892. "access_users": {
  893. "admin": [],
  894. "collaborator": [],
  895. "commit": [],
  896. "owner": ["pingou"],
  897. "ticket": [],
  898. },
  899. "close_status": [
  900. "Invalid",
  901. "Insufficient data",
  902. "Fixed",
  903. "Duplicate",
  904. ],
  905. "custom_keys": [],
  906. "date_created": "1431414800",
  907. "date_modified": "1431414800",
  908. "description": "test project #1",
  909. "full_url": "http://localhost.localdomain/test",
  910. "fullname": "test",
  911. "url_path": "test",
  912. "id": 1,
  913. "milestones": {},
  914. "name": "test",
  915. "namespace": None,
  916. "parent": None,
  917. "priorities": {},
  918. "tags": [],
  919. "user": {
  920. "full_url": "http://localhost.localdomain/user/pingou",
  921. "fullname": "PY C",
  922. "name": "pingou",
  923. "url_path": "user/pingou",
  924. },
  925. },
  926. "remote_git": None,
  927. "repo_from": {
  928. "access_groups": {
  929. "admin": [],
  930. "collaborator": [],
  931. "commit": [],
  932. "ticket": [],
  933. },
  934. "access_users": {
  935. "admin": [],
  936. "collaborator": [],
  937. "commit": [],
  938. "owner": ["pingou"],
  939. "ticket": [],
  940. },
  941. "close_status": [
  942. "Invalid",
  943. "Insufficient data",
  944. "Fixed",
  945. "Duplicate",
  946. ],
  947. "custom_keys": [],
  948. "date_created": "1431414800",
  949. "date_modified": "1431414800",
  950. "description": "test project #1",
  951. "full_url": "http://localhost.localdomain/test",
  952. "fullname": "test",
  953. "url_path": "test",
  954. "id": 1,
  955. "milestones": {},
  956. "name": "test",
  957. "namespace": None,
  958. "parent": None,
  959. "priorities": {},
  960. "tags": [],
  961. "user": {
  962. "full_url": "http://localhost.localdomain/user/pingou",
  963. "fullname": "PY C",
  964. "name": "pingou",
  965. "url_path": "user/pingou",
  966. },
  967. },
  968. "status": "Open",
  969. "tags": [],
  970. "threshold_reached": None,
  971. "title": "test pull-request",
  972. "uid": uid,
  973. "updated_on": "1431414800",
  974. "user": {
  975. "full_url": "http://localhost.localdomain/user/pingou",
  976. "fullname": "PY C",
  977. "name": "pingou",
  978. "url_path": "user/pingou",
  979. },
  980. }
  981. self.assertDictEqual(data, expected_data)
  982. headers = {"Authorization": "token aaabbbcccddd"}
  983. # Access Pull-Request authenticated
  984. output = self.app.get(
  985. "/api/0/pull-requests/{}".format(uid), headers=headers
  986. )
  987. self.assertEqual(output.status_code, 200)
  988. data2 = json.loads(output.get_data(as_text=True))
  989. data2["date_created"] = "1431414800"
  990. data2["project"]["date_created"] = "1431414800"
  991. data2["project"]["date_modified"] = "1431414800"
  992. data2["repo_from"]["date_created"] = "1431414800"
  993. data2["repo_from"]["date_modified"] = "1431414800"
  994. data2["date_created"] = "1431414800"
  995. data2["updated_on"] = "1431414800"
  996. data2["last_updated"] = "1431414800"
  997. self.assertDictEqual(data, data2)
  998. @patch("pagure.lib.notify.send_email")
  999. def test_api_pull_request_close_pr_disabled(self, send_email):
  1000. """ Test the api_pull_request_close method of the flask api. """
  1001. send_email.return_value = True
  1002. tests.create_projects(self.session)
  1003. tests.create_tokens(self.session)
  1004. tests.create_tokens_acl(self.session)
  1005. # Create the pull-request to close
  1006. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1007. forked_repo = pagure.lib.query.get_authorized_project(
  1008. self.session, "test"
  1009. )
  1010. req = pagure.lib.query.new_pull_request(
  1011. session=self.session,
  1012. repo_from=forked_repo,
  1013. branch_from="master",
  1014. repo_to=repo,
  1015. branch_to="master",
  1016. title="test pull-request",
  1017. user="pingou",
  1018. )
  1019. self.session.commit()
  1020. self.assertEqual(req.id, 1)
  1021. self.assertEqual(req.title, "test pull-request")
  1022. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1023. settings = repo.settings
  1024. settings["pull_requests"] = False
  1025. repo.settings = settings
  1026. self.session.add(repo)
  1027. self.session.commit()
  1028. headers = {"Authorization": "token aaabbbcccddd"}
  1029. output = self.app.post(
  1030. "/api/0/test/pull-request/1/close", headers=headers
  1031. )
  1032. self.assertEqual(output.status_code, 404)
  1033. data = json.loads(output.get_data(as_text=True))
  1034. self.assertDictEqual(
  1035. data,
  1036. {
  1037. "error": "Pull-Request have been deactivated for this project",
  1038. "error_code": "EPULLREQUESTSDISABLED",
  1039. },
  1040. )
  1041. @patch("pagure.lib.notify.send_email")
  1042. def test_api_pull_request_close(self, send_email):
  1043. """ Test the api_pull_request_close method of the flask api. """
  1044. send_email.return_value = True
  1045. tests.create_projects(self.session)
  1046. tests.create_tokens(self.session)
  1047. tests.create_tokens_acl(self.session)
  1048. # Create the pull-request to close
  1049. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1050. forked_repo = pagure.lib.query.get_authorized_project(
  1051. self.session, "test"
  1052. )
  1053. req = pagure.lib.query.new_pull_request(
  1054. session=self.session,
  1055. repo_from=forked_repo,
  1056. branch_from="master",
  1057. repo_to=repo,
  1058. branch_to="master",
  1059. title="test pull-request",
  1060. user="pingou",
  1061. )
  1062. self.session.commit()
  1063. self.assertEqual(req.id, 1)
  1064. self.assertEqual(req.title, "test pull-request")
  1065. headers = {"Authorization": "token aaabbbcccddd"}
  1066. # Invalid project
  1067. output = self.app.post(
  1068. "/api/0/foo/pull-request/1/close", headers=headers
  1069. )
  1070. self.assertEqual(output.status_code, 404)
  1071. data = json.loads(output.get_data(as_text=True))
  1072. self.assertDictEqual(
  1073. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  1074. )
  1075. # Valid token, wrong project
  1076. output = self.app.post(
  1077. "/api/0/test2/pull-request/1/close", headers=headers
  1078. )
  1079. self.assertEqual(output.status_code, 401)
  1080. data = json.loads(output.get_data(as_text=True))
  1081. self.assertEqual(
  1082. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  1083. )
  1084. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  1085. # Invalid PR
  1086. output = self.app.post(
  1087. "/api/0/test/pull-request/2/close", headers=headers
  1088. )
  1089. self.assertEqual(output.status_code, 404)
  1090. data = json.loads(output.get_data(as_text=True))
  1091. self.assertDictEqual(
  1092. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  1093. )
  1094. # Create a token for foo for this project
  1095. item = pagure.lib.model.Token(
  1096. id="foobar_token",
  1097. user_id=2,
  1098. project_id=1,
  1099. expiration=datetime.datetime.utcnow()
  1100. + datetime.timedelta(days=30),
  1101. )
  1102. self.session.add(item)
  1103. self.session.commit()
  1104. # Allow the token to close PR
  1105. acls = pagure.lib.query.get_acls(self.session)
  1106. for acl in acls:
  1107. if acl.name == "pull_request_close":
  1108. break
  1109. item = pagure.lib.model.TokenAcl(
  1110. token_id="foobar_token", acl_id=acl.id
  1111. )
  1112. self.session.add(item)
  1113. self.session.commit()
  1114. headers = {"Authorization": "token foobar_token"}
  1115. # User not admin
  1116. output = self.app.post(
  1117. "/api/0/test/pull-request/1/close", headers=headers
  1118. )
  1119. self.assertEqual(output.status_code, 403)
  1120. data = json.loads(output.get_data(as_text=True))
  1121. self.assertDictEqual(
  1122. data,
  1123. {
  1124. "error": "You are not allowed to merge/close pull-request "
  1125. "for this project",
  1126. "error_code": "ENOPRCLOSE",
  1127. },
  1128. )
  1129. headers = {"Authorization": "token aaabbbcccddd"}
  1130. # Close PR
  1131. output = self.app.post(
  1132. "/api/0/test/pull-request/1/close", headers=headers
  1133. )
  1134. self.assertEqual(output.status_code, 200)
  1135. data = json.loads(output.get_data(as_text=True))
  1136. self.assertDictEqual(data, {"message": "Pull-request closed!"})
  1137. @patch("pagure.lib.notify.send_email")
  1138. def test_api_pull_request_merge_pr_disabled(self, send_email):
  1139. """Test the api_pull_request_merge method of the flask api when PR
  1140. are disabled."""
  1141. send_email.return_value = True
  1142. tests.create_projects(self.session)
  1143. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1144. tests.create_projects_git(
  1145. os.path.join(self.path, "requests"), bare=True
  1146. )
  1147. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  1148. tests.add_commit_git_repo(
  1149. os.path.join(self.path, "repos", "test.git"), branch="test"
  1150. )
  1151. tests.create_tokens(self.session)
  1152. tests.create_tokens_acl(self.session)
  1153. # Create the pull-request to close
  1154. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1155. forked_repo = pagure.lib.query.get_authorized_project(
  1156. self.session, "test"
  1157. )
  1158. req = pagure.lib.query.new_pull_request(
  1159. session=self.session,
  1160. repo_from=forked_repo,
  1161. branch_from="test",
  1162. repo_to=repo,
  1163. branch_to="master",
  1164. title="test pull-request",
  1165. user="pingou",
  1166. )
  1167. self.session.commit()
  1168. self.assertEqual(req.id, 1)
  1169. self.assertEqual(req.title, "test pull-request")
  1170. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1171. settings = repo.settings
  1172. settings["pull_requests"] = False
  1173. repo.settings = settings
  1174. self.session.add(repo)
  1175. self.session.commit()
  1176. headers = {"Authorization": "token aaabbbcccddd"}
  1177. output = self.app.post(
  1178. "/api/0/test/pull-request/1/merge", headers=headers
  1179. )
  1180. self.assertEqual(output.status_code, 404)
  1181. data = json.loads(output.get_data(as_text=True))
  1182. self.assertDictEqual(
  1183. data,
  1184. {
  1185. "error": "Pull-Request have been deactivated for this project",
  1186. "error_code": "EPULLREQUESTSDISABLED",
  1187. },
  1188. )
  1189. @patch("pagure.lib.notify.send_email")
  1190. def test_api_pull_request_merge_only_assigned(self, send_email):
  1191. """Test the api_pull_request_merge method of the flask api when
  1192. only assignee can merge the PR and the PR isn't assigned."""
  1193. send_email.return_value = True
  1194. tests.create_projects(self.session)
  1195. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1196. tests.create_projects_git(
  1197. os.path.join(self.path, "requests"), bare=True
  1198. )
  1199. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  1200. tests.add_commit_git_repo(
  1201. os.path.join(self.path, "repos", "test.git"), branch="test"
  1202. )
  1203. tests.create_tokens(self.session)
  1204. tests.create_tokens_acl(self.session)
  1205. # Create the pull-request to close
  1206. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1207. forked_repo = pagure.lib.query.get_authorized_project(
  1208. self.session, "test"
  1209. )
  1210. req = pagure.lib.query.new_pull_request(
  1211. session=self.session,
  1212. repo_from=forked_repo,
  1213. branch_from="test",
  1214. repo_to=repo,
  1215. branch_to="master",
  1216. title="test pull-request",
  1217. user="pingou",
  1218. )
  1219. self.session.commit()
  1220. self.assertEqual(req.id, 1)
  1221. self.assertEqual(req.title, "test pull-request")
  1222. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1223. settings = repo.settings
  1224. settings["Only_assignee_can_merge_pull-request"] = True
  1225. repo.settings = settings
  1226. self.session.add(repo)
  1227. self.session.commit()
  1228. headers = {"Authorization": "token aaabbbcccddd"}
  1229. output = self.app.post(
  1230. "/api/0/test/pull-request/1/merge", headers=headers
  1231. )
  1232. self.assertEqual(output.status_code, 403)
  1233. data = json.loads(output.get_data(as_text=True))
  1234. self.assertDictEqual(
  1235. data,
  1236. {
  1237. "error": "This request must be assigned to be merged",
  1238. "error_code": "ENOTASSIGNED",
  1239. },
  1240. )
  1241. @patch("pagure.lib.notify.send_email")
  1242. def test_api_pull_request_merge_only_assigned_not_assignee(
  1243. self, send_email
  1244. ):
  1245. """Test the api_pull_request_merge method of the flask api when
  1246. only assignee can merge the PR and the PR isn't assigned to the
  1247. user asking to merge."""
  1248. send_email.return_value = True
  1249. tests.create_projects(self.session)
  1250. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1251. tests.create_projects_git(
  1252. os.path.join(self.path, "requests"), bare=True
  1253. )
  1254. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  1255. tests.add_commit_git_repo(
  1256. os.path.join(self.path, "repos", "test.git"), branch="test"
  1257. )
  1258. tests.create_tokens(self.session)
  1259. tests.create_tokens_acl(self.session)
  1260. # Create the pull-request to close
  1261. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1262. forked_repo = pagure.lib.query.get_authorized_project(
  1263. self.session, "test"
  1264. )
  1265. req = pagure.lib.query.new_pull_request(
  1266. session=self.session,
  1267. repo_from=forked_repo,
  1268. branch_from="test",
  1269. repo_to=repo,
  1270. branch_to="master",
  1271. title="test pull-request",
  1272. user="pingou",
  1273. )
  1274. self.session.commit()
  1275. self.assertEqual(req.id, 1)
  1276. self.assertEqual(req.title, "test pull-request")
  1277. req.assignee = pagure.lib.query.search_user(self.session, "foo")
  1278. self.session.add(req)
  1279. self.session.commit()
  1280. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1281. settings = repo.settings
  1282. settings["Only_assignee_can_merge_pull-request"] = True
  1283. repo.settings = settings
  1284. self.session.add(repo)
  1285. self.session.commit()
  1286. headers = {"Authorization": "token aaabbbcccddd"}
  1287. output = self.app.post(
  1288. "/api/0/test/pull-request/1/merge", headers=headers
  1289. )
  1290. self.assertEqual(output.status_code, 403)
  1291. data = json.loads(output.get_data(as_text=True))
  1292. self.assertDictEqual(
  1293. data,
  1294. {
  1295. "error": "Only the assignee can merge this request",
  1296. "error_code": "ENOTASSIGNEE",
  1297. },
  1298. )
  1299. @patch("pagure.lib.notify.send_email")
  1300. def test_api_pull_request_merge_minimal_score(self, send_email):
  1301. """Test the api_pull_request_merge method of the flask api when
  1302. a PR requires a certain minimal score to be merged."""
  1303. send_email.return_value = True
  1304. tests.create_projects(self.session)
  1305. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1306. tests.create_projects_git(
  1307. os.path.join(self.path, "requests"), bare=True
  1308. )
  1309. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  1310. tests.add_commit_git_repo(
  1311. os.path.join(self.path, "repos", "test.git"), branch="test"
  1312. )
  1313. tests.create_tokens(self.session)
  1314. tests.create_tokens_acl(self.session)
  1315. # Create the pull-request to close
  1316. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1317. forked_repo = pagure.lib.query.get_authorized_project(
  1318. self.session, "test"
  1319. )
  1320. req = pagure.lib.query.new_pull_request(
  1321. session=self.session,
  1322. repo_from=forked_repo,
  1323. branch_from="test",
  1324. repo_to=repo,
  1325. branch_to="master",
  1326. title="test pull-request",
  1327. user="pingou",
  1328. )
  1329. self.session.commit()
  1330. self.assertEqual(req.id, 1)
  1331. self.assertEqual(req.title, "test pull-request")
  1332. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1333. settings = repo.settings
  1334. settings["Minimum_score_to_merge_pull-request"] = 2
  1335. repo.settings = settings
  1336. self.session.add(repo)
  1337. self.session.commit()
  1338. headers = {"Authorization": "token aaabbbcccddd"}
  1339. output = self.app.post(
  1340. "/api/0/test/pull-request/1/merge", headers=headers
  1341. )
  1342. self.assertEqual(output.status_code, 403)
  1343. data = json.loads(output.get_data(as_text=True))
  1344. self.assertDictEqual(
  1345. data,
  1346. {
  1347. "error": "This request does not have the minimum review "
  1348. "score necessary to be merged",
  1349. "error_code": "EPRSCORE",
  1350. },
  1351. )
  1352. @patch("pagure.lib.notify.send_email")
  1353. def test_api_pull_request_merge(self, send_email):
  1354. """ Test the api_pull_request_merge method of the flask api. """
  1355. send_email.return_value = True
  1356. tests.create_projects(self.session)
  1357. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1358. tests.create_projects_git(
  1359. os.path.join(self.path, "requests"), bare=True
  1360. )
  1361. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  1362. tests.add_commit_git_repo(
  1363. os.path.join(self.path, "repos", "test.git"), branch="test"
  1364. )
  1365. tests.create_tokens(self.session)
  1366. tests.create_tokens_acl(self.session)
  1367. # Create the pull-request to close
  1368. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1369. forked_repo = pagure.lib.query.get_authorized_project(
  1370. self.session, "test"
  1371. )
  1372. req = pagure.lib.query.new_pull_request(
  1373. session=self.session,
  1374. repo_from=forked_repo,
  1375. branch_from="test",
  1376. repo_to=repo,
  1377. branch_to="master",
  1378. title="test pull-request",
  1379. user="pingou",
  1380. )
  1381. self.session.commit()
  1382. self.assertEqual(req.id, 1)
  1383. self.assertEqual(req.title, "test pull-request")
  1384. headers = {"Authorization": "token aaabbbcccddd"}
  1385. # Invalid project
  1386. output = self.app.post(
  1387. "/api/0/foo/pull-request/1/merge", headers=headers
  1388. )
  1389. self.assertEqual(output.status_code, 404)
  1390. data = json.loads(output.get_data(as_text=True))
  1391. self.assertDictEqual(
  1392. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  1393. )
  1394. # Valid token, wrong project
  1395. output = self.app.post(
  1396. "/api/0/test2/pull-request/1/merge", headers=headers
  1397. )
  1398. self.assertEqual(output.status_code, 401)
  1399. data = json.loads(output.get_data(as_text=True))
  1400. self.assertEqual(
  1401. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  1402. )
  1403. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  1404. # Invalid PR
  1405. output = self.app.post(
  1406. "/api/0/test/pull-request/2/merge", headers=headers
  1407. )
  1408. self.assertEqual(output.status_code, 404)
  1409. data = json.loads(output.get_data(as_text=True))
  1410. self.assertDictEqual(
  1411. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  1412. )
  1413. # Create a token for foo for this project
  1414. item = pagure.lib.model.Token(
  1415. id="foobar_token",
  1416. user_id=2,
  1417. project_id=1,
  1418. expiration=datetime.datetime.utcnow()
  1419. + datetime.timedelta(days=30),
  1420. )
  1421. self.session.add(item)
  1422. self.session.commit()
  1423. # Allow the token to merge PR
  1424. acls = pagure.lib.query.get_acls(self.session)
  1425. for acl in acls:
  1426. if acl.name == "pull_request_merge":
  1427. break
  1428. item = pagure.lib.model.TokenAcl(
  1429. token_id="foobar_token", acl_id=acl.id
  1430. )
  1431. self.session.add(item)
  1432. self.session.commit()
  1433. headers = {"Authorization": "token foobar_token"}
  1434. # User not admin
  1435. output = self.app.post(
  1436. "/api/0/test/pull-request/1/merge", headers=headers
  1437. )
  1438. self.assertEqual(output.status_code, 403)
  1439. data = json.loads(output.get_data(as_text=True))
  1440. self.assertDictEqual(
  1441. data,
  1442. {
  1443. "error": "You are not allowed to merge/close pull-request "
  1444. "for this project",
  1445. "error_code": "ENOPRCLOSE",
  1446. },
  1447. )
  1448. headers = {"Authorization": "token aaabbbcccddd"}
  1449. # Merge PR
  1450. output = self.app.post(
  1451. "/api/0/test/pull-request/1/merge", headers=headers
  1452. )
  1453. self.assertEqual(output.status_code, 200)
  1454. data = json.loads(output.get_data(as_text=True))
  1455. self.assertDictEqual(data, {"message": "Changes merged!"})
  1456. @patch("pagure.lib.notify.send_email")
  1457. def test_api_pull_request_merge_conflicting(self, send_email):
  1458. """ Test the api_pull_request_merge method of the flask api. """
  1459. send_email.return_value = True
  1460. tests.create_projects(self.session)
  1461. tests.add_content_git_repo(
  1462. os.path.join(self.path, "repos", "test.git")
  1463. )
  1464. # Fork
  1465. project = pagure.lib.query.get_authorized_project(self.session, "test")
  1466. task = pagure.lib.query.fork_project(
  1467. session=self.session, user="pingou", repo=project
  1468. )
  1469. self.session.commit()
  1470. self.assertEqual(
  1471. task.get(),
  1472. {
  1473. "endpoint": "ui_ns.view_repo",
  1474. "repo": "test",
  1475. "namespace": None,
  1476. "username": "pingou",
  1477. },
  1478. )
  1479. # Add content to the fork
  1480. tests.add_content_to_git(
  1481. os.path.join(self.path, "repos", "forks", "pingou", "test.git"),
  1482. filename="foobar",
  1483. content="content from the fork",
  1484. )
  1485. # Add content to the main repo, so they conflict
  1486. tests.add_content_to_git(
  1487. os.path.join(self.path, "repos", "test.git"),
  1488. filename="foobar",
  1489. content="content from the main repo",
  1490. )
  1491. project = pagure.lib.query.get_authorized_project(self.session, "test")
  1492. fork = pagure.lib.query.get_authorized_project(
  1493. self.session, "test", user="pingou"
  1494. )
  1495. tests.create_tokens(self.session)
  1496. tests.create_tokens_acl(self.session)
  1497. # Create the pull-request to close
  1498. req = pagure.lib.query.new_pull_request(
  1499. session=self.session,
  1500. repo_from=fork,
  1501. branch_from="master",
  1502. repo_to=project,
  1503. branch_to="master",
  1504. title="test pull-request",
  1505. user="pingou",
  1506. )
  1507. self.session.commit()
  1508. self.assertEqual(req.id, 1)
  1509. self.assertEqual(req.title, "test pull-request")
  1510. headers = {"Authorization": "token aaabbbcccddd"}
  1511. # Merge PR
  1512. output = self.app.post(
  1513. "/api/0/test/pull-request/1/merge", headers=headers
  1514. )
  1515. self.assertEqual(output.status_code, 409)
  1516. data = json.loads(output.get_data(as_text=True))
  1517. self.assertDictEqual(
  1518. data,
  1519. {
  1520. "error": "This pull-request conflicts and thus cannot be merged",
  1521. "error_code": "EPRCONFLICTS",
  1522. },
  1523. )
  1524. @patch("pagure.lib.notify.send_email")
  1525. def test_api_pull_request_merge_user_token(self, send_email):
  1526. """ Test the api_pull_request_merge method of the flask api. """
  1527. send_email.return_value = True
  1528. tests.create_projects(self.session)
  1529. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1530. tests.create_projects_git(
  1531. os.path.join(self.path, "requests"), bare=True
  1532. )
  1533. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  1534. tests.add_commit_git_repo(
  1535. os.path.join(self.path, "repos", "test.git"), branch="test"
  1536. )
  1537. tests.create_tokens(self.session, project_id=None)
  1538. tests.create_tokens_acl(self.session)
  1539. # Create the pull-request to close
  1540. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1541. forked_repo = pagure.lib.query.get_authorized_project(
  1542. self.session, "test"
  1543. )
  1544. req = pagure.lib.query.new_pull_request(
  1545. session=self.session,
  1546. repo_from=forked_repo,
  1547. branch_from="test",
  1548. repo_to=repo,
  1549. branch_to="master",
  1550. title="test pull-request",
  1551. user="pingou",
  1552. )
  1553. self.session.commit()
  1554. self.assertEqual(req.id, 1)
  1555. self.assertEqual(req.title, "test pull-request")
  1556. headers = {"Authorization": "token aaabbbcccddd"}
  1557. # Invalid project
  1558. output = self.app.post(
  1559. "/api/0/foo/pull-request/1/merge", headers=headers
  1560. )
  1561. self.assertEqual(output.status_code, 404)
  1562. data = json.loads(output.get_data(as_text=True))
  1563. self.assertDictEqual(
  1564. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  1565. )
  1566. # Valid token, invalid PR
  1567. output = self.app.post(
  1568. "/api/0/test2/pull-request/1/merge", headers=headers
  1569. )
  1570. self.assertEqual(output.status_code, 404)
  1571. data = json.loads(output.get_data(as_text=True))
  1572. self.assertDictEqual(
  1573. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  1574. )
  1575. # Valid token, invalid PR - other project
  1576. output = self.app.post(
  1577. "/api/0/test/pull-request/2/merge", headers=headers
  1578. )
  1579. self.assertEqual(output.status_code, 404)
  1580. data = json.loads(output.get_data(as_text=True))
  1581. self.assertDictEqual(
  1582. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  1583. )
  1584. # Create a token for foo for this project
  1585. item = pagure.lib.model.Token(
  1586. id="foobar_token",
  1587. user_id=2,
  1588. project_id=1,
  1589. expiration=datetime.datetime.utcnow()
  1590. + datetime.timedelta(days=30),
  1591. )
  1592. self.session.add(item)
  1593. self.session.commit()
  1594. # Allow the token to merge PR
  1595. acls = pagure.lib.query.get_acls(self.session)
  1596. acl = None
  1597. for acl in acls:
  1598. if acl.name == "pull_request_merge":
  1599. break
  1600. item = pagure.lib.model.TokenAcl(
  1601. token_id="foobar_token", acl_id=acl.id
  1602. )
  1603. self.session.add(item)
  1604. self.session.commit()
  1605. headers = {"Authorization": "token foobar_token"}
  1606. # User not admin
  1607. output = self.app.post(
  1608. "/api/0/test/pull-request/1/merge", headers=headers
  1609. )
  1610. self.assertEqual(output.status_code, 403)
  1611. data = json.loads(output.get_data(as_text=True))
  1612. self.assertDictEqual(
  1613. data,
  1614. {
  1615. "error": "You are not allowed to merge/close pull-request "
  1616. "for this project",
  1617. "error_code": "ENOPRCLOSE",
  1618. },
  1619. )
  1620. headers = {"Authorization": "token aaabbbcccddd"}
  1621. # Merge PR
  1622. output = self.app.post(
  1623. "/api/0/test/pull-request/1/merge", headers=headers
  1624. )
  1625. self.assertEqual(output.status_code, 200)
  1626. data = json.loads(output.get_data(as_text=True))
  1627. self.assertDictEqual(data, {"message": "Changes merged!"})
  1628. @patch("pagure.lib.notify.send_email")
  1629. def test_api_pull_request_add_comment(self, mockemail):
  1630. """ Test the api_pull_request_add_comment method of the flask api. """
  1631. mockemail.return_value = True
  1632. tests.create_projects(self.session)
  1633. tests.create_tokens(self.session)
  1634. tests.create_tokens_acl(self.session)
  1635. headers = {"Authorization": "token aaabbbcccddd"}
  1636. # Invalid project
  1637. output = self.app.post(
  1638. "/api/0/foo/pull-request/1/comment", headers=headers
  1639. )
  1640. self.assertEqual(output.status_code, 404)
  1641. data = json.loads(output.get_data(as_text=True))
  1642. self.assertDictEqual(
  1643. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  1644. )
  1645. # Valid token, wrong project
  1646. output = self.app.post(
  1647. "/api/0/test2/pull-request/1/comment", headers=headers
  1648. )
  1649. self.assertEqual(output.status_code, 401)
  1650. data = json.loads(output.get_data(as_text=True))
  1651. self.assertEqual(
  1652. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  1653. )
  1654. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  1655. # No input
  1656. output = self.app.post(
  1657. "/api/0/test/pull-request/1/comment", headers=headers
  1658. )
  1659. self.assertEqual(output.status_code, 404)
  1660. data = json.loads(output.get_data(as_text=True))
  1661. self.assertDictEqual(
  1662. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  1663. )
  1664. # Create a pull-request
  1665. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1666. forked_repo = pagure.lib.query.get_authorized_project(
  1667. self.session, "test"
  1668. )
  1669. req = pagure.lib.query.new_pull_request(
  1670. session=self.session,
  1671. repo_from=forked_repo,
  1672. branch_from="master",
  1673. repo_to=repo,
  1674. branch_to="master",
  1675. title="test pull-request",
  1676. user="pingou",
  1677. )
  1678. self.session.commit()
  1679. self.assertEqual(req.id, 1)
  1680. self.assertEqual(req.title, "test pull-request")
  1681. # Check comments before
  1682. self.session.commit()
  1683. request = pagure.lib.query.search_pull_requests(
  1684. self.session, project_id=1, requestid=1
  1685. )
  1686. self.assertEqual(len(request.comments), 0)
  1687. data = {"title": "test issue"}
  1688. # Incomplete request
  1689. output = self.app.post(
  1690. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  1691. )
  1692. self.assertEqual(output.status_code, 400)
  1693. data = json.loads(output.get_data(as_text=True))
  1694. self.assertDictEqual(
  1695. data,
  1696. {
  1697. "error": "Invalid or incomplete input submitted",
  1698. "error_code": "EINVALIDREQ",
  1699. "errors": {"comment": ["This field is required."]},
  1700. },
  1701. )
  1702. # No change
  1703. self.session.commit()
  1704. request = pagure.lib.query.search_pull_requests(
  1705. self.session, project_id=1, requestid=1
  1706. )
  1707. self.assertEqual(len(request.comments), 0)
  1708. data = {"comment": "This is a very interesting question"}
  1709. # Valid request
  1710. output = self.app.post(
  1711. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  1712. )
  1713. self.assertEqual(output.status_code, 200)
  1714. data = json.loads(output.get_data(as_text=True))
  1715. self.assertDictEqual(data, {"message": "Comment added"})
  1716. # One comment added
  1717. self.session.commit()
  1718. request = pagure.lib.query.search_pull_requests(
  1719. self.session, project_id=1, requestid=1
  1720. )
  1721. self.assertEqual(len(request.comments), 1)
  1722. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1723. def test_api_pull_request_add_comment_wrong_user(self):
  1724. """Test the api_pull_request_add_comment method of the flask api
  1725. when the user is not found in the DB."""
  1726. tests.create_projects(self.session)
  1727. tests.create_tokens(self.session, project_id=None)
  1728. tests.create_tokens_acl(self.session)
  1729. headers = {"Authorization": "token aaabbbcccddd"}
  1730. # Create a pull-request
  1731. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1732. forked_repo = pagure.lib.query.get_authorized_project(
  1733. self.session, "test"
  1734. )
  1735. req = pagure.lib.query.new_pull_request(
  1736. session=self.session,
  1737. repo_from=forked_repo,
  1738. branch_from="master",
  1739. repo_to=repo,
  1740. branch_to="master",
  1741. title="test pull-request",
  1742. user="pingou",
  1743. )
  1744. self.session.commit()
  1745. self.assertEqual(req.id, 1)
  1746. self.assertEqual(req.title, "test pull-request")
  1747. data = {"comment": "This is a very interesting question"}
  1748. # Valid request
  1749. with patch(
  1750. "pagure.lib.query.add_pull_request_comment",
  1751. side_effect=pagure.exceptions.PagureException("error"),
  1752. ):
  1753. output = self.app.post(
  1754. "/api/0/test/pull-request/1/comment",
  1755. data=data,
  1756. headers=headers,
  1757. )
  1758. self.assertEqual(output.status_code, 400)
  1759. data = json.loads(output.get_data(as_text=True))
  1760. self.assertDictEqual(
  1761. data, {"error": "error", "error_code": "ENOCODE"}
  1762. )
  1763. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1764. def test_api_pull_request_add_comment_pr_disabled(self):
  1765. """Test the api_pull_request_add_comment method of the flask api
  1766. when PRs are disabled."""
  1767. tests.create_projects(self.session)
  1768. tests.create_tokens(self.session, project_id=None)
  1769. tests.create_tokens_acl(self.session)
  1770. headers = {"Authorization": "token aaabbbcccddd"}
  1771. # Create a pull-request
  1772. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1773. forked_repo = pagure.lib.query.get_authorized_project(
  1774. self.session, "test"
  1775. )
  1776. req = pagure.lib.query.new_pull_request(
  1777. session=self.session,
  1778. repo_from=forked_repo,
  1779. branch_from="master",
  1780. repo_to=repo,
  1781. branch_to="master",
  1782. title="test pull-request",
  1783. user="pingou",
  1784. )
  1785. self.session.commit()
  1786. self.assertEqual(req.id, 1)
  1787. self.assertEqual(req.title, "test pull-request")
  1788. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1789. settings = repo.settings
  1790. settings["pull_requests"] = False
  1791. repo.settings = settings
  1792. self.session.add(repo)
  1793. self.session.commit()
  1794. data = {"comment": "This is a very interesting question"}
  1795. # Valid request
  1796. output = self.app.post(
  1797. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  1798. )
  1799. self.assertEqual(output.status_code, 404)
  1800. data = json.loads(output.get_data(as_text=True))
  1801. self.assertDictEqual(
  1802. data,
  1803. {
  1804. "error": "Pull-Request have been deactivated for this project",
  1805. "error_code": "EPULLREQUESTSDISABLED",
  1806. },
  1807. )
  1808. # no comment added
  1809. self.session.commit()
  1810. request = pagure.lib.query.search_pull_requests(
  1811. self.session, project_id=1, requestid=1
  1812. )
  1813. self.assertEqual(len(request.comments), 0)
  1814. @patch("pagure.lib.notify.send_email")
  1815. def test_api_pull_request_add_comment_user_token(self, mockemail):
  1816. """ Test the api_pull_request_add_comment method of the flask api. """
  1817. mockemail.return_value = True
  1818. tests.create_projects(self.session)
  1819. tests.create_tokens(self.session, project_id=None)
  1820. tests.create_tokens_acl(self.session)
  1821. headers = {"Authorization": "token aaabbbcccddd"}
  1822. # Invalid project
  1823. output = self.app.post(
  1824. "/api/0/foo/pull-request/1/comment", headers=headers
  1825. )
  1826. self.assertEqual(output.status_code, 404)
  1827. data = json.loads(output.get_data(as_text=True))
  1828. self.assertDictEqual(
  1829. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  1830. )
  1831. # Valid token, invalid request
  1832. output = self.app.post(
  1833. "/api/0/test2/pull-request/1/comment", headers=headers
  1834. )
  1835. self.assertEqual(output.status_code, 404)
  1836. data = json.loads(output.get_data(as_text=True))
  1837. self.assertDictEqual(
  1838. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  1839. )
  1840. # Valid token, invalid request in another project
  1841. output = self.app.post(
  1842. "/api/0/test/pull-request/1/comment", headers=headers
  1843. )
  1844. self.assertEqual(output.status_code, 404)
  1845. data = json.loads(output.get_data(as_text=True))
  1846. self.assertDictEqual(
  1847. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  1848. )
  1849. # Create a pull-request
  1850. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1851. forked_repo = pagure.lib.query.get_authorized_project(
  1852. self.session, "test"
  1853. )
  1854. req = pagure.lib.query.new_pull_request(
  1855. session=self.session,
  1856. repo_from=forked_repo,
  1857. branch_from="master",
  1858. repo_to=repo,
  1859. branch_to="master",
  1860. title="test pull-request",
  1861. user="pingou",
  1862. )
  1863. self.session.commit()
  1864. self.assertEqual(req.id, 1)
  1865. self.assertEqual(req.title, "test pull-request")
  1866. # Check comments before
  1867. self.session.commit()
  1868. request = pagure.lib.query.search_pull_requests(
  1869. self.session, project_id=1, requestid=1
  1870. )
  1871. self.assertEqual(len(request.comments), 0)
  1872. data = {"title": "test issue"}
  1873. # Incomplete request
  1874. output = self.app.post(
  1875. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  1876. )
  1877. self.assertEqual(output.status_code, 400)
  1878. data = json.loads(output.get_data(as_text=True))
  1879. self.assertDictEqual(
  1880. data,
  1881. {
  1882. "error": "Invalid or incomplete input submitted",
  1883. "error_code": "EINVALIDREQ",
  1884. "errors": {"comment": ["This field is required."]},
  1885. },
  1886. )
  1887. # No change
  1888. self.session.commit()
  1889. request = pagure.lib.query.search_pull_requests(
  1890. self.session, project_id=1, requestid=1
  1891. )
  1892. self.assertEqual(len(request.comments), 0)
  1893. data = {"comment": "This is a very interesting question"}
  1894. # Valid request
  1895. output = self.app.post(
  1896. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  1897. )
  1898. self.assertEqual(output.status_code, 200)
  1899. data = json.loads(output.get_data(as_text=True))
  1900. self.assertDictEqual(data, {"message": "Comment added"})
  1901. # One comment added
  1902. self.session.commit()
  1903. request = pagure.lib.query.search_pull_requests(
  1904. self.session, project_id=1, requestid=1
  1905. )
  1906. self.assertEqual(len(request.comments), 1)
  1907. @patch("pagure.lib.notify.send_email")
  1908. def test_api_subscribe_pull_request_pr_disabled(self, p_send_email):
  1909. """ Test the api_subscribe_pull_request method of the flask api. """
  1910. p_send_email.return_value = True
  1911. tests.create_projects(self.session)
  1912. tests.create_tokens(self.session)
  1913. tests.create_tokens_acl(self.session)
  1914. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1915. settings = repo.settings
  1916. settings["pull_requests"] = False
  1917. repo.settings = settings
  1918. self.session.add(repo)
  1919. self.session.commit()
  1920. headers = {"Authorization": "token aaabbbcccddd"}
  1921. # Invalid project
  1922. output = self.app.post(
  1923. "/api/0/test/pull-request/1/subscribe", headers=headers
  1924. )
  1925. self.assertEqual(output.status_code, 404)
  1926. data = json.loads(output.get_data(as_text=True))
  1927. self.assertDictEqual(
  1928. data,
  1929. {
  1930. "error": "Pull-Request have been deactivated for this project",
  1931. "error_code": "EPULLREQUESTSDISABLED",
  1932. },
  1933. )
  1934. @patch("pagure.lib.git.update_git")
  1935. @patch("pagure.lib.notify.send_email")
  1936. def test_api_subscribe_pull_request_invalid_token(
  1937. self, p_send_email, p_ugt
  1938. ):
  1939. """ Test the api_subscribe_pull_request method of the flask api. """
  1940. p_send_email.return_value = True
  1941. p_ugt.return_value = True
  1942. item = pagure.lib.model.User(
  1943. user="bar",
  1944. fullname="bar foo",
  1945. password="foo",
  1946. default_email="bar@bar.com",
  1947. )
  1948. self.session.add(item)
  1949. item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
  1950. self.session.add(item)
  1951. self.session.commit()
  1952. tests.create_projects(self.session)
  1953. tests.create_tokens(self.session, user_id=3, project_id=2)
  1954. tests.create_tokens_acl(self.session)
  1955. headers = {"Authorization": "token aaabbbcccddd"}
  1956. # Create pull-request
  1957. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1958. req = pagure.lib.query.new_pull_request(
  1959. session=self.session,
  1960. repo_from=repo,
  1961. branch_from="feature",
  1962. repo_to=repo,
  1963. branch_to="master",
  1964. title="test pull-request",
  1965. user="pingou",
  1966. )
  1967. self.session.commit()
  1968. self.assertEqual(req.id, 1)
  1969. self.assertEqual(req.title, "test pull-request")
  1970. # Check subscribtion before
  1971. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1972. request = pagure.lib.query.search_pull_requests(
  1973. self.session, project_id=1, requestid=1
  1974. )
  1975. self.assertEqual(
  1976. pagure.lib.query.get_watch_list(self.session, request),
  1977. set(["pingou"]),
  1978. )
  1979. data = {}
  1980. output = self.app.post(
  1981. "/api/0/test/pull-request/1/subscribe", data=data, headers=headers
  1982. )
  1983. self.assertEqual(output.status_code, 401)
  1984. data = json.loads(output.get_data(as_text=True))
  1985. self.assertDictEqual(
  1986. data,
  1987. {
  1988. "error": "Invalid or expired token. Please visit "
  1989. "http://localhost.localdomain/settings#nav-api-tab to get or "
  1990. "renew your API token.",
  1991. "error_code": "EINVALIDTOK",
  1992. },
  1993. )
  1994. @patch("pagure.lib.git.update_git")
  1995. @patch("pagure.lib.notify.send_email")
  1996. def test_api_subscribe_pull_request(self, p_send_email, p_ugt):
  1997. """ Test the api_subscribe_pull_request method of the flask api. """
  1998. p_send_email.return_value = True
  1999. p_ugt.return_value = True
  2000. item = pagure.lib.model.User(
  2001. user="bar",
  2002. fullname="bar foo",
  2003. password="foo",
  2004. default_email="bar@bar.com",
  2005. )
  2006. self.session.add(item)
  2007. item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
  2008. self.session.add(item)
  2009. self.session.commit()
  2010. tests.create_projects(self.session)
  2011. tests.create_tokens(self.session, user_id=3)
  2012. tests.create_tokens_acl(self.session)
  2013. headers = {"Authorization": "token aaabbbcccddd"}
  2014. # Invalid project
  2015. output = self.app.post(
  2016. "/api/0/foo/pull-request/1/subscribe", headers=headers
  2017. )
  2018. self.assertEqual(output.status_code, 404)
  2019. data = json.loads(output.get_data(as_text=True))
  2020. self.assertDictEqual(
  2021. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2022. )
  2023. # Valid token, wrong project
  2024. output = self.app.post(
  2025. "/api/0/test2/pull-request/1/subscribe", headers=headers
  2026. )
  2027. self.assertEqual(output.status_code, 401)
  2028. data = json.loads(output.get_data(as_text=True))
  2029. self.assertEqual(
  2030. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  2031. )
  2032. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  2033. # No input
  2034. output = self.app.post(
  2035. "/api/0/test/pull-request/1/subscribe", headers=headers
  2036. )
  2037. self.assertEqual(output.status_code, 404)
  2038. data = json.loads(output.get_data(as_text=True))
  2039. self.assertDictEqual(
  2040. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  2041. )
  2042. # Create pull-request
  2043. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2044. req = pagure.lib.query.new_pull_request(
  2045. session=self.session,
  2046. repo_from=repo,
  2047. branch_from="feature",
  2048. repo_to=repo,
  2049. branch_to="master",
  2050. title="test pull-request",
  2051. user="pingou",
  2052. )
  2053. self.session.commit()
  2054. self.assertEqual(req.id, 1)
  2055. self.assertEqual(req.title, "test pull-request")
  2056. # Check subscribtion before
  2057. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2058. request = pagure.lib.query.search_pull_requests(
  2059. self.session, project_id=1, requestid=1
  2060. )
  2061. self.assertEqual(
  2062. pagure.lib.query.get_watch_list(self.session, request),
  2063. set(["pingou"]),
  2064. )
  2065. # Unsubscribe - no changes
  2066. data = {}
  2067. output = self.app.post(
  2068. "/api/0/test/pull-request/1/subscribe", data=data, headers=headers
  2069. )
  2070. self.assertEqual(output.status_code, 200)
  2071. data = json.loads(output.get_data(as_text=True))
  2072. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  2073. self.assertDictEqual(
  2074. data,
  2075. {
  2076. "message": "You are no longer watching this pull-request",
  2077. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  2078. "user": "bar",
  2079. },
  2080. )
  2081. data = {}
  2082. output = self.app.post(
  2083. "/api/0/test/pull-request/1/subscribe", data=data, headers=headers
  2084. )
  2085. self.assertEqual(output.status_code, 200)
  2086. data = json.loads(output.get_data(as_text=True))
  2087. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  2088. self.assertDictEqual(
  2089. data,
  2090. {
  2091. "message": "You are no longer watching this pull-request",
  2092. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  2093. "user": "bar",
  2094. },
  2095. )
  2096. # No change
  2097. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2098. request = pagure.lib.query.search_pull_requests(
  2099. self.session, project_id=1, requestid=1
  2100. )
  2101. self.assertEqual(
  2102. pagure.lib.query.get_watch_list(self.session, request),
  2103. set(["pingou"]),
  2104. )
  2105. # Subscribe
  2106. data = {"status": True}
  2107. output = self.app.post(
  2108. "/api/0/test/pull-request/1/subscribe", data=data, headers=headers
  2109. )
  2110. self.assertEqual(output.status_code, 200)
  2111. data = json.loads(output.get_data(as_text=True))
  2112. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  2113. self.assertDictEqual(
  2114. data,
  2115. {
  2116. "message": "You are now watching this pull-request",
  2117. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  2118. "user": "bar",
  2119. },
  2120. )
  2121. # Subscribe - no changes
  2122. data = {"status": True}
  2123. output = self.app.post(
  2124. "/api/0/test/pull-request/1/subscribe", data=data, headers=headers
  2125. )
  2126. self.assertEqual(output.status_code, 200)
  2127. data = json.loads(output.get_data(as_text=True))
  2128. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  2129. self.assertDictEqual(
  2130. data,
  2131. {
  2132. "message": "You are now watching this pull-request",
  2133. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  2134. "user": "bar",
  2135. },
  2136. )
  2137. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2138. request = pagure.lib.query.search_pull_requests(
  2139. self.session, project_id=1, requestid=1
  2140. )
  2141. self.assertEqual(
  2142. pagure.lib.query.get_watch_list(self.session, request),
  2143. set(["pingou", "bar"]),
  2144. )
  2145. # Unsubscribe
  2146. data = {}
  2147. output = self.app.post(
  2148. "/api/0/test/pull-request/1/subscribe", data=data, headers=headers
  2149. )
  2150. self.assertEqual(output.status_code, 200)
  2151. data = json.loads(output.get_data(as_text=True))
  2152. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  2153. self.assertDictEqual(
  2154. data,
  2155. {
  2156. "message": "You are no longer watching this pull-request",
  2157. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  2158. "user": "bar",
  2159. },
  2160. )
  2161. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2162. request = pagure.lib.query.search_pull_requests(
  2163. self.session, project_id=1, requestid=1
  2164. )
  2165. self.assertEqual(
  2166. pagure.lib.query.get_watch_list(self.session, request),
  2167. set(["pingou"]),
  2168. )
  2169. @patch("pagure.lib.git.update_git")
  2170. @patch("pagure.lib.notify.send_email")
  2171. def test_api_subscribe_pull_request_logged_in(self, p_send_email, p_ugt):
  2172. """Test the api_subscribe_pull_request method of the flask api
  2173. when the user is logged in via the UI."""
  2174. p_send_email.return_value = True
  2175. p_ugt.return_value = True
  2176. item = pagure.lib.model.User(
  2177. user="bar",
  2178. fullname="bar foo",
  2179. password="foo",
  2180. default_email="bar@bar.com",
  2181. )
  2182. self.session.add(item)
  2183. item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
  2184. self.session.add(item)
  2185. self.session.commit()
  2186. tests.create_projects(self.session)
  2187. tests.create_tokens(self.session, user_id=3)
  2188. tests.create_tokens_acl(self.session)
  2189. # Create pull-request
  2190. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2191. req = pagure.lib.query.new_pull_request(
  2192. session=self.session,
  2193. repo_from=repo,
  2194. branch_from="feature",
  2195. repo_to=repo,
  2196. branch_to="master",
  2197. title="test pull-request",
  2198. user="pingou",
  2199. )
  2200. self.session.commit()
  2201. self.assertEqual(req.id, 1)
  2202. self.assertEqual(req.title, "test pull-request")
  2203. # Check subscribtion before
  2204. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2205. request = pagure.lib.query.search_pull_requests(
  2206. self.session, project_id=1, requestid=1
  2207. )
  2208. self.assertEqual(
  2209. pagure.lib.query.get_watch_list(self.session, request),
  2210. set(["pingou"]),
  2211. )
  2212. # Subscribe
  2213. user = tests.FakeUser(username="foo")
  2214. with tests.user_set(self.app.application, user):
  2215. data = {"status": True}
  2216. output = self.app.post(
  2217. "/api/0/test/pull-request/1/subscribe", data=data
  2218. )
  2219. self.assertEqual(output.status_code, 200)
  2220. data = json.loads(output.get_data(as_text=True))
  2221. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  2222. self.assertDictEqual(
  2223. data,
  2224. {
  2225. "message": "You are now watching this pull-request",
  2226. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  2227. "user": "foo",
  2228. },
  2229. )
  2230. # Check subscribtions after
  2231. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2232. request = pagure.lib.query.search_pull_requests(
  2233. self.session, project_id=1, requestid=1
  2234. )
  2235. self.assertEqual(
  2236. pagure.lib.query.get_watch_list(self.session, request),
  2237. set(["pingou", "foo"]),
  2238. )
  2239. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2240. def test_api_pull_request_open_invalid_project(self):
  2241. """Test the api_pull_request_create method of the flask api when
  2242. not the project doesn't exist.
  2243. """
  2244. tests.create_projects(self.session)
  2245. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2246. tests.create_projects_git(
  2247. os.path.join(self.path, "requests"), bare=True
  2248. )
  2249. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2250. tests.add_commit_git_repo(
  2251. os.path.join(self.path, "repos", "test.git"), branch="test"
  2252. )
  2253. tests.create_tokens(self.session)
  2254. tests.create_tokens_acl(self.session)
  2255. headers = {"Authorization": "token aaabbbcccddd"}
  2256. data = {
  2257. "initial_comment": "Nothing much, the changes speak for themselves",
  2258. "branch_to": "master",
  2259. "branch_from": "test",
  2260. }
  2261. output = self.app.post(
  2262. "/api/0/foobar/pull-request/new", headers=headers, data=data
  2263. )
  2264. self.assertEqual(output.status_code, 404)
  2265. data = json.loads(output.get_data(as_text=True))
  2266. self.assertDictEqual(
  2267. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2268. )
  2269. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2270. def test_api_pull_request_open_missing_title(self):
  2271. """Test the api_pull_request_create method of the flask api when
  2272. not title is submitted.
  2273. """
  2274. tests.create_projects(self.session)
  2275. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2276. tests.create_projects_git(
  2277. os.path.join(self.path, "requests"), bare=True
  2278. )
  2279. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2280. tests.add_commit_git_repo(
  2281. os.path.join(self.path, "repos", "test.git"), branch="test"
  2282. )
  2283. tests.create_tokens(self.session)
  2284. tests.create_tokens_acl(self.session)
  2285. headers = {"Authorization": "token aaabbbcccddd"}
  2286. data = {
  2287. "initial_comment": "Nothing much, the changes speak for themselves",
  2288. "branch_to": "master",
  2289. "branch_from": "test",
  2290. }
  2291. output = self.app.post(
  2292. "/api/0/test/pull-request/new", headers=headers, data=data
  2293. )
  2294. self.assertEqual(output.status_code, 400)
  2295. data = json.loads(output.get_data(as_text=True))
  2296. self.assertDictEqual(
  2297. data,
  2298. {
  2299. "error": "Invalid or incomplete input submitted",
  2300. "error_code": "EINVALIDREQ",
  2301. "errors": {"title": ["This field is required."]},
  2302. },
  2303. )
  2304. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2305. def test_api_pull_request_open_missing_branch_to(self):
  2306. """Test the api_pull_request_create method of the flask api when
  2307. not branch to is submitted.
  2308. """
  2309. tests.create_projects(self.session)
  2310. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2311. tests.create_projects_git(
  2312. os.path.join(self.path, "requests"), bare=True
  2313. )
  2314. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2315. tests.add_commit_git_repo(
  2316. os.path.join(self.path, "repos", "test.git"), branch="test"
  2317. )
  2318. tests.create_tokens(self.session)
  2319. tests.create_tokens_acl(self.session)
  2320. headers = {"Authorization": "token aaabbbcccddd"}
  2321. data = {
  2322. "title": "Test PR",
  2323. "initial_comment": "Nothing much, the changes speak for themselves",
  2324. "branch_from": "test",
  2325. }
  2326. output = self.app.post(
  2327. "/api/0/test/pull-request/new", headers=headers, data=data
  2328. )
  2329. self.assertEqual(output.status_code, 400)
  2330. data = json.loads(output.get_data(as_text=True))
  2331. self.assertDictEqual(
  2332. data,
  2333. {
  2334. "error": "Invalid or incomplete input submitted",
  2335. "error_code": "EINVALIDREQ",
  2336. "errors": {"branch_to": ["This field is required."]},
  2337. },
  2338. )
  2339. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2340. def test_api_pull_request_open_missing_branch_from(self):
  2341. """Test the api_pull_request_create method of the flask api when
  2342. not branch from is submitted.
  2343. """
  2344. tests.create_projects(self.session)
  2345. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2346. tests.create_projects_git(
  2347. os.path.join(self.path, "requests"), bare=True
  2348. )
  2349. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2350. tests.add_commit_git_repo(
  2351. os.path.join(self.path, "repos", "test.git"), branch="test"
  2352. )
  2353. tests.create_tokens(self.session)
  2354. tests.create_tokens_acl(self.session)
  2355. headers = {"Authorization": "token aaabbbcccddd"}
  2356. data = {
  2357. "title": "Test PR",
  2358. "initial_comment": "Nothing much, the changes speak for themselves",
  2359. "branch_to": "master",
  2360. }
  2361. output = self.app.post(
  2362. "/api/0/test/pull-request/new", headers=headers, data=data
  2363. )
  2364. self.assertEqual(output.status_code, 400)
  2365. data = json.loads(output.get_data(as_text=True))
  2366. self.assertDictEqual(
  2367. data,
  2368. {
  2369. "error": "Invalid or incomplete input submitted",
  2370. "error_code": "EINVALIDREQ",
  2371. "errors": {"branch_from": ["This field is required."]},
  2372. },
  2373. )
  2374. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2375. def test_api_pull_request_open_pr_disabled(self):
  2376. """Test the api_pull_request_create method of the flask api when
  2377. the parent repo disabled pull-requests.
  2378. """
  2379. tests.create_projects(self.session)
  2380. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2381. tests.create_projects_git(
  2382. os.path.join(self.path, "requests"), bare=True
  2383. )
  2384. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2385. tests.add_commit_git_repo(
  2386. os.path.join(self.path, "repos", "test.git"), branch="test"
  2387. )
  2388. tests.create_tokens(self.session)
  2389. tests.create_tokens_acl(self.session)
  2390. # Check the behavior if the project disabled the issue tracker
  2391. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2392. settings = repo.settings
  2393. settings["pull_requests"] = False
  2394. repo.settings = settings
  2395. self.session.add(repo)
  2396. self.session.commit()
  2397. headers = {"Authorization": "token aaabbbcccddd"}
  2398. data = {
  2399. "title": "Test PR",
  2400. "initial_comment": "Nothing much, the changes speak for themselves",
  2401. "branch_to": "master",
  2402. "branch_from": "test",
  2403. }
  2404. output = self.app.post(
  2405. "/api/0/test/pull-request/new", headers=headers, data=data
  2406. )
  2407. self.assertEqual(output.status_code, 404)
  2408. data = json.loads(output.get_data(as_text=True))
  2409. self.assertDictEqual(
  2410. data,
  2411. {
  2412. "error": "Pull-Request have been deactivated for this project",
  2413. "error_code": "EPULLREQUESTSDISABLED",
  2414. },
  2415. )
  2416. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2417. def test_api_pull_request_open_signed_pr(self):
  2418. """Test the api_pull_request_create method of the flask api when
  2419. the parent repo enforces signed-off pull-requests.
  2420. """
  2421. tests.create_projects(self.session)
  2422. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2423. tests.create_projects_git(
  2424. os.path.join(self.path, "requests"), bare=True
  2425. )
  2426. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2427. tests.add_commit_git_repo(
  2428. os.path.join(self.path, "repos", "test.git"), branch="test"
  2429. )
  2430. tests.create_tokens(self.session)
  2431. tests.create_tokens_acl(self.session)
  2432. # Check the behavior if the project disabled the issue tracker
  2433. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2434. settings = repo.settings
  2435. settings["Enforce_signed-off_commits_in_pull-request"] = True
  2436. repo.settings = settings
  2437. self.session.add(repo)
  2438. self.session.commit()
  2439. headers = {"Authorization": "token aaabbbcccddd"}
  2440. data = {
  2441. "title": "Test PR",
  2442. "initial_comment": "Nothing much, the changes speak for themselves",
  2443. "branch_to": "master",
  2444. "branch_from": "test",
  2445. }
  2446. output = self.app.post(
  2447. "/api/0/test/pull-request/new", headers=headers, data=data
  2448. )
  2449. self.assertEqual(output.status_code, 400)
  2450. data = json.loads(output.get_data(as_text=True))
  2451. self.assertDictEqual(
  2452. data,
  2453. {
  2454. "error": "This repo enforces that all commits are signed "
  2455. "off by their author.",
  2456. "error_code": "ENOSIGNEDOFF",
  2457. },
  2458. )
  2459. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2460. def test_api_pull_request_open_invalid_branch_from(self):
  2461. """Test the api_pull_request_create method of the flask api when
  2462. the branch from does not exist.
  2463. """
  2464. tests.create_projects(self.session)
  2465. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2466. tests.create_projects_git(
  2467. os.path.join(self.path, "requests"), bare=True
  2468. )
  2469. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2470. tests.add_commit_git_repo(
  2471. os.path.join(self.path, "repos", "test.git"), branch="test"
  2472. )
  2473. tests.create_tokens(self.session)
  2474. tests.create_tokens_acl(self.session)
  2475. # Check the behavior if the project disabled the issue tracker
  2476. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2477. settings = repo.settings
  2478. settings["Enforce_signed-off_commits_in_pull-request"] = True
  2479. repo.settings = settings
  2480. self.session.add(repo)
  2481. self.session.commit()
  2482. headers = {"Authorization": "token aaabbbcccddd"}
  2483. data = {
  2484. "title": "Test PR",
  2485. "initial_comment": "Nothing much, the changes speak for themselves",
  2486. "branch_to": "master",
  2487. "branch_from": "foobarbaz",
  2488. }
  2489. output = self.app.post(
  2490. "/api/0/test/pull-request/new", headers=headers, data=data
  2491. )
  2492. self.assertEqual(output.status_code, 400)
  2493. data = json.loads(output.get_data(as_text=True))
  2494. self.assertDictEqual(
  2495. data,
  2496. {
  2497. "error": "Invalid or incomplete input submitted",
  2498. "error_code": "EINVALIDREQ",
  2499. "errors": "Branch foobarbaz does not exist",
  2500. },
  2501. )
  2502. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2503. def test_api_pull_request_open_invalid_token(self):
  2504. """Test the api_pull_request_create method of the flask api when
  2505. queried with an invalid token.
  2506. """
  2507. tests.create_projects(self.session)
  2508. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2509. tests.create_projects_git(
  2510. os.path.join(self.path, "requests"), bare=True
  2511. )
  2512. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2513. tests.add_commit_git_repo(
  2514. os.path.join(self.path, "repos", "test.git"), branch="test"
  2515. )
  2516. tests.create_tokens(self.session)
  2517. tests.create_tokens_acl(self.session)
  2518. headers = {"Authorization": "token aaabbbcccddd"}
  2519. data = {
  2520. "title": "Test PR",
  2521. "initial_comment": "Nothing much, the changes speak for themselves",
  2522. "branch_to": "master",
  2523. "branch_from": "foobarbaz",
  2524. }
  2525. output = self.app.post(
  2526. "/api/0/test2/pull-request/new", headers=headers, data=data
  2527. )
  2528. self.assertEqual(output.status_code, 401)
  2529. data = json.loads(output.get_data(as_text=True))
  2530. self.assertDictEqual(
  2531. data,
  2532. {
  2533. "error": "Invalid or expired token. Please visit "
  2534. "http://localhost.localdomain/settings#nav-api-tab to get or "
  2535. "renew your API token.",
  2536. "error_code": "EINVALIDTOK",
  2537. },
  2538. )
  2539. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2540. def test_api_pull_request_open_invalid_access(self):
  2541. """Test the api_pull_request_create method of the flask api when
  2542. the user opening the PR doesn't have commit access.
  2543. """
  2544. tests.create_projects(self.session)
  2545. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2546. tests.create_projects_git(
  2547. os.path.join(self.path, "requests"), bare=True
  2548. )
  2549. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2550. tests.add_commit_git_repo(
  2551. os.path.join(self.path, "repos", "test.git"), branch="test"
  2552. )
  2553. tests.create_tokens(self.session, user_id=2)
  2554. tests.create_tokens_acl(self.session)
  2555. headers = {"Authorization": "token aaabbbcccddd"}
  2556. data = {
  2557. "title": "Test PR",
  2558. "initial_comment": "Nothing much, the changes speak for themselves",
  2559. "branch_to": "master",
  2560. "branch_from": "foobarbaz",
  2561. }
  2562. output = self.app.post(
  2563. "/api/0/test/pull-request/new", headers=headers, data=data
  2564. )
  2565. self.assertEqual(output.status_code, 401)
  2566. data = json.loads(output.get_data(as_text=True))
  2567. self.assertDictEqual(
  2568. data,
  2569. {
  2570. "error": "You do not have sufficient permissions to "
  2571. "perform this action",
  2572. "error_code": "ENOTHIGHENOUGH",
  2573. },
  2574. )
  2575. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2576. def test_api_pull_request_open_invalid_branch_to(self):
  2577. """Test the api_pull_request_create method of the flask api when
  2578. the branch to does not exist.
  2579. """
  2580. tests.create_projects(self.session)
  2581. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2582. tests.create_projects_git(
  2583. os.path.join(self.path, "requests"), bare=True
  2584. )
  2585. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2586. tests.add_commit_git_repo(
  2587. os.path.join(self.path, "repos", "test.git"), branch="test"
  2588. )
  2589. tests.create_tokens(self.session)
  2590. tests.create_tokens_acl(self.session)
  2591. # Check the behavior if the project disabled the issue tracker
  2592. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2593. settings = repo.settings
  2594. settings["Enforce_signed-off_commits_in_pull-request"] = True
  2595. repo.settings = settings
  2596. self.session.add(repo)
  2597. self.session.commit()
  2598. headers = {"Authorization": "token aaabbbcccddd"}
  2599. data = {
  2600. "title": "Test PR",
  2601. "initial_comment": "Nothing much, the changes speak for themselves",
  2602. "branch_to": "foobarbaz",
  2603. "branch_from": "test",
  2604. }
  2605. output = self.app.post(
  2606. "/api/0/test/pull-request/new", headers=headers, data=data
  2607. )
  2608. self.assertEqual(output.status_code, 400)
  2609. data = json.loads(output.get_data(as_text=True))
  2610. self.assertDictEqual(
  2611. data,
  2612. {
  2613. "error": "Invalid or incomplete input submitted",
  2614. "error_code": "EINVALIDREQ",
  2615. "errors": "Branch foobarbaz could not be found in the "
  2616. "target repo",
  2617. },
  2618. )
  2619. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2620. def test_api_pull_request_open_project_token_different_project(self):
  2621. """Test the api_pull_request_create method with the project token
  2622. of a different project - fails"""
  2623. tests.create_projects(self.session)
  2624. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2625. tests.create_projects_git(
  2626. os.path.join(self.path, "requests"), bare=True
  2627. )
  2628. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2629. tests.add_commit_git_repo(
  2630. os.path.join(self.path, "repos", "test.git"), branch="test"
  2631. )
  2632. tests.create_tokens(self.session, project_id=2)
  2633. tests.create_tokens_acl(self.session)
  2634. headers = {"Authorization": "token foo_token"}
  2635. data = {
  2636. "title": "Test of PR",
  2637. "inicial comment": "Some readme adjustment",
  2638. "branch_to": "master",
  2639. "branch_from": "test",
  2640. }
  2641. output = self.app.post(
  2642. "/api/0/test/pull-request/new", headers=headers, data=data
  2643. )
  2644. self.assertEqual(output.status_code, 401)
  2645. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2646. def test_api_pull_request_open_user_token_invalid_acls(self):
  2647. """Test the api_pull_request_create method with the user token, but with
  2648. no acls for opening pull request - fails"""
  2649. tests.create_projects(self.session)
  2650. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2651. tests.create_projects_git(
  2652. os.path.join(self.path, "requests"), bare=True
  2653. )
  2654. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2655. tests.add_commit_git_repo(
  2656. os.path.join(self.path, "repos", "test.git"), branch="test"
  2657. )
  2658. tests.create_tokens(self.session, project_id=None)
  2659. for acl in (
  2660. "create_project",
  2661. "fork_project",
  2662. "modify_project",
  2663. "update_watch_status",
  2664. ):
  2665. tests.create_tokens_acl(self.session, acl_name=acl)
  2666. headers = {"Authorization": "token aaabbbcccddd"}
  2667. data = {
  2668. "title": "Test of PR",
  2669. "initial_comment": "Some readme adjustment",
  2670. "branch_to": "master",
  2671. "branch_from": "test",
  2672. }
  2673. output = self.app.post(
  2674. "/api/0/test/pull-request/new", headers=headers, data=data
  2675. )
  2676. self.assertEqual(output.status_code, 401)
  2677. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2678. def test_api_pull_request_open_from_branch_to_origin(self):
  2679. """Test the api_pull_request_create method from a fork to a master,
  2680. with project token of a origin with all the acls"""
  2681. tests.create_projects(self.session)
  2682. tests.create_projects(
  2683. self.session, is_fork=True, hook_token_suffix="foo"
  2684. )
  2685. project_query = self.session.query(pagure.lib.model.Project)
  2686. for project in project_query.filter_by(name="test").all():
  2687. if project.parent_id == None:
  2688. parent = project
  2689. else:
  2690. child = project
  2691. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2692. tests.create_projects_git(
  2693. os.path.join(self.path, "requests"), bare=True
  2694. )
  2695. tests.add_readme_git_repo(
  2696. os.path.join(self.path, "repos", "forks", "pingou", "test.git"),
  2697. branch="branch",
  2698. )
  2699. tests.add_commit_git_repo(
  2700. os.path.join(self.path, "repos", "forks", "pingou", "test.git"),
  2701. branch="branch",
  2702. )
  2703. # Create tokens
  2704. parent_token = pagure.lib.model.Token(
  2705. id="iamparenttoken",
  2706. user_id=parent.user_id,
  2707. project_id=parent.id,
  2708. expiration=datetime.datetime.utcnow()
  2709. + datetime.timedelta(days=30),
  2710. )
  2711. self.session.add(parent_token)
  2712. fork_token = pagure.lib.model.Token(
  2713. id="iamforktoken",
  2714. user_id=child.user_id,
  2715. project_id=child.id,
  2716. expiration=datetime.datetime.utcnow()
  2717. + datetime.timedelta(days=30),
  2718. )
  2719. self.session.add(fork_token)
  2720. self.session.commit()
  2721. tests.create_tokens_acl(self.session, token_id="iamparenttoken")
  2722. for acl in pagure.default_config.CROSS_PROJECT_ACLS:
  2723. tests.create_tokens_acl(
  2724. self.session, token_id="iamforktoken", acl_name=acl
  2725. )
  2726. headers = {"Authorization": "token iamforktoken"}
  2727. data = {
  2728. "title": "war of tomatoes",
  2729. "initial_comment": "the manifest",
  2730. "branch_to": "master",
  2731. "branch_from": "branch",
  2732. "repo_from": "test",
  2733. "repo_from_username": "pingou",
  2734. }
  2735. output = self.app.post(
  2736. "/api/0/test/pull-request/new", headers=headers, data=data
  2737. )
  2738. self.assertEqual(output.status_code, 200)
  2739. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2740. def test_api_pull_request_open(self):
  2741. """ Test the api_pull_request_create method of the flask api. """
  2742. tests.create_projects(self.session)
  2743. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2744. tests.create_projects_git(
  2745. os.path.join(self.path, "requests"), bare=True
  2746. )
  2747. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2748. tests.add_commit_git_repo(
  2749. os.path.join(self.path, "repos", "test.git"), branch="test"
  2750. )
  2751. tests.create_tokens(self.session)
  2752. tests.create_tokens_acl(self.session)
  2753. headers = {"Authorization": "token aaabbbcccddd"}
  2754. data = {
  2755. "title": "Test PR",
  2756. "initial_comment": "Nothing much, the changes speak for themselves",
  2757. "branch_to": "master",
  2758. "branch_from": "test",
  2759. }
  2760. output = self.app.post(
  2761. "/api/0/test/pull-request/new", headers=headers, data=data
  2762. )
  2763. self.assertEqual(output.status_code, 200)
  2764. data = json.loads(output.get_data(as_text=True))
  2765. data["project"]["date_created"] = "1516348115"
  2766. data["project"]["date_modified"] = "1516348115"
  2767. data["repo_from"]["date_created"] = "1516348115"
  2768. data["repo_from"]["date_modified"] = "1516348115"
  2769. data["uid"] = "e8b68df8711648deac67c3afed15a798"
  2770. data["commit_start"] = "114f1b468a5f05e635fcb6394273f3f907386eab"
  2771. data["commit_stop"] = "114f1b468a5f05e635fcb6394273f3f907386eab"
  2772. data["date_created"] = "1516348115"
  2773. data["last_updated"] = "1516348115"
  2774. data["updated_on"] = "1516348115"
  2775. self.assertDictEqual(
  2776. data,
  2777. {
  2778. "assignee": None,
  2779. "branch": "master",
  2780. "branch_from": "test",
  2781. "cached_merge_status": "unknown",
  2782. "closed_at": None,
  2783. "closed_by": None,
  2784. "comments": [],
  2785. "commit_start": "114f1b468a5f05e635fcb6394273f3f907386eab",
  2786. "commit_stop": "114f1b468a5f05e635fcb6394273f3f907386eab",
  2787. "date_created": "1516348115",
  2788. "full_url": "http://localhost.localdomain/test/pull-request/1",
  2789. "id": 1,
  2790. "initial_comment": "Nothing much, the changes speak for themselves",
  2791. "last_updated": "1516348115",
  2792. "project": {
  2793. "access_groups": {
  2794. "admin": [],
  2795. "collaborator": [],
  2796. "commit": [],
  2797. "ticket": [],
  2798. },
  2799. "access_users": {
  2800. "admin": [],
  2801. "collaborator": [],
  2802. "commit": [],
  2803. "owner": ["pingou"],
  2804. "ticket": [],
  2805. },
  2806. "close_status": [
  2807. "Invalid",
  2808. "Insufficient data",
  2809. "Fixed",
  2810. "Duplicate",
  2811. ],
  2812. "custom_keys": [],
  2813. "date_created": "1516348115",
  2814. "date_modified": "1516348115",
  2815. "description": "test project #1",
  2816. "full_url": "http://localhost.localdomain/test",
  2817. "fullname": "test",
  2818. "id": 1,
  2819. "milestones": {},
  2820. "name": "test",
  2821. "namespace": None,
  2822. "parent": None,
  2823. "priorities": {},
  2824. "tags": [],
  2825. "url_path": "test",
  2826. "user": {
  2827. "full_url": "http://localhost.localdomain/user/pingou",
  2828. "fullname": "PY C",
  2829. "name": "pingou",
  2830. "url_path": "user/pingou",
  2831. },
  2832. },
  2833. "remote_git": None,
  2834. "repo_from": {
  2835. "access_groups": {
  2836. "admin": [],
  2837. "collaborator": [],
  2838. "commit": [],
  2839. "ticket": [],
  2840. },
  2841. "access_users": {
  2842. "admin": [],
  2843. "collaborator": [],
  2844. "commit": [],
  2845. "owner": ["pingou"],
  2846. "ticket": [],
  2847. },
  2848. "close_status": [
  2849. "Invalid",
  2850. "Insufficient data",
  2851. "Fixed",
  2852. "Duplicate",
  2853. ],
  2854. "custom_keys": [],
  2855. "date_created": "1516348115",
  2856. "date_modified": "1516348115",
  2857. "description": "test project #1",
  2858. "full_url": "http://localhost.localdomain/test",
  2859. "fullname": "test",
  2860. "id": 1,
  2861. "milestones": {},
  2862. "name": "test",
  2863. "namespace": None,
  2864. "parent": None,
  2865. "priorities": {},
  2866. "tags": [],
  2867. "url_path": "test",
  2868. "user": {
  2869. "full_url": "http://localhost.localdomain/user/pingou",
  2870. "fullname": "PY C",
  2871. "name": "pingou",
  2872. "url_path": "user/pingou",
  2873. },
  2874. },
  2875. "status": "Open",
  2876. "tags": [],
  2877. "threshold_reached": None,
  2878. "title": "Test PR",
  2879. "uid": "e8b68df8711648deac67c3afed15a798",
  2880. "updated_on": "1516348115",
  2881. "user": {
  2882. "full_url": "http://localhost.localdomain/user/pingou",
  2883. "fullname": "PY C",
  2884. "name": "pingou",
  2885. "url_path": "user/pingou",
  2886. },
  2887. },
  2888. )
  2889. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2890. def test_api_pull_request_open_missing_initial_comment(self):
  2891. """Test the api_pull_request_create method of the flask api when
  2892. not initial comment is submitted.
  2893. """
  2894. tests.create_projects(self.session)
  2895. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2896. tests.create_projects_git(
  2897. os.path.join(self.path, "requests"), bare=True
  2898. )
  2899. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2900. tests.add_commit_git_repo(
  2901. os.path.join(self.path, "repos", "test.git"), branch="test"
  2902. )
  2903. tests.create_tokens(self.session)
  2904. tests.create_tokens_acl(self.session)
  2905. headers = {"Authorization": "token aaabbbcccddd"}
  2906. data = {
  2907. "title": "Test PR",
  2908. "branch_to": "master",
  2909. "branch_from": "test",
  2910. }
  2911. output = self.app.post(
  2912. "/api/0/test/pull-request/new", headers=headers, data=data
  2913. )
  2914. self.assertEqual(output.status_code, 200)
  2915. data = json.loads(output.get_data(as_text=True))
  2916. data["project"]["date_created"] = "1516348115"
  2917. data["project"]["date_modified"] = "1516348115"
  2918. data["repo_from"]["date_created"] = "1516348115"
  2919. data["repo_from"]["date_modified"] = "1516348115"
  2920. data["uid"] = "e8b68df8711648deac67c3afed15a798"
  2921. data["commit_start"] = "114f1b468a5f05e635fcb6394273f3f907386eab"
  2922. data["commit_stop"] = "114f1b468a5f05e635fcb6394273f3f907386eab"
  2923. data["date_created"] = "1516348115"
  2924. data["last_updated"] = "1516348115"
  2925. data["updated_on"] = "1516348115"
  2926. self.assertDictEqual(
  2927. data,
  2928. {
  2929. "assignee": None,
  2930. "branch": "master",
  2931. "branch_from": "test",
  2932. "cached_merge_status": "unknown",
  2933. "closed_at": None,
  2934. "closed_by": None,
  2935. "comments": [],
  2936. "commit_start": "114f1b468a5f05e635fcb6394273f3f907386eab",
  2937. "commit_stop": "114f1b468a5f05e635fcb6394273f3f907386eab",
  2938. "date_created": "1516348115",
  2939. "full_url": "http://localhost.localdomain/test/pull-request/1",
  2940. "id": 1,
  2941. "initial_comment": None,
  2942. "last_updated": "1516348115",
  2943. "project": {
  2944. "access_groups": {
  2945. "admin": [],
  2946. "collaborator": [],
  2947. "commit": [],
  2948. "ticket": [],
  2949. },
  2950. "access_users": {
  2951. "admin": [],
  2952. "collaborator": [],
  2953. "commit": [],
  2954. "owner": ["pingou"],
  2955. "ticket": [],
  2956. },
  2957. "close_status": [
  2958. "Invalid",
  2959. "Insufficient data",
  2960. "Fixed",
  2961. "Duplicate",
  2962. ],
  2963. "custom_keys": [],
  2964. "date_created": "1516348115",
  2965. "date_modified": "1516348115",
  2966. "description": "test project #1",
  2967. "full_url": "http://localhost.localdomain/test",
  2968. "fullname": "test",
  2969. "id": 1,
  2970. "milestones": {},
  2971. "name": "test",
  2972. "namespace": None,
  2973. "parent": None,
  2974. "priorities": {},
  2975. "tags": [],
  2976. "url_path": "test",
  2977. "user": {
  2978. "full_url": "http://localhost.localdomain/user/pingou",
  2979. "fullname": "PY C",
  2980. "name": "pingou",
  2981. "url_path": "user/pingou",
  2982. },
  2983. },
  2984. "remote_git": None,
  2985. "repo_from": {
  2986. "access_groups": {
  2987. "admin": [],
  2988. "collaborator": [],
  2989. "commit": [],
  2990. "ticket": [],
  2991. },
  2992. "access_users": {
  2993. "admin": [],
  2994. "collaborator": [],
  2995. "commit": [],
  2996. "owner": ["pingou"],
  2997. "ticket": [],
  2998. },
  2999. "close_status": [
  3000. "Invalid",
  3001. "Insufficient data",
  3002. "Fixed",
  3003. "Duplicate",
  3004. ],
  3005. "custom_keys": [],
  3006. "date_created": "1516348115",
  3007. "date_modified": "1516348115",
  3008. "description": "test project #1",
  3009. "full_url": "http://localhost.localdomain/test",
  3010. "fullname": "test",
  3011. "id": 1,
  3012. "milestones": {},
  3013. "name": "test",
  3014. "namespace": None,
  3015. "parent": None,
  3016. "priorities": {},
  3017. "tags": [],
  3018. "url_path": "test",
  3019. "user": {
  3020. "full_url": "http://localhost.localdomain/user/pingou",
  3021. "fullname": "PY C",
  3022. "name": "pingou",
  3023. "url_path": "user/pingou",
  3024. },
  3025. },
  3026. "status": "Open",
  3027. "tags": [],
  3028. "threshold_reached": None,
  3029. "title": "Test PR",
  3030. "uid": "e8b68df8711648deac67c3afed15a798",
  3031. "updated_on": "1516348115",
  3032. "user": {
  3033. "full_url": "http://localhost.localdomain/user/pingou",
  3034. "fullname": "PY C",
  3035. "name": "pingou",
  3036. "url_path": "user/pingou",
  3037. },
  3038. },
  3039. )
  3040. class PagureFlaskApiForkPRDiffStatstests(tests.Modeltests):
  3041. """Tests for the flask API of pagure for the diff stats endpoint of PRs"""
  3042. maxDiff = None
  3043. def setUp(self):
  3044. """ Set up the environnment, ran before every tests. """
  3045. super(PagureFlaskApiForkPRDiffStatstests, self).setUp()
  3046. pagure.config.config["REQUESTS_FOLDER"] = None
  3047. tests.create_projects(self.session)
  3048. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  3049. tests.create_projects_git(
  3050. os.path.join(self.path, "requests"), bare=True
  3051. )
  3052. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  3053. tests.add_commit_git_repo(
  3054. os.path.join(self.path, "repos", "test.git"), ncommits=5
  3055. )
  3056. tests.add_commit_git_repo(
  3057. os.path.join(self.path, "repos", "test.git"), branch="test"
  3058. )
  3059. # Create the pull-request to close
  3060. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  3061. req = pagure.lib.query.new_pull_request(
  3062. session=self.session,
  3063. repo_from=repo,
  3064. branch_from="test",
  3065. repo_to=repo,
  3066. branch_to="master",
  3067. title="test pull-request",
  3068. user="pingou",
  3069. )
  3070. self.session.commit()
  3071. self.assertEqual(req.id, 1)
  3072. self.assertEqual(req.title, "test pull-request")
  3073. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  3074. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  3075. def test_api_pull_request_diffstats_no_repo(self):
  3076. """ Test the api_pull_request_merge method of the flask api. """
  3077. output = self.app.get("/api/0/invalid/pull-request/404/diffstats")
  3078. self.assertEqual(output.status_code, 404)
  3079. data = json.loads(output.get_data(as_text=True))
  3080. self.assertEqual(
  3081. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  3082. )
  3083. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  3084. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  3085. def test_api_pull_request_diffstats_no_pr(self):
  3086. """ Test the api_pull_request_merge method of the flask api. """
  3087. output = self.app.get("/api/0/test/pull-request/404/diffstats")
  3088. self.assertEqual(output.status_code, 404)
  3089. data = json.loads(output.get_data(as_text=True))
  3090. self.assertEqual(
  3091. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  3092. )
  3093. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  3094. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  3095. def test_api_pull_request_diffstats_file_modified(self):
  3096. """ Test the api_pull_request_merge method of the flask api. """
  3097. output = self.app.get("/api/0/test/pull-request/1/diffstats")
  3098. self.assertEqual(output.status_code, 200)
  3099. data = json.loads(output.get_data(as_text=True))
  3100. self.assertEqual(
  3101. data,
  3102. {
  3103. "sources": {
  3104. "lines_added": 10,
  3105. "lines_removed": 0,
  3106. "new_id": "540916fbd3d825d14cc0c0b2397606fda69379ce",
  3107. "old_id": "265f133a7c94ede4cb183dd808219c5bf9e08f87",
  3108. "old_path": "sources",
  3109. "status": "M",
  3110. }
  3111. },
  3112. )
  3113. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  3114. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  3115. def test_api_pull_request_diffstats_file_added_mofidied(self):
  3116. """ Test the api_pull_request_merge method of the flask api. """
  3117. tests.add_commit_git_repo(
  3118. os.path.join(self.path, "repos", "test.git"), ncommits=5
  3119. )
  3120. tests.add_readme_git_repo(
  3121. os.path.join(self.path, "repos", "test.git"),
  3122. readme_name="README.md",
  3123. branch="test",
  3124. )
  3125. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  3126. self.assertEqual(len(repo.requests), 1)
  3127. output = self.app.get("/api/0/test/pull-request/1/diffstats")
  3128. self.assertEqual(output.status_code, 200)
  3129. data = json.loads(output.get_data(as_text=True))
  3130. self.assertTrue(
  3131. data
  3132. in [
  3133. {
  3134. "README.md": {
  3135. "lines_added": 5,
  3136. "lines_removed": 0,
  3137. "new_id": "bd913ea153650b94f33f53e5164c36a28b761bf4",
  3138. "old_id": "0000000000000000000000000000000000000000",
  3139. "old_path": "README.md",
  3140. "status": "A",
  3141. },
  3142. "sources": {
  3143. "lines_added": 5,
  3144. "lines_removed": 0,
  3145. "new_id": "540916fbd3d825d14cc0c0b2397606fda69379ce",
  3146. "old_id": "293500070b9dfc6ab66e31383f8f7fccf6a95fe2",
  3147. "old_path": "sources",
  3148. "status": "M",
  3149. },
  3150. },
  3151. {
  3152. "README.md": {
  3153. "lines_added": 5,
  3154. "lines_removed": 0,
  3155. "new_id": "bd913ea153650b94f33f53e5164c36a28b761bf4",
  3156. "old_id": "0000000000000000000000000000000000000000",
  3157. "old_path": "README.md",
  3158. "status": "A",
  3159. },
  3160. "sources": {
  3161. "lines_added": 10,
  3162. "lines_removed": 0,
  3163. "new_id": "540916fbd3d825d14cc0c0b2397606fda69379ce",
  3164. "old_id": "265f133a7c94ede4cb183dd808219c5bf9e08f87",
  3165. "old_path": "sources",
  3166. "status": "M",
  3167. },
  3168. },
  3169. ]
  3170. )
  3171. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  3172. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  3173. def test_api_pull_request_diffstats_file_modified_deleted(self):
  3174. """ Test the api_pull_request_merge method of the flask api. """
  3175. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  3176. self.assertEqual(len(repo.requests), 1)
  3177. pagure.lib.tasks.update_pull_request(repo.requests[0].uid)
  3178. tests.add_readme_git_repo(
  3179. os.path.join(self.path, "repos", "test.git"),
  3180. readme_name="README.md",
  3181. branch="test",
  3182. )
  3183. tests.remove_file_git_repo(
  3184. os.path.join(self.path, "repos", "test.git"),
  3185. filename="sources",
  3186. branch="test",
  3187. )
  3188. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  3189. self.assertEqual(len(repo.requests), 1)
  3190. pagure.lib.tasks.update_pull_request(repo.requests[0].uid)
  3191. output = self.app.get("/api/0/test/pull-request/1/diffstats")
  3192. self.assertEqual(output.status_code, 200)
  3193. data = json.loads(output.get_data(as_text=True))
  3194. self.assertEqual(
  3195. data,
  3196. {
  3197. "README.md": {
  3198. "lines_added": 5,
  3199. "lines_removed": 0,
  3200. "new_id": "bd913ea153650b94f33f53e5164c36a28b761bf4",
  3201. "old_id": "0000000000000000000000000000000000000000",
  3202. "old_path": "README.md",
  3203. "status": "A",
  3204. },
  3205. "sources": {
  3206. "lines_added": 0,
  3207. "lines_removed": 5,
  3208. "new_id": "0000000000000000000000000000000000000000",
  3209. "old_id": "265f133a7c94ede4cb183dd808219c5bf9e08f87",
  3210. "old_path": "sources",
  3211. "status": "D",
  3212. },
  3213. },
  3214. )
  3215. class PagureApiThresholdReachedTests(tests.Modeltests):
  3216. """Test the behavior of the threshold_reached value returned by the API."""
  3217. maxDiff = None
  3218. def _clean_data(self, data):
  3219. data["project"]["date_created"] = "1516348115"
  3220. data["project"]["date_modified"] = "1516348115"
  3221. data["repo_from"]["date_created"] = "1516348115"
  3222. data["repo_from"]["date_modified"] = "1516348115"
  3223. data["uid"] = "e8b68df8711648deac67c3afed15a798"
  3224. data["commit_start"] = "114f1b468a5f05e635fcb6394273f3f907386eab"
  3225. data["commit_stop"] = "114f1b468a5f05e635fcb6394273f3f907386eab"
  3226. data["date_created"] = "1516348115"
  3227. data["last_updated"] = "1516348115"
  3228. data["updated_on"] = "1516348115"
  3229. data["comments"] = [] # Let's not check the comments
  3230. return data
  3231. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  3232. def setUp(self):
  3233. """ Set up the environment for the tests. """
  3234. super(PagureApiThresholdReachedTests, self).setUp()
  3235. tests.create_projects(self.session)
  3236. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  3237. tests.create_projects_git(
  3238. os.path.join(self.path, "requests"), bare=True
  3239. )
  3240. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  3241. tests.add_commit_git_repo(
  3242. os.path.join(self.path, "repos", "test.git"), branch="test"
  3243. )
  3244. tests.create_tokens(self.session)
  3245. tests.create_tokens_acl(self.session)
  3246. # Add a token for user `foo`
  3247. item = pagure.lib.model.Token(
  3248. id="aaabbbcccddd_foo",
  3249. user_id=2,
  3250. project_id=1,
  3251. expiration=datetime.datetime.utcnow()
  3252. + datetime.timedelta(days=30),
  3253. )
  3254. self.session.add(item)
  3255. self.session.commit()
  3256. tests.create_tokens_acl(self.session, token_id="aaabbbcccddd_foo")
  3257. # Add a minimal required score:
  3258. repo = pagure.lib.query._get_project(self.session, "test")
  3259. settings = repo.settings
  3260. settings["Minimum_score_to_merge_pull-request"] = 2
  3261. repo.settings = settings
  3262. self.session.add(repo)
  3263. self.session.commit()
  3264. headers = {"Authorization": "token aaabbbcccddd"}
  3265. data = {
  3266. "title": "Test PR",
  3267. "initial_comment": "Nothing much, the changes speak for themselves",
  3268. "branch_to": "master",
  3269. "branch_from": "test",
  3270. }
  3271. output = self.app.post(
  3272. "/api/0/test/pull-request/new", headers=headers, data=data
  3273. )
  3274. self.assertEqual(output.status_code, 200)
  3275. self.expected_data = {
  3276. "assignee": None,
  3277. "branch": "master",
  3278. "branch_from": "test",
  3279. "cached_merge_status": "unknown",
  3280. "closed_at": None,
  3281. "closed_by": None,
  3282. "comments": [],
  3283. "commit_start": "114f1b468a5f05e635fcb6394273f3f907386eab",
  3284. "commit_stop": "114f1b468a5f05e635fcb6394273f3f907386eab",
  3285. "date_created": "1516348115",
  3286. "full_url": "http://localhost.localdomain/test/pull-request/1",
  3287. "id": 1,
  3288. "initial_comment": "Nothing much, the changes speak for themselves",
  3289. "last_updated": "1516348115",
  3290. "project": {
  3291. "access_groups": {
  3292. "admin": [],
  3293. "collaborator": [],
  3294. "commit": [],
  3295. "ticket": [],
  3296. },
  3297. "access_users": {
  3298. "admin": [],
  3299. "collaborator": [],
  3300. "commit": [],
  3301. "owner": ["pingou"],
  3302. "ticket": [],
  3303. },
  3304. "close_status": [
  3305. "Invalid",
  3306. "Insufficient data",
  3307. "Fixed",
  3308. "Duplicate",
  3309. ],
  3310. "custom_keys": [],
  3311. "date_created": "1516348115",
  3312. "date_modified": "1516348115",
  3313. "description": "test project #1",
  3314. "full_url": "http://localhost.localdomain/test",
  3315. "fullname": "test",
  3316. "id": 1,
  3317. "milestones": {},
  3318. "name": "test",
  3319. "namespace": None,
  3320. "parent": None,
  3321. "priorities": {},
  3322. "tags": [],
  3323. "url_path": "test",
  3324. "user": {
  3325. "full_url": "http://localhost.localdomain/user/pingou",
  3326. "fullname": "PY C",
  3327. "name": "pingou",
  3328. "url_path": "user/pingou",
  3329. },
  3330. },
  3331. "remote_git": None,
  3332. "repo_from": {
  3333. "access_groups": {
  3334. "admin": [],
  3335. "collaborator": [],
  3336. "commit": [],
  3337. "ticket": [],
  3338. },
  3339. "access_users": {
  3340. "admin": [],
  3341. "collaborator": [],
  3342. "commit": [],
  3343. "owner": ["pingou"],
  3344. "ticket": [],
  3345. },
  3346. "close_status": [
  3347. "Invalid",
  3348. "Insufficient data",
  3349. "Fixed",
  3350. "Duplicate",
  3351. ],
  3352. "custom_keys": [],
  3353. "date_created": "1516348115",
  3354. "date_modified": "1516348115",
  3355. "description": "test project #1",
  3356. "full_url": "http://localhost.localdomain/test",
  3357. "fullname": "test",
  3358. "id": 1,
  3359. "milestones": {},
  3360. "name": "test",
  3361. "namespace": None,
  3362. "parent": None,
  3363. "priorities": {},
  3364. "tags": [],
  3365. "url_path": "test",
  3366. "user": {
  3367. "full_url": "http://localhost.localdomain/user/pingou",
  3368. "fullname": "PY C",
  3369. "name": "pingou",
  3370. "url_path": "user/pingou",
  3371. },
  3372. },
  3373. "status": "Open",
  3374. "tags": [],
  3375. "threshold_reached": None,
  3376. "title": "Test PR",
  3377. "uid": "e8b68df8711648deac67c3afed15a798",
  3378. "updated_on": "1516348115",
  3379. "user": {
  3380. "full_url": "http://localhost.localdomain/user/pingou",
  3381. "fullname": "PY C",
  3382. "name": "pingou",
  3383. "url_path": "user/pingou",
  3384. },
  3385. }
  3386. def test_api_pull_request_no_comments(self):
  3387. """Check the value of threshold_reach when the PR has no comments."""
  3388. # Check the PR with 0 comment:
  3389. output = self.app.get("/api/0/test/pull-request/1")
  3390. self.assertEqual(output.status_code, 200)
  3391. data = json.loads(output.get_data(as_text=True))
  3392. data = self._clean_data(data)
  3393. self.expected_data["threshold_reached"] = False
  3394. self.assertDictEqual(data, self.expected_data)
  3395. def test_api_pull_request_one_comments(self):
  3396. """Check the value of threshold_reach when the PR has one comment."""
  3397. # Check the PR with 1 comment:
  3398. headers = {"Authorization": "token aaabbbcccddd"}
  3399. data = {"comment": "This is a very interesting solution :thumbsup:"}
  3400. output = self.app.post(
  3401. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  3402. )
  3403. self.assertEqual(output.status_code, 200)
  3404. data = json.loads(output.get_data(as_text=True))
  3405. self.assertDictEqual(data, {"message": "Comment added"})
  3406. output = self.app.get("/api/0/test/pull-request/1")
  3407. self.assertEqual(output.status_code, 200)
  3408. data = json.loads(output.get_data(as_text=True))
  3409. data = self._clean_data(data)
  3410. self.expected_data["threshold_reached"] = False
  3411. self.assertDictEqual(data, self.expected_data)
  3412. def test_api_pull_request_two_comments_one_person(self):
  3413. """Check the value of threshold_reach when the PR has two comments
  3414. but from the same person.
  3415. """
  3416. # Add two comments from the same user:
  3417. headers = {"Authorization": "token aaabbbcccddd"}
  3418. data = {"comment": "This is a very interesting solution :thumbsup:"}
  3419. output = self.app.post(
  3420. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  3421. )
  3422. self.assertEqual(output.status_code, 200)
  3423. data = json.loads(output.get_data(as_text=True))
  3424. self.assertDictEqual(data, {"message": "Comment added"})
  3425. headers = {"Authorization": "token aaabbbcccddd"}
  3426. data = {"comment": "Indeed it is :thumbsup:"}
  3427. output = self.app.post(
  3428. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  3429. )
  3430. self.assertEqual(output.status_code, 200)
  3431. data = json.loads(output.get_data(as_text=True))
  3432. self.assertDictEqual(data, {"message": "Comment added"})
  3433. output = self.app.get("/api/0/test/pull-request/1")
  3434. self.assertEqual(output.status_code, 200)
  3435. data = json.loads(output.get_data(as_text=True))
  3436. data = self._clean_data(data)
  3437. self.expected_data["threshold_reached"] = False
  3438. self.assertDictEqual(data, self.expected_data)
  3439. def test_api_pull_request_two_comments_two_persons(self):
  3440. """Check the value of threshold_reach when the PR has two comments
  3441. from two different persons.
  3442. """
  3443. # Add two comments from two users:
  3444. headers = {"Authorization": "token aaabbbcccddd"}
  3445. data = {"comment": "This is a very interesting solution :thumbsup:"}
  3446. output = self.app.post(
  3447. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  3448. )
  3449. self.assertEqual(output.status_code, 200)
  3450. data = json.loads(output.get_data(as_text=True))
  3451. self.assertDictEqual(data, {"message": "Comment added"})
  3452. headers = {"Authorization": "token aaabbbcccddd_foo"}
  3453. data = {"comment": "Indeed it is :thumbsup:"}
  3454. output = self.app.post(
  3455. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  3456. )
  3457. self.assertEqual(output.status_code, 200)
  3458. data = json.loads(output.get_data(as_text=True))
  3459. self.assertDictEqual(data, {"message": "Comment added"})
  3460. output = self.app.get("/api/0/test/pull-request/1")
  3461. self.assertEqual(output.status_code, 200)
  3462. data = json.loads(output.get_data(as_text=True))
  3463. data = self._clean_data(data)
  3464. data["comments"] = [] # Let's not check the comments
  3465. self.expected_data["threshold_reached"] = True
  3466. self.assertDictEqual(data, self.expected_data)
  3467. def test_api_pull_request_three_comments_two_persons_changed_to_no(self):
  3468. """Check the value of threshold_reach when the PR has three
  3469. comments from two person among which one changed their mind from
  3470. +1 to -1.
  3471. """
  3472. # Add three comments from two users:
  3473. headers = {"Authorization": "token aaabbbcccddd"}
  3474. data = {"comment": "This is a very interesting solution :thumbsup:"}
  3475. output = self.app.post(
  3476. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  3477. )
  3478. self.assertEqual(output.status_code, 200)
  3479. data = json.loads(output.get_data(as_text=True))
  3480. self.assertDictEqual(data, {"message": "Comment added"})
  3481. headers = {"Authorization": "token aaabbbcccddd_foo"}
  3482. data = {"comment": "Indeed it is :thumbsup:"}
  3483. output = self.app.post(
  3484. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  3485. )
  3486. self.assertEqual(output.status_code, 200)
  3487. data = json.loads(output.get_data(as_text=True))
  3488. self.assertDictEqual(data, {"message": "Comment added"})
  3489. data = {
  3490. "comment": "Nevermind the bug is elsewhere in fact :thumbsdown:"
  3491. }
  3492. output = self.app.post(
  3493. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  3494. )
  3495. self.assertEqual(output.status_code, 200)
  3496. data = json.loads(output.get_data(as_text=True))
  3497. self.assertDictEqual(data, {"message": "Comment added"})
  3498. output = self.app.get("/api/0/test/pull-request/1")
  3499. self.assertEqual(output.status_code, 200)
  3500. data = json.loads(output.get_data(as_text=True))
  3501. data = self._clean_data(data)
  3502. data["comments"] = [] # Let's not check the comments
  3503. self.expected_data["threshold_reached"] = False
  3504. self.assertDictEqual(data, self.expected_data)
  3505. def test_api_pull_request_three_comments_two_persons_changed_to_yes(self):
  3506. """Check the value of threshold_reach when the PR has three
  3507. comments from two person among which one changed their mind from
  3508. -1 to +1
  3509. """
  3510. # Add three comments from two users:
  3511. headers = {"Authorization": "token aaabbbcccddd"}
  3512. data = {"comment": "This is a very interesting solution :thumbsup:"}
  3513. output = self.app.post(
  3514. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  3515. )
  3516. self.assertEqual(output.status_code, 200)
  3517. data = json.loads(output.get_data(as_text=True))
  3518. self.assertDictEqual(data, {"message": "Comment added"})
  3519. headers = {"Authorization": "token aaabbbcccddd_foo"}
  3520. data = {"comment": "I think the bug is elsewhere :thumbsdown:"}
  3521. output = self.app.post(
  3522. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  3523. )
  3524. self.assertEqual(output.status_code, 200)
  3525. data = json.loads(output.get_data(as_text=True))
  3526. self.assertDictEqual(data, {"message": "Comment added"})
  3527. data = {"comment": "Nevermind it is here :thumbsup:"}
  3528. output = self.app.post(
  3529. "/api/0/test/pull-request/1/comment", data=data, headers=headers
  3530. )
  3531. self.assertEqual(output.status_code, 200)
  3532. data = json.loads(output.get_data(as_text=True))
  3533. self.assertDictEqual(data, {"message": "Comment added"})
  3534. output = self.app.get("/api/0/test/pull-request/1")
  3535. self.assertEqual(output.status_code, 200)
  3536. data = json.loads(output.get_data(as_text=True))
  3537. data = self._clean_data(data)
  3538. data["comments"] = [] # Let's not check the comments
  3539. self.expected_data["threshold_reached"] = True
  3540. self.assertDictEqual(data, self.expected_data)
  3541. class PagureFlaskApiForkGetCommenttests(tests.Modeltests):
  3542. """Tests for the flask API of pagure for the comment endpoint of PRs"""
  3543. maxDiff = None
  3544. def setUp(self):
  3545. """ Set up the environment, ran before every tests. """
  3546. super(PagureFlaskApiForkGetCommenttests, self).setUp()
  3547. pagure.config.config["REQUESTS_FOLDER"] = None
  3548. tests.create_projects(self.session)
  3549. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  3550. tests.create_projects_git(
  3551. os.path.join(self.path, "requests"), bare=True
  3552. )
  3553. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  3554. tests.add_commit_git_repo(
  3555. os.path.join(self.path, "repos", "test.git"), ncommits=5
  3556. )
  3557. tests.add_commit_git_repo(
  3558. os.path.join(self.path, "repos", "test.git"), branch="test"
  3559. )
  3560. # Create the pull-request
  3561. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  3562. req = pagure.lib.query.new_pull_request(
  3563. session=self.session,
  3564. repo_from=repo,
  3565. branch_from="test",
  3566. repo_to=repo,
  3567. branch_to="master",
  3568. title="test pull-request",
  3569. user="pingou",
  3570. )
  3571. pagure.lib.query.add_pull_request_comment(
  3572. session=self.session,
  3573. request=req,
  3574. commit=None,
  3575. tree_id=None,
  3576. filename=None,
  3577. row=None,
  3578. comment="+1",
  3579. user="pingou",
  3580. notify=False,
  3581. notification=True,
  3582. )
  3583. self.session.commit()
  3584. self.assertEqual(req.id, 1)
  3585. self.assertEqual(req.title, "test pull-request")
  3586. self.assertEqual(len(req.comments), 1)
  3587. self.assertEqual(req.comments[0].id, 1)
  3588. def test_api_pull_request_get_comment_not_found(self):
  3589. """ Test the api_pull_request_get_comment method of the flask api. """
  3590. output = self.app.get("/api/0/test/pull-request/1/comment/2")
  3591. self.assertEqual(output.status_code, 404)
  3592. data = json.loads(output.get_data(as_text=True))
  3593. self.assertEqual(
  3594. data, {"error": "Comment not found", "error_code": "ENOCOMMENT"}
  3595. )
  3596. def test_api_pull_request_get_comment(self):
  3597. """ Test the api_pull_request_get_comment method of the flask api. """
  3598. output = self.app.get("/api/0/test/pull-request/1/comment/1")
  3599. self.assertEqual(output.status_code, 200)
  3600. data = json.loads(output.get_data(as_text=True))
  3601. self.assertEqual(data["comment"], "+1")
  3602. if __name__ == "__main__":
  3603. unittest.main(verbosity=2)