Blame tests/test_pagure_admin.py

Pierre-Yves Chibon d4c9da
# -*- coding: utf-8 -*-
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
"""
Pierre-Yves Chibon ffbe63
 (c) 2017-2018 - 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 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon 87a6d0
import datetime  # noqa
Pierre-Yves Chibon 600fa7
import os  # noqa
Pierre-Yves Chibon 600fa7
import platform  # 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 600fa7
import unittest  # noqa
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon a10996
import pygit2
Pierre-Yves Chibon d4c9da
import munch  # noqa
Pierre-Yves Chibon d4c9da
from mock import patch, MagicMock  # noqa
Fabian Arrotin 0096c9
from six import StringIO  # noqa
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon b130e5
import pagure.config  # noqa
Aurélien Bompard a29742
import pagure.exceptions  # noqa: E402
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
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
    """
Aurélien Bompard e78d79
Aurélien Bompard e78d79
    populate_db = False
Aurélien Bompard e78d79
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()
Aurélien Bompard e78d79
        pagure.cli.admin.session = self.session
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 73d120
        args = munch.Munch({"user": "pingou"})
Aurélien Bompard a29742
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Aurélien Bompard a29742
            pagure.cli.admin.do_create_admin_token(args)
Pierre-Yves Chibon 73d120
        self.assertEqual(cm.exception.args[0], 'No user "pingou" found')
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 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": False,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 73d120
        self.assertEqual(output, "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
Aurélien Bompard e78d79
    populate_db = False
Aurélien Bompard e78d79
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()
Aurélien Bompard e78d79
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon ede948
Pierre-Yves Chibon ede948
        # Create the user pingou
Pierre-Yves Chibon ede948
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            fullname="PY C",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@pingou.com",
Pierre-Yves Chibon ede948
        )
Pierre-Yves Chibon ede948
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=1, 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 930073
        msg = pagure.lib.query.add_group(
Pierre-Yves Chibon ede948
            self.session,
Pierre-Yves Chibon 73d120
            group_name="foo",
Pierre-Yves Chibon 73d120
            display_name="foo group",
Pierre-Yves Chibon ede948
            description=None,
Pierre-Yves Chibon 73d120
            group_type="bar",
Pierre-Yves Chibon 73d120
            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 73d120
        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 b130e5
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon ede948
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._ask_confirmation")
Pierre-Yves Chibon 73d120
    @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 73d120
            {"group": None, "project": None, "all_": False, "user": None}
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ede948
        pagure.cli.admin.do_generate_acl(args)
Pierre-Yves Chibon ede948
Patrick Uiterwijk ad0b72
        get_helper.assert_called_with()
Pierre-Yves Chibon ede948
        args = helper.generate_acls.call_args
Pierre-Yves Chibon 73d120
        self.assertIsNone(args[1].get("group"))
Pierre-Yves Chibon 73d120
        self.assertIsNone(args[1].get("project"))
Pierre-Yves Chibon ede948
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._ask_confirmation")
Pierre-Yves Chibon 73d120
    @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 73d120
            {"group": None, "project": None, "all_": True, "user": None}
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ede948
        pagure.cli.admin.do_generate_acl(args)
Pierre-Yves Chibon ede948
Patrick Uiterwijk ad0b72
        get_helper.assert_called_with()
Pierre-Yves Chibon ede948
        args = helper.generate_acls.call_args
Pierre-Yves Chibon 73d120
        self.assertIsNone(args[1].get("group"))
Pierre-Yves Chibon 73d120
        self.assertEqual(args[1].get("project"), -1)
Pierre-Yves Chibon ede948
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._ask_confirmation")
Pierre-Yves Chibon 73d120
    @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 73d120
            {"group": None, "project": "test", "all_": False, "user": None}
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ede948
        pagure.cli.admin.do_generate_acl(args)
Pierre-Yves Chibon ede948
Patrick Uiterwijk ad0b72
        get_helper.assert_called_with()
Pierre-Yves Chibon ede948
        args = helper.generate_acls.call_args
Pierre-Yves Chibon 73d120
        self.assertIsNone(args[1].get("group"))
Pierre-Yves Chibon 73d120
        self.assertEqual(args[1].get("project").fullname, "test")
Pierre-Yves Chibon ede948
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._ask_confirmation")
Pierre-Yves Chibon 73d120
    @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 73d120
            {"group": None, "project": "test", "all_": True, "user": None}
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ede948
        pagure.cli.admin.do_generate_acl(args)
Pierre-Yves Chibon ede948
Patrick Uiterwijk ad0b72
        get_helper.assert_called_with()
Pierre-Yves Chibon ede948
        args = helper.generate_acls.call_args
Pierre-Yves Chibon 73d120
        self.assertIsNone(args[1].get("group"))
Pierre-Yves Chibon 73d120
        self.assertEqual(args[1].get("project"), -1)
Pierre-Yves Chibon ede948
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._ask_confirmation")
Pierre-Yves Chibon 73d120
    @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 73d120
            {"group": "foo", "project": None, "all_": False, "user": None}
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ede948
        pagure.cli.admin.do_generate_acl(args)
Pierre-Yves Chibon ede948
Patrick Uiterwijk ad0b72
        get_helper.assert_called_with()
Pierre-Yves Chibon ede948
        args = helper.generate_acls.call_args
Pierre-Yves Chibon 73d120
        self.assertEqual(args[1].get("group").group_name, "foo")
Pierre-Yves Chibon 73d120
        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
Aurélien Bompard e78d79
    populate_db = False
Aurélien Bompard e78d79
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()
Aurélien Bompard e78d79
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        # Create the user pingou
Pierre-Yves Chibon d4c9da
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            fullname="PY C",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@pingou.com",
Pierre-Yves Chibon d4c9da
        )
Pierre-Yves Chibon d4c9da
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=1, 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 b130e5
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._get_input")
Pierre-Yves Chibon 73d120
    @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 73d120
        rinp.return_value = "1,2,3"
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon 73d120
        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 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": False,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon d4c9da
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 73d120
        self.assertEqual(len(output.split("\n")), 2)
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._get_input")
Pierre-Yves Chibon 73d120
    @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 73d120
        rinp.return_value = "1,2,3"
Pierre-Yves Chibon 9a36a4
Pierre-Yves Chibon 73d120
        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 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": False,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 9a36a4
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 73d120
        self.assertEqual(len(output.split("\n")), 2)
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output)
Pierre-Yves Chibon 9a36a4
Pierre-Yves Chibon 9a36a4
        # Retrieve pfrields's tokens
Pierre-Yves Chibon 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": "pfrields",
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": False,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 73d120
        self.assertEqual(output, "No admin tokens found\n")
Pierre-Yves Chibon 9a36a4
Pierre-Yves Chibon f3c6b4
    def test_do_list_admin_token_non_admin_acls(self):
Pierre-Yves Chibon f3c6b4
        """ Test the do_list_admin_token function of pagure-admin for a token
Pierre-Yves Chibon f3c6b4
        without any admin ACL. """
