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 87a6d0
import datetime  # noqa
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 25e586
    maxDiff = None
Pierre-Yves Chibon 25e586
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 3a016c
                {refresh-gitolite,refresh-ssh,clear-hook-token,admin-token,get-watch,update-watch,read-only}
Pierre-Yves Chibon f7c773
                ...
Pierre-Yves Chibon f7c773
admin.py: error: too few arguments
Pierre-Yves Chibon 25e586
''')  # noqa
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 3a016c
                {refresh-gitolite,refresh-ssh,clear-hook-token,admin-token,get-watch,update-watch,read-only}
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 3a016c
  {refresh-gitolite,refresh-ssh,clear-hook-token,admin-token,get-watch,update-watch,read-only}
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 25e586
    get-watch           Get someone's watch status on a project
Pierre-Yves Chibon 25e586
    update-watch        Update someone's watch status on a project
Pierre-Yves Chibon 3a016c
    read-only           Get or set the read-only flag on a project
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 87a6d0
            '''usage: admin.py admin-token [-h] {list,info,expire,create,update} ...
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 87a6d0
  {list,info,expire,create,update}
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 87a6d0
    update              Update the expiration date of an 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 87a6d0
    def test_parser_admin_token_update_help(self):
Pierre-Yves Chibon 87a6d0
        """ Test the parser_admin_token_create function of pagure-admin. """
Pierre-Yves Chibon 87a6d0
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'update', '--help']
Pierre-Yves Chibon 87a6d0
        self.assertEqual(
Pierre-Yves Chibon 87a6d0
            _get_ouput(cmd)[0],
Pierre-Yves Chibon 87a6d0
            '''usage: admin.py admin-token update [-h] token date
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
positional arguments:
Pierre-Yves Chibon 87a6d0
  token       API token
Pierre-Yves Chibon 87a6d0
  date        New expiration date
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
optional arguments:
Pierre-Yves Chibon 87a6d0
  -h, --help  show this help message and exit
Pierre-Yves Chibon 87a6d0
''')
Pierre-Yves Chibon 87a6d0
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 87a6d0
            '''usage: admin.py admin-token [-h] {list,info,expire,create,update} ...
Pierre-Yves Chibon 87a6d0
admin.py admin-token: error: invalid choice: 'foo' (choose from 'list', 'info', 'expire', 'create', 'update')
Pierre-Yves Chibon d4c9da
''')  # noqa
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon 25e586
    def test_parser_get_watch(self):
Pierre-Yves Chibon 25e586
        """ Test the _parser_get_watch function of pagure-admin. """
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'get-watch', '--help']
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 25e586
            _get_ouput(cmd)[0],
Pierre-Yves Chibon 25e586
            '''usage: admin.py get-watch [-h] project user
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
positional arguments:
Pierre-Yves Chibon 25e586
  project     Project (as namespace/project if there is a namespace) -- Fork
Pierre-Yves Chibon 25e586
              not supported
Pierre-Yves Chibon 25e586
  user        User to get the watch status of
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
optional arguments:
Pierre-Yves Chibon 25e586
  -h, --help  show this help message and exit
Pierre-Yves Chibon 25e586
''')
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def test_parser_update_watch(self):
Pierre-Yves Chibon 25e586
        """ Test the _parser_update_watch function of pagure-admin. """
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'update-watch', '--help']
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 25e586
            _get_ouput(cmd)[0],
Pierre-Yves Chibon 25e586
            '''usage: admin.py update-watch [-h] [-s STATUS] project user
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
positional arguments:
Pierre-Yves Chibon 25e586
  project               Project to update (as namespace/project if there is a
Pierre-Yves Chibon 25e586
                        namespace) -- Fork not supported
Pierre-Yves Chibon 25e586
  user                  User to update the watch status of
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
optional arguments:
Pierre-Yves Chibon 25e586
  -h, --help            show this help message and exit
Pierre-Yves Chibon 25e586
  -s STATUS, --status STATUS
Pierre-Yves Chibon 25e586
                        Watch status to update to
Pierre-Yves Chibon 25e586
''')
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 3a016c
    def test_parser_read_only(self):
Pierre-Yves Chibon 3a016c
        """ Test the _parser_update_watch function of pagure-admin. """
Pierre-Yves Chibon 3a016c
        cmd = ['python', PAGURE_ADMIN, 'read-only', '--help']
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 3a016c
            _get_ouput(cmd)[0],
