~nikwen/ubuntu-push/gmail-messages-missed-fix

« back to all changes in this revision

Viewing changes to docs/example-server/test/app_test.js

  • Committer: CI bot
  • Author(s): Roberto Alsina
  • Date: 2014-09-08 18:04:57 UTC
  • mfrom: (128.1.2 ubuntu-push)
  • Revision ID: ps-jenkins@lists.canonical.com-20140908180457-qga5piu3743an3lw
Latest changes from the automatic branch. 
Approved by: Roberto Alsina

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var assert = require('assert')
 
2
  ,  http = require('http')
 
3
  ,  request = require('supertest')
 
4
 
 
5
var app = require('../app')
 
6
 
 
7
var cfg = {
 
8
    'app_id': 'app1',
 
9
    'push_url': 'http://push',
 
10
    'expire_mins': 10,
 
11
    'retry_secs': 0.05,
 
12
    'retry_batch': 1,
 
13
    'happy_retry_secs': 0.02,
 
14
}
 
15
 
 
16
function cloneCfg() {
 
17
    return JSON.parse(JSON.stringify(cfg))
 
18
}
 
19
 
 
20
var PLAY_NOTIFY_FORM = '/play-notify-form'
 
21
 
 
22
suite('app', function() {
 
23
    setup(function() {
 
24
        this.db = {}
 
25
        this.app = app.wire(this.db, cloneCfg())
 
26
        this.reg = this.app.get('_reg')
 
27
        this.inbox = this.app.get('_inbox')
 
28
        this.notifier = this.app.get('_notifier')
 
29
    })
 
30
 
 
31
    test('wire', function() {
 
32
        assert.ok(this.reg)
 
33
        assert.ok(this.notifier)
 
34
        assert.equal(this.notifier.baseURL, 'http://push')
 
35
        var emitted
 
36
        this.app.on('pushError', function(err, resp, body) {
 
37
            emitted = [err, resp, body]
 
38
        })
 
39
        this.notifier.pushError('err', 'resp', 'body')
 
40
        assert.deepEqual(emitted, ["err", "resp", "body"])
 
41
    })
 
42
 
 
43
    test('wire-unknownToken', function() {
 
44
        var got
 
45
        this.reg.removeToken = function(nick, token, doneCb, errCb) {
 
46
            got = [nick, token]
 
47
            doneCb()
 
48
        }
 
49
        this.notifier.emit('unknownToken', "N", "T")
 
50
        assert.deepEqual(got, ["N", "T"])
 
51
    })
 
52
 
 
53
    test('wire-unknownToken-mongoError', function() {
 
54
        var emitted
 
55
        this.app.on('mongoError', function(err) {
 
56
            emitted = err
 
57
        })
 
58
        this.reg.removeToken = function(nick, token, doneCb, errCb) {
 
59
            errCb({})
 
60
        }
 
61
        this.notifier.emit('unknownToken', "N", "T")
 
62
        assert.ok(emitted)
 
63
    })
 
64
 
 
65
    test('_check', function(done) {
 
66
        var pingCmd
 
67
        this.db.command = function(cmd, cb) {
 
68
            pingCmd = cmd
 
69
            cb(null)
 
70
        }
 
71
        request(this.app)
 
72
            .get('/_check')
 
73
            .expect('Content-Type', 'application/json; charset=utf-8')
 
74
            .expect({ok: true})
 
75
            .expect(200, function(err) {
 
76
                assert.deepEqual(pingCmd, {ping: 1})
 
77
                done(err)
 
78
            })
 
79
    })
 
80
 
 
81
    test('_check-unavailable', function(done) {
 
82
        var pingCmd
 
83
        this.db.command = function(cmd, cb) {
 
84
            pingCmd = cmd
 
85
            cb({})
 
86
        }
 
87
        request(this.app)
 
88
            .get('/_check')
 
89
            .expect('Content-Type', 'application/json; charset=utf-8')
 
90
            .expect({error: "unavailable"})
 
91
            .expect(503, function(err) {
 
92
                done(err)
 
93
            })
 
94
    })
 
95
 
 
96
    test('any-broken', function(done) {
 
97
        request(this.app)
 
98
            .post('/register')
 
99
            .set('Content-Type', 'application/json')
 
100
            .send("")
 
101
            .expect('Content-Type', 'application/json; charset=utf-8')
 
102
            .expect({error: 'invalid', message: 'invalid json, empty body'})
 
103
            .expect(400, done)
 
104
    })
 
105
 
 
106
    test('register', function(done) {
 
107
        var got
 
108
        this.reg.insertToken = function(nick, token, doneCb, dupCb, errCb) {
 
109
            got = [nick, token]
 
110
            doneCb()
 
111
        }
 
112
        request(this.app)
 
113
            .post('/register')
 
114
            .set('Content-Type', 'application/json')
 
115
            .send({nick: "N", token: "T"})
 
116
            .expect('Content-Type', 'application/json; charset=utf-8')
 
117
            .expect({ok: true})
 
118
            .expect(200, function(err) {
 
119
                assert.deepEqual(got, ["N", "T"])
 
120
                done(err)
 
121
            })
 
122
    })
 
123
 
 
124
    test('register-invalid', function(done) {
 
125
        request(this.app)
 
126
            .post('/register')
 
127
            .set('Content-Type', 'application/json')
 
128
            .send({token: "T"})
 
129
            .expect('Content-Type', 'application/json; charset=utf-8')
 
130
            .expect({error: 'invalid'})
 
131
            .expect(400, done)
 
132
    })
 
133
 
 
134
    test('register-dup', function(done) {
 
135
        this.reg.insertToken = function(nick, token, doneCb, dupCb, errCb) {
 
136
            dupCb()
 
137
        }
 
138
        request(this.app)
 
139
            .post('/register')
 
140
            .set('Content-Type', 'application/json')
 
141
            .send({nick: "N", token: "T"})
 
142
            .expect('Content-Type', 'application/json; charset=utf-8')
 
143
            .expect({error: 'dup'})
 
144
            .expect(400, done)
 
145
    })
 
146
 
 
147
    test('register-unavailable', function(done) {
 
148
        this.reg.insertToken = function(nick, token, doneCb, dupCb, errCb) {
 
149
            errCb({})
 
150
        }
 
151
        request(this.app)
 
152
            .post('/register')
 
153
            .set('Content-Type', 'application/json')
 
154
            .send({nick: "N", token: "T"})
 
155
            .expect('Content-Type', 'application/json; charset=utf-8')
 
156
            .expect({error: 'unavailable'})
 
157
            .expect(503, done)
 
158
    })
 
159
 
 
160
    test('message', function(done) {
 
161
        var lookup = []
 
162
        var pushed
 
163
        var notify
 
164
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
165
            lookup.push(nick)
 
166
            if (nick == "N") {
 
167
                foundCb("T")
 
168
            } else {
 
169
                foundCb("T2")
 
170
            }
 
171
        }
 
172
        this.inbox.pushMessage = function(token, msg, doneCb, errCb) {
 
173
            pushed = [token]
 
174
            msg._serial = 10
 
175
            doneCb(msg)
 
176
        }
 
177
        this.notifier.notify = function(nick, token, data) {
 
178
            notify = [nick, token, data]
 
179
        }
 
180
        request(this.app)
 
181
            .post('/message')
 
182
            .set('Content-Type', 'application/json')
 
183
            .send({nick: "N2", data: {"m": 1}, from_nick: "N", from_token: "T"})
 
184
            .expect('Content-Type', 'application/json; charset=utf-8')
 
185
            .expect({ok: true})
 
186
            .expect(200, function(err) {
 
187
                assert.deepEqual(lookup, ["N", "N2"])
 
188
                assert.deepEqual(pushed, ["T2"])
 
189
                assert.deepEqual(notify, ["N2", "T2", {m: 1,_from: "N",_serial: 10}])
 
190
                done(err)
 
191
            })
 
192
    })
 
193
 
 
194
    test('message-unauthorized', function(done) {
 
195
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
196
            if (nick == "N") {
 
197
                foundCb("T")
 
198
            } else {
 
199
                notFoundCb()
 
200
            }
 
201
        }
 
202
        request(this.app)
 
203
            .post('/message')
 
204
            .set('Content-Type', 'application/json')
 
205
            .send({nick: "N2", data: {"m": 1}, from_nick: "N", from_token: "Z"})
 
206
            .expect('Content-Type', 'application/json; charset=utf-8')
 
207
            .expect({error: "unauthorized"})
 
208
            .expect(401, done)
 
209
    })
 
