Blame tests/test_pagure_flask_rebase.py

Pierre-Yves Chibon cf4c77
# -*- coding: utf-8 -*-
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
"""
Pierre-Yves Chibon cf4c77
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
 Authors:
Pierre-Yves Chibon cf4c77
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
"""
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
import datetime
Pierre-Yves Chibon cf4c77
import unittest
Pierre-Yves Chibon cf4c77
import shutil
Pierre-Yves Chibon cf4c77
import sys
Pierre-Yves Chibon cf4c77
import os
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
import json
Pierre-Yves Chibon cf4c77
from mock import patch, MagicMock
Pierre-Yves Chibon cf4c77
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 cf4c77
Pierre-Yves Chibon cf4c77
import pagure.lib.query
Pierre-Yves Chibon cf4c77
import pagure.lib.tasks
Pierre-Yves Chibon cf4c77
import tests
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
class PagureRebasetests(tests.Modeltests):
Pierre-Yves Chibon cf4c77
    """ Tests rebasing pull-request in pagure """
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
    maxDiff = None
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon cf4c77
    def setUp(self):
Pierre-Yves Chibon cf4c77
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon cf4c77
        super(PagureRebasetests, self).setUp()
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
Pierre-Yves Chibon cf4c77
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon cf4c77
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon cf4c77
        tests.add_content_to_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"),
Pierre-Yves Chibon 73d120
            branch="master",
Pierre-Yves Chibon 73d120
            content="foobarbaz",
Pierre-Yves Chibon 73d120
            filename="testfile",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon b983f9
        tests.add_content_to_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"),
Pierre-Yves Chibon 73d120
            branch="test",
Pierre-Yves Chibon 73d120
            content="foobar",
Pierre-Yves Chibon 73d120
            filename="sources",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
        # Create a PR for these changes
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon cf4c77
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon cf4c77
            session=self.session,
Pierre-Yves Chibon cf4c77
            repo_from=project,
Pierre-Yves Chibon 73d120
            branch_from="test",
Pierre-Yves Chibon cf4c77
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="PR from the test branch",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon b2d897
            allow_rebase=True,
Pierre-Yves Chibon cf4c77
        )
Pierre-Yves Chibon cf4c77
        self.session.commit()
Pierre-Yves Chibon cf4c77
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "PR from the test branch")
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
        self.project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon cf4c77
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon cf4c77
        self.request = self.project.requests[0]
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
    def test_merge_status_merge(self):