Pierre-Yves Chibon 930073
        pagure.lib.query.add_token_to_user(
Pierre-Yves Chibon f3c6b4
            self.session,
Pierre-Yves Chibon f3c6b4
            project=None,
Pierre-Yves Chibon 73d120
            acls=["issue_assign", "pull_request_subscribe"],
Pierre-Yves Chibon 73d120
            username="pingou",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon f3c6b4
Pierre-Yves Chibon f3c6b4
        # Retrieve all admin tokens
Pierre-Yves Chibon 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": False,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon f3c6b4
        with tests.capture_output() as output:
Pierre-Yves Chibon f3c6b4
            pagure.cli.admin.do_list_admin_token(list_args)
Pierre-Yves Chibon f3c6b4
        output = output.getvalue()
Pierre-Yves Chibon 73d120
        self.assertEqual(output, "No admin tokens found\n")
Pierre-Yves Chibon f3c6b4
Pierre-Yves Chibon f3c6b4
        # Retrieve all tokens
Pierre-Yves Chibon 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": False,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": True,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon f3c6b4
        with tests.capture_output() as output:
Pierre-Yves Chibon f3c6b4
            pagure.cli.admin.do_list_admin_token(list_args)
Pierre-Yves Chibon f3c6b4
        output = output.getvalue()
Pierre-Yves Chibon f3c6b4
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 73d120
        self.assertEqual(len(output.split("\n")), 2)
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output)
Pierre-Yves Chibon f3c6b4
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._get_input")
Pierre-Yves Chibon 73d120
    @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 73d120
        rinp.return_value = "2,4,5"
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon 73d120
        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 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": False,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon d4c9da
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 73d120
        self.assertEqual(len(output.split("\n")), 2)
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon 73d120
        token = output.split(" ", 1)[0]
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon 73d120
        args = munch.Munch({"token": token})
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_info_admin_token(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output.split("\n", 1)[0])
Pierre-Yves Chibon d4c9da
        self.assertEqual(
Pierre-Yves Chibon 73d120
            output.split("\n", 1)[1],
Pierre-Yves Chibon 73d120
            """ACLs:
Pierre-Yves Chibon d4c9da
  - issue_create
Pierre-Yves Chibon d4c9da
  - pull_request_comment
Pierre-Yves Chibon d4c9da
  - pull_request_flag
Pierre-Yves Chibon 73d120
""",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon f3c6b4
    def test_do_info_admin_token_non_admin_acl(self):
Pierre-Yves Chibon f3c6b4
        """ Test the do_info_admin_token function of pagure-admin for a
Pierre-Yves Chibon f3c6b4
        token not having any admin ACL. """
Pierre-Yves Chibon 930073
        pagure.lib.query.add_token_to_user(
Pierre-Yves Chibon f3c6b4
            self.session,
Pierre-Yves Chibon f3c6b4
            project=None,
Pierre-Yves Chibon 73d120
            acls=["issue_assign", "pull_request_subscribe"],
Pierre-Yves Chibon 73d120
            username="pingou",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon f3c6b4
Pierre-Yves Chibon f3c6b4
        # Retrieve the token
Pierre-Yves Chibon 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": False,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": True,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon f3c6b4
        with tests.capture_output() as output:
Pierre-Yves Chibon f3c6b4
            pagure.cli.admin.do_list_admin_token(list_args)
Pierre-Yves Chibon f3c6b4
        output = output.getvalue()
Pierre-Yves Chibon f3c6b4
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 73d120
        self.assertEqual(len(output.split("\n")), 2)
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output)
Pierre-Yves Chibon f3c6b4
Pierre-Yves Chibon 73d120
        token = output.split(" ", 1)[0]
Pierre-Yves Chibon f3c6b4
Pierre-Yves Chibon 73d120
        args = munch.Munch({"token": token})
Pierre-Yves Chibon f3c6b4
        with tests.capture_output() as output:
Pierre-Yves Chibon f3c6b4
            pagure.cli.admin.do_info_admin_token(args)
Pierre-Yves Chibon f3c6b4
        output = output.getvalue()
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output.split("\n", 1)[0])
Pierre-Yves Chibon f3c6b4
        self.assertEqual(
Pierre-Yves Chibon 73d120
            output.split("\n", 1)[1],
Pierre-Yves Chibon 73d120
            """ACLs:
Pierre-Yves Chibon f3c6b4
  - issue_assign
Pierre-Yves Chibon f3c6b4
  - pull_request_subscribe
Pierre-Yves Chibon 73d120
""",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon f3c6b4
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._get_input")
Pierre-Yves Chibon 73d120
    @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 73d120
        if "BUILD_ID" in os.environ:
Pierre-Yves Chibon 73d120
            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 73d120
        rinp.return_value = "1,2,3"
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon 73d120
        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 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": False,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon d4c9da
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 73d120
        self.assertEqual(len(output.split("\n")), 2)
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon 73d120
        token = output.split(" ", 1)[0]
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        # Before
Pierre-Yves Chibon 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": True,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 73d120
        self.assertNotEqual(output, "No admin tokens found\n")
Pierre-Yves Chibon 73d120
        self.assertEqual(len(output.split("\n")), 2)
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output)
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon d4c9da
        # Expire the token
Pierre-Yves Chibon 73d120
        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 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": True,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 73d120
        self.assertEqual(output, "No admin tokens found\n")
Pierre-Yves Chibon d4c9da
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._get_input")
Pierre-Yves Chibon 73d120
    @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 73d120
        if "BUILD_ID" in os.environ:
Pierre-Yves Chibon 73d120
            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 73d120
        rinp.return_value = "1,2,3"
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 73d120
        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 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": False,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 87a6d0
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 73d120
        self.assertEqual(len(output.split("\n")), 2)
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 73d120
        token = output.split(" ", 1)[0]
Pierre-Yves Chibon 73d120
        current_expiration = output.split(" ", 1)[1]
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Set the expiration date to the token
Pierre-Yves Chibon 73d120
        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 73d120
            args,
Pierre-Yves Chibon 87a6d0
        )
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._get_input")
Pierre-Yves Chibon 73d120
    @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 73d120
        if "BUILD_ID" in os.environ:
Pierre-Yves Chibon 73d120
            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 73d120
        rinp.return_value = "1,2,3"
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 73d120
        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 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": False,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 87a6d0
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 73d120
        self.assertEqual(len(output.split("\n")), 2)
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 73d120
        token = output.split(" ", 1)[0]
Pierre-Yves Chibon 73d120
        current_expiration = output.split(" ", 1)[1]
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Set the expiration date to the token
Pierre-Yves Chibon 73d120
        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 73d120
            args,
Pierre-Yves Chibon 87a6d0
        )
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._get_input")
Pierre-Yves Chibon 73d120
    @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 73d120
        if "BUILD_ID" in os.environ:
Pierre-Yves Chibon 73d120
            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 73d120
        rinp.return_value = "1,2,3"
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 73d120
        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 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": False,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 87a6d0
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 73d120
        self.assertEqual(len(output.split("\n")), 2)
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 73d120
        token = output.split(" ", 1)[0]
Pierre-Yves Chibon 73d120
        current_expiration = output.split(" ", 1)[1]
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Set the expiration date to the token
Pierre-Yves Chibon 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {"token": token, "date": datetime.datetime.utcnow().date()}
Pierre-Yves Chibon 73d120
        )
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 73d120
            args,
Pierre-Yves Chibon 87a6d0
        )
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._get_input")
Pierre-Yves Chibon 73d120
    @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 73d120
        if "BUILD_ID" in os.environ:
Pierre-Yves Chibon 73d120
            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 73d120
        rinp.return_value = "1,2,3"
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 73d120
        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 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": False,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 87a6d0
        self.assertNotEqual(output, 'No user "pingou" found\n')
Pierre-Yves Chibon 73d120
        self.assertEqual(len(output.split("\n")), 2)
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 73d120
        token = output.split(" ", 1)[0]
Pierre-Yves Chibon 73d120
        current_expiration = output.strip().split(" -- ", 2)[-1]
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Before
Pierre-Yves Chibon 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": True,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 73d120
        self.assertNotEqual(output, "No admin tokens found\n")
Pierre-Yves Chibon 73d120
        self.assertEqual(len(output.split("\n")), 2)
