From 961911bf82fb9592fc6495b42a0dd3cbcfec2974 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 28 Jan 2016 17:32:12 +0100 Subject: [PATCH] SONAR-7233 remove email / SCM accounts when deactivating a user --- .../BaseIdentityProviderTest.java | 19 +- .../OAuth2IdentityProviderTest.java | 6 + .../server/startup/RenameIssueWidgets.java | 15 +- .../server/user/ws/SearchActionTest.java | 5 +- .../DeactivateActionTest/deactivate_user.json | 3 +- .../user/ws/SearchActionTest/user_one.json | 2 +- .../src/main/java/org/sonar/db/DaoModule.java | 2 + .../src/main/java/org/sonar/db/DbClient.java | 7 + .../src/main/java/org/sonar/db/MyBatis.java | 5 +- .../db/dashboard/ActiveDashboardDao.java | 16 +- .../db/dashboard/ActiveDashboardMapper.java | 2 + .../org/sonar/db/dashboard/DashboardDao.java | 14 +- .../org/sonar/db/issue/IssueFilterDao.java | 10 +- .../db/issue/IssueFilterFavouriteDao.java | 8 +- .../sonar/db/measure/MeasureFilterDao.java | 14 +- .../db/measure/MeasureFilterFavouriteDao.java | 35 ++++ .../db/measure/MeasureFilterFavouriteDto.java | 65 ++++++ .../measure/MeasureFilterFavouriteMapper.java | 27 +++ .../sonar/db/measure/MeasureFilterMapper.java | 2 + .../main/java/org/sonar/db/user/UserDto.java | 21 ++ .../db/dashboard/ActiveDashboardMapper.xml | 13 ++ .../measure/MeasureFilterFavouriteMapper.xml | 24 +++ .../sonar/db/measure/MeasureFilterMapper.xml | 26 ++- .../org/sonar/db/user/UserMapper.xml | 17 +- .../test/java/org/sonar/db/DaoModuleTest.java | 2 +- .../db/dashboard/ActiveDashboardDaoTest.java | 39 +++- .../db/measure/MeasureFilterDaoTest.java | 38 +++- .../MeasureFilterFavouriteDaoTest.java | 58 ++++++ .../java/org/sonar/db/user/UserDaoTest.java | 188 ++++++++++++++++-- .../java/org/sonar/db/user/UserTesting.java | 8 + .../UserDaoTest/deactivate_user-result.xml | 45 ----- .../db/user/UserDaoTest/deactivate_user.xml | 43 ---- 32 files changed, 620 insertions(+), 159 deletions(-) create mode 100644 sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterFavouriteDao.java create mode 100644 sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterFavouriteDto.java create mode 100644 sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterFavouriteMapper.java create mode 100644 sonar-db/src/main/resources/org/sonar/db/measure/MeasureFilterFavouriteMapper.xml create mode 100644 sonar-db/src/test/java/org/sonar/db/measure/MeasureFilterFavouriteDaoTest.java delete mode 100644 sonar-db/src/test/resources/org/sonar/db/user/UserDaoTest/deactivate_user-result.xml delete mode 100644 sonar-db/src/test/resources/org/sonar/db/user/UserDaoTest/deactivate_user.xml diff --git a/it/it-tests/src/test/java/it/authorisation/BaseIdentityProviderTest.java b/it/it-tests/src/test/java/it/authorisation/BaseIdentityProviderTest.java index cb789b06c14..705ce8c5684 100644 --- a/it/it-tests/src/test/java/it/authorisation/BaseIdentityProviderTest.java +++ b/it/it-tests/src/test/java/it/authorisation/BaseIdentityProviderTest.java @@ -23,9 +23,9 @@ import com.google.common.base.Optional; import com.sonar.orchestrator.Orchestrator; import it.Category1Suite; import org.junit.After; +import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.sonarqube.ws.client.GetRequest; @@ -66,9 +66,17 @@ public class BaseIdentityProviderTest { setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.enabled", "true"); } + @AfterClass + public static void disableAuthPlugin() throws Exception { + setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.enabled", "false"); + } + @After - public void removeUsers() throws Exception { - userRule.deactivateUsers(userRule.getUsersByEmails(USER_EMAIL, USER_EMAIL_UPDATED)); + public void removeUser() throws Exception { + Optional user = userRule.getUserByLogin(USER_LOGIN); + if (user.isPresent()) { + userRule.deactivateUsers(user.get()); + } } @Test @@ -99,7 +107,6 @@ public class BaseIdentityProviderTest { } @Test - @Ignore("Waiting for SONAR-7233 to be implemented") public void reactivate_disabled_user() throws Exception { setUserCreatedByAuthPlugin(USER_LOGIN, USER_PROVIDER_ID, USER_NAME, USER_EMAIL); @@ -108,7 +115,7 @@ public class BaseIdentityProviderTest { // First connection, user is created authenticateWithFakeAuthProvider(); - Optional user = userRule.getUserByLogin(USER_EMAIL); + Optional user = userRule.getUserByLogin(USER_LOGIN); assertThat(user).isPresent(); // Disable user @@ -119,7 +126,7 @@ public class BaseIdentityProviderTest { userRule.verifyUserExists(USER_LOGIN, USER_NAME, USER_EMAIL); } - private void setUserCreatedByAuthPlugin(String login, String providerId, String name, String email){ + private void setUserCreatedByAuthPlugin(String login, String providerId, String name, String email) { setServerProperty(ORCHESTRATOR, "sonar.auth.fake-base-id-provider.user", login + "," + providerId + "," + name + "," + email); } diff --git a/it/it-tests/src/test/java/it/authorisation/OAuth2IdentityProviderTest.java b/it/it-tests/src/test/java/it/authorisation/OAuth2IdentityProviderTest.java index 9d34c18d935..1919bcf5777 100644 --- a/it/it-tests/src/test/java/it/authorisation/OAuth2IdentityProviderTest.java +++ b/it/it-tests/src/test/java/it/authorisation/OAuth2IdentityProviderTest.java @@ -25,6 +25,7 @@ import com.squareup.okhttp.mockwebserver.MockWebServer; import it.Category1Suite; import java.net.HttpURLConnection; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; @@ -77,6 +78,11 @@ public class OAuth2IdentityProviderTest { fakeServerAuthProvider.shutdown(); } + @AfterClass + public static void disableAuthPlugin() throws Exception { + setServerProperty(ORCHESTRATOR, "sonar.auth.fake-oauth2-id-provider.enabled", "false"); + } + @Test public void create_new_user_when_authenticate() throws Exception { simulateRedirectionToCallback(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/startup/RenameIssueWidgets.java b/server/sonar-server/src/main/java/org/sonar/server/startup/RenameIssueWidgets.java index 0136889710c..de1dab2ad32 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/startup/RenameIssueWidgets.java +++ b/server/sonar-server/src/main/java/org/sonar/server/startup/RenameIssueWidgets.java @@ -21,24 +21,23 @@ package org.sonar.server.startup; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; +import java.util.Date; +import java.util.List; +import java.util.Map; import org.picocontainer.Startable; import org.sonar.api.utils.System2; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.MyBatis; import org.sonar.db.dashboard.DashboardDto; import org.sonar.db.dashboard.WidgetDto; import org.sonar.db.dashboard.WidgetPropertyDto; import org.sonar.db.issue.IssueFilterDto; -import org.sonar.db.DbSession; -import org.sonar.db.MyBatis; import org.sonar.db.loadedtemplate.LoadedTemplateDto; -import org.sonar.db.DbClient; import org.sonar.server.issue.filter.RegisterIssueFilters; -import java.util.Date; -import java.util.List; -import java.util.Map; - public class RenameIssueWidgets implements Startable { private static final Logger LOGGER = Loggers.get(RenameIssueWidgets.class); @@ -149,7 +148,7 @@ public class RenameIssueWidgets implements Startable { } private String getReplacementWidgetKey(DbSession session, WidgetDto widget) { - DashboardDto dashboard = dbClient.dashboardDao().selectByKey(session, widget.getDashboardId()); + DashboardDto dashboard = dbClient.dashboardDao().selectById(session, widget.getDashboardId()); if (dashboard == null) { LOGGER.warn(String.format("Widget with ID=%d is not displayed on any dashboard, updating nevertheless", widget.getId())); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/SearchActionTest.java index ba954fef7f0..8800583aebf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/SearchActionTest.java @@ -88,7 +88,10 @@ public class SearchActionTest { @Test public void search_with_query() throws Exception { injectUsers(5); - UserDto user = userDb.insertUser(newUserDto("user-%_%-login", "user-name", "user@mail.com")); + UserDto user = userDb.insertUser( + newUserDto("user-%_%-login", "user-name", "user@mail.com") + .setScmAccounts("user1") + ); esTester.putDocuments(UserIndexDefinition.INDEX, UserIndexDefinition.TYPE_USER, new UserDoc() .setActive(true) diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/ws/DeactivateActionTest/deactivate_user.json b/server/sonar-server/src/test/resources/org/sonar/server/user/ws/DeactivateActionTest/deactivate_user.json index b532305cb1b..97cd22324f8 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/user/ws/DeactivateActionTest/deactivate_user.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/user/ws/DeactivateActionTest/deactivate_user.json @@ -2,8 +2,7 @@ "user": { "login": "john", "name": "John", - "email": "john@email.com", - "scmAccounts": ["jn"], + "scmAccounts": [], "active": false } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/ws/SearchActionTest/user_one.json b/server/sonar-server/src/test/resources/org/sonar/server/user/ws/SearchActionTest/user_one.json index a31c4ca8cea..1fc90315712 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/user/ws/SearchActionTest/user_one.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/user/ws/SearchActionTest/user_one.json @@ -8,7 +8,7 @@ "name": "user-name", "email": "user@mail.com", "active": true, - "scmAccounts": [], + "scmAccounts": ["user1"], "tokensCount": 0 } ] diff --git a/sonar-db/src/main/java/org/sonar/db/DaoModule.java b/sonar-db/src/main/java/org/sonar/db/DaoModule.java index cc21f452c37..bedee4a4bb1 100644 --- a/sonar-db/src/main/java/org/sonar/db/DaoModule.java +++ b/sonar-db/src/main/java/org/sonar/db/DaoModule.java @@ -47,6 +47,7 @@ import org.sonar.db.issue.IssueFilterFavouriteDao; import org.sonar.db.loadedtemplate.LoadedTemplateDao; import org.sonar.db.measure.MeasureDao; import org.sonar.db.measure.MeasureFilterDao; +import org.sonar.db.measure.MeasureFilterFavouriteDao; import org.sonar.db.measure.custom.CustomMeasureDao; import org.sonar.db.metric.MetricDao; import org.sonar.db.notification.NotificationQueueDao; @@ -95,6 +96,7 @@ public class DaoModule extends Module { LoadedTemplateDao.class, MeasureDao.class, MeasureFilterDao.class, + MeasureFilterFavouriteDao.class, MetricDao.class, NotificationQueueDao.class, PermissionDao.class, diff --git a/sonar-db/src/main/java/org/sonar/db/DbClient.java b/sonar-db/src/main/java/org/sonar/db/DbClient.java index 42cf7f6fc7a..be249d0e6be 100644 --- a/sonar-db/src/main/java/org/sonar/db/DbClient.java +++ b/sonar-db/src/main/java/org/sonar/db/DbClient.java @@ -47,6 +47,7 @@ import org.sonar.db.issue.IssueFilterFavouriteDao; import org.sonar.db.loadedtemplate.LoadedTemplateDao; import org.sonar.db.measure.MeasureDao; import org.sonar.db.measure.MeasureFilterDao; +import org.sonar.db.measure.MeasureFilterFavouriteDao; import org.sonar.db.measure.custom.CustomMeasureDao; import org.sonar.db.metric.MetricDao; import org.sonar.db.notification.NotificationQueueDao; @@ -83,6 +84,7 @@ public class DbClient { private final ResourceKeyUpdaterDao resourceKeyUpdaterDao; private final MeasureDao measureDao; private final MeasureFilterDao measureFilterDao; + private final MeasureFilterFavouriteDao measureFilterFavouriteDao; private final ActivityDao activityDao; private final AuthorizationDao authorizationDao; private final UserDao userDao; @@ -138,6 +140,7 @@ public class DbClient { resourceKeyUpdaterDao = getDao(map, ResourceKeyUpdaterDao.class); measureDao = getDao(map, MeasureDao.class); measureFilterDao = getDao(map, MeasureFilterDao.class); + measureFilterFavouriteDao = getDao(map, MeasureFilterFavouriteDao.class); activityDao = getDao(map, ActivityDao.class); authorizationDao = getDao(map, AuthorizationDao.class); userDao = getDao(map, UserDao.class); @@ -250,6 +253,10 @@ public class DbClient { return measureFilterDao; } + public MeasureFilterFavouriteDao measureFilterFavouriteDao() { + return measureFilterFavouriteDao; + } + public ActivityDao activityDao() { return activityDao; } diff --git a/sonar-db/src/main/java/org/sonar/db/MyBatis.java b/sonar-db/src/main/java/org/sonar/db/MyBatis.java index 9cf94000504..5cf731bcd2f 100644 --- a/sonar-db/src/main/java/org/sonar/db/MyBatis.java +++ b/sonar-db/src/main/java/org/sonar/db/MyBatis.java @@ -80,6 +80,8 @@ import org.sonar.db.loadedtemplate.LoadedTemplateDto; import org.sonar.db.loadedtemplate.LoadedTemplateMapper; import org.sonar.db.measure.MeasureDto; import org.sonar.db.measure.MeasureFilterDto; +import org.sonar.db.measure.MeasureFilterFavouriteDto; +import org.sonar.db.measure.MeasureFilterFavouriteMapper; import org.sonar.db.measure.MeasureFilterMapper; import org.sonar.db.measure.MeasureMapper; import org.sonar.db.measure.custom.CustomMeasureDto; @@ -175,6 +177,7 @@ public class MyBatis { confBuilder.loadAlias("GroupMembership", GroupMembershipDto.class); confBuilder.loadAlias("LoadedTemplate", LoadedTemplateDto.class); confBuilder.loadAlias("MeasureFilter", MeasureFilterDto.class); + confBuilder.loadAlias("MeasureFilterFavourite", MeasureFilterFavouriteDto.class); confBuilder.loadAlias("NotificationQueue", NotificationQueueDto.class); confBuilder.loadAlias("Property", PropertyDto.class); confBuilder.loadAlias("PurgeableSnapshot", PurgeableSnapshotDto.class); @@ -229,7 +232,7 @@ public class MyBatis { DuplicationMapper.class, IssueMapper.class, IssueChangeMapper.class, IssueFilterMapper.class, IssueFilterFavouriteMapper.class, IsAliveMapper.class, - LoadedTemplateMapper.class, MeasureFilterMapper.class, PermissionTemplateMapper.class, PropertiesMapper.class, PurgeMapper.class, + LoadedTemplateMapper.class, MeasureFilterMapper.class, MeasureFilterFavouriteMapper.class, PermissionTemplateMapper.class, PropertiesMapper.class, PurgeMapper.class, ResourceKeyUpdaterMapper.class, ResourceIndexMapper.class, RoleMapper.class, RuleMapper.class, SchemaMigrationMapper.class, WidgetMapper.class, WidgetPropertyMapper.class, UserMapper.class, GroupMapper.class, UserGroupMapper.class, UserTokenMapper.class, diff --git a/sonar-db/src/main/java/org/sonar/db/dashboard/ActiveDashboardDao.java b/sonar-db/src/main/java/org/sonar/db/dashboard/ActiveDashboardDao.java index 84274d52808..ed224870963 100644 --- a/sonar-db/src/main/java/org/sonar/db/dashboard/ActiveDashboardDao.java +++ b/sonar-db/src/main/java/org/sonar/db/dashboard/ActiveDashboardDao.java @@ -20,9 +20,11 @@ package org.sonar.db.dashboard; import java.util.List; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.ibatis.session.SqlSession; import org.sonar.db.Dao; +import org.sonar.db.DbSession; import org.sonar.db.MyBatis; public class ActiveDashboardDao implements Dao { @@ -33,16 +35,26 @@ public class ActiveDashboardDao implements Dao { this.mybatis = mybatis; } + public void insert(DbSession session, ActiveDashboardDto activeDashboardDto) { + mapper(session).insert(activeDashboardDto); + session.commit(); + } + public void insert(ActiveDashboardDto activeDashboardDto) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { - mapper(session).insert(activeDashboardDto); + insert(session, activeDashboardDto); session.commit(); } finally { MyBatis.closeQuietly(session); } } + @CheckForNull + public ActiveDashboardDto selectById(DbSession session, long id){ + return mapper(session).selectById(id); + } + public int selectMaxOrderIndexForNullUser() { SqlSession session = mybatis.openSession(false); try { diff --git a/sonar-db/src/main/java/org/sonar/db/dashboard/ActiveDashboardMapper.java b/sonar-db/src/main/java/org/sonar/db/dashboard/ActiveDashboardMapper.java index bb7e8eed792..d0f3d834ff5 100644 --- a/sonar-db/src/main/java/org/sonar/db/dashboard/ActiveDashboardMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/dashboard/ActiveDashboardMapper.java @@ -28,6 +28,8 @@ public interface ActiveDashboardMapper { void insert(ActiveDashboardDto activeDashboardDto); + ActiveDashboardDto selectById(long id); + @CheckForNull Integer selectMaxOrderIndexForNullUser(); diff --git a/sonar-db/src/main/java/org/sonar/db/dashboard/DashboardDao.java b/sonar-db/src/main/java/org/sonar/db/dashboard/DashboardDao.java index bd32032f6c1..fd283b3d181 100644 --- a/sonar-db/src/main/java/org/sonar/db/dashboard/DashboardDao.java +++ b/sonar-db/src/main/java/org/sonar/db/dashboard/DashboardDao.java @@ -44,12 +44,10 @@ public class DashboardDao implements Dao { } } - public void insert(DashboardDto dashboardDto) { - SqlSession session = mybatis.openSession(false); + public void insert(DbSession session, DashboardDto dashboardDto) { DashboardMapper dashboardMapper = session.getMapper(DashboardMapper.class); WidgetMapper widgetMapper = session.getMapper(WidgetMapper.class); WidgetPropertyMapper widgetPropertyMapper = session.getMapper(WidgetPropertyMapper.class); - try { dashboardMapper.insert(dashboardDto); for (WidgetDto widgetDto : dashboardDto.getWidgets()) { widgetDto.setDashboardId(dashboardDto.getId()); @@ -59,6 +57,12 @@ public class DashboardDao implements Dao { widgetPropertyMapper.insert(widgetPropertyDto); } } + } + + public void insert(DashboardDto dashboardDto) { + DbSession session = mybatis.openSession(false); + try { + insert(session, dashboardDto); session.commit(); } finally { MyBatis.closeQuietly(session); @@ -66,8 +70,8 @@ public class DashboardDao implements Dao { } @CheckForNull - public DashboardDto selectByKey(DbSession session, Long key) { - return mapper(session).selectById(key); + public DashboardDto selectById(DbSession session, long id) { + return mapper(session).selectById(id); } /** diff --git a/sonar-db/src/main/java/org/sonar/db/issue/IssueFilterDao.java b/sonar-db/src/main/java/org/sonar/db/issue/IssueFilterDao.java index d672c9cbc52..fe8b4a08937 100644 --- a/sonar-db/src/main/java/org/sonar/db/issue/IssueFilterDao.java +++ b/sonar-db/src/main/java/org/sonar/db/issue/IssueFilterDao.java @@ -34,12 +34,16 @@ public class IssueFilterDao implements Dao { this.mybatis = mybatis; } + @CheckForNull + public IssueFilterDto selectById(DbSession session, long id) { + return mapper(session).selectById(id); + } + @CheckForNull public IssueFilterDto selectById(long id) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { - session.getMapper(IssueFilterMapper.class); - return mapper(session).selectById(id); + return selectById(session, id); } finally { MyBatis.closeQuietly(session); } diff --git a/sonar-db/src/main/java/org/sonar/db/issue/IssueFilterFavouriteDao.java b/sonar-db/src/main/java/org/sonar/db/issue/IssueFilterFavouriteDao.java index 4e7e84b2964..dcb92192a14 100644 --- a/sonar-db/src/main/java/org/sonar/db/issue/IssueFilterFavouriteDao.java +++ b/sonar-db/src/main/java/org/sonar/db/issue/IssueFilterFavouriteDao.java @@ -33,10 +33,14 @@ public class IssueFilterFavouriteDao implements Dao { this.mybatis = mybatis; } + public IssueFilterFavouriteDto selectById(DbSession session, long id) { + return mapper(session).selectById(id); + } + public IssueFilterFavouriteDto selectById(long id) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { - return mapper(session).selectById(id); + return selectById(session, id); } finally { MyBatis.closeQuietly(session); } diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterDao.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterDao.java index 2744eb82931..6dbaaf1a45b 100644 --- a/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterDao.java +++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterDao.java @@ -21,6 +21,7 @@ package org.sonar.db.measure; import org.apache.ibatis.session.SqlSession; import org.sonar.db.Dao; +import org.sonar.db.DbSession; import org.sonar.db.MyBatis; public class MeasureFilterDao implements Dao { @@ -40,11 +41,18 @@ public class MeasureFilterDao implements Dao { } } + public MeasureFilterDto selectById(DbSession session, long id){ + return session.getMapper(MeasureFilterMapper.class).selectById(id); + } + + public void insert(DbSession session, MeasureFilterDto filter) { + session.getMapper(MeasureFilterMapper.class).insert(filter); + } + public void insert(MeasureFilterDto filter) { - SqlSession session = mybatis.openSession(false); - MeasureFilterMapper mapper = session.getMapper(MeasureFilterMapper.class); + DbSession session = mybatis.openSession(false); try { - mapper.insert(filter); + insert(session, filter); session.commit(); } finally { MyBatis.closeQuietly(session); diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterFavouriteDao.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterFavouriteDao.java new file mode 100644 index 00000000000..50f15b91878 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterFavouriteDao.java @@ -0,0 +1,35 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.measure; + +import org.sonar.db.Dao; +import org.sonar.db.DbSession; + +public class MeasureFilterFavouriteDao implements Dao { + + public MeasureFilterFavouriteDto selectById(DbSession session, long id) { + return session.getMapper(MeasureFilterFavouriteMapper.class).selectById(id); + } + + public void insert(DbSession session, MeasureFilterFavouriteDto filter) { + session.getMapper(MeasureFilterFavouriteMapper.class).insert(filter); + } + +} diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterFavouriteDto.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterFavouriteDto.java new file mode 100644 index 00000000000..d7f0f8671cd --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterFavouriteDto.java @@ -0,0 +1,65 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.measure; + +import java.util.Date; + +public class MeasureFilterFavouriteDto { + private Long id; + private Long userId; + private Long measureFilterId; + private Date createdAt; + + public Long getId() { + return id; + } + + public MeasureFilterFavouriteDto setId(Long id) { + this.id = id; + return this; + } + + public Long getUserId() { + return userId; + } + + public MeasureFilterFavouriteDto setUserId(Long userId) { + this.userId = userId; + return this; + } + + public Long getMeasureFilterId() { + return measureFilterId; + } + + public MeasureFilterFavouriteDto setMeasureFilterId(Long measureFilterId) { + this.measureFilterId = measureFilterId; + return this; + } + + public Date getCreatedAt() { + return createdAt; + } + + public MeasureFilterFavouriteDto setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + return this; + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterFavouriteMapper.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterFavouriteMapper.java new file mode 100644 index 00000000000..2e00f07d8ea --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterFavouriteMapper.java @@ -0,0 +1,27 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.measure; + +public interface MeasureFilterFavouriteMapper { + + MeasureFilterFavouriteDto selectById(long id); + + void insert(MeasureFilterFavouriteDto filter); +} diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterMapper.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterMapper.java index 062505e3eb5..ec5b5748c6d 100644 --- a/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureFilterMapper.java @@ -25,5 +25,7 @@ package org.sonar.db.measure; public interface MeasureFilterMapper { MeasureFilterDto findSystemFilterByName(String name); + MeasureFilterDto selectById(long id); + void insert(MeasureFilterDto filter); } diff --git a/sonar-db/src/main/java/org/sonar/db/user/UserDto.java b/sonar-db/src/main/java/org/sonar/db/user/UserDto.java index a6983bf1638..3ab6379620c 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/UserDto.java +++ b/sonar-db/src/main/java/org/sonar/db/user/UserDto.java @@ -22,6 +22,7 @@ package org.sonar.db.user; import com.google.common.base.Splitter; import com.google.common.collect.Lists; import java.util.ArrayList; +import java.util.Date; import java.util.List; import javax.annotation.CheckForNull; import javax.annotation.Nullable; @@ -44,6 +45,8 @@ public class UserDto { private String externalIdentityProvider; private String cryptedPassword; private String salt; + private String rememberToken; + private Date rememberTokenExpiresAt; private Long createdAt; private Long updatedAt; @@ -167,6 +170,24 @@ public class UserDto { return this; } + public String getRememberToken() { + return rememberToken; + } + + public UserDto setRememberToken(String rememberToken) { + this.rememberToken = rememberToken; + return this; + } + + public Date getRememberTokenExpiresAt() { + return rememberTokenExpiresAt; + } + + public UserDto setRememberTokenExpiresAt(Date rememberTokenExpiresAt) { + this.rememberTokenExpiresAt = rememberTokenExpiresAt; + return this; + } + public Long getCreatedAt() { return createdAt; } diff --git a/sonar-db/src/main/resources/org/sonar/db/dashboard/ActiveDashboardMapper.xml b/sonar-db/src/main/resources/org/sonar/db/dashboard/ActiveDashboardMapper.xml index 6c88317e890..9261c1bbd18 100644 --- a/sonar-db/src/main/resources/org/sonar/db/dashboard/ActiveDashboardMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/dashboard/ActiveDashboardMapper.xml @@ -3,11 +3,24 @@ + + ad.id, + ad.user_id as "userId", + ad.dashboard_id as "dashboardId", + ad.order_index as "orderIndex" + + INSERT INTO active_dashboards (dashboard_id, user_id, order_index) VALUES (#{dashboardId}, #{userId}, #{orderIndex}) + + + SELECT + FROM measure_filter_favourites mff + WHERE mff.id=#{id} + + + + INSERT INTO measure_filter_favourites (user_id, measure_filter_id, created_at) + VALUES (#{userId,jdbcType=BIGINT}, #{measureFilterId,jdbcType=BIGINT}, #{createdAt,jdbcType=TIMESTAMP}) + + + diff --git a/sonar-db/src/main/resources/org/sonar/db/measure/MeasureFilterMapper.xml b/sonar-db/src/main/resources/org/sonar/db/measure/MeasureFilterMapper.xml index 85e85b3fa4e..63c9be3a27a 100644 --- a/sonar-db/src/main/resources/org/sonar/db/measure/MeasureFilterMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/measure/MeasureFilterMapper.xml @@ -3,15 +3,33 @@ + + mf.id, + mf.name, + mf.user_id as "userId", + mf.shared, + mf.description, + mf.data, + mf.created_at as "createdAt", + mf.updated_at as "updatedAt" + + + + INSERT INTO measure_filters (name, user_id, shared, description, data, created_at, updated_at) - VALUES (#{name}, #{userId}, #{shared}, #{description}, #{data}, #{createdAt}, #{updatedAt}) + VALUES (#{name,jdbcType=VARCHAR}, #{userId,jdbcType=BIGINT}, #{shared,jdbcType=BOOLEAN}, #{description,jdbcType=VARCHAR}, #{data,jdbcType=VARCHAR}, + #{createdAt,jdbcType=TIMESTAMP}, #{updatedAt,jdbcType=TIMESTAMP}) diff --git a/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml b/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml index e60becc00e2..1e3eb253d42 100644 --- a/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml @@ -14,6 +14,8 @@ u.crypted_password as "cryptedPassword", u.external_identity as "externalIdentity", u.external_identity_provider as "externalIdentityProvider", + u.remember_token as "rememberToken", + u.remember_token_expires_at as "rememberTokenExpiresAt", u.created_at as "createdAt", u.updated_at as "updatedAt" @@ -137,21 +139,28 @@ - UPDATE users SET active=${_false}, updated_at=#{now,jdbcType=BIGINT} WHERE id=#{id} + UPDATE users SET active=${_false}, email=null, scm_accounts=null, + external_identity=null, external_identity_provider=null, + salt=null, crypted_password=null, + remember_token=null, remember_token_expires_at=null, + updated_at=#{now,jdbcType=BIGINT} WHERE id=#{id} - INSERT INTO users (login, name, email, active, scm_accounts, external_identity, external_identity_provider, salt, crypted_password, created_at, updated_at) + INSERT INTO users (login, name, email, active, scm_accounts, external_identity, external_identity_provider, salt, crypted_password, + remember_token, remember_token_expires_at, created_at, updated_at) VALUES (#{login,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{active,jdbcType=BOOLEAN}, #{scmAccounts,jdbcType=VARCHAR}, #{externalIdentity,jdbcType=VARCHAR}, #{externalIdentityProvider,jdbcType=VARCHAR}, - #{salt,jdbcType=VARCHAR}, #{cryptedPassword,jdbcType=VARCHAR}, #{createdAt,jdbcType=BIGINT}, - #{updatedAt,jdbcType=BIGINT}) + #{salt,jdbcType=VARCHAR}, #{cryptedPassword,jdbcType=VARCHAR}, + #{rememberToken,jdbcType=VARCHAR}, #{rememberTokenExpiresAt,jdbcType=TIMESTAMP}, + #{createdAt,jdbcType=BIGINT}, #{updatedAt,jdbcType=BIGINT}) UPDATE users set name=#{name,jdbcType=VARCHAR}, email=#{email,jdbcType=VARCHAR}, active=#{active,jdbcType=BOOLEAN}, scm_accounts=#{scmAccounts,jdbcType=VARCHAR}, external_identity=#{externalIdentity,jdbcType=VARCHAR}, external_identity_provider=#{externalIdentityProvider,jdbcType=VARCHAR}, salt=#{salt,jdbcType=VARCHAR}, crypted_password=#{cryptedPassword,jdbcType=BIGINT}, + remember_token=#{rememberToken,jdbcType=VARCHAR}, remember_token_expires_at=#{rememberTokenExpiresAt,jdbcType=TIMESTAMP}, updated_at=#{updatedAt,jdbcType=BIGINT} WHERE login = #{login} diff --git a/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java b/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java index 61215aa8b3a..7e5f79e1d22 100644 --- a/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java +++ b/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java @@ -29,6 +29,6 @@ public class DaoModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new DaoModule().configure(container); - assertThat(container.size()).isEqualTo(48); + assertThat(container.size()).isEqualTo(49); } } diff --git a/sonar-db/src/test/java/org/sonar/db/dashboard/ActiveDashboardDaoTest.java b/sonar-db/src/test/java/org/sonar/db/dashboard/ActiveDashboardDaoTest.java index 654cbf549e0..38d0d20bff9 100644 --- a/sonar-db/src/test/java/org/sonar/db/dashboard/ActiveDashboardDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/dashboard/ActiveDashboardDaoTest.java @@ -23,6 +23,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.utils.System2; +import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.test.DbTests; @@ -34,7 +35,9 @@ public class ActiveDashboardDaoTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - ActiveDashboardDao dao = dbTester.getDbClient().activeDashboardDao(); + DbSession session = dbTester.getSession(); + + ActiveDashboardDao underTest = dbTester.getDbClient().activeDashboardDao(); @Test public void shouldInsert() { @@ -44,7 +47,7 @@ public class ActiveDashboardDaoTest { dashboard.setDashboardId(2L); dashboard.setUserId(3L); dashboard.setOrderIndex(4); - dao.insert(dashboard); + underTest.insert(dashboard); dbTester.assertDbUnit(getClass(), "shouldInsert-result.xml", "active_dashboards"); } @@ -56,7 +59,7 @@ public class ActiveDashboardDaoTest { ActiveDashboardDto dashboard = new ActiveDashboardDto(); dashboard.setDashboardId(2L); dashboard.setOrderIndex(4); - dao.insert(dashboard); + underTest.insert(dashboard); dbTester.assertDbUnit(getClass(), "shouldInsertWithNoUser-result.xml", "active_dashboards"); } @@ -65,7 +68,7 @@ public class ActiveDashboardDaoTest { public void shouldGetMaxOrderIndexForNullUser() { dbTester.prepareDbUnit(getClass(), "shouldGetMaxOrderIndexForNullUser.xml"); - int index = dao.selectMaxOrderIndexForNullUser(); + int index = underTest.selectMaxOrderIndexForNullUser(); assertThat(index).isEqualTo(15); } @@ -74,7 +77,7 @@ public class ActiveDashboardDaoTest { public void shouldGetZeroMaxOrderIndex() { dbTester.prepareDbUnit(getClass(), "empty.xml"); - int index = dao.selectMaxOrderIndexForNullUser(); + int index = underTest.selectMaxOrderIndexForNullUser(); assertThat(index).isZero(); } @@ -83,27 +86,45 @@ public class ActiveDashboardDaoTest { public void should_get_dashboards_for_anonymous() { dbTester.prepareDbUnit(getClass(), "shouldSelectDashboardsForAnonymous.xml"); - assertThat(dao.selectGlobalDashboardsForUserLogin(null)).hasSize(2).extracting("id").containsExactly(2L, 1L); + assertThat(underTest.selectGlobalDashboardsForUserLogin(null)).hasSize(2).extracting("id").containsExactly(2L, 1L); } @Test public void should_get_dashboards_for_user() { dbTester.prepareDbUnit(getClass(), "shouldSelectDashboardsForUser.xml"); - assertThat(dao.selectGlobalDashboardsForUserLogin("obiwan")).hasSize(2).extracting("id").containsExactly(2L, 1L); + assertThat(underTest.selectGlobalDashboardsForUserLogin("obiwan")).hasSize(2).extracting("id").containsExactly(2L, 1L); } @Test public void should_get_project_dashboards_for_anonymous() { dbTester.prepareDbUnit(getClass(), "shouldSelectProjectDashboardsForAnonymous.xml"); - assertThat(dao.selectProjectDashboardsForUserLogin(null)).hasSize(2).extracting("id").containsExactly(2L, 1L); + assertThat(underTest.selectProjectDashboardsForUserLogin(null)).hasSize(2).extracting("id").containsExactly(2L, 1L); } @Test public void should_get_project_dashboards_for_user() { dbTester.prepareDbUnit(getClass(), "shouldSelectProjectDashboardsForUser.xml"); - assertThat(dao.selectProjectDashboardsForUserLogin("obiwan")).hasSize(2).extracting("id").containsExactly(2L, 1L); + assertThat(underTest.selectProjectDashboardsForUserLogin("obiwan")).hasSize(2).extracting("id").containsExactly(2L, 1L); + } + + @Test + public void select_by_id() throws Exception { + ActiveDashboardDto dto = new ActiveDashboardDto() + .setDashboardId(10L) + .setOrderIndex(2) + .setUserId(5L); + underTest.insert(session, dto); + session.commit(); + + ActiveDashboardDto dtoReloaded = underTest.selectById(session, dto.getId()); + assertThat(dtoReloaded).isNotNull(); + assertThat(dtoReloaded.getDashboardId()).isEqualTo(10L); + assertThat(dtoReloaded.getUserId()).isEqualTo(5L); + assertThat(dtoReloaded.getOrderIndex()).isEqualTo(2); + + assertThat(underTest.selectById(session, 123L)).isNull(); } } diff --git a/sonar-db/src/test/java/org/sonar/db/measure/MeasureFilterDaoTest.java b/sonar-db/src/test/java/org/sonar/db/measure/MeasureFilterDaoTest.java index ebdc403bc38..f9bfd12ba0c 100644 --- a/sonar-db/src/test/java/org/sonar/db/measure/MeasureFilterDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/measure/MeasureFilterDaoTest.java @@ -19,9 +19,11 @@ */ package org.sonar.db.measure; +import java.util.Date; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; +import org.sonar.db.DbSession; import org.sonar.db.DbTester; import static org.assertj.core.api.Assertions.assertThat; @@ -31,13 +33,15 @@ public class MeasureFilterDaoTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); - MeasureFilterDao dao = db.getDbClient().measureFilterDao(); + DbSession session = db.getSession(); + + MeasureFilterDao underTest = db.getDbClient().measureFilterDao(); @Test public void should_find_filter() { db.prepareDbUnit(getClass(), "shared.xml"); - MeasureFilterDto filter = dao.selectSystemFilterByName("Projects"); + MeasureFilterDto filter = underTest.selectSystemFilterByName("Projects"); assertThat(filter.getId()).isEqualTo(1L); assertThat(filter.getName()).isEqualTo("Projects"); @@ -47,7 +51,33 @@ public class MeasureFilterDaoTest { public void should_not_find_filter() { db.prepareDbUnit(getClass(), "shared.xml"); - assertThat(dao.selectSystemFilterByName("Unknown")).isNull(); + assertThat(underTest.selectSystemFilterByName("Unknown")).isNull(); + } + + @Test + public void select_by_id() throws Exception { + MeasureFilterDto dto = new MeasureFilterDto() + .setUserId(10L) + .setName("name") + .setDescription("description") + .setData("data") + .setShared(true) + .setCreatedAt(new Date(1000L)) + .setUpdatedAt(new Date(2000L)); + underTest.insert(session, dto); + session.commit(); + + MeasureFilterDto dtoReloded = underTest.selectById(session, dto.getId()); + assertThat(dtoReloded).isNotNull(); + assertThat(dtoReloded.getUserId()).isEqualTo(10L); + assertThat(dtoReloded.getName()).isEqualTo("name"); + assertThat(dtoReloded.getDescription()).isEqualTo("description"); + assertThat(dtoReloded.getData()).isEqualTo("data"); + assertThat(dtoReloded.isShared()).isTrue(); + assertThat(dtoReloded.getCreatedAt()).isEqualTo(new Date(1000L)); + assertThat(dtoReloded.getUpdatedAt()).isEqualTo(new Date(2000L)); + + assertThat(underTest.selectById(session, 123L)).isNull(); } @Test @@ -61,7 +91,7 @@ public class MeasureFilterDaoTest { filterDto.setDescription("Treemap of projects"); filterDto.setData("qualifiers=TRK|display=treemap"); - dao.insert(filterDto); + underTest.insert(filterDto); db.assertDbUnit(getClass(), "shouldInsert-result.xml", new String[]{"created_at", "updated_at"}, "measure_filters"); } diff --git a/sonar-db/src/test/java/org/sonar/db/measure/MeasureFilterFavouriteDaoTest.java b/sonar-db/src/test/java/org/sonar/db/measure/MeasureFilterFavouriteDaoTest.java new file mode 100644 index 00000000000..41402810c3a --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/measure/MeasureFilterFavouriteDaoTest.java @@ -0,0 +1,58 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.measure; + +import java.util.Date; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MeasureFilterFavouriteDaoTest { + + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + + DbSession session = db.getSession(); + + MeasureFilterFavouriteDao underTest = db.getDbClient().measureFilterFavouriteDao(); + + @Test + public void insert_and_select_by_id() throws Exception { + MeasureFilterFavouriteDto dto = new MeasureFilterFavouriteDto() + .setMeasureFilterId(5L) + .setUserId(10L) + .setCreatedAt(new Date(1000L)); + underTest.insert(session, dto); + session.commit(); + + MeasureFilterFavouriteDto dtoReloaded = underTest.selectById(session, dto.getId()); + assertThat(dtoReloaded).isNotNull(); + assertThat(dtoReloaded.getMeasureFilterId()).isEqualTo(5L); + assertThat(dtoReloaded.getUserId()).isEqualTo(10L); + assertThat(dtoReloaded.getCreatedAt()).isEqualTo(new Date(1000L)); + + assertThat(underTest.selectById(session, 123L)).isNull(); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java index a61b3b960e5..68181403823 100644 --- a/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java @@ -21,7 +21,9 @@ package org.sonar.db.user; import java.util.Collection; import java.util.Collections; +import java.util.Date; import java.util.List; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -29,16 +31,28 @@ import org.junit.rules.ExpectedException; import org.sonar.api.user.UserQuery; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.System2; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.RowNotFoundException; +import org.sonar.db.dashboard.ActiveDashboardDto; +import org.sonar.db.dashboard.DashboardDto; +import org.sonar.db.issue.IssueFilterDto; +import org.sonar.db.issue.IssueFilterFavouriteDto; +import org.sonar.db.measure.MeasureFilterDto; +import org.sonar.db.measure.MeasureFilterFavouriteDto; +import org.sonar.db.property.PropertyDto; +import org.sonar.db.property.PropertyQuery; import org.sonar.test.DbTests; import static java.util.Arrays.asList; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.sonar.db.user.GroupMembershipQuery.IN; +import static org.sonar.db.user.GroupMembershipQuery.builder; @Category(DbTests.class) public class UserDaoTest { @@ -51,9 +65,18 @@ public class UserDaoTest { @Rule public DbTester db = DbTester.create(system2); + static final long NOW = 1500000000000L; + + DbClient dbClient = db.getDbClient(); + UserDao underTest = db.getDbClient().userDao(); DbSession session = db.getSession(); + @Before + public void setUp() throws Exception { + when(system2.now()).thenReturn(NOW); + } + @Test public void selectUserByLogin_ignore_inactive() { db.prepareDbUnit(getClass(), "selectActiveUserByLogin.xml"); @@ -166,6 +189,8 @@ public class UserDaoTest { .setCryptedPassword("abcd") .setExternalIdentity("johngithub") .setExternalIdentityProvider("github") + .setRememberToken("1234") + .setRememberTokenExpiresAt(new Date()) .setCreatedAt(date) .setUpdatedAt(date); underTest.insert(db.getSession(), userDto); @@ -183,6 +208,8 @@ public class UserDaoTest { assertThat(user.getCryptedPassword()).isEqualTo("abcd"); assertThat(user.getExternalIdentity()).isEqualTo("johngithub"); assertThat(user.getExternalIdentityProvider()).isEqualTo("github"); + assertThat(user.getRememberToken()).isEqualTo("1234"); + assertThat(user.getRememberTokenExpiresAt()).isNotNull(); assertThat(user.getCreatedAt()).isEqualTo(date); assertThat(user.getUpdatedAt()).isEqualTo(date); } @@ -225,31 +252,100 @@ public class UserDaoTest { } @Test - public void deactivate_user() { - db.prepareDbUnit(getClass(), "deactivate_user.xml"); + public void deactivate_user() throws Exception { + UserDto user = newActiveUser(); + DashboardDto dashboard = newDashboard(user, false); + ActiveDashboardDto activeDashboard = newActiveDashboard(dashboard, user); + IssueFilterDto issueFilter = newIssueFilter(user, false); + IssueFilterFavouriteDto issueFilterFavourite = newIssueFilterFavourite(issueFilter, user); + MeasureFilterDto measureFilter = newMeasureFilter(user, false); + MeasureFilterFavouriteDto measureFilterFavourite = newMeasureFilterFavourite(measureFilter, user); + PropertyDto property = newProperty(user); + newUserRole(user); + newUserGroup(user); + + UserDto otherUser = newActiveUser(); + + session.commit(); + + boolean deactivated = underTest.deactivateUserByLogin(session, user.getLogin()); + assertThat(deactivated).isTrue(); - when(system2.now()).thenReturn(1500000000000L); + UserDto userReloaded = underTest.selectUserById(session, user.getId()); + assertThat(userReloaded.isActive()).isFalse(); + assertThat(userReloaded.getEmail()).isNull(); + assertThat(userReloaded.getScmAccounts()).isNull(); + assertThat(userReloaded.getSalt()).isNull(); + assertThat(userReloaded.getCryptedPassword()).isNull(); + assertThat(userReloaded.getExternalIdentity()).isNull(); + assertThat(userReloaded.getExternalIdentityProvider()).isNull(); + assertThat(userReloaded.getRememberToken()).isNull(); + assertThat(userReloaded.getRememberTokenExpiresAt()).isNull(); + assertThat(userReloaded.getUpdatedAt()).isEqualTo(NOW); + + assertThat(underTest.selectUserById(session, otherUser.getId())).isNotNull(); + + assertThat(dbClient.dashboardDao().selectById(session, dashboard.getId())).isNull(); + assertThat(dbClient.activeDashboardDao().selectById(session, activeDashboard.getId())).isNull(); + assertThat(dbClient.issueFilterDao().selectById(session, issueFilter.getId())).isNull(); + assertThat(dbClient.issueFilterFavouriteDao().selectById(session, issueFilterFavourite.getId())).isNull(); + assertThat(dbClient.measureFilterDao().selectById(session, measureFilter.getId())).isNull(); + assertThat(dbClient.measureFilterFavouriteDao().selectById(session, measureFilterFavourite.getId())).isNull(); + assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setKey(property.getKey()).build(), session)).isEmpty(); + assertThat(dbClient.roleDao().selectUserPermissions(session, user.getLogin(), null)).isEmpty(); + assertThat(dbClient.groupMembershipDao().countGroups(session, builder().login(user.getLogin()).membership(IN).build(), user.getId())).isZero(); + } - String login = "marius"; - boolean deactivated = underTest.deactivateUserByLogin(session, login); + @Test + public void deactivate_user_does_not_remove_shared_dashboard() throws Exception { + UserDto user = newActiveUser(); + DashboardDto notSharedDashboard = newDashboard(user, false); + DashboardDto sharedDashboard = newDashboard(user, true); + session.commit(); + + boolean deactivated = underTest.deactivateUserByLogin(session, user.getLogin()); assertThat(deactivated).isTrue(); - assertThat(underTest.selectActiveUserByLogin(login)).isNull(); + assertThat(dbClient.dashboardDao().selectById(session, notSharedDashboard.getId())).isNull(); + DashboardDto sharedDashboardReloaded = dbClient.dashboardDao().selectById(session, sharedDashboard.getId()); + assertThat(sharedDashboardReloaded).isNotNull(); + assertThat(sharedDashboardReloaded.getUserId()).isEqualTo(user.getId()); + } - UserDto userDto = underTest.selectUserById(100); - assertThat(userDto.isActive()).isFalse(); - assertThat(userDto.getUpdatedAt()).isEqualTo(1500000000000L); + @Test + public void deactivate_user_does_not_remove_shared_issue_filter() throws Exception { + UserDto user = newActiveUser(); + IssueFilterDto notSharedFilter = newIssueFilter(user, false); + IssueFilterDto sharedFilter = newIssueFilter(user, true); + session.commit(); + + boolean deactivated = underTest.deactivateUserByLogin(session, user.getLogin()); + assertThat(deactivated).isTrue(); - db.assertDbUnit(getClass(), "deactivate_user-result.xml", - "dashboards", "active_dashboards", "groups_users", "issue_filters", - "issue_filter_favourites", "measure_filters", "measure_filter_favourites", - "properties", "user_roles"); + assertThat(dbClient.issueFilterDao().selectById(session, notSharedFilter.getId())).isNull(); + IssueFilterDto sharedFilterReloaded = dbClient.issueFilterDao().selectById(session, sharedFilter.getId()); + assertThat(sharedFilterReloaded).isNotNull(); + assertThat(sharedFilterReloaded.getUserLogin()).isEqualTo(user.getLogin()); } @Test - public void deactivate_missing_user() { - db.prepareDbUnit(getClass(), "deactivate_user.xml"); + public void deactivate_user_does_not_remove_shared_measure_filter() throws Exception { + UserDto user = newActiveUser(); + MeasureFilterDto notSharedFilter = newMeasureFilter(user, false); + MeasureFilterDto sharedFilter = newMeasureFilter(user, true); + session.commit(); + + boolean deactivated = underTest.deactivateUserByLogin(session, user.getLogin()); + assertThat(deactivated).isTrue(); + + assertThat(dbClient.measureFilterDao().selectById(session, notSharedFilter.getId())).isNull(); + MeasureFilterDto sharedFilterReloaded = dbClient.measureFilterDao().selectById(session, sharedFilter.getId()); + assertThat(sharedFilterReloaded).isNotNull(); + assertThat(sharedFilterReloaded.getUserId()).isEqualTo(user.getId()); + } + @Test + public void deactivate_missing_user() { String login = "does_not_exist"; boolean deactivated = underTest.deactivateUserByLogin(session, login); assertThat(deactivated).isFalse(); @@ -336,4 +432,66 @@ public class UserDaoTest { assertThat(underTest.doesEmailExist(session, "unknown")).isFalse(); } + private UserDto newActiveUser(){ + UserDto dto = UserTesting.newUserDto().setActive(true); + underTest.insert(session, dto); + return dto; + } + + private DashboardDto newDashboard(UserDto user, boolean shared){ + DashboardDto dto = new DashboardDto().setUserId(user.getId()).setShared(shared); + dbClient.dashboardDao().insert(session, dto); + return dto; + } + + private ActiveDashboardDto newActiveDashboard(DashboardDto dashboard, UserDto user){ + ActiveDashboardDto dto = new ActiveDashboardDto().setDashboardId(dashboard.getId()).setUserId(user.getId()); + dbClient.activeDashboardDao().insert(session, dto); + return dto; + } + + private IssueFilterDto newIssueFilter(UserDto user, boolean shared){ + IssueFilterDto dto = new IssueFilterDto().setUserLogin(user.getLogin()).setName(randomAlphanumeric(100)).setShared(shared); + dbClient.issueFilterDao().insert(session, dto); + return dto; + } + + private IssueFilterFavouriteDto newIssueFilterFavourite(IssueFilterDto filter, UserDto user){ + IssueFilterFavouriteDto dto = new IssueFilterFavouriteDto().setUserLogin(user.getLogin()).setIssueFilterId(filter.getId()); + dbClient.issueFilterFavouriteDao().insert(session, dto); + return dto; + } + + private MeasureFilterDto newMeasureFilter(UserDto user, boolean shared){ + MeasureFilterDto dto = new MeasureFilterDto().setUserId(user.getId()).setName(randomAlphanumeric(100)).setShared(shared); + dbClient.measureFilterDao().insert(session, dto); + return dto; + } + + private MeasureFilterFavouriteDto newMeasureFilterFavourite(MeasureFilterDto measureFilter, UserDto user){ + MeasureFilterFavouriteDto dto = new MeasureFilterFavouriteDto().setUserId(user.getId()).setMeasureFilterId(measureFilter.getId()); + dbClient.measureFilterFavouriteDao().insert(session, dto); + return dto; + } + + private PropertyDto newProperty(UserDto user){ + PropertyDto dto = new PropertyDto().setKey(randomAlphanumeric(100)).setUserId(user.getId()); + dbClient.propertiesDao().insertProperty(session, dto); + return dto; + } + + private UserRoleDto newUserRole(UserDto user){ + UserRoleDto dto = new UserRoleDto().setUserId(user.getId()).setRole(randomAlphanumeric(64)); + dbClient.roleDao().insertUserRole(session, dto); + return dto; + } + + private UserGroupDto newUserGroup(UserDto user){ + GroupDto group = new GroupDto().setName(randomAlphanumeric(30)); + dbClient.groupDao().insert(session, group); + + UserGroupDto dto = new UserGroupDto().setUserId(user.getId()).setGroupId(group.getId()); + dbClient.userGroupDao().insert(session, dto); + return dto; + } } diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserTesting.java b/sonar-db/src/test/java/org/sonar/db/user/UserTesting.java index 034e623e756..5ac74c42025 100644 --- a/sonar-db/src/test/java/org/sonar/db/user/UserTesting.java +++ b/sonar-db/src/test/java/org/sonar/db/user/UserTesting.java @@ -19,7 +19,10 @@ */ package org.sonar.db.user; +import java.util.Date; + import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; +import static org.apache.commons.lang.math.RandomUtils.nextInt; import static org.apache.commons.lang.math.RandomUtils.nextLong; public class UserTesting { @@ -34,8 +37,13 @@ public class UserTesting { .setName(name) .setEmail(email) .setLogin(login) + .setScmAccounts(randomAlphanumeric(40)) .setExternalIdentity(login) .setExternalIdentityProvider("sonarqube") + .setRememberToken(randomAlphanumeric(500)) + .setRememberTokenExpiresAt(new Date((long)nextInt())) + .setSalt(randomAlphanumeric(40)) + .setCryptedPassword(randomAlphanumeric(40)) .setCreatedAt(nextLong()) .setUpdatedAt(nextLong()); } diff --git a/sonar-db/src/test/resources/org/sonar/db/user/UserDaoTest/deactivate_user-result.xml b/sonar-db/src/test/resources/org/sonar/db/user/UserDaoTest/deactivate_user-result.xml deleted file mode 100644 index 60e55e6494f..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/user/UserDaoTest/deactivate_user-result.xml +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sonar-db/src/test/resources/org/sonar/db/user/UserDaoTest/deactivate_user.xml b/sonar-db/src/test/resources/org/sonar/db/user/UserDaoTest/deactivate_user.xml deleted file mode 100644 index 804838dfa2a..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/user/UserDaoTest/deactivate_user.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- 2.39.5