diff --git a/src/partners/services/partners-auth.service.ts b/src/partners/services/partners-auth.service.ts index 72d5eacf..a22d405d 100644 --- a/src/partners/services/partners-auth.service.ts +++ b/src/partners/services/partners-auth.service.ts @@ -16,11 +16,13 @@ function removeToken(): void { } async function logIn(email: string, password: string, code?: string): Promise<{ twoFactorSetupRequired: boolean }> { - const response = await partnersAxios.post<{ token: string; twoFactorSetupRequired: boolean }>('/partners/login', { - email, - password, - code, - }); + const response = await captchaService.withCaptcha('Login', (headers) => + partnersAxios.post<{ token: string; twoFactorSetupRequired: boolean }>( + '/partners/login', + { email, password, code }, + { headers }, + ), + ); setToken(response.data.token); return { twoFactorSetupRequired: response.data.twoFactorSetupRequired }; } diff --git a/src/services/login-captcha.test.ts b/src/services/login-captcha.test.ts index b6ec821c..7194e02f 100644 --- a/src/services/login-captcha.test.ts +++ b/src/services/login-captcha.test.ts @@ -1,19 +1,29 @@ import { describe, expect, test } from 'vitest'; import managementAxios from '../management/core/management-axios'; +import partnersAxios from '../partners/core/partners-axios'; +import wholesalersAxios from '../wholesalers/core/wholesalers-axios'; import { managementAuthService } from '../management/services/management-auth.service'; +import { partnersAuthService } from '../partners/services/partners-auth.service'; +import { wholesalersAuthService } from '../wholesalers/services/wholesalers-auth.service'; import { mockAdapter } from '../testing/mock-axios-adapter'; import { stubLocation } from '../testing/stub-location'; import { CaptchaUnavailableError, captchaService } from './captcha.service'; -describe('management login', () => { - stubLocation('/management/login'); +describe('console logins', () => { + stubLocation('/login'); - test('when the backend rejects the captcha, then logIn fails with a CaptchaUnavailableError', async () => { - managementAxios.defaults.adapter = mockAdapter(() => ({ status: 403, data: { code: 'CAPTCHA_FAILED' } })); + describe.each([ + ['management', managementAxios, managementAuthService], + ['partners', partnersAxios, partnersAuthService], + ['wholesalers', wholesalersAxios, wholesalersAuthService], + ])('when the %s backend rejects the captcha', (_, axiosInstance, authService) => { + test('then logIn fails with a CaptchaUnavailableError', async () => { + axiosInstance.defaults.adapter = mockAdapter(() => ({ status: 403, data: { code: 'CAPTCHA_FAILED' } })); - const error = await managementAuthService.logIn('user@internxt.com', 'secret').catch((e: unknown) => e); + const error = await authService.logIn('user@internxt.com', 'secret').catch((e: unknown) => e); - expect(error).toBeInstanceOf(CaptchaUnavailableError); + expect(error).toBeInstanceOf(CaptchaUnavailableError); + }); }); }); diff --git a/src/wholesalers/services/wholesalers-auth.service.ts b/src/wholesalers/services/wholesalers-auth.service.ts index ec5f42df..b9babdd6 100644 --- a/src/wholesalers/services/wholesalers-auth.service.ts +++ b/src/wholesalers/services/wholesalers-auth.service.ts @@ -1,4 +1,5 @@ import wholesalersAxios from '../core/wholesalers-axios'; +import { captchaService } from '../../services/captcha.service'; const WHOLESALERS_TOKEN_KEY = 'wholesalersToken'; @@ -15,9 +16,12 @@ function removeToken(): void { } async function logIn(email: string, password: string, code?: string): Promise<{ twoFactorSetupRequired: boolean }> { - const response = await wholesalersAxios.post<{ token: string; twoFactorSetupRequired?: boolean }>( - '/wholesalers/login', - { email, password, code }, + const response = await captchaService.withCaptcha('Login', (headers) => + wholesalersAxios.post<{ token: string; twoFactorSetupRequired?: boolean }>( + '/wholesalers/login', + { email, password, code }, + { headers }, + ), ); setToken(response.data.token); return { twoFactorSetupRequired: !!response.data.twoFactorSetupRequired };