Blame tests/test_pagure_admin.py

Pierre-Yves Chibon d4c9da
# -*- coding: utf-8 -*-
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
"""
Pierre-Yves Chibon d4c9da
 (c) 2017 - Copyright Red Hat Inc
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
 Authors:
Pierre-Yves Chibon d4c9da
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
"""
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon d4c9da
import pkg_resources  # noqa
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
import unittest  # noqa
Pierre-Yves Chibon d4c9da
import shutil  # noqa
Pierre-Yves Chibon d4c9da
import subprocess  # noqa
Pierre-Yves Chibon d4c9da
import sys  # noqa
Pierre-Yves Chibon d4c9da
import os  # noqa
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
import munch  # noqa
Pierre-Yves Chibon d4c9da
from mock import patch, MagicMock  # noqa
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon d4c9da
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
import pagure.cli.admin  # noqa
Pierre-Yves Chibon d4c9da
import pagure.lib.model  # noqa
Pierre-Yves Chibon d4c9da
import tests  # noqa
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
PAGURE_ADMIN = os.path.abspath(
Pierre-Yves Chibon d4c9da
    os.path.join(tests.HERE, '..', 'pagure', 'cli', 'admin.py'))
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
def _get_ouput(cmd):
Pierre-Yves Chibon d4c9da
    """ Returns the std-out of the command specified.
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    :arg cmd: the command to run provided as a list
Pierre-Yves Chibon d4c9da
    :type cmd: list
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    """
Pierre-Yves Chibon d4c9da
    my_env = os.environ.copy()
Pierre-Yves Chibon d4c9da
    my_env["PYTHONPATH"] = os.path.abspath(os.path.join(tests.HERE, '..'))
Pierre-Yves Chibon d4c9da
    output = subprocess.Popen(
Pierre-Yves Chibon d4c9da
        cmd,
Pierre-Yves Chibon d4c9da
        stdout=subprocess.PIPE,
Pierre-Yves Chibon d4c9da
        stderr=subprocess.PIPE,
Pierre-Yves Chibon d4c9da
        env=my_env,
Pierre-Yves Chibon d4c9da
    ).communicate()
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    return output
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
class PagureAdminHelptests(tests.Modeltests):
Pierre-Yves Chibon d4c9da
    """ Tests for pagure-admin --help """
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon f7c773
    def test_parse_arguments(self):
Pierre-Yves Chibon d8759f
        """ Test the parse_arguments function of pagure-admin, empty. """
Pierre-Yves Chibon 2a4da5
        if 'BUILD_ID' in os.environ:
Pierre-Yves Chibon 2a4da5
            raise unittest.case.SkipTest('Skipping on jenkins/el7')
Pierre-Yves Chibon 2a4da5
Pierre-Yves Chibon f7c773
        cmd = ['python', PAGURE_ADMIN]
Pierre-Yves Chibon f7c773
        output = _get_ouput(cmd)
Pierre-Yves Chibon f7c773
        self.assertEqual(output[0], '')
Pierre-Yves Chibon f7c773
        self.assertEqual(output[1], '''usage: admin.py [-h] [--debug]
Pierre-Yves Chibon f7c773
                {refresh-gitolite,refresh-ssh,clear-hook-token,admin-token}
Pierre-Yves Chibon f7c773
                ...
Pierre-Yves Chibon f7c773
admin.py: error: too few arguments
Pierre-Yves Chibon f7c773
''')
Pierre-Yves Chibon f7c773
Pierre-Yves Chibon d4c9da
    def test_parse_arguments_help(self):
