Blame tests/test_pagure_lib_task_services.py

Pierre-Yves Chibon 941093
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
"""
Pierre-Yves Chibon 941093
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
 Authors:
Pierre-Yves Chibon 941093
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
"""
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
import datetime
Pierre-Yves Chibon 941093
import os
Pierre-Yves Chibon 941093
import shutil
Pierre-Yves Chibon 941093
import sys
Pierre-Yves Chibon 941093
import tempfile
Pierre-Yves Chibon 941093
import time
Pierre-Yves Chibon 941093
import unittest
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
import pygit2
Pierre-Yves Chibon 941093
import six
Pierre-Yves Chibon 941093
from mock import ANY, patch, MagicMock, call
Pierre-Yves Chibon 941093
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 941093
Pierre-Yves Chibon 930073
import pagure.lib.tasks_services
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 941093
import tests
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
import pagure.lib.tasks_services
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
class PagureLibTaskServicestests(tests.Modeltests):
Pierre-Yves Chibon 941093
    """ Tests for pagure.lib.task_services """
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
    maxDiff = None
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
    def setUp(self):
Pierre-Yves Chibon 941093
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 941093
        super(PagureLibTaskServicestests, self).setUp()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        tests.create_projects(self.session)
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        # Create a fork of test for foo
Pierre-Yves Chibon 941093
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 941093
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 941093
            is_fork=True,
Pierre-Yves Chibon 941093
            parent_id=1,
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbccc_foo",
Pierre-Yves Chibon 941093
        )
Pierre-Yves Chibon 73d120
        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 941093
        self.session.add(item)
Pierre-Yves Chibon 941093
        self.session.commit()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
    def test_webhook_notification_invalid_project(self):
