/
connectTests.js
222 lines (194 loc) · 6.12 KB
/
connectTests.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var nohm = require(__dirname+'/../lib/nohm').Nohm;
var args = require(__dirname+'/testArgs.js');
var redis = args.redis;
var h = require(__dirname+'/helper.js');
var vm = require('vm');
nohm.setExtraValidations(__dirname + '/custom_validations.js');
nohm.model('UserConnectMockup', {
properties: {
name: {
type: 'string',
defaultValue: 'testName',
validations: [
'notEmpty',
['length', {
min: 2
}
]
]
},
customValidationFile: {
type: 'string',
defaultValue: 'customValidationFile',
validations: [
'customValidationFile'
]
},
customValidationFileTimesTwo: {
type: 'string',
defaultValue: 'customValidationFileTimesTwo',
validations: [
'customValidationFileTimesTwo'
]
},
excludedProperty: {
type: 'string',
defaultValue: 'asd',
validations: [
'notEmpty'
]
},
excludedValidation: {
type: 'string',
defaultValue: 'asd',
validations: [
'notEmpty',
['length', {
min: 2
}
]
]
}
}
});
nohm.model('ExcludedConnectMockup', {
properties: {
name: {
type: 'string',
defaultValue: '',
validations: [
'notEmpty'
]
}
}
});
var setup = function (t, expected, options, callback) {
t.expect(3+expected);
var length = 0;
var headersSet = false;
var namespace = (options && options.namespace) ? options.namespace : 'nohmValidations';
var dummyRes = {
writeHead: function (status, headers) {
t.ok(headers['Content-Length'] > 0, 'Header Content-Length was 0');
length = headers['Content-Length'];
headersSet = true;
},
end: function (str) {
var sandbox = {
window: {},
console: console
};
t.ok(headersSet, 'Headers were not set before res.end() was called');
t.same(length, str.length, 'Content-Length was not equal to the actual body length');
try {
// fixes the problem that in the browser we'd have globals automatically in window, here we don't.
str = str.replace(/(typeof\(exports\) === 'undefined')/,
'(window[nohmValidationsNamespaceName] = '+namespace+') && $1');
vm.runInNewContext(str, sandbox, 'validations.vm');
} catch (e) {
console.log(str);
console.log('Parsing the javascript failed: '+e.message);
console.log(e.stack);
t.done();
}
callback(sandbox, str);
}
};
var url = (options && options.url) ? options.url : '/nohmValidations.js';
nohm.connect(options)({url: url }, dummyRes, function () {
t.ok(false, 'Connect middleware called next with valid url.');
t.done();
});
};
exports.connect = {
setUp: function (next) {
if ( ! nohm.client) {
nohm.setClient(redis);
}
next();
},
tearDown: function (next) {
h.cleanUp(redis, args.prefix, next);
},
connectNoOptions: function (t) {
setup(t, 2, undefined, function (sandbox, str) {
var val = sandbox.nohmValidations.models.UserConnectMockup;
t.ok(val.name.indexOf('notEmpty') === 0, 'UserConnectMockup did not have the proper validations');
t.same(val.name[1], [
'length', {
min: 2
}
], 'UserConnectMockup did not have the proper validations');
t.done();
});
},
connectValidate: function (t) {
setup(t, 2, undefined, function (sandbox) {
var val = sandbox.nohmValidations.validate;
val('UserConnectMockup', {name: 'asd', excludedProperty: 'asd', excludedValidation: 'asd'}, function (valid) {
t.same(valid, true, 'Validate did not work as expected.');
val('UserConnectMockup', {name: 'a', excludedProperty: '', excludedValidation: 'a'}, function (valid, errors) {
t.same(errors, {name: ['length'], excludedProperty: ['notEmpty'], excludedValidation: ['length']}, 'Validate did not work as expected.');
t.done();
});
});
});
},
connectOptions: function (t) {
setup(t, 1, {url: './nohm.js', namespace: 'hurgel'}, function (sandbox) {
t.ok(sandbox.hurgel, 'Namespace option not successful');
t.done();
});
},
connectExtraFiles: function (t) {
setup(t, 1, {extraFiles: __dirname+'/custom_validations2.js'}, function (sandbox) {
sandbox.nohmValidations.validate('UserConnectMockup', {
customValidationFile: 'NOPE',
customValidationFileTimesTwo: 'NOPE'
}, function (valid, errors) {
t.same(errors, {
customValidationFile: ['customValidationFile'],
customValidationFileTimesTwo: ['customValidationFileTimesTwo']
}, 'Validate did not work as expected.');
t.done();
});
});
},
connectExceptions: function (t) {
setup(t, 2, {exclusions: {
UserConnectMockup: {
excludedValidation: [1],
excludedProperty: true
},
ExcludedConnectMockup: true
}}, function (sandbox) {
var validate = sandbox.nohmValidations.validate;
validate('UserConnectMockup', {
excludedValidation: 'a',
excludedProperty: ''
}, function (valid) {
t.same(valid, true, 'Validate did not work as expected with exclusions.');
try {
validate('ExcludedConnectMockup', {
name: ''
}, function () {
t.ok(false, 'Validate should have thrown an error about an invalid modelname');
t.done();
});
} catch (e) {
t.same(e.message, 'Invalid modelName passed to nohm or model was not properly exported.', 'Validate did not work as expected with exclusions.');
}
t.done();
});
});
},
connectValidateEmpty: function (t) {
setup(t, 1, undefined, function (sandbox) {
var val = sandbox.nohmValidations.validate;
val('UserConnectMockup', {excludedProperty: 'asd', excludedValidation: 'asd'}, function (valid) {
t.same(valid, true, 'Validate did not work as expected.');
t.done();
});
});
}
};