From b2a786b170f61bad629bbab6331f66333416e160 Mon Sep 17 00:00:00 2001 From: stone-98 <670569467@qq.com> Date: Wed, 17 Jul 2024 22:27:39 +0800 Subject: [PATCH] [IT]Adjust core module auth integration tests to meet checkstyle requirements. --- .../nacos/test/core/auth/AuthBase.java | 94 +++++++++++-------- ..._ITCase.java => ConfigAuthCoreITCase.java} | 37 ++++---- ...th_ITCase.java => LdapAuthCoreITCase.java} | 17 ++-- ..._ITCase.java => NamingAuthCoreITCase.java} | 17 ++-- ..._ITCase.java => PermissionCoreITCase.java} | 53 ++++++----- .../{Role_ITCase.java => RoleCoreITCase.java} | 56 ++++++----- .../{User_ITCase.java => UserCoreITCase.java} | 33 ++++--- 7 files changed, 173 insertions(+), 134 deletions(-) rename test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/{ConfigAuth_ITCase.java => ConfigAuthCoreITCase.java} (93%) rename test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/{LdapAuth_ITCase.java => LdapAuthCoreITCase.java} (87%) rename test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/{NamingAuth_ITCase.java => NamingAuthCoreITCase.java} (91%) rename test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/{Permission_ITCase.java => PermissionCoreITCase.java} (84%) rename test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/{Role_ITCase.java => RoleCoreITCase.java} (82%) rename test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/{User_ITCase.java => UserCoreITCase.java} (92%) diff --git a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/AuthBase.java b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/AuthBase.java index 5db5e3eb9ac..71e25870a4e 100644 --- a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/AuthBase.java +++ b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/AuthBase.java @@ -31,8 +31,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue; - /** + * Base class for authentication tests in Nacos.` + * * @author nkorange * @since 1.2.0 */ @@ -62,10 +63,18 @@ public class AuthBase extends HttpClient4Test { protected String namespace1 = "namespace1"; + /** + * Logs in a user with the provided username and password, then returns the access token. + * + * @param username the username of the user + * @param password the password of the user + * @return the access token (accessToken) as a String + * @throws AssertionError if the response status is not 2xx or the accessToken is missing + */ public String login(String username, String password) { ResponseEntity response = request("/nacos/v1/auth/users/login", - Params.newParams().appendParam("username", username).appendParam("password", password).done(), String.class, - HttpMethod.POST); + Params.newParams().appendParam("username", username).appendParam("password", password).done(), + String.class, HttpMethod.POST); assertTrue(response.getStatusCode().is2xxSuccessful()); JsonNode json = JacksonUtils.toObj(response.getBody()); @@ -93,59 +102,64 @@ protected void init(int port) throws Exception { assertTrue(response.getStatusCode().is2xxSuccessful()); // Create a user: - response = request("/nacos/v1/auth/users", Params.newParams().appendParam("username", username2).appendParam("password", password2) - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); + response = request("/nacos/v1/auth/users", + Params.newParams().appendParam("username", username2).appendParam("password", password2) + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); System.out.println(response); assertTrue(response.getStatusCode().is2xxSuccessful()); // Create a user: - response = request("/nacos/v1/auth/users", Params.newParams().appendParam("username", username3).appendParam("password", password3) - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); + response = request("/nacos/v1/auth/users", + Params.newParams().appendParam("username", username3).appendParam("password", password3) + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); System.out.println(response); assertTrue(response.getStatusCode().is2xxSuccessful()); // Create a role: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", role1).appendParam("username", username1).appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.POST); + Params.newParams().appendParam("role", role1).appendParam("username", username1) + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); System.out.println(response); assertTrue(response.getStatusCode().is2xxSuccessful()); // Create a role: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", role2).appendParam("username", username2).appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.POST); + Params.newParams().appendParam("role", role2).appendParam("username", username2) + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); System.out.println(response); assertTrue(response.getStatusCode().is2xxSuccessful()); // Create a role: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", role3).appendParam("username", username3).appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.POST); + Params.newParams().appendParam("role", role3).appendParam("username", username3) + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); System.out.println(response); assertTrue(response.getStatusCode().is2xxSuccessful()); // Add read permission of namespace1 to role1: response = request("/nacos/v1/auth/permissions", - Params.newParams().appendParam("role", role1).appendParam("resource", namespace1 + ":*:*").appendParam("action", "r") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); + Params.newParams().appendParam("role", role1).appendParam("resource", namespace1 + ":*:*") + .appendParam("action", "r").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.POST); System.out.println(response); assertTrue(response.getStatusCode().is2xxSuccessful()); // Add write permission of namespace1 to role2: response = request("/nacos/v1/auth/permissions", - Params.newParams().appendParam("role", role2).appendParam("resource", namespace1 + ":*:*").appendParam("action", "w") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); + Params.newParams().appendParam("role", role2).appendParam("resource", namespace1 + ":*:*") + .appendParam("action", "w").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.POST); System.out.println(response); assertTrue(response.getStatusCode().is2xxSuccessful()); // Add read/write permission of namespace1 to role3: response = request("/nacos/v1/auth/permissions", - Params.newParams().appendParam("role", role3).appendParam("resource", namespace1 + ":*:*").appendParam("action", "rw") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); + Params.newParams().appendParam("role", role3).appendParam("resource", namespace1 + ":*:*") + .appendParam("action", "rw").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.POST); System.out.println(response); assertTrue(response.getStatusCode().is2xxSuccessful()); @@ -159,61 +173,67 @@ protected void destroy() { // Delete permission: ResponseEntity response = request("/nacos/v1/auth/permissions", - Params.newParams().appendParam("role", role1).appendParam("resource", namespace1 + ":*:*").appendParam("action", "r") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", role1).appendParam("resource", namespace1 + ":*:*") + .appendParam("action", "r").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete permission: response = request("/nacos/v1/auth/permissions", - Params.newParams().appendParam("role", role2).appendParam("resource", namespace1 + ":*:*").appendParam("action", "w") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", role2).appendParam("resource", namespace1 + ":*:*") + .appendParam("action", "w").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete permission: response = request("/nacos/v1/auth/permissions", - Params.newParams().appendParam("role", role3).appendParam("resource", namespace1 + ":*:*").appendParam("action", "rw") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", role3).appendParam("resource", namespace1 + ":*:*") + .appendParam("action", "rw").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete a role: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", role1).appendParam("username", username1).appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", role1).appendParam("username", username1) + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete a role: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", role2).appendParam("username", username2).appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", role2).appendParam("username", username2) + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete a role: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", role3).appendParam("username", username3).appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", role3).appendParam("username", username3) + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete a user: - response = request("/nacos/v1/auth/users", Params.newParams().appendParam("username", username1).appendParam("password", password1) - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); + response = request("/nacos/v1/auth/users", + Params.newParams().appendParam("username", username1).appendParam("password", password1) + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete a user: - response = request("/nacos/v1/auth/users", Params.newParams().appendParam("username", username2).appendParam("password", password2) - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); + response = request("/nacos/v1/auth/users", + Params.newParams().appendParam("username", username2).appendParam("password", password2) + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete a user: - response = request("/nacos/v1/auth/users", Params.newParams().appendParam("username", username3).appendParam("password", password3) - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); + response = request("/nacos/v1/auth/users", + Params.newParams().appendParam("username", username3).appendParam("password", password3) + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); diff --git a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuthCoreITCase.java similarity index 93% rename from test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java rename to test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuthCoreITCase.java index cf8ce7853c3..8b3eb7a3e26 100644 --- a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java +++ b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuthCoreITCase.java @@ -33,7 +33,6 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -41,12 +40,15 @@ import static org.junit.jupiter.api.Assertions.fail; /** + * Integration tests for Nacos configuration with authentication. + * * @author nkorange * @since 1.2.0 */ +@SuppressWarnings("checkstyle:AbbreviationAsWordInName") @SpringBootTest(classes = Nacos.class, properties = { "server.servlet.contextPath=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -public class ConfigAuth_ITCase extends AuthBase { +public class ConfigAuthCoreITCase extends AuthBase { public static final long TIME_OUT = 2000; @@ -55,15 +57,18 @@ public class ConfigAuth_ITCase extends AuthBase { @LocalServerPort private int port; - private String dataId = "yanlin"; + private final String dataId = "yanlin"; - private String group = "yanlin"; + private final String group = "yanlin"; @BeforeEach void init() throws Exception { super.init(port); } + /** + * Cleans up resources after each test execution. + */ @AfterEach public void destroy() { super.destroy(); @@ -72,11 +77,10 @@ public void destroy() { iconfig.shutDown(); } } catch (NacosException ex) { - + // Ignored exception during shutdown } } - @Test void writeWithReadPermission() throws Exception { @@ -93,10 +97,6 @@ void writeWithReadPermission() throws Exception { @Test void readWithReadPermission() throws Exception { - - CountDownLatch latch = new CountDownLatch(1); - AtomicInteger ai = new AtomicInteger(0); - properties.put(PropertyKeyConst.USERNAME, username1); properties.put(PropertyKeyConst.PASSWORD, password1); iconfig = NacosFactory.createConfigService(properties); @@ -104,6 +104,7 @@ void readWithReadPermission() throws Exception { final String content = "test" + System.currentTimeMillis(); System.out.println(content); + CountDownLatch latch = new CountDownLatch(1); iconfig.addListener(dataId, group, new AbstractConfigChangeListener() { @Override public void receiveConfigChange(ConfigChangeEvent event) { @@ -151,9 +152,6 @@ void writeWithWritePermission() throws Exception { @Test void readWithWritePermission() throws Exception { - - CountDownLatch latch = new CountDownLatch(1); - properties.put(PropertyKeyConst.NAMESPACE, namespace1); properties.put(PropertyKeyConst.USERNAME, username2); properties.put(PropertyKeyConst.PASSWORD, password2); @@ -161,6 +159,7 @@ void readWithWritePermission() throws Exception { final String content = "test" + System.currentTimeMillis(); + CountDownLatch latch = new CountDownLatch(1); iconfig.addListener(dataId, group, new AbstractConfigChangeListener() { @Override public void receiveConfigChange(ConfigChangeEvent event) { @@ -182,8 +181,8 @@ public void receiveConfigChange(ConfigChangeEvent event) { try { iconfig.getConfig(dataId, group, TIME_OUT); fail(); - } catch (NacosException ne) { - assertEquals(HttpStatus.SC_FORBIDDEN, ne.getErrCode()); + } catch (NacosException e) { + assertEquals(HttpStatus.SC_FORBIDDEN, e.getErrCode()); } latch.await(5L, TimeUnit.SECONDS); @@ -191,19 +190,15 @@ public void receiveConfigChange(ConfigChangeEvent event) { assertTrue(latch.getCount() > 0); } - @Test - void ReadWriteWithFullPermission() throws Exception { - - CountDownLatch latch = new CountDownLatch(1); - AtomicInteger ai = new AtomicInteger(0); - + void readWriteWithFullPermission() throws Exception { properties.put(PropertyKeyConst.USERNAME, username3); properties.put(PropertyKeyConst.PASSWORD, password3); iconfig = NacosFactory.createConfigService(properties); final String content = "test" + System.currentTimeMillis(); + CountDownLatch latch = new CountDownLatch(1); iconfig.addListener(dataId, group, new AbstractConfigChangeListener() { @Override public void receiveConfigChange(ConfigChangeEvent event) { diff --git a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/LdapAuth_ITCase.java b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/LdapAuthCoreITCase.java similarity index 87% rename from test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/LdapAuth_ITCase.java rename to test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/LdapAuthCoreITCase.java index ee76df54c0a..2d73cf342a3 100644 --- a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/LdapAuth_ITCase.java +++ b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/LdapAuthCoreITCase.java @@ -33,17 +33,16 @@ import java.net.URL; import java.util.concurrent.TimeUnit; - +@SuppressWarnings("checkstyle:AbbreviationAsWordInName") @Suite -@SelectClasses({LdapAuth_ITCase.NonTlsTest.class, LdapAuth_ITCase.TlsTest.class}) -class LdapAuth_ITCase { +@SelectClasses({LdapAuthCoreITCase.NonTlsTest.class, LdapAuthCoreITCase.TlsTest.class}) +class LdapAuthCoreITCase { @Test void empty() { - } - abstract class LdapBase extends AuthBase { + abstract static class LdapBase extends AuthBase { @LocalServerPort private int port; @@ -55,7 +54,8 @@ abstract class LdapBase extends AuthBase { @BeforeEach void init() throws Exception { - Mockito.when(ldapTemplate.authenticate("", "(" + filterPrefix + "=" + "karson" + ")", "karson")).thenReturn(true); + Mockito.when(ldapTemplate.authenticate("", "(" + filterPrefix + "=" + "karson" + ")", "karson")) + .thenReturn(true); AuthConfigs.setCachingEnabled(false); TimeUnit.SECONDS.sleep(5L); String url = String.format("http://localhost:%d/", port); @@ -78,7 +78,8 @@ void testLdapAuth() throws Exception { @Nested @DirtiesContext - @SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos", "nacos.core.auth.system.type=ldap", + @SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos", + "nacos.core.auth.system.type=ldap", "nacos.core.auth.ldap.url=ldaps://localhost:636"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) class TlsTest extends LdapBase { @@ -87,6 +88,4 @@ void testLdapAuth() throws Exception { super.login("karson", "karson"); } } - - } diff --git a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuth_ITCase.java b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuthCoreITCase.java similarity index 91% rename from test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuth_ITCase.java rename to test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuthCoreITCase.java index 913ad19c776..6854bb882ae 100644 --- a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuth_ITCase.java +++ b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuthCoreITCase.java @@ -38,13 +38,16 @@ import static org.junit.jupiter.api.Assertions.fail; /** + * Integration tests for Nacos naming service with various authentication scenarios. + * * @author nkorange * @since 1.2.0 */ +@SuppressWarnings("checkstyle:AbbreviationAsWordInName") @ExtendWith(SpringExtension.class) @SpringBootTest(classes = Nacos.class, properties = { "server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -class NamingAuth_ITCase extends AuthBase { +class NamingAuthCoreITCase extends AuthBase { @LocalServerPort private int port; @@ -71,16 +74,16 @@ void writeWithReadPermission() throws Exception { try { namingService.registerInstance("test.1", "1.2.3.4", 80); fail(); - } catch (NacosException ne) { - NacosException cause = (NacosException) ne.getCause(); + } catch (NacosException e) { + NacosException cause = (NacosException) e.getCause(); assertEquals(HttpStatus.SC_FORBIDDEN, cause.getErrCode()); } try { namingService.deregisterInstance("test.1", "1.2.3.4", 80); fail(); - } catch (NacosException ne) { - NacosException cause = (NacosException) ne.getCause(); + } catch (NacosException e) { + NacosException cause = (NacosException) e.getCause(); assertEquals(HttpStatus.SC_FORBIDDEN, cause.getErrCode()); } namingService.shutDown(); @@ -132,8 +135,8 @@ void readWithWritePermission() throws Exception { try { namingService.getAllInstances("test.1"); fail(); - } catch (NacosException ne) { - NacosException cause = (NacosException) ne.getCause(); + } catch (NacosException e) { + NacosException cause = (NacosException) e.getCause(); assertEquals(HttpStatus.SC_FORBIDDEN, cause.getErrCode()); } diff --git a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/Permission_ITCase.java b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/PermissionCoreITCase.java similarity index 84% rename from test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/Permission_ITCase.java rename to test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/PermissionCoreITCase.java index af52a17472e..436a99cf8c8 100644 --- a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/Permission_ITCase.java +++ b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/PermissionCoreITCase.java @@ -42,14 +42,17 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** + * Integration tests for Nacos permission management with various scenarios. + * * @author nkorange * @since 1.2.0 */ //todo fix this test case +@SuppressWarnings("checkstyle:AbbreviationAsWordInName") @ExtendWith(SpringExtension.class) @SpringBootTest(classes = Nacos.class, properties = { "server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -class Permission_ITCase extends HttpClient4Test { +class PermissionCoreITCase extends HttpClient4Test { @LocalServerPort private int port; @@ -68,29 +71,31 @@ void destroy() { // Delete permission: ResponseEntity response = request("/nacos/v1/auth/permissions", - Params.newParams().appendParam("role", "role1").appendParam("resource", "public:*:*").appendParam("action", "rw") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", "role1").appendParam("resource", "public:*:*") + .appendParam("action", "rw").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete permission: response = request("/nacos/v1/auth/permissions", - Params.newParams().appendParam("role", "role1").appendParam("resource", "test1:*:*").appendParam("action", "r") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", "role1").appendParam("resource", "test1:*:*") + .appendParam("action", "r").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete role: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", "role1").appendParam("username", "username3").appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", "role1").appendParam("username", "username3") + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete a user: response = request("/nacos/v1/auth/users", - Params.newParams().appendParam("username", "username3").appendParam("accessToken", accessToken).done(), String.class, - HttpMethod.DELETE); + Params.newParams().appendParam("username", "username3").appendParam("accessToken", accessToken).done(), + String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); } @@ -99,7 +104,8 @@ void destroy() { void login() { ResponseEntity response = request("/nacos/v1/auth/users/login", - Params.newParams().appendParam("username", "nacos").appendParam("password", "nacos").done(), String.class, HttpMethod.POST); + Params.newParams().appendParam("username", "nacos").appendParam("password", "nacos").done(), + String.class, HttpMethod.POST); assertTrue(response.getStatusCode().is2xxSuccessful()); JsonNode json = JacksonUtils.toObj(response.getBody()); @@ -121,22 +127,24 @@ void createDeleteQueryPermission() { // Create role: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", "role1").appendParam("username", "username3").appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.POST); + Params.newParams().appendParam("role", "role1").appendParam("username", "username3") + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); assertTrue(response.getStatusCode().is2xxSuccessful()); // Create permission: response = request("/nacos/v1/auth/permissions", - Params.newParams().appendParam("role", "role1").appendParam("resource", "public:*:*").appendParam("action", "rw") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); + Params.newParams().appendParam("role", "role1").appendParam("resource", "public:*:*") + .appendParam("action", "rw").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.POST); assertTrue(response.getStatusCode().is2xxSuccessful()); // Create another permission: response = request("/nacos/v1/auth/permissions", - Params.newParams().appendParam("role", "role1").appendParam("resource", "test1:*:*").appendParam("action", "r") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); + Params.newParams().appendParam("role", "role1").appendParam("resource", "test1:*:*") + .appendParam("action", "r").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.POST); assertTrue(response.getStatusCode().is2xxSuccessful()); @@ -154,7 +162,8 @@ void createDeleteQueryPermission() { assertNotNull(permissionPage); assertNotNull(permissionPage.getPageItems()); - boolean found1 = false, found2 = false; + boolean found1 = false; + boolean found2 = false; for (Permission permission : permissionPage.getPageItems()) { if (permission.getResource().equals("public:*:*") && permission.getAction().equals("rw")) { found1 = true; @@ -171,8 +180,9 @@ void createDeleteQueryPermission() { // Delete permission: response = request("/nacos/v1/auth/permissions", - Params.newParams().appendParam("role", "role1").appendParam("resource", "public:*:*").appendParam("action", "rw") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", "role1").appendParam("resource", "public:*:*") + .appendParam("action", "rw").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); @@ -205,8 +215,9 @@ void createDeleteQueryPermission() { // Delete permission: response = request("/nacos/v1/auth/permissions", - Params.newParams().appendParam("role", "role1").appendParam("resource", "test1:*:*").appendParam("action", "r") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", "role1").appendParam("resource", "test1:*:*") + .appendParam("action", "r").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); diff --git a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/Role_ITCase.java b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/RoleCoreITCase.java similarity index 82% rename from test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/Role_ITCase.java rename to test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/RoleCoreITCase.java index 83cee5c8d25..8be22fc02e8 100644 --- a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/Role_ITCase.java +++ b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/RoleCoreITCase.java @@ -41,13 +41,16 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** + * Integration tests for Nacos role management, including role creation, deletion, and querying. + * * @author nkorange * @since 1.2.0 */ +@SuppressWarnings("checkstyle:AbbreviationAsWordInName") @SpringBootTest(classes = Nacos.class, properties = { "server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @Disabled("todo fix this test case") -class Role_ITCase extends HttpClient4Test { +class RoleCoreITCase extends HttpClient4Test { @LocalServerPort private int port; @@ -66,22 +69,22 @@ void destroy() { // Delete role: ResponseEntity response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", "role1").appendParam("username", "username2").appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", "role1").appendParam("username", "username2") + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete role: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", "role2").appendParam("username", "username2").appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", "role2").appendParam("username", "username2") + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete a user: response = request("/nacos/v1/auth/users", - Params.newParams().appendParam("username", "username2").appendParam("accessToken", accessToken).done(), String.class, - HttpMethod.DELETE); + Params.newParams().appendParam("username", "username2").appendParam("accessToken", accessToken).done(), + String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); } @@ -90,7 +93,8 @@ void destroy() { void login() { ResponseEntity response = request("/nacos/v1/auth/users/login", - Params.newParams().appendParam("username", "nacos").appendParam("password", "nacos").done(), String.class, HttpMethod.POST); + Params.newParams().appendParam("username", "nacos").appendParam("password", "nacos").done(), + String.class, HttpMethod.POST); assertTrue(response.getStatusCode().is2xxSuccessful()); JsonNode json = JacksonUtils.toObj(response.getBody()); @@ -112,15 +116,16 @@ void createDeleteQueryRole() { // Create a role: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", "role1").appendParam("username", "username2").appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.POST); + Params.newParams().appendParam("role", "role1").appendParam("username", "username2") + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); assertTrue(response.getStatusCode().is2xxSuccessful()); // Query role of user: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1").appendParam("pageSize", "10") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.GET); + Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1") + .appendParam("pageSize", "10").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.GET); assertTrue(response.getStatusCode().is2xxSuccessful()); @@ -140,15 +145,16 @@ void createDeleteQueryRole() { // Add second role to user: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", "role2").appendParam("username", "username2").appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.POST); + Params.newParams().appendParam("role", "role2").appendParam("username", "username2") + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); assertTrue(response.getStatusCode().is2xxSuccessful()); // Query roles of user: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1").appendParam("pageSize", "10") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.GET); + Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1") + .appendParam("pageSize", "10").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.GET); assertTrue(response.getStatusCode().is2xxSuccessful()); @@ -175,15 +181,16 @@ void createDeleteQueryRole() { // Delete role: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", "role2").appendParam("username", "username2").appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", "role2").appendParam("username", "username2") + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Query roles of user: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1").appendParam("pageSize", "10") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.GET); + Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1") + .appendParam("pageSize", "10").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.GET); assertTrue(response.getStatusCode().is2xxSuccessful()); @@ -207,15 +214,16 @@ void createDeleteQueryRole() { // Delete role: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("role", "role1").appendParam("username", "username2").appendParam("accessToken", accessToken) - .done(), String.class, HttpMethod.DELETE); + Params.newParams().appendParam("role", "role1").appendParam("username", "username2") + .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Query roles of user: response = request("/nacos/v1/auth/roles", - Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1").appendParam("pageSize", "10") - .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.GET); + Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1") + .appendParam("pageSize", "10").appendParam("accessToken", accessToken).done(), String.class, + HttpMethod.GET); assertTrue(response.getStatusCode().is2xxSuccessful()); diff --git a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/User_ITCase.java b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/UserCoreITCase.java similarity index 92% rename from test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/User_ITCase.java rename to test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/UserCoreITCase.java index 7cc53226524..7bb4963bc18 100644 --- a/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/User_ITCase.java +++ b/test/core-test/src/test/java/com/alibaba/nacos/test/core/auth/UserCoreITCase.java @@ -45,13 +45,16 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** + * Integration tests for user management in Nacos, including user creation, deletion, updating, and permissions. + * * @author nkorange * @since 1.2.0 */ +@SuppressWarnings("checkstyle:AbbreviationAsWordInName") @ExtendWith(SpringExtension.class) @SpringBootTest(classes = Nacos.class, properties = { "server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -class User_ITCase extends HttpClient4Test { +class UserCoreITCase extends HttpClient4Test { @LocalServerPort private int port; @@ -70,25 +73,23 @@ void destroy() { // Delete a user: ResponseEntity response = request("/nacos/v1/auth/users", - Params.newParams().appendParam("username", "username1").appendParam("accessToken", accessToken).done(), String.class, - HttpMethod.DELETE); + Params.newParams().appendParam("username", "username1").appendParam("accessToken", accessToken).done(), + String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); // Delete a user: request("/nacos/v1/auth/users", - Params.newParams().appendParam("username", "username2").appendParam("accessToken", accessToken).done(), String.class, - HttpMethod.DELETE); + Params.newParams().appendParam("username", "username2").appendParam("accessToken", accessToken).done(), + String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); System.setProperty("nacos.core.auth.enabled", "false"); } - @Test void login() { - ResponseEntity response = login("nacos", "nacos"); assertTrue(response.getStatusCode().is2xxSuccessful()); JsonNode json = JacksonUtils.toObj(response.getBody()); @@ -98,8 +99,8 @@ void login() { private ResponseEntity login(String username, String password) { return request("/nacos/v1/auth/users/login", - Params.newParams().appendParam("username", username).appendParam("password", password).done(), String.class, - HttpMethod.POST); + Params.newParams().appendParam("username", username).appendParam("password", password).done(), + String.class, HttpMethod.POST); } @Test @@ -130,7 +131,8 @@ void createUpdateDeleteUser() { boolean found = false; for (User user : userPage.getPageItems()) { - if ("username1".equals(user.getUsername()) && PasswordEncoderUtil.matches("password1", user.getPassword())) { + if ("username1".equals(user.getUsername()) && PasswordEncoderUtil.matches("password1", + user.getPassword())) { found = true; break; } @@ -158,7 +160,8 @@ void createUpdateDeleteUser() { found = false; for (User user : userPage.getPageItems()) { - if ("username1".equals(user.getUsername()) && PasswordEncoderUtil.matches("password2", user.getPassword())) { + if ("username1".equals(user.getUsername()) && PasswordEncoderUtil.matches("password2", + user.getPassword())) { found = true; break; } @@ -167,8 +170,8 @@ void createUpdateDeleteUser() { // Delete a user: response = request("/nacos/v1/auth/users", - Params.newParams().appendParam("username", "username1").appendParam("accessToken", accessToken).done(), String.class, - HttpMethod.DELETE); + Params.newParams().appendParam("username", "username1").appendParam("accessToken", accessToken).done(), + String.class, HttpMethod.DELETE); assertTrue(response.getStatusCode().is2xxSuccessful()); @@ -216,11 +219,9 @@ void updateUserWithPermission() { // user login response = login("username1", "password1"); - String user1AccessToken = JacksonUtils.toObj(response.getBody()).get("accessToken").textValue(); assertTrue(response.getStatusCode().is2xxSuccessful()); response = login("username2", "password2"); - String user2AccessToken = JacksonUtils.toObj(response.getBody()).get("accessToken").textValue(); assertTrue(response.getStatusCode().is2xxSuccessful()); // update by admin @@ -230,12 +231,14 @@ void updateUserWithPermission() { assertTrue(response.getStatusCode().is2xxSuccessful()); // update by same user + String user1AccessToken = JacksonUtils.toObj(response.getBody()).get("accessToken").textValue(); response = request("/nacos/v1/auth/users", Params.newParams().appendParam("username", "username1").appendParam("newPassword", "password4") .appendParam("accessToken", user1AccessToken).done(), String.class, HttpMethod.PUT); assertTrue(response.getStatusCode().is2xxSuccessful()); // update by another user + String user2AccessToken = JacksonUtils.toObj(response.getBody()).get("accessToken").textValue(); response = request("/nacos/v1/auth/users", Params.newParams().appendParam("username", "username1").appendParam("newPassword", "password5") .appendParam("accessToken", user2AccessToken).done(), String.class, HttpMethod.PUT);