feat(evidence): project skill evidence from confirmed assessments
This commit is contained in:
1 parent
4761508f13
commit
c2a8d17195
94 files changed
+10077
-151
No files matched your search
@@ -0,0 +1,169 @@
|
||||
package candidateview
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
// categoriesProjector projects the one test source with the given categories.
|
||||
type categoriesProjector struct {
|
||||
contactAccessProjector
|
||||
|
||||
categories []domain.CandidateViewCategory
|
||||
}
|
||||
|
||||
func (projector categoriesProjector) ProjectSources(organizationID uuid.UUID, sources []domain.CandidateViewSource) ([]domain.AnonymousCandidateViewDTO, error) {
|
||||
views, err := projector.contactAccessProjector.ProjectSources(organizationID, sources)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
views[0].Categories = projector.categories
|
||||
return views, nil
|
||||
}
|
||||
|
||||
type categoriesSectionFixture struct {
|
||||
service *Service
|
||||
contact *contactAccessFixture
|
||||
actorID uuid.UUID
|
||||
organizationID uuid.UUID
|
||||
handle string
|
||||
quotaCalls int
|
||||
}
|
||||
|
||||
func newCategoriesSectionFixture(t *testing.T, categories []domain.CandidateViewCategory, allowed bool, actorScenario domain.Scenario) *categoriesSectionFixture {
|
||||
t.Helper()
|
||||
now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
|
||||
actorID, candidateID := uuid.New(), uuid.New()
|
||||
organization, membership := newContactAccessOrganization(t, actorID, now)
|
||||
actor := domain.User{ID: actorID, Scenario: actorScenario, EmailVerifiedAt: &now}
|
||||
candidate := domain.User{ID: candidateID, Scenario: domain.ScenarioCandidate, EmailVerifiedAt: &now}
|
||||
publication, err := domain.NewCardFieldSet([]domain.CardField{domain.CardFieldCategories})
|
||||
if err != nil {
|
||||
t.Fatalf("NewCardFieldSet() error = %v", err)
|
||||
}
|
||||
source, err := domain.NewCandidateViewSource(candidateID, publication, domain.CandidateViewProfile{}, nil, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewCandidateViewSource() error = %v", err)
|
||||
}
|
||||
key, err := domain.NewCandidateViewPseudonymKey(bytes.Repeat([]byte("section-view-key"), 2))
|
||||
if err != nil {
|
||||
t.Fatalf("NewCandidateViewPseudonymKey() error = %v", err)
|
||||
}
|
||||
handle, err := key.ForOrganization(organization.ID(), candidateID)
|
||||
if err != nil {
|
||||
t.Fatalf("ForOrganization() error = %v", err)
|
||||
}
|
||||
fixture := &categoriesSectionFixture{
|
||||
contact: &contactAccessFixture{}, actorID: actorID, organizationID: organization.ID(), handle: handle,
|
||||
}
|
||||
unit := contactAccessUnit{
|
||||
users: contactAccessUsers{users: map[uuid.UUID]domain.User{actorID: actor, candidateID: candidate}},
|
||||
organizations: contactAccessOrganizations{organization: organization, membership: membership},
|
||||
view: contactAccessViewRepository{fixture: fixture.contact, source: source},
|
||||
}
|
||||
fixture.service, err = NewService(Dependencies{
|
||||
Transactor: contactAccessTransactor{unit: unit},
|
||||
Projector: categoriesProjector{
|
||||
contactAccessProjector: contactAccessProjector{key: key, source: source}, categories: categories,
|
||||
},
|
||||
PseudonymKey: key,
|
||||
Quota: func(context.Context, outbound.UnitOfWork, domain.Organization, uuid.UUID) (outbound.CompanyQuotaDecision, error) {
|
||||
fixture.quotaCalls++
|
||||
return outbound.CompanyQuotaDecision{Allowed: allowed, RetryAt: now.Add(time.Minute)}, nil
|
||||
},
|
||||
Clock: func() time.Time { return now },
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewService() error = %v", err)
|
||||
}
|
||||
fixture.contact.audits = nil
|
||||
return fixture
|
||||
}
|
||||
|
||||
func (fixture *categoriesSectionFixture) authorize(t *testing.T, handle string) (uuid.UUID, []domain.CandidateViewCategory, error) {
|
||||
t.Helper()
|
||||
var candidateID uuid.UUID
|
||||
var categories []domain.CandidateViewCategory
|
||||
err := fixture.service.transactor.WithinTransaction(context.Background(), func(ctx context.Context, unit outbound.UnitOfWork) error {
|
||||
var err error
|
||||
candidateID, categories, err = fixture.service.AuthorizeCategoriesSection(ctx, unit, fixture.actorID, fixture.organizationID, handle)
|
||||
return err
|
||||
})
|
||||
return candidateID, categories, err
|
||||
}
|
||||
|
||||
func TestAuthorizeCategoriesSectionNamesTheCategoriesOfTheCurrentCard(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
shown := []domain.CandidateViewCategory{
|
||||
{Specialization: domain.SpecializationBackend, Grade: domain.GradeJunior, Strength: domain.CandidateViewStrengthStandard},
|
||||
{Specialization: domain.SpecializationQA, Grade: domain.GradeIntern, Strength: domain.CandidateViewStrengthStandard},
|
||||
}
|
||||
fixture := newCategoriesSectionFixture(t, shown, true, domain.ScenarioEmployer)
|
||||
|
||||
candidateID, categories, err := fixture.authorize(t, fixture.handle)
|
||||
if err != nil {
|
||||
t.Fatalf("AuthorizeCategoriesSection() error = %v", err)
|
||||
}
|
||||
if candidateID == uuid.Nil || !slices.Equal(categories, shown) {
|
||||
t.Fatalf("section = %s %v, want the candidate and the categories of the card", candidateID, categories)
|
||||
}
|
||||
if fixture.quotaCalls != 1 || len(fixture.contact.audits) != 1 ||
|
||||
fixture.contact.audits[0].Kind() != domain.CandidateCardAccessCardViewed ||
|
||||
!slices.Equal(fixture.contact.audits[0].FieldCodes(), []string{"categories"}) {
|
||||
t.Fatalf("quota calls = %d, audits = %v, want one view of the categories field", fixture.quotaCalls, fixture.contact.audits)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeCategoriesSectionRefusesWithoutLeavingATrace(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
categories := []domain.CandidateViewCategory{{Specialization: domain.SpecializationBackend, Grade: domain.GradeJunior, Strength: domain.CandidateViewStrengthStandard}}
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
categories []domain.CandidateViewCategory
|
||||
allowed bool
|
||||
scenario domain.Scenario
|
||||
handle func(*categoriesSectionFixture) string
|
||||
want error
|
||||
wantQuota int
|
||||
}{
|
||||
{name: "a card without categories", allowed: true, scenario: domain.ScenarioEmployer, want: ErrCandidateCardNotFound},
|
||||
{name: "an exhausted quota", categories: categories, scenario: domain.ScenarioEmployer, want: ErrCandidateViewQuotaExceeded, wantQuota: 1},
|
||||
{name: "a candidate account", categories: categories, allowed: true, scenario: domain.ScenarioCandidate, want: ErrForbidden},
|
||||
{
|
||||
name: "a handle of another organization", categories: categories, allowed: true, scenario: domain.ScenarioEmployer,
|
||||
handle: func(*categoriesSectionFixture) string { return "cv1_" + string(bytes.Repeat([]byte("A"), 43)) },
|
||||
want: ErrCandidateCardNotFound,
|
||||
},
|
||||
{
|
||||
name: "a malformed handle", categories: categories, allowed: true, scenario: domain.ScenarioEmployer,
|
||||
handle: func(*categoriesSectionFixture) string { return "candidate" }, want: ErrInvalidCandidateHandle,
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
fixture := newCategoriesSectionFixture(t, test.categories, test.allowed, test.scenario)
|
||||
handle := fixture.handle
|
||||
if test.handle != nil {
|
||||
handle = test.handle(fixture)
|
||||
}
|
||||
_, _, err := fixture.authorize(t, handle)
|
||||
if !errors.Is(err, test.want) {
|
||||
t.Fatalf("AuthorizeCategoriesSection() error = %v, want %v", err, test.want)
|
||||
}
|
||||
if len(fixture.contact.audits) != 0 || fixture.quotaCalls != test.wantQuota {
|
||||
t.Fatalf("audits = %v, quota calls = %d, want no audit and %d quota calls", fixture.contact.audits, fixture.quotaCalls, test.wantQuota)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -114,23 +115,90 @@ func (service *Service) getOrganizationCardInTransaction(
|
||||
if err != nil {
|
||||
return domain.AnonymousCandidateViewDTO{}, err
|
||||
}
|
||||
decision, err := service.quota(ctx, unit, organization, candidateID)
|
||||
err = service.reserveCardView(ctx, unit, repository, organization, candidateID, location, func(at time.Time) (domain.CandidateCardAccess, error) {
|
||||
return cardViewAccessEvent(candidateID, organizationID, actorID, view, at)
|
||||
})
|
||||
if err != nil {
|
||||
return domain.AnonymousCandidateViewDTO{}, fmt.Errorf("%s - reserve card view: %w", location, err)
|
||||
}
|
||||
if !decision.Allowed {
|
||||
return domain.AnonymousCandidateViewDTO{}, &RateLimitedError{RetryAt: decision.RetryAt.UTC()}
|
||||
}
|
||||
event, err := cardViewAccessEvent(candidateID, organizationID, actorID, view, service.clock())
|
||||
if err != nil {
|
||||
return domain.AnonymousCandidateViewDTO{}, fmt.Errorf("%s - build access audit: %w", location, err)
|
||||
}
|
||||
if err := repository.RecordCandidateCardAccess(ctx, event); err != nil {
|
||||
return domain.AnonymousCandidateViewDTO{}, fmt.Errorf("%s - record access audit: %w", location, err)
|
||||
return domain.AnonymousCandidateViewDTO{}, err
|
||||
}
|
||||
return view, nil
|
||||
}
|
||||
|
||||
// reserveCardView consumes the card-view quota and appends the access audit
|
||||
// that auditFor builds. Both commit with the visibility checks that the
|
||||
// caller ran in the same transaction, so a failed read leaves neither behind.
|
||||
func (service *Service) reserveCardView(
|
||||
ctx context.Context,
|
||||
unit outbound.UnitOfWork,
|
||||
repository outbound.PrivacyViewRepository,
|
||||
organization domain.Organization,
|
||||
candidateID uuid.UUID,
|
||||
location string,
|
||||
auditFor func(at time.Time) (domain.CandidateCardAccess, error),
|
||||
) error {
|
||||
decision, err := service.quota(ctx, unit, organization, candidateID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - reserve card view: %w", location, err)
|
||||
}
|
||||
if !decision.Allowed {
|
||||
return &RateLimitedError{RetryAt: decision.RetryAt.UTC()}
|
||||
}
|
||||
event, err := auditFor(service.clock())
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - build access audit: %w", location, err)
|
||||
}
|
||||
if err := repository.RecordCandidateCardAccess(ctx, event); err != nil {
|
||||
return fmt.Errorf("%s - record access audit: %w", location, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AuthorizeCategoriesSection runs the checks of an organization card read
|
||||
// inside the caller's transaction and reports which candidate, and which
|
||||
// categories (specialization and grade), the card shows. It exists for the sections
|
||||
// of a card that explain its categories, such as the permitted evidence: they
|
||||
// must not disclose more than the categories of the current card do.
|
||||
//
|
||||
// The checks are those of GetOrganizationCard: the active membership of the
|
||||
// actor, the effective C2 publication, the hiding of this organization, and
|
||||
// the minimum cohort, all under the same locks. The card must also show at
|
||||
// least one category after the cohort rules, otherwise the answer is
|
||||
// ErrCandidateCardNotFound, the same as for an unknown handle. A successful
|
||||
// call consumes the card-view quota and records the access with the
|
||||
// categories field, so the candidate sees the read in the access history. The
|
||||
// caller reads the section in the same transaction, after this returns.
|
||||
func (service *Service) AuthorizeCategoriesSection(
|
||||
ctx context.Context,
|
||||
unit outbound.UnitOfWork,
|
||||
actorID, organizationID uuid.UUID,
|
||||
handle string,
|
||||
) (uuid.UUID, []domain.CandidateViewCategory, error) {
|
||||
const location = "candidateview.Service.AuthorizeCategoriesSection"
|
||||
if actorID == uuid.Nil || organizationID == uuid.Nil {
|
||||
return uuid.Nil, nil, ErrForbidden
|
||||
}
|
||||
if !domain.IsCandidateViewPseudonym(handle) {
|
||||
return uuid.Nil, nil, ErrInvalidCandidateHandle
|
||||
}
|
||||
organization, repository, candidateID, view, err := service.currentOrganizationCard(ctx, unit, actorID, organizationID, handle, location)
|
||||
if err != nil {
|
||||
return uuid.Nil, nil, err
|
||||
}
|
||||
if len(view.Categories) == 0 {
|
||||
return uuid.Nil, nil, ErrCandidateCardNotFound
|
||||
}
|
||||
err = service.reserveCardView(ctx, unit, repository, organization, candidateID, location, func(at time.Time) (domain.CandidateCardAccess, error) {
|
||||
return domain.NewCandidateCardAccess(domain.CandidateCardAccessInput{
|
||||
CandidateID: candidateID, OrganizationID: organizationID, ActorID: actorID,
|
||||
Kind: domain.CandidateCardAccessCardViewed, ProjectionFields: []domain.CardField{domain.CardFieldCategories}, OccurredAt: at,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return uuid.Nil, nil, err
|
||||
}
|
||||
return candidateID, slices.Clone(view.Categories), nil
|
||||
}
|
||||
|
||||
func (service *Service) currentOrganizationCard(
|
||||
ctx context.Context,
|
||||
unit outbound.UnitOfWork,
|
||||
@@ -216,9 +284,13 @@ func projectedViewForHandle(
|
||||
// PreviewResult contains only the candidate's own current publication state
|
||||
// and the strict anonymous-card projection.
|
||||
type PreviewResult struct {
|
||||
Published bool
|
||||
Fields domain.CardFieldSet
|
||||
Card *domain.AnonymousCandidateViewDTO
|
||||
Published bool
|
||||
Fields domain.CardFieldSet
|
||||
Card *domain.AnonymousCandidateViewDTO
|
||||
// Evidence is what an organization would read with the categories of the
|
||||
// card: for each category, the one fact that supports its confirmed grade.
|
||||
// It is empty when the card shows no category.
|
||||
Evidence []domain.EvidenceOrganizationFact
|
||||
GeneratedAt time.Time
|
||||
}
|
||||
|
||||
@@ -266,7 +338,13 @@ func (service *Service) GetMyCardPreview(ctx context.Context, actorID uuid.UUID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - build private preview: %w", location, err)
|
||||
}
|
||||
result = PreviewResult{Published: true, Fields: source.PublishedFields(), Card: &card, GeneratedAt: generatedAt}
|
||||
var facts []domain.EvidenceOrganizationFact
|
||||
if len(card.Categories) > 0 {
|
||||
if facts, err = unit.Evidence().ListPreviewFacts(ctx, actorID, card.Categories); err != nil {
|
||||
return fmt.Errorf("%s - read preview evidence: %w", location, err)
|
||||
}
|
||||
}
|
||||
result = PreviewResult{Published: true, Fields: source.PublishedFields(), Card: &card, Evidence: facts, GeneratedAt: generatedAt}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
// Package evidence reads the skill evidence projection. The candidate reads
|
||||
// their own facts; an organization reads only the permitted projection of a
|
||||
// card it is currently allowed to see. The projection holds no answers,
|
||||
// failures, or anti-cheat data, and this package has no way to reach them.
|
||||
package evidence
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrForbidden reports an actor of the wrong role or without a verified
|
||||
// email.
|
||||
ErrForbidden = errors.New("skill evidence is forbidden")
|
||||
// ErrInvalidRequest reports a page request outside the contract bounds.
|
||||
ErrInvalidRequest = errors.New("skill evidence request is invalid")
|
||||
// ErrServiceMissing reports an incomplete service wiring.
|
||||
ErrServiceMissing = errors.New("skill evidence service is unavailable")
|
||||
)
|
||||
|
||||
// AuthorizeCategoriesSection runs the shared organization card checks inside
|
||||
// the given transaction: active membership, effective C2 publication, hiding of
|
||||
// the organization, the minimum cohort, the card-view quota, and the access
|
||||
// audit. It returns the candidate and the categories that the card shows. The
|
||||
// candidateview usecase provides it; bootstrap passes it in so this package
|
||||
// does not depend on another usecase.
|
||||
type AuthorizeCategoriesSection func(
|
||||
ctx context.Context,
|
||||
unit outbound.UnitOfWork,
|
||||
actorID, organizationID uuid.UUID,
|
||||
handle string,
|
||||
) (uuid.UUID, []domain.CandidateViewCategory, error)
|
||||
|
||||
// Dependencies configure the read service.
|
||||
type Dependencies struct {
|
||||
Transactor outbound.Transactor
|
||||
AuthorizeCategories AuthorizeCategoriesSection
|
||||
}
|
||||
|
||||
// Service reads the evidence projection.
|
||||
type Service struct {
|
||||
transactor outbound.Transactor
|
||||
authorizeCategories AuthorizeCategoriesSection
|
||||
}
|
||||
|
||||
func NewService(deps Dependencies) (*Service, error) {
|
||||
if deps.Transactor == nil || deps.AuthorizeCategories == nil {
|
||||
return nil, fmt.Errorf("evidence.NewService - dependencies are missing: %w", ErrServiceMissing)
|
||||
}
|
||||
return &Service{transactor: deps.Transactor, authorizeCategories: deps.AuthorizeCategories}, nil
|
||||
}
|
||||
|
||||
// ListMine returns one owner-scoped page of the candidate's facts, hidden ones
|
||||
// included. The candidate comes from the session and nothing else selects the
|
||||
// owner.
|
||||
func (service *Service) ListMine(ctx context.Context, query outbound.EvidenceListQuery) (outbound.EvidenceFactPage, error) {
|
||||
const location = "evidence.Service.ListMine"
|
||||
if query.CandidateID == uuid.Nil {
|
||||
return outbound.EvidenceFactPage{}, ErrForbidden
|
||||
}
|
||||
if query.Limit < 1 || query.Limit > outbound.MaxEvidencePageSize {
|
||||
return outbound.EvidenceFactPage{}, ErrInvalidRequest
|
||||
}
|
||||
var page outbound.EvidenceFactPage
|
||||
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
||||
if err := authorizeCandidate(ctx, unit, query.CandidateID); err != nil {
|
||||
return err
|
||||
}
|
||||
var readErr error
|
||||
page, readErr = unit.Evidence().ListCandidateFacts(ctx, query)
|
||||
if readErr != nil {
|
||||
return fmt.Errorf("%s - read owner facts: %w", location, readErr)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return outbound.EvidenceFactPage{}, fmt.Errorf("%s - run transaction: %w", location, err)
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
|
||||
// ListForOrganization returns the permitted facts of the card that the handle
|
||||
// names for the organization. The membership, the publication, the hiding, and
|
||||
// the cohort are rechecked by the card checks, and the repository rechecks the
|
||||
// publication and the hiding again in the statement that reads the facts, so a
|
||||
// change between the two cannot widen the answer. Nothing returned belongs to
|
||||
// a candidate that the organization may not see now.
|
||||
func (service *Service) ListForOrganization(
|
||||
ctx context.Context,
|
||||
actorID, organizationID uuid.UUID,
|
||||
handle string,
|
||||
) ([]domain.EvidenceOrganizationFact, error) {
|
||||
const location = "evidence.Service.ListForOrganization"
|
||||
var facts []domain.EvidenceOrganizationFact
|
||||
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
||||
candidateID, categories, err := service.authorizeCategories(ctx, unit, actorID, organizationID, handle)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
facts, err = unit.Evidence().ListOrganizationFacts(ctx, organizationID, candidateID, categories)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - read permitted facts: %w", location, err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - run transaction: %w", location, err)
|
||||
}
|
||||
return facts, nil
|
||||
}
|
||||
|
||||
func authorizeCandidate(ctx context.Context, unit outbound.UnitOfWork, actorID uuid.UUID) error {
|
||||
user, err := unit.Users().GetByID(ctx, actorID)
|
||||
if errors.Is(err, outbound.ErrUserNotFound) {
|
||||
return ErrForbidden
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("evidence.authorizeCandidate - read actor account: %w", err)
|
||||
}
|
||||
if !user.EmailVerified() || user.Scenario != domain.ScenarioCandidate {
|
||||
return ErrForbidden
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package evidence
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
type evidenceTestTransactor struct{ unit outbound.UnitOfWork }
|
||||
|
||||
func (transactor evidenceTestTransactor) WithinTransaction(ctx context.Context, fn func(context.Context, outbound.UnitOfWork) error) error {
|
||||
return fn(ctx, transactor.unit)
|
||||
}
|
||||
|
||||
type evidenceTestUnit struct {
|
||||
outbound.UnitOfWork
|
||||
|
||||
users outbound.UserRepository
|
||||
evidence *evidenceTestRepository
|
||||
}
|
||||
|
||||
func (unit evidenceTestUnit) Users() outbound.UserRepository { return unit.users }
|
||||
func (unit evidenceTestUnit) Evidence() outbound.EvidenceRepository { return unit.evidence }
|
||||
|
||||
type evidenceTestUsers struct {
|
||||
outbound.UserRepository
|
||||
|
||||
users map[uuid.UUID]domain.User
|
||||
}
|
||||
|
||||
func (repository evidenceTestUsers) GetByID(_ context.Context, id uuid.UUID) (domain.User, error) {
|
||||
user, ok := repository.users[id]
|
||||
if !ok {
|
||||
return domain.User{}, outbound.ErrUserNotFound
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
type evidenceTestRepository struct {
|
||||
query outbound.EvidenceListQuery
|
||||
organizationID uuid.UUID
|
||||
candidateID uuid.UUID
|
||||
categories []domain.CandidateViewCategory
|
||||
listCalls int
|
||||
organizationHit int
|
||||
}
|
||||
|
||||
func (repository *evidenceTestRepository) ListCandidateFacts(_ context.Context, query outbound.EvidenceListQuery) (outbound.EvidenceFactPage, error) {
|
||||
repository.listCalls++
|
||||
repository.query = query
|
||||
return outbound.EvidenceFactPage{}, nil
|
||||
}
|
||||
|
||||
func (repository *evidenceTestRepository) ListOrganizationFacts(
|
||||
_ context.Context,
|
||||
organizationID, candidateID uuid.UUID,
|
||||
categories []domain.CandidateViewCategory,
|
||||
) ([]domain.EvidenceOrganizationFact, error) {
|
||||
repository.organizationHit++
|
||||
repository.organizationID, repository.candidateID, repository.categories = organizationID, candidateID, categories
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (repository *evidenceTestRepository) ListPreviewFacts(context.Context, uuid.UUID, []domain.CandidateViewCategory) ([]domain.EvidenceOrganizationFact, error) {
|
||||
return nil, errors.New("the organization read must not read the preview")
|
||||
}
|
||||
|
||||
func newEvidenceTestService(t *testing.T, authorize AuthorizeCategoriesSection) (*Service, *evidenceTestRepository, domain.User, domain.User) {
|
||||
t.Helper()
|
||||
now := time.Date(2026, 10, 8, 12, 0, 0, 0, time.UTC)
|
||||
candidate := domain.User{ID: uuid.New(), Scenario: domain.ScenarioCandidate, EmailVerifiedAt: &now}
|
||||
employer := domain.User{ID: uuid.New(), Scenario: domain.ScenarioEmployer, EmailVerifiedAt: &now}
|
||||
unverified := domain.User{ID: uuid.New(), Scenario: domain.ScenarioCandidate}
|
||||
repository := &evidenceTestRepository{}
|
||||
unit := evidenceTestUnit{
|
||||
users: evidenceTestUsers{users: map[uuid.UUID]domain.User{
|
||||
candidate.ID: candidate, employer.ID: employer, unverified.ID: unverified,
|
||||
}},
|
||||
evidence: repository,
|
||||
}
|
||||
service, err := NewService(Dependencies{Transactor: evidenceTestTransactor{unit: unit}, AuthorizeCategories: authorize})
|
||||
if err != nil {
|
||||
t.Fatalf("NewService() error = %v", err)
|
||||
}
|
||||
return service, repository, candidate, unverified
|
||||
}
|
||||
|
||||
func noAuthorization(context.Context, outbound.UnitOfWork, uuid.UUID, uuid.UUID, string) (uuid.UUID, []domain.CandidateViewCategory, error) {
|
||||
return uuid.Nil, nil, errors.New("the owner read must not run the organization card checks")
|
||||
}
|
||||
|
||||
func TestNewServiceRequiresItsDependencies(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if _, err := NewService(Dependencies{AuthorizeCategories: noAuthorization}); !errors.Is(err, ErrServiceMissing) {
|
||||
t.Fatalf("NewService(without transactor) error = %v, want ErrServiceMissing", err)
|
||||
}
|
||||
if _, err := NewService(Dependencies{Transactor: evidenceTestTransactor{}}); !errors.Is(err, ErrServiceMissing) {
|
||||
t.Fatalf("NewService(without card checks) error = %v, want ErrServiceMissing", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListMineReadsOnlyForAVerifiedCandidate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
service, repository, candidate, unverified := newEvidenceTestService(t, noAuthorization)
|
||||
for name, test := range map[string]struct {
|
||||
query outbound.EvidenceListQuery
|
||||
want error
|
||||
}{
|
||||
"employer account": {outbound.EvidenceListQuery{CandidateID: uuid.New(), Limit: 20}, ErrForbidden},
|
||||
"unverified email": {outbound.EvidenceListQuery{CandidateID: unverified.ID, Limit: 20}, ErrForbidden},
|
||||
"no owner": {outbound.EvidenceListQuery{Limit: 20}, ErrForbidden},
|
||||
"limit below range": {outbound.EvidenceListQuery{CandidateID: candidate.ID, Limit: 0}, ErrInvalidRequest},
|
||||
"limit above range": {outbound.EvidenceListQuery{CandidateID: candidate.ID, Limit: outbound.MaxEvidencePageSize + 1}, ErrInvalidRequest},
|
||||
} {
|
||||
if _, err := service.ListMine(context.Background(), test.query); !errors.Is(err, test.want) {
|
||||
t.Errorf("%s: error = %v, want %v", name, err, test.want)
|
||||
}
|
||||
}
|
||||
if repository.listCalls != 0 {
|
||||
t.Fatalf("a refused read reached the repository %d times", repository.listCalls)
|
||||
}
|
||||
|
||||
if _, err := service.ListMine(context.Background(), outbound.EvidenceListQuery{CandidateID: candidate.ID, Limit: 20}); err != nil {
|
||||
t.Fatalf("ListMine(candidate) error = %v", err)
|
||||
}
|
||||
if repository.query.CandidateID != candidate.ID || repository.listCalls != 1 {
|
||||
t.Fatalf("repository query = %+v after %d calls, want the owner once", repository.query, repository.listCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListForOrganizationReadsOnlyWhatTheCardChecksName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
organizationID, actorID, candidateID := uuid.New(), uuid.New(), uuid.New()
|
||||
var authorized bool
|
||||
service, repository, _, _ := newEvidenceTestService(t, func(_ context.Context, _ outbound.UnitOfWork, actor, organization uuid.UUID, handle string) (uuid.UUID, []domain.CandidateViewCategory, error) {
|
||||
authorized = actor == actorID && organization == organizationID && handle == "handle"
|
||||
return candidateID, []domain.CandidateViewCategory{{Specialization: domain.SpecializationBackend, Grade: domain.GradeIntern}}, nil
|
||||
})
|
||||
if _, err := service.ListForOrganization(context.Background(), actorID, organizationID, "handle"); err != nil {
|
||||
t.Fatalf("ListForOrganization() error = %v", err)
|
||||
}
|
||||
if !authorized || repository.organizationID != organizationID || repository.candidateID != candidateID ||
|
||||
len(repository.categories) != 1 || repository.categories[0].Specialization != domain.SpecializationBackend ||
|
||||
repository.categories[0].Grade != domain.GradeIntern {
|
||||
t.Fatalf("read scope = organization %s candidate %s categories %v", repository.organizationID, repository.candidateID, repository.categories)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListForOrganizationStopsWhenTheCardChecksRefuse(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
refusal := errors.New("card checks refused")
|
||||
service, repository, _, _ := newEvidenceTestService(t, func(context.Context, outbound.UnitOfWork, uuid.UUID, uuid.UUID, string) (uuid.UUID, []domain.CandidateViewCategory, error) {
|
||||
return uuid.Nil, nil, refusal
|
||||
})
|
||||
if _, err := service.ListForOrganization(context.Background(), uuid.New(), uuid.New(), "handle"); !errors.Is(err, refusal) {
|
||||
t.Fatalf("ListForOrganization() error = %v, want the refusal of the card checks", err)
|
||||
}
|
||||
if repository.organizationHit != 0 {
|
||||
t.Fatal("facts were read after the card checks refused")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user