Backport separate error checking and done logic

This commit is contained in:
Simon Ho 2016-05-06 13:50:01 -07:00
parent 9311e918cb
commit 25ade96d27
29 changed files with 737 additions and 146 deletions

View File

@ -137,6 +137,7 @@ describe('access control - integration', function() {
var userCounter; var userCounter;
function newUserData() { function newUserData() {
userCounter = userCounter ? ++userCounter : 1; userCounter = userCounter ? ++userCounter : 1;
return { return {
email: 'new-' + userCounter + '@test.test', email: 'new-' + userCounter + '@test.test',
password: 'test' password: 'test'
@ -214,6 +215,7 @@ describe('access control - integration', function() {
balance: 100 balance: 100
}, function(err, act) { }, function(err, act) {
self.url = '/api/accounts/' + act.id; self.url = '/api/accounts/' + act.id;
done(); done();
}); });

View File

@ -149,7 +149,8 @@ describe('loopback.token(options)', function() {
.set('authorization', id) .set('authorization', id)
.end(function(err, res) { .end(function(err, res) {
assert(!err); assert(!err);
assert.deepEqual(res.body, {userId: userId}); assert.deepEqual(res.body, { userId: userId });
done(); done();
}); });
}); });
@ -164,7 +165,8 @@ describe('loopback.token(options)', function() {
.set('authorization', id) .set('authorization', id)
.end(function(err, res) { .end(function(err, res) {
assert(!err); assert(!err);
assert.deepEqual(res.body, {userId: userId, state: 1}); assert.deepEqual(res.body, { userId: userId, state: 1 });
done(); done();
}); });
}); });
@ -179,7 +181,8 @@ describe('loopback.token(options)', function() {
.set('authorization', id) .set('authorization', id)
.end(function(err, res) { .end(function(err, res) {
assert(!err); assert(!err);
assert.deepEqual(res.body, {userId: userId, state: 1}); assert.deepEqual(res.body, { userId: userId, state: 1 });
done(); done();
}); });
}); });
@ -188,6 +191,7 @@ describe('loopback.token(options)', function() {
var tokenStub = { id: 'stub id' }; var tokenStub = { id: 'stub id' };
app.use(function(req, res, next) { app.use(function(req, res, next) {
req.accessToken = tokenStub; req.accessToken = tokenStub;
next(); next();
}); });
app.use(loopback.token({ model: Token })); app.use(loopback.token({ model: Token }));
@ -200,7 +204,9 @@ describe('loopback.token(options)', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.eql(tokenStub); expect(res.body).to.eql(tokenStub);
done(); done();
}); });
}); });
@ -211,6 +217,7 @@ describe('loopback.token(options)', function() {
var tokenStub = { id: 'stub id' }; var tokenStub = { id: 'stub id' };
app.use(function(req, res, next) { app.use(function(req, res, next) {
req.accessToken = tokenStub; req.accessToken = tokenStub;
next(); next();
}); });
app.use(loopback.token({ model: Token })); app.use(loopback.token({ model: Token }));
@ -223,7 +230,9 @@ describe('loopback.token(options)', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.eql(tokenStub); expect(res.body).to.eql(tokenStub);
done(); done();
}); });
}); });
@ -234,6 +243,7 @@ describe('loopback.token(options)', function() {
var tokenStub = { id: 'stub id' }; var tokenStub = { id: 'stub id' };
app.use(function(req, res, next) { app.use(function(req, res, next) {
req.accessToken = tokenStub; req.accessToken = tokenStub;
next(); next();
}); });
app.use(loopback.token({ app.use(loopback.token({
@ -249,7 +259,9 @@ describe('loopback.token(options)', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.eql(tokenStub); expect(res.body).to.eql(tokenStub);
done(); done();
}); });
}); });
@ -262,6 +274,7 @@ describe('loopback.token(options)', function() {
app.use(function(req, res, next) { app.use(function(req, res, next) {
req.accessToken = tokenStub; req.accessToken = tokenStub;
next(); next();
}); });
app.use(loopback.token({ app.use(loopback.token({
@ -278,12 +291,14 @@ describe('loopback.token(options)', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.eql({ expect(res.body).to.eql({
id: token.id, id: token.id,
ttl: token.ttl, ttl: token.ttl,
userId: token.userId, userId: token.userId,
created: token.created.toJSON(), created: token.created.toJSON(),
}); });
done(); done();
}); });
}); });
@ -306,6 +321,7 @@ describe('AccessToken', function() {
it('should be validateable', function(done) { it('should be validateable', function(done) {
this.token.validate(function(err, isValid) { this.token.validate(function(err, isValid) {
assert(isValid); assert(isValid);
done(); done();
}); });
}); });
@ -321,7 +337,9 @@ describe('AccessToken', function() {
Token.findForRequest(req, function(err, token) { Token.findForRequest(req, function(err, token) {
if (err) return done(err); if (err) return done(err);
expect(token.id).to.eql(expectedTokenId); expect(token.id).to.eql(expectedTokenId);
done(); done();
}); });
}); });
@ -355,9 +373,11 @@ describe('app.enableAuth()', function() {
if (err) { if (err) {
return done(err); return done(err);
} }
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert(errorResponse); assert(errorResponse);
assert.equal(errorResponse.code, 'AUTHORIZATION_REQUIRED'); assert.equal(errorResponse.code, 'AUTHORIZATION_REQUIRED');
done(); done();
}); });
}); });
@ -371,9 +391,11 @@ describe('app.enableAuth()', function() {
if (err) { if (err) {
return done(err); return done(err);
} }
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert(errorResponse); assert(errorResponse);
assert.equal(errorResponse.code, 'ACCESS_DENIED'); assert.equal(errorResponse.code, 'ACCESS_DENIED');
done(); done();
}); });
}); });
@ -387,9 +409,11 @@ describe('app.enableAuth()', function() {
if (err) { if (err) {
return done(err); return done(err);
} }
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert(errorResponse); assert(errorResponse);
assert.equal(errorResponse.code, 'MODEL_NOT_FOUND'); assert.equal(errorResponse.code, 'MODEL_NOT_FOUND');
done(); done();
}); });
}); });
@ -403,9 +427,11 @@ describe('app.enableAuth()', function() {
if (err) { if (err) {
return done(err); return done(err);
} }
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert(errorResponse); assert(errorResponse);
assert.equal(errorResponse.code, 'AUTHORIZATION_REQUIRED'); assert.equal(errorResponse.code, 'AUTHORIZATION_REQUIRED');
done(); done();
}); });
}); });
@ -436,7 +462,9 @@ describe('app.enableAuth()', function() {
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.token.id).to.eql(token.id); expect(res.body.token.id).to.eql(token.id);
done(); done();
}); });
}); });
@ -446,7 +474,9 @@ function createTestingToken(done) {
var test = this; var test = this;
Token.create({userId: '123'}, function(err, token) { Token.create({userId: '123'}, function(err, token) {
if (err) return done(err); if (err) return done(err);
test.token = token; test.token = token;
done(); done();
}); });
} }

View File

@ -387,7 +387,9 @@ describe('access check', function() {
MyTestModel.beforeRemote('find', function(ctx, next) { MyTestModel.beforeRemote('find', function(ctx, next) {
// ensure this is called after checkAccess // ensure this is called after checkAccess
if (!checkAccessCalled) return done(new Error('incorrect order')); if (!checkAccessCalled) return done(new Error('incorrect order'));
beforeHookCalled = true; beforeHookCalled = true;
next(); next();
}); });
@ -396,6 +398,7 @@ describe('access check', function() {
.end(function(err, result) { .end(function(err, result) {
assert(beforeHookCalled, 'the before hook should be called'); assert(beforeHookCalled, 'the before hook should be called');
assert(checkAccessCalled, 'checkAccess should have been called'); assert(checkAccessCalled, 'checkAccess should have been called');
done(); done();
}); });
}); });

View File

@ -39,10 +39,12 @@ describe('app', function() {
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql([ expect(steps).to.eql([
'initial', 'session', 'auth', 'parse', 'initial', 'session', 'auth', 'parse',
'main', 'routes', 'files', 'final' 'main', 'routes', 'files', 'final'
]); ]);
done(); done();
}); });
}); });
@ -53,7 +55,9 @@ describe('app', function() {
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql(['first', 'second']); expect(steps).to.eql(['first', 'second']);
done(); done();
}); });
}); });
@ -65,7 +69,9 @@ describe('app', function() {
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql(['routes:before', 'main', 'routes:after']); expect(steps).to.eql(['routes:before', 'main', 'routes:after']);
done(); done();
}); });
}); });
@ -85,7 +91,9 @@ describe('app', function() {
expect(found).have.property('phase', 'routes:before'); expect(found).have.property('phase', 'routes:before');
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql(['my-handler', 'extra-handler']); expect(steps).to.eql(['my-handler', 'extra-handler']);
done(); done();
}); });
}); });
@ -103,7 +111,9 @@ describe('app', function() {
expect(found).have.property('phase', 'routes:before'); expect(found).have.property('phase', 'routes:before');
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql(['my-handler']); expect(steps).to.eql(['my-handler']);
done(); done();
}); });
}); });
@ -121,7 +131,9 @@ describe('app', function() {
expect(found).have.property('phase', 'routes:before'); expect(found).have.property('phase', 'routes:before');
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql(['my-handler']); expect(steps).to.eql(['my-handler']);
done(); done();
}); });
}); });
@ -131,6 +143,7 @@ describe('app', function() {
app.middleware('initial', function(req, res, next) { app.middleware('initial', function(req, res, next) {
steps.push('initial'); steps.push('initial');
next(expectedError); next(expectedError);
}); });
@ -138,12 +151,15 @@ describe('app', function() {
app.use(function errorHandler(err, req, res, next) { app.use(function errorHandler(err, req, res, next) {
expect(err).to.equal(expectedError); expect(err).to.equal(expectedError);
steps.push('error'); steps.push('error');
next(); next();
}); });
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql(['initial', 'error']); expect(steps).to.eql(['initial', 'error']);
done(); done();
}); });
}); });
@ -157,6 +173,7 @@ describe('app', function() {
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
expect(err).to.equal(expectedError); expect(err).to.equal(expectedError);
done(); done();
}); });
}); });
@ -175,12 +192,15 @@ describe('app', function() {
app.middleware('initial', function(err, req, res, next) { app.middleware('initial', function(err, req, res, next) {
handledError = err; handledError = err;
next(); next();
}); });
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
expect(handledError).to.equal(expectedError); expect(handledError).to.equal(expectedError);
done(); done();
}); });
}); });
@ -193,7 +213,9 @@ describe('app', function() {
function(url, next) { executeMiddlewareHandlers(app, url, next); }, function(url, next) { executeMiddlewareHandlers(app, url, next); },
function(err) { function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql(['/scope', '/scope/item']); expect(steps).to.eql(['/scope', '/scope/item']);
done(); done();
}); });
}); });
@ -206,7 +228,9 @@ describe('app', function() {
function(url, next) { executeMiddlewareHandlers(app, url, next); }, function(url, next) { executeMiddlewareHandlers(app, url, next); },
function(err) { function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql(['/a', '/b']); expect(steps).to.eql(['/a', '/b']);
done(); done();
}); });
}); });
@ -219,7 +243,9 @@ describe('app', function() {
function(url, next) { executeMiddlewareHandlers(app, url, next); }, function(url, next) { executeMiddlewareHandlers(app, url, next); },
function(err) { function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql(['/a', '/b', '/scope']); expect(steps).to.eql(['/a', '/b', '/scope']);
done(); done();
}); });
}); });
@ -227,12 +253,15 @@ describe('app', function() {
it('sets req.url to a sub-path', function(done) { it('sets req.url to a sub-path', function(done) {
app.middleware('initial', ['/scope'], function(req, res, next) { app.middleware('initial', ['/scope'], function(req, res, next) {
steps.push(req.url); steps.push(req.url);
next(); next();
}); });
executeMiddlewareHandlers(app, '/scope/id', function(err) { executeMiddlewareHandlers(app, '/scope/id', function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql(['/id']); expect(steps).to.eql(['/id']);
done(); done();
}); });
}); });
@ -244,11 +273,13 @@ describe('app', function() {
app.middleware('initial', function(rq, rs, next) { app.middleware('initial', function(rq, rs, next) {
req = rq; req = rq;
res = rs; res = rs;
next(); next();
}); });
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
expect(getObjectAndPrototypeKeys(req), 'request').to.include.members([ expect(getObjectAndPrototypeKeys(req), 'request').to.include.members([
'accepts', 'accepts',
'get', 'get',
@ -278,12 +309,15 @@ describe('app', function() {
var reqProps; var reqProps;
app.middleware('initial', function(req, res, next) { app.middleware('initial', function(req, res, next) {
reqProps = { baseUrl: req.baseUrl, originalUrl: req.originalUrl }; reqProps = { baseUrl: req.baseUrl, originalUrl: req.originalUrl };
next(); next();
}); });
executeMiddlewareHandlers(app, '/test/url', function(err) { executeMiddlewareHandlers(app, '/test/url', function(err) {
if (err) return done(err); if (err) return done(err);
expect(reqProps).to.eql({ baseUrl: '', originalUrl: '/test/url' }); expect(reqProps).to.eql({ baseUrl: '', originalUrl: '/test/url' });
done(); done();
}); });
}); });
@ -295,7 +329,9 @@ describe('app', function() {
executeMiddlewareHandlers(app, '/test', function(err) { executeMiddlewareHandlers(app, '/test', function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql(['route', 'files']); expect(steps).to.eql(['route', 'files']);
done(); done();
}); });
}); });
@ -315,7 +351,9 @@ describe('app', function() {
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done; if (err) return done;
expect(steps).to.eql(numbers); expect(steps).to.eql(numbers);
done(); done();
}); });
}); });
@ -329,6 +367,7 @@ describe('app', function() {
mountpath: req.app.mountpath, mountpath: req.app.mountpath,
parent: req.app.parent parent: req.app.parent
}; };
next(); next();
}); });
subapp.on('mount', function() { mountWasEmitted = true; }); subapp.on('mount', function() { mountWasEmitted = true; });
@ -337,11 +376,13 @@ describe('app', function() {
executeMiddlewareHandlers(app, '/mountpath/test', function(err) { executeMiddlewareHandlers(app, '/mountpath/test', function(err) {
if (err) return done(err); if (err) return done(err);
expect(mountWasEmitted, 'mountWasEmitted').to.be.true; expect(mountWasEmitted, 'mountWasEmitted').to.be.true;
expect(data).to.eql({ expect(data).to.eql({
mountpath: '/mountpath', mountpath: '/mountpath',
parent: app parent: app
}); });
done(); done();
}); });
}); });
@ -355,25 +396,30 @@ describe('app', function() {
subapp.use(function verifyTestAssumptions(req, res, next) { subapp.use(function verifyTestAssumptions(req, res, next) {
expect(req.__proto__).to.not.equal(expected.req); expect(req.__proto__).to.not.equal(expected.req);
expect(res.__proto__).to.not.equal(expected.res); expect(res.__proto__).to.not.equal(expected.res);
next(); next();
}); });
app.middleware('initial', function saveOriginalValues(req, res, next) { app.middleware('initial', function saveOriginalValues(req, res, next) {
expected.req = req.__proto__; expected.req = req.__proto__;
expected.res = res.__proto__; expected.res = res.__proto__;
next(); next();
}); });
app.middleware('routes', subapp); app.middleware('routes', subapp);
app.middleware('final', function saveActualValues(req, res, next) { app.middleware('final', function saveActualValues(req, res, next) {
actual.req = req.__proto__; actual.req = req.__proto__;
actual.res = res.__proto__; actual.res = res.__proto__;
next(); next();
}); });
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
expect(actual.req, 'req').to.equal(expected.req); expect(actual.req, 'req').to.equal(expected.req);
expect(actual.res, 'res').to.equal(expected.res); expect(actual.res, 'res').to.equal(expected.res);
done(); done();
}); });
}); });
@ -388,6 +434,7 @@ describe('app', function() {
function pathSavingHandler() { function pathSavingHandler() {
return function(req, res, next) { return function(req, res, next) {
steps.push(req.originalUrl); steps.push(req.originalUrl);
next(); next();
}; };
} }
@ -411,6 +458,7 @@ describe('app', function() {
var args = Array.prototype.slice.apply(arguments); var args = Array.prototype.slice.apply(arguments);
return function(req, res, next) { return function(req, res, next) {
steps.push(args); steps.push(args);
next(); next();
}; };
}; };
@ -461,12 +509,14 @@ describe('app', function() {
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql([ expect(steps).to.eql([
['before'], ['before'],
[expectedConfig], [expectedConfig],
['after', 2], ['after', 2],
[{x: 1}] [{x: 1}]
]); ]);
done(); done();
}); });
}); });
@ -477,6 +527,7 @@ describe('app', function() {
function factory() { function factory() {
return function(req, res, next) { return function(req, res, next) {
steps.push(req.originalUrl); steps.push(req.originalUrl);
next(); next();
}; };
}, },
@ -490,7 +541,9 @@ describe('app', function() {
function(url, next) { executeMiddlewareHandlers(app, url, next); }, function(url, next) { executeMiddlewareHandlers(app, url, next); },
function(err) { function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql(['/a', '/b', '/scope']); expect(steps).to.eql(['/a', '/b', '/scope']);
done(); done();
}); });
}); });
@ -547,13 +600,16 @@ describe('app', function() {
names.forEach(function(it) { names.forEach(function(it) {
app.middleware(it, function(req, res, next) { app.middleware(it, function(req, res, next) {
steps.push(it); steps.push(it);
next(); next();
}); });
}); });
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
expect(steps).to.eql(names); expect(steps).to.eql(names);
done(); done();
}); });
} }
@ -786,6 +842,7 @@ describe('app', function() {
app.listen(function() { app.listen(function() {
expect(app.get('port'), 'port').to.not.equal(0); expect(app.get('port'), 'port').to.not.equal(0);
done(); done();
}); });
}); });
@ -799,6 +856,7 @@ describe('app', function() {
var host = process.platform === 'win32' ? 'localhost' : app.get('host'); var host = process.platform === 'win32' ? 'localhost' : app.get('host');
var expectedUrl = 'http://' + host + ':' + app.get('port') + '/'; var expectedUrl = 'http://' + host + ':' + app.get('port') + '/';
expect(app.get('url'), 'url').to.equal(expectedUrl); expect(app.get('url'), 'url').to.equal(expectedUrl);
done(); done();
}); });
}); });
@ -809,6 +867,7 @@ describe('app', function() {
app.listen(0, '127.0.0.1', function() { app.listen(0, '127.0.0.1', function() {
expect(app.get('port'), 'port').to.not.equal(0).and.not.equal(1); expect(app.get('port'), 'port').to.not.equal(0).and.not.equal(1);
expect(this.address().address).to.equal('127.0.0.1'); expect(this.address().address).to.equal('127.0.0.1');
done(); done();
}); });
}); });
@ -819,6 +878,7 @@ describe('app', function() {
app.set('port', 1); app.set('port', 1);
app.listen(0).on('listening', function() { app.listen(0).on('listening', function() {
expect(app.get('port'), 'port') .to.not.equal(0).and.not.equal(1); expect(app.get('port'), 'port') .to.not.equal(0).and.not.equal(1);
done(); done();
}); });
} }
@ -833,6 +893,7 @@ describe('app', function() {
app.listen() app.listen()
.on('listening', function() { .on('listening', function() {
expect(this.address().address).to.equal('127.0.0.1'); expect(this.address().address).to.equal('127.0.0.1');
done(); done();
}); });
}); });

View File

