#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: test join hooks
## bugs: [47940]
## packages:
##  - univention-join
## roles:
##  - domaincontroller_master
##  - domaincontroller_backup
##  - domaincontroller_slave
##  - memberserver
## exposure: dangerous

eval "$(ucr shell)"

# shellcheck source=../../lib/base.sh
. "$TESTLIBPATH/base.sh" || exit 137
# shellcheck source=../../lib/master.sh
. "$TESTLIBPATH/master.sh" || exit 137
# shellcheck source=../../lib/random.sh
. "$TESTLIBPATH/random.sh" || exit 137


rm -f /tmp/prejoin /tmp/prejoinscripts /tmp/postjoinscripts

udm-test settings/data create --set name=prejoin --set data_type=join/pre-join \
	--set data="$(echo -e '#!/bin/bash\ntouch /tmp/prejoin' | bzip2 | base64 -w 0)"
udm-test settings/data create --set name=prejoinscripts --set data_type=join/pre-joinscripts \
	--set data="$(echo -e '#!/bin/bash\ntouch /tmp/prejoinscripts' | bzip2 | base64 -w 0)"
udm-test settings/data create --set name=postjoinscripts --set data_type=join/post-joinscripts \
	--set data="$(echo -e '#!/bin/bash\ntouch /tmp/postjoinscripts' | bzip2 | base64 -w 0)"

# indirect pre-join test
/usr/share/univention-join/univention-join-hooks \
	--server-role "$server_role" \
	--hooktype join/pre-join \
	--master "$ldap_master" \
	--binddn "$tests_domainadmin_account" \
	--bindpwdfile "$tests_domainadmin_pwdfile"
test -e /tmp/prejoin || fail_test 110

test_username="$(echo $tests_domainadmin_account | awk -F , '{print $1}' | sed 's/uid=//')"
univention-run-join-scripts -dcaccount "$test_username" -dcpwd "$tests_domainadmin_pwdfile"
test -e /tmp/prejoinscripts || fail_test 110
test -e /tmp/postjoinscripts || fail_test 110

# do cleanup
udm-test settings/data remove --dn "cn=prejoin,$ldap_base"
udm-test settings/data remove --dn "cn=prejoinscripts,$ldap_base"
udm-test settings/data remove --dn "cn=postjoinscripts,$ldap_base"

exit "$RETVAL"
