#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: |
##  Update localhost component with authentication via authorized proxy
##  1. setup local repository for maintained component with authentication
##  2. Use minimal proxy implemented in Python
##  3. check if the package is installable
## roles-not: [basesystem]
## tags: [basic]
## packages:
##  - apache2 | apache2-mpm-prefork
## exposure: dangerous

RETVAL=110 # Test fehlgeschlagen
. pool.sh || exit 137
FIXED_12571=false

compname="test${RANDOM}"
username="user${RANDOM}"
password="pass${RANDOM}"

proxyuser="user${RANDOM}"
proxypass="pass${RANDOM}"

setup_apache "${repoprefix}"
htpasswd -c -m -b "${REPODIR}/.htpasswd" "${username}" "${password}"

mkpdir "${_version_version}--component/${compname}" maintained all "${ARCH}"  # 0 1 2 3
mkdeb "${pkgname}" 1 "${ARCH}" "${DIR}"
mkpkg "${DIR}" "${DIR}"

cat <<-EOF >"${DIR}/../.htaccess"
AuthType Basic
AuthUserFile ${REPODIR}/.htpasswd
AuthGroupFile /dev/null
AuthName "${compname}"
Require user ${username}
EOF

eval "$(python proxy.py -f -p 0 -a -u "${proxyuser}" -w "${proxypass}" 3>&-)"
comp="repository/online/component/${compname}"
config_repo "${comp}=yes" "${comp}/parts=maintained" "${comp}/username=${username}" "${comp}/password=${password}" proxy/http="http://${proxyuser}:${proxypass}@localhost:${proxy_port:?}"

(
	set -e
	"${FIXED_12571}" || ucr commit /etc/apt/sources.list.d/20_ucs-online-component.list >&3
	checkapt "http://${username}:${password}@localhost\\(:80\\)\\?/${repoprefix}/" "${DIRS[0]}"
	apt-get -qq update
	apt-get -qq install "${pkgname}"
	dpkg-query -W "${pkgname}" | grep -Fqx "${pkgname}	1"
	checkdeb "${pkgname}" 1
)
# shellcheck disable=SC2181
[ $? -eq 0 ] && RETVAL=100 # Test bestanden (Keine Fehler)
dpkg -P --force-all "${pkgname}" >&3 2>&3
kill "${proxy_pid:?}" 2>&3

exit ${RETVAL}
# vim:set ft=sh:
