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