Browse Source

Merge pull request #16327 from thingsboard/fix/api-key-ws-customer-attrs-test

Authenticate the customer WS session with a customer API key in ApiKeyWebSocketApiTest
pull/16328/head
Viacheslav Klimov 2 months ago
committed by GitHub
parent
commit
06c0fe7517
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 54
      application/src/test/java/org/thingsboard/server/controller/ApiKeyWebSocketApiTest.java
  2. 2
      application/src/test/java/org/thingsboard/server/controller/WebSocketApiTest.java

54
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<TbTestWebSocketClient> 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());
}
}
}

2
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<AttributeKvEntry> attrData) throws InterruptedException {
protected void sendAttributes(Device device, TbAttributeSubscriptionScope scope, List<AttributeKvEntry> attrData) throws InterruptedException {
sendAttributes(device.getTenantId(), device.getId(), scope, attrData);
}

Loading…
Cancel
Save