1
0

test_pagure_flask_ui_issues.py 176 KB

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