#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: |
##  Update localhost with component authentication failure
##  1. setup local repository
##  2. setup maintained component with is unavailable (due wrong authentication)
##  3. check update still possible
## roles-not: [basesystem]
## tags: [basic]
## packages:
##  - apache2 | apache2-mpm-prefork
## bugs: [22567]
## versions:
##  2.4-2: found
## exposure: dangerous

. pool.sh || exit 137

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

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

mkpdir "${_version_version}-0" maintained "${ARCH}"  # 0 1
# 0:dists/ucs500/main/binary-amd64/
# 1:dists/errata500/main/binary-amd64/
mkdeb "${pkgname}" 1 "${ARCH}" "${DIR_POOL}"
mkpkg "${DIR}" "${DIR_POOL}"

declare -a BDIR=("${DIRS[@]}")

mkpdir "${_version_version}-1" maintained "${ARCH}"  # 2 3
# 2:dists/ucs501/main/binary-amd64/
# 3:dists/errata501/main/binary-amd64/
mkdeb "${pkgname}" 2 "${ARCH}" "${DIR_POOL}"
mkpkg "${DIR}" "${DIR_POOL}"
dirs_except "${BDIR[@]}" && declare -a CDIR=("${result[@]}")

patchlevel=0 mkpdir "${_version_version}--component/${compname}" maintained all "${ARCH}"  # 4 5
# 4:5.0/maintained/component/test00000/all/
# 5:5.0/maintained/component/test00000/amd64/
mkdeb "${pkgname}c" 1 "${ARCH}" "${DIR}"
mkpkg "${DIR}" "${DIR}"

python3 create_releases_json.py "${REPODIR}" "${_version_version}-0" "${_version_version}-1"

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

comp="repository/online/component/${compname}"
config_repo \
	version/patchlevel=0 \
	version/erratalevel=0 \
	"${comp}=yes" \
	"${comp}/username=${username}" \
	"${comp}/password=DUMMY"

	set -e
	checkapt "http://localhost\\(:80\\)\\?/${repoprefix}/" "${BDIR[@]}"
	apt-get -qq update
	apt-get -qq install "${pkgname}"
	dpkg-query -W "${pkgname}" | grep -Fqx "${pkgname}	1"
	/usr/share/univention-updater/univention-updater net
	checkapt "http://localhost\\(:80\\)\\?/${repoprefix}/" "${CDIR[@]}"
	msg="Configuration error: " # credentials not accepted | component not found
	grep -F "$msg" /etc/apt/sources.list.d/20_ucs-online-component.list
	dpkg-query -W "${pkgname}" | grep -Fqx "${pkgname}	2"
	test "${_version_version}" = "$(ucr get version/version)"
	test 1 = "$(ucr get version/patchlevel)"
	checkdeb "${pkgname}" 1 2
# vim:set ft=sh:
