From 0fe78321ca6cc5365efc9648155afcae3ca69045 Mon Sep 17 00:00:00 2001 From: Viacheslav Klimov Date: Fri, 16 Oct 2026 09:37:04 +0300 Subject: [PATCH] Authenticate the customer WS session with a customer API key in ApiKeyWebSocketApiTest --- .../controller/ApiKeyWebSocketApiTest.java | 54 +++++++++++++------ .../server/controller/WebSocketApiTest.java | 2 +- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/ApiKeyWebSocketApiTest.java b/application/src/test/java/org/thingsboard/server/controller/ApiKeyWebSocketApiTest.java index c5b28b92bb..e974095a30 100644 --- a/application/src/test/java/org/thingsboard/server/controller/ApiKeyWebSocketApiTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/ApiKeyWebSocketApiTest.java @@ -5,18 +5,23 @@ package org.thingsboard.server.controller; import com.fasterxml.jackson.databind.JsonNode; import lombok.extern.slf4j.Slf4j; +import org.assertj.core.api.ThrowingConsumer; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.thingsboard.common.util.JacksonUtil; +import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; +import org.thingsboard.server.common.data.kv.StringDataEntry; import org.thingsboard.server.common.data.pat.ApiKey; import org.thingsboard.server.common.data.pat.ApiKeyInfo; import org.thingsboard.server.dao.service.DaoSqlTest; import org.thingsboard.server.service.subscription.SubscriptionErrorCode; +import org.thingsboard.server.service.subscription.TbAttributeSubscriptionScope; import org.thingsboard.server.service.ws.log.cmd.LogsSubscriptionCmd; import java.net.URI; import java.net.URISyntaxException; +import java.util.List; import java.util.concurrent.TimeUnit; import static org.assertj.core.api.Assertions.assertThat; @@ -51,26 +56,27 @@ public class ApiKeyWebSocketApiTest extends WebSocketApiTest { @Test @Override public void testLogsSubscribe_accessDenied() throws Exception { - // the API key from setUpApiKey is bound to the tenant admin, who can read the device, so reuse won't deny. - // authenticate the WS session with an API key bound to the customer user, who has no READ_TELEMETRY permission. - ApiKeyInfo customerApiKeyInfo = new ApiKeyInfo(); - customerApiKeyInfo.setDescription("WS test customer API key"); - customerApiKeyInfo.setEnabled(true); - customerApiKeyInfo.setUserId(customerUserId); - ApiKey customerApiKey = doPost("/api/apiKey", customerApiKeyInfo, ApiKey.class); - - TbTestWebSocketClient customerWsClient = buildAndConnectWebSocketClientWithApiKey(customerApiKey.getValue()); - try { + withCustomerWsClient(customerWsClient -> { customerWsClient.send(new LogsSubscriptionCmd(1, device.getId().getEntityType().name(), device.getId().getId().toString(), 0)); JsonNode error = JacksonUtil.toJsonNode(customerWsClient.waitForReply(true)); assertThat(error.get("errorCode").asInt()).isEqualTo(SubscriptionErrorCode.ACCESS_DENIED.getCode()); - } finally { - customerWsClient.close(); - loginTenantAdmin(); - doDelete("/api/apiKey/" + customerApiKey.getId()).andExpect(status().isOk()); - } + }); + } + + @Test + @Override + public void testAttributesSubscription_customerUserNotAssignedDevice() throws Exception { + sendAttributes(device, TbAttributeSubscriptionScope.SERVER_SCOPE, List.of( + new BaseAttributeKvEntry(System.currentTimeMillis(), new StringDataEntry("attr", "secret")) + )); + + withCustomerWsClient(customerWsClient -> { + JsonNode update = customerWsClient.subscribeForAttributes(device.getId(), TbAttributeSubscriptionScope.SERVER_SCOPE.name(), List.of("attr")); + assertThat(update.get("errorCode").asInt()).isNotEqualTo(SubscriptionErrorCode.NO_ERROR.getCode()); + assertThat(update.path("data").isEmpty()).isTrue(); + }); } @Test @@ -85,4 +91,22 @@ public class ApiKeyWebSocketApiTest extends WebSocketApiTest { } } + // the API key from setUpApiKey is bound to the tenant admin, so access-denied cases need a key bound to the customer user + private void withCustomerWsClient(ThrowingConsumer action) throws Exception { + ApiKeyInfo customerApiKeyInfo = new ApiKeyInfo(); + customerApiKeyInfo.setDescription("WS test customer API key"); + customerApiKeyInfo.setEnabled(true); + customerApiKeyInfo.setUserId(customerUserId); + ApiKey customerApiKey = doPost("/api/apiKey", customerApiKeyInfo, ApiKey.class); + + TbTestWebSocketClient customerWsClient = buildAndConnectWebSocketClientWithApiKey(customerApiKey.getValue()); + try { + action.accept(customerWsClient); + } finally { + customerWsClient.close(); + loginTenantAdmin(); + doDelete("/api/apiKey/" + customerApiKey.getId()).andExpect(status().isOk()); + } + } + } diff --git a/application/src/test/java/org/thingsboard/server/controller/WebSocketApiTest.java b/application/src/test/java/org/thingsboard/server/controller/WebSocketApiTest.java index 8c10290d8b..82ebbd7615 100644 --- a/application/src/test/java/org/thingsboard/server/controller/WebSocketApiTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/WebSocketApiTest.java @@ -1160,7 +1160,7 @@ public class WebSocketApiTest extends AbstractControllerTest { assertThat(latch.await(TIMEOUT, TimeUnit.SECONDS)).as("await sendTelemetry callback"); } - private void sendAttributes(Device device, TbAttributeSubscriptionScope scope, List attrData) throws InterruptedException { + protected void sendAttributes(Device device, TbAttributeSubscriptionScope scope, List attrData) throws InterruptedException { sendAttributes(device.getTenantId(), device.getId(), scope, attrData); }