Work with default SMTP account

if a default SMTP account is defined, messages sent to root JID are sent with this account parameters.
Otherwise, the first SMTP account is used.

darcs-hash:20070605195924-86b55-5f8ab38dee873680f32b16619123d909d2ffb4b2.gz
This commit is contained in:
David Rousselie
2007-06-05 21:59:24 +02:00
parent 76c1d6968b
commit b69b55e1b1
4 changed files with 32 additions and 10 deletions

View File

@@ -244,12 +244,15 @@ class RootSendMailMessageHandler(SendMailMessageHandler):
def filter(self, message, lang_class): def filter(self, message, lang_class):
name = message.get_to().node name = message.get_to().node
bare_from_jid = unicode(message.get_from().bare()) bare_from_jid = unicode(message.get_from().bare())
accounts = Account.select(\ accounts = SMTPAccount.select(\
AND(Account.q.name == name, AND(SMTPAccount.q.default_account == True,
Account.q.user_jid == bare_from_jid)) SMTPAccount.q.user_jid == bare_from_jid))
if accounts.count() != 1: if accounts.count() != 1:
self.__logger.error("Account " + name + " for user " + \ self.__logger.error("No default account found for user " +
bare_from_jid + " must be uniq") str(bare_from_jid))
if accounts.count() == 0:
accounts = SMTPAccount.select(\
SMTPAccount.q.user_jid == bare_from_jid)
return accounts return accounts
def handle(self, message, lang_class, accounts): def handle(self, message, lang_class, accounts):

View File

@@ -568,6 +568,7 @@ class RootSendMailMessageHandler_TestCase(unittest.TestCase):
account11 = SMTPAccount(user_jid="user1@test.com", account11 = SMTPAccount(user_jid="user1@test.com",
name="account11", name="account11",
jid="account11@jcl.test.com") jid="account11@jcl.test.com")
account11.default_account = True
account12 = SMTPAccount(user_jid="user1@test.com", account12 = SMTPAccount(user_jid="user1@test.com",
name="account12", name="account12",
jid="account12@jcl.test.com") jid="account12@jcl.test.com")
@@ -578,6 +579,22 @@ class RootSendMailMessageHandler_TestCase(unittest.TestCase):
self.assertEquals(accounts.count(), 1) self.assertEquals(accounts.count(), 1)
del account.hub.threadConnection del account.hub.threadConnection
def test_filter_no_default_account(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
account11 = SMTPAccount(user_jid="user1@test.com",
name="account11",
jid="account11@jcl.test.com")
account12 = SMTPAccount(user_jid="user1@test.com",
name="account12",
jid="account12@jcl.test.com")
message = Message(from_jid="user1@test.com",
to_jid="account11@jcl.test.com",
body="message")
accounts = self.handler.filter(message, None)
self.assertEquals(accounts.count(), 2)
self.assertEquals(accounts[0].name, "account11")
del account.hub.threadConnection
def test_filter_wrong_dest(self): def test_filter_wrong_dest(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
account11 = SMTPAccount(user_jid="user1@test.com", account11 = SMTPAccount(user_jid="user1@test.com",
@@ -590,7 +607,7 @@ class RootSendMailMessageHandler_TestCase(unittest.TestCase):
to_jid="user2%test.com@jcl.test.com", to_jid="user2%test.com@jcl.test.com",
body="message") body="message")
accounts = self.handler.filter(message, None) accounts = self.handler.filter(message, None)
self.assertEquals(accounts.count(), 0) self.assertEquals(accounts.count(), 2)
del account.hub.threadConnection del account.hub.threadConnection
def test_filter_wrong_user(self): def test_filter_wrong_user(self):

View File

@@ -578,8 +578,7 @@ class SMTPAccount(Account):
real_class = cls real_class = cls
return Account.get_register_fields(real_class) + \ return Account.get_register_fields(real_class) + \
[("login", "text-single", None, [("login", "text-single", None,
lambda field_value, default_func: \ account.default_post_func,
account.mandatory_field(field_value),
lambda : ""), lambda : ""),
("password", "text-private", None, password_post_func, ("password", "text-private", None, password_post_func,
lambda : ""), lambda : ""),
@@ -599,7 +598,10 @@ class SMTPAccount(Account):
lambda : ""), lambda : ""),
("store_password", "boolean", None, ("store_password", "boolean", None,
account.default_post_func, account.default_post_func,
lambda : True)] lambda : True),
("default_account", "boolean", None,
account.default_post_func,
lambda : False)]
get_register_fields = classmethod(_get_register_fields) get_register_fields = classmethod(_get_register_fields)

View File

@@ -391,7 +391,7 @@ class IMAPAccount_TestCase(unittest.TestCase):
class SMTPAccount_TestCase(Account_TestCase): class SMTPAccount_TestCase(Account_TestCase):
def test_get_register_fields(self): def test_get_register_fields(self):
register_fields = SMTPAccount.get_register_fields() register_fields = SMTPAccount.get_register_fields()
self.assertEquals(len(register_fields), 7) self.assertEquals(len(register_fields), 8)
def suite(): def suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()