test_config.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. get_pass_conf)
  24. if sys.version_info.major > 2:
  25. from configparser import NoSectionError
  26. else:
  27. from ConfigParser import NoSectionError
  28. with open('testfile.conf', 'w') as f:
  29. f.write("""
  30. [Global]
  31. xsel = /usr/bin/xsel
  32. colors = yes
  33. umask = 0100
  34. cls_timeout = 5
  35. [Database]
  36. type = SQLite
  37. """)
  38. class TestConfig(unittest.TestCase):
  39. @staticmethod
  40. def clean_all():
  41. for item in ('testfile.conf', 'wrong_conf.conf', 'dummy.cfg',
  42. 'import_file.csv'):
  43. try:
  44. os.unlink(item)
  45. except OSError:
  46. continue
  47. @classmethod
  48. def tearDownClass(cls):
  49. for item in ('testfile.conf', 'wrong_conf.conf', 'dummy.cfg',
  50. 'import_file.csv'):
  51. try:
  52. os.unlink(item)
  53. except OSError:
  54. continue
  55. def setUp(self):
  56. self.conf = Config(filename='testfile.conf', defaults=default_config)
  57. def test_has_defaults(self):
  58. self.assertTrue(self.conf.parser.has_section('Readline'))
  59. #def test_has_blowfish(self):
  60. # self.assertEqual('Blowfish', self.conf.get_value('Encryption',
  61. # 'algorithm'))
  62. def test_has_user_history(self):
  63. self.assertEqual(os.path.expanduser('~/.pwman/history'),
  64. self.conf.get_value('Readline', 'history'))
  65. def test_has_user_db(self):
  66. self.assertEqual(os.path.expanduser('~/.pwman/pwman.db'),
  67. self.conf.get_value('Database', 'filename'))
  68. def test_wrong_config(self):
  69. with open('wrong_conf.conf', 'w') as f:
  70. f.write("""
  71. [Encryption
  72. algorithm = Blowfish
  73. """)
  74. self.assertRaises(ConfigException, Config, 'wrong_conf.conf')
  75. def test_set_value(self):
  76. self.conf.set_value('Global', 'colors', 'no')
  77. self.assertEqual('no', self.conf.get_value('Global', 'colors'))
  78. def test_set_value_wrong(self):
  79. self.assertRaises(NoSectionError,
  80. self.conf.set_value, *('Error', 'colors', 'no'))
  81. def test_get_pass_conf(self):
  82. ans = get_pass_conf(self.conf)
  83. self.assertFalse(any(ans))
  84. if __name__ == '__main__':
  85. unittest.main(verbosity=2)