184 lines
7.5 KiB
Go
184 lines
7.5 KiB
Go
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) ListOrganizationFactsBatch(
|
|
context.Context, uuid.UUID, []outbound.EvidenceCandidateCategories,
|
|
) (map[uuid.UUID][]domain.EvidenceOrganizationFact, error) {
|
|
return nil, errors.New("the single organization read must not use the batch read")
|
|
}
|
|
|
|
func (repository *evidenceTestRepository) CountComparableRealCategories(
|
|
context.Context, []outbound.CategoryKey,
|
|
) (map[outbound.CategoryKey]int64, error) {
|
|
return nil, errors.New("the evidence read must not count cohorts")
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|