#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Create 'known' group on ucs-side in sync-mode"
## exposure: dangerous
## packages:
## - univention-ad-connector
## tags:
##  - groupsync
##  - 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

. /usr/share/univention-lib/ucr.sh

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

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

section "Create outer group"

UDM_groups_group_name="$OUTER_GROUP"
udm_create "groups/group" || fail_test 110

section "Create inner group"

#'Known' UCS-Groups are expected not to be synced to AD if connector/ad/mapping/group/grouptype to false
UDM_groups_group_name="$INNER_GROUP"
udm_create "groups/group" "" "" "" "" --set sambaGroupType=5 || fail_test 110

section "Make $INNER_GROUP member of $OUTER_GROUP"

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

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

if is_ucr_true connector/ad/mapping/group/grouptype; then
	ad_exists "CN=$INNER_GROUP,CN=groups,$(ad_get_base)"; fail_bool 0 110
else
	ad_exists "CN=$INNER_GROUP,CN=groups,$(ad_get_base)"; fail_bool 1 110
fi
ad_exists "CN=$OUTER_GROUP,CN=groups,$(ad_get_base)"; fail_bool 0 110

udm_verify_udm_attribute "sambaGroupType" "5" "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

if is_ucr_true connector/ad/mapping/group/grouptype; then
	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
else
	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 1 110
fi

section "Clean up"

UDM_groups_group_name="$OUTER_GROUP"
udm_remove "groups/group" || fail_test 110
UDM_groups_group_name="$INNER_GROUP"
udm_remove "groups/group" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

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

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

ad_set_sync_mode "$SYNCMODE"

exit "$RETVAL"
