Exemplo n.º 1
0
 def test_add_user_to_usergroup(self):
     self.user_manager = UserManager()
     self.user_manager.create_user('test_user',
                                   'test_password')
     self.assertEqual(self.user_group_manager.add_user_to_usergroup(
         'test_user', self.user_group_manager.user_groups[0],
         self.user_manager),
         True)
Exemplo n.º 2
0
 def test_update_user_in_usergroup_not_existant_user(self):
     self.user_manager = UserManager()
     self.user_manager.create_user('test_user', 'test_password')
     self.user_group_manager.add_user_to_usergroup('test_user',
                                                   self.user_group_manager.
                                                   user_groups[0],
                                                   self.user_manager)
     self.assertEqual(self.user_group_manager.update_user_in_usergroup(
         'test_user', 'not_existing', self.user_manager), False)
def create_user(username, password):
    """
    Creates a user and adds it to the yaml configuration file.
    \b
    :param username: The username of the user you'd like to add.
    :param password: The password of the user you'd like to add.
    """
    usermanager = UserManager()
    if usermanager.create_user(username, password):
        click.secho("Successfully created the user " + username,
                    fg="green", bold=True)
def delete_user(username):
    """
    Deletes a user by given username.
    :param username: The username of the user you'd like to delete.
    """
    usermanager = UserManager()
    groupmanager = UserGroupManager()

    if username is None:
        username = select_user_from_list(usermanager)

    if click.confirm("Are you sure you want to delete the user "
                     "with username: "******"Successfully deleted user with username: "******"green", bold=True)
def update_user(username):
    """
    Edit a user. The username is an optional argument. If not entered,
    the list of users will be shown to choose a user from.
    \b
    :param username: The name of the user you'd like to edit.
    """
    usermanager = UserManager()
    groupmanager = UserGroupManager()
    if username is None:
        username = select_user_from_list(usermanager)

    if username not in (username.get_username() for username in
                        usermanager.users):
        click.secho("The given user does not exist!", fg="red", bold=True)
        return

    while True:
        click.echo("What fields of this user would you like to edit?")
        click.echo(click.style("[1]", fg="cyan") + " The username")
        click.echo(click.style("[2]", fg="cyan") + " The password")
        click.echo(click.style("[3]", fg="cyan") + " All above fields")
        edit_fields = click.prompt("", type=int)
        if edit_fields in range(1, 4):
            break

    if edit_fields == 1 or edit_fields == 3:
        while True:
            new_username = click.prompt("Please type the new username "
                                        "for this user.", type=str)
            if usermanager.update_user_username(username, new_username) and \
                    groupmanager.update_user_in_usergroup(
                            username, new_username, usermanager):
                click.secho("Succesfully changed the username " + username +
                            " to " + new_username + "!", fg="green", bold=True)
                username = new_username
                break
            else:
                click.secho("There is already a user with this username!",
                            fg="red", bold=True)

    if edit_fields == 2 or edit_fields == 3:
        new_password = click.prompt("Please type the new password "
                                    "for this user.", type=str)
        if usermanager.update_user_password(username, new_password):
            click.secho("Succesfully changed the password of " + username +
                        " to " + new_password + "!", fg="green", bold=True)
Exemplo n.º 6
0
class test_usergroupmanager(unittest.TestCase):
    'Unit tests for the usergroupmanager'

    def setUp(self):
        self.user_group_manager = UserGroupManager()
        self.user_group_manager.create_group('test_group')
        self.test_dict = ''

    def tearDown(self):
        if os.path.exists('groups.yaml'):
            os.remove('groups.yaml')
        if os.path.exists('users.yaml'):
            os.remove('users.yaml')

    # Create tests.
    def test_create_group(self):
        self.assertEqual(self.user_group_manager.create_group('test_group2'),
                         True)

    def test_create_group_duplicate(self):
        self.assertEqual(self.user_group_manager.create_group('test_group'),
                         False)

    def test_create_group_not_string_name(self):
        self.assertEqual(self.user_group_manager.create_group(1), False)

    def test_create_group_weird_string(self):
        self.assertEqual(self.user_group_manager.create_group('~*&^%$#'
                                                              ), True)

    def test_create_group_same_string_int_name(self):
        self.assertEqual(self.user_group_manager.create_group('1'), False)

    def test_add_user_to_usergroup(self):
        self.user_manager = UserManager()
        self.user_manager.create_user('test_user',
                                      'test_password')
        self.assertEqual(self.user_group_manager.add_user_to_usergroup(
            'test_user', self.user_group_manager.user_groups[0],
            self.user_manager),
            True)

    # Read tests.
    def test_read_group_list(self):
        self.assertEqual(len(self.user_group_manager.read_groups()), 1)

    def test_read_group_null(self):
        self.user_group_manager.delete_usergroup('test_group')
        self.assertEqual(len(self.user_group_manager.read_groups()), 0)

    # Update tests.
    def test_update_group_name(self):
        self.assertEqual(self.user_group_manager.update_usergroup_name(
            'test_group', 'test_group2'), True)

    def test_update_group_name_to_int(self):
        self.assertEqual(self.user_group_manager.update_usergroup_name(
            'test_group', 1), False)

    def test_update_user_in_usergroup(self):
        self.user_manager = UserManager()
        self.user_manager.create_user('test_user', 'test_password')
        self.user_manager.create_user('test_user_new', 'test_password')
        self.user_group_manager.add_user_to_usergroup('test_user',
                                                      self.user_group_manager.
                                                      user_groups[0],
                                                      self.user_manager)
        self.assertEqual(self.user_group_manager.update_user_in_usergroup(
            'test_user', 'test_user_new', self.user_manager), True)

    def test_update_user_in_usergroup_not_existant_user(self):
        self.user_manager = UserManager()
        self.user_manager.create_user('test_user', 'test_password')
        self.user_group_manager.add_user_to_usergroup('test_user',
                                                      self.user_group_manager.
                                                      user_groups[0],
                                                      self.user_manager)
        self.assertEqual(self.user_group_manager.update_user_in_usergroup(
            'test_user', 'not_existing', self.user_manager), False)

    # Delete tests.
    def test_delete_group(self):
        self.assertEqual(self.user_group_manager.delete_usergroup(
            'test_group'), True)

    def test_delete_group_not_existing(self):
        self.assertEqual(self.user_group_manager.delete_usergroup(
            'not_existant'), False)

    def test_delete_user_from_usergroup(self):
        self.user_manager = UserManager()
        self.user_manager.create_user('test_user',
                                      'test_password')
        self.user_group_manager.add_user_to_usergroup('test_user',
                                                      self.user_group_manager.
                                                      user_groups[0],
                                                      self.user_manager)
        self.assertEqual(self.user_group_manager.remove_user_from_usergroup(
            'test_user', self.user_group_manager.user_groups[0]), True)

    def test_delete_user_from_usergroup_not_existing(self):
        self.assertEqual(self.user_group_manager.remove_user_from_usergroup(
            'test_user', self.user_group_manager.user_groups[0]), False)
 def setUp(self):
     self.user_manager = UserManager()
     self.user_manager.create_user('test_user', 'test_password')
     self.test_dict = ''