@ -22,6 +22,7 @@ describe('PersistedModel.createChangeStream()', function() {
changes.on('data', function(change) { changes.on('data', function(change) {
expect(change.type).to.equal('create'); expect(change.type).to.equal('create');
changes.destroy(); changes.destroy();
done(); done();
}); });
@ -36,6 +37,7 @@ describe('PersistedModel.createChangeStream()', function() {
changes.on('data', function(change) { changes.on('data', function(change) {
expect(change.type).to.equal('update'); expect(change.type).to.equal('update');
changes.destroy(); changes.destroy();
done(); done();
}); });
newScore.updateAttributes({ newScore.updateAttributes({
@ -52,6 +54,7 @@ describe('PersistedModel.createChangeStream()', function() {
changes.on('data', function(change) { changes.on('data', function(change) {
expect(change.type).to.equal('remove'); expect(change.type).to.equal('remove');
changes.destroy(); changes.destroy();
done(); done();
}); });

View File

@ -33,9 +33,11 @@ describe('Change', function() {
}; };
TestModel.create(test.data, function(err, model) { TestModel.create(test.data, function(err, model) {
if (err) return done(err); if (err) return done(err);
test.model = model; test.model = model;
test.modelId = model.id; test.modelId = model.id;
test.revisionForModel = Change.revisionForInst(model); test.revisionForModel = Change.revisionForInst(model);
done(); done();
}); });
}); });
@ -66,6 +68,7 @@ describe('Change', function() {
var test = this; var test = this;
Change.rectifyModelChanges(this.modelName, [this.modelId], function(err, trackedChanges) { Change.rectifyModelChanges(this.modelName, [this.modelId], function(err, trackedChanges) {
if (err) return done(err); if (err) return done(err);
done(); done();
}); });
}); });
@ -74,6 +77,7 @@ describe('Change', function() {
var test = this; var test = this;
Change.find(function(err, trackedChanges) { Change.find(function(err, trackedChanges) {
assert.equal(trackedChanges[0].modelId, test.modelId.toString()); assert.equal(trackedChanges[0].modelId, test.modelId.toString());
done(); done();
}); });
}); });
@ -81,6 +85,7 @@ describe('Change', function() {
it('should only create one change', function(done) { it('should only create one change', function(done) {
Change.count(function(err, count) { Change.count(function(err, count) {
assert.equal(count, 1); assert.equal(count, 1);
done(); done();
}); });
}); });
@ -103,6 +108,7 @@ describe('Change', function() {
Change.find() Change.find()
.then(function(trackedChanges) { .then(function(trackedChanges) {
assert.equal(trackedChanges[0].modelId, test.modelId.toString()); assert.equal(trackedChanges[0].modelId, test.modelId.toString());
done(); done();
}) })
.catch(done); .catch(done);
@ -112,6 +118,7 @@ describe('Change', function() {
Change.count() Change.count()
.then(function(count) { .then(function(count) {
assert.equal(count, 1); assert.equal(count, 1);
done(); done();
}) })
.catch(done); .catch(done);
@ -126,7 +133,9 @@ describe('Change', function() {
var test = this; var test = this;
Change.findOrCreateChange(this.modelName, this.modelId, function(err, result) { Change.findOrCreateChange(this.modelName, this.modelId, function(err, result) {
if (err) return done(err); if (err) return done(err);
test.result = result; test.result = result;
done(); done();
}); });
}); });
@ -135,7 +144,9 @@ describe('Change', function() {
var test = this; var test = this;
Change.findById(this.result.id, function(err, change) { Change.findById(this.result.id, function(err, change) {
if (err) return done(err); if (err) return done(err);
assert.equal(change.id, test.result.id); assert.equal(change.id, test.result.id);
done(); done();
}); });
}); });
@ -147,6 +158,7 @@ describe('Change', function() {
Change.findOrCreateChange(this.modelName, this.modelId) Change.findOrCreateChange(this.modelName, this.modelId)
.then(function(result) { .then(function(result) {
test.result = result; test.result = result;
done(); done();
}) })
.catch(done); .catch(done);
@ -156,7 +168,9 @@ describe('Change', function() {
var test = this; var test = this;
Change.findById(this.result.id, function(err, change) { Change.findById(this.result.id, function(err, change) {
if (err) return done(err); if (err) return done(err);
assert.equal(change.id, test.result.id); assert.equal(change.id, test.result.id);
done(); done();
}); });
}); });
@ -170,6 +184,7 @@ describe('Change', function() {
modelId: test.modelId modelId: test.modelId
}, function(err, change) { }, function(err, change) {
test.existingChange = change; test.existingChange = change;
done(); done();
}); });
}); });
@ -178,7 +193,9 @@ describe('Change', function() {
var test = this; var test = this;
Change.findOrCreateChange(this.modelName, this.modelId, function(err, result) { Change.findOrCreateChange(this.modelName, this.modelId, function(err, result) {
if (err) return done(err); if (err) return done(err);
test.result = result; test.result = result;
done(); done();
}); });
}); });
@ -186,6 +203,7 @@ describe('Change', function() {
it('should find the entry', function(done) { it('should find the entry', function(done) {
var test = this; var test = this;
assert.equal(test.existingChange.id, test.result.id); assert.equal(test.existingChange.id, test.result.id);
done(); done();
}); });
}); });
@ -201,6 +219,7 @@ describe('Change', function() {
}, },
function(err, ch) { function(err, ch) {
change = ch; change = ch;
done(err); done(err);
}); });
}); });
@ -209,6 +228,7 @@ describe('Change', function() {
var test = this; var test = this;
change.rectify(function(err, ch) { change.rectify(function(err, ch) {
assert.equal(ch.rev, test.revisionForModel); assert.equal(ch.rev, test.revisionForModel);
done(); done();
}); });
}); });
@ -232,6 +252,7 @@ describe('Change', function() {
expect(change.type(), 'type').to.equal('update'); expect(change.type(), 'type').to.equal('update');
expect(change.prev, 'prev').to.equal(originalRev); expect(change.prev, 'prev').to.equal(originalRev);
expect(change.rev, 'rev').to.equal(test.revisionForModel); expect(change.rev, 'rev').to.equal(test.revisionForModel);
next(); next();
} }
], done); ], done);
@ -243,7 +264,9 @@ describe('Change', function() {
function checkpoint(next) { function checkpoint(next) {
TestModel.checkpoint(function(err, inst) { TestModel.checkpoint(function(err, inst) {
if (err) return next(err); if (err) return next(err);
cp = inst.seq; cp = inst.seq;
next(); next();
}); });
} }
@ -254,6 +277,7 @@ describe('Change', function() {
model.name += 'updated'; model.name += 'updated';
model.save(function(err) { model.save(function(err) {
test.revisionForModel = Change.revisionForInst(model); test.revisionForModel = Change.revisionForInst(model);
next(err); next(err);
}); });
} }
@ -269,8 +293,10 @@ describe('Change', function() {
change.rectify(function(err, c) { change.rectify(function(err, c) {
if (err) return done(err); if (err) return done(err);
expect(c.rev, 'rev').to.equal(originalRev); // sanity check expect(c.rev, 'rev').to.equal(originalRev); // sanity check
expect(c.checkpoint, 'checkpoint').to.equal(originalCheckpoint); expect(c.checkpoint, 'checkpoint').to.equal(originalCheckpoint);
done(); done();
}); });
}); });
@ -283,6 +309,7 @@ describe('Change', function() {
Change.findOrCreateChange(this.modelName, this.modelId) Change.findOrCreateChange(this.modelName, this.modelId)
.then(function(ch) { .then(function(ch) {
change = ch; change = ch;
done(); done();
}) })
.catch(done); .catch(done);
@ -293,6 +320,7 @@ describe('Change', function() {
change.rectify() change.rectify()
.then(function(ch) { .then(function(ch) {
assert.equal(ch.rev, test.revisionForModel); assert.equal(ch.rev, test.revisionForModel);
done(); done();
}) })
.catch(done); .catch(done);
@ -309,6 +337,7 @@ describe('Change', function() {
change.currentRevision(function(err, rev) { change.currentRevision(function(err, rev) {
assert.equal(rev, test.revisionForModel); assert.equal(rev, test.revisionForModel);
done(); done();
}); });
}); });
@ -325,6 +354,7 @@ describe('Change', function() {
change.currentRevision() change.currentRevision()
.then(function(rev) { .then(function(rev) {
assert.equal(rev, test.revisionForModel); assert.equal(rev, test.revisionForModel);
done(); done();
}) })
.catch(done); .catch(done);
@ -465,8 +495,10 @@ describe('Change', function() {
Change.diff(this.modelName, 0, remoteChanges, function(err, diff) { Change.diff(this.modelName, 0, remoteChanges, function(err, diff) {
if (err) return done(err); if (err) return done(err);
assert.equal(diff.deltas.length, 1); assert.equal(diff.deltas.length, 1);
assert.equal(diff.conflicts.length, 1); assert.equal(diff.conflicts.length, 1);
done(); done();
}); });
}); });
@ -485,6 +517,7 @@ describe('Change', function() {
.then(function(diff) { .then(function(diff) {
assert.equal(diff.deltas.length, 1); assert.equal(diff.deltas.length, 1);
assert.equal(diff.conflicts.length, 1); assert.equal(diff.conflicts.length, 1);
done(); done();
}) })
.catch(done); .catch(done);
@ -500,6 +533,7 @@ describe('Change', function() {
}; };
Change.diff(this.modelName, 0, [updateRecord], function(err, diff) { Change.diff(this.modelName, 0, [updateRecord], function(err, diff) {
if (err) return done(err); if (err) return done(err);
expect(diff.conflicts, 'conflicts').to.have.length(0); expect(diff.conflicts, 'conflicts').to.have.length(0);
expect(diff.deltas, 'deltas').to.have.length(1); expect(diff.deltas, 'deltas').to.have.length(1);
var actual = diff.deltas[0].toObject(); var actual = diff.deltas[0].toObject();
@ -511,6 +545,7 @@ describe('Change', function() {
prev: 'foo', // this is the current local revision prev: 'foo', // this is the current local revision
rev: 'foo-new', rev: 'foo-new',
}); });
done(); done();
}); });
}); });
@ -527,6 +562,7 @@ describe('Change', function() {
// with rev=foo CP=1 // with rev=foo CP=1
Change.diff(this.modelName, 2, [updateRecord], function(err, diff) { Change.diff(this.modelName, 2, [updateRecord], function(err, diff) {
if (err) return done(err); if (err) return done(err);
expect(diff.conflicts, 'conflicts').to.have.length(0); expect(diff.conflicts, 'conflicts').to.have.length(0);
expect(diff.deltas, 'deltas').to.have.length(1); expect(diff.deltas, 'deltas').to.have.length(1);
var actual = diff.deltas[0].toObject(); var actual = diff.deltas[0].toObject();
@ -538,6 +574,7 @@ describe('Change', function() {
prev: 'foo', // this is the current local revision prev: 'foo', // this is the current local revision
rev: 'foo-new', rev: 'foo-new',
}); });
done(); done();
}); });
}); });
@ -553,6 +590,7 @@ describe('Change', function() {
Change.diff(this.modelName, 0, [updateRecord], function(err, diff) { Change.diff(this.modelName, 0, [updateRecord], function(err, diff) {
if (err) return done(err); if (err) return done(err);
expect(diff.conflicts).to.have.length(0); expect(diff.conflicts).to.have.length(0);
expect(diff.deltas).to.have.length(1); expect(diff.deltas).to.have.length(1);
var actual = diff.deltas[0].toObject(); var actual = diff.deltas[0].toObject();
@ -564,6 +602,7 @@ describe('Change', function() {
prev: null, // this is the current local revision prev: null, // this is the current local revision
rev: 'new-rev', rev: 'new-rev',
}); });
done(); done();
}); });
}); });

View File

@ -25,7 +25,9 @@ describe('Checkpoint', function() {
function(next) { function(next) {
Checkpoint.current(function(err, seq) { Checkpoint.current(function(err, seq) {
if (err) next(err); if (err) next(err);
expect(seq).to.equal(3); expect(seq).to.equal(3);
next(); next();
}); });
} }
@ -38,9 +40,12 @@ describe('Checkpoint', function() {
function(next) { Checkpoint.current(next); } function(next) { Checkpoint.current(next); }
], function(err, list) { ], function(err, list) {
if (err) return done(err); if (err) return done(err);
Checkpoint.find(function(err, data) { Checkpoint.find(function(err, data) {
if (err) return done(err); if (err) return done(err);
expect(data).to.have.length(1); expect(data).to.have.length(1);
done(); done();
}); });
}); });
@ -52,6 +57,7 @@ describe('Checkpoint', function() {
function(next) { Checkpoint.bumpLastSeq(next); } function(next) { Checkpoint.bumpLastSeq(next); }
], function(err, list) { ], function(err, list) {
if (err) return done(err); if (err) return done(err);
Checkpoint.find(function(err, data) { Checkpoint.find(function(err, data) {
if (err) return done(err); if (err) return done(err);
// The invariant "we have at most 1 checkpoint instance" is preserved // The invariant "we have at most 1 checkpoint instance" is preserved
@ -64,6 +70,7 @@ describe('Checkpoint', function() {
// should be 2. // should be 2.
expect(list.map(function(it) {return it.seq;})) expect(list.map(function(it) {return it.seq;}))
.to.eql([2, 2]); .to.eql([2, 2]);
done(); done();
}); });
}); });
@ -72,6 +79,7 @@ describe('Checkpoint', function() {
it('Checkpoint.current() for non existing checkpoint should initialize checkpoint', function(done) { it('Checkpoint.current() for non existing checkpoint should initialize checkpoint', function(done) {
Checkpoint.current(function(err, seq) { Checkpoint.current(function(err, seq) {
expect(seq).to.equal(1); expect(seq).to.equal(1);
done(err); done(err);
}); });
}); });
@ -82,6 +90,7 @@ describe('Checkpoint', function() {
// `bumpLastSeq` for the first time not only initializes it to one, // `bumpLastSeq` for the first time not only initializes it to one,
// but also increments the initialized value by one. // but also increments the initialized value by one.
expect(cp.seq).to.equal(2); expect(cp.seq).to.equal(2);
done(err); done(err);
}); });
}); });

View File

@ -24,7 +24,9 @@ describe('RemoteConnector', function() {
foo: 'bar' foo: 'bar'
}, function(err, inst) { }, function(err, inst) {
if (err) return done(err); if (err) return done(err);
assert(inst.id); assert(inst.id);
done(); done();
}); });
}); });
@ -35,7 +37,9 @@ describe('RemoteConnector', function() {
}); });
m.save(function(err, data) { m.save(function(err, data) {
if (err) return done(err); if (err) return done(err);
assert(data.foo === 'bar'); assert(data.foo === 'bar');
done(); done();
}); });
}); });

View File

@ -32,8 +32,10 @@ describe('Replication', function() {
}, function(err, created) { }, function(err, created) {
LocalTestModel.replicate(0, TestModel, function() { LocalTestModel.replicate(0, TestModel, function() {
if (err) return done(err); if (err) return done(err);
TestModel.findOne({n: RANDOM}, function(err, found) {
TestModel.findOne({ n: RANDOM }, function(err, found) {
assert.equal(created.id, found.id); assert.equal(created.id, found.id);
done(); done();
}); });
}); });

View File

@ -66,6 +66,7 @@ describe('Email and SMTP', function() {
assert(mail.response); assert(mail.response);
assert(mail.envelope); assert(mail.envelope);
assert(mail.messageId); assert(mail.messageId);
done(err); done(err);
}); });
}); });
@ -83,6 +84,7 @@ describe('Email and SMTP', function() {
assert(mail.response); assert(mail.response);
assert(mail.envelope); assert(mail.envelope);
assert(mail.messageId); assert(mail.messageId);
done(err); done(err);
}); });
}); });

View File

@ -22,6 +22,7 @@ describe('loopback.errorHandler(options)', function() {
.get('/url-does-not-exist') .get('/url-does-not-exist')
.end(function(err, res) { .end(function(err, res) {
assert.ok(res.error.text.match(/<ul id="stacktrace"><li> &nbsp; &nbsp;at raiseUrlNotFoundError/)); assert.ok(res.error.text.match(/<ul id="stacktrace"><li> &nbsp; &nbsp;at raiseUrlNotFoundError/));
done(); done();
}); });
}); });
@ -38,6 +39,7 @@ describe('loopback.errorHandler(options)', function() {
.get('/url-does-not-exist') .get('/url-does-not-exist')
.end(function(err, res) { .end(function(err, res) {
assert.ok(res.error.text.match(/<ul id="stacktrace"><\/ul>/)); assert.ok(res.error.text.match(/<ul id="stacktrace"><\/ul>/));
done(); done();
}); });
}); });
@ -61,6 +63,7 @@ describe('loopback.errorHandler(options)', function() {
//assert //assert
expect(errorLogged) expect(errorLogged)
.to.have.property('message', 'Cannot GET /url-does-not-exist'); .to.have.property('message', 'Cannot GET /url-does-not-exist');
done(); done();
}); });

View File

@ -46,8 +46,10 @@ describe('hidden properties', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
var product = res.body[0]; var product = res.body[0];
assert.equal(product.secret, undefined); assert.equal(product.secret, undefined);
done(); done();
}); });
}); });
@ -60,9 +62,11 @@ describe('hidden properties', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
var category = res.body[0]; var category = res.body[0];
var product = category.products[0]; var product = category.products[0];
assert.equal(product.secret, undefined); assert.equal(product.secret, undefined);
done(); done();
}); });
}); });

View File

@ -21,7 +21,9 @@ describe('loopback application', function() {
'X', 'X',
function(err, res) { function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res).to.match(/\nX$/); expect(res).to.match(/\nX$/);
done(); done();
}); });
}); });

View File

@ -611,6 +611,7 @@ describe('loopback', function() {
}else { }else {
ctxx.result.data = 'context not available'; ctxx.result.data = 'context not available';
} }
next(); next();
}); });
@ -618,7 +619,9 @@ describe('loopback', function() {
.get('/TestModels/test') .get('/TestModels/test')
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.data).to.equal('a value stored in context'); expect(res.body.data).to.equal('a value stored in context');
done(); done();
}); });
}); });
@ -632,6 +635,7 @@ describe('loopback', function() {
var ctx = loopback.getCurrentContext(); var ctx = loopback.getCurrentContext();
expect(ctx).is.an('object'); expect(ctx).is.an('object');
expect(ctx.get('test-key')).to.equal('test-value'); expect(ctx.get('test-key')).to.equal('test-value');
done(); done();
}); });
}); });

View File

