From e6dc896babea762bbfa9aa8123d3e659f2ba1432 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Wed, 11 Jun 2014 11:34:52 -0700 Subject: [PATCH] Reworking some code to use the new CocoModel.patch method. --- app/lib/FacebookHandler.coffee | 23 +++----- app/lib/auth.coffee | 2 +- test/app/lib/FacebookHandler.spec.coffee | 75 ++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 test/app/lib/FacebookHandler.spec.coffee diff --git a/app/lib/FacebookHandler.coffee b/app/lib/FacebookHandler.coffee index 10a59c40c..e9bf6aadc 100644 --- a/app/lib/FacebookHandler.coffee +++ b/app/lib/FacebookHandler.coffee @@ -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 }) diff --git a/app/lib/auth.coffee b/app/lib/auth.coffee index c9d6fcefb..310727b65 100644 --- a/app/lib/auth.coffee +++ b/app/lib/auth.coffee @@ -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})) diff --git a/test/app/lib/FacebookHandler.spec.coffee b/test/app/lib/FacebookHandler.spec.coffee new file mode 100644 index 000000000..76c088a77 --- /dev/null +++ b/test/app/lib/FacebookHandler.spec.coffee @@ -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()