2024-11-04 12:59:04 -05:00
|
|
|
const { getCustomConfig } = require('~/server/services/Config');
|
2024-04-25 19:14:07 +02:00
|
|
|
const isDomainAllowed = require('./isDomainAllowed');
|
|
|
|
|
|
2024-11-04 12:59:04 -05:00
|
|
|
jest.mock('~/server/services/Config', () => ({
|
|
|
|
|
getCustomConfig: jest.fn(),
|
|
|
|
|
}));
|
2024-04-25 19:14:07 +02:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
});
|