@ -33,6 +33,7 @@ describe('Memory Connector', function() {
function count() { function count() {
Product.count(function(err, count) { Product.count(function(err, count) {
assert.equal(count, 3); assert.equal(count, 3);
done(); done();
}); });
} }

View File

@ -17,6 +17,7 @@ describe('Application', function() {
assert.equal(app.owner, 'rfeng'); assert.equal(app.owner, 'rfeng');
assert.equal(app.name, 'MyTestApp'); assert.equal(app.name, 'MyTestApp');
assert.equal(app.description, 'My test application'); assert.equal(app.description, 'My test application');
done(err, result); done(err, result);
}); });
}); });
@ -29,6 +30,7 @@ describe('Application', function() {
assert.equal(app.owner, 'rfeng'); assert.equal(app.owner, 'rfeng');
assert.equal(app.name, 'MyTestApp'); assert.equal(app.name, 'MyTestApp');
assert.equal(app.description, 'My test application'); assert.equal(app.description, 'My test application');
done(); done();
}) })
.catch(function(err) { .catch(function(err) {
@ -52,6 +54,7 @@ describe('Application', function() {
assert(app.created); assert(app.created);
assert(app.modified); assert(app.modified);
assert.equal(typeof app.id, 'string'); assert.equal(typeof app.id, 'string');
done(err, result); done(err, result);
}); });
}); });
@ -102,6 +105,7 @@ describe('Application', function() {
serverApiKey: 'serverKey' serverApiKey: 'serverKey'
} }
}); });
done(err, result); done(err, result);
}); });
}); });
@ -121,6 +125,7 @@ describe('Application', function() {
assert(app.created); assert(app.created);
assert(app.modified); assert(app.modified);
registeredApp = app; registeredApp = app;
done(err, result); done(err, result);
}); });
}); });
@ -146,6 +151,7 @@ describe('Application', function() {
assert(app.created); assert(app.created);
assert(app.modified); assert(app.modified);
registeredApp = app; registeredApp = app;
done(err, result); done(err, result);
}); });
}); });
@ -172,6 +178,7 @@ describe('Application', function() {
assert(app.created); assert(app.created);
assert(app.modified); assert(app.modified);
registeredApp = app; registeredApp = app;
done(); done();
}) })
.catch(function(err) { .catch(function(err) {
@ -185,6 +192,7 @@ describe('Application', function() {
assert(app.id); assert(app.id);
assert(app.id === registeredApp.id); assert(app.id === registeredApp.id);
registeredApp = app; registeredApp = app;
done(err, result); done(err, result);
}); });
}); });
@ -196,6 +204,7 @@ describe('Application', function() {
assert(app.id); assert(app.id);
assert(app.id === registeredApp.id); assert(app.id === registeredApp.id);
registeredApp = app; registeredApp = app;
done(); done();
}) })
.catch(function(err) { .catch(function(err) {
@ -208,6 +217,7 @@ describe('Application', function() {
function(err, result) { function(err, result) {
assert.equal(result.application.id, registeredApp.id); assert.equal(result.application.id, registeredApp.id);
assert.equal(result.keyType, 'clientKey'); assert.equal(result.keyType, 'clientKey');
done(err, result); done(err, result);
}); });
}); });
@ -216,10 +226,11 @@ describe('Application', function() {
function(done) { function(done) {
Application.authenticate(registeredApp.id, registeredApp.clientKey) Application.authenticate(registeredApp.id, registeredApp.clientKey)
.then(function(result) { .then(function(result) {
assert.equal(result.application.id, registeredApp.id); assert.equal(result.application.id, registeredApp.id);
assert.equal(result.keyType, 'clientKey'); assert.equal(result.keyType, 'clientKey');
done();
}) done();
})
.catch(function(err) { .catch(function(err) {
done(err); done(err);
}); });
@ -230,6 +241,7 @@ describe('Application', function() {
function(err, result) { function(err, result) {
assert.equal(result.application.id, registeredApp.id); assert.equal(result.application.id, registeredApp.id);
assert.equal(result.keyType, 'javaScriptKey'); assert.equal(result.keyType, 'javaScriptKey');
done(err, result); done(err, result);
}); });
}); });
@ -239,6 +251,7 @@ describe('Application', function() {
function(err, result) { function(err, result) {
assert.equal(result.application.id, registeredApp.id); assert.equal(result.application.id, registeredApp.id);
assert.equal(result.keyType, 'restApiKey'); assert.equal(result.keyType, 'restApiKey');
done(err, result); done(err, result);
}); });
}); });
@ -248,6 +261,7 @@ describe('Application', function() {
function(err, result) { function(err, result) {
assert.equal(result.application.id, registeredApp.id); assert.equal(result.application.id, registeredApp.id);
assert.equal(result.keyType, 'masterKey'); assert.equal(result.keyType, 'masterKey');
done(err, result); done(err, result);
}); });
}); });
@ -257,6 +271,7 @@ describe('Application', function() {
function(err, result) { function(err, result) {
assert.equal(result.application.id, registeredApp.id); assert.equal(result.application.id, registeredApp.id);
assert.equal(result.keyType, 'windowsKey'); assert.equal(result.keyType, 'windowsKey');
done(err, result); done(err, result);
}); });
}); });
@ -265,6 +280,7 @@ describe('Application', function() {
Application.authenticate(registeredApp.id, 'invalid-key', Application.authenticate(registeredApp.id, 'invalid-key',
function(err, result) { function(err, result) {
assert(!result); assert(!result);
done(err, result); done(err, result);
}); });
}); });
@ -273,6 +289,7 @@ describe('Application', function() {
Application.authenticate(registeredApp.id, 'invalid-key') Application.authenticate(registeredApp.id, 'invalid-key')
.then(function(result) { .then(function(result) {
assert(!result); assert(!result);
done(); done();
}) })
.catch(function(err) { .catch(function(err) {
@ -309,6 +326,7 @@ describe('Application subclass', function() {
Application.findById(app.id, function(err, myApp) { Application.findById(app.id, function(err, myApp) {
assert(!err); assert(!err);
assert(myApp === null); assert(myApp === null);
done(err, myApp); done(err, myApp);
}); });
}); });

View File

@ -148,6 +148,7 @@ describe.onServer('Remote Methods', function() {
User.destroyAll(function() { User.destroyAll(function() {
User.count(function(err, count) { User.count(function(err, count) {
assert.equal(count, 0); assert.equal(count, 0);
done(); done();
}); });
}); });
@ -164,7 +165,9 @@ describe.onServer('Remote Methods', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
assert.equal(res.body, 123); assert.equal(res.body, 123);
done(); done();
}); });
}); });
@ -174,12 +177,12 @@ describe.onServer('Remote Methods', function() {
.get('/users/not-found') .get('/users/not-found')
.expect(404) .expect(404)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert(errorResponse); assert(errorResponse);
assert.equal(errorResponse.code, 'MODEL_NOT_FOUND'); assert.equal(errorResponse.code, 'MODEL_NOT_FOUND');
done(); done();
}); });
}); });
@ -192,6 +195,7 @@ describe.onServer('Remote Methods', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
var userId = res.body.id; var userId = res.body.id;
assert(userId); assert(userId);
request(app) request(app)
@ -200,8 +204,10 @@ describe.onServer('Remote Methods', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
assert.equal(res.body.first, 'x', 'first should be x'); assert.equal(res.body.first, 'x', 'first should be x');
assert(res.body.last === undefined, 'last should not be present'); assert(res.body.last === undefined, 'last should not be present');
done(); done();
}); });
}); });
@ -215,6 +221,7 @@ describe.onServer('Remote Methods', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
var userId = res.body.id; var userId = res.body.id;
assert(userId); assert(userId);
request(app) request(app)
@ -224,6 +231,7 @@ describe.onServer('Remote Methods', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
var post = res.body; var post = res.body;
request(app) request(app)
.get('/users/' + userId + '?filter[include]=posts') .get('/users/' + userId + '?filter[include]=posts')
@ -231,9 +239,11 @@ describe.onServer('Remote Methods', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
assert.equal(res.body.first, 'x', 'first should be x'); assert.equal(res.body.first, 'x', 'first should be x');
assert.equal(res.body.last, 'y', 'last should be y'); assert.equal(res.body.last, 'y', 'last should be y');
assert.deepEqual(post, res.body.posts[0]); assert.deepEqual(post, res.body.posts[0]);
done(); done();
}); });
}); });
@ -248,6 +258,7 @@ describe.onServer('Remote Methods', function() {
User.beforeRemote('create', function(ctx, user, next) { User.beforeRemote('create', function(ctx, user, next) {
hookCalled = true; hookCalled = true;
next(); next();
}); });
@ -259,7 +270,9 @@ describe.onServer('Remote Methods', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
assert(hookCalled, 'hook wasnt called'); assert(hookCalled, 'hook wasnt called');
done(); done();
}); });
}); });
@ -273,11 +286,13 @@ describe.onServer('Remote Methods', function() {
User.beforeRemote('create', function(ctx, user, next) { User.beforeRemote('create', function(ctx, user, next) {
assert(!afterCalled); assert(!afterCalled);
beforeCalled = true; beforeCalled = true;
next(); next();
}); });
User.afterRemote('create', function(ctx, user, next) { User.afterRemote('create', function(ctx, user, next) {
assert(beforeCalled); assert(beforeCalled);
afterCalled = true; afterCalled = true;
next(); next();
}); });
@ -289,8 +304,10 @@ describe.onServer('Remote Methods', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
assert(beforeCalled, 'before hook was not called'); assert(beforeCalled, 'before hook was not called');
assert(afterCalled, 'after hook was not called'); assert(afterCalled, 'after hook was not called');
done(); done();
}); });
}); });
@ -301,14 +318,17 @@ describe.onServer('Remote Methods', function() {
var actualError = 'hook not called'; var actualError = 'hook not called';
User.afterRemoteError('login', function(ctx, next) { User.afterRemoteError('login', function(ctx, next) {
actualError = ctx.error; actualError = ctx.error;
next(); next();
}); });
request(app).get('/users/sign-in?username=bob&password=123') request(app).get('/users/sign-in?username=bob&password=123')
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(actualError) expect(actualError)
.to.have.property('message', 'bad username and password!'); .to.have.property('message', 'bad username and password!');
done(); done();
}); });
}); });
@ -327,6 +347,7 @@ describe.onServer('Remote Methods', function() {
assert(ctx.res); assert(ctx.res);
assert(ctx.res.write); assert(ctx.res.write);
assert(ctx.res.end); assert(ctx.res.end);
next(); next();
}); });
@ -338,7 +359,9 @@ describe.onServer('Remote Methods', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
assert(hookCalled); assert(hookCalled);
done(); done();
}); });
}); });
@ -356,6 +379,7 @@ describe.onServer('Remote Methods', function() {
assert(ctx.res); assert(ctx.res);
assert(ctx.res.write); assert(ctx.res.write);
assert(ctx.res.end); assert(ctx.res.end);
next(); next();
}); });
@ -367,7 +391,9 @@ describe.onServer('Remote Methods', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
assert(hookCalled); assert(hookCalled);
done(); done();
}); });
}); });
@ -392,6 +418,7 @@ describe.onServer('Remote Methods', function() {
book.chapters({where: {title: 'Chapter 1'}}, function(err, chapters) { book.chapters({where: {title: 'Chapter 1'}}, function(err, chapters) {
assert.equal(chapters.length, 1); assert.equal(chapters.length, 1);
assert.equal(chapters[0].title, 'Chapter 1'); assert.equal(chapters[0].title, 'Chapter 1');
done(); done();
}); });
}); });
@ -537,6 +564,7 @@ describe.onServer('Remote Methods', function() {
it('Get the Source Id', function(done) { it('Get the Source Id', function(done) {
User.getSourceId(function(err, id) { User.getSourceId(function(err, id) {
assert.equal('memory-user', id); assert.equal('memory-user', id);
done(); done();
}); });
}); });
@ -556,6 +584,7 @@ describe.onServer('Remote Methods', function() {
if (err) return done(err); if (err) return done(err);
assert.equal(result, current + 1); assert.equal(result, current + 1);
done(); done();
}); });
@ -655,7 +684,9 @@ describe.onServer('Remote Methods', function() {
app.model(TestModel, { dataSource: 'db' }); app.model(TestModel, { dataSource: 'db' });
TestModel.getApp(function(err, a) { TestModel.getApp(function(err, a) {
if (err) return done(err); if (err) return done(err);
expect(a).to.equal(app); expect(a).to.equal(app);
done(); done();
}); });
// fails on time-out when not implemented correctly // fails on time-out when not implemented correctly
@ -664,7 +695,9 @@ describe.onServer('Remote Methods', function() {
it('calls the callback after attached', function(done) { it('calls the callback after attached', function(done) {
TestModel.getApp(function(err, a) { TestModel.getApp(function(err, a) {
if (err) return done(err); if (err) return done(err);
expect(a).to.equal(app); expect(a).to.equal(app);
done(); done();
}); });
app.model(TestModel, { dataSource: 'db' }); app.model(TestModel, { dataSource: 'db' });

View File

@ -43,6 +43,7 @@ describe('Registry', function() {
expect(bars.map(function(f) { expect(bars.map(function(f) {
return f.parent; return f.parent;
})).to.eql(['bar']); })).to.eql(['bar']);
done(); done();
}); });
}); });

View File

@ -79,10 +79,12 @@ describe('relations - integration', function() {
app.models.Team.create({ name: 'Team 1' }, app.models.Team.create({ name: 'Team 1' },
function(err, team) { function(err, team) {
if (err) return done(err); if (err) return done(err);
test.team = team; test.team = team;
app.models.Reader.create({ name: 'Reader 1' }, app.models.Reader.create({ name: 'Reader 1' },
function(err, reader) { function(err, reader) {
if (err) return done(err); if (err) return done(err);
test.reader = reader; test.reader = reader;
reader.pictures.create({ name: 'Picture 1' }); reader.pictures.create({ name: 'Picture 1' });
reader.pictures.create({ name: 'Picture 2' }); reader.pictures.create({ name: 'Picture 2' });
@ -103,12 +105,13 @@ describe('relations - integration', function() {
.query({'filter': {'include' : 'pictures'}}) .query({'filter': {'include' : 'pictures'}})
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
// console.log(res.body);
expect(res.body.name).to.be.equal('Reader 1'); expect(res.body.name).to.be.equal('Reader 1');
expect(res.body.pictures).to.be.eql([ expect(res.body.pictures).to.be.eql([
{ name: 'Picture 1', id: 1, imageableId: 1, imageableType: 'Reader'}, { name: 'Picture 1', id: 1, imageableId: 1, imageableType: 'Reader'},
{ name: 'Picture 2', id: 2, imageableId: 1, imageableType: 'Reader'}, { name: 'Picture 2', id: 2, imageableId: 1, imageableType: 'Reader'},
]); ]);
done(); done();
}); });
}); });
@ -119,10 +122,11 @@ describe('relations - integration', function() {
.query({'filter': {'include' : 'imageable'}}) .query({'filter': {'include' : 'imageable'}})
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
// console.log(res.body);
expect(res.body[0].name).to.be.equal('Picture 1'); expect(res.body[0].name).to.be.equal('Picture 1');
expect(res.body[1].name).to.be.equal('Picture 2'); expect(res.body[1].name).to.be.equal('Picture 2');
expect(res.body[0].imageable).to.be.eql({ name: 'Reader 1', id: 1, teamId: 1}); expect(res.body[0].imageable).to.be.eql({ name: 'Reader 1', id: 1, teamId: 1 });
done(); done();
}); });
}); });
@ -133,10 +137,12 @@ describe('relations - integration', function() {
.query({'filter': {'include' : {'relation': 'imageable', 'scope': { 'include' : 'team'}}}}) .query({'filter': {'include' : {'relation': 'imageable', 'scope': { 'include' : 'team'}}}})
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body[0].name).to.be.equal('Picture 1'); expect(res.body[0].name).to.be.equal('Picture 1');
expect(res.body[1].name).to.be.equal('Picture 2'); expect(res.body[1].name).to.be.equal('Picture 2');
expect(res.body[0].imageable.name).to.be.eql('Reader 1'); expect(res.body[0].imageable.name).to.be.eql('Reader 1');
expect(res.body[0].imageable.team).to.be.eql({ name: 'Team 1', id: 1}); expect(res.body[0].imageable.team).to.be.eql({ name: 'Team 1', id: 1 });
done(); done();
}); });
}); });
@ -148,7 +154,9 @@ describe('relations - integration', function() {
this.get('/api/stores/superStores') this.get('/api/stores/superStores')
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.array; expect(res.body).to.be.array;
done(); done();
}); });
}); });
@ -199,8 +207,10 @@ describe('relations - integration', function() {
this.http.send(this.newWidget); this.http.send(this.newWidget);
this.http.end(function(err) { this.http.end(function(err) {
if (err) return done(err); if (err) return done(err);
this.req = this.http.req; this.req = this.http.req;
this.res = this.http.res; this.res = this.http.res;
done(); done();
}.bind(this)); }.bind(this));
}); });
@ -226,7 +236,9 @@ describe('relations - integration', function() {
storeId: this.store.id storeId: this.store.id
}, function(err, count) { }, function(err, count) {
if (err) return done(err); if (err) return done(err);
assert.equal(count, 2); assert.equal(count, 2);
done(); done();
}); });
}); });
@ -241,6 +253,7 @@ describe('relations - integration', function() {
}, function(err, widget) { }, function(err, widget) {
self.widget = widget; self.widget = widget;
self.url = '/api/stores/' + self.store.id + '/widgets/' + widget.id; self.url = '/api/stores/' + self.store.id + '/widgets/' + widget.id;
done(); done();
}); });
}); });
@ -314,6 +327,7 @@ describe('relations - integration', function() {
}, function(err, widget) { }, function(err, widget) {
self.widget = widget; self.widget = widget;
self.url = '/api/widgets/' + self.widget.id + '/store'; self.url = '/api/widgets/' + self.widget.id + '/store';
done(); done();
}); });
}); });
@ -348,6 +362,7 @@ describe('relations - integration', function() {
name: 'ph1' name: 'ph1'
}, function(err, physician) { }, function(err, physician) {
root.physician = physician; root.physician = physician;
done(); done();
}); });
}, },
@ -360,6 +375,7 @@ describe('relations - integration', function() {
root.patient = patient; root.patient = patient;
root.relUrl = '/api/physicians/' + root.physician.id + root.relUrl = '/api/physicians/' + root.physician.id +
'/patients/rel/' + root.patient.id; '/patients/rel/' + root.patient.id;
done(); done();
}); });
} : function(done) { } : function(done) {
@ -369,6 +385,7 @@ describe('relations - integration', function() {
root.patient = patient; root.patient = patient;
root.relUrl = '/api/physicians/' + root.physician.id + root.relUrl = '/api/physicians/' + root.physician.id +
'/patients/rel/' + root.patient.id; '/patients/rel/' + root.patient.id;
done(); done();
}); });
}], function(err, done) { }], function(err, done) {
@ -384,6 +401,7 @@ describe('relations - integration', function() {
self.url = root.relUrl; self.url = root.relUrl;
self.patient = root.patient; self.patient = root.patient;
self.physician = root.physician; self.physician = root.physician;
done(err); done(err);
}); });
}); });
@ -400,6 +418,7 @@ describe('relations - integration', function() {
app.models.appointment.find(function(err, apps) { app.models.appointment.find(function(err, apps) {
assert.equal(apps.length, 1); assert.equal(apps.length, 1);
assert.equal(apps[0].patientId, self.patient.id); assert.equal(apps[0].patientId, self.patient.id);
done(); done();
}); });
}); });
@ -409,6 +428,7 @@ describe('relations - integration', function() {
self.physician.patients(function(err, patients) { self.physician.patients(function(err, patients) {
assert.equal(patients.length, 1); assert.equal(patients.length, 1);
assert.equal(patients[0].id, self.patient.id); assert.equal(patients[0].id, self.patient.id);
done(); done();
}); });
}); });
@ -423,6 +443,7 @@ describe('relations - integration', function() {
self.url = root.relUrl; self.url = root.relUrl;
self.patient = root.patient; self.patient = root.patient;
self.physician = root.physician; self.physician = root.physician;
done(err); done(err);
}); });
}); });
@ -445,6 +466,7 @@ describe('relations - integration', function() {
assert.equal(apps[0].patientId, self.patient.id); assert.equal(apps[0].patientId, self.patient.id);
assert.equal(apps[0].physicianId, self.physician.id); assert.equal(apps[0].physicianId, self.physician.id);
assert.equal(apps[0].date.getTime(), NOW); assert.equal(apps[0].date.getTime(), NOW);
done(); done();
}); });
}); });
@ -454,6 +476,7 @@ describe('relations - integration', function() {
self.physician.patients(function(err, patients) { self.physician.patients(function(err, patients) {
assert.equal(patients.length, 1); assert.equal(patients.length, 1);
assert.equal(patients[0].id, self.patient.id); assert.equal(patients[0].id, self.patient.id);
done(); done();
}); });
}); });
@ -468,6 +491,7 @@ describe('relations - integration', function() {
self.url = root.relUrl; self.url = root.relUrl;
self.patient = root.patient; self.patient = root.patient;
self.physician = root.physician; self.physician = root.physician;
done(err); done(err);
}); });
}); });
@ -488,6 +512,7 @@ describe('relations - integration', function() {
'/patients/rel/' + '999'; '/patients/rel/' + '999';
self.patient = root.patient; self.patient = root.patient;
self.physician = root.physician; self.physician = root.physician;
done(err); done(err);
}); });
}); });
@ -507,6 +532,7 @@ describe('relations - integration', function() {
self.url = root.relUrl; self.url = root.relUrl;
self.patient = root.patient; self.patient = root.patient;
self.physician = root.physician; self.physician = root.physician;
done(err); done(err);
}); });
}); });
@ -516,6 +542,7 @@ describe('relations - integration', function() {
app.models.appointment.find(function(err, apps) { app.models.appointment.find(function(err, apps) {
assert.equal(apps.length, 1); assert.equal(apps.length, 1);
assert.equal(apps[0].patientId, self.patient.id); assert.equal(apps[0].patientId, self.patient.id);
done(); done();
}); });
}); });
@ -525,6 +552,7 @@ describe('relations - integration', function() {
self.physician.patients(function(err, patients) { self.physician.patients(function(err, patients) {
assert.equal(patients.length, 1); assert.equal(patients.length, 1);
assert.equal(patients[0].id, self.patient.id); assert.equal(patients[0].id, self.patient.id);
done(); done();
}); });
}); });
@ -538,6 +566,7 @@ describe('relations - integration', function() {
var self = this; var self = this;
app.models.appointment.find(function(err, apps) { app.models.appointment.find(function(err, apps) {
assert.equal(apps.length, 0); assert.equal(apps.length, 0);
done(); done();
}); });
}); });
@ -547,6 +576,7 @@ describe('relations - integration', function() {
// Need to refresh the cache // Need to refresh the cache
self.physician.patients(true, function(err, patients) { self.physician.patients(true, function(err, patients) {
assert.equal(patients.length, 0); assert.equal(patients.length, 0);
done(); done();
}); });
}); });
@ -562,6 +592,7 @@ describe('relations - integration', function() {
'/patients/' + root.patient.id; '/patients/' + root.patient.id;
self.patient = root.patient; self.patient = root.patient;
self.physician = root.physician; self.physician = root.physician;
done(err); done(err);
}); });
}); });
@ -583,6 +614,7 @@ describe('relations - integration', function() {
'/patients/' + root.patient.id; '/patients/' + root.patient.id;
self.patient = root.patient; self.patient = root.patient;
self.physician = root.physician; self.physician = root.physician;
done(err); done(err);
}); });
}); });
@ -596,6 +628,7 @@ describe('relations - integration', function() {
var self = this; var self = this;
app.models.appointment.find(function(err, apps) { app.models.appointment.find(function(err, apps) {
assert.equal(apps.length, 0); assert.equal(apps.length, 0);
done(); done();
}); });
}); });
@ -605,6 +638,7 @@ describe('relations - integration', function() {
// Need to refresh the cache // Need to refresh the cache
self.physician.patients(true, function(err, patients) { self.physician.patients(true, function(err, patients) {
assert.equal(patients.length, 0); assert.equal(patients.length, 0);
done(); done();
}); });
}); });
@ -613,6 +647,7 @@ describe('relations - integration', function() {
var self = this; var self = this;
app.models.patient.find(function(err, patients) { app.models.patient.find(function(err, patients) {
assert.equal(patients.length, 0); assert.equal(patients.length, 0);
done(); done();
}); });
}); });
@ -644,7 +679,9 @@ describe('relations - integration', function() {
name: 'a-product' name: 'a-product'
}, function(err, product) { }, function(err, product) {
if (err) return done(err); if (err) return done(err);
test.product = product; test.product = product;
done(); done();
}); });
}); });
@ -653,6 +690,7 @@ describe('relations - integration', function() {
app.models.category.create({ name: 'another-category' }, app.models.category.create({ name: 'another-category' },
function(err, cat) { function(err, cat) {
if (err) return done(err); if (err) return done(err);
cat.products.create({ name: 'another-product' }, done); cat.products.create({ name: 'another-product' }, done);
}); });
}); });
@ -667,12 +705,14 @@ describe('relations - integration', function() {
this.get('/api/products?filter[where][categoryId]=' + this.category.id) this.get('/api/products?filter[where][categoryId]=' + this.category.id)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.eql([ expect(res.body).to.eql([
{ {
id: expectedProduct.id, id: expectedProduct.id,
name: expectedProduct.name name: expectedProduct.name
} }
]); ]);
done(); done();
}); });
}); });
@ -682,12 +722,14 @@ describe('relations - integration', function() {
this.get('/api/categories/' + this.category.id + '/products') this.get('/api/categories/' + this.category.id + '/products')
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.eql([ expect(res.body).to.eql([
{ {
id: expectedProduct.id, id: expectedProduct.id,
name: expectedProduct.name name: expectedProduct.name
} }
]); ]);
done(); done();
}); });
}); });
@ -700,6 +742,7 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.have.property('products'); expect(res.body).to.have.property('products');
expect(res.body.products).to.eql([ expect(res.body.products).to.eql([
{ {
@ -707,6 +750,7 @@ describe('relations - integration', function() {
name: expectedProduct.name name: expectedProduct.name
} }
]); ]);
done(); done();
}); });
}); });
@ -720,6 +764,7 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.have.property('products'); expect(res.body).to.have.property('products');
expect(res.body.products).to.eql([ expect(res.body.products).to.eql([
{ {
@ -727,6 +772,7 @@ describe('relations - integration', function() {
name: expectedProduct.name name: expectedProduct.name
} }
]); ]);
done(); done();
}); });
}); });
@ -754,7 +800,9 @@ describe('relations - integration', function() {
app.models.group.create({ name: 'Group 1' }, app.models.group.create({ name: 'Group 1' },
function(err, group) { function(err, group) {
if (err) return done(err); if (err) return done(err);
test.group = group; test.group = group;
done(); done();
}); });
}); });
@ -772,6 +820,7 @@ describe('relations - integration', function() {
expect(res.body).to.be.eql( expect(res.body).to.be.eql(
{ url: 'http://image.url' } { url: 'http://image.url' }
); );
done(); done();
}); });
}); });
@ -782,10 +831,12 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.name).to.be.equal('Group 1'); expect(res.body.name).to.be.equal('Group 1');
expect(res.body.poster).to.be.eql( expect(res.body.poster).to.be.eql(
{ url: 'http://image.url' } { url: 'http://image.url' }
); );
done(); done();
}); });
}); });
@ -796,9 +847,11 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql( expect(res.body).to.be.eql(
{ url: 'http://image.url' } { url: 'http://image.url' }
); );
done(); done();
}); });
}); });
@ -810,6 +863,7 @@ describe('relations - integration', function() {
.send({ url: 'http://changed.url' }) .send({ url: 'http://changed.url' })
.expect(200, function(err, res) { .expect(200, function(err, res) {
expect(res.body.url).to.be.equal('http://changed.url'); expect(res.body.url).to.be.equal('http://changed.url');
done(); done();
}); });
}); });
@ -820,9 +874,11 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql( expect(res.body).to.be.eql(
{ url: 'http://changed.url' } { url: 'http://changed.url' }
); );
done(); done();
}); });
}); });
@ -861,6 +917,7 @@ describe('relations - integration', function() {
app.models.todoList.create({ name: 'List A' }, app.models.todoList.create({ name: 'List A' },
function(err, list) { function(err, list) {
if (err) return done(err); if (err) return done(err);
test.todoList = list; test.todoList = list;
list.items.build({ content: 'Todo 1' }); list.items.build({ content: 'Todo 1' });
list.items.build({ content: 'Todo 2' }); list.items.build({ content: 'Todo 2' });
@ -878,11 +935,13 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.name).to.be.equal('List A'); expect(res.body.name).to.be.equal('List A');
expect(res.body.todoItems).to.be.eql([ expect(res.body.todoItems).to.be.eql([
{ content: 'Todo 1', id: 1 }, { content: 'Todo 1', id: 1 },
{ content: 'Todo 2', id: 2 } { content: 'Todo 2', id: 2 }
]); ]);
done(); done();
}); });
}); });
@ -893,10 +952,12 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql([ expect(res.body).to.be.eql([
{ content: 'Todo 1', id: 1 }, { content: 'Todo 1', id: 1 },
{ content: 'Todo 2', id: 2 } { content: 'Todo 2', id: 2 }
]); ]);
done(); done();
}); });
}); });
@ -908,9 +969,11 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql([ expect(res.body).to.be.eql([
{ content: 'Todo 2', id: 2 } { content: 'Todo 2', id: 2 }
]); ]);
done(); done();
}); });
}); });
@ -924,6 +987,7 @@ describe('relations - integration', function() {
.send({ content: 'Todo 3' }) .send({ content: 'Todo 3' })
.expect(200, function(err, res) { .expect(200, function(err, res) {
expect(res.body).to.be.eql(expected); expect(res.body).to.be.eql(expected);
done(); done();
}); });
}); });
@ -934,11 +998,13 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql([ expect(res.body).to.be.eql([
{ content: 'Todo 1', id: 1 }, { content: 'Todo 1', id: 1 },
{ content: 'Todo 2', id: 2 }, { content: 'Todo 2', id: 2 },
{ content: 'Todo 3', id: 3 } { content: 'Todo 3', id: 3 }
]); ]);
done(); done();
}); });
}); });
@ -949,9 +1015,11 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql( expect(res.body).to.be.eql(
{ content: 'Todo 3', id: 3 } { content: 'Todo 3', id: 3 }
); );
done(); done();
}); });
}); });
@ -972,10 +1040,12 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql([ expect(res.body).to.be.eql([
{ content: 'Todo 1', id: 1 }, { content: 'Todo 1', id: 1 },
{ content: 'Todo 3', id: 3 } { content: 'Todo 3', id: 3 }
]); ]);
done(); done();
}); });
}); });
@ -984,9 +1054,11 @@ describe('relations - integration', function() {
var url = '/api/todo-lists/' + this.todoList.id + '/items/2'; var url = '/api/todo-lists/' + this.todoList.id + '/items/2';
this.get(url).expect(404, function(err, res) { this.get(url).expect(404, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.error.status).to.be.equal(404); expect(res.body.error.status).to.be.equal(404);
expect(res.body.error.message).to.be.equal('Unknown "todoItem" id "2".'); expect(res.body.error.message).to.be.equal('Unknown "todoItem" id "2".');
expect(res.body.error.code).to.be.equal('MODEL_NOT_FOUND'); expect(res.body.error.code).to.be.equal('MODEL_NOT_FOUND');
done(); done();
}); });
}); });
@ -1038,6 +1110,7 @@ describe('relations - integration', function() {
app.models.recipe.create({ name: 'Recipe' }, app.models.recipe.create({ name: 'Recipe' },
function(err, recipe) { function(err, recipe) {
if (err) return done(err); if (err) return done(err);
test.recipe = recipe; test.recipe = recipe;
recipe.ingredients.create({ recipe.ingredients.create({
name: 'Chocolate' }, name: 'Chocolate' },
@ -1052,6 +1125,7 @@ describe('relations - integration', function() {
var test = this; var test = this;
app.models.ingredient.create({ name: 'Sugar' }, function(err, ing) { app.models.ingredient.create({ name: 'Sugar' }, function(err, ing) {
test.ingredient2 = ing.id; test.ingredient2 = ing.id;
done(); done();
}); });
}); });
@ -1072,8 +1146,10 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.ingredientIds).to.eql([test.ingredient1]); expect(res.body.ingredientIds).to.eql([test.ingredient1]);
expect(res.body).to.not.have.property('ingredients'); expect(res.body).to.not.have.property('ingredients');
done(); done();
}); });
}); });
@ -1087,6 +1163,7 @@ describe('relations - integration', function() {
.expect(200, function(err, res) { .expect(200, function(err, res) {
expect(res.body.name).to.be.eql('Butter'); expect(res.body.name).to.be.eql('Butter');
test.ingredient3 = res.body.id; test.ingredient3 = res.body.id;
done(); done();
}); });
}); });
@ -1098,11 +1175,13 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql([ expect(res.body).to.be.eql([
{ name: 'Chocolate', id: test.ingredient1 }, { name: 'Chocolate', id: test.ingredient1 },
{ name: 'Sugar', id: test.ingredient2 }, { name: 'Sugar', id: test.ingredient2 },
{ name: 'Butter', id: test.ingredient3 } { name: 'Butter', id: test.ingredient3 }
]); ]);
done(); done();
}); });
}); });
@ -1114,10 +1193,12 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql([ expect(res.body).to.be.eql([
{ name: 'Chocolate', id: test.ingredient1 }, { name: 'Chocolate', id: test.ingredient1 },
{ name: 'Butter', id: test.ingredient3 } { name: 'Butter', id: test.ingredient3 }
]); ]);
done(); done();
}); });
}); });
@ -1130,9 +1211,11 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql([ expect(res.body).to.be.eql([
{ name: 'Butter', id: test.ingredient3 } { name: 'Butter', id: test.ingredient3 }
]); ]);
done(); done();
}); });
}); });
@ -1145,6 +1228,7 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.ingredientIds).to.eql([ expect(res.body.ingredientIds).to.eql([
test.ingredient1, test.ingredient3 test.ingredient1, test.ingredient3
]); ]);
@ -1152,6 +1236,7 @@ describe('relations - integration', function() {
{ name: 'Chocolate', id: test.ingredient1 }, { name: 'Chocolate', id: test.ingredient1 },
{ name: 'Butter', id: test.ingredient3 } { name: 'Butter', id: test.ingredient3 }
]); ]);
done(); done();
}); });
}); });
@ -1164,9 +1249,11 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql( expect(res.body).to.be.eql(
{ name: 'Butter', id: test.ingredient3 } { name: 'Butter', id: test.ingredient3 }
); );
done(); done();
}); });
}); });
@ -1180,8 +1267,10 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.ingredientIds).to.eql(expected); expect(res.body.ingredientIds).to.eql(expected);
expect(res.body).to.not.have.property('ingredients'); expect(res.body).to.not.have.property('ingredients');
done(); done();
}); });
}); });
@ -1204,10 +1293,12 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql([ expect(res.body).to.be.eql([
{ name: 'Chocolate', id: test.ingredient1 }, { name: 'Chocolate', id: test.ingredient1 },
{ name: 'Sugar', id: test.ingredient2 } { name: 'Sugar', id: test.ingredient2 }
]); ]);
done(); done();
}); });
}); });
@ -1219,9 +1310,11 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql([ expect(res.body).to.be.eql([
{ name: 'Chocolate', id: test.ingredient1 } { name: 'Chocolate', id: test.ingredient1 }
]); ]);
done(); done();
}); });
}); });
@ -1236,6 +1329,7 @@ describe('relations - integration', function() {
expect(res.body).to.be.eql( expect(res.body).to.be.eql(
{ name: 'Sugar', id: test.ingredient2 } { name: 'Sugar', id: test.ingredient2 }
); );
done(); done();
}); });
}); });
@ -1247,10 +1341,12 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql([ expect(res.body).to.be.eql([
{ name: 'Chocolate', id: test.ingredient1 }, { name: 'Chocolate', id: test.ingredient1 },
{ name: 'Sugar', id: test.ingredient2 } { name: 'Sugar', id: test.ingredient2 }
]); ]);
done(); done();
}); });
}); });
@ -1273,9 +1369,11 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql([ expect(res.body).to.be.eql([
{ name: 'Sugar', id: test.ingredient2 } { name: 'Sugar', id: test.ingredient2 }
]); ]);
done(); done();
}); });
}); });
@ -1287,10 +1385,12 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.eql([ expect(res.body).to.be.eql([
{ name: 'Chocolate', id: test.ingredient1 }, { name: 'Chocolate', id: test.ingredient1 },
{ name: 'Sugar', id: test.ingredient2 } { name: 'Sugar', id: test.ingredient2 }
]); ]);
done(); done();
}); });
}); });
@ -1301,8 +1401,10 @@ describe('relations - integration', function() {
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(err).to.not.exist; expect(err).to.not.exist;
expect(res.body.name).to.equal('Photo 1'); expect(res.body.name).to.equal('Photo 1');
done(); done();
}); });
}); });
@ -1379,11 +1481,13 @@ describe('relations - integration', function() {
Page.beforeRemote('prototype.__findById__notes', function(ctx, result, next) { Page.beforeRemote('prototype.__findById__notes', function(ctx, result, next) {
ctx.res.set('x-before', 'before'); ctx.res.set('x-before', 'before');
next(); next();
}); });
Page.afterRemote('prototype.__findById__notes', function(ctx, result, next) { Page.afterRemote('prototype.__findById__notes', function(ctx, result, next) {
ctx.res.set('x-after', 'after'); ctx.res.set('x-after', 'after');
next(); next();
}); });
@ -1394,14 +1498,17 @@ describe('relations - integration', function() {
app.models.Book.create({ name: 'Book 1' }, app.models.Book.create({ name: 'Book 1' },
function(err, book) { function(err, book) {
if (err) return done(err); if (err) return done(err);
test.book = book; test.book = book;
book.pages.create({ name: 'Page 1' }, book.pages.create({ name: 'Page 1' },
function(err, page) { function(err, page) {
if (err) return done(err); if (err) return done(err);
test.page = page; test.page = page;
page.notes.create({ text: 'Page Note 1' }, page.notes.create({ text: 'Page Note 1' },
function(err, note) { function(err, note) {
test.note = note; test.note = note;
done(); done();
}); });
}); });
@ -1413,9 +1520,11 @@ describe('relations - integration', function() {
test.book.chapters.create({ name: 'Chapter 1' }, test.book.chapters.create({ name: 'Chapter 1' },
function(err, chapter) { function(err, chapter) {
if (err) return done(err); if (err) return done(err);
test.chapter = chapter; test.chapter = chapter;
chapter.notes.create({ text: 'Chapter Note 1' }, function(err, note) { chapter.notes.create({ text: 'Chapter Note 1' }, function(err, note) {
test.cnote = note; test.cnote = note;
done(); done();
}); });
}); });
@ -1426,7 +1535,9 @@ describe('relations - integration', function() {
app.models.Image.create({ name: 'Cover 1', book: test.book }, app.models.Image.create({ name: 'Cover 1', book: test.book },
function(err, image) { function(err, image) {
if (err) return done(err); if (err) return done(err);
test.image = image; test.image = image;
done(); done();
}); });
}); });
@ -1436,9 +1547,11 @@ describe('relations - integration', function() {
this.get('/api/books/' + test.book.id + '/pages') this.get('/api/books/' + test.book.id + '/pages')
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.an.array; expect(res.body).to.be.an.array;
expect(res.body).to.have.length(1); expect(res.body).to.have.length(1);
expect(res.body[0].name).to.equal('Page 1'); expect(res.body[0].name).to.equal('Page 1');
done(); done();
}); });
}); });
@ -1448,10 +1561,12 @@ describe('relations - integration', function() {
this.get('/api/pages/' + test.page.id + '/notes/' + test.note.id) this.get('/api/pages/' + test.page.id + '/notes/' + test.note.id)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.headers['x-before']).to.equal('before'); expect(res.headers['x-before']).to.equal('before');
expect(res.headers['x-after']).to.equal('after'); expect(res.headers['x-after']).to.equal('after');
expect(res.body).to.be.an.object; expect(res.body).to.be.an.object;
expect(res.body.text).to.equal('Page Note 1'); expect(res.body.text).to.equal('Page Note 1');
done(); done();
}); });
}); });
@ -1461,10 +1576,12 @@ describe('relations - integration', function() {
this.get('/api/books/unknown/pages/' + test.page.id + '/notes') this.get('/api/books/unknown/pages/' + test.page.id + '/notes')
.expect(404, function(err, res) { .expect(404, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.error).to.be.an.object; expect(res.body.error).to.be.an.object;
var expected = 'could not find a model with id unknown'; var expected = 'could not find a model with id unknown';
expect(res.body.error.message).to.equal(expected); expect(res.body.error.message).to.equal(expected);
expect(res.body.error.code).to.be.equal('MODEL_NOT_FOUND'); expect(res.body.error.code).to.be.equal('MODEL_NOT_FOUND');
done(); done();
}); });
}); });
@ -1474,9 +1591,11 @@ describe('relations - integration', function() {
this.get('/api/images/' + test.image.id + '/book/pages') this.get('/api/images/' + test.image.id + '/book/pages')
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.an.array; expect(res.body).to.be.an.array;
expect(res.body).to.have.length(1); expect(res.body).to.have.length(1);
expect(res.body[0].name).to.equal('Page 1'); expect(res.body[0].name).to.equal('Page 1');
done(); done();
}); });
}); });
@ -1486,8 +1605,10 @@ describe('relations - integration', function() {
this.get('/api/images/' + test.image.id + '/book/pages/' + test.page.id) this.get('/api/images/' + test.image.id + '/book/pages/' + test.page.id)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.an.object; expect(res.body).to.be.an.object;
expect(res.body.name).to.equal('Page 1'); expect(res.body.name).to.equal('Page 1');
done(); done();
}); });
}); });
@ -1497,9 +1618,11 @@ describe('relations - integration', function() {
this.get('/api/books/' + test.book.id + '/pages/' + test.page.id + '/notes') this.get('/api/books/' + test.book.id + '/pages/' + test.page.id + '/notes')
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.an.array; expect(res.body).to.be.an.array;
expect(res.body).to.have.length(1); expect(res.body).to.have.length(1);
expect(res.body[0].text).to.equal('Page Note 1'); expect(res.body[0].text).to.equal('Page Note 1');
done(); done();
}); });
}); });
@ -1509,10 +1632,12 @@ describe('relations - integration', function() {
this.get('/api/books/' + test.book.id + '/pages/' + test.page.id + '/notes/' + test.note.id) this.get('/api/books/' + test.book.id + '/pages/' + test.page.id + '/notes/' + test.note.id)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.headers['x-before']).to.equal('before'); expect(res.headers['x-before']).to.equal('before');
expect(res.headers['x-after']).to.equal('after'); expect(res.headers['x-after']).to.equal('after');
expect(res.body).to.be.an.object; expect(res.body).to.be.an.object;
expect(res.body.text).to.equal('Page Note 1'); expect(res.body.text).to.equal('Page Note 1');
done(); done();
}); });
}); });
@ -1522,8 +1647,10 @@ describe('relations - integration', function() {
this.get('/api/books/' + test.book.id + '/chapters/' + test.chapter.id + '/notes/' + test.cnote.id) this.get('/api/books/' + test.book.id + '/chapters/' + test.chapter.id + '/notes/' + test.cnote.id)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.headers['x-before']).to.empty; expect(res.headers['x-before']).to.empty;
expect(res.headers['x-after']).to.empty; expect(res.headers['x-after']).to.empty;
done(); done();
}); });
}); });
@ -1535,6 +1662,7 @@ describe('relations - integration', function() {
http.forEach(function(opt) { http.forEach(function(opt) {
// destroyAll has been shared but missing http property // destroyAll has been shared but missing http property
if (opt.path === undefined) return; if (opt.path === undefined) return;
expect(opt.path, method.stringName).to.match(/^\/.*/); expect(opt.path, method.stringName).to.match(/^\/.*/);
}); });
}); });
@ -1546,11 +1674,13 @@ describe('relations - integration', function() {
this.get('/api/books/' + test.book.id + '/pages/' + this.page.id + '/throws') this.get('/api/books/' + test.book.id + '/pages/' + this.page.id + '/throws')
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.an('object'); expect(res.body).to.be.an('object');
expect(res.body.error).to.be.an('object'); expect(res.body.error).to.be.an('object');
expect(res.body.error.name).to.equal('Error'); expect(res.body.error.name).to.equal('Error');
expect(res.body.error.status).to.equal(500); expect(res.body.error.status).to.equal(500);
expect(res.body.error.message).to.equal('This should not crash the app'); expect(res.body.error.message).to.equal('This should not crash the app');
done(); done();
}); });
}); });
@ -1562,10 +1692,10 @@ describe('relations - integration', function() {
before(function createCustomer(done) { before(function createCustomer(done) {
var test = this; var test = this;
app.models.customer.create({ name: 'John' }, function(err, c) { app.models.customer.create({ name: 'John' }, function(err, c) {
if (err) { if (err) return done(err);
return done(err);
}
cust = c; cust = c;
done(); done();
}); });
}); });
@ -1573,9 +1703,8 @@ describe('relations - integration', function() {
after(function(done) { after(function(done) {
var self = this; var self = this;
this.app.models.customer.destroyAll(function(err) { this.app.models.customer.destroyAll(function(err) {
if (err) { if (err) return done(err);
return done(err);
}
self.app.models.profile.destroyAll(done); self.app.models.profile.destroyAll(done);
}); });
}); });
@ -1586,11 +1715,11 @@ describe('relations - integration', function() {
this.post(url) this.post(url)
.send({points: 10}) .send({points: 10})
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
expect(res.body.points).to.be.eql(10); expect(res.body.points).to.be.eql(10);
expect(res.body.customerId).to.be.eql(cust.id); expect(res.body.customerId).to.be.eql(cust.id);
done(); done();
}); });
}); });
@ -1599,11 +1728,11 @@ describe('relations - integration', function() {
var url = '/api/customers/' + cust.id + '/profile'; var url = '/api/customers/' + cust.id + '/profile';
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
expect(res.body.points).to.be.eql(10); expect(res.body.points).to.be.eql(10);
expect(res.body.customerId).to.be.eql(cust.id); expect(res.body.customerId).to.be.eql(cust.id);
done(); done();
}); });
}); });
@ -1622,11 +1751,11 @@ describe('relations - integration', function() {
this.put(url) this.put(url)
.send({points: 100}) .send({points: 100})
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
expect(res.body.points).to.be.eql(100); expect(res.body.points).to.be.eql(100);
expect(res.body.customerId).to.be.eql(cust.id); expect(res.body.customerId).to.be.eql(cust.id);
done(); done();
}); });
}); });

