示例#1
0
def test_users_instance_creation():
    """ Test creation of a user instance """
    users = Users()
    users.append(
        User(name='rod',
             uid=1001,
             gid=1001,
             gecos='rod comment',
             home_dir='/home/rod',
             shell='/bin/sh'))
    users.append(
        User(name='jane',
             uid=1002,
             gid=1002,
             gecos='jane comment',
             home_dir='/home/jane',
             shell='/bin/bash'))
    users.append(
        User(name='freddy',
             uid=1003,
             gid=1003,
             gecos='freddy comment',
             home_dir='/home/freddy',
             shell='/bin/false'))
    assert len(users) == 3
示例#2
0
def test_execute_plan_to_create_new_user_with_clashing_uid():
    """ Create a new user and then attempt to create another user with existing id """
    delete_test_user_and_group()
    current_users = Users.from_passwd()
    provided_users = Users()
    provided_users.append(
        User(name='testuserx1234', uid=59999, gecos='test user gecos'))
    plan = create_plan(existing_users=current_users,
                       proposed_users=provided_users,
                       protected_users=[
                           'travis', 'couchdb', 'ubuntu', 'nginx', 'hadfielj',
                           'vagrant', CURRENT_USER
                       ])
    assert plan[0]['action'] == 'add'
    assert plan[0]['proposed_user'].name == "testuserx1234"
    assert plan[0]['proposed_user'].uid == 59999
    assert plan[0]['proposed_user'].gecos == '\"test user gecos\"'
    execute_plan(plan=plan)
    current_users = Users.from_passwd()
    provided_users = Users()
    provided_users.append(
        User(name='testuserx12345', uid=59999, gecos='test user gecos'))
    plan = create_plan(existing_users=current_users,
                       proposed_users=provided_users,
                       purge_undefined=True,
                       protected_users=[
                           'travis', 'couchdb', 'ubuntu', 'nginx', 'hadfielj',
                           'vagrant', CURRENT_USER
                       ])
    assert plan[0]['error'] == 'uid_clash'
    execute_plan(plan=plan)
    delete_test_user_and_group()
示例#3
0
def test_users_instance_creation():
    """ Test creation of instances of User and add to Users collection. """
    input_user_list = Users()
    input_user_list.append(
        User(name='rod',
             uid=1001,
             gid=1001,
             gecos='rod comment',
             home_dir='/home/rod',
             shell='/bin/sh'))
    input_user_list.append(
        User(name='jane',
             uid=1002,
             gid=1002,
             gecos='jane comment',
             home_dir='/home/jane',
             shell='/bin/bash'))
    input_user_list.append(
        User(name='freddy',
             uid=1003,
             gid=1003,
             gecos='freddy comment',
             home_dir='/home/freddy',
             shell='/bin/false'))
    assert len(input_user_list) == 3
示例#4
0
def test_users_insert_method():
    users = Users()
    users.append(
        User(name='rod',
             uid=1001,
             gid=1001,
             gecos='rod comment',
             home_dir='/home/rod',
             shell='/bin/sh'))
    users.append(
        User(name='jane',
             uid=1002,
             gid=1002,
             gecos='jane comment',
             home_dir='/home/jane',
             shell='/bin/sh'))
    users.insert(
        0,
        User(name='freddy',
             uid=1003,
             gid=1003,
             gecos='freddy comment',
             home_dir='/home/freddy',
             shell='/bin/false'))
    assert len(users) == 3
    with pytest.raises(TypeError):
        users.insert(
            0,
            dict(name='freddy',
                 uid=1003,
                 gid=1003,
                 gecos='freddy comment',
                 home_dir='/home/freddy',
                 shell='/bin/false'))
示例#5
0
def test_users_add_and_remove():
    rod = User(name='rod',
               uid=1001,
               gid=1001,
               gecos='rod comment',
               home_dir='/home/rod',
               shell='/bin/sh')
    users = Users()
    users.append(rod)
    assert len(users) == 1
    jane = User(name='jane')
    users.append(jane)
    assert len(users) == 2
    users.remove(username='******')
    assert len(users) == 1
