1
0

test_pagure_admin.py 67 KB

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