#!/usr/share/ucs-test/runner bash
## desc: Test uidNumber attribute at zarafa objects
## bugs: [31870]
## packages:
##  - zarafa4ucs
## tags:
##  - apptest
## exposure: dangerous

. "$TESTLIBPATH/base.sh" || exit 137
. "$TESTLIBPATH/user.sh" || exit 137
. "$TESTLIBPATH/random.sh" || exit 137
. "/usr/share/univention-lib/all.sh" || exit 137

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

RETVAL=100

stop_udm_cli_server

createZarafaUser () {
	# $1 == username
	# $2 == role (contact, user, admin, store)
	local OPTIONS=""
	if [ "$2" = "contact" ] ; then
		OPTIONS="--option=person --option=mail"
	fi
	udm-test users/user create \
		--position "cn=users,$ldap_base" \
		--set username="$1" \
		--set firstname="Firstname" \
		--set lastname="Lastname" \
		--set password="univention" \
		--set mailPrimaryAddress="$(random_mailaddress)@$domainname" \
		$OPTIONS \
		--set zarafa-role="$2"
	[ ! "$?" = "0" ] && fail_test 140 "cannot create contact $1"
}

for zarafatype in contact store user admin ; do
	echo "Testing zarafa $zarafatype ..."
	USERNAME="z${zarafatype}-$(user_randomname)"
	createZarafaUser "$USERNAME" "$zarafatype"
	wait_for_replication
	univention-ldapsearch -LLL uid="$USERNAME" | grep -q "^uidNumber:" || fail_test 110 "cannot find attribute uidNumber"
	user_remove "$USERNAME"
	wait_for_replication
done

exit $RETVAL
