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_new.py
blob721e09684fda85c5fd52513321985ee07343c6a7
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/>.
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 DomainCreationTest(ViewTestCase):
30 """Tests for the new list page."""
32 def setUp(self):
33 super(DomainCreationTest, self).setUp()
34 self.user = User.objects.create_user('user', 'user@example.com', 'pwd')
35 self.superuser = User.objects.create_superuser('su', 'su@example.com',
36 'pwd')
37 for user in (self.user, self.superuser):
38 EmailAddress.objects.create(
39 user=user, email=user.email, verified=True)
41 def test_permission_denied(self):
42 self.client.login(username='user', password='pwd')
43 response = self.client.get(reverse('domain_new'))
44 self.assertEqual(response.status_code, 403)
46 def test_new_domain_created_with_owner(self):
47 self.client.login(username='su', password='pwd')
48 post_data = {'mail_host': 'example.com',
49 'description': 'A new Domain.',
50 'site': '1',
52 response = self.client.post(reverse('domain_new'), post_data)
54 self.assertHasSuccessMessage(response)
55 self.assertRedirects(response, reverse('domain_index'))
57 a_new_domain = self.mm_client.get_domain('example.com')
58 self.assertEqual(a_new_domain.mail_host, 'example.com')
59 self.assertEqual(a_new_domain.owners[0].user_id,
60 self.mm_client.get_user('su@example.com').user_id)
61 self.assertTrue(
62 MailDomain.objects.filter(mail_domain='example.com').exists())
63 a_new_domain.delete()
65 def test_validation_of_mail_host(self):
66 self.client.login(username='su', password='pwd')
67 post_data = {'mail_host': 'example com',
68 'description': 'A new Domain',
69 'site': '1',
71 response = self.client.post(reverse('domain_new'), post_data)
72 self.assertContains(response, 'Please enter a valid domain name')
73 # self.assertHasErrorMessage(response)
74 self.assertEqual(response.status_code, 200)
76 def test_only_redirect_if_domain_creation_is_successful(self):
77 self.client.login(username='su', password='pwd')
78 post_data = {'mail_host': 'example.com',
79 'site': '1',
80 'description': 'A new Domain.'}
81 response = self.client.post(reverse('domain_new'), post_data)
82 self.assertEqual(response.status_code, 302)
83 self.assertHasSuccessMessage(response)
84 response = self.client.post(reverse('domain_new'), post_data)
85 self.assertEqual(response.status_code, 200)
86 self.assertIsNotNone(response.context['form'].errors)
88 def test_new_domain_updates_site(self):
89 # Test that creating a brand new domain updates the associated Site if
90 # it is example.com
91 self.client.login(username='su', password='pwd')
92 site = Site.objects.get(pk=1)
93 self.assertEqual(site.domain, 'example.com')
94 self.assertEqual(site.name, 'example.com')
95 # Any domain other than example.com will update the Site object.
96 post_data = {'mail_host': 'otherthan.example.com',
97 'site': '1',
98 'description': 'A new Domain.'}
99 response = self.client.post(reverse('domain_new'), post_data)
100 self.assertEqual(response.status_code, 302)
101 self.assertHasSuccessMessage(response)
102 # Get the Site id 1, which was used above.
103 site = Site.objects.get(pk=1)
104 self.assertEqual(site.domain, 'otherthan.example.com')
105 self.assertEqual(site.name, 'A new Domain.')
107 def test_new_domain_does_not_update_site_when_custom(self):
108 # Test that when default Site isn't example.com, we don't change it.
109 self.client.login(username='su', password='pwd')
110 site = Site.objects.get(pk=1)
111 site.domain = 'otherthan.example.com'
112 site.name = 'Something other than Example.com'
113 site.save()
114 # Now, let's create a new Domain and check if the Site object is
115 # udpated.
116 post_data = {'mail_host': 'aexample.com',
117 'site': '1',
118 'description': 'A new Domain.'}
119 response = self.client.post(reverse('domain_new'), post_data)
120 self.assertEqual(response.status_code, 302)
121 self.assertHasSuccessMessage(response)
122 # Get the site object from database.
123 site = Site.objects.get(pk=1)
124 self.assertEqual(site.domain, 'otherthan.example.com')
125 self.assertEqual(site.name, 'Something other than Example.com')