#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Check group type synchronisation"
## exposure: dangerous
## packages:
## - univention-s4-connector
## tags:
##  - basic

# 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

. /usr/share/univention-lib/ucr.sh
. "s4connector.sh" || exit 137
test -n "$connector_s4_ldap_host" || exit 137
connector_running_on_this_host || exit 137

is_ucr_true connector/s4/mapping/group/grouptype || exit 137

GLOBAL_GROUP_TYPE="-2147483646"
LOCAL_GROUP_TYPE="-2147483643"
DOMAIN_LOCAL_GROUP_TYPE="-2147483644"
UNIVERSAL_GROUP_TYPE="-2147483640"

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

# Groups created in UCS are always global groups in S4
for groupType in 2 3 5; do
	UDM_groups_group_name="$(random_chars)"
	UDM_groups_group_sambaGroupType="$groupType"

	udm_create "groups/group"; fail_bool 0 110
	ad_wait_for_synchronization; fail_bool 0 110

	AD_DN="CN=$UDM_groups_group_name,CN=groups,$(ad_get_base)"
	ad_exists "$AD_DN"; fail_bool 0 110

	ad_verify_attribute "$AD_DN" "groupType" "-2147483646"; fail_bool 0 110
	udm_verify_udm_attribute "sambaGroupType" "$groupType" "groups/group"; fail_bool 0 110

	ad_delete "$AD_DN" || fail_test 110
	ad_wait_for_synchronization; fail_bool 0 110

	ad_exists "$AD_DN"; fail_bool 1 110
	udm_exists "groups/group"; fail_bool 1 110

	wait_for_s4_connector_to_be_inactive
done

for groupType in $GLOBAL_GROUP_TYPE $LOCAL_GROUP_TYPE $DOMAIN_LOCAL_GROUP_TYPE $UNIVERSAL_GROUP_TYPE; do
	UDM_groups_group_name="$(random_chars)"
	UDM_groups_group_adGroupType="$groupType"

	udm_create "groups/group"; fail_bool 0 110
	ad_wait_for_synchronization; fail_bool 0 110

	AD_DN="CN=$UDM_groups_group_name,CN=groups,$(ad_get_base)"
	ad_exists "$AD_DN"; fail_bool 0 110

	ad_verify_attribute "$AD_DN" "groupType" "$groupType"; fail_bool 0 110

	ad_delete "$AD_DN" || fail_test 110
	ad_wait_for_synchronization; fail_bool 0 110

	ad_exists "$AD_DN"; fail_bool 1 110
	udm_exists "groups/group"; fail_bool 1 110

	wait_for_s4_connector_to_be_inactive
done

exit "$RETVAL"
