import { Test, TestingModule } from '@nestjs/testing' import { datatype, internet, random } from 'faker' import { auth } from 'firebase-admin' import { MockedModule } from '../../../test/types' import { FirebaseAuth } from '../../typings/global' import { Firebender } from '../firebender' import { AuthenticationService, USER_NOT_FOUND_ERROR } from './authentication.service' describe('AuthenticationService', () => { let subject: AuthenticationService let authModule: MockedModule let authClientModule: MockedModule beforeAll(async () => { authModule = { getUserByEmail: jest.fn(), getUser: jest.fn(), generatePasswordResetLink: jest.fn(), } // authClientModule = { // sendPasswordResetEmail: jest.fn(), // } const app: TestingModule = await Test.createTestingModule({ providers: [AuthenticationService], }).compile() subject = app.get(AuthenticationService) jest.spyOn(Firebender.prototype, 'constructor') subject.onModuleInit() Object.defineProperty(subject['firebender'], 'firebaseAdmin', { value: { auth: jest.fn().mockReturnValue(authModule) }, }) Object.defineProperty(subject['firebender'], 'firebaseClient', { value: { auth: jest.fn().mockReturnValue(authClientModule) }, }) }) afterEach(() => { jest.clearAllMocks() }) afterAll(() => { jest.restoreAllMocks() }) describe('#getUserByEmail', () => { const email = internet.email() let tenant: string describe('when tenant is not given', () => { describe('when user with the email exists on authentication', () => { const user = { uid: datatype.uuid() } beforeEach(() => { authModule.getUserByEmail.mockResolvedValue(user) }) it('returns the user', async () => { expect(await subject.getUserByEmail(email, tenant)).toEqual(user) }) }) describe('when user with the email doesn`t exists on authentication', () => { beforeEach(() => { authModule.getUserByEmail.mockRejectedValue({ code: USER_NOT_FOUND_ERROR }) }) it('returns undefined', async () => { expect(await subject.getUserByEmail(email, tenant)).toBeUndefined() }) }) }) }) describe('#generateResetPasswordUrl', () => { let tenant: string let uid: string let email: string let getTenantAuthSpy: jest.SpyInstance beforeAll(() => { tenant = random.word() uid = datatype.uuid() email = internet.email() getTenantAuthSpy = jest.spyOn(subject, 'getTenantAuth') }) describe('when tenant is not valid', () => { beforeEach(() => { getTenantAuthSpy.mockRejectedValue(new Error()) }) it('throw error', async () => { await expect(subject.generateResetPasswordUrl(uid, tenant)).rejects.toThrowError() }) }) describe('when tenant is valid', () => { beforeEach(() => { getTenantAuthSpy.mockResolvedValue(authModule) }) describe('when not found the user', () => { beforeEach(() => { authModule.getUser.mockRejectedValue(new Error()) }) it('throw error', async () => { await expect(subject.generateResetPasswordUrl(uid, tenant)).rejects.toThrowError() }) }) describe('when user is found', () => { beforeEach(() => { authModule.getUser.mockResolvedValue({ uid, email }) }) it('generate link', async () => { await subject.generateResetPasswordUrl(uid, tenant) expect(authModule.generatePasswordResetLink).toBeCalledWith(email) }) }) }) }) // describe('#sendResetPasswordEmail', () => { // let tenant: string // let email: string // let getTenantIdSpy: jest.SpyInstance // beforeAll(() => { // tenant = random.word() // email = internet.email() // getTenantIdSpy = jest.spyOn(subject, 'getTenantId') // }) // it('send reset password firebase email', async () => { // await subject.sendResetPasswordEmail(email) // expect(authClientModule.sendPasswordResetEmail).toBeCalledWith(email) // }) // describe('when tenant is given', () => { // beforeEach(() => { // getTenantIdSpy.mockResolvedValue(tenant) // }) // it('retrieve tenant id', async () => { // await subject.sendResetPasswordEmail(email, tenant) // expect(getTenantIdSpy).toBeCalledWith(tenant) // }) // }) // }) })