Show More
@@ -1,161 +1,158 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2016 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 mock |
|
21 | import mock | |
22 | import pytest |
|
22 | import pytest | |
23 |
|
23 | |||
24 | from rhodecode.model.repo import RepoModel |
|
24 | from rhodecode.model.repo import RepoModel | |
25 | from rhodecode.tests import TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_LOGIN |
|
25 | from rhodecode.tests import TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_LOGIN | |
26 | from rhodecode.api.tests.utils import ( |
|
26 | from rhodecode.api.tests.utils import ( | |
27 | build_data, api_call, assert_error, assert_ok, crash, jsonify) |
|
27 | build_data, api_call, assert_error, assert_ok, crash, jsonify) | |
28 | from rhodecode.tests.fixture import Fixture |
|
28 | from rhodecode.tests.fixture import Fixture | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | fixture = Fixture() |
|
31 | fixture = Fixture() | |
32 |
|
32 | |||
33 | UPDATE_REPO_NAME = 'api_update_me' |
|
33 | UPDATE_REPO_NAME = 'api_update_me' | |
34 |
|
34 | |||
35 | class SAME_AS_UPDATES(object): """ Constant used for tests below """ |
|
35 | class SAME_AS_UPDATES(object): """ Constant used for tests below """ | |
36 |
|
36 | |||
37 | @pytest.mark.usefixtures("testuser_api", "app") |
|
37 | @pytest.mark.usefixtures("testuser_api", "app") | |
38 | class TestApiUpdateRepo(object): |
|
38 | class TestApiUpdateRepo(object): | |
39 |
|
39 | |||
40 |
@pytest.mark.parametrize(" |
|
40 | @pytest.mark.parametrize("updates, expected", [ | |
41 |
( |
|
41 | ({'owner': TEST_USER_REGULAR_LOGIN}, SAME_AS_UPDATES), | |
42 |
( |
|
42 | ({'description': 'new description'}, SAME_AS_UPDATES), | |
43 |
( |
|
43 | ({'clone_uri': 'http://foo.com/repo'}, SAME_AS_UPDATES), | |
44 |
( |
|
44 | ({'clone_uri': None}, {'clone_uri': ''}), | |
45 |
( |
|
45 | ({'clone_uri': ''}, {'clone_uri': ''}), | |
46 |
( |
|
46 | ({'landing_rev': 'branch:master'}, {'landing_rev': ['branch','master']}), | |
47 | {'landing_rev': ['branch', 'master']}), |
|
47 | ({'enable_statistics': True}, SAME_AS_UPDATES), | |
48 |
( |
|
48 | ({'enable_locking': True}, SAME_AS_UPDATES), | |
49 |
( |
|
49 | ({'enable_downloads': True}, SAME_AS_UPDATES), | |
50 | ('enable_downloads', {'enable_downloads': True}, SAME_AS_UPDATES), |
|
50 | ({'name': 'new_repo_name'}, {'repo_name': 'new_repo_name'}), | |
51 | ('name', {'name': 'new_repo_name'}, |
|
51 | ({'group': 'test_group_for_update'}, | |
52 | {'repo_name': 'new_repo_name'}), |
|
|||
53 | ('repo_group', |
|
|||
54 | {'group': 'test_group_for_update'}, |
|
|||
55 | {'repo_name': 'test_group_for_update/%s' % UPDATE_REPO_NAME}), |
|
52 | {'repo_name': 'test_group_for_update/%s' % UPDATE_REPO_NAME}), | |
56 | ]) |
|
53 | ]) | |
57 |
def test_api_update_repo(self |
|
54 | def test_api_update_repo(self, updates, expected, backend): | |
58 | repo_name = UPDATE_REPO_NAME |
|
55 | repo_name = UPDATE_REPO_NAME | |
59 | repo = fixture.create_repo(repo_name, repo_type=backend.alias) |
|
56 | repo = fixture.create_repo(repo_name, repo_type=backend.alias) | |
60 |
if |
|
57 | if updates.get('group'): | |
61 | fixture.create_repo_group(updates['group']) |
|
58 | fixture.create_repo_group(updates['group']) | |
62 |
|
59 | |||
63 | expected_api_data = repo.get_api_data(include_secrets=True) |
|
60 | expected_api_data = repo.get_api_data(include_secrets=True) | |
64 | if expected is SAME_AS_UPDATES: |
|
61 | if expected is SAME_AS_UPDATES: | |
65 | expected_api_data.update(updates) |
|
62 | expected_api_data.update(updates) | |
66 | else: |
|
63 | else: | |
67 | expected_api_data.update(expected) |
|
64 | expected_api_data.update(expected) | |
68 |
|
65 | |||
69 |
|
66 | |||
70 | id_, params = build_data( |
|
67 | id_, params = build_data( | |
71 | self.apikey, 'update_repo', repoid=repo_name, **updates) |
|
68 | self.apikey, 'update_repo', repoid=repo_name, **updates) | |
72 | response = api_call(self.app, params) |
|
69 | response = api_call(self.app, params) | |
73 |
|
70 | |||
74 |
if |
|
71 | if updates.get('name'): | |
75 | repo_name = updates['name'] |
|
72 | repo_name = updates['name'] | |
76 |
if |
|
73 | if updates.get('group'): | |
77 | repo_name = '/'.join([updates['group'], repo_name]) |
|
74 | repo_name = '/'.join([updates['group'], repo_name]) | |
78 |
|
75 | |||
79 | try: |
|
76 | try: | |
80 | expected = { |
|
77 | expected = { | |
81 | 'msg': 'updated repo ID:%s %s' % (repo.repo_id, repo_name), |
|
78 | 'msg': 'updated repo ID:%s %s' % (repo.repo_id, repo_name), | |
82 | 'repository': jsonify(expected_api_data) |
|
79 | 'repository': jsonify(expected_api_data) | |
83 | } |
|
80 | } | |
84 | assert_ok(id_, expected, given=response.body) |
|
81 | assert_ok(id_, expected, given=response.body) | |
85 | finally: |
|
82 | finally: | |
86 | fixture.destroy_repo(repo_name) |
|
83 | fixture.destroy_repo(repo_name) | |
87 |
if |
|
84 | if updates.get('group'): | |
88 | fixture.destroy_repo_group(updates['group']) |
|
85 | fixture.destroy_repo_group(updates['group']) | |
89 |
|
86 | |||
90 | def test_api_update_repo_fork_of_field(self, backend): |
|
87 | def test_api_update_repo_fork_of_field(self, backend): | |
91 | master_repo = backend.create_repo() |
|
88 | master_repo = backend.create_repo() | |
92 | repo = backend.create_repo() |
|
89 | repo = backend.create_repo() | |
93 | updates = { |
|
90 | updates = { | |
94 | 'fork_of': master_repo.repo_name |
|
91 | 'fork_of': master_repo.repo_name | |
95 | } |
|
92 | } | |
96 | expected_api_data = repo.get_api_data(include_secrets=True) |
|
93 | expected_api_data = repo.get_api_data(include_secrets=True) | |
97 | expected_api_data.update(updates) |
|
94 | expected_api_data.update(updates) | |
98 |
|
95 | |||
99 | id_, params = build_data( |
|
96 | id_, params = build_data( | |
100 | self.apikey, 'update_repo', repoid=repo.repo_name, **updates) |
|
97 | self.apikey, 'update_repo', repoid=repo.repo_name, **updates) | |
101 | response = api_call(self.app, params) |
|
98 | response = api_call(self.app, params) | |
102 | expected = { |
|
99 | expected = { | |
103 | 'msg': 'updated repo ID:%s %s' % (repo.repo_id, repo.repo_name), |
|
100 | 'msg': 'updated repo ID:%s %s' % (repo.repo_id, repo.repo_name), | |
104 | 'repository': jsonify(expected_api_data) |
|
101 | 'repository': jsonify(expected_api_data) | |
105 | } |
|
102 | } | |
106 | assert_ok(id_, expected, given=response.body) |
|
103 | assert_ok(id_, expected, given=response.body) | |
107 | result = response.json['result']['repository'] |
|
104 | result = response.json['result']['repository'] | |
108 | assert result['fork_of'] == master_repo.repo_name |
|
105 | assert result['fork_of'] == master_repo.repo_name | |
109 |
|
106 | |||
110 | def test_api_update_repo_fork_of_not_found(self, backend): |
|
107 | def test_api_update_repo_fork_of_not_found(self, backend): | |
111 | master_repo_name = 'fake-parent-repo' |
|
108 | master_repo_name = 'fake-parent-repo' | |
112 | repo = backend.create_repo() |
|
109 | repo = backend.create_repo() | |
113 | updates = { |
|
110 | updates = { | |
114 | 'fork_of': master_repo_name |
|
111 | 'fork_of': master_repo_name | |
115 | } |
|
112 | } | |
116 | id_, params = build_data( |
|
113 | id_, params = build_data( | |
117 | self.apikey, 'update_repo', repoid=repo.repo_name, **updates) |
|
114 | self.apikey, 'update_repo', repoid=repo.repo_name, **updates) | |
118 | response = api_call(self.app, params) |
|
115 | response = api_call(self.app, params) | |
119 | expected = 'repository `{}` does not exist'.format(master_repo_name) |
|
116 | expected = 'repository `{}` does not exist'.format(master_repo_name) | |
120 | assert_error(id_, expected, given=response.body) |
|
117 | assert_error(id_, expected, given=response.body) | |
121 |
|
118 | |||
122 | def test_api_update_repo_with_repo_group_not_existing(self): |
|
119 | def test_api_update_repo_with_repo_group_not_existing(self): | |
123 | repo_name = 'admin_owned' |
|
120 | repo_name = 'admin_owned' | |
124 | fixture.create_repo(repo_name) |
|
121 | fixture.create_repo(repo_name) | |
125 | updates = {'group': 'test_group_for_update'} |
|
122 | updates = {'group': 'test_group_for_update'} | |
126 | id_, params = build_data( |
|
123 | id_, params = build_data( | |
127 | self.apikey, 'update_repo', repoid=repo_name, **updates) |
|
124 | self.apikey, 'update_repo', repoid=repo_name, **updates) | |
128 | response = api_call(self.app, params) |
|
125 | response = api_call(self.app, params) | |
129 | try: |
|
126 | try: | |
130 | expected = 'repository group `%s` does not exist' % ( |
|
127 | expected = 'repository group `%s` does not exist' % ( | |
131 | updates['group'],) |
|
128 | updates['group'],) | |
132 | assert_error(id_, expected, given=response.body) |
|
129 | assert_error(id_, expected, given=response.body) | |
133 | finally: |
|
130 | finally: | |
134 | fixture.destroy_repo(repo_name) |
|
131 | fixture.destroy_repo(repo_name) | |
135 |
|
132 | |||
136 | def test_api_update_repo_regular_user_not_allowed(self): |
|
133 | def test_api_update_repo_regular_user_not_allowed(self): | |
137 | repo_name = 'admin_owned' |
|
134 | repo_name = 'admin_owned' | |
138 | fixture.create_repo(repo_name) |
|
135 | fixture.create_repo(repo_name) | |
139 | updates = {'active': False} |
|
136 | updates = {'active': False} | |
140 | id_, params = build_data( |
|
137 | id_, params = build_data( | |
141 | self.apikey_regular, 'update_repo', repoid=repo_name, **updates) |
|
138 | self.apikey_regular, 'update_repo', repoid=repo_name, **updates) | |
142 | response = api_call(self.app, params) |
|
139 | response = api_call(self.app, params) | |
143 | try: |
|
140 | try: | |
144 | expected = 'repository `%s` does not exist' % (repo_name,) |
|
141 | expected = 'repository `%s` does not exist' % (repo_name,) | |
145 | assert_error(id_, expected, given=response.body) |
|
142 | assert_error(id_, expected, given=response.body) | |
146 | finally: |
|
143 | finally: | |
147 | fixture.destroy_repo(repo_name) |
|
144 | fixture.destroy_repo(repo_name) | |
148 |
|
145 | |||
149 | @mock.patch.object(RepoModel, 'update', crash) |
|
146 | @mock.patch.object(RepoModel, 'update', crash) | |
150 | def test_api_update_repo_exception_occurred(self, backend): |
|
147 | def test_api_update_repo_exception_occurred(self, backend): | |
151 | repo_name = UPDATE_REPO_NAME |
|
148 | repo_name = UPDATE_REPO_NAME | |
152 | fixture.create_repo(repo_name, repo_type=backend.alias) |
|
149 | fixture.create_repo(repo_name, repo_type=backend.alias) | |
153 | id_, params = build_data( |
|
150 | id_, params = build_data( | |
154 | self.apikey, 'update_repo', repoid=repo_name, |
|
151 | self.apikey, 'update_repo', repoid=repo_name, | |
155 | owner=TEST_USER_ADMIN_LOGIN,) |
|
152 | owner=TEST_USER_ADMIN_LOGIN,) | |
156 | response = api_call(self.app, params) |
|
153 | response = api_call(self.app, params) | |
157 | try: |
|
154 | try: | |
158 | expected = 'failed to update repo `%s`' % (repo_name,) |
|
155 | expected = 'failed to update repo `%s`' % (repo_name,) | |
159 | assert_error(id_, expected, given=response.body) |
|
156 | assert_error(id_, expected, given=response.body) | |
160 | finally: |
|
157 | finally: | |
161 | fixture.destroy_repo(repo_name) |
|
158 | fixture.destroy_repo(repo_name) |
General Comments 0
You need to be logged in to leave comments.
Login now