#!/usr/share/ucs-test/runner bash
## desc: Create tine20 users and test the login
## bugs: [31870]
## tags:
##  - apptest
## exposure: dangerous

. "$TESTLIBPATH/base.sh" || exit 137
. "$TESTLIBPATH/user.sh" || exit 137
. "$TESTLIBPATH/random.sh" || exit 137
. "/usr/share/univention-lib/all.sh" || exit 137

eval "$(/usr/sbin/ucr shell)"

RETVAL=100

create_user () {
	local username=$1
	udm-test users/user create \
		--position "cn=users,$ldap_base" \
		--set username="$username" \
		--set firstname="$(random_string)" \
		--set lastname="$(random_string)" \
		--set password="univention"
}

change_password ()
{
	local username="$1"
	local new_password="$2"
	udm-test users/user modify \
		--dn "uid=$username,cn=users,$ldap_base" \
		--set password="$new_password"
}

get_json_attr () {
    local json="$1"
    # e.g. ["result"]["welcomeMessage"]
    local attr="$2"
    cat "$json" | python -c "import sys,json;data=json.loads(sys.stdin.read()); print data$attr"
   
}

tin20_login () {
	local username="$1"
	local password="$2"
	local proto="${3:-http}"
	local data='{"jsonrpc":"2.0","method":"Tinebase.login","params":{"username":"'$username'","password":"'$password'"}, "id":6}'
	local cookies=$(mktemp)
	local header='Content-Type: application/json'
	local url="$proto://$hostname.$domainname/tine20/"
	local out=$(mktemp)
	curl --cacert /etc/univention/ssl/ucsCA/CAcert.pem -c "$cookies" -o $out \
		-s -H "$header" --data-binary "$data" "$url" || fail_test 110 "Failed connect to tine20"
	local success="$(get_json_attr "$out" '["result"]["success"]')"
	if [ "$success" = "True" ]; then
		echo "login passed"
		return 0
	fi
	echo "login failed"
	return 1
}

active_sync () {
	local username="$1"
	local password="$2"
	local proto="${3:-http}"
	local url="$proto://$hostname.$domainname/tine20/Microsoft-Server-ActiveSync"
	wget --ca-certificate=/etc/univention/ssl/ucsCA/CAcert.pem --user "$username" --password="$password" "$url"
	return $?
}

webdav_test () {
	local username="$1"
	local password="$2"
	local url="http://$hostname.$domainname/tine20/webdav"
	test -n "$(echo -e "${username}\n${password}\n" | cadaver "$url" | grep 'dav:/tine20/webdav/>')" && return 0
	return 1
}

username1="tinexyz123"
username2="tinenext"
create_user "$username1"
create_user "$username2"
wait_for_replication

set -x

# standard
tin20_login "$username1" "univention" || fail_test 110 "Login was denied"
tin20_login "$username1" "univention123" && fail_test 110 "Login with wrong password"
active_sync "$username1" "univention" || fail_test 110 "ActiveSync logon failed"
active_sync "$username1" "univention1" && fail_test 110 "ActiveSync logon with wrong password"
webdav_test "$username1" "univention" || fail_test 110 "webdav logon failed"
webdav_test "$username1" "univention2" && fail_test 110 "webdav logon with wrong password"

# password change
tin20_login "$username2" "univention" || fail_test 110 "tine login was denied"
change_password "$username2" "Univention.99ä"
wait_for_replication
tin20_login "$username2" "Univention.99ä" || fail_test 110 "tine login was denied"
tin20_login "$username2" "univention" && fail_test 110 "tine login with wrong password"

# https
tin20_login "$username1" "univention" "https" || fail_test 110 "tine https login was denied"
active_sync "$username1" "univention" "https" || fail_test 110 "ActiveSync https login was denied"

set +x

user_remove "$username1"
user_remove "$username2"

wait_for_replication

exit $RETVAL
