#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Check whether the Container-Ignore-Subtree is respected on ucs-side in write-mode"
## exposure: dangerous
## packages:
## - univention-s4-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


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

CONTAINER1="$(random_chars)"
CONTAINER2="$(random_chars)"

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

UDM_container_cn_name="$CONTAINER1"
AD_CONTAINER_DN="CN=$UDM_container_cn_name,$(ad_get_base)"
udm_create "container/cn" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

udm_exists "container/cn"; fail_bool 0 110
ad_exists "$AD_CONTAINER_DN"; fail_bool 0 110

section "Modify Container-Ignore-Subtree"

invoke-rc.d univention-s4-connector stop



connector_mapping_adjust 'container' "cn=$CONTAINER1,$ldap_base"

ad_set_sync_mode "write"
invoke-rc.d univention-s4-connector start

section "Create container"
UDM_container_cn_name="$CONTAINER2"
AD_CONTAINER_DN="CN=$UDM_container_cn_name,CN=$CONTAINER1,$(ad_get_base)"

udm_create "container/cn" "" "" "cn=$CONTAINER1,$ldap_base" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

udm_exists  "container/cn" "" "" "cn=$CONTAINER1,$ldap_base"; fail_bool 0 110
ad_exists "$AD_CONTAINER_DN"; fail_bool 1 110

section "Clean up"

udm_remove "container/cn" "" "" "cn=$CONTAINER1,$ldap_base" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

ad_exists "$AD_CONTAINER_DN"; fail_bool 1 110
udm_exists  "container/cn" "" "" "cn=$CONTAINER1,$ldap_base"; fail_bool 1 110

invoke-rc.d univention-s4-connector stop
connector_mapping_restore
ad_set_sync_mode "sync"

UDM_container_cn_name="$CONTAINER1"
AD_CONTAINER_DN="CN=$UDM_container_cn_name,$(ad_get_base)"
udm_remove "container/cn" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

ad_exists "$AD_CONTAINER_DN"; fail_bool 1 110
udm_exists  "container/cn"; fail_bool 1 110

ad_set_sync_mode "$SYNCMODE"

exit "$RETVAL"
