source: wokkel/test/test_data_form.py @ 55:a8cda961ee7e

Last change on this file since 55:a8cda961ee7e was 51:d6bbf24ef053, checked in by Ralph Meijer <ralphm@…>, 14 years ago

Address Data Form type checking issues.

Author: ralphm.
Reviewer: tofu.
Fixes #41.

File size: 10.5 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_fromElementValue(self):
233        element = domish.Element((NS_X_DATA, 'field'))
234        element.addElement("value", content="text")
235        field = data_form.Field.fromElement(element)
236        self.assertEquals('text', field.value)
237
238
239
240class FormTest(unittest.TestCase):
241    """
242    Tests for L{data_form.Form}.
243    """
244
245    def test_formType(self):
246        """
247        A form has a type.
248        """
249
250        form = data_form.Form('result')
251        self.assertEqual('result', form.formType)
252
253    def test_toElement(self):
254        """
255        The toElement method returns a form's DOM representation.
256        """
257        form = data_form.Form('result')
258        element = form.toElement()
259
260        self.assertTrue(domish.IElement.providedBy(element))
261        self.assertEquals('x', element.name)
262        self.assertEquals(NS_X_DATA, element.uri)
263        self.assertEquals('result', element['type'])
264        self.assertEquals([], element.children)
265
266
267    def test_fromElement(self):
268        """
269        C{fromElement} creates a L{data_form.Form} from a DOM representation.
270        """
271        element = domish.Element((NS_X_DATA, 'x'))
272        element['type'] = 'result'
273        form = data_form.Form.fromElement(element)
274
275        self.assertEquals('result', form.formType)
276        self.assertEquals(None, form.title)
277        self.assertEquals([], form.instructions)
278        self.assertEquals({}, form.fields)
279
280
281    def test_fromElementInvalidElementName(self):
282        """
283        Bail if the passed element does not have the correct name.
284        """
285        element = domish.Element((NS_X_DATA, 'form'))
286        self.assertRaises(Exception, data_form.Form.fromElement, element)
287
288
289    def test_fromElementInvalidElementURI(self):
290        """
291        Bail if the passed element does not have the correct namespace.
292        """
293        element = domish.Element(('myns', 'x'))
294        self.assertRaises(Exception, data_form.Form.fromElement, element)
295
296
297    def test_fromElementTitle(self):
298        element = domish.Element((NS_X_DATA, 'x'))
299        element.addElement('title', content='My title')
300        form = data_form.Form.fromElement(element)
301
302        self.assertEquals('My title', form.title)
303
304
305    def test_fromElementInstructions(self):
306        element = domish.Element((NS_X_DATA, 'x'))
307        element.addElement('instructions', content='instruction')
308        form = data_form.Form.fromElement(element)
309
310        self.assertEquals(['instruction'], form.instructions)
311
312    def test_fromElementInstructions2(self):
313        element = domish.Element((NS_X_DATA, 'x'))
314        element.addElement('instructions', content='instruction 1')
315        element.addElement('instructions', content='instruction 2')
316        form = data_form.Form.fromElement(element)
317
318        self.assertEquals(['instruction 1', 'instruction 2'], form.instructions)
319
320
321    def test_fromElementOneField(self):
322        element = domish.Element((NS_X_DATA, 'x'))
323        element.addElement('field')
324        form = data_form.Form.fromElement(element)
325
326        self.assertEquals(1, len(form.fieldList))
327        self.assertNotIn('field', form.fields)
328
329
330    def test_fromElementTwoFields(self):
331        element = domish.Element((NS_X_DATA, 'x'))
332        element.addElement('field')['var'] = 'field1'
333        element.addElement('field')['var'] = 'field2'
334        form = data_form.Form.fromElement(element)
335
336        self.assertEquals(2, len(form.fieldList))
337        self.assertIn('field1', form.fields)
338        self.assertEquals('field1', form.fieldList[0].var)
339        self.assertIn('field2', form.fields)
340        self.assertEquals('field2', form.fieldList[1].var)
Note: See TracBrowser for help on using the repository browser.