210
 
 
211
    test('message-unknown-nick', function(done) {
 
212
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
213
            if (nick == "N") {
 
214
                foundCb("T")
 
215
            } else {
 
216
                notFoundCb()
 
217
            }
 
218
        }
 
219
        request(this.app)
 
220
            .post('/message')
 
221
            .set('Content-Type', 'application/json')
 
222
            .send({nick: "N2", data: {"m": 1}, from_nick: "N", from_token: "T"})
 
223
            .expect('Content-Type', 'application/json; charset=utf-8')
 
224
            .expect({error: "unknown-nick"})
 
225
            .expect(400, done)
 
226
    })
 
227
 
 
228
    test('message-invalid', function(done) {
 
229
        request(this.app)
 
230
            .post('/message')
 
231
            .set('Content-Type', 'application/json')
 
232
            .send({nick: "N"}) // missing data
 
233
            .expect('Content-Type', 'application/json; charset=utf-8')
 
234
            .expect({error: 'invalid'})
 
235
            .expect(400, done)
 
236
    })
 
237
 
 
238
    test('message-check-token-unavailable', function(done) {
 
239
        var emitted
 
240
        this.app.on('mongoError', function(err) {
 
241
            emitted = err
 
242
        })
 
243
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
244
            if (nick == "N") {
 
245
                errCb({})
 
246
            } else {
 
247
                foundCb("T2")
 
248
            }
 
249
        }
 
250
        request(this.app)
 
251
            .post('/message')
 
252
            .set('Content-Type', 'application/json')
 
253
            .send({nick: "N2", data: {"m": 1}, from_nick: "N", from_token: "T"})
 
254
            .expect('Content-Type', 'application/json; charset=utf-8')
 
255
            .expect({error: 'unavailable'})
 
256
            .expect(503, function(err) {
 
257
                assert.ok(emitted)
 
258
                done(err)
 
259
            })
 
260
    })
 
