Commit 4ffe7b9c3bb8149c25cc60ab45baa45e09c4f1ad
1 parent
522d70a2
完善 OAuth2CodeServiceImplTest 单元测试
Showing
5 changed files
with
123 additions
and
4 deletions
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2CodeService.java
| ... | ... | @@ -26,8 +26,8 @@ public interface OAuth2CodeService { |
| 26 | 26 | * @param state 状态 |
| 27 | 27 | * @return 授权码的信息 |
| 28 | 28 | */ |
| 29 | - OAuth2CodeDO createAuthorizationCode(Long userId, Integer userType, String clientId, List<String> scopes, | |
| 30 | - String redirectUri, String state); | |
| 29 | + OAuth2CodeDO createAuthorizationCode(Long userId, Integer userType, String clientId, | |
| 30 | + List<String> scopes, String redirectUri, String state); | |
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | 33 | * 使用授权码 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2CodeServiceImpl.java
| ... | ... | @@ -33,8 +33,8 @@ public class OAuth2CodeServiceImpl implements OAuth2CodeService { |
| 33 | 33 | private OAuth2CodeMapper oauth2CodeMapper; |
| 34 | 34 | |
| 35 | 35 | @Override |
| 36 | - public OAuth2CodeDO createAuthorizationCode(Long userId, Integer userType, String clientId, List<String> scopes, | |
| 37 | - String redirectUri, String state) { | |
| 36 | + public OAuth2CodeDO createAuthorizationCode(Long userId, Integer userType, String clientId, | |
| 37 | + List<String> scopes, String redirectUri, String state) { | |
| 38 | 38 | OAuth2CodeDO codeDO = new OAuth2CodeDO().setCode(generateCode()) |
| 39 | 39 | .setUserId(userId).setUserType(userType) |
| 40 | 40 | .setClientId(clientId).setScopes(scopes) | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2CodeServiceImplTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.system.service.oauth2; | |
| 2 | + | |
| 3 | +import cn.hutool.core.util.RandomUtil; | |
| 4 | +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | |
| 5 | +import cn.iocoder.yudao.framework.common.util.date.DateUtils; | |
| 6 | +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 7 | +import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2CodeDO; | |
| 8 | +import cn.iocoder.yudao.module.system.dal.mysql.oauth2.OAuth2CodeMapper; | |
| 9 | +import org.assertj.core.util.Lists; | |
| 10 | +import org.junit.jupiter.api.Test; | |
| 11 | +import org.springframework.context.annotation.Import; | |
| 12 | + | |
| 13 | +import javax.annotation.Resource; | |
| 14 | +import java.time.Duration; | |
| 15 | +import java.util.List; | |
| 16 | + | |
| 17 | +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.addTime; | |
| 18 | +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; | |
| 19 | +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; | |
| 20 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | |
| 21 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.OAUTH2_CODE_EXPIRE; | |
| 22 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.OAUTH2_CODE_NOT_EXISTS; | |
| 23 | +import static org.junit.jupiter.api.Assertions.*; | |
| 24 | + | |
| 25 | +/** | |
| 26 | + * {@link OAuth2CodeServiceImpl} 的单元测试类 | |
| 27 | + * | |
| 28 | + * @author 芋道源码 | |
| 29 | + */ | |
| 30 | +@Import(OAuth2CodeServiceImpl.class) | |
| 31 | +class OAuth2CodeServiceImplTest extends BaseDbUnitTest { | |
| 32 | + | |
| 33 | + @Resource | |
| 34 | + private OAuth2CodeServiceImpl oauth2CodeService; | |
| 35 | + | |
| 36 | + @Resource | |
| 37 | + private OAuth2CodeMapper oauth2CodeMapper; | |
| 38 | + | |
| 39 | + @Test | |
| 40 | + public void testCreateAuthorizationCode() { | |
| 41 | + // 准备参数 | |
| 42 | + Long userId = randomLongId(); | |
| 43 | + Integer userType = RandomUtil.randomEle(UserTypeEnum.values()).getValue(); | |
| 44 | + String clientId = randomString(); | |
| 45 | + List<String> scopes = Lists.newArrayList("read", "write"); | |
| 46 | + String redirectUri = randomString(); | |
| 47 | + String state = randomString(); | |
| 48 | + | |
| 49 | + // 调用 | |
| 50 | + OAuth2CodeDO codeDO = oauth2CodeService.createAuthorizationCode(userId, userType, clientId, | |
| 51 | + scopes, redirectUri, state); | |
| 52 | + // 断言 | |
| 53 | + OAuth2CodeDO dbCodeDO = oauth2CodeMapper.selectByCode(codeDO.getCode()); | |
| 54 | + assertPojoEquals(codeDO, dbCodeDO, "createTime", "updateTime", "deleted"); | |
| 55 | + assertEquals(userId, codeDO.getUserId()); | |
| 56 | + assertEquals(userType, codeDO.getUserType()); | |
| 57 | + assertEquals(clientId, codeDO.getClientId()); | |
| 58 | + assertEquals(scopes, codeDO.getScopes()); | |
| 59 | + assertEquals(redirectUri, codeDO.getRedirectUri()); | |
| 60 | + assertEquals(state, codeDO.getState()); | |
| 61 | + assertFalse(DateUtils.isExpired(codeDO.getExpiresTime())); | |
| 62 | + } | |
| 63 | + | |
| 64 | + @Test | |
| 65 | + public void testConsumeAuthorizationCode_null() { | |
| 66 | + // 调用,并断言 | |
| 67 | + assertServiceException(() -> oauth2CodeService.consumeAuthorizationCode(randomString()), | |
| 68 | + OAUTH2_CODE_NOT_EXISTS); | |
| 69 | + } | |
| 70 | + | |
| 71 | + @Test | |
| 72 | + public void testConsumeAuthorizationCode_expired() { | |
| 73 | + // 准备参数 | |
| 74 | + String code = "test_code"; | |
| 75 | + // mock 数据 | |
| 76 | + OAuth2CodeDO codeDO = randomPojo(OAuth2CodeDO.class).setCode(code) | |
| 77 | + .setExpiresTime(addTime(Duration.ofDays(-1))); | |
| 78 | + oauth2CodeMapper.insert(codeDO); | |
| 79 | + | |
| 80 | + // 调用,并断言 | |
| 81 | + assertServiceException(() -> oauth2CodeService.consumeAuthorizationCode(code), | |
| 82 | + OAUTH2_CODE_EXPIRE); | |
| 83 | + } | |
| 84 | + | |
| 85 | + @Test | |
| 86 | + public void testConsumeAuthorizationCode_success() { | |
| 87 | + // 准备参数 | |
| 88 | + String code = "test_code"; | |
| 89 | + // mock 数据 | |
| 90 | + OAuth2CodeDO codeDO = randomPojo(OAuth2CodeDO.class).setCode(code) | |
| 91 | + .setExpiresTime(addTime(Duration.ofDays(1))); | |
| 92 | + oauth2CodeMapper.insert(codeDO); | |
| 93 | + | |
| 94 | + // 调用 | |
| 95 | + OAuth2CodeDO result = oauth2CodeService.consumeAuthorizationCode(code); | |
| 96 | + assertPojoEquals(codeDO, result); | |
| 97 | + assertNull(oauth2CodeMapper.selectByCode(code)); | |
| 98 | + } | |
| 99 | + | |
| 100 | +} | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/resources/sql/clean.sql
yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql
| ... | ... | @@ -547,3 +547,21 @@ CREATE TABLE IF NOT EXISTS "system_oauth2_refresh_token" ( |
| 547 | 547 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 548 | 548 | PRIMARY KEY ("id") |
| 549 | 549 | ) COMMENT 'OAuth2 刷新令牌'; |
| 550 | + | |
| 551 | +CREATE TABLE IF NOT EXISTS "system_oauth2_code" ( | |
| 552 | + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, | |
| 553 | + "user_id" bigint NOT NULL, | |
| 554 | + "user_type" tinyint NOT NULL, | |
| 555 | + "code" varchar NOT NULL, | |
| 556 | + "client_id" varchar NOT NULL, | |
| 557 | + "scopes" varchar NOT NULL, | |
| 558 | + "expires_time" datetime NOT NULL, | |
| 559 | + "redirect_uri" varchar NOT NULL, | |
| 560 | + "state" varchar NOT NULL, | |
| 561 | + "creator" varchar DEFAULT '', | |
| 562 | + "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 563 | + "updater" varchar DEFAULT '', | |
| 564 | + "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | |
| 565 | + "deleted" bit NOT NULL DEFAULT FALSE, | |
| 566 | + PRIMARY KEY ("id") | |
| 567 | +) COMMENT 'OAuth2 刷新令牌'; | ... | ... |