~ubuntu-branches/ubuntu/wily/ubuntu-push/wily-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Roberto Alsina, John R. Lenton
  • Date: 2014-09-08 18:05:11 UTC
  • mfrom: (1.1.18)
  • Revision ID: package-import@ubuntu.com-20140908180511-pwkbwac9q2s5sk18
Tags: 0.64+14.10.20140908-0ubuntu1
[ Roberto Alsina ]
* Remove tokens from debug output
* Doc updates
* Included example code in docs directory

[ John R. Lenton]
* Use libaccounts to track changes to the u1 account used for auth; restart the session on change.
* Set the MEDIA_PROP env var to select the right media role for notification sounds

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var assert = require('assert')
 
2
  ,  http = require('http')
 
3
 
 
4
 
 
5
var Notifier = require('../lib/notifier')
 
6
 
 
7
var cfg = {
 
8
    'app_id': 'app1',
 
9
    'expire_mins': 10,
 
10
    'retry_secs': 0.05,
 
11
    'retry_batch': 1,
 
12
    'happy_retry_secs': 0.02
 
13
}
 
14
  , cfg_batch2 = {
 
15
    'app_id': 'app1',
 
16
    'expire_mins': 10,
 
17
    'retry_secs': 0.05,
 
18
    'retry_batch': 2,
 
19
    'happy_retry_secs': 0.02
 
20
}
 
