Browse Source

feat: add ut (#66)

master
vran 3 years ago
committed by GitHub
parent
commit
30765be4d4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      core/src/main/java/com/databasir/core/domain/log/service/OperationLogService.java
  2. 4
      core/src/main/java/com/databasir/core/domain/login/service/LoginService.java
  3. 35
      core/src/test/java/com/databasir/core/domain/log/service/OperationLogServiceTest.java
  4. 82
      core/src/test/java/com/databasir/core/domain/login/service/LoginServiceTest.java
  5. 2
      core/src/test/resources/application-ut.properties
  6. 3
      core/src/test/resources/sql/domain/login/Generate.sql
  7. 7
      core/src/test/resources/sql/domain/login/RefreshAccessTokens.sql
  8. 7
      core/src/test/resources/sql/domain/login/RefreshAccessTokensWhenUserDisabled.sql
  9. 4
      core/src/test/resources/sql/domain/login/RefreshAccessTokensWhenUserNotExists.sql

4
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<OperationLogPageResponse> list(Pageable page,

4
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);

35
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);
}
}

82
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<LoginPojo> loginOpt = loginDao.selectByUserId(userId);
Assertions.assertTrue(loginOpt.isPresent());
Assertions.assertEquals(token.getAccessToken(), loginOpt.get().getAccessToken());
Assertions.assertEquals(token.getRefreshToken(), loginOpt.get().getRefreshToken());
}
}

2
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

3
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);

7
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));

7
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));

4
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));
Loading…
Cancel
Save