#!/bin/sh

# autopkgtest check: test two local users via PAM authentication.
# Author: Markus Wanner <markus@bluegap.ch>

set -e

. debian/tests/common.sh

backup_config_files
create_test_users

test_enumeration() {
    ENUM_OUTPUT="$AUTOPKGTEST_ARTIFACTS/authenumerate.out"

    # test authenumerate
    authenumerate_as_courier > $ENUM_OUTPUT

    # the three test users should appear in the output of authenumerate
    grep "alice" $ENUM_OUTPUT
    grep "bob"   $ENUM_OUTPUT
    grep "carol" $ENUM_OUTPUT
}

test_authentication() {
    TEST_OUTPUT="$AUTOPKGTEST_ARTIFACTS/testauth-$1.out"
    authtest_as_courier $@ > $TEST_OUTPUT
}

test_set_password() {
    TEST_OUTPUT="$AUTOPKGTEST_ARTIFACTS/set-password-$1.out"
    echo "testing: $1 $2"
    authtest_as_courier $1 $2 > $TEST_OUTPUT
    grep "Authenticated: $1" $TEST_OUTPUT
}

echo "check enumeration..."
test_enumeration

echo "check authentication..."
test_authentication alice $PASSWORD_ALICE
test_authentication bob   $PASSWORD_BOB
test_authentication carol $PASSWORD_CAROL

echo "check setting password..."
NEW_PASSWORD_ALICE=$(gen_random_password)
test_set_password alice $PASSWORD_ALICE $NEW_PASSWORD_ALICE

# old password should not be valid, anymore
authtest_as_courier alice $PASSWORD_ALICE 2>&1 \
    | grep "Authentication FAILED"

exit 0