261
 
 
262
    test('message-inbox-unavailable', function(done) {
 
263
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
264
            if (nick == "N") {
 
265
                foundCb("T")
 
266
            } else {
 
267
                foundCb("T2")
 
268
            }
 
269
        }
 
270
        this.inbox.pushMessage = function(token, msg, doneCb, errCb) {
 
271
            errCb({})
 
272
        }
 
273
        request(this.app)
 
274
            .post('/message')
 
275
            .set('Content-Type', 'application/json')
 
276
            .send({nick: "N2", data: {"m": 1}, from_nick: "N", from_token: "T"})
 
277
            .expect('Content-Type', 'application/json; charset=utf-8')
 
278
            .expect({error: 'unavailable'})
 
279
            .expect(503, done)
 
280
    })
 
281
 
 
282
    test('message-notify-unavailable', function(done) {
 
283
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
284
            if (nick == "N") {
 
285
                foundCb("T")
 
286
            } else {
 
287
                errCb({})
 
288
            }
 
289
        }
 
290
        request(this.app)
 
291
            .post('/message')
 
292
            .set('Content-Type', 'application/json')
 
293
            .send({nick: "N2", data: {"m": 1}, from_nick: "N", from_token: "T"})
 
294
            .expect('Content-Type', 'application/json; charset=utf-8')
 
295
            .expect({error: 'unavailable'})
 
296
            .expect(503, function(err) {
 
297
                done(err)
 
298
            })
 
299
    })
 
300
 
 
301
    test('index', function(done) {
 
302
        request(this.app)
 
303
            .get('/')
 
304
            .expect(new RegExp('<title>pushAppServer'))
 
305
            .expect('Content-Type', 'text/html; charset=UTF-8')
 
306
            .expect(200, done)
 
307
    })
 
308
 
 
309
    test('play-notify-form-absent', function(done) {
 
310
        request(this.app)
 
311
            .post(PLAY_NOTIFY_FORM)
 
312
            .type('form')
 
313
            .send({nick: "N", data: '{"m": 1}'})
 
314
            .expect(404, done)
 
315
    })
 
316
 
 
317
    test('drain', function(done) {
 
318
        var lookup
 
319
        var got
 
320
        var msgs = [{'m': 42, _timestamp: 4000, _serial: 20}]
 
321
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
322
            lookup = [nick]
 
323
            foundCb("T")
 
324
        }
 
325
        this.inbox.drain = function(token, timestamp, doneCb, errCb) {
 
326
            got = [token, timestamp]
 
327
            doneCb(msgs)
 
328
        }
 
329
        request(this.app)
 
330
            .post('/drain')
 
331
            .set('Content-Type', 'application/json')
 
332
             .send({nick: "N", token: "T", timestamp: 4000})
 
333
            .expect('Content-Type', 'application/json; charset=utf-8')
 
334
            .expect({ok: true, msgs: msgs})
 
335
            .expect(200, function(err) {
 
336
                assert.deepEqual(lookup, ["N"])
 
337
                assert.deepEqual(got, ["T", 4000])
 
338
                done(err)
 
339
            })
 
340
    })
 
