mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-26 09:11:52 +08:00
完善 OAuth2CodeServiceImplTest 单元测试
This commit is contained in:
parent
522d70a29b
commit
4ffe7b9c3b
@ -26,8 +26,8 @@ public interface OAuth2CodeService {
|
|||||||
* @param state 状态
|
* @param state 状态
|
||||||
* @return 授权码的信息
|
* @return 授权码的信息
|
||||||
*/
|
*/
|
||||||
OAuth2CodeDO createAuthorizationCode(Long userId, Integer userType, String clientId, List<String> scopes,
|
OAuth2CodeDO createAuthorizationCode(Long userId, Integer userType, String clientId,
|
||||||
String redirectUri, String state);
|
List<String> scopes, String redirectUri, String state);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用授权码
|
* 使用授权码
|
||||||
|
@ -33,8 +33,8 @@ public class OAuth2CodeServiceImpl implements OAuth2CodeService {
|
|||||||
private OAuth2CodeMapper oauth2CodeMapper;
|
private OAuth2CodeMapper oauth2CodeMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OAuth2CodeDO createAuthorizationCode(Long userId, Integer userType, String clientId, List<String> scopes,
|
public OAuth2CodeDO createAuthorizationCode(Long userId, Integer userType, String clientId,
|
||||||
String redirectUri, String state) {
|
List<String> scopes, String redirectUri, String state) {
|
||||||
OAuth2CodeDO codeDO = new OAuth2CodeDO().setCode(generateCode())
|
OAuth2CodeDO codeDO = new OAuth2CodeDO().setCode(generateCode())
|
||||||
.setUserId(userId).setUserType(userType)
|
.setUserId(userId).setUserType(userType)
|
||||||
.setClientId(clientId).setScopes(scopes)
|
.setClientId(clientId).setScopes(scopes)
|
||||||
|
@ -0,0 +1,100 @@
|
|||||||
|
package cn.iocoder.yudao.module.system.service.oauth2;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.RandomUtil;
|
||||||
|
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||||
|
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
|
||||||
|
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2CodeDO;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.mysql.oauth2.OAuth2CodeMapper;
|
||||||
|
import org.assertj.core.util.Lists;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.addTime;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||||
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.OAUTH2_CODE_EXPIRE;
|
||||||
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.OAUTH2_CODE_NOT_EXISTS;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link OAuth2CodeServiceImpl} 的单元测试类
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Import(OAuth2CodeServiceImpl.class)
|
||||||
|
class OAuth2CodeServiceImplTest extends BaseDbUnitTest {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private OAuth2CodeServiceImpl oauth2CodeService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private OAuth2CodeMapper oauth2CodeMapper;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateAuthorizationCode() {
|
||||||
|
// 准备参数
|
||||||
|
Long userId = randomLongId();
|
||||||
|
Integer userType = RandomUtil.randomEle(UserTypeEnum.values()).getValue();
|
||||||
|
String clientId = randomString();
|
||||||
|
List<String> scopes = Lists.newArrayList("read", "write");
|
||||||
|
String redirectUri = randomString();
|
||||||
|
String state = randomString();
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
OAuth2CodeDO codeDO = oauth2CodeService.createAuthorizationCode(userId, userType, clientId,
|
||||||
|
scopes, redirectUri, state);
|
||||||
|
// 断言
|
||||||
|
OAuth2CodeDO dbCodeDO = oauth2CodeMapper.selectByCode(codeDO.getCode());
|
||||||
|
assertPojoEquals(codeDO, dbCodeDO, "createTime", "updateTime", "deleted");
|
||||||
|
assertEquals(userId, codeDO.getUserId());
|
||||||
|
assertEquals(userType, codeDO.getUserType());
|
||||||
|
assertEquals(clientId, codeDO.getClientId());
|
||||||
|
assertEquals(scopes, codeDO.getScopes());
|
||||||
|
assertEquals(redirectUri, codeDO.getRedirectUri());
|
||||||
|
assertEquals(state, codeDO.getState());
|
||||||
|
assertFalse(DateUtils.isExpired(codeDO.getExpiresTime()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConsumeAuthorizationCode_null() {
|
||||||
|
// 调用,并断言
|
||||||
|
assertServiceException(() -> oauth2CodeService.consumeAuthorizationCode(randomString()),
|
||||||
|
OAUTH2_CODE_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConsumeAuthorizationCode_expired() {
|
||||||
|
// 准备参数
|
||||||
|
String code = "test_code";
|
||||||
|
// mock 数据
|
||||||
|
OAuth2CodeDO codeDO = randomPojo(OAuth2CodeDO.class).setCode(code)
|
||||||
|
.setExpiresTime(addTime(Duration.ofDays(-1)));
|
||||||
|
oauth2CodeMapper.insert(codeDO);
|
||||||
|
|
||||||
|
// 调用,并断言
|
||||||
|
assertServiceException(() -> oauth2CodeService.consumeAuthorizationCode(code),
|
||||||
|
OAUTH2_CODE_EXPIRE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConsumeAuthorizationCode_success() {
|
||||||
|
// 准备参数
|
||||||
|
String code = "test_code";
|
||||||
|
// mock 数据
|
||||||
|
OAuth2CodeDO codeDO = randomPojo(OAuth2CodeDO.class).setCode(code)
|
||||||
|
.setExpiresTime(addTime(Duration.ofDays(1)));
|
||||||
|
oauth2CodeMapper.insert(codeDO);
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
OAuth2CodeDO result = oauth2CodeService.consumeAuthorizationCode(code);
|
||||||
|
assertPojoEquals(codeDO, result);
|
||||||
|
assertNull(oauth2CodeMapper.selectByCode(code));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -24,3 +24,4 @@ DELETE FROM "system_oauth2_client";
|
|||||||
DELETE FROM "system_oauth2_approve";
|
DELETE FROM "system_oauth2_approve";
|
||||||
DELETE FROM "system_oauth2_access_token";
|
DELETE FROM "system_oauth2_access_token";
|
||||||
DELETE FROM "system_oauth2_refresh_token";
|
DELETE FROM "system_oauth2_refresh_token";
|
||||||
|
DELETE FROM "system_oauth2_code";
|
||||||
|
@ -547,3 +547,21 @@ CREATE TABLE IF NOT EXISTS "system_oauth2_refresh_token" (
|
|||||||
"deleted" bit NOT NULL DEFAULT FALSE,
|
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||||
PRIMARY KEY ("id")
|
PRIMARY KEY ("id")
|
||||||
) COMMENT 'OAuth2 刷新令牌';
|
) COMMENT 'OAuth2 刷新令牌';
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS "system_oauth2_code" (
|
||||||
|
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||||
|
"user_id" bigint NOT NULL,
|
||||||
|
"user_type" tinyint NOT NULL,
|
||||||
|
"code" varchar NOT NULL,
|
||||||
|
"client_id" varchar NOT NULL,
|
||||||
|
"scopes" varchar NOT NULL,
|
||||||
|
"expires_time" datetime NOT NULL,
|
||||||
|
"redirect_uri" varchar NOT NULL,
|
||||||
|
"state" varchar NOT NULL,
|
||||||
|
"creator" varchar DEFAULT '',
|
||||||
|
"create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"updater" varchar DEFAULT '',
|
||||||
|
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
|
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||||
|
PRIMARY KEY ("id")
|
||||||
|
) COMMENT 'OAuth2 刷新令牌';
|
||||||
|
Loading…
Reference in New Issue
Block a user