Blame tests/test_pagure_flask_ui_remote_pr.py

Pierre-Yves Chibon 9f16a2
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
"""
Pierre-Yves Chibon 9f16a2
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
 Authors:
Pierre-Yves Chibon 9f16a2
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
"""
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard 626417
Pierre-Yves Chibon 9f16a2
import json
Pierre-Yves Chibon 9d1740
import os
Pierre-Yves Chibon 9d1740
import re
Pierre-Yves Chibon 9f16a2
import shutil
Pierre-Yves Chibon 9f16a2
import sys
Pierre-Yves Chibon 9f16a2
import tempfile
Pierre-Yves Chibon 9f16a2
import time
Pierre-Yves Chibon 9d1740
import unittest
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
import pygit2
Pierre-Yves Chibon 9d1740
import wtforms
Pierre-Yves Chibon 9f16a2
from mock import patch, MagicMock
Pierre-Yves Chibon 9f16a2
from bs4 import BeautifulSoup
Pierre-Yves Chibon 9f16a2
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 9f16a2
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 9f16a2
import tests
Pierre-Yves Chibon 9f16a2
from pagure.lib.repo import PagureRepo
Aurélien Bompard 626417
from pagure.lib.git import _make_signature
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
class PagureRemotePRtests(tests.Modeltests):
Pierre-Yves Chibon 9f16a2
    """ Tests for remote PRs in pagure """
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
    def setUp(self):
Pierre-Yves Chibon 9f16a2
        """ Set up the environment. """
Pierre-Yves Chibon 9f16a2
        super(PagureRemotePRtests, self).setUp()
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 73d120
        self.newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
Pierre-Yves Chibon 73d120
        self.old_value = pagure.config.config["REMOTE_GIT_FOLDER"]
Pierre-Yves Chibon 73d120
        pagure.config.config["REMOTE_GIT_FOLDER"] = os.path.join(
Pierre-Yves Chibon 73d120
            self.path, "remotes"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
    def tearDown(self):
Pierre-Yves Chibon 9f16a2
        """ Clear things up. """
Pierre-Yves Chibon 9f16a2
        super(PagureRemotePRtests, self).tearDown()
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 73d120
        pagure.config.config["REMOTE_GIT_FOLDER"] = self.old_value
Pierre-Yves Chibon 9f16a2
        shutil.rmtree(self.newpath)
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 73d120
    def set_up_git_repo(self, new_project=None, branch_from="feature"):
Pierre-Yves Chibon 9f16a2
        """ Set up the git repo and create the corresponding PullRequest
Pierre-Yves Chibon 9f16a2
        object.
Pierre-Yves Chibon 9f16a2
        """
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        # Create a git repo to play with
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 9f16a2
        repo = pygit2.init_repository(gitrepo, bare=True)
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 73d120
        repopath = os.path.join(self.newpath, "test")
Pierre-Yves Chibon 9f16a2
        clone_repo = pygit2.clone_repository(gitrepo, repopath)
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        # 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 9f16a2
        clone_repo.index.write()
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        try:
Pierre-Yves Chibon 73d120
            com = repo.revparse_single("HEAD")
Pierre-Yves Chibon 9f16a2
            prev_commit = [com.oid.hex]
Pierre-Yves Chibon 9f16a2
        except:
Pierre-Yves Chibon 9f16a2
            prev_commit = []
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        # Commits the files added
Pierre-Yves Chibon 9f16a2
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = _make_signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = _make_signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 9f16a2
        clone_repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 9f16a2
            author,
Pierre-Yves Chibon 9f16a2
            committer,
Pierre-Yves Chibon 73d120
            "Add sources file for testing",
Pierre-Yves Chibon 9f16a2
            # binary string representing the tree object ID
Pierre-Yves Chibon 9f16a2
            tree,
Pierre-Yves Chibon 9f16a2
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            prev_commit,
Pierre-Yves Chibon 9f16a2
        )
