提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.oauth2; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.enums.UserTypeEnum; |
|
4 |
import com.iailab.framework.test.core.ut.BaseMockitoUnitTest; |
|
5 |
import com.iailab.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO; |
|
6 |
import com.iailab.module.system.dal.dataobject.oauth2.OAuth2CodeDO; |
|
7 |
import com.iailab.module.system.dal.dataobject.user.AdminUserDO; |
|
8 |
import com.iailab.module.system.service.auth.AdminAuthService; |
|
9 |
import com.google.common.collect.Lists; |
|
10 |
import org.junit.jupiter.api.Test; |
|
11 |
import org.mockito.InjectMocks; |
|
12 |
import org.mockito.Mock; |
|
13 |
|
|
14 |
import java.util.List; |
|
15 |
|
|
16 |
import static cn.hutool.core.util.RandomUtil.randomEle; |
|
17 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
18 |
import static com.iailab.framework.test.core.util.RandomUtils.*; |
|
19 |
import static java.util.Collections.emptyList; |
|
20 |
import static org.junit.jupiter.api.Assertions.*; |
|
21 |
import static org.mockito.ArgumentMatchers.eq; |
|
22 |
import static org.mockito.Mockito.when; |
|
23 |
|
|
24 |
/** |
|
25 |
* {@link OAuth2GrantServiceImpl} 的单元测试 |
|
26 |
* |
|
27 |
* @author iailab |
|
28 |
*/ |
|
29 |
public class OAuth2GrantServiceImplTest extends BaseMockitoUnitTest { |
|
30 |
|
|
31 |
@InjectMocks |
|
32 |
private OAuth2GrantServiceImpl oauth2GrantService; |
|
33 |
|
|
34 |
@Mock |
|
35 |
private OAuth2TokenService oauth2TokenService; |
|
36 |
@Mock |
|
37 |
private OAuth2CodeService oauth2CodeService; |
|
38 |
@Mock |
|
39 |
private AdminAuthService adminAuthService; |
|
40 |
|
|
41 |
@Test |
|
42 |
public void testGrantImplicit() { |
|
43 |
// 准备参数 |
|
44 |
Long userId = randomLongId(); |
|
45 |
Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
|
46 |
String clientId = randomString(); |
|
47 |
List<String> scopes = Lists.newArrayList("read", "write"); |
|
48 |
// mock 方法 |
|
49 |
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class); |
|
50 |
when(oauth2TokenService.createAccessToken(eq(userId), eq(userType), |
|
51 |
eq(clientId), eq(scopes))).thenReturn(accessTokenDO); |
|
52 |
|
|
53 |
// 调用,并断言 |
|
54 |
assertPojoEquals(accessTokenDO, oauth2GrantService.grantImplicit( |
|
55 |
userId, userType, clientId, scopes)); |
|
56 |
} |
|
57 |
|
|
58 |
@Test |
|
59 |
public void testGrantAuthorizationCodeForCode() { |
|
60 |
// 准备参数 |
|
61 |
Long userId = randomLongId(); |
|
62 |
Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
|
63 |
String clientId = randomString(); |
|
64 |
List<String> scopes = Lists.newArrayList("read", "write"); |
|
65 |
String redirectUri = randomString(); |
|
66 |
String state = randomString(); |
|
67 |
// mock 方法 |
|
68 |
OAuth2CodeDO codeDO = randomPojo(OAuth2CodeDO.class); |
|
69 |
when(oauth2CodeService.createAuthorizationCode(eq(userId), eq(userType), |
|
70 |
eq(clientId), eq(scopes), eq(redirectUri), eq(state))).thenReturn(codeDO); |
|
71 |
|
|
72 |
// 调用,并断言 |
|
73 |
assertEquals(codeDO.getCode(), oauth2GrantService.grantAuthorizationCodeForCode(userId, userType, |
|
74 |
clientId, scopes, redirectUri, state)); |
|
75 |
} |
|
76 |
|
|
77 |
@Test |
|
78 |
public void testGrantAuthorizationCodeForAccessToken() { |
|
79 |
// 准备参数 |
|
80 |
String clientId = randomString(); |
|
81 |
String code = randomString(); |
|
82 |
List<String> scopes = Lists.newArrayList("read", "write"); |
|
83 |
String redirectUri = randomString(); |
|
84 |
String state = randomString(); |
|
85 |
// mock 方法(code) |
|
86 |
OAuth2CodeDO codeDO = randomPojo(OAuth2CodeDO.class, o -> { |
|
87 |
o.setClientId(clientId); |
|
88 |
o.setRedirectUri(redirectUri); |
|
89 |
o.setState(state); |
|
90 |
o.setScopes(scopes); |
|
91 |
}); |
|
92 |
when(oauth2CodeService.consumeAuthorizationCode(eq(code))).thenReturn(codeDO); |
|
93 |
// mock 方法(创建令牌) |
|
94 |
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class); |
|
95 |
when(oauth2TokenService.createAccessToken(eq(codeDO.getUserId()), eq(codeDO.getUserType()), |
|
96 |
eq(codeDO.getClientId()), eq(codeDO.getScopes()))).thenReturn(accessTokenDO); |
|
97 |
|
|
98 |
// 调用,并断言 |
|
99 |
assertPojoEquals(accessTokenDO, oauth2GrantService.grantAuthorizationCodeForAccessToken( |
|
100 |
clientId, code, redirectUri, state)); |
|
101 |
} |
|
102 |
|
|
103 |
@Test |
|
104 |
public void testGrantPassword() { |
|
105 |
// 准备参数 |
|
106 |
String username = randomString(); |
|
107 |
String password = randomString(); |
|
108 |
String clientId = randomString(); |
|
109 |
List<String> scopes = Lists.newArrayList("read", "write"); |
|
110 |
// mock 方法(认证) |
|
111 |
AdminUserDO user = randomPojo(AdminUserDO.class); |
|
112 |
when(adminAuthService.authenticate(eq(username), eq(password))).thenReturn(user); |
|
113 |
// mock 方法(访问令牌) |
|
114 |
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class); |
|
115 |
when(oauth2TokenService.createAccessToken(eq(user.getId()), eq(UserTypeEnum.ADMIN.getValue()), |
|
116 |
eq(clientId), eq(scopes))).thenReturn(accessTokenDO); |
|
117 |
|
|
118 |
// 调用,并断言 |
|
119 |
assertPojoEquals(accessTokenDO, oauth2GrantService.grantPassword( |
|
120 |
username, password, clientId, scopes)); |
|
121 |
} |
|
122 |
|
|
123 |
@Test |
|
124 |
public void testGrantRefreshToken() { |
|
125 |
// 准备参数 |
|
126 |
String refreshToken = randomString(); |
|
127 |
String clientId = randomString(); |
|
128 |
// mock 方法 |
|
129 |
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class); |
|
130 |
when(oauth2TokenService.refreshAccessToken(eq(refreshToken), eq(clientId))) |
|
131 |
.thenReturn(accessTokenDO); |
|
132 |
|
|
133 |
// 调用,并断言 |
|
134 |
assertPojoEquals(accessTokenDO, oauth2GrantService.grantRefreshToken( |
|
135 |
refreshToken, clientId)); |
|
136 |
} |
|
137 |
|
|
138 |
@Test |
|
139 |
public void testGrantClientCredentials() { |
|
140 |
assertThrows(UnsupportedOperationException.class, |
|
141 |
() -> oauth2GrantService.grantClientCredentials(randomString(), emptyList()), |
|
142 |
"暂时不支持 client_credentials 授权模式"); |
|
143 |
} |
|
144 |
|
|
145 |
@Test |
|
146 |
public void testRevokeToken_clientIdError() { |
|
147 |
// 准备参数 |
|
148 |
String clientId = randomString(); |
|
149 |
String accessToken = randomString(); |
|
150 |
// mock 方法 |
|
151 |
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class); |
|
152 |
when(oauth2TokenService.getAccessToken(eq(accessToken))).thenReturn(accessTokenDO); |
|
153 |
|
|
154 |
// 调用,并断言 |
|
155 |
assertFalse(oauth2GrantService.revokeToken(clientId, accessToken)); |
|
156 |
} |
|
157 |
|
|
158 |
@Test |
|
159 |
public void testRevokeToken_success() { |
|
160 |
// 准备参数 |
|
161 |
String clientId = randomString(); |
|
162 |
String accessToken = randomString(); |
|
163 |
// mock 方法(访问令牌) |
|
164 |
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class).setClientId(clientId); |
|
165 |
when(oauth2TokenService.getAccessToken(eq(accessToken))).thenReturn(accessTokenDO); |
|
166 |
// mock 方法(移除) |
|
167 |
when(oauth2TokenService.removeAccessToken(eq(accessToken))).thenReturn(accessTokenDO); |
|
168 |
|
|
169 |
// 调用,并断言 |
|
170 |
assertTrue(oauth2GrantService.revokeToken(clientId, accessToken)); |
|
171 |
} |
|
172 |
|
|
173 |
} |