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.

CleanupDisabledUsersTest.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.platform.db.migration.version.v67;
  21. import com.google.common.collect.ImmutableList;
  22. import java.sql.SQLException;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Objects;
  28. import java.util.Random;
  29. import java.util.stream.Collectors;
  30. import javax.annotation.Nullable;
  31. import org.junit.Rule;
  32. import org.junit.Test;
  33. import org.sonar.api.impl.utils.TestSystem2;
  34. import org.sonar.api.utils.System2;
  35. import org.sonar.db.CoreDbTester;
  36. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  37. import static org.assertj.core.api.Assertions.assertThat;
  38. public class CleanupDisabledUsersTest {
  39. private final static long PAST = 100_000_000_000L;
  40. private final static long NOW = 500_000_000_000L;
  41. private final static Random RANDOM = new Random();
  42. private final static String SELECT_USERS = "select name, scm_accounts, user_local, login, crypted_password, salt, email, external_identity, external_identity_provider, active, is_root, onboarded, created_at, updated_at from users";
  43. private System2 system2 = new TestSystem2().setNow(NOW);
  44. @Rule
  45. public CoreDbTester db = CoreDbTester.createForSchema(CleanupDisabledUsersTest.class, "users.sql");
  46. private CleanupDisabledUsers underTest = new CleanupDisabledUsers(db.database(), system2);
  47. @Test
  48. public void do_nothing_when_no_data() throws SQLException {
  49. assertThat(db.countRowsOfTable("USERS")).isEqualTo(0);
  50. underTest.execute();
  51. assertThat(db.countRowsOfTable("USERS")).isEqualTo(0);
  52. }
  53. @Test
  54. public void execute_must_update_database() throws SQLException {
  55. final List<User> users = generateAndInsertUsers();
  56. underTest.execute();
  57. assertDatabaseContainsExactly(applyCleanupOnUsers(users));
  58. }
  59. @Test
  60. public void migration_is_reentrant() throws SQLException {
  61. final List<User> users = generateAndInsertUsers();
  62. underTest.execute();
  63. underTest.execute();
  64. assertDatabaseContainsExactly(applyCleanupOnUsers(users));
  65. }
  66. private List<User> generateAndInsertUsers() {
  67. List<User> users = ImmutableList.of(
  68. new User("user1", null, null, null, null, null, false),
  69. new User("user2", randomAlphanumeric(10), null, null, null, null, false),
  70. new User("user3", null, randomAlphanumeric(10), null, null, null, false),
  71. new User("user4", null, null, randomAlphanumeric(10), null, null, false),
  72. new User("user5", null, null, null, randomAlphanumeric(10), null, false),
  73. new User("user6", null, null, null, null, randomAlphanumeric(10), false),
  74. new User("user7", randomAlphanumeric(10), randomAlphanumeric(10), randomAlphanumeric(10), randomAlphanumeric(10), randomAlphanumeric(10), false),
  75. new User("user8", randomAlphanumeric(10), randomAlphanumeric(10), randomAlphanumeric(10), randomAlphanumeric(10), randomAlphanumeric(10), true),
  76. new User("user9", randomAlphanumeric(10), randomAlphanumeric(10), randomAlphanumeric(10), randomAlphanumeric(10), randomAlphanumeric(10), true)
  77. );
  78. users.forEach(User::insert);
  79. return users;
  80. }
  81. private List<User> applyCleanupOnUsers(List<User> users) {
  82. return users.stream().map(
  83. u -> {
  84. User cleanedupUser = u.clone();
  85. // If a user is active => no change
  86. if (cleanedupUser.active) {
  87. return cleanedupUser;
  88. }
  89. // updated_at field will only be updated if there is a real change so if at least one field is not null
  90. if (cleanedupUser.cryptedPassword != null || cleanedupUser.salt != null || cleanedupUser.email != null ||
  91. cleanedupUser.externalIdentityProvider != null || cleanedupUser.externalIdentity != null) {
  92. cleanedupUser.updatedAt = NOW;
  93. }
  94. // Cleanup fields, all those fields must be null
  95. cleanedupUser.cryptedPassword = null;
  96. cleanedupUser.salt = null;
  97. cleanedupUser.email = null;
  98. cleanedupUser.externalIdentityProvider = null;
  99. cleanedupUser.externalIdentity = null;
  100. return cleanedupUser;
  101. }).collect(Collectors.toList());
  102. }
  103. private void assertDatabaseContainsExactly(List<User> expectedUsers) {
  104. assertThat(db.select(SELECT_USERS)).isEqualTo(
  105. expectedUsers.stream().map(User::toMap).collect(Collectors.toList())
  106. );
  107. }
  108. private class User {
  109. private String login;
  110. private String cryptedPassword;
  111. private String salt;
  112. private String email;
  113. private String externalIdentity;
  114. private String externalIdentityProvider;
  115. private String name;
  116. private String scmAccounts;
  117. private boolean userLocal;
  118. private boolean active;
  119. private boolean isRoot;
  120. private boolean onBoarded;
  121. private long updatedAt;
  122. private long createdAt;
  123. private User(String login, @Nullable String cryptedPassword, @Nullable String salt, @Nullable String email,
  124. @Nullable String externalIdentity, @Nullable String externalIdentityProvider, boolean active) {
  125. this.login = login;
  126. this.cryptedPassword = cryptedPassword;
  127. this.salt = salt;
  128. this.email = email;
  129. this.externalIdentity = externalIdentity;
  130. this.externalIdentityProvider = externalIdentityProvider;
  131. this.active = active;
  132. this.isRoot = RANDOM.nextBoolean();
  133. this.onBoarded = RANDOM.nextBoolean();
  134. this.userLocal = RANDOM.nextBoolean();
  135. this.scmAccounts = randomAlphanumeric(1500);
  136. this.name = randomAlphanumeric(200);
  137. this.updatedAt = PAST;
  138. this.createdAt = PAST;
  139. }
  140. private void insert() {
  141. db.executeInsert("USERS", toMap());
  142. }
  143. private Map<String, Object> toMap() {
  144. HashMap<String, Object> map = new HashMap<>();
  145. map.put("LOGIN", login);
  146. map.put("IS_ROOT", isRoot);
  147. map.put("ONBOARDED", onBoarded);
  148. map.put("ACTIVE", active);
  149. map.put("CREATED_AT", createdAt);
  150. map.put("UPDATED_AT", updatedAt);
  151. map.put("CRYPTED_PASSWORD", cryptedPassword);
  152. map.put("SALT", salt);
  153. map.put("EMAIL", email);
  154. map.put("EXTERNAL_IDENTITY", externalIdentity);
  155. map.put("EXTERNAL_IDENTITY_PROVIDER", externalIdentityProvider);
  156. map.put("NAME", name);
  157. map.put("SCM_ACCOUNTS", scmAccounts);
  158. map.put("USER_LOCAL", userLocal);
  159. return Collections.unmodifiableMap(map);
  160. }
  161. @Override
  162. public User clone() {
  163. User user = new User(this.login, this.cryptedPassword, this.salt, this.email, this.externalIdentity, this.externalIdentityProvider, this.active);
  164. user.name = this.name;
  165. user.scmAccounts = this.scmAccounts;
  166. user.userLocal = this.userLocal;
  167. user.onBoarded = this.onBoarded;
  168. user.isRoot = this.isRoot;
  169. return user;
  170. }
  171. @Override
  172. public boolean equals(Object o) {
  173. if (this == o) {
  174. return true;
  175. }
  176. if (!(o instanceof User)) {
  177. return false;
  178. }
  179. User user = (User) o;
  180. return active == user.active &&
  181. isRoot == user.isRoot &&
  182. onBoarded == user.onBoarded &&
  183. Objects.equals(login, user.login) &&
  184. Objects.equals(cryptedPassword, user.cryptedPassword) &&
  185. Objects.equals(salt, user.salt) &&
  186. Objects.equals(email, user.email) &&
  187. Objects.equals(externalIdentity, user.externalIdentity) &&
  188. Objects.equals(externalIdentityProvider, user.externalIdentityProvider);
  189. }
  190. @Override
  191. public int hashCode() {
  192. return Objects.hash(login, cryptedPassword, salt, email, externalIdentity, externalIdentityProvider, active, isRoot, onBoarded);
  193. }
  194. }
  195. }