test_config.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. @staticmethod
  43. def clean_all():
  44. for item in ('testfile.conf', 'wrong_conf.conf', 'dummy.cfg',
  45. 'import_file.csv'):
  46. try:
  47. os.unlink(item)
  48. except OSError:
  49. continue
  50. @classmethod
  51. def tearDownClass(cls):
  52. for item in ('testfile.conf', 'wrong_conf.conf', 'dummy.cfg',
  53. 'import_file.csv'):
  54. try:
  55. os.unlink(item)
  56. except OSError:
  57. continue
  58. def setUp(self):
  59. self.conf = Config(filename='testfile.conf', defaults=default_config)
  60. def test_has_defaults(self):
  61. self.assertTrue(self.conf.parser.has_section('Readline'))
  62. def test_has_blowfish(self):
  63. self.assertEqual('Blowfish', self.conf.get_value('Encryption',
  64. 'algorithm'))
  65. def test_has_user_history(self):
  66. self.assertEqual(os.path.expanduser('~/.pwman/history'),
  67. self.conf.get_value('Readline', 'history'))
  68. def test_has_user_db(self):
  69. self.assertEqual(os.path.expanduser('~/.pwman/pwman.db'),
  70. self.conf.get_value('Database', 'filename'))
  71. def test_wrong_config(self):
  72. with open('wrong_conf.conf', 'w') as f:
  73. f.write("""
  74. [Encryption
  75. algorithm = Blowfish
  76. """)
  77. self.assertRaises(ConfigException, Config, 'wrong_conf.conf')
  78. def test_set_value(self):
  79. self.conf.set_value('Global', 'colors', 'no')
  80. self.assertEqual('no', self.conf.get_value('Global', 'colors'))
  81. def test_set_value_wrong(self):
  82. self.assertRaises(NoSectionError,
  83. self.conf.set_value, *('Error', 'colors', 'no'))
  84. if __name__ == '__main__':
  85. unittest.main(verbosity=2)