#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Verify user primarygroup synchronisation after changes from 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


UDM_users_user_username="$(random_chars)"
UDM_users_user_lastname="$(random_chars)"
# If the password doesn't adhere the configured Windows-Password-Guidelines
# weird things might happen when the user is synced to AD.
UDM_users_user_password="U$(random_chars)123"
AD_USER_DN="CN=$UDM_users_user_username,CN=Users,$(ad_get_base)"

SYNCMODE="$(ad_get_sync_mode)"

ad_set_sync_mode "sync"

section "Create a user and two groups"

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

UDM_groups_group_name="$GROUP1"
udm_create "groups/group" || fail_test 110
UDM_groups_group_name="$GROUP2"
udm_create "groups/group" || fail_test 110

udm_create "users/user" || fail_test 110

section "Add user to both groups"

udm_modify "users/user" "" "" "" "" \
	--append groups="cn=$GROUP1,cn=groups,$ldap_base" \
	--append groups="cn=$GROUP2,cn=groups,$ldap_base" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

section "Check whether groups and users exist on ucs and ad-side"

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

section "Check attributes of user and group to verify membership on ucs and ad-side"

UDM_groups_group_name="$GROUP1"
udm_verify_multi_value_udm_attribute_contains "users" \
	"uid=$UDM_users_user_username,cn=users,$ldap_base" "groups/group"; fail_bool 0 110
udm_verify_multi_value_udm_attribute_contains "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 "users" \
	"uid=$UDM_users_user_username,cn=users,$ldap_base" "groups/group"; fail_bool 0 110
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 "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"

udm_modify "users/user" "" "" "" "" \
	--set primaryGroup="cn=$GROUP1,cn=groups,$ldap_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 110
udm_verify_multi_value_udm_attribute_contains "users" \
	"uid=$UDM_users_user_username,cn=users,$ldap_base" "groups/group"; fail_bool 0 110
udm_verify_multi_value_udm_attribute_contains "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 "users" \
	"uid=$UDM_users_user_username,cn=users,$ldap_base" "groups/group"; fail_bool 0 110
udm_verify_multi_value_udm_attribute_contains "groups" \
	"cn=$UDM_groups_group_name,cn=groups,$ldap_base" "users/user"; fail_bool 0 110

section "Set primary group of user to GROUP2"

udm_modify "users/user" "" "" "" "" \
	--set primaryGroup="cn=$GROUP2,cn=groups,$ldap_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 "users" \
	"uid=$UDM_users_user_username,cn=users,$ldap_base" "groups/group"; fail_bool 0 110
udm_verify_multi_value_udm_attribute_contains "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 110
udm_verify_multi_value_udm_attribute_contains "users" \
	"uid=$UDM_users_user_username,cn=users,$ldap_base" "groups/group"; fail_bool 0 110
udm_verify_multi_value_udm_attribute_contains "groups" \
	"cn=$UDM_groups_group_name,cn=groups,$ldap_base" "users/user"; fail_bool 0 110

section "Clean up"

udm_remove "users/user" || fail_test 110
UDM_groups_group_name="$GROUP1"
udm_remove "groups/group" || fail_test 110
UDM_groups_group_name="$GROUP2"
udm_remove "groups/group" || 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"