Pierre-Yves Chibon d4c9da
        """ Test the parse_arguments function of pagure-admin. """
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, '--help']
Pierre-Yves Chibon d4c9da
        self.assertEqual(
Pierre-Yves Chibon d4c9da
            _get_ouput(cmd)[0],
Pierre-Yves Chibon d4c9da
            '''usage: admin.py [-h] [--debug]
Pierre-Yves Chibon d4c9da
                {refresh-gitolite,refresh-ssh,clear-hook-token,admin-token}
Pierre-Yves Chibon d4c9da
                ...
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
The admin CLI for this pagure instance
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
optional arguments:
Pierre-Yves Chibon d4c9da
  -h, --help            show this help message and exit
Pierre-Yves Chibon d4c9da
  --debug               Increase the verbosity of the information displayed
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
actions:
Pierre-Yves Chibon d4c9da
  {refresh-gitolite,refresh-ssh,clear-hook-token,admin-token}
Pierre-Yves Chibon d4c9da
    refresh-gitolite    Re-generate the gitolite config file
Pierre-Yves Chibon d4c9da
    refresh-ssh         Re-write to disk every user's ssh key stored in the
Pierre-Yves Chibon d4c9da
                        database
Pierre-Yves Chibon d4c9da
    clear-hook-token    Generate a new hook token for every project in this
Pierre-Yves Chibon d4c9da
                        instance
Pierre-Yves Chibon d4c9da
    admin-token         Manages the admin tokens for this instance
Pierre-Yves Chibon d4c9da
''')
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def test_parser_refresh_gitolite_help(self):
Pierre-Yves Chibon d4c9da
        """ Test the parser_refresh_gitolite function of pagure-admin. """
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'refresh-gitolite', '--help']
Pierre-Yves Chibon d4c9da
        self.assertEqual(
Pierre-Yves Chibon d4c9da
            _get_ouput(cmd)[0],
Pierre-Yves Chibon ede948
            '''usage: admin.py refresh-gitolite [-h] [--user USER] [--project PROJECT]
Pierre-Yves Chibon ede948
                                 [--group GROUP] [--all]
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
optional arguments:
Pierre-Yves Chibon ede948
  -h, --help         show this help message and exit
Pierre-Yves Chibon ede948
  --user USER        User of the project (to use only on forks)
Pierre-Yves Chibon ede948
  --project PROJECT  Project to update (as namespace/project if there is a
Pierre-Yves Chibon ede948
                     namespace)
Pierre-Yves Chibon ede948
  --group GROUP      Group to refresh
Pierre-Yves Chibon ede948
  --all              Refresh all the projects
Pierre-Yves Chibon d4c9da
''')
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def test_parser_refresh_ssh_help(self):
Pierre-Yves Chibon d4c9da
        """ Test the parser_refresh_ssh function of pagure-admin. """
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'refresh-ssh', '--help']
Pierre-Yves Chibon d4c9da
        self.assertEqual(
Pierre-Yves Chibon d4c9da
            _get_ouput(cmd)[0],
Pierre-Yves Chibon d4c9da
            '''usage: admin.py refresh-ssh [-h]
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
optional arguments:
Pierre-Yves Chibon d4c9da
  -h, --help  show this help message and exit
Pierre-Yves Chibon d4c9da
''')
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def test_parser_clear_hook_token_help(self):
Pierre-Yves Chibon d4c9da
        """ Test the parser_clear_hook_token function of pagure-admin. """
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'clear-hook-token', '--help']
Pierre-Yves Chibon d4c9da
        self.assertEqual(
Pierre-Yves Chibon d4c9da
            _get_ouput(cmd)[0],
Pierre-Yves Chibon d4c9da
            '''usage: admin.py clear-hook-token [-h]
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
optional arguments:
Pierre-Yves Chibon d4c9da
  -h, --help  show this help message and exit
Pierre-Yves Chibon d4c9da
''')
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def test_parser_admin_token_help(self):
Pierre-Yves Chibon d4c9da
        """ Test the parser_admin_token function of pagure-admin. """
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', '--help']
Pierre-Yves Chibon d4c9da
        self.assertEqual(
Pierre-Yves Chibon d4c9da
            _get_ouput(cmd)[0],
Pierre-Yves Chibon d4c9da
            '''usage: admin.py admin-token [-h] {list,info,expire,create} ...
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
optional arguments:
Pierre-Yves Chibon d4c9da
  -h, --help            show this help message and exit
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
actions:
Pierre-Yves Chibon d4c9da
  {list,info,expire,create}
Pierre-Yves Chibon d4c9da
    list                List the API admin token
Pierre-Yves Chibon d4c9da
    info                Provide some information about a specific API token
Pierre-Yves Chibon d4c9da
    expire              Expire a specific API token
Pierre-Yves Chibon d4c9da
    create              Create a new API token
Pierre-Yves Chibon d4c9da
''')
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def test_parser_admin_token_create_help(self):
Pierre-Yves Chibon d4c9da
        """ Test the parser_admin_token_create function of pagure-admin. """
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'create', '--help']
Pierre-Yves Chibon d4c9da
        self.assertEqual(
Pierre-Yves Chibon d4c9da
            _get_ouput(cmd)[0],
Pierre-Yves Chibon d4c9da
            '''usage: admin.py admin-token create [-h] user
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
positional arguments:
Pierre-Yves Chibon d4c9da
  user        User to associate with the token
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
optional arguments:
Pierre-Yves Chibon d4c9da
  -h, --help  show this help message and exit
Pierre-Yves Chibon d4c9da
''')
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def test_parser_admin_token_list_help(self):
Pierre-Yves Chibon d4c9da
        """ Test the _parser_admin_token_list function of pagure-admin. """
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list', '--help']
Pierre-Yves Chibon d4c9da
        self.assertEqual(
Pierre-Yves Chibon d4c9da
            _get_ouput(cmd)[0],
Pierre-Yves Chibon d4c9da
            '''usage: admin.py admin-token list [-h] [--user USER] [--token TOKEN] [--active]
Pierre-Yves Chibon d4c9da
                                 [--expired]
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
optional arguments:
Pierre-Yves Chibon d4c9da
  -h, --help     show this help message and exit
Pierre-Yves Chibon d4c9da
  --user USER    User to associate or associated with the token
Pierre-Yves Chibon d4c9da
  --token TOKEN  API token
Pierre-Yves Chibon d4c9da
  --active       Only list active API token
Pierre-Yves Chibon d4c9da
  --expired      Only list expired API token
Pierre-Yves Chibon d4c9da
''')  # noqa
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def test_parser_admin_token_info_help(self):
Pierre-Yves Chibon d4c9da
        """ Test the _parser_admin_token_info function of pagure-admin. """
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'info', '--help']
Pierre-Yves Chibon d4c9da
        self.assertEqual(
Pierre-Yves Chibon d4c9da
            _get_ouput(cmd)[0],
Pierre-Yves Chibon d4c9da
            '''usage: admin.py admin-token info [-h] token
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
positional arguments:
Pierre-Yves Chibon d4c9da
  token       API token
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
optional arguments:
Pierre-Yves Chibon d4c9da
  -h, --help  show this help message and exit
Pierre-Yves Chibon d4c9da
''')
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def test_parser_admin_token_expire_help(self):
Pierre-Yves Chibon d4c9da
        """ Test the _parser_admin_token_expire function of pagure-admin. """
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'expire', '--help']
Pierre-Yves Chibon d4c9da
        self.assertEqual(
Pierre-Yves Chibon d4c9da
            _get_ouput(cmd)[0],
Pierre-Yves Chibon d4c9da
            '''usage: admin.py admin-token expire [-h] token
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
positional arguments:
Pierre-Yves Chibon d4c9da
  token       API token
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
optional arguments:
Pierre-Yves Chibon d4c9da
  -h, --help  show this help message and exit
Pierre-Yves Chibon d4c9da
''')
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def test_parser_admin_token_invalid_help(self):
Pierre-Yves Chibon d4c9da
        """ Test the _parser_admin_token_expire function of pagure-admin. """
