Show More
@@ -1,122 +1,123 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2020 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2020 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 |
|
21 | |||
22 | import random |
|
22 | import random | |
23 | import pytest |
|
23 | import pytest | |
24 |
|
24 | |||
25 | from rhodecode.api.utils import get_origin |
|
25 | from rhodecode.api.utils import get_origin | |
26 | from rhodecode.lib.ext_json import json |
|
26 | from rhodecode.lib.ext_json import json | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | def jsonify(obj): |
|
29 | def jsonify(obj): | |
30 | return json.loads(json.dumps(obj)) |
|
30 | return json.loads(json.dumps(obj)) | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | API_URL = '/_admin/api' |
|
33 | API_URL = '/_admin/api' | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | def assert_call_ok(id_, given): |
|
36 | def assert_call_ok(id_, given): | |
37 | expected = jsonify({ |
|
37 | expected = jsonify({ | |
38 | 'id': id_, |
|
38 | 'id': id_, | |
39 | 'error': None, |
|
39 | 'error': None, | |
40 | 'result': None |
|
40 | 'result': None | |
41 | }) |
|
41 | }) | |
42 | given = json.loads(given) |
|
42 | given = json.loads(given) | |
43 |
|
43 | |||
44 | assert expected['id'] == given['id'] |
|
44 | assert expected['id'] == given['id'] | |
45 | assert expected['error'] == given['error'] |
|
45 | assert expected['error'] == given['error'] | |
46 | return given['result'] |
|
46 | return given['result'] | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | def assert_ok(id_, expected, given): |
|
49 | def assert_ok(id_, expected, given): | |
50 | given = json.loads(given) |
|
50 | given = json.loads(given) | |
51 | if given.get('error'): |
|
51 | if given.get('error'): | |
52 | pytest.fail("Unexpected ERROR in success response: {}".format(given['error'])) |
|
52 | err = given['error'] | |
|
53 | pytest.fail(u"Unexpected ERROR in success response: {}".format(err)) | |||
53 |
|
54 | |||
54 | expected = jsonify({ |
|
55 | expected = jsonify({ | |
55 | 'id': id_, |
|
56 | 'id': id_, | |
56 | 'error': None, |
|
57 | 'error': None, | |
57 | 'result': expected |
|
58 | 'result': expected | |
58 | }) |
|
59 | }) | |
59 |
|
60 | |||
60 | assert expected == given |
|
61 | assert expected == given | |
61 |
|
62 | |||
62 |
|
63 | |||
63 | def assert_error(id_, expected, given): |
|
64 | def assert_error(id_, expected, given): | |
64 | expected = jsonify({ |
|
65 | expected = jsonify({ | |
65 | 'id': id_, |
|
66 | 'id': id_, | |
66 | 'error': expected, |
|
67 | 'error': expected, | |
67 | 'result': None |
|
68 | 'result': None | |
68 | }) |
|
69 | }) | |
69 | given = json.loads(given) |
|
70 | given = json.loads(given) | |
70 | assert expected == given |
|
71 | assert expected == given | |
71 |
|
72 | |||
72 |
|
73 | |||
73 | def build_data(apikey, method, **kw): |
|
74 | def build_data(apikey, method, **kw): | |
74 | """ |
|
75 | """ | |
75 | Builds API data with given random ID |
|
76 | Builds API data with given random ID | |
76 | """ |
|
77 | """ | |
77 | random_id = random.randrange(1, 9999) |
|
78 | random_id = random.randrange(1, 9999) | |
78 | return random_id, json.dumps({ |
|
79 | return random_id, json.dumps({ | |
79 | "id": random_id, |
|
80 | "id": random_id, | |
80 | "api_key": apikey, |
|
81 | "api_key": apikey, | |
81 | "method": method, |
|
82 | "method": method, | |
82 | "args": kw |
|
83 | "args": kw | |
83 | }) |
|
84 | }) | |
84 |
|
85 | |||
85 |
|
86 | |||
86 | def api_call(app, params, status=None): |
|
87 | def api_call(app, params, status=None): | |
87 | response = app.post( |
|
88 | response = app.post( | |
88 | API_URL, content_type='application/json', params=params, status=status) |
|
89 | API_URL, content_type='application/json', params=params, status=status) | |
89 | return response |
|
90 | return response | |
90 |
|
91 | |||
91 |
|
92 | |||
92 | def crash(*args, **kwargs): |
|
93 | def crash(*args, **kwargs): | |
93 | raise Exception('Total Crash !') |
|
94 | raise Exception('Total Crash !') | |
94 |
|
95 | |||
95 |
|
96 | |||
96 | def expected_permissions(object_with_permissions): |
|
97 | def expected_permissions(object_with_permissions): | |
97 | """ |
|
98 | """ | |
98 | Returns the expected permissions structure for the given object. |
|
99 | Returns the expected permissions structure for the given object. | |
99 |
|
100 | |||
100 | The object is expected to be a `Repository`, `RepositoryGroup`, |
|
101 | The object is expected to be a `Repository`, `RepositoryGroup`, | |
101 | or `UserGroup`. They all implement the same permission handling |
|
102 | or `UserGroup`. They all implement the same permission handling | |
102 | API. |
|
103 | API. | |
103 | """ |
|
104 | """ | |
104 | permissions = [] |
|
105 | permissions = [] | |
105 | for _user in object_with_permissions.permissions(): |
|
106 | for _user in object_with_permissions.permissions(): | |
106 | user_data = { |
|
107 | user_data = { | |
107 | 'name': _user.username, |
|
108 | 'name': _user.username, | |
108 | 'permission': _user.permission, |
|
109 | 'permission': _user.permission, | |
109 | 'origin': get_origin(_user), |
|
110 | 'origin': get_origin(_user), | |
110 | 'type': "user", |
|
111 | 'type': "user", | |
111 | } |
|
112 | } | |
112 | permissions.append(user_data) |
|
113 | permissions.append(user_data) | |
113 |
|
114 | |||
114 | for _user_group in object_with_permissions.permission_user_groups(): |
|
115 | for _user_group in object_with_permissions.permission_user_groups(): | |
115 | user_group_data = { |
|
116 | user_group_data = { | |
116 | 'name': _user_group.users_group_name, |
|
117 | 'name': _user_group.users_group_name, | |
117 | 'permission': _user_group.permission, |
|
118 | 'permission': _user_group.permission, | |
118 | 'origin': get_origin(_user_group), |
|
119 | 'origin': get_origin(_user_group), | |
119 | 'type': "user_group", |
|
120 | 'type': "user_group", | |
120 | } |
|
121 | } | |
121 | permissions.append(user_group_data) |
|
122 | permissions.append(user_group_data) | |
122 | return permissions |
|
123 | return permissions |
General Comments 0
You need to be logged in to leave comments.
Login now