test_config.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. #============================================================================
  17. # Copyright (C) 2014 Oz Nahum Tiram <nahumoz@gmail.com>
  18. #============================================================================
  19. import os
  20. import sys
  21. import unittest
  22. from pwman.util.config import Config, ConfigException, default_config
  23. if sys.version_info.major > 2:
  24. from configparser import NoSectionError
  25. else:
  26. from ConfigParser import NoSectionError
  27. # TODO: Drop support for none AES Encryption,
  28. # So the whole section [Encryption] can be dropped.
  29. with open('testfile.conf', 'w') as f:
  30. f.write("""
  31. [Encryption]
  32. algorithm = Blowfish
  33. [Global]
  34. xsel = /usr/bin/xsel
  35. colors = yes
  36. umask = 0100
  37. cls_timeout = 5
  38. [Database]
  39. type = SQLite
  40. """)
  41. class TestConfig(unittest.TestCase):
  42. def setUp(self):
  43. self.conf = Config(filename='testfile.conf', defaults=default_config)
  44. def test_has_defaults(self):
  45. self.assertTrue(self.conf.parser.has_section('Readline'))
  46. def test_has_blowfish(self):
  47. self.assertEqual('Blowfish', self.conf.get_value('Encryption',
  48. 'algorithm'))
  49. def test_has_user_history(self):
  50. self.assertEqual(os.path.expanduser('~/.pwman/history'),
  51. self.conf.get_value('Readline', 'history'))
  52. def test_has_user_db(self):
  53. self.assertEqual(os.path.expanduser('~/.pwman/pwman.db'),
  54. self.conf.get_value('Database', 'filename'))
  55. def test_wrong_config(self):
  56. with open('wrong_conf.conf', 'w') as f:
  57. f.write("""
  58. [Encryption
  59. algorithm = Blowfish
  60. """)
  61. self.assertRaises(ConfigException, Config, 'wrong_conf.conf')
  62. def test_set_value(self):
  63. self.conf.set_value('Global', 'colors', 'no')
  64. self.assertEqual('no', self.conf.get_value('Global', 'colors'))
  65. def test_set_value_wrong(self):
  66. self.assertRaises(NoSectionError,
  67. self.conf.set_value, *('Error', 'colors', 'no'))
  68. if __name__ == '__main__':
  69. unittest.main(verbosity=2)
  70. os.remove('wrong_conf.conf')
  71. os.remove('testfile.conf')