Pierre-Yves Chibon 2a4da5
        if 'BUILD_ID' in os.environ:
Pierre-Yves Chibon 2a4da5
            raise unittest.case.SkipTest('Skipping on jenkins/el7')
Pierre-Yves Chibon 2a4da5
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'foo', '--help']
Pierre-Yves Chibon d4c9da
        self.assertEqual(
Pierre-Yves Chibon d4c9da
            _get_ouput(cmd)[1],
Pierre-Yves Chibon d4c9da
            '''usage: admin.py admin-token [-h] {list,info,expire,create} ...
Pierre-Yves Chibon d4c9da
admin.py admin-token: error: invalid choice: 'foo' (choose from 'list', 'info', 'expire', 'create')
Pierre-Yves Chibon d4c9da
''')  # noqa
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
class PagureAdminAdminTokenEmptytests(tests.Modeltests):
Pierre-Yves Chibon d4c9da
    """ Tests for pagure-admin admin-token when there is nothing in the DB
Pierre-Yves Chibon d4c9da
    """
Pierre-Yves Chibon d4c9da
    def setUp(self):
Pierre-Yves Chibon d4c9da
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon d4c9da
        super(PagureAdminAdminTokenEmptytests, self).setUp()
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        self.configfile = os.path.join(self.path, 'config')
Pierre-Yves Chibon d4c9da
        self.dbpath = "sqlite:///%s/pagure_dev.sqlite" % self.path
