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
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
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 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 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 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test2.git")
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 17b6f6
    @mock.patch(
Pierre-Yves Chibon 73d120
        "pagure.lib.repo.PagureRepo.pull",
Pierre-Yves Chibon 73d120
        mock.MagicMock(side_effect=pygit2.GitError),
Pierre-Yves Chibon 73d120
    )
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(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test2.git"),
Pierre-Yves Chibon 73d120
            "master",
Pierre-Yves Chibon 73d120
            ignore_non_exist=True,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 17b6f6
Pierre-Yves Chibon 73d120
        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 73d120
        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 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
        g = munch.Munch()
Pierre-Yves Chibon 73d120
        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 73d120
        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 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 8bba77
            is_admin=False,
Pierre-Yves Chibon 73d120
            blacklist=[],
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 8bba77
        self.session.commit()
Pierre-Yves Chibon 73d120
        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 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 8bba77
            self.session,
Pierre-Yves Chibon 73d120
            username="foo",
Pierre-Yves Chibon 8bba77
            group=group,
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            is_admin=True,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 8bba77
        self.session.commit()
Pierre-Yves Chibon 73d120
        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 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon 73d120
            self.session, project=project, new_group="packager", user="pingou"
Pierre-Yves Chibon 8bba77
        )
Pierre-Yves Chibon 8bba77
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Group added")
Pierre-Yves Chibon 8bba77
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 8bba77
Pierre-Yves Chibon 8bba77
        g = munch.Munch()
Pierre-Yves Chibon 73d120
        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 73d120
        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 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 7dbcb0
            is_admin=False,
Pierre-Yves Chibon 73d120
            blacklist=[],
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7dbcb0
        self.session.commit()
Pierre-Yves Chibon 73d120
        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 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 7dbcb0
            self.session,
Pierre-Yves Chibon 73d120
            username="foo",
Pierre-Yves Chibon 7dbcb0
            group=group,
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            is_admin=True,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7dbcb0
        self.session.commit()
Pierre-Yves Chibon 73d120
        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 73d120
        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 73d120
            new_group="packager",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="ticket",
Pierre-Yves Chibon 7dbcb0
        )
Pierre-Yves Chibon 7dbcb0
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Group added")
Pierre-Yves Chibon 7dbcb0
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 7dbcb0
Pierre-Yves Chibon 7dbcb0
        g = munch.Munch()
Pierre-Yves Chibon 73d120
        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 73d120
        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 73d120
        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 73d120
        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 73d120
    config = {"provenpackager": {}}
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon 73d120
    @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 73d120
        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 73d120
        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 73d120
    @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 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
        g = munch.Munch()
Pierre-Yves Chibon 73d120
        g.fas_user = tests.FakeUser(username="foo")
Pierre-Yves Chibon 73d120
        g.fas_user.groups.append("provenpackager")
Pierre-Yves Chibon 392277
        g.authenticated = True
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 73d120
        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 73d120
    config = {"provenpackager": {"exclude": ["test"]}}
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon 73d120
    @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 73d120
        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 73d120
        g.fas_user.groups.append("provenpackager")
Pierre-Yves Chibon 392277
        g.authenticated = True
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 73d120
        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 73d120
    @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 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Patrick Uiterwijk 9c3eac
Patrick Uiterwijk 9c3eac
        g = munch.Munch()
Pierre-Yves Chibon 73d120
        g.fas_user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 73d120
        g.fas_user.groups.append("provenpackager")
Patrick Uiterwijk 9c3eac
        g.authenticated = True
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 73d120
        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 73d120
    config = {"provenpackager": {"restrict": ["test"]}}
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon 73d120
    @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 73d120
        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 73d120
        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 73d120
    @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 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
        g = munch.Munch()
Pierre-Yves Chibon 73d120
        g.fas_user = tests.FakeUser(username="foo")
Pierre-Yves Chibon 392277
        g.authenticated = True
Pierre-Yves Chibon 73d120
        g.fas_user.groups.append("provenpackager")
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 73d120
        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 73d120
    @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
Pierre-Yves Chibon 73d120
    def test_is_repo_committer_external_committer_restricting_another_one(
Pierre-Yves Chibon 73d120
        self
Pierre-Yves Chibon 73d120
    ):
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 73d120
        repo = pagure.lib.query._get_project(self.session, "test2")
Pierre-Yves Chibon a1fca3
Pierre-Yves Chibon a1fca3
        g = munch.Munch()
Pierre-Yves Chibon 73d120
        g.fas_user = tests.FakeUser(username="foo")
Pierre-Yves Chibon 392277
        g.authenticated = True
Pierre-Yves Chibon 73d120
        g.fas_user.groups.append("provenpackager")
Pierre-Yves Chibon 695f8c
        g.session = self.session
Pierre-Yves Chibon 73d120
        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 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 78e6f4
    unittest.main(verbosity=2)