diff --git a/run_tests.py b/run_tests.py index 6cdae74..8d17c82 100644 --- a/run_tests.py +++ b/run_tests.py @@ -37,7 +37,6 @@ del sys.setdefaultencoding import tests from tests.jcl.jabber.test_component import * from tests.jcl.jabber.test_feeder import * -from tests.jcl.jabber.test_x import * from tests.jcl.test_lang import * from tests.jcl.model.test_account import * @@ -52,9 +51,6 @@ if __name__ == '__main__': feeder_component_suite = unittest.makeSuite(FeederComponent_TestCase, "test") feeder_suite = unittest.makeSuite(Feeder_TestCase, "test") sender_suite = unittest.makeSuite(Sender_TestCase, "test") - dataform_suite = unittest.makeSuite(DataForm_TestCase, "test") - field_suite = unittest.makeSuite(Field_TestCase, "test") - option_suite = unittest.makeSuite(Option_TestCase, "test") lang_suite = unittest.makeSuite(Lang_TestCase, "test") account_module_suite = unittest.makeSuite(AccountModule_TestCase, "test") account_suite = unittest.makeSuite(Account_TestCase, "test") @@ -62,16 +58,13 @@ if __name__ == '__main__': jcl_suite = unittest.TestSuite() # jcl_suite.addTest(FeederComponent_TestCase('test_handle_tick')) -# jcl_suite.addTest(JCLComponent_TestCase('test_handle_get_register_new_type2')) +# jcl_suite.addTest(JCLComponent_TestCase('test_handle_get_register_exist_complex')) # jcl_suite = unittest.TestSuite((component_suite)) # jcl_suite = unittest.TestSuite((presence_account_suite)) jcl_suite = unittest.TestSuite((component_suite, \ feeder_component_suite, \ feeder_suite, \ sender_suite, \ - dataform_suite, \ - field_suite, \ - option_suite, \ lang_suite, \ account_module_suite, \ account_suite, \ @@ -82,13 +75,11 @@ if __name__ == '__main__': coverage.stop() coverage.analysis(jcl.jabber.component) coverage.analysis(jcl.jabber.feeder) -coverage.analysis(jcl.jabber.x) coverage.analysis(jcl.lang) coverage.analysis(jcl.model.account) coverage.report([jcl.jabber.component, \ jcl.jabber.feeder, \ - jcl.jabber.x, \ jcl.lang, \ jcl.model.account]) diff --git a/src/jcl/jabber/component.py b/src/jcl/jabber/component.py index 1752ff3..d34d724 100644 --- a/src/jcl/jabber/component.py +++ b/src/jcl/jabber/component.py @@ -46,9 +46,9 @@ from pyxmpp.jabberd.component import Component from pyxmpp.jabber.disco import DiscoInfo, DiscoItems, DiscoItem, DiscoIdentity from pyxmpp.message import Message from pyxmpp.presence import Presence +from pyxmpp.jabber.dataforms import Form, Field, Option import jcl -from jcl.jabber.x import DataForm from jcl.jabber.error import FieldError from jcl.model import account from jcl.model.account import Account @@ -129,6 +129,7 @@ class JCLComponent(Component, object): self.queue.put(exception) raise finally: +# self.running = False if self.stream and not self.stream.eof \ and self.stream.socket is not None: current_user_jid = None @@ -277,8 +278,8 @@ class JCLComponent(Component, object): self._list_accounts(disco_items, self.account_classes[0], \ base_from_jid, account_type) else: - print >>sys.stderr, self.account_classes[0].__name__ + \ - " name not well formed" + self.__logger.error(self.account_classes[0].__name__ + \ + " name not well formed") else: # list account types (when multiples accounts types) for account_class in self.account_classes: regexp_type = re.compile("(.*)Account$") @@ -291,8 +292,8 @@ class JCLComponent(Component, object): account_type, \ account_type) else: - print >>sys.stderr, account_class.__name__ + \ - " name not well formed" + self.__logger.error(account_class.__name__ + \ + " name not well formed") else: # second level nodes = node.split("/"); if len(nodes) == 1 \ @@ -305,8 +306,8 @@ class JCLComponent(Component, object): base_from_jid, account_type = nodes[0]) else: - print >> sys.stderr, "Error: " + account_class.__name__ \ - + " class not in account_classes" + self.__logger.error("Error: " + account_class.__name__ \ + + " class not in account_classes") return disco_items def handle_get_version(self, info_query): @@ -331,7 +332,7 @@ class JCLComponent(Component, object): AND(_account_class.q.name == name, \ _account_class.q.user_jid == base_from_jid)): self.get_reg_form_init(lang_class, \ - _account).attach_xml(query) + _account).as_xml(query) self.db_disconnect() self.__logger.debug("GET_REGISTER") @@ -355,7 +356,7 @@ class JCLComponent(Component, object): else: # get_register new account of type node_list[0] + "Account" _account_class = self._get_account_class(account_type + "Account") self.get_reg_form(lang_class, \ - _account_class).attach_xml(query) + _account_class).as_xml(query) self.stream.send(info_query) return 1 @@ -403,12 +404,8 @@ class JCLComponent(Component, object): {"jir" : "jabber:iq:register", \ "jxd" : "jabber:x:data"})[0] - x_data = DataForm() - x_data.from_xml(x_node) - - name = x_data.get_field_value("name") - self.__logger.debug("Account name received = " + str(name)) - if name is None or name == "": + x_data = Form(x_node) + if not "name" in x_data or x_data["name"].value == "": iq_error = info_query.make_error_response("not-acceptable") text = iq_error.get_error().xmlnode.newTextChild(None, \ "text", \ @@ -416,6 +413,8 @@ class JCLComponent(Component, object): text.setNs(text.newNs(error.STANZA_ERROR_NS, None)) self.stream.send(iq_error) return + name = x_data["name"].value + self.__logger.debug("Account name received = " + str(name)) self.db_connect() accounts = Account.select(\ AND(Account.q.name == name, \ @@ -426,14 +425,14 @@ class JCLComponent(Component, object): all_accounts_count = all_accounts.count() if accounts_count > 1: # Just print a warning, only the first account will be use - print >> sys.stderr, "There might not exist 2 accounts for " + \ - base_from_jid + " and named " + name + self.__logger.error("There might not exist 2 accounts for " + \ + base_from_jid + " and named " + name) if accounts_count >= 1: _account = list(accounts)[0] else: if info_query.get_to().node is None: if len(self.account_classes) > 1: - print >>sys.stderr, "There should be only one account class declared" + self.__logger.error("There should be only one account class declared") new_account_class = self.account_classes[0] else: new_account_class = self._get_account_class(info_query.get_to().node + "Account") @@ -445,13 +444,15 @@ class JCLComponent(Component, object): for (field, field_type, field_options, field_post_func, \ field_default_func) in _account.get_register_fields(): if field is not None: - setattr(_account, field, \ - x_data.get_field_value(field, \ - field_post_func, \ - field_default_func)) + if field in x_data: + setattr(_account, field, \ + field_post_func(x_data[field].value)) + else: + setattr(_account, field, \ + field_default_func(field)) except FieldError, exception: _account.destroySelf() - print >>sys.stderr, str(exception) + self.__logger.error(str(exception)) iq_error = info_query.make_error_response("not-acceptable") text = iq_error.get_error().xmlnode.newTextChild(None, \ "text", \ @@ -726,16 +727,12 @@ class JCLComponent(Component, object): def get_reg_form(self, lang_class, _account_class): """Return register form based on language and account class """ - reg_form = DataForm() - reg_form.xmlns = "jabber:x:data" - reg_form.title = lang_class.register_title - reg_form.instructions = lang_class.register_instructions - reg_form.type = "form" - + reg_form = Form(title = lang_class.register_title, \ + instructions = lang_class.register_instructions) # "name" field is mandatory reg_form.add_field(field_type = "text-single", \ label = lang_class.account_name, \ - var = "name", \ + name = "name", \ required = True) for (field_name, field_type, field_options, post_func, default_func) in \ @@ -752,7 +749,7 @@ class JCLComponent(Component, object): label = field_name field = reg_form.add_field(field_type = field_type, \ label = label, \ - var = field_name) + name = field_name) if field_options is not None: for option_value in field_options: lang_label_attr = _account_class.__name__.lower() \ @@ -762,7 +759,7 @@ class JCLComponent(Component, object): else: label = option_value field.add_option(label = label, \ - value = option_value) + values = [option_value]) if default_func == account.mandatory_field: field.required = True ## TODO : get default value if any @@ -772,11 +769,11 @@ class JCLComponent(Component, object): """Return register form for an existing account (update) """ reg_form = self.get_reg_form(lang_class, _account.__class__) - reg_form.fields["name"].value = _account.name - reg_form.fields["name"].type = "hidden" - for (field_name, field) in reg_form.fields.items(): - if hasattr(_account, field_name): - field.value = str(getattr(_account, field_name)) + reg_form["name"].value = _account.name + reg_form["name"].type = "hidden" + for field in reg_form.fields: # TODO + if hasattr(_account, field.name): + field.value = getattr(_account, field.name) return reg_form ########################################################################### diff --git a/src/jcl/jabber/x.py b/src/jcl/jabber/x.py deleted file mode 100644 index 15fb03b..0000000 --- a/src/jcl/jabber/x.py +++ /dev/null @@ -1,172 +0,0 @@ -## -## x.py -## Login : David Rousselie -## Started on Fri Jan 7 11:06:42 2005 -## $Id: x.py,v 1.3 2005/09/18 20:24:07 dax Exp $ -## -## Copyright (C) 2005 -## This program is free software; you can redistribute it and/or modify -## it under the terms of the GNU General Public License as published by -## the Free Software Foundation; either version 2 of the License, or -## (at your option) any later version. -## -## This program is distributed in the hope that it will be useful, -## but WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -## GNU General Public License for more details. -## -## You should have received a copy of the GNU General Public License -## along with this program; if not, write to the Free Software -## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -## - -"""X -- Data Form handling -""" - -__revision__ = "$Id: x.py,v 1.3 2005/09/18 20:24:07 dax Exp $" - -from pyxmpp.stanza import common_doc - -class Option(object): - """Option value for list field - """ - def __init__(self, label, value): - self.label = label - self.value = value - - def get_xml(self, parent): - """Return XML Option representation from - self.label and self.value and attach it to parent - """ - if parent is None: - option = common_doc.newChild(None, "option", None) - else: - option = parent.newChild(None, "option", None) - option.setProp("label", self.label) - option.newChild(None, "value", self.value) - return option - -class Field(object): - """Jabber Xdata form Field - """ - def __init__(self, field_type, label, var, value, required = False): - self.type = field_type - self.label = label - self.var = var - self.value = value - self.options = [] - self.required = required - - def add_option(self, label, value): - """Add an Option to this field - """ - option = Option(label, value) - self.options.append(option) - return option - - def get_xml(self, parent): - """Return XML Field representation - and attach it to parent - """ - if parent is None: - raise Exception, "parent field should not be None" - else: - field = parent.newChild(None, "field", None) - field.setProp("type", self.type) - if not self.label is None: - field.setProp("label", self.label) - if not self.var is None: - field.setProp("var", self.var) - if self.value: - field.newChild(None, "value", self.value) - if self.required: - field.newChild(None, "required", None) - for option in self.options: - option.get_xml(field) - return field - -class DataForm(object): - """Jabber Data Form - """ - def __init__(self): - self.fields = {} - self.fields_tab = [] - self.title = None - self.instructions = None - self.x_type = None - self.xmlns = None - - def add_field(self, \ - field_type = "fixed", \ - label = None, \ - var = None, \ - value = "", \ - required = False): - """Add a Field to this Xdata form - """ - field = Field(field_type, label, var, value, required) - self.fields[var] = field - # fields_tab exist to keep added fields order - self.fields_tab.append(field) - return field - - def get_field_value(self, field_name, \ - post_func = (lambda value: value), \ - default_func = (lambda field_name: None)): - """Return field value processed by post_func - or return default func processing if field does not exist""" - if self.fields.has_key(field_name): - return post_func(self.fields[field_name].value) - return default_func(field_name) - - def attach_xml(self, info_query): - """Attach this Xdata form to iq node - """ - node = info_query.newChild(None, "x", None) - _ns = node.newNs(self.xmlns, None) - node.setNs(_ns) - if not self.title is None: - node.newTextChild(None, "title", self.title) - if not self.instructions is None: - node.newTextChild(None, "instructions", self.instructions) - for field in self.fields_tab: - field.get_xml(node) - return node - - def from_xml(self, node): - """Populate this X object from an XML representation - """ - context = common_doc.xpathNewContext() - context.setContextNode(node) - context.xpathRegisterNs("jxd", "jabber:x:data") - fields_node = context.xpathEval("jxd:field") - for field_node in fields_node: - if field_node.hasProp("type"): - field_type = field_node.prop("type") - else: - field_type = "" - - if field_node.hasProp("label"): - label = field_node.prop("label") - else: - label = "" - - if field_node.hasProp("var"): - var = field_node.prop("var") - else: - var = "" - - field_context = common_doc.xpathNewContext() - field_context.setContextNode(field_node) - field_context.xpathRegisterNs("jxd", "jabber:x:data") - fields_value_node = field_context.xpathEval("jxd:value") - if len(fields_value_node) > 0: - value = fields_value_node[0].content - else: - value = "" - self.add_field(field_type = field_type, \ - label = label, \ - var = var, \ - value = value) - field_context.xpathFreeContext() - context.xpathFreeContext() diff --git a/src/jcl/model/account.py b/src/jcl/model/account.py index 4523406..241d659 100644 --- a/src/jcl/model/account.py +++ b/src/jcl/model/account.py @@ -41,10 +41,6 @@ def default_post_func(field_value): """Default post process function: do nothing""" return field_value -def boolean_post_func(field_value): - """Return a boolean from boolean field value""" - return (field_value == "1" or field_value.lower() == "true") - def int_post_func(field_value): """Return an integer from integer field value""" return int(field_value) diff --git a/tests/jcl/jabber/test_component.py b/tests/jcl/jabber/test_component.py index ed7e36d..fd54714 100644 --- a/tests/jcl/jabber/test_component.py +++ b/tests/jcl/jabber/test_component.py @@ -38,12 +38,12 @@ from pyxmpp.iq import Iq from pyxmpp.stanza import Stanza from pyxmpp.presence import Presence from pyxmpp.message import Message +from pyxmpp.jabber.dataforms import Form, Field, Option from jcl.jabber.component import JCLComponent from jcl.model import account from jcl.model.account import Account from jcl.lang import Lang -from jcl.jabber.x import DataForm from tests.jcl.model.account import ExampleAccount, Example2Account @@ -114,6 +114,10 @@ class MockStreamNoConnect(MockStream): self.connection_started = True self.eof = True +class MockStreamRaiseException(MockStream): + def connect(self): + raise Error("Test error") + class JCLComponent_TestCase(unittest.TestCase): ########################################################################### # Utility methods @@ -171,6 +175,7 @@ class JCLComponent_TestCase(unittest.TestCase): pass def test_run(self): + """Test basic main loop execution""" self.comp.time_unit = 1 # Do not loop, handle_tick is virtual # Tests in subclasses might be more precise @@ -184,7 +189,22 @@ class JCLComponent_TestCase(unittest.TestCase): if self.comp.queue.qsize(): raise self.comp.queue.get(0) + def test_run_unhandled_error(self): + """Test main loop unhandled error from a component handler""" + # TODO + self.comp.time_unit = 1 + self.comp.stream = MockStreamNoConnect() + self.comp.stream_class = MockStreamNoConnect + self.comp.run() + self.assertTrue(self.comp.stream.connection_started) + threads = threading.enumerate() + self.assertEquals(len(threads), 1) + self.assertTrue(self.comp.stream.connection_stopped) + if self.comp.queue.qsize(): + raise self.comp.queue.get(0) + def test_run_ni_handle_tick(self): + """Test JCLComponent 'NotImplemented' error from handle_tick method""" self.comp.time_unit = 1 self.comp.stream = MockStream() self.comp.stream_class = MockStream @@ -205,6 +225,7 @@ class JCLComponent_TestCase(unittest.TestCase): NotImplementedError)) def test_run_go_offline(self): + """Test main loop send offline presence when exiting""" self.comp.stream = MockStream() self.comp.stream_class = MockStream self.comp.time_unit = 1 @@ -677,22 +698,22 @@ class JCLComponent_TestCase(unittest.TestCase): password = "mypassword", \ store_password = False, \ test_enum = "choice3", \ - test_int = 21) + test_int = 1) account11 = ExampleAccount(user_jid = "user1@test.com", \ name = "account11", \ jid = "account11@jcl.test.com", \ - login = "mylogin", \ - password = "mypassword", \ + login = "mylogin11", \ + password = "mypassword11", \ store_password = False, \ - test_enum = "choice3", \ - test_int = 21) + test_enum = "choice2", \ + test_int = 11) account21 = ExampleAccount(user_jid = "user2@test.com", \ name = "account21", \ jid = "account21@jcl.test.com", \ - login = "mylogin", \ - password = "mypassword", \ + login = "mylogin21", \ + password = "mypassword21", \ store_password = False, \ - test_enum = "choice3", \ + test_enum = "choice1", \ test_int = 21) del account.hub.threadConnection self.comp.handle_get_register(Iq(stanza_type = "get", \ @@ -742,7 +763,7 @@ class JCLComponent_TestCase(unittest.TestCase): self.assertEquals(field.prop("var"), "store_password") self.assertEquals(field.prop("label"), "store_password") self.assertEquals(field.children.name, "value") - self.assertEquals(field.children.content, "False") + self.assertEquals(field.children.content, "0") field = fields[4] self.assertEquals(field.prop("type"), "list-single") self.assertEquals(field.prop("var"), "test_enum") @@ -769,7 +790,7 @@ class JCLComponent_TestCase(unittest.TestCase): self.assertEquals(field.prop("var"), "test_int") self.assertEquals(field.prop("label"), "test_int") self.assertEquals(field.children.name, "value") - self.assertEquals(field.children.content, "21") + self.assertEquals(field.children.content, "1") def test_handle_get_register_new_type1(self): self.comp.stream = MockStream() @@ -780,7 +801,6 @@ class JCLComponent_TestCase(unittest.TestCase): to_jid = "jcl.test.com/example")) self.__check_get_register_new_type() - # TODO def test_handle_get_register_new_type2(self): self.comp.stream = MockStream() self.comp.stream_class = MockStream @@ -822,17 +842,15 @@ class JCLComponent_TestCase(unittest.TestCase): def test_handle_set_register_new(self): self.comp.stream = MockStream() self.comp.stream_class = MockStream - x_data = DataForm() - x_data.xmlns = "jabber:x:data" - x_data.type = "submit" - x_data.add_field(field_type = "text-single", \ - var = "name", \ - value = "account1") + x_data = Form("submit") + x_data.add_field(name = "name", \ + value = "account1", \ + field_type = "text-single") iq_set = Iq(stanza_type = "set", \ from_jid = "user1@test.com", \ to_jid = "jcl.test.com") query = iq_set.new_query("jabber:iq:register") - x_data.attach_xml(query) + x_data.as_xml(query, None) self.comp.handle_set_register(iq_set) account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) @@ -881,32 +899,30 @@ class JCLComponent_TestCase(unittest.TestCase): self.comp.stream = MockStream() self.comp.stream_class = MockStream self.comp.account_classes = [ExampleAccount] - x_data = DataForm() - x_data.xmlns = "jabber:x:data" - x_data.type = "submit" - x_data.add_field(field_type = "text-single", \ - var = "name", \ - value = "account1") - x_data.add_field(field_type = "text-single", \ - var = "login", \ - value = "mylogin") - x_data.add_field(field_type = "text-private", \ - var = "password", \ - value = "mypassword") - x_data.add_field(field_type = "boolean", \ - var = "store_password", \ - value = "false") - x_data.add_field(field_type = "list-single", \ - var = "test_enum", \ - value = "choice3") - x_data.add_field(field_type = "text-single", \ - var = "test_int", \ - value = "43") + x_data = Form("submit") + x_data.add_field(name = "name", \ + value = "account1", \ + field_type = "text-single") + x_data.add_field(name = "login", \ + value = "mylogin", \ + field_type = "text-single") + x_data.add_field(name = "password", \ + value = "mypassword", \ + field_type = "text-private") + x_data.add_field(name = "store_password", \ + value = False, \ + field_type = "boolean") + x_data.add_field(name = "test_enum", \ + value = "choice3", \ + field_type = "list-single") + x_data.add_field(name = "test_int", \ + value = 43, \ + field_type = "text-single") iq_set = Iq(stanza_type = "set", \ from_jid = "user1@test.com", \ to_jid = "jcl.test.com") query = iq_set.new_query("jabber:iq:register") - x_data.attach_xml(query) + x_data.as_xml(query, None) self.comp.handle_set_register(iq_set) account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) @@ -960,20 +976,18 @@ class JCLComponent_TestCase(unittest.TestCase): self.comp.stream = MockStream() self.comp.stream_class = MockStream self.comp.account_classes = [ExampleAccount] - x_data = DataForm() - x_data.xmlns = "jabber:x:data" - x_data.type = "submit" - x_data.add_field(field_type = "text-single", \ - var = "name", \ - value = "account1") - x_data.add_field(field_type = "text-single", \ - var = "login", \ - value = "mylogin") + x_data = Form("submit") + x_data.add_field(name = "name", \ + value = "account1", \ + field_type = "text-single") + x_data.add_field(name = "login", \ + value = "mylogin", \ + field_type = "text-single") iq_set = Iq(stanza_type = "set", \ from_jid = "user1@test.com", \ to_jid = "jcl.test.com") query = iq_set.new_query("jabber:iq:register") - x_data.attach_xml(query) + x_data.as_xml(query) self.comp.handle_set_register(iq_set) account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) @@ -995,14 +1009,12 @@ class JCLComponent_TestCase(unittest.TestCase): def test_handle_set_register_new_name_mandatory(self): self.comp.stream = MockStream() self.comp.stream_class = MockStream - x_data = DataForm() - x_data.xmlns = "jabber:x:data" - x_data.type = "submit" + x_data = Form("submit") iq_set = Iq(stanza_type = "set", \ from_jid = "user1@test.com", \ to_jid = "jcl.test.com") query = iq_set.new_query("jabber:iq:register") - x_data.attach_xml(query) + x_data.as_xml(query) self.comp.handle_set_register(iq_set) account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) @@ -1026,17 +1038,15 @@ class JCLComponent_TestCase(unittest.TestCase): self.comp.stream = MockStream() self.comp.stream_class = MockStream self.comp.account_classes = [ExampleAccount] - x_data = DataForm() - x_data.xmlns = "jabber:x:data" - x_data.type = "submit" - x_data.add_field(field_type = "text-single", \ - var = "name", \ - value = "account1") + x_data = Form("submit") + x_data.add_field(name = "name", \ + value = "account1", \ + field_type = "text-single") iq_set = Iq(stanza_type = "set", \ from_jid = "user1@test.com", \ to_jid = "jcl.test.com") query = iq_set.new_query("jabber:iq:register") - x_data.attach_xml(query) + x_data.as_xml(query) self.comp.handle_set_register(iq_set) account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) @@ -1077,32 +1087,30 @@ class JCLComponent_TestCase(unittest.TestCase): test_enum = "choice1", \ test_int = 21) del account.hub.threadConnection - x_data = DataForm() - x_data.xmlns = "jabber:x:data" - x_data.type = "submit" - x_data.add_field(field_type = "text-single", \ - var = "name", \ - value = "account1") - x_data.add_field(field_type = "text-single", \ - var = "login", \ - value = "mylogin2") - x_data.add_field(field_type = "text-private", \ - var = "password", \ - value = "mypassword2") - x_data.add_field(field_type = "boolean", \ - var = "store_password", \ - value = "false") - x_data.add_field(field_type = "list-single", \ - var = "test_enum", \ - value = "choice3") - x_data.add_field(field_type = "text-single", \ - var = "test_int", \ - value = "43") + x_data = Form("submit") + x_data.add_field(name = "name", \ + value = "account1", \ + field_type = "text-single") + x_data.add_field(name = "login", \ + value = "mylogin2", \ + field_type = "text-single") + x_data.add_field(name = "password", \ + value = "mypassword2", \ + field_type = "text-private") + x_data.add_field(name = "store_password", \ + value = False, \ + field_type = "boolean") + x_data.add_field(name = "test_enum", \ + value = "choice3", \ + field_type = "list-single") + x_data.add_field(name = "test_int", \ + value = 43, \ + field_type = "text-single") iq_set = Iq(stanza_type = "set", \ from_jid = "user1@test.com", \ to_jid = "jcl.test.com") query = iq_set.new_query("jabber:iq:register") - x_data.attach_xml(query) + x_data.as_xml(query) self.comp.handle_set_register(iq_set) account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) diff --git a/tests/jcl/jabber/test_x.py b/tests/jcl/jabber/test_x.py deleted file mode 100644 index 59e6fc5..0000000 --- a/tests/jcl/jabber/test_x.py +++ /dev/null @@ -1,175 +0,0 @@ -## -## test_x.py -## Login : David Rousselie -## Started on Wed Nov 22 19:24:19 2006 David Rousselie -## $Id$ -## -## Copyright (C) 2006 David Rousselie -## This program is free software; you can redistribute it and/or modify -## it under the terms of the GNU General Public License as published by -## the Free Software Foundation; either version 2 of the License, or -## (at your option) any later version. -## -## This program is distributed in the hope that it will be useful, -## but WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -## GNU General Public License for more details. -## -## You should have received a copy of the GNU General Public License -## along with this program; if not, write to the Free Software -## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -## - -import unittest - -from jcl.jabber.x import * -from pyxmpp.stanza import common_doc -import libxml2 - -class DataForm_TestCase(unittest.TestCase): - def setUp(self): - self.data_form = DataForm() - - def tearDown(self): - self.data_form = None - - def test_add_field(self): - self.data_form = DataForm() - field = self.data_form.add_field(field_type = "single-text", \ - label = "Name", \ - var = "name", \ - value = "jcl") - self.assertEquals(self.data_form.fields["name"], field) - self.assertTrue(field in self.data_form.fields_tab) - self.assertEquals(field.type, "single-text") - self.assertEquals(field.label, "Name") - self.assertEquals(field.var, "name") - self.assertEquals(field.value, "jcl") - self.assertEquals(field.required, False) - - def test_add_required_field(self): - self.data_form = DataForm() - field = self.data_form.add_field(field_type = "single-text", \ - label = "Name", \ - var = "name", \ - value = "jcl", \ - required = True) - self.assertEquals(self.data_form.fields["name"], field) - self.assertTrue(field in self.data_form.fields_tab) - self.assertEquals(field.type, "single-text") - self.assertEquals(field.label, "Name") - self.assertEquals(field.var, "name") - self.assertEquals(field.value, "jcl") - self.assertEquals(field.required, True) - - def test_get_field_value(self): - self.data_form.add_field(field_type = "single-text", \ - var = "name", \ - value = "jcl") - self.assertEquals(self.data_form.get_field_value("name"), \ - "jcl") - - def test_get_field_value_not_exist(self): - self.assertEquals(self.data_form.get_field_value("name"), \ - None) - - def test_get_field_value_post_func(self): - self.data_form.add_field(field_type = "single-text", \ - var = "name", \ - value = "jcl") - self.assertEquals(self.data_form.get_field_value("name", \ - post_func = (lambda value: "_" + value + "_")), \ - "_jcl_") - - def test_get_field_value_post_func(self): - self.assertEquals(self.data_form.get_field_value("name", \ - default_func = (lambda field_name: "no '" + field_name + "' field")), \ - "no 'name' field") - - def test_attach_xml(self): - parent_node = common_doc.newChild(None, "iq", None) - self.data_form.title = "JCL Form" - self.data_form.instructions = "Fill the form" - self.data_form.add_field(label = "label1", \ - var = "var1") - self.data_form.add_field(label = "label2", \ - var = "var2") - self.data_form.xmlns = "jabber:x:data" - data_form_node = self.data_form.attach_xml(parent_node) - context = common_doc.xpathNewContext() - context.setContextNode(parent_node) - context.xpathRegisterNs("jxd", "jabber:x:data") - self.assertEquals(context.xpathEval("jxd:x/jxd:title")[0].content, "JCL Form") - self.assertEquals(context.xpathEval("jxd:x/jxd:instructions")[0].content, "Fill the form") - self.assertEquals(len(context.xpathEval("jxd:x/jxd:field")), 2) - self.assertEquals(context.xpathEval("jxd:x/jxd:field")[0].prop("type"), "fixed") - self.assertEquals(context.xpathEval("jxd:x/jxd:field")[0].prop("label"), "label1") - self.assertEquals(context.xpathEval("jxd:x/jxd:field")[0].prop("var"), "var1") - self.assertEquals(context.xpathEval("jxd:x/jxd:field")[1].prop("type"), "fixed") - self.assertEquals(context.xpathEval("jxd:x/jxd:field")[1].prop("label"), "label2") - self.assertEquals(context.xpathEval("jxd:x/jxd:field")[1].prop("var"), "var2") - context.xpathFreeContext() - - def test_from_xml(self): - xml_buffer = "" \ - "value1" \ - "value2" \ - "" - xml_node = libxml2.parseMemory(xml_buffer, len(xml_buffer)) - self.data_form.from_xml(xml_node.children) - self.assertEquals(len(self.data_form.fields_tab), 2) - field1 = self.data_form.fields["var1"] - self.assertEquals(field1.type, "text-single") - self.assertEquals(field1.var, "var1") - self.assertEquals(field1.value, "value1") - field2 = self.data_form.fields["var2"] - self.assertEquals(field2.type, "text-single") - self.assertEquals(field2.var, "var2") - self.assertEquals(field2.value, "value2") - -class Field_TestCase(unittest.TestCase): - def test_get_xml_no_option_required(self): - field = Field(field_type = "text-single", \ - label = "Name", \ - var = "name", \ - value = "myaccount", \ - required = True) - parent_node = common_doc.newChild(None, "x", None) - xml_field = field.get_xml(parent_node) - self.assertEquals(xml_field.prop("type"), "text-single") - self.assertEquals(xml_field.prop("label"), "Name") - self.assertEquals(xml_field.prop("var"), "name") - self.assertEquals(xml_field.xpathEval("value")[0].content, "myaccount") - self.assertEquals(len(xml_field.xpathEval("required")), 1) - - def test_get_xml_with_option(self): - field = Field(field_type = "text-single", \ - label = "Name", \ - var = "name", \ - value = "myaccount") - field.add_option("test_option", "option_value") - parent_node = common_doc.newChild(None, "x", None) - xml_field = field.get_xml(parent_node) - self.assertEquals(xml_field.prop("type"), "text-single") - self.assertEquals(xml_field.prop("label"), "Name") - self.assertEquals(xml_field.prop("var"), "name") - self.assertEquals(xml_field.xpathEval("value")[0].content, "myaccount") - self.assertEquals(\ - xml_field.xpathEval("option['label=test_option']/value")[0].content, \ - "option_value") - - def test_get_xml_no_parent(self): - field = Field(field_type = "text-single", \ - label = "Name", \ - var = "name", \ - value = "myaccount") - self.assertRaises(Exception, field.get_xml, None) - -class Option_TestCase(unittest.TestCase): - def test_get_xml(self): - option = Option("test_option", "option_value") - xml_option = option.get_xml(None) - self.assertEquals(xml_option.prop("label"), "test_option") - self.assertEquals(xml_option.xpathEval("value")[0].content, \ - "option_value") - diff --git a/tests/jcl/model/account.py b/tests/jcl/model/account.py index bb83d0b..1288db1 100644 --- a/tests/jcl/model/account.py +++ b/tests/jcl/model/account.py @@ -48,7 +48,7 @@ class ExampleAccount(Account): account.mandatory_field), \ ("password", "text-private", None, password_post_func, \ (lambda field_name: None)), \ - ("store_password", "boolean", None, account.boolean_post_func, \ + ("store_password", "boolean", None, account.default_post_func, \ lambda field_name: True), \ ("test_enum", "list-single", ["choice1", "choice2", "choice3"], \ account.string_not_null_post_func,\ diff --git a/tests/jcl/model/test_account.py b/tests/jcl/model/test_account.py index 21e29e5..d8bb8fb 100644 --- a/tests/jcl/model/test_account.py +++ b/tests/jcl/model/test_account.py @@ -41,30 +41,6 @@ class AccountModule_TestCase(unittest.TestCase): result = account.default_post_func("test") self.assertEquals(result, "test") - def test_boolean_post_func_1(self): - result = account.boolean_post_func("1") - self.assertTrue(result) - - def test_boolean_post_func_0(self): - result = account.boolean_post_func("0") - self.assertFalse(result) - - def test_boolean_post_func_True(self): - result = account.boolean_post_func("True") - self.assertTrue(result) - - def test_boolean_post_func_true(self): - result = account.boolean_post_func("true") - self.assertTrue(result) - - def test_boolean_post_func_False(self): - result = account.boolean_post_func("False") - self.assertFalse(result) - - def test_boolean_post_func_false(self): - result = account.boolean_post_func("false") - self.assertFalse(result) - def test_int_post_func(self): result = account.int_post_func("42") self.assertEquals(result, 42)