aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_dataforms_spec.lua
blob: 4b85444f0758fdfb5f5a8da77aaa8995b9d635fe (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
local dataforms = require "util.dataforms";
local st = require "util.stanza";
local jid = require "util.jid";
local iter = require "util.iterators";

describe("util.dataforms", function ()
	local some_form, xform;
	setup(function ()
		some_form = dataforms.new({
			title = "form-title",
			instructions = "form-instructions",
			{
				type = "hidden",
				name = "FORM_TYPE",
				value = "xmpp:prosody.im/spec/util.dataforms#1",
			};
			{
				type = "boolean",
				label = "boolean-label",
				name = "boolean-field",
				value = true,
			},
			{
				type = "fixed",
				label = "fixed-label",
				name = "fixed-field",
				value = "fixed-value",
			},
			{
				type = "hidden",
				label = "hidden-label",
				name = "hidden-field",
				value = "hidden-value",
			},
			{
				type = "jid-multi",
				label = "jid-multi-label",
				name = "jid-multi-field",
				value = {
					"jid@multi/value#1",
					"jid@multi/value#2",
				},
			},
			{
				type = "jid-single",
				label = "jid-single-label",
				name = "jid-single-field",
				value = "jid@single/value",
			},
			{
				type = "list-multi",
				label = "list-multi-label",
				name = "list-multi-field",
				value = {
					"list-multi-option-value#1",
					"list-multi-option-value#3",
				},
				options = {
					{
						label = "list-multi-option-label#1",
						value = "list-multi-option-value#1",
						default = true,
					},
					{
						label = "list-multi-option-label#2",
						value = "list-multi-option-value#2",
						default = false,
					},
					{
						label = "list-multi-option-label#3",
						value = "list-multi-option-value#3",
						default = true,
					},
				}
			},
			{
				type = "list-single",
				label = "list-single-label",
				name = "list-single-field",
				value = "list-single-value",
				options = {
					"list-single-value",
					"list-single-value#2",
					"list-single-value#3",
				}
			},
			{
				type = "text-multi",
				label = "text-multi-label",
				name = "text-multi-field",
				value = "text\nmulti\nvalue",
			},
			{
				type = "text-private",
				label = "text-private-label",
				name = "text-private-field",
				value = "text-private-value",
			},
			{
				type = "text-single",
				label = "text-single-label",
				name = "text-single-field",
				value = "text-single-value",
			},
		});
		xform = some_form:form();
	end);

	it("works", function ()
		assert.truthy(xform);
		assert.truthy(st.is_stanza(xform));
		assert.equal("x", xform.name);
		assert.equal("jabber:x:data", xform.attr.xmlns);
		assert.equal("FORM_TYPE", xform:find("field@var"));
		assert.equal("xmpp:prosody.im/spec/util.dataforms#1", xform:find("field/value#"));
		local allowed_direct_children = {
			title = true,
			instructions = true,
			field = true,
		}
		for tag in xform:childtags() do
			assert.truthy(allowed_direct_children[tag.name], "unknown direct child");
		end
	end);

	it("produced boolean field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "boolean-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("boolean-field", f.attr.var);
		assert.equal("boolean", f.attr.type);
		assert.equal("boolean-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		local val = f:get_child_text("value");
		assert.truthy(val == "true" or val == "1");
	end);

	it("produced fixed field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "fixed-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("fixed-field", f.attr.var);
		assert.equal("fixed", f.attr.type);
		assert.equal("fixed-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		assert.equal("fixed-value", f:get_child_text("value"));
	end);

	it("produced hidden field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "hidden-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("hidden-field", f.attr.var);
		assert.equal("hidden", f.attr.type);
		assert.equal("hidden-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		assert.equal("hidden-value", f:get_child_text("value"));
	end);

	it("produced jid-multi field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "jid-multi-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("jid-multi-field", f.attr.var);
		assert.equal("jid-multi", f.attr.type);
		assert.equal("jid-multi-label", f.attr.label);
		assert.equal(2, iter.count(f:childtags("value")));

		local i = 0;
		for value in f:childtags("value") do
			i = i + 1;
			assert.equal(("jid@multi/value#%d"):format(i), value:get_text());
		end
	end);

	it("produced jid-single field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "jid-single-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("jid-single-field", f.attr.var);
		assert.equal("jid-single", f.attr.type);
		assert.equal("jid-single-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		assert.equal("jid@single/value", f:get_child_text("value"));
		assert.truthy(jid.prep(f:get_child_text("value")));
	end);

	it("produced list-multi field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "list-multi-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("list-multi-field", f.attr.var);
		assert.equal("list-multi", f.attr.type);
		assert.equal("list-multi-label", f.attr.label);
		assert.equal(2, iter.count(f:childtags("value")));
		assert.equal("list-multi-option-value#1", f:get_child_text("value"));
		assert.equal(3, iter.count(f:childtags("option")));
	end);

	it("produced list-single field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "list-single-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("list-single-field", f.attr.var);
		assert.equal("list-single", f.attr.type);
		assert.equal("list-single-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		assert.equal("list-single-value", f:get_child_text("value"));
		assert.equal(3, iter.count(f:childtags("option")));
	end);

	it("produced text-multi field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "text-multi-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("text-multi-field", f.attr.var);
		assert.equal("text-multi", f.attr.type);
		assert.equal("text-multi-label", f.attr.label);
		assert.equal(3, iter.count(f:childtags("value")));
	end);

	it("produced text-private field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "text-private-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("text-private-field", f.attr.var);
		assert.equal("text-private", f.attr.type);
		assert.equal("text-private-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		assert.equal("text-private-value", f:get_child_text("value"));
	end);

	it("produced text-single field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "text-single-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("text-single-field", f.attr.var);
		assert.equal("text-single", f.attr.type);
		assert.equal("text-single-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		assert.equal("text-single-value", f:get_child_text("value"));
	end);

end);