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)
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
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."""
33 super(DomainIndexPageTest
, self
).setUp()
34 self
.domain
= self
.mm_client
.create_domain('example.com')
35 self
.domain
.add_owner('person@domain.com')
37 self
.foo_list
= self
.domain
.create_list('foo')
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')
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(
110 dict(email
='person@example.com'))
111 self
.assertEqual(response
.status_code
, 302)
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'])