]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9356 Use global setting to set value of USERS.ONBOARDED
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 12 Jun 2017 10:55:32 +0000 (12:55 +0200)
committerStas Vilchik <stas.vilchik@sonarsource.com>
Tue, 20 Jun 2017 11:10:53 +0000 (04:10 -0700)
server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java
server/sonar-server/src/test/java/org/sonar/server/authentication/SsoAuthenticatorTest.java
server/sonar-server/src/test/java/org/sonar/server/authentication/UserIdentityAuthenticatorTest.java
server/sonar-server/src/test/java/org/sonar/server/user/UserUpdaterTest.java
server/sonar-server/src/test/java/org/sonar/server/user/ws/ChangePasswordActionTest.java
server/sonar-server/src/test/java/org/sonar/server/user/ws/CreateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java

index 5467fde3fa9bb4e8ff21adf7a7c4bc86b14babce..d1ac28f0a3a1691b54cebf905e6a1d5255c4bd03 100644 (file)
@@ -28,6 +28,7 @@ import java.util.Objects;
 import java.util.Random;
 import javax.annotation.Nullable;
 import org.apache.commons.codec.digest.DigestUtils;
+import org.sonar.api.config.Settings;
 import org.sonar.api.platform.NewUserHandler;
 import org.sonar.api.server.ServerSide;
 import org.sonar.api.utils.System2;
@@ -49,6 +50,7 @@ import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Strings.isNullOrEmpty;
 import static com.google.common.collect.Lists.newArrayList;
 import static java.lang.String.format;
+import static org.sonar.core.config.CorePropertyDefinitions.ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS;
 import static org.sonar.db.user.UserDto.encryptPassword;
 import static org.sonar.server.ws.WsUtils.checkFound;
 import static org.sonar.server.ws.WsUtils.checkRequest;
