This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
Client.spec.coffee
184 lines (148 loc) · 5.13 KB
/
Client.spec.coffee
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
joap = require "../lib/node-xmpp-joap"
ltx = require "ltx"
JOAP_NS = "jabber:iq:joap"
RPC_NS = "jabber:iq:rpc"
describe "Client", ->
compJID = "comp.exmaple.tld"
xmppComp =
channels: {}
send: (data) ->
onData: (data) ->
on: (channel, cb) ->
@channels[channel] = cb
jid: compJID
beforeEach ->
@c = new joap.Client xmppComp
it "is a class", ->
(expect typeof joap.Client).toEqual "function"
it "takes an xmpp object as first argument", ->
(expect (new joap.Client xmppComp).xmpp).toEqual xmppComp
it "provides methods to perform JOAP actions", ->
c = new joap.Client xmppComp
(expect typeof c.describe).toEqual "function"
(expect typeof c.read).toEqual "function"
(expect typeof c.add).toEqual "function"
(expect typeof c.edit).toEqual "function"
(expect typeof c.search).toEqual "function"
describe "description method", ->
it "sends a correct iq", ->
done = false
iq = null
xmppComp.send = (req) ->
iq = req
res = new ltx.Element "iq", type: 'result', id: iq.tree().attrs.id
res.c("describe")
.c("attributedescription")
.c("name").t("foo").up()
.c("type").t("int").up()
.c("desc", "xml:lang": 'en-US').t("foo").up()
.c("desc", "xml:lang": 'de-DE').t("bar").up().up()
.c("methoddescription")
.c("name").t("myMethod").up()
.c("returnType").t("boolean").up()
.c("desc", "xml:lang": 'en-US').t("great method").up()
xmppComp.channels.stanza res
@c.describe "class@c.domain.tld", (err, s, res) ->
(expect s.tree().name).toEqual 'iq'
(expect s.tree().attrs.id).toEqual iq.tree().attrs.id
(expect res).toEqual
desc: {}
attributes:
foo:
name: "foo"
type: "int"
desc:
'en-US': "foo"
'de-DE': "bar"
methods:
myMethod:
name: "myMethod"
returnType: 'boolean'
desc: 'en-US': "great method"
classes: {}
done = true
(expect iq.tree().name).toEqual 'iq'
(expect iq.tree().children[0].toString()).toEqual '<describe ' +
'xmlns="jabber:iq:joap"/>'
waitsFor -> done
describe "add method", ->
it "sends a correct iq", ->
done = false
iq = null
xmppComp.send = (req) ->
iq = req.tree()
res = new ltx.Element "iq", type: 'result', id: iq.tree().attrs.id
res.c("add", xmlns: JOAP_NS)
.c("newAddress").t("class@example.org/instance")
xmppComp.channels.stanza res
@c.add "class@c.domain.tld", {x:"y"}, (err, s, res) ->
(expect s.tree().name).toEqual 'iq'
(expect res).toEqual "class@example.org/instance"
done = true
waitsFor -> done
describe "read method", ->
it "sends a correct iq", ->
done = false
iq = null
xmppComp.send = (req) ->
iq = req
res = new ltx.Element "iq", type: 'result', id: iq.tree().attrs.id
res.c("read", xmlns: JOAP_NS)
.c("attribute")
.c("name").t("magic").up()
.c("value").c("i4").t(23)
xmppComp.channels.stanza res
@c.read "class@c.domain.tld/x", ["magic"], (err, s, res) ->
(expect s.tree().name).toEqual 'iq'
(expect res).toEqual {magic: 23}
done = true
waitsFor -> done
describe "read method", ->
it "sends a correct iq", ->
done = false
iq = null
xmppComp.send = (req) ->
iq = req
res = new ltx.Element "iq", type: 'result', id: iq.tree().attrs.id
res.c("edit", xmlns: JOAP_NS)
.c("newAddress").t("x@y.z/0")
xmppComp.channels.stanza res
@c.edit "class@c.domain.tld/x", { "magic":6 } , (err, s, res) ->
(expect s.tree().name).toEqual 'iq'
(expect res).toEqual "x@y.z/0"
done = true
waitsFor -> done
describe "search method", ->
it "sends a correct iq", ->
done = false
iq = null
xmppComp.send = (req) ->
iq = req
res = new ltx.Element "iq", type: 'result', id: iq.tree().attrs.id
res.c("search", xmlns: JOAP_NS)
.c("item").t("x@y.z/0")
xmppComp.channels.stanza res
@c.search "class@c.domain.tld", { "magic":6 } , (err, s, res) ->
(expect res).toEqual ["x@y.z/0"]
done = true
waitsFor -> done
describe "rpc method", ->
it "sends a correct iq", ->
done = false
iq = null
xmppComp.send = (req) ->
iq = req.tree()
(expect iq.getChild("query")
.getChild("methodCall")
.getChild("methodName").text()).toEqual "myMethod"
res = new ltx.Element "iq", type: 'result', id: iq.tree().attrs.id
res.c("query", xmlns: RPC_NS)
.c("methodResponse")
.c("params")
.c("param")
.c("value").c("int").t(7)
xmppComp.channels.stanza res.tree()
@c.methodCall "myMethod", "class@c.domain.tld", ["avalue"] , (err, s, res) ->
(expect res).toEqual 7
done = true
waitsFor -> done