!******************************************************************************!
! Tests attribute based authorization over an SSL connection
!
! This requires:
! 1) access to a REST service that provides a JSON response with user attributes
! 2) the user to have access to a certificate that is trusted by
!    the REST service
!
! @bug      NXM-3313 insecure SSL
! @author:  Jeanette McCall
! @since    NeXtMidas 3.9.0
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)
  switch DEBUG       debug    GET DEF=FALSE SDEF=TRUE  ! to bump priority level of SSLUtiltities log messages

  invoke levelCurrent nxm.sys.lib.SSLUtilities.getCertInfoLogLevel()
  invoke levelConfig  java.util.logging.Level.CONFIG
  if DEBUG then
    invoke ,,           nxm.sys.lib.SSLUtilities.setCertInfoLogLevel(levelConfig)
  endif
  env get HOME HOME

  new java.lang.StringBuilder("sac") basename
  invoke ,, basename.reverse()
  invoke ,, basename.append("port")

  ! default test certificate for private key
  res default_cert_loc "^{HOME}client_^{basename}.p12"

  SSLCONFIGURE/INSECURE PRIVKEYFILE=^default_cert_loc PASSWORD="default"

  invoke nxm.sys.lib.SSLUtilities.setUserQuery("https://^{basename}.com:8443/^{basename}sim_tma/rest/v3/users/%s")

  res t:attrtbl {}
  table t:attrtbl setkey  "firstName" "John"
  table t:attrtbl setkey  "grantedBy" "USA"
  table t:attrtbl setkey  "isIcMember" true

  invoke nxm.sys.lib.SSLUtilities.addToJsonKeyPath("firstName","firstName")
  invoke nxm.sys.lib.SSLUtilities.addToJsonKeyPath("grantedBy","grantedBy")
  invoke nxm.sys.lib.SSLUtilities.addToJsonKeyPath("isIcMember","isIcMember")

  res authorized false
  checkauthorization USERDN="jonesjoh" USERATTR=attrtbl
  assert/text="^reg.main.name: Able to validate based on having one of user attributes" authorized ISTRUE

endmacro