#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Verify user primarygroup synchronisation after changes from ad-side in read 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
test -n "$connector_s4_ldap_host" || exit 137
connector_running_on_this_host || exit 137

UDM_users_user_username="$(random_chars)"
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 a user and two groups"

GROUP1="$(random_chars)"
GROUP2="$(random_chars)"

ad_group_create "$GROUP1" || fail_test 110
ad_group_create "$GROUP2" || fail_test 110

section "Add user to both groups"

ad_createuser "$UDM_users_user_username" || fail_test 110
ad_append_to_attribute "CN=$GROUP1,CN=groups,$(ad_get_base)" "member" "$AD_USER_DN" || fail_test 110
ad_append_to_attribute "CN=$GROUP2,CN=groups,$(ad_get_base)" "member" "$AD_USER_DN" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

ad_exists "CN=$GROUP1,CN=groups,$(ad_get_base)"; fail_bool 0 110
ad_exists "CN=$GROUP2,CN=groups,$(ad_get_base)"; fail_bool 0 110
UDM_groups_group_name="$GROUP1"
udm_exists "groups/group"; fail_bool 0 110
UDM_groups_group_name="$GROUP2"
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_groups_group_name="$GROUP1"
udm_verify_multi_value_udm_attribute_contains_ignore_case "users" \
	"uid=$UDM_users_user_username,cn=users,$ldap_base" "groups/group"; fail_bool 0 110
udm_verify_multi_value_udm_attribute_contains_ignore_case "groups" \
	"cn=$UDM_groups_group_name,cn=groups,$ldap_base" "users/user"; fail_bool 0 110
UDM_groups_group_name="$GROUP2"
udm_verify_multi_value_udm_attribute_contains_ignore_case "users" \
	"uid=$UDM_users_user_username,cn=users,$ldap_base" "groups/group"; fail_bool 0 110
udm_verify_multi_value_udm_attribute_contains_ignore_case "groups" \
	"cn=$UDM_groups_group_name,cn=groups,$ldap_base" "users/user"; fail_bool 0 110
ad_verify_multi_value_attribute_contains "CN=$GROUP1,CN=groups,$(ad_get_base)" "member" "$AD_USER_DN"; fail_bool 0 110
ad_verify_multi_value_attribute_contains "$AD_USER_DN" "memberOf" "CN=$GROUP1,CN=groups,$(ad_get_base)"; fail_bool 0 110
ad_verify_multi_value_attribute_contains "CN=$GROUP2,CN=groups,$(ad_get_base)" "member" "$AD_USER_DN"; fail_bool 0 110
ad_verify_multi_value_attribute_contains "$AD_USER_DN" "memberOf" "CN=$GROUP2,CN=groups,$(ad_get_base)"; fail_bool 0 110

section "Set primary group of user to GROUP1"

ad_set_primary_group "$AD_USER_DN" "CN=$GROUP1,CN=groups,$(ad_get_base)" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

ad_verify_user_primary_group_attribute "CN=$GROUP1,CN=groups,$(ad_get_base)" "$AD_USER_DN"; fail_bool 0 110
ad_verify_multi_value_attribute_contains "CN=$GROUP1,CN=groups,$(ad_get_base)" "member" "$AD_USER_DN"; fail_bool 1 110
ad_verify_multi_value_attribute_contains "$AD_USER_DN" "memberOf" "CN=$GROUP1,CN=groups,$(ad_get_base)"; fail_bool 1 110
ad_verify_multi_value_attribute_contains "CN=$GROUP2,CN=groups,$(ad_get_base)" "member" "$AD_USER_DN"; fail_bool 0 110
ad_verify_multi_value_attribute_contains "$AD_USER_DN" "memberOf" "CN=$GROUP2,CN=groups,$(ad_get_base)"; fail_bool 0 110

UDM_groups_group_name="$GROUP1"
udm_verify_udm_attribute "primaryGroup" "cn=$UDM_groups_group_name,cn=groups,$ldap_base" "users/user"; fail_bool 0 121 "See: #20029"
udm_verify_multi_value_udm_attribute_contains_ignore_case "users" \
	"uid=$UDM_users_user_username,cn=users,$ldap_base" "groups/group"; fail_bool 0 110
udm_verify_multi_value_udm_attribute_contains_ignore_case "groups" \
	"cn=$UDM_groups_group_name,cn=groups,$ldap_base" "users/user"; fail_bool 0 110
UDM_groups_group_name="$GROUP2"
udm_verify_multi_value_udm_attribute_contains_ignore_case "users" \
	"uid=$UDM_users_user_username,cn=users,$ldap_base" "groups/group"; fail_bool 0 110
udm_verify_multi_value_udm_attribute_contains_ignore_case "groups" \
	"cn=$UDM_groups_group_name,cn=groups,$ldap_base" "users/user"; fail_bool 0 110

section "Set primary group of user to GROUP2"

ad_set_primary_group "$AD_USER_DN" "CN=$GROUP2,CN=groups,$(ad_get_base)" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

ad_verify_user_primary_group_attribute "CN=$GROUP2,CN=groups,$(ad_get_base)" "$AD_USER_DN"; fail_bool 0 110
ad_verify_multi_value_attribute_contains "CN=$GROUP1,CN=groups,$(ad_get_base)" "member" "$AD_USER_DN"; fail_bool 0 110
ad_verify_multi_value_attribute_contains "$AD_USER_DN" "memberOf" "CN=$GROUP1,CN=groups,$(ad_get_base)"; fail_bool 0 110
ad_verify_multi_value_attribute_contains "CN=$GROUP2,CN=groups,$(ad_get_base)" "member" "$AD_USER_DN"; fail_bool 1 110
ad_verify_multi_value_attribute_contains "$AD_USER_DN" "memberOf" "CN=$GROUP2,CN=groups,$(ad_get_base)"; fail_bool 1 110

UDM_groups_group_name="$GROUP1"
udm_verify_multi_value_udm_attribute_contains_ignore_case "users" \
	"uid=$UDM_users_user_username,cn=users,$ldap_base" "groups/group"; fail_bool 0 110
udm_verify_multi_value_udm_attribute_contains_ignore_case "groups" \
	"cn=$UDM_groups_group_name,cn=groups,$ldap_base" "users/user"; fail_bool 0 110
UDM_groups_group_name="$GROUP2"
udm_verify_udm_attribute "primaryGroup" "cn=$UDM_groups_group_name,cn=groups,$ldap_base" "users/user"; fail_bool 0 121 "See: #20029"
udm_verify_multi_value_udm_attribute_contains_ignore_case "users" \
	"uid=$UDM_users_user_username,cn=users,$ldap_base" "groups/group"; fail_bool 0 110
udm_verify_multi_value_udm_attribute_contains_ignore_case "groups" \
	"cn=$UDM_groups_group_name,cn=groups,$ldap_base" "users/user"; fail_bool 0 110

section "Clean up"

ad_delete "$AD_USER_DN" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110
ad_delete "CN=$GROUP1,CN=groups,$(ad_get_base)" || fail_test 110
ad_delete "CN=$GROUP2,CN=groups,$(ad_get_base)" || 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

ad_exists "CN=$GROUP1,CN=groups,$(ad_get_base)"; fail_bool 1 110
ad_exists "CN=$GROUP2,CN=groups,$(ad_get_base)"; fail_bool 1 110
UDM_groups_group_name="$GROUP1"
udm_exists "groups/group"; fail_bool 1 110
UDM_groups_group_name="$GROUP2"
udm_exists "groups/group"; fail_bool 1 110

ad_set_sync_mode "$SYNCMODE"

exit "$RETVAL"
