Blame tests/test_pagure_flask.py

Pierre-Yves Chibon 17b6f6
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 17b6f6
"""
Pierre-Yves Chibon 17b6f6
 (c) 2017 - Copyright Red Hat Inc
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 17b6f6
 Authors:
Pierre-Yves Chibon 17b6f6
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 17b6f6
"""
Pierre-Yves Chibon 17b6f6
Aurélien Bompard dcf6f6
from __future__ import unicode_literals
Aurélien Bompard dcf6f6
Pierre-Yves Chibon 17b6f6
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 17b6f6
import pkg_resources
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 17b6f6
import unittest
Pierre-Yves Chibon 17b6f6
import shutil
Pierre-Yves Chibon 17b6f6
import sys
Pierre-Yves Chibon 17b6f6
import os
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 17b6f6
import mock
Pierre-Yves Chibon a1fca3
import munch
Pierre-Yves Chibon 17b6f6
import pygit2
Pierre-Yves Chibon 17b6f6
import werkzeug
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 17b6f6
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 17b6f6
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 17b6f6
import pagure.lib.model
Pierre-Yves Chibon b130e5
import pagure.utils
Pierre-Yves Chibon 17b6f6
import tests
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon a1fca3
Clement Verna 109c4b
class PagureGetRemoteRepoPath(tests.SimplePagureTest):
Pierre-Yves Chibon 17b6f6
    """ Tests for pagure """
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 17b6f6
    def setUp(self):
Pierre-Yves Chibon 17b6f6
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 17b6f6
        super(PagureGetRemoteRepoPath, self).setUp()
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 17b6f6
        tests.create_projects(self.session)
Pierre-Yves Chibon 17b6f6
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 17b6f6
        tests.add_content_git_repo(os.path.join(self.path, 'repos', 'test2.git'))
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 17b6f6
    @mock.patch(
Pierre-Yves Chibon 17b6f6
        'pagure.lib.repo.PagureRepo.pull',
Pierre-Yves Chibon 17b6f6
        mock.MagicMock(side_effect=pygit2.GitError))
Pierre-Yves Chibon 17b6f6
    def test_passing(self):
Pierre-Yves Chibon 17b6f6
        """ Test get_remote_repo_path in pagure. """
Pierre-Yves Chibon b130e5
        output = pagure.utils.get_remote_repo_path(
Patrick Uiterwijk 7d2547
            os.path.join(self.path, 'repos', 'test2.git'), 'master',
Patrick Uiterwijk 7d2547
            ignore_non_exist=True)
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 17b6f6
        self.assertTrue(output.endswith('repos_test2.git_master'))
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon a1fca3
    def test_is_repo_committer_logged_out(self):
Pierre-Yves Chibon a1fca3
        """ Test is_repo_committer in pagure when there is no logged in user.
Pierre-Yves Chibon a1fca3
        """
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Aurélien Bompard 626417
        with self.app.application.app_context():
Aurélien Bompard 626417
            output = pagure.utils.is_repo_committer(repo)
Pierre-Yves Chibon a1fca3
        self.assertFalse(output)
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
    def test_is_repo_committer_logged_in(self):
Pierre-Yves Chibon a1fca3
        """ Test is_repo_committer in pagure with the appropriate user logged
Pierre-Yves Chibon a1fca3
        in. """
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
        g = munch.Munch()
Pierre-Yves Chibon a1fca3
        g.fas_user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 392277
        g.authenticated = True
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 695f8c
        with mock.patch('flask.g', g):
Pierre-Yves Chibon b130e5
            output = pagure.utils.is_repo_committer(repo)
Pierre-Yves Chibon a1fca3
            self.assertTrue(output)
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon 8bba77
    def test_is_repo_committer_logged_in_in_group(self):
Pierre-Yves Chibon 8bba77
        """ Test is_repo_committer in pagure with the appropriate user logged
Pierre-Yves Chibon 8bba77
        in. """
Pierre-Yves Chibon 8bba77
        # Create group
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group(
Pierre-Yves Chibon 8bba77
            self.session,
Pierre-Yves Chibon 8bba77
            group_name='packager',
Pierre-Yves Chibon 8bba77
            display_name='packager',
Pierre-Yves Chibon 8bba77
            description='The Fedora packager groups',
Pierre-Yves Chibon 8bba77
            group_type='user',
Pierre-Yves Chibon 8bba77
            user='pingou',
Pierre-Yves Chibon 8bba77
            is_admin=False,
Pierre-Yves Chibon 8bba77
            blacklist=[])
Pierre-Yves Chibon 8bba77
        self.session.commit()
