-
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.
after some research found out that some subdomains do have mx records…
…, this will fall back to root domain if subdomain fails
- Loading branch information
1 parent
4b5cef8
commit f25bd69
Showing
2 changed files
with
29 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ jest.mock('dns'); | |
|
||
const mockedDns = dns as jest.Mocked<typeof dns>; | ||
|
||
describe('isValidEmail Function', () => { | ||
describe('isValidEmail Function (Full and Root Domain Validation)', () => { | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
|
@@ -16,7 +16,7 @@ describe('isValidEmail Function', () => { | |
expect(result).toBe(false); | ||
}); | ||
|
||
test('should return true for a valid email with MX records (full domain)', async () => { | ||
test('should return true for a valid email with MX records on the full domain', async () => { | ||
mockedDns.resolveMx.mockImplementation((domain, callback) => { | ||
if (domain === 'gmail.com') { | ||
callback(null, [{ exchange: 'mail.google.com', priority: 10 }]); | ||
|
@@ -29,22 +29,20 @@ describe('isValidEmail Function', () => { | |
expect(result).toBe(true); | ||
}); | ||
|
||
test('should return true for a valid email with MX records (root domain fallback)', async () => { | ||
test('should return true for a valid email where MX records exist on the root domain', async () => { | ||
mockedDns.resolveMx.mockImplementation((domain, callback) => { | ||
if (domain === 'subdomain.example.com') { | ||
callback(null, []); | ||
} else if (domain === 'example.com') { | ||
callback(null, [{ exchange: 'mail.example.com', priority: 20 }]); | ||
} else { | ||
callback(null, []); | ||
} | ||
}); | ||
|
||
const result = await isValidEmail('[email protected]'); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
test('should return false for a valid email but no MX records in either domain', async () => { | ||
test('should return false when neither the full nor root domain has MX records', async () => { | ||
mockedDns.resolveMx.mockImplementation((domain, callback) => { | ||
callback(null, []); | ||
}); | ||
|
@@ -53,31 +51,25 @@ describe('isValidEmail Function', () => { | |
expect(result).toBe(false); | ||
}); | ||
|
||
test('should return false for a domain with an error during MX lookup (full domain)', async () => { | ||
test('should return false when an error occurs during full domain lookup and no MX records exist on root', async () => { | ||
mockedDns.resolveMx.mockImplementation((domain, callback) => { | ||
if (domain === 'fakeexample.com') { | ||
if (domain === 'subdomain.fakeexample.com') { | ||
callback(new Error('Domain not found'), []); | ||
} else { | ||
callback(null, []); | ||
} | ||
}); | ||
|
||
const result = await isValidEmail('[email protected]'); | ||
const result = await isValidEmail('test@subdomain.fakeexample.com'); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
test('should return false for a domain with an error during MX lookup (root domain fallback)', async () => { | ||
test('should return false when an error occurs during both full and root domain lookups', async () => { | ||
mockedDns.resolveMx.mockImplementation((domain, callback) => { | ||
if (domain === 'subdomain.fakeexample.com') { | ||
callback(new Error('Domain not found'), []); | ||
} else if (domain === 'fakeexample.com') { | ||
callback(new Error('Domain not found'), []); | ||
} else { | ||
callback(null, []); | ||
} | ||
callback(new Error('Domain not found'), []); | ||
}); | ||
|
||
const result = await isValidEmail('test@subdomain.fakeexample.com'); | ||
const result = await isValidEmail('test@error.com'); | ||
expect(result).toBe(false); | ||
}); | ||
}); |
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