2013-07-26 11:02:26 +00:00
|
|
|
import unittest
|
|
|
|
from sleekxmpp import Message
|
|
|
|
from sleekxmpp.test import SleekTest
|
2011-08-12 04:59:55 +00:00
|
|
|
from sleekxmpp.thirdparty import OrderedDict
|
|
|
|
|
2010-07-20 16:16:57 +00:00
|
|
|
import sleekxmpp.plugins.xep_0004 as xep_0004
|
2013-07-26 11:02:26 +00:00
|
|
|
from sleekxmpp.xmlstream import register_stanza_plugin
|
2010-07-14 15:59:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestDataForms(SleekTest):
|
|
|
|
|
|
|
|
def setUp(self):
|
2010-10-18 01:38:22 +00:00
|
|
|
register_stanza_plugin(Message, xep_0004.Form)
|
2015-09-14 20:31:17 +00:00
|
|
|
register_stanza_plugin(xep_0004.Form, xep_0004.FormField, iterable=True)
|
|
|
|
register_stanza_plugin(xep_0004.FormField, xep_0004.FieldOption, iterable=True)
|
2010-07-14 15:59:58 +00:00
|
|
|
|
|
|
|
def testMultipleInstructions(self):
|
|
|
|
"""Testing using multiple instructions elements in a data form."""
|
|
|
|
msg = self.Message()
|
|
|
|
msg['form']['instructions'] = "Instructions\nSecond batch"
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(msg, """
|
2010-07-14 15:59:58 +00:00
|
|
|
<message>
|
2010-07-20 16:16:57 +00:00
|
|
|
<x xmlns="jabber:x:data" type="form">
|
2010-07-14 15:59:58 +00:00
|
|
|
<instructions>Instructions</instructions>
|
|
|
|
<instructions>Second batch</instructions>
|
|
|
|
</x>
|
|
|
|
</message>
|
2010-07-20 16:16:57 +00:00
|
|
|
""")
|
2010-07-14 15:59:58 +00:00
|
|
|
|
|
|
|
def testAddField(self):
|
|
|
|
"""Testing adding fields to a data form."""
|
|
|
|
|
|
|
|
msg = self.Message()
|
|
|
|
form = msg['form']
|
2010-08-06 00:23:07 +00:00
|
|
|
form.addField(var='f1',
|
|
|
|
ftype='text-single',
|
2010-07-14 15:59:58 +00:00
|
|
|
label='Text',
|
2010-08-06 00:23:07 +00:00
|
|
|
desc='A text field',
|
|
|
|
required=True,
|
2010-07-14 15:59:58 +00:00
|
|
|
value='Some text!')
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(msg, """
|
2010-07-14 15:59:58 +00:00
|
|
|
<message>
|
2010-07-20 16:16:57 +00:00
|
|
|
<x xmlns="jabber:x:data" type="form">
|
2010-07-14 15:59:58 +00:00
|
|
|
<field var="f1" type="text-single" label="Text">
|
|
|
|
<desc>A text field</desc>
|
|
|
|
<required />
|
|
|
|
<value>Some text!</value>
|
|
|
|
</field>
|
|
|
|
</x>
|
|
|
|
</message>
|
2010-07-20 16:16:57 +00:00
|
|
|
""")
|
2010-07-14 15:59:58 +00:00
|
|
|
|
2011-08-12 04:59:55 +00:00
|
|
|
fields = OrderedDict()
|
|
|
|
fields['f1'] = {'type': 'text-single',
|
|
|
|
'label': 'Username',
|
|
|
|
'required': True}
|
|
|
|
fields['f2'] = {'type': 'text-private',
|
|
|
|
'label': 'Password',
|
|
|
|
'required': True}
|
|
|
|
fields['f3'] = {'type': 'text-multi',
|
|
|
|
'label': 'Message',
|
|
|
|
'value': 'Enter message.\nA long one even.'}
|
|
|
|
fields['f4'] = {'type': 'list-single',
|
|
|
|
'label': 'Message Type',
|
|
|
|
'options': [{'label': 'Cool!',
|
|
|
|
'value': 'cool'},
|
|
|
|
{'label': 'Urgh!',
|
|
|
|
'value': 'urgh'}]}
|
2015-09-14 20:31:17 +00:00
|
|
|
form.set_fields(fields)
|
2011-08-12 04:59:55 +00:00
|
|
|
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(msg, """
|
2010-07-14 15:59:58 +00:00
|
|
|
<message>
|
2010-07-20 16:16:57 +00:00
|
|
|
<x xmlns="jabber:x:data" type="form">
|
2010-07-14 15:59:58 +00:00
|
|
|
<field var="f1" type="text-single" label="Username">
|
|
|
|
<required />
|
|
|
|
</field>
|
|
|
|
<field var="f2" type="text-private" label="Password">
|
|
|
|
<required />
|
|
|
|
</field>
|
|
|
|
<field var="f3" type="text-multi" label="Message">
|
|
|
|
<value>Enter message.</value>
|
|
|
|
<value>A long one even.</value>
|
|
|
|
</field>
|
|
|
|
<field var="f4" type="list-single" label="Message Type">
|
|
|
|
<option label="Cool!">
|
|
|
|
<value>cool</value>
|
|
|
|
</option>
|
|
|
|
<option label="Urgh!">
|
|
|
|
<value>urgh</value>
|
|
|
|
</option>
|
|
|
|
</field>
|
|
|
|
</x>
|
|
|
|
</message>
|
2010-07-20 16:16:57 +00:00
|
|
|
""")
|
|
|
|
|
|
|
|
def testSetValues(self):
|
|
|
|
"""Testing setting form values"""
|
2010-08-06 00:23:07 +00:00
|
|
|
|
2010-07-20 16:16:57 +00:00
|
|
|
msg = self.Message()
|
|
|
|
form = msg['form']
|
2011-08-12 04:59:55 +00:00
|
|
|
form.add_field(var='foo', ftype='text-single')
|
|
|
|
form.add_field(var='bar', ftype='list-multi')
|
2010-08-06 00:23:07 +00:00
|
|
|
|
2010-07-20 16:16:57 +00:00
|
|
|
form.setValues({'foo': 'Foo!',
|
|
|
|
'bar': ['a', 'b']})
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(msg, """
|
2010-07-20 16:16:57 +00:00
|
|
|
<message>
|
|
|
|
<x xmlns="jabber:x:data" type="form">
|
|
|
|
<field var="foo" type="text-single">
|
|
|
|
<value>Foo!</value>
|
|
|
|
</field>
|
|
|
|
<field var="bar" type="list-multi">
|
|
|
|
<value>a</value>
|
|
|
|
<value>b</value>
|
|
|
|
</field>
|
|
|
|
</x>
|
|
|
|
</message>""")
|
2010-07-14 15:59:58 +00:00
|
|
|
|
2011-08-13 08:28:18 +00:00
|
|
|
def testSubmitType(self):
|
|
|
|
"""Test that setting type to 'submit' clears extra details"""
|
|
|
|
msg = self.Message()
|
|
|
|
form = msg['form']
|
|
|
|
|
|
|
|
fields = OrderedDict()
|
|
|
|
fields['f1'] = {'type': 'text-single',
|
|
|
|
'label': 'Username',
|
|
|
|
'required': True}
|
|
|
|
fields['f2'] = {'type': 'text-private',
|
|
|
|
'label': 'Password',
|
|
|
|
'required': True}
|
|
|
|
fields['f3'] = {'type': 'text-multi',
|
|
|
|
'label': 'Message',
|
|
|
|
'value': 'Enter message.\nA long one even.'}
|
|
|
|
fields['f4'] = {'type': 'list-single',
|
|
|
|
'label': 'Message Type',
|
|
|
|
'options': [{'label': 'Cool!',
|
|
|
|
'value': 'cool'},
|
|
|
|
{'label': 'Urgh!',
|
|
|
|
'value': 'urgh'}]}
|
2015-09-14 20:31:17 +00:00
|
|
|
form.set_fields(fields)
|
2011-08-13 08:28:18 +00:00
|
|
|
|
|
|
|
form['type'] = 'submit'
|
2015-09-14 20:31:17 +00:00
|
|
|
form.set_values({'f1': 'username',
|
2011-08-13 08:28:18 +00:00
|
|
|
'f2': 'hunter2',
|
|
|
|
'f3': 'A long\nmultiline\nmessage',
|
2015-09-14 20:31:17 +00:00
|
|
|
'f4': 'cool'})
|
2011-08-13 08:28:18 +00:00
|
|
|
|
|
|
|
self.check(form, """
|
|
|
|
<x xmlns="jabber:x:data" type="submit">
|
|
|
|
<field var="f1">
|
|
|
|
<value>username</value>
|
|
|
|
</field>
|
|
|
|
<field var="f2">
|
|
|
|
<value>hunter2</value>
|
|
|
|
</field>
|
|
|
|
<field var="f3">
|
|
|
|
<value>A long</value>
|
|
|
|
<value>multiline</value>
|
|
|
|
<value>message</value>
|
|
|
|
</field>
|
|
|
|
<field var="f4">
|
|
|
|
<value>cool</value>
|
|
|
|
</field>
|
|
|
|
</x>
|
|
|
|
""", use_values=False)
|
|
|
|
|
|
|
|
def testCancelType(self):
|
|
|
|
"""Test that setting type to 'cancel' clears all fields"""
|
|
|
|
msg = self.Message()
|
|
|
|
form = msg['form']
|
|
|
|
|
|
|
|
fields = OrderedDict()
|
|
|
|
fields['f1'] = {'type': 'text-single',
|
|
|
|
'label': 'Username',
|
|
|
|
'required': True}
|
|
|
|
fields['f2'] = {'type': 'text-private',
|
|
|
|
'label': 'Password',
|
|
|
|
'required': True}
|
|
|
|
fields['f3'] = {'type': 'text-multi',
|
|
|
|
'label': 'Message',
|
|
|
|
'value': 'Enter message.\nA long one even.'}
|
|
|
|
fields['f4'] = {'type': 'list-single',
|
|
|
|
'label': 'Message Type',
|
|
|
|
'options': [{'label': 'Cool!',
|
|
|
|
'value': 'cool'},
|
|
|
|
{'label': 'Urgh!',
|
|
|
|
'value': 'urgh'}]}
|
2015-09-14 20:31:17 +00:00
|
|
|
form.set_fields(fields)
|
2011-08-13 08:28:18 +00:00
|
|
|
|
|
|
|
form['type'] = 'cancel'
|
|
|
|
|
|
|
|
self.check(form, """
|
|
|
|
<x xmlns="jabber:x:data" type="cancel" />
|
|
|
|
""")
|
|
|
|
|
2015-09-15 16:05:53 +00:00
|
|
|
def testReported(self):
|
|
|
|
msg = self.Message()
|
|
|
|
form = msg['form']
|
|
|
|
form['type'] = 'result'
|
|
|
|
|
|
|
|
form.add_reported(var='f1', ftype='text-single', label='Username')
|
|
|
|
|
|
|
|
form.add_item({'f1': 'username@example.org'})
|
|
|
|
|
|
|
|
self.check(msg, """
|
|
|
|
<message>
|
|
|
|
<x xmlns="jabber:x:data" type="result">
|
|
|
|
<reported>
|
|
|
|
<field var="f1" type="text-single" label="Username" />
|
|
|
|
</reported>
|
|
|
|
<item>
|
|
|
|
<field var="f1">
|
|
|
|
<value>username@example.org</value>
|
|
|
|
</field>
|
|
|
|
</item>
|
|
|
|
</x>
|
|
|
|
</message>
|
|
|
|
""")
|
|
|
|
|
|
|
|
def testSetReported(self):
|
|
|
|
msg = self.Message()
|
|
|
|
form = msg['form']
|
|
|
|
form['type'] = 'result'
|
|
|
|
|
|
|
|
reported = {'f1': {
|
|
|
|
'var': 'f1',
|
|
|
|
'type': 'text-single',
|
|
|
|
'label': 'Username'
|
|
|
|
}}
|
|
|
|
|
|
|
|
form.set_reported(reported)
|
|
|
|
|
|
|
|
self.check(msg, """
|
|
|
|
<message>
|
|
|
|
<x xmlns="jabber:x:data" type="result">
|
|
|
|
<reported>
|
|
|
|
<field var="f1" type="text-single" label="Username" />
|
|
|
|
</reported>
|
|
|
|
</x>
|
|
|
|
</message>
|
|
|
|
""")
|
|
|
|
|
2011-08-13 08:28:18 +00:00
|
|
|
|
2010-07-14 15:59:58 +00:00
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestDataForms)
|