houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.service.oauth2;
H 2
3 import cn.hutool.core.util.RandomUtil;
4 import com.iailab.framework.common.enums.UserTypeEnum;
5 import com.iailab.framework.common.util.date.DateUtils;
6 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
7 import com.iailab.module.system.dal.dataobject.oauth2.OAuth2CodeDO;
8 import com.iailab.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.LocalDateTime;
15 import java.util.List;
16
17 import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals;
18 import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException;
19 import static com.iailab.framework.test.core.util.RandomUtils.*;
20 import static com.iailab.module.system.enums.ErrorCodeConstants.OAUTH2_CODE_EXPIRE;
21 import static com.iailab.module.system.enums.ErrorCodeConstants.OAUTH2_CODE_NOT_EXISTS;
22 import static org.junit.jupiter.api.Assertions.*;
23
24 /**
25  * {@link OAuth2CodeServiceImpl} 的单元测试类
26  *
27  * @author iailab
28  */
29 @Import(OAuth2CodeServiceImpl.class)
30 class OAuth2CodeServiceImplTest extends BaseDbUnitTest {
31
32     @Resource
33     private OAuth2CodeServiceImpl oauth2CodeService;
34
35     @Resource
36     private OAuth2CodeMapper oauth2CodeMapper;
37
38     @Test
39     public void testCreateAuthorizationCode() {
40         // 准备参数
41         Long userId = randomLongId();
42         Integer userType = RandomUtil.randomEle(UserTypeEnum.values()).getValue();
43         String clientId = randomString();
44         List<String> scopes = Lists.newArrayList("read", "write");
45         String redirectUri = randomString();
46         String state = randomString();
47
48         // 调用
49         OAuth2CodeDO codeDO = oauth2CodeService.createAuthorizationCode(userId, userType, clientId,
50                 scopes, redirectUri, state);
51         // 断言
52         OAuth2CodeDO dbCodeDO = oauth2CodeMapper.selectByCode(codeDO.getCode());
53         assertPojoEquals(codeDO, dbCodeDO, "createTime", "updateTime", "deleted");
54         assertEquals(userId, codeDO.getUserId());
55         assertEquals(userType, codeDO.getUserType());
56         assertEquals(clientId, codeDO.getClientId());
57         assertEquals(scopes, codeDO.getScopes());
58         assertEquals(redirectUri, codeDO.getRedirectUri());
59         assertEquals(state, codeDO.getState());
60         assertFalse(DateUtils.isExpired(codeDO.getExpiresTime()));
61     }
62
63     @Test
64     public void testConsumeAuthorizationCode_null() {
65         // 调用,并断言
66         assertServiceException(() -> oauth2CodeService.consumeAuthorizationCode(randomString()),
67                 OAUTH2_CODE_NOT_EXISTS);
68     }
69
70     @Test
71     public void testConsumeAuthorizationCode_expired() {
72         // 准备参数
73         String code = "test_code";
74         // mock 数据
75         OAuth2CodeDO codeDO = randomPojo(OAuth2CodeDO.class).setCode(code)
76                 .setExpiresTime(LocalDateTime.now().minusDays(1));
77         oauth2CodeMapper.insert(codeDO);
78
79         // 调用,并断言
80         assertServiceException(() -> oauth2CodeService.consumeAuthorizationCode(code),
81                 OAUTH2_CODE_EXPIRE);
82     }
83
84     @Test
85     public void testConsumeAuthorizationCode_success() {
86         // 准备参数
87         String code = "test_code";
88         // mock 数据
89         OAuth2CodeDO codeDO = randomPojo(OAuth2CodeDO.class).setCode(code)
90                 .setExpiresTime(LocalDateTime.now().plusDays(1));
91         oauth2CodeMapper.insert(codeDO);
92
93         // 调用
94         OAuth2CodeDO result = oauth2CodeService.consumeAuthorizationCode(code);
95         assertPojoEquals(codeDO, result);
96         assertNull(oauth2CodeMapper.selectByCode(code));
97     }
98
99 }