+63
@@ -0,0 +1,63 @@
|
||||
package com.nanri.aiimage.modules.ziniao.service;
|
||||
|
||||
import com.nanri.aiimage.config.ZiniaoProperties;
|
||||
import com.nanri.aiimage.modules.ziniao.client.ZiniaoClient;
|
||||
import com.nanri.aiimage.modules.ziniao.memory.service.ZiniaoTransientCacheService;
|
||||
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoStaffItemVo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class ZiniaoAuthServiceTest {
|
||||
|
||||
@Test
|
||||
void invalidUserCacheSkipsUpstreamStoreLookup() {
|
||||
ZiniaoClient client = mock(ZiniaoClient.class);
|
||||
ZiniaoTransientCacheService cache = mock(ZiniaoTransientCacheService.class);
|
||||
ZiniaoAuthService service = service(client, cache);
|
||||
when(cache.get(eq("INVALID_USER_STORES"), anyString(), eq(Boolean.class)))
|
||||
.thenReturn(Optional.of(Boolean.TRUE));
|
||||
|
||||
assertTrue(service.getOrLoadUserStoresForIndex("api-key", 12L, 34L).isEmpty());
|
||||
|
||||
verify(client, never()).listUserStores(anyString(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void markingInvalidUserWritesNegativeCacheAndRemovesStaleStaffEntry() {
|
||||
ZiniaoTransientCacheService cache = mock(ZiniaoTransientCacheService.class);
|
||||
ZiniaoAuthService service = service(mock(ZiniaoClient.class), cache);
|
||||
ZiniaoStaffItemVo invalid = new ZiniaoStaffItemVo();
|
||||
invalid.setUserId(34L);
|
||||
ZiniaoStaffItemVo valid = new ZiniaoStaffItemVo();
|
||||
valid.setUserId(35L);
|
||||
when(cache.getList(eq("STAFF_LIST"), anyString(), eq(ZiniaoStaffItemVo.class)))
|
||||
.thenReturn(Optional.of(List.of(invalid, valid)));
|
||||
|
||||
service.evictInvalidUserForIndex("api-key", 12L, 34L);
|
||||
|
||||
verify(cache).put(eq("INVALID_USER_STORES"), anyString(), eq(Boolean.TRUE), eq(Duration.ofMinutes(30)));
|
||||
verify(cache).put(eq("STAFF_LIST"), anyString(), eq(List.of(valid)), eq(Duration.ofMinutes(30)));
|
||||
verify(cache).delete(eq("USER_STORES"), anyString());
|
||||
}
|
||||
|
||||
private ZiniaoAuthService service(ZiniaoClient client, ZiniaoTransientCacheService cache) {
|
||||
return new ZiniaoAuthService(
|
||||
new ZiniaoProperties(),
|
||||
client,
|
||||
mock(ZiniaoSessionCacheService.class),
|
||||
cache,
|
||||
mock(ZiniaoApiKeyProvider.class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user