Comparison

spec/util_dataforms_spec.lua @ 8862:900dff5ef498

util.dataforms: Add initial tests This covers basic form generation, that the fields have the correct attributes, children and text content.
author Kim Alvefur <zash@zash.se>
date Sat, 02 Jun 2018 19:47:09 +0200
child 8864:cf2f66b233d1
comparison
equal deleted inserted replaced
8861:2a48255f889b 8862:900dff5ef498
1 local dataforms = require "util.dataforms";
2 local st = require "util.stanza";
3 local jid = require "util.jid";
4 local iter = require "util.iterators";
5
6 describe("util.dataforms", function ()
7 local some_form, xform;
8 setup(function ()
9 some_form = dataforms.new({
10 title = "form-title",
11 instructions = "form-instructions",
12 {
13 type = "hidden",
14 name = "FORM_TYPE",
15 value = "xmpp:prosody.im/spec/util.dataforms#1",
16 };
17 {
18 type = "boolean",
19 label = "boolean-label",
20 name = "boolean-field",
21 value = true,
22 },
23 {
24 type = "fixed",
25 label = "fixed-label",
26 name = "fixed-field",
27 value = "fixed-value",
28 },
29 {
30 type = "hidden",
31 label = "hidden-label",
32 name = "hidden-field",
33 value = "hidden-value",
34 },
35 {
36 type = "jid-multi",
37 label = "jid-multi-label",
38 name = "jid-multi-field",
39 value = {
40 "jid@multi/value#1",
41 "jid@multi/value#2",
42 },
43 },
44 {
45 type = "jid-single",
46 label = "jid-single-label",
47 name = "jid-single-field",
48 value = "jid@single/value",
49 },
50 {
51 type = "list-multi",
52 label = "list-multi-label",
53 name = "list-multi-field",
54 value = {
55 "list-multi-option-value#1",
56 "list-multi-option-value#3",
57 },
58 options = {
59 {
60 label = "list-multi-option-label#1",
61 value = "list-multi-option-value#1",
62 default = true,
63 },
64 {
65 label = "list-multi-option-label#2",
66 value = "list-multi-option-value#2",
67 default = false,
68 },
69 {
70 label = "list-multi-option-label#3",
71 value = "list-multi-option-value#3",
72 default = true,
73 },
74 }
75 },
76 {
77 type = "list-single",
78 label = "list-single-label",
79 name = "list-single-field",
80 value = "list-single-value",
81 options = {
82 "list-single-value",
83 "list-single-value#2",
84 "list-single-value#3",
85 }
86 },
87 {
88 type = "text-multi",
89 label = "text-multi-label",
90 name = "text-multi-field",
91 value = "text\nmulti\nvalue",
92 },
93 {
94 type = "text-private",
95 label = "text-private-label",
96 name = "text-private-field",
97 value = "text-private-value",
98 },
99 {
100 type = "text-single",
101 label = "text-single-label",
102 name = "text-single-field",
103 value = "text-single-value",
104 },
105 });
106 xform = some_form:form();
107 end);
108
109 it("works", function ()
110 assert.truthy(xform);
111 assert.truthy(st.is_stanza(xform));
112 assert.equal("x", xform.name);
113 assert.equal("jabber:x:data", xform.attr.xmlns);
114 assert.equal("FORM_TYPE", xform:find("field@var"));
115 assert.equal("xmpp:prosody.im/spec/util.dataforms#1", xform:find("field/value#"));
116 local allowed_direct_children = {
117 title = true,
118 instructions = true,
119 field = true,
120 }
121 for tag in xform:childtags() do
122 assert.truthy(allowed_direct_children[tag.name], "unknown direct child");
123 end
124 end);
125
126 it("produced boolean field correctly", function ()
127 local f;
128 for field in xform:childtags("field") do
129 if field.attr.var == "boolean-field" then
130 f = field;
131 break;
132 end
133 end
134
135 assert.truthy(st.is_stanza(f));
136 assert.equal("boolean-field", f.attr.var);
137 assert.equal("boolean", f.attr.type);
138 assert.equal("boolean-label", f.attr.label);
139 assert.equal(1, iter.count(f:childtags("value")));
140 local val = f:get_child_text("value");
141 assert.truthy(val == "true" or val == "1");
142 end);
143
144 it("produced fixed field correctly", function ()
145 local f;
146 for field in xform:childtags("field") do
147 if field.attr.var == "fixed-field" then
148 f = field;
149 break;
150 end
151 end
152
153 assert.truthy(st.is_stanza(f));
154 assert.equal("fixed-field", f.attr.var);
155 assert.equal("fixed", f.attr.type);
156 assert.equal("fixed-label", f.attr.label);
157 assert.equal(1, iter.count(f:childtags("value")));
158 assert.equal("fixed-value", f:get_child_text("value"));
159 end);
160
161 it("produced hidden field correctly", function ()
162 local f;
163 for field in xform:childtags("field") do
164 if field.attr.var == "hidden-field" then
165 f = field;
166 break;
167 end
168 end
169
170 assert.truthy(st.is_stanza(f));
171 assert.equal("hidden-field", f.attr.var);
172 assert.equal("hidden", f.attr.type);
173 assert.equal("hidden-label", f.attr.label);
174 assert.equal(1, iter.count(f:childtags("value")));
175 assert.equal("hidden-value", f:get_child_text("value"));
176 end);
177
178 it("produced jid-multi field correctly", function ()
179 local f;
180 for field in xform:childtags("field") do
181 if field.attr.var == "jid-multi-field" then
182 f = field;
183 break;
184 end
185 end
186
187 assert.truthy(st.is_stanza(f));
188 assert.equal("jid-multi-field", f.attr.var);
189 assert.equal("jid-multi", f.attr.type);
190 assert.equal("jid-multi-label", f.attr.label);
191 assert.equal(2, iter.count(f:childtags("value")));
192
193 local i = 0;
194 for value in f:childtags("value") do
195 i = i + 1;
196 assert.equal(("jid@multi/value#%d"):format(i), value:get_text());
197 end
198 end);
199
200 it("produced jid-single field correctly", function ()
201 local f;
202 for field in xform:childtags("field") do
203 if field.attr.var == "jid-single-field" then
204 f = field;
205 break;
206 end
207 end
208
209 assert.truthy(st.is_stanza(f));
210 assert.equal("jid-single-field", f.attr.var);
211 assert.equal("jid-single", f.attr.type);
212 assert.equal("jid-single-label", f.attr.label);
213 assert.equal(1, iter.count(f:childtags("value")));
214 assert.equal("jid@single/value", f:get_child_text("value"));
215 assert.truthy(jid.prep(f:get_child_text("value")));
216 end);
217
218 it("produced list-multi field correctly", function ()
219 local f;
220 for field in xform:childtags("field") do
221 if field.attr.var == "list-multi-field" then
222 f = field;
223 break;
224 end
225 end
226
227 assert.truthy(st.is_stanza(f));
228 assert.equal("list-multi-field", f.attr.var);
229 assert.equal("list-multi", f.attr.type);
230 assert.equal("list-multi-label", f.attr.label);
231 assert.equal(2, iter.count(f:childtags("value")));
232 assert.equal("list-multi-option-value#1", f:get_child_text("value"));
233 assert.equal(3, iter.count(f:childtags("option")));
234 end);
235
236 it("produced list-single field correctly", function ()
237 local f;
238 for field in xform:childtags("field") do
239 if field.attr.var == "list-single-field" then
240 f = field;
241 break;
242 end
243 end
244
245 assert.truthy(st.is_stanza(f));
246 assert.equal("list-single-field", f.attr.var);
247 assert.equal("list-single", f.attr.type);
248 assert.equal("list-single-label", f.attr.label);
249 assert.equal(1, iter.count(f:childtags("value")));
250 assert.equal("list-single-value", f:get_child_text("value"));
251 assert.equal(3, iter.count(f:childtags("option")));
252 end);
253
254 it("produced text-multi field correctly", function ()
255 local f;
256 for field in xform:childtags("field") do
257 if field.attr.var == "text-multi-field" then
258 f = field;
259 break;
260 end
261 end
262
263 assert.truthy(st.is_stanza(f));
264 assert.equal("text-multi-field", f.attr.var);
265 assert.equal("text-multi", f.attr.type);
266 assert.equal("text-multi-label", f.attr.label);
267 assert.equal(3, iter.count(f:childtags("value")));
268 end);
269
270 it("produced text-private field correctly", function ()
271 local f;
272 for field in xform:childtags("field") do
273 if field.attr.var == "text-private-field" then
274 f = field;
275 break;
276 end
277 end
278
279 assert.truthy(st.is_stanza(f));
280 assert.equal("text-private-field", f.attr.var);
281 assert.equal("text-private", f.attr.type);
282 assert.equal("text-private-label", f.attr.label);
283 assert.equal(1, iter.count(f:childtags("value")));
284 assert.equal("text-private-value", f:get_child_text("value"));
285 end);
286
287 it("produced text-single field correctly", function ()
288 local f;
289 for field in xform:childtags("field") do
290 if field.attr.var == "text-single-field" then
291 f = field;
292 break;
293 end
294 end
295
296 assert.truthy(st.is_stanza(f));
297 assert.equal("text-single-field", f.attr.var);
298 assert.equal("text-single", f.attr.type);
299 assert.equal("text-single-label", f.attr.label);
300 assert.equal(1, iter.count(f:childtags("value")));
301 assert.equal("text-single-value", f:get_child_text("value"));
302 end);
303
304 end);
305