Pierre-Yves Chibon 8bba77
        self.assertEqual(msg, 'User `pingou` added to the group `packager`.')
Pierre-Yves Chibon 8bba77
Pierre-Yves Chibon 8bba77
        # 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 8bba77
            self.session,
Pierre-Yves Chibon 8bba77
            username='foo',
Pierre-Yves Chibon 8bba77
            group=group,
Pierre-Yves Chibon 8bba77
            user='pingou',
Pierre-Yves Chibon 8bba77
            is_admin=True)
Pierre-Yves Chibon 8bba77
        self.session.commit()
Pierre-Yves Chibon 8bba77
        self.assertEqual(msg, 'User `foo` added to the group `packager`.')
Pierre-Yves Chibon 8bba77
Pierre-Yves Chibon 8bba77
        # Add group packager to project test
Pierre-Yves Chibon 930073
        project = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon 8bba77
            self.session,
Pierre-Yves Chibon 8bba77
            project=project,
Pierre-Yves Chibon 8bba77
            new_group='packager',
Pierre-Yves Chibon 8bba77
            user='pingou',
Pierre-Yves Chibon 8bba77
        )
Pierre-Yves Chibon 8bba77
        self.session.commit()
Pierre-Yves Chibon 8bba77
        self.assertEqual(msg, 'Group added')
Pierre-Yves Chibon 8bba77
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 8bba77
Pierre-Yves Chibon 8bba77
        g = munch.Munch()
Pierre-Yves Chibon 8bba77
        g.fas_user = tests.FakeUser(username='foo')
Pierre-Yves Chibon 8bba77
        g.authenticated = True
Pierre-Yves Chibon 8bba77
        g.session = self.session
Pierre-Yves Chibon 8bba77
        with mock.patch('flask.g', g):
Pierre-Yves Chibon 8bba77
            output = pagure.utils.is_repo_committer(repo)
Pierre-Yves Chibon 8bba77
            self.assertTrue(output)
Pierre-Yves Chibon 8bba77
Pierre-Yves Chibon 7dbcb0
    def test_is_repo_committer_logged_in_in_ticket_group(self):
Pierre-Yves Chibon 7dbcb0
        """ Test is_repo_committer in pagure with the appropriate user logged
Pierre-Yves Chibon 7dbcb0
        in. """
Pierre-Yves Chibon 7dbcb0
        # Create group
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group(
Pierre-Yves Chibon 7dbcb0
            self.session,
Pierre-Yves Chibon 7dbcb0
            group_name='packager',
Pierre-Yves Chibon 7dbcb0
            display_name='packager',
Pierre-Yves Chibon 7dbcb0
            description='The Fedora packager groups',
Pierre-Yves Chibon 7dbcb0
            group_type='user',
Pierre-Yves Chibon 7dbcb0
            user='pingou',
Pierre-Yves Chibon 7dbcb0
            is_admin=False,
Pierre-Yves Chibon 7dbcb0
            blacklist=[])
Pierre-Yves Chibon 7dbcb0
        self.session.commit()
Pierre-Yves Chibon 7dbcb0
        self.assertEqual(msg, 'User `pingou` added to the group `packager`.')
Pierre-Yves Chibon 7dbcb0
Pierre-Yves Chibon 7dbcb0
        # 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 7dbcb0
            self.session,
Pierre-Yves Chibon 7dbcb0
            username='foo',
Pierre-Yves Chibon 7dbcb0
            group=group,
Pierre-Yves Chibon 7dbcb0
            user='pingou',
Pierre-Yves Chibon 7dbcb0
            is_admin=True)
Pierre-Yves Chibon 7dbcb0
        self.session.commit()
Pierre-Yves Chibon 7dbcb0
        self.assertEqual(msg, 'User `foo` added to the group `packager`.')
Pierre-Yves Chibon 7dbcb0
Pierre-Yves Chibon 7dbcb0
        # Add group packager to project test
Pierre-Yves Chibon 930073
        project = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon 7dbcb0
            self.session,
Pierre-Yves Chibon 7dbcb0
            project=project,
Pierre-Yves Chibon 7dbcb0
            new_group='packager',
Pierre-Yves Chibon 7dbcb0
            user='pingou',
Pierre-Yves Chibon 7dbcb0
            access='ticket',
Pierre-Yves Chibon 7dbcb0
        )
Pierre-Yves Chibon 7dbcb0
        self.session.commit()
Pierre-Yves Chibon 7dbcb0
        self.assertEqual(msg, 'Group added')
Pierre-Yves Chibon 7dbcb0
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 7dbcb0
Pierre-Yves Chibon 7dbcb0
        g = munch.Munch()
