test_pagure_flask_api_project.py 141 KB

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