Blame tests/test_pagure_exclude_group_index.py

Pierre-Yves Chibon 0f5019
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
"""
Pierre-Yves Chibon 0f5019
 (c) 2017 - Copyright Red Hat Inc
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
 Authors:
Pierre-Yves Chibon 0f5019
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
"""
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon 0f5019
import unittest
Pierre-Yves Chibon 0f5019
import shutil
Pierre-Yves Chibon 0f5019
import sys
Pierre-Yves Chibon 0f5019
import os
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
import mock
Pierre-Yves Chibon 0f5019
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 0f5019
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 0f5019
import pagure.lib.model
Pierre-Yves Chibon 0f5019
import tests
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 0f5019
class PagureExcludeGroupIndex(tests.Modeltests):
Pierre-Yves Chibon 0f5019
    """ Tests the EXCLUDE_GROUP_INDEX configuration key in pagure """
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
    def setUp(self):
Pierre-Yves Chibon 0f5019
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 0f5019
        super(PagureExcludeGroupIndex, self).setUp()
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        # Create a ``provenpackger`` group:
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group(
Pierre-Yves Chibon 0f5019
            self.session,
Pierre-Yves Chibon 73d120
            group_name="provenpackager",
Pierre-Yves Chibon 73d120
            display_name="Proven Packagers",
Pierre-Yves Chibon 73d120
            description="Packagers having access to all the repo",
Pierre-Yves Chibon 73d120
            group_type="user",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 0f5019
            is_admin=False,
Pierre-Yves Chibon 0f5019
            blacklist=[],
Pierre-Yves Chibon 0f5019
        )
Pierre-Yves Chibon 0f5019
        self.session.commit()
Pierre-Yves Chibon 0f5019
        self.assertEqual(
Pierre-Yves Chibon 73d120
            msg, "User `pingou` added to the group `provenpackager`."
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        # Add the `provenpackager` group to the test2 project
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test2")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon 0f5019
            session=self.session,
Pierre-Yves Chibon 0f5019
            project=project,
Pierre-Yves Chibon 73d120
            new_group="provenpackager",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 0f5019
        )
Pierre-Yves Chibon 0f5019
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Group added")
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
    def test_defaults_pingou(self):
Pierre-Yves Chibon 0f5019
        """ Test which repo pingou has by default. """
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 930073
        repos = pagure.lib.query.search_projects(
Pierre-Yves Chibon 73d120
            self.session, username="pingou", fork=False
Pierre-Yves Chibon 0f5019
        )
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        self.assertEqual(len(repos), 3)
Pierre-Yves Chibon 73d120
        for idx, name in enumerate(["test", "test2", "test3"]):
Pierre-Yves Chibon 0f5019
            self.assertEqual(repos[idx].name, name)
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
    def test_defaults_foo(self):
Pierre-Yves Chibon 0f5019
        """ Test which repo foo has by default. """
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 930073
        repos = pagure.lib.query.search_projects(
Pierre-Yves Chibon 73d120
            self.session, username="foo", fork=False
Pierre-Yves Chibon 0f5019
        )
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        self.assertEqual(len(repos), 0)
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
    def test_add_foo_test(self):
Pierre-Yves Chibon 0f5019
        """ Test adding foo to the test project. """
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 930073
        group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 73d120
            self.session, group_name="provenpackager"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(group.group_name, "provenpackager")
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        # List all foo's project before (ie: there should be none)
Pierre-Yves Chibon 930073
        repos = pagure.lib.query.search_projects(
Pierre-Yves Chibon 73d120
            self.session, username="foo", fork=False
Pierre-Yves Chibon 0f5019
        )
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        self.assertEqual(len(repos), 0)
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        # Adding `foo` to the `provenpackager` group
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_group(
Pierre-Yves Chibon 0f5019
            self.session,
Pierre-Yves Chibon 73d120
            username="foo",
Pierre-Yves Chibon 0f5019
            group=group,
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 0f5019
            is_admin=False,
Pierre-Yves Chibon 0f5019
        )
Pierre-Yves Chibon 0f5019
        self.assertEqual(
Pierre-Yves Chibon 73d120
            msg, "User `foo` added to the group `provenpackager`."
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        # Test that foo has now one project, via the provenpackager group
Pierre-Yves Chibon 930073
        repos = pagure.lib.query.search_projects(
Pierre-Yves Chibon 73d120
            self.session, username="foo", fork=False
Pierre-Yves Chibon 0f5019
        )
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        self.assertEqual(len(repos), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(repos[0].name, "test2")
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
    def test_excluding_provenpackager(self):
Pierre-Yves Chibon 0f5019
        """ Test retrieving user's repo with a group excluded. """
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        # Add `foo` to `provenpackager`
Pierre-Yves Chibon 930073
        group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 73d120
            self.session, group_name="provenpackager"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(group.group_name, "provenpackager")
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_group(
Pierre-Yves Chibon 0f5019
            self.session,
Pierre-Yves Chibon 73d120
            username="foo",
Pierre-Yves Chibon 0f5019
            group=group,
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 0f5019
            is_admin=False,
Pierre-Yves Chibon 0f5019
        )
Pierre-Yves Chibon 0f5019
        self.assertEqual(
Pierre-Yves Chibon 73d120
            msg, "User `foo` added to the group `provenpackager`."
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        # Get foo's project outside of proven packager
Pierre-Yves Chibon 930073
        repos = pagure.lib.query.search_projects(
Pierre-Yves Chibon 0f5019
            self.session,
Pierre-Yves Chibon 73d120
            username="foo",
Pierre-Yves Chibon 73d120
            exclude_groups=["provenpackager"],
Pierre-Yves Chibon 0f5019
            fork=False,
Pierre-Yves Chibon 0f5019
        )
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        self.assertEqual(len(repos), 0)
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        # Get pingou's project outside of proven packager (nothing changes)
Pierre-Yves Chibon 930073
        repos = pagure.lib.query.search_projects(
Pierre-Yves Chibon 0f5019
            self.session,
Pierre-Yves Chibon 73d120
            username="pingou",
Pierre-Yves Chibon 73d120
            exclude_groups=["provenpackager"],
Pierre-Yves Chibon 0f5019
            fork=False,
Pierre-Yves Chibon 0f5019
        )
Pierre-Yves Chibon 930073
        repos2 = pagure.lib.query.search_projects(
Pierre-Yves Chibon 73d120
            self.session, username="pingou", fork=False
Pierre-Yves Chibon 0f5019
        )
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
        self.assertEqual(repos, repos2)
Pierre-Yves Chibon 0f5019
        self.assertEqual(len(repos), 3)
Pierre-Yves Chibon 73d120
        for idx, name in enumerate(["test", "test2", "test3"]):
Pierre-Yves Chibon 0f5019
            self.assertEqual(repos[idx].name, name)
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 0f5019
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 0f5019
    unittest.main(verbosity=2)