Blame view

node_modules/isemail/test/isemail.js 4.26 KB
f7563de62   Palak Handa   first commit
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
  var Lab = require('lab');
  var Code = require('code');
  var IsEmail = require('..');
  var Tests = require('./tests.json');
  
  var internals = {
      defaultThreshold: 16
  };
  
  // Test shortcuts
  var isEmail = IsEmail;
  var lab = exports.lab = Lab.script();
  var before = lab.before;
  var after = lab.after;
  var describe = lab.describe;
  var it = lab.it;
  var expect = Code.expect;
  
  // Diagnoses
  var diag = isEmail.diagnoses;
  
  // Expectations
  var expectations = Tests.map(function mapper (value) {
  
      value[1] = diag[value[1]];
      return value;
  });
  
  // Null characters aren't supported in JSON
  expectations.push(['test@[\0', diag.errExpectingDTEXT]);
  expectations.push(['(\0)test@example.com', diag.errExpectingCTEXT]);
  
  var tldExpectations = [
      ['shouldbe@invalid', diag.errUnknownTLD],
      ['shouldbe@example.com', diag.valid]
  ];
  
  describe('isEmail', function () {
  
      it('should check options.tldWhitelist', function (done) {
  
          expect(isEmail('person@top', {
              tldWhitelist: 'top',
              checkDNS: false
          })).to.equal(true);
  
          expect(isEmail('person@top', {
              tldWhitelist: ['com'],
              checkDNS: false
          })).to.equal(false);
  
          expect(function () {
  
              isEmail('', {
                  tldWhitelist: 77
              });
          }).to.throw(/tldWhitelist/);
          done();
      });
  
      it('should check options.minDomainAtoms', function (done) {
  
          expect(function () {
  
              isEmail('person@top', {
                  minDomainAtoms: -1
              });
          }).to.throw(/minDomainAtoms/);
  
          expect(function () {
  
              isEmail('person@top', {
                  minDomainAtoms: 1.5
              });
          }).to.throw(/minDomainAtoms/);
          done();
      });
  
      it('should use options.errorLevel', function (done) {
  
          expect(isEmail('person@123', {
              errorLevel: diag.rfc5321TLDNumeric + 1
          })).to.equal(0);
  
          expect(isEmail('person@123', {
              errorLevel: diag.rfc5321TLDNumeric
          })).to.equal(diag.rfc5321TLDNumeric);
          done();
      });
  
      it('should ensure callback provided with checkDNS', function (done) {
  
          expect(function () {
  
              isEmail('person@top', {
                  checkDNS: true
              });
          }).to.throw(/(?=.*\bcheckDNS\b)(?=.*\bcallback\b)/);
          done();
      });
  
      it('should handle omitted options', function (done) {
  
          expect(isEmail(expectations[0][0])).to.equal(expectations[0][1] < internals.defaultThreshold);
          done();
      });
  
      it('should handle omitted options with callback', function (done) {
  
          isEmail(expectations[0][0], function (res) {
  
              expect(res).to.equal(expectations[0][1] < internals.defaultThreshold);
              done();
          });
      });
  
      expectations.forEach(function (obj, i) {
  
          var email = obj[0], result = obj[1];
          it('should handle test ' + (i + 1), function (done) {
  
              isEmail(email, {
                  errorLevel: 0,
                  checkDNS: true
              }, function (res) {
  
                  expect(res).to.equal(result);
                  done();
              });
          });
      });
  
      tldExpectations.forEach(function (obj, i) {
  
          var email = obj[0];
          var result = obj[1];
  
          it('should handle tld test ' + (i + 1), function (done) {
  
              expect(isEmail(email, {
                  errorLevel: 0,
                  tldWhitelist: {
                      com: true
                  }
              })).to.equal(result);
  
              expect(isEmail(email, {
                  errorLevel: 0,
                  tldWhitelist: ['com']
              })).to.equal(result);
  
              done();
          });
      });
  
      it('should handle domain atom test 1', function (done) {
  
          expect(isEmail('shouldbe@invalid', {
              errorLevel: 0,
              minDomainAtoms: 2
          })).to.equal(diag.errDomainTooShort);
  
          done();
      });
  
      it('should handle domain atom test 2', function (done) {
  
          expect(isEmail('valid@example.com', {
              errorLevel: 0,
              minDomainAtoms: 2
          })).to.equal(diag.valid);
  
          done();
      });
  });