Blame tests/test_pagure_hooks_pagure_hook.py

Pierre-Yves Chibon 7c5e53
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
"""
Pierre-Yves Chibon 7c5e53
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
 Authors:
Pierre-Yves Chibon 7c5e53
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
"""
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
import unittest
Pierre-Yves Chibon 7c5e53
import sys
Pierre-Yves Chibon 7c5e53
import os
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
import mock
Pierre-Yves Chibon 7c5e53
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 7c5e53
Pierre-Yves Chibon 7c5e53
import pagure.hooks.pagure_hook
Pierre-Yves Chibon 7c5e53
import tests
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
class PagureHooksPagureHooktests(tests.SimplePagureTest):
Pierre-Yves Chibon 7c5e53
    """ Tests for pagure.hooks.pagure_hook """
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
    def setUp(self):
Pierre-Yves Chibon 7c5e53
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 7c5e53
        super(PagureHooksPagureHooktests, self).setUp()
Pierre-Yves Chibon 7c5e53
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
        # Add one issue to each projects otherwise we won't be able to find
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 7c5e53
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon 7c5e53
            session=self.session,
Pierre-Yves Chibon 7c5e53
            repo=project,
Pierre-Yves Chibon 73d120
            title="Test issue",
Pierre-Yves Chibon 73d120
            content="We should work on this",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 7c5e53
        )
Pierre-Yves Chibon 7c5e53
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue")
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test2"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon 7c5e53
            session=self.session,
Pierre-Yves Chibon 7c5e53
            repo=project,
Pierre-Yves Chibon 73d120
            title="Test issue on test2",
Pierre-Yves Chibon 73d120
            content="We should work on this, really",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 7c5e53
        )
Pierre-Yves Chibon 7c5e53
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue on test2")
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
        # Create a fork of test for foo with its own ticket
Pierre-Yves Chibon 7c5e53
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 7c5e53
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 7c5e53
            is_fork=True,
Pierre-Yves Chibon 7c5e53
            parent_id=1,
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbccc_foo",
Pierre-Yves Chibon 7c5e53
        )
Pierre-Yves Chibon 7c5e53
        item.close_status = [
Pierre-Yves Chibon 73d120
            "Invalid",
Pierre-Yves Chibon 73d120
            "Insufficient data",
Pierre-Yves Chibon 73d120
            "Fixed",
Pierre-Yves Chibon 73d120
            "Duplicate",
Pierre-Yves Chibon 73d120
        ]
Pierre-Yves Chibon 7c5e53
        self.session.add(item)
Pierre-Yves Chibon 7c5e53
        self.session.commit()
Pierre-Yves Chibon 7c5e53
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="foo"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon 7c5e53
            session=self.session,
Pierre-Yves Chibon 7c5e53
            repo=project,
Pierre-Yves Chibon 73d120
            title="Test issue on fork/foo/test",
Pierre-Yves Chibon 73d120
            content="We should work on this, really",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 7c5e53
        )
Pierre-Yves Chibon 7c5e53
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue on fork/foo/test")
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 73d120
        self.folder = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
        # Add a README to the git repo - First commit
Pierre-Yves Chibon 7c5e53
        tests.add_readme_git_repo(self.folder)
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
    @mock.patch("pagure.hooks.pagure_hook.fixes_relation")
Pierre-Yves Chibon 7c5e53
    def test_generate_revision_change_log_short_url(self, fixes_relation):
Pierre-Yves Chibon 7c5e53
        """ Test generate_revision_change_log when the comment contains
Pierre-Yves Chibon 7c5e53
        a short link to the same project.
Pierre-Yves Chibon 7c5e53
        """
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
        # Add a commit with an url in the commit message
Pierre-Yves Chibon 7c5e53
        tests.add_content_to_git(
Pierre-Yves Chibon 73d120
            self.folder,
Pierre-Yves Chibon 73d120
            branch="master",
Pierre-Yves Chibon 73d120
            filename="sources",
Pierre-Yves Chibon 73d120
            content="foo",
Pierre-Yves Chibon 73d120
            message="Test commit message\n\nFixes #1",
Pierre-Yves Chibon 7c5e53
        )
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
        pagure.hooks.pagure_hook.generate_revision_change_log(
Pierre-Yves Chibon 7c5e53
            session=self.session,
Pierre-Yves Chibon 7c5e53
            project=project,
Pierre-Yves Chibon 7c5e53
            username=None,
Pierre-Yves Chibon 7c5e53
            repodir=self.folder,
Pierre-Yves Chibon 73d120
            new_commits_list=["HEAD"],
Pierre-Yves Chibon 7c5e53
        )
