11import { describe , expect , it } from "@jest/globals" ;
2- import { post , getAsAdmin , postAsAdmin } from "../../../testing/testingTools" ;
2+ import {
3+ post ,
4+ getAsAdmin ,
5+ postAsAdmin ,
6+ postAsSuperAdmin ,
7+ } from "../../../testing/testingTools" ;
38import { StatusCodes } from "http-status-codes" ;
49import { SupabaseDB } from "../../database" ;
510import { IncomingSubscription } from "./subscription-schema" ;
@@ -188,21 +193,21 @@ describe("GET /subscription/", () => {
188193} ) ;
189194
190195describe ( "POST /subscription/send-email" , ( ) => {
191- it ( "should send an email to all subscribers of a list" , async ( ) => {
192- const mailingList = "test_list" ;
193- const subscribers = [ "email1@test.com" , "email2@test.com" ] ;
196+ const mailingList = "test_list" ;
197+ const subscribers = [ "email1@test.com" , "email2@test.com" ] ;
198+ const emailPayload = {
199+ mailingList : mailingList ,
200+ subject : "Test Subject" ,
201+ htmlBody : "<p>Hello World</p>" ,
202+ } ;
203+ beforeEach ( async ( ) => {
194204 await SupabaseDB . SUBSCRIPTIONS . insert ( {
195205 mailingList,
196206 subscriptions : subscribers ,
197207 } ) ;
198-
199- const emailPayload = {
200- mailingList : mailingList ,
201- subject : "Test Subject" ,
202- htmlBody : "<p>Hello World</p>" ,
203- } ;
204-
205- await postAsAdmin ( "/subscription/send-email" )
208+ } ) ;
209+ it ( "should send an email to all subscribers of a list" , async ( ) => {
210+ const res = await postAsSuperAdmin ( "/subscription/send-email" )
206211 . send ( emailPayload )
207212 . expect ( StatusCodes . OK ) ;
208213
@@ -222,18 +227,30 @@ describe("POST /subscription/send-email", () => {
222227
223228 // Verify that the send method was actually invoked
224229 expect ( mockSESV2Send ) . toHaveBeenCalledTimes ( 1 ) ;
230+
231+ expect ( res . body ) . toEqual ( {
232+ status : "success" ,
233+ } ) ;
234+ } ) ;
235+ it ( "fails for a non-super admin" , async ( ) => {
236+ const res = await postAsAdmin ( "/subscription/send-email" )
237+ . send ( emailPayload )
238+ . expect ( StatusCodes . FORBIDDEN ) ;
239+
240+ expect ( SendEmailCommand ) . not . toHaveBeenCalled ( ) ;
241+ expect ( mockSESV2Send ) . not . toHaveBeenCalled ( ) ;
242+ expect ( res . body ) . toMatchObject ( { error : "Forbidden" } ) ;
225243 } ) ;
226244} ) ;
227245
228246describe ( "POST /subscription/send-email/single" , ( ) => {
247+ const emailPayload = {
248+ email : "ritam@test.com" ,
249+ subject : "Single Email Test" ,
250+ htmlBody : "<p>Single Email Body</p>" ,
251+ } ;
229252 it ( "should send an email to a single specified email address" , async ( ) => {
230- const emailPayload = {
231- email : "ritam@test.com" ,
232- subject : "Single Email Test" ,
233- htmlBody : "<p>Single Email Body</p>" ,
234- } ;
235-
236- await postAsAdmin ( "/subscription/send-email/single" )
253+ const res = await postAsSuperAdmin ( "/subscription/send-email/single" )
237254 . send ( emailPayload )
238255 . expect ( StatusCodes . OK ) ;
239256
@@ -252,5 +269,22 @@ describe("POST /subscription/send-email/single", () => {
252269
253270 // Verify that the send method was actually invoked
254271 expect ( mockSESV2Send ) . toHaveBeenCalledTimes ( 1 ) ;
272+
273+ expect ( res . body ) . toEqual ( {
274+ status : "success" ,
275+ } ) ;
276+ } ) ;
277+
278+ it ( "fails for non super admin" , async ( ) => {
279+ const res = await postAsAdmin ( "/subscription/send-email/single" )
280+ . send ( emailPayload )
281+ . expect ( StatusCodes . FORBIDDEN ) ;
282+
283+ expect ( SendEmailCommand ) . not . toHaveBeenCalled ( ) ;
284+
285+ // Verify that the send method was actually invoked
286+ expect ( mockSESV2Send ) . not . toHaveBeenCalled ( ) ;
287+
288+ expect ( res . body ) . toMatchObject ( { error : "Forbidden" } ) ;
255289 } ) ;
256290} ) ;
0 commit comments