Fixed Account get_id method to generate id from account name only.

Previous implementation used attributes from Account class which were not supported in all mailbox types, for example mbox and maildir do not have user or server attributes.
This commit is contained in:
Timo Kankare
2018-05-01 15:35:04 +03:00
parent f649f4b48b
commit 5e332c9c9d
2 changed files with 26 additions and 6 deletions

View File

@@ -5,7 +5,7 @@
#
# Copyright 2011 - 2017 Patrick Ulbrich <zulu99@gmx.net>
# Copyright 2016 Thomas Haider <t.haider@deprecate.de>
# Copyright 2016 Timo Kankare <timo.kankare@iki.fi>
# Copyright 2016, 2018 Timo Kankare <timo.kankare@iki.fi>
# Copyright 2011 Ralf Hersel <ralf.hersel@gmx.net>
#
# This program is free software; you can redistribute it and/or modify
@@ -138,11 +138,12 @@ class Account:
Returns an empty list if mailbox does not support folders.
"""
return self._get_backend().request_folders()
def get_id(self):
# TODO : this id is not really unique...
return str(hash(self.user + self.server + ', '.join(self.folders)))
"""Returns unique id for the account."""
# Assumption: The name of the account is unique.
return str(hash(self.name))
def _get_backend(self):

View File

@@ -2,7 +2,7 @@
#
# test_account.py
#
# Copyright 2016 Timo Kankare <timo.kankare@iki.fi>
# Copyright 2016, 2018 Timo Kankare <timo.kankare@iki.fi>
#
# 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
@@ -25,6 +25,25 @@
from Mailnag.common.accounts import Account
def test_account_get_id_should_be_unique():
accounts = [
Account(name='a', mailbox_type='imap', enabled=True, user='x', server='xx'),
Account(name='b', mailbox_type='pop3', enabled=True, user='y', server='yy'),
Account(name='c', mailbox_type='mbox', enabled=True),
Account(name='d', mailbox_type='maildir', enabled=True),
]
ids = set(acc.get_id() for acc in accounts)
assert len(ids) == len(accounts)
def test_account_get_id_should_be_consistent():
account = Account(name='a', mailbox_type='imap', enabled=True, user='x', server='xx')
expected_id = account.get_id()
for i in range(20):
assert account.get_id() == expected_id
def test_account_should_keep_configuration():
account = Account(enabled=True,
name='my name',