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