🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { beforeEach, describe, expect, it, vi } from 'vitest';
// Get mocked module
import { authEnv } from '@/envs/auth';
import { isEmailAllowed } from './email-whitelist';
// Mock authEnv
vi.mock('@/envs/auth', () => ({
authEnv: {
AUTH_ALLOWED_EMAILS: undefined as string | undefined,
},
}));
describe('isEmailAllowed', () => {
beforeEach(() => {
// Reset to undefined before each test
(authEnv as { AUTH_ALLOWED_EMAILS: string | undefined }).AUTH_ALLOWED_EMAILS = undefined;
});
describe('when whitelist is empty', () => {
it('should allow all emails when AUTH_ALLOWED_EMAILS is undefined', () => {
expect(isEmailAllowed('[email protected]')).toBe(true);
});
it('should allow all emails when AUTH_ALLOWED_EMAILS is empty string', () => {
(authEnv as { AUTH_ALLOWED_EMAILS: string | undefined }).AUTH_ALLOWED_EMAILS = '';
expect(isEmailAllowed('[email protected]')).toBe(true);
});
});
describe('domain matching', () => {
beforeEach(() => {
(authEnv as { AUTH_ALLOWED_EMAILS: string | undefined }).AUTH_ALLOWED_EMAILS =
'example.com,company.org';
});
it('should allow email from whitelisted domain', () => {
expect(isEmailAllowed('[email protected]')).toBe(true);
expect(isEmailAllowed('[email protected]')).toBe(true);
});
it('should reject email from non-whitelisted domain', () => {
expect(isEmailAllowed('[email protected]')).toBe(false);
});
it('should be case-sensitive for domain', () => {
expect(isEmailAllowed('[email protected]')).toBe(false);
expect(isEmailAllowed('[email protected]')).toBe(false);
});
});
describe('exact email matching', () => {
beforeEach(() => {
(authEnv as { AUTH_ALLOWED_EMAILS: string | undefined }).AUTH_ALLOWED_EMAILS =
'[email protected],[email protected]';
});
it('should allow exact email match', () => {
expect(isEmailAllowed('[email protected]')).toBe(true);
expect(isEmailAllowed('[email protected]')).toBe(true);
});
it('should reject different email at same domain', () => {
expect(isEmailAllowed('[email protected]')).toBe(false);
});
it('should be case-sensitive for email', () => {
expect(isEmailAllowed('[email protected]')).toBe(false);
});
});
describe('mixed domain and email matching', () => {
beforeEach(() => {
(authEnv as { AUTH_ALLOWED_EMAILS: string | undefined }).AUTH_ALLOWED_EMAILS =
'example.com,[email protected]';
});
it('should allow any email from whitelisted domain', () => {
expect(isEmailAllowed('[email protected]')).toBe(true);
});
it('should allow specific whitelisted email', () => {
expect(isEmailAllowed('[email protected]')).toBe(true);
});
it('should reject non-whitelisted email from non-whitelisted domain', () => {
expect(isEmailAllowed('[email protected]')).toBe(false);
});
});
describe('whitespace handling', () => {
it('should trim whitespace from whitelist entries', () => {
(authEnv as { AUTH_ALLOWED_EMAILS: string | undefined }).AUTH_ALLOWED_EMAILS =
' example.com , [email protected] ';
expect(isEmailAllowed('[email protected]')).toBe(true);
expect(isEmailAllowed('[email protected]')).toBe(true);
});
it('should filter empty entries', () => {
(authEnv as { AUTH_ALLOWED_EMAILS: string | undefined }).AUTH_ALLOWED_EMAILS =
'example.com,,other.com';
expect(isEmailAllowed('[email protected]')).toBe(true);
expect(isEmailAllowed('[email protected]')).toBe(true);
});
});
describe('edge cases', () => {
it('should reject malformed email without @', () => {
(authEnv as { AUTH_ALLOWED_EMAILS: string | undefined }).AUTH_ALLOWED_EMAILS = 'example.com';
expect(isEmailAllowed('invalid-email')).toBe(false);
});
it('should handle email with multiple @ symbols', () => {
(authEnv as { AUTH_ALLOWED_EMAILS: string | undefined }).AUTH_ALLOWED_EMAILS = 'example.com';
// split('@')[1] returns '[email protected]', which won't match 'example.com'
expect(isEmailAllowed('user@[email protected]')).toBe(false);
});
});
});