test_pagure_flask_api_ui_private_repo.py 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals, absolute_import
  3. import datetime
  4. import unittest
  5. import shutil
  6. import sys
  7. import tempfile
  8. import os
  9. import json
  10. import pygit2
  11. from mock import patch, MagicMock
  12. sys.path.insert(
  13. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  14. )
  15. import pagure.lib.query
  16. import tests
  17. from pagure.lib.repo import PagureRepo
  18. FULL_ISSUE_LIST = [
  19. {
  20. "assignee": None,
  21. "blocks": [],
  22. "close_status": None,
  23. "closed_at": None,
  24. "closed_by": None,
  25. "comments": [],
  26. "content": "We should work on this",
  27. "custom_fields": [],
  28. "date_created": "1431414800",
  29. "depends": [],
  30. "id": 8,
  31. "last_updated": "1431414800",
  32. "milestone": None,
  33. "priority": None,
  34. "private": True,
  35. "related_prs": [],
  36. "status": "Open",
  37. "tags": [],
  38. "title": "Test issue",
  39. "user": {
  40. "fullname": "PY C",
  41. "name": "pingou",
  42. "url_path": "user/pingou",
  43. },
  44. },
  45. {
  46. "assignee": None,
  47. "blocks": [],
  48. "close_status": None,
  49. "closed_at": None,
  50. "closed_by": None,
  51. "comments": [],
  52. "content": "This issue needs attention",
  53. "custom_fields": [],
  54. "date_created": "1431414800",
  55. "depends": [],
  56. "id": 7,
  57. "last_updated": "1431414800",
  58. "milestone": None,
  59. "priority": None,
  60. "private": True,
  61. "related_prs": [],
  62. "status": "Open",
  63. "tags": [],
  64. "title": "test issue",
  65. "user": {
  66. "fullname": "PY C",
  67. "name": "pingou",
  68. "url_path": "user/pingou",
  69. },
  70. },
  71. {
  72. "assignee": None,
  73. "blocks": [],
  74. "close_status": None,
  75. "closed_at": None,
  76. "closed_by": None,
  77. "comments": [],
  78. "content": "This issue needs attention",
  79. "custom_fields": [],
  80. "date_created": "1431414800",
  81. "depends": [],
  82. "id": 6,
  83. "last_updated": "1431414800",
  84. "milestone": None,
  85. "priority": None,
  86. "private": True,
  87. "related_prs": [],
  88. "status": "Open",
  89. "tags": [],
  90. "title": "test issue",
  91. "user": {
  92. "fullname": "PY C",
  93. "name": "pingou",
  94. "url_path": "user/pingou",
  95. },
  96. },
  97. {
  98. "assignee": None,
  99. "blocks": [],
  100. "close_status": None,
  101. "closed_at": None,
  102. "closed_by": None,
  103. "comments": [],
  104. "content": "This issue needs attention",
  105. "custom_fields": [],
  106. "date_created": "1431414800",
  107. "depends": [],
  108. "id": 5,
  109. "last_updated": "1431414800",
  110. "milestone": None,
  111. "priority": None,
  112. "private": False,
  113. "related_prs": [],
  114. "status": "Open",
  115. "tags": [],
  116. "title": "test issue",
  117. "user": {
  118. "fullname": "PY C",
  119. "name": "pingou",
  120. "url_path": "user/pingou",
  121. },
  122. },
  123. {
  124. "assignee": None,
  125. "blocks": [],
  126. "close_status": None,
  127. "closed_at": None,
  128. "closed_by": None,
  129. "comments": [],
  130. "content": "This issue needs attention",
  131. "custom_fields": [],
  132. "date_created": "1431414800",
  133. "depends": [],
  134. "id": 4,
  135. "last_updated": "1431414800",
  136. "milestone": None,
  137. "priority": None,
  138. "private": False,
  139. "related_prs": [],
  140. "status": "Open",
  141. "tags": [],
  142. "title": "test issue",
  143. "user": {
  144. "fullname": "PY C",
  145. "name": "pingou",
  146. "url_path": "user/pingou",
  147. },
  148. },
  149. {
  150. "assignee": None,
  151. "blocks": [],
  152. "close_status": None,
  153. "closed_at": None,
  154. "closed_by": None,
  155. "comments": [],
  156. "content": "This issue needs attention",
  157. "custom_fields": [],
  158. "date_created": "1431414800",
  159. "depends": [],
  160. "id": 3,
  161. "last_updated": "1431414800",
  162. "milestone": None,
  163. "priority": None,
  164. "private": False,
  165. "related_prs": [],
  166. "status": "Open",
  167. "tags": [],
  168. "title": "test issue",
  169. "user": {
  170. "fullname": "PY C",
  171. "name": "pingou",
  172. "url_path": "user/pingou",
  173. },
  174. },
  175. {
  176. "assignee": None,
  177. "blocks": [],
  178. "close_status": None,
  179. "closed_at": None,
  180. "closed_by": None,
  181. "comments": [],
  182. "content": "This issue needs attention",
  183. "custom_fields": [],
  184. "date_created": "1431414800",
  185. "depends": [],
  186. "id": 2,
  187. "last_updated": "1431414800",
  188. "milestone": None,
  189. "priority": None,
  190. "private": False,
  191. "related_prs": [],
  192. "status": "Open",
  193. "tags": [],
  194. "title": "test issue",
  195. "user": {
  196. "fullname": "PY C",
  197. "name": "pingou",
  198. "url_path": "user/pingou",
  199. },
  200. },
  201. {
  202. "assignee": None,
  203. "blocks": [],
  204. "close_status": None,
  205. "closed_at": None,
  206. "closed_by": None,
  207. "comments": [],
  208. "content": "This issue needs attention",
  209. "custom_fields": [],
  210. "date_created": "1431414800",
  211. "depends": [],
  212. "id": 1,
  213. "last_updated": "1431414800",
  214. "milestone": None,
  215. "priority": None,
  216. "private": False,
  217. "related_prs": [],
  218. "status": "Open",
  219. "tags": [],
  220. "title": "test issue",
  221. "user": {
  222. "fullname": "PY C",
  223. "name": "pingou",
  224. "url_path": "user/pingou",
  225. },
  226. },
  227. ]
  228. class PagurePrivateRepotest(tests.Modeltests):
  229. """ Tests for private repo in pagure """
  230. maxDiff = None
  231. def setUp(self):
  232. """ Set up the environnment, ran before every tests. """
  233. super(PagurePrivateRepotest, self).setUp()
  234. pagure.config.config["TESTING"] = True
  235. pagure.config.config["DATAGREPPER_URL"] = None
  236. pagure.config.config["PRIVATE_PROJECTS"] = True
  237. pagure.config.config["VIRUS_SCAN_ATTACHMENTS"] = False
  238. def set_up_git_repo(
  239. self, new_project=None, branch_from="feature", mtype="FF"
  240. ):
  241. """ Set up the git repo and create the corresponding PullRequest
  242. object.
  243. """
  244. # Create a git repo to play with
  245. gitrepo = os.path.join(self.path, "repos", "pmc.git")
  246. repo = pygit2.init_repository(gitrepo, bare=True)
  247. newpath = tempfile.mkdtemp(prefix="pagure-private-test")
  248. repopath = os.path.join(newpath, "test")
  249. clone_repo = pygit2.clone_repository(gitrepo, repopath)
  250. # Create a file in that git repo
  251. with open(os.path.join(repopath, "sources"), "w") as stream:
  252. stream.write("foo\n bar")
  253. clone_repo.index.add("sources")
  254. clone_repo.index.write()
  255. # Commits the files added
  256. tree = clone_repo.index.write_tree()
  257. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  258. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  259. clone_repo.create_commit(
  260. "refs/heads/master", # the name of the reference to update
  261. author,
  262. committer,
  263. "Add sources file for testing",
  264. # binary string representing the tree object ID
  265. tree,
  266. # list of binary strings representing parents of the new commit
  267. [],
  268. )
  269. refname = "refs/heads/master:refs/heads/master"
  270. ori_remote = clone_repo.remotes[0]
  271. PagureRepo.push(ori_remote, refname)
  272. first_commit = repo.revparse_single("HEAD")
  273. if mtype == "merge":
  274. with open(os.path.join(repopath, ".gitignore"), "w") as stream:
  275. stream.write("*~")
  276. clone_repo.index.add(".gitignore")
  277. clone_repo.index.write()
  278. # Commits the files added
  279. tree = clone_repo.index.write_tree()
  280. author = pygit2.Signature("Alice Äuthòr", "alice@äuthòrs.tld")
  281. committer = pygit2.Signature(
  282. "Cecil Cõmmîttër", "cecil@cõmmîttërs.tld"
  283. )
  284. clone_repo.create_commit(
  285. "refs/heads/master",
  286. author,
  287. committer,
  288. "Add .gitignore file for testing",
  289. # binary string representing the tree object ID
  290. tree,
  291. # list of binary strings representing parents of the new commit
  292. [first_commit.oid.hex],
  293. )
  294. refname = "refs/heads/master:refs/heads/master"
  295. ori_remote = clone_repo.remotes[0]
  296. PagureRepo.push(ori_remote, refname)
  297. if mtype == "conflicts":
  298. with open(os.path.join(repopath, "sources"), "w") as stream:
  299. stream.write("foo\n bar\nbaz")
  300. clone_repo.index.add("sources")
  301. clone_repo.index.write()
  302. # Commits the files added
  303. tree = clone_repo.index.write_tree()
  304. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  305. committer = pygit2.Signature(
  306. "Cecil Committer", "cecil@committers.tld"
  307. )
  308. clone_repo.create_commit(
  309. "refs/heads/master",
  310. author,
  311. committer,
  312. "Add sources conflicting",
  313. # binary string representing the tree object ID
  314. tree,
  315. # list of binary strings representing parents of the new commit
  316. [first_commit.oid.hex],
  317. )
  318. refname = "refs/heads/master:refs/heads/master"
  319. ori_remote = clone_repo.remotes[0]
  320. PagureRepo.push(ori_remote, refname)
  321. # Set the second repo
  322. new_gitrepo = repopath
  323. if new_project:
  324. # Create a new git repo to play with
  325. new_gitrepo = os.path.join(newpath, new_project.fullname)
  326. if not os.path.exists(new_gitrepo):
  327. os.makedirs(new_gitrepo)
  328. new_repo = pygit2.clone_repository(gitrepo, new_gitrepo)
  329. repo = pygit2.Repository(new_gitrepo)
  330. if mtype != "nochanges":
  331. # Edit the sources file again
  332. with open(os.path.join(new_gitrepo, "sources"), "w") as stream:
  333. stream.write("foo\n bar\nbaz\n boose")
  334. repo.index.add("sources")
  335. repo.index.write()
  336. # Commits the files added
  337. tree = repo.index.write_tree()
  338. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  339. committer = pygit2.Signature(
  340. "Cecil Committer", "cecil@committers.tld"
  341. )
  342. repo.create_commit(
  343. "refs/heads/%s" % branch_from,
  344. author,
  345. committer,
  346. "A commit on branch %s" % branch_from,
  347. tree,
  348. [first_commit.oid.hex],
  349. )
  350. refname = "refs/heads/%s" % (branch_from)
  351. ori_remote = repo.remotes[0]
  352. PagureRepo.push(ori_remote, refname)
  353. # Create a PR for these changes
  354. project = pagure.lib.query._get_project(self.session, "pmc")
  355. req = pagure.lib.query.new_pull_request(
  356. session=self.session,
  357. repo_from=project,
  358. branch_from=branch_from,
  359. repo_to=project,
  360. branch_to="master",
  361. title="PR from the %s branch" % branch_from,
  362. user="pingou",
  363. )
  364. self.session.commit()
  365. self.assertEqual(req.id, 1)
  366. self.assertEqual(req.title, "PR from the %s branch" % branch_from)
  367. shutil.rmtree(newpath)
  368. def test_index(self):
  369. """ Test the index endpoint. """
  370. output = self.app.get("/")
  371. self.assertEqual(output.status_code, 200)
  372. self.assertIn(
  373. '<h3 class="m-0 font-weight-bold">All Projects '
  374. '<span class="badge badge-secondary">0</span></h3>',
  375. output.get_data(as_text=True),
  376. )
  377. # Add a private project
  378. item = pagure.lib.model.Project(
  379. user_id=2, # foo
  380. name="test3",
  381. description="test project description",
  382. hook_token="aaabbbeee",
  383. private=True,
  384. )
  385. self.session.add(item)
  386. # Add a public project
  387. item = pagure.lib.model.Project(
  388. user_id=2, # foo
  389. name="test4",
  390. description="test project description",
  391. hook_token="aaabbbeeeccceee",
  392. )
  393. self.session.add(item)
  394. self.session.commit()
  395. output = self.app.get("/?page=abc")
  396. self.assertEqual(output.status_code, 200)
  397. output_text = output.get_data(as_text=True)
  398. self.assertIn(
  399. '<h3 class="m-0 font-weight-bold">All Projects '
  400. '<span class="badge badge-secondary">1</span></h3>',
  401. output_text,
  402. )
  403. user = tests.FakeUser(username="foo")
  404. with tests.user_set(self.app.application, user):
  405. output = self.app.get("/", follow_redirects=True)
  406. output_text = output.get_data(as_text=True)
  407. self.assertIn(
  408. '<h4 class="font-weight-bold mb-0">My Projects</h4>',
  409. output_text,
  410. )
  411. self.assertIn("2 Projects</span>", output_text)
  412. self.assertNotIn(
  413. '<span class="d-none d-md-inline">Forks', output_text
  414. )
  415. self.assertEqual(
  416. output_text.count('<span class="d-none d-md-inline">Groups'), 0
  417. )
  418. def test_view_user(self):
  419. """ Test the view_user endpoint. """
  420. output = self.app.get("/user/foo?repopage=abc&forkpage=def")
  421. self.assertEqual(output.status_code, 200)
  422. output_text = output.get_data(as_text=True)
  423. self.assertIn(
  424. """<span>
  425. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  426. <span class="d-none d-md-inline">Projects&nbsp;</span>
  427. </span>
  428. <div class="ml-auto">
  429. <span class="badge badge-secondary">
  430. 0
  431. </span>
  432. </div>""",
  433. output_text,
  434. )
  435. self.assertIn(
  436. """<span>
  437. <i class="fa fa-fw text-muted fa-code-fork"></i>
  438. <span class="d-none d-md-inline">Forks&nbsp;</span>
  439. </span>
  440. <div class="ml-auto">
  441. <span class="badge badge-secondary">
  442. 0
  443. </span>
  444. </div>""",
  445. output_text,
  446. )
  447. self.assertIn(
  448. """<span>
  449. <i class="fa fa-fw text-muted fa-users"></i>
  450. <span class="d-none d-md-inline">Groups&nbsp;</span>
  451. </span>
  452. <div class="ml-auto">
  453. <span class="badge badge-secondary">
  454. 0
  455. </span>
  456. </div>""",
  457. output_text,
  458. )
  459. # Add a private project
  460. item = pagure.lib.model.Project(
  461. user_id=2, # foo
  462. name="test3",
  463. description="test project description",
  464. hook_token="aaabbbeee",
  465. private=True,
  466. )
  467. self.session.add(item)
  468. # Add a public project
  469. item = pagure.lib.model.Project(
  470. user_id=2, # foo
  471. name="test4",
  472. description="test project description",
  473. hook_token="aaabbbeeeccceee",
  474. )
  475. self.session.add(item)
  476. self.session.commit()
  477. self.gitrepos = tests.create_projects_git(
  478. pagure.config.config["GIT_FOLDER"]
  479. )
  480. output = self.app.get("/user/foo")
  481. self.assertEqual(output.status_code, 200)
  482. output_text = output.get_data(as_text=True)
  483. self.assertIn(
  484. """<span>
  485. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  486. <span class="d-none d-md-inline">Projects&nbsp;</span>
  487. </span>
  488. <div class="ml-auto">
  489. <span class="badge badge-secondary">
  490. 1
  491. </span>
  492. </div>""",
  493. output_text,
  494. )
  495. self.assertIn(
  496. """<span>
  497. <i class="fa fa-fw text-muted fa-code-fork"></i>
  498. <span class="d-none d-md-inline">Forks&nbsp;</span>
  499. </span>
  500. <div class="ml-auto">
  501. <span class="badge badge-secondary">
  502. 0
  503. </span>
  504. </div>""",
  505. output_text,
  506. )
  507. self.assertIn(
  508. """<span>
  509. <i class="fa fa-fw text-muted fa-users"></i>
  510. <span class="d-none d-md-inline">Groups&nbsp;</span>
  511. </span>
  512. <div class="ml-auto">
  513. <span class="badge badge-secondary">
  514. 0
  515. </span>
  516. </div>""",
  517. output_text,
  518. )
  519. user = tests.FakeUser(username="foo")
  520. with tests.user_set(self.app.application, user):
  521. output = self.app.get("/user/foo")
  522. output_text = output.get_data(as_text=True)
  523. self.assertIn(
  524. """<span>
  525. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  526. <span class="d-none d-md-inline">Projects&nbsp;</span>
  527. </span>
  528. <div class="ml-auto">
  529. <span class="badge badge-secondary">
  530. 1
  531. </span>
  532. </div>""",
  533. output_text,
  534. )
  535. self.assertIn(
  536. """<span>
  537. <i class="fa fa-fw text-muted fa-code-fork"></i>
  538. <span class="d-none d-md-inline">Forks&nbsp;</span>
  539. </span>
  540. <div class="ml-auto">
  541. <span class="badge badge-secondary">
  542. 0
  543. </span>
  544. </div>""",
  545. output_text,
  546. )
  547. self.assertIn(
  548. """<span>
  549. <i class="fa fa-fw text-muted fa-users"></i>
  550. <span class="d-none d-md-inline">Groups&nbsp;</span>
  551. </span>
  552. <div class="ml-auto">
  553. <span class="badge badge-secondary">
  554. 0
  555. </span>
  556. </div>""",
  557. output_text,
  558. )
  559. user.username = "pingou"
  560. with tests.user_set(self.app.application, user):
  561. output = self.app.get("/user/foo")
  562. output_text = output.get_data(as_text=True)
  563. self.assertIn(
  564. """<span>
  565. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  566. <span class="d-none d-md-inline">Projects&nbsp;</span>
  567. </span>
  568. <div class="ml-auto">
  569. <span class="badge badge-secondary">
  570. 1
  571. </span>
  572. </div>""",
  573. output_text,
  574. )
  575. self.assertIn(
  576. """<span>
  577. <i class="fa fa-fw text-muted fa-code-fork"></i>
  578. <span class="d-none d-md-inline">Forks&nbsp;</span>
  579. </span>
  580. <div class="ml-auto">
  581. <span class="badge badge-secondary">
  582. 0
  583. </span>
  584. </div>""",
  585. output_text,
  586. )
  587. self.assertIn(
  588. """<span>
  589. <i class="fa fa-fw text-muted fa-users"></i>
  590. <span class="d-none d-md-inline">Groups&nbsp;</span>
  591. </span>
  592. <div class="ml-auto">
  593. <span class="badge badge-secondary">
  594. 0
  595. </span>
  596. </div>""",
  597. output_text,
  598. )
  599. # Check pingou has 0 projects
  600. user.username = "pingou"
  601. with tests.user_set(self.app.application, user):
  602. output = self.app.get("/", follow_redirects=True)
  603. output_text = output.get_data(as_text=True)
  604. self.assertIn(
  605. '<h4 class="font-weight-bold mb-0">My Projects</h4>',
  606. output_text,
  607. )
  608. self.assertIn("0 Projects</span>", output_text)
  609. self.assertNotIn(
  610. '<span class="d-none d-md-inline">Forks', output_text
  611. )
  612. self.assertEqual(
  613. output_text.count('<span class="d-none d-md-inline">Groups'), 0
  614. )
  615. repo = pagure.lib.query._get_project(self.session, "test3")
  616. msg = pagure.lib.query.add_user_to_project(
  617. session=self.session, project=repo, new_user="pingou", user="foo"
  618. )
  619. self.session.commit()
  620. self.assertEqual(msg, "User added")
  621. # New user added to private projects
  622. user.username = "pingou"
  623. with tests.user_set(self.app.application, user):
  624. output = self.app.get("/", follow_redirects=True)
  625. output_text = output.get_data(as_text=True)
  626. self.assertIn(
  627. '<h4 class="font-weight-bold mb-0">My Projects</h4>',
  628. output_text,
  629. )
  630. self.assertIn("1 Projects</span>", output_text)
  631. self.assertNotIn(
  632. '<span class="d-none d-md-inline">Forks', output_text
  633. )
  634. self.assertEqual(
  635. output_text.count('<span class="d-none d-md-inline">Groups'), 0
  636. )
  637. @patch("pagure.decorators.admin_session_timedout")
  638. def test_private_settings_ui(self, ast):
  639. """ Test UI for private repo"""
  640. ast.return_value = False
  641. # Add private repo
  642. item = pagure.lib.model.Project(
  643. user_id=1, # pingou
  644. name="test4",
  645. description="test project description",
  646. hook_token="aaabbbeeeceee",
  647. private=True,
  648. )
  649. self.session.add(item)
  650. self.session.commit()
  651. # Add a git repo
  652. repo_path = os.path.join(
  653. pagure.config.config.get("GIT_FOLDER"), "test4.git"
  654. )
  655. if not os.path.exists(repo_path):
  656. os.makedirs(repo_path)
  657. pygit2.init_repository(repo_path)
  658. user = tests.FakeUser(username="pingou")
  659. with tests.user_set(self.app.application, user):
  660. tests.create_projects(self.session)
  661. tests.create_projects_git(pagure.config.config.get("GIT_FOLDER"))
  662. output = self.app.get("/test/settings")
  663. # Check for a public repo
  664. self.assertEqual(output.status_code, 200)
  665. self.assertNotIn(
  666. '<input type="checkbox" value="private" name="private"',
  667. output.get_data(as_text=True),
  668. )
  669. output = self.app.get("/test4/settings")
  670. # Check for private repo
  671. self.assertEqual(output.status_code, 200)
  672. self.assertIn(
  673. '<input type="checkbox" value="private" name="private" checked="" />',
  674. output.get_data(as_text=True),
  675. )
  676. # Check the new project form has 'private' checkbox
  677. output = self.app.get("/new")
  678. self.assertEqual(output.status_code, 200)
  679. self.assertIn(
  680. '<input id="private" name="private" type="checkbox" value="y">',
  681. output.get_data(as_text=True),
  682. )
  683. @patch("pagure.decorators.admin_session_timedout")
  684. def test_private_settings_ui_update_privacy_false(self, ast):
  685. """ Test UI for private repo"""
  686. ast.return_value = False
  687. # Add private repo
  688. item = pagure.lib.model.Project(
  689. user_id=1, # pingou
  690. name="test4",
  691. description="test project description",
  692. hook_token="aaabbbeeeceee",
  693. private=True,
  694. )
  695. self.session.add(item)
  696. self.session.commit()
  697. # Add a git repo
  698. repo_path = os.path.join(
  699. pagure.config.config.get("GIT_FOLDER"), "test4.git"
  700. )
  701. pygit2.init_repository(repo_path)
  702. user = tests.FakeUser(username="pingou")
  703. with tests.user_set(self.app.application, user):
  704. # Check for private repo
  705. output = self.app.get("/test4/settings")
  706. self.assertEqual(output.status_code, 200)
  707. self.assertIn(
  708. '<input type="checkbox" value="private" name="private" checked="" />',
  709. output.get_data(as_text=True),
  710. )
  711. self.session.commit()
  712. repo = pagure.lib.query._get_project(self.session, "test4")
  713. self.assertTrue(repo.private)
  714. # Make the project public
  715. data = {
  716. "description": "test project description",
  717. "private": False,
  718. "csrf_token": self.get_csrf(),
  719. }
  720. output = self.app.post(
  721. "/test4/update", data=data, follow_redirects=True
  722. )
  723. self.assertEqual(output.status_code, 200)
  724. self.assertIn("Project updated", output.get_data(as_text=True))
  725. self.assertNotIn(
  726. '<input type="checkbox" value="private" name="private" checked="" />',
  727. output.get_data(as_text=True),
  728. )
  729. self.session.commit()
  730. repo = pagure.lib.query._get_project(self.session, "test4")
  731. self.assertFalse(repo.private)
  732. @patch("pagure.decorators.admin_session_timedout")
  733. def test_private_settings_ui_update_privacy_true(self, ast):
  734. """ Test UI for private repo"""
  735. ast.return_value = False
  736. # Add private repo
  737. item = pagure.lib.model.Project(
  738. user_id=1, # pingou
  739. name="test4",
  740. description="test project description",
  741. hook_token="aaabbbeeeceee",
  742. private=False,
  743. )
  744. self.session.add(item)
  745. self.session.commit()
  746. # Add a git repo
  747. repo_path = os.path.join(
  748. pagure.config.config.get("GIT_FOLDER"), "test4.git"
  749. )
  750. pygit2.init_repository(repo_path)
  751. user = tests.FakeUser(username="pingou")
  752. with tests.user_set(self.app.application, user):
  753. # Check for public repo
  754. output = self.app.get("/test4/settings")
  755. self.assertEqual(output.status_code, 200)
  756. self.assertNotIn(
  757. '<input type="checkbox" value="private" name="private" checked=""/>',
  758. output.get_data(as_text=True),
  759. )
  760. self.session.commit()
  761. repo = pagure.lib.query._get_project(self.session, "test4")
  762. self.assertFalse(repo.private)
  763. # Make the project private
  764. data = {
  765. "description": "test project description",
  766. "private": True,
  767. "csrf_token": self.get_csrf(),
  768. }
  769. output = self.app.post(
  770. "/test4/update", data=data, follow_redirects=True
  771. )
  772. self.assertEqual(output.status_code, 200)
  773. self.assertIn("Project updated", output.get_data(as_text=True))
  774. self.assertNotIn(
  775. '<input type="checkbox" value="private" name="private" checked=""/>',
  776. output.get_data(as_text=True),
  777. )
  778. # No change since we can't do public -> private
  779. self.session.commit()
  780. repo = pagure.lib.query._get_project(self.session, "test4")
  781. self.assertFalse(repo.private)
  782. @patch("pagure.lib.notify.send_email")
  783. def test_private_pr(self, send_email):
  784. """Test pull request made to the private repo"""
  785. send_email.return_value = True
  786. # Add a private project
  787. item = pagure.lib.model.Project(
  788. user_id=1, # pingou
  789. name="pmc",
  790. description="test project description",
  791. hook_token="aaabbbeeeceee",
  792. private=True,
  793. )
  794. self.session.add(item)
  795. self.session.commit()
  796. repo = pagure.lib.query._get_project(self.session, "pmc")
  797. msg = pagure.lib.query.add_user_to_project(
  798. session=self.session, project=repo, new_user="foo", user="pingou"
  799. )
  800. self.session.commit()
  801. self.assertEqual(msg, "User added")
  802. # Create all the git repos
  803. tests.create_projects_git(
  804. os.path.join(self.path, "requests"), bare=True
  805. )
  806. # Add a git repo
  807. repo_path = os.path.join(
  808. pagure.config.config.get("REQUESTS_FOLDER"), "pmc.git"
  809. )
  810. if not os.path.exists(repo_path):
  811. os.makedirs(repo_path)
  812. pygit2.init_repository(repo_path, bare=True)
  813. # Check repo was created - Doesn't show on the public page
  814. user = tests.FakeUser(username="pingou")
  815. with tests.user_set(self.app.application, user):
  816. output = self.app.get("/user/pingou/")
  817. self.assertEqual(output.status_code, 200)
  818. self.assertIn(
  819. """<span>
  820. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  821. <span class="d-none d-md-inline">Projects&nbsp;</span>
  822. </span>
  823. <div class="ml-auto">
  824. <span class="badge badge-secondary">
  825. 0
  826. </span>
  827. </div>""",
  828. output.get_data(as_text=True),
  829. )
  830. self.assertIn(
  831. """<span>
  832. <i class="fa fa-fw text-muted fa-code-fork"></i>
  833. <span class="d-none d-md-inline">Forks&nbsp;</span>
  834. </span>
  835. <div class="ml-auto">
  836. <span class="badge badge-secondary">
  837. 0
  838. </span>
  839. </div>""",
  840. output.get_data(as_text=True),
  841. )
  842. # Shows on the front page
  843. output = self.app.get("/dashboard/projects")
  844. self.assertEqual(output.status_code, 200)
  845. self.assertIn(
  846. """<span>
  847. <i class="fa fa-calendar-o fa-rotate-270 fa-fw text-muted"></i>
  848. <span class="d-none d-md-inline">Projects&nbsp;</span>
  849. </span>
  850. <div class="ml-auto">
  851. <span class="badge badge-secondary">
  852. 1
  853. </span>
  854. </div>""",
  855. output.get_data(as_text=True),
  856. )
  857. self.set_up_git_repo(new_project=None, branch_from="feature")
  858. project = pagure.lib.query._get_project(self.session, "pmc")
  859. self.assertEqual(len(project.requests), 1)
  860. output = self.app.get("/pmc/pull-request/1")
  861. self.assertEqual(output.status_code, 200)
  862. # Check repo was created
  863. user = tests.FakeUser()
  864. with tests.user_set(self.app.application, user):
  865. output = self.app.get("/pmc/pull-requests")
  866. self.assertEqual(output.status_code, 404)
  867. user = tests.FakeUser(username="pingou")
  868. with tests.user_set(self.app.application, user):
  869. output = self.app.get("/pmc/pull-requests")
  870. self.assertEqual(output.status_code, 200)
  871. user = tests.FakeUser(username="foo")
  872. with tests.user_set(self.app.application, user):
  873. output = self.app.get("/pmc/pull-requests")
  874. self.assertEqual(output.status_code, 200)
  875. @patch("pagure.lib.git.update_git")
  876. @patch("pagure.lib.notify.send_email")
  877. def test_private_repo_issues_ui(self, p_send_email, p_ugt):
  878. """ Test issues made to private repo"""
  879. p_send_email.return_value = True
  880. p_ugt.return_value = True
  881. # Add private repo
  882. item = pagure.lib.model.Project(
  883. user_id=1, # pingou
  884. name="test4",
  885. description="test project description",
  886. hook_token="aaabbbeeeceee",
  887. private=True,
  888. )
  889. self.session.add(item)
  890. self.session.commit()
  891. for repo in ["GIT_FOLDER", "TICKETS_FOLDER"]:
  892. # Add a git repo
  893. repo_path = os.path.join(
  894. pagure.config.config.get(repo), "test4.git"
  895. )
  896. if not os.path.exists(repo_path):
  897. os.makedirs(repo_path)
  898. pygit2.init_repository(repo_path)
  899. # Check if the private repo issues are publicly not accesible
  900. output = self.app.get("/test4/issues")
  901. self.assertEqual(output.status_code, 404)
  902. # Create issues to play with
  903. repo = pagure.lib.query._get_project(self.session, "test4")
  904. msg = pagure.lib.query.new_issue(
  905. session=self.session,
  906. repo=repo,
  907. title="Test issue",
  908. content="We should work on this",
  909. user="pingou",
  910. )
  911. self.session.commit()
  912. self.assertEqual(msg.title, "Test issue")
  913. user = tests.FakeUser()
  914. with tests.user_set(self.app.application, user):
  915. # Whole list
  916. output = self.app.get("/test4/issues")
  917. self.assertEqual(output.status_code, 404)
  918. # Check single issue
  919. output = self.app.get("/test4/issue/1")
  920. self.assertEqual(output.status_code, 404)
  921. user = tests.FakeUser()
  922. with tests.user_set(self.app.application, user):
  923. # Whole list
  924. output = self.app.get("/test4/issues")
  925. self.assertEqual(output.status_code, 404)
  926. user = tests.FakeUser(username="pingou")
  927. with tests.user_set(self.app.application, user):
  928. # Whole list
  929. output = self.app.get("/test4/issues")
  930. self.assertEqual(output.status_code, 200)
  931. self.assertIn(
  932. "<title>Issues - test4 - Pagure</title>",
  933. output.get_data(as_text=True),
  934. )
  935. self.assertTrue(
  936. '<span class="fa fa-fw fa-exclamation-circle"></span> 1 Open Issues\n'
  937. in output.get_data(as_text=True)
  938. )
  939. # Check single issue
  940. output = self.app.get("/test4/issue/1")
  941. self.assertEqual(output.status_code, 200)
  942. repo = pagure.lib.query._get_project(self.session, "test4")
  943. msg = pagure.lib.query.add_user_to_project(
  944. session=self.session, project=repo, new_user="foo", user="pingou"
  945. )
  946. self.session.commit()
  947. self.assertEqual(msg, "User added")
  948. user.username = "foo"
  949. with tests.user_set(self.app.application, user):
  950. # Whole list
  951. output = self.app.get("/test4/issues")
  952. self.assertEqual(output.status_code, 200)
  953. self.assertIn(
  954. "<title>Issues - test4 - Pagure</title>",
  955. output.get_data(as_text=True),
  956. )
  957. self.assertTrue(
  958. '<span class="fa fa-fw fa-exclamation-circle"></span> 1 Open Issues\n'
  959. in output.get_data(as_text=True)
  960. )
  961. # Check single issue
  962. output = self.app.get("/test4/issue/1")
  963. self.assertEqual(output.status_code, 200)
  964. @patch("pagure.decorators.admin_session_timedout")
  965. def test_private_repo_ui_for_different_repo_user(self, ast):
  966. """ Test the private repo for different ACLS"""
  967. ast.return_value = False
  968. # Add private repo
  969. item = pagure.lib.model.Project(
  970. user_id=1, # pingou
  971. name="test4",
  972. description="test project description",
  973. hook_token="aaabbbeeeceee",
  974. private=True,
  975. )
  976. self.session.add(item)
  977. self.session.commit()
  978. repo = pagure.lib.query._get_project(self.session, "test4")
  979. # Add a git repo
  980. repo_path = os.path.join(
  981. pagure.config.config.get("GIT_FOLDER"), "test4.git"
  982. )
  983. pygit2.init_repository(repo_path)
  984. user = tests.FakeUser(username="pingou")
  985. with tests.user_set(self.app.application, user):
  986. # Check for private repo
  987. output = self.app.get("/test4")
  988. self.assertEqual(output.status_code, 200)
  989. # Check if the user who doesn't have access to private repo can access it
  990. user = tests.FakeUser(username="foo")
  991. with tests.user_set(self.app.application, user):
  992. output = self.app.get("/test4")
  993. self.assertEqual(output.status_code, 404)
  994. # Add commit access to a user
  995. pagure.lib.query.add_user_to_project(
  996. self.session,
  997. project=repo,
  998. new_user="foo",
  999. user="pingou",
  1000. access="commit",
  1001. )
  1002. self.session.commit()
  1003. repo = pagure.lib.query._get_project(self.session, "test4")
  1004. self.assertEqual(len(repo.users), 1)
  1005. # Check if the user can access private repo
  1006. user = tests.FakeUser(username="foo")
  1007. with tests.user_set(self.app.application, user):
  1008. output = self.app.get("/test4")
  1009. self.assertEqual(output.status_code, 200)
  1010. # Making a new user bar
  1011. item = pagure.lib.model.User(
  1012. user="bar",
  1013. fullname="bar baz",
  1014. password="foo",
  1015. default_email="bar@bar.com",
  1016. )
  1017. self.session.add(item)
  1018. item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
  1019. self.session.add(item)
  1020. self.session.commit()
  1021. # Check that bar shouldn't be able to access the project
  1022. user = tests.FakeUser(username="bar")
  1023. with tests.user_set(self.app.application, user):
  1024. output = self.app.get("/test4")
  1025. self.assertEqual(output.status_code, 404)
  1026. # Adding a ticket level access to bar
  1027. pagure.lib.query.add_user_to_project(
  1028. self.session,
  1029. project=repo,
  1030. new_user="bar",
  1031. user="pingou",
  1032. access="ticket",
  1033. )
  1034. self.session.commit()
  1035. repo = pagure.lib.query._get_project(self.session, "test4")
  1036. self.assertEqual(len(repo.users), 2)
  1037. # Check if the ticket level access user can access the project
  1038. user = tests.FakeUser(username="bar")
  1039. with tests.user_set(self.app.application, user):
  1040. output = self.app.get("/test4")
  1041. self.assertEqual(output.status_code, 200)
  1042. # API checks
  1043. def test_api_private_repo_projects(self):
  1044. """ Test api points for private repo for projects"""
  1045. # Add private repo
  1046. item = pagure.lib.model.Project(
  1047. user_id=1, # pingou
  1048. name="test4",
  1049. description="test project description",
  1050. hook_token="aaabbbeeeceee",
  1051. private=True,
  1052. )
  1053. self.session.add(item)
  1054. self.session.commit()
  1055. # Create a git repo to play with
  1056. gitrepo = os.path.join(self.path, "repos", "test4.git")
  1057. repo = pygit2.init_repository(gitrepo, bare=True)
  1058. newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
  1059. repopath = os.path.join(newpath, "repos", "test4")
  1060. clone_repo = pygit2.clone_repository(gitrepo, repopath)
  1061. # Create a file in that git repo
  1062. with open(os.path.join(repopath, "sources"), "w") as stream:
  1063. stream.write("foo\n bar")
  1064. clone_repo.index.add("sources")
  1065. clone_repo.index.write()
  1066. # Commits the files added
  1067. tree = clone_repo.index.write_tree()
  1068. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  1069. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  1070. clone_repo.create_commit(
  1071. "refs/heads/master", # the name of the reference to update
  1072. author,
  1073. committer,
  1074. "Add sources file for testing",
  1075. # binary string representing the tree object ID
  1076. tree,
  1077. # list of binary strings representing parents of the new commit
  1078. [],
  1079. )
  1080. refname = "refs/heads/master:refs/heads/master"
  1081. ori_remote = clone_repo.remotes[0]
  1082. PagureRepo.push(ori_remote, refname)
  1083. # Tag our first commit
  1084. first_commit = repo.revparse_single("HEAD")
  1085. tagger = pygit2.Signature("Alice Doe", "adoe@example.com", 12347, 0)
  1086. repo.create_tag(
  1087. "0.0.1",
  1088. first_commit.oid.hex,
  1089. pygit2.GIT_OBJ_COMMIT,
  1090. tagger,
  1091. "Release 0.0.1",
  1092. )
  1093. # Create a token for foo for this project
  1094. item = pagure.lib.model.Token(
  1095. id="foobar_token",
  1096. user_id=1,
  1097. project_id=1,
  1098. expiration=datetime.datetime.utcnow()
  1099. + datetime.timedelta(days=30),
  1100. )
  1101. self.session.add(item)
  1102. self.session.commit()
  1103. item = pagure.lib.model.TokenAcl(token_id="foobar_token", acl_id=1)
  1104. self.session.add(item)
  1105. self.session.commit()
  1106. # Check if the admin requests
  1107. user = tests.FakeUser(username="pingou")
  1108. with tests.user_set(self.app.application, user):
  1109. # Check tags
  1110. output = self.app.get("/api/0/test4/git/tags")
  1111. self.assertEqual(output.status_code, 200)
  1112. data = json.loads(output.get_data(as_text=True))
  1113. self.assertDictEqual(data, {"tags": ["0.0.1"], "total_tags": 1})
  1114. output = self.app.get("/api/0/test4/git/tags")
  1115. self.assertEqual(output.status_code, 404)
  1116. # Chekc if user is not admin
  1117. user = tests.FakeUser()
  1118. with tests.user_set(self.app.application, user):
  1119. output = self.app.get("/api/0/test4/git/tags")
  1120. self.assertEqual(output.status_code, 404)
  1121. shutil.rmtree(newpath)
  1122. # Check before adding
  1123. repo = pagure.lib.query._get_project(self.session, "test4")
  1124. self.assertEqual(repo.tags, [])
  1125. # Adding a tag
  1126. output = pagure.lib.query.update_tags(
  1127. self.session, repo, "infra", "pingou"
  1128. )
  1129. self.assertEqual(output, ["Project tagged with: infra"])
  1130. # Check after adding
  1131. repo = pagure.lib.query._get_project(self.session, "test4")
  1132. self.assertEqual(len(repo.tags), 1)
  1133. self.assertEqual(repo.tags_text, ["infra"])
  1134. # Check the API
  1135. output = self.app.get("/api/0/projects?tags=inf")
  1136. self.assertEqual(output.status_code, 200)
  1137. data = json.loads(output.get_data(as_text=True))
  1138. del data["pagination"]
  1139. self.assertDictEqual(
  1140. data,
  1141. {
  1142. "args": {
  1143. "fork": None,
  1144. "namespace": None,
  1145. "owner": None,
  1146. "page": 1,
  1147. "pattern": None,
  1148. "per_page": 20,
  1149. "short": False,
  1150. "tags": ["inf"],
  1151. "username": None,
  1152. },
  1153. "projects": [],
  1154. "total_projects": 0,
  1155. },
  1156. )
  1157. # Request by not a loggged in user
  1158. output = self.app.get("/api/0/projects?tags=infra")
  1159. self.assertEqual(output.status_code, 200)
  1160. data = json.loads(output.get_data(as_text=True))
  1161. del data["pagination"]
  1162. self.assertDictEqual(
  1163. data,
  1164. {
  1165. "args": {
  1166. "fork": None,
  1167. "namespace": None,
  1168. "owner": None,
  1169. "page": 1,
  1170. "pattern": None,
  1171. "per_page": 20,
  1172. "short": False,
  1173. "tags": ["infra"],
  1174. "username": None,
  1175. },
  1176. "projects": [],
  1177. "total_projects": 0,
  1178. },
  1179. )
  1180. user = tests.FakeUser()
  1181. with tests.user_set(self.app.application, user):
  1182. # Request by a non authorized user
  1183. output = self.app.get("/api/0/projects?tags=infra")
  1184. self.assertEqual(output.status_code, 200)
  1185. data = json.loads(output.get_data(as_text=True))
  1186. del data["pagination"]
  1187. self.assertDictEqual(
  1188. data,
  1189. {
  1190. "args": {
  1191. "fork": None,
  1192. "namespace": None,
  1193. "owner": None,
  1194. "page": 1,
  1195. "pattern": None,
  1196. "per_page": 20,
  1197. "short": False,
  1198. "tags": ["infra"],
  1199. "username": None,
  1200. },
  1201. "projects": [],
  1202. "total_projects": 0,
  1203. },
  1204. )
  1205. user.username = "pingou"
  1206. with tests.user_set(self.app.application, user):
  1207. # Private repo username is compulsion to pass
  1208. output = self.app.get("/api/0/projects?tags=infra")
  1209. self.assertEqual(output.status_code, 200)
  1210. data = json.loads(output.get_data(as_text=True))
  1211. del data["pagination"]
  1212. self.assertDictEqual(
  1213. data,
  1214. {
  1215. "args": {
  1216. "fork": None,
  1217. "namespace": None,
  1218. "owner": None,
  1219. "page": 1,
  1220. "pattern": None,
  1221. "per_page": 20,
  1222. "short": False,
  1223. "tags": ["infra"],
  1224. "username": None,
  1225. },
  1226. "projects": [],
  1227. "total_projects": 0,
  1228. },
  1229. )
  1230. output = self.app.get("/api/0/projects?username=pingou")
  1231. self.assertEqual(output.status_code, 200)
  1232. data = json.loads(output.get_data(as_text=True))
  1233. data["projects"][0]["date_created"] = "1436527638"
  1234. data["projects"][0]["date_modified"] = "1436527638"
  1235. del data["pagination"]
  1236. self.assertDictEqual(
  1237. data,
  1238. {
  1239. "args": {
  1240. "fork": None,
  1241. "namespace": None,
  1242. "owner": None,
  1243. "page": 1,
  1244. "pattern": None,
  1245. "per_page": 20,
  1246. "short": False,
  1247. "tags": [],
  1248. "username": "pingou",
  1249. },
  1250. "total_projects": 1,
  1251. "projects": [
  1252. {
  1253. "access_groups": {
  1254. "admin": [],
  1255. "collaborator": [],
  1256. "commit": [],
  1257. "ticket": [],
  1258. },
  1259. "access_users": {
  1260. "admin": [],
  1261. "collaborator": [],
  1262. "commit": [],
  1263. "owner": ["pingou"],
  1264. "ticket": [],
  1265. },
  1266. "close_status": [],
  1267. "custom_keys": [],
  1268. "date_created": "1436527638",
  1269. "date_modified": "1436527638",
  1270. "description": "test project description",
  1271. "id": 1,
  1272. "milestones": {},
  1273. "name": "test4",
  1274. "fullname": "test4",
  1275. "url_path": "test4",
  1276. "namespace": None,
  1277. "parent": None,
  1278. "priorities": {},
  1279. "tags": ["infra"],
  1280. "user": {
  1281. "fullname": "PY C",
  1282. "name": "pingou",
  1283. "url_path": "user/pingou",
  1284. },
  1285. }
  1286. ],
  1287. },
  1288. )
  1289. output = self.app.get("/api/0/projects?username=pingou&tags=infra")
  1290. self.assertEqual(output.status_code, 200)
  1291. data = json.loads(output.get_data(as_text=True))
  1292. data["projects"][0]["date_created"] = "1436527638"
  1293. data["projects"][0]["date_modified"] = "1436527638"
  1294. del data["pagination"]
  1295. self.assertDictEqual(
  1296. data,
  1297. {
  1298. "args": {
  1299. "fork": None,
  1300. "namespace": None,
  1301. "owner": None,
  1302. "page": 1,
  1303. "pattern": None,
  1304. "per_page": 20,
  1305. "short": False,
  1306. "tags": ["infra"],
  1307. "username": "pingou",
  1308. },
  1309. "total_projects": 1,
  1310. "projects": [
  1311. {
  1312. "access_groups": {
  1313. "admin": [],
  1314. "collaborator": [],
  1315. "commit": [],
  1316. "ticket": [],
  1317. },
  1318. "access_users": {
  1319. "admin": [],
  1320. "collaborator": [],
  1321. "commit": [],
  1322. "owner": ["pingou"],
  1323. "ticket": [],
  1324. },
  1325. "close_status": [],
  1326. "custom_keys": [],
  1327. "date_created": "1436527638",
  1328. "date_modified": "1436527638",
  1329. "description": "test project description",
  1330. "id": 1,
  1331. "milestones": {},
  1332. "name": "test4",
  1333. "fullname": "test4",
  1334. "url_path": "test4",
  1335. "namespace": None,
  1336. "parent": None,
  1337. "priorities": {},
  1338. "tags": ["infra"],
  1339. "user": {
  1340. "fullname": "PY C",
  1341. "name": "pingou",
  1342. "url_path": "user/pingou",
  1343. },
  1344. }
  1345. ],
  1346. },
  1347. )
  1348. # Api pull-request views
  1349. @patch("pagure.lib.notify.send_email")
  1350. def test_api_private_repo_fork(self, send_email):
  1351. """ Test api endpoints in api/fork"""
  1352. send_email.return_value = True
  1353. # Add private repo
  1354. item = pagure.lib.model.Project(
  1355. user_id=1, # pingou
  1356. name="test4",
  1357. description="test project description",
  1358. hook_token="aaabbbeeeceee",
  1359. private=True,
  1360. )
  1361. self.session.add(item)
  1362. self.session.commit()
  1363. tests.create_tokens(self.session)
  1364. tests.create_tokens_acl(self.session)
  1365. headers = {"Authorization": "token aaabbbcccddd"}
  1366. # Create a pull-request
  1367. repo = pagure.lib.query._get_project(self.session, "test4")
  1368. forked_repo = pagure.lib.query._get_project(self.session, "test4")
  1369. req = pagure.lib.query.new_pull_request(
  1370. session=self.session,
  1371. repo_from=forked_repo,
  1372. branch_from="master",
  1373. repo_to=repo,
  1374. branch_to="master",
  1375. title="test pull-request",
  1376. user="pingou",
  1377. )
  1378. self.session.commit()
  1379. self.assertEqual(req.id, 1)
  1380. self.assertEqual(req.title, "test pull-request")
  1381. # Check list of PR
  1382. output = self.app.get("/api/0/test4/pull-requests")
  1383. self.assertEqual(output.status_code, 404)
  1384. # Check single PR
  1385. output = self.app.get("/api/0/test/pull-request/1")
  1386. self.assertEqual(output.status_code, 404)
  1387. user = tests.FakeUser(username="pingou")
  1388. with tests.user_set(self.app.application, user):
  1389. # List pull-requests
  1390. output = self.app.get("/api/0/test4/pull-requests")
  1391. self.assertEqual(output.status_code, 200)
  1392. data = json.loads(output.get_data(as_text=True))
  1393. data["requests"][0]["date_created"] = "1431414800"
  1394. data["requests"][0]["updated_on"] = "1431414800"
  1395. data["requests"][0]["project"]["date_created"] = "1431414800"
  1396. data["requests"][0]["project"]["date_modified"] = "1431414800"
  1397. data["requests"][0]["repo_from"]["date_created"] = "1431414800"
  1398. data["requests"][0]["repo_from"]["date_modified"] = "1431414800"
  1399. data["requests"][0]["uid"] = "1431414800"
  1400. data["requests"][0]["last_updated"] = "1431414800"
  1401. for k in ["first", "last"]:
  1402. self.assertIsNotNone(data["pagination"][k])
  1403. data["pagination"][k] = "http://localhost..."
  1404. self.assertDictEqual(
  1405. data,
  1406. {
  1407. "args": {
  1408. "assignee": None,
  1409. "author": None,
  1410. "tags": [],
  1411. "page": 1,
  1412. "per_page": 20,
  1413. "status": True,
  1414. },
  1415. "pagination": {
  1416. "first": "http://localhost...",
  1417. "last": "http://localhost...",
  1418. "next": None,
  1419. "page": 1,
  1420. "pages": 1,
  1421. "per_page": 20,
  1422. "prev": None,
  1423. },
  1424. "requests": [
  1425. {
  1426. "assignee": None,
  1427. "branch": "master",
  1428. "branch_from": "master",
  1429. "cached_merge_status": "unknown",
  1430. "closed_at": None,
  1431. "closed_by": None,
  1432. "comments": [],
  1433. "commit_start": None,
  1434. "commit_stop": None,
  1435. "date_created": "1431414800",
  1436. "last_updated": "1431414800",
  1437. "id": 1,
  1438. "initial_comment": None,
  1439. "project": {
  1440. "access_groups": {
  1441. "admin": [],
  1442. "collaborator": [],
  1443. "commit": [],
  1444. "ticket": [],
  1445. },
  1446. "access_users": {
  1447. "admin": [],
  1448. "collaborator": [],
  1449. "commit": [],
  1450. "owner": ["pingou"],
  1451. "ticket": [],
  1452. },
  1453. "close_status": [],
  1454. "custom_keys": [],
  1455. "date_created": "1431414800",
  1456. "date_modified": "1431414800",
  1457. "description": "test project description",
  1458. "id": 1,
  1459. "milestones": {},
  1460. "name": "test4",
  1461. "fullname": "test4",
  1462. "url_path": "test4",
  1463. "namespace": None,
  1464. "parent": None,
  1465. "priorities": {},
  1466. "tags": [],
  1467. "user": {
  1468. "fullname": "PY C",
  1469. "name": "pingou",
  1470. "url_path": "user/pingou",
  1471. },
  1472. },
  1473. "remote_git": None,
  1474. "repo_from": {
  1475. "access_groups": {
  1476. "admin": [],
  1477. "collaborator": [],
  1478. "commit": [],
  1479. "ticket": [],
  1480. },
  1481. "access_users": {
  1482. "admin": [],
  1483. "collaborator": [],
  1484. "commit": [],
  1485. "owner": ["pingou"],
  1486. "ticket": [],
  1487. },
  1488. "close_status": [],
  1489. "custom_keys": [],
  1490. "date_created": "1431414800",
  1491. "date_modified": "1431414800",
  1492. "description": "test project description",
  1493. "id": 1,
  1494. "milestones": {},
  1495. "fullname": "test4",
  1496. "url_path": "test4",
  1497. "name": "test4",
  1498. "namespace": None,
  1499. "parent": None,
  1500. "priorities": {},
  1501. "tags": [],
  1502. "user": {
  1503. "fullname": "PY C",
  1504. "name": "pingou",
  1505. "url_path": "user/pingou",
  1506. },
  1507. },
  1508. "status": "Open",
  1509. "tags": [],
  1510. "threshold_reached": None,
  1511. "title": "test pull-request",
  1512. "uid": "1431414800",
  1513. "updated_on": "1431414800",
  1514. "user": {
  1515. "fullname": "PY C",
  1516. "name": "pingou",
  1517. "url_path": "user/pingou",
  1518. },
  1519. }
  1520. ],
  1521. "total_requests": 1,
  1522. },
  1523. )
  1524. headers = {"Authorization": "token foobar_token"}
  1525. # Access Pull-Request authenticated
  1526. output = self.app.get(
  1527. "/api/0/test4/pull-requests", headers=headers
  1528. )
  1529. self.assertEqual(output.status_code, 200)
  1530. data2 = json.loads(output.get_data(as_text=True))
  1531. data2["requests"][0]["date_created"] = "1431414800"
  1532. data2["requests"][0]["updated_on"] = "1431414800"
  1533. data2["requests"][0]["project"]["date_created"] = "1431414800"
  1534. data2["requests"][0]["project"]["date_modified"] = "1431414800"
  1535. data2["requests"][0]["repo_from"]["date_created"] = "1431414800"
  1536. data2["requests"][0]["repo_from"]["date_modified"] = "1431414800"
  1537. data2["requests"][0]["uid"] = "1431414800"
  1538. data2["requests"][0]["last_updated"] = "1431414800"
  1539. for k in ["first", "last"]:
  1540. self.assertIsNotNone(data["pagination"][k])
  1541. data2["pagination"][k] = "http://localhost..."
  1542. self.assertDictEqual(data, data2)
  1543. # For single PR
  1544. output = self.app.get("/api/0/test4/pull-request/1")
  1545. self.assertEqual(output.status_code, 200)
  1546. data = json.loads(output.get_data(as_text=True))
  1547. data["date_created"] = "1431414800"
  1548. data["updated_on"] = "1431414800"
  1549. data["project"]["date_created"] = "1431414800"
  1550. data["project"]["date_modified"] = "1431414800"
  1551. data["repo_from"]["date_created"] = "1431414800"
  1552. data["repo_from"]["date_modified"] = "1431414800"
  1553. data["uid"] = "1431414800"
  1554. data["last_updated"] = "1431414800"
  1555. self.assertDictEqual(
  1556. data,
  1557. {
  1558. "assignee": None,
  1559. "branch": "master",
  1560. "branch_from": "master",
  1561. "cached_merge_status": "unknown",
  1562. "closed_at": None,
  1563. "closed_by": None,
  1564. "comments": [],
  1565. "commit_start": None,
  1566. "commit_stop": None,
  1567. "date_created": "1431414800",
  1568. "last_updated": "1431414800",
  1569. "id": 1,
  1570. "initial_comment": None,
  1571. "project": {
  1572. "access_groups": {
  1573. "admin": [],
  1574. "collaborator": [],
  1575. "commit": [],
  1576. "ticket": [],
  1577. },
  1578. "access_users": {
  1579. "admin": [],
  1580. "collaborator": [],
  1581. "commit": [],
  1582. "owner": ["pingou"],
  1583. "ticket": [],
  1584. },
  1585. "close_status": [],
  1586. "custom_keys": [],
  1587. "date_created": "1431414800",
  1588. "date_modified": "1431414800",
  1589. "description": "test project description",
  1590. "id": 1,
  1591. "milestones": {},
  1592. "name": "test4",
  1593. "fullname": "test4",
  1594. "url_path": "test4",
  1595. "namespace": None,
  1596. "parent": None,
  1597. "priorities": {},
  1598. "tags": [],
  1599. "user": {
  1600. "fullname": "PY C",
  1601. "name": "pingou",
  1602. "url_path": "user/pingou",
  1603. },
  1604. },
  1605. "remote_git": None,
  1606. "repo_from": {
  1607. "access_groups": {
  1608. "admin": [],
  1609. "collaborator": [],
  1610. "commit": [],
  1611. "ticket": [],
  1612. },
  1613. "access_users": {
  1614. "admin": [],
  1615. "collaborator": [],
  1616. "commit": [],
  1617. "owner": ["pingou"],
  1618. "ticket": [],
  1619. },
  1620. "close_status": [],
  1621. "custom_keys": [],
  1622. "date_created": "1431414800",
  1623. "date_modified": "1431414800",
  1624. "description": "test project description",
  1625. "id": 1,
  1626. "milestones": {},
  1627. "name": "test4",
  1628. "fullname": "test4",
  1629. "url_path": "test4",
  1630. "namespace": None,
  1631. "parent": None,
  1632. "priorities": {},
  1633. "tags": [],
  1634. "user": {
  1635. "fullname": "PY C",
  1636. "name": "pingou",
  1637. "url_path": "user/pingou",
  1638. },
  1639. },
  1640. "status": "Open",
  1641. "tags": [],
  1642. "threshold_reached": None,
  1643. "title": "test pull-request",
  1644. "uid": "1431414800",
  1645. "updated_on": "1431414800",
  1646. "user": {
  1647. "fullname": "PY C",
  1648. "name": "pingou",
  1649. "url_path": "user/pingou",
  1650. },
  1651. },
  1652. )
  1653. # Access Pull-Request authenticated
  1654. output = self.app.get(
  1655. "/api/0/test4/pull-request/1", headers=headers
  1656. )
  1657. self.assertEqual(output.status_code, 200)
  1658. data2 = json.loads(output.get_data(as_text=True))
  1659. data2["date_created"] = "1431414800"
  1660. data2["project"]["date_created"] = "1431414800"
  1661. data2["project"]["date_modified"] = "1431414800"
  1662. data2["repo_from"]["date_created"] = "1431414800"
  1663. data2["repo_from"]["date_modified"] = "1431414800"
  1664. data2["uid"] = "1431414800"
  1665. data2["date_created"] = "1431414800"
  1666. data2["updated_on"] = "1431414800"
  1667. data2["last_updated"] = "1431414800"
  1668. self.assertDictEqual(data, data2)
  1669. @patch("pagure.lib.notify.send_email")
  1670. def test_api_pr_private_repo_add_comment(self, mockemail):
  1671. """ Test the api_pull_request_add_comment method of the flask api. """
  1672. mockemail.return_value = True
  1673. pagure.config.config["REQUESTS_FOLDER"] = None
  1674. # Add private repo
  1675. item = pagure.lib.model.Project(
  1676. user_id=1, # pingou
  1677. name="test4",
  1678. description="test project description",
  1679. hook_token="aaabbbeeeceee",
  1680. private=True,
  1681. )
  1682. self.session.add(item)
  1683. self.session.commit()
  1684. tests.create_tokens(self.session)
  1685. tests.create_tokens_acl(self.session)
  1686. headers = {"Authorization": "token aaabbbcccddd"}
  1687. # Create a pull-request
  1688. repo = pagure.lib.query._get_project(self.session, "test4")
  1689. forked_repo = pagure.lib.query._get_project(self.session, "test4")
  1690. req = pagure.lib.query.new_pull_request(
  1691. session=self.session,
  1692. repo_from=forked_repo,
  1693. branch_from="master",
  1694. repo_to=repo,
  1695. branch_to="master",
  1696. title="test pull-request",
  1697. user="pingou",
  1698. )
  1699. self.session.commit()
  1700. self.assertEqual(req.id, 1)
  1701. self.assertEqual(req.title, "test pull-request")
  1702. # Check comments before
  1703. self.session.commit()
  1704. request = pagure.lib.query.search_pull_requests(
  1705. self.session, project_id=1, requestid=1
  1706. )
  1707. self.assertEqual(len(request.comments), 0)
  1708. data = {"title": "test issue"}
  1709. # Incomplete request
  1710. output = self.app.post(
  1711. "/api/0/test4/pull-request/1/comment", data=data, headers=headers
  1712. )
  1713. self.assertEqual(output.status_code, 400)
  1714. data = json.loads(output.get_data(as_text=True))
  1715. self.assertDictEqual(
  1716. data,
  1717. {
  1718. "error": "Invalid or incomplete input submitted",
  1719. "error_code": "EINVALIDREQ",
  1720. "errors": {"comment": ["This field is required."]},
  1721. },
  1722. )
  1723. # No change
  1724. self.session.commit()
  1725. request = pagure.lib.query.search_pull_requests(
  1726. self.session, project_id=1, requestid=1
  1727. )
  1728. self.assertEqual(len(request.comments), 0)
  1729. data = {"comment": "This is a very interesting question"}
  1730. # Valid request
  1731. output = self.app.post(
  1732. "/api/0/test4/pull-request/1/comment", data=data, headers=headers
  1733. )
  1734. self.assertEqual(output.status_code, 200)
  1735. data = json.loads(output.get_data(as_text=True))
  1736. self.assertDictEqual(data, {"message": "Comment added"})
  1737. # One comment added
  1738. self.session.commit()
  1739. request = pagure.lib.query.search_pull_requests(
  1740. self.session, project_id=1, requestid=1
  1741. )
  1742. self.assertEqual(len(request.comments), 1)
  1743. @patch("pagure.lib.notify.send_email")
  1744. def test_api_private_repo_pr_add_flag(self, mockemail):
  1745. """ Test the api_pull_request_add_flag method of the flask api. """
  1746. mockemail.return_value = True
  1747. pagure.config.config["REQUESTS_FOLDER"] = None
  1748. # Add private repo
  1749. item = pagure.lib.model.Project(
  1750. user_id=1, # pingou
  1751. name="test4",
  1752. description="test project description",
  1753. hook_token="aaabbbeeeceee",
  1754. private=True,
  1755. )
  1756. self.session.add(item)
  1757. self.session.commit()
  1758. # Add private repo
  1759. item = pagure.lib.model.Project(
  1760. user_id=1, # pingou
  1761. name="test2",
  1762. description="test project description",
  1763. hook_token="foo_bar",
  1764. private=True,
  1765. )
  1766. self.session.add(item)
  1767. self.session.commit()
  1768. tests.create_tokens(self.session)
  1769. tests.create_tokens_acl(self.session)
  1770. headers = {"Authorization": "token aaabbbcccddd"}
  1771. # Invalid project
  1772. output = self.app.post(
  1773. "/api/0/foo/pull-request/1/flag", headers=headers
  1774. )
  1775. self.assertEqual(output.status_code, 404)
  1776. data = json.loads(output.get_data(as_text=True))
  1777. self.assertDictEqual(
  1778. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  1779. )
  1780. # Valid token, wrong project
  1781. output = self.app.post(
  1782. "/api/0/test2/pull-request/1/flag", headers=headers
  1783. )
  1784. self.assertEqual(output.status_code, 401)
  1785. data = json.loads(output.get_data(as_text=True))
  1786. self.assertEqual(sorted(data.keys()), ["error", "error_code"])
  1787. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  1788. self.assertEqual(
  1789. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  1790. )
  1791. # No input
  1792. output = self.app.post(
  1793. "/api/0/test4/pull-request/1/flag", headers=headers
  1794. )
  1795. self.assertEqual(output.status_code, 404)
  1796. data = json.loads(output.get_data(as_text=True))
  1797. self.assertDictEqual(
  1798. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  1799. )
  1800. # Create a pull-request
  1801. repo = pagure.lib.query._get_project(self.session, "test4")
  1802. forked_repo = pagure.lib.query._get_project(self.session, "test4")
  1803. req = pagure.lib.query.new_pull_request(
  1804. session=self.session,
  1805. repo_from=forked_repo,
  1806. branch_from="master",
  1807. repo_to=repo,
  1808. branch_to="master",
  1809. title="test pull-request",
  1810. user="pingou",
  1811. )
  1812. self.session.commit()
  1813. self.assertEqual(req.id, 1)
  1814. self.assertEqual(req.title, "test pull-request")
  1815. # Check comments before
  1816. self.session.commit()
  1817. request = pagure.lib.query.search_pull_requests(
  1818. self.session, project_id=1, requestid=1
  1819. )
  1820. self.assertEqual(len(request.flags), 0)
  1821. data = {
  1822. "username": "Jenkins",
  1823. "percent": 100,
  1824. "url": "http://jenkins.cloud.fedoraproject.org/",
  1825. "uid": "jenkins_build_pagure_100+seed",
  1826. }
  1827. # Incomplete request
  1828. output = self.app.post(
  1829. "/api/0/test4/pull-request/1/flag", data=data, headers=headers
  1830. )
  1831. self.assertEqual(output.status_code, 400)
  1832. data = json.loads(output.get_data(as_text=True))
  1833. self.assertDictEqual(
  1834. data,
  1835. {
  1836. "error": "Invalid or incomplete input submitted",
  1837. "error_code": "EINVALIDREQ",
  1838. "errors": {"comment": ["This field is required."]},
  1839. },
  1840. )
  1841. # No change
  1842. self.session.commit()
  1843. request = pagure.lib.query.search_pull_requests(
  1844. self.session, project_id=1, requestid=1
  1845. )
  1846. self.assertEqual(len(request.flags), 0)
  1847. data = {
  1848. "username": "Jenkins",
  1849. "percent": 0,
  1850. "comment": "Tests failed",
  1851. "url": "http://jenkins.cloud.fedoraproject.org/",
  1852. "uid": "jenkins_build_pagure_100+seed",
  1853. }
  1854. # Valid request
  1855. output = self.app.post(
  1856. "/api/0/test4/pull-request/1/flag", data=data, headers=headers
  1857. )
  1858. self.assertEqual(output.status_code, 200)
  1859. data = json.loads(output.get_data(as_text=True))
  1860. data["flag"]["date_created"] = "1510742565"
  1861. data["flag"]["date_updated"] = "1510742565"
  1862. data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
  1863. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  1864. self.assertDictEqual(
  1865. data,
  1866. {
  1867. "flag": {
  1868. "comment": "Tests failed",
  1869. "date_created": "1510742565",
  1870. "date_updated": "1510742565",
  1871. "percent": 0,
  1872. "pull_request_uid": "62b49f00d489452994de5010565fab81",
  1873. "status": "failure",
  1874. "url": "http://jenkins.cloud.fedoraproject.org/",
  1875. "user": {
  1876. "default_email": "bar@pingou.com",
  1877. "emails": ["bar@pingou.com", "foo@pingou.com"],
  1878. "fullname": "PY C",
  1879. "name": "pingou",
  1880. "url_path": "user/pingou",
  1881. },
  1882. "username": "Jenkins",
  1883. },
  1884. "message": "Flag added",
  1885. "uid": "jenkins_build_pagure_100+seed",
  1886. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  1887. "user": "pingou",
  1888. },
  1889. )
  1890. # One flag added
  1891. self.session.commit()
  1892. request = pagure.lib.query.search_pull_requests(
  1893. self.session, project_id=1, requestid=1
  1894. )
  1895. self.assertEqual(len(request.flags), 1)
  1896. self.assertEqual(request.flags[0].comment, "Tests failed")
  1897. self.assertEqual(request.flags[0].percent, 0)
  1898. # Update flag
  1899. data = {
  1900. "username": "Jenkins",
  1901. "percent": 100,
  1902. "comment": "Tests passed",
  1903. "url": "http://jenkins.cloud.fedoraproject.org/",
  1904. "uid": "jenkins_build_pagure_100+seed",
  1905. }
  1906. output = self.app.post(
  1907. "/api/0/test4/pull-request/1/flag", data=data, headers=headers
  1908. )
  1909. self.assertEqual(output.status_code, 200)
  1910. data = json.loads(output.get_data(as_text=True))
  1911. data["flag"]["date_created"] = "1510742565"
  1912. data["flag"]["date_updated"] = "1510742565"
  1913. data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
  1914. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  1915. self.assertDictEqual(
  1916. data,
  1917. {
  1918. "flag": {
  1919. "comment": "Tests passed",
  1920. "date_created": "1510742565",
  1921. "date_updated": "1510742565",
  1922. "percent": 100,
  1923. "pull_request_uid": "62b49f00d489452994de5010565fab81",
  1924. "status": "success",
  1925. "url": "http://jenkins.cloud.fedoraproject.org/",
  1926. "user": {
  1927. "default_email": "bar@pingou.com",
  1928. "emails": ["bar@pingou.com", "foo@pingou.com"],
  1929. "fullname": "PY C",
  1930. "name": "pingou",
  1931. "url_path": "user/pingou",
  1932. },
  1933. "username": "Jenkins",
  1934. },
  1935. "message": "Flag updated",
  1936. "uid": "jenkins_build_pagure_100+seed",
  1937. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  1938. "user": "pingou",
  1939. },
  1940. )
  1941. # One flag added
  1942. self.session.commit()
  1943. request = pagure.lib.query.search_pull_requests(
  1944. self.session, project_id=1, requestid=1
  1945. )
  1946. self.assertEqual(len(request.flags), 1)
  1947. self.assertEqual(request.flags[0].comment, "Tests passed")
  1948. self.assertEqual(request.flags[0].percent, 100)
  1949. @patch("pagure.lib.notify.send_email")
  1950. def test_api_private_repo_pr_close(self, send_email):
  1951. """ Test the api_pull_request_close method of the flask api. """
  1952. send_email.return_value = True
  1953. pagure.config.config["REQUESTS_FOLDER"] = None
  1954. # Add private repo
  1955. item = pagure.lib.model.Project(
  1956. user_id=1, # pingou
  1957. name="test4",
  1958. description="test project description",
  1959. hook_token="aaabbbeeeceee",
  1960. private=True,
  1961. )
  1962. self.session.add(item)
  1963. self.session.commit()
  1964. tests.create_tokens(self.session)
  1965. tests.create_tokens_acl(self.session)
  1966. # Add private repo
  1967. item = pagure.lib.model.Project(
  1968. user_id=1, # pingou
  1969. name="test2",
  1970. description="test project description",
  1971. hook_token="foo_bar",
  1972. private=True,
  1973. )
  1974. self.session.add(item)
  1975. self.session.commit()
  1976. # Create the pull-request to close
  1977. repo = pagure.lib.query._get_project(self.session, "test4")
  1978. forked_repo = pagure.lib.query._get_project(self.session, "test4")
  1979. req = pagure.lib.query.new_pull_request(
  1980. session=self.session,
  1981. repo_from=forked_repo,
  1982. branch_from="master",
  1983. repo_to=repo,
  1984. branch_to="master",
  1985. title="test pull-request",
  1986. user="pingou",
  1987. )
  1988. self.session.commit()
  1989. self.assertEqual(req.id, 1)
  1990. self.assertEqual(req.title, "test pull-request")
  1991. headers = {"Authorization": "token aaabbbcccddd"}
  1992. # Invalid project
  1993. output = self.app.post(
  1994. "/api/0/foo/pull-request/1/close", headers=headers
  1995. )
  1996. self.assertEqual(output.status_code, 404)
  1997. data = json.loads(output.get_data(as_text=True))
  1998. self.assertDictEqual(
  1999. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2000. )
  2001. # Valid token, wrong project
  2002. output = self.app.post(
  2003. "/api/0/test2/pull-request/1/close", headers=headers
  2004. )
  2005. self.assertEqual(output.status_code, 401)
  2006. data = json.loads(output.get_data(as_text=True))
  2007. self.assertEqual(sorted(data.keys()), ["error", "error_code"])
  2008. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  2009. self.assertEqual(
  2010. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  2011. )
  2012. # Invalid PR
  2013. output = self.app.post(
  2014. "/api/0/test4/pull-request/2/close", headers=headers
  2015. )
  2016. self.assertEqual(output.status_code, 404)
  2017. data = json.loads(output.get_data(as_text=True))
  2018. self.assertDictEqual(
  2019. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  2020. )
  2021. # Create a token for foo for this project
  2022. item = pagure.lib.model.Token(
  2023. id="foobar_token",
  2024. user_id=2,
  2025. project_id=1,
  2026. expiration=datetime.datetime.utcnow()
  2027. + datetime.timedelta(days=30),
  2028. )
  2029. self.session.add(item)
  2030. self.session.commit()
  2031. # Allow the token to close PR
  2032. acls = pagure.lib.query.get_acls(self.session)
  2033. acl = None
  2034. for acl in acls:
  2035. if acl.name == "pull_request_close":
  2036. break
  2037. item = pagure.lib.model.TokenAcl(
  2038. token_id="foobar_token", acl_id=acl.id
  2039. )
  2040. self.session.add(item)
  2041. self.session.commit()
  2042. headers = {"Authorization": "token foobar_token"}
  2043. # User not admin
  2044. output = self.app.post(
  2045. "/api/0/test4/pull-request/1/close", headers=headers
  2046. )
  2047. self.assertEqual(output.status_code, 404)
  2048. data = json.loads(output.get_data(as_text=True))
  2049. self.assertDictEqual(
  2050. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2051. )
  2052. headers = {"Authorization": "token aaabbbcccddd"}
  2053. # Close PR
  2054. output = self.app.post(
  2055. "/api/0/test4/pull-request/1/close", headers=headers
  2056. )
  2057. self.assertEqual(output.status_code, 200)
  2058. data = json.loads(output.get_data(as_text=True))
  2059. self.assertDictEqual(data, {"message": "Pull-request closed!"})
  2060. @patch("pagure.lib.notify.send_email")
  2061. def test_api_private_repo_pr_merge(self, send_email):
  2062. """ Test the api_pull_request_merge method of the flask api. """
  2063. send_email.return_value = True
  2064. pagure.config.config["REQUESTS_FOLDER"] = None
  2065. # Add private repo
  2066. item = pagure.lib.model.Project(
  2067. user_id=1, # pingou
  2068. name="test4",
  2069. description="test project description",
  2070. hook_token="aaabbbeeeceee",
  2071. private=True,
  2072. )
  2073. self.session.add(item)
  2074. self.session.commit()
  2075. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2076. tests.create_projects_git(
  2077. os.path.join(self.path, "requests"), bare=True
  2078. )
  2079. tests.add_readme_git_repo(
  2080. os.path.join(self.path, "repos", "test4.git")
  2081. )
  2082. tests.add_commit_git_repo(
  2083. os.path.join(self.path, "repos", "test4.git"), branch="test"
  2084. )
  2085. tests.create_tokens(self.session)
  2086. tests.create_tokens_acl(self.session)
  2087. # Add private repo
  2088. item = pagure.lib.model.Project(
  2089. user_id=1, # pingou
  2090. name="test2",
  2091. description="test project description",
  2092. hook_token="foo_bar",
  2093. private=True,
  2094. )
  2095. self.session.add(item)
  2096. self.session.commit()
  2097. # Create the pull-request to close
  2098. repo = pagure.lib.query._get_project(self.session, "test4")
  2099. forked_repo = pagure.lib.query._get_project(self.session, "test4")
  2100. req = pagure.lib.query.new_pull_request(
  2101. session=self.session,
  2102. repo_from=forked_repo,
  2103. branch_from="test",
  2104. repo_to=repo,
  2105. branch_to="master",
  2106. title="test pull-request",
  2107. user="pingou",
  2108. )
  2109. self.session.commit()
  2110. self.assertEqual(req.id, 1)
  2111. self.assertEqual(req.title, "test pull-request")
  2112. headers = {"Authorization": "token aaabbbcccddd"}
  2113. # Invalid project
  2114. output = self.app.post(
  2115. "/api/0/foo/pull-request/1/merge", headers=headers
  2116. )
  2117. self.assertEqual(output.status_code, 404)
  2118. data = json.loads(output.get_data(as_text=True))
  2119. self.assertDictEqual(
  2120. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2121. )
  2122. # Valid token, wrong project
  2123. output = self.app.post(
  2124. "/api/0/test2/pull-request/1/merge", headers=headers
  2125. )
  2126. self.assertEqual(output.status_code, 401)
  2127. data = json.loads(output.get_data(as_text=True))
  2128. self.assertEqual(sorted(data.keys()), ["error", "error_code"])
  2129. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  2130. self.assertEqual(
  2131. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  2132. )
  2133. # Invalid PR
  2134. output = self.app.post(
  2135. "/api/0/test4/pull-request/2/merge", headers=headers
  2136. )
  2137. self.assertEqual(output.status_code, 404)
  2138. data = json.loads(output.get_data(as_text=True))
  2139. self.assertDictEqual(
  2140. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  2141. )
  2142. # Create a token for foo for this project
  2143. item = pagure.lib.model.Token(
  2144. id="foobar_token",
  2145. user_id=2,
  2146. project_id=1,
  2147. expiration=datetime.datetime.utcnow()
  2148. + datetime.timedelta(days=30),
  2149. )
  2150. self.session.add(item)
  2151. self.session.commit()
  2152. # Allow the token to merge PR
  2153. acls = pagure.lib.query.get_acls(self.session)
  2154. acl = None
  2155. for acl in acls:
  2156. if acl.name == "pull_request_merge":
  2157. break
  2158. item = pagure.lib.model.TokenAcl(
  2159. token_id="foobar_token", acl_id=acl.id
  2160. )
  2161. self.session.add(item)
  2162. self.session.commit()
  2163. headers = {"Authorization": "token foobar_token"}
  2164. # User not admin
  2165. output = self.app.post(
  2166. "/api/0/test4/pull-request/1/merge", headers=headers
  2167. )
  2168. self.assertEqual(output.status_code, 404)
  2169. data = json.loads(output.get_data(as_text=True))
  2170. self.assertDictEqual(
  2171. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2172. )
  2173. headers = {"Authorization": "token aaabbbcccddd"}
  2174. # Merge PR
  2175. output = self.app.post(
  2176. "/api/0/test4/pull-request/1/merge", headers=headers
  2177. )
  2178. self.assertEqual(output.status_code, 200)
  2179. data = json.loads(output.get_data(as_text=True))
  2180. self.assertDictEqual(data, {"message": "Changes merged!"})
  2181. def test_api_private_repo_new_issue(self):
  2182. """ Test the api_new_issue method of the flask api. """
  2183. # Add private repo
  2184. item = pagure.lib.model.Project(
  2185. user_id=1, # pingou
  2186. name="test4",
  2187. description="test project description",
  2188. hook_token="aaabbbeeeceee",
  2189. private=True,
  2190. )
  2191. self.session.add(item)
  2192. self.session.commit()
  2193. for repo in ["GIT_FOLDER", "TICKETS_FOLDER"]:
  2194. # Add a git repo
  2195. repo_path = os.path.join(
  2196. pagure.config.config.get(repo), "test4.git"
  2197. )
  2198. if not os.path.exists(repo_path):
  2199. os.makedirs(repo_path)
  2200. pygit2.init_repository(repo_path, bare=True)
  2201. tests.create_tokens(self.session)
  2202. tests.create_tokens_acl(self.session)
  2203. # Add private repo
  2204. item = pagure.lib.model.Project(
  2205. user_id=1, # pingou
  2206. name="test2",
  2207. description="test project description",
  2208. hook_token="foo_bar",
  2209. private=True,
  2210. )
  2211. self.session.add(item)
  2212. self.session.commit()
  2213. headers = {"Authorization": "token aaabbbcccddd"}
  2214. # Valid token, wrong project
  2215. output = self.app.post("/api/0/test2/new_issue", headers=headers)
  2216. self.assertEqual(output.status_code, 401)
  2217. data = json.loads(output.get_data(as_text=True))
  2218. self.assertEqual(sorted(data.keys()), ["error", "error_code"])
  2219. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  2220. self.assertEqual(
  2221. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  2222. )
  2223. # No input
  2224. output = self.app.post("/api/0/test4/new_issue", headers=headers)
  2225. self.assertEqual(output.status_code, 400)
  2226. data = json.loads(output.get_data(as_text=True))
  2227. self.assertDictEqual(
  2228. data,
  2229. {
  2230. "error": "Invalid or incomplete input submitted",
  2231. "error_code": "EINVALIDREQ",
  2232. "errors": {
  2233. "issue_content": ["This field is required."],
  2234. "title": ["This field is required."],
  2235. },
  2236. },
  2237. )
  2238. data = {"title": "test issue"}
  2239. # Invalid repo
  2240. output = self.app.post(
  2241. "/api/0/foo/new_issue", data=data, headers=headers
  2242. )
  2243. self.assertEqual(output.status_code, 404)
  2244. data = json.loads(output.get_data(as_text=True))
  2245. self.assertDictEqual(
  2246. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2247. )
  2248. # Incomplete request
  2249. output = self.app.post(
  2250. "/api/0/test4/new_issue", data=data, headers=headers
  2251. )
  2252. self.assertEqual(output.status_code, 400)
  2253. data = json.loads(output.get_data(as_text=True))
  2254. self.assertDictEqual(
  2255. data,
  2256. {
  2257. "error": "Invalid or incomplete input submitted",
  2258. "error_code": "EINVALIDREQ",
  2259. "errors": {
  2260. "issue_content": ["This field is required."],
  2261. "title": ["This field is required."],
  2262. },
  2263. },
  2264. )
  2265. data = {
  2266. "title": "test issue",
  2267. "issue_content": "This issue needs attention",
  2268. }
  2269. # Valid request
  2270. output = self.app.post(
  2271. "/api/0/test4/new_issue", data=data, headers=headers
  2272. )
  2273. self.assertEqual(output.status_code, 200)
  2274. data = json.loads(output.get_data(as_text=True))
  2275. data["issue"]["date_created"] = "1431414800"
  2276. data["issue"]["last_updated"] = "1431414800"
  2277. self.assertDictEqual(
  2278. data, {"issue": FULL_ISSUE_LIST[7], "message": "Issue created"}
  2279. )
  2280. def test_api_private_repo_view_issues(self):
  2281. """ Test the api_view_issues method of the flask api. """
  2282. self.test_api_private_repo_new_issue()
  2283. # Invalid repo
  2284. output = self.app.get("/api/0/foo/issues")
  2285. self.assertEqual(output.status_code, 404)
  2286. data = json.loads(output.get_data(as_text=True))
  2287. self.assertDictEqual(
  2288. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2289. )
  2290. # List all opened issues
  2291. user = tests.FakeUser(username="pingou")
  2292. with tests.user_set(self.app.application, user):
  2293. output = self.app.get("/api/0/test4/issues")
  2294. self.assertEqual(output.status_code, 200)
  2295. data = json.loads(output.get_data(as_text=True))
  2296. data["issues"][0]["date_created"] = "1431414800"
  2297. data["issues"][0]["last_updated"] = "1431414800"
  2298. for k in ["first", "last"]:
  2299. self.assertIsNotNone(data["pagination"][k])
  2300. data["pagination"][k] = "http://localhost..."
  2301. self.assertDictEqual(
  2302. data,
  2303. {
  2304. "args": {
  2305. "assignee": None,
  2306. "author": None,
  2307. "milestones": [],
  2308. "no_stones": None,
  2309. "order": None,
  2310. "priority": None,
  2311. "since": None,
  2312. "status": None,
  2313. "tags": [],
  2314. },
  2315. "total_issues": 1,
  2316. "issues": [
  2317. {
  2318. "assignee": None,
  2319. "blocks": [],
  2320. "close_status": None,
  2321. "closed_at": None,
  2322. "closed_by": None,
  2323. "comments": [],
  2324. "content": "This issue needs attention",
  2325. "custom_fields": [],
  2326. "date_created": "1431414800",
  2327. "last_updated": "1431414800",
  2328. "depends": [],
  2329. "id": 1,
  2330. "milestone": None,
  2331. "priority": None,
  2332. "private": False,
  2333. "related_prs": [],
  2334. "status": "Open",
  2335. "tags": [],
  2336. "title": "test issue",
  2337. "user": {
  2338. "fullname": "PY C",
  2339. "name": "pingou",
  2340. "url_path": "user/pingou",
  2341. },
  2342. }
  2343. ],
  2344. "pagination": {
  2345. "first": "http://localhost...",
  2346. "last": "http://localhost...",
  2347. "next": None,
  2348. "page": 1,
  2349. "pages": 1,
  2350. "per_page": 20,
  2351. "prev": None,
  2352. },
  2353. },
  2354. )
  2355. # Create private issue
  2356. repo = pagure.lib.query._get_project(self.session, "test4")
  2357. msg = pagure.lib.query.new_issue(
  2358. session=self.session,
  2359. repo=repo,
  2360. title="Test issue",
  2361. content="We should work on this",
  2362. user="pingou",
  2363. private=True,
  2364. )
  2365. self.session.commit()
  2366. self.assertEqual(msg.title, "Test issue")
  2367. # Private issues are retrieved
  2368. user = tests.FakeUser(username="pingou")
  2369. with tests.user_set(self.app.application, user):
  2370. output = self.app.get("/api/0/test4/issues")
  2371. self.assertEqual(output.status_code, 200)
  2372. data = json.loads(output.get_data(as_text=True))
  2373. data["issues"][0]["date_created"] = "1431414800"
  2374. data["issues"][0]["last_updated"] = "1431414800"
  2375. data["issues"][1]["date_created"] = "1431414800"
  2376. data["issues"][1]["last_updated"] = "1431414800"
  2377. for k in ["first", "last"]:
  2378. self.assertIsNotNone(data["pagination"][k])
  2379. data["pagination"][k] = "http://localhost..."
  2380. self.assertDictEqual(
  2381. data,
  2382. {
  2383. "args": {
  2384. "assignee": None,
  2385. "author": None,
  2386. "milestones": [],
  2387. "no_stones": None,
  2388. "order": None,
  2389. "priority": None,
  2390. "status": None,
  2391. "since": None,
  2392. "tags": [],
  2393. },
  2394. "issues": [
  2395. {
  2396. "assignee": None,
  2397. "blocks": [],
  2398. "close_status": None,
  2399. "closed_at": None,
  2400. "closed_by": None,
  2401. "comments": [],
  2402. "content": "We should work on this",
  2403. "custom_fields": [],
  2404. "date_created": "1431414800",
  2405. "last_updated": "1431414800",
  2406. "depends": [],
  2407. "id": 2,
  2408. "milestone": None,
  2409. "priority": None,
  2410. "private": True,
  2411. "related_prs": [],
  2412. "status": "Open",
  2413. "tags": [],
  2414. "title": "Test issue",
  2415. "user": {
  2416. "fullname": "PY C",
  2417. "name": "pingou",
  2418. "url_path": "user/pingou",
  2419. },
  2420. },
  2421. {
  2422. "assignee": None,
  2423. "blocks": [],
  2424. "close_status": None,
  2425. "closed_at": None,
  2426. "closed_by": None,
  2427. "comments": [],
  2428. "content": "This issue needs attention",
  2429. "custom_fields": [],
  2430. "date_created": "1431414800",
  2431. "last_updated": "1431414800",
  2432. "depends": [],
  2433. "id": 1,
  2434. "milestone": None,
  2435. "priority": None,
  2436. "private": False,
  2437. "related_prs": [],
  2438. "status": "Open",
  2439. "tags": [],
  2440. "title": "test issue",
  2441. "user": {
  2442. "fullname": "PY C",
  2443. "name": "pingou",
  2444. "url_path": "user/pingou",
  2445. },
  2446. },
  2447. ],
  2448. "pagination": {
  2449. "first": "http://localhost...",
  2450. "last": "http://localhost...",
  2451. "next": None,
  2452. "page": 1,
  2453. "pages": 1,
  2454. "per_page": 20,
  2455. "prev": None,
  2456. },
  2457. "total_issues": 2,
  2458. },
  2459. )
  2460. # Access issues authenticated but non-existing token
  2461. headers = {"Authorization": "token aaabbbccc"}
  2462. output = self.app.get("/api/0/test4/issues", headers=headers)
  2463. self.assertEqual(output.status_code, 401)
  2464. headers = {"Authorization": "token aaabbbcccddd"}
  2465. # Access issues authenticated correctly
  2466. output = self.app.get("/api/0/test4/issues", headers=headers)
  2467. self.assertEqual(output.status_code, 200)
  2468. data = json.loads(output.get_data(as_text=True))
  2469. data["issues"][0]["date_created"] = "1431414800"
  2470. data["issues"][0]["last_updated"] = "1431414800"
  2471. data["issues"][1]["date_created"] = "1431414800"
  2472. data["issues"][1]["last_updated"] = "1431414800"
  2473. for k in ["first", "last"]:
  2474. self.assertIsNotNone(data["pagination"][k])
  2475. data["pagination"][k] = "http://localhost..."
  2476. self.assertDictEqual(
  2477. data,
  2478. {
  2479. "args": {
  2480. "assignee": None,
  2481. "author": None,
  2482. "milestones": [],
  2483. "no_stones": None,
  2484. "order": None,
  2485. "priority": None,
  2486. "status": None,
  2487. "since": None,
  2488. "tags": [],
  2489. },
  2490. "issues": [
  2491. {
  2492. "assignee": None,
  2493. "blocks": [],
  2494. "close_status": None,
  2495. "closed_at": None,
  2496. "closed_by": None,
  2497. "comments": [],
  2498. "content": "We should work on this",
  2499. "custom_fields": [],
  2500. "date_created": "1431414800",
  2501. "last_updated": "1431414800",
  2502. "depends": [],
  2503. "id": 2,
  2504. "milestone": None,
  2505. "priority": None,
  2506. "private": True,
  2507. "related_prs": [],
  2508. "status": "Open",
  2509. "tags": [],
  2510. "title": "Test issue",
  2511. "user": {
  2512. "fullname": "PY C",
  2513. "name": "pingou",
  2514. "url_path": "user/pingou",
  2515. },
  2516. },
  2517. {
  2518. "assignee": None,
  2519. "blocks": [],
  2520. "close_status": None,
  2521. "closed_at": None,
  2522. "closed_by": None,
  2523. "comments": [],
  2524. "content": "This issue needs attention",
  2525. "custom_fields": [],
  2526. "date_created": "1431414800",
  2527. "last_updated": "1431414800",
  2528. "depends": [],
  2529. "id": 1,
  2530. "milestone": None,
  2531. "priority": None,
  2532. "private": False,
  2533. "related_prs": [],
  2534. "status": "Open",
  2535. "tags": [],
  2536. "title": "test issue",
  2537. "user": {
  2538. "fullname": "PY C",
  2539. "name": "pingou",
  2540. "url_path": "user/pingou",
  2541. },
  2542. },
  2543. ],
  2544. "pagination": {
  2545. "first": "http://localhost...",
  2546. "last": "http://localhost...",
  2547. "next": None,
  2548. "page": 1,
  2549. "pages": 1,
  2550. "per_page": 20,
  2551. "prev": None,
  2552. },
  2553. "total_issues": 2,
  2554. },
  2555. )
  2556. # List closed issue
  2557. output = self.app.get(
  2558. "/api/0/test4/issues?status=Closed", headers=headers
  2559. )
  2560. self.assertEqual(output.status_code, 200)
  2561. data = json.loads(output.get_data(as_text=True))
  2562. for k in ["first", "last"]:
  2563. self.assertIsNotNone(data["pagination"][k])
  2564. data["pagination"][k] = "http://localhost..."
  2565. self.assertDictEqual(
  2566. data,
  2567. {
  2568. "args": {
  2569. "assignee": None,
  2570. "author": None,
  2571. "milestones": [],
  2572. "no_stones": None,
  2573. "order": None,
  2574. "priority": None,
  2575. "status": "Closed",
  2576. "since": None,
  2577. "tags": [],
  2578. },
  2579. "issues": [],
  2580. "pagination": {
  2581. "first": "http://localhost...",
  2582. "last": "http://localhost...",
  2583. "next": None,
  2584. "page": 1,
  2585. "pages": 0,
  2586. "per_page": 20,
  2587. "prev": None,
  2588. },
  2589. "total_issues": 0,
  2590. },
  2591. )
  2592. # List closed issue
  2593. output = self.app.get(
  2594. "/api/0/test4/issues?status=Invalid", headers=headers
  2595. )
  2596. self.assertEqual(output.status_code, 200)
  2597. data = json.loads(output.get_data(as_text=True))
  2598. for k in ["first", "last"]:
  2599. self.assertIsNotNone(data["pagination"][k])
  2600. data["pagination"][k] = "http://localhost..."
  2601. self.assertDictEqual(
  2602. data,
  2603. {
  2604. "args": {
  2605. "assignee": None,
  2606. "author": None,
  2607. "milestones": [],
  2608. "no_stones": None,
  2609. "order": None,
  2610. "priority": None,
  2611. "status": "Invalid",
  2612. "since": None,
  2613. "tags": [],
  2614. },
  2615. "issues": [],
  2616. "pagination": {
  2617. "first": "http://localhost...",
  2618. "last": "http://localhost...",
  2619. "next": None,
  2620. "page": 1,
  2621. "pages": 0,
  2622. "per_page": 20,
  2623. "prev": None,
  2624. },
  2625. "total_issues": 0,
  2626. },
  2627. )
  2628. # List all issues
  2629. output = self.app.get(
  2630. "/api/0/test4/issues?status=All", headers=headers
  2631. )
  2632. self.assertEqual(output.status_code, 200)
  2633. data = json.loads(output.get_data(as_text=True))
  2634. data["issues"][0]["date_created"] = "1431414800"
  2635. data["issues"][0]["last_updated"] = "1431414800"
  2636. data["issues"][1]["date_created"] = "1431414800"
  2637. data["issues"][1]["last_updated"] = "1431414800"
  2638. for k in ["first", "last"]:
  2639. self.assertIsNotNone(data["pagination"][k])
  2640. data["pagination"][k] = "http://localhost..."
  2641. self.assertDictEqual(
  2642. data,
  2643. {
  2644. "args": {
  2645. "assignee": None,
  2646. "author": None,
  2647. "milestones": [],
  2648. "no_stones": None,
  2649. "order": None,
  2650. "priority": None,
  2651. "since": None,
  2652. "status": "All",
  2653. "tags": [],
  2654. },
  2655. "issues": [
  2656. {
  2657. "assignee": None,
  2658. "blocks": [],
  2659. "close_status": None,
  2660. "closed_at": None,
  2661. "closed_by": None,
  2662. "comments": [],
  2663. "content": "We should work on this",
  2664. "custom_fields": [],
  2665. "date_created": "1431414800",
  2666. "last_updated": "1431414800",
  2667. "depends": [],
  2668. "id": 2,
  2669. "milestone": None,
  2670. "priority": None,
  2671. "private": True,
  2672. "related_prs": [],
  2673. "status": "Open",
  2674. "tags": [],
  2675. "title": "Test issue",
  2676. "user": {
  2677. "fullname": "PY C",
  2678. "name": "pingou",
  2679. "url_path": "user/pingou",
  2680. },
  2681. },
  2682. {
  2683. "assignee": None,
  2684. "blocks": [],
  2685. "close_status": None,
  2686. "closed_at": None,
  2687. "closed_by": None,
  2688. "comments": [],
  2689. "content": "This issue needs attention",
  2690. "custom_fields": [],
  2691. "date_created": "1431414800",
  2692. "last_updated": "1431414800",
  2693. "depends": [],
  2694. "id": 1,
  2695. "milestone": None,
  2696. "priority": None,
  2697. "private": False,
  2698. "related_prs": [],
  2699. "status": "Open",
  2700. "tags": [],
  2701. "title": "test issue",
  2702. "user": {
  2703. "fullname": "PY C",
  2704. "name": "pingou",
  2705. "url_path": "user/pingou",
  2706. },
  2707. },
  2708. ],
  2709. "pagination": {
  2710. "first": "http://localhost...",
  2711. "last": "http://localhost...",
  2712. "next": None,
  2713. "page": 1,
  2714. "pages": 1,
  2715. "per_page": 20,
  2716. "prev": None,
  2717. },
  2718. "total_issues": 2,
  2719. },
  2720. )
  2721. def test_api_pivate_repo_view_issue(self):
  2722. """ Test the api_view_issue method of the flask api. """
  2723. self.test_api_private_repo_new_issue()
  2724. # Invalid repo
  2725. output = self.app.get("/api/0/foo/issue/1")
  2726. self.assertEqual(output.status_code, 404)
  2727. data = json.loads(output.get_data(as_text=True))
  2728. self.assertDictEqual(
  2729. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2730. )
  2731. # Invalid issue for this repo
  2732. output = self.app.get("/api/0/test4/issue/1")
  2733. self.assertEqual(output.status_code, 404)
  2734. data = json.loads(output.get_data(as_text=True))
  2735. self.assertDictEqual(
  2736. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2737. )
  2738. # Un-authorized user
  2739. user = tests.FakeUser()
  2740. with tests.user_set(self.app.application, user):
  2741. output = self.app.get("/api/0/test4/issue/1")
  2742. self.assertEqual(output.status_code, 404)
  2743. data = json.loads(output.get_data(as_text=True))
  2744. self.assertDictEqual(
  2745. data,
  2746. {"error": "Project not found", "error_code": "ENOPROJECT"},
  2747. )
  2748. # Valid issue
  2749. user = tests.FakeUser(username="pingou")
  2750. with tests.user_set(self.app.application, user):
  2751. output = self.app.get("/api/0/test4/issue/1")
  2752. self.assertEqual(output.status_code, 200)
  2753. data = json.loads(output.get_data(as_text=True))
  2754. data["date_created"] = "1431414800"
  2755. data["last_updated"] = "1431414800"
  2756. self.assertDictEqual(
  2757. data,
  2758. {
  2759. "assignee": None,
  2760. "blocks": [],
  2761. "close_status": None,
  2762. "closed_at": None,
  2763. "closed_by": None,
  2764. "comments": [],
  2765. "content": "This issue needs attention",
  2766. "custom_fields": [],
  2767. "date_created": "1431414800",
  2768. "depends": [],
  2769. "id": 1,
  2770. "last_updated": "1431414800",
  2771. "milestone": None,
  2772. "priority": None,
  2773. "private": False,
  2774. "related_prs": [],
  2775. "status": "Open",
  2776. "tags": [],
  2777. "title": "test issue",
  2778. "user": {
  2779. "fullname": "PY C",
  2780. "name": "pingou",
  2781. "url_path": "user/pingou",
  2782. },
  2783. },
  2784. )
  2785. headers = {"Authorization": "token aaabbbccc"}
  2786. # Access issue authenticated but non-existing token
  2787. output = self.app.get("/api/0/test4/issue/1", headers=headers)
  2788. self.assertEqual(output.status_code, 401)
  2789. data = json.loads(output.get_data(as_text=True))
  2790. self.assertEqual(
  2791. sorted(data.keys()), ["error", "error_code", "errors"]
  2792. )
  2793. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  2794. self.assertEqual(
  2795. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  2796. )
  2797. self.assertEqual(data["errors"], "Invalid token")
  2798. headers = {"Authorization": "token aaabbbcccddd"}
  2799. # Access issue authenticated correctly
  2800. output = self.app.get("/api/0/test4/issue/1", headers=headers)
  2801. self.assertEqual(output.status_code, 200)
  2802. data = json.loads(output.get_data(as_text=True))
  2803. data["date_created"] = "1431414800"
  2804. data["last_updated"] = "1431414800"
  2805. self.assertDictEqual(
  2806. data,
  2807. {
  2808. "assignee": None,
  2809. "blocks": [],
  2810. "close_status": None,
  2811. "closed_at": None,
  2812. "closed_by": None,
  2813. "comments": [],
  2814. "content": "This issue needs attention",
  2815. "custom_fields": [],
  2816. "date_created": "1431414800",
  2817. "depends": [],
  2818. "id": 1,
  2819. "last_updated": "1431414800",
  2820. "milestone": None,
  2821. "priority": None,
  2822. "private": False,
  2823. "related_prs": [],
  2824. "status": "Open",
  2825. "tags": [],
  2826. "title": "test issue",
  2827. "user": {
  2828. "fullname": "PY C",
  2829. "name": "pingou",
  2830. "url_path": "user/pingou",
  2831. },
  2832. },
  2833. )
  2834. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2835. def test_api_private_repo_change_status_issue(self):
  2836. """ Test the api_change_status_issue method of the flask api. """
  2837. item = pagure.lib.model.Project(
  2838. user_id=1, # pingou
  2839. name="test4",
  2840. description="test project description",
  2841. hook_token="aaabbbeeeceee",
  2842. private=True,
  2843. )
  2844. item.close_status = [
  2845. "Invalid",
  2846. "Insufficient data",
  2847. "Fixed",
  2848. "Duplicate",
  2849. ]
  2850. self.session.add(item)
  2851. self.session.commit()
  2852. for repo in ["GIT_FOLDER", "TICKETS_FOLDER"]:
  2853. # Add a git repo
  2854. repo_path = os.path.join(
  2855. pagure.config.config.get(repo), "test4.git"
  2856. )
  2857. if not os.path.exists(repo_path):
  2858. os.makedirs(repo_path)
  2859. pygit2.init_repository(repo_path, bare=True)
  2860. tests.create_tokens(self.session)
  2861. tests.create_tokens_acl(self.session)
  2862. headers = {"Authorization": "token aaabbbcccddd"}
  2863. # Invalid project
  2864. output = self.app.post("/api/0/foo/issue/1/status", headers=headers)
  2865. self.assertEqual(output.status_code, 404)
  2866. data = json.loads(output.get_data(as_text=True))
  2867. self.assertDictEqual(
  2868. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2869. )
  2870. # Valid token, wrong project
  2871. user = tests.FakeUser(username="pingou")
  2872. with tests.user_set(self.app.application, user):
  2873. output = self.app.post(
  2874. "/api/0/test2/issue/1/status", headers=headers
  2875. )
  2876. self.assertEqual(output.status_code, 404)
  2877. data = json.loads(output.get_data(as_text=True))
  2878. self.assertDictEqual(
  2879. data,
  2880. {"error": "Project not found", "error_code": "ENOPROJECT"},
  2881. )
  2882. # No input
  2883. output = self.app.post("/api/0/test4/issue/1/status", headers=headers)
  2884. self.assertEqual(output.status_code, 404)
  2885. data = json.loads(output.get_data(as_text=True))
  2886. self.assertDictEqual(
  2887. data, {"error": "Issue not found", "error_code": "ENOISSUE"}
  2888. )
  2889. # Create normal issue
  2890. repo = pagure.lib.query._get_project(self.session, "test4")
  2891. msg = pagure.lib.query.new_issue(
  2892. session=self.session,
  2893. repo=repo,
  2894. title="Test issue #1",
  2895. content="We should work on this",
  2896. user="pingou",
  2897. private=False,
  2898. )
  2899. self.session.commit()
  2900. self.assertEqual(msg.title, "Test issue #1")
  2901. # Check status before
  2902. repo = pagure.lib.query._get_project(self.session, "test4")
  2903. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2904. self.assertEqual(issue.status, "Open")
  2905. data = {"title": "test issue"}
  2906. user = tests.FakeUser(username="pingou")
  2907. with tests.user_set(self.app.application, user):
  2908. # Incomplete request
  2909. output = self.app.post(
  2910. "/api/0/test4/issue/1/status", data=data, headers=headers
  2911. )
  2912. self.assertEqual(output.status_code, 400)
  2913. data = json.loads(output.get_data(as_text=True))
  2914. self.assertDictEqual(
  2915. data,
  2916. {
  2917. "error": "Invalid or incomplete input submitted",
  2918. "error_code": "EINVALIDREQ",
  2919. "errors": {"status": ["Not a valid choice"]},
  2920. },
  2921. )
  2922. # No change
  2923. repo = pagure.lib.query._get_project(self.session, "test4")
  2924. issue = pagure.lib.query.search_issues(
  2925. self.session, repo, issueid=1
  2926. )
  2927. self.assertEqual(issue.status, "Open")
  2928. data = {"status": "Open"}
  2929. # Valid request but no change
  2930. output = self.app.post(
  2931. "/api/0/test4/issue/1/status", data=data, headers=headers
  2932. )
  2933. self.assertEqual(output.status_code, 200)
  2934. data = json.loads(output.get_data(as_text=True))
  2935. self.assertDictEqual(data, {"message": "No changes"})
  2936. # No change
  2937. repo = pagure.lib.query._get_project(self.session, "test4")
  2938. issue = pagure.lib.query.search_issues(
  2939. self.session, repo, issueid=1
  2940. )
  2941. self.assertEqual(issue.status, "Open")
  2942. data = {"status": "Fixed"}
  2943. # Valid request
  2944. output = self.app.post(
  2945. "/api/0/test4/issue/1/status", data=data, headers=headers
  2946. )
  2947. self.assertEqual(output.status_code, 200)
  2948. data = json.loads(output.get_data(as_text=True))
  2949. self.assertDictEqual(
  2950. data,
  2951. {
  2952. "message": [
  2953. "Issue status updated to: Closed (was: Open)",
  2954. "Issue close_status updated to: Fixed",
  2955. ]
  2956. },
  2957. )
  2958. @patch("pagure.lib.git.update_git")
  2959. @patch("pagure.lib.notify.send_email")
  2960. def test_api_private_repo_comment_issue(self, p_send_email, p_ugt):
  2961. """ Test the api_comment_issue method of the flask api. """
  2962. p_send_email.return_value = True
  2963. p_ugt.return_value = True
  2964. item = pagure.lib.model.Project(
  2965. user_id=1, # pingou
  2966. name="test4",
  2967. description="test project description",
  2968. hook_token="aaabbbeeeceee",
  2969. private=True,
  2970. )
  2971. self.session.add(item)
  2972. self.session.commit()
  2973. tests.create_tokens(self.session)
  2974. tests.create_tokens_acl(self.session)
  2975. headers = {"Authorization": "token aaabbbcccddd"}
  2976. # Invalid project
  2977. output = self.app.post("/api/0/foo/issue/1/comment", headers=headers)
  2978. self.assertEqual(output.status_code, 404)
  2979. data = json.loads(output.get_data(as_text=True))
  2980. self.assertDictEqual(
  2981. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2982. )
  2983. # Invalid token, right project
  2984. headers = {"Authorization": "token aaabbbccc"}
  2985. output = self.app.post("/api/0/test4/issue/1/comment", headers=headers)
  2986. self.assertEqual(output.status_code, 401)
  2987. data = json.loads(output.get_data(as_text=True))
  2988. self.assertEqual(
  2989. sorted(data.keys()), ["error", "error_code", "errors"]
  2990. )
  2991. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  2992. self.assertEqual(
  2993. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  2994. )
  2995. self.assertEqual(data["errors"], "Invalid token")
  2996. headers = {"Authorization": "token aaabbbcccddd"}
  2997. # No input
  2998. output = self.app.post("/api/0/test4/issue/1/comment", headers=headers)
  2999. self.assertEqual(output.status_code, 404)
  3000. data = json.loads(output.get_data(as_text=True))
  3001. self.assertDictEqual(
  3002. data, {"error": "Issue not found", "error_code": "ENOISSUE"}
  3003. )
  3004. # Create normal issue
  3005. repo = pagure.lib.query._get_project(self.session, "test4")
  3006. msg = pagure.lib.query.new_issue(
  3007. session=self.session,
  3008. repo=repo,
  3009. title="Test issue #1",
  3010. content="We should work on this",
  3011. user="pingou",
  3012. private=False,
  3013. issue_uid="aaabbbccc1",
  3014. )
  3015. self.session.commit()
  3016. self.assertEqual(msg.title, "Test issue #1")
  3017. # Check comments before
  3018. self.session.commit()
  3019. repo = pagure.lib.query._get_project(self.session, "test4")
  3020. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  3021. self.assertEqual(len(issue.comments), 0)
  3022. data = {"title": "test issue"}
  3023. # Incomplete request
  3024. output = self.app.post(
  3025. "/api/0/test4/issue/1/comment", data=data, headers=headers
  3026. )
  3027. self.assertEqual(output.status_code, 400)
  3028. data = json.loads(output.get_data(as_text=True))
  3029. self.assertDictEqual(
  3030. data,
  3031. {
  3032. "error": "Invalid or incomplete input submitted",
  3033. "error_code": "EINVALIDREQ",
  3034. "errors": {"comment": ["This field is required."]},
  3035. },
  3036. )
  3037. # No change
  3038. self.session.commit()
  3039. repo = pagure.lib.query._get_project(self.session, "test4")
  3040. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  3041. self.assertEqual(issue.status, "Open")
  3042. data = {"comment": "This is a very interesting question"}
  3043. # Valid request
  3044. output = self.app.post(
  3045. "/api/0/test4/issue/1/comment", data=data, headers=headers
  3046. )
  3047. self.assertEqual(output.status_code, 200)
  3048. data = json.loads(output.get_data(as_text=True))
  3049. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  3050. self.assertDictEqual(
  3051. data,
  3052. {
  3053. "message": "Comment added",
  3054. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  3055. "user": "pingou",
  3056. },
  3057. )
  3058. # One comment added
  3059. self.session.commit()
  3060. repo = pagure.lib.query._get_project(self.session, "test4")
  3061. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  3062. self.assertEqual(len(issue.comments), 1)
  3063. @patch("pagure.lib.git.update_git")
  3064. @patch("pagure.lib.notify.send_email")
  3065. def test_api_view_issue_comment(self, p_send_email, p_ugt):
  3066. """ Test the api_view_issue_comment endpoint. """
  3067. p_send_email.return_value = True
  3068. p_ugt.return_value = True
  3069. self.test_api_private_repo_comment_issue()
  3070. # View a comment that does not exist
  3071. output = self.app.get("/api/0/foo/issue/100/comment/2")
  3072. self.assertEqual(output.status_code, 404)
  3073. # Issue exists but not the comment
  3074. output = self.app.get("/api/0/test/issue/1/comment/2")
  3075. self.assertEqual(output.status_code, 404)
  3076. # Issue and comment exists
  3077. output = self.app.get("/api/0/test/issue/1/comment/1")
  3078. self.assertEqual(output.status_code, 404)
  3079. user = tests.FakeUser(username="pingou")
  3080. with tests.user_set(self.app.application, user):
  3081. output = self.app.get("/api/0/test4/issue/1/comment/1")
  3082. self.assertEqual(output.status_code, 200)
  3083. data = json.loads(output.get_data(as_text=True))
  3084. data["date_created"] = "1435821770"
  3085. data["comment_date"] = "2015-07-02 09:22"
  3086. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  3087. self.assertDictEqual(
  3088. data,
  3089. {
  3090. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  3091. "comment": "This is a very interesting question",
  3092. "comment_date": "2015-07-02 09:22",
  3093. "notification": False,
  3094. "date_created": "1435821770",
  3095. "edited_on": None,
  3096. "editor": None,
  3097. "id": 1,
  3098. "parent": None,
  3099. "reactions": {},
  3100. "user": {
  3101. "fullname": "PY C",
  3102. "name": "pingou",
  3103. "url_path": "user/pingou",
  3104. },
  3105. },
  3106. )
  3107. # Issue and comment exists, using UID
  3108. output = self.app.get("/api/0/test4/issue/aaabbbccc1/comment/1")
  3109. self.assertEqual(output.status_code, 200)
  3110. data = json.loads(output.get_data(as_text=True))
  3111. data["date_created"] = "1435821770"
  3112. data["comment_date"] = "2015-07-02 09:22"
  3113. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  3114. self.assertDictEqual(
  3115. data,
  3116. {
  3117. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  3118. "comment": "This is a very interesting question",
  3119. "comment_date": "2015-07-02 09:22",
  3120. "notification": False,
  3121. "date_created": "1435821770",
  3122. "edited_on": None,
  3123. "editor": None,
  3124. "id": 1,
  3125. "parent": None,
  3126. "reactions": {},
  3127. "user": {
  3128. "fullname": "PY C",
  3129. "name": "pingou",
  3130. "url_path": "user/pingou",
  3131. },
  3132. },
  3133. )
  3134. if __name__ == "__main__":
  3135. unittest.main(verbosity=2)