#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Check User-Mail-Attributes in read-mode (Exchange attribute-checks disabled)"
## exposure: dangerous
## packages:
## - univention-ad-connector

# shellcheck source=../../lib/base.sh
. "$TESTLIBPATH/base.sh" || exit 137
# shellcheck source=../../lib/udm.sh
. "$TESTLIBPATH/udm.sh" || exit 137
# shellcheck source=../../lib/random.sh
. "$TESTLIBPATH/random.sh" || exit 137

. "adconnector.sh" || exit 137
test -n "$connector_ad_ldap_host" || exit 137


UDM_users_user_username="$(random_chars)"
UDM_users_user_lastname="$(random_chars)"
# If the password doesn't adhere the configured Windows-Password-Guidelines
# weird things might happen when the user is synced to AD.
UDM_users_user_password="U$(random_chars)123"
UDM_users_user_firstname="$(random_chars)"
UDM_users_user_description="$(random_chars)"
UDM_users_user_street="$(random_chars)"
UDM_mail_domain_name="$(random_chars)"
UDM_users_user_mailPrimaryAddress="$(random_chars)@${UDM_mail_domain_name}"
#UDM_users_user_Exchange_Homeserver="$(random_chars)"
#UDM_users_user_Exchange_homeMDB="$(random_chars)"
#UDM_users_user_Exchange_Nickname="$(random_chars)"
AD_DN="CN=$UDM_users_user_username,CN=users,$(ad_get_base)"

SYNCMODE="$(ad_get_sync_mode)"
SYNCPRIMARYMAIL="$(ucr get connector/ad/mapping/user/primarymail)"
#SYNCEXCHANGE="$(ucr get connector/ad/mapping/user/exchange)"
udm_create mail/domain "" "" "cn=domain,cn=mail,$ldap_base"

invoke-rc.d univention-ad-connector stop
ucr set connector/ad/mapping/user/primarymail=true
#ucr set connector/ad/mapping/user/exchange=true
ad_set_sync_mode "read"
invoke-rc.d univention-ad-connector start

cleanup() {
	echo ==========Test done - Cleanup==========

	udm_remove "users/user" || fail_test 110
	ad_wait_for_synchronization; fail_bool 0 110

	udm_exists "users/user"; fail_bool 1 110
	ad_exists "$AD_DN"; fail_bool 1 110

	udm_remove mail/domain "" "" "cn=domain,cn=mail,$ldap_base"
	invoke-rc.d univention-ad-connector stop
	ucr set connector/ad/mapping/user/primarymail="$SYNCPRIMARYMAIL"
	#ucr set connector/ad/mapping/user/exchange="$SYNCEXCHANGE"
	ad_set_sync_mode "$SYNCMODE"
	invoke-rc.d univention-ad-connector start
}
trap cleanup EXIT

## Boring test:
## Creating a user in UDM/OpenLDAP in read mode should not sync the user to AD,
## so all the remaning tests just assure that nothing happens in AD.

section "==========Create user in UDM and set mailPrimaryAddress=========="
udm_create "users/user" "" "" "" "" \
	--option=person --option=mail --option=posix --option=samba --option=pki  --option=kerberos|| fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

udm_exists "users/user"; fail_bool 0 110
ad_exists "$AD_DN"; fail_bool 1 110

udm_verify_udm_attribute "mailPrimaryAddress" "$UDM_users_user_mailPrimaryAddress" "users/user"; fail_bool 0 110
#udm_verify_udm_attribute "Exchange-Homeserver" "$UDM_users_user_Exchange_Homeserver" "users/user"; fail_bool 0 110
#udm_verify_udm_attribute "Exchange-homeMDB" "$UDM_users_user_Exchange_homeMDB" "users/user"; fail_bool 0 110
#udm_verify_udm_attribute "Exchange-Nickname" "$UDM_users_user_Exchange_Nickname" "users/user"; fail_bool 0 110

#UDM_users_user_Exchange_Homeserver="$(random_chars)"
#UDM_users_user_Exchange_homeMDB="$(random_chars)"
#UDM_users_user_Exchange_Nickname="$(random_chars)"
UDM_users_user_mailPrimaryAddress="$(random_chars)@${UDM_mail_domain_name}"
udm_modify "users/user" "" "" "" "" \
		   --set mailPrimaryAddress="$UDM_users_user_mailPrimaryAddress" || fail_test 110
#		   --set Exchange-Homeserver="$UDM_users_user_Exchange_Homeserver" \
#		   --set Exchange-homeMDB="$UDM_users_user_Exchange_homeMDB" \
#		   --set Exchange-Nickname="$UDM_users_user_Exchange_Nickname" \
ad_wait_for_synchronization; fail_bool 0 110
ad_exists "$AD_DN"; fail_bool 1 110
udm_verify_udm_attribute "mailPrimaryAddress" "$UDM_users_user_mailPrimaryAddress" "users/user"; fail_bool 0 110
#udm_verify_udm_attribute "Exchange-Homeserver" "$UDM_users_user_Exchange_Homeserver" "users/user"; fail_bool 0 110
#udm_verify_udm_attribute "Exchange-homeMDB" "$UDM_users_user_Exchange_homeMDB" "users/user"; fail_bool 0 110
#udm_verify_udm_attribute "Exchange-Nickname" "$UDM_users_user_Exchange_Nickname" "users/user"; fail_bool 0 110

exit "$RETVAL"