Pierre-Yves Chibon 4babe0
        # time.sleep(1)
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master:refs/heads/master"
Pierre-Yves Chibon 9f16a2
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 9f16a2
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 73d120
        with open(os.path.join(repopath, ".gitignore"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("*~")
Pierre-Yves Chibon 73d120
        clone_repo.index.add(".gitignore")
Pierre-Yves Chibon 9f16a2
        clone_repo.index.write()
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        # Commits the files added
Pierre-Yves Chibon 9f16a2
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = _make_signature("Alice Äuthòr", "alice@äuthòrs.tld")
Pierre-Yves Chibon 73d120
        committer = _make_signature("Cecil Cõmmîttër", "cecil@cõmmîttërs.tld")
Pierre-Yves Chibon 9f16a2
        clone_repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",
Pierre-Yves Chibon 9f16a2
            author,
Pierre-Yves Chibon 9f16a2
            committer,
Pierre-Yves Chibon 73d120
            "Add .gitignore file for testing",
Pierre-Yves Chibon 9f16a2
            # binary string representing the tree object ID
Pierre-Yves Chibon 9f16a2
            tree,
Pierre-Yves Chibon 9f16a2
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon 9f16a2
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master:refs/heads/master"
Pierre-Yves Chibon 9f16a2
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 9f16a2
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        # Set the second repo
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        new_gitrepo = repopath
Pierre-Yves Chibon 9f16a2
        if new_project:
Pierre-Yves Chibon 9f16a2
            # Create a new git repo to play with
Pierre-Yves Chibon 9f16a2
            new_gitrepo = os.path.join(self.newpath, new_project.fullname)
Pierre-Yves Chibon 9f16a2
            if not os.path.exists(new_gitrepo):
Pierre-Yves Chibon 9f16a2
                os.makedirs(new_gitrepo)
Pierre-Yves Chibon 9f16a2
                new_repo = pygit2.clone_repository(gitrepo, new_gitrepo)
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        repo = pygit2.Repository(new_gitrepo)
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        # Edit the sources file again
Pierre-Yves Chibon 73d120
        with open(os.path.join(new_gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n boose")
Pierre-Yves Chibon 73d120
        repo.index.add("sources")
Pierre-Yves Chibon 9f16a2
        repo.index.write()
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        # Commits the files added
Pierre-Yves Chibon 9f16a2
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = _make_signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = _make_signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 9f16a2
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/%s" % branch_from,
Pierre-Yves Chibon 9f16a2
            author,
Pierre-Yves Chibon 9f16a2
            committer,
Pierre-Yves Chibon 73d120
            "A commit on branch %s" % branch_from,
Pierre-Yves Chibon 9f16a2
            tree,
Pierre-Yves Chibon 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon 9f16a2
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/%s" % (branch_from)
Pierre-Yves Chibon 9f16a2
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 9f16a2
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 9f16a2
    def test_new_remote_pr_unauth(self):
Pierre-Yves Chibon 9f16a2
        """ Test creating a new remote PR un-authenticated. """
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        tests.create_projects(self.session)
Pierre-Yves Chibon 9f16a2
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9f16a2
        self.set_up_git_repo()
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        # Before
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 9f16a2
        self.assertEqual(len(project.requests), 0)
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        # Try creating a remote PR
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/diff/remote")
Pierre-Yves Chibon 9f16a2
        self.assertEqual(output.status_code, 302)
Pierre-Yves Chibon 9f16a2
        self.assertIn(
Pierre-Yves Chibon 73d120
            "You should be redirected automatically to target URL: "
Pierre-Yves Chibon 73d120
            '
Pierre-Yves Chibon 73d120
            output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 9f16a2
    def test_new_remote_pr_auth(self):
Pierre-Yves Chibon 3117a1
        """ Test creating a new remote PR authenticated. """
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        tests.create_projects(self.session)
Pierre-Yves Chibon 9f16a2
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9f16a2
        self.set_up_git_repo()
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        # Before
Pierre-Yves Chibon 930073
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 9f16a2
        self.assertEqual(len(project.requests), 0)
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        # Try creating a remote PR
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon 9f16a2
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/diff/remote")
Pierre-Yves Chibon 9f16a2
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            self.assertIn(
Pierre-Yves Chibon 73d120
                "

New remote pull-request

",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
            csrf_token = self.get_csrf(output=output)
Pierre-Yves Chibon 9d1740
            with patch(
Pierre-Yves Chibon 73d120
                "pagure.forms.RemoteRequestPullForm.git_repo.args",
Pierre-Yves Chibon 73d120
                MagicMock(
Pierre-Yves Chibon 73d120
                    return_value=(
Pierre-Yves Chibon 73d120
                        "Git Repo address",
Pierre-Yves Chibon 73d120
                        [wtforms.validators.DataRequired()],
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                ),
Pierre-Yves Chibon 73d120
            ):
Pierre-Yves Chibon 9d1740
                data = {
Pierre-Yves Chibon 73d120
                    "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                    "title": "Remote PR title",
Pierre-Yves Chibon 73d120
                    "branch_from": "feature",
Pierre-Yves Chibon 73d120
                    "branch_to": "master",
Pierre-Yves Chibon 73d120
                    "git_repo": os.path.join(self.newpath, "test"),
Pierre-Yves Chibon 9d1740
                }
Pierre-Yves Chibon 73d120
                output = self.app.post("/test/diff/remote", data=data)
Pierre-Yves Chibon 9d1740
                self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9d1740
                output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
                self.assertIn("Create Pull Request\n    \n", output_text)
Pierre-Yves Chibon 73d120
                self.assertIn('
\n', output_text)
Pierre-Yves Chibon 73d120
                self.assertIn('
\n', output_text)
Pierre-Yves Chibon 9d1740
                self.assertNotIn(
Pierre-Yves Chibon 73d120
                    '
\n', output_text
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
Pierre-Yves Chibon 9d1740
                # Not saved yet
Pierre-Yves Chibon 930073
                self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
                project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
                    self.session, "test"
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
                self.assertEqual(len(project.requests), 0)
Pierre-Yves Chibon 9d1740
Pierre-Yves Chibon 9d1740
                data = {
Pierre-Yves Chibon 73d120
                    "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                    "title": "Remote PR title",
Pierre-Yves Chibon 73d120
                    "branch_from": "feature",
Pierre-Yves Chibon 73d120
                    "branch_to": "master",
Pierre-Yves Chibon 73d120
                    "git_repo": os.path.join(self.newpath, "test"),
Pierre-Yves Chibon 73d120
                    "confirm": 1,
Pierre-Yves Chibon 9d1740
                }
Pierre-Yves Chibon 73d120
                self.old_value = pagure.config.config["DISABLE_REMOTE_PR"]
Pierre-Yves Chibon 73d120
                pagure.config.config["DISABLE_REMOTE_PR"] = True
Karsten Hopp 9f48c1
                output = self.app.post(
Pierre-Yves Chibon 73d120
                    "/test/diff/remote", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
                )
Karsten Hopp 9f48c1
                self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 73d120
                pagure.config.config["DISABLE_REMOTE_PR"] = self.old_value
Pierre-Yves Chibon 9d1740
                output = self.app.post(
Pierre-Yves Chibon 73d120
                    "/test/diff/remote", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
                self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9d1740
                output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 9d1740
                self.assertIn(
Pierre-Yves Chibon 9d1740
                    '#1',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
                self.assertIn('
\n', output_text)
Pierre-Yves Chibon 73d120
                self.assertIn('
\n', output_text)
Pierre-Yves Chibon 9d1740
                self.assertNotIn(
Pierre-Yves Chibon 73d120
                    '
\n', output_text
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
Pierre-Yves Chibon 9d1740
                # Show the filename in the Changes summary
Pierre-Yves Chibon 9d1740
                self.assertIn(
Pierre-Yves Chibon 73d120
                    '
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
                self.assertIn(
Pierre-Yves Chibon 9d1740
                    '
'
Pierre-Yves Chibon 73d120
                    "\n          <small>.gitignore</small>",
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
                self.assertIn(
Pierre-Yves Chibon 73d120
                    '
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
                self.assertIn(
Pierre-Yves Chibon 9d1740
                    '
'
Pierre-Yves Chibon 73d120
                    "\n          <small>sources</small>",
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 9f16a2
        # Remote PR Created
Pierre-Yves Chibon 930073
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 9f16a2
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon 9f16a2
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 7a6fff
    def test_new_remote_no_title(self):
Pierre-Yves Chibon 7a6fff
        """ Test creating a new remote PR authenticated when no title is
Pierre-Yves Chibon 7a6fff
        specified. """
Pierre-Yves Chibon 7a6fff
Pierre-Yves Chibon 7a6fff
        tests.create_projects(self.session)
Pierre-Yves Chibon 7a6fff
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 7a6fff
        self.set_up_git_repo()
Pierre-Yves Chibon 7a6fff
Pierre-Yves Chibon 7a6fff
        # Before
Pierre-Yves Chibon 7a6fff
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 7a6fff
        self.assertEqual(len(project.requests), 0)
Pierre-Yves Chibon 7a6fff
Pierre-Yves Chibon 7a6fff
        # Try creating a remote PR
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon 7a6fff
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/diff/remote")
Pierre-Yves Chibon 7a6fff
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 7a6fff
            self.assertIn(
Pierre-Yves Chibon 73d120
                "

New remote pull-request

",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 7a6fff
Pierre-Yves Chibon 7a6fff
            csrf_token = self.get_csrf(output=output)
Pierre-Yves Chibon 7a6fff
            with patch(
Pierre-Yves Chibon 73d120
                "pagure.forms.RemoteRequestPullForm.git_repo.args",
Pierre-Yves Chibon 73d120
                MagicMock(
Pierre-Yves Chibon 73d120
                    return_value=(
Pierre-Yves Chibon 73d120
                        "Git Repo address",
Pierre-Yves Chibon 73d120
                        [wtforms.validators.DataRequired()],
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                ),
Pierre-Yves Chibon 73d120
            ):
Pierre-Yves Chibon 7a6fff
                data = {
Pierre-Yves Chibon 73d120
                    "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                    "branch_from": "master",
Pierre-Yves Chibon 73d120
                    "branch_to": "feature",
Pierre-Yves Chibon 73d120
                    "git_repo": os.path.join(self.newpath, "test"),
Pierre-Yves Chibon 7a6fff
                }
Pierre-Yves Chibon 73d120
                output = self.app.post("/test/diff/remote", data=data)
Pierre-Yves Chibon 7a6fff
                self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 7a6fff
                output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
                self.assertIn("

New remote pull-request

", output_text)
Pierre-Yves Chibon 73d120
                self.assertIn("<option selected="">feature</option>", output_text)
Pierre-Yves Chibon 7a6fff
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 59f36c
    def test_new_remote_pr_empty_target(self):
Pierre-Yves Chibon 59f36c
        """ Test creating a new remote PR authenticated against an empty
Pierre-Yves Chibon 59f36c
        git repo. """
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 59f36c
        tests.create_projects(self.session)
Pierre-Yves Chibon 59f36c
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 59f36c
        # Create empty target git repo
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 59f36c
        pygit2.init_repository(gitrepo, bare=True)
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 59f36c
        # Create git repo we'll pull from
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", "test_origin.git")
Pierre-Yves Chibon 59f36c
        repo = pygit2.init_repository(gitrepo)
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 59f36c
        # Create a file in that git repo
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        repo.index.add("sources")
Pierre-Yves Chibon 59f36c
        repo.index.write()
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 59f36c
        prev_commit = []
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 59f36c
        # Commits the files added
Pierre-Yves Chibon 59f36c
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = _make_signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = _make_signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 59f36c
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature",  # the name of the reference to update
Pierre-Yves Chibon 59f36c
            author,
Pierre-Yves Chibon 59f36c
            committer,
Pierre-Yves Chibon 73d120
            "Add sources file for testing",
Pierre-Yves Chibon 59f36c
            # binary string representing the tree object ID
Pierre-Yves Chibon 59f36c
            tree,
Pierre-Yves Chibon 59f36c
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            prev_commit,
Pierre-Yves Chibon 59f36c
        )
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 59f36c
        # Before
Pierre-Yves Chibon 930073
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 59f36c
        self.assertEqual(len(project.requests), 0)
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 59f36c
        # Try creating a remote PR
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon 59f36c
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/diff/remote")
Pierre-Yves Chibon 59f36c
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 59f36c
            self.assertIn(
Pierre-Yves Chibon 73d120
                "

New remote pull-request

",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 59f36c
            csrf_token = self.get_csrf(output=output)
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 9d1740
            with patch(
Pierre-Yves Chibon 73d120
                "pagure.forms.RemoteRequestPullForm.git_repo.args",
Pierre-Yves Chibon 73d120
                MagicMock(
Pierre-Yves Chibon 73d120
                    return_value=(
Pierre-Yves Chibon 73d120
                        "Git Repo address",
Pierre-Yves Chibon 73d120
                        [wtforms.validators.DataRequired()],
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                ),
Pierre-Yves Chibon 73d120
            ):
Pierre-Yves Chibon 9d1740
                data = {
Pierre-Yves Chibon 73d120
                    "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                    "title": "Remote PR title",
Pierre-Yves Chibon 73d120
                    "branch_from": "feature",
Pierre-Yves Chibon 73d120
                    "branch_to": "master",
Pierre-Yves Chibon 73d120
                    "git_repo": gitrepo,
Pierre-Yves Chibon 9d1740
                }
Pierre-Yves Chibon 73d120
                output = self.app.post("/test/diff/remote", data=data)
Pierre-Yves Chibon 9d1740
                self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9d1740
                output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
                self.assertIn("Create Pull Request\n    \n", output_text)
Pierre-Yves Chibon 73d120
                self.assertIn('
\n', output_text)
Pierre-Yves Chibon 9d1740
                self.assertNotIn(
Pierre-Yves Chibon 73d120
                    '
\n', output_text
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
Pierre-Yves Chibon 9d1740
                # Not saved yet
Pierre-Yves Chibon 930073
                self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
                project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
                    self.session, "test"
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
                self.assertEqual(len(project.requests), 0)
Pierre-Yves Chibon 9d1740
Pierre-Yves Chibon 9d1740
                data = {
Pierre-Yves Chibon 73d120
                    "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                    "title": "Remote PR title",
Pierre-Yves Chibon 73d120
                    "branch_from": "feature",
Pierre-Yves Chibon 73d120
                    "branch_to": "master",
Pierre-Yves Chibon 73d120
                    "git_repo": gitrepo,
Pierre-Yves Chibon 73d120
                    "confirm": 1,
Pierre-Yves Chibon 9d1740
                }
Pierre-Yves Chibon 9d1740
                output = self.app.post(
Pierre-Yves Chibon 73d120
                    "/test/diff/remote", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
                self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9d1740
                output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 9d1740
                self.assertIn(
Pierre-Yves Chibon 73d120
                    "<title>PR#1: Remote PR title - test\n - Pagure</title>",
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
                self.assertIn('
\n', output_text)
Pierre-Yves Chibon 9d1740
                self.assertNotIn(
Pierre-Yves Chibon 73d120
                    '
\n', output_text
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
Pierre-Yves Chibon 9d1740
                # Show the filename in the Changes summary
Pierre-Yves Chibon 9d1740
                self.assertIn(
Pierre-Yves Chibon 73d120
                    '
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
                self.assertIn(
Pierre-Yves Chibon 9d1740
                    '
'
Pierre-Yves Chibon 73d120
                    "\n          <small>sources</small>",
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 59f36c
        # Remote PR Created
Pierre-Yves Chibon 930073
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 59f36c
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 59f36c
        # Check the merge state of the PR
Pierre-Yves Chibon 73d120
        data = {"csrf_token": csrf_token, "requestid": project.requests[0].uid}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon 59f36c
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 59f36c
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 59f36c
        data = json.loads(output_text)
Pierre-Yves Chibon 59f36c
        self.assertEqual(
Pierre-Yves Chibon 59f36c
            data,
Pierre-Yves Chibon 59f36c
            {
Pierre-Yves Chibon 59f36c
                "code": "FFORWARD",
Pierre-Yves Chibon 59f36c
                "message": "The pull-request can be merged and fast-forwarded",
Pierre-Yves Chibon 73d120
                "short_code": "Ok",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 59f36c
        )
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 59f36c
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 59f36c
            # Merge the PR
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Pierre-Yves Chibon 59f36c
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/pull-request/1/merge", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 59f36c
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 59f36c
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 59f36c
            self.assertIn(
Julen Landa Alustiza 6725ec
                "<title>PR#1: Remote PR title - test\n - Pagure</title>",
Julen Landa Alustiza 6725ec
                output_text,
Pierre-Yves Chibon 59f36c
            )
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.trigger_ci_build")
Pierre-Yves Chibon 3117a1
    def test_new_remote_pr_ci_off(self, trigger_ci):
Pierre-Yves Chibon 3117a1
        """ Test creating a new remote PR when CI is not configured. """
Pierre-Yves Chibon 3117a1
Pierre-Yves Chibon 3117a1
        tests.create_projects(self.session)
Pierre-Yves Chibon 3117a1
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3117a1
        self.set_up_git_repo()
Pierre-Yves Chibon 3117a1
Pierre-Yves Chibon 3117a1
        # Before
Pierre-Yves Chibon 930073
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 3117a1
        self.assertEqual(len(project.requests), 0)
Pierre-Yves Chibon 3117a1
Pierre-Yves Chibon 3117a1
        # Create a remote PR
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon 3117a1
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 3117a1
Pierre-Yves Chibon 3117a1
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 3117a1
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "title": "Remote PR title",
Pierre-Yves Chibon 73d120
                "branch_from": "feature",
Pierre-Yves Chibon 73d120
                "branch_to": "master",
Pierre-Yves Chibon 73d120
                "git_repo": os.path.join(self.newpath, "test"),
Pierre-Yves Chibon 3117a1
            }
Pierre-Yves Chibon 9d1740
            with patch(
Pierre-Yves Chibon 73d120
                "pagure.forms.RemoteRequestPullForm.git_repo.args",
Pierre-Yves Chibon 73d120
                MagicMock(
Pierre-Yves Chibon 73d120
                    return_value=(
Pierre-Yves Chibon 73d120
                        "Git Repo address",
Pierre-Yves Chibon 73d120
                        [wtforms.validators.DataRequired()],
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                ),
Pierre-Yves Chibon 73d120
            ):
Pierre-Yves Chibon 9d1740
Pierre-Yves Chibon 9d1740
                output = self.app.post(
Pierre-Yves Chibon 73d120
                    "/test/diff/remote", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
                self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9d1740
Pierre-Yves Chibon 73d120
                data["confirm"] = 1
Pierre-Yves Chibon 9d1740
                output = self.app.post(
Pierre-Yves Chibon 73d120
                    "/test/diff/remote", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
                self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9d1740
                output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 9d1740
                self.assertIn(
Pierre-Yves Chibon 9d1740
                    '#1',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
                self.assertIn('
\n', output_text)
Pierre-Yves Chibon 73d120
                self.assertIn('
\n', output_text)
Pierre-Yves Chibon 9d1740
                self.assertNotIn(
Pierre-Yves Chibon 73d120
                    '
\n', output_text
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 3117a1
Pierre-Yves Chibon 3117a1
        # Remote PR Created
Pierre-Yves Chibon 930073
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 3117a1
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon 3117a1
        trigger_ci.assert_not_called()
Pierre-Yves Chibon 3117a1
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.tasks_services.trigger_ci_build")
Pierre-Yves Chibon 3117a1
    def test_new_remote_pr_ci_on(self, trigger_ci):
Pierre-Yves Chibon 3117a1
        """ Test creating a new remote PR when CI is configured. """
Pierre-Yves Chibon 3117a1
Pierre-Yves Chibon 3117a1
        tests.create_projects(self.session)
Pierre-Yves Chibon 3117a1
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3117a1
        self.set_up_git_repo()
Pierre-Yves Chibon 3117a1
Pierre-Yves Chibon 3117a1
        # Before
Pierre-Yves Chibon 930073
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 3117a1
        self.assertEqual(len(project.requests), 0)
Pierre-Yves Chibon 3117a1
Pierre-Yves Chibon 3117a1
        # Create a remote PR
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 3117a1
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 3117a1
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 3117a1
Pierre-Yves Chibon 3117a1
            # Activate CI hook
Pierre-Yves Chibon 3117a1
            data = {
Pierre-Yves Chibon 73d120
                "active_pr": "y",
Pierre-Yves Chibon 73d120
                "ci_url": "https://jenkins.fedoraproject.org",
Pierre-Yves Chibon 73d120
                "ci_job": "test/job",
Pierre-Yves Chibon 73d120
                "ci_type": "jenkins",
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 3117a1
            }
Pierre-Yves Chibon 3117a1
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/settings/Pagure CI", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 3117a1
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 3117a1
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon 3117a1
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 3117a1
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "title": "Remote PR title",
Pierre-Yves Chibon 73d120
                "branch_from": "feature",
Pierre-Yves Chibon 73d120
                "branch_to": "master",
Pierre-Yves Chibon 73d120
                "git_repo": os.path.join(self.newpath, "test"),
Pierre-Yves Chibon 3117a1
            }
Pierre-Yves Chibon 9d1740
            # Disables checking the URL pattern for git_repo
Pierre-Yves Chibon 9d1740
            with patch(
Pierre-Yves Chibon 73d120
                "pagure.forms.RemoteRequestPullForm.git_repo.args",
Pierre-Yves Chibon 73d120
                MagicMock(
Pierre-Yves Chibon 73d120
                    return_value=(
Pierre-Yves Chibon 73d120
                        "Git Repo address",
Pierre-Yves Chibon 73d120
                        [wtforms.validators.DataRequired()],
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                ),
Pierre-Yves Chibon 73d120
            ):
Pierre-Yves Chibon 9d1740
Pierre-Yves Chibon 9d1740
                # Do the preview, triggers the cache & all
Pierre-Yves Chibon 9d1740
                output = self.app.post(
Pierre-Yves Chibon 73d120
                    "/test/diff/remote", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
                self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9d1740
Pierre-Yves Chibon 9d1740
                # Confirm the PR creation
Pierre-Yves Chibon 73d120
                data["confirm"] = 1
Pierre-Yves Chibon 9d1740
                output = self.app.post(
Pierre-Yves Chibon 73d120
                    "/test/diff/remote", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 9d1740
                self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9d1740
                output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 9d1740
                self.assertIn(
Pierre-Yves Chibon 9d1740
                    '#1',
Pierre-Yves Chibon 73d120
                    output_text,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 73d120
                self.assertIn('
\n', output_text)
Pierre-Yves Chibon 73d120
                self.assertIn('
\n', output_text)
Pierre-Yves Chibon 9d1740
                self.assertNotIn(
Pierre-Yves Chibon 73d120
                    '
\n', output_text
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 3117a1
Pierre-Yves Chibon 3117a1
        # Remote PR Created
Pierre-Yves Chibon 930073
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 3117a1
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon 3117a1
        trigger_ci.assert_not_called()
Pierre-Yves Chibon 3117a1
Pierre-Yves Chibon 59f36c
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 9f16a2
    unittest.main(verbosity=2)