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 993f74
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 993f74
    os.path.abspath(__file__)), '..'))
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.create_projects_git(
Pierre-Yves Chibon 993f74
            os.path.join(self.path, "repos"),
Pierre-Yves Chibon 993f74
            bare=True
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
        tests.add_content_git_repo(
Pierre-Yves Chibon 993f74
            os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 993f74
        tests.create_projects(
Pierre-Yves Chibon 993f74
            self.session,
Pierre-Yves Chibon 993f74
            is_fork=True,
Pierre-Yves Chibon 993f74
            hook_token_suffix='fork')
Pierre-Yves Chibon 993f74
        tests.create_projects_git(
Pierre-Yves Chibon 993f74
            os.path.join(self.path, "repos", "forks", "pingou"),
Pierre-Yves Chibon 993f74
            bare=True
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
        tests.add_content_git_repo(
Pierre-Yves Chibon 993f74
            os.path.join(self.path, "repos", "forks", "pingou", "test.git"))
Pierre-Yves Chibon 993f74
        tests.add_readme_git_repo(
Pierre-Yves Chibon 993f74
            os.path.join(self.path, "repos", "forks", "pingou", "test.git"))
Pierre-Yves Chibon 993f74
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 993f74
            self.session, 'test')
Pierre-Yves Chibon 993f74
        fork = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 993f74
            self.session,
Pierre-Yves Chibon 993f74
            'test',
Pierre-Yves Chibon 993f74
            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 993f74
            branch_from='master',
Pierre-Yves Chibon 993f74
            repo_to=project,
Pierre-Yves Chibon 993f74
            branch_to='master',
Pierre-Yves Chibon 993f74
            title='test pull-request',
Pierre-Yves Chibon 993f74
            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 993f74
        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 993f74
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 993f74
            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 993f74
        output = self.app.get('/api/0/test/pull-request/1')
Pierre-Yves Chibon 993f74
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 993f74
        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 993f74
    @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 993f74
        output = self.app.get(
Pierre-Yves Chibon 993f74
            '/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 993f74
                    "status": "A"
Pierre-Yves Chibon 993f74
                }
Pierre-Yves Chibon 993f74
            }
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
    @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 993f74
            namespace=None,
Pierre-Yves Chibon 993f74
            name="test",
Pierre-Yves Chibon 993f74
            user="pingou",
Pierre-Yves Chibon 993f74
            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 993f74
        output = self.app.get(
Pierre-Yves Chibon 993f74
            '/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 993f74
                    "status": "A"
Pierre-Yves Chibon 993f74
                }
Pierre-Yves Chibon 993f74
            }
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
    @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 993f74
        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 993f74
            '/api/0/test/pull-request/1/merge', headers=headers)
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
            {"message": "Changes merged!"}
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
    @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 993f74
            namespace=None,
Pierre-Yves Chibon 993f74
            name="test",
Pierre-Yves Chibon 993f74
            user="pingou",
Pierre-Yves Chibon 993f74
            action_user="pingou",
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        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 993f74
            '/api/0/test/pull-request/1/merge', headers=headers)
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
            {"message": "Changes merged!"}
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
    @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 993f74
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 993f74
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 993f74
            data = {
Pierre-Yves Chibon 993f74
                'csrf_token': self.get_csrf()
Pierre-Yves Chibon 993f74
            }
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
            # Merge PR
Pierre-Yves Chibon 993f74
            output = self.app.post(
Pierre-Yves Chibon 993f74
                '/test/pull-request/1/merge', data=data, follow_redirects=True)
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 993f74
                "<title>Overview - test - Pagure</title>",
Pierre-Yves Chibon 993f74
                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 993f74
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 993f74
            self.session, 'test')
Pierre-Yves Chibon 993f74
        self.assertEqual(project.requests[0].status, "Merged")
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
    @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 993f74
            namespace=None,
Pierre-Yves Chibon 993f74
            name="test",
Pierre-Yves Chibon 993f74
            user="pingou",
Pierre-Yves Chibon 993f74
            action_user="pingou",
Pierre-Yves Chibon 993f74
        )
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
        user = tests.FakeUser(username='pingou')
Pierre-Yves Chibon 993f74
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 993f74
            data = {
Pierre-Yves Chibon 993f74
                'csrf_token': self.get_csrf()
Pierre-Yves Chibon 993f74
            }
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
            # Merge PR
Pierre-Yves Chibon 993f74
            output = self.app.post(
Pierre-Yves Chibon 993f74
                '/test/pull-request/1/merge', data=data, follow_redirects=True)
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 993f74
                "<title>Overview - test - Pagure</title>",
Pierre-Yves Chibon 993f74
                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 993f74
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 993f74
            self.session, 'test')
Pierre-Yves Chibon 993f74
        self.assertEqual(project.requests[0].status, "Merged")
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 28b763
    @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 28b763
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 28b763
            self.session, 'test')
Pierre-Yves Chibon 28b763
Pierre-Yves Chibon 28b763
        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 28b763
                'csrf_token': self.get_csrf(),
Pierre-Yves Chibon 28b763
                'requestid': project.requests[0].uid,
Pierre-Yves Chibon 28b763
            }
Pierre-Yves Chibon 28b763
Pierre-Yves Chibon 28b763
            # Merge PR
Pierre-Yves Chibon 28b763
            output = self.app.post(
Pierre-Yves Chibon 28b763
                '/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 28b763
                    u'code': u'FFORWARD',
Pierre-Yves Chibon 28b763
                    u'message': u'The pull-request can be merged and '
Pierre-Yves Chibon 28b763
                        'fast-forwarded',
Pierre-Yves Chibon 28b763
                    u'short_code': u'Ok'
Pierre-Yves Chibon 28b763
                }
Pierre-Yves Chibon 28b763
            )
Pierre-Yves Chibon 28b763
Pierre-Yves Chibon 28b763
    @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 28b763
            namespace=None,
Pierre-Yves Chibon 28b763
            name="test",
Pierre-Yves Chibon 28b763
            user="pingou",
Pierre-Yves Chibon 28b763
            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 28b763
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 28b763
            self.session, 'test')
Pierre-Yves Chibon 28b763
Pierre-Yves Chibon 28b763
        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 28b763
                'csrf_token': self.get_csrf(),
Pierre-Yves Chibon 28b763
                'requestid': project.requests[0].uid,
Pierre-Yves Chibon 28b763
            }
Pierre-Yves Chibon 28b763
Pierre-Yves Chibon 28b763
            # Merge PR
Pierre-Yves Chibon 28b763
            output = self.app.post(
Pierre-Yves Chibon 28b763
                '/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 28b763
                    u'code': u'FFORWARD',
Pierre-Yves Chibon 28b763
                    u'message': u'The pull-request can be merged and '
Pierre-Yves Chibon 28b763
                        'fast-forwarded',
Pierre-Yves Chibon 28b763
                    u'short_code': u'Ok'
Pierre-Yves Chibon 28b763
                }
Pierre-Yves Chibon 28b763
            )
Pierre-Yves Chibon 28b763
Pierre-Yves Chibon 993f74
Pierre-Yves Chibon 993f74
if __name__ == '__main__':
Pierre-Yves Chibon 993f74
    unittest.main(verbosity=2)