提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.oauth2; |
H |
2 |
|
|
3 |
import cn.hutool.core.date.LocalDateTimeUtil; |
|
4 |
import cn.hutool.core.util.ObjectUtil; |
|
5 |
import com.iailab.framework.common.enums.UserTypeEnum; |
|
6 |
import com.iailab.framework.common.util.date.DateUtils; |
|
7 |
import com.iailab.framework.test.core.ut.BaseDbUnitTest; |
|
8 |
import com.iailab.module.system.dal.dataobject.oauth2.OAuth2ApproveDO; |
|
9 |
import com.iailab.module.system.dal.dataobject.oauth2.OAuth2ClientDO; |
|
10 |
import com.iailab.module.system.dal.mysql.oauth2.OAuth2ApproveMapper; |
|
11 |
import org.assertj.core.util.Lists; |
|
12 |
import org.junit.jupiter.api.Test; |
|
13 |
import org.springframework.boot.test.mock.mockito.MockBean; |
|
14 |
import org.springframework.context.annotation.Import; |
|
15 |
|
|
16 |
import javax.annotation.Resource; |
|
17 |
import java.time.LocalDateTime; |
|
18 |
import java.time.ZoneId; |
|
19 |
import java.time.temporal.ChronoUnit; |
|
20 |
import java.util.*; |
|
21 |
|
|
22 |
import static cn.hutool.core.util.RandomUtil.*; |
|
23 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
24 |
import static com.iailab.framework.test.core.util.RandomUtils.randomString; |
|
25 |
import static com.iailab.framework.test.core.util.RandomUtils.*; |
|
26 |
import static org.junit.jupiter.api.Assertions.*; |
|
27 |
import static org.mockito.ArgumentMatchers.eq; |
|
28 |
import static org.mockito.Mockito.when; |
|
29 |
|
|
30 |
/** |
|
31 |
* {@link OAuth2ApproveServiceImpl} 的单元测试类 |
|
32 |
* |
|
33 |
* @author iailab |
|
34 |
*/ |
|
35 |
@Import(OAuth2ApproveServiceImpl.class) |
|
36 |
public class OAuth2ApproveServiceImplTest extends BaseDbUnitTest { |
|
37 |
|
|
38 |
@Resource |
|
39 |
private OAuth2ApproveServiceImpl oauth2ApproveService; |
|
40 |
|
|
41 |
@Resource |
|
42 |
private OAuth2ApproveMapper oauth2ApproveMapper; |
|
43 |
|
|
44 |
@MockBean |
|
45 |
private OAuth2ClientService oauth2ClientService; |
|
46 |
|
|
47 |
@Test |
|
48 |
public void checkForPreApproval_clientAutoApprove() { |
|
49 |
// 准备参数 |
|
50 |
Long userId = randomLongId(); |
|
51 |
Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
|
52 |
String clientId = randomString(); |
|
53 |
List<String> requestedScopes = Lists.newArrayList("read"); |
|
54 |
// mock 方法 |
|
55 |
when(oauth2ClientService.validOAuthClientFromCache(eq(clientId))) |
|
56 |
.thenReturn(randomPojo(OAuth2ClientDO.class).setAutoApproveScopes(requestedScopes)); |
|
57 |
|
|
58 |
// 调用 |
|
59 |
boolean success = oauth2ApproveService.checkForPreApproval(userId, userType, |
|
60 |
clientId, requestedScopes); |
|
61 |
// 断言 |
|
62 |
assertTrue(success); |
|
63 |
List<OAuth2ApproveDO> result = oauth2ApproveMapper.selectList(); |
|
64 |
assertEquals(1, result.size()); |
|
65 |
assertEquals(userId, result.get(0).getUserId()); |
|
66 |
assertEquals(userType, result.get(0).getUserType()); |
|
67 |
assertEquals(clientId, result.get(0).getClientId()); |
|
68 |
assertEquals("read", result.get(0).getScope()); |
|
69 |
assertTrue(result.get(0).getApproved()); |
|
70 |
assertFalse(DateUtils.isExpired(result.get(0).getExpiresTime())); |
|
71 |
} |
|
72 |
|
|
73 |
@Test |
|
74 |
public void checkForPreApproval_approve() { |
|
75 |
// 准备参数 |
|
76 |
Long userId = randomLongId(); |
|
77 |
Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
|
78 |
String clientId = randomString(); |
|
79 |
List<String> requestedScopes = Lists.newArrayList("read"); |
|
80 |
// mock 方法 |
|
81 |
when(oauth2ClientService.validOAuthClientFromCache(eq(clientId))) |
|
82 |
.thenReturn(randomPojo(OAuth2ClientDO.class).setAutoApproveScopes(null)); |
|
83 |
// mock 数据 |
|
84 |
OAuth2ApproveDO approve = randomPojo(OAuth2ApproveDO.class).setUserId(userId) |
|
85 |
.setUserType(userType).setClientId(clientId).setScope("read") |
|
86 |
.setExpiresTime(LocalDateTimeUtil.offset(LocalDateTime.now(), 1L, ChronoUnit.DAYS)).setApproved(true); // 同意 |
|
87 |
oauth2ApproveMapper.insert(approve); |
|
88 |
|
|
89 |
// 调用 |
|
90 |
boolean success = oauth2ApproveService.checkForPreApproval(userId, userType, |
|
91 |
clientId, requestedScopes); |
|
92 |
// 断言 |
|
93 |
assertTrue(success); |
|
94 |
} |
|
95 |
|
|
96 |
@Test |
|
97 |
public void checkForPreApproval_reject() { |
|
98 |
// 准备参数 |
|
99 |
Long userId = randomLongId(); |
|
100 |
Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
|
101 |
String clientId = randomString(); |
|
102 |
List<String> requestedScopes = Lists.newArrayList("read"); |
|
103 |
// mock 方法 |
|
104 |
when(oauth2ClientService.validOAuthClientFromCache(eq(clientId))) |
|
105 |
.thenReturn(randomPojo(OAuth2ClientDO.class).setAutoApproveScopes(null)); |
|
106 |
// mock 数据 |
|
107 |
OAuth2ApproveDO approve = randomPojo(OAuth2ApproveDO.class).setUserId(userId) |
|
108 |
.setUserType(userType).setClientId(clientId).setScope("read") |
|
109 |
.setExpiresTime(LocalDateTimeUtil.offset(LocalDateTime.now(), 1L, ChronoUnit.DAYS)).setApproved(false); // 拒绝 |
|
110 |
oauth2ApproveMapper.insert(approve); |
|
111 |
|
|
112 |
// 调用 |
|
113 |
boolean success = oauth2ApproveService.checkForPreApproval(userId, userType, |
|
114 |
clientId, requestedScopes); |
|
115 |
// 断言 |
|
116 |
assertFalse(success); |
|
117 |
} |
|
118 |
|
|
119 |
@Test |
|
120 |
public void testUpdateAfterApproval_none() { |
|
121 |
// 准备参数 |
|
122 |
Long userId = randomLongId(); |
|
123 |
Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
|
124 |
String clientId = randomString(); |
|
125 |
|
|
126 |
// 调用 |
|
127 |
boolean success = oauth2ApproveService.updateAfterApproval(userId, userType, clientId, |
|
128 |
null); |
|
129 |
// 断言 |
|
130 |
assertTrue(success); |
|
131 |
List<OAuth2ApproveDO> result = oauth2ApproveMapper.selectList(); |
|
132 |
assertEquals(0, result.size()); |
|
133 |
} |
|
134 |
|
|
135 |
@Test |
|
136 |
public void testUpdateAfterApproval_approved() { |
|
137 |
// 准备参数 |
|
138 |
Long userId = randomLongId(); |
|
139 |
Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
|
140 |
String clientId = randomString(); |
|
141 |
Map<String, Boolean> requestedScopes = new LinkedHashMap<>(); // 有序,方便判断 |
|
142 |
requestedScopes.put("read", true); |
|
143 |
requestedScopes.put("write", false); |
|
144 |
// mock 方法 |
|
145 |
|
|
146 |
// 调用 |
|
147 |
boolean success = oauth2ApproveService.updateAfterApproval(userId, userType, clientId, |
|
148 |
requestedScopes); |
|
149 |
// 断言 |
|
150 |
assertTrue(success); |
|
151 |
List<OAuth2ApproveDO> result = oauth2ApproveMapper.selectList(); |
|
152 |
assertEquals(2, result.size()); |
|
153 |
// read |
|
154 |
assertEquals(userId, result.get(0).getUserId()); |
|
155 |
assertEquals(userType, result.get(0).getUserType()); |
|
156 |
assertEquals(clientId, result.get(0).getClientId()); |
|
157 |
assertEquals("read", result.get(0).getScope()); |
|
158 |
assertTrue(result.get(0).getApproved()); |
|
159 |
assertFalse(DateUtils.isExpired(result.get(0).getExpiresTime())); |
|
160 |
// write |
|
161 |
assertEquals(userId, result.get(1).getUserId()); |
|
162 |
assertEquals(userType, result.get(1).getUserType()); |
|
163 |
assertEquals(clientId, result.get(1).getClientId()); |
|
164 |
assertEquals("write", result.get(1).getScope()); |
|
165 |
assertFalse(result.get(1).getApproved()); |
|
166 |
assertFalse(DateUtils.isExpired(result.get(1).getExpiresTime())); |
|
167 |
} |
|
168 |
|
|
169 |
@Test |
|
170 |
public void testUpdateAfterApproval_reject() { |
|
171 |
// 准备参数 |
|
172 |
Long userId = randomLongId(); |
|
173 |
Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
|
174 |
String clientId = randomString(); |
|
175 |
Map<String, Boolean> requestedScopes = new LinkedHashMap<>(); |
|
176 |
requestedScopes.put("write", false); |
|
177 |
// mock 方法 |
|
178 |
|
|
179 |
// 调用 |
|
180 |
boolean success = oauth2ApproveService.updateAfterApproval(userId, userType, clientId, |
|
181 |
requestedScopes); |
|
182 |
// 断言 |
|
183 |
assertFalse(success); |
|
184 |
List<OAuth2ApproveDO> result = oauth2ApproveMapper.selectList(); |
|
185 |
assertEquals(1, result.size()); |
|
186 |
// write |
|
187 |
assertEquals(userId, result.get(0).getUserId()); |
|
188 |
assertEquals(userType, result.get(0).getUserType()); |
|
189 |
assertEquals(clientId, result.get(0).getClientId()); |
|
190 |
assertEquals("write", result.get(0).getScope()); |
|
191 |
assertFalse(result.get(0).getApproved()); |
|
192 |
assertFalse(DateUtils.isExpired(result.get(0).getExpiresTime())); |
|
193 |
} |
|
194 |
|
|
195 |
@Test |
|
196 |
public void testGetApproveList() { |
|
197 |
// 准备参数 |
|
198 |
Long userId = 10L; |
|
199 |
Integer userType = UserTypeEnum.ADMIN.getValue(); |
|
200 |
String clientId = randomString(); |
|
201 |
// mock 数据 |
|
202 |
OAuth2ApproveDO approve = randomPojo(OAuth2ApproveDO.class).setUserId(userId) |
|
203 |
.setUserType(userType).setClientId(clientId).setExpiresTime(LocalDateTimeUtil.offset(LocalDateTime.now(), 1L, ChronoUnit.DAYS)); |
|
204 |
oauth2ApproveMapper.insert(approve); // 未过期 |
|
205 |
oauth2ApproveMapper.insert(ObjectUtil.clone(approve).setId(null) |
|
206 |
.setExpiresTime(LocalDateTimeUtil.offset(LocalDateTime.now(), -1L, ChronoUnit.DAYS))); // 已过期 |
|
207 |
|
|
208 |
// 调用 |
|
209 |
List<OAuth2ApproveDO> result = oauth2ApproveService.getApproveList(userId, userType, clientId); |
|
210 |
// 断言 |
|
211 |
assertEquals(1, result.size()); |
|
212 |
assertPojoEquals(approve, result.get(0)); |
|
213 |
} |
|
214 |
|
|
215 |
@Test |
|
216 |
public void testSaveApprove_insert() { |
|
217 |
// 准备参数 |
|
218 |
Long userId = randomLongId(); |
|
219 |
Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
|
220 |
String clientId = randomString(); |
|
221 |
String scope = randomString(); |
|
222 |
Boolean approved = randomBoolean(); |
|
223 |
LocalDateTime expireTime = LocalDateTime.ofInstant(randomDay(1, 30).toInstant(), ZoneId.systemDefault()); |
|
224 |
// mock 方法 |
|
225 |
|
|
226 |
// 调用 |
|
227 |
oauth2ApproveService.saveApprove(userId, userType, clientId, |
|
228 |
scope, approved, expireTime); |
|
229 |
// 断言 |
|
230 |
List<OAuth2ApproveDO> result = oauth2ApproveMapper.selectList(); |
|
231 |
assertEquals(1, result.size()); |
|
232 |
assertEquals(userId, result.get(0).getUserId()); |
|
233 |
assertEquals(userType, result.get(0).getUserType()); |
|
234 |
assertEquals(clientId, result.get(0).getClientId()); |
|
235 |
assertEquals(scope, result.get(0).getScope()); |
|
236 |
assertEquals(approved, result.get(0).getApproved()); |
|
237 |
assertEquals(expireTime, result.get(0).getExpiresTime()); |
|
238 |
} |
|
239 |
|
|
240 |
@Test |
|
241 |
public void testSaveApprove_update() { |
|
242 |
// mock 数据 |
|
243 |
OAuth2ApproveDO approve = randomPojo(OAuth2ApproveDO.class); |
|
244 |
oauth2ApproveMapper.insert(approve); |
|
245 |
// 准备参数 |
|
246 |
Long userId = approve.getUserId(); |
|
247 |
Integer userType = approve.getUserType(); |
|
248 |
String clientId = approve.getClientId(); |
|
249 |
String scope = approve.getScope(); |
|
250 |
Boolean approved = randomBoolean(); |
|
251 |
LocalDateTime expireTime = LocalDateTime.ofInstant(randomDay(1, 30).toInstant(), ZoneId.systemDefault()); |
|
252 |
// mock 方法 |
|
253 |
|
|
254 |
// 调用 |
|
255 |
oauth2ApproveService.saveApprove(userId, userType, clientId, |
|
256 |
scope, approved, expireTime); |
|
257 |
// 断言 |
|
258 |
List<OAuth2ApproveDO> result = oauth2ApproveMapper.selectList(); |
|
259 |
assertEquals(1, result.size()); |
|
260 |
assertEquals(approve.getId(), result.get(0).getId()); |
|
261 |
assertEquals(userId, result.get(0).getUserId()); |
|
262 |
assertEquals(userType, result.get(0).getUserType()); |
|
263 |
assertEquals(clientId, result.get(0).getClientId()); |
|
264 |
assertEquals(scope, result.get(0).getScope()); |
|
265 |
assertEquals(approved, result.get(0).getApproved()); |
|
266 |
assertEquals(expireTime, result.get(0).getExpiresTime()); |
|
267 |
} |
|
268 |
|
|
269 |
} |