Fix unit test.

This commit is contained in:
KomachiSion 2022-01-26 15:21:02 +08:00
parent e2e819379c
commit 49d19b17b0
3 changed files with 94 additions and 4 deletions

View File

@ -17,16 +17,30 @@
package com.alibaba.nacos.plugin.auth.impl;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.core.code.ControllerMethodsCache;
import com.alibaba.nacos.plugin.auth.impl.constant.AuthConstants;
import io.jsonwebtoken.lang.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.lang.reflect.Field;
import java.util.Properties;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class JwtTokenManagerTest {
@Mock
private AuthConfigs authConfigs;
@Mock
private ControllerMethodsCache methodsCache;
private NacosAuthConfig nacosAuthConfig;
@Test
public void testCreateTokenAndSecretKeyWithoutSpecialSymbol() throws NoSuchFieldException, IllegalAccessException {
createToken("SecretKey0123$567890$234567890123456789012345678901234567890123456789");
@ -39,11 +53,16 @@ public class JwtTokenManagerTest {
}
private void createToken(String secretKey) throws NoSuchFieldException, IllegalAccessException {
AuthConfigs authConfigs = new AuthConfigs();
injectProperty(authConfigs, "secretKey", secretKey);
injectProperty(authConfigs, "tokenValidityInSeconds", 300);
Properties properties = new Properties();
properties.setProperty(AuthConstants.TOKEN_SECRET_KEY, secretKey);
properties.setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS, "300");
when(authConfigs.getAuthPluginProperties(AuthConstants.AUTH_PLUGIN_TYPE)).thenReturn(properties);
nacosAuthConfig = new NacosAuthConfig();
injectProperty(nacosAuthConfig, "methodsCache", methodsCache);
injectProperty(nacosAuthConfig, "authConfigs", authConfigs);
nacosAuthConfig.init();
JwtTokenManager jwtTokenManager = new JwtTokenManager();
injectProperty(jwtTokenManager, "authConfigs", authConfigs);
injectProperty(jwtTokenManager, "nacosAuthConfig", nacosAuthConfig);
String nacosToken = jwtTokenManager.createToken("nacos");
Assert.notNull(nacosToken);
jwtTokenManager.validateToken(nacosToken);

View File

@ -0,0 +1,54 @@
/*
* Copyright 1999-2021 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.plugin.auth.spi.mock;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.plugin.auth.api.LoginIdentityContext;
import com.alibaba.nacos.plugin.auth.api.RequestResource;
import com.alibaba.nacos.plugin.auth.spi.client.AbstractClientAuthService;
import java.util.List;
import java.util.Properties;
public class MockClientAuthService extends AbstractClientAuthService {
@Override
public Boolean login(Properties properties) {
return null;
}
@Override
public void setServerList(List<String> serverList) {
}
@Override
public void setNacosRestTemplate(NacosRestTemplate nacosRestTemplate) {
}
@Override
public LoginIdentityContext getLoginIdentityContext(RequestResource resource) {
return null;
}
@Override
public void shutdown() throws NacosException {
}
}

View File

@ -0,0 +1,17 @@
#
# Copyright 1999-2021 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
com.alibaba.nacos.plugin.auth.spi.mock.MockClientAuthService