From 0f3556dffe0e6c5e5eaf232ba147908914e6f643 Mon Sep 17 00:00:00 2001 From: Cagatay Uslu Date: Wed, 28 May 2025 15:44:27 +0200 Subject: [PATCH] testing: add unit tests for sendVerificationCode and verifyVerificationCode --- src/email/email.service.spec.ts | 82 +++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/src/email/email.service.spec.ts b/src/email/email.service.spec.ts index e36f0f3..850249f 100644 --- a/src/email/email.service.spec.ts +++ b/src/email/email.service.spec.ts @@ -20,4 +20,86 @@ describe('EmailService', () => { it('should be defined', () => { expect(service).toBeDefined(); }); + + describe('sendVerificationCode', () => { + it('should call signInWithOtp with correct parameters', async () => { + SupabaseServiceMock.client.auth.signInWithOtp.mockResolvedValue({ + error: null, + }); + + const userEmail = 'test@example.com'; + await service.sendVerificationCode(userEmail); + + expect( + SupabaseServiceMock.client.auth.signInWithOtp, + ).toHaveBeenCalledWith({ + email: userEmail, + options: { shouldCreateUser: false }, + }); + }); + it('should throw an error if signInWithOtp fails', async () => { + const error = new Error('Sign-in failed'); + SupabaseServiceMock.client.auth.signInWithOtp.mockResolvedValueOnce( + { error }, + ); + + const userEmail = 'test@example.com'; + await expect( + service.sendVerificationCode(userEmail), + ).rejects.toThrow(error); + + expect( + SupabaseServiceMock.client.auth.signInWithOtp, + ).toHaveBeenCalledWith({ + email: userEmail, + options: { shouldCreateUser: false }, + }); + }); + }); + describe('verifyVerificationCode', () => { + it('should call verifyOtp with correct parameters', async () => { + SupabaseServiceMock.client.auth.verifyOtp.mockResolvedValueOnce({ + error: null, + }); + + const userEmail = 'test@example.com'; + const submittedCode = '123456'; + const result = await service.verifyVerificationCode( + userEmail, + submittedCode, + ); + + expect( + SupabaseServiceMock.client.auth.verifyOtp, + ).toHaveBeenCalledWith({ + email: userEmail, + token: submittedCode, + type: 'email', + }); + expect(result).toBe(true); + }); + + it('should return false if verifyOtp fails', async () => { + const error = new Error('Verification failed'); + SupabaseServiceMock.client.auth.verifyOtp.mockResolvedValueOnce({ + error, + }); + + const userEmail = 'test@example.com'; + const submittedCode = '123456'; + const result = await service.verifyVerificationCode( + userEmail, + submittedCode, + ); + + expect( + SupabaseServiceMock.client.auth.verifyOtp, + ).toHaveBeenCalledWith({ + email: userEmail, + token: submittedCode, + type: 'email', + }); + expect(result).toBe(false); + }); + }); });