[IT]Adjust core module auth integration tests to meet checkstyle requirements. (#12368)

This commit is contained in:
阿魁 2024-07-18 09:30:26 +08:00 committed by GitHub
parent 6c05ad5497
commit 9d0a48d54f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 177 additions and 138 deletions

View File

@ -31,8 +31,9 @@ import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Base class for authentication tests in Nacos.`
*
* @author nkorange * @author nkorange
* @since 1.2.0 * @since 1.2.0
*/ */
@ -62,10 +63,18 @@ public class AuthBase extends HttpClient4Test {
protected String namespace1 = "namespace1"; 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) { public String login(String username, String password) {
ResponseEntity<String> response = request("/nacos/v1/auth/users/login", ResponseEntity<String> response = request("/nacos/v1/auth/users/login",
Params.newParams().appendParam("username", username).appendParam("password", password).done(), String.class, Params.newParams().appendParam("username", username).appendParam("password", password).done(),
HttpMethod.POST); String.class, HttpMethod.POST);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
JsonNode json = JacksonUtils.toObj(response.getBody()); JsonNode json = JacksonUtils.toObj(response.getBody());
@ -93,59 +102,64 @@ public class AuthBase extends HttpClient4Test {
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Create a user: // Create a user:
response = request("/nacos/v1/auth/users", Params.newParams().appendParam("username", username2).appendParam("password", password2) response = request("/nacos/v1/auth/users",
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); Params.newParams().appendParam("username", username2).appendParam("password", password2)
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST);
System.out.println(response); System.out.println(response);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Create a user: // Create a user:
response = request("/nacos/v1/auth/users", Params.newParams().appendParam("username", username3).appendParam("password", password3) response = request("/nacos/v1/auth/users",
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); Params.newParams().appendParam("username", username3).appendParam("password", password3)
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST);
System.out.println(response); System.out.println(response);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Create a role: // Create a role:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("role", role1).appendParam("username", username1).appendParam("accessToken", accessToken) Params.newParams().appendParam("role", role1).appendParam("username", username1)
.done(), String.class, HttpMethod.POST); .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST);
System.out.println(response); System.out.println(response);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Create a role: // Create a role:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("role", role2).appendParam("username", username2).appendParam("accessToken", accessToken) Params.newParams().appendParam("role", role2).appendParam("username", username2)
.done(), String.class, HttpMethod.POST); .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST);
System.out.println(response); System.out.println(response);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Create a role: // Create a role:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("role", role3).appendParam("username", username3).appendParam("accessToken", accessToken) Params.newParams().appendParam("role", role3).appendParam("username", username3)
.done(), String.class, HttpMethod.POST); .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST);
System.out.println(response); System.out.println(response);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Add read permission of namespace1 to role1: // Add read permission of namespace1 to role1:
response = request("/nacos/v1/auth/permissions", response = request("/nacos/v1/auth/permissions",
Params.newParams().appendParam("role", role1).appendParam("resource", namespace1 + ":*:*").appendParam("action", "r") Params.newParams().appendParam("role", role1).appendParam("resource", namespace1 + ":*:*")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); .appendParam("action", "r").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.POST);
System.out.println(response); System.out.println(response);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Add write permission of namespace1 to role2: // Add write permission of namespace1 to role2:
response = request("/nacos/v1/auth/permissions", response = request("/nacos/v1/auth/permissions",
Params.newParams().appendParam("role", role2).appendParam("resource", namespace1 + ":*:*").appendParam("action", "w") Params.newParams().appendParam("role", role2).appendParam("resource", namespace1 + ":*:*")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); .appendParam("action", "w").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.POST);
System.out.println(response); System.out.println(response);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Add read/write permission of namespace1 to role3: // Add read/write permission of namespace1 to role3:
response = request("/nacos/v1/auth/permissions", response = request("/nacos/v1/auth/permissions",
Params.newParams().appendParam("role", role3).appendParam("resource", namespace1 + ":*:*").appendParam("action", "rw") Params.newParams().appendParam("role", role3).appendParam("resource", namespace1 + ":*:*")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); .appendParam("action", "rw").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.POST);
System.out.println(response); System.out.println(response);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
@ -159,61 +173,67 @@ public class AuthBase extends HttpClient4Test {
// Delete permission: // Delete permission:
ResponseEntity<String> response = request("/nacos/v1/auth/permissions", ResponseEntity<String> response = request("/nacos/v1/auth/permissions",
Params.newParams().appendParam("role", role1).appendParam("resource", namespace1 + ":*:*").appendParam("action", "r") Params.newParams().appendParam("role", role1).appendParam("resource", namespace1 + ":*:*")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); .appendParam("action", "r").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Delete permission: // Delete permission:
response = request("/nacos/v1/auth/permissions", response = request("/nacos/v1/auth/permissions",
Params.newParams().appendParam("role", role2).appendParam("resource", namespace1 + ":*:*").appendParam("action", "w") Params.newParams().appendParam("role", role2).appendParam("resource", namespace1 + ":*:*")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); .appendParam("action", "w").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Delete permission: // Delete permission:
response = request("/nacos/v1/auth/permissions", response = request("/nacos/v1/auth/permissions",
Params.newParams().appendParam("role", role3).appendParam("resource", namespace1 + ":*:*").appendParam("action", "rw") 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); .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Delete a role: // Delete a role:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("role", role1).appendParam("username", username1).appendParam("accessToken", accessToken) Params.newParams().appendParam("role", role2).appendParam("username", username2)
.done(), String.class, HttpMethod.DELETE); .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Delete a role: // Delete a role:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("role", role2).appendParam("username", username2).appendParam("accessToken", accessToken) Params.newParams().appendParam("role", role3).appendParam("username", username3)
.done(), String.class, HttpMethod.DELETE); .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);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Delete a user: // Delete a user:
response = request("/nacos/v1/auth/users", Params.newParams().appendParam("username", username1).appendParam("password", password1) response = request("/nacos/v1/auth/users",
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); Params.newParams().appendParam("username", username1).appendParam("password", password1)
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Delete a user: // Delete a user:
response = request("/nacos/v1/auth/users", Params.newParams().appendParam("username", username2).appendParam("password", password2) response = request("/nacos/v1/auth/users",
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); Params.newParams().appendParam("username", username2).appendParam("password", password2)
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Delete a user: // Delete a user:
response = request("/nacos/v1/auth/users", Params.newParams().appendParam("username", username3).appendParam("password", password3) response = request("/nacos/v1/auth/users",
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); Params.newParams().appendParam("username", username3).appendParam("password", password3)
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());

View File

@ -33,7 +33,6 @@ import org.springframework.boot.web.server.LocalServerPort;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@ -41,12 +40,15 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
/** /**
* Integration tests for Nacos configuration with authentication.
*
* @author nkorange * @author nkorange
* @since 1.2.0 * @since 1.2.0
*/ */
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
@SpringBootTest(classes = Nacos.class, properties = { @SpringBootTest(classes = Nacos.class, properties = {
"server.servlet.contextPath=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) "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; public static final long TIME_OUT = 2000;
@ -55,15 +57,18 @@ public class ConfigAuth_ITCase extends AuthBase {
@LocalServerPort @LocalServerPort
private int port; private int port;
private String dataId = "yanlin"; private final String dataId = "yanlin";
private String group = "yanlin"; private final String group = "yanlin";
@BeforeEach @BeforeEach
void init() throws Exception { void init() throws Exception {
super.init(port); super.init(port);
} }
/**
* Cleans up resources after each test execution.
*/
@AfterEach @AfterEach
public void destroy() { public void destroy() {
super.destroy(); super.destroy();
@ -72,11 +77,10 @@ public class ConfigAuth_ITCase extends AuthBase {
iconfig.shutDown(); iconfig.shutDown();
} }
} catch (NacosException ex) { } catch (NacosException ex) {
// Ignored exception during shutdown
} }
} }
@Test @Test
void writeWithReadPermission() throws Exception { void writeWithReadPermission() throws Exception {
@ -93,10 +97,6 @@ public class ConfigAuth_ITCase extends AuthBase {
@Test @Test
void readWithReadPermission() throws Exception { void readWithReadPermission() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
AtomicInteger ai = new AtomicInteger(0);
properties.put(PropertyKeyConst.USERNAME, username1); properties.put(PropertyKeyConst.USERNAME, username1);
properties.put(PropertyKeyConst.PASSWORD, password1); properties.put(PropertyKeyConst.PASSWORD, password1);
iconfig = NacosFactory.createConfigService(properties); iconfig = NacosFactory.createConfigService(properties);
@ -104,6 +104,7 @@ public class ConfigAuth_ITCase extends AuthBase {
final String content = "test" + System.currentTimeMillis(); final String content = "test" + System.currentTimeMillis();
System.out.println(content); System.out.println(content);
CountDownLatch latch = new CountDownLatch(1);
iconfig.addListener(dataId, group, new AbstractConfigChangeListener() { iconfig.addListener(dataId, group, new AbstractConfigChangeListener() {
@Override @Override
public void receiveConfigChange(ConfigChangeEvent event) { public void receiveConfigChange(ConfigChangeEvent event) {
@ -151,9 +152,6 @@ public class ConfigAuth_ITCase extends AuthBase {
@Test @Test
void readWithWritePermission() throws Exception { void readWithWritePermission() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
properties.put(PropertyKeyConst.NAMESPACE, namespace1); properties.put(PropertyKeyConst.NAMESPACE, namespace1);
properties.put(PropertyKeyConst.USERNAME, username2); properties.put(PropertyKeyConst.USERNAME, username2);
properties.put(PropertyKeyConst.PASSWORD, password2); properties.put(PropertyKeyConst.PASSWORD, password2);
@ -161,6 +159,7 @@ public class ConfigAuth_ITCase extends AuthBase {
final String content = "test" + System.currentTimeMillis(); final String content = "test" + System.currentTimeMillis();
CountDownLatch latch = new CountDownLatch(1);
iconfig.addListener(dataId, group, new AbstractConfigChangeListener() { iconfig.addListener(dataId, group, new AbstractConfigChangeListener() {
@Override @Override
public void receiveConfigChange(ConfigChangeEvent event) { public void receiveConfigChange(ConfigChangeEvent event) {
@ -182,8 +181,8 @@ public class ConfigAuth_ITCase extends AuthBase {
try { try {
iconfig.getConfig(dataId, group, TIME_OUT); iconfig.getConfig(dataId, group, TIME_OUT);
fail(); fail();
} catch (NacosException ne) { } catch (NacosException e) {
assertEquals(HttpStatus.SC_FORBIDDEN, ne.getErrCode()); assertEquals(HttpStatus.SC_FORBIDDEN, e.getErrCode());
} }
latch.await(5L, TimeUnit.SECONDS); latch.await(5L, TimeUnit.SECONDS);
@ -191,19 +190,15 @@ public class ConfigAuth_ITCase extends AuthBase {
assertTrue(latch.getCount() > 0); assertTrue(latch.getCount() > 0);
} }
@Test @Test
void ReadWriteWithFullPermission() throws Exception { void readWriteWithFullPermission() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
AtomicInteger ai = new AtomicInteger(0);
properties.put(PropertyKeyConst.USERNAME, username3); properties.put(PropertyKeyConst.USERNAME, username3);
properties.put(PropertyKeyConst.PASSWORD, password3); properties.put(PropertyKeyConst.PASSWORD, password3);
iconfig = NacosFactory.createConfigService(properties); iconfig = NacosFactory.createConfigService(properties);
final String content = "test" + System.currentTimeMillis(); final String content = "test" + System.currentTimeMillis();
CountDownLatch latch = new CountDownLatch(1);
iconfig.addListener(dataId, group, new AbstractConfigChangeListener() { iconfig.addListener(dataId, group, new AbstractConfigChangeListener() {
@Override @Override
public void receiveConfigChange(ConfigChangeEvent event) { public void receiveConfigChange(ConfigChangeEvent event) {

View File

@ -33,17 +33,16 @@ import org.springframework.test.annotation.DirtiesContext;
import java.net.URL; import java.net.URL;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
@Suite @Suite
@SelectClasses({LdapAuth_ITCase.NonTlsTest.class, LdapAuth_ITCase.TlsTest.class}) @SelectClasses({LdapAuthCoreITCase.NonTlsTest.class, LdapAuthCoreITCase.TlsTest.class})
class LdapAuth_ITCase { class LdapAuthCoreITCase {
@Test @Test
void empty() { void empty() {
} }
abstract class LdapBase extends AuthBase { abstract static class LdapBase extends AuthBase {
@LocalServerPort @LocalServerPort
private int port; private int port;
@ -55,7 +54,8 @@ class LdapAuth_ITCase {
@BeforeEach @BeforeEach
void init() throws Exception { 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); AuthConfigs.setCachingEnabled(false);
TimeUnit.SECONDS.sleep(5L); TimeUnit.SECONDS.sleep(5L);
String url = String.format("http://localhost:%d/", port); String url = String.format("http://localhost:%d/", port);
@ -78,7 +78,8 @@ class LdapAuth_ITCase {
@Nested @Nested
@DirtiesContext @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) "nacos.core.auth.ldap.url=ldaps://localhost:636"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class TlsTest extends LdapBase { class TlsTest extends LdapBase {
@ -87,6 +88,4 @@ class LdapAuth_ITCase {
super.login("karson", "karson"); super.login("karson", "karson");
} }
} }
} }

View File

@ -38,13 +38,16 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
/** /**
* Integration tests for Nacos naming service with various authentication scenarios.
*
* @author nkorange * @author nkorange
* @since 1.2.0 * @since 1.2.0
*/ */
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Nacos.class, properties = { @SpringBootTest(classes = Nacos.class, properties = {
"server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) "server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class NamingAuth_ITCase extends AuthBase { class NamingAuthCoreITCase extends AuthBase {
@LocalServerPort @LocalServerPort
private int port; private int port;
@ -71,16 +74,16 @@ class NamingAuth_ITCase extends AuthBase {
try { try {
namingService.registerInstance("test.1", "1.2.3.4", 80); namingService.registerInstance("test.1", "1.2.3.4", 80);
fail(); fail();
} catch (NacosException ne) { } catch (NacosException e) {
NacosException cause = (NacosException) ne.getCause(); NacosException cause = (NacosException) e.getCause();
assertEquals(HttpStatus.SC_FORBIDDEN, cause.getErrCode()); assertEquals(HttpStatus.SC_FORBIDDEN, cause.getErrCode());
} }
try { try {
namingService.deregisterInstance("test.1", "1.2.3.4", 80); namingService.deregisterInstance("test.1", "1.2.3.4", 80);
fail(); fail();
} catch (NacosException ne) { } catch (NacosException e) {
NacosException cause = (NacosException) ne.getCause(); NacosException cause = (NacosException) e.getCause();
assertEquals(HttpStatus.SC_FORBIDDEN, cause.getErrCode()); assertEquals(HttpStatus.SC_FORBIDDEN, cause.getErrCode());
} }
namingService.shutDown(); namingService.shutDown();
@ -132,8 +135,8 @@ class NamingAuth_ITCase extends AuthBase {
try { try {
namingService.getAllInstances("test.1"); namingService.getAllInstances("test.1");
fail(); fail();
} catch (NacosException ne) { } catch (NacosException e) {
NacosException cause = (NacosException) ne.getCause(); NacosException cause = (NacosException) e.getCause();
assertEquals(HttpStatus.SC_FORBIDDEN, cause.getErrCode()); assertEquals(HttpStatus.SC_FORBIDDEN, cause.getErrCode());
} }

View File

@ -42,14 +42,17 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Integration tests for Nacos permission management with various scenarios.
*
* @author nkorange * @author nkorange
* @since 1.2.0 * @since 1.2.0
*/ */
//todo fix this test case //todo fix this test case
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Nacos.class, properties = { @SpringBootTest(classes = Nacos.class, properties = {
"server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) "server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class Permission_ITCase extends HttpClient4Test { class PermissionCoreITCase extends HttpClient4Test {
@LocalServerPort @LocalServerPort
private int port; private int port;
@ -68,29 +71,31 @@ class Permission_ITCase extends HttpClient4Test {
// Delete permission: // Delete permission:
ResponseEntity<String> response = request("/nacos/v1/auth/permissions", ResponseEntity<String> response = request("/nacos/v1/auth/permissions",
Params.newParams().appendParam("role", "role1").appendParam("resource", "public:*:*").appendParam("action", "rw") Params.newParams().appendParam("role", "role1").appendParam("resource", "public:*:*")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); .appendParam("action", "rw").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Delete permission: // Delete permission:
response = request("/nacos/v1/auth/permissions", response = request("/nacos/v1/auth/permissions",
Params.newParams().appendParam("role", "role1").appendParam("resource", "test1:*:*").appendParam("action", "r") Params.newParams().appendParam("role", "role1").appendParam("resource", "test1:*:*")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); .appendParam("action", "r").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Delete role: // Delete role:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("role", "role1").appendParam("username", "username3").appendParam("accessToken", accessToken) Params.newParams().appendParam("role", "role1").appendParam("username", "username3")
.done(), String.class, HttpMethod.DELETE); .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Delete a user: // Delete a user:
response = request("/nacos/v1/auth/users", response = request("/nacos/v1/auth/users",
Params.newParams().appendParam("username", "username3").appendParam("accessToken", accessToken).done(), String.class, Params.newParams().appendParam("username", "username3").appendParam("accessToken", accessToken).done(),
HttpMethod.DELETE); String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
} }
@ -99,7 +104,8 @@ class Permission_ITCase extends HttpClient4Test {
void login() { void login() {
ResponseEntity<String> response = request("/nacos/v1/auth/users/login", ResponseEntity<String> 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()); assertTrue(response.getStatusCode().is2xxSuccessful());
JsonNode json = JacksonUtils.toObj(response.getBody()); JsonNode json = JacksonUtils.toObj(response.getBody());
@ -121,22 +127,24 @@ class Permission_ITCase extends HttpClient4Test {
// Create role: // Create role:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("role", "role1").appendParam("username", "username3").appendParam("accessToken", accessToken) Params.newParams().appendParam("role", "role1").appendParam("username", "username3")
.done(), String.class, HttpMethod.POST); .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Create permission: // Create permission:
response = request("/nacos/v1/auth/permissions", response = request("/nacos/v1/auth/permissions",
Params.newParams().appendParam("role", "role1").appendParam("resource", "public:*:*").appendParam("action", "rw") Params.newParams().appendParam("role", "role1").appendParam("resource", "public:*:*")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); .appendParam("action", "rw").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.POST);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Create another permission: // Create another permission:
response = request("/nacos/v1/auth/permissions", response = request("/nacos/v1/auth/permissions",
Params.newParams().appendParam("role", "role1").appendParam("resource", "test1:*:*").appendParam("action", "r") Params.newParams().appendParam("role", "role1").appendParam("resource", "test1:*:*")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST); .appendParam("action", "r").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.POST);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
@ -154,7 +162,8 @@ class Permission_ITCase extends HttpClient4Test {
assertNotNull(permissionPage); assertNotNull(permissionPage);
assertNotNull(permissionPage.getPageItems()); assertNotNull(permissionPage.getPageItems());
boolean found1 = false, found2 = false; boolean found1 = false;
boolean found2 = false;
for (Permission permission : permissionPage.getPageItems()) { for (Permission permission : permissionPage.getPageItems()) {
if (permission.getResource().equals("public:*:*") && permission.getAction().equals("rw")) { if (permission.getResource().equals("public:*:*") && permission.getAction().equals("rw")) {
found1 = true; found1 = true;
@ -171,8 +180,9 @@ class Permission_ITCase extends HttpClient4Test {
// Delete permission: // Delete permission:
response = request("/nacos/v1/auth/permissions", response = request("/nacos/v1/auth/permissions",
Params.newParams().appendParam("role", "role1").appendParam("resource", "public:*:*").appendParam("action", "rw") Params.newParams().appendParam("role", "role1").appendParam("resource", "public:*:*")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); .appendParam("action", "rw").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
@ -205,8 +215,9 @@ class Permission_ITCase extends HttpClient4Test {
// Delete permission: // Delete permission:
response = request("/nacos/v1/auth/permissions", response = request("/nacos/v1/auth/permissions",
Params.newParams().appendParam("role", "role1").appendParam("resource", "test1:*:*").appendParam("action", "r") Params.newParams().appendParam("role", "role1").appendParam("resource", "test1:*:*")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE); .appendParam("action", "r").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());

View File

@ -41,13 +41,16 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Integration tests for Nacos role management, including role creation, deletion, and querying.
*
* @author nkorange * @author nkorange
* @since 1.2.0 * @since 1.2.0
*/ */
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
@SpringBootTest(classes = Nacos.class, properties = { @SpringBootTest(classes = Nacos.class, properties = {
"server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) "server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@Disabled("todo fix this test case") @Disabled("todo fix this test case")
class Role_ITCase extends HttpClient4Test { class RoleCoreITCase extends HttpClient4Test {
@LocalServerPort @LocalServerPort
private int port; private int port;
@ -66,22 +69,22 @@ class Role_ITCase extends HttpClient4Test {
// Delete role: // Delete role:
ResponseEntity<String> response = request("/nacos/v1/auth/roles", ResponseEntity<String> response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("role", "role1").appendParam("username", "username2").appendParam("accessToken", accessToken) Params.newParams().appendParam("role", "role1").appendParam("username", "username2")
.done(), String.class, HttpMethod.DELETE); .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Delete role: // Delete role:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("role", "role2").appendParam("username", "username2").appendParam("accessToken", accessToken) Params.newParams().appendParam("role", "role2").appendParam("username", "username2")
.done(), String.class, HttpMethod.DELETE); .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Delete a user: // Delete a user:
response = request("/nacos/v1/auth/users", response = request("/nacos/v1/auth/users",
Params.newParams().appendParam("username", "username2").appendParam("accessToken", accessToken).done(), String.class, Params.newParams().appendParam("username", "username2").appendParam("accessToken", accessToken).done(),
HttpMethod.DELETE); String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
} }
@ -90,7 +93,8 @@ class Role_ITCase extends HttpClient4Test {
void login() { void login() {
ResponseEntity<String> response = request("/nacos/v1/auth/users/login", ResponseEntity<String> 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()); assertTrue(response.getStatusCode().is2xxSuccessful());
JsonNode json = JacksonUtils.toObj(response.getBody()); JsonNode json = JacksonUtils.toObj(response.getBody());
@ -112,15 +116,16 @@ class Role_ITCase extends HttpClient4Test {
// Create a role: // Create a role:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("role", "role1").appendParam("username", "username2").appendParam("accessToken", accessToken) Params.newParams().appendParam("role", "role1").appendParam("username", "username2")
.done(), String.class, HttpMethod.POST); .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Query role of user: // Query role of user:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1").appendParam("pageSize", "10") Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.GET); .appendParam("pageSize", "10").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.GET);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
@ -140,15 +145,16 @@ class Role_ITCase extends HttpClient4Test {
// Add second role to user: // Add second role to user:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("role", "role2").appendParam("username", "username2").appendParam("accessToken", accessToken) Params.newParams().appendParam("role", "role2").appendParam("username", "username2")
.done(), String.class, HttpMethod.POST); .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.POST);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Query roles of user: // Query roles of user:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1").appendParam("pageSize", "10") Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.GET); .appendParam("pageSize", "10").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.GET);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
@ -175,15 +181,16 @@ class Role_ITCase extends HttpClient4Test {
// Delete role: // Delete role:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("role", "role2").appendParam("username", "username2").appendParam("accessToken", accessToken) Params.newParams().appendParam("role", "role2").appendParam("username", "username2")
.done(), String.class, HttpMethod.DELETE); .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Query roles of user: // Query roles of user:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1").appendParam("pageSize", "10") Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.GET); .appendParam("pageSize", "10").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.GET);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
@ -207,15 +214,16 @@ class Role_ITCase extends HttpClient4Test {
// Delete role: // Delete role:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("role", "role1").appendParam("username", "username2").appendParam("accessToken", accessToken) Params.newParams().appendParam("role", "role1").appendParam("username", "username2")
.done(), String.class, HttpMethod.DELETE); .appendParam("accessToken", accessToken).done(), String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Query roles of user: // Query roles of user:
response = request("/nacos/v1/auth/roles", response = request("/nacos/v1/auth/roles",
Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1").appendParam("pageSize", "10") Params.newParams().appendParam("username", "username2").appendParam("pageNo", "1")
.appendParam("accessToken", accessToken).done(), String.class, HttpMethod.GET); .appendParam("pageSize", "10").appendParam("accessToken", accessToken).done(), String.class,
HttpMethod.GET);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());

View File

@ -45,13 +45,16 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Integration tests for user management in Nacos, including user creation, deletion, updating, and permissions.
*
* @author nkorange * @author nkorange
* @since 1.2.0 * @since 1.2.0
*/ */
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Nacos.class, properties = { @SpringBootTest(classes = Nacos.class, properties = {
"server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) "server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class User_ITCase extends HttpClient4Test { class UserCoreITCase extends HttpClient4Test {
@LocalServerPort @LocalServerPort
private int port; private int port;
@ -70,25 +73,23 @@ class User_ITCase extends HttpClient4Test {
// Delete a user: // Delete a user:
ResponseEntity<String> response = request("/nacos/v1/auth/users", ResponseEntity<String> response = request("/nacos/v1/auth/users",
Params.newParams().appendParam("username", "username1").appendParam("accessToken", accessToken).done(), String.class, Params.newParams().appendParam("username", "username1").appendParam("accessToken", accessToken).done(),
HttpMethod.DELETE); String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// Delete a user: // Delete a user:
request("/nacos/v1/auth/users", request("/nacos/v1/auth/users",
Params.newParams().appendParam("username", "username2").appendParam("accessToken", accessToken).done(), String.class, Params.newParams().appendParam("username", "username2").appendParam("accessToken", accessToken).done(),
HttpMethod.DELETE); String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
System.setProperty("nacos.core.auth.enabled", "false"); System.setProperty("nacos.core.auth.enabled", "false");
} }
@Test @Test
void login() { void login() {
ResponseEntity<String> response = login("nacos", "nacos"); ResponseEntity<String> response = login("nacos", "nacos");
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
JsonNode json = JacksonUtils.toObj(response.getBody()); JsonNode json = JacksonUtils.toObj(response.getBody());
@ -98,8 +99,8 @@ class User_ITCase extends HttpClient4Test {
private ResponseEntity<String> login(String username, String password) { private ResponseEntity<String> login(String username, String password) {
return request("/nacos/v1/auth/users/login", return request("/nacos/v1/auth/users/login",
Params.newParams().appendParam("username", username).appendParam("password", password).done(), String.class, Params.newParams().appendParam("username", username).appendParam("password", password).done(),
HttpMethod.POST); String.class, HttpMethod.POST);
} }
@Test @Test
@ -130,7 +131,8 @@ class User_ITCase extends HttpClient4Test {
boolean found = false; boolean found = false;
for (User user : userPage.getPageItems()) { 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; found = true;
break; break;
} }
@ -158,7 +160,8 @@ class User_ITCase extends HttpClient4Test {
found = false; found = false;
for (User user : userPage.getPageItems()) { 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; found = true;
break; break;
} }
@ -167,8 +170,8 @@ class User_ITCase extends HttpClient4Test {
// Delete a user: // Delete a user:
response = request("/nacos/v1/auth/users", response = request("/nacos/v1/auth/users",
Params.newParams().appendParam("username", "username1").appendParam("accessToken", accessToken).done(), String.class, Params.newParams().appendParam("username", "username1").appendParam("accessToken", accessToken).done(),
HttpMethod.DELETE); String.class, HttpMethod.DELETE);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
@ -216,11 +219,9 @@ class User_ITCase extends HttpClient4Test {
// user login // user login
response = login("username1", "password1"); response = login("username1", "password1");
String user1AccessToken = JacksonUtils.toObj(response.getBody()).get("accessToken").textValue();
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
response = login("username2", "password2"); response = login("username2", "password2");
String user2AccessToken = JacksonUtils.toObj(response.getBody()).get("accessToken").textValue();
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// update by admin // update by admin
@ -230,12 +231,14 @@ class User_ITCase extends HttpClient4Test {
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// update by same user // update by same user
String user1AccessToken = JacksonUtils.toObj(response.getBody()).get("accessToken").textValue();
response = request("/nacos/v1/auth/users", response = request("/nacos/v1/auth/users",
Params.newParams().appendParam("username", "username1").appendParam("newPassword", "password4") Params.newParams().appendParam("username", "username1").appendParam("newPassword", "password4")
.appendParam("accessToken", user1AccessToken).done(), String.class, HttpMethod.PUT); .appendParam("accessToken", user1AccessToken).done(), String.class, HttpMethod.PUT);
assertTrue(response.getStatusCode().is2xxSuccessful()); assertTrue(response.getStatusCode().is2xxSuccessful());
// update by another user // update by another user
String user2AccessToken = JacksonUtils.toObj(response.getBody()).get("accessToken").textValue();
response = request("/nacos/v1/auth/users", response = request("/nacos/v1/auth/users",
Params.newParams().appendParam("username", "username1").appendParam("newPassword", "password5") Params.newParams().appendParam("username", "username1").appendParam("newPassword", "password5")
.appendParam("accessToken", user2AccessToken).done(), String.class, HttpMethod.PUT); .appendParam("accessToken", user2AccessToken).done(), String.class, HttpMethod.PUT);