View File

@ -21,6 +21,7 @@ describe('RemoteConnector', function() {
port: remoteApp.get('port'), port: remoteApp.get('port'),
connector: loopback.Remote connector: loopback.Remote
}); });
done(); done();
}); });
}, },
@ -48,6 +49,7 @@ describe('RemoteConnector', function() {
port: remoteApp.get('port'), port: remoteApp.get('port'),
connector: loopback.Remote connector: loopback.Remote
}); });
done(); done();
}); });
}); });
@ -70,8 +72,10 @@ describe('RemoteConnector', function() {
var m = new RemoteModel({foo: 'bar'}); var m = new RemoteModel({foo: 'bar'});
m.save(function(err, inst) { m.save(function(err, inst) {
if (err) return done(err); if (err) return done(err);
assert(inst instanceof RemoteModel); assert(inst instanceof RemoteModel);
assert(calledServerCreate); assert(calledServerCreate);
done(); done();
}); });
}); });

View File

@ -32,7 +32,9 @@ describe('remoting coercion', function() {
}) })
.end(function(err) { .end(function(err) {
if (err) return done(err); if (err) return done(err);
assert(called); assert(called);
done(); done();
}); });
}); });

View File

@ -52,6 +52,7 @@ describe('remoting - integration', function() {
this.req = this.http.req; this.req = this.http.req;
this.res = this.http.res; this.res = this.http.res;
assert.equal(this.res.statusCode, 200); assert.equal(this.res.statusCode, 200);
done(); done();
}.bind(this)); }.bind(this));
}); });
@ -72,6 +73,7 @@ describe('remoting - integration', function() {
this.res = this.http.res; this.res = this.http.res;
// Request is rejected with 413 // Request is rejected with 413
assert.equal(this.res.statusCode, 413); assert.equal(this.res.statusCode, 413);
done(); done();
}.bind(this)); }.bind(this));
}); });

