Bump version to 1.3.6 and update NEWS.rst for 1.3.5
[mailman-postorious.git] / src / postorius / tests / mailman_api_tests / test_domain_index.py
blob65d4255d4521395b26a21ea1a496c7589ba357ce
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2012-2021 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.
10 # Postorius is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
15 # You should have received a copy of the GNU General Public License along with
16 # Postorius. If not, see <http://www.gnu.org/licenses/>.
18 from urllib.error import HTTPError
20 from django.contrib.auth.models import User
21 from django.urls import reverse
23 from allauth.account.models import EmailAddress
24 from django_mailman3.models import MailDomain
26 from postorius.tests.utils import ViewTestCase
29 class DomainIndexPageTest(ViewTestCase):
30 """Tests for the list index page."""
32 def setUp(self):
33 super(DomainIndexPageTest, self).setUp()
34 self.domain = self.mm_client.create_domain('example.com')
35 self.domain.add_owner('person@domain.com')
36 try:
37 self.foo_list = self.domain.create_list('foo')
38 except HTTPError:
39 self.foo_list = self.mm_client.get_list('foo.example.com')
41 self.user = User.objects.create_user(
42 'testuser', 'test@example.com', 'testpass')
43 self.superuser = User.objects.create_superuser(
44 'testsu', 'su@example.com', 'testpass')
45 self.owner = User.objects.create_user(
46 'testowner', 'owner@example.com', 'testpass')
47 self.moderator = User.objects.create_user(
48 'testmoderator', 'moderator@example.com', 'testpass')
49 for user in (self.user, self.superuser, self.owner, self.moderator):
50 EmailAddress.objects.create(
51 user=user, email=user.email, verified=True)
52 self.foo_list.add_owner('owner@example.com')
53 self.foo_list.add_moderator('moderator@example.com')
55 def _test_not_accesible_to_public(self, url):
56 response = self.client.get(url)
57 self.assertEqual(response.status_code, 302)
59 def _test_not_accessible_to_unpriveleged_use(self, url):
60 self.client.login(username='testuser', password='testpass')
61 response = self.client.get(url)
62 self.assertEqual(response.status_code, 403)
64 def _test_not_accessible_to_moderators(self, url):
65 self.client.login(username='testmoderator', password='testpass')
66 response = self.client.get(url)
67 self.assertEqual(response.status_code, 403)
69 def _test_not_accessible_to_owner(self, url):
70 self.client.login(username='testowner', password='testpass')
71 response = self.client.get(url)
72 self.assertEqual(response.status_code, 403)
74 def test_domain_index_not_accessible_to_public(self):
75 self._test_not_accesible_to_public(reverse('domain_index'))
77 def test_domain_index_not_accessible_to_unpriveleged_user(self):
78 self._test_not_accessible_to_unpriveleged_use(reverse('domain_index'))
80 def test_domain_index_not_accessible_to_moderators(self):
81 self._test_not_accessible_to_moderators(reverse('domain_index'))
83 def test_domain_index_not_accessible_to_owners(self):
84 self._test_not_accessible_to_owner(reverse('domain_index'))
86 def test_contains_domains_and_site(self):
87 # The list index page should contain the lists
88 self.client.login(username='testsu', password='testpass')
89 response = self.client.get(reverse('domain_index'))
90 self.assertEqual(response.status_code, 200)
91 self.assertEqual(len(response.context['domains']), 1)
92 self.assertContains(response, 'example.com')
93 self.assertTrue(
94 MailDomain.objects.filter(mail_domain='example.com').exists())
95 # Test there are owners are listed.
96 self.assertContains(response, 'person@domain.com')
98 def test_domain_add_owner_not_acceesible_to_anyone_but_superuser(self):
99 url = reverse('domain_owners', args=(self.domain.mail_host,))
100 self._test_not_accesible_to_public(url)
101 self._test_not_accessible_to_unpriveleged_use(url)
102 self._test_not_accessible_to_moderators(url)
103 self._test_not_accessible_to_owner(url)
104 self.client.login(username='testsu', password='testpass')
105 response = self.client.get(url)
106 self.assertEqual(response.status_code, 200)
107 self.assertContains(response, b"Add a new owner to example.com")
108 response = self.client.post(
109 url,
110 dict(email='person@example.com'))
111 self.assertEqual(response.status_code, 302)
112 self.assertIn(
113 'person@example.com',
114 [owner.addresses[0].email for owner in self.domain.owners])
116 def test_domain_delete_owner_not_acceesible_to_anyone_but_superuser(self):
117 self.domain.add_owner('one@example.com')
118 self.domain.add_owner('two@example.com')
119 url = reverse('remove_domain_owner',
120 args=(self.domain.mail_host,
121 'person@domain.com'))
122 self.client.login(username='testsu', password='testpass')
123 response = self.client.post(url)
124 self.assertEqual(response.status_code, 302)
125 self.assertTrue(len(self.domain.owners), 2)
126 self.assertEqual(sorted(owner.addresses[0].email
127 for owner in self.domain.owners),
128 ['one@example.com', 'two@example.com'])