Blame tests/test_pagure_merge_pr_no_fork.py

Pierre-Yves Chibon 993f74
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
"""
Pierre-Yves Chibon 993f74
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
 Authors:
Pierre-Yves Chibon 993f74
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
"""
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
import unittest
Pierre-Yves Chibon 993f74
import sys
Pierre-Yves Chibon 993f74
import os
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
import json
Pierre-Yves Chibon 993f74
from mock import patch, MagicMock
Pierre-Yves Chibon 993f74
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 993f74
Pierre-Yves Chibon 993f74
import pagure.config
Pierre-Yves Chibon 993f74
import pagure.lib.query
Pierre-Yves Chibon 993f74
import pagure.lib.tasks
Pierre-Yves Chibon 993f74
import tests
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
class PagureMergePrNoForkTest(tests.Modeltests):
Pierre-Yves Chibon 993f74
    """ Tests merging a PR in pagure when the fork no longer exists """
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
    maxDiff = None
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
    def setUp(self):
Pierre-Yves Chibon 993f74
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 993f74
        super(PagureMergePrNoForkTest, self).setUp()
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        tests.create_projects(self.session)
Pierre-Yves Chibon 993f74
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fe8d0f
Pierre-Yves Chibon fe8d0f
        # Fork
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon fe8d0f
        task = pagure.lib.query.fork_project(
Pierre-Yves Chibon 73d120
            session=self.session, user="pingou", repo=project
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon fe8d0f
        self.session.commit()
Pierre-Yves Chibon fe8d0f
        self.assertEqual(
Pierre-Yves Chibon fe8d0f
            task.get(),
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "endpoint": "ui_ns.view_repo",
Pierre-Yves Chibon 73d120
                "repo": "test",
Pierre-Yves Chibon 73d120
                "namespace": None,
Pierre-Yves Chibon 73d120
                "username": "pingou",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fe8d0f
Pierre-Yves Chibon 993f74
        tests.add_readme_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "pingou", "test.git")
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 993f74
        fork = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="pingou"
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        tests.create_tokens(self.session)
Pierre-Yves Chibon 993f74
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 993f74
            session=self.session,
Pierre-Yves Chibon 993f74
            repo_from=fork,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 993f74
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
        self.session.commit()
Pierre-Yves Chibon 993f74
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        # Assert the PR is open
Pierre-Yves Chibon 993f74
        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 993f74
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon 993f74
        self.assertEqual(project.requests[0].status, "Open")
Pierre-Yves Chibon 993f74
        # Check how the PR renders in the API and the UI
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1")
Pierre-Yves Chibon 993f74
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/pull-request/1")
Pierre-Yves Chibon 993f74
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 993f74
    def test_api_pull_request_diffstats(self):
Pierre-Yves Chibon 993f74
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        # Check the PR stats in the API
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1/diffstats")
Pierre-Yves Chibon 993f74
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 993f74
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 993f74
        self.assertDictEqual(
Pierre-Yves Chibon 993f74
            data,
Pierre-Yves Chibon 993f74
            {
Pierre-Yves Chibon 993f74
                "README.rst": {
Pierre-Yves Chibon 993f74
                    "lines_added": 16,
Pierre-Yves Chibon 993f74
                    "lines_removed": 0,
Pierre-Yves Chibon 993f74
                    "new_id": "fb7093d2ba1cf8f80d10b45e4f15b10240727db5",
Pierre-Yves Chibon 993f74
                    "old_id": "0000000000000000000000000000000000000000",
Pierre-Yves Chibon 993f74
                    "old_path": "README.rst",
Pierre-Yves Chibon 73d120
                    "status": "A",
Pierre-Yves Chibon 993f74
                }
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 993f74
    def test_api_pull_request_diffstats_no_fork(self):
Pierre-Yves Chibon 993f74
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        pagure.lib.tasks.delete_project(
Pierre-Yves Chibon 73d120
            namespace=None, name="test", user="pingou", action_user="pingou"
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        # Check the PR stats in the API
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1/diffstats")
Pierre-Yves Chibon 993f74
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 993f74
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 993f74
        self.assertDictEqual(
Pierre-Yves Chibon 993f74
            data,
Pierre-Yves Chibon 993f74
            {
Pierre-Yves Chibon 993f74
                "README.rst": {
Pierre-Yves Chibon 993f74
                    "lines_added": 16,
Pierre-Yves Chibon 993f74
                    "lines_removed": 0,
Pierre-Yves Chibon 993f74
                    "new_id": "fb7093d2ba1cf8f80d10b45e4f15b10240727db5",
Pierre-Yves Chibon 993f74
                    "old_id": "0000000000000000000000000000000000000000",
Pierre-Yves Chibon 993f74
                    "old_path": "README.rst",
Pierre-Yves Chibon 73d120
                    "status": "A",
Pierre-Yves Chibon 993f74
                }
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 993f74
    def test_api_pull_request_merge(self):
Pierre-Yves Chibon 993f74
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        # Merge PR
Pierre-Yves Chibon 993f74
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 993f74
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 993f74
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Changes merged!"})
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 993f74
    def test_api_pull_request_merge_no_fork(self):
Pierre-Yves Chibon 993f74
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        pagure.lib.tasks.delete_project(
Pierre-Yves Chibon 73d120
            namespace=None, name="test", user="pingou", action_user="pingou"
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        # Merge PR
Pierre-Yves Chibon 993f74
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 993f74
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 993f74
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Changes merged!"})
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 993f74
    def test_ui_pull_request_merge(self):
Pierre-Yves Chibon 993f74
        """ Test the api_pull_request_merge method of the flask UI. """
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 993f74
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            data = {"csrf_token": self.get_csrf()}
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
            # Merge PR
Pierre-Yves Chibon 993f74
            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 993f74
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 993f74
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 993f74
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Overview - test - Pagure</title>", output_text
Pierre-Yves Chibon 993f74
            )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        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 993f74
        self.assertEqual(project.requests[0].status, "Merged")
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 993f74
    def test_ui_pull_request_merge_no_fork(self):
Pierre-Yves Chibon 993f74
        """ Test the api_pull_request_merge method of the flask UI. """
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        pagure.lib.tasks.delete_project(
Pierre-Yves Chibon 73d120
            namespace=None, name="test", user="pingou", action_user="pingou"
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 993f74
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            data = {"csrf_token": self.get_csrf()}
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
            # Merge PR
Pierre-Yves Chibon 993f74
            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 993f74
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 993f74
            output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 993f74
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Overview - test - Pagure</title>", output_text
Pierre-Yves Chibon 993f74
            )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        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 993f74
        self.assertEqual(project.requests[0].status, "Merged")
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 28b763
    def test_internal_merge_status(self):
Pierre-Yves Chibon 28b763
        """ Test the api_pull_request_merge method of the flask UI. """
Pierre-Yves Chibon 28b763
Pierre-Yves Chibon 28b763
        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 28b763
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 28b763
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 28b763
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": self.get_csrf(),
Pierre-Yves Chibon 73d120
                "requestid": project.requests[0].uid,
Pierre-Yves Chibon 28b763
            }
Pierre-Yves Chibon 28b763
Pierre-Yves Chibon 28b763
            # Merge PR
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon 28b763
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 28b763
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 28b763
            self.assertDictEqual(
Pierre-Yves Chibon 28b763
                data,
Pierre-Yves Chibon 28b763
                {
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 28b763
            )
Pierre-Yves Chibon 28b763
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 28b763
    def test_internal_merge_status_no_fork(self):
Pierre-Yves Chibon 28b763
        """ Test the api_pull_request_merge method of the flask UI. """
Pierre-Yves Chibon 28b763
Pierre-Yves Chibon 28b763
        pagure.lib.tasks.delete_project(
Pierre-Yves Chibon 73d120
            namespace=None, name="test", user="pingou", action_user="pingou"
Pierre-Yves Chibon 28b763
        )
Pierre-Yves Chibon 28b763
Pierre-Yves Chibon 28b763
        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 28b763
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Pierre-Yves Chibon 28b763
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 28b763
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": self.get_csrf(),
Pierre-Yves Chibon 73d120
                "requestid": project.requests[0].uid,
Pierre-Yves Chibon 28b763
            }
Pierre-Yves Chibon 28b763
Pierre-Yves Chibon 28b763
            # Merge PR
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon 28b763
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 28b763
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 28b763
            self.assertDictEqual(
Pierre-Yves Chibon 28b763
                data,
Pierre-Yves Chibon 28b763
                {
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 28b763
            )
Pierre-Yves Chibon 28b763
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 993f74
    unittest.main(verbosity=2)