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.

ChangePasswordActionTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 org.junit.Before;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import org.sonar.api.config.internal.MapSettings;
  26. import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
  27. import org.sonar.api.server.ws.WebService;
  28. import org.sonar.api.utils.System2;
  29. import org.sonar.db.DbTester;
  30. import org.sonar.server.authentication.CredentialsLocalAuthentication;
  31. import org.sonar.server.es.EsTester;
  32. import org.sonar.server.exceptions.BadRequestException;
  33. import org.sonar.server.exceptions.ForbiddenException;
  34. import org.sonar.server.exceptions.NotFoundException;
  35. import org.sonar.server.organization.TestDefaultOrganizationProvider;
  36. import org.sonar.server.tester.UserSessionRule;
  37. import org.sonar.server.user.NewUser;
  38. import org.sonar.server.user.NewUserNotifier;
  39. import org.sonar.server.user.UserUpdater;
  40. import org.sonar.server.user.index.UserIndexer;
  41. import org.sonar.server.usergroups.DefaultGroupFinder;
  42. import org.sonar.server.ws.TestResponse;
  43. import org.sonar.server.ws.WsActionTester;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static org.mockito.Mockito.mock;
  46. import static org.sonar.db.user.UserTesting.newExternalUser;
  47. import static org.sonar.db.user.UserTesting.newLocalUser;
  48. public class ChangePasswordActionTest {
  49. @Rule
  50. public ExpectedException expectedException = ExpectedException.none();
  51. @Rule
  52. public DbTester db = DbTester.create();
  53. @Rule
  54. public EsTester es = EsTester.create();
  55. @Rule
  56. public UserSessionRule userSessionRule = UserSessionRule.standalone().logIn();
  57. private TestDefaultOrganizationProvider testDefaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
  58. private CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient());
  59. private UserUpdater userUpdater = new UserUpdater(
  60. mock(NewUserNotifier.class), db.getDbClient(), new UserIndexer(db.getDbClient(), es.client()), testDefaultOrganizationProvider,
  61. new DefaultGroupFinder(db.getDbClient()),
  62. new MapSettings().asConfig(),
  63. localAuthentication);
  64. private WsActionTester tester = new WsActionTester(new ChangePasswordAction(db.getDbClient(), userUpdater, userSessionRule, localAuthentication));
  65. @Before
  66. public void setUp() {
  67. db.users().insertDefaultGroup();
  68. }
  69. @Test
  70. public void a_user_can_update_his_password() {
  71. userUpdater.createAndCommit(db.getSession(), NewUser.builder()
  72. .setEmail("john@email.com")
  73. .setLogin("john")
  74. .setName("John")
  75. .setPassword("Valar Dohaeris")
  76. .build(), u -> {
  77. });
  78. String oldCryptedPassword = db.getDbClient().userDao().selectByLogin(db.getSession(), "john").getCryptedPassword();
  79. userSessionRule.logIn("john");
  80. TestResponse response = tester.newRequest()
  81. .setParam("login", "john")
  82. .setParam("previousPassword", "Valar Dohaeris")
  83. .setParam("password", "Valar Morghulis")
  84. .execute();
  85. assertThat(response.getStatus()).isEqualTo(204);
  86. String newCryptedPassword = db.getDbClient().userDao().selectByLogin(db.getSession(), "john").getCryptedPassword();
  87. assertThat(newCryptedPassword).isNotEqualTo(oldCryptedPassword);
  88. }
  89. @Test
  90. public void system_administrator_can_update_password_of_user() {
  91. userSessionRule.logIn().setSystemAdministrator();
  92. createLocalUser();
  93. String originalPassword = db.getDbClient().userDao().selectByLogin(db.getSession(), "john").getCryptedPassword();
  94. tester.newRequest()
  95. .setParam("login", "john")
  96. .setParam("password", "Valar Morghulis")
  97. .execute();
  98. String newPassword = db.getDbClient().userDao().selectByLogin(db.getSession(), "john").getCryptedPassword();
  99. assertThat(newPassword).isNotEqualTo(originalPassword);
  100. }
  101. @Test
  102. public void fail_on_missing_permission() {
  103. createLocalUser();
  104. userSessionRule.logIn("polop");
  105. expectedException.expect(ForbiddenException.class);
  106. tester.newRequest()
  107. .setParam("login", "john")
  108. .execute();
  109. }
  110. @Test
  111. public void fail_on_unknown_user() {
  112. userSessionRule.logIn().setSystemAdministrator();
  113. expectedException.expect(NotFoundException.class);
  114. expectedException.expectMessage("User with login 'polop' has not been found");
  115. tester.newRequest()
  116. .setParam("login", "polop")
  117. .setParam("password", "polop")
  118. .execute();
  119. }
  120. @Test
  121. public void fail_on_disabled_user() {
  122. db.users().insertUser(u -> u.setLogin("polop").setActive(false));
  123. userSessionRule.logIn().setSystemAdministrator();
  124. expectedException.expect(NotFoundException.class);
  125. expectedException.expectMessage("User with login 'polop' has not been found");
  126. tester.newRequest()
  127. .setParam("login", "polop")
  128. .setParam("password", "polop")
  129. .execute();
  130. }
  131. @Test
  132. public void fail_to_update_password_on_self_without_old_password() {
  133. createLocalUser();
  134. userSessionRule.logIn("john");
  135. expectedException.expect(IllegalArgumentException.class);
  136. tester.newRequest()
  137. .setParam("login", "john")
  138. .setParam("password", "Valar Morghulis")
  139. .execute();
  140. }
  141. @Test
  142. public void fail_to_update_password_on_self_with_bad_old_password() {
  143. createLocalUser();
  144. userSessionRule.logIn("john");
  145. expectedException.expect(IllegalArgumentException.class);
  146. tester.newRequest()
  147. .setParam("login", "john")
  148. .setParam("previousPassword", "I dunno")
  149. .setParam("password", "Valar Morghulis")
  150. .execute();
  151. }
  152. @Test
  153. public void fail_to_update_password_on_external_auth() {
  154. userSessionRule.logIn().setSystemAdministrator();
  155. db.users().insertUser(newExternalUser("john", "John", "john@email.com"));
  156. expectedException.expect(BadRequestException.class);
  157. tester.newRequest()
  158. .setParam("login", "john")
  159. .setParam("password", "Valar Morghulis")
  160. .execute();
  161. }
  162. @Test
  163. public void test_definition() {
  164. WebService.Action action = tester.getDef();
  165. assertThat(action).isNotNull();
  166. assertThat(action.isPost()).isTrue();
  167. assertThat(action.params()).hasSize(3);
  168. }
  169. private void createLocalUser() {
  170. db.users().insertUser(newLocalUser("john", "John", "john@email.com"));
  171. }
  172. }