-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
CreateActionTest.java
181 lines (150 loc) · 6.29 KB
/
CreateActionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.user.ws;
import java.util.Locale;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.config.Settings;
import org.sonar.api.i18n.I18n;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.user.GroupDao;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDao;
import org.sonar.db.user.UserGroupDao;
import org.sonar.db.user.UserTesting;
import org.sonar.server.es.EsTester;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.user.NewUserNotifier;
import org.sonar.server.user.UserUpdater;
import org.sonar.server.user.index.UserDoc;
import org.sonar.server.user.index.UserIndex;
import org.sonar.server.user.index.UserIndexDefinition;
import org.sonar.server.user.index.UserIndexer;
import org.sonar.server.ws.WsTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class CreateActionTest {
static final Settings settings = new Settings().setProperty("sonar.defaultGroup", "sonar-users");
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
@ClassRule
public static final EsTester esTester = new EsTester().addDefinitions(new UserIndexDefinition(settings));
@Rule
public UserSessionRule userSessionRule = UserSessionRule.standalone();
WebService.Controller controller;
WsTester tester;
UserIndex index;
DbClient dbClient;
UserIndexer userIndexer;
DbSession session;
I18n i18n = mock(I18n.class);
@Before
public void setUp() {
dbTester.truncateTables();
esTester.truncateIndices();
System2 system2 = new System2();
UserDao userDao = new UserDao(dbTester.myBatis(), system2);
UserGroupDao userGroupDao = new UserGroupDao();
GroupDao groupDao = new GroupDao(system2);
dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), userDao, userGroupDao, groupDao);
session = dbClient.openSession(false);
groupDao.insert(session, new GroupDto().setName("sonar-users"));
session.commit();
userIndexer = (UserIndexer) new UserIndexer(dbClient, esTester.client()).setEnabled(true);
index = new UserIndex(esTester.client());
tester = new WsTester(new UsersWs(new CreateAction(dbClient,
new UserUpdater(mock(NewUserNotifier.class), settings, dbClient, userIndexer, system2),
i18n, userSessionRule, new UserJsonWriter(userSessionRule))));
controller = tester.controller("api/users");
}
@After
public void tearDown() {
session.close();
}
@Test
public void create_user() throws Exception {
userSessionRule.login("admin").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
tester.newPostRequest("api/users", "create")
.setParam("login", "john")
.setParam("name", "John")
.setParam("email", "john@email.com")
.setParam("scmAccounts", "jn")
.setParam("password", "1234").execute()
.assertJson(getClass(), "create_user.json");
UserDoc user = index.getNullableByLogin("john");
assertThat(user.login()).isEqualTo("john");
assertThat(user.name()).isEqualTo("John");
assertThat(user.email()).isEqualTo("john@email.com");
assertThat(user.scmAccounts()).containsOnly("jn");
}
@Test
public void create_user_with_deprecated_scm_parameter() throws Exception {
userSessionRule.login("admin").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
tester.newPostRequest("api/users", "create")
.setParam("login", "john")
.setParam("name", "John")
.setParam("email", "john@email.com")
.setParam("scm_accounts", "jn")
.setParam("password", "1234").execute()
.assertJson(getClass(), "create_user.json");
UserDoc user = index.getNullableByLogin("john");
assertThat(user.login()).isEqualTo("john");
assertThat(user.name()).isEqualTo("John");
assertThat(user.email()).isEqualTo("john@email.com");
assertThat(user.scmAccounts()).containsOnly("jn");
}
@Test
public void reactivate_user() throws Exception {
userSessionRule.login("admin").setLocale(Locale.FRENCH).setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
dbClient.userDao().insert(session, UserTesting.newUserDto("john", "John", "john@email.com"));
session.commit();
dbClient.userDao().deactivateUserByLogin(session, "john");
userIndexer.index();
when(i18n.message(Locale.FRENCH, "user.reactivated", "user.reactivated", "john")).thenReturn("The user 'john' has been reactivated.");
tester.newPostRequest("api/users", "create")
.setParam("login", "john")
.setParam("name", "John")
.setParam("email", "john@email.com")
.setParam("scm_accounts", "jn")
.setParam("password", "1234").execute()
.assertJson(getClass(), "reactivate_user.json");
assertThat(index.getNullableByLogin("john").active()).isTrue();
}
@Test(expected = ForbiddenException.class)
public void fail_on_missing_permission() throws Exception {
userSessionRule.login("not_admin");
tester.newPostRequest("api/users", "create")
.setParam("login", "john")
.setParam("name", "John")
.setParam("email", "john@email.com")
.setParam("scm_accounts", "jn")
.setParam("password", "1234").execute();
}
}