test_config.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. import unittest.mock
  23. from pwman.util import config
  24. from pwman.util.config import find_config_dir
  25. if sys.version_info.major > 2:
  26. from configparser import NoSectionError
  27. else:
  28. from ConfigParser import NoSectionError
  29. with open('testfile.conf', 'w') as f:
  30. f.write("""
  31. [Global]
  32. xsel = /usr/bin/xsel
  33. colors = yes
  34. umask = 0100
  35. cls_timeout = 5
  36. [Database]
  37. type = SQLite
  38. """)
  39. class TestFindConfigWindows(unittest.TestCase):
  40. def test_windows_platfrom(self):
  41. with unittest.mock.patch('platform.system') as pl:
  42. pl.return_value = 'Windows'
  43. os.environ['APPDATA'] = 'balls'
  44. cdir = config.find_config_dir('zzzz')
  45. self.assertEqual(
  46. os.path.expandvars(os.path.join('$APPDATA', 'zzzz')),
  47. cdir)
  48. class TestFindConfigCompat(unittest.TestCase):
  49. c_path = os.path.expanduser("~/.zzzz_compat_posix")
  50. def setUp(self):
  51. if not os.path.exists(self.c_path):
  52. os.mkdir(self.c_path)
  53. def tearDown(self):
  54. os.rmdir(self.c_path)
  55. def test_compat(self):
  56. cdir = config.find_config_dir('zzzz_compat_posix')
  57. self.assertEqual(self.c_path, cdir)
  58. class TestFindConfigXDG(unittest.TestCase):
  59. c_path = os.path.expanduser("~/.zzzz_posix")
  60. def setUp(self):
  61. if os.path.exists(self.c_path):
  62. os.rmdir(self.c_path)
  63. def test_new_scheme(self):
  64. # assert we get xdg_fine with Linux
  65. cdir = config.find_config_dir('zzzz_posix')
  66. self.assertEqual(cdir, os.path.expanduser("~/.config/zzzz_posix"))
  67. class TestConfig(unittest.TestCase):
  68. @classmethod
  69. def tearDownClass(cls):
  70. for item in ('testfile.conf', 'wrong_conf.conf', 'dummy.cfg',
  71. 'import_file.csv'):
  72. try:
  73. os.unlink(item)
  74. except OSError:
  75. continue
  76. def setUp(self):
  77. self.conf = config.Config(filename='testfile.conf',
  78. defaults=config.default_config)
  79. def test_has_defaults(self):
  80. self.assertTrue(self.conf.parser.has_section('Readline'))
  81. def test_has_user_history(self):
  82. cdir = find_config_dir('pwman')
  83. path = os.path.join(cdir, "history")
  84. config = self.conf.get_value('Readline', 'history')
  85. self.assertEqual(path, config)
  86. def test_has_user_db(self):
  87. self.assertNotEqual(os.path.join(config.find_config_dir("pwman"),'pwman.db'),
  88. self.conf.get_value('Database', 'filename'))
  89. def test_wrong_config(self):
  90. with open('wrong_conf.conf', 'w') as f:
  91. f.write("""
  92. [Encryption
  93. algorithm = Blowfish
  94. """)
  95. self.assertRaises(config.ConfigException, config.Config,
  96. 'wrong_conf.conf')
  97. def test_set_value(self):
  98. self.conf.set_value('Global', 'colors', 'no')
  99. self.assertEqual('no', self.conf.get_value('Global', 'colors'))
  100. def test_set_value_wrong(self):
  101. self.assertRaises(NoSectionError,
  102. self.conf.set_value, *('Error', 'colors', 'no'))
  103. def test_get_pass_conf(self):
  104. ans = config.get_pass_conf(self.conf)
  105. self.assertFalse(any(ans))
  106. def test_open_file(self):
  107. cfg = config.Config("DoesNotExist")
  108. cfg.save()
  109. self.assertTrue(os.path.exists("DoesNotExist"))
  110. os.unlink("DoesNotExist")
  111. if __name__ == '__main__':
  112. unittest.main(verbosity=2)