View File

@ -114,11 +114,14 @@ describe('Replication over REST', function() {
it('allows pull from server', function(done) { it('allows pull from server', function(done) {
RemoteCar.replicate(LocalCar, function(err, conflicts, cps) { RemoteCar.replicate(LocalCar, function(err, conflicts, cps) {
if (err) return done(err); if (err) return done(err);
if (conflicts.length) return done(conflictError(conflicts)); if (conflicts.length) return done(conflictError(conflicts));
LocalCar.find(function(err, list) { LocalCar.find(function(err, list) {
if (err) return done(err); if (err) return done(err);
expect(list.map(carToString)).to.include.members(serverCars); expect(list.map(carToString)).to.include.members(serverCars);
done(); done();
}); });
}); });
@ -137,11 +140,14 @@ describe('Replication over REST', function() {
it('allows pull from server', function(done) { it('allows pull from server', function(done) {
RemoteCar.replicate(LocalCar, function(err, conflicts, cps) { RemoteCar.replicate(LocalCar, function(err, conflicts, cps) {
if (err) return done(err); if (err) return done(err);
if (conflicts.length) return done(conflictError(conflicts)); if (conflicts.length) return done(conflictError(conflicts));
LocalCar.find(function(err, list) { LocalCar.find(function(err, list) {
if (err) return done(err); if (err) return done(err);
expect(list.map(carToString)).to.include.members(serverCars); expect(list.map(carToString)).to.include.members(serverCars);
done(); done();
}); });
}); });
@ -150,11 +156,14 @@ describe('Replication over REST', function() {
it('allows push to the server', function(done) { it('allows push to the server', function(done) {
LocalCar.replicate(RemoteCar, function(err, conflicts, cps) { LocalCar.replicate(RemoteCar, function(err, conflicts, cps) {
if (err) return done(err); if (err) return done(err);
if (conflicts.length) return done(conflictError(conflicts)); if (conflicts.length) return done(conflictError(conflicts));
ServerCar.find(function(err, list) { ServerCar.find(function(err, list) {
if (err) return done(err); if (err) return done(err);
expect(list.map(carToString)).to.include.members(clientCars); expect(list.map(carToString)).to.include.members(clientCars);
done(); done();
}); });
}); });
@ -224,6 +233,7 @@ describe('Replication over REST', function() {
it('allows reverse resolve() on the client', function(done) { it('allows reverse resolve() on the client', function(done) {
RemoteCar.replicate(LocalCar, function(err, conflicts) { RemoteCar.replicate(LocalCar, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
expect(conflicts, 'conflicts').to.have.length(1); expect(conflicts, 'conflicts').to.have.length(1);
// By default, conflicts are always resolved by modifying // By default, conflicts are always resolved by modifying
@ -238,7 +248,9 @@ describe('Replication over REST', function() {
RemoteCar.replicate(LocalCar, function(err, conflicts) { RemoteCar.replicate(LocalCar, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
if (conflicts.length) return done(conflictError(conflicts)); if (conflicts.length) return done(conflictError(conflicts));
done(); done();
}); });
}); });
@ -248,6 +260,7 @@ describe('Replication over REST', function() {
it('rejects resolve() on the server', function(done) { it('rejects resolve() on the server', function(done) {
RemoteCar.replicate(LocalCar, function(err, conflicts) { RemoteCar.replicate(LocalCar, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
expect(conflicts, 'conflicts').to.have.length(1); expect(conflicts, 'conflicts').to.have.length(1);
conflicts[0].resolveUsingSource(expectHttpError(401, done)); conflicts[0].resolveUsingSource(expectHttpError(401, done));
}); });
@ -262,13 +275,17 @@ describe('Replication over REST', function() {
it('allows resolve() on the client', function(done) { it('allows resolve() on the client', function(done) {
LocalCar.replicate(RemoteCar, function(err, conflicts) { LocalCar.replicate(RemoteCar, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
expect(conflicts).to.have.length(1); expect(conflicts).to.have.length(1);
conflicts[0].resolveUsingSource(function(err) { conflicts[0].resolveUsingSource(function(err) {
if (err) return done(err); if (err) return done(err);
LocalCar.replicate(RemoteCar, function(err, conflicts) { LocalCar.replicate(RemoteCar, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
if (conflicts.length) return done(conflictError(conflicts)); if (conflicts.length) return done(conflictError(conflicts));
done(); done();
}); });
}); });
@ -278,13 +295,17 @@ describe('Replication over REST', function() {
it('allows resolve() on the server', function(done) { it('allows resolve() on the server', function(done) {
RemoteCar.replicate(LocalCar, function(err, conflicts) { RemoteCar.replicate(LocalCar, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
expect(conflicts).to.have.length(1); expect(conflicts).to.have.length(1);
conflicts[0].resolveUsingSource(function(err) { conflicts[0].resolveUsingSource(function(err) {
if (err) return done(err); if (err) return done(err);
RemoteCar.replicate(LocalCar, function(err, conflicts) { RemoteCar.replicate(LocalCar, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
if (conflicts.length) return done(conflictError(conflicts)); if (conflicts.length) return done(conflictError(conflicts));
done(); done();
}); });
}); });
@ -298,10 +319,13 @@ describe('Replication over REST', function() {
setAccessToken(aliceToken); setAccessToken(aliceToken);
RemoteUser.replicate(LocalUser, function(err, conflicts, cps) { RemoteUser.replicate(LocalUser, function(err, conflicts, cps) {
if (err) return done(err); if (err) return done(err);
if (conflicts.length) return done(conflictError(conflicts)); if (conflicts.length) return done(conflictError(conflicts));
LocalUser.find(function(err, users) { LocalUser.find(function(err, users) {
var userNames = users.map(function(u) { return u.username; }); var userNames = users.map(function(u) { return u.username; });
expect(userNames).to.eql([ALICE.username]); expect(userNames).to.eql([ALICE.username]);
done(); done();
}); });
}); });
@ -315,7 +339,9 @@ describe('Replication over REST', function() {
setAccessToken(aliceToken); setAccessToken(aliceToken);
LocalUser.replicate(RemoteUser, function(err, conflicts) { LocalUser.replicate(RemoteUser, function(err, conflicts) {
if (err) return next(err); if (err) return next(err);
if (conflicts.length) return next(conflictError(conflicts)); if (conflicts.length) return next(conflictError(conflicts));
next(); next();
}); });
}, },
@ -323,8 +349,10 @@ describe('Replication over REST', function() {
function verify(next) { function verify(next) {
RemoteUser.findById(aliceId, function(err, found) { RemoteUser.findById(aliceId, function(err, found) {
if (err) return next(err); if (err) return next(err);
expect(found.toObject()) expect(found.toObject())
.to.have.property('fullname', 'Alice Smith'); .to.have.property('fullname', 'Alice Smith');
next(); next();
}); });
} }
@ -340,7 +368,9 @@ describe('Replication over REST', function() {
LocalUser.replicate(RemoteUser, function(err, conflicts) { LocalUser.replicate(RemoteUser, function(err, conflicts) {
if (!err) if (!err)
return next(new Error('Replicate should have failed.')); return next(new Error('Replicate should have failed.'));
expect(err).to.have.property('statusCode', 401); // or 403? expect(err).to.have.property('statusCode', 401); // or 403?
next(); next();
}); });
}, },
@ -348,8 +378,10 @@ describe('Replication over REST', function() {
function verify(next) { function verify(next) {
ServerUser.findById(aliceId, function(err, found) { ServerUser.findById(aliceId, function(err, found) {
if (err) return next(err); if (err) return next(err);
expect(found.toObject()) expect(found.toObject())
.to.not.have.property('fullname'); .to.not.have.property('fullname');
next(); next();
}); });
} }
@ -461,6 +493,7 @@ describe('Replication over REST', function() {
serverApp.use(function(req, res, next) { serverApp.use(function(req, res, next) {
debug(req.method + ' ' + req.path); debug(req.method + ' ' + req.path);
next(); next();
}); });
serverApp.use(loopback.token({ model: ServerToken })); serverApp.use(loopback.token({ model: ServerToken }));
@ -472,6 +505,7 @@ describe('Replication over REST', function() {
serverApp.listen(function() { serverApp.listen(function() {
serverUrl = serverApp.get('url').replace(/\/+$/, ''); serverUrl = serverApp.get('url').replace(/\/+$/, '');
request = supertest(serverUrl); request = supertest(serverUrl);
done(); done();
}); });
} }
@ -527,18 +561,22 @@ describe('Replication over REST', function() {
function(next) { function(next) {
ServerUser.create([ALICE, PETER, EMERY], function(err, created) { ServerUser.create([ALICE, PETER, EMERY], function(err, created) {
if (err) return next(err); if (err) return next(err);
aliceId = created[0].id; aliceId = created[0].id;
peterId = created[1].id; peterId = created[1].id;
next(); next();
}); });
}, },
function(next) { function(next) {
ServerUser.login(ALICE, function(err, token) { ServerUser.login(ALICE, function(err, token) {
if (err) return next(err); if (err) return next(err);
aliceToken = token.id; aliceToken = token.id;
ServerUser.login(PETER, function(err, token) { ServerUser.login(PETER, function(err, token) {
if (err) return next(err); if (err) return next(err);
peterToken = token.id; peterToken = token.id;
ServerUser.login(EMERY, function(err, token) { ServerUser.login(EMERY, function(err, token) {
@ -557,7 +595,9 @@ describe('Replication over REST', function() {
], ],
function(err, cars) { function(err, cars) {
if (err) return next(err); if (err) return next(err);
serverCars = cars.map(carToString); serverCars = cars.map(carToString);
next(); next();
}); });
} }
@ -574,7 +614,9 @@ describe('Replication over REST', function() {
[{ maker: 'Local', model: 'Custom' }], [{ maker: 'Local', model: 'Custom' }],
function(err, cars) { function(err, cars) {
if (err) return next(err); if (err) return next(err);
clientCars = cars.map(carToString); clientCars = cars.map(carToString);
next(); next();
}); });
}, },
@ -584,9 +626,12 @@ describe('Replication over REST', function() {
function seedConflict(done) { function seedConflict(done) {
LocalCar.replicate(ServerCar, function(err, conflicts) { LocalCar.replicate(ServerCar, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
if (conflicts.length) return done(conflictError(conflicts)); if (conflicts.length) return done(conflictError(conflicts));
ServerCar.replicate(LocalCar, function(err, conflicts) { ServerCar.replicate(LocalCar, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
if (conflicts.length) return done(conflictError(conflicts)); if (conflicts.length) return done(conflictError(conflicts));
// Hard-coded, see the seed data above // Hard-coded, see the seed data above
@ -595,6 +640,7 @@ describe('Replication over REST', function() {
new LocalCar({ id: conflictedCarId }) new LocalCar({ id: conflictedCarId })
.updateAttributes({ model: 'Client' }, function(err, c) { .updateAttributes({ model: 'Client' }, function(err, c) {
if (err) return done(err); if (err) return done(err);
new ServerCar({ id: conflictedCarId }) new ServerCar({ id: conflictedCarId })
.updateAttributes({ model: 'Server' }, done); .updateAttributes({ model: 'Server' }, done);
}); });
@ -612,7 +658,9 @@ describe('Replication over REST', function() {
function expectHttpError(code, done) { function expectHttpError(code, done) {
return function(err) { return function(err) {
if (!err) return done(new Error('The method should have failed.')); if (!err) return done(new Error('The method should have failed.'));
expect(err).to.have.property('statusCode', code); expect(err).to.have.property('statusCode', code);
done(); done();
}; };
} }
@ -620,7 +668,9 @@ describe('Replication over REST', function() {
function replicateServerToLocal(next) { function replicateServerToLocal(next) {
ServerUser.replicate(LocalUser, function(err, conflicts) { ServerUser.replicate(LocalUser, function(err, conflicts) {
if (err) return next(err); if (err) return next(err);
if (conflicts.length) return next(conflictError(conflicts)); if (conflicts.length) return next(conflictError(conflicts));
next(); next();
}); });
} }

View File

@ -51,6 +51,7 @@ describe('Replication / Change APIs', function() {
this.createInitalData = function(cb) { this.createInitalData = function(cb) {
SourceModel.create({name: 'foo'}, function(err, inst) { SourceModel.create({name: 'foo'}, function(err, inst) {
if (err) return cb(err); if (err) return cb(err);
test.model = inst; test.model = inst;
SourceModel.replicate(TargetModel, cb); SourceModel.replicate(TargetModel, cb);
}); });
@ -75,7 +76,9 @@ describe('Replication / Change APIs', function() {
var calls = mockSourceModelRectify(); var calls = mockSourceModelRectify();
SourceModel.destroyAll({name: 'John'}, function(err, data) { SourceModel.destroyAll({name: 'John'}, function(err, data) {
if (err) return done(err); if (err) return done(err);
expect(calls).to.eql(['rectifyAllChanges']); expect(calls).to.eql(['rectifyAllChanges']);
done(); done();
}); });
}); });
@ -85,7 +88,9 @@ describe('Replication / Change APIs', function() {
var newData = {'name': 'Janie'}; var newData = {'name': 'Janie'};
SourceModel.update({name: 'Jane'}, newData, function(err, data) { SourceModel.update({name: 'Jane'}, newData, function(err, data) {
if (err) return done(err); if (err) return done(err);
expect(calls).to.eql(['rectifyAllChanges']); expect(calls).to.eql(['rectifyAllChanges']);
done(); done();
}); });
}); });
@ -102,7 +107,9 @@ describe('Replication / Change APIs', function() {
} }
], function(err, results) { ], function(err, results) {
if (err) return done(err); if (err) return done(err);
expect(calls).to.eql(['rectifyChange']); expect(calls).to.eql(['rectifyChange']);
done(); done();
}); });
}); });
@ -120,7 +127,9 @@ describe('Replication / Change APIs', function() {
} }
], function(err, result) { ], function(err, result) {
if (err) return done(err); if (err) return done(err);
expect(calls).to.eql(['rectifyChange']); expect(calls).to.eql(['rectifyChange']);
done(); done();
}); });
}); });
@ -138,7 +147,9 @@ describe('Replication / Change APIs', function() {
} }
], function(err, result) { ], function(err, result) {
if (err) return done(err); if (err) return done(err);
expect(calls).to.eql(['rectifyChange']); expect(calls).to.eql(['rectifyChange']);
done(); done();
}); });
}); });
@ -181,9 +192,11 @@ describe('Replication / Change APIs', function() {
var test = this; var test = this;
this.SourceModel.create({name: 'foo'}, function(err) { this.SourceModel.create({name: 'foo'}, function(err) {
if (err) return done(err); if (err) return done(err);
setTimeout(function() { setTimeout(function() {
test.SourceModel.changes(test.startingCheckpoint, {}, function(err, changes) { test.SourceModel.changes(test.startingCheckpoint, {}, function(err, changes) {
assert.equal(changes.length, 1); assert.equal(changes.length, 1);
done(); done();
}); });
}, 1); }, 1);
@ -197,8 +210,9 @@ describe('Replication / Change APIs', function() {
if (err) return done(err); if (err) return done(err);
SourceModel.changes(FUTURE_CHECKPOINT, {}, function(err, changes) { SourceModel.changes(FUTURE_CHECKPOINT, {}, function(err, changes) {
if (err) return done(err); if (err) return done(err);
/*jshint -W030 */
expect(changes).to.be.empty; expect(changes).to.be.empty; //jshint ignore:line
done(); done();
}); });
}); });
@ -217,6 +231,7 @@ describe('Replication / Change APIs', function() {
function(cb) { function(cb) {
sourceModel.find(function(err, result) { sourceModel.find(function(err, result) {
if (err) return cb(err); if (err) return cb(err);
sourceData = result; sourceData = result;
cb(); cb();
}); });
@ -224,6 +239,7 @@ describe('Replication / Change APIs', function() {
function(cb) { function(cb) {
targetModel.find(function(err, result) { targetModel.find(function(err, result) {
if (err) return cb(err); if (err) return cb(err);
targetData = result; targetData = result;
cb(); cb();
}); });
@ -232,6 +248,7 @@ describe('Replication / Change APIs', function() {
if (err) return done(err); if (err) return done(err);
assert.deepEqual(sourceData, targetData); assert.deepEqual(sourceData, targetData);
done(); done();
}); });
} }
@ -242,6 +259,7 @@ describe('Replication / Change APIs', function() {
this.SourceModel.create({name: 'foo'}, function(err) { this.SourceModel.create({name: 'foo'}, function(err) {
if (err) return done(err); if (err) return done(err);
test.SourceModel.replicate(test.startingCheckpoint, test.TargetModel, test.SourceModel.replicate(test.startingCheckpoint, test.TargetModel,
options, function(err, conflicts) { options, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
@ -258,6 +276,7 @@ describe('Replication / Change APIs', function() {
this.SourceModel.create({name: 'foo'}, function(err) { this.SourceModel.create({name: 'foo'}, function(err) {
if (err) return done(err); if (err) return done(err);
test.SourceModel.replicate(test.startingCheckpoint, test.TargetModel, test.SourceModel.replicate(test.startingCheckpoint, test.TargetModel,
options) options)
.then(function(conflicts) { .then(function(conflicts) {
@ -292,6 +311,7 @@ describe('Replication / Change APIs', function() {
if (err) return done(err); if (err) return done(err);
// '1' should be skipped by replication // '1' should be skipped by replication
expect(getIds(list)).to.eql(['2']); expect(getIds(list)).to.eql(['2']);
next(); next();
}); });
} }
@ -324,6 +344,7 @@ describe('Replication / Change APIs', function() {
if (err) return done(err); if (err) return done(err);
// '1' should be skipped by replication // '1' should be skipped by replication
expect(getIds(list)).to.eql(['2']); expect(getIds(list)).to.eql(['2']);
next(); next();
}); });
} }
@ -339,7 +360,9 @@ describe('Replication / Change APIs', function() {
SourceModel.replicate(10, TargetModel, function(err) { SourceModel.replicate(10, TargetModel, function(err) {
if (err) return done(err); if (err) return done(err);
expect(diffSince).to.eql([10]); expect(diffSince).to.eql([10]);
done(); done();
}); });
}); });
@ -354,6 +377,7 @@ describe('Replication / Change APIs', function() {
SourceModel.replicate(10, TargetModel, {}) SourceModel.replicate(10, TargetModel, {})
.then(function() { .then(function() {
expect(diffSince).to.eql([10]); expect(diffSince).to.eql([10]);
done(); done();
}) })
.catch(function(err) { .catch(function(err) {
@ -371,8 +395,10 @@ describe('Replication / Change APIs', function() {
var since = { source: 1, target: 2 }; var since = { source: 1, target: 2 };
SourceModel.replicate(since, TargetModel, function(err) { SourceModel.replicate(since, TargetModel, function(err) {
if (err) return done(err); if (err) return done(err);
expect(sourceSince).to.eql([1]); expect(sourceSince).to.eql([1]);
expect(targetSince).to.eql([2]); expect(targetSince).to.eql([2]);
done(); done();
}); });
}); });
@ -389,6 +415,7 @@ describe('Replication / Change APIs', function() {
.then(function() { .then(function() {
expect(sourceSince).to.eql([1]); expect(sourceSince).to.eql([1]);
expect(targetSince).to.eql([2]); expect(targetSince).to.eql([2]);
done(); done();
}) })
.catch(function(err) { .catch(function(err) {
@ -411,7 +438,9 @@ describe('Replication / Change APIs', function() {
function getLastCp(next) { function getLastCp(next) {
SourceModel.currentCheckpoint(function(err, cp) { SourceModel.currentCheckpoint(function(err, cp) {
if (err) return done(err); if (err) return done(err);
lastCp = cp; lastCp = cp;
next(); next();
}); });
}, },
@ -426,6 +455,7 @@ describe('Replication / Change APIs', function() {
TargetModel.find(function(err, list) { TargetModel.find(function(err, list) {
expect(getIds(list), 'target ids after first sync') expect(getIds(list), 'target ids after first sync')
.to.include.members(['init']); .to.include.members(['init']);
next(); next();
}); });
}); });
@ -436,6 +466,7 @@ describe('Replication / Change APIs', function() {
function verify(next) { function verify(next) {
TargetModel.find(function(err, list) { TargetModel.find(function(err, list) {
expect(getIds(list), 'target ids').to.eql(['init', 'racer']); expect(getIds(list), 'target ids').to.eql(['init', 'racer']);
next(); next();
}); });
} }
@ -455,11 +486,13 @@ describe('Replication / Change APIs', function() {
TargetModel, TargetModel,
function(err, conflicts, newCheckpoints) { function(err, conflicts, newCheckpoints) {
if (err) return cb(err); if (err) return cb(err);
expect(conflicts, 'conflicts').to.eql([]); expect(conflicts, 'conflicts').to.eql([]);
expect(newCheckpoints, 'currentCheckpoints').to.eql({ expect(newCheckpoints, 'currentCheckpoints').to.eql({
source: sourceCp + 1, source: sourceCp + 1,
target: targetCp + 1 target: targetCp + 1
}); });
cb(); cb();
}); });
} }
@ -468,7 +501,9 @@ describe('Replication / Change APIs', function() {
function bumpSourceCheckpoint(cb) { function bumpSourceCheckpoint(cb) {
SourceModel.checkpoint(function(err, inst) { SourceModel.checkpoint(function(err, inst) {
if (err) return cb(err); if (err) return cb(err);
sourceCp = inst.seq; sourceCp = inst.seq;
cb(); cb();
}); });
} }
@ -476,7 +511,9 @@ describe('Replication / Change APIs', function() {
function bumpTargetCheckpoint(cb) { function bumpTargetCheckpoint(cb) {
TargetModel.checkpoint(function(err, inst) { TargetModel.checkpoint(function(err, inst) {
if (err) return cb(err); if (err) return cb(err);
targetCp = inst.seq; targetCp = inst.seq;
cb(); cb();
}); });
} }
@ -491,11 +528,14 @@ describe('Replication / Change APIs', function() {
function verify(next) { function verify(next) {
TargetModel.currentCheckpoint(function(err, cp) { TargetModel.currentCheckpoint(function(err, cp) {
if (err) return next(err); if (err) return next(err);
TargetModel.getChangeModel().find( TargetModel.getChangeModel().find(
{ where: { checkpoint: { gte: cp } } }, { where: { checkpoint: { gte: cp } } },
function(err, changes) { function(err, changes) {
if (err) return done(err); if (err) return done(err);
expect(changes).to.have.length(0); expect(changes).to.have.length(0);
done(); done();
}); });
}); });
@ -659,6 +699,7 @@ describe('Replication / Change APIs', function() {
cb); cb);
} }
}); });
next(); next();
}, },
replicateExpectingSuccess(), replicateExpectingSuccess(),
@ -694,10 +735,13 @@ describe('Replication / Change APIs', function() {
} }
], function(err) { ], function(err) {
if (err) return done(err); if (err) return done(err);
SourceModel.replicate(TargetModel, function(err, conflicts) { SourceModel.replicate(TargetModel, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
test.conflicts = conflicts; test.conflicts = conflicts;
test.conflict = conflicts[0]; test.conflict = conflicts[0];
done(); done();
}); });
}); });
@ -710,6 +754,7 @@ describe('Replication / Change APIs', function() {
it('type should be UPDATE', function(done) { it('type should be UPDATE', function(done) {
this.conflict.type(function(err, type) { this.conflict.type(function(err, type) {
assert.equal(type, Change.UPDATE); assert.equal(type, Change.UPDATE);
done(); done();
}); });
}); });
@ -721,6 +766,7 @@ describe('Replication / Change APIs', function() {
assert.equal(test.model.getId(), sourceChange.getModelId()); assert.equal(test.model.getId(), sourceChange.getModelId());
assert.equal(sourceChange.type(), Change.UPDATE); assert.equal(sourceChange.type(), Change.UPDATE);
assert.equal(targetChange.type(), Change.UPDATE); assert.equal(targetChange.type(), Change.UPDATE);
done(); done();
}); });
}); });
@ -735,6 +781,7 @@ describe('Replication / Change APIs', function() {
id: test.model.id, id: test.model.id,
name: 'target update' name: 'target update'
}); });
done(); done();
}); });
}); });
@ -753,6 +800,7 @@ describe('Replication / Change APIs', function() {
function(cb) { function(cb) {
SourceModel.findOne(function(err, inst) { SourceModel.findOne(function(err, inst) {
if (err) return cb(err); if (err) return cb(err);
test.model = inst; test.model = inst;
inst.remove(cb); inst.remove(cb);
}); });
@ -760,16 +808,20 @@ describe('Replication / Change APIs', function() {
function(cb) { function(cb) {
TargetModel.findOne(function(err, inst) { TargetModel.findOne(function(err, inst) {
if (err) return cb(err); if (err) return cb(err);
inst.name = 'target update'; inst.name = 'target update';
inst.save(cb); inst.save(cb);
}); });
} }
], function(err) { ], function(err) {
if (err) return done(err); if (err) return done(err);
SourceModel.replicate(TargetModel, function(err, conflicts) { SourceModel.replicate(TargetModel, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
test.conflicts = conflicts; test.conflicts = conflicts;
test.conflict = conflicts[0]; test.conflict = conflicts[0];
done(); done();
}); });
}); });
@ -782,6 +834,7 @@ describe('Replication / Change APIs', function() {
it('type should be DELETE', function(done) { it('type should be DELETE', function(done) {
this.conflict.type(function(err, type) { this.conflict.type(function(err, type) {
assert.equal(type, Change.DELETE); assert.equal(type, Change.DELETE);
done(); done();
}); });
}); });
@ -793,6 +846,7 @@ describe('Replication / Change APIs', function() {
assert.equal(test.model.getId(), sourceChange.getModelId()); assert.equal(test.model.getId(), sourceChange.getModelId());
assert.equal(sourceChange.type(), Change.DELETE); assert.equal(sourceChange.type(), Change.DELETE);
assert.equal(targetChange.type(), Change.UPDATE); assert.equal(targetChange.type(), Change.UPDATE);
done(); done();
}); });
}); });
@ -804,6 +858,7 @@ describe('Replication / Change APIs', function() {
id: test.model.id, id: test.model.id,
name: 'target update' name: 'target update'
}); });
done(); done();
}); });
}); });
@ -830,15 +885,19 @@ describe('Replication / Change APIs', function() {
function(cb) { function(cb) {
TargetModel.findOne(function(err, inst) { TargetModel.findOne(function(err, inst) {
if (err) return cb(err); if (err) return cb(err);
inst.remove(cb); inst.remove(cb);
}); });
} }
], function(err) { ], function(err) {
if (err) return done(err); if (err) return done(err);
SourceModel.replicate(TargetModel, function(err, conflicts) { SourceModel.replicate(TargetModel, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
test.conflicts = conflicts; test.conflicts = conflicts;
test.conflict = conflicts[0]; test.conflict = conflicts[0];
done(); done();
}); });
}); });
@ -851,6 +910,7 @@ describe('Replication / Change APIs', function() {
it('type should be DELETE', function(done) { it('type should be DELETE', function(done) {
this.conflict.type(function(err, type) { this.conflict.type(function(err, type) {
assert.equal(type, Change.DELETE); assert.equal(type, Change.DELETE);
done(); done();
}); });
}); });
@ -862,6 +922,7 @@ describe('Replication / Change APIs', function() {
assert.equal(test.model.getId(), sourceChange.getModelId()); assert.equal(test.model.getId(), sourceChange.getModelId());
assert.equal(sourceChange.type(), Change.UPDATE); assert.equal(sourceChange.type(), Change.UPDATE);
assert.equal(targetChange.type(), Change.DELETE); assert.equal(targetChange.type(), Change.DELETE);
done(); done();
}); });
}); });
@ -873,6 +934,7 @@ describe('Replication / Change APIs', function() {
id: test.model.id, id: test.model.id,
name: 'source update' name: 'source update'
}); });
done(); done();
}); });
}); });
@ -891,6 +953,7 @@ describe('Replication / Change APIs', function() {
function(cb) { function(cb) {
SourceModel.findOne(function(err, inst) { SourceModel.findOne(function(err, inst) {
if (err) return cb(err); if (err) return cb(err);
test.model = inst; test.model = inst;
inst.remove(cb); inst.remove(cb);
}); });
@ -898,15 +961,19 @@ describe('Replication / Change APIs', function() {
function(cb) { function(cb) {
TargetModel.findOne(function(err, inst) { TargetModel.findOne(function(err, inst) {
if (err) return cb(err); if (err) return cb(err);
inst.remove(cb); inst.remove(cb);
}); });
} }
], function(err) { ], function(err) {
if (err) return done(err); if (err) return done(err);
SourceModel.replicate(TargetModel, function(err, conflicts) { SourceModel.replicate(TargetModel, function(err, conflicts) {
if (err) return done(err); if (err) return done(err);
test.conflicts = conflicts; test.conflicts = conflicts;
test.conflict = conflicts[0]; test.conflict = conflicts[0];
done(); done();
}); });
}); });
@ -922,6 +989,7 @@ describe('Replication / Change APIs', function() {
it('detects "create"', function(done) { it('detects "create"', function(done) {
SourceModel.create({}, function(err, inst) { SourceModel.create({}, function(err, inst) {
if (err) return done(err); if (err) return done(err);
assertChangeRecordedForId(inst.id, done); assertChangeRecordedForId(inst.id, done);
}); });
}); });
@ -929,10 +997,12 @@ describe('Replication / Change APIs', function() {
it('detects "updateOrCreate"', function(done) { it('detects "updateOrCreate"', function(done) {
givenReplicatedInstance(function(err, created) { givenReplicatedInstance(function(err, created) {
if (err) return done(err); if (err) return done(err);
var data = created.toObject(); var data = created.toObject();
created.name = 'updated'; created.name = 'updated';
SourceModel.updateOrCreate(created, function(err, inst) { SourceModel.updateOrCreate(created, function(err, inst) {
if (err) return done(err); if (err) return done(err);
assertChangeRecordedForId(inst.id, done); assertChangeRecordedForId(inst.id, done);
}); });
}); });
@ -946,6 +1016,7 @@ describe('Replication / Change APIs', function() {
this.all(model, query, function(err, list) { this.all(model, query, function(err, list) {
if (err || (list && list[0])) if (err || (list && list[0]))
return callback(err, list && list[0], false); return callback(err, list && list[0], false);
this.create(model, data, function(err) { this.create(model, data, function(err) {
callback(err, data, true); callback(err, data, true);
}); });
@ -955,6 +1026,7 @@ describe('Replication / Change APIs', function() {
this.all(model, query, {}, function(err, list) { this.all(model, query, {}, function(err, list) {
if (err || (list && list[0])) if (err || (list && list[0]))
return callback(err, list && list[0], false); return callback(err, list && list[0], false);
this.create(model, data, {}, function(err) { this.create(model, data, {}, function(err) {
callback(err, data, true); callback(err, data, true);
}); });
@ -967,6 +1039,7 @@ describe('Replication / Change APIs', function() {
{ name: 'created' }, { name: 'created' },
function(err, inst) { function(err, inst) {
if (err) return done(err); if (err) return done(err);
assertChangeRecordedForId(inst.id, done); assertChangeRecordedForId(inst.id, done);
}); });
}); });
@ -974,6 +1047,7 @@ describe('Replication / Change APIs', function() {
it('detects "deleteById"', function(done) { it('detects "deleteById"', function(done) {
givenReplicatedInstance(function(err, inst) { givenReplicatedInstance(function(err, inst) {
if (err) return done(err); if (err) return done(err);
SourceModel.deleteById(inst.id, function(err) { SourceModel.deleteById(inst.id, function(err) {
assertChangeRecordedForId(inst.id, done); assertChangeRecordedForId(inst.id, done);
}); });
@ -983,8 +1057,10 @@ describe('Replication / Change APIs', function() {
it('detects "deleteAll"', function(done) { it('detects "deleteAll"', function(done) {
givenReplicatedInstance(function(err, inst) { givenReplicatedInstance(function(err, inst) {
if (err) return done(err); if (err) return done(err);
SourceModel.deleteAll({ name: inst.name }, function(err) { SourceModel.deleteAll({ name: inst.name }, function(err) {
if (err) return done(err); if (err) return done(err);
assertChangeRecordedForId(inst.id, done); assertChangeRecordedForId(inst.id, done);
}); });
}); });
@ -993,11 +1069,13 @@ describe('Replication / Change APIs', function() {
it('detects "updateAll"', function(done) { it('detects "updateAll"', function(done) {
givenReplicatedInstance(function(err, inst) { givenReplicatedInstance(function(err, inst) {
if (err) return done(err); if (err) return done(err);
SourceModel.updateAll( SourceModel.updateAll(
{ name: inst.name }, { name: inst.name },
{ name: 'updated' }, { name: 'updated' },
function(err) { function(err) {
if (err) return done(err); if (err) return done(err);
assertChangeRecordedForId(inst.id, done); assertChangeRecordedForId(inst.id, done);
}); });
}); });
@ -1006,9 +1084,11 @@ describe('Replication / Change APIs', function() {
it('detects "prototype.save"', function(done) { it('detects "prototype.save"', function(done) {
givenReplicatedInstance(function(err, inst) { givenReplicatedInstance(function(err, inst) {
if (err) return done(err); if (err) return done(err);
inst.name = 'updated'; inst.name = 'updated';
inst.save(function(err) { inst.save(function(err) {
if (err) return done(err); if (err) return done(err);
assertChangeRecordedForId(inst.id, done); assertChangeRecordedForId(inst.id, done);
}); });
}); });
@ -1017,8 +1097,10 @@ describe('Replication / Change APIs', function() {
it('detects "prototype.updateAttributes"', function(done) { it('detects "prototype.updateAttributes"', function(done) {
givenReplicatedInstance(function(err, inst) { givenReplicatedInstance(function(err, inst) {
if (err) return done(err); if (err) return done(err);
inst.updateAttributes({ name: 'updated' }, function(err) { inst.updateAttributes({ name: 'updated' }, function(err) {
if (err) return done(err); if (err) return done(err);
assertChangeRecordedForId(inst.id, done); assertChangeRecordedForId(inst.id, done);
}); });
}); });
@ -1027,6 +1109,7 @@ describe('Replication / Change APIs', function() {
it('detects "prototype.delete"', function(done) { it('detects "prototype.delete"', function(done) {
givenReplicatedInstance(function(err, inst) { givenReplicatedInstance(function(err, inst) {
if (err) return done(err); if (err) return done(err);
inst.delete(function(err) { inst.delete(function(err) {
assertChangeRecordedForId(inst.id, done); assertChangeRecordedForId(inst.id, done);
}); });
@ -1036,8 +1119,10 @@ describe('Replication / Change APIs', function() {
function givenReplicatedInstance(cb) { function givenReplicatedInstance(cb) {
SourceModel.create({ name: 'a-name' }, function(err, inst) { SourceModel.create({ name: 'a-name' }, function(err, inst) {
if (err) return cb(err); if (err) return cb(err);
SourceModel.checkpoint(function(err) { SourceModel.checkpoint(function(err) {
if (err) return cb(err); if (err) return cb(err);
cb(null, inst); cb(null, inst);
}); });
}); });
@ -1047,8 +1132,10 @@ describe('Replication / Change APIs', function() {
SourceModel.getChangeModel().getCheckpointModel() SourceModel.getChangeModel().getCheckpointModel()
.current(function(err, cp) { .current(function(err, cp) {
if (err) return cb(err); if (err) return cb(err);
SourceModel.changes(cp - 1, {}, function(err, pendingChanges) { SourceModel.changes(cp - 1, {}, function(err, pendingChanges) {
if (err) return cb(err); if (err) return cb(err);
expect(pendingChanges, 'list of changes').to.have.length(1); expect(pendingChanges, 'list of changes').to.have.length(1);
var change = pendingChanges[0].toObject(); var change = pendingChanges[0].toObject();
expect(change).to.have.property('checkpoint', cp); // sanity check expect(change).to.have.property('checkpoint', cp); // sanity check
@ -1056,6 +1143,7 @@ describe('Replication / Change APIs', function() {
// NOTE(bajtos) Change.modelId is always String // NOTE(bajtos) Change.modelId is always String
// regardless of the type of the changed model's id property // regardless of the type of the changed model's id property
expect(change).to.have.property('modelId', '' + id); expect(change).to.have.property('modelId', '' + id);
cb(); cb();
}); });
}); });
@ -1071,6 +1159,7 @@ describe('Replication / Change APIs', function() {
SourceModel.create({ id: 'test-instance' }, function(err, result) { SourceModel.create({ id: 'test-instance' }, function(err, result) {
sourceInstance = result; sourceInstance = result;
sourceInstanceId = result.id; sourceInstanceId = result.id;
next(err); next(err);
}); });
}, },
@ -1109,7 +1198,9 @@ describe('Replication / Change APIs', function() {
function verifyTargetModelWasDeleted(next) { function verifyTargetModelWasDeleted(next) {
TargetModel.find(function(err, list) { TargetModel.find(function(err, list) {
if (err) return next(err); if (err) return next(err);
expect(getIds(list)).to.not.contain(sourceInstance.id); expect(getIds(list)).to.not.contain(sourceInstance.id);
next(); next();
}); });
} }
@ -1376,6 +1467,7 @@ describe('Replication / Change APIs', function() {
return function updateInstanceB(next) { return function updateInstanceB(next) {
ClientB.findById(sourceInstanceId, function(err, instance) { ClientB.findById(sourceInstanceId, function(err, instance) {
if (err) return next(err); if (err) return next(err);
instance.name = name; instance.name = name;
instance.save(next); instance.save(next);
}); });
@ -1408,6 +1500,7 @@ describe('Replication / Change APIs', function() {
debug('delete source instance', value); debug('delete source instance', value);
sourceInstance.remove(function(err) { sourceInstance.remove(function(err) {
sourceInstance = null; sourceInstance = null;
next(err); next(err);
}); });
}; };
@ -1415,11 +1508,14 @@ describe('Replication / Change APIs', function() {
function verifySourceWasReplicated(target) { function verifySourceWasReplicated(target) {
if (!target) target = TargetModel; if (!target) target = TargetModel;
return function verify(next) { return function verify(next) {
target.findById(sourceInstanceId, function(err, targetInstance) { target.findById(sourceInstanceId, function(err, targetInstance) {
if (err) return next(err); if (err) return next(err);
expect(targetInstance && targetInstance.toObject()) expect(targetInstance && targetInstance.toObject())
.to.eql(sourceInstance && sourceInstance.toObject()); .to.eql(sourceInstance && sourceInstance.toObject());
next(); next();
}); });
}; };
@ -1445,9 +1541,11 @@ describe('Replication / Change APIs', function() {
source.replicate(since, target, function(err, conflicts, cps) { source.replicate(since, target, function(err, conflicts, cps) {
if (err) return next(err); if (err) return next(err);
if (conflicts.length === 0) { if (conflicts.length === 0) {
_since[sinceIx] = cps; _since[sinceIx] = cps;
} }
next(err, conflicts, cps); next(err, conflicts, cps);
}); });
} }
@ -1465,10 +1563,12 @@ describe('Replication / Change APIs', function() {
return function doReplicate(next) { return function doReplicate(next) {
replicate(source, target, since, function(err, conflicts, cps) { replicate(source, target, since, function(err, conflicts, cps) {
if (err) return next(err); if (err) return next(err);
if (conflicts.length) { if (conflicts.length) {
return next(new Error('Unexpected conflicts\n' + return next(new Error('Unexpected conflicts\n' +
conflicts.map(JSON.stringify).join('\n'))); conflicts.map(JSON.stringify).join('\n')));
} }
next(); next();
}); });
}; };
@ -1482,6 +1582,7 @@ describe('Replication / Change APIs', function() {
var self = this; var self = this;
fn(function(err) { fn(function(err) {
if (err) return cb(err); if (err) return cb(err);
bulkUpdate.call(self, data, cb); bulkUpdate.call(self, data, cb);
}); });
@ -1494,11 +1595,14 @@ describe('Replication / Change APIs', function() {
return function verify(next) { return function verify(next) {
source.findById(id, function(err, expected) { source.findById(id, function(err, expected) {
if (err) return next(err); if (err) return next(err);
target.findById(id, function(err, actual) { target.findById(id, function(err, actual) {
if (err) return next(err); if (err) return next(err);
expect(actual && actual.toObject()) expect(actual && actual.toObject())
.to.eql(expected && expected.toObject()); .to.eql(expected && expected.toObject());
debug('replicated instance: %j', actual); debug('replicated instance: %j', actual);
next(); next();
}); });
}); });

View File

@ -34,6 +34,7 @@ describe('loopback.rest', function() {
.del('/mymodels/' + inst.id) .del('/mymodels/' + inst.id)
.expect(200, function(err, res) { .expect(200, function(err, res) {
expect(res.body.count).to.equal(1); expect(res.body.count).to.equal(1);
done(); done();
}); });
}); });
@ -45,12 +46,12 @@ describe('loopback.rest', function() {
request(app).get('/mymodels/1') request(app).get('/mymodels/1')
.expect(404) .expect(404)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert(errorResponse); assert(errorResponse);
assert.equal(errorResponse.code, 'MODEL_NOT_FOUND'); assert.equal(errorResponse.code, 'MODEL_NOT_FOUND');
done(); done();
}); });
}); });
@ -70,7 +71,9 @@ describe('loopback.rest', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.eql({exists: false});
expect(res.body).to.eql({ exists: false });
done(); done();
}); });
}); });
@ -103,7 +106,9 @@ describe('loopback.rest', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.eql({exists: true});
expect(res.body).to.eql({ exists: true });
done(); done();
}); });
}); });
@ -177,12 +182,15 @@ describe('loopback.rest', function() {
app.use(loopback.rest()); app.use(loopback.rest());
givenLoggedInUser(function(err, token) { givenLoggedInUser(function(err, token) {
if (err) return done(err); if (err) return done(err);
request(app).get('/users/getToken') request(app).get('/users/getToken')
.set('Authorization', token.id) .set('Authorization', token.id)
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.id).to.equal(null); expect(res.body.id).to.equal(null);
done(); done();
}); });
}, done); }, done);
@ -194,7 +202,9 @@ describe('loopback.rest', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.eql([]); expect(res.body).to.eql([]);
done(); done();
}); });
}); });
@ -205,7 +215,9 @@ describe('loopback.rest', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.eql({}); expect(res.body).to.eql({});
done(); done();
}); });
}); });
@ -262,12 +274,15 @@ describe('loopback.rest', function() {
function invokeGetToken(done) { function invokeGetToken(done) {
givenLoggedInUser(function(err, token) { givenLoggedInUser(function(err, token) {
if (err) return done(err); if (err) return done(err);
request(app).get('/users/getToken') request(app).get('/users/getToken')
.set('Authorization', token.id) .set('Authorization', token.id)
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.id).to.equal(token.id); expect(res.body.id).to.equal(token.id);
done(); done();
}); });
}); });
@ -296,6 +311,7 @@ describe('loopback.rest', function() {
{ model: app.registry.getModelByType('AccessToken') })); { model: app.registry.getModelByType('AccessToken') }));
app.use(function(req, res, next) { app.use(function(req, res, next) {
loopback.getCurrentContext().set('accessToken', req.accessToken); loopback.getCurrentContext().set('accessToken', req.accessToken);
next(); next();
}); });
app.use(loopback.rest()); app.use(loopback.rest());
@ -351,6 +367,7 @@ describe('loopback.rest', function() {
User.create(credentials, User.create(credentials,
function(err, user) { function(err, user) {
if (err) return done(err); if (err) return done(err);
User.login(credentials, cb); User.login(credentials, cb);
}); });
} }
@ -399,7 +416,9 @@ describe('loopback.rest', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.count).to.equal(3); expect(res.body.count).to.equal(3);
done(); done();
}); });
}); });
@ -445,7 +464,9 @@ describe('loopback.rest', function() {
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.count).to.equal(3); expect(res.body.count).to.equal(3);
done(); done();
}); });
}); });

View File

@ -464,7 +464,9 @@ describe('role model', function() {
Role.isOwner(User, user.id, user.id, function(err, result) { Role.isOwner(User, user.id, user.id, function(err, result) {
if (err) return done(err); if (err) return done(err);
expect(result, 'isOwner result').to.equal(true); expect(result, 'isOwner result').to.equal(true);
done(); done();
}); });
}); });

View File

@ -30,10 +30,13 @@ describe('users - integration', function() {
app.models.AccessToken.belongsTo(app.models.User); app.models.AccessToken.belongsTo(app.models.User);
app.models.User.destroyAll(function(err) { app.models.User.destroyAll(function(err) {
if (err) return done(err); if (err) return done(err);
app.models.post.destroyAll(function(err) { app.models.post.destroyAll(function(err) {
if (err) return done(err); if (err) return done(err);
app.models.blog.destroyAll(function(err) { app.models.blog.destroyAll(function(err) {
if (err) return done(err); if (err) return done(err);
done(); done();
}); });
}); });
@ -49,8 +52,10 @@ describe('users - integration', function() {
.send({username: 'x', email: 'x@y.com', password: 'x'}) .send({username: 'x', email: 'x@y.com', password: 'x'})
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.id).to.exist; expect(res.body.id).to.exist;
userId = res.body.id; userId = res.body.id;
done(); done();
}); });
}); });
@ -61,11 +66,11 @@ describe('users - integration', function() {
this.post(url) this.post(url)
.send({username: 'x', email: 'x@y.com', password: 'x'}) .send({username: 'x', email: 'x@y.com', password: 'x'})
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
expect(res.body.id).to.exist; expect(res.body.id).to.exist;
accessToken = res.body.id; accessToken = res.body.id;
done(); done();
}); });
}); });
@ -75,12 +80,12 @@ describe('users - integration', function() {
this.post(url) this.post(url)
.send({title: 'T1', content: 'C1'}) .send({title: 'T1', content: 'C1'})
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
expect(res.body.title).to.be.eql('T1'); expect(res.body.title).to.be.eql('T1');
expect(res.body.content).to.be.eql('C1'); expect(res.body.content).to.be.eql('C1');
expect(res.body.userId).to.be.eql(userId); expect(res.body.userId).to.be.eql(userId);
done(); done();
}); });
}); });
@ -91,13 +96,13 @@ describe('users - integration', function() {
var url = '/api/posts?filter={"include":{"user":"accessTokens"}}'; var url = '/api/posts?filter={"include":{"user":"accessTokens"}}';
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
expect(res.body).to.have.property('length', 1); expect(res.body).to.have.property('length', 1);
var post = res.body[0]; var post = res.body[0];
expect(post.user).to.have.property('username', 'x'); expect(post.user).to.have.property('username', 'x');
expect(post.user).to.not.have.property('accessTokens'); expect(post.user).to.not.have.property('accessTokens');
done(); done();
}); });
}); });
@ -113,11 +118,11 @@ describe('users - integration', function() {
this.post(url) this.post(url)
.send({username: 'x', email: 'x@y.com', password: 'x'}) .send({username: 'x', email: 'x@y.com', password: 'x'})
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
expect(res.body.id).to.exist; expect(res.body.id).to.exist;
userId = res.body.id; userId = res.body.id;
done(); done();
}); });
}); });
@ -128,11 +133,11 @@ describe('users - integration', function() {
this.post(url) this.post(url)
.send({username: 'x', email: 'x@y.com', password: 'x'}) .send({username: 'x', email: 'x@y.com', password: 'x'})
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
expect(res.body.id).to.exist; expect(res.body.id).to.exist;
accessToken = res.body.id; accessToken = res.body.id;
done(); done();
}); });
}); });
@ -146,9 +151,11 @@ describe('users - integration', function() {
console.error(err); console.error(err);
return done(err); return done(err);
} }
expect(res.body.title).to.be.eql('T1'); expect(res.body.title).to.be.eql('T1');
expect(res.body.content).to.be.eql('C1'); expect(res.body.content).to.be.eql('C1');
expect(res.body.userId).to.be.eql(userId); expect(res.body.userId).to.be.eql(userId);
done(); done();
}); });
}); });
@ -157,13 +164,13 @@ describe('users - integration', function() {
var url = '/api/blogs?filter={"include":{"user":"accessTokens"}}'; var url = '/api/blogs?filter={"include":{"user":"accessTokens"}}';
this.get(url) this.get(url)
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
expect(res.body).to.have.property('length', 1); expect(res.body).to.have.property('length', 1);
var blog = res.body[0]; var blog = res.body[0];
expect(blog.user).to.have.property('username', 'x'); expect(blog.user).to.have.property('username', 'x');
expect(blog.user).to.not.have.property('accessTokens'); expect(blog.user).to.not.have.property('accessTokens');
done(); done();
}); });
}); });

View File

@ -67,6 +67,7 @@ describe('User', function() {
User.create(validCredentials, function(err, user) { User.create(validCredentials, function(err, user) {
if (err) return done(err); if (err) return done(err);
User.create(validCredentialsEmailVerified, done); User.create(validCredentialsEmailVerified, done);
}); });
}); });
@ -77,6 +78,7 @@ describe('User', function() {
assert(!err); assert(!err);
assert(user.id); assert(user.id);
assert(user.email); assert(user.email);
done(); done();
}); });
}); });
@ -85,8 +87,10 @@ describe('User', function() {
User.settings.caseSensitiveEmail = false; User.settings.caseSensitiveEmail = false;
User.create({email: 'F@b.com', password: 'bar'}, function(err, user) { User.create({email: 'F@b.com', password: 'bar'}, function(err, user) {
if (err) return done(err); if (err) return done(err);
assert(user.id); assert(user.id);
assert.equal(user.email, user.email.toLowerCase()); assert.equal(user.email, user.email.toLowerCase());
done(); done();
}); });
}); });
@ -94,9 +98,11 @@ describe('User', function() {
it('Create a new user (email case-sensitive)', function(done) { it('Create a new user (email case-sensitive)', function(done) {
User.create({email: 'F@b.com', password: 'bar'}, function(err, user) { User.create({email: 'F@b.com', password: 'bar'}, function(err, user) {
if (err) return done(err); if (err) return done(err);
assert(user.id); assert(user.id);
assert(user.email); assert(user.email);
assert.notEqual(user.email, user.email.toLowerCase()); assert.notEqual(user.email, user.email.toLowerCase());
done(); done();
}); });
}); });
@ -110,8 +116,9 @@ describe('User', function() {
User.findById(user.id, function(err, user) { User.findById(user.id, function(err, user) {
assert(user.id); assert(user.id);
assert(user.email); assert(user.email);
assert.deepEqual(user.credentials, {cert: 'xxxxx', key: '111'}); assert.deepEqual(user.credentials, { cert: 'xxxxx', key: '111' });
assert.deepEqual(user.challenges, {x: 'X', a: 1}); assert.deepEqual(user.challenges, { x: 'X', a: 1 });
done(); done();
}); });
}); });
@ -138,6 +145,7 @@ describe('User', function() {
User.create({email: 'c@d.com'}, function(err) { User.create({email: 'c@d.com'}, function(err) {
assert(err); assert(err);
done(); done();
}); });
}); });
@ -145,6 +153,7 @@ describe('User', function() {
it('Requires a valid email', function(done) { it('Requires a valid email', function(done) {
User.create({email: 'foo@', password: '123'}, function(err) { User.create({email: 'foo@', password: '123'}, function(err) {
assert(err); assert(err);
done(); done();
}); });
}); });
@ -153,6 +162,7 @@ describe('User', function() {
User.create({email: 'a@b.com', password: 'foobar'}, function() { User.create({email: 'a@b.com', password: 'foobar'}, function() {
User.create({email: 'a@b.com', password: 'batbaz'}, function(err) { User.create({email: 'a@b.com', password: 'batbaz'}, function(err) {
assert(err, 'should error because the email is not unique!'); assert(err, 'should error because the email is not unique!');
done(); done();
}); });
}); });
@ -162,8 +172,10 @@ describe('User', function() {
User.settings.caseSensitiveEmail = false; User.settings.caseSensitiveEmail = false;
User.create({email: 'A@b.com', password: 'foobar'}, function(err) { User.create({email: 'A@b.com', password: 'foobar'}, function(err) {
if (err) return done(err); if (err) return done(err);
User.create({email: 'a@b.com', password: 'batbaz'}, function(err) {
User.create({ email: 'a@b.com', password: 'batbaz' }, function(err) {
assert(err, 'should error because the email is not unique!'); assert(err, 'should error because the email is not unique!');
done(); done();
}); });
}); });
@ -173,7 +185,9 @@ describe('User', function() {
User.create({email: 'A@b.com', password: 'foobar'}, function(err, user1) { User.create({email: 'A@b.com', password: 'foobar'}, function(err, user1) {
User.create({email: 'a@b.com', password: 'batbaz'}, function(err, user2) { User.create({email: 'a@b.com', password: 'batbaz'}, function(err, user2) {
if (err) return done(err); if (err) return done(err);
assert.notEqual(user1.email, user2.email); assert.notEqual(user1.email, user2.email);
done(); done();
}); });
}); });
@ -183,6 +197,7 @@ describe('User', function() {
User.create({email: 'a@b.com', username: 'abc', password: 'foobar'}, function() { User.create({email: 'a@b.com', username: 'abc', password: 'foobar'}, function() {
User.create({email: 'b@b.com', username: 'abc', password: 'batbaz'}, function(err) { User.create({email: 'b@b.com', username: 'abc', password: 'batbaz'}, function(err) {
assert(err, 'should error because the username is not unique!'); assert(err, 'should error because the username is not unique!');
done(); done();
}); });
}); });
@ -194,6 +209,7 @@ describe('User', function() {
assert(!accessToken, 'should not create a accessToken without a valid password'); assert(!accessToken, 'should not create a accessToken without a valid password');
assert(err, 'should not login without a password'); assert(err, 'should not login without a password');
assert.equal(err.code, 'LOGIN_FAILED'); assert.equal(err.code, 'LOGIN_FAILED');
done(); done();
}); });
}); });
@ -258,10 +274,10 @@ describe('User', function() {
.expect(200) .expect(200)
.send(validCredentialsEmailVerifiedOverREST) .send(validCredentialsEmailVerifiedOverREST)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
assert(!res.body.emailVerified); assert(!res.body.emailVerified);
done(); done();
}); });
}); });
@ -271,6 +287,7 @@ describe('User', function() {
it('Should not throw an error if the query does not contain {where: }', function(done) { it('Should not throw an error if the query does not contain {where: }', function(done) {
User.find({}, function(err) { User.find({}, function(err) {
if (err) done(err); if (err) done(err);
done(); done();
}); });
}); });
@ -279,8 +296,10 @@ describe('User', function() {
User.settings.caseSensitiveEmail = false; User.settings.caseSensitiveEmail = false;
User.find({where:{email: validMixedCaseEmailCredentials.email}}, function(err, result) { User.find({where:{email: validMixedCaseEmailCredentials.email}}, function(err, result) {
if (err) done(err); if (err) done(err);
assert(result[0], 'The query did not find the user'); assert(result[0], 'The query did not find the user');
assert.equal(result[0].email, validCredentialsEmail); assert.equal(result[0].email, validCredentialsEmail);
done(); done();
}); });
}); });
@ -303,6 +322,7 @@ describe('User', function() {
assert(accessToken.userId); assert(accessToken.userId);
assert(accessToken.id); assert(accessToken.id);
assert.equal(accessToken.id.length, 64); assert.equal(accessToken.id.length, 64);
done(); done();
}); });
}); });
@ -310,6 +330,7 @@ describe('User', function() {
it('Try to login with invalid email case', function(done) { it('Try to login with invalid email case', function(done) {
User.login(validMixedCaseEmailCredentials, function(err, accessToken) { User.login(validMixedCaseEmailCredentials, function(err, accessToken) {
assert(err); assert(err);
done(); done();
}); });
}); });
@ -336,6 +357,7 @@ describe('User', function() {
assert(accessToken.id); assert(accessToken.id);
assert.equal(accessToken.ttl, 120); assert.equal(accessToken.ttl, 120);
assert.equal(accessToken.id.length, 64); assert.equal(accessToken.id.length, 64);
done(); done();
}); });
}); });
@ -354,6 +376,7 @@ describe('User', function() {
assert(accessToken.id); assert(accessToken.id);
assert.equal(accessToken.ttl, 120); assert.equal(accessToken.ttl, 120);
assert.equal(accessToken.id.length, 64); assert.equal(accessToken.id.length, 64);
done(); done();
}) })
.catch(function(err) { .catch(function(err) {
@ -384,6 +407,7 @@ describe('User', function() {
assert.equal(accessToken.id.length, 64); assert.equal(accessToken.id.length, 64);
// Restore create access token // Restore create access token
User.prototype.createAccessToken = createToken; User.prototype.createAccessToken = createToken;
done(); done();
}); });
}); });
@ -414,6 +438,7 @@ describe('User', function() {
assert.equal(accessToken.scopes, 'default'); assert.equal(accessToken.scopes, 'default');
// Restore create access token // Restore create access token
User.prototype.createAccessToken = createToken; User.prototype.createAccessToken = createToken;
done(); done();
}); });
}); });
@ -425,6 +450,7 @@ describe('User', function() {
assert(err); assert(err);
assert.equal(err.code, 'LOGIN_FAILED'); assert.equal(err.code, 'LOGIN_FAILED');
assert(!accessToken); assert(!accessToken);
done(); done();
}); });
}); });
@ -433,11 +459,13 @@ describe('User', function() {
User.login(invalidCredentials) User.login(invalidCredentials)
.then(function(accessToken) { .then(function(accessToken) {
assert(!accessToken); assert(!accessToken);
done(); done();
}) })
.catch(function(err) { .catch(function(err) {
assert(err); assert(err);
assert.equal(err.code, 'LOGIN_FAILED'); assert.equal(err.code, 'LOGIN_FAILED');
done(); done();
}); });
}); });
@ -446,6 +474,7 @@ describe('User', function() {
User.login(incompleteCredentials, function(err, accessToken) { User.login(incompleteCredentials, function(err, accessToken) {
assert(err); assert(err);
assert.equal(err.code, 'USERNAME_EMAIL_REQUIRED'); assert.equal(err.code, 'USERNAME_EMAIL_REQUIRED');
done(); done();
}); });
}); });
@ -454,11 +483,13 @@ describe('User', function() {
User.login(incompleteCredentials) User.login(incompleteCredentials)
.then(function(accessToken) { .then(function(accessToken) {
assert(!accessToken); assert(!accessToken);
done(); done();
}) })
.catch(function(err) { .catch(function(err) {
assert(err); assert(err);
assert.equal(err.code, 'USERNAME_EMAIL_REQUIRED'); assert.equal(err.code, 'USERNAME_EMAIL_REQUIRED');
done(); done();
}); });
}); });
@ -470,9 +501,8 @@ describe('User', function() {
.expect(200) .expect(200)
.send(validCredentials) .send(validCredentials)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var accessToken = res.body; var accessToken = res.body;
assert(accessToken.userId); assert(accessToken.userId);
@ -491,11 +521,11 @@ describe('User', function() {
.expect(401) .expect(401)
.send(invalidCredentials) .send(invalidCredentials)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert.equal(errorResponse.code, 'LOGIN_FAILED'); assert.equal(errorResponse.code, 'LOGIN_FAILED');
done(); done();
}); });
}); });
@ -507,11 +537,11 @@ describe('User', function() {
.expect(400) .expect(400)
.send(incompleteCredentials) .send(incompleteCredentials)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert.equal(errorResponse.code, 'USERNAME_EMAIL_REQUIRED'); assert.equal(errorResponse.code, 'USERNAME_EMAIL_REQUIRED');
done(); done();
}); });
}); });
@ -524,11 +554,11 @@ describe('User', function() {
.expect(400) .expect(400)
.send(JSON.stringify(validCredentials)) .send(JSON.stringify(validCredentials))
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert.equal(errorResponse.code, 'USERNAME_EMAIL_REQUIRED'); assert.equal(errorResponse.code, 'USERNAME_EMAIL_REQUIRED');
done(); done();
}); });
}); });
@ -540,13 +570,13 @@ describe('User', function() {
.expect(200) .expect(200)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var token = res.body; var token = res.body;
expect(token.user, 'body.user').to.not.equal(undefined); expect(token.user, 'body.user').to.not.equal(undefined);
expect(token.user, 'body.user') expect(token.user, 'body.user')
.to.have.property('email', validCredentials.email); .to.have.property('email', validCredentials.email);
done(); done();
}); });
}); });
@ -558,13 +588,13 @@ describe('User', function() {
.expect(200) .expect(200)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var token = res.body; var token = res.body;
expect(token.user, 'body.user').to.not.equal(undefined); expect(token.user, 'body.user').to.not.equal(undefined);
expect(token.user, 'body.user') expect(token.user, 'body.user')
.to.have.property('email', validCredentials.email); .to.have.property('email', validCredentials.email);
done(); done();
}); });
}); });
@ -591,6 +621,7 @@ describe('User', function() {
// error message should be "login failed" and not "login failed as the email has not been verified" // error message should be "login failed" and not "login failed as the email has not been verified"
assert(err && !/verified/.test(err.message), ('expecting "login failed" error message, received: "' + err.message + '"')); assert(err && !/verified/.test(err.message), ('expecting "login failed" error message, received: "' + err.message + '"'));
assert.equal(err.code, 'LOGIN_FAILED'); assert.equal(err.code, 'LOGIN_FAILED');
done(); done();
}); });
}); });
@ -605,6 +636,7 @@ describe('User', function() {
// error message should be "login failed" and not "login failed as the email has not been verified" // error message should be "login failed" and not "login failed as the email has not been verified"
assert(err && !/verified/.test(err.message), ('expecting "login failed" error message, received: "' + err.message + '"')); assert(err && !/verified/.test(err.message), ('expecting "login failed" error message, received: "' + err.message + '"'));
assert.equal(err.code, 'LOGIN_FAILED'); assert.equal(err.code, 'LOGIN_FAILED');
done(); done();
}); });
}); });
@ -613,6 +645,7 @@ describe('User', function() {
User.login(validCredentials, function(err, accessToken) { User.login(validCredentials, function(err, accessToken) {
assert(err); assert(err);
assert.equal(err.code, 'LOGIN_FAILED_EMAIL_NOT_VERIFIED'); assert.equal(err.code, 'LOGIN_FAILED_EMAIL_NOT_VERIFIED');
done(); done();
}); });
}); });
@ -625,6 +658,7 @@ describe('User', function() {
.catch(function(err) { .catch(function(err) {
assert(err); assert(err);
assert.equal(err.code, 'LOGIN_FAILED_EMAIL_NOT_VERIFIED'); assert.equal(err.code, 'LOGIN_FAILED_EMAIL_NOT_VERIFIED');
done(); done();
}); });
}); });
@ -632,6 +666,7 @@ describe('User', function() {
it('Login a user by with email verification', function(done) { it('Login a user by with email verification', function(done) {
User.login(validCredentialsEmailVerified, function(err, accessToken) { User.login(validCredentialsEmailVerified, function(err, accessToken) {
assertGoodToken(accessToken); assertGoodToken(accessToken);
done(); done();
}); });
}); });
@ -640,6 +675,7 @@ describe('User', function() {
User.login(validCredentialsEmailVerified) User.login(validCredentialsEmailVerified)
.then(function(accessToken) { .then(function(accessToken) {
assertGoodToken(accessToken); assertGoodToken(accessToken);
done(); done();
}) })
.catch(function(err) { .catch(function(err) {
@ -654,9 +690,8 @@ describe('User', function() {
.expect(200) .expect(200)
.send(validCredentialsEmailVerified) .send(validCredentialsEmailVerified)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var accessToken = res.body; var accessToken = res.body;
assertGoodToken(accessToken); assertGoodToken(accessToken);
@ -673,14 +708,14 @@ describe('User', function() {
.expect(401) .expect(401)
.send({ email: validCredentialsEmail }) .send({ email: validCredentialsEmail })
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
// strongloop/loopback#931 // strongloop/loopback#931
// error message should be "login failed" and not "login failed as the email has not been verified" // error message should be "login failed" and not "login failed as the email has not been verified"
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert(errorResponse && !/verified/.test(errorResponse.message), ('expecting "login failed" error message, received: "' + errorResponse.message + '"')); assert(errorResponse && !/verified/.test(errorResponse.message), ('expecting "login failed" error message, received: "' + errorResponse.message + '"'));
assert.equal(errorResponse.code, 'LOGIN_FAILED'); assert.equal(errorResponse.code, 'LOGIN_FAILED');
done(); done();
}); });
}); });
@ -692,11 +727,11 @@ describe('User', function() {
.expect(401) .expect(401)
.send(validCredentials) .send(validCredentials)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert.equal(errorResponse.code, 'LOGIN_FAILED_EMAIL_NOT_VERIFIED'); assert.equal(errorResponse.code, 'LOGIN_FAILED_EMAIL_NOT_VERIFIED');
done(); done();
}); });
}); });
@ -782,9 +817,8 @@ describe('User', function() {
var user1; var user1;
beforeEach(function(done) { beforeEach(function(done) {
User.create(realm1User, function(err, u) { User.create(realm1User, function(err, u) {
if (err) { if (err) return done(err);
return done(err);
}
user1 = u; user1 = u;
User.create(realm2User, done); User.create(realm2User, done);
}); });
@ -794,6 +828,7 @@ describe('User', function() {
User.login(credentialWithoutRealm, function(err, accessToken) { User.login(credentialWithoutRealm, function(err, accessToken) {
assert(err); assert(err);
assert.equal(err.code, 'REALM_REQUIRED'); assert.equal(err.code, 'REALM_REQUIRED');
done(); done();
}); });
}); });
@ -802,6 +837,7 @@ describe('User', function() {
User.login(credentialWithBadRealm, function(err, accessToken) { User.login(credentialWithBadRealm, function(err, accessToken) {
assert(err); assert(err);
assert.equal(err.code, 'LOGIN_FAILED'); assert.equal(err.code, 'LOGIN_FAILED');
done(); done();
}); });
}); });
@ -810,6 +846,7 @@ describe('User', function() {
User.login(credentialWithBadPass, function(err, accessToken) { User.login(credentialWithBadPass, function(err, accessToken) {
assert(err); assert(err);
assert.equal(err.code, 'LOGIN_FAILED'); assert.equal(err.code, 'LOGIN_FAILED');
done(); done();
}); });
}); });
@ -818,6 +855,7 @@ describe('User', function() {
User.login(credentialWithRealm, function(err, accessToken) { User.login(credentialWithRealm, function(err, accessToken) {
assertGoodToken(accessToken); assertGoodToken(accessToken);
assert.equal(accessToken.userId, user1.id); assert.equal(accessToken.userId, user1.id);
done(); done();
}); });
}); });
@ -826,6 +864,7 @@ describe('User', function() {
User.login(credentialRealmInUsername, function(err, accessToken) { User.login(credentialRealmInUsername, function(err, accessToken) {
assertGoodToken(accessToken); assertGoodToken(accessToken);
assert.equal(accessToken.userId, user1.id); assert.equal(accessToken.userId, user1.id);
done(); done();
}); });
}); });
@ -834,6 +873,7 @@ describe('User', function() {
User.login(credentialRealmInEmail, function(err, accessToken) { User.login(credentialRealmInEmail, function(err, accessToken) {
assertGoodToken(accessToken); assertGoodToken(accessToken);
assert.equal(accessToken.userId, user1.id); assert.equal(accessToken.userId, user1.id);
done(); done();
}); });
}); });
@ -851,6 +891,7 @@ describe('User', function() {
User.login(credentialWithRealm, function(err, accessToken) { User.login(credentialWithRealm, function(err, accessToken) {
assertGoodToken(accessToken); assertGoodToken(accessToken);
assert.equal(accessToken.userId, user1.id); assert.equal(accessToken.userId, user1.id);
done(); done();
}); });
}); });
@ -860,6 +901,7 @@ describe('User', function() {
User.login(credentialRealmInEmail, function(err, accessToken) { User.login(credentialRealmInEmail, function(err, accessToken) {
assert(err); assert(err);
assert.equal(err.code, 'REALM_REQUIRED'); assert.equal(err.code, 'REALM_REQUIRED');
done(); done();
}); });
}); });
@ -904,9 +946,8 @@ describe('User', function() {
.expect(200) .expect(200)
.send({email: 'foo@bar.com', password: 'bar'}) .send({email: 'foo@bar.com', password: 'bar'})
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var accessToken = res.body; var accessToken = res.body;
assert(accessToken.userId); assert(accessToken.userId);
@ -929,12 +970,11 @@ describe('User', function() {
assert(token); assert(token);
return function(err) { return function(err) {
if (err) { if (err) return done(err);
return done(err);
}
AccessToken.findById(token, function(err, accessToken) { AccessToken.findById(token, function(err, accessToken) {
assert(!accessToken, 'accessToken should not exist after logging out'); assert(!accessToken, 'accessToken should not exist after logging out');
done(err); done(err);
}); });
}; };
@ -946,6 +986,7 @@ describe('User', function() {
var u = new User({username: 'foo', password: 'bar'}); var u = new User({username: 'foo', password: 'bar'});
u.hasPassword('bar', function(err, isMatch) { u.hasPassword('bar', function(err, isMatch) {
assert(isMatch, 'password doesnt match'); assert(isMatch, 'password doesnt match');
done(); done();
}); });
}); });
@ -955,6 +996,7 @@ describe('User', function() {
u.hasPassword('bar') u.hasPassword('bar')
.then(function(isMatch) { .then(function(isMatch) {
assert(isMatch, 'password doesnt match'); assert(isMatch, 'password doesnt match');
done(); done();
}) })
.catch(function(err) { .catch(function(err) {
@ -969,6 +1011,7 @@ describe('User', function() {
User.findById(user.id, function(err, uu) { User.findById(user.id, function(err, uu) {
uu.hasPassword('b', function(err, isMatch) { uu.hasPassword('b', function(err, isMatch) {
assert(isMatch); assert(isMatch);
done(); done();
}); });
}); });
@ -988,6 +1031,7 @@ describe('User', function() {
User.findById(user.id, function(err, uu) { User.findById(user.id, function(err, uu) {
uu.hasPassword('baz2', function(err, isMatch) { uu.hasPassword('baz2', function(err, isMatch) {
assert(isMatch); assert(isMatch);
done(); done();
}); });
}); });
@ -1022,6 +1066,7 @@ describe('User', function() {
var msg = result.email.response.toString('utf-8'); var msg = result.email.response.toString('utf-8');
assert(~msg.indexOf('/api/test-users/confirm')); assert(~msg.indexOf('/api/test-users/confirm'));
assert(~msg.indexOf('To: bar@bat.com')); assert(~msg.indexOf('To: bar@bat.com'));
done(); done();
}); });
}); });
@ -1032,9 +1077,7 @@ describe('User', function() {
.expect(200) .expect(200)
.send({email: 'bar@bat.com', password: 'bar'}) .send({email: 'bar@bat.com', password: 'bar'})
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
}); });
}); });
@ -1059,6 +1102,7 @@ describe('User', function() {
var msg = result.email.response.toString('utf-8'); var msg = result.email.response.toString('utf-8');
assert(~msg.indexOf('/api/test-users/confirm')); assert(~msg.indexOf('/api/test-users/confirm'));
assert(~msg.indexOf('To: bar@bat.com')); assert(~msg.indexOf('To: bar@bat.com'));
done(); done();
}) })
.catch(function(err) { .catch(function(err) {
@ -1072,9 +1116,7 @@ describe('User', function() {
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
}); });
}); });
@ -1095,6 +1137,7 @@ describe('User', function() {
user.verify(options, function(err, result) { user.verify(options, function(err, result) {
assert(result.email); assert(result.email);
assert.equal(result.email.messageId, 'custom-header-value'); assert.equal(result.email.messageId, 'custom-header-value');
done(); done();
}); });
}); });
@ -1105,9 +1148,7 @@ describe('User', function() {
.expect(200) .expect(200)
.send({email: 'bar@bat.com', password: 'bar'}) .send({email: 'bar@bat.com', password: 'bar'})
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
}); });
}); });
@ -1141,6 +1182,7 @@ describe('User', function() {
assert.equal(result.token, 'token-123456'); assert.equal(result.token, 'token-123456');
var msg = result.email.response.toString('utf-8'); var msg = result.email.response.toString('utf-8');
assert(~msg.indexOf('token-123456')); assert(~msg.indexOf('token-123456'));
done(); done();
}); });
}); });
@ -1151,9 +1193,7 @@ describe('User', function() {
.expect(200) .expect(200)
.send({email: 'bar@bat.com', password: 'bar'}) .send({email: 'bar@bat.com', password: 'bar'})
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
}); });
}); });
@ -1180,6 +1220,7 @@ describe('User', function() {
assert(err); assert(err);
assert.equal(err.message, 'Fake error'); assert.equal(err.message, 'Fake error');
assert.equal(result, undefined); assert.equal(result, undefined);
done(); done();
}); });
}); });
@ -1190,9 +1231,7 @@ describe('User', function() {
.expect(200) .expect(200)
.send({email: 'bar@bat.com', password: 'bar'}) .send({email: 'bar@bat.com', password: 'bar'})
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
}); });
}); });
@ -1214,6 +1253,7 @@ describe('User', function() {
user.verify(options, function(err, result) { user.verify(options, function(err, result) {
var msg = result.email.response.toString('utf-8'); var msg = result.email.response.toString('utf-8');
assert(~msg.indexOf('http://myapp.org:3000/')); assert(~msg.indexOf('http://myapp.org:3000/'));
done(); done();
}); });
}); });
@ -1224,9 +1264,7 @@ describe('User', function() {
.expect(200) .expect(200)
.send({email: 'bar@bat.com', password: 'bar'}) .send({email: 'bar@bat.com', password: 'bar'})
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
}); });
}); });
@ -1247,6 +1285,7 @@ describe('User', function() {
user.verify(options, function(err, result) { user.verify(options, function(err, result) {
var msg = result.email.response.toString('utf-8'); var msg = result.email.response.toString('utf-8');
assert(~msg.indexOf('http://myapp.org/')); assert(~msg.indexOf('http://myapp.org/'));
done(); done();
}); });
}); });
@ -1257,9 +1296,7 @@ describe('User', function() {
.expect(200) .expect(200)
.send({email: 'bar@bat.com', password: 'bar'}) .send({email: 'bar@bat.com', password: 'bar'})
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
}); });
}); });
@ -1280,6 +1317,7 @@ describe('User', function() {
user.verify(options, function(err, result) { user.verify(options, function(err, result) {
var msg = result.email.response.toString('utf-8'); var msg = result.email.response.toString('utf-8');
assert(~msg.indexOf('https://myapp.org:3000/')); assert(~msg.indexOf('https://myapp.org:3000/'));
done(); done();
}); });
}); });
@ -1290,9 +1328,7 @@ describe('User', function() {
.expect(200) .expect(200)
.send({email: 'bar@bat.com', password: 'bar'}) .send({email: 'bar@bat.com', password: 'bar'})
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
}); });
}); });
@ -1313,6 +1349,7 @@ describe('User', function() {
user.verify(options, function(err, result) { user.verify(options, function(err, result) {
var msg = result.email.response.toString('utf-8'); var msg = result.email.response.toString('utf-8');
assert(~msg.indexOf('https://myapp.org/')); assert(~msg.indexOf('https://myapp.org/'));
done(); done();
}); });
}); });
@ -1323,9 +1360,7 @@ describe('User', function() {
.expect(200) .expect(200)
.send({email: 'bar@bat.com', password: 'bar'}) .send({email: 'bar@bat.com', password: 'bar'})
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
}); });
}); });
}); });
@ -1334,6 +1369,7 @@ describe('User', function() {
var user = new User({email: 'bar@bat.com', password: 'bar', verificationToken: 'a-token' }); var user = new User({email: 'bar@bat.com', password: 'bar', verificationToken: 'a-token' });
var data = user.toJSON(); var data = user.toJSON();
assert(!('verificationToken' in data)); assert(!('verificationToken' in data));
done(); done();
}); });
}); });
@ -1355,9 +1391,8 @@ describe('User', function() {
}; };
user.verify(options, function(err, result) { user.verify(options, function(err, result) {
if (err) { if (err) return done(err);
return done(err);
}
testFunc(result, done); testFunc(result, done);
}); });
}); });
@ -1368,9 +1403,7 @@ describe('User', function() {
.expect(302) .expect(302)
.send({email: 'bar@bat.com', password: 'bar'}) .send({email: 'bar@bat.com', password: 'bar'})
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
}); });
} }
@ -1382,9 +1415,8 @@ describe('User', function() {
'&redirect=' + encodeURIComponent(options.redirect)) '&redirect=' + encodeURIComponent(options.redirect))
.expect(302) .expect(302)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
done(); done();
}); });
}, done); }, done);
@ -1420,12 +1452,12 @@ describe('User', function() {
'&redirect=' + encodeURIComponent(options.redirect)) '&redirect=' + encodeURIComponent(options.redirect))
.expect(404) .expect(404)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert(errorResponse); assert(errorResponse);
assert.equal(errorResponse.code, 'USER_NOT_FOUND'); assert.equal(errorResponse.code, 'USER_NOT_FOUND');
done(); done();
}); });
}, done); }, done);
@ -1439,12 +1471,12 @@ describe('User', function() {
'&redirect=' + encodeURIComponent(options.redirect)) '&redirect=' + encodeURIComponent(options.redirect))
.expect(400) .expect(400)
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert(errorResponse); assert(errorResponse);
assert.equal(errorResponse.code, 'INVALID_TOKEN'); assert.equal(errorResponse.code, 'INVALID_TOKEN');
done(); done();
}); });
}, done); }, done);
@ -1460,6 +1492,7 @@ describe('User', function() {
User.resetPassword({ }, function(err) { User.resetPassword({ }, function(err) {
assert(err); assert(err);
assert.equal(err.code, 'EMAIL_REQUIRED'); assert.equal(err.code, 'EMAIL_REQUIRED');
done(); done();
}); });
}); });
@ -1472,6 +1505,7 @@ describe('User', function() {
.catch(function(err) { .catch(function(err) {
assert(err); assert(err);
assert.equal(err.code, 'EMAIL_REQUIRED'); assert.equal(err.code, 'EMAIL_REQUIRED');
done(); done();
}); });
}); });
@ -1481,6 +1515,7 @@ describe('User', function() {
assert(err); assert(err);
assert.equal(err.code, 'EMAIL_NOT_FOUND'); assert.equal(err.code, 'EMAIL_NOT_FOUND');
assert.equal(err.statusCode, 404); assert.equal(err.statusCode, 404);
done(); done();
}); });
}); });
@ -1502,7 +1537,9 @@ describe('User', function() {
assert(calledBack); assert(calledBack);
info.accessToken.user(function(err, user) { info.accessToken.user(function(err, user) {
if (err) return done(err); if (err) return done(err);
assert.equal(user.email, email); assert.equal(user.email, email);
done(); done();
}); });
}); });
@ -1515,12 +1552,12 @@ describe('User', function() {
.expect(400) .expect(400)
.send({ }) .send({ })
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
var errorResponse = res.body.error; var errorResponse = res.body.error;
assert(errorResponse); assert(errorResponse);
assert.equal(errorResponse.code, 'EMAIL_REQUIRED'); assert.equal(errorResponse.code, 'EMAIL_REQUIRED');
done(); done();
}); });
}); });
@ -1532,10 +1569,10 @@ describe('User', function() {
.expect(204) .expect(204)
.send({ email: email }) .send({ email: email })
.end(function(err, res) { .end(function(err, res) {
if (err) { if (err) return done(err);
return done(err);
}
assert.deepEqual(res.body, { }); assert.deepEqual(res.body, { });
done(); done();
}); });
}); });

