summaryrefslogtreecommitdiff
path: root/src/mailman/model/tests/test_preferences.py
blob: 9298335954f272d272ac05287bf131ef2026f136 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Copyright (C) 2015 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Test preferences."""

__all__ = [
    'TestPreferences',
    ]


import unittest

from mailman.app.lifecycle import create_list
from mailman.config import config
from mailman.database.transaction import transaction
from mailman.interfaces.languages import ILanguageManager
from mailman.interfaces.member import DeliveryMode, DeliveryStatus
from mailman.interfaces.usermanager import IUserManager
from mailman.model.preferences import Preferences
from mailman.testing.layers import ConfigLayer
from zope.component import getUtility



class TestPreferences(unittest.TestCase):
    """Test preferences."""

    layer = ConfigLayer

    def setUp(self):
        self._manager = getUtility(IUserManager)
        with transaction():
            # This has to happen in a transaction so that both the user and
            # the preferences objects get valid ids.
            self._mlist = create_list('test@example.com')
            self._anne = self._manager.create_user(
                'anne@example.com', 'Anne Person')
            self._bill = self._manager.create_user(
                'bill@example.com', 'Bill Person')

    def test_absorb_all_attributes(self):
        attributes = {
            'acknowledge_posts': True,
            'hide_address': True,
            'preferred_language':
                getUtility(ILanguageManager)['fr'],
            'receive_list_copy': True,
            'receive_own_postings': True,
            'delivery_mode': DeliveryMode.mime_digests,
            'delivery_status': DeliveryStatus.by_user,
            }
        bill_prefs = self._bill.preferences
        for name, value in attributes.items():
            setattr(bill_prefs, name, value)
        self._anne.preferences.absorb(self._bill.preferences)
        for name, value in attributes.items():
            self.assertEqual(getattr(self._anne.preferences, name), value)

    def test_absorb_overwrite(self):
        # Only overwrite the pref if it is unset in the absorber
        anne_prefs = self._anne.preferences
        bill_prefs = self._bill.preferences
        self.assertIsNone(self._anne.preferences.acknowledge_posts)
        self.assertIsNone(self._anne.preferences.hide_address)
        self.assertIsNone(self._anne.preferences.receive_list_copy)
        anne_prefs.acknowledge_posts = False
        bill_prefs.acknowledge_posts = True
        anne_prefs.hide_address = True
        bill_prefs.receive_list_copy = True
        self._anne.preferences.absorb(self._bill.preferences)
        # set for both anne and bill, don't overwrite
        self.assertFalse(self._anne.preferences.acknowledge_posts)
        # set only for anne
        self.assertTrue(self._anne.preferences.hide_address)
        # set only for bill, overwrite anne's default value
        self.assertTrue(self._anne.preferences.receive_list_copy)