From 11d5e232b397ad59b203b7008b32a1c8401379d8 Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Thu, 25 Apr 2024 19:14:07 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20refactor(isDomainAllowed):=20cha?= =?UTF-8?q?nge=20directory,=20add=20tests=20(#2539)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/server/services/AuthService.js | 26 +-------- api/server/services/AuthService.spec.js | 39 -------------- api/server/services/isDomainAllowed.js | 24 +++++++++ api/server/services/isDomainAllowed.spec.js | 58 +++++++++++++++++++++ 4 files changed, 84 insertions(+), 63 deletions(-) delete mode 100644 api/server/services/AuthService.spec.js create mode 100644 api/server/services/isDomainAllowed.js create mode 100644 api/server/services/isDomainAllowed.spec.js diff --git a/api/server/services/AuthService.js b/api/server/services/AuthService.js index 098110df0d..8b91a9ac3a 100644 --- a/api/server/services/AuthService.js +++ b/api/server/services/AuthService.js @@ -1,8 +1,7 @@ const crypto = require('crypto'); const bcrypt = require('bcryptjs'); -const { errorsToString } = require('librechat-data-provider'); -const { registerSchema } = require('~/strategies/validators'); -const getCustomConfig = require('~/server/services/Config/getCustomConfig'); +const { registerSchema, errorsToString } = require('~/strategies/validators'); +const isDomainAllowed = require('./isDomainAllowed'); const Token = require('~/models/schema/tokenSchema'); const { sendEmail } = require('~/server/utils'); const Session = require('~/models/Session'); @@ -14,27 +13,6 @@ const domains = { server: process.env.DOMAIN_SERVER, }; -async function isDomainAllowed(email) { - if (!email) { - return false; - } - - const domain = email.split('@')[1]; - - if (!domain) { - return false; - } - - const customConfig = await getCustomConfig(); - if (!customConfig) { - return true; - } else if (!customConfig?.registration?.allowedDomains) { - return true; - } - - return customConfig.registration.allowedDomains.includes(domain); -} - const isProduction = process.env.NODE_ENV === 'production'; /** diff --git a/api/server/services/AuthService.spec.js b/api/server/services/AuthService.spec.js deleted file mode 100644 index fb5d8e2533..0000000000 --- a/api/server/services/AuthService.spec.js +++ /dev/null @@ -1,39 +0,0 @@ -const getCustomConfig = require('~/server/services/Config/getCustomConfig'); -const { isDomainAllowed } = require('./AuthService'); - -jest.mock('~/server/services/Config/getCustomConfig', () => jest.fn()); - -describe('isDomainAllowed', () => { - it('should allow domain when customConfig is not available', async () => { - getCustomConfig.mockResolvedValue(null); - await expect(isDomainAllowed('test@domain1.com')).resolves.toBe(true); - }); - - it('should allow domain when allowedDomains is not defined in customConfig', async () => { - getCustomConfig.mockResolvedValue({}); - await expect(isDomainAllowed('test@domain1.com')).resolves.toBe(true); - }); - - it('should reject an email if it is falsy', async () => { - getCustomConfig.mockResolvedValue({}); - await expect(isDomainAllowed('')).resolves.toBe(false); - }); - - it('should allow a domain if it is included in the allowedDomains', async () => { - getCustomConfig.mockResolvedValue({ - registration: { - allowedDomains: ['domain1.com', 'domain2.com'], - }, - }); - await expect(isDomainAllowed('user@domain1.com')).resolves.toBe(true); - }); - - it('should reject a domain if it is not included in the allowedDomains', async () => { - getCustomConfig.mockResolvedValue({ - registration: { - allowedDomains: ['domain1.com', 'domain2.com'], - }, - }); - await expect(isDomainAllowed('user@domain3.com')).resolves.toBe(false); - }); -}); diff --git a/api/server/services/isDomainAllowed.js b/api/server/services/isDomainAllowed.js new file mode 100644 index 0000000000..48e0747511 --- /dev/null +++ b/api/server/services/isDomainAllowed.js @@ -0,0 +1,24 @@ +const getCustomConfig = require('~/server/services/Config/getCustomConfig'); + +async function isDomainAllowed(email) { + if (!email) { + return false; + } + + const domain = email.split('@')[1]; + + if (!domain) { + return false; + } + + const customConfig = await getCustomConfig(); + if (!customConfig) { + return true; + } else if (!customConfig?.registration?.allowedDomains) { + return true; + } + + return customConfig.registration.allowedDomains.includes(domain); +} + +module.exports = isDomainAllowed; diff --git a/api/server/services/isDomainAllowed.spec.js b/api/server/services/isDomainAllowed.spec.js new file mode 100644 index 0000000000..b1cf03a567 --- /dev/null +++ b/api/server/services/isDomainAllowed.spec.js @@ -0,0 +1,58 @@ +const getCustomConfig = require('~/server/services/Config/getCustomConfig'); +const isDomainAllowed = require('./isDomainAllowed'); + +jest.mock('~/server/services/Config/getCustomConfig', () => jest.fn()); + +describe('isDomainAllowed', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should return false if email is falsy', async () => { + const email = ''; + const result = await isDomainAllowed(email); + expect(result).toBe(false); + }); + + it('should return false if domain is not present in the email', async () => { + const email = 'test'; + const result = await isDomainAllowed(email); + expect(result).toBe(false); + }); + + it('should return true if customConfig is not available', async () => { + const email = 'test@domain1.com'; + getCustomConfig.mockResolvedValue(null); + const result = await isDomainAllowed(email); + expect(result).toBe(true); + }); + + it('should return true if allowedDomains is not defined in customConfig', async () => { + const email = 'test@domain1.com'; + getCustomConfig.mockResolvedValue({}); + const result = await isDomainAllowed(email); + expect(result).toBe(true); + }); + + it('should return true if domain is included in the allowedDomains', async () => { + const email = 'user@domain1.com'; + getCustomConfig.mockResolvedValue({ + registration: { + allowedDomains: ['domain1.com', 'domain2.com'], + }, + }); + const result = await isDomainAllowed(email); + expect(result).toBe(true); + }); + + it('should return false if domain is not included in the allowedDomains', async () => { + const email = 'user@domain3.com'; + getCustomConfig.mockResolvedValue({ + registration: { + allowedDomains: ['domain1.com', 'domain2.com'], + }, + }); + const result = await isDomainAllowed(email); + expect(result).toBe(false); + }); +});