source: wokkel/test/test_data_form.py @ 77:3ef9bc7a0d70

Last change on this file since 77:3ef9bc7a0d70 was 56:01740c205934, checked in by Ralph Meijer <ralphm@…>, 13 years ago

Don't do type interpretation on parsing Data Forms fields.

File size: 11.9 KB
Line 
1# Copyright (c) 2003-2009 Ralph Meijer
2# See LICENSE for details.
3
4"""
5Tests for {wokkel.data_form}.
6"""
7
8from twisted.trial import unittest
9from twisted.words.xish import domish
10from twisted.words.protocols.jabber import jid
11
12from wokkel import data_form
13
14NS_X_DATA = 'jabber:x:data'
15
16class OptionTest(unittest.TestCase):
17    """
18    Tests for L{data_form.Option}.
19    """
20
21    def test_toElement(self):
22        option = data_form.Option('value', 'label')
23        element = option.toElement()
24        self.assertEquals('option', element.name)
25        self.assertEquals(NS_X_DATA, element.uri)
26        self.assertEquals('label', element['label'])
27        self.assertEquals('value', element.value.name)
28        self.assertEquals(NS_X_DATA, element.value.uri)
29        self.assertEquals('value', unicode(element.value))
30
31
32
33class FieldTest(unittest.TestCase):
34    """
35    Tests for L{data_form.Field}.
36    """
37
38    def test_basic(self):
39        """
40        Test basic field initialization.
41        """
42        field = data_form.Field(var='test')
43        self.assertEqual('text-single', field.fieldType)
44        self.assertEqual('test', field.var)
45
46
47    def test_toElement(self):
48        """
49        Test rendering to a DOM.
50        """
51        field = data_form.Field(var='test')
52        element = field.toElement()
53
54        self.assertTrue(domish.IElement.providedBy(element))
55        self.assertEquals('field', element.name)
56        self.assertEquals(NS_X_DATA, element.uri)
57        self.assertEquals('text-single',
58                          element.getAttribute('type', 'text-single'))
59        self.assertEquals('test', element['var'])
60        self.assertEquals([], element.children)
61
62
63    def test_toElementTypeNotListSingle(self):
64        """
65        Always render the field type, if different from list-single.
66        """
67        field = data_form.Field('hidden', var='test')
68        element = field.toElement()
69
70        self.assertEquals('hidden', element.getAttribute('type'))
71
72
73    def test_toElementAsForm(self):
74        """
75        Always render the field type, if asForm is True.
76        """
77        field = data_form.Field(var='test')
78        element = field.toElement(True)
79
80        self.assertEquals('text-single', element.getAttribute('type'))
81
82
83    def test_toElementOptions(self):
84        """
85        Test rendering to a DOM with options.
86        """
87        field = data_form.Field('list-single', var='test')
88        field.options = [data_form.Option(u'option1'),
89                         data_form.Option(u'option2')]
90        element = field.toElement(True)
91
92        self.assertEqual(2, len(element.children))
93
94
95    def test_toElementLabel(self):
96        """
97        Test rendering to a DOM with a label.
98        """
99        field = data_form.Field(var='test', label=u'my label')
100        element = field.toElement(True)
101
102        self.assertEqual(u'my label', element.getAttribute('label'))
103
104
105    def test_toElementDescription(self):
106        """
107        Test rendering to a DOM with options.
108        """
109        field = data_form.Field(var='test', desc=u'My desc')
110        element = field.toElement(True)
111
112        self.assertEqual(1, len(element.children))
113        child = element.children[0]
114        self.assertEqual('desc', child.name)
115        self.assertEqual(NS_X_DATA, child.uri)
116        self.assertEqual(u'My desc', unicode(child))
117
118
119    def test_toElementRequired(self):
120        """
121        Test rendering to a DOM with options.
122        """
123        field = data_form.Field(var='test', required=True)
124        element = field.toElement(True)
125
126        self.assertEqual(1, len(element.children))
127        child = element.children[0]
128        self.assertEqual('required', child.name)
129        self.assertEqual(NS_X_DATA, child.uri)
130
131
132    def test_toElementJID(self):
133        field = data_form.Field(fieldType='jid-single', var='test',
134                                value=jid.JID(u'test@example.org'))
135        element = field.toElement()
136        self.assertEqual(u'test@example.org', unicode(element.value))
137
138
139    def test_typeCheckNoFieldName(self):
140        """
141        A field not of type fixed must have a var.
142        """
143        field = data_form.Field(fieldType='list-single')
144        self.assertRaises(data_form.FieldNameRequiredError, field.typeCheck)
145
146
147    def test_typeCheckTooManyValues(self):
148        """
149        Expect an exception if too many values are set, depending on type.
150        """
151        field = data_form.Field(fieldType='list-single', var='test',
152                                values=[u'value1', u'value2'])
153        self.assertRaises(data_form.TooManyValuesError, field.typeCheck)
154
155
156    def test_typeCheckBooleanFalse(self):
157        """
158        Test possible False values for a boolean field.
159        """
160        field = data_form.Field(fieldType='boolean', var='test')
161
162        for value in (False, 0, '0', 'false', 'False', []):
163            field.value = value
164            field.typeCheck()
165            self.assertIsInstance(field.value, bool)
166            self.assertFalse(field.value)
167
168
169    def test_typeCheckBooleanTrue(self):
170        """
171        Test possible True values for a boolean field.
172        """
173        field = data_form.Field(fieldType='boolean', var='test')
174
175        for value in (True, 1, '1', 'true', 'True', ['test']):
176            field.value = value
177            field.typeCheck()
178            self.assertIsInstance(field.value, bool)
179            self.assertTrue(field.value)
180
181
182    def test_typeCheckBooleanBad(self):
183        """
184        A bad value for a boolean field should raise a ValueError
185        """
186        field = data_form.Field(fieldType='boolean', var='test')
187        field.value = 'test'
188        self.assertRaises(ValueError, field.typeCheck)
189
190
191    def test_typeCheckJID(self):
192        """
193        The value of jid field should be a JID or coercable to one.
194        """
195        field = data_form.Field(fieldType='jid-single', var='test',
196                                value=jid.JID('test@example.org'))
197        field.typeCheck()
198
199
200    def test_typeCheckJIDString(self):
201        """
202        The string value of jid field should be coercable into a JID.
203        """
204        field = data_form.Field(fieldType='jid-single', var='test',
205                                value='test@example.org')
206        field.typeCheck()
207        self.assertEquals(jid.JID(u'test@example.org'), field.value)
208
209
210    def test_typeCheckJIDBad(self):
211        """
212        An invalid JID string should raise an exception.
213        """
214        field = data_form.Field(fieldType='jid-single', var='test',
215                                value='test@@example.org')
216        self.assertRaises(jid.InvalidFormat, field.typeCheck)
217
218
219    def test_fromElementType(self):
220        element = domish.Element((NS_X_DATA, 'field'))
221        element['type'] = 'fixed'
222        field = data_form.Field.fromElement(element)
223        self.assertEquals('fixed', field.fieldType)
224
225
226    def test_fromElementNoType(self):
227        element = domish.Element((NS_X_DATA, 'field'))
228        field = data_form.Field.fromElement(element)
229        self.assertEquals(None, field.fieldType)
230
231
232    def test_fromElementValueTextSingle(self):
233        """
234        Parsed text-single field values should be of type C{unicode}.
235        """
236        element = domish.Element((NS_X_DATA, 'field'))
237        element['type'] = 'text-single'
238        element.addElement('value', content=u'text')
239        field = data_form.Field.fromElement(element)
240        self.assertEquals('text', field.value)
241
242
243    def test_fromElementValueJID(self):
244        """
245        Parsed jid-single field values should be of type C{unicode}.
246        """
247        element = domish.Element((NS_X_DATA, 'field'))
248        element['type'] = 'jid-single'
249        element.addElement('value', content=u'user@example.org')
250        field = data_form.Field.fromElement(element)
251        self.assertEquals(u'user@example.org', field.value)
252
253    def test_fromElementValueJIDMalformed(self):
254        """
255        Parsed jid-single field values should be of type C{unicode}.
256
257        No validation should be done at this point, so invalid JIDs should
258        also be passed as-is.
259        """
260        element = domish.Element((NS_X_DATA, 'field'))
261        element['type'] = 'jid-single'
262        element.addElement('value', content=u'@@')
263        field = data_form.Field.fromElement(element)
264        self.assertEquals(u'@@', field.value)
265
266
267    def test_fromElementValueBoolean(self):
268        """
269        Parsed boolean field values should be of type C{unicode}.
270        """
271        element = domish.Element((NS_X_DATA, 'field'))
272        element['type'] = 'boolean'
273        element.addElement('value', content=u'false')
274        field = data_form.Field.fromElement(element)
275        self.assertEquals(u'false', field.value)
276
277
278
279class FormTest(unittest.TestCase):
280    """
281    Tests for L{data_form.Form}.
282    """
283
284    def test_formType(self):
285        """
286        A form has a type.
287        """
288
289        form = data_form.Form('result')
290        self.assertEqual('result', form.formType)
291
292    def test_toElement(self):
293        """
294        The toElement method returns a form's DOM representation.
295        """
296        form = data_form.Form('result')
297        element = form.toElement()
298
299        self.assertTrue(domish.IElement.providedBy(element))
300        self.assertEquals('x', element.name)
301        self.assertEquals(NS_X_DATA, element.uri)
302        self.assertEquals('result', element['type'])
303        self.assertEquals([], element.children)
304
305
306    def test_fromElement(self):
307        """
308        C{fromElement} creates a L{data_form.Form} from a DOM representation.
309        """
310        element = domish.Element((NS_X_DATA, 'x'))
311        element['type'] = 'result'
312        form = data_form.Form.fromElement(element)
313
314        self.assertEquals('result', form.formType)
315        self.assertEquals(None, form.title)
316        self.assertEquals([], form.instructions)
317        self.assertEquals({}, form.fields)
318
319
320    def test_fromElementInvalidElementName(self):
321        """
322        Bail if the passed element does not have the correct name.
323        """
324        element = domish.Element((NS_X_DATA, 'form'))
325        self.assertRaises(Exception, data_form.Form.fromElement, element)
326
327
328    def test_fromElementInvalidElementURI(self):
329        """
330        Bail if the passed element does not have the correct namespace.
331        """
332        element = domish.Element(('myns', 'x'))
333        self.assertRaises(Exception, data_form.Form.fromElement, element)
334
335
336    def test_fromElementTitle(self):
337        element = domish.Element((NS_X_DATA, 'x'))
338        element.addElement('title', content='My title')
339        form = data_form.Form.fromElement(element)
340
341        self.assertEquals('My title', form.title)
342
343
344    def test_fromElementInstructions(self):
345        element = domish.Element((NS_X_DATA, 'x'))
346        element.addElement('instructions', content='instruction')
347        form = data_form.Form.fromElement(element)
348
349        self.assertEquals(['instruction'], form.instructions)
350
351    def test_fromElementInstructions2(self):
352        element = domish.Element((NS_X_DATA, 'x'))
353        element.addElement('instructions', content='instruction 1')
354        element.addElement('instructions', content='instruction 2')
355        form = data_form.Form.fromElement(element)
356
357        self.assertEquals(['instruction 1', 'instruction 2'], form.instructions)
358
359
360    def test_fromElementOneField(self):
361        element = domish.Element((NS_X_DATA, 'x'))
362        element.addElement('field')
363        form = data_form.Form.fromElement(element)
364
365        self.assertEquals(1, len(form.fieldList))
366        self.assertNotIn('field', form.fields)
367
368
369    def test_fromElementTwoFields(self):
370        element = domish.Element((NS_X_DATA, 'x'))
371        element.addElement('field')['var'] = 'field1'
372        element.addElement('field')['var'] = 'field2'
373        form = data_form.Form.fromElement(element)
374
375        self.assertEquals(2, len(form.fieldList))
376        self.assertIn('field1', form.fields)
377        self.assertEquals('field1', form.fieldList[0].var)
378        self.assertIn('field2', form.fields)
379        self.assertEquals('field2', form.fieldList[1].var)
Note: See TracBrowser for help on using the repository browser.