#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Check renaming windows hosts"
## exposure: dangerous
## packages:
## - univention-s4-connector
## roles:
## - domaincontroller_master
## - domaincontroller_backup
## bugs:
##  - 37709

# 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

RETVAL=100

test -n "$connector_s4_ldap_host" || exit 137
connector_running_on_this_host || exit 137
SYNCMODE="$(ad_get_sync_mode)"
ad_set_sync_mode "sync"

# create computer object
UDM_computers_windows_name="$(random_chars)"
UDM_computers_windows_description="$(random_chars)"
udm_create "computers/windows" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

# rename computer object
old="$UDM_computers_windows_name"
UDM_computers_windows_name="$(random_chars)"
udm-test 'computers/windows' modify \
	--dn "cn=$old,cn=computers,$(ucr get ldap/base)" \
	--set name="$UDM_computers_windows_name" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110

# check renamed object
udm_verify_ldap_attribute "cn" "$UDM_computers_windows_name" "computers/windows" || fail_test 110
udm_verify_ldap_attribute "uid" "$UDM_computers_windows_name\$" "computers/windows" || fail_test 110
AD_DN="CN=$UDM_computers_windows_name,CN=computers,$(ad_get_base)"
ad_verify_attribute "$AD_DN" "cn" "$UDM_computers_windows_name"; fail_bool 0 12
ad_verify_attribute "$AD_DN" "sAMAccountName" "$UDM_computers_windows_name\$"; fail_bool 0 12

# cleanup
udm_remove 'computers/windows' || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110
udm_exists 'computers/windows'; fail_bool 1 110
ad_set_sync_mode "$SYNCMODE"

exit "$RETVAL"
