Blame tests/test_pagure_lib_add_user_to_project.py

Pierre-Yves Chibon e1d70f
# -*- coding: utf-8 -*-
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
"""
Pierre-Yves Chibon e1d70f
 (c) 2017 - Copyright Red Hat Inc
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
 Authors:
Pierre-Yves Chibon e1d70f
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
"""
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon e1d70f
import unittest
Pierre-Yves Chibon e1d70f
import sys
Pierre-Yves Chibon e1d70f
import os
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
from mock import patch, MagicMock
Pierre-Yves Chibon e1d70f
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 e1d70f
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon e1d70f
import tests
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
class PagureLibAddUserToProjecttests(tests.Modeltests):
Pierre-Yves Chibon 930073
    """ Tests for pagure.lib.query.add_user_to_project """
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
    def setUp(self):
Pierre-Yves Chibon e1d70f
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon e1d70f
        super(PagureLibAddUserToProjecttests, self).setUp()
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        tests.create_projects(self.session)
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="bar",
Pierre-Yves Chibon 73d120
            fullname="bar baz",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@bar.com",
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
Pierre-Yves Chibon e1d70f
        self.session.add(item)
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # Before
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 0)
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 73d120
            session=self.session, project=repo, new_user="foo", user="pingou"
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User added")
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # After
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.users[0].user, "foo")
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.admins[0].user, "foo")
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon e1d70f
    def test_re_add_user_to_project_default(self):
Pierre-Yves Chibon e1d70f
        """ Update an existing user but to the same access level. """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # Try adding the same user with the same access
