This commit is contained in:
commit
db82325ed7
216 files changed
+33718
No files matched your search
@@ -0,0 +1,740 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
|
||||
@SpringBootTest(properties = "app.encryption-key=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
|
||||
@org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
|
||||
@ActiveProfiles("test")
|
||||
class AgentWorkerIntegrationTest {
|
||||
@Autowired TaskService tasks;
|
||||
@Autowired IdentityService users;
|
||||
@Autowired WalletService wallets;
|
||||
@Autowired SecretVault vault;
|
||||
@Autowired ModelGateway gateway;
|
||||
@Autowired ObjectMapper json;
|
||||
@Autowired TaskReportService reports;
|
||||
@Autowired CodeReviewService codeReviews;
|
||||
@Autowired ReconciliationService reconciliation;
|
||||
@Autowired org.springframework.test.web.servlet.MockMvc mvc;
|
||||
@MockitoBean ProviderTransport transport;
|
||||
@MockitoBean SandboxClient sandbox;
|
||||
@MockitoBean EphemeralStore ephemeral;
|
||||
UUID user, project, model;
|
||||
|
||||
UUID completedForReview(String key) throws Exception {
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build source", new BigDecimal("20"), key)
|
||||
.get("id")
|
||||
.toString());
|
||||
var claimed = tasks.claim();
|
||||
String lease = claimed.get("lease_token").toString();
|
||||
tasks.response(
|
||||
id, lease, gateway.parse("OPENAI", response("DEVELOPMENT_PRIVATE_CONTEXT", null)));
|
||||
tasks.finish(id, lease, "COMPLETED", "fixture", true);
|
||||
return id;
|
||||
}
|
||||
|
||||
JsonNode reviewSnapshotFixture(String hash) throws Exception {
|
||||
return json.readTree(
|
||||
"{\"sourceHash\":\""
|
||||
+ hash
|
||||
+ "\",\"archive\":\"fixture\",\"complete\":true,\"files\":[{\"path\":\"index.html\",\"bytes\":100}],\"samples\":[{\"path\":\"index.html\",\"content\":\"<title>Actual"
|
||||
+ " source</title>\",\"truncated\":false}]}");
|
||||
}
|
||||
|
||||
void successfulReviewStubs(String hash) throws Exception {
|
||||
when(sandbox.reviewSnapshot(project)).thenReturn(reviewSnapshotFixture(hash));
|
||||
when(sandbox.importReview(any(), any()))
|
||||
.thenReturn(json.createObjectNode().put("sourceHash", hash));
|
||||
when(sandbox.validateReview(any()))
|
||||
.thenReturn(
|
||||
json.readTree(
|
||||
"{\"sourceHash\":\""
|
||||
+ hash
|
||||
+ "\",\"steps\":[{\"name\":\"html-assets\",\"status\":\"PASSED\"},{\"name\":\"build\",\"status\":\"NOT_APPLICABLE\"}],\"hasUi\":true,\"browser\":{\"status\":\"NOT_RUN\"},\"sourceChangedByBuild\":[]}"));
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenReturn(
|
||||
response(
|
||||
"{\"verdict\":\"PASS\",\"summary\":\"Source"
|
||||
+ " checked\",\"findings\":[],\"requirements\":[],\"unverified\":[\"browser\"]}",
|
||||
null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void independentReviewerUsesActualSourceWithoutDevelopmentConversationOrCustomerCharge()
|
||||
throws Exception {
|
||||
UUID task = completedForReview("review-source");
|
||||
String hash = "a".repeat(64);
|
||||
successfulReviewStubs(hash);
|
||||
var queued = codeReviews.enqueue(task, "review-request", user, "test", "test");
|
||||
var job = codeReviews.claim();
|
||||
BigDecimal before = (BigDecimal) wallets.get(user).get("balance");
|
||||
new CodeReviewWorker(codeReviews, tasks, sandbox, gateway, vault, json).run(job);
|
||||
var record = codeReviews.list(task).getFirst();
|
||||
assertEquals("COMPLETED", record.get("status"));
|
||||
assertEquals("PARTIAL", record.get("verdict"));
|
||||
assertEquals("PASS", record.get("ai_verdict"));
|
||||
assertEquals(hash, record.get("source_hash"));
|
||||
assertNotNull(record.get("model_cost"));
|
||||
assertEquals(0, before.compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertNotEquals(project, job.get("workspace_id"));
|
||||
verify(sandbox).cleanupReview((UUID) job.get("workspace_id"));
|
||||
verify(transport)
|
||||
.request(
|
||||
anyString(),
|
||||
anyString(),
|
||||
anyString(),
|
||||
anyString(),
|
||||
anyInt(),
|
||||
argThat(
|
||||
payload ->
|
||||
!payload.has("tools")
|
||||
&& !payload.toString().contains("DEVELOPMENT_PRIVATE_CONTEXT")
|
||||
&& payload.toString().contains("Actual source")));
|
||||
assertEquals(
|
||||
queued.get("id"),
|
||||
codeReviews.enqueue(task, "review-request", user, "duplicate", "test").get("id"));
|
||||
assertFalse(record.containsKey("lease_token"));
|
||||
assertFalse(record.containsKey("model_config"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void changedSourceInvalidatesAiPassAndOldRevisionIsRetained() throws Exception {
|
||||
UUID task = completedForReview("stale-review");
|
||||
String hash = "a".repeat(64);
|
||||
successfulReviewStubs(hash);
|
||||
when(sandbox.reviewSnapshot(project))
|
||||
.thenReturn(reviewSnapshotFixture(hash), reviewSnapshotFixture("b".repeat(64)));
|
||||
codeReviews.enqueue(task, "first-review", user, "test", "test");
|
||||
new CodeReviewWorker(codeReviews, tasks, sandbox, gateway, vault, json)
|
||||
.run(codeReviews.claim());
|
||||
assertEquals("STALE", codeReviews.list(task).getFirst().get("verdict"));
|
||||
codeReviews.enqueue(task, "second-review", user, "source updated", "test");
|
||||
assertEquals(2, codeReviews.list(task).size());
|
||||
assertEquals(2, ((Number) codeReviews.list(task).getFirst().get("revision")).intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void highSeverityMissingRequirementsCannotBeClassifiedAsPartialPass() throws Exception {
|
||||
UUID task = completedForReview("missing-requirement");
|
||||
successfulReviewStubs("a".repeat(64));
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenReturn(
|
||||
response(
|
||||
"{\"verdict\":\"PARTIAL\",\"summary\":\"Missing"
|
||||
+ " feature\",\"findings\":[{\"severity\":\"HIGH\",\"file\":\"index.html\",\"line\":0,\"description\":\"Required"
|
||||
+ " section is"
|
||||
+ " absent\"}],\"requirements\":[{\"status\":\"MISSING\"}],\"unverified\":[]}",
|
||||
null));
|
||||
codeReviews.enqueue(task, "blocking-review", user, "test", "test");
|
||||
new CodeReviewWorker(codeReviews, tasks, sandbox, gateway, vault, json)
|
||||
.run(codeReviews.claim());
|
||||
assertEquals("FAIL", codeReviews.list(task).getFirst().get("verdict"));
|
||||
assertEquals("PARTIAL", codeReviews.list(task).getFirst().get("ai_verdict"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidReviewerOutputKeepsEvidenceAndCleansSeparateWorkspace() throws Exception {
|
||||
UUID task = completedForReview("invalid-review");
|
||||
successfulReviewStubs("a".repeat(64));
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenReturn(response("not a JSON report", null));
|
||||
codeReviews.enqueue(task, "bad-report", user, "test", "test");
|
||||
var job = codeReviews.claim();
|
||||
new CodeReviewWorker(codeReviews, tasks, sandbox, gateway, vault, json).run(job);
|
||||
assertEquals("FAILED", codeReviews.list(task).getFirst().get("status"));
|
||||
assertEquals("UNVERIFIED", codeReviews.list(task).getFirst().get("verdict"));
|
||||
assertTrue(
|
||||
codeReviews.list(task).getFirst().get("evidence").toString().contains("html-assets"));
|
||||
verify(sandbox).cleanupReview((UUID) job.get("workspace_id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reviewQueueClaimsOnlyOneJobAndConfigurationHasItsOwnSnapshot() throws Exception {
|
||||
UUID first = completedForReview("review-one");
|
||||
codeReviews.enqueue(first, "one", user, "test", "test");
|
||||
UUID second = completedForReview("review-two");
|
||||
codeReviews.enqueue(second, "two", user, "test", "test");
|
||||
var job = codeReviews.claim();
|
||||
assertNotNull(job);
|
||||
assertNull(codeReviews.claim());
|
||||
codeReviews.finish(
|
||||
(UUID) job.get("id"),
|
||||
job.get("lease_token").toString(),
|
||||
"PARTIAL",
|
||||
"PARTIAL",
|
||||
"{}",
|
||||
null,
|
||||
"{}",
|
||||
Map.of(),
|
||||
"",
|
||||
true);
|
||||
assertNotNull(codeReviews.claim());
|
||||
assertTrue(codeReviews.automaticCandidates().isEmpty());
|
||||
codeReviews.configure(user, false, null, 1, "disable new automatic reviews", "test");
|
||||
UUID explicit = completedForReview("explicit-review-after-disabled");
|
||||
assertNotNull(
|
||||
codeReviews.enqueue(
|
||||
explicit, "explicit", user, "manual review while automatic is disabled", "test"));
|
||||
assertTrue(codeReviews.automaticCandidates().isEmpty());
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() -> codeReviews.configure(user, true, null, 1, "stale config", "test"));
|
||||
assertTrue(job.get("model_config").toString().contains("review_config_version"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void separatelySelectedReviewerModelAndPriceAreSnapshottedBeforeLaterConfigChanges()
|
||||
throws Exception {
|
||||
UUID task = completedForReview("separate-model"),
|
||||
reviewer = UUID.randomUUID(),
|
||||
price = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_models(id,provider_id,name,model_id,context_length,max_output,tools_enabled,vision_enabled,enabled)"
|
||||
+ " SELECT ?,provider_id,'Reviewer','reviewer-model',32768,2048,FALSE,FALSE,TRUE"
|
||||
+ " FROM ai_models WHERE id=?",
|
||||
reviewer,
|
||||
model);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_model_price_versions(id,model_id,input_price,output_price,cache_read_price,cache_write_price)"
|
||||
+ " VALUES (?,?,1,2,0.1,1)",
|
||||
price,
|
||||
reviewer);
|
||||
users.db().update("UPDATE ai_models SET current_price_version=? WHERE id=?", price, reviewer);
|
||||
codeReviews.configure(user, true, reviewer, 1, "separate model", "test");
|
||||
codeReviews.enqueue(task, "override-model", user, "test", "test");
|
||||
codeReviews.configure(user, true, null, 2, "change future reviews only", "test");
|
||||
var job = codeReviews.claim();
|
||||
assertTrue(job.get("model_config").toString().contains("reviewer-model"));
|
||||
assertEquals(price, job.get("price_version_id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void codeReviewEndpointsRejectOrdinaryOwnersAndReadOnlyAdministratorsCannotStartPaidReviews()
|
||||
throws Exception {
|
||||
UUID task = completedForReview("review-access");
|
||||
codeReviews.enqueue(task, "one", user, "test", "test");
|
||||
String token =
|
||||
users
|
||||
.login("worker@test.com", "StrongPassword123", "test", "test", 1)
|
||||
.get("token")
|
||||
.toString();
|
||||
mvc.perform(
|
||||
get("/api/admin/tasks/" + task + "/code-reviews")
|
||||
.header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isForbidden());
|
||||
mvc.perform(get("/api/admin/code-reviews/config").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isForbidden());
|
||||
UUID admin = users.create("code-review-admin@test.com", "Admin", "StrongPassword123", "test");
|
||||
users.db().update("INSERT INTO user_roles VALUES (?,'FINANCE')", admin);
|
||||
String adminToken =
|
||||
users
|
||||
.login("code-review-admin@test.com", "StrongPassword123", "test", "test", 1)
|
||||
.get("token")
|
||||
.toString();
|
||||
mvc.perform(
|
||||
get("/api/admin/tasks/" + task + "/code-reviews")
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isOk());
|
||||
mvc.perform(
|
||||
post("/api/admin/tasks/" + task + "/code-reviews")
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType("application/json")
|
||||
.content("{\"idempotencyKey\":\"forbidden\",\"reason\":\"test\"}"))
|
||||
.andExpect(status().isForbidden());
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
TestDatabase.clear(users.db());
|
||||
user = users.create("worker@test.com", "Worker", "StrongPassword123", "test");
|
||||
project = UUID.randomUUID();
|
||||
model = UUID.randomUUID();
|
||||
UUID provider = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,?,'','Vue 3','')",
|
||||
project,
|
||||
user,
|
||||
"Test");
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_providers(id,name,base_url,api_type,encrypted_key,enabled,timeout_seconds)"
|
||||
+ " VALUES (?,'Test','https://api.openai.com/v1','OPENAI',?,TRUE,30)",
|
||||
provider,
|
||||
vault.encrypt("fictional-test-key"));
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_models(id,provider_id,name,model_id,context_length,max_output,tools_enabled,vision_enabled,enabled)"
|
||||
+ " VALUES (?,?,'Test','test-model',32768,512,TRUE,FALSE,TRUE)",
|
||||
model,
|
||||
provider);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_model_price_versions(id,model_id,input_price,output_price,cache_read_price,cache_write_price)"
|
||||
+ " VALUES (?,?,1,2,0.1,1)",
|
||||
UUID.randomUUID(),
|
||||
model);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE ai_models SET current_price_version=(SELECT id FROM ai_model_price_versions"
|
||||
+ " WHERE model_id=?) WHERE id=?",
|
||||
model,
|
||||
model);
|
||||
wallets.adjust(user, new BigDecimal("100"), "credit", "test", user, "test");
|
||||
when(sandbox.inspect(any()))
|
||||
.thenReturn(json.createObjectNode().put("sourceFiles", 1).put("complete", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyWorkspaceCannotBeReportedAsCompletedAndRequestsActualFiles() throws Exception {
|
||||
when(sandbox.inspect(any()))
|
||||
.thenReturn(json.createObjectNode().put("sourceFiles", 0).put("complete", true));
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenReturn(response("All done without files", null));
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "empty-workspace")
|
||||
.get("id")
|
||||
.toString());
|
||||
new AgentWorker(tasks, gateway, sandbox, vault, json).run(tasks.claim());
|
||||
assertEquals("PAUSED", tasks.config(id).get("status"));
|
||||
assertTrue(
|
||||
tasks.history(id).getLast().path("content").asText().contains("no non-empty source files"));
|
||||
verify(transport, times(2))
|
||||
.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyCompletionCanRecoverByActuallyWritingSource() throws Exception {
|
||||
when(sandbox.inspect(any()))
|
||||
.thenReturn(
|
||||
json.createObjectNode().put("sourceFiles", 0).put("complete", true),
|
||||
json.createObjectNode().put("sourceFiles", 1).put("complete", true));
|
||||
when(sandbox.execute(any(), any())).thenReturn("{\"written\":14}");
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenReturn(
|
||||
response("done", null), response("write", "write_file"), response("done", null));
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "recover-source")
|
||||
.get("id")
|
||||
.toString());
|
||||
new AgentWorker(tasks, gateway, sandbox, vault, json).run(tasks.claim());
|
||||
assertEquals("COMPLETED", tasks.config(id).get("status"));
|
||||
verify(sandbox).execute(eq(project), argThat(call -> call.name().equals("write_file")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void incompleteInspectionCannotClaimWorkspaceIsEmptyOrCompleted() throws Exception {
|
||||
when(sandbox.inspect(any()))
|
||||
.thenReturn(json.createObjectNode().put("sourceFiles", 0).put("complete", false));
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenReturn(response("done", null));
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "scan-limit")
|
||||
.get("id")
|
||||
.toString());
|
||||
new AgentWorker(tasks, gateway, sandbox, vault, json).run(tasks.claim());
|
||||
assertEquals("PAUSED", tasks.config(id).get("status"));
|
||||
verify(transport, times(1))
|
||||
.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aiReportHasNoToolsAndNeverChargesCustomerForReportGeneration() throws Exception {
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "ai-report")
|
||||
.get("id")
|
||||
.toString());
|
||||
var claimed = tasks.claim();
|
||||
tasks.finish(id, claimed.get("lease_token").toString(), "FAILED", "known failure", true);
|
||||
var job = reports.claim(id);
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenReturn(response("Administrator analysis", null));
|
||||
new TaskReportWorker(reports, tasks, gateway, sandbox, vault, json).run(job);
|
||||
assertEquals("COMPLETED", reports.reports(id).getFirst().get("status"));
|
||||
assertNotNull(reports.reports(id).getFirst().get("model_cost"));
|
||||
assertEquals(0, new BigDecimal("100").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
verify(transport)
|
||||
.request(
|
||||
anyString(),
|
||||
anyString(),
|
||||
anyString(),
|
||||
anyString(),
|
||||
anyInt(),
|
||||
argThat(payload -> !payload.has("tools")));
|
||||
verify(sandbox, never()).execute(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void exhaustedReportQuotaKeepsEvidenceWithoutCallingPaidModel() throws Exception {
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "report-quota")
|
||||
.get("id")
|
||||
.toString());
|
||||
var claimed = tasks.claim();
|
||||
tasks.finish(id, claimed.get("lease_token").toString(), "FAILED", "known failure", true);
|
||||
var job = new HashMap<>(reports.claim(id));
|
||||
job.put("aiAllowed", false);
|
||||
new TaskReportWorker(reports, tasks, gateway, sandbox, vault, json).run(job);
|
||||
assertEquals("FAILED", reports.reports(id).getFirst().get("status"));
|
||||
assertFalse(reports.reports(id).getFirst().get("evidence").toString().isEmpty());
|
||||
verifyNoInteractions(transport);
|
||||
}
|
||||
|
||||
@Test
|
||||
void existingAdministratorSettlementIsPreservedInAutomaticReport() throws Exception {
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "prior-manual")
|
||||
.get("id")
|
||||
.toString());
|
||||
var claimed = tasks.claim();
|
||||
tasks.finish(id, claimed.get("lease_token").toString(), "REVIEW_REQUIRED", "timeout", false);
|
||||
reconciliation.resolve(id, user, new BigDecimal("3"), "provider receipt", "test");
|
||||
var job = reports.claim(id);
|
||||
assertNotNull(job);
|
||||
assertEquals(Boolean.FALSE, reports.reports(id).getFirst().get("automatic_settlement"));
|
||||
assertTrue(
|
||||
reports.reports(id).getFirst().get("evidence").toString().contains("provider receipt"));
|
||||
assertEquals(0, new BigDecimal("97").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void automaticSettlementReleasesBudgetOnceAndNeverChargesAiGuesses() throws Exception {
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "auto-settlement")
|
||||
.get("id")
|
||||
.toString());
|
||||
var claimed = tasks.claim();
|
||||
String lease = claimed.get("lease_token").toString();
|
||||
tasks.response(id, lease, gateway.parse("OPENAI", response("ok", null)));
|
||||
tasks.finish(id, lease, "REVIEW_REQUIRED", "Unknown next response", false);
|
||||
assertNull(reports.claim(id), "Do not touch a newly interrupted tool before the quiet period");
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE ai_tasks SET updated_at=? WHERE id=?",
|
||||
java.sql.Timestamp.from(java.time.Instant.now().minusSeconds(181)),
|
||||
id);
|
||||
var job = reports.claim(id);
|
||||
assertNotNull(job);
|
||||
assertNull(reports.claim(id));
|
||||
assertEquals("FAILED", tasks.config(id).get("status"));
|
||||
var balance = (BigDecimal) wallets.get(user).get("balance");
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
reports.finish(
|
||||
(UUID) job.get("id"),
|
||||
job.get("lease").toString(),
|
||||
"Untrusted AI says charge 999",
|
||||
new BigDecimal("0.001"),
|
||||
"{}",
|
||||
Map.of("unknown", true),
|
||||
"");
|
||||
assertEquals(0, balance.compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertEquals(1, reports.reports(id).size());
|
||||
assertEquals(Boolean.TRUE, reports.reports(id).getFirst().get("automatic_settlement"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportFailureDoesNotUndoAutomaticSettlementOrReplayModels() throws Exception {
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "report-failure")
|
||||
.get("id")
|
||||
.toString());
|
||||
var claimed = tasks.claim();
|
||||
tasks.finish(id, claimed.get("lease_token").toString(), "REVIEW_REQUIRED", "timeout", false);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE ai_tasks SET updated_at=? WHERE id=?",
|
||||
java.sql.Timestamp.from(java.time.Instant.now().minusSeconds(181)),
|
||||
id);
|
||||
var job = reports.claim(id);
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenThrow(new ApiError.Failure(502, "report unavailable"));
|
||||
new TaskReportWorker(reports, tasks, gateway, sandbox, vault, json).run(job);
|
||||
assertEquals("FAILED", reports.reports(id).getFirst().get("status"));
|
||||
assertEquals("FAILED", tasks.config(id).get("status"));
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
assertNull(reports.claim(id));
|
||||
verify(sandbox, never()).execute(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void reconciliationRecordsAndAiReportsAreAdministratorOnly() throws Exception {
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "private-report")
|
||||
.get("id")
|
||||
.toString());
|
||||
var claimed = tasks.claim();
|
||||
tasks.finish(id, claimed.get("lease_token").toString(), "REVIEW_REQUIRED", "timeout", false);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE ai_tasks SET updated_at=? WHERE id=?",
|
||||
java.sql.Timestamp.from(java.time.Instant.now().minusSeconds(181)),
|
||||
id);
|
||||
var job = reports.claim(id);
|
||||
reports.finish(
|
||||
(UUID) job.get("id"),
|
||||
job.get("lease").toString(),
|
||||
"SECRET_ADMIN_REPORT",
|
||||
null,
|
||||
"{}",
|
||||
Map.of("private", "PRIVATE_USAGE_DATA"),
|
||||
"");
|
||||
String token =
|
||||
users
|
||||
.login("worker@test.com", "StrongPassword123", "test", "test", 1)
|
||||
.get("token")
|
||||
.toString();
|
||||
mvc.perform(get("/api/admin/tasks/" + id + "/reports")).andExpect(status().isUnauthorized());
|
||||
mvc.perform(
|
||||
get("/api/admin/tasks/" + id + "/reports").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isForbidden());
|
||||
String events =
|
||||
mvc.perform(get("/api/tasks/" + id + "/events").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
assertFalse(events.contains("AUTO_SETTLEMENT"));
|
||||
assertFalse(events.contains("SECRET_ADMIN_REPORT"));
|
||||
UUID admin = users.create("report-admin@test.com", "Admin", "StrongPassword123", "test");
|
||||
users.db().update("INSERT INTO user_roles VALUES (?,'ADMIN')", admin);
|
||||
String adminToken =
|
||||
users
|
||||
.login("report-admin@test.com", "StrongPassword123", "test", "test", 1)
|
||||
.get("token")
|
||||
.toString();
|
||||
mvc.perform(get("/api/admin/tasks").header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isOk());
|
||||
String report =
|
||||
mvc.perform(
|
||||
get("/api/admin/tasks/" + id + "/reports")
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
assertTrue(report.contains("SECRET_ADMIN_REPORT"));
|
||||
assertTrue(report.contains("PRIVATE_USAGE_DATA"));
|
||||
assertFalse(report.contains("lease_token"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancellationDuringSourceInspectionCannotMarkTaskCompleted() throws Exception {
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "cancel-inspection")
|
||||
.get("id")
|
||||
.toString());
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenReturn(response("done", null));
|
||||
when(sandbox.inspect(any()))
|
||||
.thenAnswer(
|
||||
call -> {
|
||||
tasks.action(id, user, "cancel");
|
||||
return json.createObjectNode().put("sourceFiles", 1).put("complete", true);
|
||||
});
|
||||
new AgentWorker(tasks, gateway, sandbox, vault, json).run(tasks.claim());
|
||||
assertEquals("CANCELLED", tasks.config(id).get("status"));
|
||||
}
|
||||
|
||||
JsonNode response(String content, String tool) throws Exception {
|
||||
var result = json.createObjectNode();
|
||||
result.put("id", UUID.randomUUID().toString());
|
||||
var message = result.putArray("choices").addObject().putObject("message");
|
||||
message.put("role", "assistant");
|
||||
message.put("content", content);
|
||||
if (tool != null) {
|
||||
var call = message.putArray("tool_calls").addObject();
|
||||
call.put("id", UUID.randomUUID().toString());
|
||||
call.put("type", "function");
|
||||
call.putObject("function")
|
||||
.put("name", tool)
|
||||
.put(
|
||||
"arguments",
|
||||
tool.equals("write_file")
|
||||
? "{\"path\":\"index.html\",\"content\":\"<h1>Hello</h1>\"}"
|
||||
: "{\"command\":\"npm run build\"}");
|
||||
}
|
||||
result.putObject("usage").put("prompt_tokens", 1000).put("completion_tokens", 500);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedToolArgumentsAreBilledAndReturnedWithoutSandboxExecution() throws Exception {
|
||||
var malformed = response("write", "write_file");
|
||||
((com.fasterxml.jackson.databind.node.ObjectNode)
|
||||
malformed
|
||||
.path("choices")
|
||||
.get(0)
|
||||
.path("message")
|
||||
.path("tool_calls")
|
||||
.get(0)
|
||||
.path("function"))
|
||||
.put("arguments", "{\"path\":");
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenReturn(malformed, response("Cannot continue", null));
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "bad-tool")
|
||||
.get("id")
|
||||
.toString());
|
||||
new AgentWorker(tasks, gateway, sandbox, vault, json).run(tasks.claim());
|
||||
assertEquals("COMPLETED", tasks.config(id).get("status"));
|
||||
assertEquals(
|
||||
2,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM ai_usage_records WHERE task_id=?", Integer.class, id));
|
||||
assertTrue(
|
||||
tasks.history(id).stream()
|
||||
.anyMatch(
|
||||
m ->
|
||||
m.path("role").asText().equals("tool")
|
||||
&& m.path("content").asText().contains("Invalid tool arguments")));
|
||||
verify(sandbox, never()).execute(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void truncatedToolResponsePausesAfterBillingWithoutExecutingAnyTool() throws Exception {
|
||||
var truncated = response("partial", "write_file");
|
||||
((com.fasterxml.jackson.databind.node.ObjectNode) truncated.path("choices").get(0))
|
||||
.put("finish_reason", "length");
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenReturn(truncated);
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "truncated-tool")
|
||||
.get("id")
|
||||
.toString());
|
||||
new AgentWorker(tasks, gateway, sandbox, vault, json).run(tasks.claim());
|
||||
assertEquals("PAUSED", tasks.config(id).get("status"));
|
||||
assertEquals(
|
||||
1,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM ai_usage_records WHERE task_id=?", Integer.class, id));
|
||||
assertTrue(tasks.history(id).getLast().path("content").asText().contains("not executed"));
|
||||
verify(sandbox, never()).execute(any(), any());
|
||||
verify(transport, times(1))
|
||||
.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toolLoopPersistsMessagesChargesUsageAndReleasesBudget() throws Exception {
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenReturn(
|
||||
response("write", "write_file"),
|
||||
response("build", "run_command"),
|
||||
response("Build passed", null));
|
||||
when(sandbox.execute(eq(project), any()))
|
||||
.thenReturn("{\"written\":14}", "{\"exitCode\":0,\"output\":\"build passed\"}");
|
||||
UUID task =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build hello", new BigDecimal("20"), "run")
|
||||
.get("id")
|
||||
.toString());
|
||||
new AgentWorker(tasks, gateway, sandbox, vault, json).run(tasks.claim());
|
||||
assertEquals("COMPLETED", tasks.find(task, user).get("status"));
|
||||
assertEquals(
|
||||
3, users.db().queryForObject("SELECT COUNT(*) FROM ai_usage_records", Integer.class));
|
||||
assertEquals(7, tasks.history(task).size());
|
||||
assertEquals(
|
||||
0, new BigDecimal("99.994").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
verify(sandbox, times(2)).execute(eq(project), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancellationDuringModelCallDoesNotExecuteNewTools() throws Exception {
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "cancel-in-flight")
|
||||
.get("id")
|
||||
.toString());
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenAnswer(
|
||||
call -> {
|
||||
tasks.action(id, user, "cancel");
|
||||
return response("write", "write_file");
|
||||
});
|
||||
new AgentWorker(tasks, gateway, sandbox, vault, json).run(tasks.claim());
|
||||
assertEquals("CANCELLED", tasks.find(id, user).get("status"));
|
||||
verify(sandbox, never()).execute(any(), any());
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void upstreamFailureIsNotRetriedAndRetainsBudget() {
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenThrow(new ApiError.Failure(502, "ambiguous timeout"));
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build", new BigDecimal("20"), "timeout")
|
||||
.get("id")
|
||||
.toString());
|
||||
new AgentWorker(tasks, gateway, sandbox, vault, json).run(tasks.claim());
|
||||
assertEquals("REVIEW_REQUIRED", tasks.find(id, user).get("status"));
|
||||
assertEquals(0, new BigDecimal("20").compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
verify(transport, times(1))
|
||||
.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CodeReviewDecisionTest {
|
||||
final ObjectMapper json = new ObjectMapper();
|
||||
|
||||
JsonNode source() throws Exception {
|
||||
return json.readTree(
|
||||
"{\"complete\":true,\"files\":[{\"path\":\"index.html\",\"bytes\":100}],\"samples\":[{\"truncated\":false}]}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aiCannotOverrideFailedBuildOrChangedSource() throws Exception {
|
||||
assertTrue(
|
||||
CodeReviewWorker.blockingFindings(
|
||||
json.readTree("{\"findings\":[{\"severity\":\"HIGH\"}]}")));
|
||||
assertTrue(
|
||||
CodeReviewWorker.blockingFindings(
|
||||
json.readTree("{\"requirements\":[{\"status\":\"MISSING\"}]}")));
|
||||
assertFalse(
|
||||
CodeReviewWorker.blockingFindings(
|
||||
json.readTree("{\"findings\":[{\"severity\":\"LOW\"}]}")));
|
||||
var checks = json.readTree("{\"steps\":[{\"name\":\"build\",\"status\":\"FAILED\"}]}");
|
||||
assertEquals("FAIL", CodeReviewWorker.decision("PASS", source(), checks, true));
|
||||
assertEquals("STALE", CodeReviewWorker.decision("PASS", source(), checks, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noBrowserOrUnsupportedBuildNeverBecomesFullPass() throws Exception {
|
||||
assertEquals(
|
||||
"PARTIAL",
|
||||
CodeReviewWorker.decision(
|
||||
"PASS",
|
||||
source(),
|
||||
json.readTree(
|
||||
"{\"steps\":[{\"name\":\"html-assets\",\"status\":\"PASSED\"}],\"hasUi\":true,\"browser\":{\"status\":\"NOT_RUN\"}}"),
|
||||
true));
|
||||
assertEquals(
|
||||
"UNVERIFIED",
|
||||
CodeReviewWorker.decision(
|
||||
"PASS",
|
||||
source(),
|
||||
json.readTree("{\"steps\":[{\"name\":\"build\",\"status\":\"UNSUPPORTED\"}]}"),
|
||||
true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noSourceCannotPassEvenWhenModelClaimsSuccess() throws Exception {
|
||||
assertEquals(
|
||||
"FAIL",
|
||||
CodeReviewWorker.decision(
|
||||
"PASS",
|
||||
json.readTree("{\"files\":[]}"),
|
||||
json.readTree("{\"steps\":[{\"status\":\"PASSED\"}]}"),
|
||||
true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DomainDnsGatewayTest {
|
||||
AliyunGateway api = mock(AliyunGateway.class);
|
||||
DomainDnsGateway dns = new DomainDnsGateway(api, new ObjectMapper());
|
||||
Map<String, String> config = Map.of("rootDomain", "webup.run", "recordTtl", "600");
|
||||
|
||||
@Test
|
||||
void createsOnlyWhenExactHostHasNoRecords() {
|
||||
when(api.call(anyMap(), eq("DescribeSubDomainRecords"), anyString(), anyString(), anyMap()))
|
||||
.thenReturn(Map.of("TotalCount", 0, "DomainRecords", Map.of("Record", List.of())));
|
||||
when(api.call(anyMap(), eq("AddDomainRecord"), anyString(), anyString(), anyMap()))
|
||||
.thenReturn(Map.of("RecordId", "new-id"));
|
||||
assertEquals("new-id", dns.ensure(config, "demo.webup.run", "ingress.example.test"));
|
||||
verify(api)
|
||||
.call(
|
||||
eq(config),
|
||||
eq("AddDomainRecord"),
|
||||
eq("2015-01-09"),
|
||||
eq("POST"),
|
||||
argThat(
|
||||
q -> q.get("RR").equals("demo") && q.get("Value").equals("ingress.example.test")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void existingMatchingRecordIsBorrowedNotOwned() {
|
||||
when(api.call(anyMap(), anyString(), anyString(), anyString(), anyMap()))
|
||||
.thenReturn(
|
||||
Map.of(
|
||||
"TotalCount",
|
||||
1,
|
||||
"DomainRecords",
|
||||
Map.of(
|
||||
"Record",
|
||||
List.of(
|
||||
Map.of(
|
||||
"Type",
|
||||
"CNAME",
|
||||
"Value",
|
||||
"ingress.example.test.",
|
||||
"Status",
|
||||
"ENABLE")))));
|
||||
assertNull(dns.ensure(config, "demo.webup.run", "ingress.example.test"));
|
||||
verify(api, times(1)).call(anyMap(), anyString(), anyString(), anyString(), anyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void conflictingRecordIsNeverOverwritten() {
|
||||
when(api.call(anyMap(), anyString(), anyString(), anyString(), anyMap()))
|
||||
.thenReturn(
|
||||
Map.of(
|
||||
"TotalCount",
|
||||
1,
|
||||
"DomainRecords",
|
||||
Map.of("Record", List.of(Map.of("Type", "A", "Value", "203.0.113.1")))));
|
||||
assertThrows(
|
||||
ApiError.Failure.class, () -> dns.ensure(config, "demo.webup.run", "ingress.example.test"));
|
||||
verify(api, times(1)).call(anyMap(), anyString(), anyString(), anyString(), anyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void absentDeletedRecordMakesRemovalIdempotent() {
|
||||
when(api.call(anyMap(), anyString(), anyString(), anyString(), anyMap()))
|
||||
.thenReturn(Map.of("TotalCount", 0, "DomainRecords", Map.of("Record", List.of())));
|
||||
dns.remove(config, "gone", "demo.webup.run", "ingress.example.test");
|
||||
verify(api, times(1)).call(anyMap(), anyString(), anyString(), anyString(), anyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMalformedHostsReservedSlugsAndNormalizesIdn() {
|
||||
assertEquals("xn--fsqu00a.example.com", DomainNames.hostname("例子.example.com"));
|
||||
for (String host :
|
||||
List.of("http://example.com", "127.0.0.1", "*.example.com", "a..com", "example.com:80"))
|
||||
assertThrows(ApiError.Failure.class, () -> DomainNames.hostname(host));
|
||||
assertThrows(ApiError.Failure.class, () -> DomainNames.slug("admin"));
|
||||
assertThrows(ApiError.Failure.class, () -> DomainNames.slug("Upper-Case"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,376 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest(properties = "app.encryption-key=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
|
||||
@ActiveProfiles("test")
|
||||
@AutoConfigureMockMvc
|
||||
class DomainIntegrationTest {
|
||||
@Autowired MockMvc mvc;
|
||||
@Autowired IdentityService users;
|
||||
@Autowired WalletService wallets;
|
||||
@Autowired HostingService hosting;
|
||||
@Autowired DeploymentCoordinator releases;
|
||||
@Autowired DomainService domains;
|
||||
@Autowired DomainCoordinator coordinator;
|
||||
@Autowired IntegrationService integrations;
|
||||
@Autowired ObjectMapper json;
|
||||
@MockitoBean EphemeralStore ephemeral;
|
||||
@MockitoBean DomainResolver resolver;
|
||||
@MockitoBean RuntimeGateway runtime;
|
||||
@MockitoBean DomainDnsGateway dns;
|
||||
UUID user, project, service, plan;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
TestDatabase.clear(users.db());
|
||||
reset(resolver, runtime, dns);
|
||||
user = users.create("domains@test.com", "Domains", "StrongPassword123", "test");
|
||||
project = UUID.randomUUID();
|
||||
plan = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,'Domains','','HTML','')",
|
||||
project,
|
||||
user);
|
||||
users
|
||||
.db()
|
||||
.update("INSERT INTO project_moderation(project_id,status) VALUES (?,'APPROVED')", project);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " billing_plans(id,name,monthly_price,cpu_millis,memory_mb,disk_gb,traffic_gb,max_services,max_replicas)"
|
||||
+ " VALUES (?,'Fixture',10,1000,1024,5,5,3,2)",
|
||||
plan);
|
||||
service =
|
||||
(UUID)
|
||||
hosting
|
||||
.service(
|
||||
user,
|
||||
project,
|
||||
null,
|
||||
new HostingController.ServiceSpec(
|
||||
"web",
|
||||
"example.invalid/image:v1",
|
||||
List.of(),
|
||||
Map.of(),
|
||||
3000,
|
||||
250,
|
||||
256,
|
||||
1,
|
||||
"/",
|
||||
true))
|
||||
.get("id");
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE system_settings SET setting_value='webup.run' WHERE setting_key IN"
|
||||
+ " ('deploymentDomain','mainDomain')");
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE system_settings SET setting_value='ingress.example.test' WHERE"
|
||||
+ " setting_key='domainDeliveryTarget'");
|
||||
integrations.save(
|
||||
"ALIYUN_DNS",
|
||||
user,
|
||||
Map.of(
|
||||
"accessKeyId",
|
||||
"fixture-dns-key",
|
||||
"accessKeySecret",
|
||||
"fixture-secret",
|
||||
"regionId",
|
||||
"cn-hangzhou",
|
||||
"endpoint",
|
||||
"alidns.aliyuncs.com",
|
||||
"rootDomain",
|
||||
"webup.run",
|
||||
"recordTtl",
|
||||
"600"),
|
||||
true,
|
||||
"test");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> platform(String slug) {
|
||||
return (Map<String, Object>) domains.platform(user, project, service, slug);
|
||||
}
|
||||
|
||||
void publish() {
|
||||
wallets.adjust(user, new BigDecimal("100"), "credit", "test", user, "test");
|
||||
hosting.subscribe(user, project, plan, "plan");
|
||||
UUID release = (UUID) hosting.deploy(user, project, "release", null).get("id");
|
||||
var claimed = releases.claim();
|
||||
releases.finish(
|
||||
release, claimed.get("lease_token").toString(), json.createObjectNode().put("ready", true));
|
||||
}
|
||||
|
||||
String state(UUID id) {
|
||||
return users
|
||||
.db()
|
||||
.queryForObject("SELECT status FROM project_domains WHERE id=?", String.class, id);
|
||||
}
|
||||
|
||||
void poll() {
|
||||
var row = coordinator.claim();
|
||||
assertNotNull(row);
|
||||
coordinator.execute(row);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stablePlatformBindingIsIdempotentAndNeverReassignsRemovedHost() {
|
||||
var a = platform("stable-project");
|
||||
assertEquals(a.get("id"), platform("stable-project").get("id"));
|
||||
UUID id = (UUID) a.get("id");
|
||||
domains.queue(user, project, id, true);
|
||||
assertEquals("REMOVED", state(id));
|
||||
UUID other = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,'Other','','HTML','')",
|
||||
other,
|
||||
user);
|
||||
UUID otherService =
|
||||
(UUID)
|
||||
hosting
|
||||
.service(
|
||||
user,
|
||||
other,
|
||||
null,
|
||||
new HostingController.ServiceSpec(
|
||||
"web",
|
||||
"example.invalid/image:v1",
|
||||
List.of(),
|
||||
Map.of(),
|
||||
3000,
|
||||
250,
|
||||
256,
|
||||
1,
|
||||
"/",
|
||||
true))
|
||||
.get("id");
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() -> domains.platform(user, other, otherService, "stable-project"));
|
||||
assertFalse(a.containsKey("lease_token"));
|
||||
assertFalse(a.containsKey("dns_identity_digest"));
|
||||
verifyNoInteractions(runtime);
|
||||
}
|
||||
|
||||
@Test
|
||||
void customDomainRequiresMatchingUnexpiredProofAndOwnService() {
|
||||
@SuppressWarnings("unchecked")
|
||||
var claim = (Map<String, Object>) domains.challenge(user, project, "WWW.Example.com.");
|
||||
UUID id = (UUID) claim.get("id");
|
||||
assertEquals("www.example.com", claim.get("hostname"));
|
||||
assertThrows(ApiError.Failure.class, () -> domains.verify(user, project, id, service));
|
||||
when(resolver.proof(eq("www.example.com"), eq(claim.get("proof_token").toString())))
|
||||
.thenReturn(true);
|
||||
assertThrows(
|
||||
ApiError.Failure.class, () -> domains.verify(user, project, id, UUID.randomUUID()));
|
||||
domains.verify(user, project, id, service);
|
||||
assertEquals(
|
||||
1,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM project_domains WHERE kind='CUSTOM'", Integer.class));
|
||||
assertThrows(ApiError.Failure.class, () -> domains.challenge(user, project, "admin.webup.run"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiredProofCannotCreateBindingAndOtherOwnerCannotOperate() {
|
||||
@SuppressWarnings("unchecked")
|
||||
var claim = (Map<String, Object>) domains.challenge(user, project, "example.com");
|
||||
UUID id = (UUID) claim.get("id");
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE domain_claims SET expires_at=? WHERE id=?",
|
||||
Timestamp.from(Instant.now().minusSeconds(1)),
|
||||
id);
|
||||
assertThrows(ApiError.Failure.class, () -> domains.verify(user, project, id, service));
|
||||
verifyNoInteractions(resolver);
|
||||
UUID stranger = users.create("stranger@test.com", "Stranger", "StrongPassword123", "test");
|
||||
UUID binding = (UUID) platform("owner-site").get("id");
|
||||
assertThrows(ApiError.Failure.class, () -> domains.queue(stranger, project, binding, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void primaryIsUniqueAndNoUnpaidProvisioning() {
|
||||
UUID first = (UUID) platform("first-site").get("id"),
|
||||
second = (UUID) platform("second-site").get("id");
|
||||
domains.primary(user, project, second);
|
||||
assertEquals(
|
||||
1,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM project_domains WHERE is_primary=TRUE", Integer.class));
|
||||
assertThrows(ApiError.Failure.class, () -> domains.queue(user, project, first, false));
|
||||
assertEquals("VERIFIED", state(first));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dnsCertificateAndRouteAreIndependentReadinessGates() throws Exception {
|
||||
publish();
|
||||
UUID id = (UUID) platform("ready-site").get("id");
|
||||
domains.queue(user, project, id, false);
|
||||
when(dns.ensure(anyMap(), eq("ready-site.webup.run"), eq("ingress.example.test")))
|
||||
.thenReturn("owned-record");
|
||||
when(runtime.request(anyString(), any()))
|
||||
.thenReturn(json.readTree("{\"configured\":true,\"tlsReady\":false,\"routeReady\":true}"));
|
||||
poll();
|
||||
assertEquals("WAITING_DNS", state(id));
|
||||
when(resolver.pointsTo(anyString(), anyString())).thenReturn(true);
|
||||
force(id);
|
||||
poll();
|
||||
assertEquals("WAITING_TLS", state(id));
|
||||
when(runtime.request(anyString(), any()))
|
||||
.thenReturn(json.readTree("{\"configured\":true,\"tlsReady\":true,\"routeReady\":false}"));
|
||||
force(id);
|
||||
poll();
|
||||
assertEquals("WAITING_ROUTE", state(id));
|
||||
when(runtime.request(anyString(), any()))
|
||||
.thenReturn(json.readTree("{\"configured\":true,\"tlsReady\":true,\"routeReady\":true}"));
|
||||
force(id);
|
||||
poll();
|
||||
assertEquals("ACTIVE", state(id));
|
||||
assertEquals(
|
||||
"owned-record",
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT dns_record_id FROM project_domains WHERE id=?", String.class, id));
|
||||
}
|
||||
|
||||
void force(UUID id) {
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE project_domains SET not_before=? WHERE id=?",
|
||||
Timestamp.from(Instant.now().minusSeconds(1)),
|
||||
id);
|
||||
}
|
||||
|
||||
@Test
|
||||
void staleLeaseCannotMarkNewGenerationActive() {
|
||||
publish();
|
||||
UUID id = (UUID) platform("fenced-site").get("id");
|
||||
domains.queue(user, project, id, false);
|
||||
var claimed = coordinator.claim();
|
||||
users.db().update("UPDATE project_domains SET generation=generation+1 WHERE id=?", id);
|
||||
assertThrows(ApiError.Failure.class, () -> coordinator.finish(claimed, "ACTIVE", ""));
|
||||
assertNotEquals("ACTIVE", state(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
void bannedWebsiteCannotProvisionAnIngress() {
|
||||
publish();
|
||||
UUID id = (UUID) platform("banned-site").get("id");
|
||||
domains.queue(user, project, id, false);
|
||||
users.db().update("UPDATE project_moderation SET status='BANNED' WHERE project_id=?", project);
|
||||
var claimed = coordinator.claim();
|
||||
assertThrows(ApiError.Failure.class, () -> coordinator.execute(claimed));
|
||||
verifyNoInteractions(runtime, dns);
|
||||
}
|
||||
|
||||
@Test
|
||||
void removingBorrowedDnsDoesNotDeleteExternalRecords() throws Exception {
|
||||
publish();
|
||||
UUID id = (UUID) platform("borrowed-site").get("id");
|
||||
domains.queue(user, project, id, false);
|
||||
when(runtime.request(anyString(), any()))
|
||||
.thenReturn(json.readTree("{\"configured\":true,\"tlsReady\":true,\"routeReady\":true}"));
|
||||
when(resolver.pointsTo(anyString(), anyString())).thenReturn(true);
|
||||
poll();
|
||||
domains.queue(user, project, id, true);
|
||||
when(runtime.request(anyString(), any())).thenReturn(json.readTree("{\"removed\":true}"));
|
||||
poll();
|
||||
assertEquals("REMOVED", state(id));
|
||||
verify(dns, never()).remove(anyMap(), anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminDomainMutationRequiresPermissionAndAuditsActualActor() throws Exception {
|
||||
UUID id = (UUID) platform("admin-test").get("id");
|
||||
String userToken =
|
||||
users
|
||||
.login("domains@test.com", "StrongPassword123", "test", "test", 1)
|
||||
.get("token")
|
||||
.toString();
|
||||
String payload = "{\"action\":\"REMOVE\",\"reason\":\"测试解绑请求\"}";
|
||||
mvc.perform(
|
||||
post("/api/admin/domains/" + id + "/actions")
|
||||
.header("Authorization", "Bearer " + userToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(payload))
|
||||
.andExpect(status().isForbidden());
|
||||
UUID admin = users.create("domain-admin@test.com", "Admin", "StrongPassword123", "test");
|
||||
users.db().update("INSERT INTO user_roles VALUES (?,'OPS')", admin);
|
||||
String token =
|
||||
users
|
||||
.login("domain-admin@test.com", "StrongPassword123", "test", "test", 1)
|
||||
.get("token")
|
||||
.toString();
|
||||
mvc.perform(
|
||||
post("/api/admin/domains/" + id + "/actions")
|
||||
.header("Authorization", "Bearer " + token)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(payload))
|
||||
.andExpect(status().isOk());
|
||||
assertEquals("REMOVED", state(id));
|
||||
assertEquals(
|
||||
admin,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT actor_id FROM admin_operation_logs WHERE action='DOMAIN_REMOVE'",
|
||||
UUID.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void changedDnsAccountNeverDeletesOldProviderRecord() throws Exception {
|
||||
publish();
|
||||
UUID id = (UUID) platform("rotated-key").get("id");
|
||||
domains.queue(user, project, id, false);
|
||||
when(runtime.request(anyString(), any()))
|
||||
.thenReturn(json.readTree("{\"configured\":true,\"tlsReady\":true,\"routeReady\":true}"));
|
||||
when(resolver.pointsTo(anyString(), anyString())).thenReturn(true);
|
||||
when(dns.ensure(anyMap(), anyString(), anyString())).thenReturn("owned");
|
||||
poll();
|
||||
domains.queue(user, project, id, true);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE project_domains SET dns_identity_digest=? WHERE id=?",
|
||||
SecurityConfig.hash("different-key"),
|
||||
id);
|
||||
when(runtime.request(anyString(), any())).thenReturn(json.readTree("{\"removed\":true}"));
|
||||
var row = coordinator.claim();
|
||||
assertThrows(ApiError.Failure.class, () -> coordinator.execute(row));
|
||||
verify(dns, never()).remove(anyMap(), anyString(), anyString(), anyString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@ActiveProfiles("test")
|
||||
class ExecutionAvailabilityTest {
|
||||
@Autowired IdentityService users;
|
||||
@Autowired WalletService wallets;
|
||||
@Autowired TaskService tasks;
|
||||
@Autowired ExecutionAvailability availability;
|
||||
@Autowired MockMvc mvc;
|
||||
@Autowired ObjectMapper json;
|
||||
@MockitoBean EphemeralStore ephemeral;
|
||||
@MockitoBean SandboxClient sandbox;
|
||||
UUID user, project, model;
|
||||
String token;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
TestDatabase.clear(users.db());
|
||||
reset(sandbox);
|
||||
user = users.create("availability@test.com", "Availability", "StrongPassword123", "test");
|
||||
token =
|
||||
users
|
||||
.login("availability@test.com", "StrongPassword123", "test", "test", 1)
|
||||
.get("token")
|
||||
.toString();
|
||||
project = UUID.randomUUID();
|
||||
model = UUID.randomUUID();
|
||||
UUID provider = UUID.randomUUID(), price = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,'Test','','Vue','')",
|
||||
project,
|
||||
user);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_providers(id,name,base_url,api_type,encrypted_key,enabled,timeout_seconds)"
|
||||
+ " VALUES (?,'Fixture','https://api.openai.com/v1','OPENAI','fixture',TRUE,30)",
|
||||
provider);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_models(id,provider_id,name,model_id,context_length,max_output,tools_enabled,vision_enabled,enabled)"
|
||||
+ " VALUES (?,?,'Fixture','fixture',8192,256,TRUE,FALSE,TRUE)",
|
||||
model,
|
||||
provider);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_model_price_versions(id,model_id,input_price,output_price,cache_read_price,cache_write_price)"
|
||||
+ " VALUES (?,?,1,2,0,0)",
|
||||
price,
|
||||
model);
|
||||
users.db().update("UPDATE ai_models SET current_price_version=? WHERE id=?", price, model);
|
||||
wallets.adjust(user, new BigDecimal("100"), "credit", "test", user, "test");
|
||||
}
|
||||
|
||||
String request() throws Exception {
|
||||
return json.writeValueAsString(
|
||||
Map.of(
|
||||
"projectId",
|
||||
project,
|
||||
"modelId",
|
||||
model,
|
||||
"prompt",
|
||||
"Build test",
|
||||
"budget",
|
||||
50,
|
||||
"idempotencyKey",
|
||||
"task"));
|
||||
}
|
||||
|
||||
void heartbeat(String status, int age) {
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO worker_presence(instance_id,kind,status,updated_at) VALUES (?,'AI',?,?)",
|
||||
UUID.randomUUID(),
|
||||
status,
|
||||
Timestamp.from(Instant.now().minusSeconds(age)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void offlineSubmissionDoesNotReserveFunds() throws Exception {
|
||||
mvc.perform(
|
||||
post("/api/tasks")
|
||||
.header("Authorization", "Bearer " + token)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(request()))
|
||||
.andExpect(status().isServiceUnavailable());
|
||||
assertEquals(0, users.db().queryForObject("SELECT COUNT(*) FROM ai_tasks", Integer.class));
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void statusIsAuthenticatedAndReportsNoWorker() throws Exception {
|
||||
mvc.perform(get("/api/tasks/execution-status")).andExpect(status().isUnauthorized());
|
||||
mvc.perform(get("/api/tasks/execution-status").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.ready").value(false))
|
||||
.andExpect(jsonPath("$.state").value("OFFLINE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void staleStoppedAndUnhealthyWorkersCannotAcceptBudget() {
|
||||
heartbeat("READY", 60);
|
||||
heartbeat("STOPPED", 0);
|
||||
assertEquals(false, availability.status().get("ready"));
|
||||
heartbeat("DEGRADED", 0);
|
||||
assertEquals("ENVIRONMENT_UNAVAILABLE", availability.status().get("state"));
|
||||
assertThrows(ApiError.Failure.class, availability::requireReady);
|
||||
}
|
||||
|
||||
@Test
|
||||
void readyRemoteWorkerAcceptsTaskAndOfflineReplayDoesNotReserveAgain() throws Exception {
|
||||
heartbeat("READY", 0);
|
||||
mvc.perform(
|
||||
post("/api/tasks")
|
||||
.header("Authorization", "Bearer " + token)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(request()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value("QUEUED"));
|
||||
users.db().update("DELETE FROM worker_presence");
|
||||
mvc.perform(
|
||||
post("/api/tasks")
|
||||
.header("Authorization", "Bearer " + token)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(request()))
|
||||
.andExpect(status().isOk());
|
||||
assertEquals(
|
||||
1, users.db().queryForObject("SELECT COUNT(*) FROM wallet_reservations", Integer.class));
|
||||
assertEquals(0, new BigDecimal("50").compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void offlinePauseAndCancelStillReleaseExistingReservation() throws Exception {
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
tasks
|
||||
.create(user, project, model, "Build test", new BigDecimal("50"), "task")
|
||||
.get("id")
|
||||
.toString());
|
||||
mvc.perform(post("/api/tasks/" + id + "/pause").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isOk());
|
||||
mvc.perform(post("/api/tasks/" + id + "/resume").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isServiceUnavailable());
|
||||
mvc.perform(post("/api/tasks/" + id + "/cancel").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isOk());
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
assertEquals(0, new BigDecimal("100").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void workerPresenceRequiresRealProbeAndGracefulStopIsNotReady() {
|
||||
var presence = new WorkerPresence(users.db(), sandbox);
|
||||
doThrow(new ApiError.Failure(503, "fixture")).when(sandbox).probe();
|
||||
presence.beat();
|
||||
assertEquals(false, availability.status().get("ready"));
|
||||
doNothing().when(sandbox).probe();
|
||||
presence.beat();
|
||||
assertEquals(true, availability.status().get("ready"));
|
||||
presence.stop();
|
||||
assertEquals(false, availability.status().get("ready"));
|
||||
verify(sandbox, times(2)).probe();
|
||||
}
|
||||
|
||||
@Test
|
||||
void workerInventoryRequiresOperationsPermission() throws Exception {
|
||||
mvc.perform(get("/api/admin/workers").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isForbidden());
|
||||
users.db().update("INSERT INTO user_roles VALUES (?,'OPS')", user);
|
||||
heartbeat("READY", 0);
|
||||
mvc.perform(get("/api/admin/workers").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.workers[0].fresh").value(true))
|
||||
.andExpect(jsonPath("$.execution.ready").value(true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.*;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
@AutoConfigureMockMvc
|
||||
class ExtensionIntegrationTest {
|
||||
@Autowired MockMvc mvc;
|
||||
@Autowired JdbcTemplate db;
|
||||
@Autowired ObjectMapper json;
|
||||
@MockitoBean EphemeralStore ephemeral;
|
||||
String owner, admin, user;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
TestDatabase.clear(db);
|
||||
owner = account("SUPER_ADMIN");
|
||||
admin = account("ADMIN");
|
||||
user = account("USER");
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadRequiresOwnerPrivilegesAndValidatesBoundedPackages() throws Exception {
|
||||
String manifest = manifest("plugin", "studio.tools", "1.0.0");
|
||||
byte[] valid = zip(Map.of("manifest.json", manifest, "index.html", "<p>Tool</p>"));
|
||||
upload(user, valid).andExpect(status().isForbidden());
|
||||
upload(admin, valid).andExpect(status().isForbidden());
|
||||
mvc.perform(get("/api/extensions/plugins")).andExpect(status().isUnauthorized());
|
||||
upload(owner, zip(Map.of("manifest.json", manifest, "../index.html", "<p>Tool</p>")))
|
||||
.andExpect(status().isBadRequest());
|
||||
upload(owner, zip(Map.of("manifest.json", manifest, "index.html", "a".repeat(262145))))
|
||||
.andExpect(status().isBadRequest());
|
||||
upload(owner, new byte[524289]).andExpect(status().isPayloadTooLarge());
|
||||
upload(
|
||||
owner,
|
||||
zip(
|
||||
Map.of(
|
||||
"manifest.json",
|
||||
manifest.replace("\"apiVersion\":1", "\"apiVersion\":2"),
|
||||
"index.html",
|
||||
"<p>Tool</p>")))
|
||||
.andExpect(status().isBadRequest());
|
||||
upload(
|
||||
owner,
|
||||
zip(
|
||||
Map.of(
|
||||
"manifest.json",
|
||||
manifest.replace("\"apiVersion\":1", "\"apiVersion\":2,\"apiVersion\":1"),
|
||||
"index.html",
|
||||
"<p>Tool</p>")))
|
||||
.andExpect(status().isBadRequest());
|
||||
assertEquals(0, db.queryForObject("SELECT COUNT(*) FROM extension_packages", Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void activationMakesOnlyCurrentPluginVersionFrameableAndCanBeRolledBack() throws Exception {
|
||||
byte[] archive = plugin("1.0.0", "<button>V1</button>");
|
||||
String first = installed(archive);
|
||||
assertEquals(first, installed(archive));
|
||||
upload(owner, plugin("1.0.0", "<button>Changed</button>")).andExpect(status().isConflict());
|
||||
String next = installed(plugin("1.0.1", "<button>V2</button>"));
|
||||
mvc.perform(get("/api/extensions/plugins").header("Authorization", user))
|
||||
.andExpect(content().json("[]"));
|
||||
frame(first).andExpect(status().isNotFound());
|
||||
activation(first, true, admin).andExpect(status().isForbidden());
|
||||
activation(first, true, owner).andExpect(status().isOk());
|
||||
mvc.perform(get("/api/extensions/plugins").header("Authorization", user))
|
||||
.andExpect(jsonPath("$[0].version").value("1.0.0"))
|
||||
.andExpect(header().string("X-Frame-Options", "DENY"))
|
||||
.andExpect(jsonPath("$[0].payload").doesNotExist());
|
||||
mvc.perform(get("/api/public/extensions/plugin-frame/" + first))
|
||||
.andExpect(status().isNotFound());
|
||||
mvc.perform(
|
||||
get("/api/public/extensions/plugin-frame/" + first)
|
||||
.header("Sec-Fetch-Dest", "iframe")
|
||||
.header("Sec-Fetch-Site", "cross-site"))
|
||||
.andExpect(status().isNotFound());
|
||||
frame(first)
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(header().string("X-Frame-Options", "SAMEORIGIN"))
|
||||
.andExpect(header().string("Cache-Control", "no-store"))
|
||||
.andExpect(
|
||||
header()
|
||||
.string(
|
||||
"Content-Security-Policy",
|
||||
containsString("sandbox allow-scripts; default-src 'none'")))
|
||||
.andExpect(content().string("<button>V1</button>"));
|
||||
activation(next, true, owner).andExpect(status().isOk());
|
||||
frame(first).andExpect(status().isNotFound());
|
||||
frame(next).andExpect(content().string("<button>V2</button>"));
|
||||
mvc.perform(delete("/api/admin/extensions/" + next).header("Authorization", owner))
|
||||
.andExpect(status().isConflict());
|
||||
activation(first, true, owner).andExpect(status().isOk());
|
||||
frame(first).andExpect(status().isOk());
|
||||
mvc.perform(delete("/api/admin/extensions/" + next).header("Authorization", owner))
|
||||
.andExpect(status().isOk());
|
||||
assertEquals(
|
||||
6,
|
||||
db.queryForObject(
|
||||
"SELECT COUNT(*) FROM admin_operation_logs WHERE action LIKE 'EXTENSION_%'",
|
||||
Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void themeSupportsSafeColorsAndOneGlobalSelectionAndReset() throws Exception {
|
||||
String one = manifest("theme", "studio.one", "1.0.0");
|
||||
upload(
|
||||
owner,
|
||||
zip(
|
||||
Map.of(
|
||||
"manifest.json",
|
||||
one,
|
||||
"theme.json",
|
||||
"""
|
||||
{"light":{"accent":"url(https://example.test/pixel)"}}\
|
||||
""")))
|
||||
.andExpect(status().isBadRequest());
|
||||
upload(
|
||||
owner,
|
||||
zip(
|
||||
Map.of(
|
||||
"manifest.json",
|
||||
one,
|
||||
"theme.json",
|
||||
"""
|
||||
{"light":{"unknown":"#ffffff"}}\
|
||||
""")))
|
||||
.andExpect(status().isBadRequest());
|
||||
String first =
|
||||
installed(
|
||||
zip(
|
||||
Map.of(
|
||||
"manifest.json",
|
||||
one,
|
||||
"theme.json",
|
||||
"""
|
||||
{"light":{"accent":"#297763"},"dark":{"text":"#ffffff"}}\
|
||||
""")));
|
||||
String next =
|
||||
installed(
|
||||
zip(
|
||||
Map.of(
|
||||
"manifest.json",
|
||||
manifest("theme", "studio.two", "1.0.0"),
|
||||
"theme.json",
|
||||
"""
|
||||
{"light":{"accent":"#ab2817"}}\
|
||||
""")));
|
||||
mvc.perform(get("/api/public/extensions/theme")).andExpect(content().json("{}"));
|
||||
activation(first, true, owner).andExpect(status().isOk());
|
||||
mvc.perform(get("/api/public/extensions/theme"))
|
||||
.andExpect(jsonPath("$.palettes.light.accent").value("#297763"))
|
||||
.andExpect(jsonPath("$.palettes.dark.text").value("#ffffff"));
|
||||
activation(next, true, owner).andExpect(status().isOk());
|
||||
mvc.perform(get("/api/public/extensions/theme")).andExpect(jsonPath("$.id").value(next));
|
||||
assertEquals(
|
||||
1,
|
||||
db.queryForObject(
|
||||
"SELECT COUNT(*) FROM extension_packages WHERE kind='theme' AND enabled=TRUE",
|
||||
Integer.class));
|
||||
activation(next, false, owner).andExpect(status().isOk());
|
||||
mvc.perform(get("/api/public/extensions/theme")).andExpect(content().json("{}"));
|
||||
}
|
||||
|
||||
private String account(String role) {
|
||||
UUID id = UUID.randomUUID();
|
||||
String token = UUID.randomUUID().toString();
|
||||
db.update(
|
||||
"INSERT INTO users(id,email,display_name,password_hash,registration_ip) VALUES (?,?,?,?,?)",
|
||||
id,
|
||||
role + "@example.test",
|
||||
role,
|
||||
"fixture",
|
||||
"test");
|
||||
db.update("INSERT INTO user_roles VALUES (?,?)", id, role);
|
||||
db.update(
|
||||
"INSERT INTO user_sessions(id,user_id,token_hash,ip,user_agent,expires_at) VALUES"
|
||||
+ " (?,?,?,?,?,?)",
|
||||
UUID.randomUUID(),
|
||||
id,
|
||||
SecurityConfig.hash(token),
|
||||
"test",
|
||||
"test",
|
||||
OffsetDateTime.now().plusHours(1));
|
||||
return "Bearer " + token;
|
||||
}
|
||||
|
||||
private ResultActions upload(String token, byte[] bytes) throws Exception {
|
||||
return mvc.perform(
|
||||
multipart("/api/admin/extensions")
|
||||
.file(new MockMultipartFile("file", "extension.zip", "application/zip", bytes))
|
||||
.header("Authorization", token));
|
||||
}
|
||||
|
||||
private String installed(byte[] bytes) throws Exception {
|
||||
var response = upload(owner, bytes).andExpect(status().isOk()).andReturn().getResponse();
|
||||
return json.readTree(response.getContentAsByteArray()).path("id").asText();
|
||||
}
|
||||
|
||||
private ResultActions activation(String id, boolean enabled, String token) throws Exception {
|
||||
return mvc.perform(
|
||||
put("/api/admin/extensions/" + id + "/activation")
|
||||
.header("Authorization", token)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"enabled\":" + enabled + "}"));
|
||||
}
|
||||
|
||||
private ResultActions frame(String id) throws Exception {
|
||||
return mvc.perform(
|
||||
get("/api/public/extensions/plugin-frame/" + id)
|
||||
.header("Sec-Fetch-Dest", "iframe")
|
||||
.header("Sec-Fetch-Site", "same-origin"));
|
||||
}
|
||||
|
||||
private static String manifest(String kind, String slug, String version) {
|
||||
return """
|
||||
{"apiVersion":1,"type":"%s","id":"%s","name":"Sample","version":"%s"}\
|
||||
"""
|
||||
.formatted(kind, slug, version);
|
||||
}
|
||||
|
||||
private static byte[] plugin(String version, String html) throws Exception {
|
||||
return zip(
|
||||
Map.of("manifest.json", manifest("plugin", "studio.tools", version), "index.html", html));
|
||||
}
|
||||
|
||||
private static byte[] zip(Map<String, String> files) throws Exception {
|
||||
var output = new ByteArrayOutputStream();
|
||||
try (var zip = new ZipOutputStream(output)) {
|
||||
for (var file : files.entrySet()) {
|
||||
zip.putNextEntry(new ZipEntry(file.getKey()));
|
||||
zip.write(file.getValue().getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
}
|
||||
}
|
||||
return output.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
|
||||
@SpringBootTest(properties = "app.encryption-key=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
|
||||
@ActiveProfiles("test")
|
||||
class HostingIntegrationTest {
|
||||
@Autowired IdentityService users;
|
||||
@Autowired WalletService wallets;
|
||||
@Autowired HostingService hosting;
|
||||
@Autowired DeploymentCoordinator deployments;
|
||||
@Autowired ObjectMapper json;
|
||||
@MockitoBean EphemeralStore store;
|
||||
UUID user, project, plan;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
TestDatabase.clear(users.db());
|
||||
user = users.create("hosting@test.com", "Host", "StrongPassword123", "test");
|
||||
project = UUID.randomUUID();
|
||||
plan = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,?,'','Node.js','')",
|
||||
project,
|
||||
user,
|
||||
"Host");
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " billing_plans(id,name,monthly_price,cpu_millis,memory_mb,disk_gb,traffic_gb,max_services,max_replicas)"
|
||||
+ " VALUES (?,'Test',10,1000,1024,5,5,3,2)",
|
||||
plan);
|
||||
users
|
||||
.db()
|
||||
.update("INSERT INTO project_moderation(project_id,status) VALUES (?,'APPROVED')", project);
|
||||
wallets.adjust(user, new BigDecimal("100"), "credit", "test", user, "test");
|
||||
}
|
||||
|
||||
HostingController.ServiceSpec spec(int cpu) {
|
||||
return new HostingController.ServiceSpec(
|
||||
"api",
|
||||
"registry.example.com/test@sha256:" + "a".repeat(64),
|
||||
List.of(),
|
||||
Map.of("DATABASE_PASSWORD", "project-test-secret"),
|
||||
3000,
|
||||
cpu,
|
||||
256,
|
||||
1,
|
||||
"/health",
|
||||
true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void purchaseIsIdempotentAndSnapshotsPlan() {
|
||||
hosting.subscribe(user, project, plan, "first");
|
||||
hosting.subscribe(user, project, plan, "first");
|
||||
assertEquals(0, new BigDecimal("90").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
users.db().update("UPDATE billing_plans SET monthly_price=99 WHERE id=?", plan);
|
||||
assertTrue(
|
||||
hosting
|
||||
.subscription(user, project)
|
||||
.get("plan_snapshot")
|
||||
.toString()
|
||||
.contains("10.00000000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void quotaRejectsDeploymentWithoutChargingMore() {
|
||||
hosting.subscribe(user, project, plan, "first");
|
||||
hosting.service(user, project, null, spec(2000));
|
||||
assertThrows(ApiError.Failure.class, () -> hosting.deploy(user, project, "deploy", null));
|
||||
assertEquals(
|
||||
0, users.db().queryForObject("SELECT COUNT(*) FROM project_deployments", Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledPlanStillAllowsReplayOfAlreadyPaidPurchase() {
|
||||
hosting.subscribe(user, project, plan, "paid");
|
||||
users.db().update("UPDATE billing_plans SET enabled=FALSE WHERE id=?", plan);
|
||||
hosting.subscribe(user, project, plan, "paid");
|
||||
assertEquals(0, new BigDecimal("90").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertThrows(
|
||||
ApiError.Failure.class, () -> hosting.subscribe(user, project, plan, "new-purchase"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rollbackCannotBypassCurrentSubscriptionQuota() throws Exception {
|
||||
hosting.subscribe(user, project, plan, "plan");
|
||||
hosting.service(user, project, null, spec(2000));
|
||||
var old = users.db().queryForList("SELECT * FROM project_services WHERE project_id=?", project);
|
||||
UUID previous = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " project_deployments(id,project_id,user_id,spec_json,status,idempotency_key)"
|
||||
+ " VALUES (?,?,?,?,'ROLLED_BACK','old')",
|
||||
previous,
|
||||
project,
|
||||
user,
|
||||
json.writeValueAsString(old));
|
||||
assertThrows(ApiError.Failure.class, () -> hosting.deploy(user, project, "rollback", previous));
|
||||
assertEquals(
|
||||
1, users.db().queryForObject("SELECT COUNT(*) FROM project_deployments", Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void encryptedEnvironmentNeverReturnedByServiceList() {
|
||||
var service = hosting.service(user, project, null, spec(200));
|
||||
assertFalse(service.containsKey("encrypted_environment"));
|
||||
String encrypted =
|
||||
users
|
||||
.db()
|
||||
.queryForObject("SELECT encrypted_environment FROM project_services", String.class);
|
||||
assertFalse(encrypted.contains("project-test-secret"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deploymentSnapshotActivationAndRollback() throws Exception {
|
||||
hosting.subscribe(user, project, plan, "first");
|
||||
var service = hosting.service(user, project, null, spec(200));
|
||||
UUID id = UUID.fromString(hosting.deploy(user, project, "release1", null).get("id").toString());
|
||||
var d = deployments.claim();
|
||||
assertEquals(id.toString(), d.get("id").toString());
|
||||
assertEquals(
|
||||
"project-test-secret",
|
||||
deployments
|
||||
.payload(d)
|
||||
.path("services")
|
||||
.path(0)
|
||||
.path("environment")
|
||||
.path("DATABASE_PASSWORD")
|
||||
.asText());
|
||||
deployments.finish(id, d.get("lease_token").toString(), json.readTree("{\"ready\":true}"));
|
||||
UUID next = UUID.fromString(hosting.deploy(user, project, "rollback", id).get("id").toString());
|
||||
var r = deployments.claim();
|
||||
deployments.finish(next, r.get("lease_token").toString(), json.readTree("{\"ready\":true}"));
|
||||
assertEquals(
|
||||
"ROLLED_BACK",
|
||||
users
|
||||
.db()
|
||||
.queryForObject("SELECT status FROM project_deployments WHERE id=?", String.class, id));
|
||||
assertEquals(
|
||||
"ACTIVE",
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT status FROM project_deployments WHERE id=?", String.class, next));
|
||||
}
|
||||
|
||||
@Test
|
||||
void crossOwnerCannotConfigureOrBuy() {
|
||||
UUID stranger = users.create("stranger@test.com", "Other", "StrongPassword123", "test");
|
||||
assertThrows(ApiError.Failure.class, () -> hosting.subscribe(stranger, project, plan, "other"));
|
||||
assertThrows(ApiError.Failure.class, () -> hosting.services(stranger, project));
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedHealthCheckLeavesCurrentReleaseActive() throws Exception {
|
||||
hosting.subscribe(user, project, plan, "first");
|
||||
hosting.service(user, project, null, spec(200));
|
||||
UUID first =
|
||||
UUID.fromString(hosting.deploy(user, project, "first-deploy", null).get("id").toString());
|
||||
var a = deployments.claim();
|
||||
deployments.finish(first, a.get("lease_token").toString(), json.readTree("{\"ready\":true}"));
|
||||
UUID second =
|
||||
UUID.fromString(hosting.deploy(user, project, "second-deploy", null).get("id").toString());
|
||||
var b = deployments.claim();
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() ->
|
||||
deployments.finish(
|
||||
second, b.get("lease_token").toString(), json.readTree("{\"ready\":false}")));
|
||||
deployments.fail(second, b.get("lease_token").toString(), "health failed");
|
||||
assertEquals(
|
||||
"ACTIVE",
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT status FROM project_deployments WHERE id=?", String.class, first));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,371 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.*;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@ActiveProfiles("test")
|
||||
class IdentityIntegrationTest {
|
||||
@DynamicPropertySource
|
||||
static void database(DynamicPropertyRegistry registry) {
|
||||
String url = System.getenv("TEST_DATABASE_URL");
|
||||
if (url != null && !url.isBlank()) {
|
||||
registry.add("spring.datasource.url", () -> url);
|
||||
registry.add("spring.datasource.username", () -> System.getenv("TEST_DATABASE_USER"));
|
||||
registry.add("spring.datasource.password", () -> System.getenv("TEST_DATABASE_PASSWORD"));
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired MockMvc mvc;
|
||||
@Autowired IdentityService users;
|
||||
@Autowired ObjectMapper json;
|
||||
@MockitoBean EphemeralStore store;
|
||||
final Map<String, String> ephemeral = new ConcurrentHashMap<>();
|
||||
final String password = "LongPassword123!";
|
||||
|
||||
@BeforeEach
|
||||
void reset() {
|
||||
TestDatabase.clear(users.db());
|
||||
ephemeral.clear();
|
||||
org.mockito.Mockito.reset(store);
|
||||
doAnswer(
|
||||
i -> {
|
||||
ephemeral.put(i.getArgument(0), i.getArgument(1));
|
||||
return null;
|
||||
})
|
||||
.when(store)
|
||||
.put(anyString(), anyString(), any());
|
||||
when(store.consume(anyString())).thenAnswer(i -> ephemeral.remove(i.getArgument(0)));
|
||||
}
|
||||
|
||||
String captcha() {
|
||||
String id = UUID.randomUUID().toString();
|
||||
ephemeral.put("captcha:" + id, SecurityConfig.hash("ABCDE"));
|
||||
return id;
|
||||
}
|
||||
|
||||
String credentials(String email) {
|
||||
return jsonValue(
|
||||
Map.of(
|
||||
"email", email, "password", password, "captchaId", captcha(), "captchaCode", "ABCDE"));
|
||||
}
|
||||
|
||||
String jsonValue(Object value) {
|
||||
try {
|
||||
return json.writeValueAsString(value);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
UUID user(String email) {
|
||||
return users.create(email, "Tester", password, "127.0.0.1");
|
||||
}
|
||||
|
||||
String token(String email) throws Exception {
|
||||
String result =
|
||||
mvc.perform(
|
||||
post("/api/auth/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(credentials(email)))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
return json.readTree(result).get("token").asText();
|
||||
}
|
||||
|
||||
@Test
|
||||
void captchaIsServerGeneratedAndOneUse() throws Exception {
|
||||
String result =
|
||||
mvc.perform(get("/api/auth/captcha"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(
|
||||
jsonPath("$.image")
|
||||
.value(org.hamcrest.Matchers.startsWith("data:image/png;base64,")))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
String id = json.readTree(result).get("id").asText();
|
||||
assertEquals(64, ephemeral.get("captcha:" + id).length());
|
||||
assertFalse(result.contains(ephemeral.get("captcha:" + id)));
|
||||
user("one@test.com");
|
||||
String payload = credentials("one@test.com");
|
||||
mvc.perform(post("/api/auth/login").contentType(MediaType.APPLICATION_JSON).content(payload))
|
||||
.andExpect(status().isOk());
|
||||
mvc.perform(post("/api/auth/login").contentType(MediaType.APPLICATION_JSON).content(payload))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void wrongCaptchaAlsoConsumesChallenge() throws Exception {
|
||||
String id = captcha();
|
||||
for (String code : List.of("WRONG", "ABCDE"))
|
||||
mvc.perform(
|
||||
post("/api/auth/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(
|
||||
jsonValue(
|
||||
Map.of(
|
||||
"email",
|
||||
"one@test.com",
|
||||
"password",
|
||||
password,
|
||||
"captchaId",
|
||||
id,
|
||||
"captchaCode",
|
||||
code))))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerPersistsHashAndRejectsDuplicate() throws Exception {
|
||||
for (int i = 0; i < 2; i++)
|
||||
mvc.perform(
|
||||
post("/api/auth/register")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(
|
||||
jsonValue(
|
||||
Map.of(
|
||||
"email",
|
||||
"new@test.com",
|
||||
"password",
|
||||
password,
|
||||
"displayName",
|
||||
"新用户",
|
||||
"captchaId",
|
||||
captcha(),
|
||||
"captchaCode",
|
||||
"ABCDE"))))
|
||||
.andExpect(status().is(i == 0 ? 200 : 409));
|
||||
String hash =
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT password_hash FROM users WHERE email='new@test.com'", String.class);
|
||||
assertNotEquals(password, hash);
|
||||
assertTrue(users.encoder().matches(password, hash));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsWeakPasswordsAndDisabledRegistration() throws Exception {
|
||||
assertThrows(ApiError.Failure.class, () -> userWithPassword("weak"));
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE system_settings SET setting_value='false' WHERE"
|
||||
+ " setting_key='registrationEnabled'");
|
||||
try {
|
||||
mvc.perform(
|
||||
post("/api/auth/register")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(
|
||||
jsonValue(
|
||||
Map.of(
|
||||
"email",
|
||||
"new@test.com",
|
||||
"password",
|
||||
password,
|
||||
"displayName",
|
||||
"新用户",
|
||||
"captchaId",
|
||||
captcha(),
|
||||
"captchaCode",
|
||||
"ABCDE"))))
|
||||
.andExpect(status().isForbidden());
|
||||
} finally {
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE system_settings SET setting_value='true' WHERE"
|
||||
+ " setting_key='registrationEnabled'");
|
||||
}
|
||||
}
|
||||
|
||||
void userWithPassword(String pass) {
|
||||
users.create("weak@test.com", "weak", pass, "127.0.0.1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void sessionRevocationAndAdminBoundaries() throws Exception {
|
||||
user("normal@test.com");
|
||||
String t = token("normal@test.com");
|
||||
mvc.perform(get("/api/me").header("Authorization", "Bearer " + t))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.email").value("normal@test.com"));
|
||||
mvc.perform(get("/api/admin/users").header("Authorization", "Bearer " + t))
|
||||
.andExpect(status().isForbidden());
|
||||
mvc.perform(post("/api/auth/logout").header("Authorization", "Bearer " + t))
|
||||
.andExpect(status().isOk());
|
||||
mvc.perform(get("/api/me").header("Authorization", "Bearer " + t))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
void noAuthenticationOnPrivateEndpoints() throws Exception {
|
||||
mvc.perform(get("/api/projects")).andExpect(status().isUnauthorized());
|
||||
mvc.perform(get("/api/admin/settings")).andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
void passwordChangeRevokesAllSessions() throws Exception {
|
||||
user("secure@test.com");
|
||||
String a = token("secure@test.com"), b = token("secure@test.com");
|
||||
mvc.perform(
|
||||
post("/api/account/password")
|
||||
.header("Authorization", "Bearer " + a)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(
|
||||
jsonValue(
|
||||
Map.of(
|
||||
"currentPassword", password, "newPassword", "DifferentPassword456"))))
|
||||
.andExpect(status().isOk());
|
||||
for (String t : List.of(a, b))
|
||||
mvc.perform(get("/api/me").header("Authorization", "Bearer " + t))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
void projectsAndDevicesAreIsolated() throws Exception {
|
||||
user("a@test.com");
|
||||
user("b@test.com");
|
||||
String a = token("a@test.com"), b = token("b@test.com");
|
||||
var created =
|
||||
mvc.perform(
|
||||
post("/api/projects")
|
||||
.header("Authorization", "Bearer " + a)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(
|
||||
jsonValue(
|
||||
Map.of(
|
||||
"name",
|
||||
"Real project",
|
||||
"description",
|
||||
"Persistent",
|
||||
"stack",
|
||||
"Vue 3",
|
||||
"requirements",
|
||||
"Build a forum"))))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn();
|
||||
String id = json.readTree(created.getResponse().getContentAsString()).get("id").asText();
|
||||
mvc.perform(get("/api/projects/" + id).header("Authorization", "Bearer " + b))
|
||||
.andExpect(status().isNotFound());
|
||||
mvc.perform(post("/api/projects/" + id + "/archive").header("Authorization", "Bearer " + b))
|
||||
.andExpect(status().isNotFound());
|
||||
String sid =
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT s.id FROM user_sessions s JOIN users u ON u.id=s.user_id WHERE"
|
||||
+ " u.email='a@test.com'",
|
||||
String.class);
|
||||
mvc.perform(delete("/api/account/sessions/" + sid).header("Authorization", "Bearer " + b))
|
||||
.andExpect(status().isNotFound());
|
||||
mvc.perform(get("/api/projects/" + id).header("Authorization", "Bearer " + a))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminSettingsAuditedAndDoNotAcceptUnknownKeys() throws Exception {
|
||||
UUID id = user("admin@test.com");
|
||||
users.db().update("INSERT INTO user_roles VALUES (?, 'SUPER_ADMIN')", id);
|
||||
String t = token("admin@test.com");
|
||||
mvc.perform(
|
||||
put("/api/admin/settings")
|
||||
.header("Authorization", "Bearer " + t)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"footer\":\"A tested footer\"}"))
|
||||
.andExpect(status().isOk());
|
||||
assertEquals(
|
||||
1,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM admin_operation_logs WHERE action='SETTING_UPDATE'",
|
||||
Integer.class));
|
||||
mvc.perform(
|
||||
put("/api/admin/settings")
|
||||
.header("Authorization", "Bearer " + t)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"apiKey\":\"secret\"}"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void disableUserRevokesSessionsImmediately() throws Exception {
|
||||
UUID admin = user("admin@test.com"), target = user("target@test.com");
|
||||
users.db().update("INSERT INTO user_roles VALUES (?, 'ADMIN')", admin);
|
||||
String a = token("admin@test.com"), b = token("target@test.com");
|
||||
mvc.perform(
|
||||
put("/api/admin/users/" + target + "/status")
|
||||
.header("Authorization", "Bearer " + a)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"status\":\"DISABLED\",\"reason\":\"security test\"}"))
|
||||
.andExpect(status().isOk());
|
||||
mvc.perform(get("/api/me").header("Authorization", "Bearer " + b))
|
||||
.andExpect(status().isUnauthorized());
|
||||
mvc.perform(
|
||||
post("/api/auth/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(credentials("target@test.com")))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rateLimitIsNotBypassed() throws Exception {
|
||||
doThrow(new ApiError.Failure(429, "限流"))
|
||||
.when(store)
|
||||
.limit(startsWith("auth-ip:"), anyInt(), anyInt());
|
||||
mvc.perform(
|
||||
post("/api/auth/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(credentials("any@test.com")))
|
||||
.andExpect(status().isTooManyRequests());
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidSettingsBatchRollsBackDataAndAudit() throws Exception {
|
||||
UUID id = user("admin@test.com");
|
||||
users.db().update("INSERT INTO user_roles VALUES (?, 'SUPER_ADMIN')", id);
|
||||
String t = token("admin@test.com");
|
||||
String before =
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT setting_value FROM system_settings WHERE setting_key='footer'",
|
||||
String.class);
|
||||
mvc.perform(
|
||||
put("/api/admin/settings")
|
||||
.header("Authorization", "Bearer " + t)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"footer\":\"must roll back\",\"unsupported\":\"value\"}"))
|
||||
.andExpect(status().isBadRequest());
|
||||
assertEquals(
|
||||
before,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT setting_value FROM system_settings WHERE setting_key='footer'",
|
||||
String.class));
|
||||
assertEquals(
|
||||
0, users.db().queryForObject("SELECT COUNT(*) FROM admin_operation_logs", Integer.class));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
|
||||
@SpringBootTest(properties = "app.encryption-key=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
|
||||
@ActiveProfiles("test")
|
||||
class IntegrationModerationTest {
|
||||
@Autowired IntegrationService integrations;
|
||||
@Autowired IdentityService users;
|
||||
@Autowired SecretVault vault;
|
||||
@Autowired SiteModerationService moderation;
|
||||
@Autowired RuntimeOperationService operations;
|
||||
@Autowired HostingService hosting;
|
||||
@Autowired ObjectMapper json;
|
||||
@Autowired StorageService storage;
|
||||
@MockitoBean EphemeralStore ephemeral;
|
||||
@MockitoBean AliyunGateway aliyun;
|
||||
@MockitoBean MailGateway mail;
|
||||
@MockitoBean ObjectStorageGateway objects;
|
||||
UUID user, project;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
TestDatabase.clear(users.db());
|
||||
user = users.create("operator@test.com", "Operator", "StrongPassword123", "test");
|
||||
project = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,?,'','HTML','')",
|
||||
project,
|
||||
user,
|
||||
"Site");
|
||||
users
|
||||
.db()
|
||||
.update("INSERT INTO project_moderation(project_id,status) VALUES (?,'PENDING')", project);
|
||||
org.mockito.Mockito.reset(aliyun, mail, objects);
|
||||
}
|
||||
|
||||
Map<String, String> sms() {
|
||||
return Map.of(
|
||||
"accessKeyId",
|
||||
"fictional-ak",
|
||||
"accessKeySecret",
|
||||
"fictional-secret",
|
||||
"regionId",
|
||||
"cn-hangzhou",
|
||||
"endpoint",
|
||||
"dysmsapi.aliyuncs.com",
|
||||
"signName",
|
||||
"测试签名",
|
||||
"templateCode",
|
||||
"SMS_TEST");
|
||||
}
|
||||
|
||||
@Test
|
||||
void credentialsAreEncryptedAndNeverReturned() {
|
||||
integrations.save("ALIYUN_SMS", user, sms(), true, "test");
|
||||
String returned = integrations.list().toString();
|
||||
assertFalse(returned.contains("fictional-secret"));
|
||||
assertFalse(returned.contains("fictional-ak"));
|
||||
String encrypted =
|
||||
users
|
||||
.db()
|
||||
.queryForObject("SELECT encrypted_payload FROM integration_configs", String.class);
|
||||
assertFalse(encrypted.contains("fictional-secret"));
|
||||
assertEquals(
|
||||
"fictional-secret", integrations.secrets("ALIYUN_SMS", true).get("accessKeySecret"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankSecretsPreserveExistingValues() {
|
||||
integrations.save("ALIYUN_SMS", user, sms(), true, "test");
|
||||
var update = new HashMap<>(sms());
|
||||
update.put("accessKeySecret", "");
|
||||
integrations.save("ALIYUN_SMS", user, update, false, "test");
|
||||
assertEquals(
|
||||
"fictional-secret", integrations.secrets("ALIYUN_SMS", false).get("accessKeySecret"));
|
||||
assertThrows(ApiError.Failure.class, () -> integrations.secrets("ALIYUN_SMS", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidPortAndUnknownConfigRejected() {
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() ->
|
||||
integrations.save(
|
||||
"SMTP",
|
||||
user,
|
||||
Map.of(
|
||||
"host",
|
||||
"localhost",
|
||||
"port",
|
||||
"99999",
|
||||
"fromEmail",
|
||||
"test@example.test",
|
||||
"fromName",
|
||||
"Test",
|
||||
"security",
|
||||
"TLS"),
|
||||
true,
|
||||
"test"));
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() -> integrations.save("ALIYUN_SMS", user, Map.of("unknown", "value"), false, "test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void connectionTestsRecordRealAdapterOutcome() {
|
||||
integrations.save("ALIYUN_SMS", user, sms(), true, "test");
|
||||
when(aliyun.call(anyMap(), eq("QuerySmsSignList"), anyString(), anyString(), anyMap()))
|
||||
.thenReturn(Map.of("Code", "OK", "RequestId", "fixture-id"));
|
||||
integrations.test("ALIYUN_SMS", user, "test");
|
||||
assertEquals(
|
||||
"CONNECTED",
|
||||
users
|
||||
.db()
|
||||
.queryForObject("SELECT connection_status FROM integration_configs", String.class));
|
||||
verify(aliyun).call(anyMap(), eq("QuerySmsSignList"), eq("2017-05-25"), eq("POST"), anyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedTestCannotPretendConnected() {
|
||||
integrations.save("ALIYUN_SMS", user, sms(), true, "test");
|
||||
when(aliyun.call(anyMap(), anyString(), anyString(), anyString(), anyMap()))
|
||||
.thenThrow(new ApiError.Failure(502, "fixture failure"));
|
||||
assertThrows(ApiError.Failure.class, () -> integrations.test("ALIYUN_SMS", user, "test"));
|
||||
assertEquals(
|
||||
"ERROR",
|
||||
users
|
||||
.db()
|
||||
.queryForObject("SELECT connection_status FROM integration_configs", String.class));
|
||||
assertEquals(
|
||||
1,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM integration_test_logs WHERE status='ERROR'", Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void banQueuesSuspensionAndBlocksPublish() {
|
||||
moderation.review(project, user, "APPROVED", "通过测试审核", "test");
|
||||
operations.checkAllowed(project);
|
||||
moderation.review(project, user, "BANNED", "违规测试", "test");
|
||||
assertThrows(ApiError.Failure.class, () -> operations.checkAllowed(project));
|
||||
assertThrows(ApiError.Failure.class, () -> hosting.deploy(user, project, "blocked", null));
|
||||
assertEquals(
|
||||
"QUEUED", users.db().queryForObject("SELECT status FROM runtime_operations", String.class));
|
||||
assertEquals(
|
||||
1,
|
||||
users
|
||||
.db()
|
||||
.queryForObject("SELECT COUNT(*) FROM projects WHERE id=?", Integer.class, project));
|
||||
}
|
||||
|
||||
@Test
|
||||
void liftBanDoesNotFakeActualRuntimeRecovery() {
|
||||
moderation.review(project, user, "BANNED", "test", "test");
|
||||
moderation.review(project, user, "APPROVED", "人工复核通过", "test");
|
||||
operations.checkAllowed(project);
|
||||
assertEquals(
|
||||
"QUEUED", users.db().queryForObject("SELECT status FROM runtime_operations", String.class));
|
||||
assertEquals(
|
||||
2,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM admin_operation_logs WHERE action='SITE_MODERATION'",
|
||||
Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void objectUploadsUseProjectPrefixAndReplayWithoutDuplicatePut() {
|
||||
integrations.save(
|
||||
"S3",
|
||||
user,
|
||||
Map.of(
|
||||
"endpoint",
|
||||
"http://localhost:9000",
|
||||
"region",
|
||||
"us-east-1",
|
||||
"bucket",
|
||||
"test-bucket",
|
||||
"accessKey",
|
||||
"fixture-access",
|
||||
"secretKey",
|
||||
"fixture-secret"),
|
||||
true,
|
||||
"test");
|
||||
var first =
|
||||
storage.upload(user, project, "file.txt", "text/plain", "hello".getBytes(), "upload");
|
||||
var second =
|
||||
storage.upload(user, project, "file.txt", "text/plain", "hello".getBytes(), "upload");
|
||||
assertEquals(first.get("id"), second.get("id"));
|
||||
verify(objects, times(1))
|
||||
.put(
|
||||
anyMap(),
|
||||
startsWith("projects/" + project + "/objects/"),
|
||||
any(),
|
||||
eq("text/plain"),
|
||||
anyString());
|
||||
assertFalse(first.containsKey("object_key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void objectDeletionIsSoftAndCanBeRestored() {
|
||||
integrations.save(
|
||||
"S3",
|
||||
user,
|
||||
Map.of(
|
||||
"endpoint",
|
||||
"http://localhost:9000",
|
||||
"region",
|
||||
"us-east-1",
|
||||
"bucket",
|
||||
"test-bucket",
|
||||
"accessKey",
|
||||
"fixture-access",
|
||||
"secretKey",
|
||||
"fixture-secret"),
|
||||
true,
|
||||
"test");
|
||||
UUID id =
|
||||
UUID.fromString(
|
||||
storage
|
||||
.upload(user, project, "file.txt", "text/plain", "test".getBytes(), "upload")
|
||||
.get("id")
|
||||
.toString());
|
||||
storage.trash(user, project, id);
|
||||
assertThrows(ApiError.Failure.class, () -> storage.download(user, project, id));
|
||||
storage.restore(user, project, id);
|
||||
assertEquals(
|
||||
"READY",
|
||||
users
|
||||
.db()
|
||||
.queryForObject("SELECT status FROM storage_objects WHERE id=?", String.class, id));
|
||||
verify(objects, never()).purge(anyMap(), anyString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest(properties = "app.encryption-key=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
|
||||
@AutoConfigureMockMvc
|
||||
@ActiveProfiles("test")
|
||||
class ModelApiIntegrationTest {
|
||||
@Autowired MockMvc mvc;
|
||||
@Autowired IdentityService users;
|
||||
@Autowired ObjectMapper json;
|
||||
@MockitoBean EphemeralStore store;
|
||||
@MockitoBean SandboxClient sandbox;
|
||||
UUID user, admin;
|
||||
String u, a;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
TestDatabase.clear(users.db());
|
||||
user = users.create("user@test.com", "User", "StrongPassword123", "test");
|
||||
admin = users.create("admin@test.com", "Admin", "StrongPassword123", "test");
|
||||
users.db().update("INSERT INTO user_roles VALUES (?,'SUPER_ADMIN')", admin);
|
||||
u =
|
||||
users
|
||||
.login("user@test.com", "StrongPassword123", "test", "test", 1)
|
||||
.get("token")
|
||||
.toString();
|
||||
a =
|
||||
users
|
||||
.login("admin@test.com", "StrongPassword123", "test", "test", 1)
|
||||
.get("token")
|
||||
.toString();
|
||||
clearInvocations(sandbox);
|
||||
}
|
||||
|
||||
@Test
|
||||
void providerSecretsNeverReturnedAndRBACEnforced() throws Exception {
|
||||
String payload =
|
||||
"{\"name\":\"Fictional"
|
||||
+ " fixture\",\"baseUrl\":\"https://api.openai.com/v1\",\"apiType\":\"OPENAI\",\"apiKey\":\"fictional-test-credential\",\"enabled\":false,\"timeoutSeconds\":30}";
|
||||
mvc.perform(
|
||||
post("/api/admin/providers")
|
||||
.header("Authorization", "Bearer " + u)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(payload))
|
||||
.andExpect(status().isForbidden());
|
||||
mvc.perform(
|
||||
post("/api/admin/providers")
|
||||
.header("Authorization", "Bearer " + a)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(payload))
|
||||
.andExpect(status().isOk());
|
||||
String response =
|
||||
mvc.perform(get("/api/admin/providers").header("Authorization", "Bearer " + a))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
assertFalse(response.contains("fictional-test-credential"));
|
||||
assertFalse(response.contains("encrypted_key"));
|
||||
String encrypted =
|
||||
users.db().queryForObject("SELECT encrypted_key FROM ai_providers", String.class);
|
||||
assertFalse(encrypted.contains("fictional-test-credential"));
|
||||
assertTrue(encrypted.startsWith("v1:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ordinaryUsersCannotAdjustWallets() throws Exception {
|
||||
mvc.perform(
|
||||
post("/api/admin/wallets/" + user + "/adjust")
|
||||
.header("Authorization", "Bearer " + u)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(
|
||||
"{\"amount\":100,\"reason\":\"test\",\"idempotencyKey\":\"unauthorized\"}"))
|
||||
.andExpect(status().isForbidden());
|
||||
mvc.perform(get("/api/wallet").header("Authorization", "Bearer " + u))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.balance").value(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void crossOwnerFilesAndSourceRejectedBeforeCallingSandbox() throws Exception {
|
||||
UUID project = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,?,'','HTML','')",
|
||||
project,
|
||||
admin,
|
||||
"Private");
|
||||
for (String endpoint : List.of("files", "source", "versions"))
|
||||
mvc.perform(
|
||||
get("/api/projects/" + project + "/" + endpoint)
|
||||
.header("Authorization", "Bearer " + u))
|
||||
.andExpect(status().isNotFound());
|
||||
verifyNoInteractions(sandbox);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ModelGatewayTest {
|
||||
@Test
|
||||
void outputLimitIsNotReportedAsNormalStop() throws Exception {
|
||||
var objectMapper = new ObjectMapper();
|
||||
var parsed =
|
||||
new ModelGateway(objectMapper, null)
|
||||
.parse(
|
||||
"OPENAI",
|
||||
objectMapper.readTree(
|
||||
"{\"choices\":[{\"finish_reason\":\"length\",\"message\":{\"role\":\"assistant\",\"content\":\"partial\"}}],\"usage\":{\"prompt_tokens\":10,\"completion_tokens\":20}}"));
|
||||
assertEquals("length", parsed.stopReason());
|
||||
}
|
||||
|
||||
final ObjectMapper json = new ObjectMapper();
|
||||
final ModelGateway gateway = new ModelGateway(json, null);
|
||||
|
||||
@Test
|
||||
void boundedOfficialDeepseekCallsDisableThinkingWithoutChangingOtherProviders() throws Exception {
|
||||
var transport = org.mockito.Mockito.mock(ProviderTransport.class);
|
||||
org.mockito.Mockito.when(
|
||||
transport.request(
|
||||
org.mockito.ArgumentMatchers.anyString(),
|
||||
org.mockito.ArgumentMatchers.anyString(),
|
||||
org.mockito.ArgumentMatchers.anyString(),
|
||||
org.mockito.ArgumentMatchers.anyString(),
|
||||
org.mockito.ArgumentMatchers.anyInt(),
|
||||
org.mockito.ArgumentMatchers.any()))
|
||||
.thenReturn(
|
||||
json.readTree(
|
||||
"{\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"done\"}}],\"usage\":{\"prompt_tokens\":1,\"completion_tokens\":1}}"));
|
||||
var adapter = new ModelGateway(json, transport);
|
||||
adapter.call(
|
||||
"OPENAI",
|
||||
"https://api.deepseek.com/v1",
|
||||
"fixture",
|
||||
"deepseek-flash",
|
||||
4096,
|
||||
30,
|
||||
List.of(),
|
||||
"BEARER");
|
||||
adapter.call(
|
||||
"OPENAI",
|
||||
"https://other-provider.example/v1",
|
||||
"fixture",
|
||||
"deepseek-flash",
|
||||
4096,
|
||||
30,
|
||||
List.of(),
|
||||
"BEARER");
|
||||
adapter.audit(
|
||||
"OPENAI",
|
||||
"https://api.deepseek.com/v1",
|
||||
"fixture",
|
||||
"deepseek-flash",
|
||||
30,
|
||||
List.of(),
|
||||
"BEARER");
|
||||
var payload = org.mockito.ArgumentCaptor.forClass(JsonNode.class);
|
||||
org.mockito.Mockito.verify(transport, org.mockito.Mockito.times(3))
|
||||
.request(
|
||||
org.mockito.ArgumentMatchers.anyString(),
|
||||
org.mockito.ArgumentMatchers.anyString(),
|
||||
org.mockito.ArgumentMatchers.anyString(),
|
||||
org.mockito.ArgumentMatchers.anyString(),
|
||||
org.mockito.ArgumentMatchers.anyInt(),
|
||||
payload.capture());
|
||||
assertEquals("disabled", payload.getAllValues().get(0).path("thinking").path("type").asText());
|
||||
assertFalse(payload.getAllValues().get(1).has("thinking"));
|
||||
assertEquals("disabled", payload.getAllValues().get(2).path("thinking").path("type").asText());
|
||||
assertFalse(payload.getAllValues().get(2).has("tools"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedArgumentsKeepKnownUsageAndCompatibleObjectsNormalizeHistory() throws Exception {
|
||||
var response = json.createObjectNode();
|
||||
var message = response.putArray("choices").addObject().putObject("message");
|
||||
message.put("role", "assistant");
|
||||
var function =
|
||||
message
|
||||
.putArray("tool_calls")
|
||||
.addObject()
|
||||
.put("id", "t1")
|
||||
.putObject("function")
|
||||
.put("name", "write_file");
|
||||
response.putObject("usage").put("prompt_tokens", 12).put("completion_tokens", 34);
|
||||
for (String invalid :
|
||||
List.of("{\"path\":", "[]", "{}", "{\"path\":\"x\",\"content\":\"a\"} trailing")) {
|
||||
function.put("arguments", invalid);
|
||||
var result = gateway.parse("OPENAI", response);
|
||||
assertTrue(result.usage().exact());
|
||||
assertEquals(34, result.usage().output());
|
||||
assertNotNull(result.calls().getFirst().argumentError());
|
||||
}
|
||||
function.putObject("arguments").put("path", "index.html").put("content", "hello");
|
||||
var result = gateway.parse("OPENAI", response);
|
||||
assertNull(result.calls().getFirst().argumentError());
|
||||
assertTrue(
|
||||
result
|
||||
.assistant()
|
||||
.path("tool_calls")
|
||||
.get(0)
|
||||
.path("function")
|
||||
.path("arguments")
|
||||
.isTextual());
|
||||
}
|
||||
|
||||
@Test
|
||||
void openaiSeparatesCachedInput() throws Exception {
|
||||
var r =
|
||||
gateway.parse(
|
||||
"OPENAI",
|
||||
json.readTree(
|
||||
"{\"id\":\"r\",\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"hello\"}}],\"usage\":{\"prompt_tokens\":100,\"completion_tokens\":20,\"prompt_tokens_details\":{\"cached_tokens\":60}}}"));
|
||||
assertEquals(40, r.usage().input());
|
||||
assertEquals(60, r.usage().cacheRead());
|
||||
assertTrue(r.usage().exact());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anthropicToolUseAndCacheUsage() throws Exception {
|
||||
var r =
|
||||
gateway.parse(
|
||||
"ANTHROPIC",
|
||||
json.readTree(
|
||||
"{\"id\":\"r\",\"content\":[{\"type\":\"tool_use\",\"id\":\"tool1\",\"name\":\"read_file\",\"input\":{\"path\":\"package.json\"}}],\"usage\":{\"input_tokens\":10,\"output_tokens\":20,\"cache_read_input_tokens\":30,\"cache_creation_input_tokens\":40}}"));
|
||||
assertEquals(1, r.calls().size());
|
||||
assertEquals(10, r.usage().input());
|
||||
assertEquals(40, r.usage().cacheWrite());
|
||||
assertTrue(
|
||||
gateway
|
||||
.request("ANTHROPIC", "test", 100, List.of(r.assistant()), true)
|
||||
.path("messages")
|
||||
.path(0)
|
||||
.path("content")
|
||||
.path(0)
|
||||
.path("type")
|
||||
.asText()
|
||||
.equals("tool_use"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void geminiPreservesThoughtSignatures() throws Exception {
|
||||
var r =
|
||||
gateway.parse(
|
||||
"GEMINI",
|
||||
json.readTree(
|
||||
"{\"candidates\":[{\"content\":{\"parts\":[{\"thoughtSignature\":\"sig\",\"functionCall\":{\"name\":\"list_files\",\"args\":{\"path\":\".\"}}}]}}],\"usageMetadata\":{\"promptTokenCount\":100,\"candidatesTokenCount\":30,\"thoughtsTokenCount\":20,\"cachedContentTokenCount\":40}}"));
|
||||
assertEquals(50, r.usage().output());
|
||||
assertEquals(
|
||||
"sig",
|
||||
gateway
|
||||
.request("GEMINI", "test", 100, List.of(r.assistant()), true)
|
||||
.path("contents")
|
||||
.path(0)
|
||||
.path("parts")
|
||||
.path(0)
|
||||
.path("thoughtSignature")
|
||||
.asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingUsageIsNeverMarkedExact() throws Exception {
|
||||
assertFalse(
|
||||
gateway
|
||||
.parse(
|
||||
"OPENAI",
|
||||
json.readTree(
|
||||
"{\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"test\"}}]}"))
|
||||
.usage()
|
||||
.exact());
|
||||
}
|
||||
|
||||
@Test
|
||||
void vaultUsesRandomAuthenticatedEncryption() {
|
||||
String key = Base64.getEncoder().encodeToString(new byte[32]);
|
||||
var v = new SecretVault(key);
|
||||
String a = v.encrypt("test-key"), b = v.encrypt("test-key");
|
||||
assertNotEquals(a, b);
|
||||
assertEquals("test-key", v.decrypt(a));
|
||||
assertThrows(ApiError.Failure.class, () -> new SecretVault("").encrypt("x"));
|
||||
assertThrows(ApiError.Failure.class, () -> v.decrypt(a.substring(0, a.length() - 3) + "abc"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsPrivateAddressesAndNonAllowlistedProviders() {
|
||||
var transport = new ProviderTransport("api.openai.com", json);
|
||||
assertThrows(ApiError.Failure.class, () -> transport.validate("http://127.0.0.1"));
|
||||
assertThrows(ApiError.Failure.class, () -> transport.validate("https://evil.example"));
|
||||
assertThrows(
|
||||
java.net.UnknownHostException.class, () -> ProviderTransport.publicAddresses("127.0.0.1"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
|
||||
/** Local preview launcher, deliberately excluded from the production artifact. */
|
||||
public class PreviewApplication {
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(PlatformApplication.class, PreviewConfiguration.class)
|
||||
.profiles("preview")
|
||||
.run(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
@TestConfiguration
|
||||
public class PreviewConfiguration {
|
||||
@Bean
|
||||
@Primary
|
||||
EphemeralStore previewStore() {
|
||||
return new EphemeralStore(null) {
|
||||
record Entry(String value, long expires) {}
|
||||
|
||||
final ConcurrentHashMap<String, Entry> entries = new ConcurrentHashMap<>();
|
||||
|
||||
public void put(String key, String value, Duration ttl) {
|
||||
entries.put(key, new Entry(value, System.currentTimeMillis() + ttl.toMillis()));
|
||||
}
|
||||
|
||||
public String consume(String key) {
|
||||
Entry e = entries.remove(key);
|
||||
return e != null && e.expires() > System.currentTimeMillis() ? e.value() : null;
|
||||
}
|
||||
|
||||
public void limit(String key, int max, int seconds) {
|
||||
long now = System.currentTimeMillis();
|
||||
Entry e =
|
||||
entries.compute(
|
||||
"limit:" + key,
|
||||
(k, old) ->
|
||||
old == null || old.expires() <= now
|
||||
? new Entry("1", now + seconds * 1000L)
|
||||
: new Entry(
|
||||
String.valueOf(Integer.parseInt(old.value()) + 1), old.expires()));
|
||||
if (Integer.parseInt(e.value()) > max) throw new ApiError.Failure(429, "请求过于频繁,请稍后重试");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
ApplicationRunner previewAccount(
|
||||
IdentityService users,
|
||||
WalletService wallets,
|
||||
JdbcTemplate db,
|
||||
@Value("${PREVIEW_EMAIL}") String email,
|
||||
@Value("${PREVIEW_PASSWORD}") String password) {
|
||||
return args -> {
|
||||
if (db.queryForObject("SELECT COUNT(*) FROM users WHERE email=?", Integer.class, email) > 0)
|
||||
return;
|
||||
UUID user = users.create(email, "预览管理员", password, "local-preview");
|
||||
db.update("INSERT INTO user_roles VALUES (?,'SUPER_ADMIN')", user);
|
||||
wallets.adjust(
|
||||
user,
|
||||
new BigDecimal("1000"),
|
||||
"preview-initial-credit",
|
||||
"仅限本地预览的测试余额,非真实充值",
|
||||
user,
|
||||
"local-preview");
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,439 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@ActiveProfiles("test")
|
||||
class QueueIntegrationTest {
|
||||
@Autowired IdentityService users;
|
||||
@Autowired TaskService tasks;
|
||||
@Autowired QueueService queue;
|
||||
@Autowired WalletService wallets;
|
||||
@Autowired ObjectMapper json;
|
||||
@Autowired ReconciliationService reconciliation;
|
||||
@Autowired MockMvc mvc;
|
||||
@MockitoBean EphemeralStore ephemeral;
|
||||
UUID user, model;
|
||||
int counter;
|
||||
|
||||
BigDecimal n(String value) {
|
||||
return new BigDecimal(value);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
TestDatabase.clear(users.db());
|
||||
counter = 0;
|
||||
user = users.create("queue@test.com", "Queue", "StrongPassword123", "test");
|
||||
model = UUID.randomUUID();
|
||||
UUID provider = UUID.randomUUID(), price = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_providers(id,name,base_url,api_type,encrypted_key,enabled,timeout_seconds)"
|
||||
+ " VALUES (?,'Fixture','https://api.openai.com/v1','OPENAI','fixture',TRUE,30)",
|
||||
provider);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_models(id,provider_id,name,model_id,context_length,max_output,tools_enabled,vision_enabled,enabled)"
|
||||
+ " VALUES (?,?,'Fixture','fixture',8192,256,TRUE,FALSE,TRUE)",
|
||||
model,
|
||||
provider);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_model_price_versions(id,model_id,input_price,output_price,cache_read_price,cache_write_price)"
|
||||
+ " VALUES (?,?,1,2,0,0)",
|
||||
price,
|
||||
model);
|
||||
users.db().update("UPDATE ai_models SET current_price_version=? WHERE id=?", price, model);
|
||||
wallets.adjust(user, n("100"), "credit", "test", user, "test");
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO worker_presence(instance_id,kind,status,capacity) VALUES"
|
||||
+ " (?,'AI','READY',8)",
|
||||
UUID.randomUUID());
|
||||
}
|
||||
|
||||
UUID task() {
|
||||
UUID project = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,'Queue','','HTML','')",
|
||||
project,
|
||||
user);
|
||||
UUID id =
|
||||
(UUID) tasks.create(user, project, model, "Build", n("2"), "task-" + counter).get("id");
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE ai_tasks SET created_at=? WHERE id=?",
|
||||
Timestamp.from(Instant.parse("2026-01-01T00:00:00Z").plusSeconds(counter++)),
|
||||
id);
|
||||
return id;
|
||||
}
|
||||
|
||||
void enable(int max) {
|
||||
queue.configure(user, max, true, n("2"), "test", "test");
|
||||
}
|
||||
|
||||
void finish(Map<String, Object> task) {
|
||||
tasks.finish(
|
||||
(UUID) task.get("id"), task.get("lease_token").toString(), "COMPLETED", "test", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void simultaneousClaimsNeverExceedGlobalConcurrency() throws Exception {
|
||||
for (int i = 0; i < 8; i++) task();
|
||||
enable(2);
|
||||
var pool = Executors.newFixedThreadPool(8);
|
||||
try {
|
||||
var start = new CountDownLatch(1);
|
||||
List<Future<Map<String, Object>>> calls = new ArrayList<>();
|
||||
for (int i = 0; i < 8; i++)
|
||||
calls.add(
|
||||
pool.submit(
|
||||
() -> {
|
||||
start.await();
|
||||
return tasks.claim();
|
||||
}));
|
||||
start.countDown();
|
||||
Set<UUID> claimed = new HashSet<>();
|
||||
for (var call : calls) {
|
||||
var value = call.get(15, TimeUnit.SECONDS);
|
||||
if (value != null) claimed.add((UUID) value.get("id"));
|
||||
}
|
||||
assertEquals(2, claimed.size());
|
||||
assertEquals(2L, queue.occupied());
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void administratorWeightPaidPriorityAndFifoUseOneOrder() {
|
||||
enable(1);
|
||||
UUID ordinary = task(), paid = task(), manual = task();
|
||||
queue.purchase(user, paid, n("2"), "paid");
|
||||
queue.prioritize(user, manual, 10, false, "test", "test");
|
||||
var first = tasks.claim();
|
||||
assertEquals(manual, first.get("id"));
|
||||
finish(first);
|
||||
var second = tasks.claim();
|
||||
assertEquals(paid, second.get("id"));
|
||||
tasks.beginExecution(paid, second.get("lease_token").toString());
|
||||
finish(second);
|
||||
assertEquals(ordinary, tasks.claim().get("id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void runNextDoesNotInterruptRunningOrBypassCap() {
|
||||
enable(1);
|
||||
UUID running = task(), older = task(), chosen = task();
|
||||
var first = tasks.claim();
|
||||
assertEquals(running, first.get("id"));
|
||||
queue.prioritize(user, chosen, 0, true, "urgent", "test");
|
||||
assertNull(tasks.claim());
|
||||
assertEquals("RUNNING", tasks.find(running, user).get("status"));
|
||||
finish(first);
|
||||
assertEquals(chosen, tasks.claim().get("id"));
|
||||
assertEquals("QUEUED", tasks.find(older, user).get("status"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reducedConcurrencyAppliesWithoutKillingExistingTasks() {
|
||||
enable(2);
|
||||
task();
|
||||
task();
|
||||
UUID waiting = task();
|
||||
var a = tasks.claim();
|
||||
var b = tasks.claim();
|
||||
enable(1);
|
||||
assertNull(tasks.claim());
|
||||
finish(a);
|
||||
assertNull(tasks.claim());
|
||||
finish(b);
|
||||
assertEquals(waiting, tasks.claim().get("id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void priorityPurchaseIsConcurrentIdempotentAndIndependentOfBudget() throws Exception {
|
||||
enable(2);
|
||||
UUID id = task();
|
||||
var pool = Executors.newFixedThreadPool(6);
|
||||
try {
|
||||
List<Future<Object>> calls = new ArrayList<>();
|
||||
for (int i = 0; i < 6; i++)
|
||||
calls.add(pool.submit(() -> queue.purchase(user, id, n("2"), "same")));
|
||||
for (var call : calls) call.get(15, TimeUnit.SECONDS);
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
assertEquals(
|
||||
1, users.db().queryForObject("SELECT COUNT(*) FROM queue_priority_orders", Integer.class));
|
||||
assertEquals(
|
||||
1,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM wallet_ledger WHERE kind='PRIORITY_PURCHASE'",
|
||||
Integer.class));
|
||||
assertEquals(0, n("98").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertEquals(0, n("2").compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancellationBeforeFirstExecutionRefundsExactlyOnce() {
|
||||
enable(1);
|
||||
UUID id = task();
|
||||
queue.purchase(user, id, n("2"), "paid");
|
||||
tasks.action(id, user, "pause");
|
||||
tasks.action(id, user, "cancel");
|
||||
queue.refundUnused(id);
|
||||
assertEquals(0, n("100").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
assertEquals(
|
||||
1,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM wallet_ledger WHERE kind='PRIORITY_REFUND'", Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void firstExecutionConsumesPriorityAndLaterCancellationDoesNotRefund() {
|
||||
enable(1);
|
||||
UUID id = task();
|
||||
queue.purchase(user, id, n("2"), "paid");
|
||||
var claim = tasks.claim();
|
||||
assertEquals("", tasks.beginExecution(id, claim.get("lease_token").toString()));
|
||||
tasks.action(id, user, "cancel");
|
||||
tasks.finish(id, claim.get("lease_token").toString(), "CANCELLED", "test", true);
|
||||
assertEquals(
|
||||
"CONSUMED",
|
||||
users.db().queryForObject("SELECT status FROM queue_priority_orders", String.class));
|
||||
assertEquals(0, n("98").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancellationBetweenClaimAndStartStillRefunds() {
|
||||
enable(1);
|
||||
UUID id = task();
|
||||
queue.purchase(user, id, n("2"), "paid");
|
||||
var claim = tasks.claim();
|
||||
tasks.action(id, user, "cancel");
|
||||
assertEquals("CANCEL", tasks.beginExecution(id, claim.get("lease_token").toString()));
|
||||
tasks.finish(id, claim.get("lease_token").toString(), "CANCELLED", "test", true);
|
||||
assertEquals(
|
||||
"REFUNDED",
|
||||
users.db().queryForObject("SELECT status FROM queue_priority_orders", String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stalePriceOfflineWorkerAndWrongOwnerCannotCharge() {
|
||||
enable(1);
|
||||
UUID id = task();
|
||||
UUID other = users.create("other-queue@test.com", "Other", "StrongPassword123", "test");
|
||||
assertThrows(ApiError.Failure.class, () -> queue.purchase(other, id, n("2"), "foreign"));
|
||||
assertThrows(ApiError.Failure.class, () -> queue.purchase(user, id, n("1"), "old-price"));
|
||||
users.db().update("DELETE FROM worker_presence");
|
||||
assertThrows(ApiError.Failure.class, () -> queue.purchase(user, id, n("2"), "offline"));
|
||||
assertThrows(
|
||||
ApiError.Failure.class, () -> queue.prioritize(user, id, 100, true, "test", "test"));
|
||||
assertEquals(0, n("100").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertEquals(
|
||||
0, users.db().queryForObject("SELECT COUNT(*) FROM queue_priority_orders", Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ambiguousExpiredTaskKeepsSlotUntilReconciled() {
|
||||
enable(1);
|
||||
UUID first = task(), second = task();
|
||||
tasks.claim();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE ai_tasks SET lease_expires_at=? WHERE id=?",
|
||||
Timestamp.from(Instant.now().minusSeconds(1)),
|
||||
first);
|
||||
assertNull(tasks.claim());
|
||||
assertEquals("REVIEW_REQUIRED", tasks.find(first, user).get("status"));
|
||||
reconciliation.resolve(first, user, BigDecimal.ZERO, "confirmed no extra charge", "test");
|
||||
assertEquals(second, tasks.claim().get("id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void settingsAndDispatchArePermissionProtectedAndPositionIsOwnOnly() throws Exception {
|
||||
UUID id = task();
|
||||
String token =
|
||||
users
|
||||
.login("queue@test.com", "StrongPassword123", "test", "test", 1)
|
||||
.get("token")
|
||||
.toString();
|
||||
UUID owner = user,
|
||||
other = users.create("other-position@test.com", "Other", "StrongPassword123", "test");
|
||||
wallets.adjust(other, n("10"), "credit", "test", owner, "test");
|
||||
user = other;
|
||||
task();
|
||||
user = owner;
|
||||
mvc.perform(
|
||||
put("/api/admin/queue/config")
|
||||
.header("Authorization", "Bearer " + token)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(
|
||||
"{\"maxConcurrent\":3,\"priorityEnabled\":true,\"priorityPrice\":2,\"reason\":\"test\"}"))
|
||||
.andExpect(status().isForbidden());
|
||||
mvc.perform(get("/api/queue").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.positions[0].position").value(1))
|
||||
.andExpect(jsonPath("$.positions.length()").value(1))
|
||||
.andExpect(jsonPath("$.queued").value(2));
|
||||
mvc.perform(
|
||||
post("/api/admin/tasks/" + id + "/priority")
|
||||
.header("Authorization", "Bearer " + token)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"priority\":10,\"runNext\":false,\"reason\":\"test\"}"))
|
||||
.andExpect(status().isForbidden());
|
||||
users.db().update("INSERT INTO user_roles VALUES (?,'OPS')", user);
|
||||
mvc.perform(
|
||||
put("/api/admin/queue/config")
|
||||
.header("Authorization", "Bearer " + token)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(
|
||||
"{\"maxConcurrent\":3,\"priorityEnabled\":true,\"priorityPrice\":2,\"reason\":\"test\"}"))
|
||||
.andExpect(status().isOk());
|
||||
assertEquals(
|
||||
1,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM admin_operation_logs WHERE action='QUEUE_CONFIG'",
|
||||
Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void oneWorkerActuallyExecutesTwoProjectsInParallel() throws Exception {
|
||||
enable(2);
|
||||
task();
|
||||
task();
|
||||
var gateway = mock(ModelGateway.class);
|
||||
var sandbox = mock(SandboxClient.class);
|
||||
when(sandbox.inspect(any()))
|
||||
.thenReturn(json.createObjectNode().put("sourceFiles", 1).put("complete", true));
|
||||
var vault = mock(SecretVault.class);
|
||||
when(vault.decrypt(anyString())).thenReturn("fixture-token");
|
||||
var entered = new CountDownLatch(2);
|
||||
var release = new CountDownLatch(1);
|
||||
when(gateway.call(
|
||||
anyString(),
|
||||
anyString(),
|
||||
anyString(),
|
||||
anyString(),
|
||||
anyInt(),
|
||||
anyInt(),
|
||||
anyList(),
|
||||
anyString()))
|
||||
.thenAnswer(
|
||||
call -> {
|
||||
entered.countDown();
|
||||
if (!release.await(10, TimeUnit.SECONDS))
|
||||
throw new IllegalStateException("test timeout");
|
||||
return new ModelGateway.Result(
|
||||
json.createObjectNode().put("role", "assistant").put("content", "done"),
|
||||
List.of(),
|
||||
new ModelGateway.Usage(10, 2, 0, 0, true, json.createObjectNode()),
|
||||
UUID.randomUUID().toString());
|
||||
});
|
||||
var worker = new AgentWorker(tasks, gateway, sandbox, vault, json);
|
||||
try {
|
||||
worker.poll();
|
||||
assertTrue(entered.await(5, TimeUnit.SECONDS), "Both model calls must overlap");
|
||||
assertEquals(2, worker.activeTasks());
|
||||
assertEquals(2L, queue.occupied());
|
||||
release.countDown();
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
|
||||
while (worker.activeTasks() > 0 && System.nanoTime() < deadline) Thread.sleep(10);
|
||||
assertEquals(0, worker.activeTasks());
|
||||
assertEquals(
|
||||
2,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM ai_tasks WHERE status='COMPLETED'", Integer.class));
|
||||
} finally {
|
||||
release.countDown();
|
||||
worker.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void buyingAndClaimingSameTaskCannotDoubleChargeOrLoseRefund() throws Exception {
|
||||
enable(1);
|
||||
UUID id = task();
|
||||
var pool = Executors.newFixedThreadPool(2);
|
||||
var start = new CountDownLatch(1);
|
||||
try {
|
||||
var buy =
|
||||
pool.submit(
|
||||
() -> {
|
||||
start.await();
|
||||
try {
|
||||
queue.purchase(user, id, n("2"), "raced");
|
||||
return true;
|
||||
} catch (ApiError.Failure conflict) {
|
||||
assertEquals(409, conflict.status);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
var claim =
|
||||
pool.submit(
|
||||
() -> {
|
||||
start.await();
|
||||
return tasks.claim();
|
||||
});
|
||||
start.countDown();
|
||||
buy.get(10, TimeUnit.SECONDS);
|
||||
var received = claim.get(10, TimeUnit.SECONDS);
|
||||
assertNotNull(received);
|
||||
tasks.action(id, user, "cancel");
|
||||
assertEquals("CANCEL", tasks.beginExecution(id, received.get("lease_token").toString()));
|
||||
tasks.finish(id, received.get("lease_token").toString(), "CANCELLED", "test", true);
|
||||
assertEquals(0, n("100").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertEquals(
|
||||
0,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM queue_priority_orders WHERE status<>'REFUNDED'",
|
||||
Integer.class));
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
/**
|
||||
* Requires an explicitly selected disposable local/CI Redis. Never points at production by default.
|
||||
*/
|
||||
@EnabledIfEnvironmentVariable(named = "RUN_REDIS_TESTS", matches = "true")
|
||||
class RedisIntegrationTest {
|
||||
LettuceConnectionFactory connection;
|
||||
EphemeralStore store;
|
||||
StringRedisTemplate redis;
|
||||
|
||||
@BeforeEach
|
||||
void connect() {
|
||||
var configuration =
|
||||
new RedisStandaloneConfiguration(
|
||||
System.getenv().getOrDefault("TEST_REDIS_HOST", "127.0.0.1"),
|
||||
Integer.parseInt(System.getenv().getOrDefault("TEST_REDIS_PORT", "6379")));
|
||||
String password = System.getenv().getOrDefault("TEST_REDIS_PASSWORD", "");
|
||||
if (!password.isEmpty()) configuration.setPassword(password);
|
||||
connection = new LettuceConnectionFactory(configuration);
|
||||
connection.afterPropertiesSet();
|
||||
redis = new StringRedisTemplate(connection);
|
||||
store = new EphemeralStore(redis);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void close() {
|
||||
connection.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
void consumesExactlyOnce() {
|
||||
String key = "test:captcha:" + UUID.randomUUID();
|
||||
store.put(key, "hashed-answer", Duration.ofSeconds(5));
|
||||
assertEquals("hashed-answer", store.consume(key));
|
||||
assertNull(store.consume(key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void windowsAreLimitedAndExpire() {
|
||||
String key = "test:" + UUID.randomUUID();
|
||||
try {
|
||||
store.limit(key, 2, 30);
|
||||
store.limit(key, 2, 30);
|
||||
assertThrows(ApiError.Failure.class, () -> store.limit(key, 2, 30));
|
||||
Long ttl = redis.getExpire("limit:" + key);
|
||||
assertNotNull(ttl);
|
||||
assertTrue(ttl > 0 && ttl <= 30);
|
||||
} finally {
|
||||
redis.delete("limit:" + key);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentConsumptionHasExactlyOneWinner() throws Exception {
|
||||
String key = "test:captcha:" + UUID.randomUUID();
|
||||
store.put(key, "single-use", Duration.ofSeconds(10));
|
||||
try (var pool = Executors.newVirtualThreadPerTaskExecutor()) {
|
||||
var start = new CountDownLatch(1);
|
||||
var readers = new java.util.ArrayList<Future<String>>();
|
||||
for (int i = 0; i < 16; i++)
|
||||
readers.add(
|
||||
pool.submit(
|
||||
() -> {
|
||||
start.await();
|
||||
return store.consume(key);
|
||||
}));
|
||||
start.countDown();
|
||||
int winners = 0;
|
||||
for (var reader : readers)
|
||||
if ("single-use".equals(reader.get(5, TimeUnit.SECONDS))) winners++;
|
||||
assertEquals(1, winners);
|
||||
} finally {
|
||||
redis.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void actualExpiryInvalidatesCaptchaAndResetsLimiterWindow() throws Exception {
|
||||
String key = "test:expire:" + UUID.randomUUID(), limiter = key + ":limit";
|
||||
try {
|
||||
store.put(key, "expired-answer", Duration.ofMillis(200));
|
||||
store.limit(limiter, 1, 1);
|
||||
assertThrows(ApiError.Failure.class, () -> store.limit(limiter, 1, 1));
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
|
||||
while ((Boolean.TRUE.equals(redis.hasKey(key))
|
||||
|| Boolean.TRUE.equals(redis.hasKey("limit:" + limiter)))
|
||||
&& System.nanoTime() < deadline) Thread.sleep(20);
|
||||
assertFalse(Boolean.TRUE.equals(redis.hasKey("limit:" + limiter)));
|
||||
assertNull(store.consume(key));
|
||||
assertDoesNotThrow(() -> store.limit(limiter, 1, 1));
|
||||
} finally {
|
||||
redis.delete(java.util.List.of(key, "limit:" + limiter));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.*;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest(properties = "app.encryption-key=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
|
||||
@AutoConfigureMockMvc
|
||||
@ActiveProfiles("test")
|
||||
class StorageIntegrationTest {
|
||||
@Autowired StorageService storage;
|
||||
@Autowired IdentityService users;
|
||||
@Autowired IntegrationService integrations;
|
||||
@Autowired MockMvc mvc;
|
||||
@MockitoBean EphemeralStore ephemeral;
|
||||
@MockitoBean ObjectStorageGateway objects;
|
||||
UUID user, project;
|
||||
String token;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
TestDatabase.clear(users.db());
|
||||
reset(objects);
|
||||
user = users.create("storage@test.com", "Files", "StrongPassword123", "test");
|
||||
project = UUID.randomUUID();
|
||||
token = UUID.randomUUID().toString();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,'Files','','HTML','')",
|
||||
project,
|
||||
user);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO user_sessions(id,user_id,token_hash,ip,user_agent,expires_at) VALUES"
|
||||
+ " (?,?,?,'test','test',?)",
|
||||
UUID.randomUUID(),
|
||||
user,
|
||||
SecurityConfig.hash(token),
|
||||
Timestamp.from(Instant.now().plusSeconds(3600)));
|
||||
integrations.save(
|
||||
"S3",
|
||||
user,
|
||||
Map.of(
|
||||
"endpoint",
|
||||
"http://localhost:9000",
|
||||
"region",
|
||||
"us-east-1",
|
||||
"bucket",
|
||||
"test-bucket",
|
||||
"accessKey",
|
||||
"fixture-access",
|
||||
"secretKey",
|
||||
"fixture-secret"),
|
||||
true,
|
||||
"test");
|
||||
}
|
||||
|
||||
UUID upload() {
|
||||
return UUID.fromString(
|
||||
storage
|
||||
.upload(
|
||||
user,
|
||||
project,
|
||||
"测试文件.html",
|
||||
"text/html",
|
||||
"hello".getBytes(StandardCharsets.UTF_8),
|
||||
"upload")
|
||||
.get("id")
|
||||
.toString());
|
||||
}
|
||||
|
||||
String path() {
|
||||
return "/api/projects/" + project + "/storage";
|
||||
}
|
||||
|
||||
@Test
|
||||
void multipartEndpointAndPrivateDownload() throws Exception {
|
||||
mvc.perform(
|
||||
multipart(path())
|
||||
.file(new MockMultipartFile("file", "文件.html", "text/html", "hello".getBytes()))
|
||||
.param("idempotencyKey", "multipart")
|
||||
.header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value("READY"))
|
||||
.andExpect(jsonPath("$.object_key").doesNotExist());
|
||||
UUID object = users.db().queryForObject("SELECT id FROM storage_objects", UUID.class);
|
||||
when(objects.get(anyMap(), anyString())).thenReturn("hello".getBytes());
|
||||
mvc.perform(get(path() + "/" + object + "/content").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(header().string("Content-Type", "application/octet-stream"))
|
||||
.andExpect(header().string("X-Content-Type-Options", "nosniff"))
|
||||
.andExpect(header().string("Cache-Control", "no-store"));
|
||||
mvc.perform(get(path() + "/" + object + "/content")).andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anotherOwnerCannotReadOrMutate() throws Exception {
|
||||
UUID object = upload(),
|
||||
other = users.create("other@test.com", "Other", "StrongPassword123", "test");
|
||||
assertThrows(ApiError.Failure.class, () -> storage.list(other, project, 0, false));
|
||||
assertThrows(ApiError.Failure.class, () -> storage.trash(other, project, object));
|
||||
UUID another = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,'Other','','HTML','')",
|
||||
another,
|
||||
user);
|
||||
mvc.perform(
|
||||
delete("/api/projects/" + another + "/storage/" + object)
|
||||
.header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isNotFound());
|
||||
verify(objects, never()).get(anyMap(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ambiguousUploadKeepsQuotaAndReconcilesMatchingObject() {
|
||||
doThrow(new ApiError.Failure(502, "timeout"))
|
||||
.when(objects)
|
||||
.put(anyMap(), anyString(), any(), anyString(), anyString());
|
||||
assertThrows(ApiError.Failure.class, this::upload);
|
||||
var row = users.db().queryForMap("SELECT * FROM storage_objects");
|
||||
UUID id = (UUID) row.get("id");
|
||||
assertEquals("FAILED", row.get("status"));
|
||||
assertEquals(5L, storage.summary(user, project).get("usedBytes"));
|
||||
assertThrows(ApiError.Failure.class, () -> storage.reconcile(user, project, id));
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE storage_objects SET updated_at=? WHERE id=?",
|
||||
Timestamp.from(Instant.now().minusSeconds(360)),
|
||||
id);
|
||||
when(objects.head(anyMap(), anyString()))
|
||||
.thenReturn(Map.of("size", 5L, "sha256", row.get("sha256")));
|
||||
assertEquals("READY", storage.reconcile(user, project, id).get("status"));
|
||||
verify(objects, times(1)).put(anyMap(), anyString(), any(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cleanupClaimBlocksRestoreAndDoesNotFreeSpaceBeforeDeleteConfirmation() {
|
||||
UUID id = upload();
|
||||
storage.trash(user, project, id);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE storage_objects SET deleted_at=? WHERE id=?",
|
||||
Timestamp.from(Instant.now().minus(Duration.ofDays(31))),
|
||||
id);
|
||||
doAnswer(
|
||||
call -> {
|
||||
// Even if the recovery window is changed while deletion is in flight, the lease wins.
|
||||
users
|
||||
.db()
|
||||
.update("UPDATE storage_objects SET deleted_at=CURRENT_TIMESTAMP WHERE id=?", id);
|
||||
assertThrows(ApiError.Failure.class, () -> storage.restore(user, project, id));
|
||||
throw new ApiError.Failure(502, "unknown delete result");
|
||||
})
|
||||
.when(objects)
|
||||
.purge(anyMap(), anyString());
|
||||
assertEquals(0, storage.purgeExpired());
|
||||
assertEquals(5L, storage.summary(user, project).get("usedBytes"));
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE storage_objects SET deleted_at=?,purge_started_at=? WHERE id=?",
|
||||
Timestamp.from(Instant.now().minus(Duration.ofDays(31))),
|
||||
Timestamp.from(Instant.now().minusSeconds(360)),
|
||||
id);
|
||||
doNothing().when(objects).purge(anyMap(), anyString());
|
||||
assertEquals(1, storage.purgeExpired());
|
||||
assertEquals(0L, storage.summary(user, project).get("usedBytes"));
|
||||
assertEquals(0, storage.purgeExpired());
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingOrMismatchedObjectStaysUncertain() {
|
||||
UUID id = upload();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE storage_objects SET status='FAILED',updated_at=? WHERE id=?",
|
||||
Timestamp.from(Instant.now().minusSeconds(360)),
|
||||
id);
|
||||
when(objects.head(anyMap(), anyString())).thenReturn(Map.of("missing", true));
|
||||
assertEquals("FAILED", storage.reconcile(user, project, id).get("status"));
|
||||
assertEquals(5L, storage.summary(user, project).get("usedBytes"));
|
||||
storage.trash(user, project, id);
|
||||
storage.restore(user, project, id);
|
||||
assertEquals("FAILED", storage.ownObject(user, project, id).get("status"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void changedMimeOrContentCannotReuseUploadKey() {
|
||||
upload();
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() ->
|
||||
storage.upload(user, project, "测试文件.html", "text/plain", "hello".getBytes(), "upload"));
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() -> storage.upload(user, project, "../bad", "text/plain", new byte[0], "invalid"));
|
||||
verify(objects, times(1)).put(anyMap(), anyString(), any(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void downloadRejectsCorruptedBytes() {
|
||||
UUID id = upload();
|
||||
when(objects.get(anyMap(), anyString())).thenReturn("corrupted".getBytes());
|
||||
assertThrows(ApiError.Failure.class, () -> storage.download(user, project, id));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import java.util.List;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
final class TestDatabase {
|
||||
static void clear(JdbcTemplate db) {
|
||||
db.update("UPDATE ai_models SET current_price_version=NULL");
|
||||
db.update("UPDATE ai_review_config SET model_id=NULL,enabled=TRUE,version=1 WHERE id=1");
|
||||
for (String table :
|
||||
List.of(
|
||||
"extension_packages",
|
||||
"support_ticket_messages",
|
||||
"support_tickets",
|
||||
"domain_events",
|
||||
"project_domains",
|
||||
"domain_claims",
|
||||
"worker_presence",
|
||||
"queue_priority_orders",
|
||||
"runtime_operations",
|
||||
"project_moderation",
|
||||
"runtime_nodes",
|
||||
"integration_test_logs",
|
||||
"message_records",
|
||||
"integration_configs",
|
||||
"storage_objects",
|
||||
"storage_configs",
|
||||
"deployment_events",
|
||||
"project_deployments",
|
||||
"project_services",
|
||||
"project_subscriptions",
|
||||
"billing_plans",
|
||||
"ai_reconciliations",
|
||||
"ai_code_reviews",
|
||||
"ai_task_reports",
|
||||
"ai_usage_records",
|
||||
"ai_task_steps",
|
||||
"ai_task_messages",
|
||||
"ai_tasks",
|
||||
"wallet_ledger",
|
||||
"wallet_reservations",
|
||||
"wallets",
|
||||
"ai_model_price_versions",
|
||||
"ai_models",
|
||||
"ai_providers",
|
||||
"projects",
|
||||
"admin_operation_logs",
|
||||
"login_logs",
|
||||
"user_sessions",
|
||||
"user_roles",
|
||||
"users")) db.update("DELETE FROM " + table);
|
||||
db.update(
|
||||
"UPDATE ai_queue_config SET"
|
||||
+ " max_concurrent=2,priority_enabled=FALSE,priority_price=1,version=1 WHERE id=1");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
import static run.webup.platform.TicketController.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
@AutoConfigureMockMvc
|
||||
class TicketIntegrationTest {
|
||||
@Autowired MockMvc mvc;
|
||||
@Autowired JdbcTemplate db;
|
||||
@Autowired ObjectMapper json;
|
||||
@Autowired TicketService tickets;
|
||||
@MockitoBean EphemeralStore ephemeral;
|
||||
|
||||
record Account(UUID id, String token) {}
|
||||
|
||||
Account owner, other, staff, finance;
|
||||
UUID project;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
TestDatabase.clear(db);
|
||||
owner = account("owner", "USER");
|
||||
other = account("other", "USER");
|
||||
staff = account("support", "SUPPORT");
|
||||
finance = account("finance", "FINANCE");
|
||||
project = UUID.randomUUID();
|
||||
db.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,'Ticket project','','HTML','')",
|
||||
project,
|
||||
owner.id());
|
||||
}
|
||||
|
||||
@Test
|
||||
void separatesOwnerSupportAndFinanceAndValidatesProjectOwnership() throws Exception {
|
||||
var input = createInput("Project issue", project);
|
||||
mvc.perform(MockMvcRequestBuilders.get("/api/tickets")).andExpect(status().isUnauthorized());
|
||||
post(other, "/api/tickets", input).andExpect(status().isNotFound());
|
||||
var created = json(post(owner, "/api/tickets", input));
|
||||
var id = created.path("ticket").path("id").asText();
|
||||
assertEquals("Ticket project", created.path("ticket").path("project_name").asText());
|
||||
assertEquals("WAITING_SUPPORT", created.path("ticket").path("status").asText());
|
||||
assertEquals(1, created.path("messages").size());
|
||||
get(other, "/api/tickets/" + id).andExpect(status().isNotFound());
|
||||
post(other, "/api/tickets/" + id + "/actions", command("CLOSE"))
|
||||
.andExpect(status().isNotFound());
|
||||
post(other, "/api/tickets/" + id + "/read", Map.of("revision", 0))
|
||||
.andExpect(status().isNotFound());
|
||||
assertEquals(0, json(get(other, "/api/tickets")).path("items").size());
|
||||
get(owner, "/api/admin/tickets").andExpect(status().isForbidden());
|
||||
get(finance, "/api/admin/tickets/" + id).andExpect(status().isForbidden());
|
||||
post(owner, "/api/tickets/" + id + "/actions", command("RESOLVE"))
|
||||
.andExpect(status().isForbidden());
|
||||
get(staff, "/api/admin/tickets?status=WAITING_SUPPORT&search=owner@example.test")
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.items[0].id").value(id));
|
||||
var reference = created.path("ticket").path("ticket_no").asLong();
|
||||
get(owner, "/api/tickets?search=WU-" + String.format("%06d", reference))
|
||||
.andExpect(jsonPath("$.items[0].id").value(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
void keepsNotesAssignmentAndInternalActivityOutOfCustomerProjection() throws Exception {
|
||||
var created = json(post(owner, "/api/tickets", createInput("Private thread", null)));
|
||||
var id = created.path("ticket").path("id").asText();
|
||||
String adminPath = "/api/admin/tickets/" + id;
|
||||
String ownerPath = "/api/tickets/" + id;
|
||||
var note = command("NOTE");
|
||||
note.put("content", "Staff handover only");
|
||||
json(post(staff, adminPath + "/actions", note));
|
||||
var assign = command("ASSIGN");
|
||||
assign.put("assigneeId", finance.id());
|
||||
post(staff, adminPath + "/actions", assign).andExpect(status().isBadRequest());
|
||||
assign.put("assigneeId", staff.id());
|
||||
json(post(staff, adminPath + "/actions", assign));
|
||||
var priority = command("PRIORITY");
|
||||
priority.put("priority", "URGENT");
|
||||
json(post(staff, adminPath + "/actions", priority));
|
||||
var customer = json(get(owner, ownerPath));
|
||||
assertEquals(created, customer);
|
||||
get(owner, ownerPath + "?before=" + note.get("requestId")).andExpect(status().isBadRequest());
|
||||
var admin = json(get(staff, adminPath));
|
||||
assertEquals("URGENT", admin.path("ticket").path("priority").asText());
|
||||
assertEquals(staff.id().toString(), admin.path("ticket").path("assignee_id").asText());
|
||||
assertEquals("NOTE", admin.path("messages").get(1).path("kind").asText());
|
||||
assertEquals("Staff handover only", admin.path("messages").get(1).path("content").asText());
|
||||
assertEquals(
|
||||
3,
|
||||
db.queryForObject(
|
||||
"SELECT COUNT(*) FROM admin_operation_logs WHERE target=? AND actor_id=?",
|
||||
Integer.class,
|
||||
id,
|
||||
staff.id()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void retriesAreIdempotentAndReadAcknowledgementCannotHideNewReplies() throws Exception {
|
||||
var input = createInput("Lifecycle", null);
|
||||
var created = json(post(owner, "/api/tickets", input));
|
||||
assertEquals(created, json(post(owner, "/api/tickets", input)));
|
||||
input.put("subject", "Different content");
|
||||
post(owner, "/api/tickets", input).andExpect(status().isConflict());
|
||||
String id = created.path("ticket").path("id").asText();
|
||||
String adminPath = "/api/admin/tickets/" + id;
|
||||
String ownerPath = "/api/tickets/" + id;
|
||||
var reply = command("REPLY");
|
||||
reply.put("content", "Please provide the project ID");
|
||||
var first = json(post(staff, adminPath + "/actions", reply));
|
||||
assertEquals(first, json(post(staff, adminPath + "/actions", reply)));
|
||||
assertEquals("WAITING_CUSTOMER", first.path("ticket").path("status").asText());
|
||||
assertTrue(json(get(owner, ownerPath)).path("ticket").path("unread").asBoolean());
|
||||
var customerReply = command("REPLY");
|
||||
customerReply.put("content", "<script>plain text</script>");
|
||||
post(owner, ownerPath + "/actions", customerReply)
|
||||
.andExpect(jsonPath("$.ticket.status").value("WAITING_SUPPORT"))
|
||||
.andExpect(jsonPath("$.messages[2].content").value("<script>plain text</script>"));
|
||||
post(staff, adminPath + "/actions", command("RESOLVE"))
|
||||
.andExpect(jsonPath("$.ticket.status").value("RESOLVED"));
|
||||
post(owner, ownerPath + "/read", Map.of("revision", 1)).andExpect(status().isOk());
|
||||
get(owner, ownerPath).andExpect(jsonPath("$.ticket.unread").value(true));
|
||||
post(owner, ownerPath + "/read", Map.of("revision", 3)).andExpect(status().isConflict());
|
||||
post(owner, ownerPath + "/read", Map.of("revision", 2)).andExpect(status().isOk());
|
||||
get(owner, ownerPath).andExpect(jsonPath("$.ticket.unread").value(false));
|
||||
assertEquals(4, json(post(staff, adminPath + "/actions", reply)).path("messages").size());
|
||||
reply.put("content", "Different reply");
|
||||
post(staff, adminPath + "/actions", reply).andExpect(status().isConflict());
|
||||
post(owner, ownerPath + "/actions", command("CLOSE"))
|
||||
.andExpect(jsonPath("$.ticket.status").value("CLOSED"));
|
||||
post(owner, ownerPath + "/actions", command("REOPEN"))
|
||||
.andExpect(jsonPath("$.ticket.status").value("WAITING_SUPPORT"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void serializesConcurrentCreateAndEnforcesPerOwnerOpenQuota() throws Exception {
|
||||
var request = new Create(UUID.randomUUID(), "Concurrent", Category.AI, null, "Details");
|
||||
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
|
||||
var calls =
|
||||
executor.invokeAll(
|
||||
List.<Callable<Object>>of(
|
||||
() -> tickets.create(owner.id(), request),
|
||||
() -> tickets.create(owner.id(), request)));
|
||||
assertEquals(calls.getFirst().get(), calls.getLast().get());
|
||||
}
|
||||
for (int i = 0; i < 9; i++)
|
||||
tickets.create(
|
||||
owner.id(), new Create(UUID.randomUUID(), "Ticket " + i, Category.AI, null, "Details"));
|
||||
post(owner, "/api/tickets", createInput("At limit", null))
|
||||
.andExpect(status().isTooManyRequests());
|
||||
post(other, "/api/tickets", createInput("Other owner", null)).andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
void paginatesVisibleMessagesWithoutLosingHistory() throws Exception {
|
||||
var created = json(post(owner, "/api/tickets", createInput("Long history", null)));
|
||||
var id = UUID.fromString(created.path("ticket").path("id").asText());
|
||||
for (int i = 0; i < 30; i++)
|
||||
tickets.action(
|
||||
owner.id(),
|
||||
id,
|
||||
new Action(UUID.randomUUID(), Operation.REPLY, "Reply " + i, null, null),
|
||||
false,
|
||||
"");
|
||||
tickets.action(
|
||||
staff.id(),
|
||||
id,
|
||||
new Action(UUID.randomUUID(), Operation.NOTE, "Hidden", null, null),
|
||||
true,
|
||||
"test");
|
||||
String path = "/api/tickets/" + id;
|
||||
var latest = json(get(owner, path));
|
||||
assertTrue(latest.path("hasOlder").asBoolean());
|
||||
assertEquals(30, latest.path("messages").size());
|
||||
var cursor = latest.path("messages").get(0).path("id").asText();
|
||||
var older = json(get(owner, path + "?before=" + cursor));
|
||||
assertFalse(older.path("hasOlder").asBoolean());
|
||||
assertEquals(created.path("messages"), older.path("messages"));
|
||||
}
|
||||
|
||||
private Account account(String name, String role) {
|
||||
UUID id = UUID.randomUUID();
|
||||
String token = UUID.randomUUID().toString();
|
||||
db.update(
|
||||
"INSERT INTO users(id,email,display_name,password_hash,registration_ip) VALUES (?,?,?,?,?)",
|
||||
id,
|
||||
name + "@example.test",
|
||||
name,
|
||||
"fixture",
|
||||
"test");
|
||||
db.update("INSERT INTO user_roles VALUES (?,?)", id, role);
|
||||
db.update(
|
||||
"INSERT INTO user_sessions(id,user_id,token_hash,ip,user_agent,expires_at) VALUES"
|
||||
+ " (?,?,?,?,?,?)",
|
||||
UUID.randomUUID(),
|
||||
id,
|
||||
SecurityConfig.hash(token),
|
||||
"test",
|
||||
"test",
|
||||
OffsetDateTime.now().plusHours(1));
|
||||
return new Account(id, token);
|
||||
}
|
||||
|
||||
private Map<String, Object> createInput(String subject, UUID projectId) {
|
||||
var input = new HashMap<String, Object>();
|
||||
input.put("requestId", UUID.randomUUID());
|
||||
input.put("subject", subject);
|
||||
input.put("category", "AI");
|
||||
input.put("projectId", projectId);
|
||||
input.put("content", "Description");
|
||||
return input;
|
||||
}
|
||||
|
||||
private Map<String, Object> command(String operation) {
|
||||
return new HashMap<>(Map.of("requestId", UUID.randomUUID(), "operation", operation));
|
||||
}
|
||||
|
||||
private ResultActions get(Account account, String path) throws Exception {
|
||||
return mvc.perform(
|
||||
MockMvcRequestBuilders.get(path).header("Authorization", "Bearer " + account.token()));
|
||||
}
|
||||
|
||||
private ResultActions post(Account account, String path, Object payload) throws Exception {
|
||||
return mvc.perform(
|
||||
MockMvcRequestBuilders.post(path)
|
||||
.header("Authorization", "Bearer " + account.token())
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json.writeValueAsString(payload)));
|
||||
}
|
||||
|
||||
private JsonNode json(ResultActions response) throws Exception {
|
||||
return json.readTree(
|
||||
response.andExpect(status().isOk()).andReturn().getResponse().getContentAsByteArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
class WalletTaskIntegrationTest {
|
||||
@Autowired IdentityService identity;
|
||||
@Autowired WalletService wallets;
|
||||
@Autowired TaskService tasks;
|
||||
@Autowired ObjectMapper json;
|
||||
@Autowired ReconciliationService reconciliation;
|
||||
@MockitoBean EphemeralStore store;
|
||||
UUID user, project, model, price, provider;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
TestDatabase.clear(identity.db());
|
||||
user = identity.create("billing@test.com", "Tester", "PasswordStrong123", "local");
|
||||
project = UUID.randomUUID();
|
||||
model = UUID.randomUUID();
|
||||
provider = UUID.randomUUID();
|
||||
price = UUID.randomUUID();
|
||||
identity
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,?,'','Vue 3','')",
|
||||
project,
|
||||
user,
|
||||
"Test");
|
||||
identity
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_providers(id,name,base_url,api_type,encrypted_key,enabled,timeout_seconds)"
|
||||
+ " VALUES"
|
||||
+ " (?,'Test','https://api.openai.com/v1','OPENAI','not-a-real-key',TRUE,30)",
|
||||
provider);
|
||||
identity
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_models(id,provider_id,name,model_id,context_length,max_output,tools_enabled,vision_enabled,enabled)"
|
||||
+ " VALUES (?,?,'Test','test-model',8192,256,TRUE,FALSE,TRUE)",
|
||||
model,
|
||||
provider);
|
||||
identity
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_model_price_versions(id,model_id,input_price,output_price,cache_read_price,cache_write_price)"
|
||||
+ " VALUES (?,?,1,2,0.1,1)",
|
||||
price,
|
||||
model);
|
||||
identity.db().update("UPDATE ai_models SET current_price_version=? WHERE id=?", price, model);
|
||||
}
|
||||
|
||||
BigDecimal n(String v) {
|
||||
return new BigDecimal(v);
|
||||
}
|
||||
|
||||
void credit() {
|
||||
wallets.adjust(user, n("100"), "credit", "Test credit", user, "local");
|
||||
}
|
||||
|
||||
UUID task() {
|
||||
credit();
|
||||
return UUID.fromString(
|
||||
tasks.create(user, project, model, "Build and test", n("20"), "task").get("id").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void creditIdempotencyAndConflict() {
|
||||
credit();
|
||||
credit();
|
||||
assertEquals(0, n("100").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() -> wallets.adjust(user, n("101"), "credit", "Test credit", user, "local"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reserveSettleReleaseAndReplay() {
|
||||
credit();
|
||||
UUID r = wallets.reserve(user, n("20"), "test");
|
||||
wallets.charge(r, n("13.5"), "usage:test");
|
||||
wallets.charge(r, n("13.5"), "usage:test");
|
||||
wallets.release(r);
|
||||
wallets.release(r);
|
||||
assertEquals(0, n("86.5").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cannotOverspendOrDebitReservedFunds() {
|
||||
credit();
|
||||
UUID r = wallets.reserve(user, n("90"), "test");
|
||||
assertThrows(ApiError.Failure.class, () -> wallets.reserve(user, n("11"), "second"));
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() -> wallets.adjust(user, n("-11"), "debit", "test", user, "local"));
|
||||
assertThrows(ApiError.Failure.class, () -> wallets.charge(r, n("91"), "too-much"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentReservationsCannotOverdraw() throws Exception {
|
||||
credit();
|
||||
try (var pool = Executors.newFixedThreadPool(6)) {
|
||||
List<Callable<Boolean>> calls = new ArrayList<>();
|
||||
for (int i = 0; i < 6; i++) {
|
||||
String business = "concurrent-" + i;
|
||||
calls.add(
|
||||
() -> {
|
||||
try {
|
||||
wallets.reserve(user, n("30"), business);
|
||||
return true;
|
||||
} catch (ApiError.Failure f) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
int success = 0;
|
||||
for (Future<Boolean> f : pool.invokeAll(calls)) if (f.get()) success++;
|
||||
assertEquals(3, success);
|
||||
assertEquals(0, n("90").compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void taskReplaySnapshotAndProjectExclusivity() {
|
||||
UUID id = task();
|
||||
assertEquals(
|
||||
id.toString(),
|
||||
tasks.create(user, project, model, "Build and test", n("20"), "task").get("id").toString());
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() -> tasks.create(user, project, model, "Another task", n("20"), "second"));
|
||||
identity
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_model_price_versions(id,model_id,input_price,output_price,cache_read_price,cache_write_price)"
|
||||
+ " VALUES (?,?,99,99,99,99)",
|
||||
UUID.randomUUID(),
|
||||
model);
|
||||
assertEquals(price.toString(), tasks.find(id, user).get("price_version_id").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void pausedCancelledTaskReleasesBudget() {
|
||||
UUID id = task();
|
||||
tasks.action(id, user, "pause");
|
||||
assertEquals("PAUSED", tasks.find(id, user).get("status"));
|
||||
identity.db().update("UPDATE ai_tasks SET step_count=30 WHERE id=?", id);
|
||||
tasks.action(id, user, "resume");
|
||||
assertEquals(60, ((Number) tasks.find(id, user).get("max_steps")).intValue());
|
||||
tasks.action(id, user, "cancel");
|
||||
assertEquals("CANCELLED", tasks.find(id, user).get("status"));
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactUsageChargesWithSnapshot() throws Exception {
|
||||
UUID id = task();
|
||||
var claimed = tasks.claim();
|
||||
String lease = claimed.get("lease_token").toString();
|
||||
var usage =
|
||||
new ModelGateway.Usage(
|
||||
1000000,
|
||||
1000000,
|
||||
1000000,
|
||||
0,
|
||||
true,
|
||||
json.readTree("{\"prompt_tokens\":2000000,\"completion_tokens\":1000000}"));
|
||||
var result =
|
||||
new ModelGateway.Result(
|
||||
json.createObjectNode().put("role", "assistant").put("content", "Done"),
|
||||
List.of(),
|
||||
usage,
|
||||
"request");
|
||||
tasks.response(id, lease, result);
|
||||
tasks.finish(id, lease, "COMPLETED", "done", true);
|
||||
assertEquals(0, n("96.9").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingUsageRequiresManualReconciliation() {
|
||||
UUID id = task();
|
||||
var claimed = tasks.claim();
|
||||
var usage = new ModelGateway.Usage(0, 0, 0, 0, false, json.createObjectNode());
|
||||
tasks.response(
|
||||
id,
|
||||
claimed.get("lease_token").toString(),
|
||||
new ModelGateway.Result(
|
||||
json.createObjectNode().put("role", "assistant"), List.of(), usage, "missing"));
|
||||
assertEquals("REVIEW_REQUIRED", tasks.find(id, user).get("status"));
|
||||
assertEquals(0, n("20").compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
reconciliation.resolve(id, user, n("3"), "Reconciled with provider console", "local");
|
||||
assertEquals(0, n("97").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiredLeaseDoesNotReplayOrReleaseFunds() {
|
||||
UUID id = task();
|
||||
tasks.claim();
|
||||
identity
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE ai_tasks SET lease_expires_at=TIMESTAMP '2000-01-01 00:00:00' WHERE id=?", id);
|
||||
assertNull(tasks.claim());
|
||||
assertEquals("REVIEW_REQUIRED", tasks.find(id, user).get("status"));
|
||||
assertThrows(ApiError.Failure.class, () -> tasks.action(id, user, "resume"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user