Blame tests/test_pagure_flask_ui_issue_pr_link.py

Pierre-Yves Chibon 11d019
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
"""
Pierre-Yves Chibon 11d019
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
 Authors:
Pierre-Yves Chibon 11d019
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
"""
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard 626417
Pierre-Yves Chibon 11d019
import datetime
Pierre-Yves Chibon 11d019
import json
Pierre-Yves Chibon 11d019
import unittest
Pierre-Yves Chibon 11d019
import re
Pierre-Yves Chibon 11d019
import shutil
Pierre-Yves Chibon 11d019
import sys
Pierre-Yves Chibon 11d019
import tempfile
Pierre-Yves Chibon 11d019
import time
Pierre-Yves Chibon 11d019
import os
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
import pygit2
Pierre-Yves Chibon 11d019
from mock import ANY, patch, MagicMock
Pierre-Yves Chibon 11d019
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 11d019
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 930073
import pagure.lib.tasks
Pierre-Yves Chibon 11d019
import tests
Pierre-Yves Chibon 11d019
from pagure.lib.repo import PagureRepo
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
class PagureFlaskPrIssueLinkTest(tests.Modeltests):
Pierre-Yves Chibon 11d019
    """ Tests pagure when linking PRs to tickets """
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
    maxDiff = None
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
    def setUp(self):
Pierre-Yves Chibon 11d019
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 11d019
        super(PagureFlaskPrIssueLinkTest, self).setUp()
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        tests.create_projects(self.session)
Pierre-Yves Chibon 11d019
        tests.create_projects(
Pierre-Yves Chibon 73d120
            self.session, is_fork=True, user_id=2, hook_token_suffix="bar"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "foo"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        # Create issues to play with
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon 11d019
            session=self.session,
Pierre-Yves Chibon 11d019
            repo=repo,
Pierre-Yves Chibon 73d120
            title="tést íssüé",
Pierre-Yves Chibon 73d120
            content="We should work on this",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 11d019
        )
Pierre-Yves Chibon 11d019
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "tést íssüé")
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon 11d019
            session=self.session,
Pierre-Yves Chibon 11d019
            repo=repo,
Pierre-Yves Chibon 73d120
            title="tést íssüé #2",
Pierre-Yves Chibon 73d120
            content="We should still work on this",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 11d019
        )
Pierre-Yves Chibon 11d019
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "tést íssüé #2")
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        # Add a commit to the fork
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 73d120
        newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
Pierre-Yves Chibon 73d120
        repopath = os.path.join(newpath, "test")
Pierre-Yves Chibon 73d120
        clone_repo = pygit2.clone_repository(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "foo", "test.git"),
Pierre-Yves Chibon 73d120
            repopath,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        # Create a file in that git repo
Pierre-Yves Chibon 73d120
        with open(os.path.join(repopath, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        clone_repo.index.add("sources")
Pierre-Yves Chibon 11d019
        clone_repo.index.write()
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        try:
Pierre-Yves Chibon 73d120
            com = repo.revparse_single("HEAD")
Pierre-Yves Chibon 11d019
            prev_commit = [com.oid.hex]
Pierre-Yves Chibon 11d019
        except:
Pierre-Yves Chibon 11d019
            prev_commit = []
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        # Commits the files added
Pierre-Yves Chibon 11d019
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 11d019
        clone_repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 11d019
            author,
Pierre-Yves Chibon 11d019
            committer,
Pierre-Yves Chibon 73d120
            "Add sources file for testing\n\n Relates to #2",
Pierre-Yves Chibon 11d019
            # binary string representing the tree object ID
Pierre-Yves Chibon 11d019
            tree,
Pierre-Yves Chibon 11d019
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            prev_commit,
Pierre-Yves Chibon 11d019
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master:refs/heads/master"
Pierre-Yves Chibon 11d019
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 11d019
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        # Create the corresponding PR
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        fork_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="foo"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 930073
        request = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 11d019
            self.session,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 11d019
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test PR",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 11d019
            initial_comment=None,
Pierre-Yves Chibon 11d019
            repo_from=fork_repo,
Pierre-Yves Chibon 11d019
        )
Pierre-Yves Chibon 11d019
        self.session.commit()
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
        pagure.lib.tasks.link_pr_to_ticket(request.uid)
Pierre-Yves Chibon 11d019
        self.assertEqual(request.id, 3)
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
    def test_ticket_no_link(self):
Pierre-Yves Chibon 11d019
        """ Test that no Related PR(s) block is showing in the issue page.
Pierre-Yves Chibon 11d019
        """
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/issue/1")
Pierre-Yves Chibon 11d019
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 11d019
        self.assertNotIn(
Pierre-Yves Chibon 73d120
            "Related Pull Requests", output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 11d019
    def test_ticket_link(self):
Pierre-Yves Chibon 33587e
        """ Test that a Related PR(s) block is showing in the issue page.
Pierre-Yves Chibon 11d019
        """
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/issue/2")
Pierre-Yves Chibon 11d019
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
        self.assertIn("Related Pull Requests", output.get_data(as_text=True))
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon ad1526
    def test_pr_link_issue_list(self):
Pierre-Yves Chibon ad1526
        """ Test that the related PR(s) shows in the page listing issues
Pierre-Yves Chibon ad1526
        """
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/issues")
Pierre-Yves Chibon ad1526
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ad1526
        self.assertIn(
Pierre-Yves Chibon ad1526
            '
Pierre-Yves Chibon ad1526
            'text-muted font-size-09" data-toggle="tooltip">\n'
Pierre-Yves Chibon ad1526
            '                            \n'
Pierre-Yves Chibon ad1526
            '                            
Pierre-Yves Chibon ad1526
            'class="notblue">PR#3\n                          ',
Pierre-Yves Chibon 73d120
            output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon ad1526
Pierre-Yves Chibon 11d019
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 11d019
    unittest.main(verbosity=2)