Pierre-Yves Chibon 3a016c
            '''usage: admin.py read-only [-h] [--user USER] [--ro RO] project
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
positional arguments:
Pierre-Yves Chibon 3a016c
  project      Project to update (as namespace/project if there is a
Pierre-Yves Chibon 3a016c
               namespace)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
optional arguments:
Pierre-Yves Chibon 3a016c
  -h, --help   show this help message and exit
Pierre-Yves Chibon 3a016c
  --user USER  User of the project (to use only on forks)
Pierre-Yves Chibon 3a016c
  --ro RO      Read-Only status to set (has to be: true or false), do not
Pierre-Yves Chibon 3a016c
               specify to get the current status
Pierre-Yves Chibon 3a016c
''')
Pierre-Yves Chibon 3a016c
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 87a6d0
    @patch('pagure.cli.admin._get_input')
Pierre-Yves Chibon 87a6d0
    @patch('pagure.cli.admin._ask_confirmation')
Pierre-Yves Chibon 87a6d0
    def test_do_update_admin_token_invalid_date(self, conf, rinp):
Pierre-Yves Chibon 87a6d0
        """ Test the do_update_admin_token function of pagure-admin with
Pierre-Yves Chibon 87a6d0
        an invalid date. """
Pierre-Yves Chibon 87a6d0
        if 'BUILD_ID' in os.environ:
Pierre-Yves Chibon 87a6d0
            raise unittest.case.SkipTest('Skipping on jenkins/el7')
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Create an admin token to use
Pierre-Yves Chibon 87a6d0
        conf.return_value = True
Pierre-Yves Chibon 87a6d0
        rinp.return_value = '1,2,3'
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        args = munch.Munch({'user': 'pingou'})
Pierre-Yves Chibon 87a6d0
        pagure.cli.admin.do_create_admin_token(args)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Retrieve the token
Pierre-Yves Chibon 87a6d0
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list']
Pierre-Yves Chibon 87a6d0
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 87a6d0
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 87a6d0
        self.assertEqual(len(output.split('\n')), 2)
Pierre-Yves Chibon 87a6d0
        self.assertIn(' -- pingou -- ', output)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        token = output.split(' ', 1)[0]
Pierre-Yves Chibon 87a6d0
        current_expiration = output.split(' ', 1)[1]
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Set the expiration date to the token
Pierre-Yves Chibon 87a6d0
        args = munch.Munch({'token': token, 'date': 'aa-bb-cc'})
Pierre-Yves Chibon 87a6d0
        self.assertRaises(
Pierre-Yves Chibon 87a6d0
            pagure.exceptions.PagureException,
Pierre-Yves Chibon 87a6d0
            pagure.cli.admin.do_update_admin_token,
Pierre-Yves Chibon 87a6d0
            args
Pierre-Yves Chibon 87a6d0
        )
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
    @patch('pagure.cli.admin._get_input')
Pierre-Yves Chibon 87a6d0
    @patch('pagure.cli.admin._ask_confirmation')
Pierre-Yves Chibon 87a6d0
    def test_do_update_admin_token_invalid_date2(self, conf, rinp):
Pierre-Yves Chibon 87a6d0
        """ Test the do_update_admin_token function of pagure-admin with
Pierre-Yves Chibon 87a6d0
        an invalid date. """
Pierre-Yves Chibon 87a6d0
        if 'BUILD_ID' in os.environ:
Pierre-Yves Chibon 87a6d0
            raise unittest.case.SkipTest('Skipping on jenkins/el7')
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Create an admin token to use
Pierre-Yves Chibon 87a6d0
        conf.return_value = True
Pierre-Yves Chibon 87a6d0
        rinp.return_value = '1,2,3'
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        args = munch.Munch({'user': 'pingou'})
Pierre-Yves Chibon 87a6d0
        pagure.cli.admin.do_create_admin_token(args)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Retrieve the token
Pierre-Yves Chibon 87a6d0
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list']
Pierre-Yves Chibon 87a6d0
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 87a6d0
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 87a6d0
        self.assertEqual(len(output.split('\n')), 2)
Pierre-Yves Chibon 87a6d0
        self.assertIn(' -- pingou -- ', output)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        token = output.split(' ', 1)[0]
Pierre-Yves Chibon 87a6d0
        current_expiration = output.split(' ', 1)[1]
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Set the expiration date to the token
Pierre-Yves Chibon 87a6d0
        args = munch.Munch({'token': token, 'date': '2017-18-01'})
