#!/usr/share/ucs-test/runner bash
## desc: "Remove user from group both starting with capital letters on ad-side in read mode"
## exposure: dangerous
## packages:
## - univention-ad-connector
## tags:
##  - groupsync
##  - skip_admember

. "$TESTLIBPATH/base.sh" || exit 137
. "$TESTLIBPATH/udm.sh" || exit 137
. "$TESTLIBPATH/random.sh" || exit 137 

. "adconnector.sh" || exit 137
test -n "$connector_ad_ldap_host" || exit 137


UDM_groups_group_name="A$(random_chars)"
UDM_users_user_username="B$(random_chars)"
AD_GROUP_DN="CN=$UDM_groups_group_name,CN=groups,$(ad_get_base)"
UDM_GROUP_DN="cn=$UDM_groups_group_name,cn=groups,$ldap_base"
AD_USER_DN="CN=$UDM_users_user_username,CN=Users,$(ad_get_base)"

SYNCMODE="$(ad_get_sync_mode)"

ad_set_sync_mode "read"

section "Create user and group"

## Now change the timing: First crate group and user  and then change memberships later:
/etc/init.d/univention-ad-connector stop

ad_group_create "$UDM_groups_group_name" || fail_test 110
ad_createuser "$UDM_users_user_username" || fail_test 110

## And now continue the ADC:
/etc/init.d/univention-ad-connector start

## Now wait for the sync before modifying the membership
sleep 20
section "Add user to group"

ad_append_to_attribute "$AD_GROUP_DN" "member" "$AD_USER_DN" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

ad_exists "$AD_GROUP_DN"; fail_bool 0 110
udm_exists "groups/group"; fail_bool 0 110
ad_exists "$AD_USER_DN"; fail_bool 0 110
udm_exists "users/user"; fail_bool 0 110

udm_verify_multi_value_udm_attribute_contains "users" \
	"uid=$UDM_users_user_username,cn=users,$ldap_base" "groups/group"; # fail_bool 0 121 "See #18433"
rc=$?
if [ "$rc" != 0 ]; then
	echo "ERROR: TEST FAILED:"
	univention-ldapsearch cn="$UDM_groups_group_name" memberUid uniqueMember
	read -p "Hit return to continue with teardown> "
	false || fail_bool 0 121 "See #18433"
fi
udm_verify_multi_value_udm_attribute_contains "groups" \
	"cn=$UDM_groups_group_name,cn=groups,$ldap_base" "users/user"; fail_bool 0 110
ad_verify_multi_value_attribute_contains "$AD_GROUP_DN" "member" "$AD_USER_DN"; fail_bool 0 110
ad_verify_multi_value_attribute_contains "$AD_USER_DN" "memberOf" "$AD_GROUP_DN"; fail_bool 0 110

section "Remove user"

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

ad_exists "$AD_USER_DN"; fail_bool 1 110
udm_exists "users/user"; fail_bool 1 110

udm_verify_multi_value_udm_attribute_contains "groups" \
	"cn=$UDM_groups_group_name,cn=groups,$ldap_base" "users/user"; fail_bool 1 110
ad_verify_multi_value_attribute_contains "$AD_GROUP_DN" "member" "$AD_USER_DN"; fail_bool 1 110

section "Clean up"

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

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

ad_set_sync_mode "$SYNCMODE"

exit "$RETVAL"
