|
@@ -1,5 +1,6 @@
|
|
|
-package cn.iocoder.yudao.module.system.service.auth;
|
|
|
+package cn.iocoder.yudao.module.system.service.oauth2;
|
|
|
|
|
|
+import cn.hutool.core.map.MapUtil;
|
|
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
@@ -9,13 +10,12 @@ import cn.iocoder.yudao.module.system.controller.admin.oauth2.vo.client.OAuth2Cl
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2ClientDO;
|
|
|
import cn.iocoder.yudao.module.system.dal.mysql.oauth2.OAuth2ClientMapper;
|
|
|
import cn.iocoder.yudao.module.system.mq.producer.auth.OAuth2ClientProducer;
|
|
|
-import cn.iocoder.yudao.module.system.service.oauth2.OAuth2ClientServiceImpl;
|
|
|
-import org.junit.jupiter.api.Disabled;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
|
@@ -23,7 +23,7 @@ import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.max;
|
|
|
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_CLIENT_NOT_EXISTS;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
|
@@ -132,7 +132,31 @@ public class OAuth2ClientServiceImplTest extends BaseDbUnitTest {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- @Disabled
|
|
|
+ public void testValidateClientIdExists_withId() {
|
|
|
+ // mock 数据
|
|
|
+ OAuth2ClientDO client = randomPojo(OAuth2ClientDO.class).setClientId("tudou");
|
|
|
+ oauth2ClientMapper.insert(client);
|
|
|
+ // 准备参数
|
|
|
+ Long id = randomLongId();
|
|
|
+ String clientId = "tudou";
|
|
|
+
|
|
|
+ // 调用,不会报错
|
|
|
+ assertServiceException(() -> oauth2ClientService.validateClientIdExists(id, clientId), OAUTH2_CLIENT_EXISTS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testValidateClientIdExists_noId() {
|
|
|
+ // mock 数据
|
|
|
+ OAuth2ClientDO client = randomPojo(OAuth2ClientDO.class).setClientId("tudou");
|
|
|
+ oauth2ClientMapper.insert(client);
|
|
|
+ // 准备参数
|
|
|
+ String clientId = "tudou";
|
|
|
+
|
|
|
+ // 调用,不会报错
|
|
|
+ assertServiceException(() -> oauth2ClientService.validateClientIdExists(null, clientId), OAUTH2_CLIENT_EXISTS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
public void testGetOAuth2ClientPage() {
|
|
|
// mock 数据
|
|
|
OAuth2ClientDO dbOAuth2Client = randomPojo(OAuth2ClientDO.class, o -> { // 等会查询到
|
|
@@ -143,10 +167,10 @@ public class OAuth2ClientServiceImplTest extends BaseDbUnitTest {
|
|
|
// 测试 name 不匹配
|
|
|
oauth2ClientMapper.insert(cloneIgnoreId(dbOAuth2Client, o -> o.setName("凤凰")));
|
|
|
// 测试 status 不匹配
|
|
|
- oauth2ClientMapper.insert(cloneIgnoreId(dbOAuth2Client, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())));
|
|
|
+ oauth2ClientMapper.insert(cloneIgnoreId(dbOAuth2Client, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
|
|
// 准备参数
|
|
|
OAuth2ClientPageReqVO reqVO = new OAuth2ClientPageReqVO();
|
|
|
- reqVO.setName("long");
|
|
|
+ reqVO.setName("龙");
|
|
|
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
|
|
|
|
|
// 调用
|
|
@@ -157,4 +181,35 @@ public class OAuth2ClientServiceImplTest extends BaseDbUnitTest {
|
|
|
assertPojoEquals(dbOAuth2Client, pageResult.getList().get(0));
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void testValidOAuthClientFromCache() {
|
|
|
+ // mock 方法
|
|
|
+ OAuth2ClientDO client = randomPojo(OAuth2ClientDO.class).setClientId("default")
|
|
|
+ .setStatus(CommonStatusEnum.ENABLE.getStatus());
|
|
|
+ OAuth2ClientDO client02 = randomPojo(OAuth2ClientDO.class).setClientId("disable")
|
|
|
+ .setStatus(CommonStatusEnum.DISABLE.getStatus());
|
|
|
+ Map<String, OAuth2ClientDO> clientCache = MapUtil.<String, OAuth2ClientDO>builder()
|
|
|
+ .put(client.getClientId(), client)
|
|
|
+ .put(client02.getClientId(), client02).build();
|
|
|
+ oauth2ClientService.setClientCache(clientCache);
|
|
|
+
|
|
|
+ // 调用,并断言
|
|
|
+ assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache(randomString(),
|
|
|
+ null, null, null, null), OAUTH2_CLIENT_NOT_EXISTS);
|
|
|
+ assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("disable",
|
|
|
+ null, null, null, null), OAUTH2_CLIENT_DISABLE);
|
|
|
+ assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default",
|
|
|
+ randomString(), null, null, null), OAUTH2_CLIENT_CLIENT_SECRET_ERROR);
|
|
|
+ assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default",
|
|
|
+ null, randomString(), null, null), OAUTH2_CLIENT_AUTHORIZED_GRANT_TYPE_NOT_EXISTS);
|
|
|
+ assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default",
|
|
|
+ null, null, Collections.singleton(randomString()), null), OAUTH2_CLIENT_SCOPE_OVER);
|
|
|
+ assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default",
|
|
|
+ null, null, null, "test"), OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH, "test");
|
|
|
+ // 成功调用
|
|
|
+ OAuth2ClientDO result = oauth2ClientService.validOAuthClientFromCache(client.getClientId(), client.getSecret(),
|
|
|
+ client.getAuthorizedGrantTypes().get(0), client.getScopes(), client.getRedirectUris().get(0));
|
|
|
+ assertPojoEquals(client, result);
|
|
|
+ }
|
|
|
+
|
|
|
}
|