forked from mfbx9da4/deep-email-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
126 lines (115 loc) · 3.47 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import values from 'lodash/values'
import every from 'lodash/every'
import { validate } from '../src/index'
const elevenSeconds = 11 * 1000
describe('validation tests', () => {
it('fails without sending data', async () => {
const res = await validate({
email: '[email protected]',
sender: '[email protected]',
validateTypo: false,
validateDisposable: false,
})
expect(res.valid).toBe(false)
expect(res.reason).toBe('smtp')
expect(res.validators.smtp?.valid).toBe(false)
expect(res).toMatchSnapshot()
})
it('fails with bad regex', async () => {
const res = await validate('david.gmail.com')
expect(res.valid).toBe(false)
expect(res.reason).toBe('regex')
expect(res.validators.regex?.valid).toBe(false)
expect(res).toMatchSnapshot()
})
it('fails later with malformed regex', async () => {
const res = await validate({
email: 'dav [email protected]',
validateRegex: false,
})
expect(res.valid).toBe(false)
expect(res.reason).toBe('smtp')
expect(res.validators.regex?.valid).toBe(true)
expect(res.validators.smtp?.valid).toBe(false)
expect(res).toMatchSnapshot()
})
it('fails with common typo', async () => {
const res = await validate('[email protected]')
expect(res.valid).toBe(false)
expect(res.reason).toBe('typo')
expect(res.validators.typo?.valid).toBe(false)
expect(res).toMatchSnapshot()
})
it('fails with disposable email', async () => {
const res = await validate('[email protected]')
expect(res.valid).toBe(false)
expect(res.reason).toBe('disposable')
expect(res.validators.disposable?.valid).toBe(false)
expect(res).toMatchSnapshot()
})
it(
'fails with bad dns',
async () => {
const res = await validate('[email protected]')
expect(res.valid).toBe(false)
expect(res.reason).toBe('mx')
expect(res.validators.mx?.valid).toBe(false)
expect(res).toMatchSnapshot()
},
elevenSeconds
)
it('fails with bad mailbox', async () => {
const res = await validate('[email protected]')
expect(res.valid).toBe(false)
expect(res.reason).toBe('smtp')
expect(res.validators.smtp?.valid).toBe(false)
expect(res).toMatchSnapshot()
})
it(
'passes when we skip smtp validation',
async () => {
const res = await validate({
email: '[email protected]',
validateSMTP: false,
})
expect(res.valid).toBe(true)
expect(every(values(res.validators), x => x && x.valid)).toBe(true)
expect(res).toMatchSnapshot()
},
elevenSeconds
)
it(
'passes when valid special char',
async () => {
const res = await validate('[email protected]')
expect(res.valid).toBe(true)
expect(every(values(res.validators), x => x && x.valid)).toBe(true)
expect(res).toMatchSnapshot()
},
elevenSeconds
)
it(
'passes when valid wildcard',
async () => {
const res = await validate('[email protected]')
expect(res.valid).toBe(true)
expect(every(values(res.validators), x => x && x.valid)).toBe(true)
expect(res).toMatchSnapshot()
},
elevenSeconds
)
it(
'passes with custom TLD',
async () => {
const res = await validate({
email: '[email protected]',
validateSMTP: false,
additionalTopLevelDomains: ['ir'],
})
expect(res.valid).toBe(true)
expect(every(values(res.validators), x => x && x.valid)).toBe(true)
expect(res).toMatchSnapshot()
},
elevenSeconds
)
})