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 7.2KB

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