test_pagure_admin.py 51 KB

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