def test_handle_valid_name(self): """Test with invalid supplied name""" iq_set = Iq(stanza_type="set", from_jid="[email protected]/res", to_jid="jcl.test.com") x_data = Form("submit") x_data.add_field(name="name", value="good_name", field_type="text-single") result = self.handler.handle(iq_set, Lang.en, None, x_data) self.assertEquals(result, None)
def test_handle_invalid_empty_name(self): """Test with empty supplied name""" iq_set = Iq(stanza_type="set", from_jid="[email protected]/res", to_jid="jcl.test.com") x_data = Form("submit") x_data.add_field(name="name", value="", field_type="text-single") result = self.handler.handle(iq_set, Lang.en, None, x_data) self.assertEquals(len(result), 1) self.assertEquals(result[0].xmlnode.prop("type"), "error") error = result[0].get_error() self.assertEquals(error.get_condition().name, "not-acceptable") self.assertEquals(error.get_text(), Lang.en.field_error \ % ("name", Lang.en.mandatory_field))
def execute_retrieve_attachment_1(self, info_query, session_context, command_node, lang_class): self.__logger.debug("Executing command 'retrieve-attachment' step 1") self.add_actions(command_node, [command.ACTION_NEXT]) bare_from_jid = info_query.get_from().bare() account_name = info_query.get_to().node _account = account.get_account(bare_from_jid, account_name, MailAccount) if _account is not None: result_form = Form(xmlnode_or_type="form", title="TITLE", instructions="INS") field = result_form.add_field(name="attachments", field_type="list-multi", label="select attachments") field.add_option(label="Next", value="-1") for (mail_id, mail_title) in _account.get_mail_with_attachment_list(): field.add_option(label=mail_title, value=mail_id) result_form.as_xml(command_node) return (result_form, []) else: # ERROR return (None, [])
def execute_get_email(self, info_query, session_context, command_node, lang_class): self.__logger.debug("Executing command 'get-email' step 1") if "fetch_more" in session_context \ and session_context["fetch_more"][-1] == "0": return self.execute_get_email_last(info_query, session_context, command_node, lang_class) else: self.add_actions(command_node, [command.ACTION_COMPLETE]) bare_from_jid = info_query.get_from().bare() account_name = info_query.get_to().node _account = account.get_account(bare_from_jid, account_name) if _account is not None: result_form = Form(\ xmlnode_or_type="form", title=lang_class.command_get_email, instructions=lang_class.command_get_email_1_description) if not "start_index" in session_context: session_context["start_index"] = 1 self.__logger.debug("Checking email list summary from index " + str(session_context["start_index"]) + " to " + str(session_context["start_index"] + 10)) _account.connect() email_list = _account.get_mail_list_summary(\ start_index=session_context["start_index"], end_index=session_context["start_index"] + 9) _account.disconnect() session_context["start_index"] += 10 field = result_form.add_field(name="emails", field_type="list-multi", label=lang_class.field_email_subject) for (email_index, email_subject) in email_list: field.add_option(label=email_subject, value=email_index) if len(email_list) == 10: result_form.add_field(name="fetch_more", field_type="boolean", label=lang_class.field_select_more_emails) else: session_context["fetch_more"] = ["0"] result_form.as_xml(command_node) return (result_form, []) else: raise CommandError("item-not-found")
def build_form_inc(self, form_type, title, instructions, field_data): form = Form(form_type) form.title = title form.instructions = instructions for name, ftype, values, label, options, required, desc in field_data: field = form.add_field(name = name, field_type = ftype, values = values, label = label, required = required, desc = desc) for olabel, ovalue in options: field.add_option(ovalue, olabel) return form
def complete_xml_element(self, xmlnode, _unused): """Complete the XML node with `self` content. Should be overriden in classes derived from `StanzaPayloadObject`. :Parameters: - `xmlnode`: XML node with the element being built. It has already right name and namespace, but no attributes or content. - `_unused`: document to which the element belongs. :Types: - `xmlnode`: `libxml2.xmlNode` - `_unused`: `libxml2.xmlDoc`""" if self.possible_streams: field = dataforms.Field(name='stream-method', field_type='list-single', options=[dataforms.Option(n) for n in self.possible_streams]) f = dataforms.Form(xmlnode_or_type='form', fields=[field]) else: f = Form(xmlnode_or_type="submit") f.add_field(name='stream-method', value=self.selected_stream) f.as_xml(xmlnode, _unused)
def get_form(self, form_type="form"): """Return Data Form for the `Register` object. Convert legacy fields to a data form if `self.form` is `None`, return `self.form` otherwise. :Parameters: - `form_type`: If "form", then a form to fill-in should be returned. If "sumbit", then a form with submitted data. :Types: - `form_type`: `unicode` :return: `self.form` or a form created from the legacy fields :returntype: `pyxmpp.jabber.dataforms.Form`""" if self.form: if self.form.type != form_type: raise ValueError, "Bad form type in the jabber:iq:register element" return self.form form = Form(form_type, instructions=self.instructions) form.add_field("FORM_TYPE", [u"jabber:iq:register"], "hidden") for field in legacy_fields: field_type, field_label = legacy_fields[field] value = getattr(self, field) if value is None: continue if form_type == "form": if not value: value = None form.add_field(name=field, field_type=field_type, label=field_label, value=value, required=True) else: form.add_field(name=field, value=value) return form
def complete_xml_element(self, xmlnode, _unused): """Complete the XML node with `self` content. Should be overriden in classes derived from `StanzaPayloadObject`. :Parameters: - `xmlnode`: XML node with the element being built. It has already right name and namespace, but no attributes or content. - `_unused`: document to which the element belongs. :Types: - `xmlnode`: `libxml2.xmlNode` - `_unused`: `libxml2.xmlDoc`""" if self.possible_streams: field = dataforms.Field( name='stream-method', field_type='list-single', options=[dataforms.Option(n) for n in self.possible_streams]) f = dataforms.Form(xmlnode_or_type='form', fields=[field]) else: f = Form(xmlnode_or_type="submit") f.add_field(name='stream-method', value=self.selected_stream) f.as_xml(xmlnode, _unused)
def build_form_inc(self, form_type, title, instructions, field_data): form = Form(form_type) form.title = title form.instructions = instructions for name, ftype, values, label, options, required, desc in field_data: field = form.add_field(name=name, field_type=ftype, values=values, label=label, required=required, desc=desc) for olabel, ovalue in options: field.add_option(ovalue, olabel) return form