/
strategy.profile.test.js
392 lines (306 loc) · 15.1 KB
/
strategy.profile.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
var TwitterStrategy = require('../lib/strategy')
, fs = require('fs');
describe('Strategy#userProfile', function() {
describe('fetched from default endpoint', function() {
var strategy = new TwitterStrategy({
consumerKey: 'ABC123',
consumerSecret: 'secret'
}, function verify(){});
strategy._oauth.get = function(url, token, tokenSecret, callback) {
if (url != 'https://api.twitter.com/1.1/account/verify_credentials.json') { return callback(new Error('incorrect url argument')); }
if (token != 'token') { return callback(new Error('incorrect token argument')); }
if (tokenSecret != 'token-secret') { return callback(new Error('incorrect tokenSecret argument')); }
var body = fs.readFileSync('test/fixtures/account/theSeanCook.json', 'utf8');
var response = {headers:{'x-access-level': 'read'}};
callback(null, body, response);
}
var profile;
before(function(done) {
strategy.userProfile('token', 'token-secret', { user_id: '6253282' }, function(err, p) {
if (err) { return done(err); }
profile = p;
done();
});
});
it('should parse profile', function() {
expect(profile.provider).to.equal('twitter');
expect(profile.id).to.equal('38895958');
expect(profile.username).to.equal('theSeanCook');
expect(profile.displayName).to.equal('Sean Cook');
});
it('should set raw property', function() {
expect(profile._raw).to.be.a('string');
});
it('should set json property', function() {
expect(profile._json).to.be.an('object');
});
it('should set accessLevel property', function() {
expect(profile._accessLevel).to.equal('read');
});
}); // fetched from default endpoint
describe('fetched from default endpoint, with email included', function() {
var strategy = new TwitterStrategy({
consumerKey: 'ABC123',
consumerSecret: 'secret',
includeEmail: true
}, function verify(){});
strategy._oauth.get = function(url, token, tokenSecret, callback) {
if (url != 'https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true') { return callback(new Error('incorrect url argument')); }
if (token != 'token') { return callback(new Error('incorrect token argument')); }
if (tokenSecret != 'token-secret') { return callback(new Error('incorrect tokenSecret argument')); }
var body = fs.readFileSync('test/fixtures/account/theSeanCook.json', 'utf8');
var response = {headers:{'x-access-level': 'read'}};
callback(null, body, response);
}
var profile;
before(function(done) {
strategy.userProfile('token', 'token-secret', { user_id: '6253282' }, function(err, p) {
if (err) { return done(err); }
profile = p;
done();
});
});
it('should parse profile', function() {
expect(profile.provider).to.equal('twitter');
expect(profile.id).to.equal('38895958');
expect(profile.username).to.equal('theSeanCook');
expect(profile.displayName).to.equal('Sean Cook');
});
it('should set raw property', function() {
expect(profile._raw).to.be.a('string');
});
it('should set json property', function() {
expect(profile._json).to.be.an('object');
});
it('should set accessLevel property', function() {
expect(profile._accessLevel).to.equal('read');
});
}); // fetched from default endpoint, with email included
describe('fetched from default endpoint, with status excluded', function() {
var strategy = new TwitterStrategy({
consumerKey: 'ABC123',
consumerSecret: 'secret',
includeStatus: false
}, function verify(){});
strategy._oauth.get = function(url, token, tokenSecret, callback) {
if (url != 'https://api.twitter.com/1.1/account/verify_credentials.json?skip_status=true') { return callback(new Error('incorrect url argument')); }
if (token != 'token') { return callback(new Error('incorrect token argument')); }
if (tokenSecret != 'token-secret') { return callback(new Error('incorrect tokenSecret argument')); }
var body = fs.readFileSync('test/fixtures/account/theSeanCook.json', 'utf8');
var response = {headers:{'x-access-level': 'read'}};
callback(null, body, response);
}
var profile;
before(function(done) {
strategy.userProfile('token', 'token-secret', { user_id: '6253282' }, function(err, p) {
if (err) { return done(err); }
profile = p;
done();
});
});
it('should parse profile', function() {
expect(profile.provider).to.equal('twitter');
expect(profile.id).to.equal('38895958');
expect(profile.username).to.equal('theSeanCook');
expect(profile.displayName).to.equal('Sean Cook');
});
it('should set raw property', function() {
expect(profile._raw).to.be.a('string');
});
it('should set json property', function() {
expect(profile._json).to.be.an('object');
});
it('should set accessLevel property', function() {
expect(profile._accessLevel).to.equal('read');
});
}); // fetched from default endpoint, with status excluded
describe('fetched from default endpoint, with entities excluded', function() {
var strategy = new TwitterStrategy({
consumerKey: 'ABC123',
consumerSecret: 'secret',
includeEntities: false
}, function verify(){});
strategy._oauth.get = function(url, token, tokenSecret, callback) {
if (url != 'https://api.twitter.com/1.1/account/verify_credentials.json?include_entities=false') { return callback(new Error('incorrect url argument')); }
if (token != 'token') { return callback(new Error('incorrect token argument')); }
if (tokenSecret != 'token-secret') { return callback(new Error('incorrect tokenSecret argument')); }
var body = fs.readFileSync('test/fixtures/account/theSeanCook.json', 'utf8');
var response = {headers:{'x-access-level': 'read'}};
callback(null, body, response);
}
var profile;
before(function(done) {
strategy.userProfile('token', 'token-secret', { user_id: '6253282' }, function(err, p) {
if (err) { return done(err); }
profile = p;
done();
});
});
it('should parse profile', function() {
expect(profile.provider).to.equal('twitter');
expect(profile.id).to.equal('38895958');
expect(profile.username).to.equal('theSeanCook');
expect(profile.displayName).to.equal('Sean Cook');
});
it('should set raw property', function() {
expect(profile._raw).to.be.a('string');
});
it('should set json property', function() {
expect(profile._json).to.be.an('object');
});
it('should set accessLevel property', function() {
expect(profile._accessLevel).to.equal('read');
});
}); // fetched from default endpoint, with entities excluded
describe('fetched from legacy users/show endpoint', function() {
var strategy = new TwitterStrategy({
consumerKey: 'ABC123',
consumerSecret: 'secret',
userProfileURL: 'https://api.twitter.com/1.1/users/show.json'
}, function verify(){});
strategy._oauth.get = function(url, token, tokenSecret, callback) {
if (url != 'https://api.twitter.com/1.1/users/show.json?user_id=6253282') { return callback(new Error('incorrect url argument')); }
if (token != 'token') { return callback(new Error('incorrect token argument')); }
if (tokenSecret != 'token-secret') { return callback(new Error('incorrect tokenSecret argument')); }
var body = '{"id_str":"6253282","id":6253282,"profile_text_color":"437792","created_at":"Wed May 23 06:01:13 +0000 2007","contributors_enabled":true,"follow_request_sent":null,"lang":"en","listed_count":10154,"profile_sidebar_border_color":"0094C2","show_all_inline_media":false,"friends_count":34,"utc_offset":-28800,"location":"San Francisco, CA","name":"Twitter API","profile_background_tile":false,"profile_sidebar_fill_color":"a9d9f1","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1438634086\/avatar_normal.png","protected":false,"geo_enabled":true,"following":null,"default_profile_image":false,"statuses_count":3252,"is_translator":false,"favourites_count":22,"profile_background_color":"e8f2f7","description":"The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Do not get an answer? It is on my website.","time_zone":"Pacific Time (US & Canada)","screen_name":"twitterapi","profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/229557229\/twitterapi-bg.png","profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1438634086\/avatar_normal.png","profile_link_color":"0094C2","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/229557229\/twitterapi-bg.png","followers_count":931299,"status":{"in_reply_to_status_id_str":null,"in_reply_to_user_id_str":null,"retweeted":false,"coordinates":null,"in_reply_to_screen_name":null,"created_at":"Tue Feb 14 23:39:43 +0000 2012","possibly_sensitive":false,"contributors":null,"in_reply_to_status_id":null,"entities":{"urls":[{"display_url":"tmblr.co\/ZgBqayGQi3ls","indices":[106,126],"expanded_url":"http:\/\/tmblr.co\/ZgBqayGQi3ls","url":"http:\/\/t.co\/cOzUfFNW"}],"user_mentions":[],"hashtags":[]},"geo":null,"in_reply_to_user_id":null,"place":null,"favorited":false,"truncated":false,"id_str":"169566520693882882","id":169566520693882882,"retweet_count":82,"text":"Photo Upload Issue - Some users may be experiencing an issue when uploading a photo. Our engineers are... http:\/\/t.co\/cOzUfFNW"},"default_profile":false,"notifications":null,"url":"http:\/\/dev.twitter.com","profile_use_background_image":true,"verified":true}';
var response = {headers:{'x-access-level': 'read'}};
callback(null, body, response);
}
var profile;
before(function(done) {
strategy.userProfile('token', 'token-secret', { user_id: '6253282' }, function(err, p) {
if (err) { return done(err); }
profile = p;
done();
});
});
it('should parse profile', function() {
expect(profile.provider).to.equal('twitter');
expect(profile.id).to.equal('6253282');
expect(profile.username).to.equal('twitterapi');
expect(profile.displayName).to.equal('Twitter API');
expect(profile.photos[0].value).to.equal('https://si0.twimg.com/profile_images/1438634086/avatar_normal.png');
});
it('should set raw property', function() {
expect(profile._raw).to.be.a('string');
});
it('should set json property', function() {
expect(profile._json).to.be.an('object');
});
it('should set accessLevel property', function() {
expect(profile._accessLevel).to.equal('read');
});
}); // fetched from legacy users/show endpoint
describe('skipping extended profile', function() {
var strategy = new TwitterStrategy({
consumerKey: 'ABC123',
consumerSecret: 'secret',
skipExtendedUserProfile: true
}, function verify(){});
strategy._oauth.get = function(url, token, tokenSecret, callback) {
return callback(new Error('should not fetch profile'));
}
var profile;
before(function(done) {
strategy.userProfile('token', 'token-secret', {"user_id":"1705","screen_name":"jaredhanson"}, function(err, p) {
if (err) { return done(err); }
profile = p;
done();
});
});
it('should parse profile', function() {
expect(profile.provider).to.equal('twitter');
expect(profile.id).to.equal('1705');
expect(profile.username).to.equal('jaredhanson');
});
it('should not set raw property', function() {
expect(profile._raw).to.be.undefined;
});
it('should not set json property', function() {
expect(profile._json).to.be.undefined;
});
}); // skipping extended profile
describe('error caused by invalid token', function() {
var strategy = new TwitterStrategy({
consumerKey: 'ABC123',
consumerSecret: 'secret',
userProfileURL: 'https://api.twitter.com/1.1/users/show.json'
}, function verify(){});
strategy._oauth.get = function(url, token, tokenSecret, callback) {
var body = '{"errors":[{"message":"Invalid or expired token","code":89}]}';
callback({ statusCode: 401, data: body });
}
var err, profile;
before(function(done) {
strategy.userProfile('x-token', 'token-secret', { user_id: '123' }, function(e, p) {
err = e;
profile = p;
done();
});
});
it('should error', function() {
expect(err).to.be.an.instanceOf(Error);
expect(err.constructor.name).to.equal('APIError');
expect(err.message).to.equal('Invalid or expired token');
expect(err.code).to.equal(89);
expect(err.status).to.equal(500);
});
it('should not supply profile', function() {
expect(profile).to.be.undefined;
});
}); // error caused by invalid token
describe('error caused by malformed response', function() {
var strategy = new TwitterStrategy({
consumerKey: 'ABC123',
consumerSecret: 'secret',
userProfileURL: 'https://api.twitter.com/1.1/users/show.json'
}, function verify(){});
strategy._oauth.get = function(url, token, tokenSecret, callback) {
var body = 'Hello, world.';
callback(null, body, undefined);
}
var err, profile;
before(function(done) {
strategy.userProfile('token', 'token-secret', { user_id: '123' }, function(e, p) {
err = e;
profile = p;
done();
});
});
it('should error', function() {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal('Failed to parse user profile');
});
it('should not supply profile', function() {
expect(profile).to.be.undefined;
});
}); // error caused by malformed response
describe('internal error', function() {
var strategy = new TwitterStrategy({
consumerKey: 'ABC123',
consumerSecret: 'secret',
userProfileURL: 'https://api.twitter.com/1.1/users/show.json'
}, function verify(){});
strategy._oauth.get = function(url, token, tokenSecret, callback) {
return callback(new Error('something went wrong'));
}
var err, profile;
before(function(done) {
strategy.userProfile('token', 'token-secret', { user_id: '123' }, function(e, p) {
err = e;
profile = p;
done();
});
});
it('should error', function() {
expect(err).to.be.an.instanceOf(Error);
expect(err.constructor.name).to.equal('InternalOAuthError');
expect(err.message).to.equal('Failed to fetch user profile');
expect(err.oauthError).to.be.an.instanceOf(Error);
expect(err.oauthError.message).to.equal('something went wrong');
});
it('should not supply profile', function() {
expect(profile).to.be.undefined;
});
}); // internal error
});