Source

vasm / src / tests / test_SKEL.py

Full commit
#
#    VASM is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v3 as published by
#    the Free Software Foundation.
#
#    VASM is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with VASM.  If not, see <http://www.gnu.org/licenses/>.

import os
import sys
vasmdir = os.path.join(os.path.dirname(
    os.path.dirname(os.path.abspath(__file__))), 'vasm')
if not vasmdir in sys.path: sys.path.append(vasmdir)
from backend import USERADMIN, SKEL
import unittest

__author__ = "Moises Henriquez"
__author_email__ = "moc.liamg@xnl.E0M"[::-1]


class TestSkel(unittest.TestCase):
    def setUp(self):
        # create a fake user account.
        self.usermodel = USERADMIN.UserModel()
        self._fake_user = USERADMIN.User(login='fidencio', new=True)
        if self.usermodel.hasUser(self._fake_user.login):
            self.usermodel.deleteUser(self._fake_user.login)
        self._fake_user.realname = 'Fidencio Hdz'
        self._fake_user.groups = ['sys', 'adm', 'slocate', 'netdev', 'power']
        self._fake_user.password = 'secrepassword'
        self.usermodel.addUser(self._fake_user)
        
        self.assertTrue(self.usermodel.hasUser('fidencio'))
        self.SkelModel = SKEL.UserSkel('fidencio')
        self.addCleanup(self._custom_cleanup)

    def test_ResetInvalidSetting(self):
        """ Make sure we raise an exception when we try to reset something that doesn't exist."""
        return self.assertRaises(AssertionError,
                                 self.SkelModel.reset_settings,'foo')

    def test_ResetSettingsDir(self):
        """ Test resetting a directory of settings """
        d = None
        for f in self.SkelModel.list_resettable_settings():
            if os.path.isdir(os.path.join('/etc/skel', f)):
                d = f
                break
        return self.assertTrue(self.SkelModel.reset_settings([d]))

    def test_ResetSettingsFile(self):
        """ Test resetting a settings file """
        # get a file first
        f = None
        for s in self.SkelModel.list_resettable_settings():
            if not os.path.isdir(os.path.join('/etc/skel', s)):
                f = s
                break
        return self.assertTrue(self.SkelModel.reset_settings([f]))

    def _custom_cleanup(self):
        if self.usermodel.hasUser(self._fake_user.login):
            self.usermodel.deleteUser(self._fake_user.login)

    def tearDown(self):
        # delete the created user
        if self.usermodel.hasUser(self._fake_user.login):
            self.usermodel.deleteUser(self._fake_user.login)
        super(TestSkel, self).tearDown()


if __name__ == '__main__':
    assert os.geteuid != 0, "You must be root to perform SKEL tests because these are jailed tests."
    unittest.main()