Pierre-Yves Chibon 941093
        """ Test the webhook_notification method. """
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        self.assertRaises(
Pierre-Yves Chibon 941093
            RuntimeError,
Pierre-Yves Chibon 941093
            pagure.lib.tasks_services.webhook_notification,
Pierre-Yves Chibon 73d120
            topic="topic",
Pierre-Yves Chibon 73d120
            msg={"payload": ["a", "b", "c"]},
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            name="invalid",
Pierre-Yves Chibon 73d120
            user=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.call_web_hooks")
Pierre-Yves Chibon 941093
    def test_webhook_notification_no_webhook(self, call_wh):
Pierre-Yves Chibon 941093
        """ Test the webhook_notification method. """
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.webhook_notification(
Pierre-Yves Chibon 73d120
            topic="topic",
Pierre-Yves Chibon 73d120
            msg={"payload": ["a", "b", "c"]},
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            user=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        call_wh.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.log_commits_to_db")
Pierre-Yves Chibon 941093
    def test_log_commit_send_notifications_invalid_project(self, log):
Pierre-Yves Chibon 941093
        """ Test the log_commit_send_notifications method. """
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.log_commit_send_notifications(
Pierre-Yves Chibon 73d120
            name="invalid",
Pierre-Yves Chibon 941093
            commits=[],
Pierre-Yves Chibon 941093
            abspath=None,
Pierre-Yves Chibon 941093
            branch=None,
Pierre-Yves Chibon 941093
            default_branch=None,
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            username=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        log.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.notify_new_commits")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.log_commits_to_db")
Pierre-Yves Chibon 941093
    def test_log_commit_send_notifications_valid_project(self, log, notif):
Pierre-Yves Chibon 941093
        """ Test the log_commit_send_notifications method. """
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.log_commit_send_notifications(
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            commits=["hash1", "hash2"],
Pierre-Yves Chibon 73d120
            abspath="/path/to/git",
Pierre-Yves Chibon 73d120
            branch="master",
Pierre-Yves Chibon 73d120
            default_branch="master",
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            username=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        log.assert_called_once_with(
Pierre-Yves Chibon 73d120
            ANY, ANY, ["hash1", "hash2"], "/path/to/git"
Pierre-Yves Chibon 941093
        )
Pierre-Yves Chibon 941093
        notif.assert_called_once_with(
Pierre-Yves Chibon 73d120
            "/path/to/git", ANY, "master", ["hash1", "hash2"]
Pierre-Yves Chibon 941093
        )
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.trigger_jenkins_build")
Pierre-Yves Chibon 941093
    def test_trigger_ci_build_invalid_project(self, trigger_jenk):
Pierre-Yves Chibon 941093
        """ Test the trigger_ci_build method. """
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.trigger_ci_build(
Pierre-Yves Chibon 73d120
            project_name="invalid",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            ci_type="jenkins",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        trigger_jenk.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.trigger_jenkins_build")
Pierre-Yves Chibon 941093
    def test_trigger_ci_build_not_configured_project(self, trigger_jenk):
Pierre-Yves Chibon 941093
        """ Test the trigger_ci_build method. """
Pierre-Yves Chibon 941093
        self.assertRaises(
Pierre-Yves Chibon 941093
            pagure.exceptions.PagureException,
Pierre-Yves Chibon 941093
            pagure.lib.tasks_services.trigger_ci_build,
Pierre-Yves Chibon 73d120
            project_name="test",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            ci_type="jenkins",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        trigger_jenk.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.trigger_jenkins_build")
Pierre-Yves Chibon 941093
    def test_trigger_ci_build_not_configured_project_fork(self, trigger_jenk):
Pierre-Yves Chibon 941093
        """ Test the trigger_ci_build method. """
Pierre-Yves Chibon 941093
        self.assertRaises(
Pierre-Yves Chibon 941093
            pagure.exceptions.PagureException,
Pierre-Yves Chibon 941093
            pagure.lib.tasks_services.trigger_ci_build,
Pierre-Yves Chibon 73d120
            project_name="forks/foo/test",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            ci_type="jenkins",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        trigger_jenk.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.query._get_project")
Pierre-Yves Chibon 941093
    def test_load_json_commits_to_db_invalid_data_type(self, get_project):
Pierre-Yves Chibon 941093
        """ Test the load_json_commits_to_db method. """
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.load_json_commits_to_db(
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            commits=["hash1", "hash2"],
Pierre-Yves Chibon 73d120
            abspath="/path/to/git",
Pierre-Yves Chibon 73d120
            data_type="invalid",
Pierre-Yves Chibon 73d120
            agent="pingou",
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            username=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        get_project.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.get_files_to_load")
Pierre-Yves Chibon 941093
    def test_load_json_commits_to_db_invalid_project(self, get_files):
Pierre-Yves Chibon 941093
        """ Test the load_json_commits_to_db method. """
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.load_json_commits_to_db(
Pierre-Yves Chibon 73d120
            name="invalid",
Pierre-Yves Chibon 73d120
            commits=["hash1", "hash2"],
Pierre-Yves Chibon 73d120
            abspath="/path/to/git",
Pierre-Yves Chibon 73d120
            data_type="ticket",
Pierre-Yves Chibon 73d120
            agent="pingou",
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            username=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        get_files.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_request_from_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_ticket_from_git")
Pierre-Yves Chibon 941093
    def test_load_json_commits_to_db_invalid_path(self, up_issue, up_pr):
Pierre-Yves Chibon 941093
        """ Test the load_json_commits_to_db method. """
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.load_json_commits_to_db(
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            commits=["hash1", "hash2"],
Pierre-Yves Chibon 941093
            abspath=self.path,
Pierre-Yves Chibon 73d120
            data_type="ticket",
Pierre-Yves Chibon 73d120
            agent="pingou",
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            username=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        up_issue.assert_not_called()
Pierre-Yves Chibon 941093
        up_pr.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_request_from_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_ticket_from_git")
Pierre-Yves Chibon 73d120
    def test_load_json_commits_to_db_invalid_path_one_commit(
Pierre-Yves Chibon 73d120
        self, up_issue, up_pr
Pierre-Yves Chibon 73d120
    ):
Pierre-Yves Chibon 941093
        """ Test the load_json_commits_to_db method. """
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.load_json_commits_to_db(
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            commits=["hash1"],
Pierre-Yves Chibon 941093
            abspath=self.path,
Pierre-Yves Chibon 73d120
            data_type="ticket",
Pierre-Yves Chibon 73d120
            agent="pingou",
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            username=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        up_issue.assert_not_called()
Pierre-Yves Chibon 941093
        up_pr.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_request_from_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_ticket_from_git")
Pierre-Yves Chibon 941093
    def test_load_json_commits_to_db_no_agent(self, up_issue, up_pr, send):
Pierre-Yves Chibon 941093
        """ Test the load_json_commits_to_db method. """
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.load_json_commits_to_db(
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 941093
            commits=[],
Pierre-Yves Chibon 941093
            abspath=None,
Pierre-Yves Chibon 73d120
            data_type="ticket",
Pierre-Yves Chibon 941093
            agent=None,
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            username=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        up_issue.assert_not_called()
Pierre-Yves Chibon 941093
        up_pr.assert_not_called()
Pierre-Yves Chibon 941093
        send.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_request_from_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_ticket_from_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.read_git_lines")
Pierre-Yves Chibon 941093
    def test_load_json_commits_to_db_no_agent(
Pierre-Yves Chibon 73d120
        self, git, up_issue, up_pr, send
Pierre-Yves Chibon 73d120
    ):
Pierre-Yves Chibon 941093
        """ Test the load_json_commits_to_db method. """
Pierre-Yves Chibon 73d120
        git.side_effect = [["file1"], ["file2"], ["files/image"], ["file1"]]
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.load_json_commits_to_db(
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            commits=["hash1", "hash2"],
Pierre-Yves Chibon 941093
            abspath=self.path,
Pierre-Yves Chibon 73d120
            data_type="ticket",
Pierre-Yves Chibon 941093
            agent=None,
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            username=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        up_issue.assert_not_called()
Pierre-Yves Chibon 941093
        up_pr.assert_not_called()
Pierre-Yves Chibon 941093
        send.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("json.loads")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_request_from_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_ticket_from_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.read_git_lines")
Pierre-Yves Chibon 941093
    def test_load_json_commits_to_db_tickets(
Pierre-Yves Chibon 73d120
        self, git, up_issue, up_pr, send, json_loads
Pierre-Yves Chibon 73d120
    ):
Pierre-Yves Chibon 941093
        """ Test the load_json_commits_to_db method. """
Pierre-Yves Chibon 73d120
        git.side_effect = [["file1"], ["file2"], ["files/image"], ["file1"]]
Pierre-Yves Chibon 73d120
        json_loads.return_value = "foobar"
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.load_json_commits_to_db(
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            commits=["hash1", "hash2"],
Pierre-Yves Chibon 941093
            abspath=self.path,
Pierre-Yves Chibon 73d120
            data_type="ticket",
Pierre-Yves Chibon 941093
            agent=None,
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            username=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        calls = [
Pierre-Yves Chibon 941093
            call(
Pierre-Yves Chibon 73d120
                ANY,
Pierre-Yves Chibon 73d120
                agent=None,
Pierre-Yves Chibon 73d120
                issue_uid="file1",
Pierre-Yves Chibon 73d120
                json_data="foobar",
Pierre-Yves Chibon 73d120
                namespace=None,
Pierre-Yves Chibon 73d120
                reponame="test",
Pierre-Yves Chibon 73d120
                username=None,
Pierre-Yves Chibon 941093
            ),
Pierre-Yves Chibon 941093
            call(
Pierre-Yves Chibon 73d120
                ANY,
Pierre-Yves Chibon 73d120
                agent=None,
Pierre-Yves Chibon 73d120
                issue_uid="file2",
Pierre-Yves Chibon 73d120
                json_data="foobar",
Pierre-Yves Chibon 73d120
                namespace=None,
Pierre-Yves Chibon 73d120
                reponame="test",
Pierre-Yves Chibon 73d120
                username=None,
Pierre-Yves Chibon 941093
            ),
Pierre-Yves Chibon 941093
        ]
Pierre-Yves Chibon 73d120
        self.assertEqual(calls, up_issue.mock_calls)
Pierre-Yves Chibon 941093
        up_pr.assert_not_called()
Pierre-Yves Chibon 941093
        send.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("json.loads")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_request_from_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_ticket_from_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.read_git_lines")
Pierre-Yves Chibon 941093
    def test_load_json_commits_to_db_prs(
Pierre-Yves Chibon 73d120
        self, git, up_issue, up_pr, send, json_loads
Pierre-Yves Chibon 73d120
    ):
Pierre-Yves Chibon 941093
        """ Test the load_json_commits_to_db method. """
Pierre-Yves Chibon 73d120
        git.side_effect = [["file1"], ["file2"], ["files/image"], ["file1"]]
Pierre-Yves Chibon 73d120
        json_loads.return_value = "foobar"
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.load_json_commits_to_db(
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            commits=["hash1", "hash2"],
Pierre-Yves Chibon 941093
            abspath=self.path,
Pierre-Yves Chibon 73d120
            data_type="pull-request",
Pierre-Yves Chibon 73d120
            agent="pingou",
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            username=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        calls = [
Pierre-Yves Chibon 941093
            call(
Pierre-Yves Chibon 73d120
                ANY,
Pierre-Yves Chibon 73d120
                json_data="foobar",
Pierre-Yves Chibon 73d120
                namespace=None,
Pierre-Yves Chibon 73d120
                reponame="test",
Pierre-Yves Chibon 73d120
                request_uid="file1",
Pierre-Yves Chibon 73d120
                username=None,
Pierre-Yves Chibon 941093
            ),
Pierre-Yves Chibon 941093
            call(
Pierre-Yves Chibon 73d120
                ANY,
Pierre-Yves Chibon 73d120
                json_data="foobar",
Pierre-Yves Chibon 73d120
                namespace=None,
Pierre-Yves Chibon 73d120
                reponame="test",
Pierre-Yves Chibon 73d120
                request_uid="file2",
Pierre-Yves Chibon 73d120
                username=None,
Pierre-Yves Chibon 941093
            ),
Pierre-Yves Chibon 941093
        ]
Pierre-Yves Chibon 941093
        up_issue.assert_not_called()
Pierre-Yves Chibon 73d120
        self.assertEqual(calls, up_pr.mock_calls)
Pierre-Yves Chibon 941093
        calls = [
Pierre-Yves Chibon 941093
            call(
Pierre-Yves Chibon 73d120
                "Good Morning\n\n"
Pierre-Yves Chibon 73d120
                "This is the log of loading all the files pushed in the git "
Pierre-Yves Chibon 73d120
                "repo into\n"
Pierre-Yves Chibon 73d120
                "the database. It should ignore files that are not JSON files,"
Pierre-Yves Chibon 73d120
                " this\nis fine.\n\n"
Pierre-Yves Chibon 73d120
                "Loading: file1 -- 1/2 ... ... Done\n"
Pierre-Yves Chibon 73d120
                "Loading: file2 -- 2/2 ... ... Done",
Pierre-Yves Chibon 73d120
                "Issue import report",
Pierre-Yves Chibon 73d120
                "bar@pingou.com",
Pierre-Yves Chibon 941093
            )
Pierre-Yves Chibon 941093
        ]
Pierre-Yves Chibon 73d120
        self.assertEqual(calls, send.mock_calls)
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("json.loads")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_request_from_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_ticket_from_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.read_git_lines")
Pierre-Yves Chibon 941093
    def test_load_json_commits_to_db_prs_raises_error(
Pierre-Yves Chibon 73d120
        self, git, up_issue, up_pr, send, json_loads
Pierre-Yves Chibon 73d120
    ):
Pierre-Yves Chibon 941093
        """ Test the load_json_commits_to_db method. """
Pierre-Yves Chibon 73d120
        git.side_effect = [["file1"], ["file2"], ["files/image"], ["file1"]]
Pierre-Yves Chibon 73d120
        json_loads.return_value = "foobar"
Pierre-Yves Chibon 73d120
        up_pr.side_effect = Exception("foo error")
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.load_json_commits_to_db(
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            commits=["hash1", "hash2"],
Pierre-Yves Chibon 941093
            abspath=self.path,
Pierre-Yves Chibon 73d120
            data_type="pull-request",
Pierre-Yves Chibon 73d120
            agent="pingou",
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            username=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        calls = [
Pierre-Yves Chibon 941093
            call(
Pierre-Yves Chibon 73d120
                ANY,
Pierre-Yves Chibon 73d120
                json_data="foobar",
Pierre-Yves Chibon 73d120
                namespace=None,
Pierre-Yves Chibon 73d120
                reponame="test",
Pierre-Yves Chibon 73d120
                request_uid="file1",
Pierre-Yves Chibon 73d120
                username=None,
Pierre-Yves Chibon 941093
            )
Pierre-Yves Chibon 941093
        ]
Pierre-Yves Chibon 941093
        up_issue.assert_not_called()
Pierre-Yves Chibon 73d120
        self.assertEqual(calls, up_pr.mock_calls)
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        calls = [
Pierre-Yves Chibon 941093
            call(
Pierre-Yves Chibon 73d120
                "Good Morning\n\n"
Pierre-Yves Chibon 73d120
                "This is the log of loading all the files pushed in the git "
Pierre-Yves Chibon 73d120
                "repo into\n"
Pierre-Yves Chibon 73d120
                "the database. It should ignore files that are not JSON files,"
Pierre-Yves Chibon 73d120
                " this\nis fine.\n\n"
Pierre-Yves Chibon 73d120
                "Loading: file1 -- 1/2 ... ... FAILED\n",
Pierre-Yves Chibon 73d120
                "Issue import report",
Pierre-Yves Chibon 73d120
                "bar@pingou.com",
Pierre-Yves Chibon 941093
            )
Pierre-Yves Chibon 941093
        ]
Pierre-Yves Chibon 73d120
        self.assertEqual(calls, send.mock_calls)
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
class PagureLibTaskServicesWithWebHooktests(tests.Modeltests):
Pierre-Yves Chibon 941093
    """ Tests for pagure.lib.task_services """
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
    maxDiff = None
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
    def setUp(self):
Pierre-Yves Chibon 941093
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 941093
        super(PagureLibTaskServicesWithWebHooktests, self).setUp()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
Pierre-Yves Chibon 73d120
        self.sshkeydir = os.path.join(self.path, "sshkeys")
Pierre-Yves Chibon 73d120
        pagure.config.config["MIRROR_SSHKEYS_FOLDER"] = self.sshkeydir
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 941093
        settings = project.settings
Pierre-Yves Chibon 73d120
        settings["Web-hooks"] = "http://foo.com/api/flag\nhttp://bar.org/bar"
Pierre-Yves Chibon 941093
        project.settings = settings
Pierre-Yves Chibon 941093
        self.session.add(project)
Pierre-Yves Chibon 941093
        self.session.commit()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.call_web_hooks")
Pierre-Yves Chibon 941093
    def test_webhook_notification_no_webhook(self, call_wh):
Pierre-Yves Chibon 941093
        """ Test the webhook_notification method. """
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.webhook_notification(
Pierre-Yves Chibon 73d120
            topic="topic",
Pierre-Yves Chibon 73d120
            msg={"payload": ["a", "b", "c"]},
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            user=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 941093
        call_wh.assert_called_once_with(
Pierre-Yves Chibon 73d120
            ANY,
Pierre-Yves Chibon 73d120
            "topic",
Pierre-Yves Chibon 73d120
            {"payload": ["a", "b", "c"]},
Pierre-Yves Chibon 73d120
            ["http://foo.com/api/flag", "http://bar.org/bar"],
Pierre-Yves Chibon 941093
        )
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("time.time", MagicMock(return_value=2))
Pierre-Yves Chibon 73d120
    @patch("uuid.uuid4", MagicMock(return_value="not_so_random"))
Pierre-Yves Chibon 73d120
    @patch("datetime.datetime")
Pierre-Yves Chibon 73d120
    @patch("requests.post")
Pierre-Yves Chibon e5c2f4
    def test_webhook_notification_no_webhook(self, post, dt):
Pierre-Yves Chibon 941093
        """ Test the webhook_notification method. """
Pierre-Yves Chibon 941093
        post.return_value = False
Pierre-Yves Chibon e5c2f4
        utcnow = MagicMock()
Pierre-Yves Chibon e5c2f4
        utcnow.year = 2018
Pierre-Yves Chibon e5c2f4
        dt.utcnow.return_value = utcnow
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.webhook_notification(
Pierre-Yves Chibon 73d120
            topic="topic",
Pierre-Yves Chibon 73d120
            msg={"payload": ["a", "b", "c"]},
Pierre-Yves Chibon 941093
            namespace=None,
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            user=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
        project = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 941093
        self.assertEqual(post.call_count, 2)
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        calls = [
Pierre-Yves Chibon 941093
            call(
Pierre-Yves Chibon 73d120
                "http://bar.org/bar",
Pierre-Yves Chibon 73d120
                data="{"
Pierre-Yves Chibon 73d120
                '"i": 1, '
Pierre-Yves Chibon 73d120
                '"msg": {'
Pierre-Yves Chibon 73d120
                '"pagure_instance": "http://localhost.localdomain/", '
Pierre-Yves Chibon 73d120
                '"payload": ["a", "b", "c"], '
Pierre-Yves Chibon 73d120
                '"project_fullname": "test"}, '
Pierre-Yves Chibon 73d120
                '"msg_id": "2018-not_so_random", '
Pierre-Yves Chibon 73d120
                '"timestamp": 2, '
Pierre-Yves Chibon 73d120
                '"topic": "topic"}',
Pierre-Yves Chibon 941093
                headers={
Pierre-Yves Chibon 73d120
                    "X-Pagure": "http://localhost.localdomain/",
Pierre-Yves Chibon 73d120
                    "X-Pagure-project": "test",
Pierre-Yves Chibon 73d120
                    "X-Pagure-Signature": "74b12f0b25bf7767014a0c0de9f3c10"
Pierre-Yves Chibon 73d120
                    "191e943d8",
Pierre-Yves Chibon 73d120
                    "X-Pagure-Signature-256": "f3d757796554466eac49a5282b2"
Pierre-Yves Chibon 73d120
                    "4ee32a1ecfb65dedd6c6231fb207240a9fe58",
Pierre-Yves Chibon 73d120
                    "X-Pagure-Topic": b"topic",
Pierre-Yves Chibon 73d120
                    "Content-Type": "application/json",
Pierre-Yves Chibon 941093
                },
Pierre-Yves Chibon 73d120
                timeout=60,
Pierre-Yves Chibon 941093
            ),
Pierre-Yves Chibon 941093
            call(
Pierre-Yves Chibon 73d120
                "http://foo.com/api/flag",
Pierre-Yves Chibon 73d120
                data="{"
Pierre-Yves Chibon 73d120
                '"i": 1, '
Pierre-Yves Chibon 73d120
                '"msg": {'
Pierre-Yves Chibon 73d120
                '"pagure_instance": "http://localhost.localdomain/", '
Pierre-Yves Chibon 73d120
                '"payload": ["a", "b", "c"], '
Pierre-Yves Chibon 73d120
                '"project_fullname": "test"}, '
Pierre-Yves Chibon 73d120
                '"msg_id": "2018-not_so_random", '
Pierre-Yves Chibon 73d120
                '"timestamp": 2, '
Pierre-Yves Chibon 73d120
                '"topic": "topic"}',
Pierre-Yves Chibon 941093
                headers={
Pierre-Yves Chibon 73d120
                    "X-Pagure": "http://localhost.localdomain/",
Pierre-Yves Chibon 73d120
                    "X-Pagure-project": "test",
Pierre-Yves Chibon 73d120
                    "X-Pagure-Signature": "74b12f0b25bf7767014a0c0de9f3c10"
Pierre-Yves Chibon 73d120
                    "191e943d8",
Pierre-Yves Chibon 73d120
                    "X-Pagure-Signature-256": "f3d757796554466eac49a5282b2"
Pierre-Yves Chibon 73d120
                    "4ee32a1ecfb65dedd6c6231fb207240a9fe58",
Pierre-Yves Chibon 73d120
                    "X-Pagure-Topic": b"topic",
Pierre-Yves Chibon 73d120
                    "Content-Type": "application/json",
Pierre-Yves Chibon 941093
                },
Pierre-Yves Chibon 73d120
                timeout=60,
Pierre-Yves Chibon 73d120
            ),
Pierre-Yves Chibon 941093
        ]
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        print(post.mock_calls)
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
        self.assertEqual(calls, post.mock_calls)
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
class PagureLibTaskServicesJenkinsCItests(tests.Modeltests):
Pierre-Yves Chibon 941093
    """ Tests for pagure.lib.task_services """
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
    maxDiff = None
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
    def setUp(self):
Pierre-Yves Chibon 941093
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 941093
        super(PagureLibTaskServicesJenkinsCItests, self).setUp()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
Pierre-Yves Chibon 73d120
        self.sshkeydir = os.path.join(self.path, "sshkeys")
Pierre-Yves Chibon 73d120
        pagure.config.config["MIRROR_SSHKEYS_FOLDER"] = self.sshkeydir
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        # Install the plugin at the DB level
Pierre-Yves Chibon 73d120
        plugin = pagure.lib.plugins.get_plugin("Pagure CI")
Pierre-Yves Chibon 941093
        dbobj = plugin.db_object()
Pierre-Yves Chibon 73d120
        dbobj.ci_url = "https://ci.server.org/"
Pierre-Yves Chibon 73d120
        dbobj.ci_job = "pagure"
Pierre-Yves Chibon 73d120
        dbobj.pagure_ci_token = "random_token"
Pierre-Yves Chibon 941093
        dbobj.project_id = project.id
Pierre-Yves Chibon 941093
        self.session.add(dbobj)
Pierre-Yves Chibon 941093
        self.session.commit()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
        # Create a fork of test for foo
Pierre-Yves Chibon 941093
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 941093
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 941093
            is_fork=True,
Pierre-Yves Chibon 941093
            parent_id=1,
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbccc_foo",
Pierre-Yves Chibon 941093
        )
Pierre-Yves Chibon 73d120
        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 941093
        self.session.add(item)
Pierre-Yves Chibon 941093
        self.session.commit()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.trigger_jenkins_build")
Pierre-Yves Chibon 941093
    def test_trigger_ci_build_invalid_ci(self, trigger_jenk):
Pierre-Yves Chibon 941093
        """ Test the trigger_ci_build method. """
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.trigger_ci_build(
Pierre-Yves Chibon 73d120
            project_name="test",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            ci_type="travis",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        trigger_jenk.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.trigger_jenkins_build")
Pierre-Yves Chibon 941093
    def test_trigger_ci_build_invalid_ci_fork(self, trigger_jenk):
Pierre-Yves Chibon 941093
        """ Test the trigger_ci_build method. """
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.trigger_ci_build(
Pierre-Yves Chibon 73d120
            project_name="forks/foo/test",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            ci_type="travis",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        trigger_jenk.assert_not_called()
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.trigger_jenkins_build")
Pierre-Yves Chibon 941093
    def test_trigger_ci_build_valid_project(self, trigger_jenk):
Pierre-Yves Chibon 941093
        """ Test the trigger_ci_build method. """
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.trigger_ci_build(
Pierre-Yves Chibon 73d120
            project_name="test",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            ci_type="jenkins",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        trigger_jenk.assert_called_once_with(
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            ci_password=None,
Pierre-Yves Chibon 73d120
            ci_username=None,
Pierre-Yves Chibon 73d120
            job="pagure",
Pierre-Yves Chibon 73d120
            project_path="test.git",
Pierre-Yves Chibon 73d120
            token="random_token",
Pierre-Yves Chibon 73d120
            url="https://ci.server.org/",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.trigger_jenkins_build")
Pierre-Yves Chibon 941093
    def test_trigger_ci_build_valid_project_fork(self, trigger_jenk):
Pierre-Yves Chibon 941093
        """ Test the trigger_ci_build method. """
Pierre-Yves Chibon 941093
        output = pagure.lib.tasks_services.trigger_ci_build(
Pierre-Yves Chibon 73d120
            project_name="forks/foo/test",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            ci_type="jenkins",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 941093
        self.assertIsNone(output)
Pierre-Yves Chibon 941093
        trigger_jenk.assert_called_once_with(
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            ci_password=None,
Pierre-Yves Chibon 73d120
            ci_username=None,
Pierre-Yves Chibon 73d120
            job="pagure",
Pierre-Yves Chibon 73d120
            project_path="forks/foo/test.git",
Pierre-Yves Chibon 73d120
            token="random_token",
Pierre-Yves Chibon 73d120
            url="https://ci.server.org/",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 9c6dee
        )
Pierre-Yves Chibon 9c6dee
Pierre-Yves Chibon 9c6dee
Pierre-Yves Chibon 9c6dee
class PagureLibTaskServicesJenkinsCIAuthtests(tests.Modeltests):
Pierre-Yves Chibon 9c6dee
    """ Tests for pagure.lib.task_services """
Pierre-Yves Chibon 9c6dee
Pierre-Yves Chibon 9c6dee
    maxDiff = None
Pierre-Yves Chibon 9c6dee
Pierre-Yves Chibon 9c6dee
    def setUp(self):
Pierre-Yves Chibon 9c6dee
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 9c6dee
        super(PagureLibTaskServicesJenkinsCIAuthtests, self).setUp()
Pierre-Yves Chibon 9c6dee
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
Pierre-Yves Chibon 73d120
        self.sshkeydir = os.path.join(self.path, "sshkeys")
Pierre-Yves Chibon 73d120
        pagure.config.config["MIRROR_SSHKEYS_FOLDER"] = self.sshkeydir
Pierre-Yves Chibon 9c6dee
Pierre-Yves Chibon 9c6dee
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 9c6dee
Pierre-Yves Chibon 9c6dee
        # Install the plugin at the DB level
Pierre-Yves Chibon 73d120
        plugin = pagure.lib.plugins.get_plugin("Pagure CI")
Pierre-Yves Chibon 9c6dee
        dbobj = plugin.db_object()
Pierre-Yves Chibon 73d120
        dbobj.ci_url = "https://ci.server.org/"
Pierre-Yves Chibon 73d120
        dbobj.ci_job = "pagure"
Pierre-Yves Chibon 73d120
        dbobj.ci_username = "jenkins_username"
Pierre-Yves Chibon 73d120
        dbobj.ci_password = "jenkins_password"
Pierre-Yves Chibon 73d120
        dbobj.pagure_ci_token = "random_token"
Pierre-Yves Chibon 9c6dee
        dbobj.project_id = project.id
Pierre-Yves Chibon 9c6dee
        self.session.add(dbobj)
Pierre-Yves Chibon 9c6dee
        self.session.commit()
Pierre-Yves Chibon 9c6dee
Pierre-Yves Chibon 9c6dee
        # Create a fork of test for foo
Pierre-Yves Chibon 9c6dee
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 9c6dee
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 9c6dee
            is_fork=True,
Pierre-Yves Chibon 9c6dee
            parent_id=1,
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbccc_foo",
Pierre-Yves Chibon 9c6dee
        )
Pierre-Yves Chibon 73d120
        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 9c6dee
        self.session.add(item)
Pierre-Yves Chibon 9c6dee
        self.session.commit()
Pierre-Yves Chibon 9c6dee
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.trigger_jenkins_build")
Pierre-Yves Chibon 9c6dee
    def test_trigger_ci_build_invalid_ci(self, trigger_jenk):
Pierre-Yves Chibon 9c6dee
        """ Test the trigger_ci_build method. """
Pierre-Yves Chibon 9c6dee
        output = pagure.lib.tasks_services.trigger_ci_build(
Pierre-Yves Chibon 73d120
            project_name="test",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            ci_type="travis",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9c6dee
        self.assertIsNone(output)
Pierre-Yves Chibon 9c6dee
        trigger_jenk.assert_not_called()
Pierre-Yves Chibon 9c6dee
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.trigger_jenkins_build")
Pierre-Yves Chibon 9c6dee
    def test_trigger_ci_build_invalid_ci_fork(self, trigger_jenk):
Pierre-Yves Chibon 9c6dee
        """ Test the trigger_ci_build method. """
Pierre-Yves Chibon 9c6dee
        output = pagure.lib.tasks_services.trigger_ci_build(
Pierre-Yves Chibon 73d120
            project_name="forks/foo/test",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            ci_type="travis",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9c6dee
        self.assertIsNone(output)
Pierre-Yves Chibon 9c6dee
        trigger_jenk.assert_not_called()
Pierre-Yves Chibon 9c6dee
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.trigger_jenkins_build")
Pierre-Yves Chibon 9c6dee
    def test_trigger_ci_build_valid_project(self, trigger_jenk):
Pierre-Yves Chibon 9c6dee
        """ Test the trigger_ci_build method. """
Pierre-Yves Chibon 9c6dee
        output = pagure.lib.tasks_services.trigger_ci_build(
Pierre-Yves Chibon 73d120
            project_name="test",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            ci_type="jenkins",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9c6dee
        self.assertIsNone(output)
Pierre-Yves Chibon 9c6dee
        trigger_jenk.assert_called_once_with(
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            ci_password="jenkins_password",
Pierre-Yves Chibon 73d120
            ci_username="jenkins_username",
Pierre-Yves Chibon 73d120
            job="pagure",
Pierre-Yves Chibon 73d120
            project_path="test.git",
Pierre-Yves Chibon 73d120
            token="random_token",
Pierre-Yves Chibon 73d120
            url="https://ci.server.org/",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.trigger_jenkins_build")
Pierre-Yves Chibon 9c6dee
    def test_trigger_ci_build_valid_project_fork(self, trigger_jenk):
Pierre-Yves Chibon 9c6dee
        """ Test the trigger_ci_build method. """
Pierre-Yves Chibon 9c6dee
        output = pagure.lib.tasks_services.trigger_ci_build(
Pierre-Yves Chibon 73d120
            project_name="forks/foo/test",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            ci_type="jenkins",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9c6dee
        self.assertIsNone(output)
Pierre-Yves Chibon 9c6dee
        trigger_jenk.assert_called_once_with(
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
            cause="PR#ID",
Pierre-Yves Chibon 73d120
            ci_password="jenkins_password",
Pierre-Yves Chibon 73d120
            ci_username="jenkins_username",
Pierre-Yves Chibon 73d120
            job="pagure",
Pierre-Yves Chibon 73d120
            project_path="forks/foo/test.git",
Pierre-Yves Chibon 73d120
            token="random_token",
Pierre-Yves Chibon 73d120
            url="https://ci.server.org/",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 941093
        )
Pierre-Yves Chibon 941093
Pierre-Yves Chibon 941093
Pierre-Yves Chibon dfbfac
class PagureLibTaskServicesLoadJsonTickettests(tests.Modeltests):
Pierre-Yves Chibon dfbfac
    """ Tests for pagure.lib.task_services """
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon dfbfac
    maxDiff = None
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon dfbfac
    def setUp(self):
Pierre-Yves Chibon dfbfac
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon dfbfac
        super(PagureLibTaskServicesLoadJsonTickettests, self).setUp()
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon dfbfac
        tests.create_projects(self.session)
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon 73d120
        self.gitrepo = os.path.join(self.path, "repos", "tickets", "test.git")
Pierre-Yves Chibon 73d120
        repopath = os.path.join(self.path, "repos", "tickets")
Pierre-Yves Chibon dfbfac
        os.makedirs(self.gitrepo)
Pierre-Yves Chibon dfbfac
        self.repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon dfbfac
        # Create an issue to play with
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon dfbfac
            session=self.session,
Pierre-Yves Chibon dfbfac
            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 dfbfac
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue")
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon 73d120
        issue = pagure.lib.query.search_issues(
Pierre-Yves Chibon 73d120
            self.session, project, issueid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon dfbfac
        # Add a couple of comment on the ticket
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_issue_comment(
Pierre-Yves Chibon dfbfac
            session=self.session,
Pierre-Yves Chibon dfbfac
            issue=issue,
Pierre-Yves Chibon 73d120
            comment="Hey look a comment!",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon dfbfac
        )
Pierre-Yves Chibon dfbfac
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Comment added")
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon dfbfac
        commits = [
Pierre-Yves Chibon dfbfac
            commit
Pierre-Yves Chibon dfbfac
            for commit in self.repo_obj.walk(
Pierre-Yves Chibon 73d120
                self.repo_obj.head.target, pygit2.GIT_SORT_NONE
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon dfbfac
        ]
Pierre-Yves Chibon dfbfac
        # 2 commits: creation - new comment
Pierre-Yves Chibon dfbfac
        self.assertEqual(len(commits), 2)
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon 73d120
        issue = pagure.lib.query.search_issues(
Pierre-Yves Chibon 73d120
            self.session, project, issueid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon dfbfac
        self.assertEqual(len(issue.comments), 1)
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_request_from_git")
Pierre-Yves Chibon dfbfac
    def test_loading_issue_json(self, up_pr, send):
Pierre-Yves Chibon dfbfac
        """ Test loading the JSON file of a ticket. """
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        issue = pagure.lib.query.search_issues(
Pierre-Yves Chibon 73d120
            self.session, project, issueid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon dfbfac
        commits = [
Pierre-Yves Chibon dfbfac
            commit.oid.hex
Pierre-Yves Chibon dfbfac
            for commit in self.repo_obj.walk(
Pierre-Yves Chibon 73d120
                self.repo_obj.head.target, pygit2.GIT_SORT_NONE
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon dfbfac
        ]
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon dfbfac
        output = pagure.lib.tasks_services.load_json_commits_to_db(
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon dfbfac
            commits=commits,
Pierre-Yves Chibon dfbfac
            abspath=self.gitrepo,
Pierre-Yves Chibon 73d120
            data_type="ticket",
Pierre-Yves Chibon 73d120
            agent="pingou",
Pierre-Yves Chibon dfbfac
            namespace=None,
Pierre-Yves Chibon 73d120
            username=None,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon dfbfac
        self.assertIsNone(output)
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon dfbfac
        up_pr.assert_not_called()
Pierre-Yves Chibon dfbfac
        calls = [
Pierre-Yves Chibon dfbfac
            call(
Pierre-Yves Chibon 73d120
                "Good Morning\n\n"
Pierre-Yves Chibon 73d120
                "This is the log of loading all the files pushed in the git "
Pierre-Yves Chibon 73d120
                "repo into\n"
Pierre-Yves Chibon 73d120
                "the database. It should ignore files that are not JSON files,"
Pierre-Yves Chibon 73d120
                " this\nis fine.\n\n"
Pierre-Yves Chibon 73d120
                "Loading: %s -- 1/1 ... ... Done" % issue.uid,
Pierre-Yves Chibon 73d120
                "Issue import report",
Pierre-Yves Chibon 73d120
                "bar@pingou.com",
Pierre-Yves Chibon dfbfac
            )
Pierre-Yves Chibon dfbfac
        ]
Pierre-Yves Chibon 73d120
        self.assertEqual(calls, send.mock_calls)
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        issue = pagure.lib.query.search_issues(
Pierre-Yves Chibon 73d120
            self.session, project, issueid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon dfbfac
        self.assertEqual(len(issue.comments), 1)
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon dfbfac
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 941093
    unittest.main(verbosity=2)