feat: package Beijing test deployment with HTTPS and preflight checks
This commit is contained in:
1 parent
ae749a1fe9
commit
c7278c00a6
33 files changed
+1153
-54
No files matched your search
+76
@@ -0,0 +1,76 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
import com.moyi.controlplane.task.AsyncTaskService;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.core.script.RedisScript;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class GatewayDeploymentTest {
|
||||
@Test
|
||||
void redisRateKeysAreEnvironmentScoped() {
|
||||
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
||||
doReturn(0L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
|
||||
UUID keyId = UUID.randomUUID();
|
||||
new GatewayGuard(redis, 60, 300, 4, "moyi:test:").checkRate(keyId, false);
|
||||
verify(redis).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:rate:invoke:" + keyId)), any(Object[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidRedisPrefixIsRejected() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new GatewayGuard(mock(StringRedisTemplate.class), 60, 300, 4, ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unconfiguredRealProviderNeverReturnsMockSuccess() {
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
GatewayGuard guard = mock(GatewayGuard.class);
|
||||
OpenAiCompatibleProvider provider = mock(OpenAiCompatibleProvider.class);
|
||||
UsageAuditService usage = mock(UsageAuditService.class);
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync"));
|
||||
when(catalog.keyCanInvoke(key, "model-real")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, provider, guard, usage);
|
||||
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real")));
|
||||
assertEquals("PROVIDER_NOT_CONFIGURED", error.getCode());
|
||||
assertEquals(503, error.getStatus());
|
||||
verify(provider, never()).chat(anyString(), anyString(), anyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitMockProviderStillWorks() {
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync"));
|
||||
when(catalog.keyCanInvoke(key, "mock")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), mock(GatewayGuard.class), mock(UsageAuditService.class));
|
||||
assertEquals(200, controller.chatCompletions("Bearer test-key", Map.of("model", "mock")).getStatusCode().value());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unimplementedMediaProviderDoesNotCreateFakeTask() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("tripo")).thenReturn(Map.of("id", "tripo", "providerId", "provider-vast", "type", "3d", "mode", "async"));
|
||||
when(catalog.keyCanInvoke(key, "tripo")).thenReturn(true);
|
||||
AsyncTaskService tasks = new AsyncTaskService(jdbc, new ObjectMapper(), catalog, 100, mock(GatewayGuard.class), mock(UsageAuditService.class));
|
||||
ApiException error = assertThrows(ApiException.class, () -> tasks.create("Bearer test-key", "3d", Map.of("model", "tripo", "prompt", "test")));
|
||||
assertEquals(501, error.getStatus());
|
||||
verifyNoInteractions(jdbc);
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.auth.AuthService;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class PostgresBindingTest {
|
||||
@Test
|
||||
void keyIdUsesNativeUuidBinding() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
AuthService auth = mock(AuthService.class);
|
||||
when(auth.requireUser("session")).thenReturn(new AuthService.UserView(UUID.randomUUID(), "admin", "username", "Admin", "admin", "active", null, null));
|
||||
doAnswer(call -> {
|
||||
Object[] bindings = (Object[]) call.getRawArguments()[1];
|
||||
assertInstanceOf(UUID.class, bindings[0]);
|
||||
return 1;
|
||||
}).when(jdbc).update(startsWith("INSERT INTO platform_api_keys"), any(Object[].class));
|
||||
doReturn(Map.of("name", "test")).when(jdbc).queryForObject(anyString(), any(RowMapper.class), any(Object[].class));
|
||||
new CatalogService(jdbc, new ObjectMapper(), mock(SecretCipher.class)).createKey("session", Map.of("name", "test"), auth);
|
||||
verify(jdbc).update(startsWith("INSERT INTO platform_api_keys"), any(Object[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void oversizedAuditPayloadIsStillValidJsonb() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
doAnswer(call -> {
|
||||
Object[] bindings = (Object[]) call.getRawArguments()[1];
|
||||
String payload = (String) bindings[11];
|
||||
assertTrue(payload.length() <= 32000);
|
||||
assertTrue(mapper.readTree(payload).get("_truncated").asBoolean());
|
||||
return 1;
|
||||
}).when(jdbc).update(startsWith("INSERT INTO usage_events"), any(Object[].class));
|
||||
new UsageAuditService(jdbc, mapper).recordCall(UUID.randomUUID(), UUID.randomUUID(), "model", "provider", "succeeded", 5, 0, 0,
|
||||
List.of("x".repeat(8000), "y".repeat(8000), "z".repeat(8000), "w".repeat(8000), "v".repeat(8000)), null, null, null);
|
||||
verify(jdbc).update(startsWith("INSERT INTO usage_events"), any(Object[].class));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user