200 lines
8.5 KiB
Go
200 lines
8.5 KiB
Go
package invitation
|
|
|
|
import (
|
|
"errors"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
// organizationAccess reads the invitation as its organization and returns the
|
|
// contact access of the response.
|
|
func organizationAccess(t *testing.T, test *harness, invitationID uuid.UUID) domain.InvitationContactAccessView {
|
|
t.Helper()
|
|
view, err := test.service.Get(t.Context(), test.actorID, test.organizationID, invitationID)
|
|
if err != nil {
|
|
t.Fatalf("Get() error = %v", err)
|
|
}
|
|
return view.ContactAccess
|
|
}
|
|
|
|
func requireContactAccess(t *testing.T, got domain.InvitationContactAccessView, status domain.InvitationContactAccess, fields ...domain.ContactField) {
|
|
t.Helper()
|
|
if got.Status != status || !slices.Equal(got.Fields, fields) {
|
|
t.Fatalf("contact access = %+v, want status %q with fields %v", got, status, fields)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationAccessIsNotGrantedBeforeAnAcceptance(t *testing.T) {
|
|
test := newHarness(t)
|
|
sent, _ := test.deliveredTo(t)
|
|
|
|
requireContactAccess(t, organizationAccess(t, test, sent.ID), domain.InvitationContactAccessNone)
|
|
page, err := test.service.List(t.Context(), ListCommand{ActorID: test.actorID, OrganizationID: test.organizationID, Limit: 10})
|
|
if err != nil || len(page.Items) != 1 {
|
|
t.Fatalf("List() = %d items, %v; want the delivered invitation", len(page.Items), err)
|
|
}
|
|
requireContactAccess(t, page.Items[0].ContactAccess, domain.InvitationContactAccessNone)
|
|
}
|
|
|
|
func TestOrganizationAccessIsAvailableWithTheGrantedKindsAfterAnAcceptance(t *testing.T) {
|
|
test := newHarness(t)
|
|
sent, _ := acceptedForContacts(t, test)
|
|
|
|
requireContactAccess(t, organizationAccess(t, test, sent.ID), domain.InvitationContactAccessAvailable, domain.ContactFieldEmail)
|
|
page, err := test.service.List(t.Context(), ListCommand{ActorID: test.actorID, OrganizationID: test.organizationID, Limit: 10})
|
|
if err != nil || len(page.Items) != 1 {
|
|
t.Fatalf("List() = %d items, %v; want the accepted invitation", len(page.Items), err)
|
|
}
|
|
requireContactAccess(t, page.Items[0].ContactAccess, domain.InvitationContactAccessAvailable, domain.ContactFieldEmail)
|
|
}
|
|
|
|
// TestOrganizationAccessAgreesWithTheContactRead checks each state of a grant
|
|
// against the read: the view claims available exactly when the read answers.
|
|
func TestOrganizationAccessAgreesWithTheContactRead(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
change func(t *testing.T, test *harness, candidateID uuid.UUID, sent domain.OrganizationInvitationView)
|
|
want domain.InvitationContactAccess
|
|
}{
|
|
{
|
|
name: "no change",
|
|
change: func(*testing.T, *harness, uuid.UUID, domain.OrganizationInvitationView) {},
|
|
want: domain.InvitationContactAccessAvailable,
|
|
},
|
|
{
|
|
name: "the C4 revocation stamps the grant and its record",
|
|
change: func(t *testing.T, test *harness, _ uuid.UUID, sent domain.OrganizationInvitationView) {
|
|
t.Helper()
|
|
stored := test.transactor.snapshot().grants[sent.ID].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
|
|
delete(state.consentRecords, sent.ID)
|
|
})
|
|
},
|
|
want: domain.InvitationContactAccessClosed,
|
|
},
|
|
{
|
|
name: "the C4 record is no longer current while the grant is active",
|
|
change: func(_ *testing.T, test *harness, _ uuid.UUID, sent domain.OrganizationInvitationView) {
|
|
test.setState(func(state *testState) { delete(state.consentRecords, sent.ID) })
|
|
},
|
|
want: domain.InvitationContactAccessClosed,
|
|
},
|
|
{
|
|
name: "the organization is hidden",
|
|
change: func(_ *testing.T, test *harness, candidateID uuid.UUID, _ domain.OrganizationInvitationView) {
|
|
test.setState(func(state *testState) { state.hidden[[2]uuid.UUID{candidateID, test.organizationID}] = true })
|
|
},
|
|
want: domain.InvitationContactAccessClosed,
|
|
},
|
|
{
|
|
name: "the publication is closed",
|
|
change: func(_ *testing.T, test *harness, candidateID uuid.UUID, _ domain.OrganizationInvitationView) {
|
|
test.setState(func(state *testState) { delete(state.publications, candidateID) })
|
|
},
|
|
want: domain.InvitationContactAccessClosed,
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
test := newHarness(t)
|
|
sent, candidateID := acceptedForContacts(t, test)
|
|
tc.change(t, test, candidateID, sent)
|
|
|
|
access := organizationAccess(t, test, sent.ID)
|
|
if access.Status != tc.want {
|
|
t.Fatalf("contact access = %+v, want %q", access, tc.want)
|
|
}
|
|
if access.Status == domain.InvitationContactAccessClosed && len(access.Fields) != 0 {
|
|
t.Fatalf("closed access lists fields %v", access.Fields)
|
|
}
|
|
_, readErr := test.service.ReadContacts(t.Context(), test.actorID, test.organizationID, sent.ID)
|
|
readable := readErr == nil
|
|
if readErr != nil && !errors.Is(readErr, outbound.ErrContactGrantNotFound) {
|
|
t.Fatalf("ReadContacts() error = %v, want nil or not found", readErr)
|
|
}
|
|
if readable != (access.Status == domain.InvitationContactAccessAvailable) {
|
|
t.Fatalf("read succeeds = %t while the view says %q", readable, access.Status)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOrganizationAccessOpensOnlyAGrantThatWasBlockedFromTheStart(t *testing.T) {
|
|
test := newHarness(t)
|
|
sent, candidateID := test.deliveredTo(t)
|
|
test.setProfile(t, candidateID, domain.ContactFieldEmail)
|
|
test.setTime(sent.DeliveredAt.Add(time.Hour))
|
|
test.publish(candidateID, *sent.DeliveredAt)
|
|
test.setState(func(state *testState) { state.hidden[[2]uuid.UUID{candidateID, test.organizationID}] = true })
|
|
|
|
if _, err := test.service.AcceptMine(t.Context(), acceptCommand(candidateID, sent.ID, sent.Version, sent.TermsVersion,
|
|
"accept-blocked-access", domain.ContactFieldEmail)); err != nil {
|
|
t.Fatalf("AcceptMine() while hidden error = %v", err)
|
|
}
|
|
requireContactAccess(t, organizationAccess(t, test, sent.ID), domain.InvitationContactAccessClosed)
|
|
|
|
test.setState(func(state *testState) { delete(state.hidden, [2]uuid.UUID{candidateID, test.organizationID}) })
|
|
requireContactAccess(t, organizationAccess(t, test, sent.ID), domain.InvitationContactAccessAvailable, domain.ContactFieldEmail)
|
|
}
|
|
|
|
func TestOrganizationAccessStaysClosedForARevokedGrantAfterItIsVisibleAgain(t *testing.T) {
|
|
test := newHarness(t)
|
|
sent, candidateID := acceptedForContacts(t, test)
|
|
revokedAt := sent.DeliveredAt.Add(2 * time.Hour)
|
|
stored := test.transactor.snapshot().grants[sent.ID].Stored()
|
|
stored.RevokedAt, stored.RevocationReason = &revokedAt, string(domain.ContactRevocationVisibilityClosed)
|
|
revoked, err := domain.RestoreContactGrant(stored)
|
|
if err != nil {
|
|
t.Fatalf("RestoreContactGrant() error = %v", err)
|
|
}
|
|
test.setState(func(state *testState) { state.grants[sent.ID] = revoked })
|
|
requireContactAccess(t, organizationAccess(t, test, sent.ID), domain.InvitationContactAccessClosed)
|
|
|
|
// Publishing again and unhiding never reopen a revoked grant.
|
|
test.publish(candidateID, revokedAt.Add(time.Hour))
|
|
requireContactAccess(t, organizationAccess(t, test, sent.ID), domain.InvitationContactAccessClosed)
|
|
}
|
|
|
|
func TestOrganizationAccessIsNotGrantedForADeclinedOrAnExpiredInvitation(t *testing.T) {
|
|
t.Run("declined", func(t *testing.T) {
|
|
test := newHarness(t)
|
|
sent, candidateID := test.deliveredTo(t)
|
|
if _, err := test.service.DeclineMine(t.Context(), declineCommand(candidateID, sent.ID, sent.Version,
|
|
"decline-access", domain.InvitationDeclineOther, nil)); err != nil {
|
|
t.Fatalf("DeclineMine() error = %v", err)
|
|
}
|
|
requireContactAccess(t, organizationAccess(t, test, sent.ID), domain.InvitationContactAccessNone)
|
|
})
|
|
t.Run("expired", func(t *testing.T) {
|
|
test := newHarness(t)
|
|
sent, candidateID := test.deliveredTo(t)
|
|
test.setTime(*sent.ResponseDeadlineAt)
|
|
if expired, err := test.service.GetMine(t.Context(), candidateID, sent.ID); err != nil || expired.State != domain.InvitationExpired {
|
|
t.Fatalf("GetMine() at the deadline = %v, %v; want expired", expired.State, err)
|
|
}
|
|
requireContactAccess(t, organizationAccess(t, test, sent.ID), domain.InvitationContactAccessNone)
|
|
})
|
|
}
|
|
|
|
func TestOrganizationAccessOfAnotherOrganizationIsNotFound(t *testing.T) {
|
|
test := newHarness(t)
|
|
sent, _ := acceptedForContacts(t, test)
|
|
other := uuid.Must(uuid.NewV7())
|
|
if _, err := test.service.Get(t.Context(), test.actorID, other, sent.ID); !errors.Is(err, outbound.ErrOrganizationNotFound) {
|
|
t.Fatalf("Get(other organization) error = %v, want not found", err)
|
|
}
|
|
}
|