From cbe8c15dc8e494af3c77b7085f201afe4601a67b Mon Sep 17 00:00:00 2001 From: Declan cook Date: Mon, 11 Nov 2013 19:55:22 +0000 Subject: [PATCH] Add users Gravatar on signup When a user registers try to find their gravatar. --- core/server/models/user.js | 26 ++++++++++++- .../integration/model/model_users_spec.js | 37 ++++++++++++++++++- core/test/utils/fixtures/data-generator.js | 10 ++++- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/core/server/models/user.js b/core/server/models/user.js index 6dfbff9ac9..55c7b5a07b 100644 --- a/core/server/models/user.js +++ b/core/server/models/user.js @@ -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; } }); diff --git a/core/test/integration/model/model_users_spec.js b/core/test/integration/model/model_users_spec.js index f2573a7166..ca36371e9d 100644 --- a/core/test/integration/model/model_users_spec.js +++ b/core/test/integration/model/model_users_spec.js @@ -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); }); diff --git a/core/test/utils/fixtures/data-generator.js b/core/test/utils/fixtures/data-generator.js index 4b878f602a..e89eca16ab 100644 --- a/core/test/utils/fixtures/data-generator.js +++ b/core/test/utils/fixtures/data-generator.js @@ -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' } ] }; @@ -201,8 +207,8 @@ DataGenerator.forModel = (function () { users = _.map(DataGenerator.Content.users, function (user) { var user = _.pick(user, 'name', 'email'); - return _.defaults({ - password: 'Sl1m3rson' + return _.defaults({ + password: 'Sl1m3rson' }, user); });