##// END OF EJS Templates
tests: fixed pull_reques API tests
marcink -
r2862:260b7e82 stable
parent child Browse files
Show More
@@ -1,297 +1,335 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2018 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import pytest
22 22
23 23 from rhodecode.model.db import User
24 24 from rhodecode.model.pull_request import PullRequestModel
25 25 from rhodecode.model.repo import RepoModel
26 26 from rhodecode.model.user import UserModel
27 27 from rhodecode.tests import TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_LOGIN
28 28 from rhodecode.api.tests.utils import build_data, api_call, assert_error
29 29
30 30
31 31 @pytest.mark.usefixtures("testuser_api", "app")
32 32 class TestCreatePullRequestApi(object):
33 33 finalizers = []
34 34
35 35 def teardown_method(self, method):
36 36 if self.finalizers:
37 37 for finalizer in self.finalizers:
38 38 finalizer()
39 39 self.finalizers = []
40 40
41 41 def test_create_with_wrong_data(self):
42 42 required_data = {
43 43 'source_repo': 'tests/source_repo',
44 44 'target_repo': 'tests/target_repo',
45 45 'source_ref': 'branch:default:initial',
46 46 'target_ref': 'branch:default:new-feature',
47 'title': 'Test PR 1'
48 47 }
49 48 for key in required_data:
50 49 data = required_data.copy()
51 50 data.pop(key)
52 51 id_, params = build_data(
53 52 self.apikey, 'create_pull_request', **data)
54 53 response = api_call(self.app, params)
55 54
56 55 expected = 'Missing non optional `{}` arg in JSON DATA'.format(key)
57 56 assert_error(id_, expected, given=response.body)
58 57
59 58 @pytest.mark.backends("git", "hg")
60 59 def test_create_with_correct_data(self, backend):
61 60 data = self._prepare_data(backend)
62 61 RepoModel().revoke_user_permission(
63 62 self.source.repo_name, User.DEFAULT_USER)
64 63 id_, params = build_data(
65 64 self.apikey_regular, 'create_pull_request', **data)
66 65 response = api_call(self.app, params)
67 66 expected_message = "Created new pull request `{title}`".format(
68 67 title=data['title'])
69 68 result = response.json
70 69 assert result['result']['msg'] == expected_message
71 70 pull_request_id = result['result']['pull_request_id']
72 71 pull_request = PullRequestModel().get(pull_request_id)
73 72 assert pull_request.title == data['title']
74 73 assert pull_request.description == data['description']
75 74 assert pull_request.source_ref == data['source_ref']
76 75 assert pull_request.target_ref == data['target_ref']
77 76 assert pull_request.source_repo.repo_name == data['source_repo']
78 77 assert pull_request.target_repo.repo_name == data['target_repo']
79 78 assert pull_request.revisions == [self.commit_ids['change']]
80 79 assert len(pull_request.reviewers) == 1
81 80
82 81 @pytest.mark.backends("git", "hg")
83 82 def test_create_with_empty_description(self, backend):
84 83 data = self._prepare_data(backend)
85 84 data.pop('description')
86 85 id_, params = build_data(
87 86 self.apikey_regular, 'create_pull_request', **data)
88 87 response = api_call(self.app, params)
89 88 expected_message = "Created new pull request `{title}`".format(
90 89 title=data['title'])
91 90 result = response.json
92 91 assert result['result']['msg'] == expected_message
93 92 pull_request_id = result['result']['pull_request_id']
94 93 pull_request = PullRequestModel().get(pull_request_id)
95 94 assert pull_request.description == ''
96 95
97 96 @pytest.mark.backends("git", "hg")
97 def test_create_with_empty_title(self, backend):
98 data = self._prepare_data(backend)
99 data.pop('title')
100 id_, params = build_data(
101 self.apikey_regular, 'create_pull_request', **data)
102 response = api_call(self.app, params)
103 result = response.json
104 pull_request_id = result['result']['pull_request_id']
105 pull_request = PullRequestModel().get(pull_request_id)
106 data['ref'] = backend.default_branch_name
107 title = '{source_repo}#{ref} to {target_repo}'.format(**data)
108 assert pull_request.title == title
109
110 @pytest.mark.backends("git", "hg")
98 111 def test_create_with_reviewers_specified_by_names(
99 112 self, backend, no_notifications):
100 113 data = self._prepare_data(backend)
101 114 reviewers = [
102 115 {'username': TEST_USER_REGULAR_LOGIN,
103 'reasons': ['added manually']},
116 'reasons': ['{} added manually'.format(TEST_USER_REGULAR_LOGIN)]},
104 117 {'username': TEST_USER_ADMIN_LOGIN,
105 'reasons': ['added manually']},
118 'reasons': ['{} added manually'.format(TEST_USER_ADMIN_LOGIN)],
119 'mandatory': True},
106 120 ]
107 121 data['reviewers'] = reviewers
108 122 id_, params = build_data(
109 123 self.apikey_regular, 'create_pull_request', **data)
110 124 response = api_call(self.app, params)
111 125
112 126 expected_message = "Created new pull request `{title}`".format(
113 127 title=data['title'])
114 128 result = response.json
115 129 assert result['result']['msg'] == expected_message
116 130 pull_request_id = result['result']['pull_request_id']
117 131 pull_request = PullRequestModel().get(pull_request_id)
118 actual_reviewers = [
119 {'username': r.user.username,
120 'reasons': ['added manually'],
121 } for r in pull_request.reviewers
122 ]
123 assert sorted(actual_reviewers) == sorted(reviewers)
132
133 actual_reviewers = []
134 for rev in pull_request.reviewers:
135 entry = {
136 'username': rev.user.username,
137 'reasons': rev.reasons,
138 }
139 if rev.mandatory:
140 entry['mandatory'] = rev.mandatory
141 actual_reviewers.append(entry)
142
143 # default reviewer will be added who is an owner of the repo
144 reviewers.append(
145 {'username': pull_request.author.username,
146 'reasons': [u'Default reviewer', u'Repository owner']},
147 )
148 assert sorted(actual_reviewers, key=lambda e: e['username']) \
149 == sorted(reviewers, key=lambda e: e['username'])
124 150
125 151 @pytest.mark.backends("git", "hg")
126 152 def test_create_with_reviewers_specified_by_ids(
127 153 self, backend, no_notifications):
128 154 data = self._prepare_data(backend)
129 155 reviewers = [
130 156 {'username': UserModel().get_by_username(
131 157 TEST_USER_REGULAR_LOGIN).user_id,
132 158 'reasons': ['added manually']},
133 159 {'username': UserModel().get_by_username(
134 160 TEST_USER_ADMIN_LOGIN).user_id,
135 161 'reasons': ['added manually']},
136 162 ]
137 163
138 164 data['reviewers'] = reviewers
139 165 id_, params = build_data(
140 166 self.apikey_regular, 'create_pull_request', **data)
141 167 response = api_call(self.app, params)
142 168
143 169 expected_message = "Created new pull request `{title}`".format(
144 170 title=data['title'])
145 171 result = response.json
146 172 assert result['result']['msg'] == expected_message
147 173 pull_request_id = result['result']['pull_request_id']
148 174 pull_request = PullRequestModel().get(pull_request_id)
149 actual_reviewers = [
150 {'username': r.user.user_id,
151 'reasons': ['added manually'],
152 } for r in pull_request.reviewers
153 ]
154 assert sorted(actual_reviewers) == sorted(reviewers)
175
176 actual_reviewers = []
177 for rev in pull_request.reviewers:
178 entry = {
179 'username': rev.user.user_id,
180 'reasons': rev.reasons,
181 }
182 if rev.mandatory:
183 entry['mandatory'] = rev.mandatory
184 actual_reviewers.append(entry)
185 # default reviewer will be added who is an owner of the repo
186 reviewers.append(
187 {'username': pull_request.author.user_id,
188 'reasons': [u'Default reviewer', u'Repository owner']},
189 )
190 assert sorted(actual_reviewers, key=lambda e: e['username']) \
191 == sorted(reviewers, key=lambda e: e['username'])
155 192
156 193 @pytest.mark.backends("git", "hg")
157 194 def test_create_fails_when_the_reviewer_is_not_found(self, backend):
158 195 data = self._prepare_data(backend)
159 196 data['reviewers'] = [{'username': 'somebody'}]
160 197 id_, params = build_data(
161 198 self.apikey_regular, 'create_pull_request', **data)
162 199 response = api_call(self.app, params)
163 200 expected_message = 'user `somebody` does not exist'
164 201 assert_error(id_, expected_message, given=response.body)
165 202
166 203 @pytest.mark.backends("git", "hg")
167 204 def test_cannot_create_with_reviewers_in_wrong_format(self, backend):
168 205 data = self._prepare_data(backend)
169 206 reviewers = ','.join([TEST_USER_REGULAR_LOGIN, TEST_USER_ADMIN_LOGIN])
170 207 data['reviewers'] = reviewers
171 208 id_, params = build_data(
172 209 self.apikey_regular, 'create_pull_request', **data)
173 210 response = api_call(self.app, params)
174 211 expected_message = {u'': '"test_regular,test_admin" is not iterable'}
175 212 assert_error(id_, expected_message, given=response.body)
176 213
177 214 @pytest.mark.backends("git", "hg")
178 215 def test_create_with_no_commit_hashes(self, backend):
179 216 data = self._prepare_data(backend)
180 217 expected_source_ref = data['source_ref']
181 218 expected_target_ref = data['target_ref']
182 219 data['source_ref'] = 'branch:{}'.format(backend.default_branch_name)
183 220 data['target_ref'] = 'branch:{}'.format(backend.default_branch_name)
184 221 id_, params = build_data(
185 222 self.apikey_regular, 'create_pull_request', **data)
186 223 response = api_call(self.app, params)
187 224 expected_message = "Created new pull request `{title}`".format(
188 225 title=data['title'])
189 226 result = response.json
190 227 assert result['result']['msg'] == expected_message
191 228 pull_request_id = result['result']['pull_request_id']
192 229 pull_request = PullRequestModel().get(pull_request_id)
193 230 assert pull_request.source_ref == expected_source_ref
194 231 assert pull_request.target_ref == expected_target_ref
195 232
196 233 @pytest.mark.backends("git", "hg")
197 234 @pytest.mark.parametrize("data_key", ["source_repo", "target_repo"])
198 235 def test_create_fails_with_wrong_repo(self, backend, data_key):
199 236 repo_name = 'fake-repo'
200 237 data = self._prepare_data(backend)
201 238 data[data_key] = repo_name
202 239 id_, params = build_data(
203 240 self.apikey_regular, 'create_pull_request', **data)
204 241 response = api_call(self.app, params)
205 242 expected_message = 'repository `{}` does not exist'.format(repo_name)
206 243 assert_error(id_, expected_message, given=response.body)
207 244
208 245 @pytest.mark.backends("git", "hg")
209 246 @pytest.mark.parametrize("data_key", ["source_ref", "target_ref"])
210 247 def test_create_fails_with_non_existing_branch(self, backend, data_key):
211 248 branch_name = 'test-branch'
212 249 data = self._prepare_data(backend)
213 250 data[data_key] = "branch:{}".format(branch_name)
214 251 id_, params = build_data(
215 252 self.apikey_regular, 'create_pull_request', **data)
216 253 response = api_call(self.app, params)
217 254 expected_message = 'The specified branch `{}` does not exist'.format(
218 255 branch_name)
219 256 assert_error(id_, expected_message, given=response.body)
220 257
221 258 @pytest.mark.backends("git", "hg")
222 259 @pytest.mark.parametrize("data_key", ["source_ref", "target_ref"])
223 260 def test_create_fails_with_ref_in_a_wrong_format(self, backend, data_key):
224 261 data = self._prepare_data(backend)
225 262 ref = 'stange-ref'
226 263 data[data_key] = ref
227 264 id_, params = build_data(
228 265 self.apikey_regular, 'create_pull_request', **data)
229 266 response = api_call(self.app, params)
230 267 expected_message = (
231 268 'Ref `{ref}` given in a wrong format. Please check the API'
232 269 ' documentation for more details'.format(ref=ref))
233 270 assert_error(id_, expected_message, given=response.body)
234 271
235 272 @pytest.mark.backends("git", "hg")
236 273 @pytest.mark.parametrize("data_key", ["source_ref", "target_ref"])
237 274 def test_create_fails_with_non_existing_ref(self, backend, data_key):
238 275 commit_id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa10'
239 276 ref = self._get_full_ref(backend, commit_id)
240 277 data = self._prepare_data(backend)
241 278 data[data_key] = ref
242 279 id_, params = build_data(
243 280 self.apikey_regular, 'create_pull_request', **data)
244 281 response = api_call(self.app, params)
245 282 expected_message = 'Ref `{}` does not exist'.format(ref)
246 283 assert_error(id_, expected_message, given=response.body)
247 284
248 285 @pytest.mark.backends("git", "hg")
249 286 def test_create_fails_when_no_revisions(self, backend):
250 287 data = self._prepare_data(backend, source_head='initial')
251 288 id_, params = build_data(
252 289 self.apikey_regular, 'create_pull_request', **data)
253 290 response = api_call(self.app, params)
254 291 expected_message = 'no commits found'
255 292 assert_error(id_, expected_message, given=response.body)
256 293
257 294 @pytest.mark.backends("git", "hg")
258 295 def test_create_fails_when_no_permissions(self, backend):
259 296 data = self._prepare_data(backend)
260 297 RepoModel().revoke_user_permission(
261 self.source.repo_name, User.DEFAULT_USER)
298 self.source.repo_name, self.test_user)
262 299 RepoModel().revoke_user_permission(
263 self.source.repo_name, self.test_user)
300 self.source.repo_name, User.DEFAULT_USER)
301
264 302 id_, params = build_data(
265 303 self.apikey_regular, 'create_pull_request', **data)
266 304 response = api_call(self.app, params)
267 305 expected_message = 'repository `{}` does not exist'.format(
268 306 self.source.repo_name)
269 307 assert_error(id_, expected_message, given=response.body)
270 308
271 309 def _prepare_data(
272 310 self, backend, source_head='change', target_head='initial'):
273 311 commits = [
274 312 {'message': 'initial'},
275 313 {'message': 'change'},
276 314 {'message': 'new-feature', 'parents': ['initial']},
277 315 ]
278 316 self.commit_ids = backend.create_master_repo(commits)
279 317 self.source = backend.create_repo(heads=[source_head])
280 318 self.target = backend.create_repo(heads=[target_head])
281 319 data = {
282 320 'source_repo': self.source.repo_name,
283 321 'target_repo': self.target.repo_name,
284 322 'source_ref': self._get_full_ref(
285 323 backend, self.commit_ids[source_head]),
286 324 'target_ref': self._get_full_ref(
287 325 backend, self.commit_ids[target_head]),
288 326 'title': 'Test PR 1',
289 327 'description': 'Test'
290 328 }
291 329 RepoModel().grant_user_permission(
292 330 self.source.repo_name, self.TEST_USER_LOGIN, 'repository.read')
293 331 return data
294 332
295 333 def _get_full_ref(self, backend, commit_id):
296 334 return 'branch:{branch}:{commit_id}'.format(
297 335 branch=backend.default_branch_name, commit_id=commit_id)
General Comments 0
You need to be logged in to leave comments. Login now