diff options
author | David Rousselie <david.rousselie@happycoders.org> | 2010-06-04 21:06:47 +0400 |
---|---|---|
committer | David Rousselie <david.rousselie@happycoders.org> | 2010-06-04 21:06:47 +0400 |
commit | 74feff9ea9711a7725dd4909fc7a755c95e05311 (patch) | |
tree | d8cde70cc55cba9452f769d607ca2eb32ae0556a | |
parent | a26290d9c4bee14ae6a5f76b95196fe7aaa03cb2 (diff) |
check for account login and host with whitespace
-rw-r--r-- | src/jmc/lang.py | 2 | ||||
-rw-r--r-- | src/jmc/model/account.py | 38 | ||||
-rw-r--r-- | src/jmc/model/tests/account.py | 12 |
3 files changed, 33 insertions, 19 deletions
diff --git a/src/jmc/lang.py b/src/jmc/lang.py index fa43cf1..81f5a43 100644 --- a/src/jmc/lang.py +++ b/src/jmc/lang.py @@ -95,6 +95,7 @@ class Lang(jcl.lang.Lang): field_email_subject = u"Email Subject" field_select_more_emails = u"Select more emails to fetch" mail_subject = u"Email from %s" + no_whitespace_in_field = u"There must be no whitespace in field" class fr(en, jcl.lang.Lang.fr): component_name = u"Jabber Mail Component" @@ -169,6 +170,7 @@ class Lang(jcl.lang.Lang): field_email_subject = u"Objet des emails" field_select_more_emails = u"Séléctionner plus d'emails à récupérer" mail_subject = u"Email de %s" + no_whitespace_in_field = u"Le champ ne doit pas contenir d'espace" class nl(en, jcl.lang.Lang.nl): # TODO: when finish, delete this line and uncomment in tests/lang.py the makeSuite(Language_nl_TestCase, 'test') line diff --git a/src/jmc/model/account.py b/src/jmc/model/account.py index d67cee5..4d3741c 100644 --- a/src/jmc/model/account.py +++ b/src/jmc/model/account.py @@ -40,6 +40,7 @@ from sqlobject.inheritance import InheritableSQLObject from sqlobject.col import StringCol, IntCol, BoolCol from sqlobject.sqlbuilder import AND +from jcl.error import FieldError from jcl.model import account from jcl.model.account import Account, PresenceAccount from jmc.lang import Lang @@ -49,7 +50,7 @@ class NoAccountError(Exception): pass def _get_default_status_msg(self, lang_class): - return self.get_type() + "://" + self.login + "@" + self.host + ":" + \ + return str(self.get_type()) + "://" + str(self.login) + "@" + str(self.host) + ":" + \ unicode(self.port) def validate_password(password, default_func, bare_from_jid): @@ -58,7 +59,10 @@ def validate_password(password, default_func, bare_from_jid): return password def validate_login(login, default_func, bare_from_jid): - return account.mandatory_field("login", login) + return account.no_whitespace_field("login", account.mandatory_field("login", login)) + +def validate_host(host, default_func, bare_from_jid): + return account.no_whitespace_field("host", account.mandatory_field("host", host)) class MailAccount(PresenceAccount): """ Wrapper to mail connection and action. @@ -104,13 +108,10 @@ class MailAccount(PresenceAccount): real_class = cls return PresenceAccount.get_register_fields(real_class) + \ [("login", "text-single", None, - validate_login, - lambda bare_from_jid: ""), + validate_login, lambda bare_from_jid: ""), ("password", "text-private", None, validate_password, lambda bare_from_jid: ""), - ("host", "text-single", None, - lambda field_value, default_func, bare_from_jid: \ - account.mandatory_field("host", field_value), + ("host", "text-single", None, validate_host, lambda bare_from_jid: ""), ("port", "text-single", None, account.int_post_func, @@ -272,8 +273,7 @@ class IMAPAccount(MailAccount): delimiter = StringCol(default=".") def _get_register_fields(cls, real_class=None): - """See Account._get_register_fields - """ + """See Account._get_register_fields""" if real_class is None: real_class = cls return MailAccount.get_register_fields(real_class) + \ @@ -370,7 +370,7 @@ class IMAPAccount(MailAccount): if typ == 'OK': return self.format_message(\ email.message_from_string(data[0][1])) - raise Exception(data[0] + " (email " + str(index) + ")") + raise Exception(str(data[0]) + " (email " + str(index) + ")") def get_mail_summary(self, index): self.__logger.debug("Getting mail summary " + str(index)) @@ -380,7 +380,7 @@ class IMAPAccount(MailAccount): if typ == 'OK': return self.format_message_summary(\ email.message_from_string(data[0][1])) - raise Exception(data[0] + " (email " + str(index) + ")") + raise Exception(str(data[0]) + " (email " + str(index) + ")") def get_next_mail_index(self, mail_list): """ @@ -455,7 +455,7 @@ class IMAPAccount(MailAccount): return else: self.disconnect() - raise Exception("Cannot find mailbox " + self.mailbox) + raise Exception("Cannot find mailbox " + str(self.mailbox)) else: match = self._regexp_list.match(line) if match is not None: @@ -463,10 +463,10 @@ class IMAPAccount(MailAccount): else: self.disconnect() raise Exception("Cannot find delimiter for mailbox " - + self.mailbox) + + str(self.mailbox)) else: self.disconnect() - raise Exception("Cannot find mailbox " + self.mailbox) + raise Exception("Cannot find mailbox " + str(self.mailbox)) self.disconnect() # replace any previous delimiter in self.mailbox by "/" if self.delimiter != testing_delimiter: @@ -498,7 +498,7 @@ class POP3Account(MailAccount): def connect(self): self.__logger.debug("Connecting to POP3 server " - + self.login + "@" + self.host + ":" + + + str(self.login) + "@" + str(self.host) + ":" + str(self.port) + ". SSL=" + str(self.ssl)) if self.ssl: self.connection = poplib.POP3_SSL(self.host, self.port) @@ -513,7 +513,7 @@ class POP3Account(MailAccount): def disconnect(self): - self.__logger.debug("Disconnecting from POP3 server " + self.host) + self.__logger.debug("Disconnecting from POP3 server " + str(self.host)) self.connection.quit() self.connected = False @@ -748,17 +748,17 @@ class GlobalSMTPAccount(AbstractSMTPAccount): current_error = None for auth_method in auth_methods: self.__logger.debug("Trying to authenticate using " - + auth_method + " method") + + str(auth_method) + " method") smtp_connection.esmtp_features["auth"] = auth_method try: smtp_connection.login(self.login, self.password) current_error = None self.__logger.debug("Successfuly to authenticate using " - + auth_method + " method") + + str(auth_method) + " method") break except smtplib.SMTPAuthenticationError, error: self.__logger.debug("Failed to authenticate using " - + auth_method + " method") + + str(auth_method) + " method") current_error = error if current_error is not None: raise current_error diff --git a/src/jmc/model/tests/account.py b/src/jmc/model/tests/account.py index 1fde245..6f561c4 100644 --- a/src/jmc/model/tests/account.py +++ b/src/jmc/model/tests/account.py @@ -43,6 +43,18 @@ class AccountModule_TestCase(unittest.TestCase): self.assertRaises(FieldError, jmc.model.account.validate_login, None, None, None) + def test_validate_login_with_login_with_whitespace(self): + self.assertRaises(FieldError, jmc.model.account.validate_login, + "login with spaces", None, None) + + def test_validate_host_with_empty_login(self): + self.assertRaises(FieldError, jmc.model.account.validate_host, + None, None, None) + + def test_validate_host_with_host_with_whitespace(self): + self.assertRaises(FieldError, jmc.model.account.validate_host, + "host with spaces", None, None) + class MailAccount_TestCase(PresenceAccount_TestCase): def setUp(self): PresenceAccount_TestCase.setUp(self, tables=[MailAccount]) |