|
| 1 | +/* global describe, it, before, expect */ |
| 2 | +/* jshint expr: true */ |
| 3 | + |
| 4 | +var GoogleStrategy = require('../lib/oauth2'); |
| 5 | + |
| 6 | + |
| 7 | +describe('Strategy#userProfile', function() { |
| 8 | + |
| 9 | + describe('fetched from Google+ API', function() { |
| 10 | + var strategy = new GoogleStrategy({ |
| 11 | + clientID: 'ABC123', |
| 12 | + clientSecret: 'secret', |
| 13 | + userProfileURL: 'https://www.googleapis.com/plus/v1/people/me' |
| 14 | + }, function() {}); |
| 15 | + |
| 16 | + strategy._oauth2.get = function(url, accessToken, callback) { |
| 17 | + if (url != 'https://www.googleapis.com/plus/v1/people/me') { return callback(new Error('incorrect url argument')); } |
| 18 | + if (accessToken != 'token') { return callback(new Error('incorrect token argument')); } |
| 19 | + |
| 20 | + var body = '{ \ |
| 21 | + "kind": "plus#person",\ |
| 22 | + "occupation": "Software Engineer",\ |
| 23 | + "gender": "male",\ |
| 24 | + "emails": [\ |
| 25 | + {\ |
| 26 | + "value": "example@gmail.com",\ |
| 27 | + "type": "account"\ |
| 28 | + }\ |
| 29 | + ],\ |
| 30 | + "urls": [\ |
| 31 | + {\ |
| 32 | + "value": "http://www.jaredhanson.net/",\ |
| 33 | + "type": "otherProfile",\ |
| 34 | + "label": "Jared Hanson"\ |
| 35 | + }\ |
| 36 | + ],\ |
| 37 | + "objectType": "person",\ |
| 38 | + "id": "111111111111111111111",\ |
| 39 | + "displayName": "Jared Hanson",\ |
| 40 | + "name": {\ |
| 41 | + "familyName": "Hanson",\ |
| 42 | + "givenName": "Jared"\ |
| 43 | + },\ |
| 44 | + "url": "https://plus.google.com/+JaredHanson",\ |
| 45 | + "image": {\ |
| 46 | + "url": "https://lh5.googleusercontent.com/-AAAAA-AAAAA/AAAAAAAAAAA/AAAAAAAAAAA/AAAAAAAAAAA/photo.jpg?sz=50",\ |
| 47 | + "isDefault": false\ |
| 48 | + },\ |
| 49 | + "organizations": [\ |
| 50 | + {\ |
| 51 | + "name": "South Dakota School of Mines & Technology",\ |
| 52 | + "type": "school",\ |
| 53 | + "primary": false\ |
| 54 | + }\ |
| 55 | + ],\ |
| 56 | + "placesLived": [\ |
| 57 | + {\ |
| 58 | + "value": "Berkeley, CA",\ |
| 59 | + "primary": true\ |
| 60 | + }\ |
| 61 | + ],\ |
| 62 | + "isPlusUser": true,\ |
| 63 | + "language": "en",\ |
| 64 | + "circledByCount": 77,\ |
| 65 | + "verified": false\ |
| 66 | +}'; |
| 67 | + callback(null, body, undefined); |
| 68 | + }; |
| 69 | + |
| 70 | + |
| 71 | + var profile; |
| 72 | + |
| 73 | + before(function(done) { |
| 74 | + strategy.userProfile('token', function(err, p) { |
| 75 | + if (err) { return done(err); } |
| 76 | + profile = p; |
| 77 | + done(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should parse profile', function() { |
| 82 | + expect(profile.provider).to.equal('google'); |
| 83 | + |
| 84 | + expect(profile.id).to.equal('111111111111111111111'); |
| 85 | + expect(profile.displayName).to.equal('Jared Hanson'); |
| 86 | + expect(profile.name.familyName).to.equal('Hanson'); |
| 87 | + expect(profile.name.givenName).to.equal('Jared'); |
| 88 | + expect(profile.emails[0].value).to.equal('example@gmail.com'); |
| 89 | + expect(profile.emails[0].type).to.equal('account'); |
| 90 | + expect(profile.photos[0].value).to.equal('https://lh5.googleusercontent.com/-AAAAA-AAAAA/AAAAAAAAAAA/AAAAAAAAAAA/AAAAAAAAAAA/photo.jpg?sz=50'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should set raw property', function() { |
| 94 | + expect(profile._raw).to.be.a('string'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should set json property', function() { |
| 98 | + expect(profile._json).to.be.an('object'); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('error caused by malformed response', function() { |
| 103 | + var strategy = new GoogleStrategy({ |
| 104 | + clientID: 'ABC123', |
| 105 | + clientSecret: 'secret' |
| 106 | + }, function() {}); |
| 107 | + |
| 108 | + strategy._oauth2.get = function(url, accessToken, callback) { |
| 109 | + var body = 'Hello, world.'; |
| 110 | + callback(null, body, undefined); |
| 111 | + }; |
| 112 | + |
| 113 | + |
| 114 | + var err, profile; |
| 115 | + |
| 116 | + before(function(done) { |
| 117 | + strategy.userProfile('token', function(e, p) { |
| 118 | + err = e; |
| 119 | + profile = p; |
| 120 | + done(); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should error', function() { |
| 125 | + expect(err).to.be.an.instanceOf(Error); |
| 126 | + expect(err.message).to.equal('Failed to parse user profile'); |
| 127 | + }); |
| 128 | + }); // error caused by malformed response |
| 129 | + |
| 130 | + describe('internal error', function() { |
| 131 | + var strategy = new GoogleStrategy({ |
| 132 | + clientID: 'ABC123', |
| 133 | + clientSecret: 'secret' |
| 134 | + }, function verify(){}); |
| 135 | + |
| 136 | + strategy._oauth2.get = function(url, accessToken, callback) { |
| 137 | + return callback(new Error('something went wrong')); |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + var err, profile; |
| 142 | + |
| 143 | + before(function(done) { |
| 144 | + strategy.userProfile('token', function(e, p) { |
| 145 | + err = e; |
| 146 | + profile = p; |
| 147 | + done(); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should error', function() { |
| 152 | + expect(err).to.be.an.instanceOf(Error); |
| 153 | + expect(err.constructor.name).to.equal('InternalOAuthError'); |
| 154 | + expect(err.message).to.equal('Failed to fetch user profile'); |
| 155 | + expect(err.oauthError).to.be.an.instanceOf(Error); |
| 156 | + expect(err.oauthError.message).to.equal('something went wrong'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should not load profile', function() { |
| 160 | + expect(profile).to.be.undefined; |
| 161 | + }); |
| 162 | + }); // internal error |
| 163 | + |
| 164 | +}); |
0 commit comments