From 49d19b17b0eb44963be1d8003e0e0d90c4ebe9ec Mon Sep 17 00:00:00 2001 From: KomachiSion Date: Wed, 26 Jan 2022 15:21:02 +0800 Subject: [PATCH] Fix unit test. --- .../plugin/auth/impl/JwtTokenManagerTest.java | 27 ++++++++-- .../auth/spi/mock/MockClientAuthService.java | 54 +++++++++++++++++++ ....auth.spi.client.AbstractClientAuthService | 17 ++++++ 3 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 plugin/auth/src/test/java/com/alibaba/nacos/plugin/auth/spi/mock/MockClientAuthService.java create mode 100644 plugin/auth/src/test/resources/META-INF/services/com.alibaba.nacos.plugin.auth.spi.client.AbstractClientAuthService diff --git a/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/JwtTokenManagerTest.java b/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/JwtTokenManagerTest.java index c39c6c67f..437d8f498 100644 --- a/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/JwtTokenManagerTest.java +++ b/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/JwtTokenManagerTest.java @@ -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); diff --git a/plugin/auth/src/test/java/com/alibaba/nacos/plugin/auth/spi/mock/MockClientAuthService.java b/plugin/auth/src/test/java/com/alibaba/nacos/plugin/auth/spi/mock/MockClientAuthService.java new file mode 100644 index 000000000..cc32ed685 --- /dev/null +++ b/plugin/auth/src/test/java/com/alibaba/nacos/plugin/auth/spi/mock/MockClientAuthService.java @@ -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 serverList) { + + } + + @Override + public void setNacosRestTemplate(NacosRestTemplate nacosRestTemplate) { + + } + + @Override + public LoginIdentityContext getLoginIdentityContext(RequestResource resource) { + return null; + } + + @Override + public void shutdown() throws NacosException { + + } +} diff --git a/plugin/auth/src/test/resources/META-INF/services/com.alibaba.nacos.plugin.auth.spi.client.AbstractClientAuthService b/plugin/auth/src/test/resources/META-INF/services/com.alibaba.nacos.plugin.auth.spi.client.AbstractClientAuthService new file mode 100644 index 000000000..2cc0e8a2a --- /dev/null +++ b/plugin/auth/src/test/resources/META-INF/services/com.alibaba.nacos.plugin.auth.spi.client.AbstractClientAuthService @@ -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