diff --git a/app/models/User.coffee b/app/models/User.coffee index bfc956f27..30a5a11c8 100644 --- a/app/models/User.coffee +++ b/app/models/User.coffee @@ -281,22 +281,22 @@ module.exports = class User extends CocoModel window.tracker?.trackEvent 'Finished Signup', category: "Signup", label: 'CodeCombat' return jqxhr - signupWithFacebook: (email, facebookID, options={}) -> + signupWithFacebook: (name, email, facebookID, options={}) -> options.url = _.result(@, 'url') + '/signup-with-facebook' options.type = 'POST' options.data ?= {} - _.extend(options.data, {email, facebookID, facebookAccessToken: application.facebookHandler.token()}) + _.extend(options.data, {name, email, facebookID, facebookAccessToken: application.facebookHandler.token()}) jqxhr = @fetch(options) jqxhr.then -> window.tracker?.trackEvent 'Facebook Login', category: "Signup", label: 'Facebook' window.tracker?.trackEvent 'Finished Signup', category: "Signup", label: 'Facebook' return jqxhr - signupWithGPlus: (email, gplusID, options={}) -> + signupWithGPlus: (name, email, gplusID, options={}) -> options.url = _.result(@, 'url') + '/signup-with-gplus' options.type = 'POST' options.data ?= {} - _.extend(options.data, {email, gplusID, gplusAccessToken: application.gplusHandler.token()}) + _.extend(options.data, {name, email, gplusID, gplusAccessToken: application.gplusHandler.token()}) jqxhr = @fetch(options) jqxhr.then -> window.tracker?.trackEvent 'Google Login', category: "Signup", label: 'GPlus' diff --git a/server/middleware/users.coffee b/server/middleware/users.coffee index 04c12fafc..968f725ab 100644 --- a/server/middleware/users.coffee +++ b/server/middleware/users.coffee @@ -147,9 +147,12 @@ module.exports = unless req.user.isAnonymous() throw new errors.Forbidden('You are already signed in.') - { facebookID, facebookAccessToken, email } = req.body - unless _.all([facebookID, facebookAccessToken, email]) - throw new errors.UnprocessableEntity('Requires facebookID, facebookAccessToken and email') + { facebookID, facebookAccessToken, email, name } = req.body + unless _.all([facebookID, facebookAccessToken, email, name]) + throw new errors.UnprocessableEntity('Requires facebookID, facebookAccessToken, email, and name') + + if not _.isEmpty(name) and yield User.findByName(name) + throw new errors.Conflict('Name already taken') facebookResponse = yield facebook.fetchMe(facebookAccessToken) emailsMatch = email is facebookResponse.email @@ -157,16 +160,19 @@ module.exports = unless emailsMatch and idsMatch throw new errors.UnprocessableEntity('Invalid facebookAccessToken') - req.user.set({ facebookID, email, anonymous: false }) + req.user.set({ facebookID, email, name, anonymous: false }) yield module.exports.finishSignup(req, res) signupWithGPlus: wrap (req, res) -> unless req.user.isAnonymous() throw new errors.Forbidden('You are already signed in.') - { gplusID, gplusAccessToken, email } = req.body - unless _.all([gplusID, gplusAccessToken, email]) - throw new errors.UnprocessableEntity('Requires gplusID, gplusAccessToken and email') + { gplusID, gplusAccessToken, email, name } = req.body + unless _.all([gplusID, gplusAccessToken, email, name]) + throw new errors.UnprocessableEntity('Requires gplusID, gplusAccessToken, email, and name') + + if not _.isEmpty(name) and yield User.findByName(name) + throw new errors.Conflict('Name already taken') gplusResponse = yield gplus.fetchMe(gplusAccessToken) emailsMatch = email is gplusResponse.email @@ -175,7 +181,7 @@ module.exports = unless emailsMatch and idsMatch throw new errors.UnprocessableEntity('Invalid gplusAccessToken') - req.user.set({ gplusID, email, anonymous: false }) + req.user.set({ gplusID, email, name, anonymous: false }) yield module.exports.finishSignup(req, res) finishSignup: co.wrap (req, res) -> diff --git a/spec/helpers/helper.js b/spec/helpers/helper.js index f133f469c..0d90c2cdc 100644 --- a/spec/helpers/helper.js +++ b/spec/helpers/helper.js @@ -49,6 +49,15 @@ console.error = function() { console.log.apply(console, arguments); }; +if (process.argv.indexOf('--with-test-names') > -1) { + jasmine.getEnv().addReporter({ + specStarted: function(result){ + // Printing weirdly so pass/fail indicator is on the same line as the test name + process.stdout.write('\n' + result.fullName); + } + }) +} + var initialized = false; beforeEach(function(done) { if (initialized) { diff --git a/spec/server/functional/user.spec.coffee b/spec/server/functional/user.spec.coffee index de924318c..6299b8933 100644 --- a/spec/server/functional/user.spec.coffee +++ b/spec/server/functional/user.spec.coffee @@ -696,7 +696,8 @@ describe 'POST /db/user/:handle/signup-with-password', -> user = yield utils.becomeAnonymous() url = getURL("/db/user/#{user.id}/signup-with-password") email = 'some@email.com' - json = { email, password: '12345' } + name = 'someusername' + json = { name, email, password: '12345' } [res, body] = yield request.postAsync({url, json}) expect(res.statusCode).toBe(200) updatedUser = yield User.findById(user.id) @@ -801,6 +802,7 @@ describe 'POST /db/user/:handle/signup-with-password', -> describe 'POST /db/user/:handle/signup-with-facebook', -> facebookID = '12345' facebookEmail = 'some@email.com' + name = 'someusername' validFacebookResponse = new Promise((resolve) -> resolve({ id: facebookID, @@ -835,7 +837,7 @@ describe 'POST /db/user/:handle/signup-with-facebook', -> spyOn(sendwithus.api, 'send') user = yield utils.becomeAnonymous() url = getURL("/db/user/#{user.id}/signup-with-facebook") - json = { email: facebookEmail, facebookID, facebookAccessToken: '...' } + json = { name, email: facebookEmail, facebookID, facebookAccessToken: '...' } [res, body] = yield request.postAsync({url, json}) expect(res.statusCode).toBe(200) updatedUser = yield User.findById(user.id) @@ -858,11 +860,11 @@ describe 'POST /db/user/:handle/signup-with-facebook', -> user = yield utils.becomeAnonymous() url = getURL("/db/user/#{user.id}/signup-with-facebook") - json = { email: 'some-other@email.com', facebookID, facebookAccessToken: '...' } + json = { name, email: 'some-other@email.com', facebookID, facebookAccessToken: '...' } [res, body] = yield request.postAsync({url, json}) expect(res.statusCode).toBe(422) - json = { email: facebookEmail, facebookID: '54321', facebookAccessToken: '...' } + json = { name, email: facebookEmail, facebookID: '54321', facebookAccessToken: '...' } [res, body] = yield request.postAsync({url, json}) expect(res.statusCode).toBe(422) @@ -874,7 +876,7 @@ describe 'POST /db/user/:handle/signup-with-facebook', -> spyOn(facebook, 'fetchMe').and.returnValue(validFacebookResponse) user = yield utils.becomeAnonymous() url = getURL("/db/user/#{user.id}/signup-with-facebook") - json = { email: facebookEmail, facebookID, facebookAccessToken: '...' } + json = { name, email: facebookEmail, facebookID, facebookAccessToken: '...' } [res, body] = yield request.postAsync({url, json}) expect(res.statusCode).toBe(409) done() @@ -883,6 +885,7 @@ describe 'POST /db/user/:handle/signup-with-facebook', -> describe 'POST /db/user/:handle/signup-with-gplus', -> gplusID = '12345' gplusEmail = 'some@email.com' + name = 'someusername' validGPlusResponse = new Promise((resolve) -> resolve({ id: gplusID @@ -923,10 +926,11 @@ describe 'POST /db/user/:handle/signup-with-gplus', -> spyOn(sendwithus.api, 'send') user = yield utils.becomeAnonymous() url = getURL("/db/user/#{user.id}/signup-with-gplus") - json = { email: gplusEmail, gplusID, gplusAccessToken: '...' } + json = { name, email: gplusEmail, gplusID, gplusAccessToken: '...' } [res, body] = yield request.postAsync({url, json}) expect(res.statusCode).toBe(200) updatedUser = yield User.findById(user.id) + expect(updatedUser.get('name')).toBe(name) expect(updatedUser.get('email')).toBe(gplusEmail) expect(updatedUser.get('gplusID')).toBe(gplusID) expect(sendwithus.api.send).toHaveBeenCalled() @@ -936,7 +940,7 @@ describe 'POST /db/user/:handle/signup-with-gplus', -> spyOn(gplus, 'fetchMe').and.returnValue(invalidGPlusResponse) user = yield utils.becomeAnonymous() url = getURL("/db/user/#{user.id}/signup-with-gplus") - json = { email: gplusEmail, gplusID, gplusAccessToken: '...' } + json = { name, email: gplusEmail, gplusID, gplusAccessToken: '...' } [res, body] = yield request.postAsync({url, json}) expect(res.statusCode).toBe(422) done() @@ -946,22 +950,22 @@ describe 'POST /db/user/:handle/signup-with-gplus', -> user = yield utils.becomeAnonymous() url = getURL("/db/user/#{user.id}/signup-with-gplus") - json = { email: 'some-other@email.com', gplusID, gplusAccessToken: '...' } + json = { name, email: 'some-other@email.com', gplusID, gplusAccessToken: '...' } [res, body] = yield request.postAsync({url, json}) expect(res.statusCode).toBe(422) - json = { email: gplusEmail, gplusID: '54321', gplusAccessToken: '...' } + json = { name, email: gplusEmail, gplusID: '54321', gplusAccessToken: '...' } [res, body] = yield request.postAsync({url, json}) expect(res.statusCode).toBe(422) done() it 'returns 409 if there is already a user with the given email', utils.wrap (done) -> - yield utils.initUser({email: gplusEmail}) + yield utils.initUser({name: 'someusername', email: gplusEmail}) spyOn(gplus, 'fetchMe').and.returnValue(validGPlusResponse) user = yield utils.becomeAnonymous() url = getURL("/db/user/#{user.id}/signup-with-gplus") - json = { email: gplusEmail, gplusID, gplusAccessToken: '...' } + json = { name: 'differentusername', email: gplusEmail, gplusID, gplusAccessToken: '...' } [res, body] = yield request.postAsync({url, json}) expect(res.statusCode).toBe(409) done()