0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Merge pull request #1442 from Decad/user-gravatar

Add users Gravatar on signup
This commit is contained in:
Hannah Wolfe 2013-11-12 03:53:49 -08:00
commit 3235a3a3e2
3 changed files with 69 additions and 4 deletions

View file

@ -9,7 +9,9 @@ var User,
Posts = require('./post').Posts,
ghostBookshelf = require('./base'),
Role = require('./role').Role,
Permission = require('./permission').Permission;
Permission = require('./permission').Permission,
http = require('http'),
crypto = require('crypto');
function validatePasswordLength(password) {
@ -113,6 +115,9 @@ User = ghostBookshelf.Model.extend({
}).then(function (hash) {
// Assign the hashed password
userData.password = hash;
// LookupGravatar
return self.gravatarLookup(userData);
}).then(function (userData) {
// Save the user with the hashed password
return ghostBookshelf.Model.add.call(self, userData);
}).then(function (addedUser) {
@ -245,6 +250,25 @@ User = ghostBookshelf.Model.extend({
return when.resolve(allPerms);
}, errors.logAndThrowError);
},
gravatarLookup: function (userData) {
var gravatarUrl = 'http://www.gravatar.com/avatar/' +
crypto.createHash('md5').update(userData.email.toLowerCase().trim()).digest('hex') +
"?d=404",
checkPromise = when.defer();
http.get(gravatarUrl, function (res) {
if (res.statusCode !== 404) {
userData.image = gravatarUrl;
}
checkPromise.resolve(userData);
}).on('error', function () {
//Error making request just continue.
checkPromise.resolve(userData);
});
return checkPromise.promise;
}
});

View file

@ -4,6 +4,7 @@ var testUtils = require('../../utils'),
when = require('when'),
_ = require('underscore'),
errors = require('../../../server/errorHandling'),
sinon = require('sinon'),
// Stuff we are testing
Models = require('../../../server/models');
@ -32,14 +33,48 @@ describe('User Model', function run() {
});
it('can add first', function (done) {
var userData = testUtils.DataGenerator.forModel.users[0];
var userData = testUtils.DataGenerator.forModel.users[0],
gravatarStub = sinon.stub(UserModel, 'gravatarLookup', function (userData) {
return when.resolve(userData);
});
UserModel.add(userData).then(function (createdUser) {
should.exist(createdUser);
createdUser.has('uuid').should.equal(true);
createdUser.attributes.password.should.not.equal(userData.password, "password was hashed");
createdUser.attributes.email.should.eql(userData.email, "email address correct");
gravatarStub.restore();
done();
}).then(null, done);
});
it('can find gravatar', function (done) {
var userData = testUtils.DataGenerator.forModel.users[4],
gravatarStub = sinon.stub(UserModel, 'gravatarLookup', function (userData) {
userData.image = 'http://www.gravatar.com/avatar/2fab21a4c4ed88e76add10650c73bae1?d=404'
return when.resolve(userData);
});
UserModel.add(userData).then(function (createdUser) {
should.exist(createdUser);
createdUser.has('uuid').should.equal(true);
createdUser.attributes.image.should.eql('http://www.gravatar.com/avatar/2fab21a4c4ed88e76add10650c73bae1?d=404', 'Gravatar found');
gravatarStub.restore();
done();
}).then(null, done);
});
it('can handle no gravatar', function(done) {
var userData = testUtils.DataGenerator.forModel.users[0],
gravatarStub = sinon.stub(UserModel, 'gravatarLookup', function (userData) {
return when.resolve(userData);
});
UserModel.add(userData).then(function (createdUser) {
should.exist(createdUser);
createdUser.has('uuid').should.equal(true);
should.not.exist(createdUser.image);
gravatarStub.restore();
done();
}).then(null, done);
});

View file

@ -71,6 +71,12 @@ DataGenerator.Content = {
slug: 'slimer-mcectoplasm',
email: 'smcectoplasm@example.com',
password: '$2a$10$.pZeeBE0gHXd0PTnbT/ph.GEKgd0Wd3q2pWna3ynTGBkPKnGIKZL6'
},
{
name: 'Ivan Email',
slug: 'ivan-email',
email: 'info@ghost.org',
password: '$2a$10$.pZeeBE0gHXd0PTnbT/ph.GEKgd0Wd3q2pWna3ynTGBkPKnGIKZL6'
}
]
};