Pierre-Yves Chibon d4c9da
        with open(self.configfile, 'w') as stream:
Pierre-Yves Chibon d4c9da
            stream.write('DB_URL="%s"\n' % self.dbpath)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        os.environ['PAGURE_CONFIG'] = self.configfile
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        createdb = os.path.abspath(
Pierre-Yves Chibon d4c9da
            os.path.join(tests.HERE, '..', 'createdb.py'))
Pierre-Yves Chibon d4c9da
        cmd = ['python', createdb]
Pierre-Yves Chibon d4c9da
        _get_ouput(cmd)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def tearDown(self):
Pierre-Yves Chibon d4c9da
        """ Tear down the environnment after running the tests. """
Pierre-Yves Chibon d4c9da
        super(PagureAdminAdminTokenEmptytests, self).tearDown()
Pierre-Yves Chibon d4c9da
        del(os.environ['PAGURE_CONFIG'])
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def test_do_create_admin_token_no_user(self):
Pierre-Yves Chibon d4c9da
        """ Test the do_create_admin_token function of pagure-admin without
Pierre-Yves Chibon d4c9da
        user.
Pierre-Yves Chibon d4c9da
        """
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'create', 'pingou']
Pierre-Yves Chibon d4c9da
        self.assertEqual(_get_ouput(cmd)[0], 'No user "pingou" found\n')
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def test_do_list_admin_token_empty(self):
Pierre-Yves Chibon d4c9da
        """ Test the do_list_admin_token function of pagure-admin when there
Pierre-Yves Chibon d4c9da
        are not tokens in the db.
Pierre-Yves Chibon d4c9da
        """
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list']
Pierre-Yves Chibon d4c9da
        self.assertEqual(_get_ouput(cmd)[0], 'No admin tokens found\n')
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon ede948
class PagureAdminAdminRefreshGitolitetests(tests.Modeltests):
Pierre-Yves Chibon ede948
    """ Tests for pagure-admin refresh-gitolite """
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
    def setUp(self):
Pierre-Yves Chibon ede948
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon ede948
        super(PagureAdminAdminRefreshGitolitetests, self).setUp()
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        self.configfile = os.path.join(self.path, 'config')
Pierre-Yves Chibon ede948
        self.dbpath = "sqlite:///%s/pagure_dev.sqlite" % self.path
Pierre-Yves Chibon ede948
        with open(self.configfile, 'w') as stream:
Pierre-Yves Chibon ede948
            stream.write('DB_URL="%s"\n' % self.dbpath)
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        os.environ['PAGURE_CONFIG'] = self.configfile
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        createdb = os.path.abspath(
Pierre-Yves Chibon ede948
            os.path.join(tests.HERE, '..', 'createdb.py'))
Pierre-Yves Chibon ede948
        cmd = ['python', createdb]
Pierre-Yves Chibon ede948
        _get_ouput(cmd)
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        self.session = pagure.lib.model.create_tables(
Pierre-Yves Chibon ede948
            self.dbpath, acls=pagure.APP.config.get('ACLS', {}))
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        # Create the user pingou
Pierre-Yves Chibon ede948
        item = pagure.lib.model.User(
Pierre-Yves Chibon ede948
            user='pingou',
Pierre-Yves Chibon ede948
            fullname='PY C',
Pierre-Yves Chibon ede948
            password='foo',
Pierre-Yves Chibon ede948
            default_email='bar@pingou.com',
Pierre-Yves Chibon ede948
        )
Pierre-Yves Chibon ede948
        self.session.add(item)
Pierre-Yves Chibon ede948
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon ede948
            user_id=1,
