Implement default SMTP account unicity

darcs-hash:20070612055200-86b55-c6c26309c503c9e5c8a8540223795d885dd405f2.gz
This commit is contained in:
David Rousselie
2007-06-12 07:52:00 +02:00
parent e59e91b2ad
commit d18117cd79
2 changed files with 128 additions and 15 deletions

View File

@@ -33,6 +33,7 @@ import socket
from sqlobject.inheritance import InheritableSQLObject from sqlobject.inheritance import InheritableSQLObject
from sqlobject.col import StringCol, IntCol, BoolCol from sqlobject.col import StringCol, IntCol, BoolCol
from sqlobject.sqlbuilder import AND
from jcl.model import account from jcl.model import account
from jcl.model.account import Account, PresenceAccount from jcl.model.account import Account, PresenceAccount
@@ -569,6 +570,36 @@ class SMTPAccount(Account):
return None return None
return password return password
def default_account_default_func(bare_from_jid):
accounts = SMTPAccount.select(\
AND(SMTPAccount.q.default_account == True,
SMTPAccount.q.user_jid == bare_from_jid))
if accounts.count() == 0:
return True
else:
return False
def default_account_post_func(value, default_func, bare_from_jid):
accounts = SMTPAccount.select(\
AND(SMTPAccount.q.default_account == True,
SMTPAccount.q.user_jid == bare_from_jid))
already_default_account = (accounts.count() != 0)
if isinstance(value, str):
value = value.lower()
bool_value = (value == "true" or value == "1")
else:
bool_value = value
if bool_value:
if already_default_account:
for _account in accounts:
_account.default_account = False
return True
else:
if not already_default_account:
return True
else:
return False
if real_class is None: if real_class is None:
real_class = cls real_class = cls
return Account.get_register_fields(real_class) + \ return Account.get_register_fields(real_class) + \
@@ -595,8 +626,8 @@ class SMTPAccount(Account):
account.default_post_func, account.default_post_func,
lambda bare_from_jid: True), lambda bare_from_jid: True),
("default_account", "boolean", None, ("default_account", "boolean", None,
account.default_post_func, default_account_post_func,
lambda bare_from_jid: False)] default_account_default_func)]
get_register_fields = classmethod(_get_register_fields) get_register_fields = classmethod(_get_register_fields)

View File