示例#6
0
def test_execute_plan_to_create_user_with_invalid_sudoers_entry():
    """ Create a new user but specify an invalid sudoers entry """
    delete_test_user_and_group()
    raw_public_key_2 = PUBLIC_KEYS[1].get('raw')
    public_key_2 = PublicKey(raw=raw_public_key_2)
    current_users = Users.from_passwd()
    provided_users = Users()
    provided_users.append(
        User(name='testuserx1234',
             uid=59998,
             gid=1,
             gecos='test user gecos update',
             shell='/bin/false',
             public_keys=[public_key_2],
             sudoers_entry='INVALID ALL=(ALL:ALL) ALL'))
    plan = create_plan(existing_users=current_users,
                       proposed_users=provided_users,
                       manage_home=False,
                       protected_users=[
                           'travis', 'couchdb', 'ubuntu', 'nginx', 'hadfielj',
                           'vagrant', CURRENT_USER
                       ])
    assert plan[0]['proposed_user'].gecos == '\"test user gecos update\"'
    with pytest.raises(ValueError):
        execute_plan(plan=plan)
    delete_test_user_and_group()
示例#7
0
def test_user_instance_with_missing_gecos():
    rod = User(name='rod',
               uid=1001,
               gid=1001,
               home_dir='/home/rod',
               shell='/bin/sh')
    assert rod.gecos == None
示例#8
0
def test_update_existing_user():
    delete_test_user_and_group()
    create_test_user()
    current_users = Users.from_passwd()
    provided_users = Users()
    raw_public_key = PUBLIC_KEYS[0].get('raw')
    public_key = PublicKey(raw=raw_public_key)
    provided_users.append(
        User(name='testuserx1234',
             uid=59999,
             gecos='test user gecos update',
             home_dir='/tmp/temp',
             public_keys=[public_key]))
    plan = create_plan(existing_users=current_users,
                       proposed_users=provided_users,
                       protected_users=[
                           'travis', 'couchdb', 'ubuntu', 'nginx', 'hadfielj',
                           'vagrant', CURRENT_USER
                       ])
    assert plan[0]['action'] == 'update'
    execute_plan(plan)
    current_users = Users.from_passwd()
    new_user = current_users.describe_users(users_filter=dict(
        name='testuserx1234'))
    assert new_user[0].public_keys[0].raw == raw_public_key
    delete_test_user_and_group()
示例#9
0
def test_users_set_item():
    rod = User(name='rod',
               uid=1001,
               gid=1001,
               gecos='rod comment',
               home_dir='/home/rod',
               shell='/bin/sh')
    users = Users()
    users.append(rod)
    users[0] = User(name='jane',
                    uid=1002,
                    gid=1002,
                    gecos='jane comment',
                    home_dir='/home/jane',
                    shell='/bin/sh')
    assert len(users) == 1
    assert users[0].name == 'jane'
示例#10
0
def test_users_del_method():
    users = Users()
    users.append(
        User(name='rod',
             uid=1001,
             gid=1001,
             gecos='rod comment',
             home_dir='/home/rod',
             shell='/bin/sh'))
    users.append(
        User(name='jane',
             uid=1002,
             gid=1002,
             gecos='jane comment',
             home_dir='/home/jane',
             shell='/bin/sh'))
    assert len(users) == 2
    del users[0]
    assert len(users) == 1
示例#11
0
def test_users_repr():
    users = Users()
    users.append(
        User(name='rod',
             uid=1001,
             gid=1001,
             gecos='rod comment',
             home_dir='/home/rod',
             shell='/bin/sh'))
    assert str(users) == users.__repr__()
示例#12
0
def test_users_filters():
    users = Users()
    users.append(
        User(name='rod',
             uid=1001,
             gid=1001,
             gecos='rod comment',
             home_dir='/home/rod',
             shell='/bin/sh'))
    assert not users.describe_users(users_filter=dict(name='nobody'))
    assert not users.describe_users(users_filter=dict(uid=1000))
