You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CreateActionTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.user.ws;
  21. import java.util.HashSet;
  22. import java.util.Optional;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.sonar.api.config.internal.MapSettings;
  28. import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
  29. import org.sonar.api.server.ws.WebService;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.core.config.CorePropertyDefinitions;
  32. import org.sonar.db.DbTester;
  33. import org.sonar.db.user.GroupDto;
  34. import org.sonar.db.user.UserDto;
  35. import org.sonar.server.authentication.CredentialsLocalAuthentication;
  36. import org.sonar.server.es.EsTester;
  37. import org.sonar.server.exceptions.ForbiddenException;
  38. import org.sonar.server.organization.DefaultOrganizationProvider;
  39. import org.sonar.server.organization.TestDefaultOrganizationProvider;
  40. import org.sonar.server.tester.UserSessionRule;
  41. import org.sonar.server.user.NewUserNotifier;
  42. import org.sonar.server.user.UserUpdater;
  43. import org.sonar.server.user.index.UserIndexDefinition;
  44. import org.sonar.server.user.index.UserIndexer;
  45. import org.sonar.server.user.ws.CreateAction.CreateRequest;
  46. import org.sonar.server.usergroups.DefaultGroupFinder;
  47. import org.sonar.server.ws.TestRequest;
  48. import org.sonar.server.ws.WsActionTester;
  49. import org.sonarqube.ws.Users.CreateWsResponse;
  50. import org.sonarqube.ws.Users.CreateWsResponse.User;
  51. import static java.lang.String.format;
  52. import static java.util.Collections.singletonList;
  53. import static java.util.Optional.ofNullable;
  54. import static org.assertj.core.api.Assertions.assertThat;
  55. import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
  56. import static org.elasticsearch.index.query.QueryBuilders.termQuery;
  57. import static org.mockito.Mockito.mock;
  58. import static org.sonar.db.user.UserTesting.newUserDto;
  59. import static org.sonar.server.user.index.UserIndexDefinition.FIELD_EMAIL;
  60. import static org.sonar.server.user.index.UserIndexDefinition.FIELD_LOGIN;
  61. import static org.sonar.server.user.index.UserIndexDefinition.FIELD_NAME;
  62. import static org.sonar.server.user.index.UserIndexDefinition.FIELD_SCM_ACCOUNTS;
  63. public class CreateActionTest {
  64. private MapSettings settings = new MapSettings();
  65. private System2 system2 = new AlwaysIncreasingSystem2();
  66. @Rule
  67. public DbTester db = DbTester.create(system2);
  68. @Rule
  69. public EsTester es = EsTester.create();
  70. @Rule
  71. public UserSessionRule userSessionRule = UserSessionRule.standalone();
  72. @Rule
  73. public ExpectedException expectedException = ExpectedException.none();
  74. private UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client());
  75. private GroupDto defaultGroup;
  76. private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
  77. private CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient());
  78. private WsActionTester tester = new WsActionTester(new CreateAction(
  79. db.getDbClient(),
  80. new UserUpdater(mock(NewUserNotifier.class), db.getDbClient(), userIndexer, defaultOrganizationProvider,
  81. new DefaultGroupFinder(db.getDbClient()), settings.asConfig(), localAuthentication),
  82. userSessionRule));
  83. @Before
  84. public void setUp() {
  85. defaultGroup = db.users().insertDefaultGroup();
  86. }
  87. @Test
  88. public void create_user() {
  89. logInAsSystemAdministrator();
  90. CreateWsResponse response = call(CreateRequest.builder()
  91. .setLogin("john")
  92. .setName("John")
  93. .setEmail("john@email.com")
  94. .setScmAccounts(singletonList("jn"))
  95. .setPassword("1234")
  96. .build());
  97. assertThat(response.getUser())
  98. .extracting(User::getLogin, User::getName, User::getEmail, User::getScmAccountsList, User::getLocal)
  99. .containsOnly("john", "John", "john@email.com", singletonList("jn"), true);
  100. // exists in index
  101. assertThat(es.client().prepareSearch(UserIndexDefinition.TYPE_USER)
  102. .setQuery(boolQuery()
  103. .must(termQuery(FIELD_LOGIN, "john"))
  104. .must(termQuery(FIELD_NAME, "John"))
  105. .must(termQuery(FIELD_EMAIL, "john@email.com"))
  106. .must(termQuery(FIELD_SCM_ACCOUNTS, "jn")))
  107. .get().getHits().getHits()).hasSize(1);
  108. // exists in db
  109. Optional<UserDto> dbUser = db.users().selectUserByLogin("john");
  110. assertThat(dbUser).isPresent();
  111. assertThat(dbUser.get().isRoot()).isFalse();
  112. // member of default group in default organization
  113. assertThat(db.users().selectGroupUuidsOfUser(dbUser.get())).containsOnly(defaultGroup.getUuid());
  114. }
  115. @Test
  116. public void create_local_user() {
  117. logInAsSystemAdministrator();
  118. call(CreateRequest.builder()
  119. .setLogin("john")
  120. .setName("John")
  121. .setPassword("1234")
  122. .setLocal(true)
  123. .build());
  124. assertThat(db.users().selectUserByLogin("john").get())
  125. .extracting(UserDto::isLocal, UserDto::getExternalIdentityProvider, UserDto::getExternalLogin, UserDto::isRoot)
  126. .containsOnly(true, "sonarqube", "john", false);
  127. }
  128. @Test
  129. public void create_none_local_user() {
  130. logInAsSystemAdministrator();
  131. call(CreateRequest.builder()
  132. .setLogin("john")
  133. .setName("John")
  134. .setLocal(false)
  135. .build());
  136. assertThat(db.users().selectUserByLogin("john").get())
  137. .extracting(UserDto::isLocal, UserDto::getExternalIdentityProvider, UserDto::getExternalLogin, UserDto::isRoot)
  138. .containsOnly(false, "sonarqube", "john", false);
  139. }
  140. @Test
  141. public void create_user_with_comma_in_scm_account() {
  142. logInAsSystemAdministrator();
  143. CreateWsResponse response = call(CreateRequest.builder()
  144. .setLogin("john")
  145. .setName("John")
  146. .setEmail("john@email.com")
  147. .setScmAccounts(singletonList("j,n"))
  148. .setPassword("1234")
  149. .build());
  150. assertThat(response.getUser().getScmAccountsList()).containsOnly("j,n");
  151. }
  152. @Test
  153. public void create_user_with_empty_email() {
  154. logInAsSystemAdministrator();
  155. call(CreateRequest.builder()
  156. .setLogin("john")
  157. .setName("John")
  158. .setPassword("1234")
  159. .setEmail("")
  160. .build());
  161. assertThat(db.users().selectUserByLogin("john").get())
  162. .extracting(UserDto::getExternalLogin)
  163. .isEqualTo("john");
  164. }
  165. @Test
  166. public void create_user_with_deprecated_scmAccounts_parameter() {
  167. logInAsSystemAdministrator();
  168. tester.newRequest()
  169. .setParam("login", "john")
  170. .setParam("name", "John")
  171. .setParam("password", "1234")
  172. .setParam("scmAccounts", "jn")
  173. .execute();
  174. assertThat(db.users().selectUserByLogin("john").get().getScmAccountsAsList()).containsOnly("jn");
  175. }
  176. @Test
  177. public void create_user_with_deprecated_scm_accounts_parameter() {
  178. logInAsSystemAdministrator();
  179. tester.newRequest()
  180. .setParam("login", "john")
  181. .setParam("name", "John")
  182. .setParam("password", "1234")
  183. .setParam("scm_accounts", "jn")
  184. .execute();
  185. assertThat(db.users().selectUserByLogin("john").get().getScmAccountsAsList()).containsOnly("jn");
  186. }
  187. @Test
  188. public void reactivate_user() {
  189. logInAsSystemAdministrator();
  190. db.users().insertUser(newUserDto("john", "John", "john@email.com").setActive(false));
  191. userIndexer.indexOnStartup(new HashSet<>());
  192. call(CreateRequest.builder()
  193. .setLogin("john")
  194. .setName("John")
  195. .setEmail("john@email.com")
  196. .setScmAccounts(singletonList("jn"))
  197. .setPassword("1234")
  198. .build());
  199. assertThat(db.users().selectUserByLogin("john").get().isActive()).isTrue();
  200. }
  201. @Test
  202. public void fail_to_reactivate_user_when_active_user_exists() {
  203. logInAsSystemAdministrator();
  204. UserDto user = db.users().insertUser();
  205. expectedException.expect(IllegalArgumentException.class);
  206. expectedException.expectMessage(format("An active user with login '%s' already exists", user.getLogin()));
  207. call(CreateRequest.builder()
  208. .setLogin(user.getLogin())
  209. .setName("John")
  210. .setPassword("1234")
  211. .build());
  212. }
  213. @Test
  214. public void fail_when_missing_login() {
  215. logInAsSystemAdministrator();
  216. expectedException.expect(IllegalArgumentException.class);
  217. expectedException.expectMessage("Login is mandatory and must not be empty");
  218. call(CreateRequest.builder()
  219. .setLogin(null)
  220. .setName("John")
  221. .setPassword("1234")
  222. .build());
  223. }
  224. @Test
  225. public void fail_when_login_is_too_short() {
  226. logInAsSystemAdministrator();
  227. expectedException.expect(IllegalArgumentException.class);
  228. expectedException.expectMessage("'login' length (1) is shorter than the minimum authorized (2)");
  229. call(CreateRequest.builder()
  230. .setLogin("a")
  231. .setName("John")
  232. .setPassword("1234")
  233. .build());
  234. }
  235. @Test
  236. public void fail_when_missing_name() {
  237. logInAsSystemAdministrator();
  238. expectedException.expect(IllegalArgumentException.class);
  239. expectedException.expectMessage("Name is mandatory and must not be empty");
  240. call(CreateRequest.builder()
  241. .setLogin("john")
  242. .setName(null)
  243. .setPassword("1234")
  244. .build());
  245. }
  246. @Test
  247. public void fail_when_missing_password() {
  248. logInAsSystemAdministrator();
  249. expectedException.expect(IllegalArgumentException.class);
  250. expectedException.expectMessage("Password is mandatory and must not be empty");
  251. call(CreateRequest.builder()
  252. .setLogin("john")
  253. .setName("John")
  254. .setPassword(null)
  255. .build());
  256. }
  257. @Test
  258. public void fail_when_password_is_set_on_none_local_user() {
  259. logInAsSystemAdministrator();
  260. expectedException.expect(IllegalArgumentException.class);
  261. expectedException.expectMessage("Password should only be set on local user");
  262. call(CreateRequest.builder()
  263. .setLogin("john")
  264. .setName("John")
  265. .setPassword("1234")
  266. .setLocal(false)
  267. .build());
  268. }
  269. @Test
  270. public void fail_when_email_is_invalid() {
  271. logInAsSystemAdministrator();
  272. expectedException.expect(IllegalArgumentException.class);
  273. expectedException.expectMessage("Email 'invalid-email' is not valid");
  274. call(CreateRequest.builder()
  275. .setLogin("pipo")
  276. .setName("John")
  277. .setPassword("1234")
  278. .setEmail("invalid-email")
  279. .build());
  280. }
  281. @Test
  282. public void throw_ForbiddenException_if_not_system_administrator() {
  283. userSessionRule.logIn().setNonSystemAdministrator();
  284. expectedException.expect(ForbiddenException.class);
  285. expectedException.expectMessage("");
  286. expectedException.expect(ForbiddenException.class);
  287. executeRequest("john");
  288. }
  289. @Test
  290. public void test_definition() {
  291. WebService.Action action = tester.getDef();
  292. assertThat(action).isNotNull();
  293. assertThat(action.isPost()).isTrue();
  294. assertThat(action.params()).hasSize(7);
  295. }
  296. private CreateWsResponse executeRequest(String login) {
  297. return call(CreateRequest.builder()
  298. .setLogin(login)
  299. .setName("name of " + login)
  300. .setEmail(login + "@email.com")
  301. .setScmAccounts(singletonList(login.substring(0, 2)))
  302. .setPassword("pwd_" + login)
  303. .build());
  304. }
  305. private void logInAsSystemAdministrator() {
  306. userSessionRule.logIn().setSystemAdministrator();
  307. }
  308. private CreateWsResponse call(CreateRequest createRequest) {
  309. TestRequest request = tester.newRequest();
  310. ofNullable(createRequest.getLogin()).ifPresent(e4 -> request.setParam("login", e4));
  311. ofNullable(createRequest.getName()).ifPresent(e3 -> request.setParam("name", e3));
  312. ofNullable(createRequest.getEmail()).ifPresent(e2 -> request.setParam("email", e2));
  313. ofNullable(createRequest.getPassword()).ifPresent(e1 -> request.setParam("password", e1));
  314. ofNullable(createRequest.getScmAccounts()).ifPresent(e -> request.setMultiParam("scmAccount", e));
  315. request.setParam("local", createRequest.isLocal() ? "true" : "false");
  316. return request.executeProtobuf(CreateWsResponse.class);
  317. }
  318. private void enableCreatePersonalOrg(boolean flag) {
  319. settings.setProperty(CorePropertyDefinitions.ORGANIZATIONS_CREATE_PERSONAL_ORG, flag);
  320. }
  321. }