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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -32,8 +32,10 @@ describe('Replication', function() {
}, function(err, created) {
LocalTestModel.replicate(0, TestModel, function() {
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);
done();
});
});

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -130,6 +130,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
user.isValid(function(valid) {
assert(valid === false);
assert(user.errors.age, 'model should have age error');
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) {
User.create({first: 'Joe', last: 'Bob'}, function(err, user) {
assert(user instanceof User);
done();
});
});
@ -151,6 +153,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
assert(user.id);
assert(!err);
assert(!user.errors);
done();
});
});
@ -170,6 +173,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
assert.equal(updatedUser.first, 'updatedFirst');
assert.equal(updatedUser.last, 'updatedLast');
assert.equal(updatedUser.age, 100);
done();
});
});
@ -185,6 +189,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
User.upsert({first: 'bob', id: 7}, function(err, updatedUser) {
assert(!err);
assert.equal(updatedUser.first, 'bob');
done();
});
});
@ -196,12 +201,16 @@ module.exports = function defineModelTestsWithDataSource(options) {
User.create({first: 'joe', last: 'bob'}, function(err, user) {
User.findById(user.id, function(err, foundUser) {
if (err) return done(err);
assert.equal(user.id, foundUser.id);
User.deleteById(foundUser.id, function(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);
assert.equal(found.length, 0);
done();
});
});
@ -216,6 +225,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
User.deleteById(user.id, function(err) {
User.findById(user.id, function(err, notFound) {
assert.equal(notFound, null);
done();
});
});
@ -230,6 +240,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
assert.equal(user.id, 23);
assert.equal(user.first, 'michael');
assert.equal(user.last, 'jordan');
done();
});
});
@ -247,6 +258,7 @@ module.exports = function defineModelTestsWithDataSource(options) {
.on('done', function() {
User.count({age: {gt: 99}}, function(err, count) {
assert.equal(count, 2);
done();
});
});