Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 5231a8b

Browse files
author
Miroslav Bajtoš
committed
Merge branch 'release/0.2.0' into production
2 parents bd4763a + d5151b1 commit 5231a8b

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

lib/helpers.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ _beforeEach.givenAnAnonymousToken = function(attrs, optionalHandler) {
142142
_beforeEach.givenModel('accessToken', {id: '$anonymous'}, optionalHandler);
143143
}
144144

145-
_describe.whenCalledRemotely = function(verb, url, cb) {
145+
_describe.whenCalledRemotely = function(verb, url, data, cb) {
146+
if (cb == undefined) {
147+
cb = data;
148+
data = null;
149+
}
150+
146151
var urlStr = url;
147152
if(typeof url === 'function') {
148153
urlStr = '/<dynamic>';
@@ -160,17 +165,23 @@ _describe.whenCalledRemotely = function(verb, url, cb) {
160165
if(methodForVerb === 'delete') methodForVerb = 'del';
161166

162167
this.http = this.request[methodForVerb](this.url);
163-
this.url = undefined;
168+
delete this.url;
164169
this.http.set('Accept', 'application/json');
165170
if(this.loggedInAccessToken) {
166171
this.http.set('authorization', this.loggedInAccessToken.id);
167172
}
173+
if (data) {
174+
var payload = data;
175+
if (typeof data === 'function')
176+
payload = data.call(this);
177+
this.http.send(payload);
178+
}
168179
this.req = this.http.req;
169180
var test = this;
170181
this.http.end(function(err) {
171182
test.req = test.http.req;
172183
test.res = test.http.res;
173-
test.url = undefined;
184+
delete test.url;
174185
cb();
175186
});
176187
});
@@ -186,40 +197,43 @@ _describe.whenLoggedInAsUser = function(credentials, cb) {
186197
});
187198
}
188199

189-
_describe.whenCalledByUser = function(credentials, verb, url, cb) {
200+
_describe.whenCalledByUser = function(credentials, verb, url, data, cb) {
190201
describe('when called by logged in user', function () {
191202
_beforeEach.givenLoggedInUser(credentials);
192-
_describe.whenCalledRemotely(verb, url, cb);
203+
_describe.whenCalledRemotely(verb, url, data, cb);
193204
});
194205
}
195206

196-
_describe.whenCalledAnonymously = function(verb, url, cb) {
207+
_describe.whenCalledAnonymously = function(verb, url, data, cb) {
197208
describe('when called anonymously', function () {
198209
_beforeEach.givenAnAnonymousToken();
199-
_describe.whenCalledRemotely(verb, url, cb);
210+
_describe.whenCalledRemotely(verb, url, data, cb);
200211
});
201212
}
202213

203-
_describe.whenCalledUnauthenticated = function(verb, url, cb) {
214+
_describe.whenCalledUnauthenticated = function(verb, url, data, cb) {
204215
describe('when called with unauthenticated token', function () {
205216
_beforeEach.givenAnAnonymousToken();
206-
_describe.whenCalledRemotely(verb, url, cb);
217+
_describe.whenCalledRemotely(verb, url, data, cb);
207218
});
208219
}
209220

210221
_it.shouldBeAllowed = function() {
211222
it('should be allowed', function() {
212223
assert(this.req);
213224
assert(this.res);
214-
assert.notEqual(this.res.statusCode, 401);
225+
// expect success - status 2xx or 3xx
226+
expect(this.res.statusCode).to.be.within(100, 399);
215227
});
216228
}
217229

218230
_it.shouldBeDenied = function() {
219231
it('should not be allowed', function() {
220232
assert(this.res);
221-
var status = this.res.statusCode;
222-
assert(status === 401 || status === 404);
233+
var expectedStatus = this.aclErrorStatus ||
234+
this.app && this.app.get('aclErrorStatus') ||
235+
401;
236+
expect(this.res.statusCode).to.equal(expectedStatus);
223237
});
224238
}
225239

@@ -231,8 +245,8 @@ _it.shouldNotBeFound = function() {
231245
}
232246

233247
_it.shouldBeAllowedWhenCalledAnonymously =
234-
function(verb, url) {
235-
_describe.whenCalledAnonymously(verb, url, function() {
248+
function(verb, url, data) {
249+
_describe.whenCalledAnonymously(verb, url, data, function() {
236250
_it.shouldBeAllowed();
237251
});
238252
}
@@ -245,8 +259,8 @@ function(verb, url) {
245259
}
246260

247261
_it.shouldBeAllowedWhenCalledUnauthenticated =
248-
function(verb, url) {
249-
_describe.whenCalledUnauthenticated(verb, url, function() {
262+
function(verb, url, data) {
263+
_describe.whenCalledUnauthenticated(verb, url, data, function() {
250264
_it.shouldBeAllowed();
251265
});
252266
}
@@ -259,8 +273,8 @@ function(verb, url) {
259273
}
260274

261275
_it.shouldBeAllowedWhenCalledByUser =
262-
function(credentials, verb, url) {
263-
_describe.whenCalledByUser(credentials, verb, url, function() {
276+
function(credentials, verb, url, data) {
277+
_describe.whenCalledByUser(credentials, verb, url, data, function() {
264278
_it.shouldBeAllowed();
265279
});
266280
}

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "loopback-testing",
3-
"version": "0.1.5",
3+
"version": "0.2.0",
44
"description": "Utilities for testing LoopBack applications",
55
"main": "index.js",
66
"scripts": {
@@ -10,12 +10,9 @@
1010
"author": "Ritchie Martori",
1111
"dependencies": {
1212
"supertest": "~0.9.0",
13-
"mocha": "~1.17.1",
13+
"mocha": "~1.20.1",
1414
"async": "~0.2.10"
1515
},
16-
"peerDependencies": {
17-
"loopback": "2.x || ^1.6.1"
18-
},
1916
"devDependencies": {
2017
"chai": "~1.9.0",
2118
"loopback": "^1.6.1"

0 commit comments

Comments
 (0)