#! /bin/sh

set -e

# PATH TO VERIFY
CRONTABS_DIR=/var/spool/cron/crontabs
ALLOW_FILE=/etc/cron.allow
DENY_FILE=/etc/cron.deny


dir=$(pwd)/debian/tests
. $dir/textutils.sh

frame "Here we are testing the patch" \
      "features/Debian-paths-and-commands.patch" \
      "" \
      "First of all we check path ${CRONTABS_DIR}"

echo "================= adding user 'dummy' ====================="
getent passwd dummy || adduser --disabled-password --gecos "" dummy

echo "========= make user 'dummy' fill his crontab =============="
# stderr must be deactivated since crontab will report there
# that there is no initial crontab for user "dummy"
su dummy -c "EDITOR=$(pwd)/debian/tests/cronfiller.py crontab -e" 2>/dev/null

echo "========== testing the crontab of user 'dummy' ============="

expected_crontab_filename=${CRONTABS_DIR}/dummy
[ -f ${expected_crontab_filename} ] && grep "* * * * * /bin/ls" ${expected_crontab_filename}

crontab -u dummy -r

frame "Now we check path ${DENY_FILE}"

rm -f ${ALLOW_FILE}
echo dummy > ${DENY_FILE}

su dummy -c "EDITOR=$(pwd)/debian/tests/cronfiller.py crontab -e" 2>&1 |grep "not allowed"

frame "Now we check path ${ALLOW_FILE}"

echo goofy > ${ALLOW_FILE}
rm -f ${DENY_FILE}

su dummy -c "EDITOR=$(pwd)/debian/tests/cronfiller.py crontab -e" 2>&1 |grep "not allowed"

echo dummy > ${ALLOW_FILE}
echo dummy > ${DENY_FILE}

# allow take precedence
su dummy -c "EDITOR=$(pwd)/debian/tests/cronfiller.py crontab -e" 2>/dev/null

echo "========== test ok ========================================="

rm -f ${DENY_FILE}
rm -f ${ALLOW_FILE}