Pierre-Yves Chibon cf4c77
        """ Test that the PR can be merged with a merge commit. """
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon cf4c77
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            data = {
Pierre-Yves Chibon 73d120
                "requestid": self.request.uid,
Pierre-Yves Chibon 73d120
                "csrf_token": self.get_csrf(),
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon cf4c77
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon cf4c77
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon cf4c77
            self.assertEqual(
Pierre-Yves Chibon cf4c77
                data,
Pierre-Yves Chibon cf4c77
                {
Pierre-Yves Chibon 73d120
                    "code": "MERGE",
Pierre-Yves Chibon 73d120
                    "message": "The pull-request can be merged with a "
Pierre-Yves Chibon 73d120
                    "merge commit",
Pierre-Yves Chibon 73d120
                    "short_code": "With merge",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon cf4c77
            )
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
    def test_merge_status_needsrebase(self):
Pierre-Yves Chibon cf4c77
        """ Test that the PR is marked as needing a rebase if the project
Pierre-Yves Chibon cf4c77
        disables non-fast-forward merges. """
Pierre-Yves Chibon cf4c77
        self.project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon cf4c77
        settings = self.project.settings
Pierre-Yves Chibon 73d120
        settings["disable_non_fast-forward_merges"] = True
Pierre-Yves Chibon cf4c77
        self.project.settings = settings
Pierre-Yves Chibon cf4c77
        self.session.add(self.project)
Pierre-Yves Chibon cf4c77
        self.session.commit()
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon cf4c77
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            data = {
Pierre-Yves Chibon 73d120
                "requestid": self.request.uid,
Pierre-Yves Chibon 73d120
                "csrf_token": self.get_csrf(),
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon cf4c77
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon cf4c77
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon cf4c77
            self.assertEqual(
Pierre-Yves Chibon cf4c77
                data,
Pierre-Yves Chibon cf4c77
                {
Pierre-Yves Chibon 73d120
                    "code": "NEEDSREBASE",
Pierre-Yves Chibon 73d120
                    "message": "The pull-request must be rebased before "
Pierre-Yves Chibon 73d120
                    "merging",
Pierre-Yves Chibon 73d120
                    "short_code": "Needs rebase",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon cf4c77
            )
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
    def test_rebase_task(self):
Pierre-Yves Chibon cf4c77
        """ Test the rebase PR task and its outcome. """
Pierre-Yves Chibon cf4c77
        pagure.lib.tasks.rebase_pull_request(
Pierre-Yves Chibon 73d120
            "test",
Pierre-Yves Chibon 73d120
            namespace=None,
Pierre-Yves Chibon 73d120
            user=None,
Pierre-Yves Chibon 73d120
            requestid=self.request.id,
Pierre-Yves Chibon 73d120
            user_rebaser="pingou",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon cf4c77
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            data = {
Pierre-Yves Chibon 73d120
                "requestid": self.request.uid,
Pierre-Yves Chibon 73d120
                "csrf_token": self.get_csrf(),
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon cf4c77
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon cf4c77
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon cf4c77
            self.assertEqual(
Pierre-Yves Chibon cf4c77
                data,
Pierre-Yves Chibon cf4c77
                {
Pierre-Yves Chibon 73d120
                    "code": "FFORWARD",
Pierre-Yves Chibon 73d120
                    "message": "The pull-request can be merged and "
Pierre-Yves Chibon 73d120
                    "fast-forwarded",
Pierre-Yves Chibon 73d120
                    "short_code": "Ok",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon cf4c77
            )
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
    def test_rebase_api_ui_logged_in(self):
Pierre-Yves Chibon cf4c77
        """ Test the rebase PR API endpoint when logged in from the UI and
Pierre-Yves Chibon cf4c77
        its outcome. """
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon cf4c77
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon b983f9
            # Get the merge status first so it's cached and can be refreshed
Pierre-Yves Chibon b983f9
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"requestid": self.request.uid, "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon b983f9
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon b983f9
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon b983f9
            self.assertEqual(
Pierre-Yves Chibon b983f9
                data,
Pierre-Yves Chibon b983f9
                {
Pierre-Yves Chibon 73d120
                    "code": "MERGE",
Pierre-Yves Chibon 73d120
                    "message": "The pull-request can be merged with "
Pierre-Yves Chibon 73d120
                    "a merge commit",
Pierre-Yves Chibon 73d120
                    "short_code": "With merge",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon b983f9
            )
Pierre-Yves Chibon b983f9
Pierre-Yves Chibon 73d120
            output = self.app.post("/api/0/test/pull-request/1/rebase")
Pierre-Yves Chibon cf4c77
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon cf4c77
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            self.assertEqual(data, {"message": "Pull-request rebased"})
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
            data = {"requestid": self.request.uid, "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon cf4c77
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon cf4c77
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon cf4c77
            self.assertEqual(
Pierre-Yves Chibon cf4c77
                data,
Pierre-Yves Chibon cf4c77
                {
Pierre-Yves Chibon 73d120
                    "code": "FFORWARD",
Pierre-Yves Chibon 73d120
                    "message": "The pull-request can be merged and "
Pierre-Yves Chibon 73d120
                    "fast-forwarded",
Pierre-Yves Chibon 73d120
                    "short_code": "Ok",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon cf4c77
            )
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/pull-request/1")
Pierre-Yves Chibon b983f9
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon b983f9
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            self.assertIn("rebased onto", output_text)
Pierre-Yves Chibon 73d120
            repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon b983f9
            self.assertEqual(
Pierre-Yves Chibon 73d120
                repo.requests[0].comments[0].user.username, "pingou"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b983f9
Pierre-Yves Chibon b983f9
    def test_rebase_api_ui_logged_in_different_user(self):
Pierre-Yves Chibon b983f9
        """ Test the rebase PR API endpoint when logged in from the UI and
Pierre-Yves Chibon b983f9
        its outcome. """
Pierre-Yves Chibon b983f9
        # Add 'foo' to the project 'test' so 'foo' can rebase the PR
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon b983f9
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon 73d120
            session=self.session, project=repo, new_user="foo", user="pingou"
Pierre-Yves Chibon b983f9
        )
Pierre-Yves Chibon b983f9
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User added")
Pierre-Yves Chibon b983f9
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon b983f9
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon b983f9
            # Get the merge status first so it's cached and can be refreshed
Pierre-Yves Chibon b983f9
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"requestid": self.request.uid, "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon b983f9
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon b983f9
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon b983f9
            self.assertEqual(
Pierre-Yves Chibon b983f9
                data,
Pierre-Yves Chibon b983f9
                {
Pierre-Yves Chibon 73d120
                    "code": "MERGE",
Pierre-Yves Chibon 73d120
                    "message": "The pull-request can be merged with "
Pierre-Yves Chibon 73d120
                    "a merge commit",
Pierre-Yves Chibon 73d120
                    "short_code": "With merge",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon b983f9
            )
Pierre-Yves Chibon b983f9
Pierre-Yves Chibon 73d120
            output = self.app.post("/api/0/test/pull-request/1/rebase")
Pierre-Yves Chibon b983f9
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon b983f9
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            self.assertEqual(data, {"message": "Pull-request rebased"})
Pierre-Yves Chibon b983f9
Pierre-Yves Chibon 73d120
            data = {"requestid": self.request.uid, "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon b983f9
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon b983f9
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon b983f9
            self.assertEqual(
Pierre-Yves Chibon b983f9
                data,
Pierre-Yves Chibon b983f9
                {
Pierre-Yves Chibon 73d120
                    "code": "FFORWARD",
Pierre-Yves Chibon 73d120
                    "message": "The pull-request can be merged and "
Pierre-Yves Chibon 73d120
                    "fast-forwarded",
Pierre-Yves Chibon 73d120
                    "short_code": "Ok",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon b983f9
            )
Pierre-Yves Chibon b983f9
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/pull-request/1")
Pierre-Yves Chibon b983f9
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon b983f9
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            self.assertIn("rebased onto", output_text)
Pierre-Yves Chibon 73d120
            repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 73d120
            self.assertEqual(repo.requests[0].comments[0].user.username, "foo")
Pierre-Yves Chibon b983f9
Pierre-Yves Chibon cf4c77
    def test_rebase_api_api_logged_in(self):
Pierre-Yves Chibon cf4c77
        """ Test the rebase PR API endpoint when using an API token and
Pierre-Yves Chibon cf4c77
        its outcome. """
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
        tests.create_tokens(self.session)
Pierre-Yves Chibon cf4c77
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/rebase", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon cf4c77
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon cf4c77
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(data, {"message": "Pull-request rebased"})
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon cf4c77
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
            data = {
Pierre-Yves Chibon 73d120
                "requestid": self.request.uid,
Pierre-Yves Chibon 73d120
                "csrf_token": self.get_csrf(),
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon cf4c77
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon cf4c77
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon cf4c77
            self.assertEqual(
Pierre-Yves Chibon cf4c77
                data,
Pierre-Yves Chibon cf4c77
                {
Pierre-Yves Chibon 73d120
                    "code": "FFORWARD",
Pierre-Yves Chibon 73d120
                    "message": "The pull-request can be merged and "
Pierre-Yves Chibon 73d120
                    "fast-forwarded",
Pierre-Yves Chibon 73d120
                    "short_code": "Ok",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon cf4c77
            )
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
    def test_rebase_api_conflicts(self):
Pierre-Yves Chibon cf4c77
        """ Test the rebase PR API endpoint when logged in from the UI and
Pierre-Yves Chibon cf4c77
        its outcome. """
Pierre-Yves Chibon cf4c77
        tests.add_content_to_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"),
Pierre-Yves Chibon 73d120
            branch="master",
Pierre-Yves Chibon 73d120
            content="foobar baz",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon cf4c77
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.post("/api/0/test/pull-request/1/rebase")
Pierre-Yves Chibon cf4c77
            self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon cf4c77
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon cf4c77
            self.assertEqual(
Pierre-Yves Chibon cf4c77
                data,
Pierre-Yves Chibon cf4c77
                {
Pierre-Yves Chibon 73d120
                    "error": "Did not manage to rebase this pull-request",
Pierre-Yves Chibon 73d120
                    "error_code": "ENOCODE",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon cf4c77
            )
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
            data = {
Pierre-Yves Chibon 73d120
                "requestid": self.request.uid,
Pierre-Yves Chibon 73d120
                "csrf_token": self.get_csrf(),
Pierre-Yves Chibon 73d120
            }
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon cf4c77
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon cf4c77
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon cf4c77
            self.assertEqual(
Pierre-Yves Chibon cf4c77
                data,
Pierre-Yves Chibon cf4c77
                {
Pierre-Yves Chibon 73d120
                    "code": "CONFLICTS",
Pierre-Yves Chibon 73d120
                    "message": "The pull-request cannot be merged due "
Pierre-Yves Chibon 73d120
                    "to conflicts",
Pierre-Yves Chibon 73d120
                    "short_code": "Conflicts",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon cf4c77
            )
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
    def test_rebase_api_api_logged_in_unknown_project(self):
Pierre-Yves Chibon cf4c77
        """ Test the rebase PR API endpoint when the project doesn't exist """
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
        tests.create_tokens(self.session)
Pierre-Yves Chibon cf4c77
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/unknown/pull-request/1/rebase", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon cf4c77
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon cf4c77
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon cf4c77
        self.assertEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon cf4c77
        )
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
    def test_rebase_api_api_logged_in_unknown_pr(self):
Pierre-Yves Chibon cf4c77
        """ Test the rebase PR API endpoint when the PR doesn't exist """
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
        tests.create_tokens(self.session)
Pierre-Yves Chibon cf4c77
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/404/rebase", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon cf4c77
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon cf4c77
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon cf4c77
        self.assertEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon cf4c77
        )
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
    def test_rebase_api_api_logged_in_unknown_token(self):
Pierre-Yves Chibon cf4c77
        """ Test the rebase PR API endpoint with an invalid API token """
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
        tests.create_tokens(self.session)
Pierre-Yves Chibon cf4c77
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token unknown"}
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/rebase", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon cf4c77
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon cf4c77
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon cf4c77
        self.assertEqual(
Pierre-Yves Chibon cf4c77
            data,
Pierre-Yves Chibon cf4c77
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or expired token. Please visit "
Julen Landa Alustiza d477d2
                "http://localhost.localdomain/settings#nav-api-tab to get "
Pierre-Yves Chibon 73d120
                "or renew your API token.",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDTOK",
Pierre-Yves Chibon 73d120
                "errors": "Invalid token",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon cf4c77
        )
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon cf4c77
Pierre-Yves Chibon b2d897
class PagureRebaseNotAllowedtests(tests.Modeltests):
Pierre-Yves Chibon b2d897
    """ Tests rebasing pull-request in pagure """
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
    maxDiff = None
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon b2d897
    def setUp(self):
Pierre-Yves Chibon b2d897
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon b2d897
        super(PagureRebaseNotAllowedtests, self).setUp()
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
        pagure.config.config["REQUESTS_FOLDER"] = None
Pierre-Yves Chibon b2d897
        tests.create_projects(self.session)
Pierre-Yves Chibon b2d897
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon b2d897
        tests.create_projects_git(
Pierre-Yves Chibon b2d897
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon b2d897
        )
Pierre-Yves Chibon b2d897
        tests.add_content_to_git(
Pierre-Yves Chibon b2d897
            os.path.join(self.path, "repos", "test.git"),
Pierre-Yves Chibon b2d897
            branch="master",
Pierre-Yves Chibon b2d897
            content="foobarbaz",
Pierre-Yves Chibon b2d897
            filename="testfile",
Pierre-Yves Chibon b2d897
        )
Pierre-Yves Chibon b2d897
        tests.add_content_to_git(
Pierre-Yves Chibon b2d897
            os.path.join(self.path, "repos", "test.git"),
Pierre-Yves Chibon b2d897
            branch="test",
Pierre-Yves Chibon b2d897
            content="foobar",
Pierre-Yves Chibon b2d897
            filename="sources",
Pierre-Yves Chibon b2d897
        )
Pierre-Yves Chibon b2d897
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
        # Create a PR for these changes
Pierre-Yves Chibon b2d897
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon b2d897
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon b2d897
            session=self.session,
Pierre-Yves Chibon b2d897
            repo_from=project,
Pierre-Yves Chibon b2d897
            branch_from="test",
Pierre-Yves Chibon b2d897
            repo_to=project,
Pierre-Yves Chibon b2d897
            branch_to="master",
Pierre-Yves Chibon b2d897
            title="PR from the test branch",
Pierre-Yves Chibon b2d897
            user="pingou",
Pierre-Yves Chibon b2d897
            allow_rebase=False,
Pierre-Yves Chibon b2d897
        )
Pierre-Yves Chibon b2d897
        self.session.commit()
Pierre-Yves Chibon b2d897
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon b2d897
        self.assertEqual(req.title, "PR from the test branch")
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
        self.project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon b2d897
            self.session, "test"
Pierre-Yves Chibon b2d897
        )
Pierre-Yves Chibon b2d897
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon b2d897
        self.request = self.project.requests[0]
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
    def test_rebase_api_ui_logged_in(self):
Pierre-Yves Chibon b2d897
        """ Test the rebase PR API endpoint when logged in from the UI and
Pierre-Yves Chibon b2d897
        its outcome. """
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon b2d897
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon b2d897
            # Get the merge status first so it's cached and can be refreshed
Pierre-Yves Chibon b2d897
            csrf_token = self.get_csrf()
Pierre-Yves Chibon b2d897
            data = {"requestid": self.request.uid, "csrf_token": csrf_token}
Pierre-Yves Chibon b2d897
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon b2d897
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon b2d897
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon b2d897
            self.assertEqual(
Pierre-Yves Chibon b2d897
                data,
Pierre-Yves Chibon b2d897
                {
Pierre-Yves Chibon b2d897
                    "code": "MERGE",
Pierre-Yves Chibon b2d897
                    "message": "The pull-request can be merged with "
Pierre-Yves Chibon b2d897
                    "a merge commit",
Pierre-Yves Chibon b2d897
                    "short_code": "With merge",
Pierre-Yves Chibon b2d897
                },
Pierre-Yves Chibon b2d897
            )
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
            output = self.app.post("/api/0/test/pull-request/1/rebase")
Pierre-Yves Chibon b2d897
            self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon b2d897
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon b2d897
            self.assertEqual(
Pierre-Yves Chibon b2d897
                data,
Pierre-Yves Chibon b2d897
                {
Pierre-Yves Chibon b2d897
                    "error": "You are not authorized to rebase this pull-request",
Pierre-Yves Chibon b2d897
                    "error_code": "EREBASENOTALLOWED",
Pierre-Yves Chibon b2d897
                },
Pierre-Yves Chibon b2d897
            )
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
    def test_rebase_api_ui_logged_in_different_user(self):
Pierre-Yves Chibon b2d897
        """ Test the rebase PR API endpoint when logged in from the UI and
Pierre-Yves Chibon b2d897
        its outcome. """
Pierre-Yves Chibon b2d897
        # Add 'foo' to the project 'test' so 'foo' can rebase the PR
Pierre-Yves Chibon b2d897
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon b2d897
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon b2d897
            session=self.session, project=repo, new_user="foo", user="pingou"
Pierre-Yves Chibon b2d897
        )
Pierre-Yves Chibon b2d897
        self.session.commit()
Pierre-Yves Chibon b2d897
        self.assertEqual(msg, "User added")
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon b2d897
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon b2d897
            # Get the merge status first so it's cached and can be refreshed
Pierre-Yves Chibon b2d897
            csrf_token = self.get_csrf()
Pierre-Yves Chibon b2d897
            data = {"requestid": self.request.uid, "csrf_token": csrf_token}
Pierre-Yves Chibon b2d897
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon b2d897
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon b2d897
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon b2d897
            self.assertEqual(
Pierre-Yves Chibon b2d897
                data,
Pierre-Yves Chibon b2d897
                {
Pierre-Yves Chibon b2d897
                    "code": "MERGE",
Pierre-Yves Chibon b2d897
                    "message": "The pull-request can be merged with "
Pierre-Yves Chibon b2d897
                    "a merge commit",
Pierre-Yves Chibon b2d897
                    "short_code": "With merge",
Pierre-Yves Chibon b2d897
                },
Pierre-Yves Chibon b2d897
            )
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
            output = self.app.post("/api/0/test/pull-request/1/rebase")
Pierre-Yves Chibon b2d897
            self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon b2d897
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon b2d897
            self.assertEqual(
Pierre-Yves Chibon b2d897
                data,
Pierre-Yves Chibon b2d897
                {
Pierre-Yves Chibon b2d897
                    "error": "You are not authorized to rebase this pull-request",
Pierre-Yves Chibon b2d897
                    "error_code": "EREBASENOTALLOWED",
Pierre-Yves Chibon b2d897
                },
Pierre-Yves Chibon b2d897
            )
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
    def test_rebase_api_api_logged_in(self):
Pierre-Yves Chibon b2d897
        """ Test the rebase PR API endpoint when using an API token and
Pierre-Yves Chibon b2d897
        its outcome. """
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
        tests.create_tokens(self.session)
Pierre-Yves Chibon b2d897
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
        output = self.app.post(
Pierre-Yves Chibon b2d897
            "/api/0/test/pull-request/1/rebase", headers=headers
Pierre-Yves Chibon b2d897
        )
Pierre-Yves Chibon b2d897
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon b2d897
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon b2d897
        self.assertEqual(
Pierre-Yves Chibon b2d897
            data,
Pierre-Yves Chibon b2d897
            {
Pierre-Yves Chibon b2d897
                "error": "You are not authorized to rebase this pull-request",
Pierre-Yves Chibon b2d897
                "error_code": "EREBASENOTALLOWED",
Pierre-Yves Chibon b2d897
            },
Pierre-Yves Chibon b2d897
        )
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon b2d897
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon cf4c77
    unittest.main(verbosity=2)