1
0

test_pagure_admin.py 51 KB

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