def test_containsNot(self): """ A form does not contains an unknown field. """ fields = [data_form.Field(var='botname', value='The Jabber Bot')] form = data_form.Form('submit', fields=fields) self.assertNotIn('features', form)
def test_getUnknownDefault(self): """ Getting the value of a unknown field returns specified default. """ fields = [data_form.Field(var='botname', value='The Jabber Bot')] form = data_form.Form('submit', fields=fields) self.assertTrue(form.get('public', True))
def test_contains(self): """ A form contains a known field. """ fields = [data_form.Field(var='botname', value='The Jabber Bot')] form = data_form.Form('submit', fields=fields) self.assertIn('botname', form)
def test_get(self): """ Getting the value of a known field succeeds. """ fields = [data_form.Field(var='botname', value='The Jabber Bot')] form = data_form.Form('submit', fields=fields) self.assertEqual('The Jabber Bot', form.get('botname'))
def test_getUnknownNone(self): """ Getting the value of a unknown field returns None. """ fields = [data_form.Field(var='botname', value='The Jabber Bot')] form = data_form.Form('submit', fields=fields) self.assertIdentical(None, form.get('features'))
def test_formType(self): """ A form has a type. """ form = data_form.Form('result') self.assertEqual('result', form.formType)
def test_toElementFields(self): """ Fields are rendered as child elements, in order. """ fields = [data_form.Field('fixed', value='Section 1'), data_form.Field('text-single', var='botname', label='The name of your bot'), data_form.Field('text-multi', var='description', label='Helpful description of your bot'), data_form.Field('boolean', var='public', label='Public bot?', required=True) ] form = data_form.Form('form', fields=fields) element = form.toElement() elements = list(element.elements()) self.assertEqual(4, len(elements)) for field in elements: self.assertEqual('field', field.name) self.assertEqual(NS_X_DATA, field.uri) # Check order self.assertEqual('fixed', elements[0]['type']) self.assertEqual('botname', elements[1]['var']) self.assertEqual('description', elements[2]['var']) self.assertEqual('public', elements[3]['var'])
def test_getitemMultipleValuesTypeSingle(self): """ Multiple values for a single-value field type returns the first value. """ fields = [data_form.Field('text-single', var='features', values=['news', 'search'])] form = data_form.Form('submit', fields=fields) self.assertEqual('news', form['features'])
def test_getitemMultipleValuesNoType(self): """ Multiple values for a field without type are returned in a list. """ fields = [data_form.Field(None, var='features', values=['news', 'search'])] form = data_form.Form('submit', fields=fields) self.assertEqual(['news', 'search'], form['features'])
def test_getitemOneValueTypeMulti(self): """ A single value for a multi-value field type is returned in a list. """ fields = [data_form.Field('list-multi', var='features', values=['news'])] form = data_form.Form('submit', fields=fields) self.assertEqual(['news'], form['features'])
def _formFromConfiguration(self, resource, values): options = resource.getConfigurationOptions() fields = self._makeFields(options, values) form = data_form.Form(formType="form", formNamespace=NS_PUBSUB_NODE_CONFIG, fields=fields) return form
def test_addField(self): """ A field should occur in fieldList. """ form = data_form.Form('result') field = data_form.Field('fixed', value='Section 1') form.addField(field) self.assertEqual([field], form.fieldList)
def test_addFieldNamed(self): """ A named field should occur in fields. """ form = data_form.Form('result') field = data_form.Field(var='test') form.addField(field) self.assertEqual({'test': field}, form.fields)
def every(self, user, iq, cmd): if cmd.firstChildElement(): form = data_form.Form.fromElement(cmd.firstChildElement()) return orig(self, user, iq, cmd, form) else: form = data_form.Form(formType="form", title=self.name) self.fillForm(user, iq, cmd, form) return self.genFormCmdResponse(iq, cmd, form)
def test_addFieldNotNamed(self): """ A non-named field should not occur in fields. """ form = data_form.Form('result') field = data_form.Field('fixed', value='Section 1') form.addField(field) self.assertEqual({}, form.fields)
def test_itervalues(self): """ Iterating over the values of a form yields all field values. """ fields = [data_form.Field(var='botname', value='The Jabber Bot'), data_form.Field('boolean', var='public', value=True)] form = data_form.Form('submit', fields=fields) self.assertEqual(set(['The Jabber Bot', True]), set(form.itervalues()))
def test_otherFormTypeCancel(self): """ Cancelled forms with another FORM_TYPE are ignored. """ element = domish.Element((None, 'test')) cancelledForm = data_form.Form('cancel', formNamespace='otherns') element.addChild(cancelledForm.toElement()) form = data_form.findForm(element, 'myns') self.assertIdentical(None, form)
def __init__(self, to=None, frm=None): GroupChat.__init__(self, to=to, frm=frm) # build data form form = data_form.Form('submit', formNamespace=NS_MUC_REQUEST) form.addField( data_form.Field(var='muc#role', value='participant', label='Requested role')) self.addChild(form.toElement())
def test_noFormTypeCancel(self): """ Cancelled forms don't have a FORM_TYPE field, the first is returned. """ element = domish.Element((None, 'test')) cancelledForm = data_form.Form('cancel') element.addChild(cancelledForm.toElement()) form = data_form.findForm(element, 'myns') self.assertEqual('cancel', form.formType)
def test_removeFieldNamed(self): """ A removed named field should not occur in fields. """ form = data_form.Form('result') field = data_form.Field(var='test', value='test1') form.addField(field) form.removeField(field) self.assertNotIn('test', form.fields)
def toElement(self): element = generic.Request.toElement(self) query = element.addElement((self.requestNamespace, 'query')) if self.options is None: # This is a request for the configuration form. form = None elif self.options is False: form = data_form.Form(formType='cancel') else: form = data_form.Form(formType='submit', formNamespace=self.formNamespace) form.makeFields(self.options) if form is not None: query.addChild(form.toElement()) return element
def test_removeField(self): """ A removed field should not occur in fieldList. """ form = data_form.Form('result') field = data_form.Field('fixed', value='Section 1') form.addField(field) form.removeField(field) self.assertNotIn(field, form.fieldList)
def test_addFieldSameName(self): """ A named field cannot occur twice. """ form = data_form.Form('result') field1 = data_form.Field(var='test', value='value') field2 = data_form.Field(var='test', value='value2') form.addField(field1) self.assertRaises(data_form.Error, form.addField, field2)
def test_otherFormType(self): """ Forms with other FORM_TYPEs are ignored. """ element = domish.Element((None, 'test')) otherForm = data_form.Form('submit', formNamespace='otherns') element.addChild(otherForm.toElement()) form = data_form.findForm(element, 'myns') self.assertIdentical(None, form)
def test_addFieldTwiceNamed(self): """ A second named field should occur in fields. """ form = data_form.Form('result') field1 = data_form.Field(var='test') field2 = data_form.Field(var='test2') form.addField(field2) form.addField(field1) self.assertEqual({'test': field1, 'test2': field2}, form.fields)
def test_values(self): """ Getting the values of a form yields a list of field values. """ fields = [data_form.Field(var='botname', value='The Jabber Bot'), data_form.Field('boolean', var='public', value=True)] form = data_form.Form('submit', fields=fields) values = form.values() self.assertIsInstance(values, list) self.assertEqual(set(['The Jabber Bot', True]), set(values))
def test_addFieldTwice(self): """ Fields occur in fieldList in the order they were added. """ form = data_form.Form('result') field1 = data_form.Field('fixed', value='Section 1') field2 = data_form.Field('fixed', value='Section 2') form.addField(field1) form.addField(field2) self.assertEqual([field1, field2], form.fieldList)
def toElement(self): element = xmppim.Message.toElement(self) # build data form form = data_form.Form('submit', formNamespace=NS_MUC_REQUEST) form.addField(data_form.Field(var='muc#role', value='participant', label='Requested role')) element.addChild(form.toElement()) return element
def test_makeFieldsUnknownTypeListMulti(self): """ Without type, multiple values sets field type list-multi. """ values = {'pubsub#show-values': ['chat', 'online', 'away']} form = data_form.Form('result') form.makeFields(values) field = form.fields['pubsub#show-values'] self.assertEqual(None, field.fieldType) self.assertEqual(values, form.getValues())
def test_makeFieldsUnknownTypeBoolean(self): """ Without type, a boolean value sets field type boolean. """ values = {'pubsub#persist_items': True} form = data_form.Form('result') form.makeFields(values) field = form.fields['pubsub#persist_items'] self.assertEqual(None, field.fieldType) self.assertEqual(values, form.getValues())