1. Moises Henriquez
  2. vasm

Source

vasm / src / tests / test_usermanage.py

#!/usr/bin/env python

#    This file is part of VASM.
#
#    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
import unittest
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

class Test(unittest.TestCase):
    """ Subclass of jailedtests to test how we do when creating accounts """

    @classmethod
    def setUpClass(cls):
        pass

    @classmethod
    def tearDownClass(cls):
        pass

    def tearDown(self):
        if self.usermodel.hasUser(self.user.login):
            self.usermodel.deleteUser(self.user.login)
    
    def setUp(self):
        self.usermodel = useradmin.UserModel()
        # create a dummy account
        self.user = useradmin.User('fidencio', new=True)
        self.user.fullname = 'Fidencio Hdz'
        self.user.groups = ['sys', 'adm', 'slocate', 'power']
        self.user.password = 'supersecretpassword'
        self.addCleanup(self._cleanup)

    def _cleanup(self):
        if self.usermodel.hasUser(self.user.login):
            self.usermodel.deleteUser(self.user.login)
        return

    def test_Invalid_user_object(self):
        # Make sure we raise an exception when invalid user argument is given to addUser
        self.assertRaises(AssertionError,
                          self.usermodel.addUser, self.user.login)
        return

    def test_missing_login_attribute(self):
        user = self.user
        user.login = None
        return self.assertRaises(useradmin.EmptyAttributeError,
                                 self.usermodel.addUser, user)

    def test_missing_passsword_attribute(self):
        user = self.user
        user.password = None
        return self.assertRaises(useradmin.EmptyAttributeError,
                                 self.usermodel.addUser, user)

    def test_short_password_attribute(self):
        user = self.user
        user.password = 'foo'
        return self.assertRaises(AssertionError,
                                 self.usermodel.addUser, user)

    def test_missing_groups_attribute(self):
        user = self.user
        user.groups = None
        return self.assertRaises(useradmin.EmptyAttributeError,
                                 self.usermodel.addUser, user)

    def test_wrong_groups_attribute(self):
        user = self.user
        user.groups = "foo,bar"
        return self.assertRaises(useradmin.EmptyAttributeError,
                                 self.usermodel.addUser, user)
    
    def test_Delete_Nonexistant_User(self):
        # Try to delete a user account that doesn't exist.  This should except.
        return self.assertRaises(AssertionError,
                                 self.usermodel.deleteUser, self.user)

    def test_MakeUser(self):
        # Create a new user account
        self.usermodel.addUser(self.user)
        self.assertTrue(self.usermodel.hasUser(self.user.login)), "User account was not successfully created."
        # test to make sure the attributes of the created account match what we wanted.
        return

    def test_Add_Existing_user(self):
        # Try to add a user that already exists.  This should except
        self.test_MakeUser()
        return self.assertRaises(AssertionError,
                          self.usermodel.addUser, self.user)

    def test_user_attributes(self):
        # Test the user attributes, with the resulting user account
        self.test_MakeUser()
        euser = self.usermodel.getUser(self.user.login)
        self.assertEqual(self.user.login, euser.login), "User login mismatch."
        self.assertTrue(euser.uid > 1000), "User ID is in invalid range."
        self.assertEqual(self.user.groups, euser.groups), "User groups mismatch."
        self.assertEqual(self.user.fullname, euser.fullname), "User full name mismatch."
    
    def test_DeleteUser(self):
        """ Testing user deletion capabilities ... """
        self.test_MakeUser()
        # make sure the user is created
        self.assertTrue, self.usermodel.hasUser(self.user.login)
        # delete the account
        self.usermodel.deleteUser(self.user.login)
        self.assertTrue(self.usermodel.hasUser(self.user.login) is False), "User account was not deleted."


if __name__ =='__main__':
    assert os.geteuid() == 0, "You must be root to run jailed tests."
    unittest.main()