#!/usr/share/ucs-test/runner bash
## desc: Create OX users with all attributes
## bugs: [34140]
## packages:
##  - univention-ox (>= 6.0.0-2)
## tags:
##  - apptest
## exposure: dangerous

. "/usr/share/univention-lib/all.sh" || exit 137
. "ox.sh" || exit 137

eval "$(/usr/sbin/ucr shell)"

RETVAL=100

username="$(user_randomname)"
firstname="$(user_randomname)"
lastname="$(user_randomname)"
mail="$(random_mailaddress)@$domainname"

GLOBAL_CONTACT_FOLDER=6
FIXED_DATE="1957-02-22"
FIXED_LANGUAGE="de_DE"
FIXED_TIMEZONE="Europe/Berlin"

randomize_user () {
	local suffix="$1"
	attr=()
	while IFS=, read objectClass module ldapMapping name shortDescription longDescription translationShortDescription translationLongDescription tabName translationTabName overwriteTab valueRequired CLIName syntax tabAdvanced default mayChange multivalue deleteObjectClass options tabPosition hook overwritePosition doNotSearch oxOption oxField groupName translationGroupName groupPosition
	do
		name="${name//\"}" CLIName="${CLIName//\"}"
		[ "${module//\"}" = "users/user" ] || continue
		[ "${mayChange//\"}" = "1" ] || continue

		case "$name" in
		isOxUser) attr+=(--set "$CLIName=OK") ; continue ;;
		oxBirthday|oxAnniversary) attr+=(--set "$CLIName=$FIXED_DATE") ; continue ;;
		esac

		case "${syntax//\"}" in
		string) attr+=(--set "$CLIName=${name}${suffix}") ;;
		emailAddress) attr+=(--set "$CLIName=${mail%@*}${suffix}@${mail##*@}") ;;
		OkOrNot) attr+=(--set "$CLIName=OK") ;;
		oxaccess) attr+=(--set "$CLIName=premium") ;;
		oxlanguage) attr+=(--set "$CLIName=$FIXED_LANGUAGE") ;;
		oxtimezone) attr+=(--set "$CLIName=$FIXED_TIMEZONE") ;;
		esac
	done </usr/share/univention-ox/attrlist.csv
	[ -n "$attr" ] ||
		fail_test 110 "Failed to build arguments to create user"
	echo "User '$username' with ${attr[*]}"
}

check_vcard () {
	local X="$1"
	declare -a expected=(
		"FN:oxDisplayName$X"
		"N:$lastname;$firstname;oxMiddleName$X;;oxSuffix$X"
		"NICKNAME:oxNickName$X"
		"BDAY:${FIXED_DATE//-}"
		"ADR;TYPE=work:;;;;oxStateBusiness$X;;oxCountryBusiness$X"
		"ADR;TYPE=home:;;oxStreetHome$X;oxCityHome$X;oxStateHome$X;oxPostalCodeHome$X;oxCountryHome$X"
		"ADR;TYPE=dom:;;oxStreetOther$X;oxCityOther$X;oxStateOther$X;oxPostalCodeOther$X;oxCountryOther$X"
		"TEL;TYPE=work,fax:oxFaxBusiness$X"
		"TEL;TYPE=car,voice:oxTelephoneCar$X"
		"TEL;TYPE=home,fax:oxFaxHome$X"
		"TEL;TYPE=cell,voice,pref:oxMobileBusiness$X"
		"TEL;TYPE=dom,voice:oxTelephoneOther$X"
		"TEL;TYPE=dom,fax:oxFaxOther$X"
		"EMAIL;TYPE=INTERNET,work:$mail"
		"EMAIL;TYPE=INTERNET,home:${mail%@*}${X}@${mail##*@}"
		"EMAIL;TYPE=INTERNET,other:${mail%@*}${X}@${mail##*@}"
		"TITLE:oxProfession$X"
		"ROLE:oxPosition$X"
		"ORG:;oxBranches$X;oxDepartment$X"
		"NOTE:oxNote$X"
		"URL:oxUrl$X"
		"IMPP;TYPE=work:oxInstantMessenger1$X"
		"IMPP;TYPE=home:oxInstantMessenger2$X"
	)
	local session tmp line
	tmp=$(mktemp)
	session=$(ox_session)
	cmd=(ox_ajax export action=VCARD session="$session" folder="$GLOBAL_CONTACT_FOLDER")
	if "${cmd[@]}" >"${OX_TMP}/vcard"
	then
		ldapsearch-wrapper <"${OX_TMP}/vcard" >"${OX_TMP}/vcard2"
		for line in "${expected[@]}"
		do
			grep -F -e "$line" "${OX_TMP}/vcard2" || fail_test 110 "Missing $line"
		done
	else
		fail_test 110 "Failed to fetch VCARDs"
	fi
}

randomize_user 0
create_ox_user "$username" "$mail" "$firstname" "$lastname" "${attr[@]}"

wait_for_replication

ox_login "$username" "univention" || fail_test 110 "Login as OX user was denied"
check_vcard 0

new_password="$(random_string)"
randomize_user 1
change_password "$username" "$new_password" "${attr[@]}"

wait_for_replication

ox_login "$username" "$new_password" || fail_test 110 "Login as OX user with the new password was denied"
check_vcard 1

user_remove "$username"

wait_for_replication

exit $RETVAL
# vim:set ft=sh:
