-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.spec.js
184 lines (182 loc) · 7.24 KB
/
index.spec.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use strict'
const { expect } = require('chai')
const EmailValidation = require('./index')
describe('EmailValidation', () => {
describe('Validation of email format', () => {
const ev = new EmailValidation()
it('should detect null as an invalid email', () => {
const result = ev.check(null)
expect(result).to.be.an('object')
expect(result.valid).eq(false)
expect(result.errors[0]).eq('invalid')
})
it('should detect an empty string as an invalid email', () => {
const result = ev.check('')
expect(result).to.be.an('object')
expect(result.valid).eq(false)
expect(result.errors[0]).eq('invalid')
})
it('should detect a random string as an invalid email', () => {
const result = ev.check('fapojfp')
expect(result).to.be.an('object')
expect(result.valid).eq(false)
expect(result.errors[0]).eq('invalid')
})
it('should detect an email with spaces as an invalid email', () => {
const result = ev.check('roger @ test . com')
expect(result).to.be.an('object')
expect(result.valid).eq(false)
expect(result.errors[0]).eq('invalid')
})
it('should detect an valid looking email as valid', () => {
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(true)
expect(result.errors).have.lengthOf(0)
})
it('should reformat email address and extract domain', () => {
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(true)
expect(result.email).eq('[email protected]')
expect(result.domain).eq('testemail.com')
})
it('should not suggest possible typos for gmail.com', () => {
const result = ev.check('[email protected]')
expect(result.typo).eq(null)
})
it('should suggest possible typos for gmal.com', () => {
const result = ev.check('[email protected]')
expect(result.typo).eq('[email protected]')
})
it('should suggest possible typos for gnaul.com', () => {
const result = ev.check('[email protected]')
expect(result.typo).eq('[email protected]')
})
})
describe('Detection of disposable and free emails', () => {
const ev = new EmailValidation()
it('should detect gmail.com as an free email', () => {
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(false)
expect(result.errors[0]).eq('freemail')
})
it('should detect yopmail.com as an disposable email', () => {
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(false)
expect(result.errors[0]).eq('disposable')
})
it('should not detect romainsimon.net as a disposable or freemail', () => {
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(true)
expect(result.errors).have.lengthOf(0)
})
})
describe('Configuration to allow disposable emails', () => {
const ev = new EmailValidation({ allowDisposable: true })
it('should still detect gmail.com as an free email', () => {
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(false)
expect(result.errors[0]).eq('freemail')
})
it('should not detect yopmail.com as an disposable email', () => {
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(true)
expect(result.errors).have.lengthOf(0)
})
it('should not detect romainsimon.net as a disposable or freemail', () => {
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(true)
expect(result.errors).have.lengthOf(0)
})
})
describe('Configuration to allow free emails', () => {
const ev = new EmailValidation({ allowFreemail: true })
it('should not detect gmail.com as an free email', () => {
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(true)
expect(result.errors).have.lengthOf(0)
})
it('should still detect yopmail.com as an disposable email', () => {
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(false)
expect(result.errors[0]).eq('disposable')
})
it('should not detect romainsimon.net as a disposable or freemail', () => {
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(true)
expect(result.errors).have.lengthOf(0)
})
})
describe('Configuration to whitelist or blacklist domains', () => {
it('should disallow domains blacklisted', () => {
const ev = new EmailValidation({ blacklist: ['domain.com'] })
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(false)
expect(result.errors[0]).eq('blacklist')
})
it('should allow domains whitelisted', () => {
const ev = new EmailValidation({ whitelist: ['gmail.com'] })
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(true)
expect(result.errors).have.lengthOf(0)
})
})
describe('Blacklist or whitelist domains after initialization', () => {
const ev = new EmailValidation()
it('should say domain random.com is valid', () => {
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(true)
})
it('should say domain random.com is invalid after it is blacklisted', () => {
ev.blacklist('random.com')
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(false)
})
it('should say domain random.com is invalid after it is whitelisted again', () => {
ev.whitelist('random.com')
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(true)
})
it('should say domain random.com is invalid after it is blacklisted from email', () => {
ev.blacklist('[email protected]')
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(false)
})
it('should say domain random.com is invalid after it is whitelisted again from email', () => {
ev.whitelist('[email protected]')
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(true)
})
})
describe('Modify options after initialization', () => {
const ev = new EmailValidation()
it('should say domain gmail.com is invalid', () => {
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(false)
})
it('should say domain gmail.com is valid after free emails are allowed', () => {
ev.setOptions({ allowFreemail: true })
const result = ev.check('[email protected]')
expect(result).to.be.an('object')
expect(result.valid).eq(true)
})
})
})