Pierre-Yves Chibon 7c5e53
        fixes_relation.assert_called_once_with(
Pierre-Yves Chibon 73d120
            mock.ANY,
Pierre-Yves Chibon 73d120
            None,
Pierre-Yves Chibon 73d120
            mock.ANY,
Pierre-Yves Chibon 73d120
            project.issues[0],
Pierre-Yves Chibon 73d120
            "http://localhost.localdomain/",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
    @mock.patch("pagure.hooks.pagure_hook.fixes_relation")
Pierre-Yves Chibon 7c5e53
    def test_generate_revision_change_log_full_url(self, fixes_relation):
Pierre-Yves Chibon 7c5e53
        """ Test generate_revision_change_log when the comment contains
Pierre-Yves Chibon 7c5e53
        a full link to another project.
Pierre-Yves Chibon 7c5e53
        """
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
        # Add a commit with an url in the commit message
Pierre-Yves Chibon 7c5e53
        tests.add_content_to_git(
Pierre-Yves Chibon 73d120
            self.folder,
Pierre-Yves Chibon 73d120
            branch="master",
Pierre-Yves Chibon 73d120
            filename="sources",
Pierre-Yves Chibon 73d120
            content="foo",
Pierre-Yves Chibon 73d120
            message="Test commit message\n\n"
Pierre-Yves Chibon 73d120
            "Fixes http://localhost.localdomain/test2/issue/1",
Pierre-Yves Chibon 7c5e53
        )
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 7c5e53
        project2 = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test2"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
        pagure.hooks.pagure_hook.generate_revision_change_log(
Pierre-Yves Chibon 7c5e53
            session=self.session,
Pierre-Yves Chibon 7c5e53
            project=project,
Pierre-Yves Chibon 7c5e53
            username=None,
Pierre-Yves Chibon 7c5e53
            repodir=self.folder,
Pierre-Yves Chibon 73d120
            new_commits_list=["HEAD"],
Pierre-Yves Chibon 7c5e53
        )
Pierre-Yves Chibon 7c5e53
        fixes_relation.assert_called_once_with(
Pierre-Yves Chibon 73d120
            mock.ANY,
Pierre-Yves Chibon 73d120
            None,
Pierre-Yves Chibon 73d120
            mock.ANY,
Pierre-Yves Chibon 73d120
            project2.issues[0],
Pierre-Yves Chibon 73d120
            "http://localhost.localdomain/",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
    @mock.patch("pagure.hooks.pagure_hook.fixes_relation")
Pierre-Yves Chibon 7c5e53
    def test_generate_revision_change_log_full_url_fork(self, fixes_relation):
Pierre-Yves Chibon 7c5e53
        """ Test generate_revision_change_log when the comment contains
Pierre-Yves Chibon 7c5e53
        a full link to a fork.
Pierre-Yves Chibon 7c5e53
        """
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
        # Add a commit with an url in the commit message
Pierre-Yves Chibon 7c5e53
        tests.add_content_to_git(
Pierre-Yves Chibon 73d120
            self.folder,
Pierre-Yves Chibon 73d120
            branch="master",
Pierre-Yves Chibon 73d120
            filename="sources",
Pierre-Yves Chibon 73d120
            content="foo",
Pierre-Yves Chibon 73d120
            message="Test commit message\n\n"
Pierre-Yves Chibon 73d120
            "Fixes http://localhost.localdomain/fork/foo/test/issue/1",
Pierre-Yves Chibon 7c5e53
        )
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 7c5e53
        project_fork = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="foo"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
        pagure.hooks.pagure_hook.generate_revision_change_log(
Pierre-Yves Chibon 7c5e53
            session=self.session,
Pierre-Yves Chibon 7c5e53
            project=project,
Pierre-Yves Chibon 7c5e53
            username=None,
Pierre-Yves Chibon 7c5e53
            repodir=self.folder,
Pierre-Yves Chibon 73d120
            new_commits_list=["HEAD"],
Pierre-Yves Chibon 7c5e53
        )
Pierre-Yves Chibon 7c5e53
        fixes_relation.assert_called_once_with(
Pierre-Yves Chibon 73d120
            mock.ANY,
Pierre-Yves Chibon 73d120
            None,
Pierre-Yves Chibon 73d120
            mock.ANY,
Pierre-Yves Chibon 73d120
            project_fork.issues[0],
Pierre-Yves Chibon 73d120
            "http://localhost.localdomain/",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 7c5e53
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 7c5e53
    unittest.main(verbosity=2)