265 lines
9.8 KiB
Go
265 lines
9.8 KiB
Go
package candidateview
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
type contactAccessFixture struct {
|
|
fields domain.ContactFieldSet
|
|
grantActive bool
|
|
workflowAccept bool
|
|
audits []domain.CandidateCardAccess
|
|
}
|
|
|
|
func TestCurrentContactFieldsUsesOnlyCurrentGrantFields(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
selected, err := domain.NewContactFieldSet(domain.ContactFieldPhone, domain.ContactFieldEmail)
|
|
if err != nil {
|
|
t.Fatalf("NewContactFieldSet() error = %v", err)
|
|
}
|
|
full, err := domain.NewContactFieldSet(
|
|
domain.ContactFieldEmail, domain.ContactFieldFullName,
|
|
domain.ContactFieldPhone, domain.ContactFieldTelegram,
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("NewContactFieldSet(full) error = %v", err)
|
|
}
|
|
|
|
for _, test := range []struct {
|
|
name string
|
|
grantFields domain.ContactFieldSet
|
|
grantActive bool
|
|
workflowAccept bool
|
|
wantFields []domain.ContactField
|
|
wantError error
|
|
}{
|
|
{
|
|
name: "active invitation C4 returns only selected fields",
|
|
grantFields: selected, grantActive: true, workflowAccept: true,
|
|
wantFields: []domain.ContactField{domain.ContactFieldEmail, domain.ContactFieldPhone},
|
|
},
|
|
{
|
|
name: "accepted workflow without a C4 grant remains closed",
|
|
grantFields: full, workflowAccept: true, wantError: outbound.ErrContactAccessClosed,
|
|
},
|
|
{
|
|
name: "revoked C4 remains closed after workflow acceptance",
|
|
grantFields: selected, workflowAccept: true, wantError: outbound.ErrContactAccessClosed,
|
|
},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
service, fixture, actorID, organizationID, handle := newContactAccessService(t, test.grantFields, test.grantActive, test.workflowAccept)
|
|
|
|
got, err := service.CurrentContactFields(context.Background(), actorID, organizationID, handle)
|
|
if test.wantError != nil {
|
|
if !errors.Is(err, test.wantError) {
|
|
t.Fatalf("CurrentContactFields() error = %v, want %v", err, test.wantError)
|
|
}
|
|
if len(fixture.audits) != 0 {
|
|
t.Fatalf("closed contact read recorded audits: %v", fixture.audits)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("CurrentContactFields() error = %v", err)
|
|
}
|
|
if gotFields := got.Fields(); !slices.Equal(gotFields, test.wantFields) {
|
|
t.Fatalf("CurrentContactFields() fields = %v, want %v", gotFields, test.wantFields)
|
|
}
|
|
if len(fixture.audits) != 1 || fixture.audits[0].Kind() != domain.CandidateCardAccessContactRead ||
|
|
!slices.Equal(fixture.audits[0].FieldCodes(), []string{"email", "phone"}) {
|
|
t.Fatalf("contact read audit = %v, want only selected email and phone codes", fixture.audits)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func newContactAccessService(
|
|
t *testing.T,
|
|
fields domain.ContactFieldSet,
|
|
grantActive, workflowAccepted bool,
|
|
) (*Service, *contactAccessFixture, uuid.UUID, uuid.UUID, string) {
|
|
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)
|
|
organizationID := organization.ID()
|
|
actor := domain.User{ID: actorID, Scenario: domain.ScenarioEmployer, EmailVerifiedAt: &now}
|
|
candidate := domain.User{ID: candidateID, Scenario: domain.ScenarioCandidate, EmailVerifiedAt: &now}
|
|
users := contactAccessUsers{users: map[uuid.UUID]domain.User{actorID: actor, candidateID: candidate}}
|
|
organizations := contactAccessOrganizations{organization: organization, membership: membership}
|
|
publication, err := domain.NewCardFieldSet([]domain.CardField{domain.CardFieldStack})
|
|
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("contact-view-key"), 2))
|
|
if err != nil {
|
|
t.Fatalf("NewCandidateViewPseudonymKey() error = %v", err)
|
|
}
|
|
handle, err := key.ForOrganization(organizationID, candidateID)
|
|
if err != nil {
|
|
t.Fatalf("ForOrganization() error = %v", err)
|
|
}
|
|
fixture := &contactAccessFixture{
|
|
fields: fields, grantActive: grantActive, workflowAccept: workflowAccepted,
|
|
}
|
|
viewRepository := contactAccessViewRepository{fixture: fixture, source: source}
|
|
unit := contactAccessUnit{users: users, organizations: organizations, view: viewRepository}
|
|
service, err := NewService(Dependencies{
|
|
Transactor: contactAccessTransactor{unit: unit},
|
|
Projector: contactAccessProjector{key: key, source: source},
|
|
PseudonymKey: key, Quota: func(context.Context, outbound.UnitOfWork, domain.Organization, uuid.UUID) (outbound.CompanyQuotaDecision, error) {
|
|
return outbound.CompanyQuotaDecision{Allowed: true}, nil
|
|
}, Clock: func() time.Time { return now },
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewService() error = %v", err)
|
|
}
|
|
return service, fixture, actorID, organizationID, handle
|
|
}
|
|
|
|
func newContactAccessOrganization(t *testing.T, actorID uuid.UUID, at time.Time) (domain.Organization, domain.Membership) {
|
|
t.Helper()
|
|
profile, err := domain.NewOrganizationProfile(domain.OrganizationProfileInput{
|
|
Name: "Example Company", Description: "A sample company", Sphere: "Technology",
|
|
Contacts: domain.OrganizationContactsInput{Website: "https://example.com", Email: "jobs@example.com", Phone: "+12025550101", Telegram: "@examplejobs"},
|
|
Representatives: []domain.OrganizationRepresentativeInput{{Name: "Example Person", JobTitle: "Recruiter"}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewOrganizationProfile() error = %v", err)
|
|
}
|
|
organization, err := domain.NewOrganization(profile, at)
|
|
if err != nil {
|
|
t.Fatalf("NewOrganization() error = %v", err)
|
|
}
|
|
membership, err := domain.NewOwnerMembership(organization.ID(), actorID, at)
|
|
if err != nil {
|
|
t.Fatalf("NewOwnerMembership() error = %v", err)
|
|
}
|
|
return organization, membership
|
|
}
|
|
|
|
type contactAccessTransactor struct{ unit outbound.UnitOfWork }
|
|
|
|
func (transactor contactAccessTransactor) WithinTransaction(ctx context.Context, fn func(context.Context, outbound.UnitOfWork) error) error {
|
|
return fn(ctx, transactor.unit)
|
|
}
|
|
|
|
type contactAccessUnit struct {
|
|
outbound.UnitOfWork
|
|
|
|
users outbound.UserRepository
|
|
organizations outbound.OrganizationRepository
|
|
view outbound.PrivacyViewRepository
|
|
}
|
|
|
|
func (unit contactAccessUnit) Users() outbound.UserRepository { return unit.users }
|
|
func (unit contactAccessUnit) Organizations() outbound.OrganizationRepository {
|
|
return unit.organizations
|
|
}
|
|
func (unit contactAccessUnit) PrivacyView() outbound.PrivacyViewRepository { return unit.view }
|
|
func (unit contactAccessUnit) CompanyQuotas() outbound.CompanyQuotaRepository {
|
|
return organizationLockStub{}
|
|
}
|
|
|
|
// organizationLockStub accepts the organization lock and is not used for more.
|
|
type organizationLockStub struct {
|
|
outbound.CompanyQuotaRepository
|
|
}
|
|
|
|
func (organizationLockStub) LockOrganization(context.Context, uuid.UUID) error { return nil }
|
|
|
|
type contactAccessUsers struct {
|
|
outbound.UserRepository
|
|
|
|
users map[uuid.UUID]domain.User
|
|
}
|
|
|
|
func (repository contactAccessUsers) 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 contactAccessOrganizations struct {
|
|
outbound.OrganizationRepository
|
|
|
|
organization domain.Organization
|
|
membership domain.Membership
|
|
}
|
|
|
|
func (repository contactAccessOrganizations) LockActiveMembership(context.Context, uuid.UUID, uuid.UUID) (domain.Membership, error) {
|
|
return repository.membership, nil
|
|
}
|
|
func (repository contactAccessOrganizations) GetForMember(context.Context, uuid.UUID, uuid.UUID) (domain.Organization, domain.Membership, error) {
|
|
return repository.organization, repository.membership, nil
|
|
}
|
|
|
|
type contactAccessViewRepository struct {
|
|
outbound.PrivacyViewRepository
|
|
|
|
fixture *contactAccessFixture
|
|
source domain.CandidateViewSource
|
|
}
|
|
|
|
func (repository contactAccessViewRepository) ListVisibleCandidateViewSources(context.Context, uuid.UUID) ([]domain.CandidateViewSource, error) {
|
|
return []domain.CandidateViewSource{repository.source}, nil
|
|
}
|
|
func (contactAccessViewRepository) LockCandidateAccountForView(context.Context, uuid.UUID) error {
|
|
return nil
|
|
}
|
|
func (contactAccessViewRepository) LockCurrentC2ForView(context.Context, uuid.UUID) (uuid.UUID, error) {
|
|
return uuid.New(), nil
|
|
}
|
|
func (contactAccessViewRepository) LockVisiblePublicationForView(context.Context, uuid.UUID, uuid.UUID, uuid.UUID) (bool, error) {
|
|
return true, nil
|
|
}
|
|
func (repository contactAccessViewRepository) CurrentFields(context.Context, uuid.UUID, uuid.UUID) (domain.ContactFieldSet, error) {
|
|
if !repository.fixture.grantActive {
|
|
return domain.ContactFieldSet{}, outbound.ErrContactAccessClosed
|
|
}
|
|
return repository.fixture.fields, nil
|
|
}
|
|
func (repository contactAccessViewRepository) RecordCandidateCardAccess(_ context.Context, access domain.CandidateCardAccess) error {
|
|
repository.fixture.audits = append(repository.fixture.audits, access)
|
|
return nil
|
|
}
|
|
|
|
type contactAccessProjector struct {
|
|
key domain.CandidateViewPseudonymKey
|
|
source domain.CandidateViewSource
|
|
}
|
|
|
|
func (projector contactAccessProjector) ProjectSources(organizationID uuid.UUID, sources []domain.CandidateViewSource) ([]domain.AnonymousCandidateViewDTO, error) {
|
|
if len(sources) != 1 || sources[0].CandidateID() != projector.source.CandidateID() {
|
|
return nil, errors.New("contact access test projector received an unexpected source")
|
|
}
|
|
pseudonym, err := projector.key.ForOrganization(organizationID, projector.source.CandidateID())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []domain.AnonymousCandidateViewDTO{{Pseudonym: pseudonym}}, nil
|
|
}
|
|
|
|
func (projector contactAccessProjector) PreviewSource(domain.CandidateViewSource) (domain.AnonymousCandidateViewDTO, error) {
|
|
return domain.AnonymousCandidateViewDTO{}, errors.New("preview is not part of the contact access test")
|
|
}
|