Pierre-Yves Chibon 87a6d0
        self.assertRaises(
Pierre-Yves Chibon 87a6d0
            pagure.exceptions.PagureException,
Pierre-Yves Chibon 87a6d0
            pagure.cli.admin.do_update_admin_token,
Pierre-Yves Chibon 87a6d0
            args
Pierre-Yves Chibon 87a6d0
        )
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
    @patch('pagure.cli.admin._get_input')
Pierre-Yves Chibon 87a6d0
    @patch('pagure.cli.admin._ask_confirmation')
Pierre-Yves Chibon 87a6d0
    def test_do_update_admin_token_invalid_date3(self, conf, rinp):
Pierre-Yves Chibon 87a6d0
        """ Test the do_update_admin_token function of pagure-admin with
Pierre-Yves Chibon 87a6d0
        an invalid date (is today). """
Pierre-Yves Chibon 87a6d0
        if 'BUILD_ID' in os.environ:
Pierre-Yves Chibon 87a6d0
            raise unittest.case.SkipTest('Skipping on jenkins/el7')
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Create an admin token to use
Pierre-Yves Chibon 87a6d0
        conf.return_value = True
Pierre-Yves Chibon 87a6d0
        rinp.return_value = '1,2,3'
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        args = munch.Munch({'user': 'pingou'})
Pierre-Yves Chibon 87a6d0
        pagure.cli.admin.do_create_admin_token(args)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Retrieve the token
Pierre-Yves Chibon 87a6d0
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list']
Pierre-Yves Chibon 87a6d0
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 87a6d0
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 87a6d0
        self.assertEqual(len(output.split('\n')), 2)
Pierre-Yves Chibon 87a6d0
        self.assertIn(' -- pingou -- ', output)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        token = output.split(' ', 1)[0]
Pierre-Yves Chibon 87a6d0
        current_expiration = output.split(' ', 1)[1]
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Set the expiration date to the token
Pierre-Yves Chibon 87a6d0
        args = munch.Munch({
Pierre-Yves Chibon 87a6d0
            'token': token, 'date': datetime.datetime.utcnow().date()
Pierre-Yves Chibon 87a6d0
        })
Pierre-Yves Chibon 87a6d0
        self.assertRaises(
Pierre-Yves Chibon 87a6d0
            pagure.exceptions.PagureException,
Pierre-Yves Chibon 87a6d0
            pagure.cli.admin.do_update_admin_token,
Pierre-Yves Chibon 87a6d0
            args
Pierre-Yves Chibon 87a6d0
        )
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
    @patch('pagure.cli.admin._get_input')
Pierre-Yves Chibon 87a6d0
    @patch('pagure.cli.admin._ask_confirmation')
Pierre-Yves Chibon 87a6d0
    def test_do_update_admin_token(self, conf, rinp):
Pierre-Yves Chibon 87a6d0
        """ Test the do_update_admin_token function of pagure-admin. """
Pierre-Yves Chibon 87a6d0
        if 'BUILD_ID' in os.environ:
Pierre-Yves Chibon 87a6d0
            raise unittest.case.SkipTest('Skipping on jenkins/el7')
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Create an admin token to use
Pierre-Yves Chibon 87a6d0
        conf.return_value = True
Pierre-Yves Chibon 87a6d0
        rinp.return_value = '1,2,3'
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        args = munch.Munch({'user': 'pingou'})
Pierre-Yves Chibon 87a6d0
        pagure.cli.admin.do_create_admin_token(args)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Retrieve the token
Pierre-Yves Chibon 87a6d0
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list']
Pierre-Yves Chibon 87a6d0
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 87a6d0
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 87a6d0
        self.assertEqual(len(output.split('\n')), 2)
Pierre-Yves Chibon 87a6d0
        self.assertIn(' -- pingou -- ', output)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        token = output.split(' ', 1)[0]
Pierre-Yves Chibon 87a6d0
        current_expiration = output.strip().split(' -- ', 2)[-1]
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Before
Pierre-Yves Chibon 87a6d0
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list', '--active']
Pierre-Yves Chibon 87a6d0
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 87a6d0
        self.assertNotEqual(output, 'No admin tokens found\n')
Pierre-Yves Chibon 87a6d0
        self.assertEqual(len(output.split('\n')), 2)
Pierre-Yves Chibon 87a6d0
        self.assertIn(' -- pingou -- ', output)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        deadline = datetime.datetime.utcnow().date() \
Pierre-Yves Chibon 87a6d0
            + datetime.timedelta(days=3)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Set the expiration date to the token
