-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
193 lines (167 loc) · 6.32 KB
/
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
"use strict"
var assert = require('assert');
var clone = require('clone');
var querystring = require('querystring');
var url = require('url');
var http = require('http');
var sanction = process.env.EXPRESS_COV
? require('./lib-cov/sanction')
: require('./lib/sanction');
var CLIENT_OPTS = {
clientId: 'client_id',
clientSecret: 'client_secret',
authEndpoint: 'http://localhost:4242/o/auth',
tokenEndpoint: 'http://localhost:4242/o/token',
resourceEndpoint: 'http://localhost:4242/api',
redirectUri: 'http://localhost/login'
};
var ACCESS_TOKEN = {
access_token: 'accessToken',
refresh_token: 'refreshToken',
expires_in: 4200
};
var USER_DATA = {
first_name: 'foo',
last_name: 'bar',
email: 'foo@bar.com'
};
var encoder = JSON.stringify;
var server = http.createServer(function(req, res) {
res.end(encoder(ACCESS_TOKEN));
}).listen(4242, 'localhost');
describe('client', function() {
describe('#ctor', function() {
it('should have JSON.parse assigned to parser', function() {
var c = new sanction.Client(CLIENT_OPTS);
assert.equal(c.clientId, CLIENT_OPTS.clientId);
assert.equal(c.clientSecret, CLIENT_OPTS.clientSecret);
assert.equal(JSON.stringify(c.authEndpoint),
JSON.stringify(url.parse(CLIENT_OPTS.authEndpoint)));
assert.equal(JSON.stringify(c.tokenEndpoint),
JSON.stringify(url.parse(CLIENT_OPTS.tokenEndpoint)));
assert.equal(JSON.stringify(c.resourceEndpoint),
JSON.stringify(url.parse(CLIENT_OPTS.resourceEndpoint)));
});
it('should have nulled out credentials', function() {
var c = new sanction.Client(CLIENT_OPTS);
assert.equal(c.credentials.accessToken, null);
assert.equal(c.credentials.refreshToken, null);
assert.equal(c.credentials.tokenExpires, null);
});
});
describe('#authUri', function() {
it('should have no optional params', function(done) {
var c = new sanction.Client(CLIENT_OPTS);
c.authUri({}, function(e, uri) {
assert.equal(e, null);
var o = url.parse(uri);
var q = querystring.decode(o.query);
assert.equal(q.client_id, CLIENT_OPTS.clientId);
assert.equal(q.redirect_uri, CLIENT_OPTS.redirectUri);
assert.equal(q.response_type, 'code');
var p = url.parse(CLIENT_OPTS.redirectUri);
assert.equal(o.protocol, p.protocol);
assert.equal(o.hostname, p.hostname);
assert.equal(q.state, undefined);
assert.equal(q.scope, undefined);
done();
});
});
it('should have a scope and state', function() {
var c = new sanction.Client(CLIENT_OPTS);
var opts = {
scope: 'scope',
state: 'state'
};
c.authUri(opts, function(e, uri) {
assert.equal(e, null);
var q = querystring.decode(url.parse(uri).query);
assert.equal(q.state, opts.state);
assert.equal(q.scope, opts.scope);
});
});
});
describe('#requestToken', function() {
var client = new sanction.Client(CLIENT_OPTS);
it('should get a valid token via JSON', function(done) {
var opts = {code: 'code'};
client.requestToken(opts, function(e, data) {
assert.notEqual(data, null);
done();
});
});
it('should get a valid token via querystring.parse', function(done) {
encoder = querystring.stringify
var opts = {
code: 'code',
parser: function(r) {
var o = querystring.parse(r);
o.expires_in = parseInt(o.expires_in);
return o;
}
};
client.requestToken(opts, function(e, data) {
assert.equal(JSON.stringify(data), JSON.stringify(ACCESS_TOKEN));
encoder = JSON.stringify;
done();
});
});
});
describe('#request', function() {
it('should get dummy user data', function(done) {
var client = new sanction.Client(CLIENT_OPTS);
client.credentials.accessToken = 'token';
var opts = {
path: '/me'
};
client.request(opts, function(e, data) {
// still just getting dummy data back from previous tests
// (access token data)
assert.equal(JSON.stringify(data), JSON.stringify(ACCESS_TOKEN));
done();
});
});
});
});
describe('transport', function() {
describe('#query()', function() {
it('should have access_token=bar in query', function() {
var opts = {
path: '/foo'
};
sanction.transport.query(opts, 'bar');
assert.equal(opts.path, '/foo?access_token=bar');
});
it('should have access_token=bar&foo=bar in query', function() {
var opts = {
path: '/foo?foo=bar'
};
sanction.transport.query(opts, 'bar');
assert.equal(opts.path, '/foo?foo=bar&access_token=bar');
});
it('should have access_token and hash #bar', function() {
var opts = {
path: '/foo#bar'
};
sanction.transport.query(opts, 'bar');
assert.equal(opts.path, '/foo?access_token=bar#bar');
});
});
describe('#headers()', function() {
it('should have access_token=bar in headers', function() {
var opts = {};
sanction.transport.headers(opts, 'bar');
assert.equal(opts.headers.Authorization, 'Bearer bar');
});
it('should have access_token=bar and foo=bar in headers', function() {
var opts = {
headers: {
foo: 'bar'
}
};
sanction.transport.headers(opts, 'bar');
assert.equal(opts.headers.Authorization, 'Bearer bar');
assert.equal(opts.headers.foo, 'bar');
});
});
});