Pierre-Yves Chibon 7dbcb0
        g.fas_user = tests.FakeUser(username='foo')
Pierre-Yves Chibon 7dbcb0
        g.authenticated = True
Pierre-Yves Chibon 7dbcb0
        g.session = self.session
Pierre-Yves Chibon 7dbcb0
        with mock.patch('flask.g', g):
Pierre-Yves Chibon 7dbcb0
            output = pagure.utils.is_repo_committer(repo)
Pierre-Yves Chibon 7dbcb0
            self.assertFalse(output)
Pierre-Yves Chibon 7dbcb0
Pierre-Yves Chibon a1fca3
    def test_is_repo_committer_logged_in_wrong_user(self):
Pierre-Yves Chibon a1fca3
        """ Test is_repo_committer in pagure with the wrong user logged in.
Pierre-Yves Chibon a1fca3
        """
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
        g = munch.Munch()
Pierre-Yves Chibon a1fca3
        g.fas_user = tests.FakeUser()
Pierre-Yves Chibon 392277
        g.authenticated = True
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 695f8c
        with mock.patch('flask.g', g):
Pierre-Yves Chibon b130e5
            output = pagure.utils.is_repo_committer(repo)
Pierre-Yves Chibon a1fca3
            self.assertFalse(output)
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
    # Mocked config
Pierre-Yves Chibon a1fca3
    config = {
Pierre-Yves Chibon a1fca3
        'provenpackager': {}
Pierre-Yves Chibon a1fca3
    }
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon b130e5
    @mock.patch.dict('pagure.config.config', {'EXTERNAL_COMMITTER': config})
Pierre-Yves Chibon a1fca3
    def test_is_repo_committer_external_committer_generic_no_member(self):
Pierre-Yves Chibon a1fca3
        """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
Pierre-Yves Chibon a1fca3
        configured to give access to all the provenpackager, but the user
Pierre-Yves Chibon a1fca3
        is not one.
Pierre-Yves Chibon a1fca3
        """
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
        user = tests.FakeUser()
Pierre-Yves Chibon a1fca3
        g = munch.Munch()
Pierre-Yves Chibon a1fca3
        g.fas_user = user
Pierre-Yves Chibon 392277
        g.authenticated = True
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 695f8c
        with mock.patch('flask.g', g):
Pierre-Yves Chibon b130e5
            output = pagure.utils.is_repo_committer(repo)
Pierre-Yves Chibon a1fca3
            self.assertFalse(output)
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon b130e5
    @mock.patch.dict('pagure.config.config', {'EXTERNAL_COMMITTER': config})
Pierre-Yves Chibon a1fca3
    def test_is_repo_committer_external_committer_generic_member(self):
Pierre-Yves Chibon a1fca3
        """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
Pierre-Yves Chibon a1fca3
        configured to give access to all the provenpackager, and the user
Pierre-Yves Chibon a1fca3
        is one
Pierre-Yves Chibon a1fca3
        """
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
        g = munch.Munch()
Pierre-Yves Chibon 695f8c
        g.fas_user = tests.FakeUser(username='foo')
Pierre-Yves Chibon a1fca3
        g.fas_user.groups.append('provenpackager')
Pierre-Yves Chibon 392277
        g.authenticated = True
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 695f8c
        with mock.patch('flask.g', g):
Pierre-Yves Chibon b130e5
            output = pagure.utils.is_repo_committer(repo)
Pierre-Yves Chibon a1fca3
            self.assertTrue(output)
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
    config = {
Pierre-Yves Chibon a1fca3
        'provenpackager': {
Pierre-Yves Chibon a1fca3
            'exclude': ['test']
Pierre-Yves Chibon a1fca3
        }
Pierre-Yves Chibon a1fca3
    }
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon b130e5
    @mock.patch.dict('pagure.config.config', {'EXTERNAL_COMMITTER': config})
Pierre-Yves Chibon a1fca3
    def test_is_repo_committer_external_committer_excluding_one(self):
Pierre-Yves Chibon a1fca3
        """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
Pierre-Yves Chibon a1fca3
        configured to give access to all the provenpackager but for this
Pierre-Yves Chibon a1fca3
        one repo
Pierre-Yves Chibon a1fca3
        """
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
        g = munch.Munch()
Pierre-Yves Chibon a1fca3
        g.fas_user = tests.FakeUser()
Pierre-Yves Chibon a1fca3
        g.fas_user.groups.append('provenpackager')
Pierre-Yves Chibon 392277
        g.authenticated = True
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 695f8c
        with mock.patch('flask.g', g):
Pierre-Yves Chibon b130e5
            output = pagure.utils.is_repo_committer(repo)
