PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mailman/model/tests/test_preferences.py

https://gitlab.com/khushbuparakh/mailman
Python | 94 lines | 55 code | 9 blank | 30 comment | 5 complexity | 6e2840e49ebca10d5c1e444dca1045b7 MD5 | raw file
  1. # Copyright (C) 2016 by the Free Software Foundation, Inc.
  2. #
  3. # This file is part of GNU Mailman.
  4. #
  5. # GNU Mailman is free software: you can redistribute it and/or modify it under
  6. # the terms of the GNU General Public License as published by the Free
  7. # Software Foundation, either version 3 of the License, or (at your option)
  8. # any later version.
  9. #
  10. # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. # more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along with
  16. # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
  17. """Test preferences."""
  18. import unittest
  19. from mailman.interfaces.languages import ILanguageManager
  20. from mailman.interfaces.member import DeliveryMode, DeliveryStatus
  21. from mailman.interfaces.preferences import IPreferences
  22. from mailman.model.preferences import Preferences
  23. from mailman.testing.layers import ConfigLayer
  24. from zope.component import getUtility
  25. from zope.interface import Attribute
  26. from zope.interface.interface import Method
  27. class TestPreferences(unittest.TestCase):
  28. """Test preferences."""
  29. layer = ConfigLayer
  30. def test_absorb_all_attributes(self):
  31. # Test that all attributes in the IPreferences interface are properly
  32. # absorbed, and that none are missed.
  33. attributes = []
  34. for name in IPreferences.names():
  35. attribute = IPreferences.getDescriptionFor(name)
  36. if (not isinstance(attribute, Method)
  37. and isinstance(attribute, Attribute)): # noqa
  38. attributes.append(name)
  39. values = {
  40. 'acknowledge_posts': True,
  41. 'hide_address': True,
  42. 'preferred_language': getUtility(ILanguageManager)['fr'],
  43. 'receive_list_copy': True,
  44. 'receive_own_postings': True,
  45. 'delivery_mode': DeliveryMode.mime_digests,
  46. 'delivery_status': DeliveryStatus.by_user,
  47. }
  48. # If this fails, the IPreferences interface has been mutated. Be sure
  49. # to update this test!
  50. self.assertEqual(sorted(attributes), sorted(values))
  51. preferences_1 = Preferences()
  52. preferences_2 = Preferences()
  53. for name, value in values.items():
  54. setattr(preferences_1, name, value)
  55. preferences_2.absorb(preferences_1)
  56. for name, value in values.items():
  57. self.assertEqual(getattr(preferences_2, name), value)
  58. def test_absorb_overwrite(self):
  59. # Only overwrite the preference if it is unset in the absorber.
  60. preferences_1 = Preferences()
  61. preferences_2 = Preferences()
  62. preferences_1.acknowledge_posts = False
  63. preferences_2.acknowledge_posts = True
  64. preferences_1.hide_address = True
  65. preferences_2.receive_list_copy = True
  66. # Ensure that our preconditions are met.
  67. self.assertIsNotNone(preferences_1.acknowledge_posts)
  68. self.assertIsNotNone(preferences_2.acknowledge_posts)
  69. self.assertIsNotNone(preferences_1.hide_address)
  70. self.assertIsNone(preferences_2.hide_address)
  71. self.assertIsNone(preferences_1.receive_list_copy)
  72. self.assertIsNotNone(preferences_2.receive_list_copy)
  73. # Do the absorb.
  74. preferences_1.absorb(preferences_2)
  75. # This attribute is set in both preferences, so it wasn't absorbed.
  76. self.assertFalse(preferences_1.acknowledge_posts)
  77. # This attribute is only set in the first preferences so it also
  78. # wasn't absorbed.
  79. self.assertTrue(preferences_1.hide_address)
  80. # This attribute is only set in the second preferences, so it was
  81. # absorbed.
  82. self.assertTrue(preferences_1.receive_list_copy)
  83. def test_type_error(self):
  84. preferences = Preferences()
  85. self.assertRaises(TypeError, preferences.absorb, None)