test_pagure_admin.py 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources # noqa
  9. import datetime # noqa
  10. import os # noqa
  11. import platform # noqa
  12. import shutil # noqa
  13. import subprocess # noqa
  14. import sys # noqa
  15. import unittest # noqa
  16. import munch # noqa
  17. from mock import patch, MagicMock # 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. })
  54. with tests.capture_output() as output:
  55. pagure.cli.admin.do_list_admin_token(list_args)
  56. output = output.getvalue()
  57. self.assertEqual(output, 'No admin tokens found\n')
  58. class PagureAdminAdminRefreshGitolitetests(tests.Modeltests):
  59. """ Tests for pagure-admin refresh-gitolite """
  60. populate_db = False
  61. def setUp(self):
  62. """ Set up the environnment, ran before every tests. """
  63. super(PagureAdminAdminRefreshGitolitetests, self).setUp()
  64. pagure.cli.admin.session = self.session
  65. # Create the user pingou
  66. item = pagure.lib.model.User(
  67. user='pingou',
  68. fullname='PY C',
  69. password='foo',
  70. default_email='bar@pingou.com',
  71. )
  72. self.session.add(item)
  73. item = pagure.lib.model.UserEmail(
  74. user_id=1,
  75. email='bar@pingou.com')
  76. self.session.add(item)
  77. self.session.commit()
  78. # Create a couple of projects
  79. tests.create_projects(self.session)
  80. # Add a group
  81. msg = pagure.lib.add_group(
  82. self.session,
  83. group_name='foo',
  84. display_name='foo group',
  85. description=None,
  86. group_type='bar',
  87. user='pingou',
  88. is_admin=False,
  89. blacklist=[],
  90. )
  91. self.session.commit()
  92. self.assertEqual(msg, 'User `pingou` added to the group `foo`.')
  93. # Make the imported pagure use the correct db session
  94. pagure.cli.admin.session = self.session
  95. @patch('pagure.cli.admin._ask_confirmation')
  96. @patch('pagure.lib.git_auth.get_git_auth_helper')
  97. def test_do_refresh_gitolite_no_args(self, get_helper, conf):
  98. """ Test the do_generate_acl function with no special args. """
  99. conf.return_value = True
  100. helper = MagicMock()
  101. get_helper.return_value = helper
  102. args = munch.Munch(
  103. {'group': None, 'project': None, 'all_': False, 'user': None})
  104. pagure.cli.admin.do_generate_acl(args)
  105. get_helper.assert_called_with('gitolite3')
  106. args = helper.generate_acls.call_args
  107. self.assertIsNone(args[1].get('group'))
  108. self.assertIsNone(args[1].get('project'))
  109. @patch('pagure.cli.admin._ask_confirmation')
  110. @patch('pagure.lib.git_auth.get_git_auth_helper')
  111. def test_do_refresh_gitolite_all_project(self, get_helper, conf):
  112. """ Test the do_generate_acl function for all projects. """
  113. conf.return_value = True
  114. helper = MagicMock()
  115. get_helper.return_value = helper
  116. args = munch.Munch(
  117. {'group': None, 'project': None, 'all_': True, 'user': None})
  118. pagure.cli.admin.do_generate_acl(args)
  119. get_helper.assert_called_with('gitolite3')
  120. args = helper.generate_acls.call_args
  121. self.assertIsNone(args[1].get('group'))
  122. self.assertEqual(args[1].get('project'), -1)
  123. @patch('pagure.cli.admin._ask_confirmation')
  124. @patch('pagure.lib.git_auth.get_git_auth_helper')
  125. def test_do_refresh_gitolite_one_project(self, get_helper, conf):
  126. """ Test the do_generate_acl function for a certain project. """
  127. conf.return_value = True
  128. helper = MagicMock()
  129. get_helper.return_value = helper
  130. args = munch.Munch(
  131. {'group': None, 'project': 'test', 'all_': False, 'user': None})
  132. pagure.cli.admin.do_generate_acl(args)
  133. get_helper.assert_called_with('gitolite3')
  134. args = helper.generate_acls.call_args
  135. self.assertIsNone(args[1].get('group'))
  136. self.assertEqual(args[1].get('project').fullname, 'test')
  137. @patch('pagure.cli.admin._ask_confirmation')
  138. @patch('pagure.lib.git_auth.get_git_auth_helper')
  139. def test_do_refresh_gitolite_one_project_and_all(self, get_helper, conf):
  140. """ Test the do_generate_acl function for a certain project and all.
  141. """
  142. conf.return_value = True
  143. helper = MagicMock()
  144. get_helper.return_value = helper
  145. args = munch.Munch(
  146. {'group': None, 'project': 'test', 'all_': True, 'user': None})
  147. pagure.cli.admin.do_generate_acl(args)
  148. get_helper.assert_called_with('gitolite3')
  149. args = helper.generate_acls.call_args
  150. self.assertIsNone(args[1].get('group'))
  151. self.assertEqual(args[1].get('project'), -1)
  152. @patch('pagure.cli.admin._ask_confirmation')
  153. @patch('pagure.lib.git_auth.get_git_auth_helper')
  154. def test_do_refresh_gitolite_one_group(self, get_helper, conf):
  155. """ Test the do_generate_acl function for a certain group. """
  156. conf.return_value = True
  157. helper = MagicMock()
  158. get_helper.return_value = helper
  159. args = munch.Munch(
  160. {'group': 'foo', 'project': None, 'all_': False, 'user': None})
  161. pagure.cli.admin.do_generate_acl(args)
  162. get_helper.assert_called_with('gitolite3')
  163. args = helper.generate_acls.call_args
  164. self.assertEqual(args[1].get('group').group_name, 'foo')
  165. self.assertIsNone(args[1].get('project'))
  166. class PagureAdminAdminTokentests(tests.Modeltests):
  167. """ Tests for pagure-admin admin-token """
  168. populate_db = False
  169. def setUp(self):
  170. """ Set up the environnment, ran before every tests. """
  171. super(PagureAdminAdminTokentests, self).setUp()
  172. pagure.cli.admin.session = self.session
  173. # Create the user pingou
  174. item = pagure.lib.model.User(
  175. user='pingou',
  176. fullname='PY C',
  177. password='foo',
  178. default_email='bar@pingou.com',
  179. )
  180. self.session.add(item)
  181. item = pagure.lib.model.UserEmail(
  182. user_id=1,
  183. email='bar@pingou.com')
  184. self.session.add(item)
  185. self.session.commit()
  186. # Make the imported pagure use the correct db session
  187. pagure.cli.admin.session = self.session
  188. @patch('pagure.cli.admin._get_input')
  189. @patch('pagure.cli.admin._ask_confirmation')
  190. def test_do_create_admin_token(self, conf, rinp):
  191. """ Test the do_create_admin_token function of pagure-admin. """
  192. conf.return_value = True
  193. rinp.return_value = '1,2,3'
  194. args = munch.Munch({'user': 'pingou'})
  195. pagure.cli.admin.do_create_admin_token(args)
  196. # Check the outcome
  197. list_args = munch.Munch({
  198. 'user': None,
  199. 'token': None,
  200. 'active': False,
  201. 'expired': False,
  202. })
  203. with tests.capture_output() as output:
  204. pagure.cli.admin.do_list_admin_token(list_args)
  205. output = output.getvalue()
  206. self.assertNotEqual(output, 'No user "pingou" found\n')
  207. self.assertEqual(len(output.split('\n')), 2)
  208. self.assertIn(' -- pingou -- ', output)
  209. @patch('pagure.cli.admin._get_input')
  210. @patch('pagure.cli.admin._ask_confirmation')
  211. def test_do_list_admin_token(self, conf, rinp):
  212. """ Test the do_list_admin_token function of pagure-admin. """
  213. # Create an admin token to use
  214. conf.return_value = True
  215. rinp.return_value = '1,2,3'
  216. args = munch.Munch({'user': 'pingou'})
  217. pagure.cli.admin.do_create_admin_token(args)
  218. # Retrieve all tokens
  219. list_args = munch.Munch({
  220. 'user': None,
  221. 'token': None,
  222. 'active': False,
  223. 'expired': False,
  224. })
  225. with tests.capture_output() as output:
  226. pagure.cli.admin.do_list_admin_token(list_args)
  227. output = output.getvalue()
  228. self.assertNotEqual(output, 'No user "pingou" found\n')
  229. self.assertEqual(len(output.split('\n')), 2)
  230. self.assertIn(' -- pingou -- ', output)
  231. # Retrieve pfrields's tokens
  232. list_args = munch.Munch({
  233. 'user': 'pfrields',
  234. 'token': None,
  235. 'active': False,
  236. 'expired': False,
  237. })
  238. with tests.capture_output() as output:
  239. pagure.cli.admin.do_list_admin_token(list_args)
  240. output = output.getvalue()
  241. self.assertEqual(output, 'No admin tokens found\n')
  242. @patch('pagure.cli.admin._get_input')
  243. @patch('pagure.cli.admin._ask_confirmation')
  244. def test_do_info_admin_token(self, conf, rinp):
  245. """ Test the do_info_admin_token function of pagure-admin. """
  246. # Create an admin token to use
  247. conf.return_value = True
  248. rinp.return_value = '1,3,4'
  249. args = munch.Munch({'user': 'pingou'})
  250. pagure.cli.admin.do_create_admin_token(args)
  251. # Retrieve the token
  252. list_args = munch.Munch({
  253. 'user': None,
  254. 'token': None,
  255. 'active': False,
  256. 'expired': False,
  257. })
  258. with tests.capture_output() as output:
  259. pagure.cli.admin.do_list_admin_token(list_args)
  260. output = output.getvalue()
  261. self.assertNotEqual(output, 'No user "pingou" found\n')
  262. self.assertEqual(len(output.split('\n')), 2)
  263. self.assertIn(' -- pingou -- ', output)
  264. token = output.split(' ', 1)[0]
  265. args = munch.Munch({'token': token})
  266. with tests.capture_output() as output:
  267. pagure.cli.admin.do_info_admin_token(args)
  268. output = output.getvalue()
  269. self.assertIn(' -- pingou -- ', output.split('\n', 1)[0])
  270. self.assertEqual(
  271. output.split('\n', 1)[1], '''ACLs:
  272. - issue_create
  273. - pull_request_comment
  274. - pull_request_flag
  275. ''')
  276. @patch('pagure.cli.admin._get_input')
  277. @patch('pagure.cli.admin._ask_confirmation')
  278. def test_do_expire_admin_token(self, conf, rinp):
  279. """ Test the do_expire_admin_token function of pagure-admin. """
  280. if 'BUILD_ID' in os.environ:
  281. raise unittest.case.SkipTest('Skipping on jenkins/el7')
  282. # Create an admin token to use
  283. conf.return_value = True
  284. rinp.return_value = '1,2,3'
  285. args = munch.Munch({'user': 'pingou'})
  286. pagure.cli.admin.do_create_admin_token(args)
  287. # Retrieve the token
  288. list_args = munch.Munch({
  289. 'user': None,
  290. 'token': None,
  291. 'active': False,
  292. 'expired': False,
  293. })
  294. with tests.capture_output() as output:
  295. pagure.cli.admin.do_list_admin_token(list_args)
  296. output = output.getvalue()
  297. self.assertNotEqual(output, 'No user "pingou" found\n')
  298. self.assertEqual(len(output.split('\n')), 2)
  299. self.assertIn(' -- pingou -- ', output)
  300. token = output.split(' ', 1)[0]
  301. # Before
  302. list_args = munch.Munch({
  303. 'user': None,
  304. 'token': None,
  305. 'active': True,
  306. 'expired': False,
  307. })
  308. with tests.capture_output() as output:
  309. pagure.cli.admin.do_list_admin_token(list_args)
  310. output = output.getvalue()
  311. self.assertNotEqual(output, 'No admin tokens found\n')
  312. self.assertEqual(len(output.split('\n')), 2)
  313. self.assertIn(' -- pingou -- ', output)
  314. # Expire the token
  315. args = munch.Munch({'token': token})
  316. pagure.cli.admin.do_expire_admin_token(args)
  317. # After
  318. list_args = munch.Munch({
  319. 'user': None,
  320. 'token': None,
  321. 'active': True,
  322. 'expired': False,
  323. })
  324. with tests.capture_output() as output:
  325. pagure.cli.admin.do_list_admin_token(list_args)
  326. output = output.getvalue()
  327. self.assertEqual(output, 'No admin tokens found\n')
  328. @patch('pagure.cli.admin._get_input')
  329. @patch('pagure.cli.admin._ask_confirmation')
  330. def test_do_update_admin_token_invalid_date(self, conf, rinp):
  331. """ Test the do_update_admin_token function of pagure-admin with
  332. an invalid date. """
  333. if 'BUILD_ID' in os.environ:
  334. raise unittest.case.SkipTest('Skipping on jenkins/el7')
  335. # Create an admin token to use
  336. conf.return_value = True
  337. rinp.return_value = '1,2,3'
  338. args = munch.Munch({'user': 'pingou'})
  339. pagure.cli.admin.do_create_admin_token(args)
  340. # Retrieve the token
  341. list_args = munch.Munch({
  342. 'user': None,
  343. 'token': None,
  344. 'active': False,
  345. 'expired': False,
  346. })
  347. with tests.capture_output() as output:
  348. pagure.cli.admin.do_list_admin_token(list_args)
  349. output = output.getvalue()
  350. self.assertNotEqual(output, 'No user "pingou" found\n')
  351. self.assertEqual(len(output.split('\n')), 2)
  352. self.assertIn(' -- pingou -- ', output)
  353. token = output.split(' ', 1)[0]
  354. current_expiration = output.split(' ', 1)[1]
  355. # Set the expiration date to the token
  356. args = munch.Munch({'token': token, 'date': 'aa-bb-cc'})
  357. self.assertRaises(
  358. pagure.exceptions.PagureException,
  359. pagure.cli.admin.do_update_admin_token,
  360. args
  361. )
  362. @patch('pagure.cli.admin._get_input')
  363. @patch('pagure.cli.admin._ask_confirmation')
  364. def test_do_update_admin_token_invalid_date2(self, conf, rinp):
  365. """ Test the do_update_admin_token function of pagure-admin with
  366. an invalid date. """
  367. if 'BUILD_ID' in os.environ:
  368. raise unittest.case.SkipTest('Skipping on jenkins/el7')
  369. # Create an admin token to use
  370. conf.return_value = True
  371. rinp.return_value = '1,2,3'
  372. args = munch.Munch({'user': 'pingou'})
  373. pagure.cli.admin.do_create_admin_token(args)
  374. # Retrieve the token
  375. list_args = munch.Munch({
  376. 'user': None,
  377. 'token': None,
  378. 'active': False,
  379. 'expired': False,
  380. })
  381. with tests.capture_output() as output:
  382. pagure.cli.admin.do_list_admin_token(list_args)
  383. output = output.getvalue()
  384. self.assertNotEqual(output, 'No user "pingou" found\n')
  385. self.assertEqual(len(output.split('\n')), 2)
  386. self.assertIn(' -- pingou -- ', output)
  387. token = output.split(' ', 1)[0]
  388. current_expiration = output.split(' ', 1)[1]
  389. # Set the expiration date to the token
  390. args = munch.Munch({'token': token, 'date': '2017-18-01'})
  391. self.assertRaises(
  392. pagure.exceptions.PagureException,
  393. pagure.cli.admin.do_update_admin_token,
  394. args
  395. )
  396. @patch('pagure.cli.admin._get_input')
  397. @patch('pagure.cli.admin._ask_confirmation')
  398. def test_do_update_admin_token_invalid_date3(self, conf, rinp):
  399. """ Test the do_update_admin_token function of pagure-admin with
  400. an invalid date (is today). """
  401. if 'BUILD_ID' in os.environ:
  402. raise unittest.case.SkipTest('Skipping on jenkins/el7')
  403. # Create an admin token to use
  404. conf.return_value = True
  405. rinp.return_value = '1,2,3'
  406. args = munch.Munch({'user': 'pingou'})
  407. pagure.cli.admin.do_create_admin_token(args)
  408. # Retrieve the token
  409. list_args = munch.Munch({
  410. 'user': None,
  411. 'token': None,
  412. 'active': False,
  413. 'expired': False,
  414. })
  415. with tests.capture_output() as output:
  416. pagure.cli.admin.do_list_admin_token(list_args)
  417. output = output.getvalue()
  418. self.assertNotEqual(output, 'No user "pingou" found\n')
  419. self.assertEqual(len(output.split('\n')), 2)
  420. self.assertIn(' -- pingou -- ', output)
  421. token = output.split(' ', 1)[0]
  422. current_expiration = output.split(' ', 1)[1]
  423. # Set the expiration date to the token
  424. args = munch.Munch({
  425. 'token': token, 'date': datetime.datetime.utcnow().date()
  426. })
  427. self.assertRaises(
  428. pagure.exceptions.PagureException,
  429. pagure.cli.admin.do_update_admin_token,
  430. args
  431. )
  432. @patch('pagure.cli.admin._get_input')
  433. @patch('pagure.cli.admin._ask_confirmation')
  434. def test_do_update_admin_token(self, conf, rinp):
  435. """ Test the do_update_admin_token function of pagure-admin. """
  436. if 'BUILD_ID' in os.environ:
  437. raise unittest.case.SkipTest('Skipping on jenkins/el7')
  438. # Create an admin token to use
  439. conf.return_value = True
  440. rinp.return_value = '1,2,3'
  441. args = munch.Munch({'user': 'pingou'})
  442. pagure.cli.admin.do_create_admin_token(args)
  443. # Retrieve the token
  444. list_args = munch.Munch({
  445. 'user': None,
  446. 'token': None,
  447. 'active': False,
  448. 'expired': False,
  449. })
  450. with tests.capture_output() as output:
  451. pagure.cli.admin.do_list_admin_token(list_args)
  452. output = output.getvalue()
  453. self.assertNotEqual(output, 'No user "pingou" found\n')
  454. self.assertEqual(len(output.split('\n')), 2)
  455. self.assertIn(' -- pingou -- ', output)
  456. token = output.split(' ', 1)[0]
  457. current_expiration = output.strip().split(' -- ', 2)[-1]
  458. # Before
  459. list_args = munch.Munch({
  460. 'user': None,
  461. 'token': None,
  462. 'active': True,
  463. 'expired': False,
  464. })
  465. with tests.capture_output() as output:
  466. pagure.cli.admin.do_list_admin_token(list_args)
  467. output = output.getvalue()
  468. self.assertNotEqual(output, 'No admin tokens found\n')
  469. self.assertEqual(len(output.split('\n')), 2)
  470. self.assertIn(' -- pingou -- ', output)
  471. deadline = datetime.datetime.utcnow().date() \
  472. + datetime.timedelta(days=3)
  473. # Set the expiration date to the token
  474. args = munch.Munch({
  475. 'token': token,
  476. 'date': deadline.strftime('%Y-%m-%d')
  477. })
  478. pagure.cli.admin.do_update_admin_token(args)
  479. # After
  480. list_args = munch.Munch({
  481. 'user': None,
  482. 'token': None,
  483. 'active': True,
  484. 'expired': False,
  485. })
  486. with tests.capture_output() as output:
  487. pagure.cli.admin.do_list_admin_token(list_args)
  488. output = output.getvalue()
  489. self.assertEqual(output.split(' ', 1)[0], token)
  490. self.assertNotEqual(
  491. output.strip().split(' -- ', 2)[-1],
  492. current_expiration)
  493. class PagureAdminGetWatchTests(tests.Modeltests):
  494. """ Tests for pagure-admin get-watch """
  495. populate_db = False
  496. def setUp(self):
  497. """ Set up the environnment, ran before every tests. """
  498. super(PagureAdminGetWatchTests, self).setUp()
  499. pagure.cli.admin.session = self.session
  500. # Create the user pingou
  501. item = pagure.lib.model.User(
  502. user='pingou',
  503. fullname='PY C',
  504. password='foo',
  505. default_email='bar@pingou.com',
  506. )
  507. self.session.add(item)
  508. item = pagure.lib.model.UserEmail(
  509. user_id=1,
  510. email='bar@pingou.com')
  511. self.session.add(item)
  512. # Create the user foo
  513. item = pagure.lib.model.User(
  514. user='foo',
  515. fullname='foo B.',
  516. password='foob',
  517. default_email='foo@pingou.com',
  518. )
  519. self.session.add(item)
  520. # Create two projects for the user pingou
  521. item = pagure.lib.model.Project(
  522. user_id=1, # pingou
  523. name='test',
  524. description='namespaced test project',
  525. hook_token='aaabbbeee',
  526. namespace='somenamespace',
  527. )
  528. self.session.add(item)
  529. item = pagure.lib.model.Project(
  530. user_id=1, # pingou
  531. name='test',
  532. description='Test project',
  533. hook_token='aaabbbccc',
  534. namespace=None,
  535. )
  536. self.session.add(item)
  537. self.session.commit()
  538. # Make the imported pagure use the correct db session
  539. pagure.cli.admin.session = self.session
  540. def test_get_watch_get_project_unknown_project(self):
  541. """ Test the get-watch function of pagure-admin with an unknown
  542. project.
  543. """
  544. args = munch.Munch({
  545. 'project': 'foobar',
  546. 'user': 'pingou',
  547. })
  548. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  549. pagure.cli.admin.do_get_watch_status(args)
  550. self.assertEqual(
  551. cm.exception.args[0],
  552. 'No project found with: foobar'
  553. )
  554. def test_get_watch_get_project_invalid_project(self):
  555. """ Test the get-watch function of pagure-admin with an invalid
  556. project.
  557. """
  558. args = munch.Munch({
  559. 'project': 'fo/o/bar',
  560. 'user': 'pingou',
  561. })
  562. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  563. pagure.cli.admin.do_get_watch_status(args)
  564. self.assertEqual(
  565. cm.exception.args[0],
  566. 'Invalid project name, has more than one "/": fo/o/bar',
  567. )
  568. def test_get_watch_get_project_invalid_user(self):
  569. """ Test the get-watch function of pagure-admin on a invalid user.
  570. """
  571. args = munch.Munch({
  572. 'project': 'test',
  573. 'user': 'beebop',
  574. })
  575. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  576. pagure.cli.admin.do_get_watch_status(args)
  577. self.assertEqual(
  578. cm.exception.args[0],
  579. 'No user "beebop" found'
  580. )
  581. def test_get_watch_get_project(self):
  582. """ Test the get-watch function of pagure-admin on a regular project.
  583. """
  584. args = munch.Munch({
  585. 'project': 'test',
  586. 'user': 'pingou',
  587. })
  588. with tests.capture_output() as output:
  589. pagure.cli.admin.do_get_watch_status(args)
  590. output = output.getvalue()
  591. self.assertEqual(
  592. 'On test user: pingou is watching the following items: '
  593. 'issues, pull-requests\n', output)
  594. def test_get_watch_get_project_not_watching(self):
  595. """ Test the get-watch function of pagure-admin on a regular project.
  596. """
  597. args = munch.Munch({
  598. 'project': 'test',
  599. 'user': 'foo',
  600. })
  601. with tests.capture_output() as output:
  602. pagure.cli.admin.do_get_watch_status(args)
  603. output = output.getvalue()
  604. self.assertEqual(
  605. 'On test user: foo is watching the following items: None\n',
  606. output)
  607. def test_get_watch_get_project_namespaced(self):
  608. """ Test the get-watch function of pagure-admin on a namespaced project.
  609. """
  610. args = munch.Munch({
  611. 'project': 'somenamespace/test',
  612. 'user': 'pingou',
  613. })
  614. with tests.capture_output() as output:
  615. pagure.cli.admin.do_get_watch_status(args)
  616. output = output.getvalue()
  617. self.assertEqual(
  618. 'On somenamespace/test user: pingou is watching the following '
  619. 'items: issues, pull-requests\n', output)
  620. def test_get_watch_get_project_namespaced_not_watching(self):
  621. """ Test the get-watch function of pagure-admin on a namespaced project.
  622. """
  623. args = munch.Munch({
  624. 'project': 'somenamespace/test',
  625. 'user': 'foo',
  626. })
  627. with tests.capture_output() as output:
  628. pagure.cli.admin.do_get_watch_status(args)
  629. output = output.getvalue()
  630. with tests.capture_output() as _discarded:
  631. pagure.cli.admin.do_get_watch_status(args)
  632. self.assertEqual(
  633. 'On somenamespace/test user: foo is watching the following '
  634. 'items: None\n', output)
  635. class PagureAdminUpdateWatchTests(tests.Modeltests):
  636. """ Tests for pagure-admin update-watch """
  637. populate_db = False
  638. def setUp(self):
  639. """ Set up the environnment, ran before every tests. """
  640. super(PagureAdminUpdateWatchTests, self).setUp()
  641. pagure.cli.admin.session = self.session
  642. # Create the user pingou
  643. item = pagure.lib.model.User(
  644. user='pingou',
  645. fullname='PY C',
  646. password='foo',
  647. default_email='bar@pingou.com',
  648. )
  649. self.session.add(item)
  650. item = pagure.lib.model.UserEmail(
  651. user_id=1,
  652. email='bar@pingou.com')
  653. self.session.add(item)
  654. # Create the user foo
  655. item = pagure.lib.model.User(
  656. user='foo',
  657. fullname='foo B.',
  658. password='foob',
  659. default_email='foo@pingou.com',
  660. )
  661. self.session.add(item)
  662. # Create two projects for the user pingou
  663. item = pagure.lib.model.Project(
  664. user_id=1, # pingou
  665. name='test',
  666. description='namespaced test project',
  667. hook_token='aaabbbeee',
  668. namespace='somenamespace',
  669. )
  670. self.session.add(item)
  671. item = pagure.lib.model.Project(
  672. user_id=1, # pingou
  673. name='test',
  674. description='Test project',
  675. hook_token='aaabbbccc',
  676. namespace=None,
  677. )
  678. self.session.add(item)
  679. self.session.commit()
  680. # Make the imported pagure use the correct db session
  681. pagure.cli.admin.session = self.session
  682. def test_get_watch_update_project_unknown_project(self):
  683. """ Test the update-watch function of pagure-admin on an unknown
  684. project.
  685. """
  686. args = munch.Munch({
  687. 'project': 'foob',
  688. 'user': 'pingou',
  689. 'status': '1'
  690. })
  691. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  692. pagure.cli.admin.do_update_watch_status(args)
  693. self.assertEqual(
  694. cm.exception.args[0],
  695. 'No project found with: foob'
  696. )
  697. def test_get_watch_update_project_invalid_project(self):
  698. """ Test the update-watch function of pagure-admin on an invalid
  699. project.
  700. """
  701. args = munch.Munch({
  702. 'project': 'fo/o/b',
  703. 'user': 'pingou',
  704. 'status': '1'
  705. })
  706. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  707. pagure.cli.admin.do_update_watch_status(args)
  708. self.assertEqual(
  709. cm.exception.args[0],
  710. 'Invalid project name, has more than one "/": fo/o/b',
  711. )
  712. def test_get_watch_update_project_invalid_user(self):
  713. """ Test the update-watch function of pagure-admin on an invalid user.
  714. """
  715. args = munch.Munch({
  716. 'project': 'test',
  717. 'user': 'foob',
  718. 'status': '1'
  719. })
  720. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  721. pagure.cli.admin.do_update_watch_status(args)
  722. self.assertEqual(
  723. cm.exception.args[0],
  724. 'No user "foob" found'
  725. )
  726. def test_get_watch_update_project_invalid_status(self):
  727. """ Test the update-watch function of pagure-admin with an invalid
  728. status.
  729. """
  730. args = munch.Munch({
  731. 'project': 'test',
  732. 'user': 'pingou',
  733. 'status': '10'
  734. })
  735. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  736. pagure.cli.admin.do_update_watch_status(args)
  737. self.assertEqual(
  738. cm.exception.args[0],
  739. 'Invalid status provided: 10 not in -1, 0, 1, 2, 3'
  740. )
  741. def test_get_watch_update_project_no_effect(self):
  742. """ Test the update-watch function of pagure-admin with a regular
  743. project - nothing changed.
  744. """
  745. args = munch.Munch({
  746. 'project': 'test',
  747. 'user': 'pingou',
  748. })
  749. with tests.capture_output() as output:
  750. pagure.cli.admin.do_get_watch_status(args)
  751. output = output.getvalue()
  752. self.assertEqual(
  753. 'On test user: pingou is watching the following items: '
  754. 'issues, pull-requests\n', output)
  755. args = munch.Munch({
  756. 'project': 'test',
  757. 'user': 'pingou',
  758. 'status': '1'
  759. })
  760. with tests.capture_output() as output:
  761. pagure.cli.admin.do_update_watch_status(args)
  762. output = output.getvalue()
  763. self.assertEqual(
  764. 'Updating watch status of pingou to 1 (watch issues and PRs) '
  765. 'on test\n', output)
  766. args = munch.Munch({
  767. 'project': 'test',
  768. 'user': 'pingou',
  769. })
  770. with tests.capture_output() as output:
  771. pagure.cli.admin.do_get_watch_status(args)
  772. output = output.getvalue()
  773. self.assertEqual(
  774. 'On test user: pingou is watching the following items: '
  775. 'issues, pull-requests\n', output)
  776. class PagureAdminReadOnlyTests(tests.Modeltests):
  777. """ Tests for pagure-admin read-only """
  778. populate_db = False
  779. def setUp(self):
  780. """ Set up the environnment, ran before every tests. """
  781. super(PagureAdminReadOnlyTests, self).setUp()
  782. pagure.cli.admin.session = self.session
  783. # Create the user pingou
  784. item = pagure.lib.model.User(
  785. user='pingou',
  786. fullname='PY C',
  787. password='foo',
  788. default_email='bar@pingou.com',
  789. )
  790. self.session.add(item)
  791. item = pagure.lib.model.UserEmail(
  792. user_id=1,
  793. email='bar@pingou.com')
  794. self.session.add(item)
  795. # Create two projects for the user pingou
  796. item = pagure.lib.model.Project(
  797. user_id=1, # pingou
  798. name='test',
  799. description='namespaced test project',
  800. hook_token='aaabbbeee',
  801. namespace='somenamespace',
  802. )
  803. self.session.add(item)
  804. item = pagure.lib.model.Project(
  805. user_id=1, # pingou
  806. name='test',
  807. description='Test project',
  808. hook_token='aaabbbccc',
  809. namespace=None,
  810. )
  811. self.session.add(item)
  812. self.session.commit()
  813. # Make the imported pagure use the correct db session
  814. pagure.cli.admin.session = self.session
  815. def test_read_only_unknown_project(self):
  816. """ Test the read-only function of pagure-admin on an unknown
  817. project.
  818. """
  819. args = munch.Munch({
  820. 'project': 'foob',
  821. 'user': None,
  822. 'ro': None,
  823. })
  824. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  825. pagure.cli.admin.do_read_only(args)
  826. self.assertEqual(
  827. cm.exception.args[0],
  828. 'No project found with: foob'
  829. )
  830. def test_read_only_invalid_project(self):
  831. """ Test the read-only function of pagure-admin on an invalid
  832. project.
  833. """
  834. args = munch.Munch({
  835. 'project': 'fo/o/b',
  836. 'user': None,
  837. 'ro': None,
  838. })
  839. with self.assertRaises(pagure.exceptions.PagureException) as cm:
  840. pagure.cli.admin.do_read_only(args)
  841. self.assertEqual(
  842. cm.exception.args[0],
  843. 'Invalid project name, has more than one "/": fo/o/b'
  844. )
  845. def test_read_only(self):
  846. """ Test the read-only function of pagure-admin to get status of
  847. a non-namespaced project.
  848. """
  849. args = munch.Munch({
  850. 'project': 'test',
  851. 'user': None,
  852. 'ro': None,
  853. })
  854. with tests.capture_output() as output:
  855. pagure.cli.admin.do_read_only(args)
  856. output = output.getvalue()
  857. self.assertEqual(
  858. 'The current read-only flag of the project test is set to True\n',
  859. output)
  860. def test_read_only_namespace(self):
  861. """ Test the read-only function of pagure-admin to get status of
  862. a namespaced project.
  863. """
  864. args = munch.Munch({
  865. 'project': 'somenamespace/test',
  866. 'user': None,
  867. 'ro': None,
  868. })
  869. with tests.capture_output() as output:
  870. pagure.cli.admin.do_read_only(args)
  871. output = output.getvalue()
  872. self.assertEqual(
  873. 'The current read-only flag of the project somenamespace/test '\
  874. 'is set to True\n', output)
  875. def test_read_only_namespace_changed(self):
  876. """ Test the read-only function of pagure-admin to set the status of
  877. a namespaced project.
  878. """
  879. # Before
  880. args = munch.Munch({
  881. 'project': 'somenamespace/test',
  882. 'user': None,
  883. 'ro': None,
  884. })
  885. with tests.capture_output() as output:
  886. pagure.cli.admin.do_read_only(args)
  887. output = output.getvalue()
  888. self.assertEqual(
  889. 'The current read-only flag of the project somenamespace/test '\
  890. 'is set to True\n', output)
  891. args = munch.Munch({
  892. 'project': 'somenamespace/test',
  893. 'user': None,
  894. 'ro': 'false',
  895. })
  896. with tests.capture_output() as output:
  897. pagure.cli.admin.do_read_only(args)
  898. output = output.getvalue()
  899. self.assertEqual(
  900. 'The read-only flag of the project somenamespace/test has been '
  901. 'set to False\n', output)
  902. # After
  903. args = munch.Munch({
  904. 'project': 'somenamespace/test',
  905. 'user': None,
  906. 'ro': None,
  907. })
  908. with tests.capture_output() as output:
  909. pagure.cli.admin.do_read_only(args)
  910. output = output.getvalue()
  911. self.assertEqual(
  912. 'The current read-only flag of the project somenamespace/test '\
  913. 'is set to False\n', output)
  914. def test_read_only_no_change(self):
  915. """ Test the read-only function of pagure-admin to set the status of
  916. a namespaced project.
  917. """
  918. # Before
  919. args = munch.Munch({
  920. 'project': 'test',
  921. 'user': None,
  922. 'ro': None,
  923. })
  924. with tests.capture_output() as output:
  925. pagure.cli.admin.do_read_only(args)
  926. output = output.getvalue()
  927. self.assertEqual(
  928. 'The current read-only flag of the project test '\
  929. 'is set to True\n', output)
  930. args = munch.Munch({
  931. 'project': 'test',
  932. 'user': None,
  933. 'ro': 'true',
  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 read-only flag of the project test has been '
  940. 'set to True\n', output)
  941. # After
  942. args = munch.Munch({
  943. 'project': 'test',
  944. 'user': None,
  945. 'ro': None,
  946. })
  947. with tests.capture_output() as output:
  948. pagure.cli.admin.do_read_only(args)
  949. output = output.getvalue()
  950. self.assertEqual(
  951. 'The current read-only flag of the project test '\
  952. 'is set to True\n', output)
  953. if __name__ == '__main__':
  954. unittest.main(verbosity=2)