Pierre-Yves Chibon 87a6d0
        args = munch.Munch({
Pierre-Yves Chibon 87a6d0
            'token': token,
Pierre-Yves Chibon 87a6d0
            'date': deadline.strftime('%Y-%m-%d')
Pierre-Yves Chibon 87a6d0
        })
Pierre-Yves Chibon 87a6d0
        pagure.cli.admin.do_update_admin_token(args)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # After
Pierre-Yves Chibon 87a6d0
        cmd = ['python', PAGURE_ADMIN, 'admin-token', 'list', '--active']
Pierre-Yves Chibon 87a6d0
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 87a6d0
        self.assertEqual(output.split(' ', 1)[0], token)
Pierre-Yves Chibon 87a6d0
        self.assertNotEqual(
Pierre-Yves Chibon 87a6d0
            output.strip().split(' -- ', 2)[-1],
Pierre-Yves Chibon 87a6d0
            current_expiration)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon 25e586
class PagureAdminGetWatchTests(tests.Modeltests):
Pierre-Yves Chibon 25e586
    """ Tests for pagure-admin get-watch """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def setUp(self):
Pierre-Yves Chibon 25e586
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 25e586
        super(PagureAdminGetWatchTests, self).setUp()
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        self.configfile = os.path.join(self.path, 'config')
Pierre-Yves Chibon 25e586
        self.dbpath = "sqlite:///%s/pagure_dev.sqlite" % self.path
Pierre-Yves Chibon 25e586
        with open(self.configfile, 'w') as stream:
Pierre-Yves Chibon 25e586
            stream.write('DB_URL="%s"\n' % self.dbpath)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        os.environ['PAGURE_CONFIG'] = self.configfile
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        createdb = os.path.abspath(
Pierre-Yves Chibon 25e586
            os.path.join(tests.HERE, '..', 'createdb.py'))
Pierre-Yves Chibon 25e586
        cmd = ['python', createdb]
Pierre-Yves Chibon 25e586
        _get_ouput(cmd)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        self.session = pagure.lib.model.create_tables(
Pierre-Yves Chibon 25e586
            self.dbpath, acls=pagure.APP.config.get('ACLS', {}))
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        # Create the user pingou
Pierre-Yves Chibon 25e586
        item = pagure.lib.model.User(
Pierre-Yves Chibon 25e586
            user='pingou',
Pierre-Yves Chibon 25e586
            fullname='PY C',
Pierre-Yves Chibon 25e586
            password='foo',
Pierre-Yves Chibon 25e586
            default_email='bar@pingou.com',
Pierre-Yves Chibon 25e586
        )
Pierre-Yves Chibon 25e586
        self.session.add(item)
Pierre-Yves Chibon 25e586
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon 25e586
            user_id=1,
Pierre-Yves Chibon 25e586
            email='bar@pingou.com')
Pierre-Yves Chibon 25e586
        self.session.add(item)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        # Create the user foo
Pierre-Yves Chibon 25e586
        item = pagure.lib.model.User(
Pierre-Yves Chibon 25e586
            user='foo',
Pierre-Yves Chibon 25e586
            fullname='foo B.',
Pierre-Yves Chibon 25e586
            password='foob',
Pierre-Yves Chibon 25e586
            default_email='foo@pingou.com',
Pierre-Yves Chibon 25e586
        )
Pierre-Yves Chibon 25e586
        self.session.add(item)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        # Create two projects for the user pingou
Pierre-Yves Chibon 25e586
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 25e586
            user_id=1,  # pingou
Pierre-Yves Chibon 25e586
            name='test',
Pierre-Yves Chibon 25e586
            description='namespaced test project',
Pierre-Yves Chibon 25e586
            hook_token='aaabbbeee',
Pierre-Yves Chibon 25e586
            namespace='somenamespace',
Pierre-Yves Chibon 25e586
        )
Pierre-Yves Chibon 25e586
        self.session.add(item)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 25e586
            user_id=1,  # pingou
Pierre-Yves Chibon 25e586
            name='test',
Pierre-Yves Chibon 25e586
            description='Test project',
Pierre-Yves Chibon 25e586
            hook_token='aaabbbccc',
Pierre-Yves Chibon 25e586
            namespace=None,
Pierre-Yves Chibon 25e586
        )
Pierre-Yves Chibon 25e586
        self.session.add(item)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        self.session.commit()
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        # Make the imported pagure use the correct db session
Pierre-Yves Chibon 25e586
        pagure.cli.admin.SESSION = self.session
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def tearDown(self):
Pierre-Yves Chibon 25e586
        """ Tear down the environnment after running the tests. """
