def test_invalid_user(binddn, bindpwdfile):
    user = '******'
    args = univention_lastbind.parse_args(
        ['--user', user, '--binddn', binddn, '--bindpwdfile', bindpwdfile])
    with pytest.raises(univention_lastbind.ScriptError) as excinfo:
        univention_lastbind.main(args)
    assert 'The provided user "%s" could not be found' % (user, ) in str(
        excinfo.value)
def test_main_single_server(activate_lastbind, binddn, bindpwdfile, testudm,
                            readudm):
    o = readudm.obj_by_dn(testudm.create_user()[0])
    assert o.props.lastbind is None
    timestamp = bind_for_timestamp(o.dn)
    assert timestamp is not None
    args = univention_lastbind.parse_args(
        ['--binddn', binddn, '--bindpwdfile', bindpwdfile, '--user', o.dn])
    univention_lastbind.main(args)
    o.reload()
    assert o.props.lastbind == timestamp
def test_main_multi_server(activate_lastbind, binddn, bindpwdfile, testudm,
                           readudm, other_server):
    assert other_server is not None
    o = readudm.obj_by_dn(testudm.create_user()[0])
    assert o.props.lastbind is None
    local_timestamp = bind_for_timestamp(o.dn)
    assert local_timestamp is not None
    time.sleep(2)
    other_timestamp = bind_for_timestamp(o.dn, other_server.props.fqdn)
    assert other_timestamp is not None
    youngest_timestamp = max(local_timestamp, other_timestamp)
    assert youngest_timestamp is not None
    args = univention_lastbind.parse_args(
        ['--binddn', binddn, '--bindpwdfile', bindpwdfile, '--user', o.dn])
    univention_lastbind.main(args)
    o.reload()
    assert o.props.lastbind == youngest_timestamp
def test_tracebacks(binddn, failbinddn, bindpwdfile, failbindpwdfile, ucr):
    args = univention_lastbind.parse_args(
        ['--user', 'foo', '--binddn', binddn])
    with pytest.raises(univention_lastbind.ScriptError) as excinfo:
        univention_lastbind.main(args)
    assert '"binddn" provided but not "bindpwdfile".' in str(excinfo.value)

    args = univention_lastbind.parse_args([
        '--user', 'foo', '--binddn', binddn, '--bindpwdfile', failbindpwdfile
    ])
    with pytest.raises(univention_lastbind.ScriptError) as excinfo:
        univention_lastbind.main(args)
    assert 'Could not open "bindpwdfile" "%s"' % (failbindpwdfile) in str(
        excinfo.value)

    args = univention_lastbind.parse_args([
        '--user', 'foo', '--binddn', failbinddn, '--bindpwdfile', bindpwdfile
    ])
    with pytest.raises(univention_lastbind.ScriptError) as excinfo:
        univention_lastbind.main(args)
    assert 'Could not connect to server "%s" with provided "binddn" "%s" and "bindpwdfile" "%s"' % (
        ucr.get('ldap/server/name'), failbinddn, bindpwdfile)
def test_main_not_enough_arguments():
    args = univention_lastbind.parse_args([])
    with pytest.raises(univention_lastbind.ScriptError) as excinfo:
        univention_lastbind.main(args)
    assert 'Provide either --user USER or --allusers.' in str(excinfo.value)