#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Check whether ou can be recursively moved on ad-side in sync-mode with mixed case"
## exposure: dangerous
## packages:
## - univention-s4-connector
## bugs:
##  - 33482
## versions:
##  3.2-0: fixed

# 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

. "s4connector.sh" || exit 137
test -n "$connector_s4_ldap_host" || exit 137
connector_running_on_this_host || exit 137

does_user_exist_in_ldap ()
{
	dn="$1"
	# if udm-test users/user list | egrep -i "^DN: $dn$"; then
	if udm users/user list | egrep -i "^DN: $dn$"; then
		return 0
	else
		return 1
	fi
}

RETRYREJECTED="$(ucr get connector/s4/retryrejected)"
ad_set_retry_rejected 2

OU1="$(random_chars)A1"
AD_OU1_DN="OU=$OU1,$(ad_get_base)"
OU2="$(random_chars)A2"
AD_OU2_DN="OU=$OU2,$(ad_get_base)"
USER="$(random_chars)"
AD_USER_DN="CN=$USER,OU=$OU1,$(ad_get_base)"
UDM_users_user_username="$USER"

SYNCMODE="$(ad_get_sync_mode)"
ad_set_sync_mode "sync"

section "Create initial Tree structure"

ad_createou "$OU1" || fail_test 110
ad_createou "$OU2" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

ad_createuser "$USER" "" "OU=$OU1,$(ad_get_base)" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

UDM_container_ou_name="$OU1"
udm_exists "container/ou"; fail_bool 0 110
ad_exists "$AD_OU1_DN"; fail_bool 0 110
UDM_container_ou_name="$OU2"
udm_exists "container/ou"; fail_bool 0 110
ad_exists "$AD_OU2_DN"; fail_bool 0 110
does_user_exist_in_ldap "uid=$UDM_users_user_username,ou=$OU1,$ldap_base"; fail_bool 0 110
# udm_exists "users/user" "" "" "ou=$OU1,$ldap_base"; fail_bool 0 110

section "Move $OU1 into $OU2"

ad_move "OU=$OU1,$(ad_get_base)" "OU=$OU1,OU=$OU2,$(ad_get_base)" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

UDM_container_ou_name="$OU1"
udm_exists "container/ou"; fail_bool 1 110
ad_exists "$AD_OU1_DN"; fail_bool 1 110
# udm_exists "users/user" "" "" "ou=$OU1,$ldap_base"; fail_bool 1 110
does_user_exist_in_ldap "uid=$UDM_users_user_username,ou=$OU1,$ldap_base"; fail_bool 1 110

UDM_container_ou_name="$OU2"
udm_exists "container/ou"; fail_bool 0 110
ad_exists "$AD_OU2_DN"; fail_bool 0 110
UDM_container_ou_name="$OU1"
AD_OU1_DN="OU=$OU1,OU=$OU2,$(ad_get_base)"
udm_exists "container/ou" "" "" "ou=$OU2,$ldap_base"; fail_bool 0 110
ad_exists "$AD_OU1_DN"; fail_bool 0 110
AD_USER_DN="CN=$USER,OU=$OU1,OU=$OU2,$(ad_get_base)"
udm users/user list | grep ^DN
# udm_exists "users/user" "" "" "ou=$OU1,ou=$OU2,$ldap_base"; fail_bool 0 110
does_user_exist_in_ldap "uid=$UDM_users_user_username,ou=$OU1,ou=$OU2,$ldap_base"; fail_bool 0 110
ad_exists "$AD_USER_DN"; fail_bool 0 110

section "Clean up"

ad_delete "CN=$USER,OU=$OU1,OU=$OU2,$(ad_get_base)" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110
ad_delete "OU=$OU1,OU=$OU2,$(ad_get_base)" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110
ad_delete "OU=$OU2,$(ad_get_base)" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

UDM_container_ou_name="$OU2"
udm_exists "container/ou"; fail_bool 1 110
ad_exists "$AD_OU2_DN"; fail_bool 1 110
UDM_container_ou_name="$OU1"
udm_exists "container/ou" "" "" "ou=$OU2,$ldap_base"; fail_bool 1 110
ad_exists "$AD_OU1_DN"; fail_bool 1 110
# udm_exists "users/user" "" "" "ou=$OU1,ou=$OU2,$ldap_base"; fail_bool 1 110
does_user_exist_in_ldap "uid=$UDM_users_user_username,ou=$OU1,ou=$OU2,$ldap_base"; fail_bool 1 110
ad_exists "$AD_USER_DN"; fail_bool 1 110

ad_set_retry_rejected "$RETRYREJECTED"
ad_set_sync_mode "$SYNCMODE"

exit "$RETVAL"