Pierre-Yves Chibon e1d70f
        self.assertRaises(
Pierre-Yves Chibon e1d70f
            pagure.exceptions.PagureException,
Pierre-Yves Chibon 930073
            pagure.lib.query.add_user_to_project,
Pierre-Yves Chibon e1d70f
            session=self.session,
Pierre-Yves Chibon e1d70f
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="admin",
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon e1d70f
    def test_update_user_to_project_default(self):
Pierre-Yves Chibon e1d70f
        """ Update an existing user without any required group membership.
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # Update the access of the user
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon e1d70f
            session=self.session,
Pierre-Yves Chibon e1d70f
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="commit",
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User access updated")
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.users[0].user, "foo")
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.committers[0].user, "foo")
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon e1d70f
    def test_update_user_to_project_require_packager_on_all(self):
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon e1d70f
        Update an existing user but required group membership on all
Pierre-Yves Chibon e1d70f
        projects.
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 73d120
        config = {"*": ["packager"]}
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # Update the access of the user
Pierre-Yves Chibon e1d70f
        self.assertRaises(
Pierre-Yves Chibon e1d70f
            pagure.exceptions.PagureException,
Pierre-Yves Chibon 930073
            pagure.lib.query.add_user_to_project,
Pierre-Yves Chibon e1d70f
            session=self.session,
Pierre-Yves Chibon e1d70f
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="admin",
Pierre-Yves Chibon 73d120
            required_groups=config,
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.users[0].user, "foo")
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.committers[0].user, "foo")
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon e1d70f
    def test_update_user_to_project_require_packager_on_st(self):
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon e1d70f
        Update an existing user but required group membership on all
Pierre-Yves Chibon e1d70f
        projects match *st.
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 73d120
        config = {"*st": ["packager"]}
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # Update the access of the user
Pierre-Yves Chibon e1d70f
        self.assertRaises(
Pierre-Yves Chibon e1d70f
            pagure.exceptions.PagureException,
Pierre-Yves Chibon 930073
            pagure.lib.query.add_user_to_project,
Pierre-Yves Chibon e1d70f
            session=self.session,
Pierre-Yves Chibon e1d70f
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="admin",
Pierre-Yves Chibon 73d120
            required_groups=config,
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.users[0].user, "foo")
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.committers[0].user, "foo")
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon e1d70f
    def test_update_user_to_project_require_packager_on_te(self):
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon e1d70f
        Update an existing user but required group membership on all
Pierre-Yves Chibon e1d70f
        projects match te*.
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 73d120
        config = {"te*": ["packager"]}
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # Update the access of the user
Pierre-Yves Chibon e1d70f
        self.assertRaises(
Pierre-Yves Chibon e1d70f
            pagure.exceptions.PagureException,
Pierre-Yves Chibon 930073
            pagure.lib.query.add_user_to_project,
Pierre-Yves Chibon e1d70f
            session=self.session,
Pierre-Yves Chibon e1d70f
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="admin",
Pierre-Yves Chibon 73d120
            required_groups=config,
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.users[0].user, "foo")
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.committers[0].user, "foo")
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon e1d70f
    def test_update_user_to_project_require_packager_on_test(self):
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon e1d70f
        Update an existing user but required group membership on a specific
Pierre-Yves Chibon e1d70f
        project: test.
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 73d120
        config = {"test": ["packager"]}
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # Update the access of the user
Pierre-Yves Chibon e1d70f
        self.assertRaises(
Pierre-Yves Chibon e1d70f
            pagure.exceptions.PagureException,
Pierre-Yves Chibon 930073
            pagure.lib.query.add_user_to_project,
Pierre-Yves Chibon e1d70f
            session=self.session,
Pierre-Yves Chibon e1d70f
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="admin",
Pierre-Yves Chibon 73d120
            required_groups=config,
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.users[0].user, "foo")
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.committers[0].user, "foo")
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon e1d70f
    def test_add_user_to_test2_require_packager_on_test(self):
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon e1d70f
        Add user to project test2 while the configuration requires group
Pierre-Yves Chibon e1d70f
        membership on the project test.
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test2")
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 0)
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
        config = {"test": ["packager"]}
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # Add the user
Pierre-Yves Chibon 930073
        pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon e1d70f
            session=self.session,
Pierre-Yves Chibon e1d70f
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="admin",
Pierre-Yves Chibon 73d120
            required_groups=config,
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.users[0].user, "foo")
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.committers[0].user, "foo")
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
class PagureLibAddUserToProjectWithGrouptests(PagureLibAddUserToProjecttests):
Pierre-Yves Chibon 930073
    """ Tests for pagure.lib.query.add_user_to_project """
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
    def setUp(self):
Pierre-Yves Chibon e1d70f
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon e1d70f
        super(PagureLibAddUserToProjectWithGrouptests, self).setUp()
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # Create group
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group(
Pierre-Yves Chibon e1d70f
            self.session,
Pierre-Yves Chibon 73d120
            group_name="packager",
Pierre-Yves Chibon 73d120
            display_name="packager",
Pierre-Yves Chibon 73d120
            description="The Fedora packager groups",
Pierre-Yves Chibon 73d120
            group_type="user",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon e1d70f
            is_admin=False,
Pierre-Yves Chibon 73d120
            blacklist=[],
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User `pingou` added to the group `packager`.")
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # Add user to group
Pierre-Yves Chibon 73d120
        group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 73d120
            self.session, group_name="packager"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_group(
Pierre-Yves Chibon e1d70f
            self.session,
Pierre-Yves Chibon 73d120
            username="bar",
Pierre-Yves Chibon e1d70f
            group=group,
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            is_admin=True,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User `bar` added to the group `packager`.")
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon e1d70f
    def test_add_user_to_test_require_packager_on_test(self):
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon e1d70f
        Add user to project test while the configuration requires group
Pierre-Yves Chibon e1d70f
        membership on the project test.
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 1)
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
        config = {"test": ["packager"]}
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # Add the user to the project
Pierre-Yves Chibon 930073
        pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon e1d70f
            session=self.session,
Pierre-Yves Chibon e1d70f
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="bar",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="commit",
Pierre-Yves Chibon 73d120
            required_groups=config,
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 2)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.users[0].user, "foo")
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.committers[0].user, "foo")
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.users[1].user, "bar")
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.committers[1].user, "bar")
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon e1d70f
    def test_add_user_to_test_require_packager(self):
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon e1d70f
        Add user to project test while the configuration requires group
Pierre-Yves Chibon e1d70f
        membership on all the projects.
Pierre-Yves Chibon e1d70f
        """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 1)
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
        config = {"*": ["packager"]}
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # Add the user to the project
Pierre-Yves Chibon 930073
        pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon e1d70f
            session=self.session,
Pierre-Yves Chibon e1d70f
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="bar",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="commit",
Pierre-Yves Chibon 73d120
            required_groups=config,
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 2)
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.users[0].user, "foo")
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.committers[0].user, "foo")
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.users[1].user, "bar")
Pierre-Yves Chibon 73d120
        self.assertEqual(repo.committers[1].user, "bar")
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon e1d70f
    unittest.main(verbosity=2)