def test_toElement(self): """ The toElement method returns a form's DOM representation. """ form = Form('result') element = form.toElement() self.assertTrue(domish.IElement.providedBy(element)) self.assertEquals('x', element.name) self.assertEquals(NS_X_DATA, element.uri) self.assertEquals('result', element['type']) self.assertEquals([], element.children)
def test_formType(self): """ A form has a type. """ form = Form('result') self.assertEqual('result', form.formType)
def test_fromElementOneField(self): element = domish.Element((NS_X_DATA, 'x')) element.addElement('field') form = Form.fromElement(element) self.assertEquals(1, len(form.fieldList)) self.assertNotIn('field', form.fields)
def test_fromElementInstructions2(self): element = domish.Element((NS_X_DATA, 'x')) element.addElement('instructions', content='instruction 1') element.addElement('instructions', content='instruction 2') form = Form.fromElement(element) self.assertEquals(['instruction 1', 'instruction 2'], form.instructions)
def test_fromElementTwoFields(self): element = domish.Element((NS_X_DATA, 'x')) element.addElement('field')['var'] = 'field1' element.addElement('field')['var'] = 'field2' form = Form.fromElement(element) self.assertEquals(2, len(form.fieldList)) self.assertIn('field1', form.fields) self.assertEquals('field1', form.fieldList[0].var) self.assertIn('field2', form.fields) self.assertEquals('field2', form.fieldList[1].var)
def test_fromElement(self): """ The fromElement static method creates a L{Form} from a L{DOM. """ element = domish.Element((NS_X_DATA, 'x')) element['type'] = 'result' form = Form.fromElement(element) self.assertEquals('result', form.formType) self.assertEquals(None, form.title) self.assertEquals([], form.instructions) self.assertEquals({}, form.fields)
def get_userstats(self,user,comp_name,domain): """ Get stats of user. Accepts params: user - user jid comp_name - component name which should receive answer (usually we are) domain - our domain returns callback with one cb added - it converts answer to dict with fields name,fulljid,ip,status """ def process_stats(item): """ Process result of user-stats command Extract IP address and related info and returns it as dict """ x = xpath.queryForNodes("//x/field[@var='ipaddresses']",item) if not x: raise ValueError, "Wrong response from stats-request" frm = Form.fromElement(x[0]) values = frm.getValues() jid = JID(values['accountjid']) ipport = values['ipaddresses'] ip, port = ipport.split(':') userstats = dict() userstats['username'] = jid.userhost() userstats['fulljid'] = jid.full() #full jid which causes current change, i.e. which is connestion actualy used userstats['ip'] = ip return userstats #TODO: implement error handling if ACL doesn't allows us to call admin commands iq = IQ(self.xmlstream) iq['to'] = domain iq['from'] = comp_name cmd = iq.addElement((NS_COMMANDS,'command')) cmd['node'] = CMD_ADMIN_USER_STATS frm = Form('submit') frm.addField( Field(fieldType='hidden',var='FORM_TYPE',value=NS_ADMIN) ) frm.addField( Field(fieldType='jid-single',var='accountjid',value=user.full()) ) cmd.addChild(frm.toElement()) d = iq.send(domain) d.addCallback(process_stats) return d
def process_stats(item): """ Process result of user-stats command Extract IP address and related info and returns it as dict """ x = xpath.queryForNodes("//x/field[@var='ipaddresses']",item) if not x: raise ValueError, "Wrong response from stats-request" frm = Form.fromElement(x[0]) values = frm.getValues() jid = JID(values['accountjid']) ipport = values['ipaddresses'] ip, port = ipport.split(':') userstats = dict() userstats['username'] = jid.userhost() userstats['fulljid'] = jid.full() #full jid which causes current change, i.e. which is connestion actualy used userstats['ip'] = ip return userstats
def test_fromElementTitle(self): element = domish.Element((NS_X_DATA, 'x')) element.addElement('title', content='My title') form = Form.fromElement(element) self.assertEquals('My title', form.title)
def configure_node(data): request = PubSubRequestWithConf('configureSet') request.recipient = JID(self.pubsub_name) request.nodeIdentifier = PUB_NODE request.sender = JID(self.name) frm = Form('submit') frm.addField( Field(fieldType='hidden',var='FORM_TYPE',value=NS_PUBSUB_NODE_CONFIG) ) frm.addField( Field(var='pubsub#title',value='Live presence events') ) frm.addField( Field(var='pubsub#persist_items',value='1') ) frm.addField( Field(var='pubsub#deliver_payloads',value='1') ) frm.addField( Field(var='pubsub#send_last_published_item',value='never') ) frm.addField( Field(var='pubsub#presence_based_delivery',value='0') ) frm.addField( Field(var='pubsub#access_model',value='open') ) frm.addField( Field(var='pubsub#node_type',value='leaf') ) frm.addField( Field(var='pubsub#max_items',value='100') ) request.configureForm = frm request.send(self.xmlstream)