12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109 |
- # -*- coding: utf-8 -*-
- """
- (c) 2017 - Copyright Red Hat Inc
- Authors:
- Pierre-Yves Chibon <pingou@pingoured.fr>
- """
- __requires__ = ['SQLAlchemy >= 0.8']
- import pkg_resources # noqa
- import datetime # noqa
- import os # noqa
- import platform # noqa
- import shutil # noqa
- import subprocess # noqa
- import sys # noqa
- import unittest # noqa
- import munch # noqa
- from mock import patch, MagicMock # noqa
- sys.path.insert(0, os.path.join(os.path.dirname(
- os.path.abspath(__file__)), '..'))
- import pagure.config # noqa
- import pagure.exceptions # noqa: E402
- import pagure.cli.admin # noqa
- import pagure.lib.model # noqa
- import tests # noqa
- class PagureAdminAdminTokenEmptytests(tests.Modeltests):
- """ Tests for pagure-admin admin-token when there is nothing in the DB
- """
- populate_db = False
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(PagureAdminAdminTokenEmptytests, self).setUp()
- pagure.cli.admin.session = self.session
- def test_do_create_admin_token_no_user(self):
- """ Test the do_create_admin_token function of pagure-admin without
- user.
- """
- args = munch.Munch({'user': "pingou"})
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_create_admin_token(args)
- self.assertEqual(
- cm.exception.args[0],
- 'No user "pingou" found'
- )
- def test_do_list_admin_token_empty(self):
- """ Test the do_list_admin_token function of pagure-admin when there
- are not tokens in the db.
- """
- list_args = munch.Munch({
- 'user': None,
- 'token': None,
- 'active': False,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertEqual(output, 'No admin tokens found\n')
- class PagureAdminAdminRefreshGitolitetests(tests.Modeltests):
- """ Tests for pagure-admin refresh-gitolite """
- populate_db = False
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(PagureAdminAdminRefreshGitolitetests, self).setUp()
- pagure.cli.admin.session = self.session
- # Create the user pingou
- item = pagure.lib.model.User(
- user='pingou',
- fullname='PY C',
- password='foo',
- default_email='bar@pingou.com',
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(
- user_id=1,
- email='bar@pingou.com')
- self.session.add(item)
- self.session.commit()
- # Create a couple of projects
- tests.create_projects(self.session)
- # Add a group
- msg = pagure.lib.add_group(
- self.session,
- group_name='foo',
- display_name='foo group',
- description=None,
- group_type='bar',
- user='pingou',
- is_admin=False,
- blacklist=[],
- )
- self.session.commit()
- self.assertEqual(msg, 'User `pingou` added to the group `foo`.')
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- @patch('pagure.cli.admin._ask_confirmation')
- @patch('pagure.lib.git_auth.get_git_auth_helper')
- def test_do_refresh_gitolite_no_args(self, get_helper, conf):
- """ Test the do_generate_acl function with no special args. """
- conf.return_value = True
- helper = MagicMock()
- get_helper.return_value = helper
- args = munch.Munch(
- {'group': None, 'project': None, 'all_': False, 'user': None})
- pagure.cli.admin.do_generate_acl(args)
- get_helper.assert_called_with('gitolite3')
- args = helper.generate_acls.call_args
- self.assertIsNone(args[1].get('group'))
- self.assertIsNone(args[1].get('project'))
- @patch('pagure.cli.admin._ask_confirmation')
- @patch('pagure.lib.git_auth.get_git_auth_helper')
- def test_do_refresh_gitolite_all_project(self, get_helper, conf):
- """ Test the do_generate_acl function for all projects. """
- conf.return_value = True
- helper = MagicMock()
- get_helper.return_value = helper
- args = munch.Munch(
- {'group': None, 'project': None, 'all_': True, 'user': None})
- pagure.cli.admin.do_generate_acl(args)
- get_helper.assert_called_with('gitolite3')
- args = helper.generate_acls.call_args
- self.assertIsNone(args[1].get('group'))
- self.assertEqual(args[1].get('project'), -1)
- @patch('pagure.cli.admin._ask_confirmation')
- @patch('pagure.lib.git_auth.get_git_auth_helper')
- def test_do_refresh_gitolite_one_project(self, get_helper, conf):
- """ Test the do_generate_acl function for a certain project. """
- conf.return_value = True
- helper = MagicMock()
- get_helper.return_value = helper
- args = munch.Munch(
- {'group': None, 'project': 'test', 'all_': False, 'user': None})
- pagure.cli.admin.do_generate_acl(args)
- get_helper.assert_called_with('gitolite3')
- args = helper.generate_acls.call_args
- self.assertIsNone(args[1].get('group'))
- self.assertEqual(args[1].get('project').fullname, 'test')
- @patch('pagure.cli.admin._ask_confirmation')
- @patch('pagure.lib.git_auth.get_git_auth_helper')
- def test_do_refresh_gitolite_one_project_and_all(self, get_helper, conf):
- """ Test the do_generate_acl function for a certain project and all.
- """
- conf.return_value = True
- helper = MagicMock()
- get_helper.return_value = helper
- args = munch.Munch(
- {'group': None, 'project': 'test', 'all_': True, 'user': None})
- pagure.cli.admin.do_generate_acl(args)
- get_helper.assert_called_with('gitolite3')
- args = helper.generate_acls.call_args
- self.assertIsNone(args[1].get('group'))
- self.assertEqual(args[1].get('project'), -1)
- @patch('pagure.cli.admin._ask_confirmation')
- @patch('pagure.lib.git_auth.get_git_auth_helper')
- def test_do_refresh_gitolite_one_group(self, get_helper, conf):
- """ Test the do_generate_acl function for a certain group. """
- conf.return_value = True
- helper = MagicMock()
- get_helper.return_value = helper
- args = munch.Munch(
- {'group': 'foo', 'project': None, 'all_': False, 'user': None})
- pagure.cli.admin.do_generate_acl(args)
- get_helper.assert_called_with('gitolite3')
- args = helper.generate_acls.call_args
- self.assertEqual(args[1].get('group').group_name, 'foo')
- self.assertIsNone(args[1].get('project'))
- class PagureAdminAdminTokentests(tests.Modeltests):
- """ Tests for pagure-admin admin-token """
- populate_db = False
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(PagureAdminAdminTokentests, self).setUp()
- pagure.cli.admin.session = self.session
- # Create the user pingou
- item = pagure.lib.model.User(
- user='pingou',
- fullname='PY C',
- password='foo',
- default_email='bar@pingou.com',
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(
- user_id=1,
- email='bar@pingou.com')
- self.session.add(item)
- self.session.commit()
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- @patch('pagure.cli.admin._get_input')
- @patch('pagure.cli.admin._ask_confirmation')
- def test_do_create_admin_token(self, conf, rinp):
- """ Test the do_create_admin_token function of pagure-admin. """
- conf.return_value = True
- rinp.return_value = '1,2,3'
- args = munch.Munch({'user': 'pingou'})
- pagure.cli.admin.do_create_admin_token(args)
- # Check the outcome
- list_args = munch.Munch({
- 'user': None,
- 'token': None,
- 'active': False,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split('\n')), 2)
- self.assertIn(' -- pingou -- ', output)
- @patch('pagure.cli.admin._get_input')
- @patch('pagure.cli.admin._ask_confirmation')
- def test_do_list_admin_token(self, conf, rinp):
- """ Test the do_list_admin_token function of pagure-admin. """
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = '1,2,3'
- args = munch.Munch({'user': 'pingou'})
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve all tokens
- list_args = munch.Munch({
- 'user': None,
- 'token': None,
- 'active': False,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split('\n')), 2)
- self.assertIn(' -- pingou -- ', output)
- # Retrieve pfrields's tokens
- list_args = munch.Munch({
- 'user': 'pfrields',
- 'token': None,
- 'active': False,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertEqual(output, 'No admin tokens found\n')
- @patch('pagure.cli.admin._get_input')
- @patch('pagure.cli.admin._ask_confirmation')
- def test_do_info_admin_token(self, conf, rinp):
- """ Test the do_info_admin_token function of pagure-admin. """
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = '1,3,4'
- args = munch.Munch({'user': 'pingou'})
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve the token
- list_args = munch.Munch({
- 'user': None,
- 'token': None,
- 'active': False,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split('\n')), 2)
- self.assertIn(' -- pingou -- ', output)
- token = output.split(' ', 1)[0]
- args = munch.Munch({'token': token})
- with tests.capture_output() as output:
- pagure.cli.admin.do_info_admin_token(args)
- output = output.getvalue()
- self.assertIn(' -- pingou -- ', output.split('\n', 1)[0])
- self.assertEqual(
- output.split('\n', 1)[1], '''ACLs:
- - issue_create
- - pull_request_comment
- - pull_request_flag
- ''')
- @patch('pagure.cli.admin._get_input')
- @patch('pagure.cli.admin._ask_confirmation')
- def test_do_expire_admin_token(self, conf, rinp):
- """ Test the do_expire_admin_token function of pagure-admin. """
- if 'BUILD_ID' in os.environ:
- raise unittest.case.SkipTest('Skipping on jenkins/el7')
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = '1,2,3'
- args = munch.Munch({'user': 'pingou'})
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve the token
- list_args = munch.Munch({
- 'user': None,
- 'token': None,
- 'active': False,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split('\n')), 2)
- self.assertIn(' -- pingou -- ', output)
- token = output.split(' ', 1)[0]
- # Before
- list_args = munch.Munch({
- 'user': None,
- 'token': None,
- 'active': True,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No admin tokens found\n')
- self.assertEqual(len(output.split('\n')), 2)
- self.assertIn(' -- pingou -- ', output)
- # Expire the token
- args = munch.Munch({'token': token})
- pagure.cli.admin.do_expire_admin_token(args)
- # After
- list_args = munch.Munch({
- 'user': None,
- 'token': None,
- 'active': True,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertEqual(output, 'No admin tokens found\n')
- @patch('pagure.cli.admin._get_input')
- @patch('pagure.cli.admin._ask_confirmation')
- def test_do_update_admin_token_invalid_date(self, conf, rinp):
- """ Test the do_update_admin_token function of pagure-admin with
- an invalid date. """
- if 'BUILD_ID' in os.environ:
- raise unittest.case.SkipTest('Skipping on jenkins/el7')
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = '1,2,3'
- args = munch.Munch({'user': 'pingou'})
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve the token
- list_args = munch.Munch({
- 'user': None,
- 'token': None,
- 'active': False,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split('\n')), 2)
- self.assertIn(' -- pingou -- ', output)
- token = output.split(' ', 1)[0]
- current_expiration = output.split(' ', 1)[1]
- # Set the expiration date to the token
- args = munch.Munch({'token': token, 'date': 'aa-bb-cc'})
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.cli.admin.do_update_admin_token,
- args
- )
- @patch('pagure.cli.admin._get_input')
- @patch('pagure.cli.admin._ask_confirmation')
- def test_do_update_admin_token_invalid_date2(self, conf, rinp):
- """ Test the do_update_admin_token function of pagure-admin with
- an invalid date. """
- if 'BUILD_ID' in os.environ:
- raise unittest.case.SkipTest('Skipping on jenkins/el7')
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = '1,2,3'
- args = munch.Munch({'user': 'pingou'})
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve the token
- list_args = munch.Munch({
- 'user': None,
- 'token': None,
- 'active': False,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split('\n')), 2)
- self.assertIn(' -- pingou -- ', output)
- token = output.split(' ', 1)[0]
- current_expiration = output.split(' ', 1)[1]
- # Set the expiration date to the token
- args = munch.Munch({'token': token, 'date': '2017-18-01'})
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.cli.admin.do_update_admin_token,
- args
- )
- @patch('pagure.cli.admin._get_input')
- @patch('pagure.cli.admin._ask_confirmation')
- def test_do_update_admin_token_invalid_date3(self, conf, rinp):
- """ Test the do_update_admin_token function of pagure-admin with
- an invalid date (is today). """
- if 'BUILD_ID' in os.environ:
- raise unittest.case.SkipTest('Skipping on jenkins/el7')
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = '1,2,3'
- args = munch.Munch({'user': 'pingou'})
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve the token
- list_args = munch.Munch({
- 'user': None,
- 'token': None,
- 'active': False,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split('\n')), 2)
- self.assertIn(' -- pingou -- ', output)
- token = output.split(' ', 1)[0]
- current_expiration = output.split(' ', 1)[1]
- # Set the expiration date to the token
- args = munch.Munch({
- 'token': token, 'date': datetime.datetime.utcnow().date()
- })
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.cli.admin.do_update_admin_token,
- args
- )
- @patch('pagure.cli.admin._get_input')
- @patch('pagure.cli.admin._ask_confirmation')
- def test_do_update_admin_token(self, conf, rinp):
- """ Test the do_update_admin_token function of pagure-admin. """
- if 'BUILD_ID' in os.environ:
- raise unittest.case.SkipTest('Skipping on jenkins/el7')
- # Create an admin token to use
- conf.return_value = True
- rinp.return_value = '1,2,3'
- args = munch.Munch({'user': 'pingou'})
- pagure.cli.admin.do_create_admin_token(args)
- # Retrieve the token
- list_args = munch.Munch({
- 'user': None,
- 'token': None,
- 'active': False,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No user "pingou" found\n')
- self.assertEqual(len(output.split('\n')), 2)
- self.assertIn(' -- pingou -- ', output)
- token = output.split(' ', 1)[0]
- current_expiration = output.strip().split(' -- ', 2)[-1]
- # Before
- list_args = munch.Munch({
- 'user': None,
- 'token': None,
- 'active': True,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertNotEqual(output, 'No admin tokens found\n')
- self.assertEqual(len(output.split('\n')), 2)
- self.assertIn(' -- pingou -- ', output)
- deadline = datetime.datetime.utcnow().date() \
- + datetime.timedelta(days=3)
- # Set the expiration date to the token
- args = munch.Munch({
- 'token': token,
- 'date': deadline.strftime('%Y-%m-%d')
- })
- pagure.cli.admin.do_update_admin_token(args)
- # After
- list_args = munch.Munch({
- 'user': None,
- 'token': None,
- 'active': True,
- 'expired': False,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_list_admin_token(list_args)
- output = output.getvalue()
- self.assertEqual(output.split(' ', 1)[0], token)
- self.assertNotEqual(
- output.strip().split(' -- ', 2)[-1],
- current_expiration)
- class PagureAdminGetWatchTests(tests.Modeltests):
- """ Tests for pagure-admin get-watch """
- populate_db = False
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(PagureAdminGetWatchTests, self).setUp()
- pagure.cli.admin.session = self.session
- # Create the user pingou
- item = pagure.lib.model.User(
- user='pingou',
- fullname='PY C',
- password='foo',
- default_email='bar@pingou.com',
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(
- user_id=1,
- email='bar@pingou.com')
- self.session.add(item)
- # Create the user foo
- item = pagure.lib.model.User(
- user='foo',
- fullname='foo B.',
- password='foob',
- default_email='foo@pingou.com',
- )
- self.session.add(item)
- # Create two projects for the user pingou
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name='test',
- description='namespaced test project',
- hook_token='aaabbbeee',
- namespace='somenamespace',
- )
- self.session.add(item)
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name='test',
- description='Test project',
- hook_token='aaabbbccc',
- namespace=None,
- )
- self.session.add(item)
- self.session.commit()
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- def test_get_watch_get_project_unknown_project(self):
- """ Test the get-watch function of pagure-admin with an unknown
- project.
- """
- args = munch.Munch({
- 'project': 'foobar',
- 'user': 'pingou',
- })
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_get_watch_status(args)
- self.assertEqual(
- cm.exception.args[0],
- 'No project found with: foobar'
- )
- def test_get_watch_get_project_invalid_project(self):
- """ Test the get-watch function of pagure-admin with an invalid
- project.
- """
- args = munch.Munch({
- 'project': 'fo/o/bar',
- 'user': 'pingou',
- })
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_get_watch_status(args)
- self.assertEqual(
- cm.exception.args[0],
- 'Invalid project name, has more than one "/": fo/o/bar',
- )
- def test_get_watch_get_project_invalid_user(self):
- """ Test the get-watch function of pagure-admin on a invalid user.
- """
- args = munch.Munch({
- 'project': 'test',
- 'user': 'beebop',
- })
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_get_watch_status(args)
- self.assertEqual(
- cm.exception.args[0],
- 'No user "beebop" found'
- )
- def test_get_watch_get_project(self):
- """ Test the get-watch function of pagure-admin on a regular project.
- """
- args = munch.Munch({
- 'project': 'test',
- 'user': 'pingou',
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_get_watch_status(args)
- output = output.getvalue()
- self.assertEqual(
- 'On test user: pingou is watching the following items: '
- 'issues, pull-requests\n', output)
- def test_get_watch_get_project_not_watching(self):
- """ Test the get-watch function of pagure-admin on a regular project.
- """
- args = munch.Munch({
- 'project': 'test',
- 'user': 'foo',
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_get_watch_status(args)
- output = output.getvalue()
- self.assertEqual(
- 'On test user: foo is watching the following items: None\n',
- output)
- def test_get_watch_get_project_namespaced(self):
- """ Test the get-watch function of pagure-admin on a namespaced project.
- """
- args = munch.Munch({
- 'project': 'somenamespace/test',
- 'user': 'pingou',
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_get_watch_status(args)
- output = output.getvalue()
- self.assertEqual(
- 'On somenamespace/test user: pingou is watching the following '
- 'items: issues, pull-requests\n', output)
- def test_get_watch_get_project_namespaced_not_watching(self):
- """ Test the get-watch function of pagure-admin on a namespaced project.
- """
- args = munch.Munch({
- 'project': 'somenamespace/test',
- 'user': 'foo',
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_get_watch_status(args)
- output = output.getvalue()
- with tests.capture_output() as _discarded:
- pagure.cli.admin.do_get_watch_status(args)
- self.assertEqual(
- 'On somenamespace/test user: foo is watching the following '
- 'items: None\n', output)
- class PagureAdminUpdateWatchTests(tests.Modeltests):
- """ Tests for pagure-admin update-watch """
- populate_db = False
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(PagureAdminUpdateWatchTests, self).setUp()
- pagure.cli.admin.session = self.session
- # Create the user pingou
- item = pagure.lib.model.User(
- user='pingou',
- fullname='PY C',
- password='foo',
- default_email='bar@pingou.com',
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(
- user_id=1,
- email='bar@pingou.com')
- self.session.add(item)
- # Create the user foo
- item = pagure.lib.model.User(
- user='foo',
- fullname='foo B.',
- password='foob',
- default_email='foo@pingou.com',
- )
- self.session.add(item)
- # Create two projects for the user pingou
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name='test',
- description='namespaced test project',
- hook_token='aaabbbeee',
- namespace='somenamespace',
- )
- self.session.add(item)
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name='test',
- description='Test project',
- hook_token='aaabbbccc',
- namespace=None,
- )
- self.session.add(item)
- self.session.commit()
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- def test_get_watch_update_project_unknown_project(self):
- """ Test the update-watch function of pagure-admin on an unknown
- project.
- """
- args = munch.Munch({
- 'project': 'foob',
- 'user': 'pingou',
- 'status': '1'
- })
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_update_watch_status(args)
- self.assertEqual(
- cm.exception.args[0],
- 'No project found with: foob'
- )
- def test_get_watch_update_project_invalid_project(self):
- """ Test the update-watch function of pagure-admin on an invalid
- project.
- """
- args = munch.Munch({
- 'project': 'fo/o/b',
- 'user': 'pingou',
- 'status': '1'
- })
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_update_watch_status(args)
- self.assertEqual(
- cm.exception.args[0],
- 'Invalid project name, has more than one "/": fo/o/b',
- )
- def test_get_watch_update_project_invalid_user(self):
- """ Test the update-watch function of pagure-admin on an invalid user.
- """
- args = munch.Munch({
- 'project': 'test',
- 'user': 'foob',
- 'status': '1'
- })
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_update_watch_status(args)
- self.assertEqual(
- cm.exception.args[0],
- 'No user "foob" found'
- )
- def test_get_watch_update_project_invalid_status(self):
- """ Test the update-watch function of pagure-admin with an invalid
- status.
- """
- args = munch.Munch({
- 'project': 'test',
- 'user': 'pingou',
- 'status': '10'
- })
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_update_watch_status(args)
- self.assertEqual(
- cm.exception.args[0],
- 'Invalid status provided: 10 not in -1, 0, 1, 2, 3'
- )
- def test_get_watch_update_project_no_effect(self):
- """ Test the update-watch function of pagure-admin with a regular
- project - nothing changed.
- """
- args = munch.Munch({
- 'project': 'test',
- 'user': 'pingou',
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_get_watch_status(args)
- output = output.getvalue()
- self.assertEqual(
- 'On test user: pingou is watching the following items: '
- 'issues, pull-requests\n', output)
- args = munch.Munch({
- 'project': 'test',
- 'user': 'pingou',
- 'status': '1'
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_update_watch_status(args)
- output = output.getvalue()
- self.assertEqual(
- 'Updating watch status of pingou to 1 (watch issues and PRs) '
- 'on test\n', output)
- args = munch.Munch({
- 'project': 'test',
- 'user': 'pingou',
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_get_watch_status(args)
- output = output.getvalue()
- self.assertEqual(
- 'On test user: pingou is watching the following items: '
- 'issues, pull-requests\n', output)
- class PagureAdminReadOnlyTests(tests.Modeltests):
- """ Tests for pagure-admin read-only """
- populate_db = False
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(PagureAdminReadOnlyTests, self).setUp()
- pagure.cli.admin.session = self.session
- # Create the user pingou
- item = pagure.lib.model.User(
- user='pingou',
- fullname='PY C',
- password='foo',
- default_email='bar@pingou.com',
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(
- user_id=1,
- email='bar@pingou.com')
- self.session.add(item)
- # Create two projects for the user pingou
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name='test',
- description='namespaced test project',
- hook_token='aaabbbeee',
- namespace='somenamespace',
- )
- self.session.add(item)
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name='test',
- description='Test project',
- hook_token='aaabbbccc',
- namespace=None,
- )
- self.session.add(item)
- self.session.commit()
- # Make the imported pagure use the correct db session
- pagure.cli.admin.session = self.session
- def test_read_only_unknown_project(self):
- """ Test the read-only function of pagure-admin on an unknown
- project.
- """
- args = munch.Munch({
- 'project': 'foob',
- 'user': None,
- 'ro': None,
- })
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_read_only(args)
- self.assertEqual(
- cm.exception.args[0],
- 'No project found with: foob'
- )
- def test_read_only_invalid_project(self):
- """ Test the read-only function of pagure-admin on an invalid
- project.
- """
- args = munch.Munch({
- 'project': 'fo/o/b',
- 'user': None,
- 'ro': None,
- })
- with self.assertRaises(pagure.exceptions.PagureException) as cm:
- pagure.cli.admin.do_read_only(args)
- self.assertEqual(
- cm.exception.args[0],
- 'Invalid project name, has more than one "/": fo/o/b'
- )
- def test_read_only(self):
- """ Test the read-only function of pagure-admin to get status of
- a non-namespaced project.
- """
- args = munch.Munch({
- 'project': 'test',
- 'user': None,
- 'ro': None,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- 'The current read-only flag of the project test is set to True\n',
- output)
- def test_read_only_namespace(self):
- """ Test the read-only function of pagure-admin to get status of
- a namespaced project.
- """
- args = munch.Munch({
- 'project': 'somenamespace/test',
- 'user': None,
- 'ro': None,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- 'The current read-only flag of the project somenamespace/test '\
- 'is set to True\n', output)
- def test_read_only_namespace_changed(self):
- """ Test the read-only function of pagure-admin to set the status of
- a namespaced project.
- """
- # Before
- args = munch.Munch({
- 'project': 'somenamespace/test',
- 'user': None,
- 'ro': None,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- 'The current read-only flag of the project somenamespace/test '\
- 'is set to True\n', output)
- args = munch.Munch({
- 'project': 'somenamespace/test',
- 'user': None,
- 'ro': 'false',
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- 'The read-only flag of the project somenamespace/test has been '
- 'set to False\n', output)
- # After
- args = munch.Munch({
- 'project': 'somenamespace/test',
- 'user': None,
- 'ro': None,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- 'The current read-only flag of the project somenamespace/test '\
- 'is set to False\n', output)
- def test_read_only_no_change(self):
- """ Test the read-only function of pagure-admin to set the status of
- a namespaced project.
- """
- # Before
- args = munch.Munch({
- 'project': 'test',
- 'user': None,
- 'ro': None,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- 'The current read-only flag of the project test '\
- 'is set to True\n', output)
- args = munch.Munch({
- 'project': 'test',
- 'user': None,
- 'ro': 'true',
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- 'The read-only flag of the project test has been '
- 'set to True\n', output)
- # After
- args = munch.Munch({
- 'project': 'test',
- 'user': None,
- 'ro': None,
- })
- with tests.capture_output() as output:
- pagure.cli.admin.do_read_only(args)
- output = output.getvalue()
- self.assertEqual(
- 'The current read-only flag of the project test '\
- 'is set to True\n', output)
- if __name__ == '__main__':
- unittest.main(verbosity=2)
|