Yimaru-BackEnd/internal/services/notification/service_get_test.go
Yared Yemane 685e1d104f feat: add GET notification by ID endpoint
Expose read-only in-app notification details at GET /notifications/:id with owner scoping and list-all admin access.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-05 05:31:46 -07:00

94 lines
2.1 KiB
Go

package notificationservice
import (
"context"
"errors"
"testing"
"Yimaru-Backend/internal/domain"
"Yimaru-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)
}
}