602 lines
25 KiB
Go
602 lines
25 KiB
Go
package notification
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
func TestInboxServiceListRechecksContentAndFallsBackSafely(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
|
first := fixture.add(t, fixture.entryInput())
|
|
second := fixture.add(t, fixture.entryInput())
|
|
fixture.resolver.unavailable[second.EventID()] = true
|
|
otherRecipient := fixtureEntryInput(uuid.New(), fixture.organizationID)
|
|
fixture.add(t, otherRecipient)
|
|
|
|
page, err := fixture.service.List(context.Background(), InboxListRequest{RecipientUserID: fixture.user.ID, Limit: 10})
|
|
if err != nil {
|
|
t.Fatalf("List() error = %v", err)
|
|
}
|
|
if len(page.Items) != 2 {
|
|
t.Fatalf("List() returned %d items, want only the recipient's 2", len(page.Items))
|
|
}
|
|
byID := map[uuid.UUID]InboxItem{}
|
|
for _, item := range page.Items {
|
|
byID[item.Entry.EventID()] = item
|
|
}
|
|
if item := byID[first.EventID()]; item.Content == nil || item.ContentUnavailable {
|
|
t.Fatalf("visible item = %+v, want current content", item)
|
|
}
|
|
if item := byID[second.EventID()]; item.Content != nil || !item.ContentUnavailable {
|
|
t.Fatalf("hidden item = %+v, want safe fallback without source content", item)
|
|
}
|
|
if fixture.resolver.calls != 2 {
|
|
t.Fatalf("resolver calls = %d, want one current visibility check per returned item", fixture.resolver.calls)
|
|
}
|
|
}
|
|
|
|
func TestInboxServiceAuthorizesTheCabinetInTheReadTransaction(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("the candidate inbox refuses an employer", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixture := newInboxFixture(t, domain.ScenarioEmployer)
|
|
_, err := fixture.service.List(context.Background(), InboxListRequest{RecipientUserID: fixture.user.ID, Limit: 10})
|
|
if !errors.Is(err, ErrInboxForbidden) {
|
|
t.Fatalf("List(employer in the candidate cabinet) error = %v, want ErrInboxForbidden", err)
|
|
}
|
|
if fixture.repository.listCalls != 0 {
|
|
t.Fatal("the repository was read before the account was authorized")
|
|
}
|
|
})
|
|
t.Run("the organization inbox refuses a candidate", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
|
_, err := fixture.service.List(context.Background(), InboxListRequest{
|
|
RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, Limit: 10,
|
|
})
|
|
if !errors.Is(err, ErrInboxForbidden) {
|
|
t.Fatalf("List(candidate in the organization cabinet) error = %v, want ErrInboxForbidden", err)
|
|
}
|
|
})
|
|
t.Run("an unknown or unverified account is refused", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
|
if _, err := fixture.service.List(context.Background(), InboxListRequest{RecipientUserID: uuid.New(), Limit: 10}); !errors.Is(err, ErrInboxForbidden) {
|
|
t.Fatalf("List(unknown account) error = %v, want ErrInboxForbidden", err)
|
|
}
|
|
fixture.user.EmailVerifiedAt = nil
|
|
fixture.users[fixture.user.ID] = fixture.user
|
|
if _, err := fixture.service.List(context.Background(), InboxListRequest{RecipientUserID: fixture.user.ID, Limit: 10}); !errors.Is(err, ErrInboxForbidden) {
|
|
t.Fatalf("List(unverified account) error = %v, want ErrInboxForbidden", err)
|
|
}
|
|
})
|
|
t.Run("a revoked membership answers like an unknown organization", func(t *testing.T) {
|
|
t.Parallel()
|
|
fixture := newInboxFixture(t, domain.ScenarioEmployer)
|
|
fixture.addMembership(t, domain.OrganizationRoleOwner, domain.MembershipStateActive)
|
|
entry := fixture.add(t, fixture.entryInput())
|
|
request := InboxListRequest{RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, Limit: 10}
|
|
page, err := fixture.service.List(context.Background(), request)
|
|
if err != nil || len(page.Items) != 1 {
|
|
t.Fatalf("List(active member) = %d items, %v, want 1 item", len(page.Items), err)
|
|
}
|
|
fixture.addMembership(t, domain.OrganizationRoleOwner, domain.MembershipStateRemoved)
|
|
if _, err := fixture.service.List(context.Background(), request); !errors.Is(err, outbound.ErrOrganizationNotFound) {
|
|
t.Fatalf("List(revoked member) error = %v, want ErrOrganizationNotFound", err)
|
|
}
|
|
_, err = fixture.service.MarkRead(context.Background(), InboxMarkReadRequest{
|
|
RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, EventID: entry.EventID(),
|
|
})
|
|
if !errors.Is(err, outbound.ErrOrganizationNotFound) {
|
|
t.Fatalf("MarkRead(revoked member) error = %v, want ErrOrganizationNotFound", err)
|
|
}
|
|
if _, isRead := fixture.repository.entries[0].ReadAt(); isRead {
|
|
t.Fatal("a revoked member marked an item read")
|
|
}
|
|
unknown := uuid.New()
|
|
_, err = fixture.service.List(context.Background(), InboxListRequest{RecipientUserID: fixture.user.ID, OrganizationID: &unknown, Limit: 10})
|
|
if !errors.Is(err, outbound.ErrOrganizationNotFound) {
|
|
t.Fatalf("List(unknown organization) error = %v, want the same ErrOrganizationNotFound", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestInboxServiceOrganizationCabinetServesEveryRoleItsOwnItems(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, role := range []domain.OrganizationRole{
|
|
domain.OrganizationRoleOwner, domain.OrganizationRoleRecruiter,
|
|
domain.OrganizationRoleHiringManager, domain.OrganizationRoleTechnicalExpert,
|
|
} {
|
|
t.Run(string(role), func(t *testing.T) {
|
|
t.Parallel()
|
|
fixture := newInboxFixture(t, domain.ScenarioEmployer)
|
|
fixture.addMembership(t, role, domain.MembershipStateActive)
|
|
own := fixture.add(t, fixture.entryInput())
|
|
otherMember := fixture.add(t, fixtureEntryInput(uuid.New(), fixture.organizationID))
|
|
otherOrganization := fixture.add(t, fixtureEntryInput(fixture.user.ID, uuid.New()))
|
|
|
|
page, err := fixture.service.List(context.Background(), InboxListRequest{
|
|
RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, Limit: 10,
|
|
})
|
|
if err != nil || len(page.Items) != 1 || page.Items[0].Entry.EventID() != own.EventID() {
|
|
t.Fatalf("List(%s) = %d items, %v, want only its own item", role, len(page.Items), err)
|
|
}
|
|
if fixture.locks == 0 {
|
|
t.Fatal("the membership was not locked for the read")
|
|
}
|
|
for _, foreign := range []domain.NotificationInboxEntry{otherMember, otherOrganization} {
|
|
_, err := fixture.service.MarkRead(context.Background(), InboxMarkReadRequest{
|
|
RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, EventID: foreign.EventID(),
|
|
})
|
|
if !errors.Is(err, outbound.ErrNotificationInboxEntryNotFound) {
|
|
t.Fatalf("MarkRead(%s, foreign item) error = %v, want ErrNotificationInboxEntryNotFound", role, err)
|
|
}
|
|
}
|
|
if _, err := fixture.service.MarkRead(context.Background(), InboxMarkReadRequest{
|
|
RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, EventID: own.EventID(),
|
|
}); err != nil {
|
|
t.Fatalf("MarkRead(%s, own item) error = %v", role, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInboxServiceOrganizationCabinetNeedsAConfirmedEmail(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fixture := newInboxFixture(t, domain.ScenarioEmployer)
|
|
fixture.addMembership(t, domain.OrganizationRoleOwner, domain.MembershipStateActive)
|
|
fixture.user.EmailVerifiedAt = nil
|
|
fixture.users[fixture.user.ID] = fixture.user
|
|
_, err := fixture.service.List(context.Background(), InboxListRequest{
|
|
RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, Limit: 10,
|
|
})
|
|
if !errors.Is(err, ErrInboxEmailNotVerified) {
|
|
t.Fatalf("List(unverified employer) error = %v, want ErrInboxEmailNotVerified", err)
|
|
}
|
|
}
|
|
|
|
func TestInboxServiceListRejectsRepositoryRowsOutsideRecipientScope(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
|
fixture.repository.ignoreListScope = true
|
|
fixture.add(t, fixtureEntryInput(uuid.New(), fixture.organizationID))
|
|
_, err := fixture.service.List(context.Background(), InboxListRequest{RecipientUserID: fixture.user.ID, Limit: 10})
|
|
if !errors.Is(err, ErrInboxUnavailable) {
|
|
t.Fatalf("List(out-of-scope repository row) error = %v, want ErrInboxUnavailable", err)
|
|
}
|
|
}
|
|
|
|
func TestInboxServiceMarkReadPersistsAndKeepsFirstTime(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
firstRead := time.Date(2026, time.October, 8, 13, 0, 0, 1000, time.UTC)
|
|
clockCalls := 0
|
|
fixture := newInboxFixtureWithClock(t, domain.ScenarioCandidate, func() time.Time {
|
|
clockCalls++
|
|
return firstRead.Add(time.Duration(clockCalls-1) * time.Hour)
|
|
})
|
|
entry := fixture.add(t, fixture.entryInput())
|
|
for range 2 {
|
|
read, err := fixture.service.MarkRead(context.Background(), InboxMarkReadRequest{
|
|
RecipientUserID: fixture.user.ID, EventID: entry.EventID(),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("MarkRead() error = %v", err)
|
|
}
|
|
got, isRead := read.ReadAt()
|
|
if !isRead || !got.Equal(firstRead.UTC().Truncate(time.Microsecond)) {
|
|
t.Fatalf("ReadAt() = %s, %t, want the first read time", got, isRead)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInboxServiceMarkReadRefusesAnotherRecipientsItem(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
|
foreign := fixture.add(t, fixtureEntryInput(uuid.New(), fixture.organizationID))
|
|
_, err := fixture.service.MarkRead(context.Background(), InboxMarkReadRequest{
|
|
RecipientUserID: fixture.user.ID, EventID: foreign.EventID(),
|
|
})
|
|
if !errors.Is(err, outbound.ErrNotificationInboxEntryNotFound) {
|
|
t.Fatalf("MarkRead(foreign item) error = %v, want ErrNotificationInboxEntryNotFound", err)
|
|
}
|
|
if _, isRead := fixture.repository.entries[0].ReadAt(); isRead {
|
|
t.Fatal("another recipient's item was marked read")
|
|
}
|
|
}
|
|
|
|
func TestNewInboxContentRejectsExternalOrAmbiguousLinks(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
path string
|
|
}{
|
|
{name: "external URL", path: "https://example.test/invitation"},
|
|
{name: "protocol-relative URL", path: "//example.test/invitation"},
|
|
{name: "query string", path: "/candidate/invitations?token=secret"},
|
|
{name: "fragment", path: "/candidate/invitations#current"},
|
|
{name: "backslash", path: `/candidate\\invitations`},
|
|
{name: "escape", path: "/candidate/%2e%2e/invitations"},
|
|
{name: "dot segment", path: "/candidate/../invitations"},
|
|
{name: "too long", path: "/" + strings.Repeat("a", 300)},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
if _, err := NewInboxContent("Приглашение", "Проверьте новое приглашение.", test.path); !errors.Is(err, ErrInboxUnavailable) {
|
|
t.Fatalf("NewInboxContent() error = %v, want ErrInboxUnavailable", err)
|
|
}
|
|
})
|
|
}
|
|
if _, err := NewInboxContent("Приглашение", "Проверьте новое приглашение.", "/candidate/invitations/01928f7a-3c4d-7e5f-8a9b-0c1d2e3f4a5b"); err != nil {
|
|
t.Fatalf("NewInboxContent(same-origin path) error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSyntheticNoticeResolverFollowsTheRecipientStanding(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
|
entry := fixture.add(t, fixture.entryInput())
|
|
unit := fixture.unit()
|
|
content, err := ResolveSyntheticNotice(context.Background(), unit, entry)
|
|
if err != nil {
|
|
t.Fatalf("ResolveSyntheticNotice(visible) error = %v", err)
|
|
}
|
|
if content.Title() != syntheticNoticeTitle || content.LinkPath() != "" || content.Summary() == "" {
|
|
t.Fatalf("synthetic content = %+v", content)
|
|
}
|
|
fixture.repository.organizationName = ""
|
|
if _, err := ResolveSyntheticNotice(context.Background(), unit, entry); !errors.Is(err, ErrInboxContentUnavailable) {
|
|
t.Fatalf("ResolveSyntheticNotice(not visible) error = %v, want ErrInboxContentUnavailable", err)
|
|
}
|
|
resolvers := NewInboxContentResolver(DefaultInboxSourceResolvers(true))
|
|
invitation := fixtureEntry(t, domain.NotificationInboxEntryInput{
|
|
EventID: uuid.New(), RecipientUserID: fixture.user.ID, OrganizationID: fixture.organizationID,
|
|
EventType: domain.NotificationInboxInvitationDelivered, SchemaVersion: 1,
|
|
SourceType: domain.NotificationInboxSourceInvitation, SourceID: uuid.New(),
|
|
OccurredAt: time.Date(2026, time.October, 8, 12, 0, 0, 0, time.UTC),
|
|
})
|
|
if _, err := resolvers.Resolve(context.Background(), unit, invitation); !errors.Is(err, ErrInboxContentUnavailable) {
|
|
t.Fatalf("Resolve(invitation the recipient cannot read) error = %v, want ErrInboxContentUnavailable", err)
|
|
}
|
|
}
|
|
|
|
func TestResolveInvitationNoticeNamesTheSenderOnlyWhileVisible(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
|
delivered := fixture.deliveredInvitation(t)
|
|
entry := fixtureEntry(t, domain.NotificationInboxEntryInput{
|
|
EventID: uuid.New(), RecipientUserID: fixture.user.ID, OrganizationID: fixture.organizationID,
|
|
EventType: domain.NotificationInboxInvitationDelivered, SchemaVersion: 1,
|
|
SourceType: domain.NotificationInboxSourceInvitation, SourceID: delivered.ID(),
|
|
OccurredAt: time.Date(2026, time.October, 8, 12, 0, 0, 0, time.UTC),
|
|
})
|
|
resolvers := NewInboxContentResolver(DefaultInboxSourceResolvers(false))
|
|
content, err := resolvers.Resolve(context.Background(), fixture.unit(), entry)
|
|
if err != nil {
|
|
t.Fatalf("Resolve(delivered invitation) error = %v", err)
|
|
}
|
|
if content.Title() != invitationNoticeTitle || content.LinkPath() != candidateInvitationsPath ||
|
|
!strings.Contains(content.Summary(), "«Север»") || !strings.Contains(content.Summary(), "«Inbox role»") {
|
|
t.Fatalf("invitation content = %q / %q / %q", content.Title(), content.Summary(), content.LinkPath())
|
|
}
|
|
if strings.Contains(content.Summary(), fixture.user.ID.String()) {
|
|
t.Fatal("invitation content names the candidate")
|
|
}
|
|
|
|
// Another recipient and another organization context find nothing.
|
|
foreign := fixtureEntry(t, domain.NotificationInboxEntryInput{
|
|
EventID: uuid.New(), RecipientUserID: uuid.New(), OrganizationID: fixture.organizationID,
|
|
EventType: domain.NotificationInboxInvitationDelivered, SchemaVersion: 1,
|
|
SourceType: domain.NotificationInboxSourceInvitation, SourceID: delivered.ID(),
|
|
OccurredAt: time.Date(2026, time.October, 8, 12, 0, 0, 0, time.UTC),
|
|
})
|
|
if _, err := ResolveInvitationNotice(context.Background(), fixture.unit(), foreign); !errors.Is(err, ErrInboxContentUnavailable) {
|
|
t.Fatalf("ResolveInvitationNotice(other recipient) error = %v, want ErrInboxContentUnavailable", err)
|
|
}
|
|
otherContext := fixtureEntry(t, domain.NotificationInboxEntryInput{
|
|
EventID: uuid.New(), RecipientUserID: fixture.user.ID, OrganizationID: uuid.New(),
|
|
EventType: domain.NotificationInboxInvitationDelivered, SchemaVersion: 1,
|
|
SourceType: domain.NotificationInboxSourceInvitation, SourceID: delivered.ID(),
|
|
OccurredAt: time.Date(2026, time.October, 8, 12, 0, 0, 0, time.UTC),
|
|
})
|
|
if _, err := ResolveInvitationNotice(context.Background(), fixture.unit(), otherContext); !errors.Is(err, ErrInboxContentUnavailable) {
|
|
t.Fatalf("ResolveInvitationNotice(other organization) error = %v, want ErrInboxContentUnavailable", err)
|
|
}
|
|
// A hidden organization is no longer named.
|
|
fixture.repository.organizationName = ""
|
|
if _, err := ResolveInvitationNotice(context.Background(), fixture.unit(), entry); !errors.Is(err, ErrInboxContentUnavailable) {
|
|
t.Fatalf("ResolveInvitationNotice(hidden sender) error = %v, want ErrInboxContentUnavailable", err)
|
|
}
|
|
}
|
|
|
|
type inboxFixture struct {
|
|
service *InboxService
|
|
invitations map[uuid.UUID]domain.Invitation
|
|
repository *memoryInboxRepository
|
|
resolver *memoryInboxResolver
|
|
users map[uuid.UUID]domain.User
|
|
memberships map[uuid.UUID]domain.Membership
|
|
user domain.User
|
|
organizationID uuid.UUID
|
|
locks int
|
|
}
|
|
|
|
func newInboxFixture(t *testing.T, scenario domain.Scenario) *inboxFixture {
|
|
t.Helper()
|
|
return newInboxFixtureWithClock(t, scenario, time.Now)
|
|
}
|
|
|
|
func newInboxFixtureWithClock(t *testing.T, scenario domain.Scenario, clock func() time.Time) *inboxFixture {
|
|
t.Helper()
|
|
verifiedAt := time.Date(2026, time.October, 1, 10, 0, 0, 0, time.UTC)
|
|
fixture := &inboxFixture{
|
|
invitations: map[uuid.UUID]domain.Invitation{},
|
|
repository: &memoryInboxRepository{organizationName: "Север"},
|
|
resolver: &memoryInboxResolver{unavailable: map[uuid.UUID]bool{}},
|
|
users: map[uuid.UUID]domain.User{},
|
|
memberships: map[uuid.UUID]domain.Membership{},
|
|
user: domain.User{ID: uuid.New(), Scenario: scenario, EmailVerifiedAt: &verifiedAt},
|
|
organizationID: uuid.New(),
|
|
}
|
|
fixture.users[fixture.user.ID] = fixture.user
|
|
service, err := NewInboxService(inboxTestTransactor{fixture: fixture}, fixture.resolver, clock)
|
|
if err != nil {
|
|
t.Fatalf("NewInboxService() error = %v", err)
|
|
}
|
|
fixture.service = service
|
|
return fixture
|
|
}
|
|
|
|
func (fixture *inboxFixture) unit() outbound.UnitOfWork { return inboxTestUnit{fixture: fixture} }
|
|
|
|
// deliveredInvitation stores a delivered invitation of the fixture
|
|
// organization addressed to the fixture user.
|
|
func (fixture *inboxFixture) deliveredInvitation(t *testing.T) domain.Invitation {
|
|
t.Helper()
|
|
policyID, err := domain.NewPolicyVersionID()
|
|
if err != nil {
|
|
t.Fatalf("NewPolicyVersionID() error = %v", err)
|
|
}
|
|
none := domain.InvitationPackageItem{Status: domain.InvitationPackageNotProvided}
|
|
at := time.Date(2026, time.October, 8, 11, 0, 0, 0, time.UTC)
|
|
draft, err := domain.NewInvitationDraft(domain.NewInvitationDraftInput{
|
|
ID: uuid.New(), OrganizationID: fixture.organizationID, CandidateID: fixture.user.ID, ActorID: uuid.New(),
|
|
PolicyVersionID: policyID, At: at,
|
|
Content: domain.InvitationDraftContent{ResponsePeriod: 7, Terms: domain.InvitationTerms{
|
|
RoleTitle: "Inbox role", Specialization: domain.SpecializationBackend, Grade: domain.GradeMiddle,
|
|
OfferType: domain.InvitationOfferFullTime, Tasks: []string{"Read the inbox"}, Team: "Inbox team",
|
|
TechStack: []string{"Go"}, WorkFormats: []domain.NeedWorkFormat{domain.NeedWorkFormatRemote},
|
|
CommunicationMethod: domain.InvitationCommunicationMethod{Channel: domain.InvitationChannelEmail},
|
|
Salary: domain.SalaryRange{SalaryFrom: 100_000, SalaryTo: 140_000, Currency: "RUB", Period: "month", TaxBasis: "net"},
|
|
Package: domain.InvitationPackage{Bonus: none, Equipment: none, Training: none, HealthInsurance: none},
|
|
}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewInvitationDraft() error = %v", err)
|
|
}
|
|
delivered, err := draft.Deliver(at.Add(time.Minute))
|
|
if err != nil {
|
|
t.Fatalf("Deliver() error = %v", err)
|
|
}
|
|
fixture.invitations[delivered.ID()] = delivered
|
|
return delivered
|
|
}
|
|
|
|
func (fixture *inboxFixture) entryInput() domain.NotificationInboxEntryInput {
|
|
return fixtureEntryInput(fixture.user.ID, fixture.organizationID)
|
|
}
|
|
|
|
func (fixture *inboxFixture) add(t *testing.T, input domain.NotificationInboxEntryInput) domain.NotificationInboxEntry {
|
|
t.Helper()
|
|
entry := fixtureEntry(t, input)
|
|
fixture.repository.entries = append(fixture.repository.entries, entry)
|
|
return entry
|
|
}
|
|
|
|
func (fixture *inboxFixture) addMembership(t *testing.T, role domain.OrganizationRole, state domain.MembershipState) {
|
|
t.Helper()
|
|
now := time.Date(2026, time.October, 1, 10, 0, 0, 0, time.UTC)
|
|
membership, err := domain.RestoreMembership(domain.StoredMembership{
|
|
ID: uuid.New(), OrganizationID: fixture.organizationID, UserID: fixture.user.ID,
|
|
Role: string(role), State: string(state), CreatedAt: now, UpdatedAt: now,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("RestoreMembership() error = %v", err)
|
|
}
|
|
fixture.memberships[fixture.user.ID] = membership
|
|
}
|
|
|
|
func fixtureEntryInput(recipientID, organizationID uuid.UUID) domain.NotificationInboxEntryInput {
|
|
return domain.NotificationInboxEntryInput{
|
|
EventID: uuid.New(), RecipientUserID: recipientID, OrganizationID: organizationID,
|
|
EventType: domain.NotificationInboxSyntheticDelivered, SchemaVersion: 1,
|
|
SourceType: domain.NotificationInboxSourceOrganization, SourceID: organizationID,
|
|
OccurredAt: time.Date(2026, time.October, 8, 12, 0, 0, 0, time.UTC),
|
|
}
|
|
}
|
|
|
|
func fixtureEntry(t *testing.T, input domain.NotificationInboxEntryInput) domain.NotificationInboxEntry {
|
|
t.Helper()
|
|
entry, err := domain.NewNotificationInboxEntry(input)
|
|
if err != nil {
|
|
t.Fatalf("NewNotificationInboxEntry() error = %v", err)
|
|
}
|
|
return entry
|
|
}
|
|
|
|
type inboxTestTransactor struct{ fixture *inboxFixture }
|
|
|
|
func (transactor inboxTestTransactor) WithinTransaction(ctx context.Context, fn func(context.Context, outbound.UnitOfWork) error) error {
|
|
return fn(ctx, transactor.fixture.unit())
|
|
}
|
|
|
|
type inboxTestUnit struct {
|
|
outbound.UnitOfWork
|
|
|
|
fixture *inboxFixture
|
|
}
|
|
|
|
func (unit inboxTestUnit) Users() outbound.UserRepository {
|
|
return inboxTestUsers{fixture: unit.fixture}
|
|
}
|
|
func (unit inboxTestUnit) Organizations() outbound.OrganizationRepository {
|
|
return inboxTestOrganizations{fixture: unit.fixture}
|
|
}
|
|
func (unit inboxTestUnit) NotificationInbox() outbound.NotificationInboxRepository {
|
|
return unit.fixture.repository
|
|
}
|
|
func (unit inboxTestUnit) Invitations() outbound.InvitationRepository {
|
|
return inboxTestInvitations{fixture: unit.fixture}
|
|
}
|
|
|
|
type inboxTestInvitations struct {
|
|
outbound.InvitationRepository
|
|
|
|
fixture *inboxFixture
|
|
}
|
|
|
|
// GetForCandidate answers like the real repository: only a delivered
|
|
// invitation addressed to the candidate is found.
|
|
func (repository inboxTestInvitations) GetForCandidate(_ context.Context, candidateID, invitationID uuid.UUID) (domain.Invitation, error) {
|
|
invitation, ok := repository.fixture.invitations[invitationID]
|
|
if !ok || invitation.CandidateID() != candidateID || !invitation.IsDelivered() {
|
|
return domain.Invitation{}, outbound.ErrInvitationNotFound
|
|
}
|
|
return invitation, nil
|
|
}
|
|
|
|
type inboxTestUsers struct {
|
|
outbound.UserRepository
|
|
|
|
fixture *inboxFixture
|
|
}
|
|
|
|
func (repository inboxTestUsers) GetByID(_ context.Context, id uuid.UUID) (domain.User, error) {
|
|
user, ok := repository.fixture.users[id]
|
|
if !ok {
|
|
return domain.User{}, outbound.ErrUserNotFound
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
type inboxTestOrganizations struct {
|
|
outbound.OrganizationRepository
|
|
|
|
fixture *inboxFixture
|
|
}
|
|
|
|
// LockActiveMembership answers like the real repository: only an active
|
|
// membership of the account in the organization is returned.
|
|
func (repository inboxTestOrganizations) LockActiveMembership(_ context.Context, organizationID, userID uuid.UUID) (domain.Membership, error) {
|
|
repository.fixture.locks++
|
|
membership, ok := repository.fixture.memberships[userID]
|
|
if !ok || membership.OrganizationID() != organizationID || !membership.GrantsAccess() {
|
|
return domain.Membership{}, outbound.ErrOrganizationNotFound
|
|
}
|
|
return membership, nil
|
|
}
|
|
|
|
type memoryInboxRepository struct {
|
|
entries []domain.NotificationInboxEntry
|
|
ignoreListScope bool
|
|
listCalls int
|
|
organizationName string
|
|
storeErr error
|
|
}
|
|
|
|
func (repository *memoryInboxRepository) Store(_ context.Context, entry domain.NotificationInboxEntry) (bool, error) {
|
|
if repository.storeErr != nil {
|
|
return false, repository.storeErr
|
|
}
|
|
for _, existing := range repository.entries {
|
|
if existing.RecipientUserID() != entry.RecipientUserID() || existing.EventID() != entry.EventID() {
|
|
continue
|
|
}
|
|
if !sameInboxEvent(existing, entry) {
|
|
return false, outbound.ErrNotificationInboxEventConflict
|
|
}
|
|
return false, nil
|
|
}
|
|
repository.entries = append(repository.entries, entry)
|
|
return true, nil
|
|
}
|
|
|
|
func (repository *memoryInboxRepository) List(_ context.Context, query outbound.NotificationInboxListQuery) (outbound.NotificationInboxPage, error) {
|
|
repository.listCalls++
|
|
page := outbound.NotificationInboxPage{}
|
|
for _, entry := range repository.entries {
|
|
if !repository.ignoreListScope && (entry.RecipientUserID() != query.RecipientUserID || (query.OrganizationID != nil && entry.OrganizationID() != *query.OrganizationID)) {
|
|
continue
|
|
}
|
|
page.Items = append(page.Items, entry)
|
|
}
|
|
return page, nil
|
|
}
|
|
|
|
func (repository *memoryInboxRepository) MarkRead(
|
|
_ context.Context,
|
|
recipientUserID uuid.UUID,
|
|
organizationID *uuid.UUID,
|
|
eventID uuid.UUID,
|
|
at time.Time,
|
|
) (domain.NotificationInboxEntry, error) {
|
|
for index, entry := range repository.entries {
|
|
if entry.RecipientUserID() != recipientUserID || entry.EventID() != eventID ||
|
|
(organizationID != nil && entry.OrganizationID() != *organizationID) {
|
|
continue
|
|
}
|
|
read, err := entry.MarkRead(at)
|
|
if err != nil {
|
|
return domain.NotificationInboxEntry{}, err
|
|
}
|
|
repository.entries[index] = read
|
|
return read, nil
|
|
}
|
|
return domain.NotificationInboxEntry{}, outbound.ErrNotificationInboxEntryNotFound
|
|
}
|
|
|
|
func (repository *memoryInboxRepository) OrganizationNameForRecipient(context.Context, uuid.UUID, uuid.UUID) (string, error) {
|
|
if repository.organizationName == "" {
|
|
return "", outbound.ErrNotificationInboxSourceNotVisible
|
|
}
|
|
return repository.organizationName, nil
|
|
}
|
|
|
|
func sameInboxEvent(left, right domain.NotificationInboxEntry) bool {
|
|
return left.OrganizationID() == right.OrganizationID() && left.EventType() == right.EventType() &&
|
|
left.SchemaVersion() == right.SchemaVersion() && left.SourceType() == right.SourceType() &&
|
|
left.SourceID() == right.SourceID() && left.OccurredAt().Equal(right.OccurredAt())
|
|
}
|
|
|
|
type memoryInboxResolver struct {
|
|
unavailable map[uuid.UUID]bool
|
|
calls int
|
|
}
|
|
|
|
func (resolver *memoryInboxResolver) Resolve(_ context.Context, _ outbound.UnitOfWork, entry domain.NotificationInboxEntry) (InboxContent, error) {
|
|
resolver.calls++
|
|
if resolver.unavailable[entry.EventID()] {
|
|
return InboxContent{}, ErrInboxContentUnavailable
|
|
}
|
|
return NewInboxContent("Приглашение", "Проверьте новое приглашение.", "/candidate/invitations")
|
|
}
|