This repository has been archived by the owner on Mar 19, 2019. It is now read-only.
/
functional_test.js
308 lines (252 loc) · 8.29 KB
/
functional_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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var expect = require("chai").expect;
var request = require("supertest");
var sinon = require("sinon");
var app = require("../loop").app;
var urlsStore = require("../loop").urlsStore;
var tokenlib = require("../loop/tokenlib");
var conf = require("../loop/config.js");
var auth = require("../loop/authentication");
var ONE_MINUTE = 60 * 60 * 1000;
var fakeNow = 1393595554796;
function getMiddlewares(method, url) {
return app.routes[method].filter(function(e){
return e.path === url;
}).shift().callbacks;
}
describe("HTTP API exposed by the server", function() {
var sandbox, expectedAssertion, pushURL, user;
beforeEach(function() {
sandbox = sinon.sandbox.create();
expectedAssertion = "BID-ASSERTION";
user = "alexis@notmyidea.org";
// Mock the calls to the external BrowserID verifier.
sandbox.stub(auth, "verify", function(assertion, audience, cb){
if (assertion === expectedAssertion)
cb(null, user, {});
else
cb("error");
});
// Let's do the tests with a real URL.
pushURL = 'https://push.services.mozilla.com/update/MGlYke2SrEmYE8ceyu' +
'STwxawxuEJnMeHtTCFDckvUo9Gwat44C5Z5vjlQEd1od1hj6o38UB6Ytc5x' +
'gXwSLAH2VS8qKyZ1eLNTQSX6_AEeH73ohUy2A==';
});
afterEach(function(done) {
sandbox.restore();
urlsStore.drop(function() {
done();
});
});
describe("authentication middleware", function() {
var jsonReq;
// Create a route with the auth middleware installed.
app.post('/with-middleware', auth.isAuthenticated, function(req, res) {
res.json(200, req.user);
});
beforeEach(function() {
jsonReq = request(app)
.post('/with-middleware');
});
it("should require user authentication", function(done) {
jsonReq
.expect(401)
.end(function(err, res) {
if (err) throw err;
expect(res.headers['www-authenticate']).to.eql('BrowserID');
done();
});
});
it("should reject invalid browserid assertions", function(done) {
// Mock the calls to the external BrowserID verifier.
jsonReq
.set('Authorization', 'BrowserID ' + "invalid-assertion")
.expect(401)
.end(done);
});
it("should accept valid browserid assertions", function(done) {
jsonReq
.set('Authorization', 'BrowserID ' + expectedAssertion)
.expect(200)
.end(function(err, res) {
if (err) throw err;
done();
});
});
it("should set an 'user' property on the request object", function(done) {
jsonReq
.set('Authorization', 'BrowserID ' + expectedAssertion)
.expect(200)
.end(function(err, res) {
if (err) throw err;
expect(res.body).eql("alexis@notmyidea.org");
done();
});
});
});
describe("POST /call-url", function() {
var jsonReq;
beforeEach(function() {
jsonReq = request(app)
.post('/call-url')
.send({})
.set('Authorization', 'BrowserID ' + expectedAssertion)
.type('json')
.expect('Content-Type', /json/);
});
it("should have the authentication middleware installed", function() {
expect(getMiddlewares('post', '/call-url')).include(auth.isAuthenticated);
});
it("should generate a valid call-url", function(done) {
var clock = sinon.useFakeTimers(fakeNow);
var tokenManager = new tokenlib.TokenManager({
macSecret: conf.get('macSecret'),
encryptionSecret: conf.get('encryptionSecret')
});
jsonReq
.expect(200)
.end(function(err, res) {
var callUrl = res.body && res.body.call_url, token;
expect(callUrl).to.not.equal(null);
expect(callUrl).to.match(/^http:\/\/127.0.0.1/);
// XXX: the content of the token should change in the
// future.
token = callUrl.split("/").pop();
expect(tokenManager.decode(token)).to.deep.equal({
expires: Math.round((fakeNow / ONE_MINUTE) + tokenManager.timeout)
});
clock.restore();
done(err);
});
});
});
describe("POST /registration", function() {
var jsonReq;
beforeEach(function() {
jsonReq = request(app)
.post('/registration')
.set('Authorization', 'BrowserID ' + expectedAssertion)
.type('json')
.expect('Content-Type', /json/);
});
it("should have the authentication middleware installed", function() {
expect(getMiddlewares('post', '/registration'))
.include(auth.isAuthenticated);
});
it("should require simple push url", function(done) {
jsonReq
.send({}) // XXX sending nothing fails here, investigate
.expect(400)
.end(function(err, res) {
if (err) throw err;
expect(res.body.error).eql('simple_push_url is required');
done();
});
});
it("should validate the simple push url", function(done) {
jsonReq
.send({'simple_push_url': 'not-an-url'})
.expect(400)
.end(function(err, res) {
if (err) throw err;
expect(res.body.error).eql('simple_push_url should be a valid url');
done();
});
});
it("should reject non-JSON requests", function(done) {
request(app)
.post('/registration')
.set('Authorization', 'BrowserID ' + expectedAssertion)
.type('html')
.expect(406).end(function(err, res) {
if (err) throw err;
expect(res.body).eql(["application/json"]);
done();
});
});
it("should return a 200 if everything went fine", function(done) {
jsonReq
.send({'simple_push_url': pushURL})
.expect(200).end(done);
});
it("should store push url", function(done) {
jsonReq
.send({'simple_push_url': pushURL})
.expect(200).end(function(err, res) {
if (err) {
throw err;
}
urlsStore.findOne({user: user}, function(err, record) {
if (err) {
throw err;
}
expect(record.simplepushURL).eql(pushURL);
done();
});
});
});
it("should be able to store multiple push urls for one user",
function(done) {
function addPushURL(url, callback) {
request(app)
.post('/registration')
.set('Authorization', 'BrowserID ' + expectedAssertion)
.type('json')
.send({'simple_push_url': pushURL})
.expect('Content-Type', /json/)
.expect(200).end(callback);
}
addPushURL("http://url1", function(err, res) {
if (err) {
throw err;
}
addPushURL("http://url2", function(err, res) {
urlsStore.find({user: user}, function(err, records) {
if (err) {
throw err;
}
expect(records.length).eql(2);
done();
});
});
});
});
it("should answer a 503 if the database isn't available", function(done) {
sandbox.stub(urlsStore, "add", function(record, cb) {
cb("error");
});
jsonReq
.send({'simple_push_url': pushURL})
.expect(503).end(done);
});
});
describe("GET /calls/{call_token}", function() {
it.skip("should return a valid HTML page", function() {
});
it.skip("should validate the token", function() {
});
});
describe("GET /calls", function() {
it.skip("should list existing calls", function() {
});
it.skip("should require a user session", function() {
});
it.skip("should validate a user session", function() {
});
});
describe("POST /calls/{call_token}", function() {
it.skip("should trigger simple push", function() {
});
it.skip("should store incoming call info", function() {
});
it.skip("should return provider info", function() {
});
it.skip("should accept valid call token", function() {
});
it.skip("should reject invalid call token", function() {
});
});
});