Blame tests/test_pagure_flask_api_fork_update.py

Pierre-Yves Chibon 5a9017
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
"""
Pierre-Yves Chibon 5a9017
 (c) 2019 - Copyright Red Hat Inc
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
 Authors:
Pierre-Yves Chibon 5a9017
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
"""
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
from __future__ import unicode_literals, absolute_import
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
import arrow
Pierre-Yves Chibon 5a9017
import copy
Pierre-Yves Chibon 5a9017
import datetime
Pierre-Yves Chibon 5a9017
import unittest
Pierre-Yves Chibon 5a9017
import shutil
Pierre-Yves Chibon 5a9017
import sys
Pierre-Yves Chibon 5a9017
import time
Pierre-Yves Chibon 5a9017
import os
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
import flask
Pierre-Yves Chibon 5a9017
import json
Pierre-Yves Chibon 5a9017
import munch
Pierre-Yves Chibon 5a9017
from mock import patch, MagicMock
Pierre-Yves Chibon 5a9017
from sqlalchemy.exc import SQLAlchemyError
Pierre-Yves Chibon 5a9017
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 5a9017
Pierre-Yves Chibon 5a9017
import pagure.lib.query
Pierre-Yves Chibon 5a9017
import tests
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
class PagureFlaskApiForkUpdatetests(tests.SimplePagureTest):
Pierre-Yves Chibon 5a9017
    """ Tests for the flask API of pagure for updating a PR """
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
    maxDiff = None
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 5a9017
    def setUp(self):
Pierre-Yves Chibon 5a9017
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 5a9017
        super(PagureFlaskApiForkUpdatetests, self).setUp()
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        tests.create_projects(self.session)
Pierre-Yves Chibon 5a9017
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        # Fork
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 5a9017
        task = pagure.lib.query.fork_project(
Pierre-Yves Chibon 73d120
            session=self.session, user="pingou", repo=project
Pierre-Yves Chibon 5a9017
        )
Pierre-Yves Chibon 5a9017
        self.session.commit()
Pierre-Yves Chibon 5a9017
        self.assertEqual(
Pierre-Yves Chibon 5a9017
            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 5a9017
Pierre-Yves Chibon 5a9017
        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 5a9017
        fork = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="pingou"
Pierre-Yves Chibon 5a9017
        )
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        tests.create_tokens(self.session)
Pierre-Yves Chibon 5a9017
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 5a9017
            session=self.session,
Pierre-Yves Chibon 5a9017
            repo_from=fork,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 5a9017
            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 5a9017
        )
Pierre-Yves Chibon 5a9017
        self.session.commit()
Pierre-Yves Chibon 5a9017
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        # Assert the PR is open
Pierre-Yves Chibon 5a9017
        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 5a9017
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon 5a9017
        self.assertEqual(project.requests[0].status, "Open")
Pierre-Yves Chibon 5a9017
        # 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 5a9017
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/pull-request/1")
Pierre-Yves Chibon 5a9017
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
    def test_api_pull_request_update_invalid_project_namespace(self):
Pierre-Yves Chibon 5a9017
        """ Test api_pull_request_update method when the project doesn't exist.
Pierre-Yves Chibon 5a9017
        """
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        # Valid token, wrong project
Pierre-Yves Chibon 5a9017
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/somenamespace/test3/pull-request/1", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 5a9017
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 5a9017
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 5a9017
        self.assertDictEqual(
Pierre-Yves Chibon 5a9017
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or expired token. Please visit "
Pierre-Yves Chibon 73d120
                "http://localhost.localdomain/settings#api-keys to get or renew your "
Pierre-Yves Chibon 73d120
                "API token.",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDTOK",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 5a9017
        )
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
    def test_api_pull_request_update_invalid_project(self):
Pierre-Yves Chibon 5a9017
        """ Test api_pull_request_update method when the project doesn't exist.
Pierre-Yves Chibon 5a9017
        """
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        # Invalid project
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/foo/pull-request/1", headers=headers)
Pierre-Yves Chibon 5a9017
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 5a9017
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 5a9017
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 5a9017
        )
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
    def test_api_pull_request_update_invalid_project_token(self):
Pierre-Yves Chibon 5a9017
        """ Test api_pull_request_update method when the token doesn't correspond
Pierre-Yves Chibon 5a9017
        to the project.
Pierre-Yves Chibon 5a9017
        """
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        # Valid token, wrong project
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test2/pull-request/1", headers=headers)
Pierre-Yves Chibon 5a9017
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 5a9017
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(data.keys()), ["error", "error_code"])
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 5a9017
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
    def test_api_pull_request_update_invalid_pr(self):
