From 30765be4d4548e67119aaa86834c03c98da7fb50 Mon Sep 17 00:00:00 2001 From: vran Date: Sun, 27 Mar 2022 19:59:21 +0800 Subject: [PATCH] feat: add ut (#66) --- .../log/service/OperationLogService.java | 4 +- .../domain/login/service/LoginService.java | 4 +- .../log/service/OperationLogServiceTest.java | 35 ++++++++ .../login/service/LoginServiceTest.java | 82 +++++++++++++++++++ .../test/resources/application-ut.properties | 2 +- .../resources/sql/domain/login/Generate.sql | 3 + .../sql/domain/login/RefreshAccessTokens.sql | 7 ++ .../RefreshAccessTokensWhenUserDisabled.sql | 7 ++ .../RefreshAccessTokensWhenUserNotExists.sql | 4 + 9 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 core/src/test/java/com/databasir/core/domain/log/service/OperationLogServiceTest.java create mode 100644 core/src/test/java/com/databasir/core/domain/login/service/LoginServiceTest.java create mode 100644 core/src/test/resources/sql/domain/login/Generate.sql create mode 100644 core/src/test/resources/sql/domain/login/RefreshAccessTokens.sql create mode 100644 core/src/test/resources/sql/domain/login/RefreshAccessTokensWhenUserDisabled.sql create mode 100644 core/src/test/resources/sql/domain/login/RefreshAccessTokensWhenUserNotExists.sql diff --git a/core/src/main/java/com/databasir/core/domain/log/service/OperationLogService.java b/core/src/main/java/com/databasir/core/domain/log/service/OperationLogService.java index f62544e..70538f5 100644 --- a/core/src/main/java/com/databasir/core/domain/log/service/OperationLogService.java +++ b/core/src/main/java/com/databasir/core/domain/log/service/OperationLogService.java @@ -39,9 +39,9 @@ public class OperationLogService { private final OperationLogPojoConverter operationLogPojoConverter; - public void save(OperationLogRequest request) { + public Long save(OperationLogRequest request) { OperationLogPojo pojo = operationLogRequestConverter.toPojo(request); - operationLogDao.insertAndReturnId(pojo); + return operationLogDao.insertAndReturnId(pojo); } public Page list(Pageable page, diff --git a/core/src/main/java/com/databasir/core/domain/login/service/LoginService.java b/core/src/main/java/com/databasir/core/domain/login/service/LoginService.java index 9b0217d..ec9cd9e 100644 --- a/core/src/main/java/com/databasir/core/domain/login/service/LoginService.java +++ b/core/src/main/java/com/databasir/core/domain/login/service/LoginService.java @@ -56,11 +56,11 @@ public class LoginService { UserPojo user = userDao.selectOptionalById(login.getUserId()) .orElseThrow(() -> { log.warn("user not exists but refresh token exists for " + login.getRefreshToken()); - return DomainErrors.INVALID_REFRESH_TOKEN_OPERATION.exception(); + return DomainErrors.INVALID_REFRESH_TOKEN_OPERATION.exception("invalid user"); }); if (!user.getEnabled()) { log.warn("user disabled but refresh token exists for " + login.getRefreshToken()); - throw DomainErrors.INVALID_REFRESH_TOKEN_OPERATION.exception(); + throw DomainErrors.INVALID_REFRESH_TOKEN_OPERATION.exception("invalid user status"); } String accessToken = jwtTokens.accessToken(user.getEmail()); LocalDateTime accessTokenExpireAt = jwtTokens.expireAt(accessToken); diff --git a/core/src/test/java/com/databasir/core/domain/log/service/OperationLogServiceTest.java b/core/src/test/java/com/databasir/core/domain/log/service/OperationLogServiceTest.java new file mode 100644 index 0000000..23a8baf --- /dev/null +++ b/core/src/test/java/com/databasir/core/domain/log/service/OperationLogServiceTest.java @@ -0,0 +1,35 @@ +package com.databasir.core.domain.log.service; + +import com.databasir.common.JsonData; +import com.databasir.core.BaseTest; +import com.databasir.core.domain.log.data.OperationLogRequest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; + +@Transactional +class OperationLogServiceTest extends BaseTest { + + @Autowired + private OperationLogService operationLogService; + + @Test + void save() { + var request = OperationLogRequest.builder() + .operatorUserId(-1000) + .operatorUsername("ut") + .operatorNickname("ut") + .operationModule("system") + .operationCode("updateEmail") + .operationName("更新邮箱") + .operationResponse(JsonData.ok()) + .isSuccess(true) + .involvedProjectId(null) + .involvedGroupId(null) + .involvedUserId(null) + .build(); + Long id = operationLogService.save(request); + Assertions.assertNotNull(id); + } +} \ No newline at end of file diff --git a/core/src/test/java/com/databasir/core/domain/login/service/LoginServiceTest.java b/core/src/test/java/com/databasir/core/domain/login/service/LoginServiceTest.java new file mode 100644 index 0000000..69f314d --- /dev/null +++ b/core/src/test/java/com/databasir/core/domain/login/service/LoginServiceTest.java @@ -0,0 +1,82 @@ +package com.databasir.core.domain.login.service; + +import com.databasir.common.DatabasirException; +import com.databasir.core.BaseTest; +import com.databasir.core.domain.DomainErrors; +import com.databasir.core.domain.login.data.AccessTokenRefreshRequest; +import com.databasir.core.domain.login.data.AccessTokenRefreshResponse; +import com.databasir.core.domain.login.data.LoginKeyResponse; +import com.databasir.dao.impl.LoginDao; +import com.databasir.dao.tables.pojos.LoginPojo; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Optional; +import java.util.UUID; + +@Transactional +class LoginServiceTest extends BaseTest { + + @Autowired + private LoginService loginService; + + @Autowired + private LoginDao loginDao; + + @Test + void refreshAccessTokensWhenRefreshTokenNotExists() { + AccessTokenRefreshRequest request = new AccessTokenRefreshRequest(); + request.setRefreshToken(UUID.randomUUID().toString()); + DatabasirException err = Assertions.assertThrows(DatabasirException.class, + () -> loginService.refreshAccessTokens(request)); + Assertions.assertEquals(DomainErrors.INVALID_REFRESH_TOKEN_OPERATION.getErrCode(), err.getErrCode()); + } + + @Test + @Sql("classpath:sql/domain/login/RefreshAccessTokensWhenUserNotExists.sql") + void refreshAccessTokensWhenUserNotExists() { + AccessTokenRefreshRequest request = new AccessTokenRefreshRequest(); + request.setRefreshToken("2a884c14ef654e14b069f8ca32ce0261"); + DatabasirException err = Assertions.assertThrows(DatabasirException.class, + () -> loginService.refreshAccessTokens(request)); + Assertions.assertEquals(DomainErrors.INVALID_REFRESH_TOKEN_OPERATION.getErrCode(), err.getErrCode()); + Assertions.assertEquals("invalid user", err.getMessage()); + } + + @Test + @Sql("classpath:sql/domain/login/RefreshAccessTokensWhenUserDisabled.sql") + void refreshAccessTokensWhenUserDisabled() { + AccessTokenRefreshRequest request = new AccessTokenRefreshRequest(); + request.setRefreshToken("2a884c14ef654e14b069f8ca32ce0261"); + DatabasirException err = Assertions.assertThrows(DatabasirException.class, + () -> loginService.refreshAccessTokens(request)); + Assertions.assertEquals(DomainErrors.INVALID_REFRESH_TOKEN_OPERATION.getErrCode(), err.getErrCode()); + Assertions.assertEquals("invalid user status", err.getMessage()); + } + + @Test + @Sql("classpath:sql/domain/login/RefreshAccessTokens.sql") + void refreshAccessTokens() { + AccessTokenRefreshRequest request = new AccessTokenRefreshRequest(); + request.setRefreshToken("2a884c14ef654e14b069f8ca32ce0261"); + + AccessTokenRefreshResponse response = loginService.refreshAccessTokens(request); + Assertions.assertNotNull(response); + Assertions.assertNotEquals("xxxx", response.getAccessToken()); + } + + @Test + @Sql("classpath:sql/domain/login/Generate.sql") + void generate() { + int userId = -1000; + LoginKeyResponse token = loginService.generate(userId); + Assertions.assertNotNull(token); + Optional loginOpt = loginDao.selectByUserId(userId); + Assertions.assertTrue(loginOpt.isPresent()); + Assertions.assertEquals(token.getAccessToken(), loginOpt.get().getAccessToken()); + Assertions.assertEquals(token.getRefreshToken(), loginOpt.get().getRefreshToken()); + } +} \ No newline at end of file diff --git a/core/src/test/resources/application-ut.properties b/core/src/test/resources/application-ut.properties index 20aeaa1..e339f38 100644 --- a/core/src/test/resources/application-ut.properties +++ b/core/src/test/resources/application-ut.properties @@ -4,7 +4,7 @@ server.port=8080 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=${databasir.db.username} spring.datasource.password=${databasir.db.password} -spring.datasource.url=jdbc:mysql://${databasir.db.url}/${databasir.db.name:databasir} +spring.datasource.url=jdbc:mysql://${databasir.db.url}/${databasir.db.name:databasir}?serverTimezone=UTC&characterEncoding=utf-8 # driver directory spring.jooq.sql-dialect=mysql # flyway diff --git a/core/src/test/resources/sql/domain/login/Generate.sql b/core/src/test/resources/sql/domain/login/Generate.sql new file mode 100644 index 0000000..0a41450 --- /dev/null +++ b/core/src/test/resources/sql/domain/login/Generate.sql @@ -0,0 +1,3 @@ +INSERT IGNORE INTO databasir.user (id, email, username, password, nickname, avatar, enabled) +VALUES (-1000, 'sysOwner@databasir.com', 'sysOwner', '$2a$10$wXPDzPceCXeU1PLXGKRAEW', 'a', NULL, 1); + diff --git a/core/src/test/resources/sql/domain/login/RefreshAccessTokens.sql b/core/src/test/resources/sql/domain/login/RefreshAccessTokens.sql new file mode 100644 index 0000000..a886f40 --- /dev/null +++ b/core/src/test/resources/sql/domain/login/RefreshAccessTokens.sql @@ -0,0 +1,7 @@ +INSERT IGNORE INTO databasir.user (id, email, username, password, nickname, avatar, enabled) +VALUES (-1000, 'sysOwner@databasir.com', 'sysOwner', '$2a$10$wXPDzPceCXeU1PLXGKRAEW', 'a', NULL, 1); + +INSERT INTO databasir.login (user_id, access_token, refresh_token, access_token_expire_at, refresh_token_expire_at) +VALUES (-1000, + 'xxxx', + '2a884c14ef654e14b069f8ca32ce0261', DATE_SUB(NOW(), INTERVAL 2 MINUTE), DATE_ADD(NOW(), INTERVAL 1 HOUR)); diff --git a/core/src/test/resources/sql/domain/login/RefreshAccessTokensWhenUserDisabled.sql b/core/src/test/resources/sql/domain/login/RefreshAccessTokensWhenUserDisabled.sql new file mode 100644 index 0000000..c8dce6d --- /dev/null +++ b/core/src/test/resources/sql/domain/login/RefreshAccessTokensWhenUserDisabled.sql @@ -0,0 +1,7 @@ +INSERT IGNORE INTO databasir.user (id, email, username, password, nickname, avatar, enabled) +VALUES (-1000, 'sysOwner@databasir.com', 'sysOwner', '$2a$10$wXPDzPceCXeU1PLXGKRAEW', 'a', NULL, 0); + +INSERT INTO databasir.login (user_id, access_token, refresh_token, access_token_expire_at, refresh_token_expire_at) +VALUES (-1000, + 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJEYXRhYmFzaXIiLCJleHAiOjE2NDcwODc4NjgsInVzZXJuYW1lIjoidGVzdEBkYXRhYmFzaXIuY29tIn0.QXtY0R_2_nqpO2LueY0GFXHR1TCaFj1Y9yl1OoP2yYQ', + '2a884c14ef654e14b069f8ca32ce0261', DATE_SUB(NOW(), INTERVAL 2 MINUTE), DATE_ADD(NOW(), INTERVAL 1 HOUR)); diff --git a/core/src/test/resources/sql/domain/login/RefreshAccessTokensWhenUserNotExists.sql b/core/src/test/resources/sql/domain/login/RefreshAccessTokensWhenUserNotExists.sql new file mode 100644 index 0000000..4b2d4d5 --- /dev/null +++ b/core/src/test/resources/sql/domain/login/RefreshAccessTokensWhenUserNotExists.sql @@ -0,0 +1,4 @@ +INSERT INTO databasir.login (user_id, access_token, refresh_token, access_token_expire_at, refresh_token_expire_at) +VALUES (-1000, + 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJEYXRhYmFzaXIiLCJleHAiOjE2NDcwODc4NjgsInVzZXJuYW1lIjoidGVzdEBkYXRhYmFzaXIuY29tIn0.QXtY0R_2_nqpO2LueY0GFXHR1TCaFj1Y9yl1OoP2yYQ', + '2a884c14ef654e14b069f8ca32ce0261', DATE_SUB(NOW(), INTERVAL 2 MINUTE), DATE_ADD(NOW(), INTERVAL 1 HOUR));