From 42bb47e625b413d05752e52b798de5f09e641d12 Mon Sep 17 00:00:00 2001 From: Sergey Matvienko Date: Tue, 27 Oct 2026 20:01:48 +0100 Subject: [PATCH] Verify exact API key cache eviction and call count on rotation --- .../dao/pat/ApiKeyCacheEvictionTest.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 dao/src/test/java/org/thingsboard/server/dao/pat/ApiKeyCacheEvictionTest.java diff --git a/dao/src/test/java/org/thingsboard/server/dao/pat/ApiKeyCacheEvictionTest.java b/dao/src/test/java/org/thingsboard/server/dao/pat/ApiKeyCacheEvictionTest.java new file mode 100644 index 0000000000..70e95f31f6 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/pat/ApiKeyCacheEvictionTest.java @@ -0,0 +1,76 @@ +// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors +// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1 +package org.thingsboard.server.dao.pat; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; +import org.thingsboard.server.cache.TbTransactionalCache; +import org.thingsboard.server.common.data.User; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.id.UserId; +import org.thingsboard.server.common.data.pat.ApiKey; +import org.thingsboard.server.common.data.pat.ApiKeyInfo; +import org.thingsboard.server.common.data.security.Authority; +import org.thingsboard.server.dao.service.AbstractServiceTest; +import org.thingsboard.server.dao.service.DaoSqlTest; +import org.thingsboard.server.dao.user.UserService; + +/** + * Verifies the exact cache interactions of {@link ApiKeyService} rotation: which {@link ApiKeyCacheKey}s are evicted + * and how many times. Lives in this package because {@link ApiKeyCacheKey} is package-private. + */ +@DaoSqlTest +public class ApiKeyCacheEvictionTest extends AbstractServiceTest { + + @Autowired + ApiKeyService apiKeyService; + + @Autowired + UserService userService; + + @MockitoSpyBean + TbTransactionalCache apiKeyCache; + + private UserId userId; + + @Before + public void before() { + User tenantAdmin = new User(); + tenantAdmin.setAuthority(Authority.TENANT_ADMIN); + tenantAdmin.setTenantId(tenantId); + tenantAdmin.setEmail("tenant-api-key-cache@thingsboard.org"); + User user = userService.saveUser(TenantId.SYS_TENANT_ID, tenantAdmin); + userId = user.getId(); + } + + @After + public void after() { + apiKeyService.deleteByTenantId(tenantId); + User user = userService.findUserById(tenantId, userId); + userService.deleteUser(tenantId, user); + } + + @Test + public void testRotateInternalApiKey_evictsOldValueExactlyOnce() { + ApiKeyInfo apiKeyInfo = new ApiKeyInfo(); + apiKeyInfo.setTenantId(TenantId.SYS_TENANT_ID); + apiKeyInfo.setUserId(userId); + apiKeyInfo.setDescription("Internal key for rotation eviction counting"); + apiKeyInfo.setEnabled(true); + apiKeyInfo.setInternal(true); + ApiKey savedApiKey = apiKeyService.saveApiKey(TenantId.SYS_TENANT_ID, apiKeyInfo); + String originalValue = savedApiKey.getValue(); + Mockito.clearInvocations(apiKeyCache); + + apiKeyService.rotateInternalApiKey(TenantId.SYS_TENANT_ID, savedApiKey); + + Mockito.verify(apiKeyCache).evict(ApiKeyCacheKey.of(originalValue)); + Mockito.verify(apiKeyCache, Mockito.times(1)).evict(Mockito.any(ApiKeyCacheKey.class)); + } + +}