forked from longtao.wu/droboticflow
218 lines
7.5 KiB
Go
218 lines
7.5 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"droboticflow/internal/testsupport"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func TestQueryReplicaPoolPermissionRevocationAndSelfReviewLocal(t *testing.T) {
|
|
testQueryReplicaPermissionRevocationAndSelfReview(t, false)
|
|
}
|
|
|
|
func TestQueryReplicaCountOnlyPermissionRevocationAndSelfReviewLocal(t *testing.T) {
|
|
testQueryReplicaPermissionRevocationAndSelfReview(t, true)
|
|
}
|
|
|
|
func TestQueryPeriodicPermissionRevocationAndSelfReviewLocal(t *testing.T) {
|
|
testQueryReplicaPermissionRevocationAndSelfReview(t, false, true)
|
|
}
|
|
|
|
func testQueryReplicaPermissionRevocationAndSelfReview(t *testing.T, countsOnly bool, periodic ...bool) {
|
|
if os.Getenv("QUERY_REPLICA_LOCAL_TEST") != "1" {
|
|
t.Skip("isolated local source and query replica required")
|
|
}
|
|
t.Setenv("ANNOTATION_SOURCE_VALIDATION_MODE", "advisory")
|
|
ctx := t.Context()
|
|
clients := []*mongo.Client{}
|
|
stores := []*store.MongoStore{}
|
|
for _, uri := range []string{"mongodb://127.0.0.1:27119/?directConnection=true", testsupport.QueryReplicaTargetURI(t)} {
|
|
c, e := mongo.Connect(ctx, options.Client().ApplyURI(uri))
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
db := c.Database(domain.NewID("query_api_test"))
|
|
t.Cleanup(func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
_ = db.Drop(ctx)
|
|
_ = c.Disconnect(ctx)
|
|
})
|
|
clients = append(clients, c)
|
|
stores = append(stores, store.NewMongoStore(db))
|
|
}
|
|
source, target := stores[0], stores[1]
|
|
if len(periodic) > 0 && periodic[0] {
|
|
if err := target.ConfigureQueryRefreshMode("periodic"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
_ = clients
|
|
source.EnableQuerySourceTracking(true)
|
|
if e := source.EnsureIndexes(ctx); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
f := newFanyuV5FourStageFixture(t, domain.FanyuV5StageSemanticReview, domain.HumanItemReady, "", 1)
|
|
project, e := f.store.GetProject(ctx, f.item.ProjectID)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = source.UpsertProject(ctx, *project); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
supplier, e := f.store.GetSupplier(ctx, f.quality.SupplierID)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = source.UpsertSupplier(ctx, *supplier); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for i, u := range []domain.UserRecord{f.cutter, f.annotator, f.quality} {
|
|
u.Phone = fmt.Sprint(13000000001 + i)
|
|
if e = source.UpsertUser(ctx, u); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
membership := domain.ProjectAnnotationMembershipRecord{ID: "quality-membership", OrganizationID: f.quality.OrganizationID, ProjectID: f.item.ProjectID, UserID: f.quality.ID, WorkgroupID: f.task.WorkgroupID, VendorID: f.task.VendorID, AssignmentRole: domain.AnnotationRoleQualityReviewer}
|
|
if e = source.UpsertProjectAnnotationMembership(ctx, membership); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
member := domain.ProjectMemberRecord{ID: "project-quality", OrganizationID: f.quality.OrganizationID, ProjectID: f.item.ProjectID, UserID: f.quality.ID, SupplierID: f.quality.SupplierID}
|
|
if e = source.UpsertProjectMember(ctx, member); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = source.UpsertAnnotationTask(ctx, f.task); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = source.UpsertHumanBatch(ctx, f.batch); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
f.item.Subject.Payload = map[string]any{"annotationDraft": "latest annotation must remain"}
|
|
if e = source.UpsertHumanItem(ctx, f.item); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
q, e := store.NewQueryReplica(source, target)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for _, fn := range []func(context.Context) error{q.VerifyTopology, q.EnsureSourceIndexes, q.EnsureTargetIndexes, q.Acquire} {
|
|
if e = fn(ctx); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
for n := 0; n < 100; n++ {
|
|
ready, e := q.RunOnce(ctx, true)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if ready {
|
|
break
|
|
}
|
|
if n == 99 {
|
|
t.Fatal("projection incomplete")
|
|
}
|
|
}
|
|
if e = source.ConfigureQueryReplicaReports(target, time.Minute); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
source.EnableQueryReplicaPoolReads(!countsOnly)
|
|
source.EnableQueryReplicaCountReads(countsOnly)
|
|
handler := NewHandler(source, nil)
|
|
request := func(path string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(http.MethodGet, path, nil)
|
|
r = r.WithContext(context.WithValue(r.Context(), authContextKey{}, &authenticatedPrincipal{User: f.quality, Organization: domain.OrganizationRecord{ID: f.quality.OrganizationID, Status: domain.OrganizationStatusActive}}))
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
path := "/api/human-item-summaries?pluginId=annotation-qa&pool=available&stageIds=semantic_review"
|
|
result := request(path)
|
|
var body struct {
|
|
Summaries []humanPoolSummary `json:"summaries"`
|
|
}
|
|
if result.Code != 200 || json.Unmarshal(result.Body.Bytes(), &body) != nil || len(body.Summaries) != 1 || body.Summaries[0].CanClaim == nil || !*body.Summaries[0].CanClaim {
|
|
t.Fatalf("initial claim flag %d %s", result.Code, result.Body.String())
|
|
}
|
|
// Do not project this change. Eligibility must still use current source data.
|
|
f.item.LatestAuthorUserID = f.quality.ID
|
|
f.item.Version++
|
|
if e = source.UpsertHumanItem(ctx, f.item); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
result = request(path)
|
|
body.Summaries = nil
|
|
if result.Code != 200 || json.Unmarshal(result.Body.Bytes(), &body) != nil || len(body.Summaries) != 1 || body.Summaries[0].CanClaim == nil || *body.Summaries[0].CanClaim {
|
|
t.Fatalf("self review escaped current authority %d %s", result.Code, result.Body.String())
|
|
}
|
|
|
|
// A batch policy change not yet projected must affect this very next count.
|
|
f.item.LatestAuthorUserID = f.annotator.ID
|
|
f.item.Version++
|
|
if e = source.UpsertHumanItem(ctx, f.item); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
changed := f.batch
|
|
changed.Stages = []domain.HumanStageDefinition{{ID: "different-stage", Kind: domain.HumanStageReview, RequiredRole: string(domain.UserRoleQualityReviewer)}}
|
|
if e = source.UpsertHumanBatch(ctx, changed); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
countPath := "/api/human-item-counts?pluginId=annotation-qa&stageIds=semantic_review"
|
|
result = request(countPath)
|
|
var counted struct {
|
|
Counts map[string]int `json:"counts"`
|
|
}
|
|
if result.Code != 200 || json.Unmarshal(result.Body.Bytes(), &counted) != nil || counted.Counts["available"] != 0 {
|
|
t.Fatalf("pending policy bypassed authority: %d %s", result.Code, result.Body.String())
|
|
}
|
|
if e = source.UpsertHumanBatch(ctx, f.batch); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for n := 0; n < 100; n++ {
|
|
ready, e := q.RunOnce(ctx, true)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if ready {
|
|
break
|
|
}
|
|
}
|
|
result = request(countPath)
|
|
if result.Code != 200 || json.Unmarshal(result.Body.Bytes(), &counted) != nil || counted.Counts["available"] != 1 {
|
|
t.Fatalf("fact epoch retained stale count: %d %s", result.Code, result.Body.String())
|
|
}
|
|
if _, e = source.DeleteProjectMember(ctx, member.ID); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for _, p := range []string{path, "/api/human-item-counts?pluginId=annotation-qa&stageIds=semantic_review"} {
|
|
result = request(p)
|
|
if result.Code != 200 {
|
|
t.Fatalf("revocation request failed %d %s", result.Code, result.Body.String())
|
|
}
|
|
var response map[string]any
|
|
if e = json.Unmarshal(result.Body.Bytes(), &response); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if list, ok := response["summaries"].([]any); ok && len(list) != 0 {
|
|
t.Fatal("revoked project still visible")
|
|
}
|
|
if counts, ok := response["counts"].(map[string]any); ok && counts["available"] != float64(0) {
|
|
t.Fatal("revoked count cached")
|
|
}
|
|
}
|
|
item, e := source.GetHumanItem(ctx, f.item.ID)
|
|
if e != nil || item.Subject.Payload["annotationDraft"] != "latest annotation must remain" {
|
|
t.Fatal("read path mutated annotation", e)
|
|
}
|
|
}
|