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

Commit a8045d7

Browse files
committed
Fix access token not being cleaned up
1 parent 8d57e1f commit a8045d7

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

index.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,17 @@ _beforeEach.withArgs = function() {
7979
}
8080

8181
_beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
82+
var modelKey = modelName;
83+
8284
if(typeof attrs === 'funciton') {
8385
optionalHandler = attrs;
8486
attrs = undefined;
8587
}
8688

89+
if(typeof optionalHandler === 'string') {
90+
modelKey = optionalHandler;
91+
}
92+
8793
attrs = attrs || {};
8894

8995
var model = loopback.getModel(modelName);
@@ -103,9 +109,11 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
103109

104110
model.create(attrs, function(err, result) {
105111
if(err) {
112+
console.error(err.message);
113+
if(err.details) console.error(err.details);
106114
done(err);
107115
} else {
108-
test[modelName] = result;
116+
test[modelKey] = result;
109117
done();
110118
}
111119
});
@@ -116,7 +124,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
116124
}
117125

118126
afterEach(function(done) {
119-
this[modelName].destroy(done);
127+
this[modelKey].destroy(done);
120128
});
121129
}
122130

@@ -137,23 +145,32 @@ _describe.whenCalledRemotely = function(verb, url, cb) {
137145
if(typeof url === 'function') {
138146
urlStr = '/<dynamic>';
139147
}
148+
140149
describe(verb.toUpperCase() + ' ' + urlStr, function() {
141150
beforeEach(function(cb) {
142151
if(typeof url === 'function') {
143152
url = url.call(this);
144153
}
145154
this.remotely = true;
146155
this.verb = verb.toUpperCase();
147-
this.url = url;
156+
this.url = this.url || url;
148157
var methodForVerb = verb.toLowerCase();
149158
if(methodForVerb === 'delete') methodForVerb = 'del';
150159

151-
this.http = this.request[methodForVerb](url);
160+
this.http = this.request[methodForVerb](this.url);
161+
162+
// request settings
152163
this.http.set('Accept', 'application/json');
153-
this.req = this.http.req;
164+
this.http.set('authorization', null);
165+
if(this.accessToken) {
166+
this.http.set('authorization', this.accessToken.id);
167+
}
168+
154169
var test = this;
155170
this.http.end(function(err) {
171+
test.req = test.http.req;
156172
test.res = test.http.res;
173+
test.url = undefined;
157174
cb();
158175
});
159176
});
@@ -186,13 +203,11 @@ _describe.whenLoggedInAsUser = function(credentials, cb) {
186203
describe('when logged in user', function() {
187204
_beforeEach.givenUser(credentials, function(done) {
188205
var test = this;
189-
this.remotely = true;
190206
this.user.constructor.login(credentials, function(err, token) {
191207
if(err) {
192208
done(err);
193209
} else {
194210
test.accessToken = token;
195-
test.req.set('authorization', token.id);
196211
done();
197212
}
198213
});
@@ -201,6 +216,12 @@ _describe.whenLoggedInAsUser = function(credentials, cb) {
201216
afterEach(function(done) {
202217
this.accessToken.destroy(done);
203218
});
219+
220+
afterEach(function() {
221+
this.accessToken = undefined;
222+
});
223+
224+
cb();
204225
});
205226
}
206227

@@ -215,7 +236,8 @@ _it.shouldBeAllowed = function() {
215236
_it.shouldBeDenied = function() {
216237
it('should not be allowed', function() {
217238
assert(this.res);
218-
assert.equal(this.res.statusCode, 401);
239+
var status = this.res.statusCode;
240+
assert(status === 401 || status === 404);
219241
});
220242
}
221243

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"dependencies": {
1313
"supertest": "~0.8.2",
1414
"mocha": "~1.15.1",
15-
"loopback-datasource-juggler": "~1.2.7",
15+
"loopback-datasource-juggler": "~1.2.7"
16+
},
17+
"peerDependencies": {
1618
"loopback": "~1.3.3"
1719
}
1820
}

0 commit comments

Comments
 (0)