Pierre-Yves Chibon 25e586
        super(PagureAdminGetWatchTests, self).tearDown()
Pierre-Yves Chibon 25e586
        del(os.environ['PAGURE_CONFIG'])
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def test_get_watch_get_project_unknown_project(self):
Pierre-Yves Chibon 25e586
        """ Test the get-watch function of pagure-admin with an unknown
Pierre-Yves Chibon 25e586
        project.
Pierre-Yves Chibon 25e586
        """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'get-watch', 'foobar', 'pingou']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual('No project found with: foobar\n', output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def test_get_watch_get_project_invalid_project(self):
Pierre-Yves Chibon 25e586
        """ Test the get-watch function of pagure-admin with an invalid
Pierre-Yves Chibon 25e586
        project.
Pierre-Yves Chibon 25e586
        """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'get-watch', 'fo/o/bar', 'pingou']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 25e586
            'Invalid project name, has more than one "/": fo/o/bar\n',
Pierre-Yves Chibon 25e586
            output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def test_get_watch_get_project_invalid_user(self):
Pierre-Yves Chibon 25e586
        """ Test the get-watch function of pagure-admin on a invalid user.
Pierre-Yves Chibon 25e586
        """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'get-watch', 'test', 'beebop']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual('No user "beebop" found\n', output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def test_get_watch_get_project(self):
Pierre-Yves Chibon 25e586
        """ Test the get-watch function of pagure-admin on a regular project.
Pierre-Yves Chibon 25e586
        """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'get-watch', 'test', 'pingou']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 25e586
            'On test user: pingou is watching the following items: '
Pierre-Yves Chibon 25e586
            'issues, pull-requests\n', output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def test_get_watch_get_project_not_watching(self):
Pierre-Yves Chibon 25e586
        """ Test the get-watch function of pagure-admin on a regular project.
Pierre-Yves Chibon 25e586
        """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'get-watch', 'test', 'foo']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 25e586
            'On test user: foo is watching the following items: None\n',
Pierre-Yves Chibon 25e586
            output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def test_get_watch_get_project_namespaced(self):
Pierre-Yves Chibon 25e586
        """ Test the get-watch function of pagure-admin on a namespaced project.
Pierre-Yves Chibon 25e586
        """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = [
Pierre-Yves Chibon 25e586
            'python', PAGURE_ADMIN, 'get-watch',
Pierre-Yves Chibon 25e586
            'somenamespace/test', 'pingou']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 25e586
            'On somenamespace/test user: pingou is watching the following '
Pierre-Yves Chibon 25e586
            'items: issues, pull-requests\n', output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def test_get_watch_get_project_namespaced_not_watching(self):
Pierre-Yves Chibon 25e586
        """ Test the get-watch function of pagure-admin on a namespaced project.
Pierre-Yves Chibon 25e586
        """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = [
Pierre-Yves Chibon 25e586
            'python', PAGURE_ADMIN, 'get-watch',
Pierre-Yves Chibon 25e586
            'somenamespace/test', 'foo']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 25e586
            'On somenamespace/test user: foo is watching the following '
Pierre-Yves Chibon 25e586
            'items: None\n', output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
class PagureAdminUpdateWatchTests(tests.Modeltests):
Pierre-Yves Chibon 25e586
    """ Tests for pagure-admin update-watch """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def setUp(self):
Pierre-Yves Chibon 25e586
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 25e586
        super(PagureAdminUpdateWatchTests, self).setUp()
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        self.configfile = os.path.join(self.path, 'config')
Pierre-Yves Chibon 25e586
        self.dbpath = "sqlite:///%s/pagure_dev.sqlite" % self.path
Pierre-Yves Chibon 25e586
        with open(self.configfile, 'w') as stream:
Pierre-Yves Chibon 25e586
            stream.write('DB_URL="%s"\n' % self.dbpath)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        os.environ['PAGURE_CONFIG'] = self.configfile
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        createdb = os.path.abspath(
Pierre-Yves Chibon 25e586
            os.path.join(tests.HERE, '..', 'createdb.py'))
Pierre-Yves Chibon 25e586
        cmd = ['python', createdb]
