async def test_saving_loading(data, hass):
    """Test saving and loading JSON."""
    data.add_user('test-user', 'test-pass')
    data.add_user('second-user', 'second-pass')
    await data.async_save()

    data = hass_auth.Data(hass)
    await data.async_load()
    data.validate_login('test-user', 'test-pass')
    data.validate_login('second-user', 'second-pass')
Esempio n. 2
0
def test_change_password_invalid_user(capsys):
    """Test changing password of non-existing user."""
    data = hass_auth.Data(MOCK_PATH, None)
    data.add_user('test-user', 'test-pass')

    with patch.object(data, 'save') as mock_save:
        script_auth.change_password(
            data, Mock(username='******', new_password='******'))

    assert len(mock_save.mock_calls) == 0
    captured = capsys.readouterr()
    assert captured.out == 'User not found\n'
    data.validate_login('test-user', 'test-pass')
    with pytest.raises(hass_auth.InvalidAuth):
        data.validate_login('invalid-user', 'new-pass')
Esempio n. 3
0
def test_change_password(capsys):
    """Test we can change a password."""
    data = hass_auth.Data(MOCK_PATH, None)
    data.add_user('test-user', 'test-pass')

    with patch.object(data, 'save') as mock_save:
        script_auth.change_password(
            data, Mock(username='******', new_password='******'))

    assert len(mock_save.mock_calls) == 1
    captured = capsys.readouterr()
    assert captured.out == 'Password changed\n'
    data.validate_login('test-user', 'new-pass')
    with pytest.raises(hass_auth.InvalidAuth):
        data.validate_login('test-user', 'test-pass')
Esempio n. 4
0
def test_add_user(capsys):
    """Test we can add a user."""
    data = hass_auth.Data(MOCK_PATH, None)

    with patch.object(data, 'save') as mock_save:
        script_auth.add_user(
            data, Mock(username='******', password='******'))

    assert len(mock_save.mock_calls) == 1

    captured = capsys.readouterr()
    assert captured.out == 'User created\n'

    assert len(data.users) == 1
    data.validate_login('paulus', 'test-pass')
Esempio n. 5
0
def test_list_user(capsys):
    """Test we can list users."""
    data = hass_auth.Data(MOCK_PATH, None)
    data.add_user('test-user', 'test-pass')
    data.add_user('second-user', 'second-pass')

    script_auth.list_users(data, None)

    captured = capsys.readouterr()

    assert captured.out == '\n'.join([
        'test-user',
        'second-user',
        '',
        'Total users: 2',
        ''
    ])
Esempio n. 6
0
def test_validate_login(capsys):
    """Test we can validate a user login."""
    data = hass_auth.Data(MOCK_PATH, None)
    data.add_user('test-user', 'test-pass')

    script_auth.validate_login(
        data, Mock(username='******', password='******'))
    captured = capsys.readouterr()
    assert captured.out == 'Auth valid\n'

    script_auth.validate_login(
        data, Mock(username='******', password='******'))
    captured = capsys.readouterr()
    assert captured.out == 'Auth invalid\n'

    script_auth.validate_login(
        data, Mock(username='******', password='******'))
    captured = capsys.readouterr()
    assert captured.out == 'Auth invalid\n'
Esempio n. 7
0
def run(args):
    """Handle Home Assistant auth provider script."""
    parser = argparse.ArgumentParser(
        description=("Manage Home Assistant users"))
    parser.add_argument('--script', choices=['auth'])
    parser.add_argument(
        '-c',
        '--config',
        default=get_default_config_dir(),
        help="Directory that contains the Home Assistant configuration")

    subparsers = parser.add_subparsers(dest='func')
    subparsers.required = True
    parser_list = subparsers.add_parser('list')
    parser_list.set_defaults(func=list_users)

    parser_add = subparsers.add_parser('add')
    parser_add.add_argument('username', type=str)
    parser_add.add_argument('password', type=str)
    parser_add.set_defaults(func=add_user)

    parser_validate_login = subparsers.add_parser('validate')
    parser_validate_login.add_argument('username', type=str)
    parser_validate_login.add_argument('password', type=str)
    parser_validate_login.set_defaults(func=validate_login)

    parser_change_pw = subparsers.add_parser('change_password')
    parser_change_pw.add_argument('username', type=str)
    parser_change_pw.add_argument('new_password', type=str)
    parser_change_pw.set_defaults(func=change_password)

    args = parser.parse_args(args)
    loop = asyncio.get_event_loop()
    hass = HomeAssistant(loop=loop)
    hass.config.config_dir = os.path.join(os.getcwd(), args.config)
    data = hass_auth.Data(hass)
    loop.run_until_complete(data.async_load())
    loop.run_until_complete(args.func(data, args))
def data(hass):
    """Create a loaded data class."""
    data = hass_auth.Data(hass)
    hass.loop.run_until_complete(data.async_load())
    return data