feat(assessment): record capture hints and keep one window per attempt
This commit is contained in:
1 parent
c975a0d506
commit
bb917bf62e
108 files changed
+6649
-139
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -29,10 +30,18 @@ type AssessmentPresenter interface {
|
||||
Present(snapshot domain.AssessmentAttemptPublicSnapshot) (domain.AssessmentAttemptPublicSnapshot, error)
|
||||
}
|
||||
|
||||
// AssessmentWorkSessions holds and releases the single active work session of a
|
||||
// real attempt for the open page that calls.
|
||||
type AssessmentWorkSessions interface {
|
||||
Hold(ctx context.Context, command assessmentusecase.HoldWorkSessionCommand) (assessmentusecase.WorkSessionHold, error)
|
||||
Release(ctx context.Context, command assessmentusecase.ReleaseWorkSessionCommand) error
|
||||
}
|
||||
|
||||
// AssessmentHandler serves the candidate's frozen assessment attempts.
|
||||
type AssessmentHandler struct {
|
||||
operations AssessmentOperations
|
||||
presenter AssessmentPresenter
|
||||
operations AssessmentOperations
|
||||
presenter AssessmentPresenter
|
||||
workSessions AssessmentWorkSessions
|
||||
}
|
||||
|
||||
func NewAssessmentHandler(operations AssessmentOperations) *AssessmentHandler {
|
||||
@@ -49,6 +58,13 @@ func (handler *AssessmentHandler) WithPresenter(presenter AssessmentPresenter) *
|
||||
return handler
|
||||
}
|
||||
|
||||
// WithWorkSessions enables the hold and release operations of the work session.
|
||||
// Without it they answer as not implemented.
|
||||
func (handler *AssessmentHandler) WithWorkSessions(sessions AssessmentWorkSessions) *AssessmentHandler {
|
||||
handler.workSessions = sessions
|
||||
return handler
|
||||
}
|
||||
|
||||
// attemptResponse presents the snapshot and maps it to the response. Every
|
||||
// operation that returns an attempt goes through it.
|
||||
func (handler *AssessmentHandler) attemptResponse(snapshot domain.AssessmentAttemptPublicSnapshot) (api.AssessmentAttempt, error) {
|
||||
@@ -114,6 +130,26 @@ func (handler *Handler) SubmitMyAssessmentAnswer(
|
||||
return handler.assessment.SubmitMyAssessmentAnswer(ctx, request, params)
|
||||
}
|
||||
|
||||
func (handler *Handler) HoldMyAssessmentWorkSession(
|
||||
ctx context.Context,
|
||||
params api.HoldMyAssessmentWorkSessionParams,
|
||||
) (api.HoldMyAssessmentWorkSessionRes, error) {
|
||||
if handler.assessment == nil || handler.assessment.workSessions == nil {
|
||||
return handler.UnimplementedHandler.HoldMyAssessmentWorkSession(ctx, params)
|
||||
}
|
||||
return handler.assessment.HoldMyAssessmentWorkSession(ctx, params)
|
||||
}
|
||||
|
||||
func (handler *Handler) ReleaseMyAssessmentWorkSession(
|
||||
ctx context.Context,
|
||||
params api.ReleaseMyAssessmentWorkSessionParams,
|
||||
) (api.ReleaseMyAssessmentWorkSessionRes, error) {
|
||||
if handler.assessment == nil || handler.assessment.workSessions == nil {
|
||||
return handler.UnimplementedHandler.ReleaseMyAssessmentWorkSession(ctx, params)
|
||||
}
|
||||
return handler.assessment.ReleaseMyAssessmentWorkSession(ctx, params)
|
||||
}
|
||||
|
||||
func (handler *Handler) FinalizeMyAssessmentAttempt(
|
||||
ctx context.Context,
|
||||
request *api.AssessmentFinalization,
|
||||
@@ -199,6 +235,7 @@ func (handler *AssessmentHandler) StartMyAssessmentAttemptSession(
|
||||
result, err := handler.operations.StartSession(ctx, assessmentusecase.StartAttemptSessionCommand{
|
||||
ActorID: principal.UserID, AttemptID: domain.AssessmentAttemptID(params.AttemptID),
|
||||
ExpectedVersion: int64(request.ExpectedVersion), IdempotencyKey: params.IdempotencyKey,
|
||||
Client: params.XAttemptClient.Or(""),
|
||||
})
|
||||
if err != nil {
|
||||
return assessmentSessionFailure(ctx, err)
|
||||
@@ -236,6 +273,7 @@ func (handler *AssessmentHandler) SubmitMyAssessmentAnswer(
|
||||
ActorID: principal.UserID, AttemptID: domain.AssessmentAttemptID(params.AttemptID),
|
||||
ItemID: uuid.UUID(request.ItemID), ExpectedVersion: int64(request.ExpectedVersion),
|
||||
IdempotencyKey: params.IdempotencyKey, Answer: answer, Origin: assessmentRequestOrigin(ctx),
|
||||
Client: params.XAttemptClient.Or(""),
|
||||
})
|
||||
if err != nil {
|
||||
return assessmentAnswerFailure(ctx, err)
|
||||
@@ -250,6 +288,46 @@ func (handler *AssessmentHandler) SubmitMyAssessmentAnswer(
|
||||
return &api.AssessmentAttemptHeaders{CacheControl: api.CacheControlNoStoreNoStore, Response: response}, nil
|
||||
}
|
||||
|
||||
func (handler *AssessmentHandler) HoldMyAssessmentWorkSession(
|
||||
ctx context.Context,
|
||||
params api.HoldMyAssessmentWorkSessionParams,
|
||||
) (api.HoldMyAssessmentWorkSessionRes, error) {
|
||||
principal, ok := PrincipalFromContext(ctx)
|
||||
if !ok {
|
||||
return (*api.HoldMyAssessmentWorkSessionUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
|
||||
}
|
||||
hold, err := handler.workSessions.Hold(ctx, assessmentusecase.HoldWorkSessionCommand{
|
||||
ActorID: principal.UserID, AttemptID: domain.AssessmentAttemptID(params.AttemptID),
|
||||
Client: params.XAttemptClient, Origin: assessmentRequestOrigin(ctx),
|
||||
})
|
||||
if err != nil {
|
||||
return assessmentHoldFailure(ctx, err)
|
||||
}
|
||||
return &api.AssessmentWorkSessionHeaders{
|
||||
CacheControl: api.CacheControlNoStoreNoStore,
|
||||
Response: api.AssessmentWorkSession{
|
||||
HeldUntil: api.Timestamp(hold.HeldUntil), RenewAfterSeconds: int32(math.Ceil(hold.RenewEvery.Seconds())),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (handler *AssessmentHandler) ReleaseMyAssessmentWorkSession(
|
||||
ctx context.Context,
|
||||
params api.ReleaseMyAssessmentWorkSessionParams,
|
||||
) (api.ReleaseMyAssessmentWorkSessionRes, error) {
|
||||
principal, ok := PrincipalFromContext(ctx)
|
||||
if !ok {
|
||||
return (*api.ReleaseMyAssessmentWorkSessionUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
|
||||
}
|
||||
err := handler.workSessions.Release(ctx, assessmentusecase.ReleaseWorkSessionCommand{
|
||||
ActorID: principal.UserID, AttemptID: domain.AssessmentAttemptID(params.AttemptID), Client: params.XAttemptClient,
|
||||
})
|
||||
if err != nil {
|
||||
return assessmentReleaseFailure(ctx, err)
|
||||
}
|
||||
return &api.ReleaseMyAssessmentWorkSessionNoContent{CacheControl: api.CacheControlNoStoreNoStore}, nil
|
||||
}
|
||||
|
||||
func (handler *AssessmentHandler) FinalizeMyAssessmentAttempt(
|
||||
ctx context.Context,
|
||||
request *api.AssessmentFinalization,
|
||||
@@ -407,6 +485,8 @@ func assessmentSessionFailure(ctx context.Context, err error) (api.StartMyAssess
|
||||
case errors.Is(err, assessmentusecase.ErrAttemptVersionConflict), errors.Is(err, outbound.ErrAssessmentAttemptVersionConflict),
|
||||
errors.Is(err, domain.ErrAssessmentAttemptVersionConflict):
|
||||
return (*api.StartMyAssessmentAttemptSessionPreconditionFailed)(&problem.New(ctx, api.ProblemCodeVersionConflict).Response), nil
|
||||
case errors.Is(err, assessmentusecase.ErrAttemptWorkSessionBusy):
|
||||
return (*api.StartMyAssessmentAttemptSessionConflict)(&problem.New(ctx, api.ProblemCodeStateConflict).Response), nil
|
||||
case errors.Is(err, assessmentusecase.ErrAttemptKeyReused), errors.Is(err, outbound.ErrAssessmentAttemptIdempotencyConflict):
|
||||
return (*api.StartMyAssessmentAttemptSessionUnprocessableEntity)(&problem.New(ctx, api.ProblemCodeIdempotencyKeyReused).Response), nil
|
||||
case errors.Is(err, assessmentusecase.ErrInvalidAttemptKey):
|
||||
@@ -418,6 +498,35 @@ func assessmentSessionFailure(ctx context.Context, err error) (api.StartMyAssess
|
||||
}
|
||||
}
|
||||
|
||||
func assessmentHoldFailure(ctx context.Context, err error) (api.HoldMyAssessmentWorkSessionRes, error) {
|
||||
switch {
|
||||
case errors.Is(err, assessmentusecase.ErrAttemptForbidden), errors.Is(err, assessmentusecase.ErrAttemptEmailNotVerified),
|
||||
errors.Is(err, assessmentusecase.ErrAttemptConsentRequired):
|
||||
return (*api.HoldMyAssessmentWorkSessionForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil
|
||||
case errors.Is(err, assessmentusecase.ErrAttemptNotFound), errors.Is(err, outbound.ErrAssessmentAttemptNotFound):
|
||||
return (*api.HoldMyAssessmentWorkSessionNotFound)(&problem.New(ctx, api.ProblemCodeNotFound).Response), nil
|
||||
case errors.Is(err, assessmentusecase.ErrAttemptWorkSessionBusy), errors.Is(err, assessmentusecase.ErrAttemptWorkSessionState):
|
||||
return (*api.HoldMyAssessmentWorkSessionConflict)(&problem.New(ctx, api.ProblemCodeStateConflict).Response), nil
|
||||
case errors.Is(err, assessmentusecase.ErrInvalidWorkSessionClient):
|
||||
return (*api.HoldMyAssessmentWorkSessionBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationHeader, "/X-Attempt-Client").Response), nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func assessmentReleaseFailure(ctx context.Context, err error) (api.ReleaseMyAssessmentWorkSessionRes, error) {
|
||||
switch {
|
||||
case errors.Is(err, assessmentusecase.ErrAttemptForbidden), errors.Is(err, assessmentusecase.ErrAttemptEmailNotVerified):
|
||||
return (*api.ReleaseMyAssessmentWorkSessionForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil
|
||||
case errors.Is(err, assessmentusecase.ErrAttemptNotFound), errors.Is(err, outbound.ErrAssessmentAttemptNotFound):
|
||||
return (*api.ReleaseMyAssessmentWorkSessionNotFound)(&problem.New(ctx, api.ProblemCodeNotFound).Response), nil
|
||||
case errors.Is(err, assessmentusecase.ErrInvalidWorkSessionClient):
|
||||
return (*api.ReleaseMyAssessmentWorkSessionBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationHeader, "/X-Attempt-Client").Response), nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func assessmentAnswerFailure(ctx context.Context, err error) (api.SubmitMyAssessmentAnswerRes, error) {
|
||||
switch {
|
||||
case errors.Is(err, assessmentusecase.ErrAttemptForbidden), errors.Is(err, assessmentusecase.ErrAttemptEmailNotVerified),
|
||||
@@ -428,7 +537,7 @@ func assessmentAnswerFailure(ctx context.Context, err error) (api.SubmitMyAssess
|
||||
case errors.Is(err, assessmentusecase.ErrAttemptVersionConflict), errors.Is(err, outbound.ErrAssessmentAttemptVersionConflict),
|
||||
errors.Is(err, domain.ErrAssessmentAttemptVersionConflict):
|
||||
return (*api.SubmitMyAssessmentAnswerPreconditionFailed)(&problem.New(ctx, api.ProblemCodeVersionConflict).Response), nil
|
||||
case errors.Is(err, assessmentusecase.ErrAttemptStateConflict):
|
||||
case errors.Is(err, assessmentusecase.ErrAttemptStateConflict), errors.Is(err, assessmentusecase.ErrAttemptWorkSessionBusy):
|
||||
return (*api.SubmitMyAssessmentAnswerConflict)(&problem.New(ctx, api.ProblemCodeStateConflict).Response), nil
|
||||
case errors.Is(err, assessmentusecase.ErrAttemptKeyReused), errors.Is(err, outbound.ErrAssessmentAttemptIdempotencyConflict):
|
||||
return (*api.SubmitMyAssessmentAnswerUnprocessableEntity)(&problem.New(ctx, api.ProblemCodeIdempotencyKeyReused).Response), nil
|
||||
|
||||
@@ -3,16 +3,19 @@ package v1
|
||||
import (
|
||||
"context"
|
||||
|
||||
httpmiddleware "lincodex/backend/internal/adapter/primary/http/middleware"
|
||||
assessmentusecase "lincodex/backend/internal/usecase/assessment"
|
||||
)
|
||||
|
||||
// assessmentRequestOrigin hands the authenticated session and the limiter key
|
||||
// of the client address to the server signals. The usecase turns them into
|
||||
// keyed digests; neither value is stored or logged.
|
||||
// assessmentRequestOrigin hands the authenticated session, the limiter key of
|
||||
// the client address, and the User-Agent to the server signals. The usecase
|
||||
// turns them into keyed digests; no value is stored or logged.
|
||||
func assessmentRequestOrigin(ctx context.Context) assessmentusecase.RequestOrigin {
|
||||
session, ok := sessionFromContext(ctx)
|
||||
if !ok {
|
||||
return assessmentusecase.RequestOrigin{}
|
||||
}
|
||||
return assessmentusecase.RequestOrigin{SessionID: session.SessionID, ClientKey: clientKey(ctx)}
|
||||
return assessmentusecase.RequestOrigin{
|
||||
SessionID: session.SessionID, ClientKey: clientKey(ctx), UserAgent: httpmiddleware.RequestUserAgent(ctx),
|
||||
}
|
||||
}
|
||||
@@ -105,6 +105,9 @@ func assessmentSignalEventInput(requestEvent api.AssessmentSignalEvent) (domain.
|
||||
return assessmentClipboardInput(requestEvent)
|
||||
case api.AssessmentFullscreenExitSignalAssessmentSignalEvent:
|
||||
return assessmentFullscreenExitInput(requestEvent)
|
||||
case api.AssessmentSignalEventScreenshotKeyAssessmentSignalEvent,
|
||||
api.AssessmentSignalEventDevtoolsOpenAssessmentSignalEvent:
|
||||
return assessmentCaptureInput(requestEvent)
|
||||
default:
|
||||
return domain.AssessmentSignalEventInput{}, false
|
||||
}
|
||||
@@ -149,6 +152,20 @@ func assessmentFullscreenExitInput(requestEvent api.AssessmentSignalEvent) (doma
|
||||
}, true
|
||||
}
|
||||
|
||||
func assessmentCaptureInput(requestEvent api.AssessmentSignalEvent) (domain.AssessmentSignalEventInput, bool) {
|
||||
value, ok := requestEvent.GetAssessmentCaptureSignal()
|
||||
if !ok {
|
||||
return domain.AssessmentSignalEventInput{}, false
|
||||
}
|
||||
kind := domain.AssessmentSignalKind(value.Kind)
|
||||
if kind != domain.AssessmentSignalKind(requestEvent.Type) {
|
||||
return domain.AssessmentSignalEventInput{}, false
|
||||
}
|
||||
return domain.AssessmentSignalEventInput{
|
||||
Kind: kind, ItemInstanceID: uuid.UUID(value.ItemInstanceID), OccurredAt: time.Time(value.OccurredAt),
|
||||
}, true
|
||||
}
|
||||
|
||||
func signalInvalidBody(ctx context.Context) *api.RecordMyAssessmentAttemptSignalsBadRequest {
|
||||
return (*api.RecordMyAssessmentAttemptSignalsBadRequest)(
|
||||
&problem.InvalidField(ctx, api.ProblemFieldLocationBody, "").Response,
|
||||
|
||||
@@ -55,8 +55,14 @@ func TestAssessmentSignalHandlerMapsAllowlistedEventVariants(t *testing.T) {
|
||||
Kind: api.AssessmentFullscreenExitSignalKindFullscreenExit, ItemInstanceID: api.AssessmentItemId(itemID),
|
||||
OccurredAt: at,
|
||||
}),
|
||||
api.NewAssessmentSignalEventScreenshotKeyAssessmentSignalEvent(api.AssessmentCaptureSignal{
|
||||
Kind: api.AssessmentCaptureSignalKindScreenshotKey, ItemInstanceID: api.AssessmentItemId(itemID), OccurredAt: at,
|
||||
}),
|
||||
api.NewAssessmentSignalEventDevtoolsOpenAssessmentSignalEvent(api.AssessmentCaptureSignal{
|
||||
Kind: api.AssessmentCaptureSignalKindDevtoolsOpen, ItemInstanceID: api.AssessmentItemId(itemID), OccurredAt: at,
|
||||
}),
|
||||
}}
|
||||
receipt, err := domain.NewAssessmentSignalReceipt(domain.AssessmentSignalReceiptAccepted, 5, time.Now())
|
||||
receipt, err := domain.NewAssessmentSignalReceipt(domain.AssessmentSignalReceiptAccepted, 7, time.Now())
|
||||
if err != nil {
|
||||
t.Fatalf("NewAssessmentSignalReceipt() error = %v", err)
|
||||
}
|
||||
@@ -71,18 +77,19 @@ func TestAssessmentSignalHandlerMapsAllowlistedEventVariants(t *testing.T) {
|
||||
if !ok {
|
||||
t.Fatalf("RecordMyAssessmentAttemptSignals() result = %T, want receipt headers", result)
|
||||
}
|
||||
if response.CacheControl != api.CacheControlNoStoreNoStore || response.Response.AcceptedEvents != 5 ||
|
||||
if response.CacheControl != api.CacheControlNoStoreNoStore || response.Response.AcceptedEvents != 7 ||
|
||||
response.Response.Status != api.AssessmentSignalReceiptStatusAccepted {
|
||||
t.Fatalf("receipt response = %+v", response)
|
||||
}
|
||||
command := operations.command
|
||||
if operations.calls != 1 || command.ActorID != actorID || command.AttemptID != domain.AssessmentAttemptID(attemptID) ||
|
||||
command.IdempotencyKey != "event-batch" || len(command.Events) != 5 {
|
||||
command.IdempotencyKey != "event-batch" || len(command.Events) != 7 {
|
||||
t.Fatalf("record command = %+v, calls %d", command, operations.calls)
|
||||
}
|
||||
wantKinds := []domain.AssessmentSignalKind{
|
||||
domain.AssessmentSignalFocusLoss, domain.AssessmentSignalCopy, domain.AssessmentSignalPaste,
|
||||
domain.AssessmentSignalCut, domain.AssessmentSignalFullscreenExit,
|
||||
domain.AssessmentSignalScreenshotKey, domain.AssessmentSignalDevtoolsOpen,
|
||||
}
|
||||
for index, event := range command.Events {
|
||||
if event.Kind() != wantKinds[index] || event.ItemInstanceID() != itemID || !event.OccurredAt().Equal(time.Time(at)) {
|
||||
@@ -99,6 +106,25 @@ func TestAssessmentSignalHandlerMapsAllowlistedEventVariants(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssessmentSignalHandlerRefusesACaptureEventWhoseKindDisagreesWithItsVariant(t *testing.T) {
|
||||
itemID := uuid.New()
|
||||
mismatched := api.NewAssessmentSignalEventDevtoolsOpenAssessmentSignalEvent(api.AssessmentCaptureSignal{
|
||||
Kind: api.AssessmentCaptureSignalKindScreenshotKey, ItemInstanceID: api.AssessmentItemId(itemID),
|
||||
OccurredAt: api.Timestamp(time.Now().UTC()),
|
||||
})
|
||||
operations := &assessmentSignalOperationsStub{}
|
||||
handler := NewAssessmentSignalHandler(operations)
|
||||
result, err := handler.RecordMyAssessmentAttemptSignals(principalForTest(uuid.New()),
|
||||
&api.AssessmentSignalBatch{Events: []api.AssessmentSignalEvent{mismatched}},
|
||||
api.RecordMyAssessmentAttemptSignalsParams{AttemptID: uuid.New(), IdempotencyKey: "mismatch"})
|
||||
if err != nil {
|
||||
t.Fatalf("RecordMyAssessmentAttemptSignals() error = %v", err)
|
||||
}
|
||||
if _, ok := result.(*api.RecordMyAssessmentAttemptSignalsBadRequest); !ok || operations.calls != 0 {
|
||||
t.Fatalf("mismatched capture event result = %T, calls %d, want a 400 before the usecase", result, operations.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssessmentSignalHandlerMapsAuthorizationAndBusinessProblems(t *testing.T) {
|
||||
for _, testCase := range []struct {
|
||||
name string
|
||||
|
||||
@@ -3,6 +3,7 @@ package v1
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -305,3 +306,150 @@ func TestAPresenterFailureIsAServerErrorNotAnUnmarkedAttempt(t *testing.T) {
|
||||
t.Fatal("GetMyAssessmentAttempt() succeeded, want an error so that the task text is never served unmarked")
|
||||
}
|
||||
}
|
||||
|
||||
type workSessionsStub struct {
|
||||
holdCommand assessmentusecase.HoldWorkSessionCommand
|
||||
hold assessmentusecase.WorkSessionHold
|
||||
holdErr error
|
||||
releaseCommand assessmentusecase.ReleaseWorkSessionCommand
|
||||
releaseErr error
|
||||
}
|
||||
|
||||
func (stub *workSessionsStub) Hold(_ context.Context, command assessmentusecase.HoldWorkSessionCommand) (assessmentusecase.WorkSessionHold, error) {
|
||||
stub.holdCommand = command
|
||||
return stub.hold, stub.holdErr
|
||||
}
|
||||
|
||||
func (stub *workSessionsStub) Release(_ context.Context, command assessmentusecase.ReleaseWorkSessionCommand) error {
|
||||
stub.releaseCommand = command
|
||||
return stub.releaseErr
|
||||
}
|
||||
|
||||
const testPageClient = "page-one-AAAAAAAAAAAAAAAA"
|
||||
|
||||
func TestHoldWorkSessionAnswersTheLeaseWithoutTheClientIdentifier(t *testing.T) {
|
||||
userID, attemptID := uuid.New(), uuid.New()
|
||||
until := time.Date(2026, time.October, 11, 9, 2, 0, 0, time.UTC)
|
||||
stub := &workSessionsStub{hold: assessmentusecase.WorkSessionHold{HeldUntil: until, RenewEvery: 15 * time.Second}}
|
||||
handler := NewAssessmentHandler(&assessmentOperationsStub{}).WithWorkSessions(stub)
|
||||
response, err := handler.HoldMyAssessmentWorkSession(principalForTest(userID),
|
||||
api.HoldMyAssessmentWorkSessionParams{AttemptID: attemptID, XAttemptClient: testPageClient})
|
||||
if err != nil {
|
||||
t.Fatalf("HoldMyAssessmentWorkSession() error = %v", err)
|
||||
}
|
||||
headers, ok := response.(*api.AssessmentWorkSessionHeaders)
|
||||
if !ok || headers.CacheControl != api.CacheControlNoStoreNoStore ||
|
||||
!time.Time(headers.Response.HeldUntil).Equal(until) || headers.Response.RenewAfterSeconds != 15 {
|
||||
t.Fatalf("hold response = %T %+v, want a no-store lease that ends at %s", response, response, until)
|
||||
}
|
||||
if stub.holdCommand.ActorID != userID || stub.holdCommand.AttemptID != domain.AssessmentAttemptID(attemptID) || stub.holdCommand.Client != testPageClient {
|
||||
t.Fatalf("hold command = %+v, want the verified principal, the attempt, and the page identifier", stub.holdCommand)
|
||||
}
|
||||
encoded, err := headers.Response.MarshalJSON()
|
||||
if err != nil || strings.Contains(string(encoded), testPageClient) {
|
||||
t.Fatalf("the response %s repeats the page identifier (error %v)", encoded, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHoldAndReleaseWorkSessionMapTheirFailures(t *testing.T) {
|
||||
attemptID := uuid.New()
|
||||
stub := &workSessionsStub{}
|
||||
handler := NewAssessmentHandler(&assessmentOperationsStub{}).WithWorkSessions(stub)
|
||||
hold := func(err error) api.HoldMyAssessmentWorkSessionRes {
|
||||
t.Helper()
|
||||
stub.holdErr = err
|
||||
response, handleErr := handler.HoldMyAssessmentWorkSession(principalForTest(uuid.New()),
|
||||
api.HoldMyAssessmentWorkSessionParams{AttemptID: attemptID, XAttemptClient: testPageClient})
|
||||
if handleErr != nil {
|
||||
t.Fatalf("HoldMyAssessmentWorkSession(%v) error = %v", err, handleErr)
|
||||
}
|
||||
return response
|
||||
}
|
||||
if _, ok := hold(assessmentusecase.ErrAttemptWorkSessionBusy).(*api.HoldMyAssessmentWorkSessionConflict); !ok {
|
||||
t.Fatal("a held session did not answer 409")
|
||||
}
|
||||
if _, ok := hold(assessmentusecase.ErrAttemptWorkSessionState).(*api.HoldMyAssessmentWorkSessionConflict); !ok {
|
||||
t.Fatal("an attempt with no work session did not answer 409")
|
||||
}
|
||||
if _, ok := hold(assessmentusecase.ErrAttemptNotFound).(*api.HoldMyAssessmentWorkSessionNotFound); !ok {
|
||||
t.Fatal("an unknown attempt did not answer 404")
|
||||
}
|
||||
if _, ok := hold(assessmentusecase.ErrAttemptConsentRequired).(*api.HoldMyAssessmentWorkSessionForbidden); !ok {
|
||||
t.Fatal("a missing consent did not answer 403")
|
||||
}
|
||||
if _, ok := hold(assessmentusecase.ErrInvalidWorkSessionClient).(*api.HoldMyAssessmentWorkSessionBadRequest); !ok {
|
||||
t.Fatal("a malformed page identifier did not answer 400")
|
||||
}
|
||||
stub.holdErr = errors.New("private database detail")
|
||||
if _, err := handler.HoldMyAssessmentWorkSession(principalForTest(uuid.New()),
|
||||
api.HoldMyAssessmentWorkSessionParams{AttemptID: attemptID, XAttemptClient: testPageClient}); err == nil || err.Error() != "private database detail" {
|
||||
t.Fatalf("internal error mapping = %v", err)
|
||||
}
|
||||
|
||||
stub.holdErr = nil
|
||||
release, err := handler.ReleaseMyAssessmentWorkSession(principalForTest(uuid.New()),
|
||||
api.ReleaseMyAssessmentWorkSessionParams{AttemptID: attemptID, XAttemptClient: testPageClient})
|
||||
if err != nil {
|
||||
t.Fatalf("ReleaseMyAssessmentWorkSession() error = %v", err)
|
||||
}
|
||||
if noContent, ok := release.(*api.ReleaseMyAssessmentWorkSessionNoContent); !ok || noContent.CacheControl != api.CacheControlNoStoreNoStore {
|
||||
t.Fatalf("release response = %T, want no-store 204", release)
|
||||
}
|
||||
if stub.releaseCommand.Client != testPageClient || stub.releaseCommand.AttemptID != domain.AssessmentAttemptID(attemptID) {
|
||||
t.Fatalf("release command = %+v", stub.releaseCommand)
|
||||
}
|
||||
stub.releaseErr = assessmentusecase.ErrAttemptNotFound
|
||||
missing, err := handler.ReleaseMyAssessmentWorkSession(principalForTest(uuid.New()),
|
||||
api.ReleaseMyAssessmentWorkSessionParams{AttemptID: attemptID, XAttemptClient: testPageClient})
|
||||
if _, ok := missing.(*api.ReleaseMyAssessmentWorkSessionNotFound); !ok || err != nil {
|
||||
t.Fatalf("release of an unknown attempt = %T, %v, want 404", missing, err)
|
||||
}
|
||||
guest, err := handler.HoldMyAssessmentWorkSession(context.Background(), api.HoldMyAssessmentWorkSessionParams{AttemptID: attemptID, XAttemptClient: testPageClient})
|
||||
if _, ok := guest.(*api.HoldMyAssessmentWorkSessionUnauthorized); !ok || err != nil {
|
||||
t.Fatalf("guest hold = %T, %v, want 401", guest, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartSessionAndAnswerPassThePageAndRefuseAHeldAttemptWith409(t *testing.T) {
|
||||
snapshot := assessmentPublicSnapshot()
|
||||
operations := &assessmentOperationsStub{start: outbound.AssessmentAttemptSessionResult{Response: snapshot}}
|
||||
handler := NewAssessmentHandler(operations)
|
||||
ctx := principalForTest(uuid.New())
|
||||
params := api.StartMyAssessmentAttemptSessionParams{
|
||||
AttemptID: snapshot.ID.UUID(), IdempotencyKey: "session-key", XAttemptClient: api.NewOptString(testPageClient),
|
||||
}
|
||||
if _, err := handler.StartMyAssessmentAttemptSession(ctx, &api.AssessmentAttemptSessionStart{ExpectedVersion: 7}, params); err != nil {
|
||||
t.Fatalf("StartMyAssessmentAttemptSession() error = %v", err)
|
||||
}
|
||||
if operations.startCommand.Client != testPageClient {
|
||||
t.Fatalf("StartSession() client = %q, want the page identifier", operations.startCommand.Client)
|
||||
}
|
||||
operations.startErr = assessmentusecase.ErrAttemptWorkSessionBusy
|
||||
response, err := handler.StartMyAssessmentAttemptSession(ctx, &api.AssessmentAttemptSessionStart{ExpectedVersion: 7}, params)
|
||||
if err != nil {
|
||||
t.Fatalf("StartMyAssessmentAttemptSession(held) error = %v", err)
|
||||
}
|
||||
conflict, ok := response.(*api.StartMyAssessmentAttemptSessionConflict)
|
||||
if !ok || api.Problem(*conflict).Code != api.ProblemCodeStateConflict {
|
||||
t.Fatalf("held attempt response = %T, want 409 state_conflict", response)
|
||||
}
|
||||
// Without the header the request is one anonymous page.
|
||||
params.XAttemptClient = api.OptString{}
|
||||
operations.startErr = nil
|
||||
if _, err := handler.StartMyAssessmentAttemptSession(ctx, &api.AssessmentAttemptSessionStart{ExpectedVersion: 7}, params); err != nil || operations.startCommand.Client != "" {
|
||||
t.Fatalf("StartSession() without a header: client %q, error %v, want an empty client", operations.startCommand.Client, err)
|
||||
}
|
||||
|
||||
if _, ok := assessmentAnswerFailureOf(t, assessmentusecase.ErrAttemptWorkSessionBusy).(*api.SubmitMyAssessmentAnswerConflict); !ok {
|
||||
t.Fatal("a held attempt did not answer 409 to an answer")
|
||||
}
|
||||
}
|
||||
|
||||
func assessmentAnswerFailureOf(t *testing.T, err error) api.SubmitMyAssessmentAnswerRes {
|
||||
t.Helper()
|
||||
response, mapped := assessmentAnswerFailure(context.Background(), err)
|
||||
if mapped != nil {
|
||||
t.Fatalf("assessmentAnswerFailure(%v) error = %v", err, mapped)
|
||||
}
|
||||
return response
|
||||
}
|
||||
Reference in new issue
Block a user