Pierre-Yves Chibon 25e586
        _get_ouput(cmd)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        self.session = pagure.lib.model.create_tables(
Pierre-Yves Chibon 25e586
            self.dbpath, acls=pagure.APP.config.get('ACLS', {}))
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        # Create the user pingou
Pierre-Yves Chibon 25e586
        item = pagure.lib.model.User(
Pierre-Yves Chibon 25e586
            user='pingou',
Pierre-Yves Chibon 25e586
            fullname='PY C',
Pierre-Yves Chibon 25e586
            password='foo',
Pierre-Yves Chibon 25e586
            default_email='bar@pingou.com',
Pierre-Yves Chibon 25e586
        )
Pierre-Yves Chibon 25e586
        self.session.add(item)
Pierre-Yves Chibon 25e586
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon 25e586
            user_id=1,
Pierre-Yves Chibon 25e586
            email='bar@pingou.com')
Pierre-Yves Chibon 25e586
        self.session.add(item)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        # Create the user foo
Pierre-Yves Chibon 25e586
        item = pagure.lib.model.User(
Pierre-Yves Chibon 25e586
            user='foo',
Pierre-Yves Chibon 25e586
            fullname='foo B.',
Pierre-Yves Chibon 25e586
            password='foob',
Pierre-Yves Chibon 25e586
            default_email='foo@pingou.com',
Pierre-Yves Chibon 25e586
        )
Pierre-Yves Chibon 25e586
        self.session.add(item)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        # Create two projects for the user pingou
Pierre-Yves Chibon 25e586
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 25e586
            user_id=1,  # pingou
Pierre-Yves Chibon 25e586
            name='test',
Pierre-Yves Chibon 25e586
            description='namespaced test project',
Pierre-Yves Chibon 25e586
            hook_token='aaabbbeee',
Pierre-Yves Chibon 25e586
            namespace='somenamespace',
Pierre-Yves Chibon 25e586
        )
Pierre-Yves Chibon 25e586
        self.session.add(item)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 25e586
            user_id=1,  # pingou
Pierre-Yves Chibon 25e586
            name='test',
Pierre-Yves Chibon 25e586
            description='Test project',
Pierre-Yves Chibon 25e586
            hook_token='aaabbbccc',
Pierre-Yves Chibon 25e586
            namespace=None,
Pierre-Yves Chibon 25e586
        )
Pierre-Yves Chibon 25e586
        self.session.add(item)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        self.session.commit()
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        # Make the imported pagure use the correct db session
Pierre-Yves Chibon 25e586
        pagure.cli.admin.SESSION = self.session
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def tearDown(self):
Pierre-Yves Chibon 25e586
        """ Tear down the environnment after running the tests. """
Pierre-Yves Chibon 25e586
        super(PagureAdminUpdateWatchTests, self).tearDown()
Pierre-Yves Chibon 25e586
        del(os.environ['PAGURE_CONFIG'])
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def test_get_watch_update_project_unknown_project(self):
Pierre-Yves Chibon 25e586
        """ Test the update-watch function of pagure-admin on an unknown
Pierre-Yves Chibon 25e586
        project.
Pierre-Yves Chibon 25e586
        """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'update-watch', 'foob', 'pingou', '-s=1']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual('No project found with: foob\n', output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def test_get_watch_update_project_invalid_project(self):
Pierre-Yves Chibon 25e586
        """ Test the update-watch function of pagure-admin on an invalid
Pierre-Yves Chibon 25e586
        project.
Pierre-Yves Chibon 25e586
        """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'update-watch', 'fo/o/b', 'pingou', '-s=1']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 25e586
            'Invalid project name, has more than one "/": fo/o/b\n',
Pierre-Yves Chibon 25e586
            output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def test_get_watch_update_project_invalid_user(self):
Pierre-Yves Chibon 25e586
        """ Test the update-watch function of pagure-admin on an invalid user.
Pierre-Yves Chibon 25e586
        """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'update-watch', 'test', 'foob', '-s=1']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual('No user "foob" found\n', output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def test_get_watch_update_project_invalid_status(self):
Pierre-Yves Chibon 25e586
        """ Test the update-watch function of pagure-admin with an invalid
Pierre-Yves Chibon 25e586
        status.
Pierre-Yves Chibon 25e586
        """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'update-watch', 'test', 'pingou', '-s=10']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 25e586
            'Invalid status provided: 10 not in -1, 0, 1, 2, 3\n', output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
    def test_get_watch_update_project_no_effect(self):
Pierre-Yves Chibon 25e586
        """ Test the update-watch function of pagure-admin with a regular
Pierre-Yves Chibon 25e586
        project - nothing changed.
Pierre-Yves Chibon 25e586
        """
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'get-watch', 'test', 'pingou']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 25e586
            'On test user: pingou is watching the following items: '
