#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Create and modify AD-User with umlauts in write-mode"
## exposure: dangerous
## packages:
## - univention-ad-connector
## tags:
##  - skip_admember

# 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)ä"
UDM_users_user_firstname="ü$(random_chars)ß"
UDM_users_user_description="ß$(random_chars)ü"
UDM_users_user_street="ä$(random_chars)ö"
UDM_users_user_city="ö$(random_chars)ä"
UDM_users_user_postcode="ü$(random_chars)ß"
UDM_users_user_profilepath="ß$(random_chars)ü"
UDM_users_user_scriptpath="ä$(random_chars)ö"
AD_DN="CN=$UDM_users_user_username,CN=users,$(ad_get_base)"

SYNCMODE="$(ad_get_sync_mode)"

ad_set_sync_mode "write"

ad_createuser "$UDM_users_user_username" || fail_test 110
ad_set_attribute "$AD_DN" "givenName" "$UDM_users_user_firstname"; fail_bool 0 110
ad_set_attribute "$AD_DN" "sn" "$UDM_users_user_lastname"; fail_bool 0 110
ad_set_attribute "$AD_DN" "description" "$UDM_users_user_description"; fail_bool 0 110
ad_set_attribute "$AD_DN" "streetAddress" "$UDM_users_user_street"; fail_bool 0 110
ad_set_attribute "$AD_DN" "l" "$UDM_users_user_city"; fail_bool 0 110
ad_set_attribute "$AD_DN" "postalCode" "$UDM_users_user_postcode"; fail_bool 0 110
ad_set_attribute "$AD_DN" "profilePath" "$UDM_users_user_profilepath"; fail_bool 0 110
ad_set_attribute "$AD_DN" "scriptPath" "$UDM_users_user_scriptpath"; fail_bool 0 110
ad_wait_for_synchronization; fail_bool 0 110

ad_verify_attribute	"$AD_DN" "sAMAccountName" "$UDM_users_user_username"; fail_bool 0 110
ad_verify_attribute "$AD_DN" "givenName" "$UDM_users_user_firstname"; fail_bool 0 110
ad_verify_attribute "$AD_DN" "sn" "$UDM_users_user_lastname"; fail_bool 0 110
ad_verify_attribute "$AD_DN" "description" "$UDM_users_user_description"; fail_bool 0 110
ad_verify_attribute "$AD_DN" "streetAddress" "$UDM_users_user_street"; fail_bool 0 110
ad_verify_attribute "$AD_DN" "l" "$UDM_users_user_city"; fail_bool 0 110
ad_verify_attribute "$AD_DN" "postalCode" "$UDM_users_user_postcode"; fail_bool 0 110
ad_verify_attribute "$AD_DN" "profilePath" "$UDM_users_user_profilepath"; fail_bool 0 110
ad_verify_attribute "$AD_DN" "scriptPath" "$UDM_users_user_scriptpath"; fail_bool 0 110

udm_exists "users/user"; fail_bool 1 110

ad_delete "$AD_DN" || 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

ad_set_sync_mode "$SYNCMODE"

exit "$RETVAL"
