forked from longtao.wu/droboticflow
218 lines
13 KiB
Go
218 lines
13 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
func TestSupplierProjectAuthorizationAdminAndFutureWorkerAccess(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
ds := store.NewMemoryStore()
|
|
h := NewHandler(ds, nil)
|
|
root := bootstrapSuperAdminSession(t, h)
|
|
org := adminCreateOrganization(t, h, root, "Grant Org", "grant-org")
|
|
admin := adminCreateUser(t, h, root, org.ID, "Admin", "grant-admin@example.com", domain.UserRoleAdmin, "test-password")
|
|
adminCookie := loginSession(t, h, admin.Phone, "test-password")
|
|
project, err := ds.CreateProject(t.Context(), domain.CreateProjectInput{OrganizationID: org.ID, Name: "Shared Capture"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
supplier := domain.SupplierRecord{ID: "supplier-grant-http", OrganizationID: org.ID, Name: "Supplier", Status: "active"}
|
|
if err := ds.UpsertSupplier(t.Context(), supplier); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manager := adminCreateUser(t, h, root, org.ID, "Supplier Manager", "grant-manager@example.com", domain.UserRoleSupplierManager, "test-password", supplier.ID)
|
|
managerCookie := loginSession(t, h, manager.Phone, "test-password")
|
|
worker := adminCreateUser(t, h, root, org.ID, "Future Worker", "grant-worker@example.com", domain.UserRoleAnnotator, "test-password", supplier.ID)
|
|
workerCookie := loginSession(t, h, worker.Phone, "test-password")
|
|
grantPath := "/api/projects/" + project.ID + "/supplier-authorizations/" + supplier.ID
|
|
if r := serveJSONWithCookie(h, "PUT", grantPath, nil, managerCookie); r.Code != 403 {
|
|
t.Fatalf("supplier manager granted project: %d %s", r.Code, r.Body.String())
|
|
}
|
|
r := serveJSONWithCookie(h, "PUT", grantPath, nil, adminCookie)
|
|
if r.Code != 200 || !jsonContains(r.Body.String(), `"qualityReviewerAvailable":false`) {
|
|
t.Fatalf("grant response: %d %s", r.Code, r.Body.String())
|
|
}
|
|
roles, err := (&server{store: ds}).annotationMembershipsForUser(t.Context(), org.ID, project.ID, worker.ID)
|
|
if err != nil || !membershipHasRole(roles, domain.AnnotationTaskRecord{ProjectID: project.ID, SupplierPool: true}, domain.AnnotationRoleAnnotator) {
|
|
t.Fatalf("future staff cannot work without personal member: %+v err=%v", roles, err)
|
|
}
|
|
r = serveJSONWithCookie(h, "GET", "/api/projects", nil, workerCookie)
|
|
if r.Code != 200 || !jsonContains(r.Body.String(), project.ID) {
|
|
t.Fatalf("future staff cannot see granted project: %d %s", r.Code, r.Body.String())
|
|
}
|
|
r = serveJSONWithCookie(h, "GET", "/api/suppliers/"+supplier.ID+"/project-authorizations", nil, managerCookie)
|
|
if r.Code != 200 || !jsonContains(r.Body.String(), project.ID) {
|
|
t.Fatalf("supplier manager cannot see own grant: %d %s", r.Code, r.Body.String())
|
|
}
|
|
batch := domain.HumanBatchRecord{ID: "grant-http-batch", OrganizationID: org.ID, ProjectID: project.ID, PluginID: "annotation-qa", WorkflowID: "grant-http-workflow", ProcessingProfile: domain.ProcessingProfileRawHuman, Stages: domain.DefaultFanyuV58HumanStages()}
|
|
task := domain.AnnotationTaskRecord{ID: "grant-http-task", OrganizationID: org.ID, ProjectID: project.ID, WorkflowID: batch.WorkflowID, SupplierID: supplier.ID, ProcessingProfile: domain.ProcessingProfileRawHuman, AssignedUserID: worker.ID, Status: domain.AnnotationTaskStatusPendingAnnotation}
|
|
item := domain.HumanItemRecord{ID: "grant-http-item", OrganizationID: org.ID, ProjectID: project.ID, PluginID: "annotation-qa", BatchID: batch.ID, WorkflowID: batch.WorkflowID, LegacyTaskID: task.ID, SupplierID: supplier.ID, ProcessingProfile: domain.ProcessingProfileRawHuman, CurrentStageID: domain.FanyuV58StageAnnotationCombined, AssignedUserID: worker.ID, Status: domain.HumanItemClaimed}
|
|
if err := ds.UpsertHumanBatch(t.Context(), batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ds.UpsertAnnotationTask(t.Context(), task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ds.UpsertHumanItem(t.Context(), item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ds.UpsertAnnotationTemplate(t.Context(), domain.AnnotationTemplateRecord{ID: "grant-http-template", OrganizationID: org.ID, ProjectID: project.ID, Name: "Private labels", Version: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ds.UpsertAnnotationWorklogEvent(t.Context(), domain.AnnotationWorklogEventRecord{ID: "grant-http-worklog", OrganizationID: org.ID, ProjectID: project.ID, TaskID: task.ID, UserID: worker.ID, SupplierID: supplier.ID, EventType: "assigned"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r = serveJSONWithCookie(h, "GET", "/api/plugins/annotation-qa/tasks/"+task.ID+"?view=summary", nil, workerCookie)
|
|
if r.Code != 200 {
|
|
t.Fatalf("grant-only worker cannot read assigned task: %d %s", r.Code, r.Body.String())
|
|
}
|
|
for _, path := range []string{"/api/plugins/annotation-qa/templates?projectId=" + project.ID, "/api/plugins/annotation-qa/workload-summary?projectId=" + project.ID} {
|
|
response := serveJSONWithCookie(h, "GET", path, nil, workerCookie)
|
|
if response.Code != 200 || !jsonContains(response.Body.String(), worker.ID) && !jsonContains(response.Body.String(), "grant-http-template") {
|
|
t.Fatalf("active grant lost metadata %s: %d %s", path, response.Code, response.Body.String())
|
|
}
|
|
}
|
|
r = serveJSONWithCookie(h, "DELETE", grantPath, nil, adminCookie)
|
|
if r.Code != 200 {
|
|
t.Fatalf("revoke response: %d %s", r.Code, r.Body.String())
|
|
}
|
|
var revoked domain.SupplierProjectAuthorization
|
|
if err := json.Unmarshal(r.Body.Bytes(), &revoked); err != nil || revoked.Status != store.SupplierAuthorizationRevoked {
|
|
t.Fatalf("revoke content=%s err=%v", r.Body.String(), err)
|
|
}
|
|
r = serveJSONWithCookie(h, "GET", "/api/projects", nil, workerCookie)
|
|
if r.Code != 200 || !jsonContains(r.Body.String(), project.ID) {
|
|
t.Fatalf("in-progress frozen task disappeared: %d %s", r.Code, r.Body.String())
|
|
}
|
|
item.Status = domain.HumanItemAccepted
|
|
if err := ds.UpsertHumanItem(t.Context(), item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r = serveJSONWithCookie(h, "GET", "/api/projects", nil, workerCookie)
|
|
if r.Code != 200 || jsonContains(r.Body.String(), project.ID) {
|
|
t.Fatalf("finished frozen task retained project listing: %d %s", r.Code, r.Body.String())
|
|
}
|
|
for _, path := range []string{"/api/plugins/annotation-qa/templates?projectId=" + project.ID, "/api/plugins/annotation-qa/workload-summary?projectId=" + project.ID} {
|
|
response := serveJSONWithCookie(h, "GET", path, nil, workerCookie)
|
|
if response.Code != 200 || jsonContains(response.Body.String(), "grant-http-template") || jsonContains(response.Body.String(), worker.ID) {
|
|
t.Fatalf("completed revoked grant leaked metadata %s: %d %s", path, response.Code, response.Body.String())
|
|
}
|
|
}
|
|
r = serveJSONWithCookie(h, "GET", "/api/suppliers/"+supplier.ID+"/project-authorizations", nil, managerCookie)
|
|
if r.Code != 200 || !jsonContains(r.Body.String(), `"status":"revoked"`) {
|
|
t.Fatalf("supplier manager cannot see revoked status: %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestSupplierGrantScopeDoesNotChangeOtherRawHumanWorkflows(t *testing.T) {
|
|
project, supplier := "project-other-raw", "supplier-other-raw"
|
|
legacy := []annotationProjectRole{{ProjectID: project, SupplierID: supplier, Role: domain.AnnotationRoleAnnotator, AuthorizationMode: "legacy"}}
|
|
grant := []annotationProjectRole{{ProjectID: project, SupplierID: supplier, Role: domain.AnnotationRoleAnnotator, AuthorizationMode: "supplier_grant", GrantStatus: store.SupplierAuthorizationActive}}
|
|
task := domain.AnnotationTaskRecord{ID: "other-raw-task", ProjectID: project, SupplierID: supplier, ProcessingProfile: domain.ProcessingProfileRawHuman}
|
|
if !membershipHasRole(legacy, task, domain.AnnotationRoleAnnotator) {
|
|
t.Fatal("non-Fanyu raw_human task lost its existing member access")
|
|
}
|
|
if membershipHasRole(grant, task, domain.AnnotationRoleAnnotator) {
|
|
t.Fatal("supplier grant leaked into unrelated raw_human workflow")
|
|
}
|
|
task.SupplierPool = true
|
|
if membershipHasRole(legacy, task, domain.AnnotationRoleAnnotator) || !membershipHasRole(grant, task, domain.AnnotationRoleAnnotator) {
|
|
t.Fatal("strict Fanyu pool must require explicit supplier authorization")
|
|
}
|
|
revoked := []annotationProjectRole{{ProjectID: project, SupplierID: supplier, Role: domain.AnnotationRoleAnnotator, AuthorizationMode: "supplier_grant", GrantStatus: store.SupplierAuthorizationRevoked, DrainTaskIDs: map[string]bool{task.ID: true}}}
|
|
if !membershipHasRole(revoked, task, domain.AnnotationRoleAnnotator) {
|
|
t.Fatal("unfinished frozen task inaccessible")
|
|
}
|
|
task.SupplierPoolClosed = true
|
|
if membershipHasRole(revoked, task, domain.AnnotationRoleAnnotator) {
|
|
t.Fatal("completed frozen task retained access")
|
|
}
|
|
}
|
|
|
|
func TestSupplierPoolTaskClassificationUsesHumanBatchStages(t *testing.T) {
|
|
ctx := t.Context()
|
|
ds := store.NewMemoryStore()
|
|
s := &server{store: ds}
|
|
org, project := "scope-org", "scope-project"
|
|
for _, tc := range []struct {
|
|
id string
|
|
stages []domain.HumanStageDefinition
|
|
want bool
|
|
}{
|
|
{id: "fanyu", stages: domain.DefaultFanyuV58HumanStages(), want: true},
|
|
{id: "other", stages: domain.DefaultFanyuV5HumanStages(), want: false},
|
|
} {
|
|
batch := domain.HumanBatchRecord{ID: "batch-" + tc.id, OrganizationID: org, ProjectID: project, WorkflowID: "workflow-" + tc.id, ProcessingProfile: domain.ProcessingProfileRawHuman, Stages: tc.stages}
|
|
if err := ds.UpsertHumanBatch(ctx, batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
task := domain.AnnotationTaskRecord{ID: "task-" + tc.id, OrganizationID: org, ProjectID: project, WorkflowID: batch.WorkflowID, ProcessingProfile: domain.ProcessingProfileRawHuman}
|
|
if err := ds.UpsertAnnotationTask(ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ds.UpsertHumanItem(ctx, domain.HumanItemRecord{ID: "item-" + tc.id, OrganizationID: org, ProjectID: project, PluginID: "annotation-qa", BatchID: batch.ID, WorkflowID: batch.WorkflowID, LegacyTaskID: task.ID, Status: domain.HumanItemReady}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := s.getAnnotationTaskForAccess(ctx, task.ID)
|
|
if err != nil || got == nil || got.SupplierPool != tc.want {
|
|
t.Fatalf("%s classification=%+v err=%v", tc.id, got, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRevokedPublicGrantKeepsIndependentWorkflowProjectVisible(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
ds := store.NewMemoryStore()
|
|
h := NewHandler(ds, nil)
|
|
root := bootstrapSuperAdminSession(t, h)
|
|
org := adminCreateOrganization(t, h, root, "Mixed Workflow Org", "mixed-workflow-org")
|
|
admin := adminCreateUser(t, h, root, org.ID, "Admin", "mixed-admin@example.com", domain.UserRoleAdmin, "test-password")
|
|
adminCookie := loginSession(t, h, admin.Phone, "test-password")
|
|
project, err := ds.CreateProject(t.Context(), domain.CreateProjectInput{OrganizationID: org.ID, Name: "Mixed Workflow"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
supplier := domain.SupplierRecord{ID: "supplier-mixed-workflow", OrganizationID: org.ID, Name: "Supplier", Status: "active"}
|
|
if err := ds.UpsertSupplier(t.Context(), supplier); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
worker := adminCreateUser(t, h, root, org.ID, "Worker", "mixed-worker@example.com", domain.UserRoleAnnotator, "test-password", supplier.ID)
|
|
workerCookie := loginSession(t, h, worker.Phone, "test-password")
|
|
if err := ds.UpsertProjectMember(t.Context(), domain.ProjectMemberRecord{
|
|
ID: "mixed-member", OrganizationID: org.ID, ProjectID: project.ID, UserID: worker.ID, SupplierID: supplier.ID,
|
|
Roles: []domain.AnnotationAssignmentRole{domain.AnnotationRoleAnnotator},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ds.UpsertWorkflowRelease(t.Context(), domain.WorkflowReleaseRecord{ID: "mixed-legacy-release", OrganizationID: org.ID, TemplateID: "mixed-template", Version: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ds.UpsertProjectWorkflowInstance(t.Context(), domain.ProjectWorkflowInstanceRecord{
|
|
ID: "mixed-legacy-instance", OrganizationID: org.ID, ProjectID: project.ID, ReleaseID: "mixed-legacy-release", Status: domain.ProjectWorkflowInstanceActive,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ds.UpsertAnnotationTemplate(t.Context(), domain.AnnotationTemplateRecord{ID: "mixed-template", OrganizationID: org.ID, ProjectID: project.ID, Name: "Legacy template", Version: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
grantPath := "/api/projects/" + project.ID + "/supplier-authorizations/" + supplier.ID
|
|
if r := serveJSONWithCookie(h, "PUT", grantPath, nil, adminCookie); r.Code != 200 {
|
|
t.Fatalf("grant response: %d %s", r.Code, r.Body.String())
|
|
}
|
|
if r := serveJSONWithCookie(h, "DELETE", grantPath, nil, adminCookie); r.Code != 200 {
|
|
t.Fatalf("revoke response: %d %s", r.Code, r.Body.String())
|
|
}
|
|
projects := serveJSONWithCookie(h, "GET", "/api/projects", nil, workerCookie)
|
|
if projects.Code != 200 || !jsonContains(projects.Body.String(), project.ID) {
|
|
t.Fatalf("independent workflow project disappeared after public grant revoke: %d %s", projects.Code, projects.Body.String())
|
|
}
|
|
templates := serveJSONWithCookie(h, "GET", "/api/plugins/annotation-qa/templates?projectId="+project.ID, nil, workerCookie)
|
|
if templates.Code != 200 || !jsonContains(templates.Body.String(), "mixed-template") {
|
|
t.Fatalf("independent workflow template disappeared after public grant revoke: %d %s", templates.Code, templates.Body.String())
|
|
}
|
|
}
|