@@ -76,9 +78,10 @@ public class UserUpdater {
   private final DefaultOrganizationProvider defaultOrganizationProvider;
   private final OrganizationCreation organizationCreation;
   private final DefaultGroupFinder defaultGroupFinder;
+  private final Settings settings;
 
   public UserUpdater(NewUserNotifier newUserNotifier, DbClient dbClient, UserIndexer userIndexer, System2 system2, OrganizationFlags organizationFlags,
-    DefaultOrganizationProvider defaultOrganizationProvider, OrganizationCreation organizationCreation, DefaultGroupFinder defaultGroupFinder) {
+    DefaultOrganizationProvider defaultOrganizationProvider, OrganizationCreation organizationCreation, DefaultGroupFinder defaultGroupFinder, Settings settings) {
     this.newUserNotifier = newUserNotifier;
     this.dbClient = dbClient;
     this.userIndexer = userIndexer;
@@ -87,6 +90,7 @@ public class UserUpdater {
     this.defaultOrganizationProvider = defaultOrganizationProvider;
     this.organizationCreation = organizationCreation;
     this.defaultGroupFinder = defaultGroupFinder;
+    this.settings = settings;
   }
 
   public UserDto create(DbSession dbSession, NewUser newUser) {
@@ -115,6 +119,7 @@ public class UserUpdater {
     }
     // Hack to allow to change the password of the user
     existingUser.setLocal(true);
+    setOnboarded(existingUser);
     updateUserDto(dbSession, updateUser, existingUser);
     updateUser(dbSession, existingUser);
     addUserToDefaultOrganizationAndDefaultGroup(dbSession, existingUser);
@@ -162,6 +167,7 @@ public class UserUpdater {
     }
 
     setExternalIdentity(userDto, newUser.externalIdentity());
+    setOnboarded(userDto);
 
     checkRequest(messages.isEmpty(), messages);
     return userDto;
@@ -253,6 +259,11 @@ public class UserUpdater {
     }
   }
 
+  private void setOnboarded(UserDto userDto) {
+    boolean showOnboarding = settings.getBoolean(ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS);
+    userDto.setOnboarded(!showOnboarding);
+  }
+
   private static boolean checkNotEmptyParam(@Nullable String value, String param, List<String> messages) {
     if (isNullOrEmpty(value)) {
       messages.add(format(Validation.CANT_BE_EMPTY_MESSAGE, param));
index 39006cdc8f350adf26e1f840a5cbe7ce21b21a26..574832e5805544195a5db84baece3a35d608b5b1 100644 (file)
@@ -102,7 +102,7 @@ public class SsoAuthenticatorTest {
   private UserIdentityAuthenticator userIdentityAuthenticator = new UserIdentityAuthenticator(
     db.getDbClient(),
     new UserUpdater(mock(NewUserNotifier.class), db.getDbClient(), mock(UserIndexer.class), System2.INSTANCE, organizationFlags, defaultOrganizationProvider, organizationCreation,
-      new DefaultGroupFinder(db.getDbClient())),
+      new DefaultGroupFinder(db.getDbClient()), settings),
     defaultOrganizationProvider, organizationFlags, new DefaultGroupFinder(db.getDbClient()));
 
   private HttpServletResponse response = mock(HttpServletResponse.class);
index d412742e394a2de89c88fc6072b6bc53ef1465c6..f54e0f93ff48e7e380bfdbd97caf453c7a94b109 100644 (file)
@@ -24,6 +24,7 @@ import java.util.stream.Collectors;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.config.MapSettings;
 import org.sonar.api.server.authentication.UserIdentity;
 import org.sonar.api.utils.System2;
 import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
@@ -45,6 +46,7 @@ import static com.google.common.collect.Sets.newHashSet;
 import static java.util.Arrays.stream;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
+import static org.sonar.core.config.CorePropertyDefinitions.ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS;
 import static org.sonar.db.user.UserTesting.newUserDto;
 import static org.sonar.server.authentication.event.AuthenticationEvent.Method;
 import static org.sonar.server.authentication.event.AuthenticationEvent.Source;
@@ -76,6 +78,7 @@ public class UserIdentityAuthenticatorTest {
   private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
   private OrganizationCreation organizationCreation = mock(OrganizationCreation.class);
   private TestOrganizationFlags organizationFlags = TestOrganizationFlags.standalone();
+  private MapSettings settings = new MapSettings();
 
   private UserUpdater userUpdater = new UserUpdater(
     mock(NewUserNotifier.class),
@@ -85,7 +88,8 @@ public class UserIdentityAuthenticatorTest {
     organizationFlags,
     defaultOrganizationProvider,
     organizationCreation,
-    new DefaultGroupFinder(db.getDbClient()));
+    new DefaultGroupFinder(db.getDbClient()),
+    settings);
   private UserIdentityAuthenticator underTest = new UserIdentityAuthenticator(db.getDbClient(), userUpdater, defaultOrganizationProvider, organizationFlags,
     new DefaultGroupFinder(db.getDbClient()));
 
@@ -146,6 +150,26 @@ public class UserIdentityAuthenticatorTest {
     checkGroupMembership(user, group1);
   }
 
+  @Test
+  public void authenticate_new_user_sets_onboarded_flag_to_false_when_onboarding_setting_is_set_to_true() {
+    organizationFlags.setEnabled(true);
+    settings.setProperty(ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS, true);
+
+    underTest.authenticate(USER_IDENTITY, IDENTITY_PROVIDER, Source.realm(Method.BASIC, IDENTITY_PROVIDER.getName()));
+
+    assertThat(db.users().selectUserByLogin(USER_LOGIN).get().isOnboarded()).isFalse();
+  }
+
+  @Test
+  public void authenticate_new_user_sets_onboarded_flag_to_true_when_onboarding_setting_is_set_to_false() {
+    organizationFlags.setEnabled(true);
+    settings.setProperty(ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS, false);
+
+    underTest.authenticate(USER_IDENTITY, IDENTITY_PROVIDER, Source.realm(Method.BASIC, IDENTITY_PROVIDER.getName()));
+
+    assertThat(db.users().selectUserByLogin(USER_LOGIN).get().isOnboarded()).isTrue();
+  }
+
   @Test
   public void authenticate_existing_user() throws Exception {
     db.users().insertUser(newUserDto()
index c9145623c0e5c1e649cdf683ab05eabd877fcd98..d7aaeabb1a9946b2951cfee20ca882161198d06d 100644 (file)
@@ -24,7 +24,6 @@ import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Multimap;
 import java.util.List;
 import org.elasticsearch.search.SearchHit;
-import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -32,6 +31,7 @@ import org.mockito.ArgumentCaptor;
 import org.sonar.api.config.MapSettings;
 import org.sonar.api.platform.NewUserHandler;
 import org.sonar.api.utils.System2;
+import org.sonar.api.utils.internal.TestSystem2;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
@@ -59,7 +59,7 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.sonar.core.config.CorePropertyDefinitions.ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS;
 import static org.sonar.db.user.UserTesting.newDisabledUser;
 import static org.sonar.db.user.UserTesting.newLocalUser;
 import static org.sonar.db.user.UserTesting.newUserDto;
@@ -71,7 +71,7 @@ public class UserUpdaterTest {
   private static final long PAST = 1000000000000L;
   private static final String DEFAULT_LOGIN = "marius";
 
-  private System2 system2 = mock(System2.class);
+  private System2 system2 = new TestSystem2().setNow(NOW);
 
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
@@ -90,13 +90,9 @@ public class UserUpdaterTest {
   private OrganizationCreation organizationCreation = mock(OrganizationCreation.class);
   private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
   private TestOrganizationFlags organizationFlags = TestOrganizationFlags.standalone();
+  private MapSettings settings = new MapSettings();
   private UserUpdater underTest = new UserUpdater(newUserNotifier, dbClient, userIndexer, system2, organizationFlags, defaultOrganizationProvider, organizationCreation,
-    new DefaultGroupFinder(dbClient));
-
-  @Before
-  public void setUp() {
-    when(system2.now()).thenReturn(NOW);
-  }
+    new DefaultGroupFinder(dbClient), settings);
 
   @Test
   public void create_user() {
@@ -133,6 +129,24 @@ public class UserUpdaterTest {
         entry("email", "user@mail.com"));
   }
 
+  @Test
+  public void create_user_with_minimum_fields() {
+    createDefaultGroup();
+
+    underTest.create(db.getSession(), NewUser.builder()
+      .setLogin("us")
+      .setName("User")
+      .build());
+
+    UserDto dto = dbClient.userDao().selectByLogin(session, "us");
+    assertThat(dto.getId()).isNotNull();
+    assertThat(dto.getLogin()).isEqualTo("us");
+    assertThat(dto.getName()).isEqualTo("User");
+    assertThat(dto.getEmail()).isNull();
+    assertThat(dto.getScmAccounts()).isNull();
+    assertThat(dto.isActive()).isTrue();
+  }
+
   @Test
   public void create_user_with_sq_authority_when_no_authority_set() throws Exception {
     createDefaultGroup();
@@ -185,24 +199,6 @@ public class UserUpdaterTest {
     assertThat(dto.getSalt()).isNull();
   }
 
-  @Test
-  public void create_user_with_minimum_fields() {
-    createDefaultGroup();
-
-    underTest.create(db.getSession(), NewUser.builder()
-      .setLogin("us")
-      .setName("User")
-      .build());
-
-    UserDto dto = dbClient.userDao().selectByLogin(session, "us");
-    assertThat(dto.getId()).isNotNull();
-    assertThat(dto.getLogin()).isEqualTo("us");
-    assertThat(dto.getName()).isEqualTo("User");
-    assertThat(dto.getEmail()).isNull();
-    assertThat(dto.getScmAccounts()).isNull();
-    assertThat(dto.isActive()).isTrue();
-  }
-
   @Test
   public void create_user_with_scm_accounts_containing_blank_or_null_entries() {
     createDefaultGroup();
@@ -245,6 +241,32 @@ public class UserUpdaterTest {
     assertThat(dbClient.userDao().selectByLogin(session, "user").getScmAccountsAsList()).containsOnly("u1");
   }
 
+  @Test
+  public void create_not_onboarded_user_if_onboarding_setting_is_set_to_false() {
+    createDefaultGroup();
+    settings.setProperty(ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS, false);
+
+    underTest.create(db.getSession(), NewUser.builder()
+      .setLogin("user")
+      .setName("User")
+      .build());
+
+    assertThat(dbClient.userDao().selectByLogin(session, "user").isOnboarded()).isTrue();
+  }
+
+  @Test
+  public void create_onboarded_user_if_onboarding_setting_is_set_to_true() {
+    createDefaultGroup();
+    settings.setProperty(ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS, true);
+
+    UserDto user = underTest.create(db.getSession(), NewUser.builder()
+      .setLogin("user")
+      .setName("User")
+      .build());
+
+    assertThat(dbClient.userDao().selectByLogin(session, "user").isOnboarded()).isFalse();
+  }
+
   @Test
   public void fail_to_create_user_with_missing_login() {
     expectedException.expect(BadRequestException.class);
@@ -701,6 +723,38 @@ public class UserUpdaterTest {
     assertThat(dbClient.organizationMemberDao().select(db.getSession(), defaultOrganizationProvider.get().getUuid(), dto.getId())).isNotPresent();
   }
 
+  @Test
+  public void reactivate_not_onboarded_user_if_onboarding_setting_is_set_to_false() {
+    settings.setProperty(ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS, false);
+    UserDto user = db.users().insertUser(u -> u
+      .setActive(false)
+      .setOnboarded(false));
+    createDefaultGroup();
+
+    underTest.create(db.getSession(), NewUser.builder()
+      .setLogin(user.getLogin())
+      .setName("name")
+      .build());
+
+    assertThat(dbClient.userDao().selectByLogin(session, user.getLogin()).isOnboarded()).isTrue();
+  }
+
+  @Test
+  public void reactivate_onboarded_user_if_onboarding_setting_is_set_to_true() {
+    settings.setProperty(ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS, true);
+    UserDto user = db.users().insertUser(u -> u
+      .setActive(false)
+      .setOnboarded(true));
+    createDefaultGroup();
+
+    underTest.create(db.getSession(), NewUser.builder()
+      .setLogin(user.getLogin())
+      .setName("name")
+      .build());
+
+    assertThat(dbClient.userDao().selectByLogin(session, user.getLogin()).isOnboarded()).isFalse();
+  }
+
   @Test
   public void update_user() {
     UserDto user = db.users().insertUser(newLocalUser(DEFAULT_LOGIN, "Marius", "marius@email.com")
index b2a5be3b41ca4f674cadbf8a46fa29b63f42445b..76389fe45cc6fef463ef762efaeb0008cb162f84 100644 (file)
@@ -64,7 +64,8 @@ public class ChangePasswordActionTest {
     organizationFlags,
     TestDefaultOrganizationProvider.from(db),
     mock(OrganizationCreation.class),
-    new DefaultGroupFinder(db.getDbClient()));
+    new DefaultGroupFinder(db.getDbClient()),
+    new MapSettings());
 
   private WsTester tester = new WsTester(new UsersWs(new ChangePasswordAction(db.getDbClient(), userUpdater, userSessionRule)));
 
index 9076e41d9d13cf90feb9e0621a9e5c1a65e585a3..9022c7c79e5ee197d1ae9897d9c6577c41013580 100644 (file)
@@ -87,7 +87,7 @@ public class CreateActionTest {
   private WsActionTester tester = new WsActionTester(new CreateAction(
     db.getDbClient(),
     new UserUpdater(mock(NewUserNotifier.class), db.getDbClient(), userIndexer, system2, organizationFlags, defaultOrganizationProvider,
-      organizationCreation, new DefaultGroupFinder(db.getDbClient())),
+      organizationCreation, new DefaultGroupFinder(db.getDbClient()), settings),
     userSessionRule));
 
   @Before
index 227b9478c1466e5cd1d81a401dc9dd4707449f21..53f5461133fbe9ca50546d9ab7db1d6d6fb08d97 100644 (file)
@@ -77,7 +77,7 @@ public class UpdateActionTest {
     userIndexer = new UserIndexer(dbClient, esTester.client());
     tester = new WsTester(new UsersWs(new UpdateAction(
       new UserUpdater(mock(NewUserNotifier.class), dbClient, userIndexer, system2, organizationFlags, defaultOrganizationProvider, ORGANIZATION_CREATION_NOT_USED_FOR_UPDATE,
-        new DefaultGroupFinder(dbTester.getDbClient())),
+        new DefaultGroupFinder(dbTester.getDbClient()), settings),
       userSessionRule,
       new UserJsonWriter(userSessionRule), dbClient)));
   }