droboticflow/internal/api/annotation_task_claim_eligibility_test.go

151 lines
5.4 KiB
Go

package api
import (
"encoding/json"
"net/http"
"testing"
"droboticflow/internal/domain"
)
func TestAnnotationTaskDetailShipsServerClaimEligibilityForSupplierGrant(t *testing.T) {
fixture := newCombinedDraftFixture(t)
grantReviewer := domain.UserRecord{
ID: "grant-only-reviewer", OrganizationID: domain.DefaultOrganizationID,
SupplierID: fixture.reviewer.SupplierID, Role: domain.UserRoleQualityReviewer,
Status: domain.UserStatusActive,
}
if err := fixture.store.UpsertUser(fixture.ctx, grantReviewer); err != nil {
t.Fatal(err)
}
if _, err := fixture.store.SetSupplierProjectAuthorization(fixture.ctx, domain.DefaultOrganizationID, fixture.task.ProjectID, grantReviewer.SupplierID, true, fixture.admin.ID); err != nil {
t.Fatal(err)
}
batch, err := fixture.store.GetHumanBatch(fixture.ctx, fixture.item.BatchID)
if err != nil || batch == nil {
t.Fatalf("load batch: batch=%#v err=%v", batch, err)
}
batch.ProcessingProfile = domain.ProcessingProfileRawHuman
batch.Stages = domain.DefaultFanyuV58HumanStages()
if err := fixture.store.UpsertHumanBatch(fixture.ctx, *batch); err != nil {
t.Fatal(err)
}
task := fixture.task
task.SupplierID = grantReviewer.SupplierID
task.ProcessingProfile = domain.ProcessingProfileRawHuman
task.Status = domain.AnnotationTaskStatusPendingQuality
if err := fixture.store.UpsertAnnotationTask(fixture.ctx, task); err != nil {
t.Fatal(err)
}
item := fixture.item
item.SupplierID = grantReviewer.SupplierID
item.ProcessingProfile = domain.ProcessingProfileRawHuman
item.CurrentStageID = domain.FanyuV58StageQualityReview
item.Status = domain.HumanItemReady
item.AssignedUserID = ""
if err := fixture.store.UpsertHumanItem(fixture.ctx, item); err != nil {
t.Fatal(err)
}
path := "/api/plugins/annotation-qa/tasks/" + task.ID
read := func(user domain.UserRecord) (*http.Response, map[string]json.RawMessage, error) {
recorder := fixture.request(http.MethodGet, path, user, nil)
var body map[string]json.RawMessage
if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
return recorder.Result(), nil, err
}
return recorder.Result(), body, nil
}
assertCanClaim := func(user domain.UserRecord, want bool) {
t.Helper()
response, body, err := read(user)
if err != nil {
t.Fatal(err)
}
if response.StatusCode != http.StatusOK {
t.Fatalf("detail status=%d body=%v", response.StatusCode, body)
}
var detail struct {
HumanItem struct {
CanClaim *bool `json:"canClaim"`
} `json:"humanItem"`
}
if err := json.Unmarshal(mustJSON(t, body), &detail); err != nil {
t.Fatal(err)
}
if detail.HumanItem.CanClaim == nil || *detail.HumanItem.CanClaim != want {
t.Fatalf("detail humanItem.canClaim=%v want %v; response=%s", detail.HumanItem.CanClaim, want, mustJSON(t, body))
}
}
assertCanClaim(grantReviewer, true)
// The same active project grant does not make a task claimable once owned,
// processing, or terminal. Detail access remains governed by the existing
// read authorization path.
for _, tc := range []struct {
name string
status domain.HumanItemStatus
owner string
}{
{name: "assigned", status: domain.HumanItemReady, owner: grantReviewer.ID},
{name: "processing", status: domain.HumanItemClaimed, owner: grantReviewer.ID},
{name: "terminal", status: domain.HumanItemAccepted},
} {
t.Run(tc.name, func(t *testing.T) {
updated := item
updated.Status, updated.AssignedUserID = tc.status, tc.owner
if err := fixture.store.UpsertHumanItem(fixture.ctx, updated); err != nil {
t.Fatal(err)
}
assertCanClaim(grantReviewer, false)
})
}
// Revoke the grant while the task is unstarted and unassigned. This user
// must lose detail access instead of receiving a claimable candidate.
if _, err := fixture.store.SetSupplierProjectAuthorization(fixture.ctx, domain.DefaultOrganizationID, task.ProjectID, grantReviewer.SupplierID, false, fixture.admin.ID); err != nil {
t.Fatal(err)
}
updated := item
if err := fixture.store.UpsertHumanItem(fixture.ctx, updated); err != nil {
t.Fatal(err)
}
revoked := fixture.request(http.MethodGet, path, grantReviewer, nil)
if revoked.Code != http.StatusForbidden {
t.Fatalf("revoked grant detail status=%d body=%s", revoked.Code, revoked.Body.String())
}
// No authorization also cannot expose a detail payload with a guessed flag.
noGrant := grantReviewer
noGrant.ID, noGrant.SupplierID = "no-grant-reviewer", "supplier-without-task-grant"
if err := fixture.store.UpsertSupplier(fixture.ctx, domain.SupplierRecord{ID: noGrant.SupplierID, OrganizationID: domain.DefaultOrganizationID, Name: "Unrelated supplier", Status: "active"}); err != nil {
t.Fatal(err)
}
if err := fixture.store.UpsertUser(fixture.ctx, noGrant); err != nil {
t.Fatal(err)
}
withoutGrant := fixture.request(http.MethodGet, path, noGrant, nil)
if withoutGrant.Code != http.StatusForbidden {
t.Fatalf("missing grant detail status=%d body=%s", withoutGrant.Code, withoutGrant.Body.String())
}
stored, err := fixture.store.GetHumanItem(fixture.ctx, item.ID)
if err != nil || stored == nil {
t.Fatalf("load final item: item=%#v err=%v", stored, err)
}
if stored.Status != item.Status || stored.Version != item.Version || stored.AssignedUserID != "" {
t.Fatalf("detail GET changed persisted item: %#v", stored)
}
}
func mustJSON(t *testing.T, value any) []byte {
t.Helper()
encoded, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
return encoded
}