forked from jubbens/loopback-api-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (104 loc) · 3.67 KB
/
index.js
File metadata and controls
122 lines (104 loc) · 3.67 KB
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
'use strict';
var supertest = require('supertest');
var async = require('async');
module.exports = {
run: function(conf, app, url, callback) {
var server;
var agent = supertest.agent(url);
var baseURL = '/';
if (typeof conf !== 'object') {
return callback('Failed to load test configuration from file');
}
if (app) {
before(function(done) {
server = app.listen(done);
});
after(function(done) {
server.close(done);
});
}
describe('Loopback API', function() {
async.each(conf, function(c, asyncCallback) {
if (!c.hasOwnProperty('method')) {
callback('Test has no method specified');
return asyncCallback();
}
if (!c.hasOwnProperty('model')) {
callback('Test has no route specified');
return asyncCallback();
}
if (!c.hasOwnProperty('expect')) {
callback('Test has no expected response code specified');
return asyncCallback();
}
var hasData = (c.hasOwnProperty('withData'));
var isWithAuthentication = (c.hasOwnProperty('username') && c.hasOwnProperty('password'));
var authenticationDescription = (isWithAuthentication) ? 'authenticated' : 'unauthenticated';
var description = 'should respond '+c.expect+' on '+authenticationDescription+' '+c.method+' requests to /'+c.model;
var parsedMethod;
var loginBlock;
if (c.method.toUpperCase() === 'GET') {
parsedMethod = agent.get(baseURL+c.model);
} else if (c.method.toUpperCase() === 'POST') {
parsedMethod = agent.post(baseURL+c.model);
} else if (c.method.toUpperCase() === 'PUT') {
parsedMethod = agent.put(baseURL+c.model);
} else if (c.method.toUpperCase() === 'DELETE') {
parsedMethod = agent.delete(baseURL+c.model);
} else if (c.method.toUpperCase() === 'PATCH') {
parsedMethod = agent.patch(baseURL+c.model);
} else {
callback('Test has an unrecognized method type');
return asyncCallback();
}
if (isWithAuthentication) {
loginBlock = function(loginCallback) {
agent
.post(baseURL+'users/login')
.send({ email: c.username, password: c.password, ttl: '1209600000' })
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.expect(200)
.end(function(err, authRes) {
if (err) {
return loginCallback('Could not log in with provided credentials', null);
}
var token = authRes.body.id;
return loginCallback(null, token);
});
}
} else {
loginBlock = function(loginCallback) {
return loginCallback(null, null);
};
}
it(description, function(done) {
loginBlock(function(loginError, loginToken) {
if (loginError) {
done(loginError);
return asyncCallback();
}
if (loginToken) {
parsedMethod = parsedMethod.set('Authorization', loginToken);
}
if (hasData) {
parsedMethod = parsedMethod.send(c.withData)
.set('Content-Type', 'application/json');
}
parsedMethod
.expect(c.expect)
.end(function(err, res) {
if (err) {
done(err);
return asyncCallback();
} else {
done();
return asyncCallback();
}
});
});
});
});
});
}
}