Debian Codegraph @

django_mailman3/tests/test_forms.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2016-2023 by the Free Software Foundation, Inc.
4 #
5 # This file is part of Django-Mailman.
6 #
7 # Django-Mailman is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the Free
9 # Software Foundation, either version 3 of the License, or (at your option)
10 # any later version.
11 #
12 # Django-Mailman is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 # more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Django-Mailman. If not, see <http://www.gnu.org/licenses/>.
19
20
21 from django.contrib.auth.models import User
22 from django.test import TestCase
23
24 from django_mailman3.forms import UserProfileForm
25
26
27 class TestUserProfileForm(TestCase):
28
29 def setUp(self):
30 self.user = User.objects.create(username='bob',
31 email='[email protected]')
32
33 def tearDown(self):
34 self.user.delete()
35
36 def test_valid_form(self):
37 data = dict(username='alice',
38 first_name='Alice',
39 last_name='Wonderland',
40 timezone='UTC')
41 form = UserProfileForm(data=data)
42 self.assertTrue(form.is_valid())
43
44 def test_invalid_form(self):
45 data = dict(username='alice',
46 first_name='Alice',
47 last_name='',
48 timezone='')
49 form = UserProfileForm(data=data)
50 self.assertFalse(form.is_valid())
51 self.assertTrue('This field is required.' in str(form.errors))
52 self.assertTrue('timezone' in str(form.errors))
53
54 def test_form_cleans_username(self):
55 data = dict(username='bob',
56 first_name='Bob',
57 last_name='User',
58 timezone='UTC')
59 form = UserProfileForm(data=data)
60 self.assertFalse(form.is_valid())
61 self.assertTrue('A user with that username already exists.'
62 in str(form.errors))
63
64 def test_name_not_required(self):
65 data = dict(username='alice',
66 timezone='UTC')
67 form = UserProfileForm(data=data)
68 self.assertTrue(form.is_valid())