feat(evidence): project skill evidence from confirmed assessments

This commit is contained in:
shira committed 2026-10-09 11:25:19 +03:00
1 parent 4761508f13
commit c2a8d17195
94 files changed
+10077 -151

No files matched your search

+131
View File
@@ -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")
}
}