From 5e332c9c9d4ffc93b115e7f4145b2c5772519d84 Mon Sep 17 00:00:00 2001 From: Timo Kankare Date: Tue, 1 May 2018 15:35:04 +0300 Subject: [PATCH] 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. --- Mailnag/common/accounts.py | 11 ++++++----- tests/test_account.py | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Mailnag/common/accounts.py b/Mailnag/common/accounts.py index f36b813..7cfc584 100644 --- a/Mailnag/common/accounts.py +++ b/Mailnag/common/accounts.py @@ -5,7 +5,7 @@ # # Copyright 2011 - 2017 Patrick Ulbrich # Copyright 2016 Thomas Haider -# Copyright 2016 Timo Kankare +# Copyright 2016, 2018 Timo Kankare # Copyright 2011 Ralf Hersel # # 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): diff --git a/tests/test_account.py b/tests/test_account.py index 9c08e17..4caa0cd 100644 --- a/tests/test_account.py +++ b/tests/test_account.py @@ -2,7 +2,7 @@ # # test_account.py # -# Copyright 2016 Timo Kankare +# Copyright 2016, 2018 Timo Kankare # # 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',