456 lines
22 KiB
Go
456 lines
22 KiB
Go
package v1
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/adapter/primary/http/api"
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
assessmentusecase "lincodex/backend/internal/usecase/assessment"
|
|
)
|
|
|
|
func TestCreateAssessmentAttemptMapsRootAndPublicSnapshot(t *testing.T) {
|
|
userID := uuid.New()
|
|
snapshot := assessmentPublicSnapshot()
|
|
operations := &assessmentOperationsStub{create: outbound.AssessmentAttemptCreateResult{
|
|
Outcome: outbound.AssessmentAttemptCreateOutcome{StatusCode: 201, Response: snapshot},
|
|
}}
|
|
handler := NewAssessmentHandler(operations)
|
|
request := api.NewAssessmentQualifiedAttemptCreateAssessmentAttemptCreate(api.AssessmentQualifiedAttemptCreate{
|
|
Kind: api.AssessmentQualifiedAttemptCreateKindRoot, Specialization: "backend", TargetGrade: "junior",
|
|
TaskLanguage: api.NewNilTaskLanguage(api.TaskLanguage("go")),
|
|
})
|
|
response, err := handler.CreateMyAssessmentAttempt(principalForTest(userID), request, api.CreateMyAssessmentAttemptParams{IdempotencyKey: "create-key"})
|
|
if err != nil {
|
|
t.Fatalf("CreateMyAssessmentAttempt() error = %v", err)
|
|
}
|
|
if operations.createCalls != 1 || operations.createCommand.ActorID != userID || operations.createCommand.Kind != domain.AssessmentAttemptKindRoot ||
|
|
operations.createCommand.IdempotencyKey != "create-key" || operations.createCommand.Specialization == nil ||
|
|
*operations.createCommand.Specialization != "backend" || operations.createCommand.TargetGrade == nil ||
|
|
*operations.createCommand.TargetGrade != "junior" || operations.createCommand.TaskLanguage == nil ||
|
|
*operations.createCommand.TaskLanguage != "go" {
|
|
t.Fatalf("CreateAttempt() command = %+v, calls %d", operations.createCommand, operations.createCalls)
|
|
}
|
|
headers, ok := response.(*api.AssessmentAttemptHeaders)
|
|
if !ok || headers.CacheControl != api.CacheControlNoStoreNoStore {
|
|
t.Fatalf("create response = %T %+v, want no-store headers", response, response)
|
|
}
|
|
qualified, ok := headers.Response.GetAssessmentQualifiedAttempt()
|
|
if !ok || qualified.ID != snapshot.ID.UUID() || qualified.Version != api.AssessmentAttemptVersion(snapshot.Version) ||
|
|
qualified.CurrentItem.IsNull() || qualified.Progress.MaxItems != snapshot.MaxItems ||
|
|
qualified.Versions.AlgorithmVersion != api.AssessmentVersionLabel(snapshot.AlgorithmVersion) {
|
|
t.Fatalf("qualified attempt response = %+v, %v", qualified, ok)
|
|
}
|
|
item, ok := qualified.CurrentItem.Get()
|
|
if !ok || item.Prompt != "Choose the number" || !item.Content.IsNumberItemContent() {
|
|
t.Fatalf("current item = %+v, %v", item, ok)
|
|
}
|
|
}
|
|
|
|
func TestCreateAssessmentAttemptMapsPersistedConflictAndRejectsGuest(t *testing.T) {
|
|
operations := &assessmentOperationsStub{create: outbound.AssessmentAttemptCreateResult{
|
|
Outcome: outbound.AssessmentAttemptCreateOutcome{StatusCode: 409, ProblemCode: "state_conflict"},
|
|
}}
|
|
handler := NewAssessmentHandler(operations)
|
|
request := api.NewAssessmentExpressAttemptCreateAssessmentAttemptCreate(api.AssessmentExpressAttemptCreate{
|
|
Kind: api.AssessmentExpressAttemptCreateKindDemoExpress,
|
|
})
|
|
response, err := handler.CreateMyAssessmentAttempt(principalForTest(uuid.New()), request, api.CreateMyAssessmentAttemptParams{IdempotencyKey: "blocked"})
|
|
if err != nil {
|
|
t.Fatalf("CreateMyAssessmentAttempt() error = %v", err)
|
|
}
|
|
if _, ok := response.(*api.CreateMyAssessmentAttemptConflict); !ok {
|
|
t.Fatalf("state conflict response = %T, want conflict", response)
|
|
}
|
|
if operations.createCommand.Kind != domain.AssessmentAttemptKindDemoExpress || operations.createCommand.Specialization != nil {
|
|
t.Fatalf("express command = %+v", operations.createCommand)
|
|
}
|
|
response, err = handler.CreateMyAssessmentAttempt(context.Background(), request, api.CreateMyAssessmentAttemptParams{IdempotencyKey: "blocked"})
|
|
if err != nil {
|
|
t.Fatalf("guest CreateMyAssessmentAttempt() error = %v", err)
|
|
}
|
|
if _, ok := response.(*api.CreateMyAssessmentAttemptUnauthorized); !ok || operations.createCalls != 1 {
|
|
t.Fatalf("guest create response = %T, operations called %d", response, operations.createCalls)
|
|
}
|
|
}
|
|
|
|
func TestStartAndReadAssessmentAttemptUseVerifiedPrincipal(t *testing.T) {
|
|
userID := uuid.New()
|
|
snapshot := assessmentPublicSnapshot()
|
|
operations := &assessmentOperationsStub{
|
|
read: snapshot,
|
|
start: outbound.AssessmentAttemptSessionResult{Response: snapshot},
|
|
}
|
|
handler := NewAssessmentHandler(operations)
|
|
ctx := principalForTest(userID)
|
|
read, err := handler.GetMyAssessmentAttempt(ctx, api.GetMyAssessmentAttemptParams{AttemptID: snapshot.ID.UUID()})
|
|
if err != nil {
|
|
t.Fatalf("GetMyAssessmentAttempt() error = %v", err)
|
|
}
|
|
if operations.readUserID != userID || operations.readAttemptID != snapshot.ID {
|
|
t.Fatalf("GetMyAttempt() IDs = %s/%s", operations.readUserID, operations.readAttemptID)
|
|
}
|
|
if _, ok := read.(*api.AssessmentAttemptHeaders); !ok {
|
|
t.Fatalf("read response = %T, want attempt headers", read)
|
|
}
|
|
started, err := handler.StartMyAssessmentAttemptSession(ctx, &api.AssessmentAttemptSessionStart{
|
|
ExpectedVersion: api.AssessmentAttemptVersion(7),
|
|
}, api.StartMyAssessmentAttemptSessionParams{AttemptID: snapshot.ID.UUID(), IdempotencyKey: "session-key"})
|
|
if err != nil {
|
|
t.Fatalf("StartMyAssessmentAttemptSession() error = %v", err)
|
|
}
|
|
if operations.startCommand.ActorID != userID || operations.startCommand.AttemptID != snapshot.ID ||
|
|
operations.startCommand.ExpectedVersion != 7 || operations.startCommand.IdempotencyKey != "session-key" {
|
|
t.Fatalf("StartSession() command = %+v", operations.startCommand)
|
|
}
|
|
if _, ok := started.(*api.AssessmentAttemptHeaders); !ok {
|
|
t.Fatalf("start response = %T, want attempt headers", started)
|
|
}
|
|
}
|
|
|
|
func TestAssessmentAttemptFailuresMapWithoutExposingInternalErrors(t *testing.T) {
|
|
operations := &assessmentOperationsStub{startErr: assessmentusecase.ErrAttemptVersionConflict}
|
|
handler := NewAssessmentHandler(operations)
|
|
response, err := handler.StartMyAssessmentAttemptSession(principalForTest(uuid.New()),
|
|
&api.AssessmentAttemptSessionStart{ExpectedVersion: 3},
|
|
api.StartMyAssessmentAttemptSessionParams{AttemptID: uuid.New(), IdempotencyKey: "key"})
|
|
if err != nil {
|
|
t.Fatalf("StartMyAssessmentAttemptSession() error = %v", err)
|
|
}
|
|
problem, ok := response.(*api.StartMyAssessmentAttemptSessionPreconditionFailed)
|
|
if !ok || api.Problem(*problem).Code != api.ProblemCodeVersionConflict {
|
|
t.Fatalf("version conflict response = %T %+v", response, response)
|
|
}
|
|
|
|
operations.readErr = assessmentusecase.ErrAttemptNotFound
|
|
read, err := handler.GetMyAssessmentAttempt(principalForTest(uuid.New()), api.GetMyAssessmentAttemptParams{AttemptID: uuid.New()})
|
|
if err != nil {
|
|
t.Fatalf("GetMyAssessmentAttempt() error = %v", err)
|
|
}
|
|
if _, ok := read.(*api.GetMyAssessmentAttemptNotFound); !ok {
|
|
t.Fatalf("missing attempt response = %T", read)
|
|
}
|
|
|
|
operations.createErr = &assessmentusecase.AttemptFieldError{Pointer: "/task_language", Reason: "is not available"}
|
|
create, err := handler.CreateMyAssessmentAttempt(principalForTest(uuid.New()), api.NewAssessmentQualifiedAttemptCreateAssessmentAttemptCreate(
|
|
api.AssessmentQualifiedAttemptCreate{Kind: api.AssessmentQualifiedAttemptCreateKindRoot, Specialization: "backend", TargetGrade: "junior"},
|
|
), api.CreateMyAssessmentAttemptParams{IdempotencyKey: "key"})
|
|
if err != nil {
|
|
t.Fatalf("CreateMyAssessmentAttempt() error = %v", err)
|
|
}
|
|
badRequest, ok := create.(*api.CreateMyAssessmentAttemptBadRequest)
|
|
if !ok || len(api.Problem(*badRequest).Errors) != 1 || api.Problem(*badRequest).Errors[0].Pointer != "/task_language" {
|
|
t.Fatalf("validation response = %T %+v", create, create)
|
|
}
|
|
|
|
operations.startErr = errors.New("private database detail")
|
|
if _, err := handler.StartMyAssessmentAttemptSession(principalForTest(uuid.New()),
|
|
&api.AssessmentAttemptSessionStart{ExpectedVersion: 1},
|
|
api.StartMyAssessmentAttemptSessionParams{AttemptID: uuid.New(), IdempotencyKey: "key"}); err == nil || err.Error() != "private database detail" {
|
|
t.Fatalf("unexpected internal error mapping = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestConfirmedAttemptResultDoesNotInferStoredGradeFromTarget(t *testing.T) {
|
|
result := assessmentusecase.AssessmentAttemptResult{
|
|
Attempt: domain.AssessmentAttemptPublicSnapshot{
|
|
Kind: domain.AssessmentAttemptKindRoot, TargetGrade: domainCodePointer(domain.GradeCode("junior")),
|
|
},
|
|
Outcome: domain.AssessmentAttemptOutcomeConfirmed,
|
|
}
|
|
fields, err := assessmentRealResultFields(result)
|
|
if err != nil {
|
|
t.Fatalf("assessmentRealResultFields() error = %v", err)
|
|
}
|
|
if !fields.confirmedGrade.IsNull() {
|
|
t.Fatalf("confirmed grade = %+v, want null until a stored grade decision exists", fields.confirmedGrade)
|
|
}
|
|
}
|
|
|
|
type assessmentOperationsStub struct {
|
|
availabilityCalls int
|
|
availabilityUserID uuid.UUID
|
|
availability assessmentusecase.AssessmentAvailability
|
|
availabilityErr error
|
|
createCalls int
|
|
createCommand assessmentusecase.CreateAttemptCommand
|
|
create outbound.AssessmentAttemptCreateResult
|
|
createErr error
|
|
read domain.AssessmentAttemptPublicSnapshot
|
|
readUserID uuid.UUID
|
|
readAttemptID domain.AssessmentAttemptID
|
|
readErr error
|
|
startCommand assessmentusecase.StartAttemptSessionCommand
|
|
start outbound.AssessmentAttemptSessionResult
|
|
startErr error
|
|
}
|
|
|
|
func (stub *assessmentOperationsStub) GetMyAssessmentAvailability(
|
|
_ context.Context,
|
|
userID uuid.UUID,
|
|
) (assessmentusecase.AssessmentAvailability, error) {
|
|
stub.availabilityCalls++
|
|
stub.availabilityUserID = userID
|
|
return stub.availability, stub.availabilityErr
|
|
}
|
|
|
|
func (stub *assessmentOperationsStub) CreateAttempt(_ context.Context, command assessmentusecase.CreateAttemptCommand) (outbound.AssessmentAttemptCreateResult, error) {
|
|
stub.createCalls++
|
|
stub.createCommand = command
|
|
return stub.create, stub.createErr
|
|
}
|
|
|
|
func (stub *assessmentOperationsStub) GetMyAttempt(_ context.Context, userID uuid.UUID, attemptID domain.AssessmentAttemptID) (domain.AssessmentAttemptPublicSnapshot, error) {
|
|
stub.readUserID, stub.readAttemptID = userID, attemptID
|
|
return stub.read, stub.readErr
|
|
}
|
|
|
|
func (stub *assessmentOperationsStub) StartSession(_ context.Context, command assessmentusecase.StartAttemptSessionCommand) (outbound.AssessmentAttemptSessionResult, error) {
|
|
stub.startCommand = command
|
|
return stub.start, stub.startErr
|
|
}
|
|
|
|
func principalForTest(userID uuid.UUID) context.Context {
|
|
return context.WithValue(context.Background(), principalContextKey{}, Principal{UserID: userID, EmailVerified: true})
|
|
}
|
|
|
|
func assessmentPublicSnapshot() domain.AssessmentAttemptPublicSnapshot {
|
|
started := time.Date(2026, time.October, 7, 12, 0, 0, 0, time.UTC)
|
|
return domain.AssessmentAttemptPublicSnapshot{
|
|
ID: domain.AssessmentAttemptID(uuid.New()), Version: 7, Kind: domain.AssessmentAttemptKindRoot,
|
|
Mode: domain.AssessmentAttemptModeDemoBasic, State: domain.AssessmentAttemptStateInProgress,
|
|
Specialization: domainCodePointer(domain.SpecializationCode("backend")), TargetGrade: domainCodePointer(domain.GradeCode("junior")),
|
|
PolicyVersion: 2, BankVersion: "bank-v1", AlgorithmVersion: "assessment.v1", StartedAt: started,
|
|
ExpiresAt: started.Add(30 * time.Minute), MaxItems: 10, SessionsUsed: 1, MaxSessions: 2, IdleTimeout: 1800,
|
|
CurrentItem: &domain.AssessmentAttemptPublicItem{ID: uuid.New(), Sequence: 1, Prompt: "Choose the number",
|
|
Content: domain.AssessmentAttemptPublicItemContent{Format: "number"}}, Practices: []domain.AssessmentAttemptPublicPractice{},
|
|
}
|
|
}
|
|
|
|
func domainCodePointer[T ~string](value T) *T {
|
|
return &value
|
|
}
|
|
|
|
type presenterStub struct{ err error }
|
|
|
|
func (stub presenterStub) Present(snapshot domain.AssessmentAttemptPublicSnapshot) (domain.AssessmentAttemptPublicSnapshot, error) {
|
|
if stub.err != nil {
|
|
return domain.AssessmentAttemptPublicSnapshot{}, stub.err
|
|
}
|
|
snapshot.Watermark = &domain.AssessmentAttemptPublicWatermark{Code: "7K3QD-9XM2T"}
|
|
item := *snapshot.CurrentItem
|
|
item.Prompt += "\u200b"
|
|
snapshot.CurrentItem = &item
|
|
return snapshot, nil
|
|
}
|
|
|
|
func TestEveryAttemptResponsePassesThroughThePresenter(t *testing.T) {
|
|
snapshot := assessmentPublicSnapshot()
|
|
operations := &assessmentOperationsStub{read: snapshot, start: outbound.AssessmentAttemptSessionResult{Response: snapshot}}
|
|
handler := NewAssessmentHandler(operations).WithPresenter(presenterStub{})
|
|
ctx := principalForTest(uuid.New())
|
|
|
|
read, err := handler.GetMyAssessmentAttempt(ctx, api.GetMyAssessmentAttemptParams{AttemptID: snapshot.ID.UUID()})
|
|
if err != nil {
|
|
t.Fatalf("GetMyAssessmentAttempt() error = %v", err)
|
|
}
|
|
started, err := handler.StartMyAssessmentAttemptSession(ctx, &api.AssessmentAttemptSessionStart{ExpectedVersion: 7},
|
|
api.StartMyAssessmentAttemptSessionParams{AttemptID: snapshot.ID.UUID(), IdempotencyKey: "session-key"})
|
|
if err != nil {
|
|
t.Fatalf("StartMyAssessmentAttemptSession() error = %v", err)
|
|
}
|
|
for name, response := range map[string]any{"read": read, "start": started} {
|
|
headers, ok := response.(*api.AssessmentAttemptHeaders)
|
|
if !ok {
|
|
t.Fatalf("%s response = %T, want attempt headers", name, response)
|
|
}
|
|
qualified, ok := headers.Response.GetAssessmentQualifiedAttempt()
|
|
if !ok {
|
|
t.Fatalf("%s response is not a qualified attempt", name)
|
|
}
|
|
code, hasCode := qualified.Watermark.Get()
|
|
item, _ := qualified.CurrentItem.Get()
|
|
if !hasCode || code.Code != "7K3QD-9XM2T" || item.Prompt != "Choose the number\u200b" {
|
|
t.Fatalf("%s response watermark = %+v/%v and prompt %q, want the presented values", name, code, hasCode, item.Prompt)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAnAttemptWithoutAPresenterCarriesNoWatermark(t *testing.T) {
|
|
snapshot := assessmentPublicSnapshot()
|
|
handler := NewAssessmentHandler(&assessmentOperationsStub{read: snapshot})
|
|
read, err := handler.GetMyAssessmentAttempt(principalForTest(uuid.New()), api.GetMyAssessmentAttemptParams{AttemptID: snapshot.ID.UUID()})
|
|
if err != nil {
|
|
t.Fatalf("GetMyAssessmentAttempt() error = %v", err)
|
|
}
|
|
headers, ok := read.(*api.AssessmentAttemptHeaders)
|
|
if !ok {
|
|
t.Fatalf("read response = %T, want attempt headers", read)
|
|
}
|
|
qualified, _ := headers.Response.GetAssessmentQualifiedAttempt()
|
|
if qualified.Watermark.IsSet() {
|
|
t.Fatal("an attempt served without a presenter carries a watermark")
|
|
}
|
|
}
|
|
|
|
func TestAPresenterFailureIsAServerErrorNotAnUnmarkedAttempt(t *testing.T) {
|
|
snapshot := assessmentPublicSnapshot()
|
|
handler := NewAssessmentHandler(&assessmentOperationsStub{read: snapshot}).WithPresenter(presenterStub{err: errors.New("seed unavailable")})
|
|
if _, err := handler.GetMyAssessmentAttempt(principalForTest(uuid.New()), api.GetMyAssessmentAttemptParams{AttemptID: snapshot.ID.UUID()}); err == nil {
|
|
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
|
|
}
|