Files
lincodex/backend/internal/adapter/primary/http/v1/notifications_test.go
T

264 lines
11 KiB
Go

package v1
import (
"context"
"errors"
"reflect"
"testing"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/adapter/primary/http/api"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
"lincodex/backend/internal/usecase/notification"
)
type notificationOperationsStub struct {
page notification.InboxPage
entry domain.NotificationInboxEntry
err error
listRequest notification.InboxListRequest
markRequest notification.InboxMarkReadRequest
}
func (stub *notificationOperationsStub) List(_ context.Context, request notification.InboxListRequest) (notification.InboxPage, error) {
stub.listRequest = request
return stub.page, stub.err
}
func (stub *notificationOperationsStub) MarkRead(_ context.Context, request notification.InboxMarkReadRequest) (domain.NotificationInboxEntry, error) {
stub.markRequest = request
return stub.entry, stub.err
}
func notificationTestEntry(t *testing.T, recipientID, organizationID uuid.UUID, at time.Time) domain.NotificationInboxEntry {
t.Helper()
entry, err := domain.NewNotificationInboxEntry(domain.NotificationInboxEntryInput{
EventID: uuid.New(), RecipientUserID: recipientID, OrganizationID: organizationID,
EventType: domain.NotificationInboxSyntheticDelivered, SchemaVersion: 1,
SourceType: domain.NotificationInboxSourceOrganization, SourceID: organizationID, OccurredAt: at,
})
if err != nil {
t.Fatalf("NewNotificationInboxEntry() error = %v", err)
}
return entry
}
func TestListMyNotificationsMapsContentAndFallbackAndBindsTheCursor(t *testing.T) {
userID, organizationID := uuid.New(), uuid.New()
at := time.Date(2026, 10, 9, 8, 30, 0, 123000, time.UTC)
withContent := notificationTestEntry(t, userID, organizationID, at)
content, err := notification.NewInboxContent("Тестовое уведомление", "Проверка доставки.", "/candidate/invitations")
if err != nil {
t.Fatalf("NewInboxContent() error = %v", err)
}
withoutContent := notificationTestEntry(t, userID, organizationID, at.Add(-time.Hour))
read, err := withoutContent.MarkRead(at)
if err != nil {
t.Fatalf("MarkRead() error = %v", err)
}
stub := &notificationOperationsStub{page: notification.InboxPage{
Items: []notification.InboxItem{
{Entry: withContent, Content: &content},
{Entry: read, ContentUnavailable: true},
},
HasMore: true,
}}
handler := NewNotificationHandler(stub)
ctx := privacyHTTPContext(userID, "")
response, err := handler.ListMyNotifications(ctx, api.ListMyNotificationsParams{Limit: api.NewOptInt32(2)})
if err != nil {
t.Fatalf("ListMyNotifications() error = %v", err)
}
page, ok := response.(*api.NotificationListHeaders)
if !ok || page.CacheControl != api.CacheControlNoStoreNoStore || len(page.Response.Items) != 2 {
t.Fatalf("ListMyNotifications() = %#v, want a no-store page of two items", response)
}
if stub.listRequest.RecipientUserID != userID || stub.listRequest.OrganizationID != nil || stub.listRequest.Limit != 2 {
t.Fatalf("list request = %+v, want the session account in the candidate cabinet", stub.listRequest)
}
first, second := page.Response.Items[0], page.Response.Items[1]
if first.ID != withContent.EventID() || first.Kind != api.NotificationKindSynthetic || !first.ReadAt.Null {
t.Fatalf("first item = %+v, want an unread synthetic item", first)
}
if first.Content.Null || first.Content.Value.Title != "Тестовое уведомление" || first.Content.Value.LinkPath.Null ||
first.Content.Value.LinkPath.Value != "/candidate/invitations" {
t.Fatalf("first content = %+v, want the resolved text and link", first.Content)
}
if !second.Content.Null || second.ReadAt.Null {
t.Fatalf("second item = %+v, want no content and a read time", second)
}
if !page.Response.Page.HasMore || page.Response.Page.NextCursor.Null {
t.Fatalf("page info = %+v, want a next cursor", page.Response.Page)
}
// The cursor continues only the same account in the same cabinet.
next := page.Response.Page.NextCursor.Value
if _, err := handler.ListMyNotifications(ctx, api.ListMyNotificationsParams{Cursor: api.NewOptString(next)}); err != nil {
t.Fatalf("ListMyNotifications(cursor) error = %v", err)
}
if stub.listRequest.After == nil || stub.listRequest.After.EventID != read.EventID() {
t.Fatalf("continuation = %+v, want the position of the last item", stub.listRequest.After)
}
foreign, err := handler.ListMyNotifications(privacyHTTPContext(uuid.New(), ""), api.ListMyNotificationsParams{Cursor: api.NewOptString(next)})
if err != nil {
t.Fatalf("ListMyNotifications(foreign cursor) error = %v", err)
}
if _, ok := foreign.(*api.ListMyNotificationsBadRequest); !ok {
t.Fatalf("ListMyNotifications(foreign cursor) = %#v, want 400", foreign)
}
// A cursor of the candidate cabinet cannot continue an organization cabinet.
crossed, err := handler.ListOrganizationNotifications(ctx, api.ListOrganizationNotificationsParams{
OrgID: organizationID, Cursor: api.NewOptString(next),
})
if err != nil {
t.Fatalf("ListOrganizationNotifications(candidate cursor) error = %v", err)
}
if _, ok := crossed.(*api.ListOrganizationNotificationsBadRequest); !ok {
t.Fatalf("ListOrganizationNotifications(candidate cursor) = %#v, want 400", crossed)
}
for _, limit := range []int32{0, 101} {
bad, err := handler.ListMyNotifications(ctx, api.ListMyNotificationsParams{Limit: api.NewOptInt32(limit)})
if err != nil {
t.Fatalf("ListMyNotifications(limit %d) error = %v", limit, err)
}
if _, ok := bad.(*api.ListMyNotificationsBadRequest); !ok {
t.Fatalf("ListMyNotifications(limit %d) = %#v, want 400", limit, bad)
}
}
}
func TestNotificationOperationsMapAccessFailuresWithoutRevealingObjects(t *testing.T) {
userID, organizationID, eventID := uuid.New(), uuid.New(), uuid.New()
ctx := privacyHTTPContext(userID, "")
for _, test := range []struct {
name string
err error
list func(*NotificationHandler) (any, error)
mark func(*NotificationHandler) (any, error)
want []any
code api.ProblemCode
}{
{
name: "an account of the other scenario",
err: notification.ErrInboxForbidden,
list: func(h *NotificationHandler) (any, error) {
return h.ListMyNotifications(ctx, api.ListMyNotificationsParams{})
},
mark: func(h *NotificationHandler) (any, error) {
return h.MarkMyNotificationRead(ctx, api.MarkMyNotificationReadParams{NotificationID: eventID})
},
want: []any{(*api.ListMyNotificationsForbidden)(nil), (*api.MarkMyNotificationReadForbidden)(nil)},
},
{
name: "no active membership",
err: outbound.ErrOrganizationNotFound,
list: func(h *NotificationHandler) (any, error) {
return h.ListOrganizationNotifications(ctx, api.ListOrganizationNotificationsParams{OrgID: organizationID})
},
mark: func(h *NotificationHandler) (any, error) {
return h.MarkOrganizationNotificationRead(ctx, api.MarkOrganizationNotificationReadParams{OrgID: organizationID, NotificationID: eventID})
},
want: []any{(*api.ListOrganizationNotificationsNotFound)(nil), (*api.MarkOrganizationNotificationReadNotFound)(nil)},
},
{
name: "an employer whose email is not confirmed",
err: notification.ErrInboxEmailNotVerified,
list: func(h *NotificationHandler) (any, error) {
return h.ListOrganizationNotifications(ctx, api.ListOrganizationNotificationsParams{OrgID: organizationID})
},
mark: func(h *NotificationHandler) (any, error) {
return h.MarkOrganizationNotificationRead(ctx, api.MarkOrganizationNotificationReadParams{OrgID: organizationID, NotificationID: eventID})
},
want: []any{(*api.ListOrganizationNotificationsForbidden)(nil), (*api.MarkOrganizationNotificationReadForbidden)(nil)},
code: api.ProblemCodeEmailNotVerified,
},
{
name: "an item of another account",
err: outbound.ErrNotificationInboxEntryNotFound,
mark: func(h *NotificationHandler) (any, error) {
return h.MarkMyNotificationRead(ctx, api.MarkMyNotificationReadParams{NotificationID: eventID})
},
want: []any{nil, (*api.MarkMyNotificationReadNotFound)(nil)},
},
} {
t.Run(test.name, func(t *testing.T) {
handler := NewNotificationHandler(&notificationOperationsStub{err: test.err})
if test.list != nil {
got, err := test.list(handler)
if err != nil || !sameResponseType(got, test.want[0]) {
t.Fatalf("list = %#v, %v, want %T", got, err, test.want[0])
}
requireProblemCode(t, got, test.code)
}
got, err := test.mark(handler)
if err != nil || !sameResponseType(got, test.want[1]) {
t.Fatalf("mark = %#v, %v, want %T", got, err, test.want[1])
}
requireProblemCode(t, got, test.code)
})
}
handler := NewNotificationHandler(&notificationOperationsStub{err: errors.New("database is down")})
if _, err := handler.ListMyNotifications(ctx, api.ListMyNotificationsParams{}); err == nil {
t.Fatal("ListMyNotifications() hid an unexpected failure")
}
response, err := handler.ListMyNotifications(context.Background(), api.ListMyNotificationsParams{})
if err != nil {
t.Fatalf("ListMyNotifications(no session) error = %v", err)
}
if _, ok := response.(*api.ListMyNotificationsUnauthorized); !ok {
t.Fatalf("ListMyNotifications(no session) = %#v, want 401", response)
}
}
func sameResponseType(got, want any) bool {
return want != nil && reflect.TypeOf(got) == reflect.TypeOf(want)
}
func TestMarkNotificationReadReturnsTheFirstReadTimeOnly(t *testing.T) {
userID, organizationID := uuid.New(), uuid.New()
readAt := time.Date(2026, 10, 9, 8, 45, 0, 0, time.UTC)
entry, err := notificationTestEntry(t, userID, organizationID, readAt.Add(-time.Hour)).MarkRead(readAt)
if err != nil {
t.Fatalf("MarkRead() error = %v", err)
}
stub := &notificationOperationsStub{entry: entry}
handler := NewNotificationHandler(stub)
response, err := handler.MarkOrganizationNotificationRead(privacyHTTPContext(userID, ""),
api.MarkOrganizationNotificationReadParams{OrgID: organizationID, NotificationID: entry.EventID()})
if err != nil {
t.Fatalf("MarkOrganizationNotificationRead() error = %v", err)
}
receipt, ok := response.(*api.NotificationReadReceiptHeaders)
if !ok || receipt.CacheControl != api.CacheControlNoStoreNoStore || receipt.Response.ID != entry.EventID() ||
!time.Time(receipt.Response.ReadAt).Equal(readAt) {
t.Fatalf("MarkOrganizationNotificationRead() = %#v, want the receipt of the first read", response)
}
if stub.markRequest.RecipientUserID != userID || stub.markRequest.OrganizationID == nil || *stub.markRequest.OrganizationID != organizationID {
t.Fatalf("mark request = %+v, want the session account in the path organization", stub.markRequest)
}
}
func requireProblemCode(t *testing.T, response any, want api.ProblemCode) {
t.Helper()
if want == "" {
return
}
var got api.ProblemCode
switch typed := response.(type) {
case *api.ListOrganizationNotificationsForbidden:
got = typed.Code
case *api.MarkOrganizationNotificationReadForbidden:
got = typed.Code
default:
t.Fatalf("response %T carries no problem code", response)
}
if got != want {
t.Fatalf("problem code = %s, want %s", got, want)
}
}