summaryrefslogtreecommitdiff
path: root/tests/test_stanza_xep_0004.py
blob: 7b01b5750dc4b19ada721afb78501f2475138f0e (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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, iterable=True)
        register_stanza_plugin(xep_0004.FormField, xep_0004.FieldOption, iterable=True)

    def testMultipleInstructions(self):
        """Testing using multiple instructions elements in a data form."""
        msg = self.Message()
        msg['form']['instructions'] = "Instructions\nSecond batch"

        self.check(msg, """
          <message>
            <x xmlns="jabber:x:data" type="form">
              <instructions>Instructions</instructions>
              <instructions>Second batch</instructions>
            </x>
          </message>
        """)

    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, """
          <message>
            <x xmlns="jabber:x:data" type="form">
              <field var="f1" type="text-single" label="Text">
                <desc>A text field</desc>
                <required />
                <value>Some text!</value>
              </field>
            </x>
          </message>
        """)

        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.set_fields(fields)


        self.check(msg, """
          <message>
            <x xmlns="jabber:x:data" type="form">
              <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>
        """)

    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, """
          <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>""")

    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.set_fields(fields)

        form['type'] = 'submit'
        form.set_values({'f1': 'username',
                          'f2': 'hunter2',
                          'f3': 'A long\nmultiline\nmessage',
                          'f4': 'cool'})

        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'}]}
        form.set_fields(fields)

        form['type'] = 'cancel'

        self.check(form, """
          <x xmlns="jabber:x:data" type="cancel" />
        """)

    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>
        """)


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