test_pagure_admin.py 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime # noqa
  9. import os # noqa
  10. import platform # noqa
  11. import shutil # noqa
  12. import subprocess # noqa
  13. import sys # noqa
  14. import unittest # noqa
  15. import pygit2
  16. import munch # noqa
  17. from mock import patch, MagicMock # noqa
  18. from six import StringIO # noqa
  19. sys.path.insert(
  20. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  21. )
  22. import pagure.config # noqa
  23. import pagure.exceptions # noqa: E402
  24. import pagure.cli.admin # noqa
  25. import pagure.lib.model # noqa
  26. import tests # noqa
  27. class PagureAdminAdminTokenEmptytests(tests.Modeltests):
  28. """Tests for pagure-admin admin-token when there is nothing in the DB"""
  29. populate_db = False
  30. def setUp(self):
  31. """ Set up the environnment, ran before every tests. """
  32. super(PagureAdminAdminTokenEmptytests, self).setUp()
  33. pagure.cli.admin.session = self.session
  34. def test_do_create_admin_token_no_user(self):
  35. """Test the do_create_admin_token function of pagure-admin without
  36. user.
  37. """
  38. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  39. args = munch.Munch(
  40. {
  41. "user": "pingou",
  42. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  43. }
  44. )
  45. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  46. pagure.cli.admin.do_create_admin_token(args)
  47. self.assertEqual(cm.exception.args[0], 'No user "pingou" found')
  48. def test_do_list_admin_token_empty(self):
  49. """Test the do_list_admin_token function of pagure-admin when there
  50. are not tokens in the db.
  51. """
  52. list_args = munch.Munch(
  53. {
  54. "user": None,
  55. "token": None,
  56. "active": False,
  57. "expired": False,
  58. "all": False,
  59. }
  60. )
  61. with tests.capture_output() as output:
  62. pagure.cli.admin.do_list_admin_token(list_args)
  63. output = output.getvalue()
  64. self.assertEqual(output, "No admin tokens found\n")
  65. class PagureAdminAdminRefreshGitolitetests(tests.Modeltests):
  66. """ Tests for pagure-admin refresh-gitolite """
  67. populate_db = False
  68. def setUp(self):
  69. """ Set up the environnment, ran before every tests. """
  70. super(PagureAdminAdminRefreshGitolitetests, self).setUp()
  71. pagure.cli.admin.session = self.session
  72. # Create the user pingou
  73. item = pagure.lib.model.User(
  74. user="pingou",
  75. fullname="PY C",
  76. password="foo",
  77. default_email="bar@pingou.com",
  78. )
  79. self.session.add(item)
  80. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  81. self.session.add(item)
  82. self.session.commit()
  83. # Create a couple of projects
  84. tests.create_projects(self.session)
  85. # Add a group
  86. msg = pagure.lib.query.add_group(
  87. self.session,
  88. group_name="foo",
  89. display_name="foo group",
  90. description=None,
  91. group_type="bar",
  92. user="pingou",
  93. is_admin=False,
  94. blacklist=[],
  95. )
  96. self.session.commit()
  97. self.assertEqual(msg, "User `pingou` added to the group `foo`.")
  98. # Make the imported pagure use the correct db session
  99. pagure.cli.admin.session = self.session
  100. @patch("pagure.cli.admin._ask_confirmation")
  101. @patch("pagure.lib.git_auth.get_git_auth_helper")
  102. def test_do_refresh_gitolite_no_args(self, get_helper, conf):
  103. """ Test the do_generate_acl function with no special args. """
  104. conf.return_value = True
  105. helper = MagicMock()
  106. get_helper.return_value = helper
  107. args = munch.Munch(
  108. {"group": None, "project": None, "all_": False, "user": None}
  109. )
  110. pagure.cli.admin.do_generate_acl(args)
  111. get_helper.assert_called_with()
  112. args = helper.generate_acls.call_args
  113. self.assertIsNone(args[1].get("group"))
  114. self.assertIsNone(args[1].get("project"))
  115. @patch("pagure.cli.admin._ask_confirmation")
  116. @patch("pagure.lib.git_auth.get_git_auth_helper")
  117. def test_do_refresh_gitolite_all_project(self, get_helper, conf):
  118. """ Test the do_generate_acl function for all projects. """
  119. conf.return_value = True
  120. helper = MagicMock()
  121. get_helper.return_value = helper
  122. args = munch.Munch(
  123. {"group": None, "project": None, "all_": True, "user": None}
  124. )
  125. pagure.cli.admin.do_generate_acl(args)
  126. get_helper.assert_called_with()
  127. args = helper.generate_acls.call_args
  128. self.assertIsNone(args[1].get("group"))
  129. self.assertEqual(args[1].get("project"), -1)
  130. @patch("pagure.cli.admin._ask_confirmation")
  131. @patch("pagure.lib.git_auth.get_git_auth_helper")
  132. def test_do_refresh_gitolite_one_project(self, get_helper, conf):
  133. """ Test the do_generate_acl function for a certain project. """
  134. conf.return_value = True
  135. helper = MagicMock()
  136. get_helper.return_value = helper
  137. args = munch.Munch(
  138. {"group": None, "project": "test", "all_": False, "user": None}
  139. )
  140. pagure.cli.admin.do_generate_acl(args)
  141. get_helper.assert_called_with()
  142. args = helper.generate_acls.call_args
  143. self.assertIsNone(args[1].get("group"))
  144. self.assertEqual(args[1].get("project").fullname, "test")
  145. @patch("pagure.cli.admin._ask_confirmation")
  146. @patch("pagure.lib.git_auth.get_git_auth_helper")
  147. def test_do_refresh_gitolite_one_project_and_all(self, get_helper, conf):
  148. """Test the do_generate_acl function for a certain project and all."""
  149. conf.return_value = True
  150. helper = MagicMock()
  151. get_helper.return_value = helper
  152. args = munch.Munch(
  153. {"group": None, "project": "test", "all_": True, "user": None}
  154. )
  155. pagure.cli.admin.do_generate_acl(args)
  156. get_helper.assert_called_with()
  157. args = helper.generate_acls.call_args
  158. self.assertIsNone(args[1].get("group"))
  159. self.assertEqual(args[1].get("project"), -1)
  160. @patch("pagure.cli.admin._ask_confirmation")
  161. @patch("pagure.lib.git_auth.get_git_auth_helper")
  162. def test_do_refresh_gitolite_one_group(self, get_helper, conf):
  163. """ Test the do_generate_acl function for a certain group. """
  164. conf.return_value = True
  165. helper = MagicMock()
  166. get_helper.return_value = helper
  167. args = munch.Munch(
  168. {"group": "foo", "project": None, "all_": False, "user": None}
  169. )
  170. pagure.cli.admin.do_generate_acl(args)
  171. get_helper.assert_called_with()
  172. args = helper.generate_acls.call_args
  173. self.assertEqual(args[1].get("group").group_name, "foo")
  174. self.assertIsNone(args[1].get("project"))
  175. class PagureAdminAdminTokentests(tests.Modeltests):
  176. """ Tests for pagure-admin admin-token """
  177. populate_db = False
  178. def setUp(self):
  179. """ Set up the environnment, ran before every tests. """
  180. super(PagureAdminAdminTokentests, self).setUp()
  181. pagure.cli.admin.session = self.session
  182. # Create the user pingou
  183. item = pagure.lib.model.User(
  184. user="pingou",
  185. fullname="PY C",
  186. password="foo",
  187. default_email="bar@pingou.com",
  188. )
  189. self.session.add(item)
  190. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  191. self.session.add(item)
  192. self.session.commit()
  193. # Make the imported pagure use the correct db session
  194. pagure.cli.admin.session = self.session
  195. @patch("pagure.cli.admin._get_input")
  196. @patch("pagure.cli.admin._ask_confirmation")
  197. def test_do_create_admin_token(self, conf, rinp):
  198. """ Test the do_create_admin_token function of pagure-admin. """
  199. conf.return_value = True
  200. rinp.return_value = "1,2,3"
  201. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  202. args = munch.Munch(
  203. {
  204. "user": "pingou",
  205. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  206. }
  207. )
  208. pagure.cli.admin.do_create_admin_token(args)
  209. # Check the outcome
  210. list_args = munch.Munch(
  211. {
  212. "user": None,
  213. "token": None,
  214. "active": False,
  215. "expired": False,
  216. "all": False,
  217. }
  218. )
  219. with tests.capture_output() as output:
  220. pagure.cli.admin.do_list_admin_token(list_args)
  221. output = output.getvalue()
  222. self.assertNotEqual(output, 'No user "pingou" found\n')
  223. self.assertEqual(len(output.split("\n")), 2)
  224. self.assertIn(" -- pingou -- ", output)
  225. @patch("pagure.cli.admin._get_input")
  226. @patch("pagure.cli.admin._ask_confirmation")
  227. def test_do_list_admin_token(self, conf, rinp):
  228. """ Test the do_list_admin_token function of pagure-admin. """
  229. # Create an admin token to use
  230. conf.return_value = True
  231. rinp.return_value = "1,2,3"
  232. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  233. args = munch.Munch(
  234. {
  235. "user": "pingou",
  236. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  237. }
  238. )
  239. pagure.cli.admin.do_create_admin_token(args)
  240. # Retrieve all tokens
  241. list_args = munch.Munch(
  242. {
  243. "user": None,
  244. "token": None,
  245. "active": False,
  246. "expired": False,
  247. "all": False,
  248. }
  249. )
  250. with tests.capture_output() as output:
  251. pagure.cli.admin.do_list_admin_token(list_args)
  252. output = output.getvalue()
  253. self.assertNotEqual(output, 'No user "pingou" found\n')
  254. self.assertEqual(len(output.split("\n")), 2)
  255. self.assertIn(" -- pingou -- ", output)
  256. # Retrieve pfrields's tokens
  257. list_args = munch.Munch(
  258. {
  259. "user": "pfrields",
  260. "token": None,
  261. "active": False,
  262. "expired": False,
  263. "all": False,
  264. }
  265. )
  266. with tests.capture_output() as output:
  267. pagure.cli.admin.do_list_admin_token(list_args)
  268. output = output.getvalue()
  269. self.assertEqual(output, "No admin tokens found\n")
  270. def test_do_list_admin_token_non_admin_acls(self):
  271. """Test the do_list_admin_token function of pagure-admin for a token
  272. without any admin ACL."""
  273. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  274. pagure.lib.query.add_token_to_user(
  275. self.session,
  276. project=None,
  277. acls=["issue_assign", "pull_request_subscribe"],
  278. username="pingou",
  279. expiration_date=exp_date,
  280. )
  281. # Retrieve all admin tokens
  282. list_args = munch.Munch(
  283. {
  284. "user": None,
  285. "token": None,
  286. "active": False,
  287. "expired": False,
  288. "all": False,
  289. }
  290. )
  291. with tests.capture_output() as output:
  292. pagure.cli.admin.do_list_admin_token(list_args)
  293. output = output.getvalue()
  294. self.assertEqual(output, "No admin tokens found\n")
  295. # Retrieve all tokens
  296. list_args = munch.Munch(
  297. {
  298. "user": None,
  299. "token": None,
  300. "active": False,
  301. "expired": False,
  302. "all": True,
  303. }
  304. )
  305. with tests.capture_output() as output:
  306. pagure.cli.admin.do_list_admin_token(list_args)
  307. output = output.getvalue()
  308. self.assertNotEqual(output, 'No user "pingou" found\n')
  309. self.assertEqual(len(output.split("\n")), 2)
  310. self.assertIn(" -- pingou -- ", output)
  311. @patch("pagure.cli.admin._get_input")
  312. @patch("pagure.cli.admin._ask_confirmation")
  313. def test_do_info_admin_token(self, conf, rinp):
  314. """ Test the do_info_admin_token function of pagure-admin. """
  315. # Create an admin token to use
  316. conf.return_value = True
  317. rinp.return_value = "2,4,5"
  318. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  319. args = munch.Munch(
  320. {
  321. "user": "pingou",
  322. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  323. }
  324. )
  325. pagure.cli.admin.do_create_admin_token(args)
  326. # Retrieve the token
  327. list_args = munch.Munch(
  328. {
  329. "user": None,
  330. "token": None,
  331. "active": False,
  332. "expired": False,
  333. "all": False,
  334. }
  335. )
  336. with tests.capture_output() as output:
  337. pagure.cli.admin.do_list_admin_token(list_args)
  338. output = output.getvalue()
  339. self.assertNotEqual(output, 'No user "pingou" found\n')
  340. self.assertEqual(len(output.split("\n")), 2)
  341. self.assertIn(" -- pingou -- ", output)
  342. token = output.split(" ", 1)[0]
  343. args = munch.Munch({"token": token})
  344. with tests.capture_output() as output:
  345. pagure.cli.admin.do_info_admin_token(args)
  346. output = output.getvalue()
  347. self.assertIn(" -- pingou -- ", output.split("\n", 1)[0])
  348. self.assertEqual(
  349. output.split("\n", 1)[1],
  350. """ACLs:
  351. - issue_create
  352. - pull_request_comment
  353. - pull_request_flag
  354. """,
  355. )
  356. def test_do_info_admin_token_non_admin_acl(self):
  357. """Test the do_info_admin_token function of pagure-admin for a
  358. token not having any admin ACL."""
  359. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  360. pagure.lib.query.add_token_to_user(
  361. self.session,
  362. project=None,
  363. acls=["issue_assign", "pull_request_subscribe"],
  364. username="pingou",
  365. expiration_date=exp_date,
  366. )
  367. # Retrieve the token
  368. list_args = munch.Munch(
  369. {
  370. "user": None,
  371. "token": None,
  372. "active": False,
  373. "expired": False,
  374. "all": True,
  375. }
  376. )
  377. with tests.capture_output() as output:
  378. pagure.cli.admin.do_list_admin_token(list_args)
  379. output = output.getvalue()
  380. self.assertNotEqual(output, 'No user "pingou" found\n')
  381. self.assertEqual(len(output.split("\n")), 2)
  382. self.assertIn(" -- pingou -- ", output)
  383. token = output.split(" ", 1)[0]
  384. args = munch.Munch({"token": token})
  385. with tests.capture_output() as output:
  386. pagure.cli.admin.do_info_admin_token(args)
  387. output = output.getvalue()
  388. self.assertIn(" -- pingou -- ", output.split("\n", 1)[0])
  389. self.assertEqual(
  390. output.split("\n", 1)[1],
  391. """ACLs:
  392. - issue_assign
  393. - pull_request_subscribe
  394. """,
  395. )
  396. @patch("pagure.cli.admin._get_input")
  397. @patch("pagure.cli.admin._ask_confirmation")
  398. def test_do_expire_admin_token(self, conf, rinp):
  399. """ Test the do_expire_admin_token function of pagure-admin. """
  400. # Create an admin token to use
  401. conf.return_value = True
  402. rinp.return_value = "1,2,3"
  403. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  404. args = munch.Munch(
  405. {
  406. "user": "pingou",
  407. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  408. }
  409. )
  410. pagure.cli.admin.do_create_admin_token(args)
  411. # Retrieve the token
  412. list_args = munch.Munch(
  413. {
  414. "user": None,
  415. "token": None,
  416. "active": False,
  417. "expired": False,
  418. "all": False,
  419. }
  420. )
  421. with tests.capture_output() as output:
  422. pagure.cli.admin.do_list_admin_token(list_args)
  423. output = output.getvalue()
  424. self.assertNotEqual(output, 'No user "pingou" found\n')
  425. self.assertEqual(len(output.split("\n")), 2)
  426. self.assertIn(" -- pingou -- ", output)
  427. token = output.split(" ", 1)[0]
  428. # Before
  429. list_args = munch.Munch(
  430. {
  431. "user": None,
  432. "token": None,
  433. "active": True,
  434. "expired": False,
  435. "all": False,
  436. }
  437. )
  438. with tests.capture_output() as output:
  439. pagure.cli.admin.do_list_admin_token(list_args)
  440. output = output.getvalue()
  441. self.assertNotEqual(output, "No admin tokens found\n")
  442. self.assertEqual(len(output.split("\n")), 2)
  443. self.assertIn(" -- pingou -- ", output)
  444. # Expire the token
  445. args = munch.Munch({"token": token, "all": False})
  446. pagure.cli.admin.do_expire_admin_token(args)
  447. # After
  448. list_args = munch.Munch(
  449. {
  450. "user": None,
  451. "token": None,
  452. "active": True,
  453. "expired": False,
  454. "all": False,
  455. }
  456. )
  457. with tests.capture_output() as output:
  458. pagure.cli.admin.do_list_admin_token(list_args)
  459. output = output.getvalue()
  460. self.assertEqual(output, "No admin tokens found\n")
  461. @patch("pagure.cli.admin._get_input")
  462. @patch("pagure.cli.admin._ask_confirmation")
  463. def test_do_expire_admin_token_non_admin_acls(self, conf, rinp):
  464. """Test the do_expire_admin_token function of pagure-admin for a token
  465. without any admin ACL."""
  466. # Create an admin token to use
  467. conf.return_value = True
  468. rinp.return_value = "1,2,3"
  469. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  470. pagure.lib.query.add_token_to_user(
  471. self.session,
  472. project=None,
  473. acls=["issue_assign", "pull_request_subscribe"],
  474. username="pingou",
  475. expiration_date=exp_date,
  476. )
  477. # Retrieve all tokens to get the one of interest
  478. list_args = munch.Munch(
  479. {
  480. "user": None,
  481. "token": None,
  482. "active": False,
  483. "expired": False,
  484. "all": True,
  485. }
  486. )
  487. with tests.capture_output() as output:
  488. pagure.cli.admin.do_list_admin_token(list_args)
  489. output = output.getvalue()
  490. self.assertNotEqual(output, 'No user "pingou" found\n')
  491. self.assertEqual(len(output.split("\n")), 2)
  492. self.assertIn(" -- pingou -- ", output)
  493. token = output.split(" ", 1)[0]
  494. # Expire the token
  495. args = munch.Munch({"token": token, "all": True})
  496. pagure.cli.admin.do_expire_admin_token(args)
  497. # After
  498. list_args = munch.Munch(
  499. {
  500. "user": None,
  501. "token": None,
  502. "active": True,
  503. "expired": False,
  504. "all": False,
  505. }
  506. )
  507. with tests.capture_output() as output:
  508. pagure.cli.admin.do_list_admin_token(list_args)
  509. output = output.getvalue()
  510. self.assertEqual(output, "No admin tokens found\n")
  511. @patch("pagure.cli.admin._get_input")
  512. @patch("pagure.cli.admin._ask_confirmation")
  513. def test_do_update_admin_token_invalid_date(self, conf, rinp):
  514. """Test the do_update_admin_token function of pagure-admin with
  515. an invalid date."""
  516. # Create an admin token to use
  517. conf.return_value = True
  518. rinp.return_value = "1,2,3"
  519. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  520. args = munch.Munch(
  521. {
  522. "user": "pingou",
  523. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  524. }
  525. )
  526. pagure.cli.admin.do_create_admin_token(args)
  527. # Retrieve the token
  528. list_args = munch.Munch(
  529. {
  530. "user": None,
  531. "token": None,
  532. "active": False,
  533. "expired": False,
  534. "all": False,
  535. }
  536. )
  537. with tests.capture_output() as output:
  538. pagure.cli.admin.do_list_admin_token(list_args)
  539. output = output.getvalue()
  540. self.assertNotEqual(output, 'No user "pingou" found\n')
  541. self.assertEqual(len(output.split("\n")), 2)
  542. self.assertIn(" -- pingou -- ", output)
  543. token = output.split(" ", 1)[0]
  544. current_expiration = output.split(" ", 1)[1]
  545. # Set the expiration date to the token
  546. args = munch.Munch({"token": token, "date": "aa-bb-cc", "all": False})
  547. self.assertRaises(
  548. pagure.exceptions.PagureException,
  549. pagure.cli.admin.do_update_admin_token,
  550. args,
  551. )
  552. @patch("pagure.cli.admin._get_input")
  553. @patch("pagure.cli.admin._ask_confirmation")
  554. def test_do_update_admin_token_invalid_date2(self, conf, rinp):
  555. """Test the do_update_admin_token function of pagure-admin with
  556. an invalid date."""
  557. # Create an admin token to use
  558. conf.return_value = True
  559. rinp.return_value = "1,2,3"
  560. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  561. args = munch.Munch(
  562. {
  563. "user": "pingou",
  564. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  565. }
  566. )
  567. pagure.cli.admin.do_create_admin_token(args)
  568. # Retrieve the token
  569. list_args = munch.Munch(
  570. {
  571. "user": None,
  572. "token": None,
  573. "active": False,
  574. "expired": False,
  575. "all": False,
  576. }
  577. )
  578. with tests.capture_output() as output:
  579. pagure.cli.admin.do_list_admin_token(list_args)
  580. output = output.getvalue()
  581. self.assertNotEqual(output, 'No user "pingou" found\n')
  582. self.assertEqual(len(output.split("\n")), 2)
  583. self.assertIn(" -- pingou -- ", output)
  584. token = output.split(" ", 1)[0]
  585. current_expiration = output.split(" ", 1)[1]
  586. # Set the expiration date to the token
  587. args = munch.Munch(
  588. {"token": token, "date": "2017-18-01", "all": False}
  589. )
  590. self.assertRaises(
  591. pagure.exceptions.PagureException,
  592. pagure.cli.admin.do_update_admin_token,
  593. args,
  594. )
  595. @patch("pagure.cli.admin._get_input")
  596. @patch("pagure.cli.admin._ask_confirmation")
  597. def test_do_update_admin_token_invalid_date3(self, conf, rinp):
  598. """Test the do_update_admin_token function of pagure-admin with
  599. an invalid date (is today)."""
  600. # Create an admin token to use
  601. conf.return_value = True
  602. rinp.return_value = "1,2,3"
  603. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  604. args = munch.Munch(
  605. {
  606. "user": "pingou",
  607. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  608. }
  609. )
  610. pagure.cli.admin.do_create_admin_token(args)
  611. # Retrieve the token
  612. list_args = munch.Munch(
  613. {
  614. "user": None,
  615. "token": None,
  616. "active": False,
  617. "expired": False,
  618. "all": False,
  619. }
  620. )
  621. with tests.capture_output() as output:
  622. pagure.cli.admin.do_list_admin_token(list_args)
  623. output = output.getvalue()
  624. self.assertNotEqual(output, 'No user "pingou" found\n')
  625. self.assertEqual(len(output.split("\n")), 2)
  626. self.assertIn(" -- pingou -- ", output)
  627. token = output.split(" ", 1)[0]
  628. current_expiration = output.split(" ", 1)[1]
  629. # Set the expiration date to the token
  630. args = munch.Munch(
  631. {
  632. "token": token,
  633. "date": datetime.datetime.utcnow().date(),
  634. "all": False,
  635. }
  636. )
  637. self.assertRaises(
  638. pagure.exceptions.PagureException,
  639. pagure.cli.admin.do_update_admin_token,
  640. args,
  641. )
  642. @patch("pagure.cli.admin._get_input")
  643. @patch("pagure.cli.admin._ask_confirmation")
  644. def test_do_update_admin_token(self, conf, rinp):
  645. """ Test the do_update_admin_token function of pagure-admin. """
  646. # Create an admin token to use
  647. conf.return_value = True
  648. rinp.return_value = "1,2,3"
  649. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  650. args = munch.Munch(
  651. {
  652. "user": "pingou",
  653. "expiration_date": exp_date.strftime("%Y-%m-%d"),
  654. }
  655. )
  656. pagure.cli.admin.do_create_admin_token(args)
  657. # Retrieve the token
  658. list_args = munch.Munch(
  659. {
  660. "user": None,
  661. "token": None,
  662. "active": False,
  663. "expired": False,
  664. "all": False,
  665. }
  666. )
  667. with tests.capture_output() as output:
  668. pagure.cli.admin.do_list_admin_token(list_args)
  669. output = output.getvalue()
  670. self.assertNotEqual(output, 'No user "pingou" found\n')
  671. self.assertEqual(len(output.split("\n")), 2)
  672. self.assertIn(" -- pingou -- ", output)
  673. token = output.split(" ", 1)[0]
  674. current_expiration = output.strip().split(" -- ", 2)[-1]
  675. # Before
  676. list_args = munch.Munch(
  677. {
  678. "user": None,
  679. "token": None,
  680. "active": True,
  681. "expired": False,
  682. "all": False,
  683. }
  684. )
  685. with tests.capture_output() as output:
  686. pagure.cli.admin.do_list_admin_token(list_args)
  687. output = output.getvalue()
  688. self.assertNotEqual(output, "No admin tokens found\n")
  689. self.assertEqual(len(output.split("\n")), 2)
  690. self.assertIn(" -- pingou -- ", output)
  691. deadline = datetime.datetime.utcnow().date() + datetime.timedelta(
  692. days=3
  693. )
  694. # Set the expiration date to the token
  695. args = munch.Munch(
  696. {
  697. "token": token,
  698. "date": deadline.strftime("%Y-%m-%d"),
  699. "all": False,
  700. }
  701. )
  702. pagure.cli.admin.do_update_admin_token(args)
  703. # After
  704. list_args = munch.Munch(
  705. {
  706. "user": None,
  707. "token": None,
  708. "active": True,
  709. "expired": False,
  710. "all": False,
  711. }
  712. )
  713. with tests.capture_output() as output:
  714. pagure.cli.admin.do_list_admin_token(list_args)
  715. output = output.getvalue()
  716. self.assertEqual(output.split(" ", 1)[0], token)
  717. self.assertNotEqual(
  718. output.strip().split(" -- ", 2)[-1], current_expiration
  719. )
  720. @patch("pagure.cli.admin._get_input")
  721. @patch("pagure.cli.admin._ask_confirmation")
  722. def test_do_update_admin_token_non_admin_acls(self, conf, rinp):
  723. """Test the do_update_admin_token function of pagure-admin for a token
  724. without any admin ACL."""
  725. # Create an admin token to use
  726. conf.return_value = True
  727. rinp.return_value = "1,2,3"
  728. exp_date = datetime.date.today() + datetime.timedelta(days=300)
  729. pagure.lib.query.add_token_to_user(
  730. self.session,
  731. project=None,
  732. acls=["issue_assign", "pull_request_subscribe"],
  733. username="pingou",
  734. expiration_date=exp_date,
  735. )
  736. # Retrieve all tokens to get the one of interest
  737. list_args = munch.Munch(
  738. {
  739. "user": None,
  740. "token": None,
  741. "active": False,
  742. "expired": False,
  743. "all": True,
  744. }
  745. )
  746. with tests.capture_output() as output:
  747. pagure.cli.admin.do_list_admin_token(list_args)
  748. output = output.getvalue()
  749. self.assertNotEqual(output, 'No user "pingou" found\n')
  750. self.assertEqual(len(output.split("\n")), 2)
  751. self.assertIn(" -- pingou -- ", output)
  752. token = output.split(" ", 1)[0]
  753. current_expiration = output.strip().split(" -- ", 2)[-1]
  754. deadline = datetime.datetime.utcnow().date() + datetime.timedelta(
  755. days=3
  756. )
  757. # Set the expiration date to the token
  758. args = munch.Munch(
  759. {
  760. "token": token,
  761. "date": deadline.strftime("%Y-%m-%d"),
  762. "all": True,
  763. }
  764. )
  765. pagure.cli.admin.do_update_admin_token(args)
  766. # After
  767. list_args = munch.Munch(
  768. {
  769. "user": None,
  770. "token": None,
  771. "active": True,
  772. "expired": False,
  773. "all": True,
  774. }
  775. )
  776. with tests.capture_output() as output:
  777. pagure.cli.admin.do_list_admin_token(list_args)
  778. output = output.getvalue()
  779. self.assertEqual(output.split(" ", 1)[0], token)
  780. self.assertNotEqual(
  781. output.strip().split(" -- ", 2)[-1], current_expiration
  782. )
  783. class PagureAdminGetWatchTests(tests.Modeltests):
  784. """ Tests for pagure-admin get-watch """
  785. populate_db = False
  786. def setUp(self):
  787. """ Set up the environnment, ran before every tests. """
  788. super(PagureAdminGetWatchTests, self).setUp()
  789. pagure.cli.admin.session = self.session
  790. # Create the user pingou
  791. item = pagure.lib.model.User(
  792. user="pingou",
  793. fullname="PY C",
  794. password="foo",
  795. default_email="bar@pingou.com",
  796. )
  797. self.session.add(item)
  798. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  799. self.session.add(item)
  800. # Create the user foo
  801. item = pagure.lib.model.User(
  802. user="foo",
  803. fullname="foo B.",
  804. password="foob",
  805. default_email="foo@pingou.com",
  806. )
  807. self.session.add(item)
  808. # Create two projects for the user pingou
  809. item = pagure.lib.model.Project(
  810. user_id=1, # pingou
  811. name="test",
  812. description="namespaced test project",
  813. hook_token="aaabbbeee",
  814. namespace="somenamespace",
  815. )
  816. self.session.add(item)
  817. item = pagure.lib.model.Project(
  818. user_id=1, # pingou
  819. name="test",
  820. description="Test project",
  821. hook_token="aaabbbccc",
  822. namespace=None,
  823. )
  824. self.session.add(item)
  825. self.session.commit()
  826. # Make the imported pagure use the correct db session
  827. pagure.cli.admin.session = self.session
  828. def test_get_watch_get_project_unknown_project(self):
  829. """Test the get-watch function of pagure-admin with an unknown
  830. project.
  831. """
  832. args = munch.Munch({"project": "foobar", "user": "pingou"})
  833. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  834. pagure.cli.admin.do_get_watch_status(args)
  835. self.assertEqual(
  836. cm.exception.args[0], "No project found with: project=foobar"
  837. )
  838. def test_get_watch_get_project_invalid_project(self):
  839. """Test the get-watch function of pagure-admin with an invalid
  840. project.
  841. """
  842. args = munch.Munch({"project": "fo/o/bar", "user": "pingou"})
  843. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  844. pagure.cli.admin.do_get_watch_status(args)
  845. self.assertEqual(
  846. cm.exception.args[0],
  847. 'Invalid project name, has more than one "/": fo/o/bar',
  848. )
  849. def test_get_watch_get_project_invalid_user(self):
  850. """Test the get-watch function of pagure-admin on a invalid user."""
  851. args = munch.Munch({"project": "test", "user": "beebop"})
  852. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  853. pagure.cli.admin.do_get_watch_status(args)
  854. self.assertEqual(cm.exception.args[0], 'No user "beebop" found')
  855. def test_get_watch_get_project(self):
  856. """Test the get-watch function of pagure-admin on a regular project."""
  857. args = munch.Munch({"project": "test", "user": "pingou"})
  858. with tests.capture_output() as output:
  859. pagure.cli.admin.do_get_watch_status(args)
  860. output = output.getvalue()
  861. self.assertEqual(
  862. "On test user: pingou is watching the following items: "
  863. "issues, pull-requests\n",
  864. output,
  865. )
  866. def test_get_watch_get_project_not_watching(self):
  867. """Test the get-watch function of pagure-admin on a regular project."""
  868. args = munch.Munch({"project": "test", "user": "foo"})
  869. with tests.capture_output() as output:
  870. pagure.cli.admin.do_get_watch_status(args)
  871. output = output.getvalue()
  872. self.assertEqual(
  873. "On test user: foo is watching the following items: None\n", output
  874. )
  875. def test_get_watch_get_project_namespaced(self):
  876. """Test the get-watch function of pagure-admin on a namespaced project."""
  877. args = munch.Munch({"project": "somenamespace/test", "user": "pingou"})
  878. with tests.capture_output() as output:
  879. pagure.cli.admin.do_get_watch_status(args)
  880. output = output.getvalue()
  881. self.assertEqual(
  882. "On somenamespace/test user: pingou is watching the following "
  883. "items: issues, pull-requests\n",
  884. output,
  885. )
  886. def test_get_watch_get_project_namespaced_not_watching(self):
  887. """Test the get-watch function of pagure-admin on a namespaced project."""
  888. args = munch.Munch({"project": "somenamespace/test", "user": "foo"})
  889. with tests.capture_output() as output:
  890. pagure.cli.admin.do_get_watch_status(args)
  891. output = output.getvalue()
  892. with tests.capture_output() as _discarded:
  893. pagure.cli.admin.do_get_watch_status(args)
  894. self.assertEqual(
  895. "On somenamespace/test user: foo is watching the following "
  896. "items: None\n",
  897. output,
  898. )
  899. class PagureAdminUpdateWatchTests(tests.Modeltests):
  900. """ Tests for pagure-admin update-watch """
  901. populate_db = False
  902. def setUp(self):
  903. """ Set up the environnment, ran before every tests. """
  904. super(PagureAdminUpdateWatchTests, self).setUp()
  905. pagure.cli.admin.session = self.session
  906. # Create the user pingou
  907. item = pagure.lib.model.User(
  908. user="pingou",
  909. fullname="PY C",
  910. password="foo",
  911. default_email="bar@pingou.com",
  912. )
  913. self.session.add(item)
  914. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  915. self.session.add(item)
  916. # Create the user foo
  917. item = pagure.lib.model.User(
  918. user="foo",
  919. fullname="foo B.",
  920. password="foob",
  921. default_email="foo@pingou.com",
  922. )
  923. self.session.add(item)
  924. # Create two projects for the user pingou
  925. item = pagure.lib.model.Project(
  926. user_id=1, # pingou
  927. name="test",
  928. description="namespaced test project",
  929. hook_token="aaabbbeee",
  930. namespace="somenamespace",
  931. )
  932. self.session.add(item)
  933. item = pagure.lib.model.Project(
  934. user_id=1, # pingou
  935. name="test",
  936. description="Test project",
  937. hook_token="aaabbbccc",
  938. namespace=None,
  939. )
  940. self.session.add(item)
  941. self.session.commit()
  942. # Make the imported pagure use the correct db session
  943. pagure.cli.admin.session = self.session
  944. def test_get_watch_update_project_unknown_project(self):
  945. """Test the update-watch function of pagure-admin on an unknown
  946. project.
  947. """
  948. args = munch.Munch(
  949. {"project": "foob", "user": "pingou", "status": "1"}
  950. )
  951. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  952. pagure.cli.admin.do_update_watch_status(args)
  953. self.assertEqual(
  954. cm.exception.args[0], "No project found with: project=foob"
  955. )
  956. def test_get_watch_update_project_invalid_project(self):
  957. """Test the update-watch function of pagure-admin on an invalid
  958. project.
  959. """
  960. args = munch.Munch(
  961. {"project": "fo/o/b", "user": "pingou", "status": "1"}
  962. )
  963. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  964. pagure.cli.admin.do_update_watch_status(args)
  965. self.assertEqual(
  966. cm.exception.args[0],
  967. 'Invalid project name, has more than one "/": fo/o/b',
  968. )
  969. def test_get_watch_update_project_invalid_user(self):
  970. """Test the update-watch function of pagure-admin on an invalid user."""
  971. args = munch.Munch({"project": "test", "user": "foob", "status": "1"})
  972. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  973. pagure.cli.admin.do_update_watch_status(args)
  974. self.assertEqual(cm.exception.args[0], 'No user "foob" found')
  975. def test_get_watch_update_project_invalid_status(self):
  976. """Test the update-watch function of pagure-admin with an invalid
  977. status.
  978. """
  979. args = munch.Munch(
  980. {"project": "test", "user": "pingou", "status": "10"}
  981. )
  982. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  983. pagure.cli.admin.do_update_watch_status(args)
  984. self.assertEqual(
  985. cm.exception.args[0],
  986. "Invalid status provided: 10 not in -1, 0, 1, 2, 3",
  987. )
  988. def test_get_watch_update_project_no_effect(self):
  989. """Test the update-watch function of pagure-admin with a regular
  990. project - nothing changed.
  991. """
  992. args = munch.Munch({"project": "test", "user": "pingou"})
  993. with tests.capture_output() as output:
  994. pagure.cli.admin.do_get_watch_status(args)
  995. output = output.getvalue()
  996. self.assertEqual(
  997. "On test user: pingou is watching the following items: "
  998. "issues, pull-requests\n",
  999. output,
  1000. )
  1001. args = munch.Munch(
  1002. {"project": "test", "user": "pingou", "status": "1"}
  1003. )
  1004. with tests.capture_output() as output:
  1005. pagure.cli.admin.do_update_watch_status(args)
  1006. output = output.getvalue()
  1007. self.assertEqual(
  1008. "Updating watch status of pingou to 1 (watch issues and PRs) "
  1009. "on test\n",
  1010. output,
  1011. )
  1012. args = munch.Munch({"project": "test", "user": "pingou"})
  1013. with tests.capture_output() as output:
  1014. pagure.cli.admin.do_get_watch_status(args)
  1015. output = output.getvalue()
  1016. self.assertEqual(
  1017. "On test user: pingou is watching the following items: "
  1018. "issues, pull-requests\n",
  1019. output,
  1020. )
  1021. class PagureAdminReadOnlyTests(tests.Modeltests):
  1022. """ Tests for pagure-admin read-only """
  1023. populate_db = False
  1024. def setUp(self):
  1025. """ Set up the environnment, ran before every tests. """
  1026. super(PagureAdminReadOnlyTests, self).setUp()
  1027. pagure.cli.admin.session = self.session
  1028. # Create the user pingou
  1029. item = pagure.lib.model.User(
  1030. user="pingou",
  1031. fullname="PY C",
  1032. password="foo",
  1033. default_email="bar@pingou.com",
  1034. )
  1035. self.session.add(item)
  1036. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  1037. self.session.add(item)
  1038. # Create two projects for the user pingou
  1039. item = pagure.lib.model.Project(
  1040. user_id=1, # pingou
  1041. name="test",
  1042. description="namespaced test project",
  1043. hook_token="aaabbbeee",
  1044. namespace="somenamespace",
  1045. )
  1046. self.session.add(item)
  1047. item = pagure.lib.model.Project(
  1048. user_id=1, # pingou
  1049. name="test",
  1050. description="Test project",
  1051. hook_token="aaabbbccc",
  1052. namespace=None,
  1053. )
  1054. self.session.add(item)
  1055. self.session.commit()
  1056. # Make the imported pagure use the correct db session
  1057. pagure.cli.admin.session = self.session
  1058. def test_read_only_unknown_project(self):
  1059. """Test the read-only function of pagure-admin on an unknown
  1060. project.
  1061. """
  1062. args = munch.Munch({"project": "foob", "user": None, "ro": None})
  1063. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1064. pagure.cli.admin.do_read_only(args)
  1065. self.assertEqual(
  1066. cm.exception.args[0], "No project found with: project=foob"
  1067. )
  1068. def test_read_only_invalid_project(self):
  1069. """Test the read-only function of pagure-admin on an invalid
  1070. project.
  1071. """
  1072. args = munch.Munch({"project": "fo/o/b", "user": None, "ro": None})
  1073. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1074. pagure.cli.admin.do_read_only(args)
  1075. self.assertEqual(
  1076. cm.exception.args[0],
  1077. 'Invalid project name, has more than one "/": fo/o/b',
  1078. )
  1079. def test_read_only(self):
  1080. """Test the read-only function of pagure-admin to get status of
  1081. a non-namespaced project.
  1082. """
  1083. args = munch.Munch({"project": "test", "user": None, "ro": None})
  1084. with tests.capture_output() as output:
  1085. pagure.cli.admin.do_read_only(args)
  1086. output = output.getvalue()
  1087. self.assertEqual(
  1088. "The current read-only flag of the project test is set to True\n",
  1089. output,
  1090. )
  1091. def test_read_only_namespace(self):
  1092. """Test the read-only function of pagure-admin to get status of
  1093. a namespaced project.
  1094. """
  1095. args = munch.Munch(
  1096. {"project": "somenamespace/test", "user": None, "ro": None}
  1097. )
  1098. with tests.capture_output() as output:
  1099. pagure.cli.admin.do_read_only(args)
  1100. output = output.getvalue()
  1101. self.assertEqual(
  1102. "The current read-only flag of the project somenamespace/test "
  1103. "is set to True\n",
  1104. output,
  1105. )
  1106. def test_read_only_namespace_changed(self):
  1107. """Test the read-only function of pagure-admin to set the status of
  1108. a namespaced project.
  1109. """
  1110. # Before
  1111. args = munch.Munch(
  1112. {"project": "somenamespace/test", "user": None, "ro": None}
  1113. )
  1114. with tests.capture_output() as output:
  1115. pagure.cli.admin.do_read_only(args)
  1116. output = output.getvalue()
  1117. self.assertEqual(
  1118. "The current read-only flag of the project somenamespace/test "
  1119. "is set to True\n",
  1120. output,
  1121. )
  1122. args = munch.Munch(
  1123. {"project": "somenamespace/test", "user": None, "ro": "false"}
  1124. )
  1125. with tests.capture_output() as output:
  1126. pagure.cli.admin.do_read_only(args)
  1127. output = output.getvalue()
  1128. self.assertEqual(
  1129. "The read-only flag of the project somenamespace/test has been "
  1130. "set to False\n",
  1131. output,
  1132. )
  1133. # After
  1134. args = munch.Munch(
  1135. {"project": "somenamespace/test", "user": None, "ro": None}
  1136. )
  1137. with tests.capture_output() as output:
  1138. pagure.cli.admin.do_read_only(args)
  1139. output = output.getvalue()
  1140. self.assertEqual(
  1141. "The current read-only flag of the project somenamespace/test "
  1142. "is set to False\n",
  1143. output,
  1144. )
  1145. def test_read_only_no_change(self):
  1146. """Test the read-only function of pagure-admin to set the status of
  1147. a namespaced project.
  1148. """
  1149. # Before
  1150. args = munch.Munch({"project": "test", "user": None, "ro": None})
  1151. with tests.capture_output() as output:
  1152. pagure.cli.admin.do_read_only(args)
  1153. output = output.getvalue()
  1154. self.assertEqual(
  1155. "The current read-only flag of the project test "
  1156. "is set to True\n",
  1157. output,
  1158. )
  1159. args = munch.Munch({"project": "test", "user": None, "ro": "true"})
  1160. with tests.capture_output() as output:
  1161. pagure.cli.admin.do_read_only(args)
  1162. output = output.getvalue()
  1163. self.assertEqual(
  1164. "The read-only flag of the project test has been " "set to True\n",
  1165. output,
  1166. )
  1167. # After
  1168. args = munch.Munch({"project": "test", "user": None, "ro": None})
  1169. with tests.capture_output() as output:
  1170. pagure.cli.admin.do_read_only(args)
  1171. output = output.getvalue()
  1172. self.assertEqual(
  1173. "The current read-only flag of the project test "
  1174. "is set to True\n",
  1175. output,
  1176. )
  1177. class PagureNewGroupTests(tests.Modeltests):
  1178. """ Tests for pagure-admin new-group """
  1179. populate_db = False
  1180. def setUp(self):
  1181. """ Set up the environnment, ran before every tests. """
  1182. super(PagureNewGroupTests, self).setUp()
  1183. pagure.cli.admin.session = self.session
  1184. # Create the user pingou
  1185. item = pagure.lib.model.User(
  1186. user="pingou",
  1187. fullname="PY C",
  1188. password="foo",
  1189. default_email="bar@pingou.com",
  1190. )
  1191. self.session.add(item)
  1192. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  1193. self.session.add(item)
  1194. self.session.commit()
  1195. # Make the imported pagure use the correct db session
  1196. pagure.cli.admin.session = self.session
  1197. groups = pagure.lib.query.search_groups(self.session)
  1198. self.assertEqual(len(groups), 0)
  1199. def test_missing_display_name(self):
  1200. """Test the new-group function of pagure-admin when the display name
  1201. is missing from the args.
  1202. """
  1203. args = munch.Munch(
  1204. {
  1205. "group_name": "foob",
  1206. "display": None,
  1207. "description": None,
  1208. "username": "pingou",
  1209. }
  1210. )
  1211. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1212. pagure.cli.admin.do_new_group(args)
  1213. self.assertEqual(
  1214. cm.exception.args[0],
  1215. "A display name must be provided for the group",
  1216. )
  1217. groups = pagure.lib.query.search_groups(self.session)
  1218. self.assertEqual(len(groups), 0)
  1219. def test_missing_username(self):
  1220. """Test the new-group function of pagure-admin when the username
  1221. is missing from the args.
  1222. """
  1223. args = munch.Munch(
  1224. {
  1225. "group_name": "foob",
  1226. "display": "foo group",
  1227. "description": None,
  1228. "username": None,
  1229. }
  1230. )
  1231. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1232. pagure.cli.admin.do_new_group(args)
  1233. self.assertEqual(
  1234. cm.exception.args[0],
  1235. "An username must be provided to associate with the group",
  1236. )
  1237. groups = pagure.lib.query.search_groups(self.session)
  1238. self.assertEqual(len(groups), 0)
  1239. def test_new_group(self):
  1240. """Test the new-group function of pagure-admin when all arguments
  1241. are provided.
  1242. """
  1243. args = munch.Munch(
  1244. {
  1245. "group_name": "foob",
  1246. "display": "foo group",
  1247. "description": None,
  1248. "username": "pingou",
  1249. }
  1250. )
  1251. pagure.cli.admin.do_new_group(args)
  1252. groups = pagure.lib.query.search_groups(self.session)
  1253. self.assertEqual(len(groups), 1)
  1254. @patch.dict("pagure.config.config", {"ENABLE_GROUP_MNGT": False})
  1255. @patch("pagure.cli.admin._ask_confirmation")
  1256. def test_new_group_grp_mngt_off_no(self, conf):
  1257. """Test the new-group function of pagure-admin when all arguments
  1258. are provided and ENABLE_GROUP_MNGT if off in the config and the user
  1259. replies no to the question.
  1260. """
  1261. conf.return_value = False
  1262. args = munch.Munch(
  1263. {
  1264. "group_name": "foob",
  1265. "display": "foo group",
  1266. "description": None,
  1267. "username": "pingou",
  1268. }
  1269. )
  1270. pagure.cli.admin.do_new_group(args)
  1271. groups = pagure.lib.query.search_groups(self.session)
  1272. self.assertEqual(len(groups), 0)
  1273. @patch.dict("pagure.config.config", {"ENABLE_GROUP_MNGT": False})
  1274. @patch("pagure.cli.admin._ask_confirmation")
  1275. def test_new_group_grp_mngt_off_yes(self, conf):
  1276. """Test the new-group function of pagure-admin when all arguments
  1277. are provided and ENABLE_GROUP_MNGT if off in the config and the user
  1278. replies yes to the question.
  1279. """
  1280. conf.return_value = True
  1281. args = munch.Munch(
  1282. {
  1283. "group_name": "foob",
  1284. "display": "foo group",
  1285. "description": None,
  1286. "username": "pingou",
  1287. }
  1288. )
  1289. pagure.cli.admin.do_new_group(args)
  1290. groups = pagure.lib.query.search_groups(self.session)
  1291. self.assertEqual(len(groups), 1)
  1292. @patch.dict("pagure.config.config", {"BLACKLISTED_GROUPS": ["foob"]})
  1293. def test_new_group_grp_mngt_off_yes(self):
  1294. """Test the new-group function of pagure-admin when all arguments
  1295. are provided but the group is black listed.
  1296. """
  1297. args = munch.Munch(
  1298. {
  1299. "group_name": "foob",
  1300. "display": "foo group",
  1301. "description": None,
  1302. "username": "pingou",
  1303. }
  1304. )
  1305. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1306. pagure.cli.admin.do_new_group(args)
  1307. self.assertEqual(
  1308. cm.exception.args[0],
  1309. "This group name has been blacklisted, please choose another one",
  1310. )
  1311. groups = pagure.lib.query.search_groups(self.session)
  1312. self.assertEqual(len(groups), 0)
  1313. class PagureListGroupEmptyTests(tests.Modeltests):
  1314. """ Tests for pagure-admin list-groups """
  1315. populate_db = False
  1316. def setUp(self):
  1317. """ Set up the environnment, ran before every tests. """
  1318. super(PagureListGroupEmptyTests, self).setUp()
  1319. pagure.cli.admin.session = self.session
  1320. # Create the user pingou
  1321. item = pagure.lib.model.User(
  1322. user="pingou",
  1323. fullname="PY C",
  1324. password="foo",
  1325. default_email="bar@pingou.com",
  1326. )
  1327. self.session.add(item)
  1328. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  1329. self.session.add(item)
  1330. self.session.commit()
  1331. # Make the imported pagure use the correct db session
  1332. pagure.cli.admin.session = self.session
  1333. groups = pagure.lib.query.search_groups(self.session)
  1334. self.assertEqual(len(groups), 0)
  1335. @patch("sys.stdout", new_callable=StringIO)
  1336. def test_no_groups(self, mock_stdout):
  1337. """Test the list-groups function of pagure-admin when there are no
  1338. groups in the database
  1339. """
  1340. args = munch.Munch()
  1341. pagure.cli.admin.do_list_groups(args)
  1342. self.assertEqual(
  1343. mock_stdout.getvalue(),
  1344. "No groups found in this pagure instance.\n",
  1345. )
  1346. groups = pagure.lib.query.search_groups(self.session)
  1347. self.assertEqual(len(groups), 0)
  1348. class PagureListGroupTests(tests.Modeltests):
  1349. """ Tests for pagure-admin list-groups """
  1350. populate_db = False
  1351. def setUp(self):
  1352. """ Set up the environnment, ran before every tests. """
  1353. super(PagureListGroupTests, self).setUp()
  1354. pagure.cli.admin.session = self.session
  1355. # Create the user pingou
  1356. item = pagure.lib.model.User(
  1357. user="pingou",
  1358. fullname="PY C",
  1359. password="foo",
  1360. default_email="bar@pingou.com",
  1361. )
  1362. self.session.add(item)
  1363. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  1364. self.session.add(item)
  1365. # Create a group
  1366. pagure.lib.query.add_group(
  1367. self.session,
  1368. group_name="JL",
  1369. display_name="Justice League",
  1370. description="Nope, it's not JLA anymore",
  1371. group_type="user",
  1372. user="pingou",
  1373. is_admin=False,
  1374. blacklist=[],
  1375. )
  1376. self.session.commit()
  1377. # Make the imported pagure use the correct db session
  1378. pagure.cli.admin.session = self.session
  1379. groups = pagure.lib.query.search_groups(self.session)
  1380. self.assertEqual(len(groups), 1)
  1381. @patch("sys.stdout", new_callable=StringIO)
  1382. def test_list_groups(self, mock_stdout):
  1383. """Test the list-groups function of pagure-admin when there is one
  1384. group in the database
  1385. """
  1386. args = munch.Munch()
  1387. pagure.cli.admin.do_list_groups(args)
  1388. self.assertEqual(
  1389. mock_stdout.getvalue(),
  1390. "List of groups on this Pagure instance:\n" "Group: 1 - name JL\n",
  1391. )
  1392. groups = pagure.lib.query.search_groups(self.session)
  1393. self.assertEqual(len(groups), 1)
  1394. class PagureBlockUserTests(tests.Modeltests):
  1395. """ Tests for pagure-admin block-user """
  1396. populate_db = False
  1397. def setUp(self):
  1398. """ Set up the environnment, ran before every tests. """
  1399. super(PagureBlockUserTests, self).setUp()
  1400. pagure.cli.admin.session = self.session
  1401. # Create the user pingou
  1402. item = pagure.lib.model.User(
  1403. user="pingou",
  1404. fullname="PY C",
  1405. password="foo",
  1406. default_email="bar@pingou.com",
  1407. )
  1408. self.session.add(item)
  1409. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  1410. self.session.add(item)
  1411. self.session.commit()
  1412. # Make the imported pagure use the correct db session
  1413. pagure.cli.admin.session = self.session
  1414. user = pagure.lib.query.get_user(self.session, "pingou")
  1415. self.assertIsNone(user.refuse_sessions_before)
  1416. def test_missing_date(self):
  1417. """Test the block-user function of pagure-admin when the no date is
  1418. provided.
  1419. """
  1420. args = munch.Munch({"username": "pingou", "date": None, "list": False})
  1421. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1422. pagure.cli.admin.do_block_user(args)
  1423. self.assertEqual(
  1424. cm.exception.args[0],
  1425. "Invalid date submitted: None, not of the format YYYY-MM-DD",
  1426. )
  1427. user = pagure.lib.query.get_user(self.session, "pingou")
  1428. self.assertIsNone(user.refuse_sessions_before)
  1429. def test_missing_username(self):
  1430. """Test the block-user function of pagure-admin when the username
  1431. is missing from the args.
  1432. """
  1433. args = munch.Munch(
  1434. {"date": "2018-06-11", "username": None, "list": False}
  1435. )
  1436. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1437. pagure.cli.admin.do_block_user(args)
  1438. self.assertEqual(cm.exception.args[0], "An username must be specified")
  1439. user = pagure.lib.query.get_user(self.session, "pingou")
  1440. self.assertIsNone(user.refuse_sessions_before)
  1441. def test_invalid_username(self):
  1442. """Test the block-user function of pagure-admin when the username
  1443. provided does correspond to any user in the DB.
  1444. """
  1445. args = munch.Munch(
  1446. {"date": "2018-06-11", "username": "invalid", "list": False}
  1447. )
  1448. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1449. pagure.cli.admin.do_block_user(args)
  1450. self.assertEqual(cm.exception.args[0], 'No user "invalid" found')
  1451. user = pagure.lib.query.get_user(self.session, "pingou")
  1452. self.assertIsNone(user.refuse_sessions_before)
  1453. def test_invalide_date(self):
  1454. """Test the block-user function of pagure-admin when the provided
  1455. date is incorrect.
  1456. """
  1457. args = munch.Munch(
  1458. {"date": "2018-14-05", "username": "pingou", "list": False}
  1459. )
  1460. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1461. pagure.cli.admin.do_block_user(args)
  1462. self.assertEqual(
  1463. cm.exception.args[0],
  1464. "Invalid date submitted: 2018-14-05, not of the format YYYY-MM-DD",
  1465. )
  1466. user = pagure.lib.query.get_user(self.session, "pingou")
  1467. self.assertIsNone(user.refuse_sessions_before)
  1468. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1469. def test_block_user(self):
  1470. """Test the block-user function of pagure-admin when all arguments
  1471. are provided correctly.
  1472. """
  1473. args = munch.Munch(
  1474. {"date": "2050-12-31", "username": "pingou", "list": False}
  1475. )
  1476. pagure.cli.admin.do_block_user(args)
  1477. user = pagure.lib.query.get_user(self.session, "pingou")
  1478. self.assertIsNotNone(user.refuse_sessions_before)
  1479. def test_list_blocked_user(self):
  1480. """Test the block-user function of pagure-admin when all arguments
  1481. are provided correctly.
  1482. """
  1483. args = munch.Munch({"list": True, "username": None, "date": None})
  1484. with tests.capture_output() as output:
  1485. pagure.cli.admin.do_block_user(args)
  1486. output = output.getvalue()
  1487. self.assertEqual("No users are currently blocked\n", output)
  1488. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1489. def test_list_blocked_user_with_data(self):
  1490. """Test the block-user function of pagure-admin when all arguments
  1491. are provided correctly.
  1492. """
  1493. args = munch.Munch(
  1494. {"date": "2050-12-31", "username": "pingou", "list": False}
  1495. )
  1496. pagure.cli.admin.do_block_user(args)
  1497. args = munch.Munch({"list": True, "username": None, "date": None})
  1498. with tests.capture_output() as output:
  1499. pagure.cli.admin.do_block_user(args)
  1500. output = output.getvalue()
  1501. self.assertEqual(
  1502. "Users blocked:\n"
  1503. " pingou - 2050-12-31T00:00:00\n",
  1504. output,
  1505. )
  1506. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1507. def test_list_blocked_user_with_username_data(self):
  1508. """Test the block-user function of pagure-admin when all arguments
  1509. are provided correctly.
  1510. """
  1511. args = munch.Munch(
  1512. {"date": "2050-12-31", "username": "pingou", "list": False}
  1513. )
  1514. pagure.cli.admin.do_block_user(args)
  1515. args = munch.Munch({"list": True, "username": "ralph", "date": None})
  1516. with tests.capture_output() as output:
  1517. pagure.cli.admin.do_block_user(args)
  1518. output = output.getvalue()
  1519. self.assertEqual("No users are currently blocked\n", output)
  1520. args = munch.Munch({"list": True, "username": "pin*", "date": None})
  1521. with tests.capture_output() as output:
  1522. pagure.cli.admin.do_block_user(args)
  1523. output = output.getvalue()
  1524. self.assertEqual(
  1525. "Users blocked:\n"
  1526. " pingou - 2050-12-31T00:00:00\n",
  1527. output,
  1528. )
  1529. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1530. def test_list_blocked_user_with_date(self):
  1531. """Test the block-user function of pagure-admin when all arguments
  1532. are provided correctly.
  1533. """
  1534. args = munch.Munch(
  1535. {"list": True, "username": None, "date": "2050-12-31"}
  1536. )
  1537. with tests.capture_output() as output:
  1538. pagure.cli.admin.do_block_user(args)
  1539. output = output.getvalue()
  1540. self.assertIn("No users are currently blocked\n", output)
  1541. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1542. def test_list_blocked_user_with_date_and_data(self):
  1543. """Test the block-user function of pagure-admin when all arguments
  1544. are provided correctly.
  1545. """
  1546. args = munch.Munch(
  1547. {"date": "2050-12-31", "username": "pingou", "list": False}
  1548. )
  1549. pagure.cli.admin.do_block_user(args)
  1550. args = munch.Munch(
  1551. {"list": True, "username": None, "date": "2050-12-31"}
  1552. )
  1553. with tests.capture_output() as output:
  1554. pagure.cli.admin.do_block_user(args)
  1555. output = output.getvalue()
  1556. self.assertIn(
  1557. "Users blocked:\n"
  1558. " pingou - 2050-12-31T00:00:00\n",
  1559. output,
  1560. )
  1561. args = munch.Munch(
  1562. {"list": True, "username": None, "date": "2051-01-01"}
  1563. )
  1564. with tests.capture_output() as output:
  1565. pagure.cli.admin.do_block_user(args)
  1566. output = output.getvalue()
  1567. self.assertIn("No users are currently blocked\n", output)
  1568. class PagureAdminDeleteProjectTests(tests.Modeltests):
  1569. """ Tests for pagure-admin delete-project """
  1570. populate_db = False
  1571. def setUp(self):
  1572. """ Set up the environnment, ran before every tests. """
  1573. super(PagureAdminDeleteProjectTests, self).setUp()
  1574. pagure.cli.admin.session = self.session
  1575. # Create the user pingou
  1576. item = pagure.lib.model.User(
  1577. user="pingou",
  1578. fullname="PY C",
  1579. password="foo",
  1580. default_email="bar@pingou.com",
  1581. )
  1582. self.session.add(item)
  1583. item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
  1584. self.session.add(item)
  1585. # Create two projects for the user pingou
  1586. item = pagure.lib.model.Project(
  1587. user_id=1, # pingou
  1588. name="test",
  1589. description="namespaced test project",
  1590. hook_token="aaabbbeee",
  1591. namespace="somenamespace",
  1592. )
  1593. self.session.add(item)
  1594. item = pagure.lib.model.Project(
  1595. user_id=1, # pingou
  1596. name="test",
  1597. description="Test project",
  1598. hook_token="aaabbbccc",
  1599. namespace=None,
  1600. )
  1601. self.session.add(item)
  1602. self.session.commit()
  1603. # Make the imported pagure use the correct db session
  1604. pagure.cli.admin.session = self.session
  1605. def test_delete_project_unknown_project(self):
  1606. """Test the read-only function of pagure-admin on an unknown
  1607. project.
  1608. """
  1609. args = munch.Munch(
  1610. {"project": "foob", "user": None, "action_user": "pingou"}
  1611. )
  1612. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1613. pagure.cli.admin.do_delete_project(args)
  1614. self.assertEqual(
  1615. cm.exception.args[0],
  1616. "No project found with: project=foob, user=None",
  1617. )
  1618. def test_delete_project_invalid_project(self):
  1619. """Test the read-only function of pagure-admin on an invalid
  1620. project.
  1621. """
  1622. args = munch.Munch(
  1623. {"project": "fo/o/b", "user": None, "action_user": "pingou"}
  1624. )
  1625. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1626. pagure.cli.admin.do_delete_project(args)
  1627. self.assertEqual(
  1628. cm.exception.args[0],
  1629. 'Invalid project name, has more than one "/": fo/o/b',
  1630. )
  1631. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1632. def test_delete_project(self):
  1633. """Test the read-only function of pagure-admin to get status of
  1634. a non-namespaced project.
  1635. """
  1636. args = munch.Munch(
  1637. {"project": "test", "user": None, "action_user": "pingou"}
  1638. )
  1639. with tests.capture_output() as output:
  1640. pagure.cli.admin.do_delete_project(args)
  1641. output = output.getvalue()
  1642. self.assertEqual(
  1643. "Are you sure you want to delete: test?\n"
  1644. " This cannot be undone!\n"
  1645. "Project deleted\n",
  1646. output,
  1647. )
  1648. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1649. def test_delete_project_namespace(self):
  1650. """Test the read-only function of pagure-admin to get status of
  1651. a namespaced project.
  1652. """
  1653. args = munch.Munch(
  1654. {
  1655. "project": "somenamespace/test",
  1656. "user": None,
  1657. "action_user": "pingou",
  1658. }
  1659. )
  1660. with tests.capture_output() as output:
  1661. pagure.cli.admin.do_delete_project(args)
  1662. output = output.getvalue()
  1663. self.assertEqual(
  1664. "Are you sure you want to delete: somenamespace/test?\n"
  1665. " This cannot be undone!\n"
  1666. "Project deleted\n",
  1667. output,
  1668. )
  1669. @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
  1670. def test_delete_project_namespace_changed(self):
  1671. """Test the read-only function of pagure-admin to set the status of
  1672. a namespaced project.
  1673. """
  1674. # Before
  1675. projects = pagure.lib.query.search_projects(self.session)
  1676. self.assertEqual(len(projects), 2)
  1677. args = munch.Munch(
  1678. {
  1679. "project": "somenamespace/test",
  1680. "user": None,
  1681. "action_user": "pingou",
  1682. }
  1683. )
  1684. with tests.capture_output() as output:
  1685. pagure.cli.admin.do_delete_project(args)
  1686. output = output.getvalue()
  1687. self.assertEqual(
  1688. "Are you sure you want to delete: somenamespace/test?\n"
  1689. " This cannot be undone!\n"
  1690. "Project deleted\n",
  1691. output,
  1692. )
  1693. # After
  1694. projects = pagure.lib.query.search_projects(self.session)
  1695. self.assertEqual(len(projects), 1)
  1696. class PagureCreateBranchTests(tests.Modeltests):
  1697. """ Tests for pagure-admin create-branch """
  1698. populate_db = True
  1699. def setUp(self):
  1700. """ Set up the environnment, ran before every tests. """
  1701. super(PagureCreateBranchTests, self).setUp()
  1702. # Create a couple of projects
  1703. tests.create_projects(self.session)
  1704. # Create their git repo
  1705. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1706. # Make the imported pagure use the correct db session
  1707. pagure.cli.admin.session = self.session
  1708. def test_create_branch_unknown_project(self):
  1709. """Test the read-only function of pagure-admin on an unknown
  1710. project.
  1711. """
  1712. args = munch.Munch(
  1713. {
  1714. "project": "foob",
  1715. "user": None,
  1716. "new_branch": "new_branch",
  1717. "from_branch": "master",
  1718. "from_commit": None,
  1719. "action_user": "pingou",
  1720. }
  1721. )
  1722. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1723. pagure.cli.admin.do_create_branch(args)
  1724. self.assertEqual(
  1725. cm.exception.args[0],
  1726. "No project found with: project=foob, user=None",
  1727. )
  1728. def test_create_branch_invalid_project(self):
  1729. """Test the read-only function of pagure-admin on an invalid
  1730. project.
  1731. """
  1732. args = munch.Munch(
  1733. {
  1734. "project": "f/o/o/b",
  1735. "user": None,
  1736. "new_branch": "new_branch",
  1737. "from_branch": "master",
  1738. "from_commit": None,
  1739. "action_user": "pingou",
  1740. }
  1741. )
  1742. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1743. pagure.cli.admin.do_create_branch(args)
  1744. self.assertEqual(
  1745. cm.exception.args[0],
  1746. 'Invalid project name, has more than one "/": f/o/o/b',
  1747. )
  1748. def test_create_branch_commit_and_branch_from(self):
  1749. """Test the read-only function of pagure-admin to get status of
  1750. a non-namespaced project.
  1751. """
  1752. args = munch.Munch(
  1753. {
  1754. "project": "test",
  1755. "user": None,
  1756. "new_branch": "new_branch",
  1757. "from_branch": "master",
  1758. "from_commit": "foobar",
  1759. "action_user": "pingou",
  1760. }
  1761. )
  1762. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1763. pagure.cli.admin.do_create_branch(args)
  1764. self.assertEqual(
  1765. cm.exception.args[0],
  1766. "You must create the branch from something, either a commit "
  1767. "or another branch, not from both",
  1768. )
  1769. def test_create_branch_no_branch_from(self):
  1770. """Test the read-only function of pagure-admin to get status of
  1771. a non-namespaced project.
  1772. """
  1773. args = munch.Munch(
  1774. {
  1775. "project": "test",
  1776. "user": None,
  1777. "new_branch": "new_branch",
  1778. "from_branch": "master",
  1779. "from_commit": None,
  1780. "action_user": "pingou",
  1781. }
  1782. )
  1783. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1784. pagure.cli.admin.do_create_branch(args)
  1785. self.assertEqual(
  1786. cm.exception.args[0], 'The "master" branch does not exist'
  1787. )
  1788. def test_create_branch_no_commit_from(self):
  1789. """Test the read-only function of pagure-admin to get status of
  1790. a non-namespaced project.
  1791. """
  1792. args = munch.Munch(
  1793. {
  1794. "project": "test",
  1795. "user": None,
  1796. "new_branch": "new_branch",
  1797. "from_branch": None,
  1798. "from_commit": "foobar",
  1799. "action_user": "pingou",
  1800. }
  1801. )
  1802. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1803. pagure.cli.admin.do_create_branch(args)
  1804. self.assertEqual(
  1805. cm.exception.args[0], "No commit foobar found from which to branch"
  1806. )
  1807. def test_create_branch_from_branch(self):
  1808. """ Test the do_create_admin_token function of pagure-admin. """
  1809. gitrepo_path = os.path.join(self.path, "repos", "test.git")
  1810. tests.add_content_git_repo(gitrepo_path)
  1811. # Check branch before:
  1812. gitrepo = pygit2.Repository(gitrepo_path)
  1813. self.assertEqual(gitrepo.listall_branches(), ["master"])
  1814. args = munch.Munch(
  1815. {
  1816. "project": "test",
  1817. "user": None,
  1818. "new_branch": "new_branch",
  1819. "from_branch": "master",
  1820. "from_commit": None,
  1821. "action_user": "pingou",
  1822. }
  1823. )
  1824. with tests.capture_output() as output:
  1825. pagure.cli.admin.do_create_branch(args)
  1826. output = output.getvalue()
  1827. self.assertEqual("Branch created\n", output)
  1828. # Check branch after:
  1829. gitrepo = pygit2.Repository(gitrepo_path)
  1830. self.assertEqual(
  1831. sorted(gitrepo.listall_branches()), ["master", "new_branch"]
  1832. )
  1833. class PagureSetDefaultBranchTests(tests.Modeltests):
  1834. """ Tests for pagure-admin set-default-branch """
  1835. populate_db = True
  1836. def setUp(self):
  1837. """ Set up the environment, run before every tests. """
  1838. super(PagureSetDefaultBranchTests, self).setUp()
  1839. # Create a couple of projects
  1840. tests.create_projects(self.session)
  1841. # Create their git repo
  1842. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1843. # Make the imported pagure use the correct db session
  1844. pagure.cli.admin.session = self.session
  1845. def test_set_default_branch_unknown_project(self):
  1846. """Test the set-default-branch function of pagure-admin on an unknown
  1847. project.
  1848. """
  1849. args = munch.Munch(
  1850. {"project": "foob", "user": None, "branch": "master"}
  1851. )
  1852. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1853. pagure.cli.admin.do_set_default_branch(args)
  1854. self.assertEqual(
  1855. cm.exception.args[0],
  1856. "No project found with: project=foob, user=None",
  1857. )
  1858. def test_set_default_branch_invalid_project(self):
  1859. """Test the set-default-branch function of pagure-admin on an invalid
  1860. project.
  1861. """
  1862. args = munch.Munch(
  1863. {"project": "f/o/o/b", "user": None, "branch": "master"}
  1864. )
  1865. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1866. pagure.cli.admin.do_set_default_branch(args)
  1867. self.assertEqual(
  1868. cm.exception.args[0],
  1869. 'Invalid project name, has more than one "/": f/o/o/b',
  1870. )
  1871. def test_set_default_branch_unknown_branch(self):
  1872. """Test the set-default-branch function of pagure-admin on an unknown
  1873. branch.
  1874. """
  1875. args = munch.Munch({"project": "test", "user": None, "branch": "foob"})
  1876. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1877. pagure.cli.admin.do_set_default_branch(args)
  1878. self.assertEqual(
  1879. cm.exception.args[0], "No foob branch found on project: test"
  1880. )
  1881. def test_set_default_branch_invalid_branch(self):
  1882. """Test the set-default-branch function of pagure-admin on an invalid branch."""
  1883. args = munch.Munch(
  1884. {"project": "test", "user": None, "branch": "~invalid~"}
  1885. )
  1886. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  1887. pagure.cli.admin.do_set_default_branch(args)
  1888. self.assertEqual(
  1889. cm.exception.args[0], "No ~invalid~ branch found on project: test"
  1890. )
  1891. def test_set_default_branch(self):
  1892. """ Test the set-default-branch funcion of pagure-admin. """
  1893. gitrepo_path = os.path.join(self.path, "repos", "test.git")
  1894. tests.add_content_git_repo(gitrepo_path)
  1895. tests.add_commit_git_repo(gitrepo_path, branch="dev")
  1896. # Check default branch before:
  1897. gitrepo = pygit2.Repository(gitrepo_path)
  1898. self.assertEqual(gitrepo.head.shorthand, "master")
  1899. args = munch.Munch({"project": "test", "user": None, "branch": "dev"})
  1900. with tests.capture_output() as output:
  1901. pagure.cli.admin.do_set_default_branch(args)
  1902. output = output.getvalue()
  1903. self.assertEqual("Branch dev set as default\n", output)
  1904. # Check default branch after:
  1905. self.assertEqual(gitrepo.head.shorthand, "dev")
  1906. class PagureAdminUpdateAclsTests(tests.Modeltests):
  1907. """ Tests for pagure-admin update-acls """
  1908. populate_db = False
  1909. maxDiff = None
  1910. def setUp(self):
  1911. """ Set up the environnment, ran before every tests. """
  1912. super(PagureAdminUpdateAclsTests, self).setUp()
  1913. pagure.cli.admin.session = self.session
  1914. # Make the imported pagure use the correct db session
  1915. pagure.cli.admin.session = self.session
  1916. def test_update_acls(self):
  1917. """Test the update-acls function of pagure-admin."""
  1918. args = munch.Munch()
  1919. with tests.capture_output() as output:
  1920. pagure.cli.admin.do_update_acls(args)
  1921. output = output.getvalue()
  1922. self.assertEqual(
  1923. "ACLS in the database synced with the list in the configuration file\n",
  1924. output,
  1925. )
  1926. acls = pagure.lib.query.get_acls(self.session)
  1927. self.assertEqual(
  1928. [a.name for a in acls],
  1929. [
  1930. "commit",
  1931. "commit_flag",
  1932. "create_branch",
  1933. "create_git_alias",
  1934. "create_project",
  1935. "delete_git_alias",
  1936. "fork_project",
  1937. "generate_acls_project",
  1938. "internal_access",
  1939. "issue_assign",
  1940. "issue_change_status",
  1941. "issue_comment",
  1942. "issue_create",
  1943. "issue_subscribe",
  1944. "issue_update",
  1945. "issue_update_custom_fields",
  1946. "issue_update_milestone",
  1947. "modify_git_alias",
  1948. "modify_project",
  1949. "pull_request_assign",
  1950. "pull_request_close",
  1951. "pull_request_comment",
  1952. "pull_request_create",
  1953. "pull_request_flag",
  1954. "pull_request_merge",
  1955. "pull_request_rebase",
  1956. "pull_request_subscribe",
  1957. "pull_request_update",
  1958. "tag_project",
  1959. "update_watch_status",
  1960. ],
  1961. )
  1962. @patch.dict(
  1963. "pagure.cli.admin._config",
  1964. {"ACLS": {"dummy_acls": "Dummy ACL for testing"}},
  1965. )
  1966. def test_update_acls_new_acls(self):
  1967. args = munch.Munch({"debug": True})
  1968. with tests.capture_output() as output:
  1969. pagure.cli.admin.do_update_acls(args)
  1970. output = output.getvalue()
  1971. self.assertEqual(
  1972. "ACLS in the database synced with the list in the configuration file\n",
  1973. output,
  1974. )
  1975. acls = pagure.lib.query.get_acls(self.session)
  1976. self.assertEqual(
  1977. [a.name for a in acls],
  1978. [
  1979. "commit",
  1980. "commit_flag",
  1981. "create_branch",
  1982. "create_git_alias",
  1983. "create_project",
  1984. "delete_git_alias",
  1985. "dummy_acls",
  1986. "fork_project",
  1987. "generate_acls_project",
  1988. "internal_access",
  1989. "issue_assign",
  1990. "issue_change_status",
  1991. "issue_comment",
  1992. "issue_create",
  1993. "issue_subscribe",
  1994. "issue_update",
  1995. "issue_update_custom_fields",
  1996. "issue_update_milestone",
  1997. "modify_git_alias",
  1998. "modify_project",
  1999. "pull_request_assign",
  2000. "pull_request_close",
  2001. "pull_request_comment",
  2002. "pull_request_create",
  2003. "pull_request_flag",
  2004. "pull_request_merge",
  2005. "pull_request_rebase",
  2006. "pull_request_subscribe",
  2007. "pull_request_update",
  2008. "tag_project",
  2009. "update_watch_status",
  2010. ],
  2011. )
  2012. if __name__ == "__main__":
  2013. unittest.main(verbosity=2)