1
0

test_pagure_admin.py 65 KB

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