update test case

This commit is contained in:
loay 2017-10-02 00:07:44 -04:00
parent 2b7beed14b
commit a74f8726fb
1 changed files with 18 additions and 28 deletions

View File

@ -73,23 +73,18 @@ describe('User', function() {
}); });
BasicUser = app.registry.createModel({ BasicUser = app.registry.createModel({
name: 'BasicUser', name: 'Basic',
base: 'User', base: 'User',
http: {path: 'test-basic'}, http: {path: 'test-basic'},
// forceId is set to false for the purpose of updating the same affected user within the
// `Email Update` test cases.
forceId: false, forceId: false,
// Speed up the password hashing algorithm for tests
saltWorkFactor: 4, saltWorkFactor: 4,
}); });
AdminUser = app.registry.createModel({ AdminUser = app.registry.createModel({
name: 'AdminUser', name: 'AdminU',
base: 'User', base: 'User',
http: {path: 'test-basic'}, http: {path: 'test-basic'},
// forceId is set to false for the purpose of updating the same affected user within the
// `Email Update` test cases.
forceId: false, forceId: false,
// Speed up the password hashing algorithm for tests
saltWorkFactor: 4, saltWorkFactor: 4,
}); });
@ -101,8 +96,6 @@ describe('User', function() {
app.model(AccessToken, {dataSource: 'db'}); app.model(AccessToken, {dataSource: 'db'});
User.email = Email; User.email = Email;
BasicUser.email=Email;
AdminUser.email=Email;
// Update the AccessToken relation to use the subclass of User // Update the AccessToken relation to use the subclass of User
AccessToken.belongsTo(User, {as: 'user', foreignKey: 'userId'}); AccessToken.belongsTo(User, {as: 'user', foreignKey: 'userId'});
@ -119,6 +112,8 @@ describe('User', function() {
// allow many User.afterRemote's to be called // allow many User.afterRemote's to be called
User.setMaxListeners(0); User.setMaxListeners(0);
BasicUser.setMaxListeners(0);
AdminUser.setMaxListeners(0);
app.enableAuth({dataSource: 'db'}); app.enableAuth({dataSource: 'db'});
app.use(loopback.token({model: AccessToken})); app.use(loopback.token({model: AccessToken}));
@ -2464,7 +2459,6 @@ describe('User', function() {
async.series([ async.series([
function(next) { function(next) {
BasicUser.create({ name: 'BU', email: 'u@example.com', password: 'foobar'}, function(err, user) { BasicUser.create({ name: 'BU', email: 'u@example.com', password: 'foobar'}, function(err, user) {
console.log(user);
if (err) return done(err); if (err) return done(err);
next(); next();
}); });
@ -2472,34 +2466,30 @@ describe('User', function() {
function(next) { function(next) {
AdminUser.create({name: 'AU', email: 'a@example.com', password: 'foobar'}, function(err, user) { AdminUser.create({name: 'AU', email: 'a@example.com', password: 'foobar'}, function(err, user) {
AdminUserReset = user; AdminUserReset = user;
console.log(user);
if (err) return done(err); if (err) return done(err);
next(); next();
}); });
}, },
function(next) { function(next) {
User.login({email:'u@example.com', password: 'foobar'}, function(err, accessToken1) { BasicUser.login({email:'u@example.com', password: 'foobar'}, function(err, accessToken1) {
accessToken2 = accessToken1; accessToken2 = accessToken1;
if (err) return next(err); if (err) return next(err);
console.log(accessToken1); assert(accessToken1.id);
assert(accessToken1.userId);
next(); next();
}); });
}, },
function(next) { function(next) {
return triggerPasswordReset(AdminUserReset.email) request(app)
.then(info => { .post('/test-basic/reset-password')
// Make a REST request to change the password .set('Authorization', accessToken2.id)
return request(app) .expect('Content-Type', /json/)
.patch(`/test-basic/1`) .expect(401)
.set('Authorization', accessToken2.id) .send({email: AdminUserReset.email, newPassword: 'new-pass'})
.send({password: 'new-pass'}) .end(function(err, res) {
.expect(401); var errorResponse = res.body.error;
}) assert.equal(errorResponse.statusCode, 401);
.then(() => { if (err) return done(err);
// Call login to verify the password was changed next();
const credentials = {email: AdminUserReset.email, password: 'new-pass'};
return User.login(credentials);
}); });
}, },
], done); ], done);