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 17b6f6
import pagure.lib
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 a1fca3
        repo = pagure.lib._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 a1fca3
        repo = pagure.lib._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 b130e5
        with mock.patch('pagure.flask_app.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
    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 a1fca3
        repo = pagure.lib._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 b130e5
        with mock.patch('pagure.flask_app.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 a1fca3
        repo = pagure.lib._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 b130e5
        with mock.patch('pagure.flask_app.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 a1fca3
        repo = pagure.lib._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 b130e5
        with mock.patch('pagure.flask_app.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 a1fca3
        repo = pagure.lib._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 b130e5
        with mock.patch('pagure.flask_app.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
        """
Patrick Uiterwijk 9c3eac
        repo = pagure.lib._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
Patrick Uiterwijk 9c3eac
        with mock.patch('pagure.flask_app.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 a1fca3
        repo = pagure.lib._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 b130e5
        with mock.patch('pagure.flask_app.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 a1fca3
        repo = pagure.lib._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 a1fca3
        g.fas_user.groups.append('provenpackager')
Pierre-Yves Chibon b130e5
        with mock.patch('pagure.flask_app.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 a1fca3
        repo = pagure.lib._get_project(self.session, 'test2')
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 a1fca3
        g.fas_user.groups.append('provenpackager')
Pierre-Yves Chibon b130e5
        with mock.patch('pagure.flask_app.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
Patrick Uiterwijk 9c3eac
Pierre-Yves Chibon 17b6f6
if __name__ == '__main__':
Pierre-Yves Chibon 78e6f4
    unittest.main(verbosity=2)