Pierre-Yves Chibon 5a9017
        """ Test api_assign_pull_request method when asking for an invalid PR
Pierre-Yves Chibon 5a9017
        """
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        # Invalid PR id
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test/pull-request/404", headers=headers)
Pierre-Yves Chibon 5a9017
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 5a9017
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 5a9017
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon 5a9017
        )
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
    def test_api_pull_request_update_no_input(self):
Pierre-Yves Chibon 5a9017
        """ Test api_assign_pull_request method when no input is specified
Pierre-Yves Chibon 5a9017
        """
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        # No input
Pierre-Yves Chibon 73d120
        output = self.app.post("/api/0/test/pull-request/1", headers=headers)
Pierre-Yves Chibon 5a9017
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 5a9017
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 5a9017
        self.assertDictEqual(
Pierre-Yves Chibon 5a9017
            data,
Pierre-Yves Chibon 5a9017
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"title": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 5a9017
        )
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
    def test_api_pull_request_update_insufficient_input(self):
Pierre-Yves Chibon 5a9017
        """ Test api_assign_pull_request method when no input is specified
Pierre-Yves Chibon 5a9017
        """
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        data = {"initial_comment": "will not work"}
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        # Missing the required title field
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 5a9017
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 5a9017
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 5a9017
        self.assertDictEqual(
Pierre-Yves Chibon 5a9017
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"title": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 5a9017
        )
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
    def test_api_pull_request_update_edited(self):
Pierre-Yves Chibon 5a9017
        """ Test api_assign_pull_request method when with valid input
Pierre-Yves Chibon 5a9017
        """
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        data = {
Pierre-Yves Chibon 73d120
            "title": "edited test PR",
Pierre-Yves Chibon 73d120
            "initial_comment": "Edited initial comment",
Pierre-Yves Chibon 5a9017
        }
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        # Valid request
Pierre-Yves Chibon 5a9017
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 5a9017
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5a9017
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 5a9017
        # Hard-code all the values that will change from a test to another
Pierre-Yves Chibon 5a9017
        # because either random or time-based
Pierre-Yves Chibon 73d120
        data["date_created"] = "1551276260"
Pierre-Yves Chibon 73d120
        data["last_updated"] = "1551276261"
Pierre-Yves Chibon 73d120
        data["updated_on"] = "1551276260"
Pierre-Yves Chibon 73d120
        data["commit_start"] = "5f5d609db65d447f77ba00e25afd17ba5053344b"
Pierre-Yves Chibon 73d120
        data["commit_stop"] = "5f5d609db65d447f77ba00e25afd17ba5053344b"