Pierre-Yves Chibon 73d120
        self.assertIn(" -- pingou -- ", output)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 73d120
        deadline = datetime.datetime.utcnow().date() + datetime.timedelta(
Pierre-Yves Chibon 73d120
            days=3
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # Set the expiration date to the token
Pierre-Yves Chibon 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {"token": token, "date": deadline.strftime("%Y-%m-%d")}
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 87a6d0
        pagure.cli.admin.do_update_admin_token(args)
Pierre-Yves Chibon 87a6d0
Pierre-Yves Chibon 87a6d0
        # After
Pierre-Yves Chibon 73d120
        list_args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "user": None,
Pierre-Yves Chibon 73d120
                "token": None,
Pierre-Yves Chibon 73d120
                "active": True,
Pierre-Yves Chibon 73d120
                "expired": False,
Pierre-Yves Chibon 73d120
                "all": False,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_list_admin_token(list_args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 73d120
        self.assertEqual(output.split(" ", 1)[0], token)
Pierre-Yves Chibon 87a6d0
        self.assertNotEqual(
Pierre-Yves Chibon 73d120
            output.strip().split(" -- ", 2)[-1], current_expiration
Pierre-Yves Chibon 73d120
        )
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
Aurélien Bompard e78d79
    populate_db = False
Aurélien Bompard e78d79
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()
Aurélien Bompard e78d79
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        # Create the user pingou
Pierre-Yves Chibon 25e586
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            fullname="PY C",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@pingou.com",
Pierre-Yves Chibon 25e586
        )
Pierre-Yves Chibon 25e586
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=1, 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 73d120
            user="foo",
Pierre-Yves Chibon 73d120
            fullname="foo B.",
Pierre-Yves Chibon 73d120
            password="foob",
Pierre-Yves Chibon 73d120
            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 73d120
            name="test",
Pierre-Yves Chibon 73d120
            description="namespaced test project",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeee",
Pierre-Yves Chibon 73d120
            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 73d120
            name="test",
Pierre-Yves Chibon 73d120
            description="Test project",
Pierre-Yves Chibon 73d120
            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 b130e5
        pagure.cli.admin.session = self.session
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 73d120
        args = munch.Munch({"project": "foobar", "user": "pingou"})
Aurélien Bompard a29742
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Aurélien Bompard a29742
            pagure.cli.admin.do_get_watch_status(args)
Pierre-Yves Chibon 73d120
        self.assertEqual(cm.exception.args[0], "No project found with: foobar")
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 73d120
        args = munch.Munch({"project": "fo/o/bar", "user": "pingou"})
Aurélien Bompard a29742
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Aurélien Bompard a29742
            pagure.cli.admin.do_get_watch_status(args)
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon ffbe63
            cm.exception.args[0],
Aurélien Bompard a29742
            'Invalid project name, has more than one "/": fo/o/bar',
Aurélien Bompard a29742
        )
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 73d120
        args = munch.Munch({"project": "test", "user": "beebop"})
Aurélien Bompard a29742
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Aurélien Bompard a29742
            pagure.cli.admin.do_get_watch_status(args)
Pierre-Yves Chibon 73d120
        self.assertEqual(cm.exception.args[0], 'No user "beebop" found')
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 73d120
        args = munch.Munch({"project": "test", "user": "pingou"})
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_get_watch_status(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "On test user: pingou is watching the following items: "
Pierre-Yves Chibon 73d120
            "issues, pull-requests\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
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 73d120
        args = munch.Munch({"project": "test", "user": "foo"})
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_get_watch_status(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "On test user: foo is watching the following items: None\n", output
Pierre-Yves Chibon 73d120
        )
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 73d120
        args = munch.Munch({"project": "somenamespace/test", "user": "pingou"})
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_get_watch_status(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "On somenamespace/test user: pingou is watching the following "
Pierre-Yves Chibon 73d120
            "items: issues, pull-requests\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
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 73d120
        args = munch.Munch({"project": "somenamespace/test", "user": "foo"})
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_get_watch_status(args)
Aurélien Bompard a29742
        output = output.getvalue()
Aurélien Bompard a29742
        with tests.capture_output() as _discarded:
Aurélien Bompard a29742
            pagure.cli.admin.do_get_watch_status(args)
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "On somenamespace/test user: foo is watching the following "
Pierre-Yves Chibon 73d120
            "items: None\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
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
Aurélien Bompard e78d79
    populate_db = False
Aurélien Bompard e78d79
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()
Aurélien Bompard e78d79
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 25e586
        # Create the user pingou
Pierre-Yves Chibon 25e586
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            fullname="PY C",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@pingou.com",
Pierre-Yves Chibon 25e586
        )
Pierre-Yves Chibon 25e586
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=1, 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 73d120
            user="foo",
Pierre-Yves Chibon 73d120
            fullname="foo B.",
Pierre-Yves Chibon 73d120
            password="foob",
Pierre-Yves Chibon 73d120
            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 73d120
            name="test",
Pierre-Yves Chibon 73d120
            description="namespaced test project",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeee",
Pierre-Yves Chibon 73d120
            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 73d120
            name="test",
Pierre-Yves Chibon 73d120
            description="Test project",
Pierre-Yves Chibon 73d120
            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 b130e5
        pagure.cli.admin.session = self.session
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 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {"project": "foob", "user": "pingou", "status": "1"}
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Aurélien Bompard a29742
            pagure.cli.admin.do_update_watch_status(args)
Pierre-Yves Chibon 73d120
        self.assertEqual(cm.exception.args[0], "No project found with: foob")
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 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {"project": "fo/o/b", "user": "pingou", "status": "1"}
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Aurélien Bompard a29742
            pagure.cli.admin.do_update_watch_status(args)
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon ffbe63
            cm.exception.args[0],
Aurélien Bompard a29742
            'Invalid project name, has more than one "/": fo/o/b',
Aurélien Bompard a29742
        )
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 73d120
        args = munch.Munch({"project": "test", "user": "foob", "status": "1"})
Aurélien Bompard a29742
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Aurélien Bompard a29742
            pagure.cli.admin.do_update_watch_status(args)
Pierre-Yves Chibon 73d120
        self.assertEqual(cm.exception.args[0], 'No user "foob" found')
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 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {"project": "test", "user": "pingou", "status": "10"}
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Aurélien Bompard a29742
            pagure.cli.admin.do_update_watch_status(args)
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon ffbe63
            cm.exception.args[0],
Pierre-Yves Chibon 73d120
            "Invalid status provided: 10 not in -1, 0, 1, 2, 3",
