test_update_pull_request.py
212 lines
| 8.2 KiB
| text/x-python
|
PythonLexer
r1 | # -*- coding: utf-8 -*- | |||
r3363 | # Copyright (C) 2010-2019 RhodeCode GmbH | |||
r1 | # | |||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU Affero General Public License, version 3 | ||||
# (only), as published by the Free Software Foundation. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU Affero General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | ||||
# This program is dual-licensed. If you wish to learn more about the | ||||
# RhodeCode Enterprise Edition, including its added features, Support services, | ||||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | ||||
import pytest | ||||
from rhodecode.lib.vcs.nodes import FileNode | ||||
from rhodecode.model.db import User | ||||
from rhodecode.model.pull_request import PullRequestModel | ||||
from rhodecode.tests import TEST_USER_ADMIN_LOGIN | ||||
r1373 | from rhodecode.api.tests.utils import ( | |||
build_data, api_call, assert_ok, assert_error) | ||||
r1 | ||||
@pytest.mark.usefixtures("testuser_api", "app") | ||||
class TestUpdatePullRequest(object): | ||||
@pytest.mark.backends("git", "hg") | ||||
def test_api_update_pull_request_title_or_description( | ||||
r1807 | self, pr_util, no_notifications): | |||
r1 | pull_request = pr_util.create_pull_request() | |||
id_, params = build_data( | ||||
self.apikey, 'update_pull_request', | ||||
repoid=pull_request.target_repo.repo_name, | ||||
pullrequestid=pull_request.pull_request_id, | ||||
title='New TITLE OF A PR', | ||||
description='New DESC OF A PR', | ||||
) | ||||
response = api_call(self.app, params) | ||||
expected = { | ||||
"msg": "Updated pull request `{}`".format( | ||||
pull_request.pull_request_id), | ||||
"pull_request": response.json['result']['pull_request'], | ||||
"updated_commits": {"added": [], "common": [], "removed": []}, | ||||
"updated_reviewers": {"added": [], "removed": []}, | ||||
} | ||||
response_json = response.json['result'] | ||||
assert response_json == expected | ||||
pr = response_json['pull_request'] | ||||
assert pr['title'] == 'New TITLE OF A PR' | ||||
assert pr['description'] == 'New DESC OF A PR' | ||||
@pytest.mark.backends("git", "hg") | ||||
def test_api_try_update_closed_pull_request( | ||||
r1807 | self, pr_util, no_notifications): | |||
r1 | pull_request = pr_util.create_pull_request() | |||
PullRequestModel().close_pull_request( | ||||
pull_request, TEST_USER_ADMIN_LOGIN) | ||||
id_, params = build_data( | ||||
self.apikey, 'update_pull_request', | ||||
repoid=pull_request.target_repo.repo_name, | ||||
pullrequestid=pull_request.pull_request_id) | ||||
response = api_call(self.app, params) | ||||
expected = 'pull request `{}` update failed, pull request ' \ | ||||
'is closed'.format(pull_request.pull_request_id) | ||||
r1373 | assert_error(id_, expected, response.body) | |||
r1 | ||||
@pytest.mark.backends("git", "hg") | ||||
r1807 | def test_api_update_update_commits(self, pr_util, no_notifications): | |||
r1 | commits = [ | |||
{'message': 'a'}, | ||||
{'message': 'b', 'added': [FileNode('file_b', 'test_content\n')]}, | ||||
{'message': 'c', 'added': [FileNode('file_c', 'test_content\n')]}, | ||||
] | ||||
pull_request = pr_util.create_pull_request( | ||||
commits=commits, target_head='a', source_head='b', revisions=['b']) | ||||
pr_util.update_source_repository(head='c') | ||||
repo = pull_request.source_repo.scm_instance() | ||||
commits = [x for x in repo.get_commits()] | ||||
added_commit_id = commits[-1].raw_id # c commit | ||||
r1373 | common_commit_id = commits[1].raw_id # b commit is common ancestor | |||
total_commits = [added_commit_id, common_commit_id] | ||||
r1 | ||||
id_, params = build_data( | ||||
self.apikey, 'update_pull_request', | ||||
repoid=pull_request.target_repo.repo_name, | ||||
pullrequestid=pull_request.pull_request_id, | ||||
update_commits=True | ||||
) | ||||
response = api_call(self.app, params) | ||||
expected = { | ||||
"msg": "Updated pull request `{}`".format( | ||||
pull_request.pull_request_id), | ||||
"pull_request": response.json['result']['pull_request'], | ||||
"updated_commits": {"added": [added_commit_id], | ||||
r1373 | "common": [common_commit_id], | |||
"total": total_commits, | ||||
"removed": []}, | ||||
r1 | "updated_reviewers": {"added": [], "removed": []}, | |||
} | ||||
r1373 | assert_ok(id_, expected, response.body) | |||
r1 | ||||
@pytest.mark.backends("git", "hg") | ||||
def test_api_update_change_reviewers( | ||||
r1807 | self, user_util, pr_util, no_notifications): | |||
r1769 | a = user_util.create_user() | |||
b = user_util.create_user() | ||||
c = user_util.create_user() | ||||
new_reviewers = [ | ||||
{'username': b.username,'reasons': ['updated via API'], | ||||
'mandatory':False}, | ||||
{'username': c.username, 'reasons': ['updated via API'], | ||||
'mandatory':False}, | ||||
] | ||||
r1 | ||||
r1769 | added = [b.username, c.username] | |||
removed = [a.username] | ||||
r1 | ||||
r1769 | pull_request = pr_util.create_pull_request( | |||
r2484 | reviewers=[(a.username, ['added via API'], False, [])]) | |||
r1 | ||||
id_, params = build_data( | ||||
self.apikey, 'update_pull_request', | ||||
repoid=pull_request.target_repo.repo_name, | ||||
pullrequestid=pull_request.pull_request_id, | ||||
r1769 | reviewers=new_reviewers) | |||
r1 | response = api_call(self.app, params) | |||
expected = { | ||||
"msg": "Updated pull request `{}`".format( | ||||
pull_request.pull_request_id), | ||||
"pull_request": response.json['result']['pull_request'], | ||||
"updated_commits": {"added": [], "common": [], "removed": []}, | ||||
"updated_reviewers": {"added": added, "removed": removed}, | ||||
} | ||||
r1373 | assert_ok(id_, expected, response.body) | |||
r1 | ||||
@pytest.mark.backends("git", "hg") | ||||
def test_api_update_bad_user_in_reviewers(self, pr_util): | ||||
pull_request = pr_util.create_pull_request() | ||||
id_, params = build_data( | ||||
self.apikey, 'update_pull_request', | ||||
repoid=pull_request.target_repo.repo_name, | ||||
pullrequestid=pull_request.pull_request_id, | ||||
r1769 | reviewers=[{'username': 'bad_name'}]) | |||
r1 | response = api_call(self.app, params) | |||
expected = 'user `bad_name` does not exist' | ||||
r1373 | assert_error(id_, expected, response.body) | |||
r1 | ||||
@pytest.mark.backends("git", "hg") | ||||
def test_api_update_repo_error(self, pr_util): | ||||
r2395 | pull_request = pr_util.create_pull_request() | |||
r1 | id_, params = build_data( | |||
self.apikey, 'update_pull_request', | ||||
repoid='fake', | ||||
r2395 | pullrequestid=pull_request.pull_request_id, | |||
r1769 | reviewers=[{'username': 'bad_name'}]) | |||
r1 | response = api_call(self.app, params) | |||
expected = 'repository `fake` does not exist' | ||||
response_json = response.json['error'] | ||||
assert response_json == expected | ||||
@pytest.mark.backends("git", "hg") | ||||
def test_api_update_pull_request_error(self, pr_util): | ||||
pull_request = pr_util.create_pull_request() | ||||
id_, params = build_data( | ||||
self.apikey, 'update_pull_request', | ||||
repoid=pull_request.target_repo.repo_name, | ||||
pullrequestid=999999, | ||||
r1769 | reviewers=[{'username': 'bad_name'}]) | |||
r1 | response = api_call(self.app, params) | |||
expected = 'pull request `999999` does not exist' | ||||
r1373 | assert_error(id_, expected, response.body) | |||
r1 | ||||
@pytest.mark.backends("git", "hg") | ||||
def test_api_update_pull_request_no_perms_to_update( | ||||
self, user_util, pr_util): | ||||
user = user_util.create_user() | ||||
pull_request = pr_util.create_pull_request() | ||||
id_, params = build_data( | ||||
user.api_key, 'update_pull_request', | ||||
repoid=pull_request.target_repo.repo_name, | ||||
pullrequestid=pull_request.pull_request_id,) | ||||
response = api_call(self.app, params) | ||||
expected = ('pull request `%s` update failed, ' | ||||
'no permission to update.') % pull_request.pull_request_id | ||||
r1373 | assert_error(id_, expected, response.body) | |||