def test_users_add(mocker): """ Given a user dictionary user.add(..) should utilize `useradd` to create a user with the password """ mocker.patch('subprocess.call') users.add(user_dict) subprocess.call.assert_called_with( ['useradd', '-p', password, '-G', 'wheel,dev', 'kevin'])
def test_users_add(mocker): """ Given a user dictionary. `user.add(...)` should utilize `useradd` to create a user with the password and groups. """ mocker.patch('subprocess.call') users.add(user_dict) subprocess.call.assert_called_with([ 'useradd', '-p', password, '-G', 'wheel,dev', 'raminder', ])
def test_users_add(mocker): """ users.add should run successfully if all requirements are met """ mocker.patch('subprocess.run') assert users.add(user_dict) subprocess.run.assert_called_with(add_command, check=True)
def test_users_add(mocker): """ Given a user dictionary. 'user.add(...)' should create a user with the password and groups using 'useradd' linux command. """ mocker.patch('subprocess.call') users.add(user_dict) subprocess.call.assert_called_with([ 'useradd', '-p', password, '-G', 'wheel,dev', 'kevin', ])
def test_users_useradd_fails(mocker): """ if useradd returns != 0 users.add should return False """ mocker.patch('subprocess.run', side_effect=subprocess.CalledProcessError( add_command, 'command failed')) assert users.add(user_dict) == False
if index == 0: group_str += group else: group_str += ",%s" % group index+=1 # To manually test this you’ll need to (temporarily) run the following from within your project’s directory: sudo pip3.6 install -e . # Then you will be able to run the following to be able to use your module in a REPL without getting permissions # errors for calling out to usermod, userdel, and useradd: sudo python3.6 >>> from hr import users >>> password = '******' >>> user_dict = { ... 'name': 'kevin', ... 'groups': ['wheel'], ... 'password': password ... } >>> users.add(user_dict) Adding user 'kevin' >>> user_dict['groups'] = [] >>> users.update(user_dict) Updating user 'kevin' >>> users.remove(user_dict) Removing user 'kevin' >>>