Merge pull request #975 from greaterweb/fix/user-tests

Fix User Tests
This commit is contained in:
Raymond Feng 2015-01-06 08:39:12 -08:00
commit f3f1298f3e
1 changed files with 90 additions and 21 deletions

View File

@ -11,7 +11,7 @@ describe('User', function() {
var validCredentialsEmailVerified = {email: 'foo1@bar.com', password: 'bar1', emailVerified: true};
var validCredentialsEmailVerifiedOverREST = {email: 'foo2@bar.com', password: 'bar2', emailVerified: true};
var validCredentialsWithTTL = {email: 'foo@bar.com', password: 'bar', ttl: 3600};
var invalidCredentials = {email: 'foo1@bar.com', password: 'bar1'};
var invalidCredentials = {email: 'foo1@bar.com', password: 'invalid'};
var incompleteCredentials = {password: 'bar1'};
beforeEach(function() {
@ -142,6 +142,9 @@ describe('User', function() {
.expect(200)
.send(validCredentialsEmailVerifiedOverREST)
.end(function(err, res) {
if (err) {
return done(err);
}
assert(!res.body.emailVerified);
done();
});
@ -197,6 +200,14 @@ describe('User', function() {
});
});
it('Login should only allow correct credentials', function(done) {
User.login(invalidCredentials, function(err, accessToken) {
assert(err);
assert(!accessToken);
done();
});
});
it('Login a user over REST by providing credentials', function(done) {
request(app)
.post('/users/login')
@ -204,7 +215,9 @@ 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);
@ -223,6 +236,9 @@ describe('User', function() {
.expect(401)
.send(invalidCredentials)
.end(function(err, res) {
if (err) {
return done(err);
}
done();
});
});
@ -234,6 +250,9 @@ describe('User', function() {
.expect(400)
.send(incompleteCredentials)
.end(function(err, res) {
if (err) {
return done(err);
}
done();
});
});
@ -244,8 +263,11 @@ describe('User', function() {
.set('Content-Type', null)
.expect('Content-Type', /json/)
.expect(400)
.send(validCredentials)
.send(JSON.stringify(validCredentials))
.end(function(err, res) {
if (err) {
return done(err);
}
done();
});
});
@ -257,7 +279,9 @@ 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')
@ -273,7 +297,9 @@ 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')
@ -282,15 +308,6 @@ describe('User', function() {
});
});
it('Login should only allow correct credentials', function(done) {
User.create({email: 'foo22@bar.com', password: 'bar'}, function(user, err) {
User.login({email: 'foo44@bar.com', password: 'bar'}, function(err, accessToken) {
assert(err);
assert(!accessToken);
done();
});
});
});
});
function assertGoodToken(accessToken) {
@ -329,7 +346,9 @@ 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);
@ -346,6 +365,9 @@ describe('User', function() {
.expect(401)
.send(validCredentials)
.end(function(err, res) {
if (err) {
return done(err);
}
done();
});
});
@ -535,7 +557,9 @@ 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);
@ -558,7 +582,9 @@ 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');
@ -647,7 +673,9 @@ 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);
}
});
});
@ -678,7 +706,9 @@ 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);
}
});
});
@ -761,7 +791,9 @@ describe('User', function() {
+ '&redirect=' + encodeURIComponent(options.redirect))
.expect(400)
.end(function(err, res) {
if (err) return done(err);
if (err) {
return done(err);
}
assert(res.body.error);
done();
});
@ -772,9 +804,17 @@ describe('User', function() {
describe('Password Reset', function() {
describe('User.resetPassword(options, cb)', function() {
var email = 'foo@bar.com';
it('Requires email address to reset password', function(done) {
User.resetPassword({ }, function(err) {
assert(err);
done();
});
});
it('Creates a temp accessToken to allow a user to change password', function(done) {
var calledBack = false;
var email = 'foo@bar.com';
User.resetPassword({
email: email
@ -794,6 +834,35 @@ describe('User', function() {
});
});
});
it('Password reset over REST rejected without email address', function(done) {
request(app)
.post('/users/reset')
.expect('Content-Type', /json/)
.expect(400)
.send({ })
.end(function(err, res) {
if (err) {
return done(err);
}
done();
});
});
it('Password reset over REST requires email address', function(done) {
request(app)
.post('/users/reset')
.expect('Content-Type', /json/)
.expect(204)
.send({ email: email })
.end(function(err, res) {
if (err) {
return done(err);
}
assert.deepEqual(res.body, { });
done();
});
});
});
});