Pierre-Yves Chibon 25e586
            'issues, pull-requests\n', output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'update-watch', 'test', 'pingou', '-s=1']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 25e586
            'Updating watch status of pingou to 1 (watch issues and PRs) '
Pierre-Yves Chibon 25e586
            'on test\n', output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        cmd = ['python', PAGURE_ADMIN, 'get-watch', 'test', 'pingou']
Pierre-Yves Chibon 25e586
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 25e586
            'On test user: pingou is watching the following items: '
Pierre-Yves Chibon 25e586
            'issues, pull-requests\n', output)
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 3a016c
class PagureAdminReadOnlyTests(tests.Modeltests):
Pierre-Yves Chibon 3a016c
    """ Tests for pagure-admin read-only """
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
    def setUp(self):
Pierre-Yves Chibon 3a016c
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 3a016c
        super(PagureAdminReadOnlyTests, self).setUp()
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        self.configfile = os.path.join(self.path, 'config')
Pierre-Yves Chibon 3a016c
        self.dbpath = "sqlite:///%s/pagure_dev.sqlite" % self.path
Pierre-Yves Chibon 3a016c
        with open(self.configfile, 'w') as stream:
Pierre-Yves Chibon 3a016c
            stream.write('DB_URL="%s"\n' % self.dbpath)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        os.environ['PAGURE_CONFIG'] = self.configfile
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        createdb = os.path.abspath(
Pierre-Yves Chibon 3a016c
            os.path.join(tests.HERE, '..', 'createdb.py'))
Pierre-Yves Chibon 3a016c
        cmd = ['python', createdb]
Pierre-Yves Chibon 3a016c
        _get_ouput(cmd)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        self.session = pagure.lib.model.create_tables(
Pierre-Yves Chibon 3a016c
            self.dbpath, acls=pagure.APP.config.get('ACLS', {}))
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        # Create the user pingou
Pierre-Yves Chibon 3a016c
        item = pagure.lib.model.User(
Pierre-Yves Chibon 3a016c
            user='pingou',
Pierre-Yves Chibon 3a016c
            fullname='PY C',
Pierre-Yves Chibon 3a016c
            password='foo',
Pierre-Yves Chibon 3a016c
            default_email='bar@pingou.com',
Pierre-Yves Chibon 3a016c
        )
Pierre-Yves Chibon 3a016c
        self.session.add(item)
Pierre-Yves Chibon 3a016c
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon 3a016c
            user_id=1,
Pierre-Yves Chibon 3a016c
            email='bar@pingou.com')
Pierre-Yves Chibon 3a016c
        self.session.add(item)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        # Create two projects for the user pingou
Pierre-Yves Chibon 3a016c
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 3a016c
            user_id=1,  # pingou
Pierre-Yves Chibon 3a016c
            name='test',
Pierre-Yves Chibon 3a016c
            description='namespaced test project',
Pierre-Yves Chibon 3a016c
            hook_token='aaabbbeee',
Pierre-Yves Chibon 3a016c
            namespace='somenamespace',
Pierre-Yves Chibon 3a016c
        )
Pierre-Yves Chibon 3a016c
        self.session.add(item)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 3a016c
            user_id=1,  # pingou
Pierre-Yves Chibon 3a016c
            name='test',
Pierre-Yves Chibon 3a016c
            description='Test project',
Pierre-Yves Chibon 3a016c
            hook_token='aaabbbccc',
Pierre-Yves Chibon 3a016c
            namespace=None,
Pierre-Yves Chibon 3a016c
        )
Pierre-Yves Chibon 3a016c
        self.session.add(item)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        self.session.commit()
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        # Make the imported pagure use the correct db session
Pierre-Yves Chibon 3a016c
        pagure.cli.admin.SESSION = self.session
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
    def tearDown(self):
Pierre-Yves Chibon 3a016c
        """ Tear down the environnment after running the tests. """
Pierre-Yves Chibon 3a016c
        super(PagureAdminReadOnlyTests, self).tearDown()
Pierre-Yves Chibon 3a016c
        del(os.environ['PAGURE_CONFIG'])
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
    def test_read_only_unknown_project(self):
Pierre-Yves Chibon 3a016c
        """ Test the read-only function of pagure-admin on an unknown
Pierre-Yves Chibon 3a016c
        project.
Pierre-Yves Chibon 3a016c
        """
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        cmd = ['python', PAGURE_ADMIN, 'read-only', 'foob']
Pierre-Yves Chibon 3a016c
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 3a016c
        self.assertEqual('No project found with: foob\n', output)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
    def test_read_only_invalid_project(self):