示例#13
0
def test_create_and_execute_plan_to_create_new_user_with_sudo_all():
    """ Test creation of a user instance with sudo all and then write """
    delete_test_user_and_group()
    create_test_group()
    current_users = Users.from_passwd()
    provided_users = Users()

    public_keys = [PublicKey(b64encoded=PUBLIC_KEYS[0]['encoded'])]
    provided_users.append(
        User(name='testuserx1234',
             home_dir='/home/testuserx1234',
             shell='/bin/false',
             gid=59999,
             uid=59999,
             gecos='test user gecos',
             public_keys=public_keys,
             sudoers_entry='ALL=(ALL)\tNOPASSWD:ALL'))
    plan = create_plan(existing_users=current_users,
                       proposed_users=provided_users,
                       purge_undefined=True,
                       protected_users=[
                           'travis', 'couchdb', 'ubuntu', 'vagrant',
                           CURRENT_USER
                       ])
    assert plan[0]['state'] == 'missing'
    assert plan[0]['proposed_user'].name == "testuserx1234"
    assert plan[0]['proposed_user'].home_dir == "/home/testuserx1234"
    assert plan[0]['proposed_user'].uid == 59999
    assert plan[0]['proposed_user'].gid == 59999
    assert plan[0]['proposed_user'].gecos == '\"test user gecos\"'
    assert plan[0]['proposed_user'].shell == '/bin/false'
    assert plan[0]['proposed_user'].sudoers_entry == 'ALL=(ALL)\tNOPASSWD:ALL'
    assert type(plan[0]['proposed_user'].public_keys[0].raw) == text_type
    assert plan[0]['proposed_user'].public_keys[0].raw == text_type(
        PUBLIC_KEYS[0]['raw'])
    execute_plan(plan=plan)
    current_users = Users.from_passwd()
    created_user = current_users.describe_users(users_filter=dict(
        name='testuserx1234'))
    assert created_user[0].sudoers_entry == 'ALL=(ALL)\tNOPASSWD:ALL'
    plan = create_plan(existing_users=current_users,
                       proposed_users=provided_users,
                       purge_undefined=True,
                       protected_users=[
                           'travis', 'couchdb', 'ubuntu', 'nginx', 'vagrant',
                           CURRENT_USER
                       ])
    assert not plan
示例#14
0
def test_user_instance_creation_precommented_gecos():
    name = 'Fred'
    uid = 1024
    gid = 1024
    gecos = '\'Fred Bloggs\''
    home_dir = '/home/fred'
    shell = '/bin/false'
    test_user = User(name=name,
                     uid=uid,
                     gid=gid,
                     gecos=gecos,
                     home_dir=home_dir,
                     shell=shell)
    assert test_user.name == name
    assert test_user.uid == uid
    assert test_user.gid == gid
    # Ensure gecos is surrounded with double quotes
    assert test_user.gecos.startswith('\"') and test_user.gecos.endswith('\"')
    assert test_user.home_dir == home_dir
    assert test_user.shell == shell
示例#15
0
def test_execute_plan_to_update_existing_user():
    """ Create a new user and then attempt to create another user with existing id """

    delete_test_user_and_group()
    create_test_user()
    raw_public_key_2 = PUBLIC_KEYS[1].get('raw')
    public_key_2 = PublicKey(raw=raw_public_key_2)
    current_users = Users.from_passwd()
    provided_users = Users()
    provided_users.append(
        User(name='testuserx1234',
             uid=59998,
             gid=1,
             gecos='test user gecos update',
             shell='/bin/false',
             public_keys=[public_key_2],
             sudoers_entry='ALL=(ALL:ALL) ALL'))
    plan = create_plan(existing_users=current_users,
                       proposed_users=provided_users,
                       protected_users=[
                           'travis', 'couchdb', 'ubuntu', 'nginx', 'hadfielj',
                           'vagrant', CURRENT_USER
                       ])
    assert plan[0]['proposed_user'].gecos == '\"test user gecos update\"'
    execute_plan(plan=plan)
    updated_users = Users.from_passwd()
    updated_user = updated_users.describe_users(users_filter=dict(
        name='testuserx1234'))
    assert len(updated_user) == 1
    assert updated_user[0].name == 'testuserx1234'
    assert updated_user[0].uid == 59998
    assert updated_user[0].gid == 1
    assert updated_user[0].gecos == '\"test user gecos update\"'
    assert updated_user[0].shell == '/bin/false'
    assert updated_user[0].public_keys[0].raw == text_type(
        PUBLIC_KEYS[1]['raw'])
    assert updated_user[0].sudoers_entry == 'ALL=(ALL:ALL) ALL'
    delete_test_user_and_group()
示例#16
0
def test_create_and_execute_plan_to_create_identical_user():
    delete_test_user_and_group()
    create_test_user()
    current_users = Users.from_passwd()
    provided_users = Users()
    provided_users.append(
        User(name='testuserx1234', uid=59999, gecos='test user gecos'))
    plan = create_plan(existing_users=current_users,
                       proposed_users=provided_users,
                       protected_users=[
                           'travis', 'couchdb', 'ubuntu', 'nginx', 'hadfielj',
                           'vagrant', CURRENT_USER
                       ])
    execute_plan(plan=plan)
    current_users = Users.from_passwd()
    plan = create_plan(existing_users=current_users,
                       proposed_users=provided_users,
                       protected_users=[
                           'travis', 'couchdb', 'ubuntu', 'nginx', 'hadfielj',
                           'vagrant', CURRENT_USER
                       ])
    assert not plan
    delete_test_user_and_group()