View File

@ -130,6 +130,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
user.isValid(function(valid) { user.isValid(function(valid) {
assert(valid === false); assert(valid === false);
assert(user.errors.age, 'model should have age error'); assert(user.errors.age, 'model should have age error');
done(); done();
}); });
}); });
@ -139,6 +140,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
it('Create an instance of Model with given data and save to the attached data source', function(done) { it('Create an instance of Model with given data and save to the attached data source', function(done) {
User.create({first: 'Joe', last: 'Bob'}, function(err, user) { User.create({first: 'Joe', last: 'Bob'}, function(err, user) {
assert(user instanceof User); assert(user instanceof User);
done(); done();
}); });
}); });
@ -151,6 +153,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
assert(user.id); assert(user.id);
assert(!err); assert(!err);
assert(!user.errors); assert(!user.errors);
done(); done();
}); });
}); });
@ -170,6 +173,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
assert.equal(updatedUser.first, 'updatedFirst'); assert.equal(updatedUser.first, 'updatedFirst');
assert.equal(updatedUser.last, 'updatedLast'); assert.equal(updatedUser.last, 'updatedLast');
assert.equal(updatedUser.age, 100); assert.equal(updatedUser.age, 100);
done(); done();
}); });
}); });
@ -185,6 +189,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
User.upsert({first: 'bob', id: 7}, function(err, updatedUser) { User.upsert({first: 'bob', id: 7}, function(err, updatedUser) {
assert(!err); assert(!err);
assert.equal(updatedUser.first, 'bob'); assert.equal(updatedUser.first, 'bob');
done(); done();
}); });
}); });
@ -196,12 +201,16 @@ module.exports = function defineModelTestsWithDataSource(options) {
User.create({first: 'joe', last: 'bob'}, function(err, user) { User.create({first: 'joe', last: 'bob'}, function(err, user) {
User.findById(user.id, function(err, foundUser) { User.findById(user.id, function(err, foundUser) {
if (err) return done(err); if (err) return done(err);
assert.equal(user.id, foundUser.id); assert.equal(user.id, foundUser.id);
User.deleteById(foundUser.id, function(err) { User.deleteById(foundUser.id, function(err) {
if (err) return done(err); if (err) return done(err);
User.find({ where: { id: user.id } }, function(err, found) {
User.find({ where: { id: user.id }}, function(err, found) {
if (err) return done(err); if (err) return done(err);
assert.equal(found.length, 0); assert.equal(found.length, 0);
done(); done();
}); });
}); });
@ -216,6 +225,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
User.deleteById(user.id, function(err) { User.deleteById(user.id, function(err) {
User.findById(user.id, function(err, notFound) { User.findById(user.id, function(err, notFound) {
assert.equal(notFound, null); assert.equal(notFound, null);
done(); done();
}); });
}); });
@ -230,6 +240,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
assert.equal(user.id, 23); assert.equal(user.id, 23);
assert.equal(user.first, 'michael'); assert.equal(user.first, 'michael');
assert.equal(user.last, 'jordan'); assert.equal(user.last, 'jordan');
done(); done();
}); });
}); });
@ -247,6 +258,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
.on('done', function() { .on('done', function() {
User.count({age: {gt: 99}}, function(err, count) { User.count({age: {gt: 99}}, function(err, count) {
assert.equal(count, 2); assert.equal(count, 2);
done(); done();
}); });
}); });