Pierre-Yves Chibon 3a016c
        """ Test the read-only function of pagure-admin on an invalid
Pierre-Yves Chibon 3a016c
        project.
Pierre-Yves Chibon 3a016c
        """
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        cmd = ['python', PAGURE_ADMIN, 'read-only', 'fo/o/b']
Pierre-Yves Chibon 3a016c
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 3a016c
            'Invalid project name, has more than one "/": fo/o/b\n',
Pierre-Yves Chibon 3a016c
            output)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
    def test_read_only(self):
Pierre-Yves Chibon 3a016c
        """ Test the read-only function of pagure-admin to get status of
Pierre-Yves Chibon 3a016c
        a non-namespaced project.
Pierre-Yves Chibon 3a016c
        """
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        cmd = ['python', PAGURE_ADMIN, 'read-only', 'test']
Pierre-Yves Chibon 3a016c
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 3a016c
            'The current read-only flag of the project test is set to True\n',
Pierre-Yves Chibon 3a016c
            output)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
    def test_read_only_namespace(self):
Pierre-Yves Chibon 3a016c
        """ Test the read-only function of pagure-admin to get status of
Pierre-Yves Chibon 3a016c
        a namespaced project.
Pierre-Yves Chibon 3a016c
        """
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        cmd = ['python', PAGURE_ADMIN, 'read-only', 'somenamespace/test']
Pierre-Yves Chibon 3a016c
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 3a016c
            'The current read-only flag of the project somenamespace/test '\
Pierre-Yves Chibon 3a016c
            'is set to True\n', output)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
    def test_read_only_namespace_changed(self):
Pierre-Yves Chibon 3a016c
        """ Test the read-only function of pagure-admin to set the status of
Pierre-Yves Chibon 3a016c
        a namespaced project.
Pierre-Yves Chibon 3a016c
        """
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        # Before
Pierre-Yves Chibon 3a016c
        cmd = ['python', PAGURE_ADMIN, 'read-only', 'somenamespace/test']
Pierre-Yves Chibon 3a016c
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 3a016c
            'The current read-only flag of the project somenamespace/test '\
Pierre-Yves Chibon 3a016c
            'is set to True\n', output)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        cmd = [
Pierre-Yves Chibon 3a016c
            'python', PAGURE_ADMIN, 'read-only',
Pierre-Yves Chibon 3a016c
            'somenamespace/test', '--ro', 'false']
Pierre-Yves Chibon 3a016c
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 3a016c
            'The read-only flag of the project somenamespace/test has been '
Pierre-Yves Chibon 3a016c
            'set to False\n', output)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        # After
Pierre-Yves Chibon 3a016c
        cmd = ['python', PAGURE_ADMIN, 'read-only', 'somenamespace/test']
Pierre-Yves Chibon 3a016c
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 3a016c
            'The current read-only flag of the project somenamespace/test '\
Pierre-Yves Chibon 3a016c
            'is set to False\n', output)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
    def test_read_only_no_change(self):
Pierre-Yves Chibon 3a016c
        """ Test the read-only function of pagure-admin to set the status of
Pierre-Yves Chibon 3a016c
        a namespaced project.
Pierre-Yves Chibon 3a016c
        """
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        # Before
Pierre-Yves Chibon 3a016c
        cmd = ['python', PAGURE_ADMIN, 'read-only', 'test']
Pierre-Yves Chibon 3a016c
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 3a016c
            'The current read-only flag of the project test '\
Pierre-Yves Chibon 3a016c
            'is set to True\n', output)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        cmd = [
Pierre-Yves Chibon 3a016c
            'python', PAGURE_ADMIN, 'read-only', 'test', '--ro', 'true']
Pierre-Yves Chibon 3a016c
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 3a016c
            'The read-only flag of the project test has been '
Pierre-Yves Chibon 3a016c
            'set to True\n', output)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        # After
Pierre-Yves Chibon 3a016c
        cmd = ['python', PAGURE_ADMIN, 'read-only', 'test']
Pierre-Yves Chibon 3a016c
        output = _get_ouput(cmd)[0]
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 3a016c
            'The current read-only flag of the project test '\
Pierre-Yves Chibon 3a016c
            'is set to True\n', output)
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon d4c9da
if __name__ == '__main__':
Pierre-Yves Chibon d4c9da
    unittest.main(verbosity=2)