Pierre-Yves Chibon ede948
            email='bar@pingou.com')
Pierre-Yves Chibon ede948
        self.session.add(item)
Pierre-Yves Chibon ede948
        self.session.commit()
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        # Create a couple of projects
Pierre-Yves Chibon ede948
        tests.create_projects(self.session)
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        # Add a group
Pierre-Yves Chibon ede948
        msg = pagure.lib.add_group(
Pierre-Yves Chibon ede948
            self.session,
Pierre-Yves Chibon ede948
            group_name='foo',
Pierre-Yves Chibon ede948
            display_name='foo group',
Pierre-Yves Chibon ede948
            description=None,
Pierre-Yves Chibon ede948
            group_type='bar',
Pierre-Yves Chibon ede948
            user='pingou',
Pierre-Yves Chibon ede948
            is_admin=False,
Pierre-Yves Chibon ede948
            blacklist=[],
Pierre-Yves Chibon ede948
        )
Pierre-Yves Chibon ede948
        self.session.commit()
Pierre-Yves Chibon ede948
        self.assertEqual(msg, 'User `pingou` added to the group `foo`.')
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        # Make the imported pagure use the correct db session
Pierre-Yves Chibon ede948
        pagure.cli.admin.SESSION = self.session
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
    def tearDown(self):
Pierre-Yves Chibon ede948
        """ Tear down the environnment after running the tests. """
Pierre-Yves Chibon ede948
        super(PagureAdminAdminRefreshGitolitetests, self).tearDown()
Pierre-Yves Chibon ede948
        del(os.environ['PAGURE_CONFIG'])
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
    @patch('pagure.cli.admin._ask_confirmation')
Pierre-Yves Chibon ede948
    @patch('pagure.lib.git_auth.get_git_auth_helper')
Pierre-Yves Chibon ede948
    def test_do_refresh_gitolite_no_args(self, get_helper, conf):
Pierre-Yves Chibon ede948
        """ Test the do_generate_acl function with no special args. """
Pierre-Yves Chibon ede948
        conf.return_value = True
Pierre-Yves Chibon ede948
        helper = MagicMock()
Pierre-Yves Chibon ede948
        get_helper.return_value = helper
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        args = munch.Munch(
Pierre-Yves Chibon ede948
            {'group': None, 'project': None, 'all_': False, 'user': None})
Pierre-Yves Chibon ede948
        pagure.cli.admin.do_generate_acl(args)
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        get_helper.assert_called_with('gitolite3')
Pierre-Yves Chibon ede948
        args = helper.generate_acls.call_args
Pierre-Yves Chibon ede948
        self.assertIsNone(args[1].get('group'))
Pierre-Yves Chibon ede948
        self.assertIsNone(args[1].get('project'))
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
    @patch('pagure.cli.admin._ask_confirmation')
Pierre-Yves Chibon ede948
    @patch('pagure.lib.git_auth.get_git_auth_helper')
Pierre-Yves Chibon ede948
    def test_do_refresh_gitolite_all_project(self, get_helper, conf):
Pierre-Yves Chibon ede948
        """ Test the do_generate_acl function for all projects. """
Pierre-Yves Chibon ede948
        conf.return_value = True
Pierre-Yves Chibon ede948
        helper = MagicMock()
Pierre-Yves Chibon ede948
        get_helper.return_value = helper
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        args = munch.Munch(
Pierre-Yves Chibon ede948
            {'group': None, 'project': None, 'all_': True, 'user': None})
Pierre-Yves Chibon ede948
        pagure.cli.admin.do_generate_acl(args)
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        get_helper.assert_called_with('gitolite3')
Pierre-Yves Chibon ede948
        args = helper.generate_acls.call_args
Pierre-Yves Chibon ede948
        self.assertIsNone(args[1].get('group'))
Pierre-Yves Chibon ede948
        self.assertEqual(args[1].get('project'), -1)
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
    @patch('pagure.cli.admin._ask_confirmation')
Pierre-Yves Chibon ede948
    @patch('pagure.lib.git_auth.get_git_auth_helper')
Pierre-Yves Chibon ede948
    def test_do_refresh_gitolite_one_project(self, get_helper, conf):
Pierre-Yves Chibon ede948
        """ Test the do_generate_acl function for a certain project. """
