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
.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.
43 form
.fields
['delivery_status'].widget
.disabled_choices
,
45 delivery_status_field
= None
46 for each
in form
.visible_fields():
47 if each
.name
== 'delivery_status':
48 delivery_status_field
= each
50 # Make sure the field is actually disabled in the form html.
51 self
.assertIsNotNone(delivery_status_field
)
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
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.
74 # A user can set the delivery_status field to any value other than
76 form
= UserPreferences(
77 dict(delivery_status
='by_user'),
79 preferences
=preferences
,
80 disabled_delivery_choices
=['by_moderator', 'by_bounces'],
82 self
.assertTrue(form
.is_bound
)
83 self
.assertTrue(form
.is_valid())
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.
90 form
= UserPreferences(
91 dict(hide_address
='False'),
93 preferences
=preferences
,
94 disabled_delivery_choices
=['by_moderator', 'by_bounces'],
96 self
.assertTrue(form
.is_bound
)
97 self
.assertTrue(form
.is_valid())
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.
104 form
= UserPreferences(
105 dict(delivery_status
='by_moderator'),
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')