class test_groupmanager(unittest.TestCase):
    """Unit tests for groupmanager"""
    def setUp(self):
        self.user_manager = UserManager()
        self.user_manager.create_user('test_user', 'test_password')
        self.test_dict = ''

    def tearDown(self):
        if os.path.exists('users.yaml'):
            os.remove('users.yaml')

    # Create tests.
    def test_create_user(self):
        self.assertEqual(
            self.user_manager.create_user('test_user2', 'test_password'), True)

    def test_create_user_duplicate(self):
        self.assertEqual(
            self.user_manager.create_user('test_user', 'test_password'), False)

    def test_create_user_username_not_string(self):
        self.assertEqual(self.user_manager.create_user(1, 'test_password'),
                         False)

    def test_create_user_password_not_string(self):
        self.assertEqual(self.user_manager.create_user('test_user2', 1), True)

    def test_create_user_both_not_string(self):
        self.assertEqual(self.user_manager.create_user(1, 1), False)

    # Read tests.
    def test_read_user_list(self):
        self.assertEqual(len(self.user_manager.read_users()), 1)

    def test_read_group_null(self):
        self.user_manager.delete_user('test_user')
        self.assertEqual(len(self.user_manager.read_users()), 0)

    # Update tests.
    def test_update_group_name(self):
        self.assertEqual(
            self.user_manager.update_user_username('test_user',
                                                   'test_user_new'), True)

    # Delete tests.
    def test_delete_user(self):
        self.assertEqual(self.user_manager.delete_user('test_user'), True)

    def test_delete_user_not_existing(self):
        self.assertEqual(self.user_manager.delete_user('not_existant'), False)
 def setUp(self):
     self.user_manager = UserManager()
     self.user_manager.create_user('test_user', 'test_password')
     self.test_dict = ''
class test_groupmanager(unittest.TestCase):
    """Unit tests for groupmanager"""

    def setUp(self):
        self.user_manager = UserManager()
        self.user_manager.create_user('test_user', 'test_password')
        self.test_dict = ''

    def tearDown(self):
        if os.path.exists('users.yaml'):
            os.remove('users.yaml')

    # Create tests.
    def test_create_user(self):
        self.assertEqual(self.user_manager.create_user('test_user2',
                                                       'test_password'), True)

    def test_create_user_duplicate(self):
        self.assertEqual(self.user_manager.create_user('test_user',
                                                       'test_password'), False)

    def test_create_user_username_not_string(self):
        self.assertEqual(self.user_manager.create_user(1, 'test_password'),
                         False)

    def test_create_user_password_not_string(self):
        self.assertEqual(self.user_manager.create_user('test_user2', 1),
                         True)

    def test_create_user_both_not_string(self):
        self.assertEqual(self.user_manager.create_user(1, 1), False)

    # Read tests.
    def test_read_user_list(self):
        self.assertEqual(len(self.user_manager.read_users()), 1)

    def test_read_group_null(self):
        self.user_manager.delete_user('test_user')
        self.assertEqual(len(self.user_manager.read_users()), 0)

    # Update tests.
    def test_update_group_name(self):
        self.assertEqual(self.user_manager.update_user_username(
            'test_user', 'test_user_new'), True)

    # Delete tests.
    def test_delete_user(self):
        self.assertEqual(self.user_manager.delete_user(
            'test_user'), True)

    def test_delete_user_not_existing(self):
        self.assertEqual(self.user_manager.delete_user(
          'not_existant'), False)