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

« back to all changes in this revision

Viewing changes to docs/example-server/test/registry_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
 
 
3
var MongoClient = require('mongodb').MongoClient
 
4
 
 
5
function unexpected(msg) {
 
6
    assert.ok(false, "unexpected: "+msg)
 
7
}
 
8
 
 
9
var Registry = require('../lib/registry')
 
10
 
 
11
suite('Registry', function(){
 
12
    setup(function(done) {
 
13
        var self = this
 
14
        MongoClient.connect("mongodb://localhost:27017/pushAppTestDb", function(err, database) {
 
15
            if(err) throw err
 
16
            self.db = database
 
17
            // cleanup
 
18
            self.db.collection('registry').drop(function(err) {
 
19
                if(err && err.errmsg != 'ns not found') throw err
 
20
                done()
 
21
            })
 
22
        })
 
23
    })
 
24
 
 
25
    test('insert-and-find', function(done) {
 
26
        var reg = new Registry(this.db)
 
27
        reg.insertToken("N", "T", function() {
 
28
            reg.findToken("N", function(token) {
 
29
                assert.equal(token, "T")
 
30
                done()
 
31
            }, function() {
 
32
                unexpected("not-found")
 
33
            }, function(err) {
 
34
                unexpected(err)
 
35
            })
 
36
        }, function() {
 
37
            unexpected("dup")
 
38
        }, function(err) {
 
39
            unexpected(err)
 
40
        })
 
41
    })
 
42
 
 
43
    test('find-not-found', function(done) {
 
44
        var reg = new Registry(this.db)
 
45
        reg.findToken("N", function() {
 
46
            unexpected("found")
 
47
        }, function() {
 
48
            done()
 
49
        }, function(err) {
 
50
            unexpected(err)
 
51
        })
 
52
    })
 
53
 
 
54
    test('insert-identical-dup', function(done) {
 
55
        var reg = new Registry(this.db)
 
56
        reg.insertToken("N", "T", function() {
 
57
            reg.insertToken("N", "T", function() {
 
58
                done()
 
59
            }, function() {
 
60
                unexpected("dup")
 
61
            }, function(err) {
 
62
                unexpected(err)
 
63
            })
 
64
        }, function() {
 
65
            unexpected("dup")
 
66
        }, function(err) {
 
67
            unexpected(err)
 
68
        })
 
69
    })
 
70
 
 
71
    test('insert-dup', function(done) {
 
72
        var reg = new Registry(this.db)
 
73
        reg.insertToken("N", "T1", function() {
 
74
            reg.insertToken("N", "T2", function() {
 
75
                unexpected("success")
 
76
            }, function() {
 
77
                done()
 
78
            }, function(err) {
 
79
                unexpected(err)
 
80
            })
 
81
        }, function() {
 
82
            unexpected("dup")
 
83
        }, function(err) {
 
84
            unexpected(err)
 
85
        })
 
86
    })
 
87
 
 
88
    test('insert-temp-dup', function(done) {
 
89
        var reg = new Registry(this.db)
 
90
        var findToken = reg.findToken
 
91
          , insertToken = reg.insertToken
 
92
        var notFoundOnce = 0
 
93
        var insertInvocations = 0
 
94
        reg.findToken = function(nick, foundCb, notFoundCb, errCb) {
 
95
            if (notFoundOnce == 0) {
 
96
                notFoundOnce++
 
97
                notFoundCb()
 
98
                return
 
99
            }
 
100
            findToken.call(reg, nick, foundCb, notFoundCb, errCb)
 
101
        }
 
102
        reg.insertToken = function(nick, token, doneCb, dupCb, errCb) {
 
103
            insertInvocations++
 
104
            insertToken.call(reg, nick, token, doneCb, dupCb, errCb)
 
105
        }
 
106
        reg.insertToken("N", "T1", function() {
 
107
            reg.insertToken("N", "T2", function() {
 
108
                unexpected("success")
 
109
            }, function() {
 
110
                assert.equal(insertInvocations, 3)
 
111
                done()
 
112
            }, function(err) {
 
113
                unexpected(err)
 
114
            })
 
115
        }, function() {
 
116
            unexpected("dup")
 
117
        }, function(err) {
 
118
            unexpected(err)
 
119
        })
 
120
    })
 
121
 
 
122
    test('remove', function(done) {
 
123
        var reg = new Registry(this.db)
 
124
        reg.insertToken("N", "T", function() {
 
125
            reg.removeToken("N", "T", function() {
 
126
                reg.findToken("N", function(token) {
 
127
                    unexpected("found")
 
128
                }, function() {
 
129
                    done()
 
130
                }, function(err) {
 
131
                    unexpected(err)
 
132
                })
 
133
            }, function(err) {
 
134
                unexpected(err)
 
135
            })
 
136
        },  function() {
 
137
            unexpected("dup")
 
138
        }, function(err) {
 
139
            unexpected(err)
 
140
        })
 
141
    })
 
142
 
 
143
    test('remove-exact', function(done) {
 
144
        var reg = new Registry(this.db)
 
145
        reg.insertToken("N", "T1", function() {
 
146
            reg.removeToken("N", "T2", function() {
 
147
                reg.findToken("N", function(token) {
 
148
                    assert.equal(token, "T1")
 
149
                    done()
 
150
                }, function() {
 
151
                    unexpected("no-found")
 
152
                }, function(err) {
 
153
                    unexpected(err)
 
154
                })
 
155
            }, function(err) {
 
156
                unexpected(err)
 
157
            })
 
158
        },  function() {
 
159
            unexpected("dup")
 
160
        }, function(err) {
 
161
            unexpected(err)
 
162
        })
 
163
    })
 
164
 
 
165
    test('remove-nop', function(done) {
 
166
        var reg = new Registry(this.db)
 
167
        reg.removeToken("N1", "T1", function() {
 
168
            reg.findToken("N1", function(token) {
 
169
                unexpected("found")
 
170
            }, function() {
 
171
                done()
 
172
            }, function(err) {
 
173
                unexpected(err)
 
174
            })
 
175
        }, function(err) {
 
176
            unexpected(err)
 
177
        })
 
178
    })
 
179
})