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

Commit 977f3a2

Browse files
committed
Merge pull request #9 from strongloop/refactor/loopback-dep
Refactor/loopback dep
2 parents f49dcd5 + 5b9cf3e commit 977f3a2

File tree

5 files changed

+280
-456
lines changed

5 files changed

+280
-456
lines changed

index.js

Lines changed: 4 additions & 297 deletions
Original file line numberDiff line numberDiff line change
@@ -1,298 +1,5 @@
1-
var _describe = {};
2-
var _it = {};
3-
var _beforeEach = {};
4-
var helpers = exports = module.exports = {
5-
describe: _describe,
6-
it: _it,
7-
beforeEach: _beforeEach
8-
};
9-
var loopback = require('loopback');
10-
var assert = require('assert');
11-
var request = require('supertest');
12-
13-
_beforeEach.withApp = function(app) {
14-
beforeEach(function() {
15-
this.app = app;
16-
var _request = this.request = request(app);
17-
this.post = _request.post;
18-
this.get = _request.get;
19-
this.put = _request.put;
20-
this.del = _request.del;
21-
});
22-
}
23-
24-
_beforeEach.withDefaultDataSource = function(dataSource) {
25-
beforeEach(function() {
26-
this.dataSource = dataSource;
27-
28-
// TODO(ritch) this should not be required
29-
loopback.AccessToken.attachTo(dataSource);
30-
});
31-
}
32-
33-
_describe.model = function describeModel(app, modelName, cb) {
34-
assert(app, 'describeModel called without app');
35-
36-
describe('Model: ' + modelName, function() {
37-
beforeEach(function() {
38-
this.app = app;
39-
this.model = loopback.getModel(modelName);
40-
assert(this.model, 'you must define a model before testing it');
41-
});
42-
cb();
43-
});
44-
}
45-
46-
function mixin(obj, into) {
47-
Object.keys(obj).forEach(function(key) {
48-
if(typeof obj[key] === 'function') {
49-
into[key] = obj[key];
50-
}
51-
});
52-
}
53-
54-
_describe.staticMethod = function(methodName, cb) {
55-
describe('.' + methodName, function() {
56-
beforeEach(function() {
57-
this.method = methodName;
58-
this.isStaticMethod = true;
59-
});
60-
cb();
61-
});
62-
}
63-
64-
_describe.instanceMethod = function(methodName, cb) {
65-
describe('.prototype.' + methodName, function() {
66-
beforeEach(function() {
67-
this.method = methodName;
68-
this.isInstanceMethod = true;
69-
});
70-
cb();
71-
});
72-
}
73-
74-
_beforeEach.withArgs = function() {
75-
var args = Array.prototype.slice.call(arguments, 0);
76-
beforeEach(function() {
77-
this.args = args;
78-
});
79-
}
80-
81-
_beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
82-
var modelKey = modelName
83-
84-
if(typeof attrs === 'funciton') {
85-
optionalHandler = attrs;
86-
attrs = undefined;
87-
}
88-
89-
if(typeof optionalHandler === 'string') {
90-
modelKey = optionalHandler;
91-
}
92-
93-
attrs = attrs || {};
94-
95-
var model = loopback.getModel(modelName);
96-
97-
assert(model, 'cannot get model of name ' + modelName);
98-
99-
beforeEach(function(done) {
100-
var test = this;
101-
102-
if(!model.dataSource) {
103-
model.attachTo(this.dataSource);
104-
}
105-
106-
assert(model.dataSource, modelName + ' is not attached to a dataSource');
107-
assert(
108-
typeof model.create === 'function',
109-
modelName + ' does not have a create method'
110-
);
111-
112-
model.create(attrs, function(err, result) {
113-
if(err) {
114-
console.error(err.message);
115-
if(err.details) console.error(err.details);
116-
done(err);
117-
} else {
118-
test[modelKey] = result;
119-
done();
120-
}
121-
});
122-
});
123-
124-
if(typeof optionalHandler === 'function') {
125-
beforeEach(optionalHandler);
126-
}
127-
128-
afterEach(function(done) {
129-
this[modelKey].destroy(done);
130-
});
131-
}
132-
133-
_beforeEach.givenUser = function(attrs, optionalHandler) {
134-
_beforeEach.givenModel('user', attrs, optionalHandler);
135-
}
136-
137-
_beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
138-
_beforeEach.givenUser(credentials, function(done) {
139-
var test = this;
140-
this.user.constructor.login(credentials, function(err, token) {
141-
if(err) {
142-
done(err);
143-
} else {
144-
test.loggedInAccessToken = token;
145-
done();
146-
}
147-
});
148-
});
149-
150-
afterEach(function(done) {
151-
var test = this;
152-
this.loggedInAccessToken.destroy(function(err) {
153-
if(err) return done(err);
154-
test.loggedInAccessToken = undefined;
155-
done();
156-
});
157-
});
158-
}
159-
160-
_beforeEach.givenAnUnauthenticatedToken = function(attrs, optionalHandler) {
161-
_beforeEach.givenModel('accessToken', attrs, optionalHandler);
162-
}
163-
164-
_beforeEach.givenAnAnonymousToken = function(attrs, optionalHandler) {
165-
_beforeEach.givenModel('accessToken', {id: '$anonymous'}, optionalHandler);
166-
}
167-
168-
_describe.whenCalledRemotely = function(verb, url, cb) {
169-
var urlStr = url;
170-
if(typeof url === 'function') {
171-
urlStr = '/<dynamic>';
172-
}
173-
174-
describe(verb.toUpperCase() + ' ' + urlStr, function() {
175-
beforeEach(function(cb) {
176-
if(typeof url === 'function') {
177-
url = url.call(this);
178-
}
179-
this.remotely = true;
180-
this.verb = verb.toUpperCase();
181-
this.url = this.url || url;
182-
var methodForVerb = verb.toLowerCase();
183-
if(methodForVerb === 'delete') methodForVerb = 'del';
184-
185-
this.http = this.request[methodForVerb](this.url);
186-
this.url = undefined;
187-
this.http.set('Accept', 'application/json');
188-
if(this.loggedInAccessToken) {
189-
this.http.set('authorization', this.loggedInAccessToken.id);
190-
}
191-
this.req = this.http.req;
192-
var test = this;
193-
this.http.end(function(err) {
194-
test.req = test.http.req;
195-
test.res = test.http.res;
196-
test.url = undefined;
197-
cb();
198-
});
199-
});
200-
201-
cb();
202-
});
203-
}
204-
205-
_describe.whenLoggedInAsUser = function(credentials, cb) {
206-
describe('when logged in as user', function () {
207-
_beforeEach.givenLoggedInUser(credentials);
208-
cb();
209-
});
210-
}
211-
212-
_describe.whenCalledByUser = function(credentials, verb, url, cb) {
213-
describe('when called by logged in user', function () {
214-
_beforeEach.givenLoggedInUser(credentials);
215-
_describe.whenCalledRemotely(verb, url, cb);
216-
});
217-
}
218-
219-
_describe.whenCalledAnonymously = function(verb, url, cb) {
220-
describe('when called anonymously', function () {
221-
_beforeEach.givenAnAnonymousToken();
222-
_describe.whenCalledRemotely(verb, url, cb);
223-
});
224-
}
225-
226-
_describe.whenCalledUnauthenticated = function(verb, url, cb) {
227-
describe('when called with unauthenticated token', function () {
228-
_beforeEach.givenAnAnonymousToken();
229-
_describe.whenCalledRemotely(verb, url, cb);
230-
});
231-
}
232-
233-
_it.shouldBeAllowed = function() {
234-
it('should be allowed', function() {
235-
assert(this.req);
236-
assert(this.res);
237-
assert.notEqual(this.res.statusCode, 401);
238-
});
239-
}
240-
241-
_it.shouldBeDenied = function() {
242-
it('should not be allowed', function() {
243-
assert(this.res);
244-
var status = this.res.statusCode;
245-
assert(status === 401 || status === 404);
246-
});
247-
}
248-
249-
_it.shouldNotBeFound = function() {
250-
it('should not be found', function() {
251-
assert(this.res);
252-
assert.equal(this.res.statusCode, 404);
253-
});
254-
}
255-
256-
_it.shouldBeAllowedWhenCalledAnonymously =
257-
function(verb, url) {
258-
_describe.whenCalledAnonymously(verb, url, function() {
259-
_it.shouldBeAllowed();
260-
});
261-
}
262-
263-
_it.shouldBeDeniedWhenCalledAnonymously =
264-
function(verb, url) {
265-
_describe.whenCalledAnonymously(verb, url, function() {
266-
_it.shouldBeDenied();
267-
});
268-
}
269-
270-
_it.shouldBeAllowedWhenCalledUnauthenticated =
271-
function(verb, url) {
272-
_describe.whenCalledUnauthenticated(verb, url, function() {
273-
_it.shouldBeAllowed();
274-
});
275-
}
276-
277-
_it.shouldBeDeniedWhenCalledUnauthenticated =
278-
function(verb, url) {
279-
_describe.whenCalledUnauthenticated(verb, url, function() {
280-
_it.shouldBeDenied();
281-
});
282-
}
283-
284-
_it.shouldBeAllowedWhenCalledByUser =
285-
function(credentials, verb, url) {
286-
_describe.whenCalledByUser(credentials, verb, url, function() {
287-
_it.shouldBeAllowed();
288-
});
289-
}
290-
291-
_it.shouldBeDeniedWhenCalledByUser =
292-
function(credentials, verb, url) {
293-
_describe.whenCalledByUser(credentials, verb, url, function() {
294-
_it.shouldBeDenied();
295-
});
296-
}
297-
1+
var helpers = require('./lib/helpers');
2+
exports.describe = helpers.describe;
3+
exports.it = helpers.it;
4+
exports.beforeEach = helpers.beforeEach;
2985
exports.TestDataBuilder = require('./lib/test-data-builder');

0 commit comments

Comments
 (0)