diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-11-23 09:04:16 +0100 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-11-23 17:26:29 +0100 |
commit | 7fb14c3d55ba19b85e7361577a92ee5a0f8ccefc (patch) | |
tree | 5cfe848ea35b532f0804b7db4fcc0b9c1e0be66e /sonar-db | |
parent | fac2525b1ba5448bbfd2efdb672d708d889e993d (diff) | |
download | sonarqube-7fb14c3d55ba19b85e7361577a92ee5a0f8ccefc.tar.gz sonarqube-7fb14c3d55ba19b85e7361577a92ee5a0f8ccefc.zip |
SONAR-7039 Generate user tokens
Diffstat (limited to 'sonar-db')
17 files changed, 439 insertions, 48 deletions
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 38536bf226b..38554e038bd 100644 --- a/sonar-db/src/main/java/org/sonar/db/DaoModule.java +++ b/sonar-db/src/main/java/org/sonar/db/DaoModule.java @@ -68,6 +68,7 @@ import org.sonar.db.user.GroupMembershipDao; import org.sonar.db.user.RoleDao; import org.sonar.db.user.UserDao; import org.sonar.db.user.UserGroupDao; +import org.sonar.db.user.UserTokenDao; public class DaoModule extends Module { private static final List<Class<? extends Dao>> classes = ImmutableList.<Class<? extends Dao>>builder().add( @@ -114,6 +115,7 @@ public class DaoModule extends Module { SnapshotDao.class, UserDao.class, UserGroupDao.class, + UserTokenDao.class, WidgetDao.class, WidgetPropertyDao.class).build(); 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 1759feb17be..f827492969f 100644 --- a/sonar-db/src/main/java/org/sonar/db/DbClient.java +++ b/sonar-db/src/main/java/org/sonar/db/DbClient.java @@ -67,6 +67,7 @@ import org.sonar.db.user.GroupMembershipDao; import org.sonar.db.user.RoleDao; import org.sonar.db.user.UserDao; import org.sonar.db.user.UserGroupDao; +import org.sonar.db.user.UserTokenDao; public class DbClient { @@ -86,6 +87,7 @@ public class DbClient { private final AuthorizationDao authorizationDao; private final UserDao userDao; private final UserGroupDao userGroupDao; + private final UserTokenDao userTokenDao; private final GroupMembershipDao groupMembershipDao; private final RoleDao roleDao; private final PermissionDao permissionDao; @@ -140,6 +142,7 @@ public class DbClient { authorizationDao = getDao(map, AuthorizationDao.class); userDao = getDao(map, UserDao.class); userGroupDao = getDao(map, UserGroupDao.class); + userTokenDao = getDao(map, UserTokenDao.class); groupMembershipDao = getDao(map, GroupMembershipDao.class); roleDao = getDao(map, RoleDao.class); permissionDao = getDao(map, PermissionDao.class); @@ -263,6 +266,10 @@ public class DbClient { return userGroupDao; } + public UserTokenDao userTokenDao() { + return userTokenDao; + } + public GroupMembershipDao groupMembershipDao() { return groupMembershipDao; } 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 5887b49c205..9011a073e62 100644 --- a/sonar-db/src/main/java/org/sonar/db/MyBatis.java +++ b/sonar-db/src/main/java/org/sonar/db/MyBatis.java @@ -126,6 +126,8 @@ import org.sonar.db.user.UserGroupDto; import org.sonar.db.user.UserGroupMapper; import org.sonar.db.user.UserMapper; import org.sonar.db.user.UserRoleDto; +import org.sonar.db.user.UserTokenDto; +import org.sonar.db.user.UserTokenMapper; import org.sonar.db.version.SchemaMigrationDto; import org.sonar.db.version.SchemaMigrationMapper; import org.sonar.db.version.v45.Migration45Mapper; @@ -211,6 +213,7 @@ public class MyBatis { confBuilder.loadAlias("Event", EventDto.class); confBuilder.loadAlias("CustomMeasure", CustomMeasureDto.class); confBuilder.loadAlias("ViewsSnapshot", ViewsSnapshotDto.class); + confBuilder.loadAlias("UserToken", UserTokenDto.class); // AuthorizationMapper has to be loaded before IssueMapper because this last one used it confBuilder.loadMapper("org.sonar.db.user.AuthorizationMapper"); @@ -224,9 +227,9 @@ public class MyBatis { IsAliveMapper.class, LoadedTemplateMapper.class, MeasureFilterMapper.class, PermissionTemplateMapper.class, PropertiesMapper.class, PurgeMapper.class, ResourceKeyUpdaterMapper.class, ResourceIndexMapper.class, RoleMapper.class, RuleMapper.class, - SchemaMigrationMapper.class, UserMapper.class, GroupMapper.class, UserGroupMapper.class, WidgetMapper.class, WidgetPropertyMapper.class, - FileSourceMapper.class, ActionPlanMapper.class, - ActionPlanStatsMapper.class, + SchemaMigrationMapper.class, WidgetMapper.class, WidgetPropertyMapper.class, + UserMapper.class, GroupMapper.class, UserGroupMapper.class, UserTokenMapper.class, + FileSourceMapper.class, ActionPlanMapper.class, ActionPlanStatsMapper.class, NotificationQueueMapper.class, CharacteristicMapper.class, GroupMembershipMapper.class, QualityProfileMapper.class, ActiveRuleMapper.class, MeasureMapper.class, MetricMapper.class, CustomMeasureMapper.class, QualityGateMapper.class, QualityGateConditionMapper.class, ComponentMapper.class, SnapshotMapper.class, diff --git a/sonar-db/src/main/java/org/sonar/db/user/UserDao.java b/sonar-db/src/main/java/org/sonar/db/user/UserDao.java index 451554b2a49..50984dd8ac4 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/UserDao.java +++ b/sonar-db/src/main/java/org/sonar/db/user/UserDao.java @@ -131,31 +131,25 @@ public class UserDao implements Dao { * Deactivate a user and drops all his preferences. * @return false if the user does not exist, true if the existing user has been deactivated */ - public boolean deactivateUserByLogin(String login) { - SqlSession session = mybatis.openSession(false); - try { - UserMapper mapper = session.getMapper(UserMapper.class); - UserDto dto = mapper.selectUserByLogin(login); - if (dto == null) { - return false; - } - - mapper.removeUserFromGroups(dto.getId()); - mapper.deleteUserActiveDashboards(dto.getId()); - mapper.deleteUnsharedUserDashboards(dto.getId()); - mapper.deleteUnsharedUserIssueFilters(dto.getLogin()); - mapper.deleteUserIssueFilterFavourites(dto.getLogin()); - mapper.deleteUnsharedUserMeasureFilters(dto.getId()); - mapper.deleteUserMeasureFilterFavourites(dto.getId()); - mapper.deleteUserProperties(dto.getId()); - mapper.deleteUserRoles(dto.getId()); - mapper.deactivateUser(dto.getId(), system2.now()); - session.commit(); - return true; - - } finally { - MyBatis.closeQuietly(session); + public boolean deactivateUserByLogin(DbSession dbSession, String login) { + UserMapper mapper = dbSession.getMapper(UserMapper.class); + UserDto dto = mapper.selectUserByLogin(login); + if (dto == null) { + return false; } + + mapper.removeUserFromGroups(dto.getId()); + mapper.deleteUserActiveDashboards(dto.getId()); + mapper.deleteUnsharedUserDashboards(dto.getId()); + mapper.deleteUnsharedUserIssueFilters(dto.getLogin()); + mapper.deleteUserIssueFilterFavourites(dto.getLogin()); + mapper.deleteUnsharedUserMeasureFilters(dto.getId()); + mapper.deleteUserMeasureFilterFavourites(dto.getId()); + mapper.deleteUserProperties(dto.getId()); + mapper.deleteUserRoles(dto.getId()); + mapper.deactivateUser(dto.getId(), system2.now()); + dbSession.commit(); + return true; } @CheckForNull diff --git a/sonar-db/src/main/java/org/sonar/db/user/UserTokenDao.java b/sonar-db/src/main/java/org/sonar/db/user/UserTokenDao.java new file mode 100644 index 00000000000..27cfbcea2cd --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/user/UserTokenDao.java @@ -0,0 +1,64 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.user; + +import com.google.common.base.Optional; +import java.util.List; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; +import org.sonar.db.RowNotFoundException; + +import static java.lang.String.format; + +public class UserTokenDao implements Dao { + public void insert(DbSession dbSession, UserTokenDto userTokenDto) { + mapper(dbSession).insert(userTokenDto); + } + + public UserTokenDto selectOrFailByTokenHash(DbSession dbSession, String tokenHash) { + UserTokenDto userToken = mapper(dbSession).selectByTokenHash(tokenHash); + if (userToken == null) { + throw new RowNotFoundException(format("User token with token hash '%s' not found", tokenHash)); + } + + return userToken; + } + + public Optional<UserTokenDto> selectByTokenHash(DbSession dbSession, String tokenHash) { + return Optional.fromNullable(mapper(dbSession).selectByTokenHash(tokenHash)); + } + + public Optional<UserTokenDto> selectByLoginAndName(DbSession dbSession, String login, String name) { + return Optional.fromNullable(mapper(dbSession).selectByLoginAndName(login, name)); + } + + public List<UserTokenDto> selectByLogin(DbSession dbSession, String login) { + return mapper(dbSession).selectByLogin(login); + } + + public void deleteByLogin(DbSession dbSession, String login) { + mapper(dbSession).deleteByLogin(login); + } + + private static UserTokenMapper mapper(DbSession dbSession) { + return dbSession.getMapper(UserTokenMapper.class); + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/user/UserTokenDto.java b/sonar-db/src/main/java/org/sonar/db/user/UserTokenDto.java new file mode 100644 index 00000000000..28d4d2a2ecc --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/user/UserTokenDto.java @@ -0,0 +1,64 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.user; + +public class UserTokenDto { + private String login; + private String name; + private String tokenHash; + private Long createdAt; + + public String getLogin() { + return login; + } + + public UserTokenDto setLogin(String login) { + this.login = login; + return this; + } + + public String getName() { + return name; + } + + public UserTokenDto setName(String name) { + this.name = name; + return this; + } + + public String getTokenHash() { + return tokenHash; + } + + public UserTokenDto setTokenHash(String tokenHash) { + this.tokenHash = tokenHash; + return this; + } + + public Long getCreatedAt() { + return createdAt; + } + + public UserTokenDto setCreatedAt(long createdAt) { + this.createdAt = createdAt; + return this; + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/user/UserTokenMapper.java b/sonar-db/src/main/java/org/sonar/db/user/UserTokenMapper.java new file mode 100644 index 00000000000..42e3d95dd17 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/user/UserTokenMapper.java @@ -0,0 +1,37 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.user; + +import java.util.List; +import org.apache.ibatis.annotations.Param; + +public interface UserTokenMapper { + + void insert(UserTokenDto userToken); + + UserTokenDto selectByTokenHash(String tokenHash); + + UserTokenDto selectByLoginAndName(@Param("login") String login, @Param("name") String name); + + List<UserTokenDto> selectByLogin(String login); + + void deleteByLogin(String login); +} diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java index a1c0b130446..f8d1d84e9f3 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java +++ b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java @@ -29,7 +29,7 @@ import org.sonar.db.MyBatis; public class DatabaseVersion { - public static final int LAST_VERSION = 941; + public static final int LAST_VERSION = 1000; /** * The minimum supported version which can be upgraded. Lower @@ -88,6 +88,7 @@ public class DatabaseVersion { "snapshots", "users", "user_roles", + "user_tokens", "widgets", "widget_properties" ); diff --git a/sonar-db/src/main/resources/org/sonar/db/user/UserTokenMapper.xml b/sonar-db/src/main/resources/org/sonar/db/user/UserTokenMapper.xml new file mode 100644 index 00000000000..b1bcf5d0709 --- /dev/null +++ b/sonar-db/src/main/resources/org/sonar/db/user/UserTokenMapper.xml @@ -0,0 +1,42 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> + +<mapper namespace="org.sonar.db.user.UserTokenMapper"> + + <sql id="userTokensColumns"> + t.login as "login", + t.name as "name", + t.token_hash as "tokenHash", + t.created_at as "createdAt" + </sql> + + <insert id="insert" parameterType="UserToken" keyColumn="id" useGeneratedKeys="true" keyProperty="id"> + INSERT INTO user_tokens (login, name, token_hash, created_at) + VALUES (#{login,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{tokenHash,jdbcType=VARCHAR}, #{createdAt,jdbcType=BIGINT}) + </insert> + + <select id="selectByTokenHash" parameterType="String" resultType="UserToken"> + SELECT + <include refid="userTokensColumns"/> + FROM user_tokens t + WHERE t.token_hash=#{tokenHash} + </select> + + <select id="selectByLoginAndName" parameterType="map" resultType="UserToken"> + SELECT + <include refid="userTokensColumns"/> + FROM user_tokens t + WHERE t.login=#{login} and t.name=#{name} + </select> + + <select id="selectByLogin" parameterType="map" resultType="UserToken"> + SELECT + <include refid="userTokensColumns"/> + FROM user_tokens t + WHERE t.login=#{login} + </select> + + <delete id="deleteByLogin"> + DELETE FROM user_tokens WHERE login=#{login} + </delete> +</mapper> diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql index 0671ac8a706..25a9cb3425c 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql +++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql @@ -360,6 +360,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('938'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('939'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('940'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('941'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1000'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl index f68481ba36e..403d58addd2 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -551,6 +551,14 @@ CREATE TABLE "CE_ACTIVITY" ( "EXECUTION_TIME_MS" BIGINT NULL ); +CREATE TABLE "USER_TOKENS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "LOGIN" VARCHAR(255) NOT NULL, + "NAME" VARCHAR(255) NOT NULL, + "TOKEN_HASH" VARCHAR(255) NOT NULL, + "CREATED_AT" BIGINT NOT NULL +); + -- ---------------------------------------------- -- DDL Statements for indexes -- ---------------------------------------------- @@ -698,3 +706,7 @@ CREATE UNIQUE INDEX "CE_QUEUE_UUID" ON "CE_QUEUE" ("UUID"); CREATE UNIQUE INDEX "CE_ACTIVITY_UUID" ON "CE_ACTIVITY" ("UUID"); CREATE INDEX "CE_ACTIVITY_COMPONENT_UUID" ON "CE_ACTIVITY" ("COMPONENT_UUID"); + +CREATE UNIQUE INDEX "USER_TOKENS_TOKEN_HASH" ON "USER_TOKENS" ("TOKEN_HASH"); + +CREATE UNIQUE INDEX "USER_TOKENS_LOGIN_NAME" ON "USER_TOKENS" ("LOGIN", "NAME"); 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 29e06e13da5..df0f4964ef9 100644 --- a/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java +++ b/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java @@ -30,6 +30,6 @@ public class DaoModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new DaoModule().configure(container); - assertThat(container.size()).isEqualTo(47); + assertThat(container.size()).isEqualTo(48); } } 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 d0fe4e7ee84..2502d75ff39 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 @@ -22,8 +22,6 @@ package org.sonar.db.user; import java.util.Collection; import java.util.Collections; import java.util.List; -import org.junit.After; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -50,19 +48,7 @@ public class UserDaoTest { public DbTester db = DbTester.create(system2); UserDao underTest = db.getDbClient().userDao(); - DbSession session; - - @Before - public void before() { - db.truncateTables(); - - this.session = db.getSession(); - } - - @After - public void after() { - this.session.close(); - } + DbSession session = db.getSession(); @Test public void selectUserByLogin_ignore_inactive() { @@ -233,7 +219,7 @@ public class UserDaoTest { when(system2.now()).thenReturn(1500000000000L); String login = "marius"; - boolean deactivated = underTest.deactivateUserByLogin(login); + boolean deactivated = underTest.deactivateUserByLogin(session, login); assertThat(deactivated).isTrue(); assertThat(underTest.selectActiveUserByLogin(login)).isNull(); @@ -253,7 +239,7 @@ public class UserDaoTest { db.prepareDbUnit(getClass(), "deactivate_user.xml"); String login = "does_not_exist"; - boolean deactivated = underTest.deactivateUserByLogin(login); + boolean deactivated = underTest.deactivateUserByLogin(session, login); assertThat(deactivated).isFalse(); assertThat(underTest.selectActiveUserByLogin(login)).isNull(); } diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserDbTester.java b/sonar-db/src/test/java/org/sonar/db/user/UserDbTester.java new file mode 100644 index 00000000000..ec25f9b0821 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/user/UserDbTester.java @@ -0,0 +1,44 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.user; + +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; + +public class UserDbTester { + private final DbTester db; + private final DbClient dbClient; + private final DbSession dbSession; + + public UserDbTester(DbTester db) { + this.db = db; + this.dbClient = db.getDbClient(); + this.dbSession = db.getSession(); + } + + public UserDto insertUser(UserDto userDto) { + UserDto updatedUser = dbClient.userDao().insert(dbSession, userDto); + db.commit(); + + return updatedUser; + } +} 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 04bb4ab02bb..77799132dad 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 @@ -22,14 +22,13 @@ package org.sonar.db.user; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; -import static org.apache.commons.lang.math.RandomUtils.nextBoolean; import static org.apache.commons.lang.math.RandomUtils.nextLong; public class UserTesting { public static UserDto newUserDto() { UserDto user = new UserDto() - .setActive(nextBoolean()) + .setActive(true) .setName(randomAlphanumeric(30)) .setEmail(randomAlphabetic(30)) .setLogin(randomAlphanumeric(30)); diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserTokenDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/UserTokenDaoTest.java new file mode 100644 index 00000000000..55ea930b442 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/user/UserTokenDaoTest.java @@ -0,0 +1,101 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.user; + +import com.google.common.base.Optional; +import org.assertj.guava.api.Assertions; +import org.junit.Before; +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; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.db.user.UserTokenTesting.newUserToken; + +@Category(DbTests.class) +public class UserTokenDaoTest { + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + + DbSession dbSession; + + UserTokenDao underTest; + + @Before + public void setUp() { + underTest = db.getDbClient().userTokenDao(); + dbSession = db.getSession(); + } + + @Test + public void insert_token() { + UserTokenDto userToken = newUserToken(); + + insertToken(userToken); + + UserTokenDto userTokenFromDb = underTest.selectOrFailByTokenHash(dbSession, userToken.getTokenHash()); + assertThat(userTokenFromDb).isNotNull(); + assertThat(userTokenFromDb.getName()).isEqualTo(userToken.getName()); + assertThat(userTokenFromDb.getCreatedAt()).isEqualTo(userToken.getCreatedAt()); + assertThat(userTokenFromDb.getTokenHash()).isEqualTo(userToken.getTokenHash()); + assertThat(userTokenFromDb.getLogin()).isEqualTo(userToken.getLogin()); + } + + @Test + public void select_by_login_and_name() { + UserTokenDto userToken = newUserToken().setLogin("login").setName("name").setTokenHash("token"); + insertToken(userToken); + + Optional<UserTokenDto> optionalResultByLoginAndName = underTest.selectByLoginAndName(dbSession, userToken.getLogin(), userToken.getName()); + UserTokenDto resultByLoginAndName = optionalResultByLoginAndName.get(); + Optional<UserTokenDto> unfoundResult1 = underTest.selectByLoginAndName(dbSession, "unknown-login", userToken.getName()); + Optional<UserTokenDto> unfoundResult2 = underTest.selectByLoginAndName(dbSession, userToken.getLogin(), "unknown-name"); + + Assertions.assertThat(unfoundResult1).isAbsent(); + Assertions.assertThat(unfoundResult2).isAbsent(); + assertThat(resultByLoginAndName.getLogin()).isEqualTo(userToken.getLogin()); + assertThat(resultByLoginAndName.getName()).isEqualTo(userToken.getName()); + assertThat(resultByLoginAndName.getCreatedAt()).isEqualTo(userToken.getCreatedAt()); + assertThat(resultByLoginAndName.getTokenHash()).isEqualTo(userToken.getTokenHash()); + } + + @Test + public void delete_tokens_by_login() { + insertToken(newUserToken().setLogin("login-to-delete")); + insertToken(newUserToken().setLogin("login-to-delete")); + insertToken(newUserToken().setLogin("login-to-keep")); + + underTest.deleteByLogin(dbSession, "login-to-delete"); + db.commit(); + + assertThat(underTest.selectByLogin(dbSession, "login-to-delete")).isEmpty(); + assertThat(underTest.selectByLogin(dbSession, "login-to-keep")).hasSize(1); + } + + private void insertToken(UserTokenDto userToken) { + underTest.insert(dbSession, userToken); + dbSession.commit(); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserTokenTesting.java b/sonar-db/src/test/java/org/sonar/db/user/UserTokenTesting.java new file mode 100644 index 00000000000..84536ed7265 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/user/UserTokenTesting.java @@ -0,0 +1,34 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.user; + +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; +import static org.apache.commons.lang.math.RandomUtils.nextLong; + +public class UserTokenTesting { + public static UserTokenDto newUserToken() { + return new UserTokenDto() + .setLogin(randomAlphanumeric(255)) + .setName(randomAlphanumeric(255)) + .setTokenHash(randomAlphanumeric(40)) + .setCreatedAt(nextLong()); + } +} |