Files
lincodex/backend/internal/usecase/invitation/contacts_test.go
T

206 lines
8.8 KiB
Go

package invitation
import (
"context"
"errors"
"testing"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
// testPrivacyView is the visibility barrier of the fakes. A published
// publication has one active C2 record, and a hidden organization may not read
// the profile. The database keeps the stamps; these fakes only answer.
type testPrivacyView struct {
outbound.PrivacyViewRepository
state *testState
}
func (unit *testUnit) PrivacyView() outbound.PrivacyViewRepository {
return testPrivacyView{state: unit.state}
}
func (testPrivacyView) LockCandidateAccountForView(context.Context, uuid.UUID) error { return nil }
func (repository testPrivacyView) LockCurrentC2ForView(_ context.Context, candidateID uuid.UUID) (uuid.UUID, error) {
publication, ok := repository.state.publications[candidateID]
if !ok || publication.State() != domain.PublicationPublished {
return uuid.Nil, outbound.ErrCandidateC2Unavailable
}
return publication.ConsentRecordID(), nil
}
func (repository testPrivacyView) LockVisiblePublicationForView(_ context.Context, organizationID, candidateID, _ uuid.UUID) (bool, error) {
publication, ok := repository.state.publications[candidateID]
return ok && publication.State() == domain.PublicationPublished &&
!repository.state.hidden[[2]uuid.UUID{candidateID, organizationID}], nil
}
func (testPrivacyView) RecordCandidateCardAccess(context.Context, domain.CandidateCardAccess) error {
return nil
}
// LockForRead answers from the stored grant and its C4 record, the way the
// database does: the record must be unrevoked for the grant to be current.
func (repository testContactGrants) LockForRead(_ context.Context, grant domain.ContactGrant) (domain.ContactGrant, bool, error) {
stored, ok := repository.state.grants[grant.InvitationID()]
if !ok || stored.ID() != grant.ID() {
return domain.ContactGrant{}, false, outbound.ErrContactGrantNotFound
}
record, ok := repository.state.consentRecords[grant.InvitationID()]
if !ok {
return stored, false, nil
}
_, revoked := record.RevokedAt()
return stored, !revoked, nil
}
// acceptedForContacts delivers an invitation to a published candidate who
// holds an email and a phone number, and accepts it for the email only.
func acceptedForContacts(t *testing.T, test *harness) (domain.OrganizationInvitationView, uuid.UUID) {
t.Helper()
sent, candidateID := test.deliveredTo(t)
test.setProfile(t, candidateID, domain.ContactFieldEmail, domain.ContactFieldPhone)
test.setTime(sent.DeliveredAt.Add(time.Hour))
test.publish(candidateID, *sent.DeliveredAt)
if _, err := test.service.AcceptMine(t.Context(), acceptCommand(candidateID, sent.ID, sent.Version, sent.TermsVersion,
"accept-for-contacts", domain.ContactFieldEmail)); err != nil {
t.Fatalf("AcceptMine() error = %v", err)
}
return sent, candidateID
}
func (test *harness) addMember(t *testing.T, organizationID, actorID uuid.UUID, role domain.OrganizationRole) {
t.Helper()
verified := time.Date(2026, time.October, 9, 9, 0, 0, 0, time.UTC)
test.setState(func(state *testState) {
state.users[actorID] = domain.User{ID: actorID, Scenario: domain.ScenarioEmployer, EmailVerifiedAt: &verified}
membership, err := domain.NewMembership(domain.MembershipInput{
OrganizationID: organizationID, UserID: actorID, Role: role, At: verified,
})
if err != nil {
t.Fatalf("NewMembership() error = %v", err)
}
state.memberships[[2]uuid.UUID{organizationID, actorID}] = membership
})
}
func TestReadContactsReturnsOnlyTheGrantedKindsToTheOwner(t *testing.T) {
test := newHarness(t)
sent, _ := acceptedForContacts(t, test)
disclosure, err := test.service.ReadContacts(t.Context(), test.actorID, test.organizationID, sent.ID)
if err != nil {
t.Fatalf("ReadContacts() error = %v", err)
}
if email, ok := disclosure.Email(); !ok || email != "candidate@example.test" {
t.Fatalf("email = %q, %v; want the confirmed address", email, ok)
}
if _, ok := disclosure.Phone(); ok {
t.Fatal("phone was disclosed although the grant names only the email")
}
if len(disclosure.Fields()) != 1 || disclosure.Fields()[0] != domain.ContactFieldEmail {
t.Fatalf("fields = %v, want the email only", disclosure.Fields())
}
if disclosure.GrantID() != test.transactor.snapshot().grants[sent.ID].ID() {
t.Fatal("the disclosure names another grant than the stored one")
}
}
func TestReadContactsRefusesEveryOtherActorBeforeTheGrant(t *testing.T) {
test := newHarness(t)
sent, candidateID := acceptedForContacts(t, test)
recruiter := uuid.Must(uuid.NewV7())
test.addMember(t, test.organizationID, recruiter, domain.OrganizationRoleRecruiter)
outsider := uuid.Must(uuid.NewV7())
test.addMember(t, uuid.Must(uuid.NewV7()), outsider, domain.OrganizationRoleOwner)
otherOrganization := uuid.Must(uuid.NewV7())
otherOwner := uuid.Must(uuid.NewV7())
test.addMember(t, otherOrganization, otherOwner, domain.OrganizationRoleOwner)
tests := []struct {
name string
actorID uuid.UUID
organizationID uuid.UUID
want error
}{
{"non-owner member of the organization", recruiter, test.organizationID, outbound.ErrOrganizationManagerRequired},
{"candidate account", candidateID, test.organizationID, ErrForbidden},
{"employer without a membership in the organization", outsider, test.organizationID, outbound.ErrOrganizationNotFound},
{"owner of another organization", otherOwner, otherOrganization, outbound.ErrContactGrantNotFound},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
_, err := test.service.ReadContacts(t.Context(), tc.actorID, tc.organizationID, sent.ID)
if !errors.Is(err, tc.want) {
t.Fatalf("ReadContacts() error = %v, want %v", err, tc.want)
}
})
}
}
func TestReadContactsIsClosedWhileTheProfileIsNotReadable(t *testing.T) {
test := newHarness(t)
sent, candidateID := acceptedForContacts(t, test)
publication := test.transactor.snapshot().publications[candidateID]
test.setState(func(state *testState) { state.hidden[[2]uuid.UUID{candidateID, test.organizationID}] = true })
if _, err := test.service.ReadContacts(t.Context(), test.actorID, test.organizationID, sent.ID); !errors.Is(err, outbound.ErrContactGrantNotFound) {
t.Fatalf("ReadContacts() while hidden error = %v, want not found", err)
}
test.setState(func(state *testState) {
delete(state.hidden, [2]uuid.UUID{candidateID, test.organizationID})
delete(state.publications, candidateID)
})
if _, err := test.service.ReadContacts(t.Context(), test.actorID, test.organizationID, sent.ID); !errors.Is(err, outbound.ErrContactGrantNotFound) {
t.Fatalf("ReadContacts() without a publication error = %v, want not found", err)
}
// The same grant is readable again once the profile is visible, which
// shows that the refusals above came from the visibility and not the grant.
test.setState(func(state *testState) { state.publications[candidateID] = publication })
if _, err := test.service.ReadContacts(t.Context(), test.actorID, test.organizationID, sent.ID); err != nil {
t.Fatalf("ReadContacts() after publishing again error = %v", err)
}
}
func TestReadContactsRefusesARevokedGrantOrAnInvitationWithoutOne(t *testing.T) {
test := newHarness(t)
sent, _ := acceptedForContacts(t, test)
grant := test.transactor.snapshot().grants[sent.ID]
stored := grant.Stored()
revokedAt := sent.DeliveredAt.Add(2 * time.Hour)
stored.RevokedAt, stored.RevocationReason = &revokedAt, string(domain.ContactRevocationConsentRevoked)
revoked, err := domain.RestoreContactGrant(stored)
if err != nil {
t.Fatalf("RestoreContactGrant() error = %v", err)
}
test.setState(func(state *testState) { state.grants[sent.ID] = revoked })
if _, err := test.service.ReadContacts(t.Context(), test.actorID, test.organizationID, sent.ID); !errors.Is(err, outbound.ErrContactGrantNotFound) {
t.Fatalf("ReadContacts() of a revoked grant error = %v, want not found", err)
}
delivered, _ := test.deliveredTo(t)
if _, err := test.service.ReadContacts(t.Context(), test.actorID, test.organizationID, delivered.ID); !errors.Is(err, outbound.ErrContactGrantNotFound) {
t.Fatalf("ReadContacts() of a delivered invitation error = %v, want not found", err)
}
for _, invitationID := range []uuid.UUID{uuid.Nil, uuid.Must(uuid.NewV7())} {
if _, err := test.service.ReadContacts(t.Context(), test.actorID, test.organizationID, invitationID); !errors.Is(err, outbound.ErrContactGrantNotFound) {
t.Fatalf("ReadContacts(%v) error = %v, want not found", invitationID, err)
}
}
}
func TestReadContactsRefusesAConsentRecordThatIsRevoked(t *testing.T) {
test := newHarness(t)
sent, _ := acceptedForContacts(t, test)
test.setState(func(state *testState) { delete(state.consentRecords, sent.ID) })
if _, err := test.service.ReadContacts(t.Context(), test.actorID, test.organizationID, sent.ID); !errors.Is(err, outbound.ErrContactGrantNotFound) {
t.Fatalf("ReadContacts() without a current C4 record error = %v, want not found", err)
}
}