import unittest
from slixmpp import Message
from slixmpp.test import SlixTest
from collections import OrderedDict
import slixmpp.plugins.xep_0004 as xep_0004
from slixmpp.xmlstream import register_stanza_plugin
class TestDataForms(SlixTest):
def setUp(self):
register_stanza_plugin(Message, xep_0004.Form)
register_stanza_plugin(xep_0004.Form, xep_0004.FormField)
register_stanza_plugin(xep_0004.FormField, xep_0004.FieldOption)
def testMultipleInstructions(self):
"""Testing using multiple instructions elements in a data form."""
msg = self.Message()
msg['form']['instructions'] = "Instructions\nSecond batch"
self.check(msg, """
Instructions
Second batch
""")
def testAddField(self):
"""Testing adding fields to a data form."""
msg = self.Message()
form = msg['form']
form.addField(var='f1',
ftype='text-single',
label='Text',
desc='A text field',
required=True,
value='Some text!')
self.check(msg, """
A text field
Some text!
""")
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'}]}
form['fields'] = fields
self.check(msg, """
Enter message.
A long one even.
""")
def testSetValues(self):
"""Testing setting form values"""
msg = self.Message()
form = msg['form']
form.add_field(var='foo', ftype='text-single')
form.add_field(var='bar', ftype='list-multi')
form.setValues({'foo': 'Foo!',
'bar': ['a', 'b']})
self.check(msg, """
Foo!
a
b
""")
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'}]}
form['fields'] = fields
form['type'] = 'submit'
form['values'] = {'f1': 'username',
'f2': 'hunter2',
'f3': 'A long\nmultiline\nmessage',
'f4': 'cool'}
self.check(form, """
username
hunter2
A long
multiline
message
cool
""", 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'}]}
form['fields'] = fields
form['type'] = 'cancel'
self.check(form, """
""")
suite = unittest.TestLoader().loadTestsFromTestCase(TestDataForms)