Reworking some code to use the new CocoModel.patch method.

This commit is contained in:
Scott Erickson 2014-06-11 11:34:52 -07:00
parent eb433c6591
commit e6dc896bab
3 changed files with 84 additions and 16 deletions

View file

@ -13,9 +13,6 @@ userPropsToSave =
module.exports = FacebookHandler = class FacebookHandler extends CocoClass
constructor: ->
super()
subscriptions:
'facebook-logged-in':'onFacebookLogin'
'facebook-logged-out': 'onFacebookLogout'
@ -42,22 +39,18 @@ module.exports = FacebookHandler = class FacebookHandler extends CocoClass
return
oldEmail = me.get('email')
patch = {}
patch.firstName = r.first_name if r.first_name
patch.lastName = r.last_name if r.last_name
patch.gender = r.gender if r.gender
patch.email = r.email if r.email
patch.facebookID = r.id if r.id
me.set(patch)
patch._id = me.id
me.set('firstName', r.first_name) if r.first_name
me.set('lastName', r.last_name) if r.last_name
me.set('gender', r.gender) if r.gender
me.set('email', r.email) if r.email
me.set('facebookID', r.id) if r.id
Backbone.Mediator.publish('logging-in-with-facebook')
window.tracker?.trackEvent 'Facebook Login'
window.tracker?.identify()
me.save(patch, {
patch: true
me.patch({
error: backboneFailure,
url: "/db/user?facebookID=#{r.id}&facebookAccessToken=#{@authResponse.accessToken}"
url: "/db/user/#{me.id}?facebookID=#{r.id}&facebookAccessToken=#{@authResponse.accessToken}"
success: (model) ->
window.location.reload() if model.get('email') isnt oldEmail
})

View file

@ -10,7 +10,7 @@ init = ->
if me and not me.get('testGroupNumber')?
# Assign testGroupNumber to returning visitors; new ones in server/routes/auth
me.set 'testGroupNumber', Math.floor(Math.random() * 256)
me.save()
me.patch()
Backbone.listenTo(me, 'sync', Backbone.Mediator.publish('me:synced', {me:me}))

View file

@ -0,0 +1,75 @@
FacebookHandler = require 'lib/FacebookHandler'
mockAuthEvent =
response:
authResponse:
accessToken: "aksdhjflkqjrj245234b52k345q344le4j4k5l45j45s4dkljvdaskl"
userID: "4301938"
expiresIn: 5138
signedRequest: "akjsdhfjkhea.3423nkfkdsejnfkd"
status: "connected"
# Whatev, it's all public info anyway
mockMe =
id: "4301938"
email: "scott@codecombat.com"
first_name: "Scott"
gender: "male"
last_name: "Erickson"
link: "https://www.facebook.com/scott.erickson.779"
locale: "en_US"
name: "Scott Erickson"
timezone: -7
updated_time: "2014-05-21T04:58:06+0000"
username: "scott.erickson.779"
verified: true
work: [
{
employer:
id: "167559910060759"
name: "CodeCombat"
location:
id: "114952118516947"
name: "San Francisco, California"
start_date: "2013-02-28"
}
{
end_date: "2013-01-31"
employer:
id: "39198748555"
name: "Skritter"
location:
id: "106109576086811"
name: "Oberlin, Ohio"
start_date: "2008-06-01"
}
]
describe 'lib/FacebookHandler.coffee', ->
it 'on facebook-logged-in, gets data from FB and sends a patch to the server', ->
me.clear({silent:true})
me.markToRevert()
me.set({_id: '12345'})
spyOn FB, 'api'
new FacebookHandler()
Backbone.Mediator.publish 'facebook-logged-in', mockAuthEvent
expect(FB.api).toHaveBeenCalled()
apiArgs = FB.api.calls.argsFor(0)
expect(apiArgs[0]).toBe('/me')
apiArgs[1](mockMe) # sending the 'response'
request = jasmine.Ajax.requests.mostRecent()
expect(request).toBeDefined()
params = JSON.parse request.params
expect(params.firstName).toBe(mockMe.first_name)
expect(params.lastName).toBe(mockMe.last_name)
expect(params.gender).toBe(mockMe.gender)
expect(params.email).toBe(mockMe.email)
expect(params.facebookID).toBe(mockMe.id)
expect(request.method).toBe('PATCH')
expect(_.string.startsWith(request.url, '/db/user/12345')).toBeTruthy()