Skip to content

Commit

Permalink
🧪 refactor(isDomainAllowed): change directory, add tests (danny-avila…
Browse files Browse the repository at this point in the history
  • Loading branch information
berry-13 committed Apr 25, 2024
1 parent 886738f commit ca92223
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 63 deletions.
26 changes: 2 additions & 24 deletions api/server/services/AuthService.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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';

/**
Expand Down
39 changes: 0 additions & 39 deletions api/server/services/AuthService.spec.js

This file was deleted.

24 changes: 24 additions & 0 deletions api/server/services/isDomainAllowed.js
Original file line number Diff line number Diff line change
@@ -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;
58 changes: 58 additions & 0 deletions api/server/services/isDomainAllowed.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit ca92223

Please sign in to comment.