Skip to content

Commit

Permalink
upgrade module naocs-auth from junit4 to junit5 (#12105)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalk committed May 16, 2024
1 parent 67b6722 commit dc0e46e
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,30 @@
import com.alibaba.nacos.api.config.remote.request.ConfigPublishRequest;
import com.alibaba.nacos.api.naming.remote.request.AbstractNamingRequest;
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.Permission;
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.exception.AccessException;
import com.alibaba.nacos.auth.mock.MockAuthPluginService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

import java.lang.reflect.Method;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@RunWith(MockitoJUnitRunner.class)
public class GrpcProtocolAuthServiceTest {
@ExtendWith(MockitoExtension.class)
class GrpcProtocolAuthServiceTest {

@Mock
private AuthConfigs authConfigs;
Expand All @@ -53,8 +53,8 @@ public class GrpcProtocolAuthServiceTest {

private GrpcProtocolAuthService protocolAuthService;

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
protocolAuthService = new GrpcProtocolAuthService(authConfigs);
protocolAuthService.initialize();
mockConfigRequest();
Expand All @@ -78,7 +78,7 @@ private void mockNamingRequest() {

@Test
@Secured(resource = "testResource")
public void testParseResourceWithSpecifiedResource() throws NoSuchMethodException {
void testParseResourceWithSpecifiedResource() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithSpecifiedResource");
Resource actual = protocolAuthService.parseResource(namingRequest, secured);
assertEquals("testResource", actual.getName());
Expand All @@ -90,15 +90,15 @@ public void testParseResourceWithSpecifiedResource() throws NoSuchMethodExceptio

@Test
@Secured(signType = "non-exist")
public void testParseResourceWithNonExistType() throws NoSuchMethodException {
void testParseResourceWithNonExistType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithNonExistType");
Resource actual = protocolAuthService.parseResource(namingRequest, secured);
assertEquals(Resource.EMPTY_RESOURCE, actual);
}

@Test
@Secured()
public void testParseResourceWithNamingType() throws NoSuchMethodException {
void testParseResourceWithNamingType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithNamingType");
Resource actual = protocolAuthService.parseResource(namingRequest, secured);
assertEquals(SignType.NAMING, actual.getType());
Expand All @@ -110,7 +110,7 @@ public void testParseResourceWithNamingType() throws NoSuchMethodException {

@Test
@Secured(signType = SignType.CONFIG)
public void testParseResourceWithConfigType() throws NoSuchMethodException {
void testParseResourceWithConfigType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithConfigType");
Resource actual = protocolAuthService.parseResource(configRequest, secured);
assertEquals(SignType.CONFIG, actual.getType());
Expand All @@ -121,39 +121,39 @@ public void testParseResourceWithConfigType() throws NoSuchMethodException {
}

@Test
public void testParseIdentity() {
void testParseIdentity() {
IdentityContext actual = protocolAuthService.parseIdentity(namingRequest);
assertNotNull(actual);
}

@Test
public void testValidateIdentityWithoutPlugin() throws AccessException {
void testValidateIdentityWithoutPlugin() throws AccessException {
IdentityContext identityContext = new IdentityContext();
assertTrue(protocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE));
}

@Test
public void testValidateIdentityWithPlugin() throws AccessException {
void testValidateIdentityWithPlugin() throws AccessException {
Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN);
IdentityContext identityContext = new IdentityContext();
assertFalse(protocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE));
}

@Test
public void testValidateAuthorityWithoutPlugin() throws AccessException {
assertTrue(protocolAuthService
.validateAuthority(new IdentityContext(), new Permission(Resource.EMPTY_RESOURCE, "")));
void testValidateAuthorityWithoutPlugin() throws AccessException {
assertTrue(protocolAuthService.validateAuthority(new IdentityContext(),
new Permission(Resource.EMPTY_RESOURCE, "")));
}

@Test
public void testValidateAuthorityWithPlugin() throws AccessException {
void testValidateAuthorityWithPlugin() throws AccessException {
Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN);
assertFalse(protocolAuthService
.validateAuthority(new IdentityContext(), new Permission(Resource.EMPTY_RESOURCE, "")));
assertFalse(protocolAuthService.validateAuthority(new IdentityContext(),
new Permission(Resource.EMPTY_RESOURCE, "")));
}

private Secured getMethodSecure(String methodName) throws NoSuchMethodException {
Method method = GrpcProtocolAuthServiceTest.class.getMethod(methodName);
Method method = GrpcProtocolAuthServiceTest.class.getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,36 @@
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.CommonParams;
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.Permission;
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.exception.AccessException;
import com.alibaba.nacos.auth.mock.MockAuthPluginService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
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 java.lang.reflect.Method;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;

@RunWith(MockitoJUnitRunner.class)
public class HttpProtocolAuthServiceTest {
@ExtendWith(MockitoExtension.class)
// todo remove this
@MockitoSettings(strictness = Strictness.LENIENT)
class HttpProtocolAuthServiceTest {

@Mock
private AuthConfigs authConfigs;
Expand All @@ -54,8 +58,8 @@ public class HttpProtocolAuthServiceTest {

private HttpProtocolAuthService httpProtocolAuthService;

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
httpProtocolAuthService = new HttpProtocolAuthService(authConfigs);
httpProtocolAuthService.initialize();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNNs");
Expand All @@ -68,7 +72,7 @@ public void setUp() throws Exception {

@Test
@Secured(resource = "testResource")
public void testParseResourceWithSpecifiedResource() throws NoSuchMethodException {
void testParseResourceWithSpecifiedResource() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithSpecifiedResource");
Resource actual = httpProtocolAuthService.parseResource(request, secured);
assertEquals("testResource", actual.getName());
Expand All @@ -80,15 +84,15 @@ public void testParseResourceWithSpecifiedResource() throws NoSuchMethodExceptio

@Test
@Secured(signType = "non-exist")
public void testParseResourceWithNonExistType() throws NoSuchMethodException {
void testParseResourceWithNonExistType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithNonExistType");
Resource actual = httpProtocolAuthService.parseResource(request, secured);
assertEquals(Resource.EMPTY_RESOURCE, actual);
}

@Test
@Secured()
public void testParseResourceWithNamingType() throws NoSuchMethodException {
void testParseResourceWithNamingType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithNamingType");
Resource actual = httpProtocolAuthService.parseResource(request, secured);
assertEquals(SignType.NAMING, actual.getType());
Expand All @@ -100,7 +104,7 @@ public void testParseResourceWithNamingType() throws NoSuchMethodException {

@Test
@Secured(signType = SignType.CONFIG)
public void testParseResourceWithConfigType() throws NoSuchMethodException {
void testParseResourceWithConfigType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithConfigType");
Resource actual = httpProtocolAuthService.parseResource(request, secured);
assertEquals(SignType.CONFIG, actual.getType());
Expand All @@ -111,39 +115,39 @@ public void testParseResourceWithConfigType() throws NoSuchMethodException {
}

@Test
public void testParseIdentity() {
void testParseIdentity() {
IdentityContext actual = httpProtocolAuthService.parseIdentity(request);
assertNotNull(actual);
}

@Test
public void testValidateIdentityWithoutPlugin() throws AccessException {
void testValidateIdentityWithoutPlugin() throws AccessException {
IdentityContext identityContext = new IdentityContext();
assertTrue(httpProtocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE));
}

@Test
public void testValidateIdentityWithPlugin() throws AccessException {
void testValidateIdentityWithPlugin() throws AccessException {
Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN);
IdentityContext identityContext = new IdentityContext();
assertFalse(httpProtocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE));
}

@Test
public void testValidateAuthorityWithoutPlugin() throws AccessException {
assertTrue(httpProtocolAuthService
.validateAuthority(new IdentityContext(), new Permission(Resource.EMPTY_RESOURCE, "")));
void testValidateAuthorityWithoutPlugin() throws AccessException {
assertTrue(httpProtocolAuthService.validateAuthority(new IdentityContext(),
new Permission(Resource.EMPTY_RESOURCE, "")));
}

@Test
public void testValidateAuthorityWithPlugin() throws AccessException {
void testValidateAuthorityWithPlugin() throws AccessException {
Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN);
assertFalse(httpProtocolAuthService
.validateAuthority(new IdentityContext(), new Permission(Resource.EMPTY_RESOURCE, "")));
assertFalse(httpProtocolAuthService.validateAuthority(new IdentityContext(),
new Permission(Resource.EMPTY_RESOURCE, "")));
}

private Secured getMethodSecure(String methodName) throws NoSuchMethodException {
Method method = HttpProtocolAuthServiceTest.class.getMethod(methodName);
Method method = HttpProtocolAuthServiceTest.class.getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

import com.alibaba.nacos.common.event.ServerConfigChangeEvent;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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;

Expand All @@ -40,16 +40,16 @@ public class AuthConfigsTest {

private MockEnvironment environment;

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
environment = new MockEnvironment();
EnvUtil.setEnvironment(environment);
environment.setProperty("nacos.core.auth.plugin.test.key", "test");
authConfigs = new AuthConfigs();
}

@Test
public void testUpgradeFromEvent() {
void testUpgradeFromEvent() {
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.server.identity.key", TEST_SERVER_IDENTITY_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,41 @@

import com.alibaba.nacos.sys.module.ModuleState;
import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ConfigurableApplicationContext;

import static com.alibaba.nacos.auth.config.AuthModuleStateBuilder.AUTH_ENABLED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class AuthModuleStateBuilderTest {
@ExtendWith(MockitoExtension.class)
class AuthModuleStateBuilderTest {

@Mock
private ConfigurableApplicationContext context;

@Mock
private AuthConfigs authConfigs;

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
when(context.getBean(AuthConfigs.class)).thenReturn(authConfigs);
ApplicationUtils.injectContext(context);
when(authConfigs.getNacosAuthSystemType()).thenReturn("nacos");
}

@After
public void tearDown() throws Exception {
@AfterEach
void tearDown() throws Exception {
}

@Test
public void testBuild() {
void testBuild() {
ModuleState actual = new AuthModuleStateBuilder().build();
assertFalse((Boolean) actual.getStates().get(AUTH_ENABLED));
assertFalse((Boolean) actual.getStates().get("login_page_enabled"));
Expand Down

0 comments on commit dc0e46e

Please sign in to comment.