package notificationservice import ( "context" "errors" "testing" "Rezina-Backend/internal/domain" "Rezina-Backend/internal/ports" "github.com/jackc/pgx/v5" ) type getNotificationStoreStub struct { ports.NotificationStore notification *domain.Notification err error } func (s *getNotificationStoreStub) GetNotification(_ context.Context, id int64) (*domain.Notification, error) { if s.err != nil { return nil, s.err } if s.notification == nil { return nil, pgx.ErrNoRows } return s.notification, nil } func TestGetNotificationByID_ownNotification(t *testing.T) { svc := &Service{ store: &getNotificationStoreStub{ notification: &domain.Notification{ ID: "10", RecipientID: 42, }, }, } got, err := svc.GetNotificationByID(context.Background(), 10, 42, false) if err != nil { t.Fatalf("expected nil error, got %v", err) } if got.RecipientID != 42 { t.Fatalf("expected recipient 42, got %d", got.RecipientID) } } func TestGetNotificationByID_forbiddenForOtherUser(t *testing.T) { svc := &Service{ store: &getNotificationStoreStub{ notification: &domain.Notification{ ID: "10", RecipientID: 99, }, }, } _, err := svc.GetNotificationByID(context.Background(), 10, 42, false) if !errors.Is(err, ErrNotificationForbidden) { t.Fatalf("expected ErrNotificationForbidden, got %v", err) } } func TestGetNotificationByID_adminCanReadAny(t *testing.T) { svc := &Service{ store: &getNotificationStoreStub{ notification: &domain.Notification{ ID: "10", RecipientID: 99, }, }, } got, err := svc.GetNotificationByID(context.Background(), 10, 42, true) if err != nil { t.Fatalf("expected nil error, got %v", err) } if got.RecipientID != 99 { t.Fatalf("expected recipient 99, got %d", got.RecipientID) } } func TestGetNotificationByID_notFound(t *testing.T) { svc := &Service{ store: &getNotificationStoreStub{}, } _, err := svc.GetNotificationByID(context.Background(), 10, 42, false) if !errors.Is(err, ErrNotificationNotFound) { t.Fatalf("expected ErrNotificationNotFound, got %v", err) } } func TestGetNotificationByID_DistinguishesReceiverTypes(t *testing.T) { svc := &Service{store: &getNotificationStoreStub{notification: &domain.Notification{ID: "10", RecipientID: 42, ReceiverType: domain.ReceiverTypeTeamMember}}} customerCtx := domain.WithNotificationScope(context.Background(), 42, domain.ReceiverTypeUser) if _, err := svc.GetNotificationByID(customerCtx, 10, 42, false); !errors.Is(err, ErrNotificationForbidden) { t.Fatalf("customer read a team member notification: %v", err) } staffCtx := domain.WithNotificationScope(context.Background(), 42, domain.ReceiverTypeTeamMember) if _, err := svc.GetNotificationByID(staffCtx, 10, 42, false); err != nil { t.Fatalf("staff could not read own notification: %v", err) } }