Show More
@@ -1,161 +1,192 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.lib.auth import check_password |
|
24 | from rhodecode.lib.auth import check_password | |
25 | from rhodecode.model.user import UserModel |
|
25 | from rhodecode.model.user import UserModel | |
26 | from rhodecode.tests import ( |
|
26 | from rhodecode.tests import ( | |
27 | TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_EMAIL) |
|
27 | TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_EMAIL) | |
28 | from rhodecode.api.tests.utils import ( |
|
28 | from rhodecode.api.tests.utils import ( | |
29 | build_data, api_call, assert_ok, assert_error, jsonify, crash) |
|
29 | build_data, api_call, assert_ok, assert_error, jsonify, crash) | |
30 | from rhodecode.tests.fixture import Fixture |
|
30 | from rhodecode.tests.fixture import Fixture | |
|
31 | from rhodecode.model.db import RepoGroup | |||
31 |
|
32 | |||
32 |
|
33 | |||
33 | # TODO: mikhail: remove fixture from here |
|
34 | # TODO: mikhail: remove fixture from here | |
34 | fixture = Fixture() |
|
35 | fixture = Fixture() | |
35 |
|
36 | |||
36 |
|
37 | |||
37 | @pytest.mark.usefixtures("testuser_api", "app") |
|
38 | @pytest.mark.usefixtures("testuser_api", "app") | |
38 | class TestCreateUser(object): |
|
39 | class TestCreateUser(object): | |
39 | def test_api_create_existing_user(self): |
|
40 | def test_api_create_existing_user(self): | |
40 | id_, params = build_data( |
|
41 | id_, params = build_data( | |
41 | self.apikey, 'create_user', |
|
42 | self.apikey, 'create_user', | |
42 | username=TEST_USER_ADMIN_LOGIN, |
|
43 | username=TEST_USER_ADMIN_LOGIN, | |
43 | email='test@foo.com', |
|
44 | email='test@foo.com', | |
44 | password='trololo') |
|
45 | password='trololo') | |
45 | response = api_call(self.app, params) |
|
46 | response = api_call(self.app, params) | |
46 |
|
47 | |||
47 | expected = "user `%s` already exist" % (TEST_USER_ADMIN_LOGIN,) |
|
48 | expected = "user `%s` already exist" % (TEST_USER_ADMIN_LOGIN,) | |
48 | assert_error(id_, expected, given=response.body) |
|
49 | assert_error(id_, expected, given=response.body) | |
49 |
|
50 | |||
50 | def test_api_create_user_with_existing_email(self): |
|
51 | def test_api_create_user_with_existing_email(self): | |
51 | id_, params = build_data( |
|
52 | id_, params = build_data( | |
52 | self.apikey, 'create_user', |
|
53 | self.apikey, 'create_user', | |
53 | username=TEST_USER_ADMIN_LOGIN + 'new', |
|
54 | username=TEST_USER_ADMIN_LOGIN + 'new', | |
54 | email=TEST_USER_REGULAR_EMAIL, |
|
55 | email=TEST_USER_REGULAR_EMAIL, | |
55 | password='trololo') |
|
56 | password='trololo') | |
56 | response = api_call(self.app, params) |
|
57 | response = api_call(self.app, params) | |
57 |
|
58 | |||
58 | expected = "email `%s` already exist" % (TEST_USER_REGULAR_EMAIL,) |
|
59 | expected = "email `%s` already exist" % (TEST_USER_REGULAR_EMAIL,) | |
59 | assert_error(id_, expected, given=response.body) |
|
60 | assert_error(id_, expected, given=response.body) | |
60 |
|
61 | |||
61 | def test_api_create_user(self): |
|
62 | def test_api_create_user(self): | |
62 | username = 'test_new_api_user' |
|
63 | username = 'test_new_api_user' | |
63 | email = username + "@foo.com" |
|
64 | email = username + "@foo.com" | |
64 |
|
65 | |||
65 | id_, params = build_data( |
|
66 | id_, params = build_data( | |
66 | self.apikey, 'create_user', |
|
67 | self.apikey, 'create_user', | |
67 | username=username, |
|
68 | username=username, | |
68 | email=email, |
|
69 | email=email, | |
69 | password='example') |
|
70 | password='example') | |
70 | response = api_call(self.app, params) |
|
71 | response = api_call(self.app, params) | |
71 |
|
72 | |||
72 | usr = UserModel().get_by_username(username) |
|
73 | usr = UserModel().get_by_username(username) | |
73 | ret = { |
|
74 | ret = { | |
74 | 'msg': 'created new user `%s`' % (username,), |
|
75 | 'msg': 'created new user `%s`' % (username,), | |
75 | 'user': jsonify(usr.get_api_data(include_secrets=True)), |
|
76 | 'user': jsonify(usr.get_api_data(include_secrets=True)), | |
76 | } |
|
77 | } | |
77 | try: |
|
78 | try: | |
78 | expected = ret |
|
79 | expected = ret | |
79 | assert check_password('example', usr.password) |
|
80 | assert check_password('example', usr.password) | |
80 | assert_ok(id_, expected, given=response.body) |
|
81 | assert_ok(id_, expected, given=response.body) | |
81 | finally: |
|
82 | finally: | |
82 | fixture.destroy_user(usr.user_id) |
|
83 | fixture.destroy_user(usr.user_id) | |
83 |
|
84 | |||
84 | def test_api_create_user_without_password(self): |
|
85 | def test_api_create_user_without_password(self): | |
85 | username = 'test_new_api_user_passwordless' |
|
86 | username = 'test_new_api_user_passwordless' | |
86 | email = username + "@foo.com" |
|
87 | email = username + "@foo.com" | |
87 |
|
88 | |||
88 | id_, params = build_data( |
|
89 | id_, params = build_data( | |
89 | self.apikey, 'create_user', |
|
90 | self.apikey, 'create_user', | |
90 | username=username, |
|
91 | username=username, | |
91 | email=email) |
|
92 | email=email) | |
92 | response = api_call(self.app, params) |
|
93 | response = api_call(self.app, params) | |
93 |
|
94 | |||
94 | usr = UserModel().get_by_username(username) |
|
95 | usr = UserModel().get_by_username(username) | |
95 | ret = { |
|
96 | ret = { | |
96 | 'msg': 'created new user `%s`' % (username,), |
|
97 | 'msg': 'created new user `%s`' % (username,), | |
97 | 'user': jsonify(usr.get_api_data(include_secrets=True)), |
|
98 | 'user': jsonify(usr.get_api_data(include_secrets=True)), | |
98 | } |
|
99 | } | |
99 | try: |
|
100 | try: | |
100 | expected = ret |
|
101 | expected = ret | |
101 | assert_ok(id_, expected, given=response.body) |
|
102 | assert_ok(id_, expected, given=response.body) | |
102 | finally: |
|
103 | finally: | |
103 | fixture.destroy_user(usr.user_id) |
|
104 | fixture.destroy_user(usr.user_id) | |
104 |
|
105 | |||
105 | def test_api_create_user_with_extern_name(self): |
|
106 | def test_api_create_user_with_extern_name(self): | |
106 | username = 'test_new_api_user_passwordless' |
|
107 | username = 'test_new_api_user_passwordless' | |
107 | email = username + "@foo.com" |
|
108 | email = username + "@foo.com" | |
108 |
|
109 | |||
109 | id_, params = build_data( |
|
110 | id_, params = build_data( | |
110 | self.apikey, 'create_user', |
|
111 | self.apikey, 'create_user', | |
111 | username=username, |
|
112 | username=username, | |
112 | email=email, extern_name='rhodecode') |
|
113 | email=email, extern_name='rhodecode') | |
113 | response = api_call(self.app, params) |
|
114 | response = api_call(self.app, params) | |
114 |
|
115 | |||
115 | usr = UserModel().get_by_username(username) |
|
116 | usr = UserModel().get_by_username(username) | |
116 | ret = { |
|
117 | ret = { | |
117 | 'msg': 'created new user `%s`' % (username,), |
|
118 | 'msg': 'created new user `%s`' % (username,), | |
118 | 'user': jsonify(usr.get_api_data(include_secrets=True)), |
|
119 | 'user': jsonify(usr.get_api_data(include_secrets=True)), | |
119 | } |
|
120 | } | |
120 | try: |
|
121 | try: | |
121 | expected = ret |
|
122 | expected = ret | |
122 | assert_ok(id_, expected, given=response.body) |
|
123 | assert_ok(id_, expected, given=response.body) | |
123 | finally: |
|
124 | finally: | |
124 | fixture.destroy_user(usr.user_id) |
|
125 | fixture.destroy_user(usr.user_id) | |
125 |
|
126 | |||
126 | def test_api_create_user_with_password_change(self): |
|
127 | def test_api_create_user_with_password_change(self): | |
127 | username = 'test_new_api_user_password_change' |
|
128 | username = 'test_new_api_user_password_change' | |
128 | email = username + "@foo.com" |
|
129 | email = username + "@foo.com" | |
129 |
|
130 | |||
130 | id_, params = build_data( |
|
131 | id_, params = build_data( | |
131 | self.apikey, 'create_user', |
|
132 | self.apikey, 'create_user', | |
132 | username=username, |
|
133 | username=username, | |
133 | email=email, extern_name='rhodecode', |
|
134 | email=email, extern_name='rhodecode', | |
134 | force_password_change=True) |
|
135 | force_password_change=True) | |
135 | response = api_call(self.app, params) |
|
136 | response = api_call(self.app, params) | |
136 |
|
137 | |||
137 | usr = UserModel().get_by_username(username) |
|
138 | usr = UserModel().get_by_username(username) | |
138 | ret = { |
|
139 | ret = { | |
139 | 'msg': 'created new user `%s`' % (username,), |
|
140 | 'msg': 'created new user `%s`' % (username,), | |
140 | 'user': jsonify(usr.get_api_data(include_secrets=True)), |
|
141 | 'user': jsonify(usr.get_api_data(include_secrets=True)), | |
141 | } |
|
142 | } | |
142 | try: |
|
143 | try: | |
143 | expected = ret |
|
144 | expected = ret | |
144 | assert_ok(id_, expected, given=response.body) |
|
145 | assert_ok(id_, expected, given=response.body) | |
145 | finally: |
|
146 | finally: | |
146 | fixture.destroy_user(usr.user_id) |
|
147 | fixture.destroy_user(usr.user_id) | |
147 |
|
148 | |||
|
149 | def test_api_create_user_with_personal_repo_group(self): | |||
|
150 | username = 'test_new_api_user_personal_group' | |||
|
151 | email = username + "@foo.com" | |||
|
152 | ||||
|
153 | id_, params = build_data( | |||
|
154 | self.apikey, 'create_user', | |||
|
155 | username=username, | |||
|
156 | email=email, extern_name='rhodecode', | |||
|
157 | create_personal_repo_group=True) | |||
|
158 | response = api_call(self.app, params) | |||
|
159 | ||||
|
160 | usr = UserModel().get_by_username(username) | |||
|
161 | ret = { | |||
|
162 | 'msg': 'created new user `%s`' % (username,), | |||
|
163 | 'user': jsonify(usr.get_api_data(include_secrets=True)), | |||
|
164 | } | |||
|
165 | ||||
|
166 | personal_group = RepoGroup.get_by_group_name(username) | |||
|
167 | assert personal_group | |||
|
168 | assert personal_group.personal == True | |||
|
169 | assert personal_group.user.username == username | |||
|
170 | ||||
|
171 | try: | |||
|
172 | expected = ret | |||
|
173 | assert_ok(id_, expected, given=response.body) | |||
|
174 | finally: | |||
|
175 | fixture.destroy_repo_group(username) | |||
|
176 | fixture.destroy_user(usr.user_id) | |||
|
177 | ||||
|
178 | ||||
148 | @mock.patch.object(UserModel, 'create_or_update', crash) |
|
179 | @mock.patch.object(UserModel, 'create_or_update', crash) | |
149 | def test_api_create_user_when_exception_happened(self): |
|
180 | def test_api_create_user_when_exception_happened(self): | |
150 |
|
181 | |||
151 | username = 'test_new_api_user' |
|
182 | username = 'test_new_api_user' | |
152 | email = username + "@foo.com" |
|
183 | email = username + "@foo.com" | |
153 |
|
184 | |||
154 | id_, params = build_data( |
|
185 | id_, params = build_data( | |
155 | self.apikey, 'create_user', |
|
186 | self.apikey, 'create_user', | |
156 | username=username, |
|
187 | username=username, | |
157 | email=email, |
|
188 | email=email, | |
158 | password='trololo') |
|
189 | password='trololo') | |
159 | response = api_call(self.app, params) |
|
190 | response = api_call(self.app, params) | |
160 | expected = 'failed to create user `%s`' % (username,) |
|
191 | expected = 'failed to create user `%s`' % (username,) | |
161 | assert_error(id_, expected, given=response.body) |
|
192 | assert_error(id_, expected, given=response.body) |
General Comments 0
You need to be logged in to leave comments.
Login now