Pierre-Yves Chibon ede948
        conf.return_value = True
Pierre-Yves Chibon ede948
        helper = MagicMock()
Pierre-Yves Chibon ede948
        get_helper.return_value = helper
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        args = munch.Munch(
Pierre-Yves Chibon ede948
            {'group': None, 'project': 'test', 'all_': False, 'user': None})
Pierre-Yves Chibon ede948
        pagure.cli.admin.do_generate_acl(args)
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        get_helper.assert_called_with('gitolite3')
Pierre-Yves Chibon ede948
        args = helper.generate_acls.call_args
Pierre-Yves Chibon ede948
        self.assertIsNone(args[1].get('group'))
Pierre-Yves Chibon ede948
        self.assertEqual(args[1].get('project').fullname, 'test')
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
    @patch('pagure.cli.admin._ask_confirmation')
Pierre-Yves Chibon ede948
    @patch('pagure.lib.git_auth.get_git_auth_helper')
Pierre-Yves Chibon ede948
    def test_do_refresh_gitolite_one_project_and_all(self, get_helper, conf):
Pierre-Yves Chibon ede948
        """ Test the do_generate_acl function for a certain project and all.
Pierre-Yves Chibon ede948
        """
Pierre-Yves Chibon ede948
        conf.return_value = True
Pierre-Yves Chibon ede948
        helper = MagicMock()
Pierre-Yves Chibon ede948
        get_helper.return_value = helper
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        args = munch.Munch(
Pierre-Yves Chibon ede948
            {'group': None, 'project': 'test', 'all_': True, 'user': None})
Pierre-Yves Chibon ede948
        pagure.cli.admin.do_generate_acl(args)
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        get_helper.assert_called_with('gitolite3')
Pierre-Yves Chibon ede948
        args = helper.generate_acls.call_args
Pierre-Yves Chibon ede948
        self.assertIsNone(args[1].get('group'))
Pierre-Yves Chibon ede948
        self.assertEqual(args[1].get('project'), -1)
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
    @patch('pagure.cli.admin._ask_confirmation')
Pierre-Yves Chibon ede948
    @patch('pagure.lib.git_auth.get_git_auth_helper')
Pierre-Yves Chibon ede948
    def test_do_refresh_gitolite_one_group(self, get_helper, conf):
Pierre-Yves Chibon ede948
        """ Test the do_generate_acl function for a certain group. """
Pierre-Yves Chibon ede948
        conf.return_value = True
Pierre-Yves Chibon ede948
        helper = MagicMock()
Pierre-Yves Chibon ede948
        get_helper.return_value = helper
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        args = munch.Munch(
Pierre-Yves Chibon ede948
            {'group': 'foo', 'project': None, 'all_': False, 'user': None})
Pierre-Yves Chibon ede948
        pagure.cli.admin.do_generate_acl(args)
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        get_helper.assert_called_with('gitolite3')
Pierre-Yves Chibon ede948
        args = helper.generate_acls.call_args
Pierre-Yves Chibon ede948
        self.assertEqual(args[1].get('group').group_name, 'foo')
Pierre-Yves Chibon ede948
        self.assertIsNone(args[1].get('project'))
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
Pierre-Yves Chibon d4c9da
class PagureAdminAdminTokentests(tests.Modeltests):
Pierre-Yves Chibon d4c9da
    """ Tests for pagure-admin admin-token """
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def setUp(self):
Pierre-Yves Chibon d4c9da
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon d4c9da
        super(PagureAdminAdminTokentests, self).setUp()
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        self.configfile = os.path.join(self.path, 'config')
Pierre-Yves Chibon d4c9da
        self.dbpath = "sqlite:///%s/pagure_dev.sqlite" % self.path
Pierre-Yves Chibon d4c9da
        with open(self.configfile, 'w') as stream:
Pierre-Yves Chibon d4c9da
            stream.write('DB_URL="%s"\n' % self.dbpath)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        os.environ['PAGURE_CONFIG'] = self.configfile
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        createdb = os.path.abspath(
Pierre-Yves Chibon d4c9da
            os.path.join(tests.HERE, '..', 'createdb.py'))
