-
Notifications
You must be signed in to change notification settings - Fork 21
/
test.js
283 lines (262 loc) · 8.39 KB
/
test.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//LOAD MODULES WE WILL USE
var assert = require('assert'); //node's basic assertion library
var nsrestlet = require('./nsrestlet.js'); //nsretlet module (which we are testing)
var qs = require('qs'); //we will use this for query string processing
//ATTEMPT TO LOAD THE ENVIROMENTAL VARIABLES
try
{
require('env2')('.env');
}
catch(err)
{
//do nothing, this only happens in a travis-ci enviroment
}
//SEPERATE THE SCRIPT AND DEPLOYMENT NUMBER IDS FROM THE EXTERNAL URL
var query = process.env.EXTERNAL_URL.slice(process.env.EXTERNAL_URL.indexOf("?") + 1);
var params = qs.parse(query);
//CREATE OUR VARIABLE OF SECRETS FROM THE ENVIROMENTAL VARIABLES
var secret = {
accountSettings:[
{
name: "OAuth",
accountId: process.env.ACCOUNT_ID,
tokenKey: process.env.TOKEN_KEY,
tokenSecret: process.env.TOKEN_SECRET,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET
},
{
name: "NLAuth",
accountId: process.env.ACCOUNT_ID,
email: process.env.NON_PRIVELAGED_EMAIL,
password: process.env.NON_PRIVELAGED_PASSWORD,
role: process.env.NON_PRIVELAGED_ROLE
},
{
name: "NLAuth",
accountId: process.env.ACCOUNT_ID,
email: process.env.NON_PRIVELAGED_EMAIL,
password: process.env.NON_PRIVELAGED_PASSWORD
}],
urlSettings:[
{
name: "String S&D IDs",
script: process.env.CUSTOMSCRIPT_STRING_ID,
deployment: process.env.CUSTOMDEPLOY_STRING_ID
},
{
name: "Number S&D IDs",
script: params.script,
deployment: params.deploy
},
{
name: "URL S&D",
url: process.env.EXTERNAL_URL
}]
}
//CREATE LIST OF REQUESTS
//this part of the module generates combinations of each of our settings
//we test each of them because this is largely how the module is used
//so it's important it works right
var requests = [];
secret.accountSettings.forEach(function(accountSetting)
{
secret.urlSettings.forEach(function(urlSetting)
{
var functions = ["get", "post", "put", "delete"];
functions.forEach(function(func)
{
var messages = [{message: "hi"}];
messages.forEach(function(message)
{
["Callback Style", "Promise Style"].forEach(function(style)
{
requests.push({
accountSetting: accountSetting,
urlSetting: urlSetting,
func: func,
message: message,
style: style
})
});
});
});
});
});
//save two of the requests for later
var valid_request = requests[0];
var valid_request2 = requests[requests.length - 1];
//GENERIC REQUEST TEST
//make the generic requests, one at a time
it("Testing Generic Request Calls", function(done)
{
function performRequest()
{
console.log(Date.now() + " doing work...");
if(requests.length > 0)
{
var req = requests.shift();
var test_restlet = nsrestlet.createLink(req.accountSetting, req.urlSetting);
var test_function = test_restlet[req.func];
if(req.style == "Callback Style")
{
test_function(req.message, function(error, body){
if(error) done(error);
if(req.func != "delete")
{
assert.equal(make_string(body), make_string({"success": true, "data": req.message}));
}
performRequest();
});
}
else
{
test_function(req.message).then(function(body){
if(req.func != "delete")
{
assert.equal(make_string(body), make_string({"success": true, "data": req.message}));
}
performRequest();
}).catch(function(error)
{
done(error);
});
}
}
else
{
done();
}
}
performRequest();
});
//OTHER REQUEST TEST
//what happens if we don't provide account settings?
it("Account Settings Error", function()
{
try { nsrestlet.createLink(); }
catch(err) { assert.ok(true); }
});
//what happens if we don't provide an account id?
it("Account ID Error", function()
{
var copy = clone(valid_request.accountSetting)
delete copy.accountId;
try { nsrestlet.createLink(copy); }
catch(err) { assert.ok(true); }
});
//what happens if we are missing some values required for OAuth or NLAuth?
it("Missing OAuth or NLAuth required Values Error", function()
{
var copy = clone(valid_request2.accountSetting)
delete copy.email;
try { nsrestlet.createLink(copy); }
catch(err) { assert.ok(true); }
});
//what happens if we don't provide URL Settings?
it("URL Settings Error", function()
{
try { nsrestlet.createLink(valid_request.accountSetting); }
catch(err) { assert.ok(true); }
});
//what happens if we are missing some values required in the URL Settings
it("Missing required URL Seetings Values Error", function()
{
try { nsrestlet.createLink(valid_request.accountSetting, {}); }
catch(err) { assert.ok(true); }
});
//what happens if we call a restlet that does not exist?
it("Non-Existant Restlet Error", function(done)
{
var link = nsrestlet.createLink(valid_request.accountSetting,
{
script: "fakescript",
deployment: "fakedeploy"
});
link.post(valid_request.message).then(function(body)
{
done("Did not get an error, instead got:" + JSON.stringify(body));
}).catch(function(error)
{
assert.ok(true);
done();
});
});
//what happens if the restlet throws an error?
it("Restlet Throwing Error", function(done)
{
var link = nsrestlet.createLink(valid_request.accountSetting,valid_request.urlSetting);
link.post({"throw": true}).then(function(body)
{
done("Did not get an error, instead got:" + JSON.stringify(body));
}).catch(function(error)
{
assert.ok(true);
done();
});
});
//what happens if we have backoff set, and our request gets dropped
it("Testing a Dropped Call with Backoff", function(done)
{
var copy = clone(valid_request.urlSetting)
copy.retries = 4;
copy.backoff = 10;
var link = nsrestlet.createLink(valid_request.accountSetting, copy);
link.post({"drop": true}).then(function(body)
{
done("Did not get an error, instead got:" + JSON.stringify(body));
}).catch(function(error)
{
assert.ok(true);
done();
});
});
//what happens if we dont't have backoff set, and our request gets dropped
it("Testing a Dropped Call without Backoff", function(done)
{
var copy = clone(valid_request.urlSetting)
copy.retries = 4;
var link = nsrestlet.createLink(valid_request.accountSetting, copy);
link.post({"drop": true}).then(function(body)
{
done("Did not get an error, instead got:" + JSON.stringify(body));
}).catch(function(error)
{
assert.ok(true);
done();
});
});
//just making sure strings work properly (they should, but just checking)
it("Restlet properly send strings (because sometimes we don't need JSON)", function(done)
{
var link = nsrestlet.createLink(valid_request.accountSetting, valid_request.urlSetting);
link.post({"string": "12345"}).then(function(body)
{
assert.equal(body, "12345");
done();
}).catch(function(error)
{
done(error);
});
});
//HELPER FUNCTIONS
//just turns the value into a string if it isn't one already
//(we use this to compare things)
function make_string(value)
{
if(typeof value !== 'string' && !(value instanceof String))
{
return JSON.stringify(value);
}
else
{
return value;
}
}
//makes a copy of an object (shallow, I believe)
function clone(obj)
{
var copy = {};
Object.assign(copy, obj);
return copy;
}