#!/bin/zsh

set -e

typeset -a attributes

attributes=(
  --set "serviceProviderOrganizationName=BiLo-Broker"
  --set "AssertionConsumerService=https://login.test.sso.bildungslogin.de/realms/bilo_broker/broker/USR__VBM_INT_Mein/endpoint"
  --set "singleLogoutService=https://login.test.sso.bildungslogin.de/realms/bilo_broker/broker/USR__VBM_INT_Mein/endpoint"
  --set "NameIDFormat=urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified"
  --set "attributesNameFormat=urn:oasis:names:tc:SAML:2.0:attrname-format:basic"
  --set "isActivated=TRUE"
  --set "signLogouts=TRUE"
  --set "simplesamlAttributes=TRUE"
  --set "simplesamlNameIDAttribute=uid"
)

base_dn=cn=saml-serviceprovider,cn=univention,$(ucr get ldap/base)

print "\nTest 1: create with one LDAP attribute, try to replace with --set & --set\n"

identifier=test-create-and-replace-set-set
dn=SAMLServiceProviderIdentifier=${identifier},${base_dn}
udm saml/serviceprovider create \
    --position ${base_dn} \
    ${attributes} \
    --set "Identifier=${identifier}" \
    --set "LDAPattributes=mail mail"
univention-ldapsearch -b ${dn} | grep -i ldapattribute | sort

udm saml/serviceprovider modify \
    --dn ${dn} \
    --set "LDAPattributes=givenName first_name" \
    --set "LDAPattributes=sn last_name"
univention-ldapsearch -b ${dn} | grep -i ldapattribute | sort

print "\nTest 2: create with one LDAP attribute, try to replace with --set & --append\n"

identifier=test-create-and-replace-set-append
dn=SAMLServiceProviderIdentifier=${identifier},${base_dn}
udm saml/serviceprovider create \
    --position ${base_dn} \
    ${attributes} \
    --set "Identifier=${identifier}" \
    --set "LDAPattributes=mail mail"
univention-ldapsearch -b ${dn} | grep -i ldapattribute | sort

udm saml/serviceprovider modify \
    --dn ${dn} \
    --set "LDAPattributes=givenName first_name" \
    --append "LDAPattributes=sn last_name"
univention-ldapsearch -b ${dn} | grep -i ldapattribute | sort

print "\nTest 3: create with one LDAP attribute, try to replace with --append & --append\n"

identifier=test-create-and-replace-append-append
dn=SAMLServiceProviderIdentifier=${identifier},${base_dn}
udm saml/serviceprovider create \
    --position ${base_dn} \
    ${attributes} \
    --set "Identifier=${identifier}" \
    --set "LDAPattributes=mail mail"
univention-ldapsearch -b ${dn} | grep -i ldapattribute | sort

udm saml/serviceprovider modify \
    --dn ${dn} \
    --append "LDAPattributes=givenName first_name" \
    --append "LDAPattributes=sn last_name"
univention-ldapsearch -b ${dn} | grep -i ldapattribute | sort

# cleanup
for identifier in test-create-and-replace-set-set test-create-and-replace-set-append test-create-and-replace-append-append ; do
  udm saml/serviceprovider delete --dn SAMLServiceProviderIdentifier=${identifier},${base_dn} > /dev/null
done
