1
0

test_pagure_admin.py 74 KB

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