示例#17
0
def test_user_instance_creation():
    name = 'Fred'
    uid = 1024
    gid = 1024
    gecos = 'Fred Bloggs'
    home_dir = '/home/fred'
    shell = '/bin/false'
    public_key = PublicKey(raw=PUBLIC_KEYS[0]['raw'])
    test_user = User(name=name,
                     uid=uid,
                     gid=gid,
                     gecos=gecos,
                     home_dir=home_dir,
                     shell=shell,
                     public_keys=[public_key])
    assert test_user.name == name
    assert test_user.uid == uid
    assert test_user.gid == gid
    # Ensure gecos is surrounded with double quotes
    assert test_user.gecos.startswith('\"') and test_user.gecos.endswith('\"')
    assert test_user.home_dir == home_dir
    assert test_user.shell == shell
    assert test_user.public_keys == [public_key]
示例#18
0
def test_execute_plan_to_update_existing_user_with_multiple_keys():
    """ Create a new user with 2 keys and then replace with a new one """
    create_test_user()
    raw_public_key_1 = PUBLIC_KEYS[0].get('raw')
    public_key_1 = PublicKey(raw=raw_public_key_1)
    raw_public_key_2 = PUBLIC_KEYS[1].get('raw')
    public_key_2 = PublicKey(raw=raw_public_key_2)
    raw_public_key_3 = PUBLIC_KEYS[2].get('raw')
    public_key_3 = PublicKey(raw=raw_public_key_3)
    raw_public_key_4 = PUBLIC_KEYS[3].get('raw')
    public_key_4 = PublicKey(raw=raw_public_key_4)
    current_users = Users.from_passwd()
    provided_users_2 = Users()
    provided_users_2.append(
        User(name='testuserx1234',
             uid=59998,
             gid=1,
             gecos='test user gecos update',
             shell='/bin/false',
             public_keys=[public_key_1, public_key_2]))
    plan = create_plan(existing_users=current_users,
                       proposed_users=provided_users_2,
                       protected_users=[
                           'travis', 'couchdb', 'ubuntu', 'nginx', 'hadfielj',
                           'vagrant', CURRENT_USER
                       ])
    execute_plan(plan=plan)
    updated_users = Users.from_passwd()
    updated_user = updated_users.describe_users(users_filter=dict(
        name='testuserx1234'))
    assert updated_user[0].public_keys[0].raw == text_type(
        PUBLIC_KEYS[0]['raw'])
    assert updated_user[0].public_keys[1].raw == text_type(
        PUBLIC_KEYS[1]['raw'])
    # Replace both keys
    current_users = Users.from_passwd()
    provided_users_3 = Users()
    provided_users_3.append(
        User(name='testuserx1234',
             uid=59998,
             gid=1,
             gecos='test user gecos update',
             shell='/bin/false',
             public_keys=[public_key_3, public_key_4]))
    plan = create_plan(existing_users=current_users,
                       proposed_users=provided_users_3,
                       protected_users=[
                           'travis', 'couchdb', 'ubuntu', 'nginx', 'hadfielj',
                           'vagrant', CURRENT_USER
                       ])
    execute_plan(plan=plan)
    updated_users = Users.from_passwd()
    updated_user = updated_users.describe_users(users_filter=dict(
        name='testuserx1234'))
    assert updated_user[0].public_keys[0].raw == text_type(
        PUBLIC_KEYS[2]['raw'])
    assert updated_user[0].public_keys[1].raw == text_type(
        PUBLIC_KEYS[3]['raw'])
    # Replace one key
    current_users = Users.from_passwd()
    provided_users_4 = Users()
    provided_users_4.append(
        User(name='testuserx1234',
             uid=59998,
             gid=1,
             gecos='test user gecos update',
             shell='/bin/false',
             public_keys=[public_key_2, public_key_4]))
    plan = create_plan(existing_users=current_users,
                       proposed_users=provided_users_4,
                       protected_users=[
                           'travis', 'couchdb', 'ubuntu', 'nginx', 'hadfielj',
                           'vagrant', CURRENT_USER
                       ])
    execute_plan(plan=plan)
    updated_users = Users.from_passwd()
    updated_user = updated_users.describe_users(users_filter=dict(
        name='testuserx1234'))
    assert updated_user[0].public_keys[0].raw == text_type(
        PUBLIC_KEYS[1]['raw'])
    assert updated_user[0].public_keys[1].raw == text_type(
        PUBLIC_KEYS[3]['raw'])
    delete_test_user_and_group()