Pierre-Yves Chibon d4c9da
        cmd = ['python', createdb]
Pierre-Yves Chibon d4c9da
        _get_ouput(cmd)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        self.session = pagure.lib.model.create_tables(
Pierre-Yves Chibon d4c9da
            self.dbpath, acls=pagure.APP.config.get('ACLS', {}))
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        # Create the user pingou
Pierre-Yves Chibon d4c9da
        item = pagure.lib.model.User(
Pierre-Yves Chibon d4c9da
            user='pingou',
Pierre-Yves Chibon d4c9da
            fullname='PY C',
Pierre-Yves Chibon d4c9da
            password='foo',
Pierre-Yves Chibon d4c9da
            default_email='bar@pingou.com',
Pierre-Yves Chibon d4c9da
        )
Pierre-Yves Chibon d4c9da
        self.session.add(item)
Pierre-Yves Chibon d4c9da
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon d4c9da
            user_id=1,
Pierre-Yves Chibon d4c9da
            email='bar@pingou.com')
Pierre-Yves Chibon d4c9da
        self.session.add(item)
Pierre-Yves Chibon d4c9da
        self.session.commit()
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        # Make the imported pagure use the correct db session
Pierre-Yves Chibon d4c9da
        pagure.cli.admin.SESSION = self.session
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    def tearDown(self):
Pierre-Yves Chibon d4c9da
        """ Tear down the environnment after running the tests. """
Pierre-Yves Chibon d4c9da
        super(PagureAdminAdminTokentests, self).tearDown()
Pierre-Yves Chibon d4c9da
        del(os.environ['PAGURE_CONFIG'])
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    @patch('pagure.cli.admin._get_input')
Pierre-Yves Chibon d4c9da
    @patch('pagure.cli.admin._ask_confirmation')
Pierre-Yves Chibon d4c9da
    def test_do_create_admin_token(self, conf, rinp):
Pierre-Yves Chibon d4c9da
        """ Test the do_create_admin_token function of pagure-admin. """
Pierre-Yves Chibon d4c9da
        conf.return_value = True
Pierre-Yves Chibon d4c9da
        rinp.return_value = '1,2,3'
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        args = munch.Munch({'user': 'pingou'})
Pierre-Yves Chibon d4c9da
        pagure.cli.admin.do_create_admin_token(args)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        # Check the outcome
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list']
Pierre-Yves Chibon d4c9da
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon d4c9da
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon d4c9da
        self.assertEqual(len(output.split('\n')), 2)
Pierre-Yves Chibon d4c9da
        self.assertIn(' -- pingou -- ', output)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    @patch('pagure.cli.admin._get_input')
Pierre-Yves Chibon d4c9da
    @patch('pagure.cli.admin._ask_confirmation')
Pierre-Yves Chibon 9a36a4
    def test_do_list_admin_token(self, conf, rinp):
Pierre-Yves Chibon 9a36a4
        """ Test the do_list_admin_token function of pagure-admin. """
Pierre-Yves Chibon 9a36a4
        # Create an admin token to use
Pierre-Yves Chibon 9a36a4
        conf.return_value = True
Pierre-Yves Chibon 9a36a4
        rinp.return_value = '1,2,3'
Pierre-Yves Chibon 9a36a4
Pierre-Yves Chibon 9a36a4
        args = munch.Munch({'user': 'pingou'})
Pierre-Yves Chibon 9a36a4
        pagure.cli.admin.do_create_admin_token(args)
Pierre-Yves Chibon 9a36a4
Pierre-Yves Chibon 9a36a4
        # Retrieve all tokens
Pierre-Yves Chibon 9a36a4
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list']
Pierre-Yves Chibon 9a36a4
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 9a36a4
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 9a36a4
        self.assertEqual(len(output.split('\n')), 2)
Pierre-Yves Chibon 9a36a4
        self.assertIn(' -- pingou -- ', output)
Pierre-Yves Chibon 9a36a4
Pierre-Yves Chibon 9a36a4
        # Retrieve pfrields's tokens
Pierre-Yves Chibon 9a36a4
        cmd = [
Pierre-Yves Chibon 9a36a4
            'python', PAGURE_ADMIN,
Pierre-Yves Chibon 9a36a4
            'admin-token', 'list', '--user', 'pfrields']
