diff --git a/pagure/cli/admin.py b/pagure/cli/admin.py index c4eea5c..7ba5419 100644 --- a/pagure/cli/admin.py +++ b/pagure/cli/admin.py @@ -262,7 +262,7 @@ def _parser_read_only(subparser): local_parser.set_defaults(func=do_read_only) -def parse_arguments(): +def parse_arguments(args=None): """ Set-up the argument parsing. """ parser = argparse.ArgumentParser( description='The admin CLI for this pagure instance') @@ -298,7 +298,7 @@ def parse_arguments(): # read-only _parser_read_only(subparser) - return parser.parse_args() + return parser.parse_args(args) def _ask_confirmation(): diff --git a/tests/__init__.py b/tests/__init__.py index 2c10f7a..d6da102 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -893,6 +893,24 @@ def add_binary_git_repo(folder, filename): shutil.rmtree(newfolder) +@contextmanager +def capture_output(merge_stderr=True): + import sys + from cStringIO import StringIO + oldout, olderr = sys.stdout, sys.stderr + try: + out = StringIO() + err = StringIO() + if merge_stderr: + sys.stdout = sys.stderr = out + yield out + else: + sys.stdout, sys.stderr = out, err + yield out, err + finally: + sys.stdout, sys.stderr = oldout, olderr + + if __name__ == '__main__': SUITE = unittest.TestLoader().loadTestsFromTestCase(Modeltests) unittest.TextTestRunner(verbosity=2).run(SUITE) diff --git a/tests/test_pagure_admin.py b/tests/test_pagure_admin.py index 7e1461b..a3479c0 100644 --- a/tests/test_pagure_admin.py +++ b/tests/test_pagure_admin.py @@ -26,310 +26,11 @@ 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 -PAGURE_ADMIN = os.path.abspath( - os.path.join(tests.HERE, '..', 'pagure', 'cli', 'admin.py')) - - -def _get_ouput(cmd): - """ Returns the std-out of the command specified. - - :arg cmd: the command to run provided as a list - :type cmd: list - - """ - my_env = os.environ.copy() - my_env["PYTHONPATH"] = os.path.abspath(os.path.join(tests.HERE, '..')) - output = subprocess.Popen( - cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - env=my_env, - ).communicate() - - return output - - -class PagureAdminHelptests(tests.Modeltests): - """ Tests for pagure-admin --help """ - - maxDiff = None - - def test_parse_arguments(self): - """ Test the parse_arguments function of pagure-admin, empty. """ - if 'BUILD_ID' in os.environ: - raise unittest.case.SkipTest('Skipping on jenkins/el7') - - header = 'usage: admin.py [-h] [-c CONFIG] [--debug]\n' + \ - ' {refresh-gitolite,refresh-ssh,' + \ - 'clear-hook-token,admin-token,get-watch,update-watch,' + \ - 'read-only}\n' - - py_version = tuple(int(el) for el in platform.python_version_tuple()) - if py_version < (2, 7, 7): - header = 'usage: admin.py [-h] [-c CONFIG] [--debug]\n' + \ - ' \n' + \ - ' {refresh-gitolite,refresh-ssh,' + \ - 'clear-hook-token,admin-token,get-watch,update-watch,' + \ - 'read-only}\n' - - cmd = ['python', PAGURE_ADMIN] - output = _get_ouput(cmd) - self.assertEqual(output[0], '') - self.assertEqual(output[1], header + ''' ... -admin.py: error: too few arguments -''') # noqa - - def test_parse_arguments_help(self): - """ Test the parse_arguments function of pagure-admin. """ - cmd = ['python', PAGURE_ADMIN, '--help'] - header = 'usage: admin.py [-h] [-c CONFIG] [--debug]\n' + \ - ' {refresh-gitolite,refresh-ssh,' + \ - 'clear-hook-token,admin-token,get-watch,update-watch,' + \ - 'read-only}\n' - - py_version = tuple(int(el) for el in platform.python_version_tuple()) - if py_version < (2, 7, 7): - header = 'usage: admin.py [-h] [-c CONFIG] [--debug]\n' + \ - ' \n' + \ - ' {refresh-gitolite,refresh-ssh,' + \ - 'clear-hook-token,admin-token,get-watch,update-watch,' + \ - 'read-only}\n' - - self.assertEqual( - _get_ouput(cmd)[0], - header + ''' ... - -The admin CLI for this pagure instance - -optional arguments: - -h, --help show this help message and exit - -c CONFIG, --config CONFIG - Specify a configuration to use - --debug Increase the verbosity of the information displayed - -actions: - {refresh-gitolite,refresh-ssh,clear-hook-token,admin-token,get-watch,update-watch,read-only} - refresh-gitolite Re-generate the gitolite config file - refresh-ssh Re-write to disk every user's ssh key stored in the - database - clear-hook-token Generate a new hook token for every project in this - instance - admin-token Manages the admin tokens for this instance - get-watch Get someone's watch status on a project - update-watch Update someone's watch status on a project - read-only Get or set the read-only flag on a project -''') - - def test_parser_refresh_gitolite_help(self): - """ Test the parser_refresh_gitolite function of pagure-admin. """ - cmd = ['python', PAGURE_ADMIN, 'refresh-gitolite', '--help'] - header = 'usage: admin.py refresh-gitolite [-h] [--user USER] ' + \ - '[--project PROJECT]\n' + \ - ' [--group GROUP] [--all]' - - py_version = tuple(int(el) for el in platform.python_version_tuple()) - if py_version < (2, 7, 7): - header = 'usage: admin.py refresh-gitolite [-h] [--user USER] '+ \ - '[--project PROJECT]\n' + \ - ' [--group GROUP] [--all]' - - print(_get_ouput(cmd)[0]) - - self.assertEqual( - _get_ouput(cmd)[0], - header + ''' - -optional arguments: - -h, --help show this help message and exit - --user USER User of the project (to use only on forks) - --project PROJECT Project to update (as namespace/project if there is a - namespace) - --group GROUP Group to refresh - --all Refresh all the projects -''') - - def test_parser_refresh_ssh_help(self): - """ Test the parser_refresh_ssh function of pagure-admin. """ - cmd = ['python', PAGURE_ADMIN, 'refresh-ssh', '--help'] - self.assertEqual( - _get_ouput(cmd)[0], - '''usage: admin.py refresh-ssh [-h] - -optional arguments: - -h, --help show this help message and exit -''') - - def test_parser_clear_hook_token_help(self): - """ Test the parser_clear_hook_token function of pagure-admin. """ - cmd = ['python', PAGURE_ADMIN, 'clear-hook-token', '--help'] - self.assertEqual( - _get_ouput(cmd)[0], - '''usage: admin.py clear-hook-token [-h] - -optional arguments: - -h, --help show this help message and exit -''') - - def test_parser_admin_token_help(self): - """ Test the parser_admin_token function of pagure-admin. """ - cmd = ['python', PAGURE_ADMIN, 'admin-token', '--help'] - self.assertEqual( - _get_ouput(cmd)[0], - '''usage: admin.py admin-token [-h] {list,info,expire,create,update} ... - -optional arguments: - -h, --help show this help message and exit - -actions: - {list,info,expire,create,update} - list List the API admin token - info Provide some information about a specific API token - expire Expire a specific API token - create Create a new API token - update Update the expiration date of an API token -''') - - def test_parser_admin_token_create_help(self): - """ Test the parser_admin_token_create function of pagure-admin. """ - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'create', '--help'] - self.assertEqual( - _get_ouput(cmd)[0], - '''usage: admin.py admin-token create [-h] user - -positional arguments: - user User to associate with the token - -optional arguments: - -h, --help show this help message and exit -''') - - def test_parser_admin_token_update_help(self): - """ Test the parser_admin_token_create function of pagure-admin. """ - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'update', '--help'] - self.assertEqual( - _get_ouput(cmd)[0], - '''usage: admin.py admin-token update [-h] token date - -positional arguments: - token API token - date New expiration date - -optional arguments: - -h, --help show this help message and exit -''') - - def test_parser_admin_token_list_help(self): - """ Test the _parser_admin_token_list function of pagure-admin. """ - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list', '--help'] - self.assertEqual( - _get_ouput(cmd)[0], - '''usage: admin.py admin-token list [-h] [--user USER] [--token TOKEN] [--active] - [--expired] - -optional arguments: - -h, --help show this help message and exit - --user USER User to associate or associated with the token - --token TOKEN API token - --active Only list active API token - --expired Only list expired API token -''') # noqa - - def test_parser_admin_token_info_help(self): - """ Test the _parser_admin_token_info function of pagure-admin. """ - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'info', '--help'] - self.assertEqual( - _get_ouput(cmd)[0], - '''usage: admin.py admin-token info [-h] token - -positional arguments: - token API token - -optional arguments: - -h, --help show this help message and exit -''') - - def test_parser_admin_token_expire_help(self): - """ Test the _parser_admin_token_expire function of pagure-admin. """ - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'expire', '--help'] - self.assertEqual( - _get_ouput(cmd)[0], - '''usage: admin.py admin-token expire [-h] token - -positional arguments: - token API token - -optional arguments: - -h, --help show this help message and exit -''') - - def test_parser_admin_token_invalid_help(self): - """ Test the _parser_admin_token_expire function of pagure-admin. """ - if 'BUILD_ID' in os.environ: - raise unittest.case.SkipTest('Skipping on jenkins/el7') - - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'foo', '--help'] - self.assertEqual( - _get_ouput(cmd)[1], - '''usage: admin.py admin-token [-h] {list,info,expire,create,update} ... -admin.py admin-token: error: invalid choice: 'foo' (choose from 'list', 'info', 'expire', 'create', 'update') -''') # noqa - - def test_parser_get_watch(self): - """ Test the _parser_get_watch function of pagure-admin. """ - cmd = ['python', PAGURE_ADMIN, 'get-watch', '--help'] - self.assertEqual( - _get_ouput(cmd)[0], - '''usage: admin.py get-watch [-h] project user - -positional arguments: - project Project (as namespace/project if there is a namespace) -- Fork - not supported - user User to get the watch status of - -optional arguments: - -h, --help show this help message and exit -''') - - def test_parser_update_watch(self): - """ Test the _parser_update_watch function of pagure-admin. """ - cmd = ['python', PAGURE_ADMIN, 'update-watch', '--help'] - self.assertEqual( - _get_ouput(cmd)[0], - '''usage: admin.py update-watch [-h] [-s STATUS] project user - -positional arguments: - project Project to update (as namespace/project if there is a - namespace) -- Fork not supported - user User to update the watch status of - -optional arguments: - -h, --help show this help message and exit - -s STATUS, --status STATUS - Watch status to update to -''') - - def test_parser_read_only(self): - """ Test the _parser_update_watch function of pagure-admin. """ - cmd = ['python', PAGURE_ADMIN, 'read-only', '--help'] - self.assertEqual( - _get_ouput(cmd)[0], - '''usage: admin.py read-only [-h] [--user USER] [--ro RO] project - -positional arguments: - project Project to update (as namespace/project if there is a - namespace) - -optional arguments: - -h, --help show this help message and exit - --user USER User of the project (to use only on forks) - --ro RO Read-Only status to set (has to be: true or false), do not - specify to get the current status -''') - class PagureAdminAdminTokenEmptytests(tests.Modeltests): """ Tests for pagure-admin admin-token when there is nothing in the DB @@ -346,15 +47,28 @@ class PagureAdminAdminTokenEmptytests(tests.Modeltests): """ Test the do_create_admin_token function of pagure-admin without user. """ - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'create', 'pingou'] - self.assertEqual(_get_ouput(cmd)[0], 'No user "pingou" found\n') + 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. """ - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list'] - self.assertEqual(_get_ouput(cmd)[0], 'No admin tokens found\n') + 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): @@ -526,8 +240,15 @@ class PagureAdminAdminTokentests(tests.Modeltests): pagure.cli.admin.do_create_admin_token(args) # Check the outcome - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list'] - output = _get_ouput(cmd)[0] + 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) @@ -544,17 +265,29 @@ class PagureAdminAdminTokentests(tests.Modeltests): pagure.cli.admin.do_create_admin_token(args) # Retrieve all tokens - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list'] - output = _get_ouput(cmd)[0] + 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 - cmd = [ - 'python', PAGURE_ADMIN, - 'admin-token', 'list', '--user', 'pfrields'] - output = _get_ouput(cmd)[0] + 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') @@ -569,16 +302,25 @@ class PagureAdminAdminTokentests(tests.Modeltests): pagure.cli.admin.do_create_admin_token(args) # Retrieve the token - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list'] - output = _get_ouput(cmd)[0] + 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] - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'info', token] - output = _get_ouput(cmd)[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: @@ -602,8 +344,15 @@ class PagureAdminAdminTokentests(tests.Modeltests): pagure.cli.admin.do_create_admin_token(args) # Retrieve the token - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list'] - output = _get_ouput(cmd)[0] + 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) @@ -611,8 +360,15 @@ class PagureAdminAdminTokentests(tests.Modeltests): token = output.split(' ', 1)[0] # Before - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list', '--active'] - output = _get_ouput(cmd)[0] + 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) @@ -622,8 +378,15 @@ class PagureAdminAdminTokentests(tests.Modeltests): pagure.cli.admin.do_expire_admin_token(args) # After - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list', '--active'] - output = _get_ouput(cmd)[0] + 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') @@ -642,8 +405,15 @@ class PagureAdminAdminTokentests(tests.Modeltests): pagure.cli.admin.do_create_admin_token(args) # Retrieve the token - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list'] - output = _get_ouput(cmd)[0] + 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) @@ -675,8 +445,15 @@ class PagureAdminAdminTokentests(tests.Modeltests): pagure.cli.admin.do_create_admin_token(args) # Retrieve the token - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list'] - output = _get_ouput(cmd)[0] + 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) @@ -708,8 +485,15 @@ class PagureAdminAdminTokentests(tests.Modeltests): pagure.cli.admin.do_create_admin_token(args) # Retrieve the token - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list'] - output = _get_ouput(cmd)[0] + 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) @@ -742,8 +526,15 @@ class PagureAdminAdminTokentests(tests.Modeltests): pagure.cli.admin.do_create_admin_token(args) # Retrieve the token - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list'] - output = _get_ouput(cmd)[0] + 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) @@ -752,8 +543,15 @@ class PagureAdminAdminTokentests(tests.Modeltests): current_expiration = output.strip().split(' -- ', 2)[-1] # Before - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list', '--active'] - output = _get_ouput(cmd)[0] + 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) @@ -769,8 +567,15 @@ class PagureAdminAdminTokentests(tests.Modeltests): pagure.cli.admin.do_update_admin_token(args) # After - cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list', '--active'] - output = _get_ouput(cmd)[0] + 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], @@ -837,36 +642,56 @@ class PagureAdminGetWatchTests(tests.Modeltests): """ Test the get-watch function of pagure-admin with an unknown project. """ - - cmd = ['python', PAGURE_ADMIN, 'get-watch', 'foobar', 'pingou'] - output = _get_ouput(cmd)[0] - self.assertEqual('No project found with: foobar\n', output) + 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. """ - - cmd = ['python', PAGURE_ADMIN, 'get-watch', 'fo/o/bar', 'pingou'] - output = _get_ouput(cmd)[0] + 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( - 'Invalid project name, has more than one "/": fo/o/bar\n', - output) + 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. """ - - cmd = ['python', PAGURE_ADMIN, 'get-watch', 'test', 'beebop'] - output = _get_ouput(cmd)[0] - self.assertEqual('No user "beebop" found\n', output) + 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. """ - - cmd = ['python', PAGURE_ADMIN, 'get-watch', 'test', 'pingou'] - output = _get_ouput(cmd)[0] + 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) @@ -875,8 +700,13 @@ class PagureAdminGetWatchTests(tests.Modeltests): """ Test the get-watch function of pagure-admin on a regular project. """ - cmd = ['python', PAGURE_ADMIN, 'get-watch', 'test', 'foo'] - output = _get_ouput(cmd)[0] + 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) @@ -885,10 +715,13 @@ class PagureAdminGetWatchTests(tests.Modeltests): """ Test the get-watch function of pagure-admin on a namespaced project. """ - cmd = [ - 'python', PAGURE_ADMIN, 'get-watch', - 'somenamespace/test', 'pingou'] - output = _get_ouput(cmd)[0] + 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) @@ -897,11 +730,15 @@ class PagureAdminGetWatchTests(tests.Modeltests): """ Test the get-watch function of pagure-admin on a namespaced project. """ - cmd = [ - 'python', PAGURE_ADMIN, 'get-watch', - 'somenamespace/test', 'foo'] - output = _get_ouput(cmd)[0] - _get_ouput(cmd) + 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) @@ -967,59 +804,100 @@ class PagureAdminUpdateWatchTests(tests.Modeltests): """ Test the update-watch function of pagure-admin on an unknown project. """ - - cmd = ['python', PAGURE_ADMIN, 'update-watch', 'foob', 'pingou', '-s=1'] - output = _get_ouput(cmd)[0] - self.assertEqual('No project found with: foob\n', output) + 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. """ - - cmd = ['python', PAGURE_ADMIN, 'update-watch', 'fo/o/b', 'pingou', '-s=1'] - output = _get_ouput(cmd)[0] + 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( - 'Invalid project name, has more than one "/": fo/o/b\n', - output) + 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. """ - - cmd = ['python', PAGURE_ADMIN, 'update-watch', 'test', 'foob', '-s=1'] - output = _get_ouput(cmd)[0] - self.assertEqual('No user "foob" found\n', output) + 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. """ - - cmd = ['python', PAGURE_ADMIN, 'update-watch', 'test', 'pingou', '-s=10'] - output = _get_ouput(cmd)[0] + 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( - 'Invalid status provided: 10 not in -1, 0, 1, 2, 3\n', output) + 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. """ - cmd = ['python', PAGURE_ADMIN, 'get-watch', 'test', 'pingou'] - output = _get_ouput(cmd)[0] + 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) - cmd = ['python', PAGURE_ADMIN, 'update-watch', 'test', 'pingou', '-s=1'] - output = _get_ouput(cmd)[0] + 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) - cmd = ['python', PAGURE_ADMIN, 'get-watch', 'test', 'pingou'] - output = _get_ouput(cmd)[0] + 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) @@ -1077,28 +955,48 @@ class PagureAdminReadOnlyTests(tests.Modeltests): project. """ - cmd = ['python', PAGURE_ADMIN, 'read-only', 'foob'] - output = _get_ouput(cmd)[0] - self.assertEqual('No project found with: foob\n', output) + 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. """ - cmd = ['python', PAGURE_ADMIN, 'read-only', 'fo/o/b'] - output = _get_ouput(cmd)[0] + 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( - 'Invalid project name, has more than one "/": fo/o/b\n', - output) + 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. """ - cmd = ['python', PAGURE_ADMIN, 'read-only', 'test'] - output = _get_ouput(cmd)[0] + 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) @@ -1108,8 +1006,14 @@ class PagureAdminReadOnlyTests(tests.Modeltests): a namespaced project. """ - cmd = ['python', PAGURE_ADMIN, 'read-only', 'somenamespace/test'] - output = _get_ouput(cmd)[0] + 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) @@ -1120,23 +1024,39 @@ class PagureAdminReadOnlyTests(tests.Modeltests): """ # Before - cmd = ['python', PAGURE_ADMIN, 'read-only', 'somenamespace/test'] - output = _get_ouput(cmd)[0] + 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) - cmd = [ - 'python', PAGURE_ADMIN, 'read-only', - 'somenamespace/test', '--ro', 'false'] - output = _get_ouput(cmd)[0] + 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 - cmd = ['python', PAGURE_ADMIN, 'read-only', 'somenamespace/test'] - output = _get_ouput(cmd)[0] + 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) @@ -1147,22 +1067,39 @@ class PagureAdminReadOnlyTests(tests.Modeltests): """ # Before - cmd = ['python', PAGURE_ADMIN, 'read-only', 'test'] - output = _get_ouput(cmd)[0] + 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) - cmd = [ - 'python', PAGURE_ADMIN, 'read-only', 'test', '--ro', 'true'] - output = _get_ouput(cmd)[0] + 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 - cmd = ['python', PAGURE_ADMIN, 'read-only', 'test'] - output = _get_ouput(cmd)[0] + 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)