feat: add assessment, privacy and support flows
This commit is contained in:
1 parent
ee3738ee73
commit
e4f24fa202
339 files changed
+56454
-1126
No files matched your search
@@ -0,0 +1,120 @@
|
||||
package assessment
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
)
|
||||
|
||||
type signalKeyring struct{}
|
||||
|
||||
func (signalKeyring) IdempotencyScope(operation, key, principal string) [32]byte {
|
||||
return sha256.Sum256([]byte(operation + ":" + key + ":" + principal))
|
||||
}
|
||||
|
||||
func (signalKeyring) Fingerprint(body []byte) [32]byte {
|
||||
return sha256.Sum256(body)
|
||||
}
|
||||
|
||||
func TestCanonicalSignalBodyContainsOnlyBoundedAllowlistedFields(t *testing.T) {
|
||||
attemptID, itemID := domain.AssessmentAttemptID(uuid.New()), uuid.New()
|
||||
at := time.Date(2026, time.October, 7, 12, 0, 0, 987654321, time.FixedZone("offset", 3*60*60))
|
||||
size := int32(128)
|
||||
event, err := domain.NewAssessmentSignalEvent(domain.AssessmentSignalEventInput{
|
||||
Kind: domain.AssessmentSignalPaste, ItemInstanceID: itemID, OccurredAt: at, SizeBytes: &size,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewAssessmentSignalEvent() error = %v", err)
|
||||
}
|
||||
validated, encoded, err := canonicalSignalBody(RecordSignalsCommand{
|
||||
ActorID: uuid.New(), AttemptID: attemptID, Events: []domain.AssessmentSignalEvent{event},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("canonicalSignalBody() error = %v", err)
|
||||
}
|
||||
if len(validated) != 1 || int64(len(encoded)) > assessmentSignalMaximumBodySize {
|
||||
t.Fatalf("canonical body has %d events and %d bytes", len(validated), len(encoded))
|
||||
}
|
||||
var document map[string]any
|
||||
if err := json.Unmarshal(encoded, &document); err != nil {
|
||||
t.Fatalf("json.Unmarshal() error = %v", err)
|
||||
}
|
||||
if document["attempt_id"] != attemptID.String() || len(document) != 2 {
|
||||
t.Fatalf("canonical request keys/value = %+v", document)
|
||||
}
|
||||
events, ok := document["events"].([]any)
|
||||
if !ok || len(events) != 1 {
|
||||
t.Fatalf("canonical events = %#v", document["events"])
|
||||
}
|
||||
fields, ok := events[0].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("canonical event = %#v", events[0])
|
||||
}
|
||||
if fields["kind"] != "paste" || fields["item_instance_id"] != itemID.String() || fields["size_bytes"] != float64(size) {
|
||||
t.Fatalf("canonical event fields = %+v", fields)
|
||||
}
|
||||
if _, ok := fields["occurred_at"].(string); !ok || len(fields) != 4 {
|
||||
t.Fatalf("canonical event includes unexpected fields: %+v", fields)
|
||||
}
|
||||
for _, forbidden := range []string{"answer", "answer_text", "clipboard", "content", "organization_id", "score"} {
|
||||
if strings.Contains(string(encoded), forbidden) {
|
||||
t.Errorf("canonical request contains forbidden term %q", forbidden)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignalIdempotencyFingerprintBindsAttemptAndRejectsInvalidKeys(t *testing.T) {
|
||||
actorID, itemID := uuid.New(), uuid.New()
|
||||
at := time.Now().UTC().Truncate(time.Microsecond)
|
||||
event, err := domain.NewAssessmentSignalEvent(domain.AssessmentSignalEventInput{
|
||||
Kind: domain.AssessmentSignalFullscreenExit, ItemInstanceID: itemID, OccurredAt: at,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewAssessmentSignalEvent() error = %v", err)
|
||||
}
|
||||
service := &SignalService{keyring: signalKeyring{}}
|
||||
command := RecordSignalsCommand{ActorID: actorID, AttemptID: domain.AssessmentAttemptID(uuid.New()), IdempotencyKey: "same-key", Events: []domain.AssessmentSignalEvent{event}}
|
||||
_, firstBody, err := canonicalSignalBody(command)
|
||||
if err != nil {
|
||||
t.Fatalf("canonicalSignalBody(first) error = %v", err)
|
||||
}
|
||||
first, err := service.prepareSignalIdempotency(command, firstBody, at)
|
||||
if err != nil {
|
||||
t.Fatalf("prepareSignalIdempotency(first) error = %v", err)
|
||||
}
|
||||
command.AttemptID = domain.AssessmentAttemptID(uuid.New())
|
||||
_, secondBody, err := canonicalSignalBody(command)
|
||||
if err != nil {
|
||||
t.Fatalf("canonicalSignalBody(second) error = %v", err)
|
||||
}
|
||||
second, err := service.prepareSignalIdempotency(command, secondBody, at)
|
||||
if err != nil {
|
||||
t.Fatalf("prepareSignalIdempotency(second) error = %v", err)
|
||||
}
|
||||
if first.scope != second.scope || first.record.Fingerprint == second.record.Fingerprint {
|
||||
t.Fatal("same actor key should share its scope and reject a different attempt body")
|
||||
}
|
||||
for _, invalidKey := range []string{"", strings.Repeat("a", maxAttemptIdempotencyKeyBytes+1), "key with spaces"} {
|
||||
command.IdempotencyKey = invalidKey
|
||||
if _, err := service.prepareSignalIdempotency(command, secondBody, at); !errors.Is(err, ErrInvalidAttemptKey) {
|
||||
t.Errorf("prepareSignalIdempotency(%q) error = %v, want ErrInvalidAttemptKey", invalidKey, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanonicalSignalBodyRejectsUnbuiltEvents(t *testing.T) {
|
||||
_, _, err := canonicalSignalBody(RecordSignalsCommand{
|
||||
ActorID: uuid.New(), AttemptID: domain.AssessmentAttemptID(uuid.New()),
|
||||
Events: []domain.AssessmentSignalEvent{{}},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("canonicalSignalBody() accepted a zero event")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user