Bump version to 1.3.6 and update NEWS.rst for 1.3.5
[mailman-postorious.git] / src / postorius / tests / test_user_forms.py
blob7375e65d87db132289a5a1baf25760b9bde6b04c
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)
9 # any later version.
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
14 # more details.
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.test import TestCase
22 from postorius.forms.user_forms import UserPreferences
25 class UserPreferencesTest(TestCase):
27 def test_form_fields_valid(self):
28 form = UserPreferences({
29 'acknowledge_posts': 'True',
30 'hide_address': 'True',
31 'receive_list_copy': 'False',
32 'receive_own_postings': 'False',
34 self.assertTrue(form.is_valid())
36 def test_disabled_fields(self):
37 # Test that when some fields are disabled, their widgets actually have
38 # the right attrs set.
39 form = UserPreferences({}, disabled_delivery_choices=['by_moderator'])
40 self.assertTrue(form.is_valid())
41 # Verify that the disabled choices are set on the widget.
42 self.assertEqual(
43 form.fields['delivery_status'].widget.disabled_choices,
44 ['by_moderator'])
45 delivery_status_field = None
46 for each in form.visible_fields():
47 if each.name == 'delivery_status':
48 delivery_status_field = each
49 break
50 # Make sure the field is actually disabled in the form html.
51 self.assertIsNotNone(delivery_status_field)
52 self.assertTrue(
53 '<option value="by_moderator" disabled="disabled">'
54 'Disabled by Admin</option>',
55 str(delivery_status_field))
57 def test_value_for_disabled_field_cannot_be_set(self):
58 # Initially, this value is set to by_bounces, it shouldn't change on
59 # saving the form.
60 initial = {
61 'acknowledge_posts': 'True',
62 'hide_address': 'True',
63 'receive_list_copy': 'False',
64 'receive_own_postings': 'True',
65 'delivery_status': 'by_bounces'
68 # Mock preferences obj that can be saved.
69 class Pref(dict):
70 def save(self):
71 pass
73 preferences = Pref()
74 # A user can set the delivery_status field to any value other than
75 # disabled_choices
76 form = UserPreferences(
77 dict(delivery_status='by_user'),
78 initial=initial,
79 preferences=preferences,
80 disabled_delivery_choices=['by_moderator', 'by_bounces'],
82 self.assertTrue(form.is_bound)
83 self.assertTrue(form.is_valid())
84 form.save()
85 # Verify that the preference object was updated to that value.
86 self.assertEqual(preferences, {'delivery_status': 'by_user'})
88 # If the intial value was by_bounces, it can still be saved.
89 preferences = Pref()
90 form = UserPreferences(
91 dict(hide_address='False'),
92 initial=initial,
93 preferences=preferences,
94 disabled_delivery_choices=['by_moderator', 'by_bounces'],
96 self.assertTrue(form.is_bound)
97 self.assertTrue(form.is_valid())
98 form.save()
99 # Verify that the preference object was updated to that value.
100 self.assertEqual(preferences, {'hide_address': False})
102 # If the value is set to a disabled choice, it raises validaiton error.
103 preferences = Pref()
104 form = UserPreferences(
105 dict(delivery_status='by_moderator'),
106 initial=initial,
107 preferences=preferences,
108 disabled_delivery_choices=['by_moderator', 'by_bounces'],
110 self.assertTrue(form.is_bound)
111 self.assertFalse(form.is_valid())
112 self.assertEqual(form.errors.get('delivery_status')[0],
113 'Cannot set delivery_status to by_moderator')