Aurélien Bompard a29742
        )
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 73d120
        args = munch.Munch({"project": "test", "user": "pingou"})
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_get_watch_status(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "On test user: pingou is watching the following items: "
Pierre-Yves Chibon 73d120
            "issues, pull-requests\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {"project": "test", "user": "pingou", "status": "1"}
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_update_watch_status(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "Updating watch status of pingou to 1 (watch issues and PRs) "
Pierre-Yves Chibon 73d120
            "on test\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 25e586
Pierre-Yves Chibon 73d120
        args = munch.Munch({"project": "test", "user": "pingou"})
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_get_watch_status(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 25e586
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "On test user: pingou is watching the following items: "
Pierre-Yves Chibon 73d120
            "issues, pull-requests\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
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
Aurélien Bompard e78d79
    populate_db = False
Aurélien Bompard e78d79
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()
Aurélien Bompard e78d79
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        # Create the user pingou
Pierre-Yves Chibon 3a016c
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            fullname="PY C",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@pingou.com",
Pierre-Yves Chibon 3a016c
        )
Pierre-Yves Chibon 3a016c
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=1, 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 73d120
            name="test",
Pierre-Yves Chibon 73d120
            description="namespaced test project",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeee",
Pierre-Yves Chibon 73d120
            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 73d120
            name="test",
Pierre-Yves Chibon 73d120
            description="Test project",
Pierre-Yves Chibon 73d120
            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 b130e5
        pagure.cli.admin.session = self.session
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 73d120
        args = munch.Munch({"project": "foob", "user": None, "ro": None})
Aurélien Bompard a29742
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Aurélien Bompard a29742
            pagure.cli.admin.do_read_only(args)
Pierre-Yves Chibon 73d120
        self.assertEqual(cm.exception.args[0], "No project found with: foob")
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 73d120
        args = munch.Munch({"project": "fo/o/b", "user": None, "ro": None})
Aurélien Bompard a29742
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Aurélien Bompard a29742
            pagure.cli.admin.do_read_only(args)
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon ffbe63
            cm.exception.args[0],
Pierre-Yves Chibon 73d120
            'Invalid project name, has more than one "/": fo/o/b',
Aurélien Bompard a29742
        )
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 73d120
        args = munch.Munch({"project": "test", "user": None, "ro": None})
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_read_only(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "The current read-only flag of the project test is set to True\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
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 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {"project": "somenamespace/test", "user": None, "ro": None}
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_read_only(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "The current read-only flag of the project somenamespace/test "
Pierre-Yves Chibon 73d120
            "is set to True\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
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 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {"project": "somenamespace/test", "user": None, "ro": None}
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_read_only(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "The current read-only flag of the project somenamespace/test "
Pierre-Yves Chibon 73d120
            "is set to True\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {"project": "somenamespace/test", "user": None, "ro": "false"}
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_read_only(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "The read-only flag of the project somenamespace/test has been "
Pierre-Yves Chibon 73d120
            "set to False\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        # After
Pierre-Yves Chibon 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {"project": "somenamespace/test", "user": None, "ro": None}
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_read_only(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "The current read-only flag of the project somenamespace/test "
Pierre-Yves Chibon 73d120
            "is set to False\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
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 73d120
        args = munch.Munch({"project": "test", "user": None, "ro": None})
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_read_only(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "The current read-only flag of the project test "
Pierre-Yves Chibon 73d120
            "is set to True\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
        args = munch.Munch({"project": "test", "user": None, "ro": "true"})
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_read_only(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "The read-only flag of the project test has been " "set to True\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
        # After
Pierre-Yves Chibon 73d120
        args = munch.Munch({"project": "test", "user": None, "ro": None})
Aurélien Bompard a29742
        with tests.capture_output() as output:
Aurélien Bompard a29742
            pagure.cli.admin.do_read_only(args)
Aurélien Bompard a29742
        output = output.getvalue()
Pierre-Yves Chibon 3a016c
        self.assertEqual(
Pierre-Yves Chibon 73d120
            "The current read-only flag of the project test "
Pierre-Yves Chibon 73d120
            "is set to True\n",
Pierre-Yves Chibon 73d120
            output,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon 3a016c
Pierre-Yves Chibon ffbe63
class PagureNewGroupTests(tests.Modeltests):
Pierre-Yves Chibon ffbe63
    """ Tests for pagure-admin new-group """
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
    populate_db = False
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
    def setUp(self):
Pierre-Yves Chibon ffbe63
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon ffbe63
        super(PagureNewGroupTests, self).setUp()
Pierre-Yves Chibon ffbe63
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
        # Create the user pingou
Pierre-Yves Chibon ffbe63
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            fullname="PY C",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@pingou.com",
Pierre-Yves Chibon ffbe63
        )
Pierre-Yves Chibon ffbe63
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
Pierre-Yves Chibon ffbe63
        self.session.add(item)
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
        self.session.commit()
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
        # Make the imported pagure use the correct db session
Pierre-Yves Chibon ffbe63
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 930073
        groups = pagure.lib.query.search_groups(self.session)
Pierre-Yves Chibon ffbe63
        self.assertEqual(len(groups), 0)
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
    def test_missing_display_name(self):
Pierre-Yves Chibon ffbe63
        """ Test the new-group function of pagure-admin when the display name
Pierre-Yves Chibon ffbe63
        is missing from the args.
Pierre-Yves Chibon ffbe63
        """
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "group_name": "foob",
Pierre-Yves Chibon 73d120
                "display": None,
Pierre-Yves Chibon 73d120
                "description": None,
Pierre-Yves Chibon 73d120
                "username": "pingou",
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ffbe63
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon ffbe63
            pagure.cli.admin.do_new_group(args)
Pierre-Yves Chibon ffbe63
        self.assertEqual(
Pierre-Yves Chibon ffbe63
            cm.exception.args[0],
Pierre-Yves Chibon 73d120
            "A display name must be provided for the group",
Pierre-Yves Chibon ffbe63
        )
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 930073
        groups = pagure.lib.query.search_groups(self.session)
Pierre-Yves Chibon ffbe63
        self.assertEqual(len(groups), 0)
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
    def test_missing_username(self):
Pierre-Yves Chibon ffbe63
        """ Test the new-group function of pagure-admin when the username
Pierre-Yves Chibon ffbe63
        is missing from the args.
Pierre-Yves Chibon ffbe63
        """
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "group_name": "foob",
Pierre-Yves Chibon 73d120
                "display": "foo group",
Pierre-Yves Chibon 73d120
                "description": None,
Pierre-Yves Chibon 73d120
                "username": None,
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon ffbe63
            pagure.cli.admin.do_new_group(args)
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
        self.assertEqual(
Pierre-Yves Chibon ffbe63
            cm.exception.args[0],
Pierre-Yves Chibon 73d120
            "An username must be provided to associate with the group",
Pierre-Yves Chibon ffbe63
        )
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 930073
        groups = pagure.lib.query.search_groups(self.session)
Pierre-Yves Chibon ffbe63
        self.assertEqual(len(groups), 0)
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
    def test_new_group(self):
Pierre-Yves Chibon ffbe63
        """ Test the new-group function of pagure-admin when all arguments
Pierre-Yves Chibon ffbe63
        are provided.
Pierre-Yves Chibon ffbe63
        """
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "group_name": "foob",
Pierre-Yves Chibon 73d120
                "display": "foo group",
Pierre-Yves Chibon 73d120
                "description": None,
Pierre-Yves Chibon 73d120
                "username": "pingou",
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
        pagure.cli.admin.do_new_group(args)
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 930073
        groups = pagure.lib.query.search_groups(self.session)
Pierre-Yves Chibon ffbe63
        self.assertEqual(len(groups), 1)
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 73d120
    @patch.dict("pagure.config.config", {"ENABLE_GROUP_MNGT": False})
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._ask_confirmation")
Pierre-Yves Chibon ffbe63
    def test_new_group_grp_mngt_off_no(self, conf):
Pierre-Yves Chibon ffbe63
        """ Test the new-group function of pagure-admin when all arguments
Pierre-Yves Chibon ffbe63
        are provided and ENABLE_GROUP_MNGT if off in the config and the user
Pierre-Yves Chibon ffbe63
        replies no to the question.
Pierre-Yves Chibon ffbe63
        """
Pierre-Yves Chibon ffbe63
        conf.return_value = False
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "group_name": "foob",
Pierre-Yves Chibon 73d120
                "display": "foo group",
Pierre-Yves Chibon 73d120
                "description": None,
Pierre-Yves Chibon 73d120
                "username": "pingou",
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
        pagure.cli.admin.do_new_group(args)
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 930073
        groups = pagure.lib.query.search_groups(self.session)
Pierre-Yves Chibon ffbe63
        self.assertEqual(len(groups), 0)
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 73d120
    @patch.dict("pagure.config.config", {"ENABLE_GROUP_MNGT": False})
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._ask_confirmation")
Pierre-Yves Chibon ffbe63
    def test_new_group_grp_mngt_off_yes(self, conf):
Pierre-Yves Chibon ffbe63
        """ Test the new-group function of pagure-admin when all arguments
Pierre-Yves Chibon ffbe63
        are provided and ENABLE_GROUP_MNGT if off in the config and the user
Pierre-Yves Chibon ffbe63
        replies yes to the question.
Pierre-Yves Chibon ffbe63
        """
Pierre-Yves Chibon ffbe63
        conf.return_value = True
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "group_name": "foob",
Pierre-Yves Chibon 73d120
                "display": "foo group",
Pierre-Yves Chibon 73d120
                "description": None,
Pierre-Yves Chibon 73d120
                "username": "pingou",
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
        pagure.cli.admin.do_new_group(args)
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 930073
        groups = pagure.lib.query.search_groups(self.session)
Pierre-Yves Chibon ffbe63
        self.assertEqual(len(groups), 1)
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 73d120
    @patch.dict("pagure.config.config", {"BLACKLISTED_GROUPS": ["foob"]})
Pierre-Yves Chibon ffbe63
    def test_new_group_grp_mngt_off_yes(self):
Pierre-Yves Chibon ffbe63
        """ Test the new-group function of pagure-admin when all arguments
Pierre-Yves Chibon ffbe63
        are provided but the group is black listed.
Pierre-Yves Chibon ffbe63
        """
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 73d120
        args = munch.Munch(
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "group_name": "foob",
Pierre-Yves Chibon 73d120
                "display": "foo group",
Pierre-Yves Chibon 73d120
                "description": None,
Pierre-Yves Chibon 73d120
                "username": "pingou",
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon ffbe63
            pagure.cli.admin.do_new_group(args)
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon ffbe63
        self.assertEqual(
Pierre-Yves Chibon ffbe63
            cm.exception.args[0],
Pierre-Yves Chibon 73d120
            "This group name has been blacklisted, please choose another one",
Pierre-Yves Chibon ffbe63
        )
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon 930073
        groups = pagure.lib.query.search_groups(self.session)
Pierre-Yves Chibon ffbe63
        self.assertEqual(len(groups), 0)
Pierre-Yves Chibon ffbe63
Pierre-Yves Chibon bee78a
Fabian Arrotin 0096c9
class PagureListGroupEmptyTests(tests.Modeltests):
Fabian Arrotin 0096c9
    """ Tests for pagure-admin list-groups """
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
    populate_db = False
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
    def setUp(self):
Fabian Arrotin 0096c9
        """ Set up the environnment, ran before every tests. """
Fabian Arrotin 0096c9
        super(PagureListGroupEmptyTests, self).setUp()
Fabian Arrotin 0096c9
        pagure.cli.admin.session = self.session
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
        # Create the user pingou
Fabian Arrotin 0096c9
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            fullname="PY C",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@pingou.com",
Fabian Arrotin 0096c9
        )
Fabian Arrotin 0096c9
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
Fabian Arrotin 0096c9
        self.session.add(item)
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
        self.session.commit()
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
        # Make the imported pagure use the correct db session
Fabian Arrotin 0096c9
        pagure.cli.admin.session = self.session
Fabian Arrotin 0096c9
Pierre-Yves Chibon 930073
        groups = pagure.lib.query.search_groups(self.session)
Fabian Arrotin 0096c9
        self.assertEqual(len(groups), 0)
Fabian Arrotin 0096c9
Pierre-Yves Chibon 73d120
    @patch("sys.stdout", new_callable=StringIO)
Fabian Arrotin 0096c9
    def test_no_groups(self, mock_stdout):
Fabian Arrotin 0096c9
        """ Test the list-groups function of pagure-admin when there are no
Fabian Arrotin 0096c9
        groups in the database
Fabian Arrotin 0096c9
        """
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
        args = munch.Munch()
Fabian Arrotin 0096c9
        pagure.cli.admin.do_list_groups(args)
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
        self.assertEqual(
Fabian Arrotin 0096c9
            mock_stdout.getvalue(),
Pierre-Yves Chibon 73d120
            "No groups found in this pagure instance.\n",
Fabian Arrotin 0096c9
        )
Fabian Arrotin 0096c9
Pierre-Yves Chibon 930073
        groups = pagure.lib.query.search_groups(self.session)
Fabian Arrotin 0096c9
        self.assertEqual(len(groups), 0)
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
class PagureListGroupTests(tests.Modeltests):
Fabian Arrotin 0096c9
    """ Tests for pagure-admin list-groups """
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
    populate_db = False
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
    def setUp(self):
Fabian Arrotin 0096c9
        """ Set up the environnment, ran before every tests. """
Fabian Arrotin 0096c9
        super(PagureListGroupTests, self).setUp()
Fabian Arrotin 0096c9
        pagure.cli.admin.session = self.session
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
        # Create the user pingou
Fabian Arrotin 0096c9
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            fullname="PY C",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@pingou.com",
Fabian Arrotin 0096c9
        )
Fabian Arrotin 0096c9
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
Fabian Arrotin 0096c9
        self.session.add(item)
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
        # Create a group
Pierre-Yves Chibon 930073
        pagure.lib.query.add_group(
Fabian Arrotin 0096c9
            self.session,
Pierre-Yves Chibon 73d120
            group_name="JL",
Pierre-Yves Chibon 73d120
            display_name="Justice League",
Pierre-Yves Chibon 73d120
            description="Nope, it's not JLA anymore",
Pierre-Yves Chibon 73d120
            group_type="user",
Pierre-Yves Chibon 73d120
            user="pingou",
Fabian Arrotin 0096c9
            is_admin=False,
Pierre-Yves Chibon 73d120
            blacklist=[],
Fabian Arrotin 0096c9
        )
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
        self.session.commit()
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
        # Make the imported pagure use the correct db session
Fabian Arrotin 0096c9
        pagure.cli.admin.session = self.session
Fabian Arrotin 0096c9
Pierre-Yves Chibon 930073
        groups = pagure.lib.query.search_groups(self.session)
Fabian Arrotin 0096c9
        self.assertEqual(len(groups), 1)
Fabian Arrotin 0096c9
Pierre-Yves Chibon 73d120
    @patch("sys.stdout", new_callable=StringIO)
Fabian Arrotin 0096c9
    def test_list_groups(self, mock_stdout):
Fabian Arrotin 0096c9
        """ Test the list-groups function of pagure-admin when there is one
Fabian Arrotin 0096c9
        group in the database
Fabian Arrotin 0096c9
        """
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
        args = munch.Munch()
Fabian Arrotin 0096c9
        pagure.cli.admin.do_list_groups(args)
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
        self.assertEqual(
Fabian Arrotin 0096c9
            mock_stdout.getvalue(),
Pierre-Yves Chibon 73d120
            "List of groups on this Pagure instance:\n" "Group: 1 - name JL\n",
Fabian Arrotin 0096c9
        )
Fabian Arrotin 0096c9
Pierre-Yves Chibon 930073
        groups = pagure.lib.query.search_groups(self.session)
Fabian Arrotin 0096c9
        self.assertEqual(len(groups), 1)
Fabian Arrotin 0096c9
Fabian Arrotin 0096c9
Pierre-Yves Chibon bee78a
class PagureBlockUserTests(tests.Modeltests):
Pierre-Yves Chibon bee78a
    """ Tests for pagure-admin block-user """
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
    populate_db = False
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
    def setUp(self):
Pierre-Yves Chibon bee78a
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon bee78a
        super(PagureBlockUserTests, self).setUp()
Pierre-Yves Chibon bee78a
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
        # Create the user pingou
Pierre-Yves Chibon bee78a
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            fullname="PY C",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@pingou.com",
Pierre-Yves Chibon bee78a
        )
Pierre-Yves Chibon bee78a
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
Pierre-Yves Chibon bee78a
        self.session.add(item)
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
        self.session.commit()
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
        # Make the imported pagure use the correct db session
Pierre-Yves Chibon bee78a
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 73d120
        user = pagure.lib.query.get_user(self.session, "pingou")
Pierre-Yves Chibon bee78a
        self.assertIsNone(user.refuse_sessions_before)
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
    def test_missing_date(self):
Pierre-Yves Chibon bee78a
        """ Test the block-user function of pagure-admin when the no date is
Pierre-Yves Chibon bee78a
        provided.
Pierre-Yves Chibon bee78a
        """
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 6e0576
        args = munch.Munch({"username": "pingou", "date": None, "list": False})
Pierre-Yves Chibon bee78a
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon bee78a
            pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon bee78a
        self.assertEqual(
Pierre-Yves Chibon bee78a
            cm.exception.args[0],
Pierre-Yves Chibon 73d120
            "Invalid date submitted: None, not of the format YYYY-MM-DD",
Pierre-Yves Chibon bee78a
        )
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 73d120
        user = pagure.lib.query.get_user(self.session, "pingou")
Pierre-Yves Chibon bee78a
        self.assertIsNone(user.refuse_sessions_before)
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
    def test_missing_username(self):
Pierre-Yves Chibon bee78a
        """ Test the block-user function of pagure-admin when the username
Pierre-Yves Chibon bee78a
        is missing from the args.
Pierre-Yves Chibon bee78a
        """
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 6e0576
        args = munch.Munch(
Pierre-Yves Chibon 6e0576
            {"date": "2018-06-11", "username": None, "list": False}
Pierre-Yves Chibon 6e0576
        )
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon bee78a
            pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 73d120
        self.assertEqual(cm.exception.args[0], "An username must be specified")
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 73d120
        user = pagure.lib.query.get_user(self.session, "pingou")
Pierre-Yves Chibon bee78a
        self.assertIsNone(user.refuse_sessions_before)
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
    def test_invalid_username(self):
Pierre-Yves Chibon bee78a
        """ Test the block-user function of pagure-admin when the username
Pierre-Yves Chibon bee78a
        provided does correspond to any user in the DB.
Pierre-Yves Chibon bee78a
        """
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 6e0576
        args = munch.Munch(
Pierre-Yves Chibon 6e0576
            {"date": "2018-06-11", "username": "invalid", "list": False}
Pierre-Yves Chibon 6e0576
        )
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon bee78a
            pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 73d120
        self.assertEqual(cm.exception.args[0], 'No user "invalid" found')
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 73d120
        user = pagure.lib.query.get_user(self.session, "pingou")
Pierre-Yves Chibon bee78a
        self.assertIsNone(user.refuse_sessions_before)
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
    def test_invalide_date(self):
Pierre-Yves Chibon bee78a
        """ Test the block-user function of pagure-admin when the provided
Pierre-Yves Chibon bee78a
        date is incorrect.
Pierre-Yves Chibon bee78a
        """
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 6e0576
        args = munch.Munch(
Pierre-Yves Chibon 6e0576
            {"date": "2018-14-05", "username": "pingou", "list": False}
Pierre-Yves Chibon 6e0576
        )
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon bee78a
            pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
        self.assertEqual(
Pierre-Yves Chibon bee78a
            cm.exception.args[0],
Pierre-Yves Chibon 73d120
            "Invalid date submitted: 2018-14-05, not of the format YYYY-MM-DD",
Pierre-Yves Chibon bee78a
        )
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 73d120
        user = pagure.lib.query.get_user(self.session, "pingou")
Pierre-Yves Chibon bee78a
        self.assertIsNone(user.refuse_sessions_before)
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 73d120
    @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
Pierre-Yves Chibon bee78a
    def test_block_user(self):
Pierre-Yves Chibon bee78a
        """ Test the block-user function of pagure-admin when all arguments
Pierre-Yves Chibon bee78a
        are provided correctly.
Pierre-Yves Chibon bee78a
        """
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 6e0576
        args = munch.Munch(
Pierre-Yves Chibon 6e0576
            {"date": "2050-12-31", "username": "pingou", "list": False}
Pierre-Yves Chibon 6e0576
        )
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon bee78a
        pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 73d120
        user = pagure.lib.query.get_user(self.session, "pingou")
Pierre-Yves Chibon 4c9301
        self.assertIsNotNone(user.refuse_sessions_before)
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 6e0576
    def test_list_blocked_user(self):
Pierre-Yves Chibon 6e0576
        """ Test the block-user function of pagure-admin when all arguments
Pierre-Yves Chibon 6e0576
        are provided correctly.
Pierre-Yves Chibon 6e0576
        """
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        args = munch.Munch({"list": True, "username": None, "date": None})
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        with tests.capture_output() as output:
Pierre-Yves Chibon 6e0576
            pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        output = output.getvalue()
Pierre-Yves Chibon 6e0576
        self.assertEqual("No users are currently blocked\n", output)
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
    @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
Pierre-Yves Chibon 6e0576
    def test_list_blocked_user_with_data(self):
Pierre-Yves Chibon 6e0576
        """ Test the block-user function of pagure-admin when all arguments
Pierre-Yves Chibon 6e0576
        are provided correctly.
Pierre-Yves Chibon 6e0576
        """
Pierre-Yves Chibon 6e0576
        args = munch.Munch(
Pierre-Yves Chibon 6e0576
            {"date": "2050-12-31", "username": "pingou", "list": False}
Pierre-Yves Chibon 6e0576
        )
Pierre-Yves Chibon 6e0576
        pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        args = munch.Munch({"list": True, "username": None, "date": None})
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        with tests.capture_output() as output:
Pierre-Yves Chibon 6e0576
            pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        output = output.getvalue()
Pierre-Yves Chibon 6e0576
        self.assertEqual(
Pierre-Yves Chibon 6e0576
            "Users blocked:\n"
Pierre-Yves Chibon 6e0576
            " pingou                -  2050-12-31T00:00:00\n",
Pierre-Yves Chibon 6e0576
            output,
Pierre-Yves Chibon 6e0576
        )
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
    @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
Pierre-Yves Chibon 6e0576
    def test_list_blocked_user_with_username_data(self):
Pierre-Yves Chibon 6e0576
        """ Test the block-user function of pagure-admin when all arguments
Pierre-Yves Chibon 6e0576
        are provided correctly.
Pierre-Yves Chibon 6e0576
        """
Pierre-Yves Chibon 6e0576
        args = munch.Munch(
Pierre-Yves Chibon 6e0576
            {"date": "2050-12-31", "username": "pingou", "list": False}
Pierre-Yves Chibon 6e0576
        )
Pierre-Yves Chibon 6e0576
        pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        args = munch.Munch({"list": True, "username": "ralph", "date": None})
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        with tests.capture_output() as output:
Pierre-Yves Chibon 6e0576
            pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        output = output.getvalue()
Pierre-Yves Chibon 6e0576
        self.assertEqual("No users are currently blocked\n", output)
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        args = munch.Munch({"list": True, "username": "pin*", "date": None})
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        with tests.capture_output() as output:
Pierre-Yves Chibon 6e0576
            pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon 6e0576
        output = output.getvalue()
Pierre-Yves Chibon 6e0576
        self.assertEqual(
Pierre-Yves Chibon 6e0576
            "Users blocked:\n"
Pierre-Yves Chibon 6e0576
            " pingou                -  2050-12-31T00:00:00\n",
Pierre-Yves Chibon 6e0576
            output,
Pierre-Yves Chibon 6e0576
        )
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
    @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
Pierre-Yves Chibon 6e0576
    def test_list_blocked_user_with_date(self):
Pierre-Yves Chibon 6e0576
        """ Test the block-user function of pagure-admin when all arguments
Pierre-Yves Chibon 6e0576
        are provided correctly.
Pierre-Yves Chibon 6e0576
        """
Pierre-Yves Chibon 6e0576
        args = munch.Munch(
Pierre-Yves Chibon 6e0576
            {"list": True, "username": None, "date": "2050-12-31"}
Pierre-Yves Chibon 6e0576
        )
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        with tests.capture_output() as output:
Pierre-Yves Chibon 6e0576
            pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        output = output.getvalue()
Pierre-Yves Chibon d437db
        self.assertIn("No users are currently blocked\n", output)
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
    @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
Pierre-Yves Chibon 6e0576
    def test_list_blocked_user_with_date_and_data(self):
Pierre-Yves Chibon 6e0576
        """ Test the block-user function of pagure-admin when all arguments
Pierre-Yves Chibon 6e0576
        are provided correctly.
Pierre-Yves Chibon 6e0576
        """
Pierre-Yves Chibon 6e0576
        args = munch.Munch(
Pierre-Yves Chibon 6e0576
            {"date": "2050-12-31", "username": "pingou", "list": False}
Pierre-Yves Chibon 6e0576
        )
Pierre-Yves Chibon 6e0576
        pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        args = munch.Munch(
Pierre-Yves Chibon 6e0576
            {"list": True, "username": None, "date": "2050-12-31"}
Pierre-Yves Chibon 6e0576
        )
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        with tests.capture_output() as output:
Pierre-Yves Chibon 6e0576
            pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        output = output.getvalue()
Pierre-Yves Chibon d437db
        self.assertIn(
Pierre-Yves Chibon 6e0576
            "Users blocked:\n"
Pierre-Yves Chibon 6e0576
            " pingou                -  2050-12-31T00:00:00\n",
Pierre-Yves Chibon 6e0576
            output,
Pierre-Yves Chibon 6e0576
        )
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        args = munch.Munch(
Pierre-Yves Chibon 6e0576
            {"list": True, "username": None, "date": "2051-01-01"}
Pierre-Yves Chibon 6e0576
        )
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        with tests.capture_output() as output:
Pierre-Yves Chibon 6e0576
            pagure.cli.admin.do_block_user(args)
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon 6e0576
        output = output.getvalue()
Pierre-Yves Chibon d437db
        self.assertIn("No users are currently blocked\n", output)
Pierre-Yves Chibon 6e0576
Pierre-Yves Chibon bee78a
Pierre-Yves Chibon 74b8f8
class PagureAdminDeleteProjectTests(tests.Modeltests):
Pierre-Yves Chibon 74b8f8
    """ Tests for pagure-admin delete-project """
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
    populate_db = False
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
    def setUp(self):
Pierre-Yves Chibon 74b8f8
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 74b8f8
        super(PagureAdminDeleteProjectTests, self).setUp()
Pierre-Yves Chibon 74b8f8
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
        # Create the user pingou
Pierre-Yves Chibon 74b8f8
        item = pagure.lib.model.User(
Pierre-Yves Chibon 74b8f8
            user="pingou",
Pierre-Yves Chibon 74b8f8
            fullname="PY C",
Pierre-Yves Chibon 74b8f8
            password="foo",
Pierre-Yves Chibon 74b8f8
            default_email="bar@pingou.com",
Pierre-Yves Chibon 74b8f8
        )
Pierre-Yves Chibon 74b8f8
        self.session.add(item)
Pierre-Yves Chibon 74b8f8
        item = pagure.lib.model.UserEmail(user_id=1, email="bar@pingou.com")
Pierre-Yves Chibon 74b8f8
        self.session.add(item)
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
        # Create two projects for the user pingou
Pierre-Yves Chibon 74b8f8
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 74b8f8
            user_id=1,  # pingou
Pierre-Yves Chibon 74b8f8
            name="test",
Pierre-Yves Chibon 74b8f8
            description="namespaced test project",
Pierre-Yves Chibon 74b8f8
            hook_token="aaabbbeee",
Pierre-Yves Chibon 74b8f8
            namespace="somenamespace",
Pierre-Yves Chibon 74b8f8
        )
Pierre-Yves Chibon 74b8f8
        self.session.add(item)
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 74b8f8
            user_id=1,  # pingou
Pierre-Yves Chibon 74b8f8
            name="test",
Pierre-Yves Chibon 74b8f8
            description="Test project",
Pierre-Yves Chibon 74b8f8
            hook_token="aaabbbccc",
Pierre-Yves Chibon 74b8f8
            namespace=None,
Pierre-Yves Chibon 74b8f8
        )
Pierre-Yves Chibon 74b8f8
        self.session.add(item)
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
        self.session.commit()
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
        # Make the imported pagure use the correct db session
Pierre-Yves Chibon 74b8f8
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
    def test_delete_project_unknown_project(self):
Pierre-Yves Chibon 74b8f8
        """ Test the read-only function of pagure-admin on an unknown
Pierre-Yves Chibon 74b8f8
        project.
Pierre-Yves Chibon 74b8f8
        """
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
        args = munch.Munch(
Pierre-Yves Chibon 74b8f8
            {"project": "foob", "user": None, "action_user": "pingou"}
Pierre-Yves Chibon 74b8f8
        )
Pierre-Yves Chibon 74b8f8
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon 74b8f8
            pagure.cli.admin.do_delete_project(args)
Pierre-Yves Chibon 74b8f8
        self.assertEqual(cm.exception.args[0], "No project found with: foob")
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
    def test_delete_project_invalid_project(self):
Pierre-Yves Chibon 74b8f8
        """ Test the read-only function of pagure-admin on an invalid
Pierre-Yves Chibon 74b8f8
        project.
Pierre-Yves Chibon 74b8f8
        """
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
        args = munch.Munch(
Pierre-Yves Chibon 74b8f8
            {"project": "fo/o/b", "user": None, "action_user": "pingou"}
Pierre-Yves Chibon 74b8f8
        )
Pierre-Yves Chibon 74b8f8
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon 74b8f8
            pagure.cli.admin.do_delete_project(args)
Pierre-Yves Chibon 74b8f8
        self.assertEqual(
Pierre-Yves Chibon 74b8f8
            cm.exception.args[0],
Pierre-Yves Chibon 74b8f8
            'Invalid project name, has more than one "/": fo/o/b',
Pierre-Yves Chibon 74b8f8
        )
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
    @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
Pierre-Yves Chibon 74b8f8
    def test_delete_project(self):
Pierre-Yves Chibon 74b8f8
        """ Test the read-only function of pagure-admin to get status of
Pierre-Yves Chibon 74b8f8
        a non-namespaced project.
Pierre-Yves Chibon 74b8f8
        """
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
        args = munch.Munch(
Pierre-Yves Chibon 74b8f8
            {"project": "test", "user": None, "action_user": "pingou"}
Pierre-Yves Chibon 74b8f8
        )
Pierre-Yves Chibon 74b8f8
        with tests.capture_output() as output:
Pierre-Yves Chibon 74b8f8
            pagure.cli.admin.do_delete_project(args)
Pierre-Yves Chibon 74b8f8
        output = output.getvalue()
Pierre-Yves Chibon 74b8f8
        self.assertEqual(
Pierre-Yves Chibon 74b8f8
            "Are you sure you want to delete: test?\n"
Pierre-Yves Chibon 74b8f8
            "  This cannot be undone!\n"
Pierre-Yves Chibon 74b8f8
            "Project deleted\n",
Pierre-Yves Chibon 74b8f8
            output,
Pierre-Yves Chibon 74b8f8
        )
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
    @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
Pierre-Yves Chibon 74b8f8
    def test_delete_project_namespace(self):
Pierre-Yves Chibon 74b8f8
        """ Test the read-only function of pagure-admin to get status of
Pierre-Yves Chibon 74b8f8
        a namespaced project.
Pierre-Yves Chibon 74b8f8
        """
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
        args = munch.Munch(
Pierre-Yves Chibon 74b8f8
            {
Pierre-Yves Chibon 74b8f8
                "project": "somenamespace/test",
Pierre-Yves Chibon 74b8f8
                "user": None,
Pierre-Yves Chibon 74b8f8
                "action_user": "pingou",
Pierre-Yves Chibon 74b8f8
            }
Pierre-Yves Chibon 74b8f8
        )
Pierre-Yves Chibon 74b8f8
        with tests.capture_output() as output:
Pierre-Yves Chibon 74b8f8
            pagure.cli.admin.do_delete_project(args)
Pierre-Yves Chibon 74b8f8
        output = output.getvalue()
Pierre-Yves Chibon 74b8f8
        self.assertEqual(
Pierre-Yves Chibon 74b8f8
            "Are you sure you want to delete: somenamespace/test?\n"
Pierre-Yves Chibon 74b8f8
            "  This cannot be undone!\n"
Pierre-Yves Chibon 74b8f8
            "Project deleted\n",
Pierre-Yves Chibon 74b8f8
            output,
Pierre-Yves Chibon 74b8f8
        )
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
    @patch("pagure.cli.admin._ask_confirmation", MagicMock(return_value=True))
Pierre-Yves Chibon 74b8f8
    def test_delete_project_namespace_changed(self):
Pierre-Yves Chibon 74b8f8
        """ Test the read-only function of pagure-admin to set the status of
Pierre-Yves Chibon 74b8f8
        a namespaced project.
Pierre-Yves Chibon 74b8f8
        """
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
        # Before
Pierre-Yves Chibon 74b8f8
        projects = pagure.lib.query.search_projects(self.session)
Pierre-Yves Chibon 74b8f8
        self.assertEqual(len(projects), 2)
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
        args = munch.Munch(
Pierre-Yves Chibon 74b8f8
            {
Pierre-Yves Chibon 74b8f8
                "project": "somenamespace/test",
Pierre-Yves Chibon 74b8f8
                "user": None,
Pierre-Yves Chibon 74b8f8
                "action_user": "pingou",
Pierre-Yves Chibon 74b8f8
            }
Pierre-Yves Chibon 74b8f8
        )
Pierre-Yves Chibon 74b8f8
        with tests.capture_output() as output:
Pierre-Yves Chibon 74b8f8
            pagure.cli.admin.do_delete_project(args)
Pierre-Yves Chibon 74b8f8
        output = output.getvalue()
Pierre-Yves Chibon 74b8f8
        self.assertEqual(
Pierre-Yves Chibon 74b8f8
            "Are you sure you want to delete: somenamespace/test?\n"
Pierre-Yves Chibon 74b8f8
            "  This cannot be undone!\n"
Pierre-Yves Chibon 74b8f8
            "Project deleted\n",
Pierre-Yves Chibon 74b8f8
            output,
Pierre-Yves Chibon 74b8f8
        )
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
        # After
Pierre-Yves Chibon 74b8f8
        projects = pagure.lib.query.search_projects(self.session)
Pierre-Yves Chibon 74b8f8
        self.assertEqual(len(projects), 1)
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon 74b8f8
Pierre-Yves Chibon a10996
class PagureCreateBranchTests(tests.Modeltests):
Pierre-Yves Chibon a10996
    """ Tests for pagure-admin create-branch """
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
    populate_db = True
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
    def setUp(self):
Pierre-Yves Chibon a10996
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon a10996
        super(PagureCreateBranchTests, self).setUp()
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
        # Create a couple of projects
Pierre-Yves Chibon a10996
        tests.create_projects(self.session)
Pierre-Yves Chibon a10996
        # Create their git repo
Pierre-Yves Chibon a10996
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
        # Make the imported pagure use the correct db session
Pierre-Yves Chibon a10996
        pagure.cli.admin.session = self.session
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
    def test_create_branch_unknown_project(self):
Pierre-Yves Chibon a10996
        """ Test the read-only function of pagure-admin on an unknown
Pierre-Yves Chibon a10996
        project.
Pierre-Yves Chibon a10996
        """
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
        args = munch.Munch(
Pierre-Yves Chibon a10996
            {
Pierre-Yves Chibon a10996
                "project": "foob",
Pierre-Yves Chibon a10996
                "user": None,
Pierre-Yves Chibon a10996
                "new_branch": "new_branch",
Pierre-Yves Chibon a10996
                "from_branch": "master",
Pierre-Yves Chibon a10996
                "from_commit": None,
Pierre-Yves Chibon a10996
                "action_user": "pingou",
Pierre-Yves Chibon a10996
            }
Pierre-Yves Chibon a10996
        )
Pierre-Yves Chibon a10996
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon a10996
            pagure.cli.admin.do_create_branch(args)
Pierre-Yves Chibon a10996
        self.assertEqual(
Pierre-Yves Chibon a10996
            cm.exception.args[0], "No project found with: foob, user: None"
Pierre-Yves Chibon a10996
        )
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
    def test_create_branch_invalid_project(self):
Pierre-Yves Chibon a10996
        """ Test the read-only function of pagure-admin on an invalid
Pierre-Yves Chibon a10996
        project.
Pierre-Yves Chibon a10996
        """
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
        args = munch.Munch(
Pierre-Yves Chibon a10996
            {
Pierre-Yves Chibon a10996
                "project": "f/o/o/b",
Pierre-Yves Chibon a10996
                "user": None,
Pierre-Yves Chibon a10996
                "new_branch": "new_branch",
Pierre-Yves Chibon a10996
                "from_branch": "master",
Pierre-Yves Chibon a10996
                "from_commit": None,
Pierre-Yves Chibon a10996
                "action_user": "pingou",
Pierre-Yves Chibon a10996
            }
Pierre-Yves Chibon a10996
        )
Pierre-Yves Chibon a10996
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon a10996
            pagure.cli.admin.do_create_branch(args)
Pierre-Yves Chibon a10996
        self.assertEqual(
Pierre-Yves Chibon a10996
            cm.exception.args[0],
Pierre-Yves Chibon a10996
            'Invalid project name, has more than one "/": f/o/o/b',
Pierre-Yves Chibon a10996
        )
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
    def test_create_branch_commit_and_branch_from(self):
Pierre-Yves Chibon a10996
        """ Test the read-only function of pagure-admin to get status of
Pierre-Yves Chibon a10996
        a non-namespaced project.
Pierre-Yves Chibon a10996
        """
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
        args = munch.Munch(
Pierre-Yves Chibon a10996
            {
Pierre-Yves Chibon a10996
                "project": "test",
Pierre-Yves Chibon a10996
                "user": None,
Pierre-Yves Chibon a10996
                "new_branch": "new_branch",
Pierre-Yves Chibon a10996
                "from_branch": "master",
Pierre-Yves Chibon a10996
                "from_commit": "foobar",
Pierre-Yves Chibon a10996
                "action_user": "pingou",
Pierre-Yves Chibon a10996
            }
Pierre-Yves Chibon a10996
        )
Pierre-Yves Chibon a10996
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon a10996
            pagure.cli.admin.do_create_branch(args)
Pierre-Yves Chibon a10996
        self.assertEqual(
Pierre-Yves Chibon a10996
            cm.exception.args[0],
Pierre-Yves Chibon a10996
            "You must create the branch from something, either a commit "
Pierre-Yves Chibon a10996
            "or another branch, not from both",
Pierre-Yves Chibon a10996
        )
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
    def test_create_branch_no_branch_from(self):
Pierre-Yves Chibon a10996
        """ Test the read-only function of pagure-admin to get status of
Pierre-Yves Chibon a10996
        a non-namespaced project.
Pierre-Yves Chibon a10996
        """
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
        args = munch.Munch(
Pierre-Yves Chibon a10996
            {
Pierre-Yves Chibon a10996
                "project": "test",
Pierre-Yves Chibon a10996
                "user": None,
Pierre-Yves Chibon a10996
                "new_branch": "new_branch",
Pierre-Yves Chibon a10996
                "from_branch": "master",
Pierre-Yves Chibon a10996
                "from_commit": None,
Pierre-Yves Chibon a10996
                "action_user": "pingou",
Pierre-Yves Chibon a10996
            }
Pierre-Yves Chibon a10996
        )
Pierre-Yves Chibon a10996
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon a10996
            pagure.cli.admin.do_create_branch(args)
Pierre-Yves Chibon a10996
        self.assertEqual(
Pierre-Yves Chibon a10996
            cm.exception.args[0], 'The "master" branch does not exist'
Pierre-Yves Chibon a10996
        )
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
    def test_create_branch_no_commit_from(self):
Pierre-Yves Chibon a10996
        """ Test the read-only function of pagure-admin to get status of
Pierre-Yves Chibon a10996
        a non-namespaced project.
Pierre-Yves Chibon a10996
        """
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
        args = munch.Munch(
Pierre-Yves Chibon a10996
            {
Pierre-Yves Chibon a10996
                "project": "test",
Pierre-Yves Chibon a10996
                "user": None,
Pierre-Yves Chibon a10996
                "new_branch": "new_branch",
Pierre-Yves Chibon a10996
                "from_branch": None,
Pierre-Yves Chibon a10996
                "from_commit": "foobar",
Pierre-Yves Chibon a10996
                "action_user": "pingou",
Pierre-Yves Chibon a10996
            }
Pierre-Yves Chibon a10996
        )
Pierre-Yves Chibon a10996
        with self.assertRaises(pagure.exceptions.PagureException) as cm:
Pierre-Yves Chibon a10996
            pagure.cli.admin.do_create_branch(args)
Pierre-Yves Chibon a10996
        self.assertEqual(
Pierre-Yves Chibon a10996
            cm.exception.args[0], "No commit foobar found from which to branch"
Pierre-Yves Chibon a10996
        )
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
    def test_create_branch_from_branch(self):
Pierre-Yves Chibon a10996
        """ Test the do_create_admin_token function of pagure-admin. """
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
        gitrepo_path = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon a10996
        tests.add_content_git_repo(gitrepo_path)
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
        # Check branch before:
Pierre-Yves Chibon a10996
        gitrepo = pygit2.Repository(gitrepo_path)
Pierre-Yves Chibon a10996
        self.assertEqual(gitrepo.listall_branches(), ["master"])
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
        args = munch.Munch(
Pierre-Yves Chibon a10996
            {
Pierre-Yves Chibon a10996
                "project": "test",
Pierre-Yves Chibon a10996
                "user": None,
Pierre-Yves Chibon a10996
                "new_branch": "new_branch",
Pierre-Yves Chibon a10996
                "from_branch": "master",
Pierre-Yves Chibon a10996
                "from_commit": None,
Pierre-Yves Chibon a10996
                "action_user": "pingou",
Pierre-Yves Chibon a10996
            }
Pierre-Yves Chibon a10996
        )
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
        with tests.capture_output() as output:
Pierre-Yves Chibon a10996
            pagure.cli.admin.do_create_branch(args)
Pierre-Yves Chibon a10996
        output = output.getvalue()
Pierre-Yves Chibon a10996
        self.assertEqual("Branch created\n", output)
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
        # Check branch after:
Pierre-Yves Chibon a10996
        gitrepo = pygit2.Repository(gitrepo_path)
Pierre-Yves Chibon a10996
        self.assertEqual(
Pierre-Yves Chibon a10996
            sorted(gitrepo.listall_branches()), ["master", "new_branch"]
Pierre-Yves Chibon a10996
        )
Pierre-Yves Chibon a10996
Pierre-Yves Chibon a10996
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon d4c9da
    unittest.main(verbosity=2)