Replace our gconf-based settings module with a file-based one
[mevemon] / package / src / tests / test_file_settings.py
1 """ this tests the file_settings.py module """
2 import unittest
3 import StringIO
4
5 import file_settings
6
7 DEFAULT = """
8 [accounts]
9     [[account.11111]]
10     uid = 11111
11     apikey = aaaaaaaaa
12     [[account.22222]]
13     uid = 22222
14     apikey = bbbbbbbbb
15 """
16
17 class TestFileSettings(unittest.TestCase):
18     def setUp(self):
19         self._file = StringIO.StringIO(DEFAULT)
20         self._settings = file_settings.Settings(self._file)
21
22     def test_get_accounts(self):
23         """ Tests the get_accounts method under normal operation """
24         expected = {"11111": "aaaaaaaaa", "22222": "bbbbbbbbb"}
25         self.assertEqual(self._settings.get_accounts(), expected)
26     
27     def test_get_accounts_empty(self):
28         """ Tet the get_accounts method when the config file is empty """
29         empty_settings = file_settings.Settings(None)
30         expected = {}
31         self.assertEqual(empty_settings.get_accounts(), expected)
32
33     def test_get_api_key(self):
34         """ Tests get_api_key for an existing key """
35         self.assertEqual(self._settings.get_api_key('11111'), "aaaaaaaaa")
36    
37     def test_get_api_key_invalid(self):
38         """ Tests get_api_key when trying to get the key for a non-existing uid."""
39         self.assertRaises(Exception, self._settings.get_api_key, 'foo')
40
41
42     def test_add_account(self):
43         """ Test that adding an account adds it to the configObj AND the config file """
44         self._file.seek(0) # we have to do this, because we are using a StringIO object instead of a real file
45         self._settings.add_account("33333", "ccccccc")
46         self.assertEqual(self._settings.get_api_key("33333"), "ccccccc")
47         self.assertTrue('[[account.33333]]' in self._file.getvalue())
48
49     def test_remove_account(self):
50         """ Test that removing an account removes it from the configObj AND the config file """
51         self._file.seek(0) # we have to do this, because we are using a StringIO object instead of a real file
52         self._settings.remove_account("22222")
53         self.assertRaises(Exception, self._settings.get_api_key, '22222')
54         self.assertTrue('22222' not in self._settings.get_accounts().keys())
55         self.assertTrue("[[account.22222]]" not in self._file.getvalue())
56
57
58