@@ -34,7 +34,8 @@ from jcl.model.account import Account, PresenceAccount
from jmc.model.account import MailAccount, POP3Account, IMAPAccount, SMTPAccount from jmc.model.account import MailAccount, POP3Account, IMAPAccount, SMTPAccount
from jcl.model.tests.account import Account_TestCase, \ from jcl.model.tests.account import Account_TestCase, \
PresenceAccount_TestCase, InheritableAccount_TestCase PresenceAccount_TestCase, InheritableAccount_TestCase, \
ExampleAccount
from jmc.model.tests import email_generator, server from jmc.model.tests import email_generator, server
if sys.platform == "win32": if sys.platform == "win32":
@@ -47,7 +48,8 @@ class MailAccount_TestCase(PresenceAccount_TestCase):
def setUp(self): def setUp(self):
if os.path.exists(DB_PATH): if os.path.exists(DB_PATH):
os.unlink(DB_PATH) os.unlink(DB_PATH)
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) self.db_url = DB_URL
account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
Account.createTable(ifNotExists = True) Account.createTable(ifNotExists = True)
PresenceAccount.createTable(ifNotExists = True) PresenceAccount.createTable(ifNotExists = True)
MailAccount.createTable(ifNotExists = True) MailAccount.createTable(ifNotExists = True)
@@ -58,11 +60,11 @@ class MailAccount_TestCase(PresenceAccount_TestCase):
self.account_class = MailAccount self.account_class = MailAccount
def tearDown(self): def tearDown(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
MailAccount.dropTable(ifExists = True) MailAccount.dropTable(ifExists = True)
PresenceAccount.dropTable(ifExists = True) PresenceAccount.dropTable(ifExists = True)
Account.dropTable(ifExists = True) Account.dropTable(ifExists = True)
del TheURIOpener.cachedURIs['sqlite://' + DB_URL] del TheURIOpener.cachedURIs['sqlite://' + self.db_url]
account.hub.threadConnection.close() account.hub.threadConnection.close()
del account.hub.threadConnection del account.hub.threadConnection
if os.path.exists(DB_PATH): if os.path.exists(DB_PATH):
@@ -150,7 +152,8 @@ class POP3Account_TestCase(InheritableAccount_TestCase):
def setUp(self): def setUp(self):
if os.path.exists(DB_PATH): if os.path.exists(DB_PATH):
os.unlink(DB_PATH) os.unlink(DB_PATH)
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) self.db_url = DB_URL
account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
Account.createTable(ifNotExists = True) Account.createTable(ifNotExists = True)
PresenceAccount.createTable(ifNotExists = True) PresenceAccount.createTable(ifNotExists = True)
MailAccount.createTable(ifNotExists = True) MailAccount.createTable(ifNotExists = True)
@@ -167,12 +170,12 @@ class POP3Account_TestCase(InheritableAccount_TestCase):
self.account_class = POP3Account self.account_class = POP3Account
def tearDown(self): def tearDown(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
POP3Account.dropTable(ifExists = True) POP3Account.dropTable(ifExists = True)
MailAccount.dropTable(ifExists = True) MailAccount.dropTable(ifExists = True)
PresenceAccount.dropTable(ifExists = True) PresenceAccount.dropTable(ifExists = True)
Account.dropTable(ifExists = True) Account.dropTable(ifExists = True)
del TheURIOpener.cachedURIs['sqlite://' + DB_URL] del TheURIOpener.cachedURIs['sqlite://' + self.db_url]
account.hub.threadConnection.close() account.hub.threadConnection.close()
del account.hub.threadConnection del account.hub.threadConnection
if os.path.exists(DB_PATH): if os.path.exists(DB_PATH):
@@ -198,7 +201,7 @@ class POP3Account_TestCase(InheritableAccount_TestCase):
self.failUnless(self.pop3_account.connection, \ self.failUnless(self.pop3_account.connection, \
"Cannot establish connection") "Cannot establish connection")
if core: if core:
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
core(self) core(self)
del account.hub.threadConnection del account.hub.threadConnection
self.pop3_account.disconnect() self.pop3_account.disconnect()
@@ -277,7 +280,8 @@ class IMAPAccount_TestCase(InheritableAccount_TestCase):
def setUp(self): def setUp(self):
if os.path.exists(DB_PATH): if os.path.exists(DB_PATH):
os.unlink(DB_PATH) os.unlink(DB_PATH)
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) self.db_url = DB_URL
account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
Account.createTable(ifNotExists = True) Account.createTable(ifNotExists = True)
PresenceAccount.createTable(ifNotExists = True) PresenceAccount.createTable(ifNotExists = True)
MailAccount.createTable(ifNotExists = True) MailAccount.createTable(ifNotExists = True)
@@ -294,12 +298,12 @@ class IMAPAccount_TestCase(InheritableAccount_TestCase):
self.account_class = IMAPAccount self.account_class = IMAPAccount
def tearDown(self): def tearDown(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
IMAPAccount.dropTable(ifExists = True) IMAPAccount.dropTable(ifExists = True)
MailAccount.dropTable(ifExists = True) MailAccount.dropTable(ifExists = True)
PresenceAccount.dropTable(ifExists = True) PresenceAccount.dropTable(ifExists = True)
Account.dropTable(ifExists = True) Account.dropTable(ifExists = True)
del TheURIOpener.cachedURIs['sqlite://' + DB_URL] del TheURIOpener.cachedURIs['sqlite://' + self.db_url]
account.hub.threadConnection.close() account.hub.threadConnection.close()
del account.hub.threadConnection del account.hub.threadConnection
if os.path.exists(DB_PATH): if os.path.exists(DB_PATH):
@@ -330,7 +334,7 @@ class IMAPAccount_TestCase(InheritableAccount_TestCase):
self.failUnless(self.imap_account.connection, \ self.failUnless(self.imap_account.connection, \
"Cannot establish connection") "Cannot establish connection")
if core: if core:
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
core(self) core(self)
del account.hub.threadConnection del account.hub.threadConnection
self.imap_account.disconnect() self.imap_account.disconnect()
@@ -381,9 +385,87 @@ class IMAPAccount_TestCase(InheritableAccount_TestCase):
class SMTPAccount_TestCase(Account_TestCase): class SMTPAccount_TestCase(Account_TestCase):
def setUp(self): def setUp(self):
Account_TestCase.setUp(self) if os.path.exists(DB_PATH):
os.unlink(DB_PATH)
self.db_url = DB_URL
account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
Account.createTable(ifNotExists = True)
ExampleAccount.createTable(ifNotExists = True)
SMTPAccount.createTable(ifNotExists = True)
del account.hub.threadConnection
self.account_class = SMTPAccount self.account_class = SMTPAccount
def tearDown(self):
account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
SMTPAccount.dropTable(ifExists = True)
ExampleAccount.dropTable(ifExists = True)
Account.dropTable(ifExists = True)
del TheURIOpener.cachedURIs['sqlite://' + self.db_url]
account.hub.threadConnection.close()
del account.hub.threadConnection
if os.path.exists(DB_PATH):
os.unlink(DB_PATH)
def test_default_account_post_func_no_default_true(self):
account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
account11 = SMTPAccount(user_jid="user1@test.com",
name="account11",
jid="account11@jmc.test.com")
account12 = SMTPAccount(user_jid="user1@test.com",
name="account12",
jid="account12@jmc.test.com")
(name, field_type, field_options, post_func, default_func) = \
SMTPAccount.get_register_fields()[7]
value = post_func("True", None, "user1@test.com")
self.assertTrue(value)
del account.hub.threadConnection
def test_default_account_post_func_no_default_false(self):
account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
account11 = SMTPAccount(user_jid="user1@test.com",
name="account11",
jid="account11@jmc.test.com")
account12 = SMTPAccount(user_jid="user1@test.com",
name="account12",
jid="account12@jmc.test.com")
(name, field_type, field_options, post_func, default_func) = \
SMTPAccount.get_register_fields()[7]
value = post_func("False", None, "user1@test.com")
self.assertTrue(value)
del account.hub.threadConnection
def test_default_account_post_func_true(self):
account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
account11 = SMTPAccount(user_jid="user1@test.com",
name="account11",
jid="account11@jmc.test.com")
account12 = SMTPAccount(user_jid="user1@test.com",
name="account12",
jid="account12@jmc.test.com")
account12.default_account = True
(name, field_type, field_options, post_func, default_func) = \
SMTPAccount.get_register_fields()[7]
value = post_func("True", None, "user1@test.com")
self.assertTrue(value)
self.assertFalse(account12.default_account)
del account.hub.threadConnection
def test_default_account_post_func_false(self):
account.hub.threadConnection = connectionForURI('sqlite://' + self.db_url)
account11 = SMTPAccount(user_jid="user1@test.com",
name="account11",
jid="account11@jmc.test.com")
account12 = SMTPAccount(user_jid="user1@test.com",
name="account12",
jid="account12@jmc.test.com")
account12.default_account = True
(name, field_type, field_options, post_func, default_func) = \
SMTPAccount.get_register_fields()[7]
value = post_func("False", None, "user1@test.com")
self.assertFalse(value)
self.assertTrue(account12.default_account)
del account.hub.threadConnection
def suite(): def suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(MailAccount_TestCase, 'test')) suite.addTest(unittest.makeSuite(MailAccount_TestCase, 'test'))