21
 
 
22
suite('Notifier', function() {
 
23
    setup(function(done) {
 
24
        var self = this
 
25
        self.s = http.createServer(function(req, resp) {
 
26
            self.s.emit(req.method, req, resp)
 
27
        })
 
28
        self.s.listen(0, 'localhost', function() {
 
29
            self.url = 'http://localhost:' + self.s.address().port
 
30
            done()
 
31
        })
 
32
    })
 
33
 
 
34
    teardown(function() {
 
35
        this.s.close()
 
36
    })
 
37
 
 
38
    test('happy-notify', function(done) {
 
39
        var b = ""
 
40
        this.s.on('POST', function(req, resp) {
 
41
            req.on('data', function(chunk) {
 
42
                b += chunk
 
43
            })
 
44
            req.on('end', function() {
 
45
                resp.writeHead(200, {"Content-Type": "application/json"})
 
46
                resp.end('{}')
 
47
            })
 
48
        })
 
49
        var n = new Notifier(this.url, cfg)
 
50
        var approxExpire = new Date
 
51
        approxExpire.setUTCMinutes(approxExpire.getUTCMinutes()+10)
 
52
        n.notify("N", "T", {m: 42}, function() {
 
53
            var reqObj = JSON.parse(b)
 
54
            var expireOn = Date.parse(reqObj.expire_on)
 
55
            delete reqObj.expire_on
 
56
            assert.ok(expireOn >= approxExpire)
 
57
            assert.deepEqual(reqObj, {
 
58
                "token": "T",
 
59
                "appid": "app1",
 
60
                "data": {"m": 42}
 
61
            })
 
62
            done()
 
63
        })
 
64
    })
 
65
 
 
66
    test('retry-notify', function(done) {
 
67
        var b = ""
 
68
        var fail = 1
 
69
        this.s.on('POST', function(req, resp) {
 
70
            if (fail) {
 
71
                fail--
 
72
                resp.writeHead(503, {"Content-Type": "application/json"})
 
73
                resp.end('')
 
74
                return
 
75
            }
 
76
            req.on('data', function(chunk) {
 
77
                b += chunk
 
78
            })
 
79
            req.on('end', function() {
 
80
                resp.writeHead(200, {"Content-Type": "application/json"})
 
81
                resp.end('{}')
 
82
            })
 
83
        })
 
84
        var n = new Notifier(this.url, cfg)
 
85
        var approxExpire = new Date
 
86
        approxExpire.setUTCMinutes(approxExpire.getUTCMinutes()+10)
 
87
        n.notify("N", "T", {m: 42}, function() {
 
88
            var reqObj = JSON.parse(b)
 
89
            var expireOn = Date.parse(reqObj.expire_on)
 
90
            delete reqObj.expire_on
 
91
            assert.ok(expireOn >= approxExpire)
 
92
            assert.deepEqual(reqObj, {
 
93
                "token": "T",
 
94
                "appid": "app1",
 
95
                "data": {"m": 42}
 
96
            })
 
97
            done()
 
98
        })
 
99
    })
 
100
 
 
101
    function flakyPOST(s, fail, tokens) {
 
102
        s.on('POST', function(req, resp) {
 
103
            var b = ""
 
104
            req.on('data', function(chunk) {
 
105
                b += chunk
 
106
            })
 
107
            req.on('end', function() {
 
108
                var reqObj = JSON.parse(b)
 
109
                if (fail) {
 
110
                    fail--
 
111
                    resp.writeHead(503, {"Content-Type": "application/json"})
 
112
                    resp.end('')
 
113
                    return
 
114
                }
 
115
                tokens[reqObj.token] = 1
 
116
                resp.writeHead(200, {"Content-Type": "application/json"})
 
117
                resp.end('{}')
 
118
            })
 
119
        })
 
120
    }
 
121
 
 
122
    test('retry-notify-2-retries', function(done) {
 
123
        var tokens = {}
 
124
        flakyPOST(this.s, 2, tokens)
 
125
        var n = new Notifier(this.url, cfg)
 
126
        function yay() {
 
127
            assert.deepEqual(tokens, {"T1": 1})
 
128
            done()
 
129
        }
 
130
        n.notify("N1", "T1", {m: 42}, yay)
 
131
    })
 
132
 
 
133
    test('retry-notify-2-batches', function(done) {
 
134
        var tokens = {}
 
135
        flakyPOST(this.s, 2, tokens)
 
136
        var n = new Notifier(this.url, cfg)
 
137
        var waiting = 2
 
138
        function yay() {
 
139
            waiting--
 
140
            if (waiting == 0) {
 
141
                assert.deepEqual(tokens, {"T1": 1, "T2": 1})
 
142
                done()
 
143
            }
 
144
        }
 
145
        n.notify("N1", "T1", {m: 42}, yay)
 
146
        n.notify("N2", "T2", {m: 42}, yay)
 
147
    })
 
148
 
 
149
    test('retry-notify-expired', function(done) {
 
150
        var tokens = {}
 
151
        flakyPOST(this.s, 2, tokens)
 
152
        var n = new Notifier(this.url, cfg_batch2)
 
153
        var waiting = 1
 
154
        function yay() {
 
155
            waiting--
 
156
            if (waiting == 0) {
 
157
                assert.deepEqual(tokens, {"T2": 1})
 
158
                done()
 
159
            }
 
160
        }
 
161
        n.notify("N1", "T1", {m: 42}, yay, new Date)
 
162
        n.notify("N2", "T2", {m: 42}, yay)
 
163
    })
 
164
 
 
165
    test('unknown-token-notify', function(done) {
 
166
        this.s.on('POST', function(req, resp) {
 
167
            resp.writeHead(400, {"Content-Type": "application/json"})
 
168
            resp.end('{"error": "unknown-token"}')
 
169
        })
 
170
        var n = new Notifier(this.url, cfg)
 
171
        n.on('unknownToken', function(nick, token) {
 
172
            assert.equal(nick, "N")
 
173
            assert.equal(token, "T")
 
174
            done()
 
175
        })
 
176
        n.notify("N", "T", {m: 42})
 
177
    })
 
178
 
 
179
    test('error-notify', function(done) {
 
180
        this.s.on('POST', function(req, resp) {
 
181
            resp.writeHead(500)
 
182
            resp.end('')
 
183
        })
 
184
        var n = new Notifier(this.url, cfg)
 
185
        n.on('pushError', function(err, resp, body) {
 
186
            assert.equal(resp.statusCode, 500)
 
187
            done()
 
188
        })
 
189
        n.notify("N", "T", {m: 42})
 
190
    })
 
191
 
 
192
})