#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Check if tombstone reanimation works for users and groups"
## exposure: dangerous
## tags:
## - apptest
## - ucsschool
## packages:
## - univention-s4-connector
## roles:
## - domaincontroller_master
## bugs:
##  - 49792

# 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
SYNCMODE="$(ad_get_sync_mode)"
ad_set_sync_mode "sync"

TIMEZONE="$(cat /etc/timezone)"
UDM_users_user_username="$(random_chars)"
UDM_users_user_lastname="$(random_chars)"
UDM_users_user_password="U$(random_chars)123"
UDM_users_user_firstname="$(random_chars)"
UDM_users_user_sambaRID="$(( RANDOM % 10000 + 10000 ))"
AD_DN="CN=$UDM_users_user_username,CN=users,$(ad_get_base)"
UCS_DN="uid=$UDM_users_user_username,cn=users,$(ucr get ldap/base)"

UDM_groups_group_name="$(random_chars)"
UDM_groups_group_sambaRID="$(( RANDOM % 10000 + 10000 ))"
GROUP_AD_DN="CN=$UDM_groups_group_name,CN=groups,$(ad_get_base)"

function cleanup {
	timedatectl set-timezone "$TIMEZONE"
	udm_remove 'users/user'
	udm_remove 'groups/group'
	ad_wait_for_synchronization; fail_bool 0 110
	udm_exists 'users/user'; fail_bool 1 110
	udm_exists 'groups/group'; fail_bool 1 110
	ad_set_sync_mode "$SYNCMODE"
}
trap cleanup EXIT

udm_create "users/user" || fail_fast 110
ad_wait_for_synchronization
ad_exists "$AD_DN" ; fail_bool 0 12
udm_remove 'users/user'
ad_wait_for_synchronization
ad_exists "$AD_DN" ; fail_bool 1 12
udm_create "users/user" || fail_fast 110
ad_wait_for_synchronization
ad_exists "$AD_DN" ; fail_bool 0 12

udm_create "groups/group"|| fail_fast 110
ad_wait_for_synchronization
ad_exists "$GROUP_AD_DN" ; fail_bool 0 12
udm_remove 'groups/group'
ad_wait_for_synchronization
ad_exists "$GROUP_AD_DN" ; fail_bool 1 12
udm_create "groups/group"|| fail_fast 110
ad_wait_for_synchronization
ad_exists "$GROUP_AD_DN" ; fail_bool 0 12
