1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2017-2023 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
):
26 def test_form_fields_valid(self
):
27 form
= UserPreferences(
29 'acknowledge_posts': 'True',
30 'hide_address': 'True',
31 'receive_list_copy': 'False',
32 'receive_own_postings': 'False',
35 self
.assertTrue(form
.is_valid())
37 def test_disabled_fields(self
):
38 # Test that when some fields are disabled, their widgets actually have
39 # the right attrs set.
40 form
= UserPreferences({}, disabled_delivery_choices
=['by_moderator'])
41 self
.assertTrue(form
.is_valid())
42 # Verify that the disabled choices are set on the widget.
44 form
.fields
['delivery_status'].widget
.disabled_choices
,
47 delivery_status_field
= None
48 for each
in form
.visible_fields():
49 if each
.name
== 'delivery_status':
50 delivery_status_field
= each
52 # Make sure the field is actually disabled in the form html.
53 self
.assertIsNotNone(delivery_status_field
)
55 '<option value="by_moderator" disabled="disabled">'
56 'Disabled by Admin</option>',
57 str(delivery_status_field
),
60 def test_value_for_disabled_field_cannot_be_set(self
):
61 # Initially, this value is set to by_bounces, it shouldn't change on
64 'acknowledge_posts': 'True',
65 'hide_address': 'True',
66 'receive_list_copy': 'False',
67 'receive_own_postings': 'True',
68 'delivery_status': 'by_bounces',
71 # Mock preferences obj that can be saved.
77 # A user can set the delivery_status field to any value other than
79 form
= UserPreferences(
80 dict(delivery_status
='by_user'),
82 preferences
=preferences
,
83 disabled_delivery_choices
=['by_moderator', 'by_bounces'],
85 self
.assertTrue(form
.is_bound
)
86 self
.assertTrue(form
.is_valid())
88 # Verify that the preference object was updated to that value.
89 self
.assertEqual(preferences
, {'delivery_status': 'by_user'})
91 # If the intial value was by_bounces, it can still be saved.
93 form
= UserPreferences(
94 dict(hide_address
='False'),
96 preferences
=preferences
,
97 disabled_delivery_choices
=['by_moderator', 'by_bounces'],
99 self
.assertTrue(form
.is_bound
)
100 self
.assertTrue(form
.is_valid())
102 # Verify that the preference object was updated to that value.
103 self
.assertEqual(preferences
, {'hide_address': False})
105 # If the value is set to a disabled choice, it raises validaiton error.
107 form
= UserPreferences(
108 dict(delivery_status
='by_moderator'),
110 preferences
=preferences
,
111 disabled_delivery_choices
=['by_moderator', 'by_bounces'],
113 self
.assertTrue(form
.is_bound
)
114 self
.assertFalse(form
.is_valid())
116 form
.errors
.get('delivery_status')[0],
117 'Cannot set delivery_status to by_moderator',