oauth2-test.js
6.26 KB
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
var vows = require('vows');
var assert = require('assert');
var util = require('util');
vows.describe('NaverStrategy').addBatch({
'strategy': {
topic: function() {
return new NaverStrategy({
clientID: 'CLIENTID',
clientSecret: 'CLIENTSECRET'
},
function() {});
},
'should be named google': function (strategy) {
assert.equal(strategy.name, 'naver');
},
},
'strategy authorization params': {
topic: function() {
return new NaverStrategy({
clientID: 'CLIENTID',
clientSecret: 'CLIENTSECRET'
},
function() {});
},
'should return empty object when parsing invalid options': function (strategy) {
var params = strategy.authorizationParams({ foo: 'bar' });
assert.lengthOf(Object.keys(params), 0);
},
'should return access_type': function (strategy) {
var params = strategy.authorizationParams({ accessType: 'offline' });
assert.equal(params.access_type, 'offline');
},
'should return approval_prompt': function (strategy) {
var params = strategy.authorizationParams({ approvalPrompt: 'force' });
assert.equal(params.approval_prompt, 'force');
},
'should return prompt': function (strategy) {
var params = strategy.authorizationParams({ prompt: 'consent' });
assert.equal(params.prompt, 'consent');
},
'should return login_hint': function (strategy) {
var params = strategy.authorizationParams({ loginHint: 'bob@gmail.com' });
assert.equal(params.login_hint, 'bob@gmail.com');
},
'should return user_id': function (strategy) {
var params = strategy.authorizationParams({ userID: 'bob@gmail.com' });
assert.equal(params.user_id, 'bob@gmail.com');
},
'should return hd from hostedDomain option': function (strategy) {
var params = strategy.authorizationParams({ hostedDomain: 'mycollege.edu' });
assert.equal(params.hd, 'mycollege.edu');
},
'should return hd from hd option': function (strategy) {
var params = strategy.authorizationParams({ hd: 'mycollege.edu' });
assert.equal(params.hd, 'mycollege.edu');
},
'should return display from display option': function (strategy) {
var params = strategy.authorizationParams({ display: 'touch' });
assert.equal(params.display, 'touch');
},
'should return access_type and approval_prompt': function (strategy) {
var params = strategy.authorizationParams({ accessType: 'offline', approvalPrompt: 'force' });
assert.equal(params.access_type, 'offline');
assert.equal(params.approval_prompt, 'force');
},
},
'strategy when loading user profile': {
topic: function() {
var strategy = new NaverStrategy({
clientID: 'CLIENTID',
clientSecret: 'CLIENTSECRET'
},
function() {});
// mock
strategy._oauth2.get = function(url, accessToken, callback) {
var body = '{ \
"kind": "plus#person",\
"occupation": "Software Engineer",\
"gender": "male",\
"emails": [\
{\
"value": "example@gmail.com",\
"type": "account"\
}\
],\
"urls": [\
{\
"value": "http://www.jaredhanson.net/",\
"type": "otherProfile",\
"label": "Jared Hanson"\
}\
],\
"objectType": "person",\
"id": "111111111111111111111",\
"displayName": "Jared Hanson",\
"name": {\
"familyName": "Hanson",\
"givenName": "Jared"\
},\
"url": "https://plus.google.com/+JaredHanson",\
"image": {\
"url": "https://lh5.googleusercontent.com/-AAAAA-AAAAA/AAAAAAAAAAA/AAAAAAAAAAA/AAAAAAAAAAA/photo.jpg?sz=50",\
"isDefault": false\
},\
"organizations": [\
{\
"name": "South Dakota School of Mines & Technology",\
"type": "school",\
"primary": false\
}\
],\
"placesLived": [\
{\
"value": "Berkeley, CA",\
"primary": true\
}\
],\
"isPlusUser": true,\
"language": "en",\
"circledByCount": 77,\
"verified": false\
}';
callback(null, body, undefined);
}
return strategy;
},
'when told to load user profile': {
topic: function(strategy) {
var self = this;
function done(err, profile) {
self.callback(err, profile);
}
process.nextTick(function () {
strategy.userProfile('access-token', done);
});
},
'should not error' : function(err, req) {
assert.isNull(err);
},
'should load profile' : function(err, profile) {
assert.equal(profile.provider, 'google');
assert.equal(profile.id, '111111111111111111111');
assert.equal(profile.displayName, 'Jared Hanson');
assert.equal(profile.name.familyName, 'Hanson');
assert.equal(profile.name.givenName, 'Jared');
assert.equal(profile.emails[0].value, 'example@gmail.com');
assert.equal(profile.emails[0].type, 'account');
assert.equal(profile.photos[0].value, 'https://lh5.googleusercontent.com/-AAAAA-AAAAA/AAAAAAAAAAA/AAAAAAAAAAA/AAAAAAAAAAA/photo.jpg?sz=50');
},
'should set raw property' : function(err, profile) {
assert.isString(profile._raw);
},
'should set json property' : function(err, profile) {
assert.isObject(profile._json);
},
},
},
'strategy when loading user profile and encountering an error': {
topic: function() {
var strategy = new GoogleStrategy({
clientID: 'ABC123',
clientSecret: 'secret'
},
function() {});
// mock
strategy._oauth2.get = function(url, accessToken, callback) {
callback(new Error('something-went-wrong'));
}
return strategy;
},
'when told to load user profile': {
topic: function(strategy) {
var self = this;
function done(err, profile) {
self.callback(err, profile);
}
process.nextTick(function () {
strategy.userProfile('access-token', done);
});
},
'should error' : function(err, req) {
assert.isNotNull(err);
},
'should wrap error in InternalOAuthError' : function(err, req) {
assert.equal(err.constructor.name, 'InternalOAuthError');
},
'should not load profile' : function(err, profile) {
assert.isUndefined(profile);
},
},
},
}).export(module);