+++ /dev/null
-#!/usr/bin/env python
-# This Python file uses the following encoding: utf-8
-
-# The software in this package is distributed under the GNU General
-# Public License version 2 (with a special exception described below).
-#
-# A copy of GNU General Public License (GPL) is included in this distribution,
-# in the file COPYING.GPL.
-#
-# As a special exception, if other files instantiate templates or use macros
-# or inline functions from this file, or you compile this file and link it
-# with other works to produce a work based on this file, this file
-# does not by itself cause the resulting work to be covered
-# by the GNU General Public License.
-#
-# However the source code for this file must still be made available
-# in accordance with section (3) of the GNU General Public License.
-#
-# This exception does not invalidate any other reasons why a work based
-# on this file might be covered by the GNU General Public License.
-#
-# Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
-
-import unittest
-from traceback import print_exc
-from tempfile import mkstemp
-import os
-import sys
-
-from src.simple_cnf import SimpleCnf
-from src.simple_cnf import InvalidCnf
-
-TEST_SET = """
-1 USER,1: "admin"
-2 (1) USER_DISABLED,0: "0"
-3 (1) USER_FULLNAME,0: "Administrator"
-4 (1) USER_GROUPWARE_FOLDER_CALENDAR,0: "INBOX/Kalender"
-5 (1) USER_GROUPWARE_FOLDER_CONTACTS,0: "INBOX/Kontakte"
-6 (1) USER_GROUPWARE_FOLDER_DRAFTS,0: "INBOX/Entwürfe"
-7 (1) USER_GROUPWARE_FOLDER_NOTES,0: "INBOX/Notizen"
-8 (1) USER_GROUPWARE_FOLDER_OUTBOX,0: "INBOX/Gesendete Elemente"
-9 (1) USER_GROUPWARE_FOLDER_TASKS,0: "INBOX/Aufgaben"
-10 (1) USER_GROUPWARE_FOLDER_TRASH,0: "INBOX/Gelöschte Elemente"
-11 (1) USER_GROUP_MEMBER_REF,0: "1"
-12 (1) USER_GROUP_MEMBER_REF,1: "2"
-13 (1) USER_LOCALE,0: ""
-14 (1) USER_PASSWORD,0: "test1234"
-15 (1) USER_TRASH_DELETEDAYS,0: "30"
-16 USER,2: "test"
-17 (16) USER_DISABLED,0: "0"
-18 (16) USER_EMAIL_VACATION,0: "ON"
-19 (16) USER_EMAIL_VACATION_AUTOMATIC_END,0: ""
-20 (16) USER_EMAIL_VACATION_AUTOMATIC_START,0: ""
-21 (16) USER_EMAIL_VACATION_AUTOMATIC_STATE,0: "UNKNOWN"
-22 (16) USER_EMAIL_VACATION_REPLYDAYS,0: "1"
-23 (16) USER_EMAIL_VACATION_TEXT,0: "Bin im Urlaub"
-24 (16) USER_FULLNAME,0: "testnutzer"
-25 (16) USER_GROUPWARE_FOLDER_CALENDAR,0: "INBOX/Kalender"
-26 (16) USER_GROUPWARE_FOLDER_CONTACTS,0: "INBOX/Kontakte"
-27 (16) USER_GROUPWARE_FOLDER_DRAFTS,0: "INBOX/Entwürfe"
-28 (16) USER_GROUPWARE_FOLDER_NOTES,0: "INBOX/Notizen"
-29 (16) USER_GROUPWARE_FOLDER_OUTBOX,0: "INBOX/Gesendete Elemente"
-30 (16) USER_GROUPWARE_FOLDER_TASKS,0: "INBOX/Aufgaben"
-31 (16) USER_GROUPWARE_FOLDER_TRASH,0: "INBOX/Gelöschte Elemente"
-32 (16) USER_GROUP_MEMBER_REF,0: "2"
-33 (16) USER_GROUP_MEMBER_REF,1: "3"
-34 (16) USER_LOCALE,0: ""
-35 (16) USER_PASSWORD,0: "test1234"
-36 (16) USER_TRASH_DELETEDAYS,0: "30"
-37 (16) USER_WEBMAIL_MESSAGES_PER_PAGE,0: "25"
-38 (16) USER_WEBMAIL_SIGNATURE,0: ""
-39 USER,3: "mueller"
-40 (39) USER_DISABLED,0: "0"
-41 (39) USER_FULLNAME,0: "Kärößü"
-42 (39) USER_GROUPWARE_FOLDER_CALENDAR,0: "INBOX/Kalender"
-43 (39) USER_GROUPWARE_FOLDER_CONTACTS,0: "INBOX/Kontakte"
-44 (39) USER_GROUPWARE_FOLDER_DRAFTS,0: "INBOX/Entwürfe"
-45 (39) USER_GROUPWARE_FOLDER_NOTES,0: "INBOX/Notizen"
-46 (39) USER_GROUPWARE_FOLDER_OUTBOX,0: "INBOX/Gesendete Elemente"
-47 (39) USER_GROUPWARE_FOLDER_TASKS,0: "INBOX/Aufgaben"
-48 (39) USER_GROUPWARE_FOLDER_TRASH,0: "INBOX/Gelöschte Elemente"
-49 (39) USER_GROUP_MEMBER_REF,0: "2"
-50 (39) USER_GROUP_MEMBER_REF,1: "3"
-51 (39) USER_LOCALE,0: ""
-52 (39) USER_PASSWORD,0: "grmpfl"
-53 (39) USER_TRASH_DELETEDAYS,0: "30"
-54 (39) USER_WEBMAIL_MESSAGES_PER_PAGE,0: "25"
-55 (39) USER_WEBMAIL_SIGNATURE,0: ""
-56 BACKUP_COMPRESS_ENABLE,0: "1"
-57 BACKUP_CRON,0: "0123456"
-58 (57) BACKUP_CRON_BEGIN,0: "7200"
-59 BACKUP_ENCRYPT_ENABLE,0: "0"
-60 BACKUP_ENCRYPT_PASSWORD,0: ""
-61 TESTVAR,0: "test"
-"""
-
-#: encoding for text files, set in :py:func:`setUpModule`
-ENCODING = None
-
-
-def setUpModule():
- """Called once before all tests; determines encoding
-
- This test might run in very limited build environments without locale, so
- auto-selection of temp-file text encoding may return 'ASCII'.
- Therefore, do the encoding "manually".
- """
- global ENCODING
- ENCODING = sys.getfilesystemencoding()
- if ENCODING.lower() in ('ascii', 'ansi_x3.4-1968'):
- ENCODING = 'utf8'
- print('\nWARNING: Using fallback encoding {} for temp file creation'
- .format(ENCODING))
-
-
-class SimpleCnfTest(unittest.TestCase):
- """ The one and only test case in this module `-->` see module doc """
-
- # setup and cleanup
-
- import_file = None
- cnf = None
-
- @classmethod
- def _import_cnf(cls):
- """ import conf var data from temp file """
- cls.cnf = SimpleCnf()
- cls.cnf.append_file(cls.import_file)
-
- @classmethod
- def setUpClass(cls):
- """ before running tests: write conf var string to temp file """
- try:
- sys_file_descriptor, cls.import_file = mkstemp()
- os.close(sys_file_descriptor)
- with open(cls.import_file, 'wb') as file_handle:
- file_handle.write(TEST_SET.encode(ENCODING))
- except Exception:
- print('\nWARNING: exception creating temp file:')
- print_exc()
-
- # clean up
- cls.tearDownClass()
-
- # re-raise
- raise
-
- cls._import_cnf()
-
- @classmethod
- def tearDownClass(cls):
- """ after all tests have run, delete temp file """
- if cls.import_file is not None:
- try:
- os.unlink(cls.import_file)
- except Exception:
- print('\nWARNING: exception deleting temp file:')
- print_exc()
-
- # tests
-
- def test_ctor_ok (self):
- """
- Test initializer :py:meth:`SimpleCnf.__init__` must succeed on sane
- inputs.
- """
- self.assertNotEqual (SimpleCnf ({"cnf": []}), None)
- self.assertNotEqual (SimpleCnf ([]), None)
- self.assertNotEqual (SimpleCnf (), None)
-
- def test_ctor_fail (self):
- """
- Test initializer :py:meth:`SimpleCnf.__init__` must reject inputs that
- ``cnfvar.py`` does not handle.
- """
- with self.assertRaises (InvalidCnf):
- _void = SimpleCnf (b"junk")
- with self.assertRaises (InvalidCnf):
- _void = SimpleCnf (42)
- with self.assertRaises (InvalidCnf):
- _void = SimpleCnf (tuple ([1701, 1337]))
- with self.assertRaises (InvalidCnf):
- _void = SimpleCnf (set (["one", "two"]))
-
- def test_eq(self):
- """ test method :py:meth:`SimpleCnf.__eq__` """
- self.assertEqual(self.cnf[61], self.cnf[61])
- self.assertEqual(self.cnf['testvar'], self.cnf[61])
- self.assertEqual(self.cnf, self.cnf)
- self.assertNotEqual(self.cnf[56], self.cnf[57])
- self.assertNotEqual(SimpleCnf ({"cnf": []}), None)
- self.assertNotEqual(SimpleCnf ({"cnf": []}), 42)
- self.assertNotEqual(SimpleCnf ({"cnf": []}), "got wood?")
- self.assertEqual(SimpleCnf (), SimpleCnf ({"cnf": []}))
-
- def test_len(self):
- """ test method :py:meth:`SimpleCnf.__len__` """
- self.assertEqual(len(self.cnf), 8)
-
- def test_getitem(self):
- """ test method :py:meth:`SimpleCnf.__item__` """
- self.assertEqual(len(self.cnf['user']), 3)
- self.assertEqual(self.cnf['USER'], self.cnf['user'])
- self.assertEqual(len(self.cnf['backup_encrypt_password']), 1)
- self.assertEqual(len(self.cnf[12232]), 0)
- self.assertEqual(len(self.cnf[55]), 0)
- self.assertEqual(len(self.cnf[61]), 1)
- self.assertEqual(len(self.cnf['user_webmail_signature']), 0)
-
- def test_get(self):
- """ test method :py:meth:`SimpleCnf.get` """
- self.assertEqual(len(self.cnf.get()), 8)
- self.assertEqual(len(self.cnf.get(name='user')), 3)
-
- def test_get_value(self):
- """ test method :py:meth:`SimpleCnf.get_value` """
-
- with self.assertRaises(ValueError):
- self.cnf.get_value()
-
- self.assertEqual(self.cnf[56].get_value(), '1')
- self.assertEqual(self.cnf[61].get_value(), 'test')
-
- def test_get_children(self):
- """ test method :py:meth:`SimpleCnf.get_children` """
- with self.assertRaises(ValueError):
- self.cnf.get_children()
- self.assertEqual(len(self.cnf.get(name='user', value='mueller')
- .get_children()), 16)
- self.assertEqual(self.cnf.get(name='user'),
- self.cnf.get(name='USER'))
- self.assertEqual(self.cnf.get(name='user', value='mueller'),
- self.cnf.get(name='USER', value='mueller'))
- self.assertEqual(len(self.cnf[57].get_children()), 1)
- self.assertEqual(self.cnf[57].get_children().get_value(), '7200')
-
- def test_add_alone(self):
- """ test method :py:meth:`SimpleCnf.add` on empty conf """
- # do not use self.cnf since that would void other test methods
- cnf = SimpleCnf()
- self.assertEqual(len(cnf), 0)
- cnf.add('new_var', 'new_value')
- self.assertEqual(len(cnf), 1)
- cnf_var = cnf.get(name='new_var').get_single_dict()
- self.assertEqual(cnf_var['data'], 'new_value')
- self.assertIsInstance(cnf_var['data'], str)
- self.assertEqual(cnf_var['number'], 1)
-
- def test_add_on_top(self):
- """ test method :py:meth:`SimpleCnf.add` on regular conf """
- cnf = SimpleCnf()
- self.assertEqual(len(cnf), 0)
- cnf.append_file(self.import_file)
- self.assertEqual(len(cnf), 8)
-
- cnf.add('new_var', 'new_value')
- self.assertEqual(len(cnf), 9)
- cnf_var = cnf.get(name='new_var').get_single_dict()
- self.assertEqual(cnf_var['data'], 'new_value')
- self.assertIsInstance(cnf_var['data'], str)
- self.assertEqual(cnf_var['number'], 62)
-
- def test_add_with_children(self):
- """ test method :py:meth:`SimpleCnf.add` by adding var with children"""
- # load config
- cnf = SimpleCnf()
- cnf.append_file(self.import_file)
- self.assertEqual(len(cnf['user']), 3)
-
- # get a certain user with all its sub config
- user_cnf = cnf.get(name='user', value='admin')
- self.assertEqual(len(user_cnf), 1)
-
- # copy as new user with different name but same children
- cnf.add('user', 'admin2', children=user_cnf.get_children())
- self.assertEqual(len(cnf['user']), 4)
- self.assertEqual(len(cnf.get(name='user', value='admin2')), 1)
- self.assertEqual(len(cnf.get(name='user', value='admin2').get_children()), 14)
-
-
-if __name__ == '__main__':
- unittest.main()