341
 
 
342
    test('drain-unavailable', function(done) {
 
343
        var msgs = [{'m': 42, _timestamp: 4000, _serial: 20}]
 
344
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
345
            foundCb("T")
 
346
        }
 
347
        this.inbox.drain = function(token, timestamp, doneCb, errCb) {
 
348
            errCb()
 
349
        }
 
350
        request(this.app)
 
351
            .post('/drain')
 
352
            .set('Content-Type', 'application/json')
 
353
             .send({nick: "N", token: "T", timestamp: 4000})
 
354
            .expect('Content-Type', 'application/json; charset=utf-8')
 
355
            .expect({error: 'unavailable'})
 
356
            .expect(503, function(err) {
 
357
                done(err)
 
358
            })
 
359
    })
 
360
 
 
361
    test('drain-invalid', function(done) {
 
362
        request(this.app)
 
363
            .post('/drain')
 
364
            .set('Content-Type', 'application/json')
 
365
            .send({nick: "N"}) // missing data
 
366
            .expect('Content-Type', 'application/json; charset=utf-8')
 
367
            .expect({error: 'invalid'})
 
368
            .expect(400, done)
 
369
    })
 
370
 
 
371
    test('drain-invalid-timestamp', function(done) {
 
372
        request(this.app)
 
373
            .post('/drain')
 
374
            .set('Content-Type', 'application/json')
 
375
            .send({nick: "N", token: "T", timestamp: "foo"})
 
376
            .expect('Content-Type', 'application/json; charset=utf-8')
 
377
            .expect({error: 'invalid'})
 
378
            .expect(400, done)
 
379
    })
 
380
 
 
381
})
 
382
 
 
383
suite('app-with-play-notify', function() {
 
384
    setup(function() {
 
385
        this.db = {}
 
386
        var cfg = cloneCfg()
 
387
        cfg.play_notify_form = true
 
388
        this.app = app.wire(this.db, cfg)
 
389
        this.reg = this.app.get('_reg')
 
390
        this.notifier = this.app.get('_notifier')
 
391
    })
 
392
 
 
393
    test('root-form', function(done) {
 
394
        request(this.app)
 
395
            .get('/')
 
396
            .expect(new RegExp('<form.*action="' + PLAY_NOTIFY_FORM + '"(.|\n)*<input id="nick" name="nick"'))
 
397
            .expect('Content-Type', 'text/html; charset=UTF-8')
 
398
            .expect(200, done)
 
399
    })
 
400
 
 
401
    test('play-notify-form', function(done) {
 
402
        var notify
 
403
          , start = Date.now()
 
404
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
405
            foundCb("T")
 
406
        }
 
407
        this.notifier.notify = function(nick, token, data) {
 
408
            notify = [nick, token, data]
 
409
        }
 
410
        request(this.app)
 
411
            .post(PLAY_NOTIFY_FORM)
 
412
            .type('form')
 
413
            .send({nick: "N", data: '{"m": 1}'})
 
414
            .expect('Content-Type', 'text/plain; charset=utf-8')
 
415
            .expect("OK\n")
 
416
            .expect(200, function(err) {
 
417
                assert.deepEqual(notify.slice(0, 2), ["N", "T"])
 
418
                var data = notify[2]
 
419
                assert.equal(typeof(data._ephemeral), "number")
 
420
                assert.ok(data._ephemeral >= start)
 
421
                delete data._ephemeral
 
422
                assert.deepEqual(data, {"m": 1})
 
423
                done(err)
 
424
            })
 
425
    })
 
426
 
 
427
    test('play-notify-form-not-json', function(done) {
 
428
        var notify
 
429
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
430
            foundCb("T")
 
431
        }
 
432
        this.notifier.notify = function(nick, token, data) {
 
433
            notify = [nick, token, data]
 
434
        }
 
435
        request(this.app)
 
436
            .post(PLAY_NOTIFY_FORM)
 
437
            .type('form')
 
438
            .send({nick: "N", data: '{X'})
 
439
            .expect('Content-Type', 'text/plain; charset=utf-8')
 
440
            .expect("data is not JSON\n")
 
441
            .expect(400, done)
 
442
    })
 
