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