From: Julien Lancelot Date: Mon, 12 Jun 2017 10:55:32 +0000 (+0200) Subject: SONAR-9356 Use global setting to set value of USERS.ONBOARDED X-Git-Tag: 6.5-M2~124 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3d443765f55e9cea1c09f858598538991e39aa49;p=sonarqube.git SONAR-9356 Use global setting to set value of USERS.ONBOARDED --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java index 5467fde3fa9..d1ac28f0a3a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java @@ -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 messages) { if (isNullOrEmpty(value)) { messages.add(format(Validation.CANT_BE_EMPTY_MESSAGE, param)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/authentication/SsoAuthenticatorTest.java b/server/sonar-server/src/test/java/org/sonar/server/authentication/SsoAuthenticatorTest.java index 39006cdc8f3..574832e5805 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/authentication/SsoAuthenticatorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/authentication/SsoAuthenticatorTest.java @@ -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); diff --git a/server/sonar-server/src/test/java/org/sonar/server/authentication/UserIdentityAuthenticatorTest.java b/server/sonar-server/src/test/java/org/sonar/server/authentication/UserIdentityAuthenticatorTest.java index d412742e394..f54e0f93ff4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/authentication/UserIdentityAuthenticatorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/authentication/UserIdentityAuthenticatorTest.java @@ -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() diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/UserUpdaterTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/UserUpdaterTest.java index c9145623c0e..d7aaeabb1a9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/UserUpdaterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/UserUpdaterTest.java @@ -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") diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/ChangePasswordActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/ChangePasswordActionTest.java index b2a5be3b41c..76389fe45cc 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/ChangePasswordActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/ChangePasswordActionTest.java @@ -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))); diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/CreateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CreateActionTest.java index 9076e41d9d1..9022c7c79e5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/CreateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CreateActionTest.java @@ -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 diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java index 227b9478c14..53f5461133f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java @@ -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))); }