Bump copyright year
[mailman-postorious.git] / src / postorius / tests / mailman_api_tests / test_domain_delete.py
blob59eef1ffa684dcacd295bc54a1833b8f4895f821
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2016-2022 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/>.
19 from django.contrib.auth.models import User
20 from django.contrib.sites.models import Site
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 DomainDeleteTest(ViewTestCase):
30 """Tests for the domain delete page."""
32 def setUp(self):
33 super(DomainDeleteTest, self).setUp()
34 self.domain = self.mm_client.create_domain('example.com')
35 self.foo_list = self.domain.create_list('foo')
37 self.user = User.objects.create_user(
38 'testuser', 'test@example.com', 'testpass')
39 self.superuser = User.objects.create_superuser(
40 'testsu', 'su@example.com', 'testpass')
41 self.owner = User.objects.create_user(
42 'testowner', 'owner@example.com', 'testpass')
43 self.moderator = User.objects.create_user(
44 'testmoderator', 'moderator@example.com', 'testpass')
45 for user in (self.user, self.superuser, self.owner, self.moderator):
46 EmailAddress.objects.create(
47 user=user, email=user.email, verified=True)
48 self.foo_list.add_owner('owner@example.com')
49 self.foo_list.add_moderator('moderator@example.com')
50 MailDomain.objects.create(
51 site=Site.objects.get_current(), mail_domain='example.com')
52 self.url = reverse('domain_delete', args=['example.com'])
54 def test_access_anonymous(self):
55 # Anonymous users users can't delete domains
56 self.assertRedirectsToLogin(self.url)
58 def test_access_basic_user(self):
59 # Basic users can't delete domains
60 self.client.login(username='testuser', password='testpass')
61 response = self.client.get(self.url)
62 self.assertEqual(response.status_code, 403)
64 def test_access_moderators(self):
65 # Moderators can't delete domains
66 self.client.login(username='testmoderator', password='testpass')
67 response = self.client.get(self.url)
68 self.assertEqual(response.status_code, 403)
70 def test_access_owners(self):
71 # Owners can't delete domains
72 self.client.login(username='testowner', password='testpass')
73 response = self.client.get(self.url)
74 self.assertEqual(response.status_code, 403)
76 def test_domain_delete_confirm(self):
77 # The user should be ask to confirm domain deletion on GET requests
78 self.client.login(username='testsu', password='testpass')
79 response = self.client.get(self.url)
80 self.assertEqual(response.status_code, 200)
81 self.assertEqual(len(self.mm_client.domains), 1)
82 self.assertEqual(len(self.mm_client.lists), 1)
84 def test_domain_delete_page_lists_all_mailinglists(self):
85 # Test that the domain delete page lists all the mailing lists
86 # associated with the domain.
87 self.client.login(username='testsu', password='testpass')
88 response = self.client.get(self.url)
89 self.assertEqual(response.status_code, 200)
90 self.assertTrue(
91 'This would delete 1 lists, some of which are'
92 in str(response.content))
93 self.assertTrue('foo@example.com' in str(response.content))
95 def test_domain_delete(self):
96 # The domain should be deleted
97 self.client.login(username='testsu', password='testpass')
98 response = self.client.post(self.url)
99 self.assertRedirects(response, reverse('domain_index'))
100 self.assertEqual(len(self.mm_client.domains), 0)
101 self.assertEqual(len(self.mm_client.lists), 0)
102 self.assertHasSuccessMessage(response)
103 self.assertFalse(
104 MailDomain.objects.filter(mail_domain='example.com').exists())