@@ -693,3 +693,175 @@ func TestNotificationService_DeleteAdminNotification(t *testing.T) {
693693 })
694694 }
695695}
696+
697+ func TestNotificationService_PublishAdminNotification (t * testing.T ) {
698+ t .Parallel ()
699+ mockController := gomock .NewController (t )
700+ defer mockController .Finish ()
701+
702+ adminCtx := metadata .NewIncomingContext (
703+ createContextWithToken (t , true ),
704+ metadata.MD {"accept-language" : []string {"en" }},
705+ )
706+ memberCtx := metadata .NewIncomingContext (
707+ createContextWithToken (t , false ),
708+ metadata.MD {"accept-language" : []string {"en" }},
709+ )
710+
711+ draft := func () * domain.Notification {
712+ return & domain.Notification {
713+ Notification : & proto.Notification {
714+ Id : "notification-id-0" ,
715+ Status : proto .Notification_DRAFT ,
716+ CreatedBy : "admin@example.com" ,
717+ LastEditedBy : "admin@example.com" ,
718+ CreatedAt : 1 ,
719+ UpdatedAt : 1 ,
720+ Localizations : []* proto.NotificationLocalization {
721+ {Language : "en" , Title : "New feature" , Content : "# New feature" },
722+ },
723+ },
724+ }
725+ }
726+
727+ patterns := []struct {
728+ desc string
729+ ctx context.Context
730+ setup func (* NotificationService )
731+ req * proto.PublishAdminNotificationRequest
732+ expectedErr error
733+ }{
734+ {
735+ desc : "err: unauthenticated" ,
736+ ctx : context .TODO (),
737+ req : & proto.PublishAdminNotificationRequest {
738+ Id : "notification-id-0" ,
739+ },
740+ expectedErr : statusUnauthenticated .Err (),
741+ },
742+ {
743+ desc : "err: permission denied" ,
744+ ctx : memberCtx ,
745+ req : & proto.PublishAdminNotificationRequest {
746+ Id : "notification-id-0" ,
747+ },
748+ expectedErr : statusPermissionDenied .Err (),
749+ },
750+ {
751+ desc : "err: id required" ,
752+ ctx : adminCtx ,
753+ req : & proto.PublishAdminNotificationRequest {Id : " " },
754+ expectedErr : statusNotificationIDRequired .Err (),
755+ },
756+ {
757+ desc : "err: not found" ,
758+ ctx : adminCtx ,
759+ setup : func (s * NotificationService ) {
760+ s .dbClient .(* databasemock.MockClient ).EXPECT ().RunInTransactionV2 (
761+ gomock .Any (), gomock .Any (),
762+ ).DoAndReturn (func (ctx context.Context , fn func (ctx context.Context ) error ) error {
763+ return fn (ctx )
764+ })
765+ s .notificationStorage .(* notificationstoragemock.MockNotificationStorage ).EXPECT ().GetAdminNotification (
766+ gomock .Any (), "notification-id-0" ,
767+ ).Return (nil , storage .ErrNotificationNotFound )
768+ },
769+ req : & proto.PublishAdminNotificationRequest {
770+ Id : "notification-id-0" ,
771+ },
772+ expectedErr : statusNotificationNotFound .Err (),
773+ },
774+ {
775+ desc : "err: already published" ,
776+ ctx : adminCtx ,
777+ setup : func (s * NotificationService ) {
778+ published := draft ()
779+ published .Status = proto .Notification_PUBLISHED
780+ s .dbClient .(* databasemock.MockClient ).EXPECT ().RunInTransactionV2 (
781+ gomock .Any (), gomock .Any (),
782+ ).DoAndReturn (func (ctx context.Context , fn func (ctx context.Context ) error ) error {
783+ return fn (ctx )
784+ })
785+ s .notificationStorage .(* notificationstoragemock.MockNotificationStorage ).EXPECT ().GetAdminNotification (
786+ gomock .Any (), "notification-id-0" ,
787+ ).Return (published , nil )
788+ },
789+ req : & proto.PublishAdminNotificationRequest {
790+ Id : "notification-id-0" ,
791+ },
792+ expectedErr : statusNotificationAlreadyPublished .Err (),
793+ },
794+ {
795+ desc : "err: localization required" ,
796+ ctx : adminCtx ,
797+ setup : func (s * NotificationService ) {
798+ empty := draft ()
799+ empty .Localizations = nil
800+ s .dbClient .(* databasemock.MockClient ).EXPECT ().RunInTransactionV2 (
801+ gomock .Any (), gomock .Any (),
802+ ).DoAndReturn (func (ctx context.Context , fn func (ctx context.Context ) error ) error {
803+ return fn (ctx )
804+ })
805+ s .notificationStorage .(* notificationstoragemock.MockNotificationStorage ).EXPECT ().GetAdminNotification (
806+ gomock .Any (), "notification-id-0" ,
807+ ).Return (empty , nil )
808+ },
809+ req : & proto.PublishAdminNotificationRequest {
810+ Id : "notification-id-0" ,
811+ },
812+ expectedErr : statusLocalizationRequired .Err (),
813+ },
814+ {
815+ desc : "err: internal" ,
816+ ctx : adminCtx ,
817+ setup : func (s * NotificationService ) {
818+ s .dbClient .(* databasemock.MockClient ).EXPECT ().RunInTransactionV2 (
819+ gomock .Any (), gomock .Any (),
820+ ).Return (errors .New ("error" ))
821+ },
822+ req : & proto.PublishAdminNotificationRequest {
823+ Id : "notification-id-0" ,
824+ },
825+ expectedErr : api .NewGRPCStatus (errors .New ("error" )).Err (),
826+ },
827+ {
828+ desc : "success" ,
829+ ctx : adminCtx ,
830+ setup : func (s * NotificationService ) {
831+ s .dbClient .(* databasemock.MockClient ).EXPECT ().RunInTransactionV2 (
832+ gomock .Any (), gomock .Any (),
833+ ).DoAndReturn (func (ctx context.Context , fn func (ctx context.Context ) error ) error {
834+ return fn (ctx )
835+ })
836+ s .notificationStorage .(* notificationstoragemock.MockNotificationStorage ).EXPECT ().GetAdminNotification (
837+ gomock .Any (), "notification-id-0" ,
838+ ).Return (draft (), nil )
839+ s .notificationStorage .(* notificationstoragemock.MockNotificationStorage ).EXPECT ().PublishAdminNotification (
840+ gomock .Any (), gomock .Any (),
841+ ).Return (nil )
842+ },
843+ req : & proto.PublishAdminNotificationRequest {
844+ Id : "notification-id-0" ,
845+ },
846+ expectedErr : nil ,
847+ },
848+ }
849+ for _ , p := range patterns {
850+ t .Run (p .desc , func (t * testing.T ) {
851+ s := createNotificationService (mockController )
852+ if p .setup != nil {
853+ p .setup (s )
854+ }
855+ resp , err := s .PublishAdminNotification (p .ctx , p .req )
856+ assert .Equal (t , p .expectedErr , err )
857+ if p .expectedErr == nil {
858+ assert .NotNil (t , resp )
859+ assert .Equal (t , "notification-id-0" , resp .Notification .Id )
860+ assert .Equal (t , proto .Notification_PUBLISHED , resp .Notification .Status )
861+ assert .Equal (t , "email" , resp .Notification .PublishedBy )
862+ assert .True (t , resp .Notification .PublishedAt > 0 )
863+ assert .Equal (t , resp .Notification .PublishedAt , resp .Notification .UpdatedAt )
864+ }
865+ })
866+ }
867+ }
0 commit comments