268 lines
13 KiB
Go
268 lines
13 KiB
Go
package invitation
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/google/uuid"
|
||
|
||
"lincodex/backend/internal/domain"
|
||
"lincodex/backend/internal/port/outbound"
|
||
)
|
||
|
||
func (repository testInvitations) GetCandidateEntry(ctx context.Context, candidateID, invitationID uuid.UUID) (outbound.CandidateInvitationEntry, error) {
|
||
invitation, err := repository.GetForCandidate(ctx, candidateID, invitationID)
|
||
if err != nil {
|
||
return outbound.CandidateInvitationEntry{}, err
|
||
}
|
||
return outbound.CandidateInvitationEntry{Invitation: invitation, OrganizationName: "Test organization"}, nil
|
||
}
|
||
|
||
func (repository testInvitations) GetCandidateEntryForUpdate(ctx context.Context, candidateID, invitationID uuid.UUID) (outbound.CandidateInvitationEntry, error) {
|
||
return repository.GetCandidateEntry(ctx, candidateID, invitationID)
|
||
}
|
||
|
||
func (repository testInvitations) RecordView(_ context.Context, expectedVersion int64, next domain.Invitation, event domain.InvitationEvent) error {
|
||
current, ok := repository.state.invitations[next.ID()]
|
||
if !ok || current.Version() != expectedVersion || current.State() != domain.InvitationDelivered {
|
||
return outbound.ErrInvitationVersionConflict
|
||
}
|
||
repository.state.invitations[next.ID()] = next
|
||
repository.state.events = append(repository.state.events, event)
|
||
return nil
|
||
}
|
||
|
||
func (repository testInvitations) Decline(_ context.Context, expectedVersion int64, next domain.Invitation, decline domain.InvitationDecline, event domain.InvitationEvent) error {
|
||
current, ok := repository.state.invitations[next.ID()]
|
||
if !ok || current.Version() != expectedVersion {
|
||
return outbound.ErrInvitationVersionConflict
|
||
}
|
||
repository.state.invitations[next.ID()] = next
|
||
repository.state.declines[next.ID()] = decline
|
||
repository.state.events = append(repository.state.events, event)
|
||
return nil
|
||
}
|
||
|
||
func (repository testInvitations) GetDecline(_ context.Context, organizationID, invitationID uuid.UUID) (domain.InvitationDecline, error) {
|
||
decline, ok := repository.state.declines[invitationID]
|
||
if !ok || decline.OrganizationID != organizationID {
|
||
return domain.InvitationDecline{}, outbound.ErrInvitationDeclineNotFound
|
||
}
|
||
return decline, nil
|
||
}
|
||
|
||
func (repository testInvitations) SaveDeclineResult(_ context.Context, scope [outbound.IdempotencyDigestSize]byte, result outbound.InvitationDeclineResult) error {
|
||
repository.state.declineResults[scope] = result
|
||
return nil
|
||
}
|
||
|
||
func (repository testInvitations) GetDeclineResult(_ context.Context, scope [outbound.IdempotencyDigestSize]byte) (outbound.InvitationDeclineResult, error) {
|
||
result, ok := repository.state.declineResults[scope]
|
||
if !ok {
|
||
return outbound.InvitationDeclineResult{}, outbound.ErrInvitationDeclineResultNotFound
|
||
}
|
||
return result, nil
|
||
}
|
||
|
||
// deliveredTo sends a new draft to a new candidate who can sign in and
|
||
// returns the delivered invitation with the candidate.
|
||
func (test *harness) deliveredTo(t *testing.T) (domain.OrganizationInvitationView, uuid.UUID) {
|
||
t.Helper()
|
||
draft, _, candidateID := test.draftFor(t, nil)
|
||
sent, err := test.service.Send(t.Context(), test.sendCommand(draft.ID, draft.Version, "send-"+draft.ID.String()))
|
||
if err != nil {
|
||
t.Fatalf("Send() error = %v", err)
|
||
}
|
||
verified := time.Date(2026, time.October, 9, 9, 0, 0, 0, time.UTC)
|
||
test.transactor.mu.Lock()
|
||
test.transactor.state.users[candidateID] = domain.User{ID: candidateID, Scenario: domain.ScenarioCandidate, EmailVerifiedAt: &verified}
|
||
test.transactor.mu.Unlock()
|
||
return sent, candidateID
|
||
}
|
||
|
||
func declineCommand(actorID, invitationID uuid.UUID, version int64, key string, reason domain.InvitationDeclineReason, comment *string) DeclineCommand {
|
||
return DeclineCommand{
|
||
ActorID: actorID, InvitationID: invitationID, IdempotencyKey: key, ExpectedVersion: version,
|
||
Choice: domain.InvitationDeclineChoice{Reason: reason, Comment: comment},
|
||
}
|
||
}
|
||
|
||
func TestGetMineHasNoSideEffectAndViewMineKeepsTheDeadline(t *testing.T) {
|
||
test := newHarness(t)
|
||
sent, candidateID := test.deliveredTo(t)
|
||
events := len(test.transactor.snapshot().events)
|
||
|
||
read, err := test.service.GetMine(t.Context(), candidateID, sent.ID)
|
||
if err != nil {
|
||
t.Fatalf("GetMine() error = %v", err)
|
||
}
|
||
if read.State != domain.InvitationDelivered || read.Version != sent.Version || read.ViewedAt != nil || read.Decline != nil ||
|
||
read.Organization.ID != test.organizationID || read.Organization.Name == "" || read.Terms.RoleTitle == "" {
|
||
t.Fatalf("read = %+v, want the delivered invitation unchanged", read)
|
||
}
|
||
if len(test.transactor.snapshot().events) != events {
|
||
t.Fatal("a read recorded an event")
|
||
}
|
||
|
||
viewedAt := sent.DeliveredAt.Add(time.Hour)
|
||
test.setTime(viewedAt)
|
||
viewed, err := test.service.ViewMine(t.Context(), candidateID, sent.ID)
|
||
if err != nil {
|
||
t.Fatalf("ViewMine() error = %v", err)
|
||
}
|
||
if viewed.State != domain.InvitationViewed || viewed.Version != sent.Version+1 || viewed.TermsVersion != sent.TermsVersion ||
|
||
viewed.ViewedAt == nil || !viewed.ViewedAt.Equal(viewedAt) || !viewed.ResponseDeadlineAt.Equal(*sent.ResponseDeadlineAt) {
|
||
t.Fatalf("viewed = %+v, want viewed now with the deadline of the delivery", viewed)
|
||
}
|
||
state := test.transactor.snapshot()
|
||
last := state.events[len(state.events)-1]
|
||
if len(state.events) != events+1 || last.Kind != domain.InvitationEventViewed || last.ActorID != candidateID || last.Version != viewed.Version {
|
||
t.Fatalf("events = %+v, want one view event by the candidate", state.events[events:])
|
||
}
|
||
|
||
// A repeat changes nothing and records nothing.
|
||
test.setTime(viewedAt.Add(time.Hour))
|
||
again, err := test.service.ViewMine(t.Context(), candidateID, sent.ID)
|
||
if err != nil || again.Version != viewed.Version || !again.ViewedAt.Equal(viewedAt) {
|
||
t.Fatalf("repeated view = %+v, %v; want the first view", again, err)
|
||
}
|
||
if len(test.transactor.snapshot().events) != events+1 {
|
||
t.Fatal("a repeated view recorded an event")
|
||
}
|
||
}
|
||
|
||
func TestViewMineAtTheDeadlineChangesNothing(t *testing.T) {
|
||
test := newHarness(t)
|
||
sent, candidateID := test.deliveredTo(t)
|
||
test.setTime(*sent.ResponseDeadlineAt)
|
||
view, err := test.service.ViewMine(t.Context(), candidateID, sent.ID)
|
||
if err != nil || view.State != domain.InvitationDelivered || view.Version != sent.Version || view.ViewedAt != nil {
|
||
t.Fatalf("late view = %+v, %v; want the delivered invitation unchanged", view, err)
|
||
}
|
||
}
|
||
|
||
func TestDeclineMineWithOtherAndNoCommentIsAStoredAnswer(t *testing.T) {
|
||
test := newHarness(t)
|
||
sent, candidateID := test.deliveredTo(t)
|
||
notified := len(test.transactor.snapshot().notified)
|
||
declinedAt := sent.DeliveredAt.Add(2 * time.Hour)
|
||
test.setTime(declinedAt)
|
||
command := declineCommand(candidateID, sent.ID, sent.Version, "decline-1", domain.InvitationDeclineOther, nil)
|
||
|
||
declined, err := test.service.DeclineMine(t.Context(), command)
|
||
if err != nil {
|
||
t.Fatalf("DeclineMine() error = %v", err)
|
||
}
|
||
if declined.State != domain.InvitationDeclined || declined.Version != sent.Version+1 || declined.RespondedAt == nil ||
|
||
!declined.RespondedAt.Equal(declinedAt) || declined.Decline == nil || declined.Decline.Reason != domain.InvitationDeclineOther ||
|
||
declined.Decline.Comment != nil || !declined.Decline.DeclinedAt.Equal(declinedAt) ||
|
||
!declined.ResponseDeadlineAt.Equal(*sent.ResponseDeadlineAt) {
|
||
t.Fatalf("declined = %+v, want declined now with the reason other and no comment", declined)
|
||
}
|
||
state := test.transactor.snapshot()
|
||
last := state.events[len(state.events)-1]
|
||
if last.Kind != domain.InvitationEventDeclined || last.ActorID != candidateID || last.Version != declined.Version ||
|
||
len(last.ChangedFields) != 0 || len(state.notified) != notified {
|
||
t.Fatalf("event %+v and %d notifications, want one decline event and no notification", last, len(state.notified)-notified)
|
||
}
|
||
|
||
// The same key replays, and the same decline under a new key with the
|
||
// older version answers with the declined invitation.
|
||
test.setTime(declinedAt.Add(time.Hour))
|
||
for _, repeat := range []DeclineCommand{command, declineCommand(candidateID, sent.ID, sent.Version, "decline-2", domain.InvitationDeclineOther, nil)} {
|
||
again, err := test.service.DeclineMine(t.Context(), repeat)
|
||
if err != nil || again.Version != declined.Version || !again.Decline.DeclinedAt.Equal(declinedAt) {
|
||
t.Fatalf("repeat %s = %+v, %v; want the original decline", repeat.IdempotencyKey, again, err)
|
||
}
|
||
}
|
||
if events := len(test.transactor.snapshot().events); events != len(state.events) {
|
||
t.Fatalf("repeats recorded %d events", events-len(state.events))
|
||
}
|
||
comment := "Не подходит стек"
|
||
if _, err := test.service.DeclineMine(t.Context(), declineCommand(candidateID, sent.ID, declined.Version, "decline-3", domain.InvitationDeclineOther, &comment)); !errors.Is(err, ErrAnswerBlocked) {
|
||
t.Fatalf("another comment on a declined invitation error = %v, want ErrAnswerBlocked", err)
|
||
}
|
||
if _, err := test.service.DeclineMine(t.Context(), declineCommand(candidateID, sent.ID, sent.Version, "decline-1", domain.InvitationDeclineSalary, nil)); !errors.Is(err, ErrIdempotencyKeyReused) {
|
||
t.Fatalf("the same key with another reason error = %v, want ErrIdempotencyKeyReused", err)
|
||
}
|
||
}
|
||
|
||
func TestDeclineMineFollowsTheOrderOfChecks(t *testing.T) {
|
||
test := newHarness(t)
|
||
sent, candidateID := test.deliveredTo(t)
|
||
|
||
// A stale version answers 412 and is not stored under the key.
|
||
if _, err := test.service.DeclineMine(t.Context(), declineCommand(candidateID, sent.ID, sent.Version+1, "stale", domain.InvitationDeclineStack, nil)); !errors.Is(err, domain.ErrInvitationVersionConflict) {
|
||
t.Fatalf("stale decline error = %v, want ErrInvitationVersionConflict", err)
|
||
}
|
||
if state := test.transactor.snapshot(); len(state.declineResults) != 0 || state.invitations[sent.ID].State() != domain.InvitationDelivered {
|
||
t.Fatal("a stale decline stored an outcome or changed the invitation")
|
||
}
|
||
|
||
// At the deadline a decline is late whatever the version, and the
|
||
// refusal is stored.
|
||
test.setTime(*sent.ResponseDeadlineAt)
|
||
late := declineCommand(candidateID, sent.ID, sent.Version+5, "late", domain.InvitationDeclineStack, nil)
|
||
for range 2 {
|
||
if _, err := test.service.DeclineMine(t.Context(), late); !errors.Is(err, ErrAnswerBlocked) {
|
||
t.Fatalf("late decline error = %v, want ErrAnswerBlocked", err)
|
||
}
|
||
}
|
||
if state := test.transactor.snapshot(); len(state.declineResults) != 1 || state.invitations[sent.ID].State() != domain.InvitationDelivered {
|
||
t.Fatal("a late decline was not stored once or changed the invitation")
|
||
}
|
||
|
||
// An unknown identifier is not found and stored as well.
|
||
unknown := declineCommand(candidateID, uuid.New(), 1, "unknown", domain.InvitationDeclineOther, nil)
|
||
for range 2 {
|
||
if _, err := test.service.DeclineMine(t.Context(), unknown); !errors.Is(err, outbound.ErrInvitationNotFound) {
|
||
t.Fatalf("unknown decline error = %v, want ErrInvitationNotFound", err)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestCandidateCommandsShowNothingOfAnotherCandidatesInvitation(t *testing.T) {
|
||
test := newHarness(t)
|
||
sent, _ := test.deliveredTo(t)
|
||
_, stranger := test.deliveredTo(t)
|
||
draft, _, _ := test.draftFor(t, nil)
|
||
for name, invitationID := range map[string]uuid.UUID{"of another candidate": sent.ID, "draft": draft.ID, "unknown": uuid.New()} {
|
||
if _, err := test.service.GetMine(t.Context(), stranger, invitationID); !errors.Is(err, outbound.ErrInvitationNotFound) {
|
||
t.Errorf("GetMine(%s) error = %v, want ErrInvitationNotFound", name, err)
|
||
}
|
||
if _, err := test.service.ViewMine(t.Context(), stranger, invitationID); !errors.Is(err, outbound.ErrInvitationNotFound) {
|
||
t.Errorf("ViewMine(%s) error = %v, want ErrInvitationNotFound", name, err)
|
||
}
|
||
if _, err := test.service.DeclineMine(t.Context(), declineCommand(stranger, invitationID, 2, "foreign-"+name, domain.InvitationDeclineOther, nil)); !errors.Is(err, outbound.ErrInvitationNotFound) {
|
||
t.Errorf("DeclineMine(%s) error = %v, want ErrInvitationNotFound", name, err)
|
||
}
|
||
}
|
||
// The employer that sent it has no candidate inbox.
|
||
if _, err := test.service.GetMine(t.Context(), test.actorID, sent.ID); !errors.Is(err, ErrForbidden) {
|
||
t.Fatalf("GetMine(employer) error = %v, want ErrForbidden", err)
|
||
}
|
||
if _, err := test.service.DeclineMine(t.Context(), declineCommand(test.actorID, sent.ID, sent.Version, "employer", domain.InvitationDeclineOther, nil)); !errors.Is(err, ErrForbidden) {
|
||
t.Fatalf("DeclineMine(employer) error = %v, want ErrForbidden", err)
|
||
}
|
||
if state := test.transactor.snapshot(); state.invitations[sent.ID].State() != domain.InvitationDelivered {
|
||
t.Fatal("a foreign caller changed the invitation")
|
||
}
|
||
}
|
||
|
||
func TestDeclineMineRefusesAnUnstorableCommentBeforeAnyRead(t *testing.T) {
|
||
test := newHarness(t)
|
||
sent, candidateID := test.deliveredTo(t)
|
||
for name, comment := range map[string]string{"only non-breaking spaces": " ", "NUL": "ok\x00", "too long": strings.Repeat("я", 501)} {
|
||
_, err := test.service.DeclineMine(t.Context(), declineCommand(candidateID, sent.ID, sent.Version, "bad-"+name, domain.InvitationDeclineOther, &comment))
|
||
if pointer := fieldPointer(t, err); pointer != "/comment" {
|
||
t.Errorf("%s: pointer = %s, want /comment", name, pointer)
|
||
}
|
||
}
|
||
if state := test.transactor.snapshot(); len(state.declineResults) != 0 || len(state.declines) != 0 {
|
||
t.Fatal("an invalid comment stored an outcome or a decline")
|
||
}
|
||
}
|