109 lines
4.3 KiB
Go
109 lines
4.3 KiB
Go
package candidateview
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
func (fixture *categoriesSectionFixture) listVisible(t *testing.T) ([]domain.VisibleCandidateCard, error) {
|
|
t.Helper()
|
|
var cards []domain.VisibleCandidateCard
|
|
err := fixture.service.transactor.WithinTransaction(context.Background(), func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
var err error
|
|
cards, err = fixture.service.ListVisibleCards(ctx, unit, fixture.actorID, fixture.organizationID)
|
|
return err
|
|
})
|
|
return cards, err
|
|
}
|
|
|
|
func TestListVisibleCardsReturnsTheCardsOfTheCurrentCohortWithoutAViewTrace(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
shown := []domain.CandidateViewCategory{{Specialization: domain.SpecializationBackend, Grade: domain.GradeJunior, Strength: domain.CandidateViewStrengthStandard}}
|
|
fixture := newCategoriesSectionFixture(t, shown, true, domain.ScenarioEmployer)
|
|
|
|
cards, err := fixture.listVisible(t)
|
|
if err != nil {
|
|
t.Fatalf("ListVisibleCards() error = %v", err)
|
|
}
|
|
if len(cards) != 1 || cards[0].CandidateID == uuid.Nil || cards[0].Card.Pseudonym != fixture.handle ||
|
|
!slices.Equal(cards[0].Card.Categories, shown) {
|
|
t.Fatalf("cards = %v, want the one visible card under the organization handle", cards)
|
|
}
|
|
if fixture.quotaCalls != 0 || len(fixture.contact.audits) != 0 {
|
|
t.Fatalf("quota calls = %d, audits = %v, want a listing that is not a card view", fixture.quotaCalls, fixture.contact.audits)
|
|
}
|
|
}
|
|
|
|
func TestListVisibleCardsRefusesAnActorWithoutAnEmployerRole(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fixture := newCategoriesSectionFixture(t, nil, true, domain.ScenarioCandidate)
|
|
if _, err := fixture.listVisible(t); !errors.Is(err, ErrForbidden) {
|
|
t.Fatalf("ListVisibleCards() error = %v, want ErrForbidden", err)
|
|
}
|
|
if fixture.quotaCalls != 0 || len(fixture.contact.audits) != 0 {
|
|
t.Fatalf("quota calls = %d, audits = %v, want no trace", fixture.quotaCalls, fixture.contact.audits)
|
|
}
|
|
}
|
|
|
|
func TestSearchViewsChargeAndRecordWhatTheSearchReveals(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fixture := newCategoriesSectionFixture(t, nil, true, domain.ScenarioEmployer)
|
|
candidateID := uuid.New()
|
|
err := fixture.service.transactor.WithinTransaction(context.Background(), func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
views, err := fixture.service.BeginSearchViews(ctx, unit, fixture.actorID, fixture.organizationID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
allowed, err := views.Reserve(ctx, candidateID)
|
|
if err != nil || !allowed {
|
|
t.Fatalf("Reserve() = %v, %v, want allowed", allowed, err)
|
|
}
|
|
return views.Record(ctx, candidateID, []domain.CardField{domain.CardFieldCategories, domain.CardFieldStack})
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("search views error = %v", err)
|
|
}
|
|
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", "stack"}) {
|
|
t.Fatalf("quota calls = %d, audits = %v, want one charge and one view of the returned fields", fixture.quotaCalls, fixture.contact.audits)
|
|
}
|
|
}
|
|
|
|
func TestSearchViewsReportAnExhaustedAllowanceWithoutAWrite(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fixture := newCategoriesSectionFixture(t, nil, false, domain.ScenarioEmployer)
|
|
err := fixture.service.transactor.WithinTransaction(context.Background(), func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
views, err := fixture.service.BeginSearchViews(ctx, unit, fixture.actorID, fixture.organizationID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
allowed, err := views.Reserve(ctx, uuid.New())
|
|
if err != nil || allowed {
|
|
t.Fatalf("Reserve() = %v, %v, want a refusal without an error", allowed, err)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil || len(fixture.contact.audits) != 0 {
|
|
t.Fatalf("error = %v, audits = %v, want no record for a refused view", err, fixture.contact.audits)
|
|
}
|
|
candidate := newCategoriesSectionFixture(t, nil, true, domain.ScenarioCandidate)
|
|
err = candidate.service.transactor.WithinTransaction(context.Background(), func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
_, err := candidate.service.BeginSearchViews(ctx, unit, candidate.actorID, candidate.organizationID)
|
|
return err
|
|
})
|
|
if !errors.Is(err, ErrForbidden) {
|
|
t.Fatalf("BeginSearchViews(candidate) error = %v, want ErrForbidden", err)
|
|
}
|
|
}
|