Merge branch 'weblate-gnu-mailman-postorius' into 'master'
[mailman-postorious.git] / src / postorius / tests / mailman_api_tests / test_membership_helpers.py
blob9b78e2e5dfce5bce8ece871cb833522ce3e198f9
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2017-2023 by the Free Software Foundation, Inc.
4 # This file is part of Postorius.
6 # Postorius is free software: you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option)
9 # any later version.
11 # Postorius is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 # more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Postorius. If not, see <http://www.gnu.org/licenses/>.
20 from django.contrib.auth.models import User
22 from allauth.account.models import EmailAddress
23 from mailmanclient import MailingList
25 from postorius.models import Mailman404Error
26 from postorius.templatetags.membership_helpers import (
27 get_list,
28 user_is_list_moderator,
29 user_is_list_owner,
31 from postorius.tests.utils import ViewTestCase
34 class TestMembershipHelpers(ViewTestCase):
35 def setUp(self):
36 super(TestMembershipHelpers, self).setUp()
37 # Create a domain.
38 self.domain = self.mm_client.create_domain('example.com')
39 self.mlist = self.domain.create_list('test_list')
40 self.test_user = User.objects.create_user(
41 'test_user', 'test_user@example.com', 'pwd'
43 self.test_superuser = User.objects.create_superuser(
44 'test_superuser', 'test_superuser@example.com', 'pwd'
46 self.test_owner = User.objects.create_user(
47 'testowner', 'owner@example.com', 'testpass'
49 self.test_moderator = User.objects.create_user(
50 'testmoderator', 'moderator@example.com', 'testpass'
52 for user in (
53 self.test_user,
54 self.test_superuser,
55 self.test_owner,
56 self.test_moderator,
58 EmailAddress.objects.create(
59 user=user, email=user.email, verified=True
61 self.mlist.add_owner(self.test_owner.email)
62 self.mlist.add_moderator(self.test_moderator.email)
64 def tearDown(self):
65 self.domain.delete()
66 for user in (
67 self.test_user,
68 self.test_superuser,
69 self.test_owner,
70 self.test_moderator,
72 user.delete()
74 def test_get_list(self):
75 # Given the list's posting address, get_list should return the
76 # MailingList object.
77 mlist = get_list('test_list@example.com')
78 self.assertTrue(isinstance(mlist, MailingList))
79 self.assertEqual(mlist.fqdn_listname, 'test_list@example.com')
80 # Given a list's fqdn, it should be possible to get the mlist too.
81 mlist = get_list('test_list.example.com')
82 self.assertTrue(isinstance(mlist, MailingList))
83 self.assertEqual(mlist.fqdn_listname, 'test_list@example.com')
84 # Given a MailingList object, make sure it is returned back.
85 res = get_list(mlist)
86 self.assertTrue(res is mlist)
87 # Given wrong MailingList identifier should raise proper error.
88 # TODO (maxking): Ideally, this should raise a validation error, given
89 # that the MailingList identifier is wrong. However, given the error
90 # propagation right now, it doesn't seem to be possible.
91 with self.assertRaises(Mailman404Error):
92 mlist = get_list('example.com')
94 def test_user_is_list_owner(self):
95 # First, it should return false as the user is not a list owner.
96 self.assertFalse(user_is_list_owner(self.test_user, self.mlist))
97 # It should return True for the list owner.
98 self.assertTrue(user_is_list_owner(self.test_owner, self.mlist))
99 # It should return False for the list moderator.
100 self.assertFalse(user_is_list_owner(self.test_moderator, self.mlist))
101 # Now let's add the test_user as an owner of the list.
102 self.mlist.add_owner(self.test_user.email)
103 self.assertTrue(user_is_list_owner(self.test_user, self.mlist))
105 def test_user_is_list_moderator(self):
106 # First, it should return false as the user is not a list moderator.
107 self.assertFalse(user_is_list_moderator(self.test_user, self.mlist))
108 # It should return False for the list owner.
109 self.assertFalse(user_is_list_moderator(self.test_owner, self.mlist))
110 # It should return False for the list moderator.
111 self.assertTrue(
112 user_is_list_moderator(self.test_moderator, self.mlist)
114 # Now let's add the test_user as an owner of the list.
115 self.mlist.add_moderator(self.test_user.email)
116 self.assertTrue(user_is_list_moderator(self.test_user, self.mlist))