#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Create two groups on ad-side and nest them on ucs-side in sync-mode. This will fail on w2k and w2k3. Can't consider that in the test, as I can't distinguish between w2k3 and w2k8 yet."
## 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

INNER_GROUP="$(random_chars)"
OUTER_GROUP="$(random_chars)"

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

ad_group_create "$INNER_GROUP" || fail_test 110
ad_group_create "$OUTER_GROUP" || fail_test 110

ad_wait_for_synchronization; fail_bool 0 110

UDM_groups_group_name="$INNER_GROUP"
udm_modify "groups/group" "" "" "" "" \
	--append memberOf="cn=$OUTER_GROUP,cn=groups,$ldap_base" || fail_test 110

ad_wait_for_synchronization; fail_bool 0 110

ad_exists "CN=$OUTER_GROUP,CN=groups,$(ad_get_base)"; fail_bool 0 110
ad_exists "CN=$INNER_GROUP,CN=groups,$(ad_get_base)"; fail_bool 0 110

UDM_groups_group_name="$INNER_GROUP"
udm_exists "groups/group"; fail_bool 0 110
UDM_groups_group_name="$OUTER_GROUP"
udm_exists "groups/group"; fail_bool 0 110

UDM_groups_group_name="$OUTER_GROUP"
udm_verify_multi_value_udm_attribute_contains "nestedGroup" \
	"cn=$INNER_GROUP,cn=groups,$ldap_base" "groups/group"; fail_bool 0 110
UDM_groups_group_name="$INNER_GROUP"
udm_verify_multi_value_udm_attribute_contains "memberOf" \
	"cn=$OUTER_GROUP,cn=groups,$ldap_base" "groups/group"; fail_bool 0 110

ad_verify_multi_value_attribute_contains "CN=$OUTER_GROUP,CN=groups,$(ad_get_base)" \
	"member" "CN=$INNER_GROUP,CN=groups,$(ad_get_base)"; fail_bool 0 110
ad_verify_multi_value_attribute_contains "CN=$INNER_GROUP,CN=groups,$(ad_get_base)" \
	"memberOf" "CN=$OUTER_GROUP,CN=groups,$(ad_get_base)"; fail_bool 0 110

section "Clean up"

ad_delete "CN=$OUTER_GROUP,CN=groups,$(ad_get_base)" || fail_test 110
ad_delete "CN=$INNER_GROUP,CN=groups,$(ad_get_base)" || fail_test 110

ad_set_sync_mode "$SYNCMODE"

exit "$RETVAL"
