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 {
|
||||
|
||||
Reference in new issue
Block a user