##// END OF EJS Templates
tests: fixed api merge tests after introducing merge checks.
marcink -
r1336:6cf63f71 default
parent child Browse files
Show More
@@ -1,104 +1,136 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2017 RhodeCode GmbH
3 # Copyright (C) 2010-2017 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import pytest
21 import pytest
22
22
23 from rhodecode.model.db import UserLog
23 from rhodecode.model.db import UserLog, PullRequest
24 from rhodecode.model.meta import Session
24 from rhodecode.model.meta import Session
25 from rhodecode.model.pull_request import PullRequestModel
26 from rhodecode.tests import TEST_USER_ADMIN_LOGIN
25 from rhodecode.tests import TEST_USER_ADMIN_LOGIN
27 from rhodecode.api.tests.utils import (
26 from rhodecode.api.tests.utils import (
28 build_data, api_call, assert_error)
27 build_data, api_call, assert_error, assert_ok)
29
28
30
29
31 @pytest.mark.usefixtures("testuser_api", "app")
30 @pytest.mark.usefixtures("testuser_api", "app")
32 class TestMergePullRequest(object):
31 class TestMergePullRequest(object):
33 @pytest.mark.backends("git", "hg")
32 @pytest.mark.backends("git", "hg")
34 def test_api_merge_pull_request(self, pr_util, no_notifications):
33 def test_api_merge_pull_request_merge_failed(self, pr_util, no_notifications):
35 pull_request = pr_util.create_pull_request(mergeable=True)
34 pull_request = pr_util.create_pull_request(mergeable=True)
36 author = pull_request.user_id
35 author = pull_request.user_id
37 repo = pull_request.target_repo.repo_id
36 repo = pull_request.target_repo.repo_id
38 pull_request_id = pull_request.pull_request_id
37 pull_request_id = pull_request.pull_request_id
39 pull_request_repo = pull_request.target_repo.repo_name
38 pull_request_repo = pull_request.target_repo.repo_name
40
39
41 id_, params = build_data(
40 id_, params = build_data(
42 self.apikey, 'merge_pull_request',
41 self.apikey, 'merge_pull_request',
43 repoid=pull_request_repo,
42 repoid=pull_request_repo,
44 pullrequestid=pull_request_id)
43 pullrequestid=pull_request_id)
45
44
46 response = api_call(self.app, params)
45 response = api_call(self.app, params)
47
46
48 # The above api call detaches the pull request DB object from the
47 # The above api call detaches the pull request DB object from the
49 # session because of an unconditional transaction rollback in our
48 # session because of an unconditional transaction rollback in our
50 # middleware. Therefore we need to add it back here if we want to use
49 # middleware. Therefore we need to add it back here if we want to use
51 # it.
50 # it.
52 Session().add(pull_request)
51 Session().add(pull_request)
53
52
53 expected = 'merge not possible for following reasons: ' \
54 'Pull request reviewer approval is pending.'
55 assert_error(id_, expected, given=response.body)
56
57 @pytest.mark.backends("git", "hg")
58 def test_api_merge_pull_request(self, pr_util, no_notifications):
59 pull_request = pr_util.create_pull_request(mergeable=True, approved=True)
60 author = pull_request.user_id
61 repo = pull_request.target_repo.repo_id
62 pull_request_id = pull_request.pull_request_id
63 pull_request_repo = pull_request.target_repo.repo_name
64
65 id_, params = build_data(
66 self.apikey, 'comment_pull_request',
67 repoid=pull_request_repo,
68 pullrequestid=pull_request_id,
69 status='approved')
70
71 response = api_call(self.app, params)
72 expected = {
73 'comment_id': response.json.get('result', {}).get('comment_id'),
74 'pull_request_id': pull_request_id,
75 'status': {'given': 'approved', 'was_changed': True}
76 }
77 assert_ok(id_, expected, given=response.body)
78
79 id_, params = build_data(
80 self.apikey, 'merge_pull_request',
81 repoid=pull_request_repo,
82 pullrequestid=pull_request_id)
83
84 response = api_call(self.app, params)
85
86 pull_request = PullRequest.get(pull_request_id)
87
54 expected = {
88 expected = {
55 'executed': True,
89 'executed': True,
56 'failure_reason': 0,
90 'failure_reason': 0,
57 'possible': True,
91 'possible': True,
58 'merge_commit_id': pull_request.shadow_merge_ref.commit_id,
92 'merge_commit_id': pull_request.shadow_merge_ref.commit_id,
59 'merge_ref': pull_request.shadow_merge_ref._asdict()
93 'merge_ref': pull_request.shadow_merge_ref._asdict()
60 }
94 }
61
95
62 response_json = response.json['result']
96 assert_ok(id_, expected, response.body)
63 assert response_json == expected
64
97
65 action = 'user_merged_pull_request:%d' % (pull_request_id, )
98 action = 'user_merged_pull_request:%d' % (pull_request_id, )
66 journal = UserLog.query()\
99 journal = UserLog.query()\
67 .filter(UserLog.user_id == author)\
100 .filter(UserLog.user_id == author)\
68 .filter(UserLog.repository_id == repo)\
101 .filter(UserLog.repository_id == repo)\
69 .filter(UserLog.action == action)\
102 .filter(UserLog.action == action)\
70 .all()
103 .all()
71 assert len(journal) == 1
104 assert len(journal) == 1
72
105
73 id_, params = build_data(
106 id_, params = build_data(
74 self.apikey, 'merge_pull_request',
107 self.apikey, 'merge_pull_request',
75 repoid=pull_request_repo, pullrequestid=pull_request_id)
108 repoid=pull_request_repo, pullrequestid=pull_request_id)
76 response = api_call(self.app, params)
109 response = api_call(self.app, params)
77
110
78 expected = 'pull request `%s` merge failed, pull request is closed' % (
111 expected = 'merge not possible for following reasons: This pull request is closed.'
79 pull_request_id)
80 assert_error(id_, expected, given=response.body)
112 assert_error(id_, expected, given=response.body)
81
113
82 @pytest.mark.backends("git", "hg")
114 @pytest.mark.backends("git", "hg")
83 def test_api_merge_pull_request_repo_error(self):
115 def test_api_merge_pull_request_repo_error(self):
84 id_, params = build_data(
116 id_, params = build_data(
85 self.apikey, 'merge_pull_request',
117 self.apikey, 'merge_pull_request',
86 repoid=666, pullrequestid=1)
118 repoid=666, pullrequestid=1)
87 response = api_call(self.app, params)
119 response = api_call(self.app, params)
88
120
89 expected = 'repository `666` does not exist'
121 expected = 'repository `666` does not exist'
90 assert_error(id_, expected, given=response.body)
122 assert_error(id_, expected, given=response.body)
91
123
92 @pytest.mark.backends("git", "hg")
124 @pytest.mark.backends("git", "hg")
93 def test_api_merge_pull_request_non_admin_with_userid_error(self,
125 def test_api_merge_pull_request_non_admin_with_userid_error(self,
94 pr_util):
126 pr_util):
95 pull_request = pr_util.create_pull_request(mergeable=True)
127 pull_request = pr_util.create_pull_request(mergeable=True)
96 id_, params = build_data(
128 id_, params = build_data(
97 self.apikey_regular, 'merge_pull_request',
129 self.apikey_regular, 'merge_pull_request',
98 repoid=pull_request.target_repo.repo_name,
130 repoid=pull_request.target_repo.repo_name,
99 pullrequestid=pull_request.pull_request_id,
131 pullrequestid=pull_request.pull_request_id,
100 userid=TEST_USER_ADMIN_LOGIN)
132 userid=TEST_USER_ADMIN_LOGIN)
101 response = api_call(self.app, params)
133 response = api_call(self.app, params)
102
134
103 expected = 'userid is not the same as your user'
135 expected = 'userid is not the same as your user'
104 assert_error(id_, expected, given=response.body)
136 assert_error(id_, expected, given=response.body)
General Comments 0
You need to be logged in to leave comments. Login now