summaryrefslogtreecommitdiff
path: root/tests/test_forms.py
blob: 16160249ee12a49f4245597538a767d3cca3c91e (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
from sleektest import *
import sleekxmpp.plugins.alt_0004 as xep_0004


class TestDataForms(SleekTest):

    def setUp(self):
        registerStanzaPlugin(Message, xep_0004.Form)
        registerStanzaPlugin(xep_0004.Form, xep_0004.FormField)
        registerStanzaPlugin(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.checkMessage(msg, """
          <message>
            <x xmlns="jabber:x:data">
              <instructions>Instructions</instructions>
              <instructions>Second batch</instructions>
            </x>
          </message>
        """, use_values=False)

    def testAddField(self):
        """Testing adding fields to a data form."""

        msg = self.Message()
        form = msg['form']
        form.addField('f1', 
                      ftype='text-single', 
                      label='Text',
                      desc='A text field', 
                      required=True, 
                      value='Some text!')

        self.checkMessage(msg, """
          <message>
            <x xmlns="jabber:x:data">
              <field var="f1" type="text-single" label="Text">
                <desc>A text field</desc>
                <required />
                <value>Some text!</value>
              </field>
            </x>
          </message>
        """, use_values=False)

        form['fields'] = {'f1': {'type': 'text-single', 
                                 'label': 'Username', 
                                 'required': True},
                          'f2': {'type': 'text-private',
                                 'label': 'Password',
                                 'required': True},
                          'f3': {'type': 'text-multi',
                                 'label': 'Message',
                                 'value': 'Enter message.\nA long one even.'},
                          'f4': {'type': 'list-single',
                                 'label': 'Message Type',
                                 'options': [{'label': 'Cool!', 
                                              'value': 'cool'},
                                             {'label': 'Urgh!',
                                              'value': 'urgh'}]}}
        self.checkMessage(msg, """
          <message>
            <x xmlns="jabber:x:data">
              <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>
        """, use_values=False)

suite = unittest.TestLoader().loadTestsFromTestCase(TestDataForms)