upgrade module naocs-auth from junit4 to junit5 (#12105)

This commit is contained in:
shalk(xiao kun) 2024-05-16 09:41:05 +08:00 committed by GitHub
parent 67b672251e
commit dc0e46e9f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 205 additions and 187 deletions

View File

@ -19,30 +19,30 @@ package com.alibaba.nacos.auth;
import com.alibaba.nacos.api.config.remote.request.ConfigPublishRequest; import com.alibaba.nacos.api.config.remote.request.ConfigPublishRequest;
import com.alibaba.nacos.api.naming.remote.request.AbstractNamingRequest; import com.alibaba.nacos.api.naming.remote.request.AbstractNamingRequest;
import com.alibaba.nacos.auth.annotation.Secured; import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.auth.mock.MockAuthPluginService;
import com.alibaba.nacos.plugin.auth.api.IdentityContext; import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.plugin.auth.api.Permission; import com.alibaba.nacos.plugin.auth.api.Permission;
import com.alibaba.nacos.plugin.auth.api.Resource; import com.alibaba.nacos.plugin.auth.api.Resource;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.plugin.auth.constant.SignType; import com.alibaba.nacos.plugin.auth.constant.SignType;
import com.alibaba.nacos.plugin.auth.exception.AccessException; import com.alibaba.nacos.plugin.auth.exception.AccessException;
import com.alibaba.nacos.auth.mock.MockAuthPluginService; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class GrpcProtocolAuthServiceTest { class GrpcProtocolAuthServiceTest {
@Mock @Mock
private AuthConfigs authConfigs; private AuthConfigs authConfigs;
@ -53,8 +53,8 @@ public class GrpcProtocolAuthServiceTest {
private GrpcProtocolAuthService protocolAuthService; private GrpcProtocolAuthService protocolAuthService;
@Before @BeforeEach
public void setUp() throws Exception { void setUp() throws Exception {
protocolAuthService = new GrpcProtocolAuthService(authConfigs); protocolAuthService = new GrpcProtocolAuthService(authConfigs);
protocolAuthService.initialize(); protocolAuthService.initialize();
mockConfigRequest(); mockConfigRequest();
@ -78,7 +78,7 @@ public class GrpcProtocolAuthServiceTest {
@Test @Test
@Secured(resource = "testResource") @Secured(resource = "testResource")
public void testParseResourceWithSpecifiedResource() throws NoSuchMethodException { void testParseResourceWithSpecifiedResource() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithSpecifiedResource"); Secured secured = getMethodSecure("testParseResourceWithSpecifiedResource");
Resource actual = protocolAuthService.parseResource(namingRequest, secured); Resource actual = protocolAuthService.parseResource(namingRequest, secured);
assertEquals("testResource", actual.getName()); assertEquals("testResource", actual.getName());
@ -90,7 +90,7 @@ public class GrpcProtocolAuthServiceTest {
@Test @Test
@Secured(signType = "non-exist") @Secured(signType = "non-exist")
public void testParseResourceWithNonExistType() throws NoSuchMethodException { void testParseResourceWithNonExistType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithNonExistType"); Secured secured = getMethodSecure("testParseResourceWithNonExistType");
Resource actual = protocolAuthService.parseResource(namingRequest, secured); Resource actual = protocolAuthService.parseResource(namingRequest, secured);
assertEquals(Resource.EMPTY_RESOURCE, actual); assertEquals(Resource.EMPTY_RESOURCE, actual);
@ -98,7 +98,7 @@ public class GrpcProtocolAuthServiceTest {
@Test @Test
@Secured() @Secured()
public void testParseResourceWithNamingType() throws NoSuchMethodException { void testParseResourceWithNamingType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithNamingType"); Secured secured = getMethodSecure("testParseResourceWithNamingType");
Resource actual = protocolAuthService.parseResource(namingRequest, secured); Resource actual = protocolAuthService.parseResource(namingRequest, secured);
assertEquals(SignType.NAMING, actual.getType()); assertEquals(SignType.NAMING, actual.getType());
@ -110,7 +110,7 @@ public class GrpcProtocolAuthServiceTest {
@Test @Test
@Secured(signType = SignType.CONFIG) @Secured(signType = SignType.CONFIG)
public void testParseResourceWithConfigType() throws NoSuchMethodException { void testParseResourceWithConfigType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithConfigType"); Secured secured = getMethodSecure("testParseResourceWithConfigType");
Resource actual = protocolAuthService.parseResource(configRequest, secured); Resource actual = protocolAuthService.parseResource(configRequest, secured);
assertEquals(SignType.CONFIG, actual.getType()); assertEquals(SignType.CONFIG, actual.getType());
@ -121,39 +121,39 @@ public class GrpcProtocolAuthServiceTest {
} }
@Test @Test
public void testParseIdentity() { void testParseIdentity() {
IdentityContext actual = protocolAuthService.parseIdentity(namingRequest); IdentityContext actual = protocolAuthService.parseIdentity(namingRequest);
assertNotNull(actual); assertNotNull(actual);
} }
@Test @Test
public void testValidateIdentityWithoutPlugin() throws AccessException { void testValidateIdentityWithoutPlugin() throws AccessException {
IdentityContext identityContext = new IdentityContext(); IdentityContext identityContext = new IdentityContext();
assertTrue(protocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE)); assertTrue(protocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE));
} }
@Test @Test
public void testValidateIdentityWithPlugin() throws AccessException { void testValidateIdentityWithPlugin() throws AccessException {
Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN); Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN);
IdentityContext identityContext = new IdentityContext(); IdentityContext identityContext = new IdentityContext();
assertFalse(protocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE)); assertFalse(protocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE));
} }
@Test @Test
public void testValidateAuthorityWithoutPlugin() throws AccessException { void testValidateAuthorityWithoutPlugin() throws AccessException {
assertTrue(protocolAuthService assertTrue(protocolAuthService.validateAuthority(new IdentityContext(),
.validateAuthority(new IdentityContext(), new Permission(Resource.EMPTY_RESOURCE, ""))); new Permission(Resource.EMPTY_RESOURCE, "")));
} }
@Test @Test
public void testValidateAuthorityWithPlugin() throws AccessException { void testValidateAuthorityWithPlugin() throws AccessException {
Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN); Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN);
assertFalse(protocolAuthService assertFalse(protocolAuthService.validateAuthority(new IdentityContext(),
.validateAuthority(new IdentityContext(), new Permission(Resource.EMPTY_RESOURCE, ""))); new Permission(Resource.EMPTY_RESOURCE, "")));
} }
private Secured getMethodSecure(String methodName) throws NoSuchMethodException { private Secured getMethodSecure(String methodName) throws NoSuchMethodException {
Method method = GrpcProtocolAuthServiceTest.class.getMethod(methodName); Method method = GrpcProtocolAuthServiceTest.class.getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class); return method.getAnnotation(Secured.class);
} }
} }

View File

@ -19,32 +19,36 @@ package com.alibaba.nacos.auth;
import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.auth.annotation.Secured; import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.auth.mock.MockAuthPluginService;
import com.alibaba.nacos.plugin.auth.api.IdentityContext; import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.plugin.auth.api.Permission; import com.alibaba.nacos.plugin.auth.api.Permission;
import com.alibaba.nacos.plugin.auth.api.Resource; import com.alibaba.nacos.plugin.auth.api.Resource;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.plugin.auth.constant.SignType; import com.alibaba.nacos.plugin.auth.constant.SignType;
import com.alibaba.nacos.plugin.auth.exception.AccessException; import com.alibaba.nacos.plugin.auth.exception.AccessException;
import com.alibaba.nacos.auth.mock.MockAuthPluginService; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class HttpProtocolAuthServiceTest { // todo remove this
@MockitoSettings(strictness = Strictness.LENIENT)
class HttpProtocolAuthServiceTest {
@Mock @Mock
private AuthConfigs authConfigs; private AuthConfigs authConfigs;
@ -54,8 +58,8 @@ public class HttpProtocolAuthServiceTest {
private HttpProtocolAuthService httpProtocolAuthService; private HttpProtocolAuthService httpProtocolAuthService;
@Before @BeforeEach
public void setUp() throws Exception { void setUp() throws Exception {
httpProtocolAuthService = new HttpProtocolAuthService(authConfigs); httpProtocolAuthService = new HttpProtocolAuthService(authConfigs);
httpProtocolAuthService.initialize(); httpProtocolAuthService.initialize();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNNs"); Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNNs");
@ -68,7 +72,7 @@ public class HttpProtocolAuthServiceTest {
@Test @Test
@Secured(resource = "testResource") @Secured(resource = "testResource")
public void testParseResourceWithSpecifiedResource() throws NoSuchMethodException { void testParseResourceWithSpecifiedResource() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithSpecifiedResource"); Secured secured = getMethodSecure("testParseResourceWithSpecifiedResource");
Resource actual = httpProtocolAuthService.parseResource(request, secured); Resource actual = httpProtocolAuthService.parseResource(request, secured);
assertEquals("testResource", actual.getName()); assertEquals("testResource", actual.getName());
@ -80,7 +84,7 @@ public class HttpProtocolAuthServiceTest {
@Test @Test
@Secured(signType = "non-exist") @Secured(signType = "non-exist")
public void testParseResourceWithNonExistType() throws NoSuchMethodException { void testParseResourceWithNonExistType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithNonExistType"); Secured secured = getMethodSecure("testParseResourceWithNonExistType");
Resource actual = httpProtocolAuthService.parseResource(request, secured); Resource actual = httpProtocolAuthService.parseResource(request, secured);
assertEquals(Resource.EMPTY_RESOURCE, actual); assertEquals(Resource.EMPTY_RESOURCE, actual);
@ -88,7 +92,7 @@ public class HttpProtocolAuthServiceTest {
@Test @Test
@Secured() @Secured()
public void testParseResourceWithNamingType() throws NoSuchMethodException { void testParseResourceWithNamingType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithNamingType"); Secured secured = getMethodSecure("testParseResourceWithNamingType");
Resource actual = httpProtocolAuthService.parseResource(request, secured); Resource actual = httpProtocolAuthService.parseResource(request, secured);
assertEquals(SignType.NAMING, actual.getType()); assertEquals(SignType.NAMING, actual.getType());
@ -100,7 +104,7 @@ public class HttpProtocolAuthServiceTest {
@Test @Test
@Secured(signType = SignType.CONFIG) @Secured(signType = SignType.CONFIG)
public void testParseResourceWithConfigType() throws NoSuchMethodException { void testParseResourceWithConfigType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithConfigType"); Secured secured = getMethodSecure("testParseResourceWithConfigType");
Resource actual = httpProtocolAuthService.parseResource(request, secured); Resource actual = httpProtocolAuthService.parseResource(request, secured);
assertEquals(SignType.CONFIG, actual.getType()); assertEquals(SignType.CONFIG, actual.getType());
@ -111,39 +115,39 @@ public class HttpProtocolAuthServiceTest {
} }
@Test @Test
public void testParseIdentity() { void testParseIdentity() {
IdentityContext actual = httpProtocolAuthService.parseIdentity(request); IdentityContext actual = httpProtocolAuthService.parseIdentity(request);
assertNotNull(actual); assertNotNull(actual);
} }
@Test @Test
public void testValidateIdentityWithoutPlugin() throws AccessException { void testValidateIdentityWithoutPlugin() throws AccessException {
IdentityContext identityContext = new IdentityContext(); IdentityContext identityContext = new IdentityContext();
assertTrue(httpProtocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE)); assertTrue(httpProtocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE));
} }
@Test @Test
public void testValidateIdentityWithPlugin() throws AccessException { void testValidateIdentityWithPlugin() throws AccessException {
Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN); Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN);
IdentityContext identityContext = new IdentityContext(); IdentityContext identityContext = new IdentityContext();
assertFalse(httpProtocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE)); assertFalse(httpProtocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE));
} }
@Test @Test
public void testValidateAuthorityWithoutPlugin() throws AccessException { void testValidateAuthorityWithoutPlugin() throws AccessException {
assertTrue(httpProtocolAuthService assertTrue(httpProtocolAuthService.validateAuthority(new IdentityContext(),
.validateAuthority(new IdentityContext(), new Permission(Resource.EMPTY_RESOURCE, ""))); new Permission(Resource.EMPTY_RESOURCE, "")));
} }
@Test @Test
public void testValidateAuthorityWithPlugin() throws AccessException { void testValidateAuthorityWithPlugin() throws AccessException {
Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN); Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN);
assertFalse(httpProtocolAuthService assertFalse(httpProtocolAuthService.validateAuthority(new IdentityContext(),
.validateAuthority(new IdentityContext(), new Permission(Resource.EMPTY_RESOURCE, ""))); new Permission(Resource.EMPTY_RESOURCE, "")));
} }
private Secured getMethodSecure(String methodName) throws NoSuchMethodException { private Secured getMethodSecure(String methodName) throws NoSuchMethodException {
Method method = HttpProtocolAuthServiceTest.class.getMethod(methodName); Method method = HttpProtocolAuthServiceTest.class.getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class); return method.getAnnotation(Secured.class);
} }
} }

View File

@ -18,13 +18,13 @@ package com.alibaba.nacos.auth.config;
import com.alibaba.nacos.common.event.ServerConfigChangeEvent; import com.alibaba.nacos.common.event.ServerConfigChangeEvent;
import com.alibaba.nacos.sys.env.EnvUtil; import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class AuthConfigsTest { class AuthConfigsTest {
private static final boolean TEST_AUTH_ENABLED = true; private static final boolean TEST_AUTH_ENABLED = true;
@ -40,8 +40,8 @@ public class AuthConfigsTest {
private MockEnvironment environment; private MockEnvironment environment;
@Before @BeforeEach
public void setUp() throws Exception { void setUp() throws Exception {
environment = new MockEnvironment(); environment = new MockEnvironment();
EnvUtil.setEnvironment(environment); EnvUtil.setEnvironment(environment);
environment.setProperty("nacos.core.auth.plugin.test.key", "test"); environment.setProperty("nacos.core.auth.plugin.test.key", "test");
@ -49,7 +49,7 @@ public class AuthConfigsTest {
} }
@Test @Test
public void testUpgradeFromEvent() { void testUpgradeFromEvent() {
environment.setProperty("nacos.core.auth.enabled", String.valueOf(TEST_AUTH_ENABLED)); environment.setProperty("nacos.core.auth.enabled", String.valueOf(TEST_AUTH_ENABLED));
environment.setProperty("nacos.core.auth.caching.enabled", String.valueOf(TEST_CACHING_ENABLED)); environment.setProperty("nacos.core.auth.caching.enabled", String.valueOf(TEST_CACHING_ENABLED));
environment.setProperty("nacos.core.auth.server.identity.key", TEST_SERVER_IDENTITY_KEY); environment.setProperty("nacos.core.auth.server.identity.key", TEST_SERVER_IDENTITY_KEY);

View File

@ -18,21 +18,21 @@ package com.alibaba.nacos.auth.config;
import com.alibaba.nacos.sys.module.ModuleState; import com.alibaba.nacos.sys.module.ModuleState;
import com.alibaba.nacos.sys.utils.ApplicationUtils; import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import static com.alibaba.nacos.auth.config.AuthModuleStateBuilder.AUTH_ENABLED; import static com.alibaba.nacos.auth.config.AuthModuleStateBuilder.AUTH_ENABLED;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class AuthModuleStateBuilderTest { class AuthModuleStateBuilderTest {
@Mock @Mock
private ConfigurableApplicationContext context; private ConfigurableApplicationContext context;
@ -40,19 +40,19 @@ public class AuthModuleStateBuilderTest {
@Mock @Mock
private AuthConfigs authConfigs; private AuthConfigs authConfigs;
@Before @BeforeEach
public void setUp() throws Exception { void setUp() throws Exception {
when(context.getBean(AuthConfigs.class)).thenReturn(authConfigs); when(context.getBean(AuthConfigs.class)).thenReturn(authConfigs);
ApplicationUtils.injectContext(context); ApplicationUtils.injectContext(context);
when(authConfigs.getNacosAuthSystemType()).thenReturn("nacos"); when(authConfigs.getNacosAuthSystemType()).thenReturn("nacos");
} }
@After @AfterEach
public void tearDown() throws Exception { void tearDown() throws Exception {
} }
@Test @Test
public void testBuild() { void testBuild() {
ModuleState actual = new AuthModuleStateBuilder().build(); ModuleState actual = new AuthModuleStateBuilder().build();
assertFalse((Boolean) actual.getStates().get(AUTH_ENABLED)); assertFalse((Boolean) actual.getStates().get(AUTH_ENABLED));
assertFalse((Boolean) actual.getStates().get("login_page_enabled")); assertFalse((Boolean) actual.getStates().get("login_page_enabled"));

View File

@ -17,24 +17,28 @@
package com.alibaba.nacos.auth.context; package com.alibaba.nacos.auth.context;
import com.alibaba.nacos.api.remote.request.Request; import com.alibaba.nacos.api.remote.request.Request;
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.auth.config.AuthConfigs; import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.plugin.auth.constant.Constants; import com.alibaba.nacos.plugin.auth.constant.Constants;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class GrpcIdentityContextBuilderTest { // todo remove this
@MockitoSettings(strictness = Strictness.LENIENT)
class GrpcIdentityContextBuilderTest {
private static final String TEST_PLUGIN = "test"; private static final String TEST_PLUGIN = "test";
@ -50,8 +54,8 @@ public class GrpcIdentityContextBuilderTest {
private GrpcIdentityContextBuilder identityContextBuilder; private GrpcIdentityContextBuilder identityContextBuilder;
@Before @BeforeEach
public void setUp() throws Exception { void setUp() throws Exception {
identityContextBuilder = new GrpcIdentityContextBuilder(authConfigs); identityContextBuilder = new GrpcIdentityContextBuilder(authConfigs);
when(authConfigs.getNacosAuthSystemType()).thenReturn(TEST_PLUGIN); when(authConfigs.getNacosAuthSystemType()).thenReturn(TEST_PLUGIN);
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
@ -61,14 +65,14 @@ public class GrpcIdentityContextBuilderTest {
} }
@Test @Test
public void testBuildWithoutPlugin() { void testBuildWithoutPlugin() {
when(authConfigs.getNacosAuthSystemType()).thenReturn("non-exist"); when(authConfigs.getNacosAuthSystemType()).thenReturn("non-exist");
IdentityContext actual = identityContextBuilder.build(request); IdentityContext actual = identityContextBuilder.build(request);
assertNull(actual.getParameter(IDENTITY_TEST_KEY)); assertNull(actual.getParameter(IDENTITY_TEST_KEY));
} }
@Test @Test
public void testBuild() { void testBuild() {
IdentityContext actual = identityContextBuilder.build(request); IdentityContext actual = identityContextBuilder.build(request);
assertEquals(IDENTITY_TEST_VALUE, actual.getParameter(IDENTITY_TEST_KEY)); assertEquals(IDENTITY_TEST_VALUE, actual.getParameter(IDENTITY_TEST_KEY));
assertEquals("1.1.1.1", actual.getParameter(Constants.Identity.REMOTE_IP)); assertEquals("1.1.1.1", actual.getParameter(Constants.Identity.REMOTE_IP));

View File

@ -16,24 +16,28 @@
package com.alibaba.nacos.auth.context; package com.alibaba.nacos.auth.context;
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.auth.config.AuthConfigs; import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.plugin.auth.constant.Constants; import com.alibaba.nacos.plugin.auth.constant.Constants;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration; import java.util.Enumeration;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class HtppIdentityContextBuilderTest { // todo remove this
@MockitoSettings(strictness = Strictness.LENIENT)
class HtppIdentityContextBuilderTest {
private static final String TEST_PLUGIN = "test"; private static final String TEST_PLUGIN = "test";
@ -55,14 +59,14 @@ public class HtppIdentityContextBuilderTest {
private HttpIdentityContextBuilder identityContextBuilder; private HttpIdentityContextBuilder identityContextBuilder;
@Before @BeforeEach
public void setUp() throws Exception { void setUp() throws Exception {
identityContextBuilder = new HttpIdentityContextBuilder(authConfigs); identityContextBuilder = new HttpIdentityContextBuilder(authConfigs);
when(authConfigs.getNacosAuthSystemType()).thenReturn(TEST_PLUGIN); when(authConfigs.getNacosAuthSystemType()).thenReturn(TEST_PLUGIN);
} }
@Test @Test
public void testBuildWithoutPlugin() { void testBuildWithoutPlugin() {
mockHeader(true); mockHeader(true);
mockParameter(true); mockParameter(true);
when(authConfigs.getNacosAuthSystemType()).thenReturn("non-exist"); when(authConfigs.getNacosAuthSystemType()).thenReturn("non-exist");
@ -71,7 +75,7 @@ public class HtppIdentityContextBuilderTest {
} }
@Test @Test
public void testBuildWithHeader() { void testBuildWithHeader() {
mockHeader(true); mockHeader(true);
mockParameter(false); mockParameter(false);
IdentityContext actual = identityContextBuilder.build(request); IdentityContext actual = identityContextBuilder.build(request);
@ -80,7 +84,7 @@ public class HtppIdentityContextBuilderTest {
} }
@Test @Test
public void testBuildWithParameter() { void testBuildWithParameter() {
mockHeader(false); mockHeader(false);
mockParameter(true); mockParameter(true);
IdentityContext actual = identityContextBuilder.build(request); IdentityContext actual = identityContextBuilder.build(request);

View File

@ -16,12 +16,12 @@
package com.alibaba.nacos.auth.mock; package com.alibaba.nacos.auth.mock;
import com.alibaba.nacos.plugin.auth.api.Resource;
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
import com.alibaba.nacos.plugin.auth.spi.server.AuthPluginService;
import com.alibaba.nacos.plugin.auth.api.IdentityContext; import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.plugin.auth.api.Permission; import com.alibaba.nacos.plugin.auth.api.Permission;
import com.alibaba.nacos.plugin.auth.api.Resource;
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
import com.alibaba.nacos.plugin.auth.exception.AccessException; import com.alibaba.nacos.plugin.auth.exception.AccessException;
import com.alibaba.nacos.plugin.auth.spi.server.AuthPluginService;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;

View File

@ -23,25 +23,25 @@ import com.alibaba.nacos.api.remote.request.Request;
import com.alibaba.nacos.auth.annotation.Secured; import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.auth.api.Resource; import com.alibaba.nacos.plugin.auth.api.Resource;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class ConfigGrpcResourceParserTest { class ConfigGrpcResourceParserTest {
private ConfigGrpcResourceParser resourceParser; private ConfigGrpcResourceParser resourceParser;
@Before @BeforeEach
public void setUp() throws Exception { void setUp() throws Exception {
resourceParser = new ConfigGrpcResourceParser(); resourceParser = new ConfigGrpcResourceParser();
} }
@Test @Test
@Secured(signType = Constants.Config.CONFIG_MODULE) @Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithFullContext() throws NoSuchMethodException { void testParseWithFullContext() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Request request = mockConfigRequest("testNs", "testG", "testD"); Request request = mockConfigRequest("testNs", "testG", "testD");
Resource actual = resourceParser.parse(request, secured); Resource actual = resourceParser.parse(request, secured);
@ -53,7 +53,7 @@ public class ConfigGrpcResourceParserTest {
@Test @Test
@Secured(signType = Constants.Config.CONFIG_MODULE) @Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithoutNamespace() throws NoSuchMethodException { void testParseWithoutNamespace() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Request request = mockConfigRequest("", "testG", "testD"); Request request = mockConfigRequest("", "testG", "testD");
Resource actual = resourceParser.parse(request, secured); Resource actual = resourceParser.parse(request, secured);
@ -65,7 +65,7 @@ public class ConfigGrpcResourceParserTest {
@Test @Test
@Secured(signType = Constants.Config.CONFIG_MODULE) @Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithoutGroup() throws NoSuchMethodException { void testParseWithoutGroup() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Request request = mockConfigRequest("testNs", "", "testD"); Request request = mockConfigRequest("testNs", "", "testD");
Resource actual = resourceParser.parse(request, secured); Resource actual = resourceParser.parse(request, secured);
@ -77,7 +77,7 @@ public class ConfigGrpcResourceParserTest {
@Test @Test
@Secured(signType = Constants.Config.CONFIG_MODULE) @Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithoutDataId() throws NoSuchMethodException { void testParseWithoutDataId() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Request request = mockConfigRequest("testNs", "testG", ""); Request request = mockConfigRequest("testNs", "testG", "");
Resource actual = resourceParser.parse(request, secured); Resource actual = resourceParser.parse(request, secured);
@ -89,7 +89,7 @@ public class ConfigGrpcResourceParserTest {
@Test @Test
@Secured(signType = Constants.Config.CONFIG_MODULE) @Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithConfigBatchListenRequest() throws NoSuchMethodException { void testParseWithConfigBatchListenRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
ConfigBatchListenRequest request = new ConfigBatchListenRequest(); ConfigBatchListenRequest request = new ConfigBatchListenRequest();
request.addConfigListenContext("testG", "testD", "testNs", "111"); request.addConfigListenContext("testG", "testD", "testNs", "111");
@ -112,7 +112,7 @@ public class ConfigGrpcResourceParserTest {
StackTraceElement[] traces = new Exception().getStackTrace(); StackTraceElement[] traces = new Exception().getStackTrace();
StackTraceElement callerElement = traces[1]; StackTraceElement callerElement = traces[1];
String methodName = callerElement.getMethodName(); String methodName = callerElement.getMethodName();
Method method = this.getClass().getMethod(methodName); Method method = this.getClass().getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class); return method.getAnnotation(Secured.class);
} }
} }

View File

@ -23,26 +23,26 @@ import com.alibaba.nacos.api.remote.request.Request;
import com.alibaba.nacos.auth.annotation.Secured; import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.auth.api.Resource; import com.alibaba.nacos.plugin.auth.api.Resource;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
public class NamingGrpcResourceParserTest { class NamingGrpcResourceParserTest {
private NamingGrpcResourceParser resourceParser; private NamingGrpcResourceParser resourceParser;
@Before @BeforeEach
public void setUp() throws Exception { void setUp() throws Exception {
resourceParser = new NamingGrpcResourceParser(); resourceParser = new NamingGrpcResourceParser();
} }
@Test @Test
@Secured() @Secured()
public void testParseWithFullContextForNamingRequest() throws NoSuchMethodException { void testParseWithFullContextForNamingRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
AbstractNamingRequest request = mockNamingRequest("testNs", "testG", "testS"); AbstractNamingRequest request = mockNamingRequest("testNs", "testG", "testS");
Resource actual = resourceParser.parse(request, secured); Resource actual = resourceParser.parse(request, secured);
@ -50,13 +50,13 @@ public class NamingGrpcResourceParserTest {
assertEquals("testG", actual.getGroup()); assertEquals("testG", actual.getGroup());
assertEquals("testS", actual.getName()); assertEquals("testS", actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties().getProperty( assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties()
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); .getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
} }
@Test @Test
@Secured() @Secured()
public void testParseWithFullContextForOtherRequest() throws NoSuchMethodException { void testParseWithFullContextForOtherRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Request request = mockOtherRequest("testNs", "testG", "testS"); Request request = mockOtherRequest("testNs", "testG", "testS");
Resource actual = resourceParser.parse(request, secured); Resource actual = resourceParser.parse(request, secured);
@ -64,13 +64,13 @@ public class NamingGrpcResourceParserTest {
assertEquals("testG", actual.getGroup()); assertEquals("testG", actual.getGroup());
assertEquals("testS", actual.getName()); assertEquals("testS", actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties().getProperty( assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties()
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); .getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
} }
@Test @Test
@Secured() @Secured()
public void testParseWithoutNamespaceForNamingRequest() throws NoSuchMethodException { void testParseWithoutNamespaceForNamingRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
AbstractNamingRequest request = mockNamingRequest(null, "testG", "testS"); AbstractNamingRequest request = mockNamingRequest(null, "testG", "testS");
Resource actual = resourceParser.parse(request, secured); Resource actual = resourceParser.parse(request, secured);
@ -78,13 +78,13 @@ public class NamingGrpcResourceParserTest {
assertEquals("testG", actual.getGroup()); assertEquals("testG", actual.getGroup());
assertEquals("testS", actual.getName()); assertEquals("testS", actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties().getProperty( assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties()
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); .getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
} }
@Test @Test
@Secured() @Secured()
public void testParseWithoutNamespaceForOtherRequest() throws NoSuchMethodException { void testParseWithoutNamespaceForOtherRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Request request = mockOtherRequest(null, "testG", "testS"); Request request = mockOtherRequest(null, "testG", "testS");
Resource actual = resourceParser.parse(request, secured); Resource actual = resourceParser.parse(request, secured);
@ -92,13 +92,13 @@ public class NamingGrpcResourceParserTest {
assertEquals("testG", actual.getGroup()); assertEquals("testG", actual.getGroup());
assertEquals("testS", actual.getName()); assertEquals("testS", actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties().getProperty( assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties()
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); .getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
} }
@Test @Test
@Secured() @Secured()
public void testParseWithoutGroupForNamingRequest() throws NoSuchMethodException { void testParseWithoutGroupForNamingRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
AbstractNamingRequest request = mockNamingRequest("testNs", null, "testS"); AbstractNamingRequest request = mockNamingRequest("testNs", null, "testS");
Resource actual = resourceParser.parse(request, secured); Resource actual = resourceParser.parse(request, secured);
@ -106,13 +106,13 @@ public class NamingGrpcResourceParserTest {
assertEquals(StringUtils.EMPTY, actual.getGroup()); assertEquals(StringUtils.EMPTY, actual.getGroup());
assertEquals("testS", actual.getName()); assertEquals("testS", actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties().getProperty( assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties()
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); .getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
} }
@Test @Test
@Secured() @Secured()
public void testParseWithoutGroupForOtherRequest() throws NoSuchMethodException { void testParseWithoutGroupForOtherRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Request request = mockOtherRequest("testNs", null, "testS"); Request request = mockOtherRequest("testNs", null, "testS");
Resource actual = resourceParser.parse(request, secured); Resource actual = resourceParser.parse(request, secured);
@ -120,13 +120,13 @@ public class NamingGrpcResourceParserTest {
assertEquals(StringUtils.EMPTY, actual.getGroup()); assertEquals(StringUtils.EMPTY, actual.getGroup());
assertEquals("testS", actual.getName()); assertEquals("testS", actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties().getProperty( assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties()
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); .getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
} }
@Test @Test
@Secured() @Secured()
public void testParseWithoutDataIdForNamingRequest() throws NoSuchMethodException { void testParseWithoutDataIdForNamingRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
AbstractNamingRequest request = mockNamingRequest("testNs", "testG", null); AbstractNamingRequest request = mockNamingRequest("testNs", "testG", null);
Resource actual = resourceParser.parse(request, secured); Resource actual = resourceParser.parse(request, secured);
@ -134,13 +134,13 @@ public class NamingGrpcResourceParserTest {
assertEquals("testG", actual.getGroup()); assertEquals("testG", actual.getGroup());
assertEquals(StringUtils.EMPTY, actual.getName()); assertEquals(StringUtils.EMPTY, actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties().getProperty( assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties()
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); .getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
} }
@Test @Test
@Secured() @Secured()
public void testParseWithoutDataIdForOtherRequest() throws NoSuchMethodException { void testParseWithoutDataIdForOtherRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Request request = mockOtherRequest("testNs", "testG", null); Request request = mockOtherRequest("testNs", "testG", null);
Resource actual = resourceParser.parse(request, secured); Resource actual = resourceParser.parse(request, secured);
@ -148,8 +148,8 @@ public class NamingGrpcResourceParserTest {
assertEquals("testG", actual.getGroup()); assertEquals("testG", actual.getGroup());
assertEquals(StringUtils.EMPTY, actual.getName()); assertEquals(StringUtils.EMPTY, actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties().getProperty( assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties()
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); .getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
} }
private AbstractNamingRequest mockNamingRequest(String testNs, String testG, String testS) { private AbstractNamingRequest mockNamingRequest(String testNs, String testG, String testS) {
@ -172,7 +172,7 @@ public class NamingGrpcResourceParserTest {
StackTraceElement[] traces = new Exception().getStackTrace(); StackTraceElement[] traces = new Exception().getStackTrace();
StackTraceElement callerElement = traces[1]; StackTraceElement callerElement = traces[1];
String methodName = callerElement.getMethodName(); String methodName = callerElement.getMethodName();
Method method = this.getClass().getMethod(methodName); Method method = this.getClass().getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class); return method.getAnnotation(Secured.class);
} }

View File

@ -20,36 +20,39 @@ import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.auth.annotation.Secured; import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.auth.api.Resource; import com.alibaba.nacos.plugin.auth.api.Resource;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class ConfigHttpResourceParserTest { // todo remove this
@MockitoSettings(strictness = Strictness.LENIENT)
class ConfigHttpResourceParserTest {
@Mock @Mock
private HttpServletRequest request; private HttpServletRequest request;
private ConfigHttpResourceParser resourceParser; private ConfigHttpResourceParser resourceParser;
@Before @BeforeEach
public void setUp() throws Exception { void setUp() throws Exception {
resourceParser = new ConfigHttpResourceParser(); resourceParser = new ConfigHttpResourceParser();
} }
@Test @Test
@Secured(signType = Constants.Config.CONFIG_MODULE) @Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithFullContext() throws NoSuchMethodException { void testParseWithFullContext() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq("tenant"))).thenReturn("testNs"); Mockito.when(request.getParameter(eq("tenant"))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testG"); Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testG");
@ -63,7 +66,7 @@ public class ConfigHttpResourceParserTest {
@Test @Test
@Secured(signType = Constants.Config.CONFIG_MODULE) @Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithoutNamespace() throws NoSuchMethodException { void testParseWithoutNamespace() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testG"); Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testG");
Mockito.when(request.getParameter(eq(Constants.DATAID))).thenReturn("testD"); Mockito.when(request.getParameter(eq(Constants.DATAID))).thenReturn("testD");
@ -76,7 +79,7 @@ public class ConfigHttpResourceParserTest {
@Test @Test
@Secured(signType = Constants.Config.CONFIG_MODULE) @Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithoutGroup() throws NoSuchMethodException { void testParseWithoutGroup() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq("tenant"))).thenReturn("testNs"); Mockito.when(request.getParameter(eq("tenant"))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(Constants.DATAID))).thenReturn("testD"); Mockito.when(request.getParameter(eq(Constants.DATAID))).thenReturn("testD");
@ -89,7 +92,7 @@ public class ConfigHttpResourceParserTest {
@Test @Test
@Secured(signType = Constants.Config.CONFIG_MODULE) @Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithoutDataId() throws NoSuchMethodException { void testParseWithoutDataId() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq("tenant"))).thenReturn("testNs"); Mockito.when(request.getParameter(eq("tenant"))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testG"); Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testG");
@ -104,7 +107,7 @@ public class ConfigHttpResourceParserTest {
StackTraceElement[] traces = new Exception().getStackTrace(); StackTraceElement[] traces = new Exception().getStackTrace();
StackTraceElement callerElement = traces[1]; StackTraceElement callerElement = traces[1];
String methodName = callerElement.getMethodName(); String methodName = callerElement.getMethodName();
Method method = this.getClass().getMethod(methodName); Method method = this.getClass().getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class); return method.getAnnotation(Secured.class);
} }
} }

View File

@ -21,37 +21,40 @@ import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.auth.annotation.Secured; import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.auth.api.Resource; import com.alibaba.nacos.plugin.auth.api.Resource;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class NamingHttpResourceParserTest { // todo remove this
@MockitoSettings(strictness = Strictness.LENIENT)
class NamingHttpResourceParserTest {
@Mock @Mock
private HttpServletRequest request; private HttpServletRequest request;
private NamingHttpResourceParser resourceParser; private NamingHttpResourceParser resourceParser;
@Before @BeforeEach
public void setUp() throws Exception { void setUp() throws Exception {
resourceParser = new NamingHttpResourceParser(); resourceParser = new NamingHttpResourceParser();
} }
@Test @Test
@Secured() @Secured()
public void testParseWithFullContext() throws NoSuchMethodException { void testParseWithFullContext() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs"); Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(CommonParams.GROUP_NAME))).thenReturn("testG"); Mockito.when(request.getParameter(eq(CommonParams.GROUP_NAME))).thenReturn("testG");
@ -65,7 +68,7 @@ public class NamingHttpResourceParserTest {
@Test @Test
@Secured() @Secured()
public void testParseWithoutNamespace() throws NoSuchMethodException { void testParseWithoutNamespace() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.GROUP_NAME))).thenReturn("testG"); Mockito.when(request.getParameter(eq(CommonParams.GROUP_NAME))).thenReturn("testG");
Mockito.when(request.getParameter(eq(CommonParams.SERVICE_NAME))).thenReturn("testS"); Mockito.when(request.getParameter(eq(CommonParams.SERVICE_NAME))).thenReturn("testS");
@ -78,7 +81,7 @@ public class NamingHttpResourceParserTest {
@Test @Test
@Secured() @Secured()
public void testParseWithoutGroup() throws NoSuchMethodException { void testParseWithoutGroup() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs"); Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(CommonParams.SERVICE_NAME))).thenReturn("testS"); Mockito.when(request.getParameter(eq(CommonParams.SERVICE_NAME))).thenReturn("testS");
@ -91,7 +94,7 @@ public class NamingHttpResourceParserTest {
@Test @Test
@Secured() @Secured()
public void testParseWithGroupInService() throws NoSuchMethodException { void testParseWithGroupInService() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs"); Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(CommonParams.SERVICE_NAME))).thenReturn("testG@@testS"); Mockito.when(request.getParameter(eq(CommonParams.SERVICE_NAME))).thenReturn("testG@@testS");
@ -104,7 +107,7 @@ public class NamingHttpResourceParserTest {
@Test @Test
@Secured() @Secured()
public void testParseWithoutService() throws NoSuchMethodException { void testParseWithoutService() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs"); Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(CommonParams.GROUP_NAME))).thenReturn("testG"); Mockito.when(request.getParameter(eq(CommonParams.GROUP_NAME))).thenReturn("testG");
@ -117,7 +120,7 @@ public class NamingHttpResourceParserTest {
@Test @Test
@Secured() @Secured()
public void testParseWithoutGroupAndService() throws NoSuchMethodException { void testParseWithoutGroupAndService() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs"); Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs");
Resource actual = resourceParser.parse(request, secured); Resource actual = resourceParser.parse(request, secured);
@ -129,7 +132,7 @@ public class NamingHttpResourceParserTest {
@Test @Test
@Secured(tags = {"testTag"}) @Secured(tags = {"testTag"})
public void testParseWithTags() throws NoSuchMethodException { void testParseWithTags() throws NoSuchMethodException {
Secured secured = getMethodSecure(); Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs"); Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(CommonParams.GROUP_NAME))).thenReturn("testG"); Mockito.when(request.getParameter(eq(CommonParams.GROUP_NAME))).thenReturn("testG");
@ -146,7 +149,7 @@ public class NamingHttpResourceParserTest {
StackTraceElement[] traces = new Exception().getStackTrace(); StackTraceElement[] traces = new Exception().getStackTrace();
StackTraceElement callerElement = traces[1]; StackTraceElement callerElement = traces[1];
String methodName = callerElement.getMethodName(); String methodName = callerElement.getMethodName();
Method method = this.getClass().getMethod(methodName); Method method = this.getClass().getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class); return method.getAnnotation(Secured.class);
} }
} }