#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Create and modify AD-Group and verify attributes in sync-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
[ -n "${connector_s4_ldap_host:-}" ] || exit 137
connector_running_on_this_host || exit 137

UDM_groups_group_name="$(random_chars)"
UDM_groups_group_description="$(random_chars)"
AD_DN="CN=$UDM_groups_group_name,CN=groups,$(ad_get_base)"

SYNCMODE="$(ad_get_sync_mode)"

ad_set_sync_mode "sync"

ad_group_create "$UDM_groups_group_name" || fail_test 110

UDM_groups_group_description="$(random_chars)"

ad_set_attribute "$AD_DN" "description" "$UDM_groups_group_description" || fail_test 110

ad_wait_for_synchronization; fail_bool 0 110

UDM_groups_group_gidNumber=$(univention-ldapsearch "(&(cn=$UDM_groups_group_name)(objectClass=univentionGroup))" gidNumber | VAL gidNumber)

ad_verify_attribute "$AD_DN" "description" "$UDM_groups_group_description"; fail_bool 0 110
ad_verify_attribute "$AD_DN" "gidNumber" "$UDM_groups_group_gidNumber"; fail_bool 0 110

if [ "${connector_s4_windows_version:-}" != "win2000" ] || [ "${connector_s4_mapping_group_win2000_description:-}" != "false" ]
then
	udm_verify_udm_attribute "description" "$UDM_groups_group_description" "groups/group"; fail_bool 0 110
fi

udm_remove "groups/group" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

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

ad_set_sync_mode "$SYNCMODE"

exit "$RETVAL"
