1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2017-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)
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
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
.sites
.models
import Site
21 from django
.test
import TestCase
23 from postorius
.forms
.domain_forms
import (
24 DomainEditForm
, DomainForm
, DomainOwnerForm
)
27 class TestDomainEditForm(TestCase
):
28 def test_form_does_not_contain_mail_host(self
):
29 form
= DomainEditForm()
30 self
.assertTrue('description' in form
.fields
)
31 self
.assertTrue('alias_domain' in form
.fields
)
32 self
.assertTrue('site' in form
.fields
)
33 self
.assertFalse('mail_host' in form
.fields
)
36 class TestDomainOwnerAddForm(TestCase
):
37 def test_form_sanity(self
):
38 form
= DomainOwnerForm(dict(email
='some'))
39 self
.assertFalse(form
.is_valid())
40 self
.assertIn('email', form
.errors
)
41 form
= DomainOwnerForm(dict(email
='some@example.com'))
42 self
.assertTrue(form
.is_valid())
45 class TestDomainForm(TestCase
):
46 def test_form_contains_mail_host(self
):
48 self
.assertTrue('description' in form
.fields
)
49 self
.assertTrue('alias_domain' in form
.fields
)
50 self
.assertTrue('site' in form
.fields
)
51 self
.assertTrue('mail_host' in form
.fields
)
53 def test_form_labels(self
):
55 self
.assertTrue(form
.fields
['mail_host'].label
== 'Mail Host')
56 self
.assertTrue(form
.fields
['description'].label
== 'Description')
57 self
.assertTrue(form
.fields
['alias_domain'].label
== 'Alias Domain')
58 self
.assertTrue(form
.fields
['site'].label
== 'Web Host')
60 def test_error_messages(self
):
62 'mail_host': 'mailman.most-desirable.org',
64 self
.assertFalse(form
.is_valid())
65 self
.assertTrue('site' in form
.errors
.keys())
66 self
.assertEqual(form
.errors
['site'][0], 'Please enter a domain name')
67 form
= DomainForm({'site': 1})
68 self
.assertFalse(form
.is_valid())
69 self
.assertTrue('mail_host' in form
.errors
.keys())
70 self
.assertEqual(form
.errors
['mail_host'][0],
71 'Please enter a domain name')
73 def test_form_fields_validation(self
):
74 # With all valid values, the form should be valid.
76 'mail_host': 'mailman.most-desirable.org',
77 'description': 'The Most Desirable organization',
80 self
.assertTrue(form
.is_valid())
81 # With a valid alias_domain the form should be valid.
83 'mail_host': 'mailman.most-desirable.org',
84 'description': 'The Most Desirable organization',
85 'alias_domain': 'x.most-desirable.org',
88 self
.assertTrue(form
.is_valid())
89 # Because there is no site_id 2 by default in Django, this form should
92 'mail_host': 'mailman.most-desirable.org',
93 'description': 'The Most Desirable organization',
96 self
.assertFalse(form
.is_valid())
97 self
.assertTrue('site' in form
.errors
.keys())
98 self
.assertEqual(form
.errors
['site'][0],
99 'Select a valid choice.'
100 ' That choice is not one of the available choices.')
101 # Now we use an invalid value for domain name.
103 'mail_host': 'mailman@most-desirable.org',
104 'description': 'The Most Desirable organization',
107 self
.assertFalse(form
.is_valid())
108 self
.assertTrue('mail_host' in form
.errors
.keys())
109 self
.assertEqual(form
.errors
['mail_host'][0],
110 'Please enter a valid domain name')
111 # Now we use an invalid value for alias domain.
113 'mail_host': 'mailman.most-desirable.org',
114 'description': 'The Most Desirable organization',
115 'alias_domain': 'x@most-desirable.org',
118 self
.assertFalse(form
.is_valid())
119 self
.assertTrue('alias_domain' in form
.errors
.keys())
120 self
.assertEqual(form
.errors
['alias_domain'][0],
121 'Please enter a valid domain name or nothing.')
123 def test_site_field_values(self
):
125 self
.assertTrue('site' in form
.fields
.keys())
126 self
.assertTrue([x
for x
in form
.fields
['site'].choices
],
127 [(1, 'example.com (example.com)')])
128 # Now let's create a new domain and see if it shows up.
129 Site
.objects
.create(domain
='mail.most-desirable.org', name
='My Domain')
130 Site
.objects
.create(domain
='dom.most-desirable.org', name
='A Domain')
131 # Items are ordered by "name".
132 self
.assertTrue([x
for x
in form
.fields
['site'].choices
],
133 [(1, 'A Domain (dom.most-desirable.org)'),
134 (2, 'example.com (example.com)'),
135 (3, 'My Domain (mail.most-desirable.org)')])
136 # Initial value should be set to the current site.
137 self
.assertEqual(form
.fields
['site'].initial(),
138 Site
.objects
.get(domain
='example.com'))