vran
3 years ago
committed by
GitHub
9 changed files with 143 additions and 5 deletions
@ -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); |
|||
} |
|||
} |
@ -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()); |
|||
} |
|||
} |
@ -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); |
|||
|
@ -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)); |
@ -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)); |
@ -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…
Reference in new issue