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
Aurélien Bompard dcf6f6
from __future__ import unicode_literals
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 e1d70f
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon e1d70f
    os.path.abspath(__file__)), '..'))
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 e1d70f
            user='bar',
Pierre-Yves Chibon e1d70f
            fullname='bar baz',
Pierre-Yves Chibon e1d70f
            password='foo',
Pierre-Yves Chibon e1d70f
            default_email='bar@bar.com',
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.add(item)
Pierre-Yves Chibon e1d70f
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon e1d70f
            user_id=3,
Pierre-Yves Chibon e1d70f
            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 930073
        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 e1d70f
            session=self.session,
Pierre-Yves Chibon e1d70f
            project=repo,
Pierre-Yves Chibon e1d70f
            new_user='foo',
Pierre-Yves Chibon e1d70f
            user='pingou',
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
        self.assertEqual(msg, 'User added')
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
        # After
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 1)
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.users[0].user, 'foo')
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.admins[0].user, 'foo')
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
    @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 930073
        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 e1d70f
            new_user='foo',
Pierre-Yves Chibon e1d70f
            user='pingou',
Pierre-Yves Chibon e1d70f
            access='admin'
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
    @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 930073
        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 e1d70f
            new_user='foo',
Pierre-Yves Chibon e1d70f
            user='pingou',
Pierre-Yves Chibon e1d70f
            access='commit'
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
        self.assertEqual(msg, 'User access updated')
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 1)
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.users[0].user, 'foo')
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.committers[0].user, 'foo')
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
    @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 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon e1d70f
        config = {
Pierre-Yves Chibon e1d70f
            '*': ['packager']
Pierre-Yves Chibon e1d70f
        }
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 e1d70f
            new_user='foo',
Pierre-Yves Chibon e1d70f
            user='pingou',
Pierre-Yves Chibon e1d70f
            access='admin',
Pierre-Yves Chibon e1d70f
            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 e1d70f
        self.assertEqual(repo.users[0].user, 'foo')
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.committers[0].user, 'foo')
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
    @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 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon e1d70f
        config = {
Pierre-Yves Chibon e1d70f
            '*st': ['packager']
Pierre-Yves Chibon e1d70f
        }
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 e1d70f
            new_user='foo',
Pierre-Yves Chibon e1d70f
            user='pingou',
Pierre-Yves Chibon e1d70f
            access='admin',
Pierre-Yves Chibon e1d70f
            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 e1d70f
        self.assertEqual(repo.users[0].user, 'foo')
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.committers[0].user, 'foo')
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
    @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 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon e1d70f
        config = {
Pierre-Yves Chibon e1d70f
            'te*': ['packager']
Pierre-Yves Chibon e1d70f
        }
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 e1d70f
            new_user='foo',
Pierre-Yves Chibon e1d70f
            user='pingou',
Pierre-Yves Chibon e1d70f
            access='admin',
Pierre-Yves Chibon e1d70f
            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 e1d70f
        self.assertEqual(repo.users[0].user, 'foo')
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.committers[0].user, 'foo')
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
    @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 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon e1d70f
        config = {
Pierre-Yves Chibon e1d70f
            'test': ['packager']
Pierre-Yves Chibon e1d70f
        }
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 e1d70f
            new_user='foo',
Pierre-Yves Chibon e1d70f
            user='pingou',
Pierre-Yves Chibon e1d70f
            access='admin',
Pierre-Yves Chibon e1d70f
            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 e1d70f
        self.assertEqual(repo.users[0].user, 'foo')
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.committers[0].user, 'foo')
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
    @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 930073
        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 e1d70f
        config = {
Pierre-Yves Chibon e1d70f
            'test': ['packager']
Pierre-Yves Chibon e1d70f
        }
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 e1d70f
            new_user='foo',
Pierre-Yves Chibon e1d70f
            user='pingou',
Pierre-Yves Chibon e1d70f
            access='admin',
Pierre-Yves Chibon e1d70f
            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 e1d70f
        self.assertEqual(repo.users[0].user, 'foo')
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.committers[0].user, 'foo')
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
class PagureLibAddUserToProjectWithGrouptests(
Pierre-Yves Chibon e1d70f
        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 e1d70f
            group_name='packager',
Pierre-Yves Chibon e1d70f
            display_name='packager',
Pierre-Yves Chibon e1d70f
            description='The Fedora packager groups',
Pierre-Yves Chibon e1d70f
            group_type='user',
Pierre-Yves Chibon e1d70f
            user='pingou',
Pierre-Yves Chibon e1d70f
            is_admin=False,
Pierre-Yves Chibon e1d70f
            blacklist=[])
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
        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 930073
        group = pagure.lib.query.search_groups(self.session, group_name='packager')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_group(
Pierre-Yves Chibon e1d70f
            self.session,
Pierre-Yves Chibon e1d70f
            username='bar',
Pierre-Yves Chibon e1d70f
            group=group,
Pierre-Yves Chibon e1d70f
            user='pingou',
Pierre-Yves Chibon e1d70f
            is_admin=True)
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
        self.assertEqual(msg, 'User `bar` added to the group `packager`.')
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
    @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 930073
        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 e1d70f
        config = {
Pierre-Yves Chibon e1d70f
            'test': ['packager']
Pierre-Yves Chibon e1d70f
        }
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 e1d70f
            new_user='bar',
Pierre-Yves Chibon e1d70f
            user='pingou',
Pierre-Yves Chibon e1d70f
            access='commit',
Pierre-Yves Chibon e1d70f
            required_groups=config
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 2)
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.users[0].user, 'foo')
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.committers[0].user, 'foo')
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.users[1].user, 'bar')
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.committers[1].user, 'bar')
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
    @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 930073
        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 e1d70f
        config = {
Pierre-Yves Chibon e1d70f
            '*': ['packager']
Pierre-Yves Chibon e1d70f
        }
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 e1d70f
            new_user='bar',
Pierre-Yves Chibon e1d70f
            user='pingou',
Pierre-Yves Chibon e1d70f
            access='commit',
Pierre-Yves Chibon e1d70f
            required_groups=config
Pierre-Yves Chibon e1d70f
        )
Pierre-Yves Chibon e1d70f
        self.session.commit()
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon e1d70f
        self.assertEqual(len(repo.users), 2)
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.users[0].user, 'foo')
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.committers[0].user, 'foo')
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.users[1].user, 'bar')
Pierre-Yves Chibon e1d70f
        self.assertEqual(repo.committers[1].user, 'bar')
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
Pierre-Yves Chibon e1d70f
if __name__ == '__main__':
Pierre-Yves Chibon e1d70f
    unittest.main(verbosity=2)