forked from danny-avila/LibreChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧪 refactor(isDomainAllowed): change directory, add tests (danny-avila…
- Loading branch information
Showing
4 changed files
with
84 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |