forked from longtao.wu/droboticflow
97 lines
3.3 KiB
Go
97 lines
3.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestProjectDeletionBatchIsolatesFailuresAndKeepsReceipts(t *testing.T) {
|
|
t.Setenv("DROSYNC_COORDINATOR_PORT", "3003")
|
|
f := newCombinedDraftFixture(t)
|
|
projects := []domain.ProjectRecord{}
|
|
requests := []map[string]any{}
|
|
s := &server{store: f.store}
|
|
for i := 0; i < 3; i++ {
|
|
org := domain.DefaultOrganizationID
|
|
if i == 2 {
|
|
org = "other-org"
|
|
}
|
|
p, err := f.store.CreateProject(f.ctx, domain.CreateProjectInput{Name: "batch-fixture", OrganizationID: org})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
projects = append(projects, *p)
|
|
plan, err := s.buildProjectDeletionPlan(f.ctx, p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
digest := plan.Digest
|
|
if i == 1 {
|
|
digest = "stale-preview"
|
|
}
|
|
requests = append(requests, map[string]any{"projectId": p.ID, "confirmationName": p.Name, "digest": digest, "reason": "isolated test", "stopRunning": true})
|
|
}
|
|
response := f.request(http.MethodPost, "/api/project-deletions/batch", f.admin, map[string]any{"projects": requests})
|
|
if response.Code != http.StatusOK {
|
|
t.Fatalf("%d %s", response.Code, response.Body.String())
|
|
}
|
|
var batch struct {
|
|
Results []struct {
|
|
ProjectID string `json:"projectId"`
|
|
Status int `json:"status"`
|
|
}
|
|
}
|
|
if err := json.Unmarshal(response.Body.Bytes(), &batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(batch.Results) != 3 || batch.Results[0].Status != 202 || batch.Results[1].Status != 400 || batch.Results[2].Status != 404 {
|
|
t.Fatalf("unexpected isolated results: %s", response.Body.String())
|
|
}
|
|
deadline := time.Now().Add(3 * time.Second)
|
|
for {
|
|
task, _ := f.store.GetProjectDeletion(context.Background(), deletionTaskID(projects[0].ID))
|
|
if task != nil && task.Status == "completed" {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("not completed: %+v", task)
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
for _, p := range projects[1:] {
|
|
if got, _ := f.store.GetProject(f.ctx, p.ID); got == nil {
|
|
t.Fatal("failed/unauthorized project was deleted")
|
|
}
|
|
}
|
|
// An ambiguous client response can be retried without creating another deletion.
|
|
response = f.request(http.MethodPost, "/api/project-deletions/batch", f.admin, map[string]any{"projects": requests[:1]})
|
|
if response.Code != 200 {
|
|
t.Fatal(response.Body.String())
|
|
}
|
|
task, _ := f.store.GetProjectDeletion(f.ctx, deletionTaskID(projects[0].ID))
|
|
if task.Attempt != 1 {
|
|
t.Fatalf("duplicate attempt: %+v", task)
|
|
}
|
|
}
|
|
|
|
func TestProjectDeletionBatchRejectsDuplicateSelectionAndNonAdmin(t *testing.T) {
|
|
t.Setenv("DROSYNC_COORDINATOR_PORT", "3003")
|
|
f := newCombinedDraftFixture(t)
|
|
payload := map[string]any{"projects": []map[string]any{{"projectId": "draft-project"}, {"projectId": "draft-project"}}}
|
|
if r := f.request(http.MethodPost, "/api/project-deletions/batch", f.admin, payload); r.Code != 400 {
|
|
t.Fatalf("duplicates: %d", r.Code)
|
|
}
|
|
s := &server{store: f.store, requireSessionAuth: true}
|
|
f.handler = s.requireBusinessRole(domain.UserRoleAdmin)(http.HandlerFunc(s.handleProjectDeletionBatch))
|
|
if r := f.request(http.MethodPost, "/api/project-deletions/batch", f.annotator, payload); r.Code != 403 {
|
|
t.Fatalf("role: %d", r.Code)
|
|
}
|
|
if tasks, _ := f.store.ListProjectDeletions(f.ctx, domain.DefaultOrganizationID); len(tasks) != 0 {
|
|
t.Fatal("invalid request started deletion")
|
|
}
|
|
}
|