完善 OAuth2ClientServiceImpl、OAuth2GrantServiceImpl 单元测试

This commit is contained in:
YunaiV 2023-02-01 21:06:14 +08:00
parent dfffbe3823
commit eadad75590
2 changed files with 48 additions and 24 deletions

View File

@ -27,10 +27,10 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
/** /**
* {@link OAuth2ClientServiceImpl} 的单元测试类 * {@link OAuth2ClientServiceImpl} 的单元测试类
* *
* @author 芋道源码 * @author 芋道源码
*/ */
@Import(OAuth2ClientServiceImpl.class) @Import(OAuth2ClientServiceImpl.class)
public class OAuth2ClientServiceImplTest extends BaseDbUnitTest { public class OAuth2ClientServiceImplTest extends BaseDbUnitTest {
@ -154,28 +154,41 @@ public class OAuth2ClientServiceImplTest extends BaseDbUnitTest {
} }
@Test @Test
public void testGetOAuth2ClientPage() { public void testGetOAuth2Client() {
// mock 数据 // mock 数据
OAuth2ClientDO dbOAuth2Client = randomPojo(OAuth2ClientDO.class, o -> { // 等会查询到 OAuth2ClientDO clientDO = randomPojo(OAuth2ClientDO.class);
o.setName("潜龙"); oauth2ClientMapper.insert(clientDO);
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
oauth2ClientMapper.insert(dbOAuth2Client);
// 测试 name 不匹配
oauth2ClientMapper.insert(cloneIgnoreId(dbOAuth2Client, o -> o.setName("凤凰")));
// 测试 status 不匹配
oauth2ClientMapper.insert(cloneIgnoreId(dbOAuth2Client, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 准备参数 // 准备参数
OAuth2ClientPageReqVO reqVO = new OAuth2ClientPageReqVO(); Long id = clientDO.getId();
reqVO.setName("");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
// 调用 // 调用并断言
PageResult<OAuth2ClientDO> pageResult = oauth2ClientService.getOAuth2ClientPage(reqVO); OAuth2ClientDO dbClientDO = oauth2ClientService.getOAuth2Client(id);
// 断言 assertPojoEquals(clientDO, dbClientDO);
assertEquals(1, pageResult.getTotal()); }
assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbOAuth2Client, pageResult.getList().get(0)); @Test
public void testGetOAuth2ClientPage() {
// mock 数据
OAuth2ClientDO dbOAuth2Client = randomPojo(OAuth2ClientDO.class, o -> { // 等会查询到
o.setName("潜龙");
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
oauth2ClientMapper.insert(dbOAuth2Client);
// 测试 name 不匹配
oauth2ClientMapper.insert(cloneIgnoreId(dbOAuth2Client, o -> o.setName("凤凰")));
// 测试 status 不匹配
oauth2ClientMapper.insert(cloneIgnoreId(dbOAuth2Client, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 准备参数
OAuth2ClientPageReqVO reqVO = new OAuth2ClientPageReqVO();
reqVO.setName("");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
// 调用
PageResult<OAuth2ClientDO> pageResult = oauth2ClientService.getOAuth2ClientPage(reqVO);
// 断言
assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbOAuth2Client, pageResult.getList().get(0));
} }
@Test @Test
@ -203,10 +216,13 @@ public class OAuth2ClientServiceImplTest extends BaseDbUnitTest {
null, null, Collections.singleton(randomString()), null), OAUTH2_CLIENT_SCOPE_OVER); null, null, Collections.singleton(randomString()), null), OAUTH2_CLIENT_SCOPE_OVER);
assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default",
null, null, null, "test"), OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH, "test"); null, null, null, "test"), OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH, "test");
// 成功调用 // 成功调用1参数完整
OAuth2ClientDO result = oauth2ClientService.validOAuthClientFromCache(client.getClientId(), client.getSecret(), OAuth2ClientDO result = oauth2ClientService.validOAuthClientFromCache(client.getClientId(), client.getSecret(),
client.getAuthorizedGrantTypes().get(0), client.getScopes(), client.getRedirectUris().get(0)); client.getAuthorizedGrantTypes().get(0), client.getScopes(), client.getRedirectUris().get(0));
assertPojoEquals(client, result); assertPojoEquals(client, result);
// 成功调用2只有 clientId 参数
result = oauth2ClientService.validOAuthClientFromCache(client.getClientId());
assertPojoEquals(client, result);
} }
} }

View File

@ -16,6 +16,7 @@ import java.util.List;
import static cn.hutool.core.util.RandomUtil.randomEle; import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -134,6 +135,13 @@ public class OAuth2GrantServiceImplTest extends BaseMockitoUnitTest {
refreshToken, clientId)); refreshToken, clientId));
} }
@Test
public void testGrantClientCredentials() {
assertThrows(UnsupportedOperationException.class,
() -> oauth2GrantService.grantClientCredentials(randomString(), emptyList()),
"暂时不支持 client_credentials 授权模式");
}
@Test @Test
public void testRevokeToken_clientIdError() { public void testRevokeToken_clientIdError() {
// 准备参数 // 准备参数