test_pagure_admin.py 56 KB

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