Pierre-Yves Chibon 73d120
        data["project"]["date_created"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["project"]["date_modified"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_created"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_modified"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["repo_from"]["parent"]["date_created"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["repo_from"]["parent"]["date_modified"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["uid"] = "a2bddecc8ea548e88c22a0df77670092"
Pierre-Yves Chibon 5a9017
        self.assertDictEqual(
Pierre-Yves Chibon 5a9017
            data,
Pierre-Yves Chibon 5a9017
            {
Pierre-Yves Chibon 73d120
                "assignee": None,
Pierre-Yves Chibon 73d120
                "branch": "master",
Pierre-Yves Chibon 73d120
                "branch_from": "master",
Pierre-Yves Chibon 73d120
                "cached_merge_status": "unknown",
Pierre-Yves Chibon 73d120
                "closed_at": None,
Pierre-Yves Chibon 73d120
                "closed_by": None,
Pierre-Yves Chibon 73d120
                "comments": [],
Pierre-Yves Chibon 73d120
                "commit_start": "5f5d609db65d447f77ba00e25afd17ba5053344b",
Pierre-Yves Chibon 73d120
                "commit_stop": "5f5d609db65d447f77ba00e25afd17ba5053344b",
Pierre-Yves Chibon 73d120
                "date_created": "1551276260",
Pierre-Yves Chibon 73d120
                "id": 1,
Pierre-Yves Chibon 73d120
                "initial_comment": "Edited initial comment",
Pierre-Yves Chibon 73d120
                "last_updated": "1551276261",
Pierre-Yves Chibon 73d120
                "project": {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                    "access_users": {
Pierre-Yves Chibon 73d120
                        "admin": [],
Pierre-Yves Chibon 73d120
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "close_status": [
Pierre-Yves Chibon 73d120
                        "Invalid",
Pierre-Yves Chibon 73d120
                        "Insufficient data",
Pierre-Yves Chibon 73d120
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Pierre-Yves Chibon 73d120
                    ],
Pierre-Yves Chibon 73d120
                    "custom_keys": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1551276259",
Pierre-Yves Chibon 73d120
                    "date_modified": "1551276259",
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "test",
Pierre-Yves Chibon 73d120
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "milestones": {},
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "parent": None,
Pierre-Yves Chibon 73d120
                    "priorities": {},
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "url_path": "test",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "remote_git": None,
Pierre-Yves Chibon 73d120
                "repo_from": {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                    "access_users": {
Pierre-Yves Chibon 73d120
                        "admin": [],
Pierre-Yves Chibon 73d120
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "close_status": [],
Pierre-Yves Chibon 73d120
                    "custom_keys": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1551276259",
Pierre-Yves Chibon 73d120
                    "date_modified": "1551276259",
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "forks/pingou/test",
Pierre-Yves Chibon 73d120
                    "id": 4,
Pierre-Yves Chibon 73d120
                    "milestones": {},
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "parent": {
Pierre-Yves Chibon 73d120
                        "access_groups": {
Pierre-Yves Chibon 73d120
                            "admin": [],
Pierre-Yves Chibon 73d120
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Pierre-Yves Chibon 73d120
                        },
Pierre-Yves Chibon 73d120
                        "access_users": {
Pierre-Yves Chibon 73d120
                            "admin": [],
Pierre-Yves Chibon 73d120
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Pierre-Yves Chibon 73d120
                        },
Pierre-Yves Chibon 73d120
                        "close_status": [
Pierre-Yves Chibon 73d120
                            "Invalid",
Pierre-Yves Chibon 73d120
                            "Insufficient data",
Pierre-Yves Chibon 73d120
                            "Fixed",
Pierre-Yves Chibon 73d120
                            "Duplicate",
Pierre-Yves Chibon 73d120
                        ],
Pierre-Yves Chibon 73d120
                        "custom_keys": [],
Pierre-Yves Chibon 73d120
                        "date_created": "1551276259",
Pierre-Yves Chibon 73d120
                        "date_modified": "1551276259",
Pierre-Yves Chibon 73d120
                        "description": "test project #1",
Pierre-Yves Chibon 73d120
                        "fullname": "test",
Pierre-Yves Chibon 73d120
                        "id": 1,
Pierre-Yves Chibon 73d120
                        "milestones": {},
Pierre-Yves Chibon 73d120
                        "name": "test",
Pierre-Yves Chibon 73d120
                        "namespace": None,
Pierre-Yves Chibon 73d120
                        "parent": None,
Pierre-Yves Chibon 73d120
                        "priorities": {},
Pierre-Yves Chibon 73d120
                        "tags": [],
Pierre-Yves Chibon 73d120
                        "url_path": "test",
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "priorities": {},
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "url_path": "fork/pingou/test",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "status": "Open",
Pierre-Yves Chibon 73d120
                "tags": [],
Pierre-Yves Chibon 73d120
                "threshold_reached": None,
Pierre-Yves Chibon 73d120
                "title": "edited test PR",
Pierre-Yves Chibon 73d120
                "uid": "a2bddecc8ea548e88c22a0df77670092",
Pierre-Yves Chibon 73d120
                "updated_on": "1551276260",
Pierre-Yves Chibon 73d120
                "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 5a9017
        )
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
    def test_api_pull_request_update_edited_no_comment(self):
Pierre-Yves Chibon 5a9017
        """ Test api_assign_pull_request method when with valid input
Pierre-Yves Chibon 5a9017
        """
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 73d120
        data = {"title": "edited test PR"}
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 5a9017
        # Valid request
Pierre-Yves Chibon 5a9017
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 5a9017
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5a9017
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 5a9017
        # Hard-code all the values that will change from a test to another
Pierre-Yves Chibon 5a9017
        # because either random or time-based
Pierre-Yves Chibon 73d120
        data["date_created"] = "1551276260"
Pierre-Yves Chibon 73d120
        data["last_updated"] = "1551276261"
Pierre-Yves Chibon 73d120
        data["updated_on"] = "1551276260"
Pierre-Yves Chibon 73d120
        data["commit_start"] = "5f5d609db65d447f77ba00e25afd17ba5053344b"
Pierre-Yves Chibon 73d120
        data["commit_stop"] = "5f5d609db65d447f77ba00e25afd17ba5053344b"
Pierre-Yves Chibon 73d120
        data["project"]["date_created"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["project"]["date_modified"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_created"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_modified"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["repo_from"]["parent"]["date_created"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["repo_from"]["parent"]["date_modified"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["uid"] = "a2bddecc8ea548e88c22a0df77670092"
Pierre-Yves Chibon 5a9017
        self.assertDictEqual(
Pierre-Yves Chibon 5a9017
            data,
Pierre-Yves Chibon 5a9017
            {
Pierre-Yves Chibon 73d120
                "assignee": None,
Pierre-Yves Chibon 73d120
                "branch": "master",
Pierre-Yves Chibon 73d120
                "branch_from": "master",
Pierre-Yves Chibon 73d120
                "cached_merge_status": "unknown",
Pierre-Yves Chibon 73d120
                "closed_at": None,
Pierre-Yves Chibon 73d120
                "closed_by": None,
Pierre-Yves Chibon 73d120
                "comments": [],
Pierre-Yves Chibon 73d120
                "commit_start": "5f5d609db65d447f77ba00e25afd17ba5053344b",
Pierre-Yves Chibon 73d120
                "commit_stop": "5f5d609db65d447f77ba00e25afd17ba5053344b",
Pierre-Yves Chibon 73d120
                "date_created": "1551276260",
Pierre-Yves Chibon 73d120
                "id": 1,
Pierre-Yves Chibon 73d120
                "initial_comment": "",
Pierre-Yves Chibon 73d120
                "last_updated": "1551276261",
Pierre-Yves Chibon 73d120
                "project": {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                    "access_users": {
Pierre-Yves Chibon 73d120
                        "admin": [],
Pierre-Yves Chibon 73d120
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "close_status": [
Pierre-Yves Chibon 73d120
                        "Invalid",
Pierre-Yves Chibon 73d120
                        "Insufficient data",
Pierre-Yves Chibon 73d120
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Pierre-Yves Chibon 73d120
                    ],
Pierre-Yves Chibon 73d120
                    "custom_keys": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1551276259",
Pierre-Yves Chibon 73d120
                    "date_modified": "1551276259",
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "test",
Pierre-Yves Chibon 73d120
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "milestones": {},
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "parent": None,
Pierre-Yves Chibon 73d120
                    "priorities": {},
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "url_path": "test",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "remote_git": None,
Pierre-Yves Chibon 73d120
                "repo_from": {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                    "access_users": {
Pierre-Yves Chibon 73d120
                        "admin": [],
Pierre-Yves Chibon 73d120
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "close_status": [],
Pierre-Yves Chibon 73d120
                    "custom_keys": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1551276259",
Pierre-Yves Chibon 73d120
                    "date_modified": "1551276259",
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "forks/pingou/test",
Pierre-Yves Chibon 73d120
                    "id": 4,
Pierre-Yves Chibon 73d120
                    "milestones": {},
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "parent": {
Pierre-Yves Chibon 73d120
                        "access_groups": {
Pierre-Yves Chibon 73d120
                            "admin": [],
Pierre-Yves Chibon 73d120
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Pierre-Yves Chibon 73d120
                        },
Pierre-Yves Chibon 73d120
                        "access_users": {
Pierre-Yves Chibon 73d120
                            "admin": [],
Pierre-Yves Chibon 73d120
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Pierre-Yves Chibon 73d120
                        },
Pierre-Yves Chibon 73d120
                        "close_status": [
Pierre-Yves Chibon 73d120
                            "Invalid",
Pierre-Yves Chibon 73d120
                            "Insufficient data",
Pierre-Yves Chibon 73d120
                            "Fixed",
Pierre-Yves Chibon 73d120
                            "Duplicate",
Pierre-Yves Chibon 73d120
                        ],
Pierre-Yves Chibon 73d120
                        "custom_keys": [],
Pierre-Yves Chibon 73d120
                        "date_created": "1551276259",
Pierre-Yves Chibon 73d120
                        "date_modified": "1551276259",
Pierre-Yves Chibon 73d120
                        "description": "test project #1",
Pierre-Yves Chibon 73d120
                        "fullname": "test",
Pierre-Yves Chibon 73d120
                        "id": 1,
Pierre-Yves Chibon 73d120
                        "milestones": {},
Pierre-Yves Chibon 73d120
                        "name": "test",
Pierre-Yves Chibon 73d120
                        "namespace": None,
Pierre-Yves Chibon 73d120
                        "parent": None,
Pierre-Yves Chibon 73d120
                        "priorities": {},
Pierre-Yves Chibon 73d120
                        "tags": [],
Pierre-Yves Chibon 73d120
                        "url_path": "test",
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "priorities": {},
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "url_path": "fork/pingou/test",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "status": "Open",
Pierre-Yves Chibon 73d120
                "tags": [],
Pierre-Yves Chibon 73d120
                "threshold_reached": None,
Pierre-Yves Chibon 73d120
                "title": "edited test PR",
Pierre-Yves Chibon 73d120
                "uid": "a2bddecc8ea548e88c22a0df77670092",
Pierre-Yves Chibon 73d120
                "updated_on": "1551276260",
Pierre-Yves Chibon 73d120
                "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 5a9017
        )
Pierre-Yves Chibon 5a9017
Pierre-Yves Chibon 2479ca
    def test_api_pull_request_update_edited_linked(self):
Pierre-Yves Chibon 2479ca
        """ Test api_assign_pull_request method when with valid input
Pierre-Yves Chibon 2479ca
        """
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 2479ca
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon 2479ca
        self.assertEqual(len(project.requests[0].related_issues), 0)
Pierre-Yves Chibon 2479ca
        self.assertEqual(len(project.issues), 0)
Pierre-Yves Chibon 2479ca
Pierre-Yves Chibon 2479ca
        # Create issues to link to
Pierre-Yves Chibon 2479ca
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon 2479ca
            session=self.session,
Pierre-Yves Chibon 2479ca
            repo=project,
Pierre-Yves Chibon 73d120
            title="tést íssüé",
Pierre-Yves Chibon 73d120
            content="We should work on this",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 2479ca
        )
Pierre-Yves Chibon 2479ca
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "tést íssüé")
Pierre-Yves Chibon 2479ca
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 2479ca
Pierre-Yves Chibon 2479ca
        data = {
Pierre-Yves Chibon 73d120
            "title": "edited test PR",
Pierre-Yves Chibon 73d120
            "initial_comment": "Edited initial comment\n\n"
Pierre-Yves Chibon 73d120
            "this PR fixes #2 \n\nThanks",
Pierre-Yves Chibon 2479ca
        }
Pierre-Yves Chibon 2479ca
Pierre-Yves Chibon 2479ca
        # Valid request
Pierre-Yves Chibon 2479ca
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2479ca
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2479ca
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 2479ca
        # Hard-code all the values that will change from a test to another
Pierre-Yves Chibon 2479ca
        # because either random or time-based
Pierre-Yves Chibon 73d120
        data["date_created"] = "1551276260"
Pierre-Yves Chibon 73d120
        data["last_updated"] = "1551276261"
Pierre-Yves Chibon 73d120
        data["updated_on"] = "1551276260"
Pierre-Yves Chibon 73d120
        data["commit_start"] = "5f5d609db65d447f77ba00e25afd17ba5053344b"
Pierre-Yves Chibon 73d120
        data["commit_stop"] = "5f5d609db65d447f77ba00e25afd17ba5053344b"
Pierre-Yves Chibon 73d120
        data["project"]["date_created"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["project"]["date_modified"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_created"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_modified"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["repo_from"]["parent"]["date_created"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["repo_from"]["parent"]["date_modified"] = "1551276259"
Pierre-Yves Chibon 73d120
        data["uid"] = "a2bddecc8ea548e88c22a0df77670092"
Pierre-Yves Chibon 2479ca
        self.assertDictEqual(
Pierre-Yves Chibon 2479ca
            data,
Pierre-Yves Chibon 2479ca
            {
Pierre-Yves Chibon 73d120
                "assignee": None,
Pierre-Yves Chibon 73d120
                "branch": "master",
Pierre-Yves Chibon 73d120
                "branch_from": "master",
Pierre-Yves Chibon 73d120
                "cached_merge_status": "unknown",
Pierre-Yves Chibon 73d120
                "closed_at": None,
Pierre-Yves Chibon 73d120
                "closed_by": None,
Pierre-Yves Chibon 73d120
                "comments": [],
Pierre-Yves Chibon 73d120
                "commit_start": "5f5d609db65d447f77ba00e25afd17ba5053344b",
Pierre-Yves Chibon 73d120
                "commit_stop": "5f5d609db65d447f77ba00e25afd17ba5053344b",
Pierre-Yves Chibon 73d120
                "date_created": "1551276260",
Pierre-Yves Chibon 73d120
                "id": 1,
Pierre-Yves Chibon 73d120
                "initial_comment": "Edited initial comment\n\nthis PR "
Pierre-Yves Chibon 73d120
                "fixes #2 \n\nThanks",
Pierre-Yves Chibon 73d120
                "last_updated": "1551276261",
Pierre-Yves Chibon 73d120
                "project": {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                    "access_users": {
Pierre-Yves Chibon 73d120
                        "admin": [],
Pierre-Yves Chibon 73d120
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "close_status": [
Pierre-Yves Chibon 73d120
                        "Invalid",
Pierre-Yves Chibon 73d120
                        "Insufficient data",
Pierre-Yves Chibon 73d120
                        "Fixed",
Pierre-Yves Chibon 73d120
                        "Duplicate",
Pierre-Yves Chibon 73d120
                    ],
Pierre-Yves Chibon 73d120
                    "custom_keys": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1551276259",
Pierre-Yves Chibon 73d120
                    "date_modified": "1551276259",
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "test",
Pierre-Yves Chibon 73d120
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "milestones": {},
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "parent": None,
Pierre-Yves Chibon 73d120
                    "priorities": {},
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "url_path": "test",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "remote_git": None,
Pierre-Yves Chibon 73d120
                "repo_from": {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                    "access_users": {
Pierre-Yves Chibon 73d120
                        "admin": [],
Pierre-Yves Chibon 73d120
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "close_status": [],
Pierre-Yves Chibon 73d120
                    "custom_keys": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1551276259",
Pierre-Yves Chibon 73d120
                    "date_modified": "1551276259",
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "forks/pingou/test",
Pierre-Yves Chibon 73d120
                    "id": 4,
Pierre-Yves Chibon 73d120
                    "milestones": {},
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "parent": {
Pierre-Yves Chibon 73d120
                        "access_groups": {
Pierre-Yves Chibon 73d120
                            "admin": [],
Pierre-Yves Chibon 73d120
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Pierre-Yves Chibon 73d120
                        },
Pierre-Yves Chibon 73d120
                        "access_users": {
Pierre-Yves Chibon 73d120
                            "admin": [],
Pierre-Yves Chibon 73d120
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Pierre-Yves Chibon 73d120
                        },
Pierre-Yves Chibon 73d120
                        "close_status": [
Pierre-Yves Chibon 73d120
                            "Invalid",
Pierre-Yves Chibon 73d120
                            "Insufficient data",
Pierre-Yves Chibon 73d120
                            "Fixed",
Pierre-Yves Chibon 73d120
                            "Duplicate",
Pierre-Yves Chibon 73d120
                        ],
Pierre-Yves Chibon 73d120
                        "custom_keys": [],
Pierre-Yves Chibon 73d120
                        "date_created": "1551276259",
Pierre-Yves Chibon 73d120
                        "date_modified": "1551276259",
Pierre-Yves Chibon 73d120
                        "description": "test project #1",
Pierre-Yves Chibon 73d120
                        "fullname": "test",
Pierre-Yves Chibon 73d120
                        "id": 1,
Pierre-Yves Chibon 73d120
                        "milestones": {},
Pierre-Yves Chibon 73d120
                        "name": "test",
Pierre-Yves Chibon 73d120
                        "namespace": None,
Pierre-Yves Chibon 73d120
                        "parent": None,
Pierre-Yves Chibon 73d120
                        "priorities": {},
Pierre-Yves Chibon 73d120
                        "tags": [],
Pierre-Yves Chibon 73d120
                        "url_path": "test",
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "priorities": {},
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "url_path": "fork/pingou/test",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "status": "Open",
Pierre-Yves Chibon 73d120
                "tags": [],
Pierre-Yves Chibon 73d120
                "threshold_reached": None,
Pierre-Yves Chibon 73d120
                "title": "edited test PR",
Pierre-Yves Chibon 73d120
                "uid": "a2bddecc8ea548e88c22a0df77670092",
Pierre-Yves Chibon 73d120
                "updated_on": "1551276260",
Pierre-Yves Chibon 73d120
                "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 2479ca
        )
Pierre-Yves Chibon 2479ca
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 2479ca
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon 2479ca
        self.assertEqual(len(project.requests[0].related_issues), 1)
Pierre-Yves Chibon 2479ca
        self.assertEqual(len(project.issues), 1)
Pierre-Yves Chibon 2479ca
        self.assertEqual(len(project.issues[0].related_prs), 1)
Pierre-Yves Chibon 2479ca
Pierre-Yves Chibon 2479ca
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 5a9017
    unittest.main(verbosity=2)