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