443
 
 
444
    test('play-notify-form-unknown-nick', function(done) {
 
445
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
446
            notFoundCb()
 
447
        }
 
448
        request(this.app)
 
449
            .post(PLAY_NOTIFY_FORM)
 
450
            .set('Content-Type', 'application/json')
 
451
            .send({nick: "N", data: '{"m": 1}'})
 
452
            .expect('Content-Type', 'text/plain; charset=utf-8')
 
453
            .expect("unknown nick\n")
 
454
            .expect(400, done)
 
455
    })
 
456
 
 
457
    test('play-notify-form-unavailable', function(done) {
 
458
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
459
            errCb({})
 
460
        }
 
461
        request(this.app)
 
462
            .post(PLAY_NOTIFY_FORM)
 
463
            .type('form')
 
464
            .send({nick: "N", data: '{"m": 1}'})
 
465
            .expect('Content-Type', 'text/plain; charset=utf-8')
 
466
            .expect('db is hopefully only momentarily :(\n')
 
467
            .expect(503, function(err) {
 
468
                done(err)
 
469
            })
 
470
    })
 
471
 
 
472
    test('play-notify-form-invalid', function(done) {
 
473
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
474
            notFoundCb()
 
475
        }
 
476
        request(this.app)
 
477
            .post(PLAY_NOTIFY_FORM)
 
478
            .set('Content-Type', 'application/json')
 
479
            .send({nick: "", data: '{"m": 1}'})
 
480
            .expect('Content-Type', 'text/plain; charset=utf-8')
 
481
            .expect("invalid/empty fields\n")
 
482
            .expect(400, done)
 
483
    })
 
484
 
 
485
    test('play-notify-form-broken', function(done) {
 
486
        request(this.app)
 
487
            .post(PLAY_NOTIFY_FORM)
 
488
            .type('form')
 
489
            .send("=")
 
490
            .expect(400, done)
 
491
    })
 
492
 
 
493
})
 
494
 
 
495
suite('app-with-no-inbox', function() {
 
496
    setup(function() {
 
497
        this.db = {}
 
498
        var cfg = cloneCfg()
 
499
        cfg.no_inbox = true
 
500
        this.app = app.wire(this.db, cfg)
 
501
        this.reg = this.app.get('_reg')
 
502
        this.inbox = this.app.get('_inbox')
 
503
        this.notifier = this.app.get('_notifier')
 
504
    })
 
505
 
 
506
    test('no-inbox', function() {
 
507
        assert.equal(this.inbox, null)
 
508
    })
 
509
 
 
510
    test('message-no-inbox', function(done) {
 
511
        var lookup = []
 
512
        var notify
 
513
          , start = Date.now()
 
514
        this.reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
515
            lookup.push(nick)
 
516
            if (nick == "N") {
 
517
                foundCb("T")
 
518
            } else {
 
519
                foundCb("T2")
 
520
            }
 
521
        }
 
522
        this.notifier.notify = function(nick, token, data) {
 
523
            notify = [nick, token, data]
 
524
        }
 
525
        request(this.app)
 
526
            .post('/message')
 
527
            .set('Content-Type', 'application/json')
 
528
            .send({nick: "N2", data: {"m": 1}, from_nick: "N", from_token: "T"})
 
529
            .expect('Content-Type', 'application/json; charset=utf-8')
 
530
            .expect({ok: true})
 
531
            .expect(200, function(err) {
 
532
                assert.deepEqual(lookup, ["N", "N2"])
 
533
                assert.deepEqual(notify.slice(0, 2), ["N2", "T2"])
 
534
                var data = notify[2]
 
535
                assert.equal(typeof(data._ephemeral), "number")
 
536
                assert.ok(data._ephemeral >= start)
 
537
                delete data._ephemeral
 
538
                assert.deepEqual(data, {"m": 1, _from:"N"})
 
539
                done(err)
 
540
            })
 
541
    })
 
542
 
 
543
    test('drain-not-there', function(done) {
 
544
        request(this.app)
 
545
            .post('/drain')
 
546
            .set('Content-Type', 'application/json')
 
547
             .send({nick: "N", token: "T", timestamp: 4000})
 
548
            .expect(404, done)
 
549
    })
 
550
 
 
551
})