forked from longtao.wu/droboticflow
74 lines
3.8 KiB
Go
74 lines
3.8 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
func TestSupplierPoolConversionAdminPreviewAndConfirmation(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
ctx := t.Context()
|
|
db := store.NewMemoryStore()
|
|
h := NewHandler(db, nil)
|
|
root := bootstrapSuperAdminSession(t, h)
|
|
org := adminCreateOrganization(t, h, root, "Pool conversion", "pool-conversion")
|
|
if err := db.UpsertSupplier(ctx, domain.SupplierRecord{ID: "supplier-a", OrganizationID: org.ID, Name: "Supplier A", Status: "active"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
admin := adminCreateUser(t, h, root, org.ID, "Admin", "pool-admin@example.invalid", domain.UserRoleAdmin, "test-password")
|
|
manager := adminCreateUser(t, h, root, org.ID, "Manager", "pool-manager@example.invalid", domain.UserRoleSupplierManager, "test-password", "supplier-a")
|
|
adminCookie := loginSession(t, h, admin.Phone, "test-password")
|
|
managerCookie := loginSession(t, h, manager.Phone, "test-password")
|
|
project := domain.ProjectRecord{ID: "pool-project", OrganizationID: org.ID, Name: "Pool project"}
|
|
if err := db.UpsertProject(ctx, project); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
batch := domain.HumanBatchRecord{ID: "pool-batch", OrganizationID: org.ID, ProjectID: project.ID, PluginID: "annotation-qa", WorkflowID: "pool-workflow", SupplierID: "supplier-a", ProcessingProfile: domain.ProcessingProfileRawHuman, Stages: domain.DefaultFanyuV58HumanStages(), Status: domain.HumanBatchActive, ItemCount: 1}
|
|
if err := db.UpsertHumanBatch(ctx, batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
task := domain.AnnotationTaskRecord{ID: "pool-task", OrganizationID: org.ID, ProjectID: project.ID, WorkflowID: batch.WorkflowID, SupplierID: batch.SupplierID, ProcessingProfile: domain.ProcessingProfileRawHuman, Status: domain.AnnotationTaskStatusPendingAnnotation}
|
|
if err := db.UpsertAnnotationTask(ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item := domain.HumanItemRecord{ID: "pool-item", OrganizationID: org.ID, ProjectID: project.ID, PluginID: "annotation-qa", BatchID: batch.ID, WorkflowID: batch.WorkflowID, LegacyTaskID: task.ID, SupplierID: batch.SupplierID, ProcessingProfile: domain.ProcessingProfileRawHuman, CurrentStageID: domain.FanyuV58StageAnnotationCombined, Status: domain.HumanItemReady, Version: 1}
|
|
if err := db.UpsertHumanItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := "/api/projects/" + project.ID + "/supplier-pool-preview"
|
|
if response := serveJSONWithCookie(h, "GET", path, nil, managerCookie); response.Code != 403 {
|
|
t.Fatalf("manager preview status=%d", response.Code)
|
|
}
|
|
response := serveJSONWithCookie(h, "GET", path, nil, adminCookie)
|
|
if response.Code != 200 {
|
|
t.Fatalf("admin preview status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
var preview store.SupplierPoolPreview
|
|
if err := json.Unmarshal(response.Body.Bytes(), &preview); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if preview.EligibleItemCount != 1 || preview.PreviewToken == "" {
|
|
t.Fatalf("bad preview: %+v", preview)
|
|
}
|
|
postPath := "/api/projects/" + project.ID + "/supplier-pool-conversions"
|
|
body, _ := json.Marshal(map[string]string{"previewToken": preview.PreviewToken})
|
|
if response := serveJSONWithCookie(h, "POST", postPath, body, managerCookie); response.Code != 403 {
|
|
t.Fatalf("manager conversion status=%d", response.Code)
|
|
}
|
|
response = serveJSONWithCookie(h, "POST", postPath, body, adminCookie)
|
|
if response.Code != 200 {
|
|
t.Fatalf("conversion status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
converted, _ := db.GetHumanItem(ctx, item.ID)
|
|
if converted.SupplierID != "" {
|
|
t.Fatal("item was not converted")
|
|
}
|
|
response = serveJSONWithCookie(h, "POST", postPath, body, adminCookie)
|
|
if response.Code != 200 {
|
|
t.Fatalf("idempotent retry status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
}
|