test_pagure_flask_api_ui_private_repo.py 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418
  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. "commit": [],
  1256. "ticket": [],
  1257. },
  1258. "access_users": {
  1259. "admin": [],
  1260. "commit": [],
  1261. "owner": ["pingou"],
  1262. "ticket": [],
  1263. },
  1264. "close_status": [],
  1265. "custom_keys": [],
  1266. "date_created": "1436527638",
  1267. "date_modified": "1436527638",
  1268. "description": "test project description",
  1269. "id": 1,
  1270. "milestones": {},
  1271. "name": "test4",
  1272. "fullname": "test4",
  1273. "url_path": "test4",
  1274. "namespace": None,
  1275. "parent": None,
  1276. "priorities": {},
  1277. "tags": ["infra"],
  1278. "user": {
  1279. "fullname": "PY C",
  1280. "name": "pingou",
  1281. "url_path": "user/pingou",
  1282. },
  1283. }
  1284. ],
  1285. },
  1286. )
  1287. output = self.app.get("/api/0/projects?username=pingou&tags=infra")
  1288. self.assertEqual(output.status_code, 200)
  1289. data = json.loads(output.get_data(as_text=True))
  1290. data["projects"][0]["date_created"] = "1436527638"
  1291. data["projects"][0]["date_modified"] = "1436527638"
  1292. del data["pagination"]
  1293. self.assertDictEqual(
  1294. data,
  1295. {
  1296. "args": {
  1297. "fork": None,
  1298. "namespace": None,
  1299. "owner": None,
  1300. "page": 1,
  1301. "pattern": None,
  1302. "per_page": 20,
  1303. "short": False,
  1304. "tags": ["infra"],
  1305. "username": "pingou",
  1306. },
  1307. "total_projects": 1,
  1308. "projects": [
  1309. {
  1310. "access_groups": {
  1311. "admin": [],
  1312. "commit": [],
  1313. "ticket": [],
  1314. },
  1315. "access_users": {
  1316. "admin": [],
  1317. "commit": [],
  1318. "owner": ["pingou"],
  1319. "ticket": [],
  1320. },
  1321. "close_status": [],
  1322. "custom_keys": [],
  1323. "date_created": "1436527638",
  1324. "date_modified": "1436527638",
  1325. "description": "test project description",
  1326. "id": 1,
  1327. "milestones": {},
  1328. "name": "test4",
  1329. "fullname": "test4",
  1330. "url_path": "test4",
  1331. "namespace": None,
  1332. "parent": None,
  1333. "priorities": {},
  1334. "tags": ["infra"],
  1335. "user": {
  1336. "fullname": "PY C",
  1337. "name": "pingou",
  1338. "url_path": "user/pingou",
  1339. },
  1340. }
  1341. ],
  1342. },
  1343. )
  1344. # Api pull-request views
  1345. @patch("pagure.lib.notify.send_email")
  1346. def test_api_private_repo_fork(self, send_email):
  1347. """ Test api endpoints in api/fork"""
  1348. send_email.return_value = True
  1349. # Add private repo
  1350. item = pagure.lib.model.Project(
  1351. user_id=1, # pingou
  1352. name="test4",
  1353. description="test project description",
  1354. hook_token="aaabbbeeeceee",
  1355. private=True,
  1356. )
  1357. self.session.add(item)
  1358. self.session.commit()
  1359. tests.create_tokens(self.session)
  1360. tests.create_tokens_acl(self.session)
  1361. headers = {"Authorization": "token aaabbbcccddd"}
  1362. # Create a pull-request
  1363. repo = pagure.lib.query._get_project(self.session, "test4")
  1364. forked_repo = pagure.lib.query._get_project(self.session, "test4")
  1365. req = pagure.lib.query.new_pull_request(
  1366. session=self.session,
  1367. repo_from=forked_repo,
  1368. branch_from="master",
  1369. repo_to=repo,
  1370. branch_to="master",
  1371. title="test pull-request",
  1372. user="pingou",
  1373. )
  1374. self.session.commit()
  1375. self.assertEqual(req.id, 1)
  1376. self.assertEqual(req.title, "test pull-request")
  1377. # Check list of PR
  1378. output = self.app.get("/api/0/test4/pull-requests")
  1379. self.assertEqual(output.status_code, 404)
  1380. # Check single PR
  1381. output = self.app.get("/api/0/test/pull-request/1")
  1382. self.assertEqual(output.status_code, 404)
  1383. user = tests.FakeUser(username="pingou")
  1384. with tests.user_set(self.app.application, user):
  1385. # List pull-requests
  1386. output = self.app.get("/api/0/test4/pull-requests")
  1387. self.assertEqual(output.status_code, 200)
  1388. data = json.loads(output.get_data(as_text=True))
  1389. data["requests"][0]["date_created"] = "1431414800"
  1390. data["requests"][0]["updated_on"] = "1431414800"
  1391. data["requests"][0]["project"]["date_created"] = "1431414800"
  1392. data["requests"][0]["project"]["date_modified"] = "1431414800"
  1393. data["requests"][0]["repo_from"]["date_created"] = "1431414800"
  1394. data["requests"][0]["repo_from"]["date_modified"] = "1431414800"
  1395. data["requests"][0]["uid"] = "1431414800"
  1396. data["requests"][0]["last_updated"] = "1431414800"
  1397. for k in ["first", "last"]:
  1398. self.assertIsNotNone(data["pagination"][k])
  1399. data["pagination"][k] = "http://localhost..."
  1400. self.assertDictEqual(
  1401. data,
  1402. {
  1403. "args": {
  1404. "assignee": None,
  1405. "author": None,
  1406. "tags": [],
  1407. "page": 1,
  1408. "per_page": 20,
  1409. "status": True,
  1410. },
  1411. "pagination": {
  1412. "first": "http://localhost...",
  1413. "last": "http://localhost...",
  1414. "next": None,
  1415. "page": 1,
  1416. "pages": 1,
  1417. "per_page": 20,
  1418. "prev": None,
  1419. },
  1420. "requests": [
  1421. {
  1422. "assignee": None,
  1423. "branch": "master",
  1424. "branch_from": "master",
  1425. "cached_merge_status": "unknown",
  1426. "closed_at": None,
  1427. "closed_by": None,
  1428. "comments": [],
  1429. "commit_start": None,
  1430. "commit_stop": None,
  1431. "date_created": "1431414800",
  1432. "last_updated": "1431414800",
  1433. "id": 1,
  1434. "initial_comment": None,
  1435. "project": {
  1436. "access_groups": {
  1437. "admin": [],
  1438. "commit": [],
  1439. "ticket": [],
  1440. },
  1441. "access_users": {
  1442. "admin": [],
  1443. "commit": [],
  1444. "owner": ["pingou"],
  1445. "ticket": [],
  1446. },
  1447. "close_status": [],
  1448. "custom_keys": [],
  1449. "date_created": "1431414800",
  1450. "date_modified": "1431414800",
  1451. "description": "test project description",
  1452. "id": 1,
  1453. "milestones": {},
  1454. "name": "test4",
  1455. "fullname": "test4",
  1456. "url_path": "test4",
  1457. "namespace": None,
  1458. "parent": None,
  1459. "priorities": {},
  1460. "tags": [],
  1461. "user": {
  1462. "fullname": "PY C",
  1463. "name": "pingou",
  1464. "url_path": "user/pingou",
  1465. },
  1466. },
  1467. "remote_git": None,
  1468. "repo_from": {
  1469. "access_groups": {
  1470. "admin": [],
  1471. "commit": [],
  1472. "ticket": [],
  1473. },
  1474. "access_users": {
  1475. "admin": [],
  1476. "commit": [],
  1477. "owner": ["pingou"],
  1478. "ticket": [],
  1479. },
  1480. "close_status": [],
  1481. "custom_keys": [],
  1482. "date_created": "1431414800",
  1483. "date_modified": "1431414800",
  1484. "description": "test project description",
  1485. "id": 1,
  1486. "milestones": {},
  1487. "fullname": "test4",
  1488. "url_path": "test4",
  1489. "name": "test4",
  1490. "namespace": None,
  1491. "parent": None,
  1492. "priorities": {},
  1493. "tags": [],
  1494. "user": {
  1495. "fullname": "PY C",
  1496. "name": "pingou",
  1497. "url_path": "user/pingou",
  1498. },
  1499. },
  1500. "status": "Open",
  1501. "tags": [],
  1502. "threshold_reached": None,
  1503. "title": "test pull-request",
  1504. "uid": "1431414800",
  1505. "updated_on": "1431414800",
  1506. "user": {
  1507. "fullname": "PY C",
  1508. "name": "pingou",
  1509. "url_path": "user/pingou",
  1510. },
  1511. }
  1512. ],
  1513. "total_requests": 1,
  1514. },
  1515. )
  1516. headers = {"Authorization": "token foobar_token"}
  1517. # Access Pull-Request authenticated
  1518. output = self.app.get(
  1519. "/api/0/test4/pull-requests", headers=headers
  1520. )
  1521. self.assertEqual(output.status_code, 200)
  1522. data2 = json.loads(output.get_data(as_text=True))
  1523. data2["requests"][0]["date_created"] = "1431414800"
  1524. data2["requests"][0]["updated_on"] = "1431414800"
  1525. data2["requests"][0]["project"]["date_created"] = "1431414800"
  1526. data2["requests"][0]["project"]["date_modified"] = "1431414800"
  1527. data2["requests"][0]["repo_from"]["date_created"] = "1431414800"
  1528. data2["requests"][0]["repo_from"]["date_modified"] = "1431414800"
  1529. data2["requests"][0]["uid"] = "1431414800"
  1530. data2["requests"][0]["last_updated"] = "1431414800"
  1531. for k in ["first", "last"]:
  1532. self.assertIsNotNone(data["pagination"][k])
  1533. data2["pagination"][k] = "http://localhost..."
  1534. self.assertDictEqual(data, data2)
  1535. # For single PR
  1536. output = self.app.get("/api/0/test4/pull-request/1")
  1537. self.assertEqual(output.status_code, 200)
  1538. data = json.loads(output.get_data(as_text=True))
  1539. data["date_created"] = "1431414800"
  1540. data["updated_on"] = "1431414800"
  1541. data["project"]["date_created"] = "1431414800"
  1542. data["project"]["date_modified"] = "1431414800"
  1543. data["repo_from"]["date_created"] = "1431414800"
  1544. data["repo_from"]["date_modified"] = "1431414800"
  1545. data["uid"] = "1431414800"
  1546. data["last_updated"] = "1431414800"
  1547. self.assertDictEqual(
  1548. data,
  1549. {
  1550. "assignee": None,
  1551. "branch": "master",
  1552. "branch_from": "master",
  1553. "cached_merge_status": "unknown",
  1554. "closed_at": None,
  1555. "closed_by": None,
  1556. "comments": [],
  1557. "commit_start": None,
  1558. "commit_stop": None,
  1559. "date_created": "1431414800",
  1560. "last_updated": "1431414800",
  1561. "id": 1,
  1562. "initial_comment": None,
  1563. "project": {
  1564. "access_groups": {
  1565. "admin": [],
  1566. "commit": [],
  1567. "ticket": [],
  1568. },
  1569. "access_users": {
  1570. "admin": [],
  1571. "commit": [],
  1572. "owner": ["pingou"],
  1573. "ticket": [],
  1574. },
  1575. "close_status": [],
  1576. "custom_keys": [],
  1577. "date_created": "1431414800",
  1578. "date_modified": "1431414800",
  1579. "description": "test project description",
  1580. "id": 1,
  1581. "milestones": {},
  1582. "name": "test4",
  1583. "fullname": "test4",
  1584. "url_path": "test4",
  1585. "namespace": None,
  1586. "parent": None,
  1587. "priorities": {},
  1588. "tags": [],
  1589. "user": {
  1590. "fullname": "PY C",
  1591. "name": "pingou",
  1592. "url_path": "user/pingou",
  1593. },
  1594. },
  1595. "remote_git": None,
  1596. "repo_from": {
  1597. "access_groups": {
  1598. "admin": [],
  1599. "commit": [],
  1600. "ticket": [],
  1601. },
  1602. "access_users": {
  1603. "admin": [],
  1604. "commit": [],
  1605. "owner": ["pingou"],
  1606. "ticket": [],
  1607. },
  1608. "close_status": [],
  1609. "custom_keys": [],
  1610. "date_created": "1431414800",
  1611. "date_modified": "1431414800",
  1612. "description": "test project description",
  1613. "id": 1,
  1614. "milestones": {},
  1615. "name": "test4",
  1616. "fullname": "test4",
  1617. "url_path": "test4",
  1618. "namespace": None,
  1619. "parent": None,
  1620. "priorities": {},
  1621. "tags": [],
  1622. "user": {
  1623. "fullname": "PY C",
  1624. "name": "pingou",
  1625. "url_path": "user/pingou",
  1626. },
  1627. },
  1628. "status": "Open",
  1629. "tags": [],
  1630. "threshold_reached": None,
  1631. "title": "test pull-request",
  1632. "uid": "1431414800",
  1633. "updated_on": "1431414800",
  1634. "user": {
  1635. "fullname": "PY C",
  1636. "name": "pingou",
  1637. "url_path": "user/pingou",
  1638. },
  1639. },
  1640. )
  1641. # Access Pull-Request authenticated
  1642. output = self.app.get(
  1643. "/api/0/test4/pull-request/1", headers=headers
  1644. )
  1645. self.assertEqual(output.status_code, 200)
  1646. data2 = json.loads(output.get_data(as_text=True))
  1647. data2["date_created"] = "1431414800"
  1648. data2["project"]["date_created"] = "1431414800"
  1649. data2["project"]["date_modified"] = "1431414800"
  1650. data2["repo_from"]["date_created"] = "1431414800"
  1651. data2["repo_from"]["date_modified"] = "1431414800"
  1652. data2["uid"] = "1431414800"
  1653. data2["date_created"] = "1431414800"
  1654. data2["updated_on"] = "1431414800"
  1655. data2["last_updated"] = "1431414800"
  1656. self.assertDictEqual(data, data2)
  1657. @patch("pagure.lib.notify.send_email")
  1658. def test_api_pr_private_repo_add_comment(self, mockemail):
  1659. """ Test the api_pull_request_add_comment method of the flask api. """
  1660. mockemail.return_value = True
  1661. pagure.config.config["REQUESTS_FOLDER"] = None
  1662. # Add private repo
  1663. item = pagure.lib.model.Project(
  1664. user_id=1, # pingou
  1665. name="test4",
  1666. description="test project description",
  1667. hook_token="aaabbbeeeceee",
  1668. private=True,
  1669. )
  1670. self.session.add(item)
  1671. self.session.commit()
  1672. tests.create_tokens(self.session)
  1673. tests.create_tokens_acl(self.session)
  1674. headers = {"Authorization": "token aaabbbcccddd"}
  1675. # Create a pull-request
  1676. repo = pagure.lib.query._get_project(self.session, "test4")
  1677. forked_repo = pagure.lib.query._get_project(self.session, "test4")
  1678. req = pagure.lib.query.new_pull_request(
  1679. session=self.session,
  1680. repo_from=forked_repo,
  1681. branch_from="master",
  1682. repo_to=repo,
  1683. branch_to="master",
  1684. title="test pull-request",
  1685. user="pingou",
  1686. )
  1687. self.session.commit()
  1688. self.assertEqual(req.id, 1)
  1689. self.assertEqual(req.title, "test pull-request")
  1690. # Check comments before
  1691. self.session.commit()
  1692. request = pagure.lib.query.search_pull_requests(
  1693. self.session, project_id=1, requestid=1
  1694. )
  1695. self.assertEqual(len(request.comments), 0)
  1696. data = {"title": "test issue"}
  1697. # Incomplete request
  1698. output = self.app.post(
  1699. "/api/0/test4/pull-request/1/comment", data=data, headers=headers
  1700. )
  1701. self.assertEqual(output.status_code, 400)
  1702. data = json.loads(output.get_data(as_text=True))
  1703. self.assertDictEqual(
  1704. data,
  1705. {
  1706. "error": "Invalid or incomplete input submitted",
  1707. "error_code": "EINVALIDREQ",
  1708. "errors": {"comment": ["This field is required."]},
  1709. },
  1710. )
  1711. # No change
  1712. self.session.commit()
  1713. request = pagure.lib.query.search_pull_requests(
  1714. self.session, project_id=1, requestid=1
  1715. )
  1716. self.assertEqual(len(request.comments), 0)
  1717. data = {"comment": "This is a very interesting question"}
  1718. # Valid request
  1719. output = self.app.post(
  1720. "/api/0/test4/pull-request/1/comment", data=data, headers=headers
  1721. )
  1722. self.assertEqual(output.status_code, 200)
  1723. data = json.loads(output.get_data(as_text=True))
  1724. self.assertDictEqual(data, {"message": "Comment added"})
  1725. # One comment added
  1726. self.session.commit()
  1727. request = pagure.lib.query.search_pull_requests(
  1728. self.session, project_id=1, requestid=1
  1729. )
  1730. self.assertEqual(len(request.comments), 1)
  1731. @patch("pagure.lib.notify.send_email")
  1732. def test_api_private_repo_pr_add_flag(self, mockemail):
  1733. """ Test the api_pull_request_add_flag method of the flask api. """
  1734. mockemail.return_value = True
  1735. pagure.config.config["REQUESTS_FOLDER"] = None
  1736. # Add private repo
  1737. item = pagure.lib.model.Project(
  1738. user_id=1, # pingou
  1739. name="test4",
  1740. description="test project description",
  1741. hook_token="aaabbbeeeceee",
  1742. private=True,
  1743. )
  1744. self.session.add(item)
  1745. self.session.commit()
  1746. # Add private repo
  1747. item = pagure.lib.model.Project(
  1748. user_id=1, # pingou
  1749. name="test2",
  1750. description="test project description",
  1751. hook_token="foo_bar",
  1752. private=True,
  1753. )
  1754. self.session.add(item)
  1755. self.session.commit()
  1756. tests.create_tokens(self.session)
  1757. tests.create_tokens_acl(self.session)
  1758. headers = {"Authorization": "token aaabbbcccddd"}
  1759. # Invalid project
  1760. output = self.app.post(
  1761. "/api/0/foo/pull-request/1/flag", headers=headers
  1762. )
  1763. self.assertEqual(output.status_code, 404)
  1764. data = json.loads(output.get_data(as_text=True))
  1765. self.assertDictEqual(
  1766. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  1767. )
  1768. # Valid token, wrong project
  1769. output = self.app.post(
  1770. "/api/0/test2/pull-request/1/flag", headers=headers
  1771. )
  1772. self.assertEqual(output.status_code, 401)
  1773. data = json.loads(output.get_data(as_text=True))
  1774. self.assertEqual(sorted(data.keys()), ["error", "error_code"])
  1775. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  1776. self.assertEqual(
  1777. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  1778. )
  1779. # No input
  1780. output = self.app.post(
  1781. "/api/0/test4/pull-request/1/flag", headers=headers
  1782. )
  1783. self.assertEqual(output.status_code, 404)
  1784. data = json.loads(output.get_data(as_text=True))
  1785. self.assertDictEqual(
  1786. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  1787. )
  1788. # Create a pull-request
  1789. repo = pagure.lib.query._get_project(self.session, "test4")
  1790. forked_repo = pagure.lib.query._get_project(self.session, "test4")
  1791. req = pagure.lib.query.new_pull_request(
  1792. session=self.session,
  1793. repo_from=forked_repo,
  1794. branch_from="master",
  1795. repo_to=repo,
  1796. branch_to="master",
  1797. title="test pull-request",
  1798. user="pingou",
  1799. )
  1800. self.session.commit()
  1801. self.assertEqual(req.id, 1)
  1802. self.assertEqual(req.title, "test pull-request")
  1803. # Check comments before
  1804. self.session.commit()
  1805. request = pagure.lib.query.search_pull_requests(
  1806. self.session, project_id=1, requestid=1
  1807. )
  1808. self.assertEqual(len(request.flags), 0)
  1809. data = {
  1810. "username": "Jenkins",
  1811. "percent": 100,
  1812. "url": "http://jenkins.cloud.fedoraproject.org/",
  1813. "uid": "jenkins_build_pagure_100+seed",
  1814. }
  1815. # Incomplete request
  1816. output = self.app.post(
  1817. "/api/0/test4/pull-request/1/flag", data=data, headers=headers
  1818. )
  1819. self.assertEqual(output.status_code, 400)
  1820. data = json.loads(output.get_data(as_text=True))
  1821. self.assertDictEqual(
  1822. data,
  1823. {
  1824. "error": "Invalid or incomplete input submitted",
  1825. "error_code": "EINVALIDREQ",
  1826. "errors": {"comment": ["This field is required."]},
  1827. },
  1828. )
  1829. # No change
  1830. self.session.commit()
  1831. request = pagure.lib.query.search_pull_requests(
  1832. self.session, project_id=1, requestid=1
  1833. )
  1834. self.assertEqual(len(request.flags), 0)
  1835. data = {
  1836. "username": "Jenkins",
  1837. "percent": 0,
  1838. "comment": "Tests failed",
  1839. "url": "http://jenkins.cloud.fedoraproject.org/",
  1840. "uid": "jenkins_build_pagure_100+seed",
  1841. }
  1842. # Valid request
  1843. output = self.app.post(
  1844. "/api/0/test4/pull-request/1/flag", data=data, headers=headers
  1845. )
  1846. self.assertEqual(output.status_code, 200)
  1847. data = json.loads(output.get_data(as_text=True))
  1848. data["flag"]["date_created"] = "1510742565"
  1849. data["flag"]["date_updated"] = "1510742565"
  1850. data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
  1851. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  1852. self.assertDictEqual(
  1853. data,
  1854. {
  1855. "flag": {
  1856. "comment": "Tests failed",
  1857. "date_created": "1510742565",
  1858. "date_updated": "1510742565",
  1859. "percent": 0,
  1860. "pull_request_uid": "62b49f00d489452994de5010565fab81",
  1861. "status": "failure",
  1862. "url": "http://jenkins.cloud.fedoraproject.org/",
  1863. "user": {
  1864. "default_email": "bar@pingou.com",
  1865. "emails": ["bar@pingou.com", "foo@pingou.com"],
  1866. "fullname": "PY C",
  1867. "name": "pingou",
  1868. "url_path": "user/pingou",
  1869. },
  1870. "username": "Jenkins",
  1871. },
  1872. "message": "Flag added",
  1873. "uid": "jenkins_build_pagure_100+seed",
  1874. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  1875. "user": "pingou",
  1876. },
  1877. )
  1878. # One flag added
  1879. self.session.commit()
  1880. request = pagure.lib.query.search_pull_requests(
  1881. self.session, project_id=1, requestid=1
  1882. )
  1883. self.assertEqual(len(request.flags), 1)
  1884. self.assertEqual(request.flags[0].comment, "Tests failed")
  1885. self.assertEqual(request.flags[0].percent, 0)
  1886. # Update flag
  1887. data = {
  1888. "username": "Jenkins",
  1889. "percent": 100,
  1890. "comment": "Tests passed",
  1891. "url": "http://jenkins.cloud.fedoraproject.org/",
  1892. "uid": "jenkins_build_pagure_100+seed",
  1893. }
  1894. output = self.app.post(
  1895. "/api/0/test4/pull-request/1/flag", data=data, headers=headers
  1896. )
  1897. self.assertEqual(output.status_code, 200)
  1898. data = json.loads(output.get_data(as_text=True))
  1899. data["flag"]["date_created"] = "1510742565"
  1900. data["flag"]["date_updated"] = "1510742565"
  1901. data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
  1902. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  1903. self.assertDictEqual(
  1904. data,
  1905. {
  1906. "flag": {
  1907. "comment": "Tests passed",
  1908. "date_created": "1510742565",
  1909. "date_updated": "1510742565",
  1910. "percent": 100,
  1911. "pull_request_uid": "62b49f00d489452994de5010565fab81",
  1912. "status": "success",
  1913. "url": "http://jenkins.cloud.fedoraproject.org/",
  1914. "user": {
  1915. "default_email": "bar@pingou.com",
  1916. "emails": ["bar@pingou.com", "foo@pingou.com"],
  1917. "fullname": "PY C",
  1918. "name": "pingou",
  1919. "url_path": "user/pingou",
  1920. },
  1921. "username": "Jenkins",
  1922. },
  1923. "message": "Flag updated",
  1924. "uid": "jenkins_build_pagure_100+seed",
  1925. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  1926. "user": "pingou",
  1927. },
  1928. )
  1929. # One flag added
  1930. self.session.commit()
  1931. request = pagure.lib.query.search_pull_requests(
  1932. self.session, project_id=1, requestid=1
  1933. )
  1934. self.assertEqual(len(request.flags), 1)
  1935. self.assertEqual(request.flags[0].comment, "Tests passed")
  1936. self.assertEqual(request.flags[0].percent, 100)
  1937. @patch("pagure.lib.notify.send_email")
  1938. def test_api_private_repo_pr_close(self, send_email):
  1939. """ Test the api_pull_request_close method of the flask api. """
  1940. send_email.return_value = True
  1941. pagure.config.config["REQUESTS_FOLDER"] = None
  1942. # Add private repo
  1943. item = pagure.lib.model.Project(
  1944. user_id=1, # pingou
  1945. name="test4",
  1946. description="test project description",
  1947. hook_token="aaabbbeeeceee",
  1948. private=True,
  1949. )
  1950. self.session.add(item)
  1951. self.session.commit()
  1952. tests.create_tokens(self.session)
  1953. tests.create_tokens_acl(self.session)
  1954. # Add private repo
  1955. item = pagure.lib.model.Project(
  1956. user_id=1, # pingou
  1957. name="test2",
  1958. description="test project description",
  1959. hook_token="foo_bar",
  1960. private=True,
  1961. )
  1962. self.session.add(item)
  1963. self.session.commit()
  1964. # Create the pull-request to close
  1965. repo = pagure.lib.query._get_project(self.session, "test4")
  1966. forked_repo = pagure.lib.query._get_project(self.session, "test4")
  1967. req = pagure.lib.query.new_pull_request(
  1968. session=self.session,
  1969. repo_from=forked_repo,
  1970. branch_from="master",
  1971. repo_to=repo,
  1972. branch_to="master",
  1973. title="test pull-request",
  1974. user="pingou",
  1975. )
  1976. self.session.commit()
  1977. self.assertEqual(req.id, 1)
  1978. self.assertEqual(req.title, "test pull-request")
  1979. headers = {"Authorization": "token aaabbbcccddd"}
  1980. # Invalid project
  1981. output = self.app.post(
  1982. "/api/0/foo/pull-request/1/close", headers=headers
  1983. )
  1984. self.assertEqual(output.status_code, 404)
  1985. data = json.loads(output.get_data(as_text=True))
  1986. self.assertDictEqual(
  1987. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  1988. )
  1989. # Valid token, wrong project
  1990. output = self.app.post(
  1991. "/api/0/test2/pull-request/1/close", headers=headers
  1992. )
  1993. self.assertEqual(output.status_code, 401)
  1994. data = json.loads(output.get_data(as_text=True))
  1995. self.assertEqual(sorted(data.keys()), ["error", "error_code"])
  1996. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  1997. self.assertEqual(
  1998. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  1999. )
  2000. # Invalid PR
  2001. output = self.app.post(
  2002. "/api/0/test4/pull-request/2/close", headers=headers
  2003. )
  2004. self.assertEqual(output.status_code, 404)
  2005. data = json.loads(output.get_data(as_text=True))
  2006. self.assertDictEqual(
  2007. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  2008. )
  2009. # Create a token for foo for this project
  2010. item = pagure.lib.model.Token(
  2011. id="foobar_token",
  2012. user_id=2,
  2013. project_id=1,
  2014. expiration=datetime.datetime.utcnow()
  2015. + datetime.timedelta(days=30),
  2016. )
  2017. self.session.add(item)
  2018. self.session.commit()
  2019. # Allow the token to close PR
  2020. acls = pagure.lib.query.get_acls(self.session)
  2021. acl = None
  2022. for acl in acls:
  2023. if acl.name == "pull_request_close":
  2024. break
  2025. item = pagure.lib.model.TokenAcl(
  2026. token_id="foobar_token", acl_id=acl.id
  2027. )
  2028. self.session.add(item)
  2029. self.session.commit()
  2030. headers = {"Authorization": "token foobar_token"}
  2031. # User not admin
  2032. output = self.app.post(
  2033. "/api/0/test4/pull-request/1/close", headers=headers
  2034. )
  2035. self.assertEqual(output.status_code, 404)
  2036. data = json.loads(output.get_data(as_text=True))
  2037. self.assertDictEqual(
  2038. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2039. )
  2040. headers = {"Authorization": "token aaabbbcccddd"}
  2041. # Close PR
  2042. output = self.app.post(
  2043. "/api/0/test4/pull-request/1/close", headers=headers
  2044. )
  2045. self.assertEqual(output.status_code, 200)
  2046. data = json.loads(output.get_data(as_text=True))
  2047. self.assertDictEqual(data, {"message": "Pull-request closed!"})
  2048. @patch("pagure.lib.notify.send_email")
  2049. def test_api_private_repo_pr_merge(self, send_email):
  2050. """ Test the api_pull_request_merge method of the flask api. """
  2051. send_email.return_value = True
  2052. pagure.config.config["REQUESTS_FOLDER"] = None
  2053. # Add private repo
  2054. item = pagure.lib.model.Project(
  2055. user_id=1, # pingou
  2056. name="test4",
  2057. description="test project description",
  2058. hook_token="aaabbbeeeceee",
  2059. private=True,
  2060. )
  2061. self.session.add(item)
  2062. self.session.commit()
  2063. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2064. tests.create_projects_git(
  2065. os.path.join(self.path, "requests"), bare=True
  2066. )
  2067. tests.add_readme_git_repo(
  2068. os.path.join(self.path, "repos", "test4.git")
  2069. )
  2070. tests.add_commit_git_repo(
  2071. os.path.join(self.path, "repos", "test4.git"), branch="test"
  2072. )
  2073. tests.create_tokens(self.session)
  2074. tests.create_tokens_acl(self.session)
  2075. # Add private repo
  2076. item = pagure.lib.model.Project(
  2077. user_id=1, # pingou
  2078. name="test2",
  2079. description="test project description",
  2080. hook_token="foo_bar",
  2081. private=True,
  2082. )
  2083. self.session.add(item)
  2084. self.session.commit()
  2085. # Create the pull-request to close
  2086. repo = pagure.lib.query._get_project(self.session, "test4")
  2087. forked_repo = pagure.lib.query._get_project(self.session, "test4")
  2088. req = pagure.lib.query.new_pull_request(
  2089. session=self.session,
  2090. repo_from=forked_repo,
  2091. branch_from="test",
  2092. repo_to=repo,
  2093. branch_to="master",
  2094. title="test pull-request",
  2095. user="pingou",
  2096. )
  2097. self.session.commit()
  2098. self.assertEqual(req.id, 1)
  2099. self.assertEqual(req.title, "test pull-request")
  2100. headers = {"Authorization": "token aaabbbcccddd"}
  2101. # Invalid project
  2102. output = self.app.post(
  2103. "/api/0/foo/pull-request/1/merge", headers=headers
  2104. )
  2105. self.assertEqual(output.status_code, 404)
  2106. data = json.loads(output.get_data(as_text=True))
  2107. self.assertDictEqual(
  2108. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2109. )
  2110. # Valid token, wrong project
  2111. output = self.app.post(
  2112. "/api/0/test2/pull-request/1/merge", headers=headers
  2113. )
  2114. self.assertEqual(output.status_code, 401)
  2115. data = json.loads(output.get_data(as_text=True))
  2116. self.assertEqual(sorted(data.keys()), ["error", "error_code"])
  2117. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  2118. self.assertEqual(
  2119. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  2120. )
  2121. # Invalid PR
  2122. output = self.app.post(
  2123. "/api/0/test4/pull-request/2/merge", headers=headers
  2124. )
  2125. self.assertEqual(output.status_code, 404)
  2126. data = json.loads(output.get_data(as_text=True))
  2127. self.assertDictEqual(
  2128. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  2129. )
  2130. # Create a token for foo for this project
  2131. item = pagure.lib.model.Token(
  2132. id="foobar_token",
  2133. user_id=2,
  2134. project_id=1,
  2135. expiration=datetime.datetime.utcnow()
  2136. + datetime.timedelta(days=30),
  2137. )
  2138. self.session.add(item)
  2139. self.session.commit()
  2140. # Allow the token to merge PR
  2141. acls = pagure.lib.query.get_acls(self.session)
  2142. acl = None
  2143. for acl in acls:
  2144. if acl.name == "pull_request_merge":
  2145. break
  2146. item = pagure.lib.model.TokenAcl(
  2147. token_id="foobar_token", acl_id=acl.id
  2148. )
  2149. self.session.add(item)
  2150. self.session.commit()
  2151. headers = {"Authorization": "token foobar_token"}
  2152. # User not admin
  2153. output = self.app.post(
  2154. "/api/0/test4/pull-request/1/merge", headers=headers
  2155. )
  2156. self.assertEqual(output.status_code, 404)
  2157. data = json.loads(output.get_data(as_text=True))
  2158. self.assertDictEqual(
  2159. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2160. )
  2161. headers = {"Authorization": "token aaabbbcccddd"}
  2162. # Merge PR
  2163. output = self.app.post(
  2164. "/api/0/test4/pull-request/1/merge", headers=headers
  2165. )
  2166. self.assertEqual(output.status_code, 200)
  2167. data = json.loads(output.get_data(as_text=True))
  2168. self.assertDictEqual(data, {"message": "Changes merged!"})
  2169. def test_api_private_repo_new_issue(self):
  2170. """ Test the api_new_issue method of the flask api. """
  2171. # Add private repo
  2172. item = pagure.lib.model.Project(
  2173. user_id=1, # pingou
  2174. name="test4",
  2175. description="test project description",
  2176. hook_token="aaabbbeeeceee",
  2177. private=True,
  2178. )
  2179. self.session.add(item)
  2180. self.session.commit()
  2181. for repo in ["GIT_FOLDER", "TICKETS_FOLDER"]:
  2182. # Add a git repo
  2183. repo_path = os.path.join(
  2184. pagure.config.config.get(repo), "test4.git"
  2185. )
  2186. if not os.path.exists(repo_path):
  2187. os.makedirs(repo_path)
  2188. pygit2.init_repository(repo_path, bare=True)
  2189. tests.create_tokens(self.session)
  2190. tests.create_tokens_acl(self.session)
  2191. # Add private repo
  2192. item = pagure.lib.model.Project(
  2193. user_id=1, # pingou
  2194. name="test2",
  2195. description="test project description",
  2196. hook_token="foo_bar",
  2197. private=True,
  2198. )
  2199. self.session.add(item)
  2200. self.session.commit()
  2201. headers = {"Authorization": "token aaabbbcccddd"}
  2202. # Valid token, wrong project
  2203. output = self.app.post("/api/0/test2/new_issue", headers=headers)
  2204. self.assertEqual(output.status_code, 401)
  2205. data = json.loads(output.get_data(as_text=True))
  2206. self.assertEqual(sorted(data.keys()), ["error", "error_code"])
  2207. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  2208. self.assertEqual(
  2209. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  2210. )
  2211. # No input
  2212. output = self.app.post("/api/0/test4/new_issue", headers=headers)
  2213. self.assertEqual(output.status_code, 400)
  2214. data = json.loads(output.get_data(as_text=True))
  2215. self.assertDictEqual(
  2216. data,
  2217. {
  2218. "error": "Invalid or incomplete input submitted",
  2219. "error_code": "EINVALIDREQ",
  2220. "errors": {
  2221. "issue_content": ["This field is required."],
  2222. "title": ["This field is required."],
  2223. },
  2224. },
  2225. )
  2226. data = {"title": "test issue"}
  2227. # Invalid repo
  2228. output = self.app.post(
  2229. "/api/0/foo/new_issue", data=data, headers=headers
  2230. )
  2231. self.assertEqual(output.status_code, 404)
  2232. data = json.loads(output.get_data(as_text=True))
  2233. self.assertDictEqual(
  2234. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2235. )
  2236. # Incomplete request
  2237. output = self.app.post(
  2238. "/api/0/test4/new_issue", data=data, headers=headers
  2239. )
  2240. self.assertEqual(output.status_code, 400)
  2241. data = json.loads(output.get_data(as_text=True))
  2242. self.assertDictEqual(
  2243. data,
  2244. {
  2245. "error": "Invalid or incomplete input submitted",
  2246. "error_code": "EINVALIDREQ",
  2247. "errors": {
  2248. "issue_content": ["This field is required."],
  2249. "title": ["This field is required."],
  2250. },
  2251. },
  2252. )
  2253. data = {
  2254. "title": "test issue",
  2255. "issue_content": "This issue needs attention",
  2256. }
  2257. # Valid request
  2258. output = self.app.post(
  2259. "/api/0/test4/new_issue", data=data, headers=headers
  2260. )
  2261. self.assertEqual(output.status_code, 200)
  2262. data = json.loads(output.get_data(as_text=True))
  2263. data["issue"]["date_created"] = "1431414800"
  2264. data["issue"]["last_updated"] = "1431414800"
  2265. self.assertDictEqual(
  2266. data, {"issue": FULL_ISSUE_LIST[7], "message": "Issue created"}
  2267. )
  2268. def test_api_private_repo_view_issues(self):
  2269. """ Test the api_view_issues method of the flask api. """
  2270. self.test_api_private_repo_new_issue()
  2271. # Invalid repo
  2272. output = self.app.get("/api/0/foo/issues")
  2273. self.assertEqual(output.status_code, 404)
  2274. data = json.loads(output.get_data(as_text=True))
  2275. self.assertDictEqual(
  2276. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2277. )
  2278. # List all opened issues
  2279. user = tests.FakeUser(username="pingou")
  2280. with tests.user_set(self.app.application, user):
  2281. output = self.app.get("/api/0/test4/issues")
  2282. self.assertEqual(output.status_code, 200)
  2283. data = json.loads(output.get_data(as_text=True))
  2284. data["issues"][0]["date_created"] = "1431414800"
  2285. data["issues"][0]["last_updated"] = "1431414800"
  2286. for k in ["first", "last"]:
  2287. self.assertIsNotNone(data["pagination"][k])
  2288. data["pagination"][k] = "http://localhost..."
  2289. self.assertDictEqual(
  2290. data,
  2291. {
  2292. "args": {
  2293. "assignee": None,
  2294. "author": None,
  2295. "milestones": [],
  2296. "no_stones": None,
  2297. "order": None,
  2298. "priority": None,
  2299. "since": None,
  2300. "status": None,
  2301. "tags": [],
  2302. },
  2303. "total_issues": 1,
  2304. "issues": [
  2305. {
  2306. "assignee": None,
  2307. "blocks": [],
  2308. "close_status": None,
  2309. "closed_at": None,
  2310. "closed_by": None,
  2311. "comments": [],
  2312. "content": "This issue needs attention",
  2313. "custom_fields": [],
  2314. "date_created": "1431414800",
  2315. "last_updated": "1431414800",
  2316. "depends": [],
  2317. "id": 1,
  2318. "milestone": None,
  2319. "priority": None,
  2320. "private": False,
  2321. "related_prs": [],
  2322. "status": "Open",
  2323. "tags": [],
  2324. "title": "test issue",
  2325. "user": {
  2326. "fullname": "PY C",
  2327. "name": "pingou",
  2328. "url_path": "user/pingou",
  2329. },
  2330. }
  2331. ],
  2332. "pagination": {
  2333. "first": "http://localhost...",
  2334. "last": "http://localhost...",
  2335. "next": None,
  2336. "page": 1,
  2337. "pages": 1,
  2338. "per_page": 20,
  2339. "prev": None,
  2340. },
  2341. },
  2342. )
  2343. # Create private issue
  2344. repo = pagure.lib.query._get_project(self.session, "test4")
  2345. msg = pagure.lib.query.new_issue(
  2346. session=self.session,
  2347. repo=repo,
  2348. title="Test issue",
  2349. content="We should work on this",
  2350. user="pingou",
  2351. private=True,
  2352. )
  2353. self.session.commit()
  2354. self.assertEqual(msg.title, "Test issue")
  2355. # Private issues are retrieved
  2356. user = tests.FakeUser(username="pingou")
  2357. with tests.user_set(self.app.application, user):
  2358. output = self.app.get("/api/0/test4/issues")
  2359. self.assertEqual(output.status_code, 200)
  2360. data = json.loads(output.get_data(as_text=True))
  2361. data["issues"][0]["date_created"] = "1431414800"
  2362. data["issues"][0]["last_updated"] = "1431414800"
  2363. data["issues"][1]["date_created"] = "1431414800"
  2364. data["issues"][1]["last_updated"] = "1431414800"
  2365. for k in ["first", "last"]:
  2366. self.assertIsNotNone(data["pagination"][k])
  2367. data["pagination"][k] = "http://localhost..."
  2368. self.assertDictEqual(
  2369. data,
  2370. {
  2371. "args": {
  2372. "assignee": None,
  2373. "author": None,
  2374. "milestones": [],
  2375. "no_stones": None,
  2376. "order": None,
  2377. "priority": None,
  2378. "status": None,
  2379. "since": None,
  2380. "tags": [],
  2381. },
  2382. "issues": [
  2383. {
  2384. "assignee": None,
  2385. "blocks": [],
  2386. "close_status": None,
  2387. "closed_at": None,
  2388. "closed_by": None,
  2389. "comments": [],
  2390. "content": "We should work on this",
  2391. "custom_fields": [],
  2392. "date_created": "1431414800",
  2393. "last_updated": "1431414800",
  2394. "depends": [],
  2395. "id": 2,
  2396. "milestone": None,
  2397. "priority": None,
  2398. "private": True,
  2399. "related_prs": [],
  2400. "status": "Open",
  2401. "tags": [],
  2402. "title": "Test issue",
  2403. "user": {
  2404. "fullname": "PY C",
  2405. "name": "pingou",
  2406. "url_path": "user/pingou",
  2407. },
  2408. },
  2409. {
  2410. "assignee": None,
  2411. "blocks": [],
  2412. "close_status": None,
  2413. "closed_at": None,
  2414. "closed_by": None,
  2415. "comments": [],
  2416. "content": "This issue needs attention",
  2417. "custom_fields": [],
  2418. "date_created": "1431414800",
  2419. "last_updated": "1431414800",
  2420. "depends": [],
  2421. "id": 1,
  2422. "milestone": None,
  2423. "priority": None,
  2424. "private": False,
  2425. "related_prs": [],
  2426. "status": "Open",
  2427. "tags": [],
  2428. "title": "test issue",
  2429. "user": {
  2430. "fullname": "PY C",
  2431. "name": "pingou",
  2432. "url_path": "user/pingou",
  2433. },
  2434. },
  2435. ],
  2436. "pagination": {
  2437. "first": "http://localhost...",
  2438. "last": "http://localhost...",
  2439. "next": None,
  2440. "page": 1,
  2441. "pages": 1,
  2442. "per_page": 20,
  2443. "prev": None,
  2444. },
  2445. "total_issues": 2,
  2446. },
  2447. )
  2448. # Access issues authenticated but non-existing token
  2449. headers = {"Authorization": "token aaabbbccc"}
  2450. output = self.app.get("/api/0/test4/issues", headers=headers)
  2451. self.assertEqual(output.status_code, 401)
  2452. headers = {"Authorization": "token aaabbbcccddd"}
  2453. # Access issues authenticated correctly
  2454. output = self.app.get("/api/0/test4/issues", headers=headers)
  2455. self.assertEqual(output.status_code, 200)
  2456. data = json.loads(output.get_data(as_text=True))
  2457. data["issues"][0]["date_created"] = "1431414800"
  2458. data["issues"][0]["last_updated"] = "1431414800"
  2459. data["issues"][1]["date_created"] = "1431414800"
  2460. data["issues"][1]["last_updated"] = "1431414800"
  2461. for k in ["first", "last"]:
  2462. self.assertIsNotNone(data["pagination"][k])
  2463. data["pagination"][k] = "http://localhost..."
  2464. self.assertDictEqual(
  2465. data,
  2466. {
  2467. "args": {
  2468. "assignee": None,
  2469. "author": None,
  2470. "milestones": [],
  2471. "no_stones": None,
  2472. "order": None,
  2473. "priority": None,
  2474. "status": None,
  2475. "since": None,
  2476. "tags": [],
  2477. },
  2478. "issues": [
  2479. {
  2480. "assignee": None,
  2481. "blocks": [],
  2482. "close_status": None,
  2483. "closed_at": None,
  2484. "closed_by": None,
  2485. "comments": [],
  2486. "content": "We should work on this",
  2487. "custom_fields": [],
  2488. "date_created": "1431414800",
  2489. "last_updated": "1431414800",
  2490. "depends": [],
  2491. "id": 2,
  2492. "milestone": None,
  2493. "priority": None,
  2494. "private": True,
  2495. "related_prs": [],
  2496. "status": "Open",
  2497. "tags": [],
  2498. "title": "Test issue",
  2499. "user": {
  2500. "fullname": "PY C",
  2501. "name": "pingou",
  2502. "url_path": "user/pingou",
  2503. },
  2504. },
  2505. {
  2506. "assignee": None,
  2507. "blocks": [],
  2508. "close_status": None,
  2509. "closed_at": None,
  2510. "closed_by": None,
  2511. "comments": [],
  2512. "content": "This issue needs attention",
  2513. "custom_fields": [],
  2514. "date_created": "1431414800",
  2515. "last_updated": "1431414800",
  2516. "depends": [],
  2517. "id": 1,
  2518. "milestone": None,
  2519. "priority": None,
  2520. "private": False,
  2521. "related_prs": [],
  2522. "status": "Open",
  2523. "tags": [],
  2524. "title": "test issue",
  2525. "user": {
  2526. "fullname": "PY C",
  2527. "name": "pingou",
  2528. "url_path": "user/pingou",
  2529. },
  2530. },
  2531. ],
  2532. "pagination": {
  2533. "first": "http://localhost...",
  2534. "last": "http://localhost...",
  2535. "next": None,
  2536. "page": 1,
  2537. "pages": 1,
  2538. "per_page": 20,
  2539. "prev": None,
  2540. },
  2541. "total_issues": 2,
  2542. },
  2543. )
  2544. # List closed issue
  2545. output = self.app.get(
  2546. "/api/0/test4/issues?status=Closed", headers=headers
  2547. )
  2548. self.assertEqual(output.status_code, 200)
  2549. data = json.loads(output.get_data(as_text=True))
  2550. for k in ["first", "last"]:
  2551. self.assertIsNotNone(data["pagination"][k])
  2552. data["pagination"][k] = "http://localhost..."
  2553. self.assertDictEqual(
  2554. data,
  2555. {
  2556. "args": {
  2557. "assignee": None,
  2558. "author": None,
  2559. "milestones": [],
  2560. "no_stones": None,
  2561. "order": None,
  2562. "priority": None,
  2563. "status": "Closed",
  2564. "since": None,
  2565. "tags": [],
  2566. },
  2567. "issues": [],
  2568. "pagination": {
  2569. "first": "http://localhost...",
  2570. "last": "http://localhost...",
  2571. "next": None,
  2572. "page": 1,
  2573. "pages": 0,
  2574. "per_page": 20,
  2575. "prev": None,
  2576. },
  2577. "total_issues": 0,
  2578. },
  2579. )
  2580. # List closed issue
  2581. output = self.app.get(
  2582. "/api/0/test4/issues?status=Invalid", headers=headers
  2583. )
  2584. self.assertEqual(output.status_code, 200)
  2585. data = json.loads(output.get_data(as_text=True))
  2586. for k in ["first", "last"]:
  2587. self.assertIsNotNone(data["pagination"][k])
  2588. data["pagination"][k] = "http://localhost..."
  2589. self.assertDictEqual(
  2590. data,
  2591. {
  2592. "args": {
  2593. "assignee": None,
  2594. "author": None,
  2595. "milestones": [],
  2596. "no_stones": None,
  2597. "order": None,
  2598. "priority": None,
  2599. "status": "Invalid",
  2600. "since": None,
  2601. "tags": [],
  2602. },
  2603. "issues": [],
  2604. "pagination": {
  2605. "first": "http://localhost...",
  2606. "last": "http://localhost...",
  2607. "next": None,
  2608. "page": 1,
  2609. "pages": 0,
  2610. "per_page": 20,
  2611. "prev": None,
  2612. },
  2613. "total_issues": 0,
  2614. },
  2615. )
  2616. # List all issues
  2617. output = self.app.get(
  2618. "/api/0/test4/issues?status=All", headers=headers
  2619. )
  2620. self.assertEqual(output.status_code, 200)
  2621. data = json.loads(output.get_data(as_text=True))
  2622. data["issues"][0]["date_created"] = "1431414800"
  2623. data["issues"][0]["last_updated"] = "1431414800"
  2624. data["issues"][1]["date_created"] = "1431414800"
  2625. data["issues"][1]["last_updated"] = "1431414800"
  2626. for k in ["first", "last"]:
  2627. self.assertIsNotNone(data["pagination"][k])
  2628. data["pagination"][k] = "http://localhost..."
  2629. self.assertDictEqual(
  2630. data,
  2631. {
  2632. "args": {
  2633. "assignee": None,
  2634. "author": None,
  2635. "milestones": [],
  2636. "no_stones": None,
  2637. "order": None,
  2638. "priority": None,
  2639. "since": None,
  2640. "status": "All",
  2641. "tags": [],
  2642. },
  2643. "issues": [
  2644. {
  2645. "assignee": None,
  2646. "blocks": [],
  2647. "close_status": None,
  2648. "closed_at": None,
  2649. "closed_by": None,
  2650. "comments": [],
  2651. "content": "We should work on this",
  2652. "custom_fields": [],
  2653. "date_created": "1431414800",
  2654. "last_updated": "1431414800",
  2655. "depends": [],
  2656. "id": 2,
  2657. "milestone": None,
  2658. "priority": None,
  2659. "private": True,
  2660. "related_prs": [],
  2661. "status": "Open",
  2662. "tags": [],
  2663. "title": "Test issue",
  2664. "user": {
  2665. "fullname": "PY C",
  2666. "name": "pingou",
  2667. "url_path": "user/pingou",
  2668. },
  2669. },
  2670. {
  2671. "assignee": None,
  2672. "blocks": [],
  2673. "close_status": None,
  2674. "closed_at": None,
  2675. "closed_by": None,
  2676. "comments": [],
  2677. "content": "This issue needs attention",
  2678. "custom_fields": [],
  2679. "date_created": "1431414800",
  2680. "last_updated": "1431414800",
  2681. "depends": [],
  2682. "id": 1,
  2683. "milestone": None,
  2684. "priority": None,
  2685. "private": False,
  2686. "related_prs": [],
  2687. "status": "Open",
  2688. "tags": [],
  2689. "title": "test issue",
  2690. "user": {
  2691. "fullname": "PY C",
  2692. "name": "pingou",
  2693. "url_path": "user/pingou",
  2694. },
  2695. },
  2696. ],
  2697. "pagination": {
  2698. "first": "http://localhost...",
  2699. "last": "http://localhost...",
  2700. "next": None,
  2701. "page": 1,
  2702. "pages": 1,
  2703. "per_page": 20,
  2704. "prev": None,
  2705. },
  2706. "total_issues": 2,
  2707. },
  2708. )
  2709. def test_api_pivate_repo_view_issue(self):
  2710. """ Test the api_view_issue method of the flask api. """
  2711. self.test_api_private_repo_new_issue()
  2712. # Invalid repo
  2713. output = self.app.get("/api/0/foo/issue/1")
  2714. self.assertEqual(output.status_code, 404)
  2715. data = json.loads(output.get_data(as_text=True))
  2716. self.assertDictEqual(
  2717. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2718. )
  2719. # Invalid issue for this repo
  2720. output = self.app.get("/api/0/test4/issue/1")
  2721. self.assertEqual(output.status_code, 404)
  2722. data = json.loads(output.get_data(as_text=True))
  2723. self.assertDictEqual(
  2724. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2725. )
  2726. # Un-authorized user
  2727. user = tests.FakeUser()
  2728. with tests.user_set(self.app.application, user):
  2729. output = self.app.get("/api/0/test4/issue/1")
  2730. self.assertEqual(output.status_code, 404)
  2731. data = json.loads(output.get_data(as_text=True))
  2732. self.assertDictEqual(
  2733. data,
  2734. {"error": "Project not found", "error_code": "ENOPROJECT"},
  2735. )
  2736. # Valid issue
  2737. user = tests.FakeUser(username="pingou")
  2738. with tests.user_set(self.app.application, user):
  2739. output = self.app.get("/api/0/test4/issue/1")
  2740. self.assertEqual(output.status_code, 200)
  2741. data = json.loads(output.get_data(as_text=True))
  2742. data["date_created"] = "1431414800"
  2743. data["last_updated"] = "1431414800"
  2744. self.assertDictEqual(
  2745. data,
  2746. {
  2747. "assignee": None,
  2748. "blocks": [],
  2749. "close_status": None,
  2750. "closed_at": None,
  2751. "closed_by": None,
  2752. "comments": [],
  2753. "content": "This issue needs attention",
  2754. "custom_fields": [],
  2755. "date_created": "1431414800",
  2756. "depends": [],
  2757. "id": 1,
  2758. "last_updated": "1431414800",
  2759. "milestone": None,
  2760. "priority": None,
  2761. "private": False,
  2762. "related_prs": [],
  2763. "status": "Open",
  2764. "tags": [],
  2765. "title": "test issue",
  2766. "user": {
  2767. "fullname": "PY C",
  2768. "name": "pingou",
  2769. "url_path": "user/pingou",
  2770. },
  2771. },
  2772. )
  2773. headers = {"Authorization": "token aaabbbccc"}
  2774. # Access issue authenticated but non-existing token
  2775. output = self.app.get("/api/0/test4/issue/1", headers=headers)
  2776. self.assertEqual(output.status_code, 401)
  2777. data = json.loads(output.get_data(as_text=True))
  2778. self.assertEqual(
  2779. sorted(data.keys()), ["error", "error_code", "errors"]
  2780. )
  2781. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  2782. self.assertEqual(
  2783. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  2784. )
  2785. self.assertEqual(data["errors"], "Invalid token")
  2786. headers = {"Authorization": "token aaabbbcccddd"}
  2787. # Access issue authenticated correctly
  2788. output = self.app.get("/api/0/test4/issue/1", headers=headers)
  2789. self.assertEqual(output.status_code, 200)
  2790. data = json.loads(output.get_data(as_text=True))
  2791. data["date_created"] = "1431414800"
  2792. data["last_updated"] = "1431414800"
  2793. self.assertDictEqual(
  2794. data,
  2795. {
  2796. "assignee": None,
  2797. "blocks": [],
  2798. "close_status": None,
  2799. "closed_at": None,
  2800. "closed_by": None,
  2801. "comments": [],
  2802. "content": "This issue needs attention",
  2803. "custom_fields": [],
  2804. "date_created": "1431414800",
  2805. "depends": [],
  2806. "id": 1,
  2807. "last_updated": "1431414800",
  2808. "milestone": None,
  2809. "priority": None,
  2810. "private": False,
  2811. "related_prs": [],
  2812. "status": "Open",
  2813. "tags": [],
  2814. "title": "test issue",
  2815. "user": {
  2816. "fullname": "PY C",
  2817. "name": "pingou",
  2818. "url_path": "user/pingou",
  2819. },
  2820. },
  2821. )
  2822. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2823. def test_api_private_repo_change_status_issue(self):
  2824. """ Test the api_change_status_issue method of the flask api. """
  2825. item = pagure.lib.model.Project(
  2826. user_id=1, # pingou
  2827. name="test4",
  2828. description="test project description",
  2829. hook_token="aaabbbeeeceee",
  2830. private=True,
  2831. )
  2832. item.close_status = [
  2833. "Invalid",
  2834. "Insufficient data",
  2835. "Fixed",
  2836. "Duplicate",
  2837. ]
  2838. self.session.add(item)
  2839. self.session.commit()
  2840. for repo in ["GIT_FOLDER", "TICKETS_FOLDER"]:
  2841. # Add a git repo
  2842. repo_path = os.path.join(
  2843. pagure.config.config.get(repo), "test4.git"
  2844. )
  2845. if not os.path.exists(repo_path):
  2846. os.makedirs(repo_path)
  2847. pygit2.init_repository(repo_path, bare=True)
  2848. tests.create_tokens(self.session)
  2849. tests.create_tokens_acl(self.session)
  2850. headers = {"Authorization": "token aaabbbcccddd"}
  2851. # Invalid project
  2852. output = self.app.post("/api/0/foo/issue/1/status", headers=headers)
  2853. self.assertEqual(output.status_code, 404)
  2854. data = json.loads(output.get_data(as_text=True))
  2855. self.assertDictEqual(
  2856. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2857. )
  2858. # Valid token, wrong project
  2859. user = tests.FakeUser(username="pingou")
  2860. with tests.user_set(self.app.application, user):
  2861. output = self.app.post(
  2862. "/api/0/test2/issue/1/status", headers=headers
  2863. )
  2864. self.assertEqual(output.status_code, 404)
  2865. data = json.loads(output.get_data(as_text=True))
  2866. self.assertDictEqual(
  2867. data,
  2868. {"error": "Project not found", "error_code": "ENOPROJECT"},
  2869. )
  2870. # No input
  2871. output = self.app.post("/api/0/test4/issue/1/status", headers=headers)
  2872. self.assertEqual(output.status_code, 404)
  2873. data = json.loads(output.get_data(as_text=True))
  2874. self.assertDictEqual(
  2875. data, {"error": "Issue not found", "error_code": "ENOISSUE"}
  2876. )
  2877. # Create normal issue
  2878. repo = pagure.lib.query._get_project(self.session, "test4")
  2879. msg = pagure.lib.query.new_issue(
  2880. session=self.session,
  2881. repo=repo,
  2882. title="Test issue #1",
  2883. content="We should work on this",
  2884. user="pingou",
  2885. private=False,
  2886. )
  2887. self.session.commit()
  2888. self.assertEqual(msg.title, "Test issue #1")
  2889. # Check status before
  2890. repo = pagure.lib.query._get_project(self.session, "test4")
  2891. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2892. self.assertEqual(issue.status, "Open")
  2893. data = {"title": "test issue"}
  2894. user = tests.FakeUser(username="pingou")
  2895. with tests.user_set(self.app.application, user):
  2896. # Incomplete request
  2897. output = self.app.post(
  2898. "/api/0/test4/issue/1/status", data=data, headers=headers
  2899. )
  2900. self.assertEqual(output.status_code, 400)
  2901. data = json.loads(output.get_data(as_text=True))
  2902. self.assertDictEqual(
  2903. data,
  2904. {
  2905. "error": "Invalid or incomplete input submitted",
  2906. "error_code": "EINVALIDREQ",
  2907. "errors": {"status": ["Not a valid choice"]},
  2908. },
  2909. )
  2910. # No change
  2911. repo = pagure.lib.query._get_project(self.session, "test4")
  2912. issue = pagure.lib.query.search_issues(
  2913. self.session, repo, issueid=1
  2914. )
  2915. self.assertEqual(issue.status, "Open")
  2916. data = {"status": "Open"}
  2917. # Valid request but no change
  2918. output = self.app.post(
  2919. "/api/0/test4/issue/1/status", data=data, headers=headers
  2920. )
  2921. self.assertEqual(output.status_code, 200)
  2922. data = json.loads(output.get_data(as_text=True))
  2923. self.assertDictEqual(data, {"message": "No changes"})
  2924. # No change
  2925. repo = pagure.lib.query._get_project(self.session, "test4")
  2926. issue = pagure.lib.query.search_issues(
  2927. self.session, repo, issueid=1
  2928. )
  2929. self.assertEqual(issue.status, "Open")
  2930. data = {"status": "Fixed"}
  2931. # Valid request
  2932. output = self.app.post(
  2933. "/api/0/test4/issue/1/status", data=data, headers=headers
  2934. )
  2935. self.assertEqual(output.status_code, 200)
  2936. data = json.loads(output.get_data(as_text=True))
  2937. self.assertDictEqual(
  2938. data,
  2939. {
  2940. "message": [
  2941. "Issue status updated to: Closed (was: Open)",
  2942. "Issue close_status updated to: Fixed",
  2943. ]
  2944. },
  2945. )
  2946. @patch("pagure.lib.git.update_git")
  2947. @patch("pagure.lib.notify.send_email")
  2948. def test_api_private_repo_comment_issue(self, p_send_email, p_ugt):
  2949. """ Test the api_comment_issue method of the flask api. """
  2950. p_send_email.return_value = True
  2951. p_ugt.return_value = True
  2952. item = pagure.lib.model.Project(
  2953. user_id=1, # pingou
  2954. name="test4",
  2955. description="test project description",
  2956. hook_token="aaabbbeeeceee",
  2957. private=True,
  2958. )
  2959. self.session.add(item)
  2960. self.session.commit()
  2961. tests.create_tokens(self.session)
  2962. tests.create_tokens_acl(self.session)
  2963. headers = {"Authorization": "token aaabbbcccddd"}
  2964. # Invalid project
  2965. output = self.app.post("/api/0/foo/issue/1/comment", headers=headers)
  2966. self.assertEqual(output.status_code, 404)
  2967. data = json.loads(output.get_data(as_text=True))
  2968. self.assertDictEqual(
  2969. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  2970. )
  2971. # Invalid token, right project
  2972. headers = {"Authorization": "token aaabbbccc"}
  2973. output = self.app.post("/api/0/test4/issue/1/comment", headers=headers)
  2974. self.assertEqual(output.status_code, 401)
  2975. data = json.loads(output.get_data(as_text=True))
  2976. self.assertEqual(
  2977. sorted(data.keys()), ["error", "error_code", "errors"]
  2978. )
  2979. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  2980. self.assertEqual(
  2981. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  2982. )
  2983. self.assertEqual(data["errors"], "Invalid token")
  2984. headers = {"Authorization": "token aaabbbcccddd"}
  2985. # No input
  2986. output = self.app.post("/api/0/test4/issue/1/comment", headers=headers)
  2987. self.assertEqual(output.status_code, 404)
  2988. data = json.loads(output.get_data(as_text=True))
  2989. self.assertDictEqual(
  2990. data, {"error": "Issue not found", "error_code": "ENOISSUE"}
  2991. )
  2992. # Create normal issue
  2993. repo = pagure.lib.query._get_project(self.session, "test4")
  2994. msg = pagure.lib.query.new_issue(
  2995. session=self.session,
  2996. repo=repo,
  2997. title="Test issue #1",
  2998. content="We should work on this",
  2999. user="pingou",
  3000. private=False,
  3001. issue_uid="aaabbbccc1",
  3002. )
  3003. self.session.commit()
  3004. self.assertEqual(msg.title, "Test issue #1")
  3005. # Check comments before
  3006. self.session.commit()
  3007. repo = pagure.lib.query._get_project(self.session, "test4")
  3008. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  3009. self.assertEqual(len(issue.comments), 0)
  3010. data = {"title": "test issue"}
  3011. # Incomplete request
  3012. output = self.app.post(
  3013. "/api/0/test4/issue/1/comment", data=data, headers=headers
  3014. )
  3015. self.assertEqual(output.status_code, 400)
  3016. data = json.loads(output.get_data(as_text=True))
  3017. self.assertDictEqual(
  3018. data,
  3019. {
  3020. "error": "Invalid or incomplete input submitted",
  3021. "error_code": "EINVALIDREQ",
  3022. "errors": {"comment": ["This field is required."]},
  3023. },
  3024. )
  3025. # No change
  3026. self.session.commit()
  3027. repo = pagure.lib.query._get_project(self.session, "test4")
  3028. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  3029. self.assertEqual(issue.status, "Open")
  3030. data = {"comment": "This is a very interesting question"}
  3031. # Valid request
  3032. output = self.app.post(
  3033. "/api/0/test4/issue/1/comment", data=data, headers=headers
  3034. )
  3035. self.assertEqual(output.status_code, 200)
  3036. data = json.loads(output.get_data(as_text=True))
  3037. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  3038. self.assertDictEqual(
  3039. data,
  3040. {
  3041. "message": "Comment added",
  3042. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  3043. "user": "pingou",
  3044. },
  3045. )
  3046. # One comment added
  3047. self.session.commit()
  3048. repo = pagure.lib.query._get_project(self.session, "test4")
  3049. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  3050. self.assertEqual(len(issue.comments), 1)
  3051. @patch("pagure.lib.git.update_git")
  3052. @patch("pagure.lib.notify.send_email")
  3053. def test_api_view_issue_comment(self, p_send_email, p_ugt):
  3054. """ Test the api_view_issue_comment endpoint. """
  3055. p_send_email.return_value = True
  3056. p_ugt.return_value = True
  3057. self.test_api_private_repo_comment_issue()
  3058. # View a comment that does not exist
  3059. output = self.app.get("/api/0/foo/issue/100/comment/2")
  3060. self.assertEqual(output.status_code, 404)
  3061. # Issue exists but not the comment
  3062. output = self.app.get("/api/0/test/issue/1/comment/2")
  3063. self.assertEqual(output.status_code, 404)
  3064. # Issue and comment exists
  3065. output = self.app.get("/api/0/test/issue/1/comment/1")
  3066. self.assertEqual(output.status_code, 404)
  3067. user = tests.FakeUser(username="pingou")
  3068. with tests.user_set(self.app.application, user):
  3069. output = self.app.get("/api/0/test4/issue/1/comment/1")
  3070. self.assertEqual(output.status_code, 200)
  3071. data = json.loads(output.get_data(as_text=True))
  3072. data["date_created"] = "1435821770"
  3073. data["comment_date"] = "2015-07-02 09:22"
  3074. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  3075. self.assertDictEqual(
  3076. data,
  3077. {
  3078. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  3079. "comment": "This is a very interesting question",
  3080. "comment_date": "2015-07-02 09:22",
  3081. "notification": False,
  3082. "date_created": "1435821770",
  3083. "edited_on": None,
  3084. "editor": None,
  3085. "id": 1,
  3086. "parent": None,
  3087. "reactions": {},
  3088. "user": {
  3089. "fullname": "PY C",
  3090. "name": "pingou",
  3091. "url_path": "user/pingou",
  3092. },
  3093. },
  3094. )
  3095. # Issue and comment exists, using UID
  3096. output = self.app.get("/api/0/test4/issue/aaabbbccc1/comment/1")
  3097. self.assertEqual(output.status_code, 200)
  3098. data = json.loads(output.get_data(as_text=True))
  3099. data["date_created"] = "1435821770"
  3100. data["comment_date"] = "2015-07-02 09:22"
  3101. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  3102. self.assertDictEqual(
  3103. data,
  3104. {
  3105. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  3106. "comment": "This is a very interesting question",
  3107. "comment_date": "2015-07-02 09:22",
  3108. "notification": False,
  3109. "date_created": "1435821770",
  3110. "edited_on": None,
  3111. "editor": None,
  3112. "id": 1,
  3113. "parent": None,
  3114. "reactions": {},
  3115. "user": {
  3116. "fullname": "PY C",
  3117. "name": "pingou",
  3118. "url_path": "user/pingou",
  3119. },
  3120. },
  3121. )
  3122. if __name__ == "__main__":
  3123. unittest.main(verbosity=2)