|
| 1 | +/** |
| 2 | + * Copyright 2025 The Ground Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { |
| 18 | + createMockFirestore, |
| 19 | + newDocumentSnapshot, |
| 20 | + newEventContext, |
| 21 | + stubAdminApi, |
| 22 | +} from '@ground/lib/dist/testing/firestore'; |
| 23 | +import {resetDatastore} from './common/context'; |
| 24 | +import {Firestore} from 'firebase-admin/firestore'; |
| 25 | +import * as functions from './index'; |
| 26 | +import * as context from './common/context'; |
| 27 | +import {MailService} from './common/mail-service'; |
| 28 | + |
| 29 | +const test = require('firebase-functions-test')(); |
| 30 | + |
| 31 | +describe('onCreatePasslistEntry()', () => { |
| 32 | + let mockFirestore: Firestore; |
| 33 | + let getMailServiceMock: any; |
| 34 | + let mailServiceMock: any; |
| 35 | + |
| 36 | + const serverConfig = { |
| 37 | + port: 5555, |
| 38 | + }; |
| 39 | + const mail = { |
| 40 | + html: 'html', |
| 41 | + subject: 'subject', |
| 42 | + |
| 43 | + }; |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + mockFirestore = createMockFirestore(); |
| 47 | + stubAdminApi(mockFirestore); |
| 48 | + |
| 49 | + mailServiceMock = jasmine.createSpyObj('MailService', [ |
| 50 | + 'sendMail', |
| 51 | + ]) as jasmine.SpyObj<MailService>; |
| 52 | + |
| 53 | + getMailServiceMock = spyOn(context, 'getMailService').and.returnValue( |
| 54 | + mailServiceMock |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + resetDatastore(); |
| 60 | + }); |
| 61 | + |
| 62 | + afterAll(() => { |
| 63 | + test.cleanup(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('passlist notification email template exists', async () => { |
| 67 | + await mockFirestore.collection('passlists').add({}); |
| 68 | + await test.wrap(functions.onCreatePasslistEntry)(newDocumentSnapshot({})); |
| 69 | + expect(getMailServiceMock).not.toHaveBeenCalled(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('mail server config exists', async () => { |
| 73 | + const docRef = mockFirestore.doc('config/mail'); |
| 74 | + docRef.set({server: serverConfig}); |
| 75 | + const docSnapshot = await docRef.get(); |
| 76 | + const data = docSnapshot.data(); |
| 77 | + expect(data).toEqual({server: serverConfig}); |
| 78 | + expect(docSnapshot.exists).toBe(true); |
| 79 | + expect(docSnapshot.id).toBe('mail'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('sends email notification', async () => { |
| 83 | + mockFirestore.doc('config/mail').set({server: serverConfig}); |
| 84 | + mockFirestore.doc('config/mail/templates/passlisted').set(mail); |
| 85 | + mockFirestore.doc(`passlists/${mail.to}`).set({}); |
| 86 | + await test.wrap(functions.onCreatePasslistEntry)( |
| 87 | + newDocumentSnapshot({}), |
| 88 | + newEventContext({entryId: mail.to}) |
| 89 | + ); |
| 90 | + expect(getMailServiceMock).toHaveBeenCalled(); |
| 91 | + expect(mailServiceMock.sendMail).toHaveBeenCalled(); |
| 92 | + expect(mailServiceMock.sendMail).toHaveBeenCalledWith(mail); |
| 93 | + }); |
| 94 | +}); |
0 commit comments