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