Pierre-Yves Chibon 9a36a4
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 9a36a4
        self.assertEqual(output, 'No admin tokens found\n')
Pierre-Yves Chibon 9a36a4
Pierre-Yves Chibon 9a36a4
    @patch('pagure.cli.admin._get_input')
Pierre-Yves Chibon 9a36a4
    @patch('pagure.cli.admin._ask_confirmation')
Pierre-Yves Chibon d4c9da
    def test_do_info_admin_token(self, conf, rinp):
Pierre-Yves Chibon d4c9da
        """ Test the do_info_admin_token function of pagure-admin. """
Pierre-Yves Chibon d4c9da
        # Create an admin token to use
Pierre-Yves Chibon d4c9da
        conf.return_value = True
Pierre-Yves Chibon e22186
        rinp.return_value = '1,3,4'
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        args = munch.Munch({'user': 'pingou'})
Pierre-Yves Chibon d4c9da
        pagure.cli.admin.do_create_admin_token(args)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        # Retrieve the token
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list']
Pierre-Yves Chibon d4c9da
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon d4c9da
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon d4c9da
        self.assertEqual(len(output.split('\n')), 2)
Pierre-Yves Chibon d4c9da
        self.assertIn(' -- pingou -- ', output)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        token = output.split(' ', 1)[0]
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'info', token]
Pierre-Yves Chibon d4c9da
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon d4c9da
        self.assertIn(' -- pingou -- ', output.split('\n', 1)[0])
Pierre-Yves Chibon d4c9da
        self.assertEqual(
Pierre-Yves Chibon d4c9da
            output.split('\n', 1)[1], '''ACLs:
Pierre-Yves Chibon d4c9da
  - issue_create
Pierre-Yves Chibon d4c9da
  - pull_request_comment
Pierre-Yves Chibon d4c9da
  - pull_request_flag
Pierre-Yves Chibon d4c9da
''')
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
    @patch('pagure.cli.admin._get_input')
Pierre-Yves Chibon d4c9da
    @patch('pagure.cli.admin._ask_confirmation')
Pierre-Yves Chibon d4c9da
    def test_do_expire_admin_token(self, conf, rinp):
Pierre-Yves Chibon d4c9da
        """ Test the do_expire_admin_token function of pagure-admin. """
Pierre-Yves Chibon d8759f
        if 'BUILD_ID' in os.environ:
Pierre-Yves Chibon d8759f
            raise unittest.case.SkipTest('Skipping on jenkins/el7')
Pierre-Yves Chibon d8759f
Pierre-Yves Chibon d4c9da
        # Create an admin token to use
Pierre-Yves Chibon d4c9da
        conf.return_value = True
Pierre-Yves Chibon d4c9da
        rinp.return_value = '1,2,3'
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        args = munch.Munch({'user': 'pingou'})
Pierre-Yves Chibon d4c9da
        pagure.cli.admin.do_create_admin_token(args)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        # Retrieve the token
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list']
Pierre-Yves Chibon d4c9da
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon d4c9da
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon d4c9da
        self.assertEqual(len(output.split('\n')), 2)
Pierre-Yves Chibon d4c9da
        self.assertIn(' -- pingou -- ', output)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        token = output.split(' ', 1)[0]
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        # Before
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list', '--active']
Pierre-Yves Chibon d4c9da
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon d4c9da
        self.assertNotEqual(output, 'No admin tokens found\n')
Pierre-Yves Chibon d4c9da
        self.assertEqual(len(output.split('\n')), 2)
Pierre-Yves Chibon d4c9da
        self.assertIn(' -- pingou -- ', output)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        # Expire the token
Pierre-Yves Chibon d4c9da
        args = munch.Munch({'token': token})
Pierre-Yves Chibon d4c9da
        pagure.cli.admin.do_expire_admin_token(args)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        # After
Pierre-Yves Chibon d4c9da
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list', '--active']
Pierre-Yves Chibon d4c9da
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon d4c9da
        self.assertEqual(output, 'No admin tokens found\n')
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
if __name__ == '__main__':
Pierre-Yves Chibon d4c9da
    unittest.main(verbosity=2)