Pierre-Yves Chibon a1fca3
            self.assertFalse(output)
Pierre-Yves Chibon a1fca3
Patrick Uiterwijk 9c3eac
    @mock.patch.dict('pagure.config.config', {'EXTERNAL_COMMITTER': config})
Patrick Uiterwijk 9c3eac
    def test_is_repo_committer_owner_external_committer_excluding_one(self):
Patrick Uiterwijk 9c3eac
        """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
Patrick Uiterwijk 9c3eac
        configured to give access to all the provenpackager but for this
Patrick Uiterwijk 9c3eac
        one repo, but the user is still a direct committer
Patrick Uiterwijk 9c3eac
        """
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Patrick Uiterwijk 9c3eac
Patrick Uiterwijk 9c3eac
        g = munch.Munch()
Patrick Uiterwijk 9c3eac
        g.fas_user = tests.FakeUser(username='pingou')
Patrick Uiterwijk 9c3eac
        g.fas_user.groups.append('provenpackager')
Patrick Uiterwijk 9c3eac
        g.authenticated = True
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 695f8c
        with mock.patch('flask.g', g):
Patrick Uiterwijk 9c3eac
            output = pagure.utils.is_repo_committer(repo)
Patrick Uiterwijk 9c3eac
            self.assertTrue(output)
Patrick Uiterwijk 9c3eac
Pierre-Yves Chibon a1fca3
    config = {
Pierre-Yves Chibon a1fca3
        'provenpackager': {
Pierre-Yves Chibon a1fca3
            'restrict': ['test']
Pierre-Yves Chibon a1fca3
        }
Pierre-Yves Chibon a1fca3
    }
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon b130e5
    @mock.patch.dict('pagure.config.config', {'EXTERNAL_COMMITTER': config})
Pierre-Yves Chibon a1fca3
    def test_is_repo_committer_external_committer_restricted_not_member(self):
Pierre-Yves Chibon a1fca3
        """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
Pierre-Yves Chibon a1fca3
        configured to give access the provenpackager just for one repo
Pierre-Yves Chibon a1fca3
        """
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
        g = munch.Munch()
Pierre-Yves Chibon a1fca3
        g.fas_user = tests.FakeUser()
Pierre-Yves Chibon 392277
        g.authenticated = True
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 695f8c
        with mock.patch('flask.g', g):
Pierre-Yves Chibon b130e5
            output = pagure.utils.is_repo_committer(repo)
Pierre-Yves Chibon a1fca3
            self.assertFalse(output)
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon b130e5
    @mock.patch.dict('pagure.config.config', {'EXTERNAL_COMMITTER': config})
Pierre-Yves Chibon a1fca3
    def test_is_repo_committer_external_committer_restricting_to_one(self):
Pierre-Yves Chibon a1fca3
        """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
Pierre-Yves Chibon a1fca3
        configured to give access the provenpackager just for one repo
Pierre-Yves Chibon a1fca3
        """
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
        g = munch.Munch()
Pierre-Yves Chibon 695f8c
        g.fas_user = tests.FakeUser(username='foo')
Pierre-Yves Chibon 392277
        g.authenticated = True
Pierre-Yves Chibon a1fca3
        g.fas_user.groups.append('provenpackager')
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 695f8c
        with mock.patch('flask.g', g):
Pierre-Yves Chibon b130e5
            output = pagure.utils.is_repo_committer(repo)
Pierre-Yves Chibon a1fca3
            self.assertTrue(output)
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon b130e5
    @mock.patch.dict('pagure.config.config', {'EXTERNAL_COMMITTER': config})
Pierre-Yves Chibon a1fca3
    def test_is_repo_committer_external_committer_restricting_another_one(self):
Pierre-Yves Chibon a1fca3
        """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
Pierre-Yves Chibon a1fca3
        configured to give access the provenpackager just for one repo not
Pierre-Yves Chibon a1fca3
        this one
Pierre-Yves Chibon a1fca3
        """
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test2')
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
        g = munch.Munch()
Pierre-Yves Chibon 695f8c
        g.fas_user = tests.FakeUser(username='foo')
Pierre-Yves Chibon 392277
        g.authenticated = True
Pierre-Yves Chibon a1fca3
        g.fas_user.groups.append('provenpackager')
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 695f8c
        with mock.patch('flask.g', g):
Pierre-Yves Chibon b130e5
            output = pagure.utils.is_repo_committer(repo)
Pierre-Yves Chibon a1fca3
            self.assertFalse(output)
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 17b6f6
if __name__ == '__main__':
Pierre-Yves Chibon 78e6f4
    unittest.main(verbosity=2)