!******************************************************************************!
! Tests attribute based authorization via suppolied JSON response
!
! @bug      NXM-3984 SSLUtilities/CHECKAUTHORIZATION need updating
! @author:  Jeanette McCall
! @since    NeXtMidas 4.1.3
!******************************************************************************!
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

  res propsFileDefault "nxm.sys.test.JsonKeyPaths.properties"

  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

  ! Test with having a porperties file for JSON Key Paths
  invoke ,, nxm.sys.lib.SSLUtilities.overideJsonKeyPaths(^{propsFileDefault})
  res t:attrtbl {}
  TABLE ^attrtbl SETFLAGS "+CaseSensitive"
  table t:attrtbl setkey  "title" "engineer"
  checkauthorization LABEL=ALLOW_ACCESS USERATTR=attrtbl JSONRESPONSE="{\"title\": \"engineer\"}"
  assert/text="^reg.main.name: Authorization allowed jsonKeyPath" ALLOW_ACCESS ISTRUE
  checkauthorization LABEL=ALLOW_ACCESS USERATTR=attrtbl JSONRESPONSE="{\"title\": \"manager\"}"
  assert/text="^reg.main.name: Authorization denied jsonKeyPath" ALLOW_ACCESS ISFALSE

  ! Test again without JSON Key Paths - use attribute name for path
  invoke ,, nxm.sys.lib.SSLUtilities.overideJsonKeyPaths("")
  res t:attrtbl {}
  TABLE ^attrtbl SETFLAGS "+CaseSensitive"
  table t:attrtbl setkey  "title" "engineer"
  checkauthorization LABEL=ALLOW_ACCESS USERATTR=attrtbl JSONRESPONSE="{\"title\": \"engineer\"}"
  assert/text="^reg.main.name: Authorization allowed" ALLOW_ACCESS ISTRUE
  checkauthorization LABEL=ALLOW_ACCESS USERATTR=attrtbl JSONRESPONSE="{\"title\": \"manager\"}"
  assert/text="^reg.main.name: Authorization denied" ALLOW_ACCESS ISFALSE

  ! restore logging level
  invoke ,, nxm.sys.lib.SSLUtilities.setCertInfoLogLevel(levelCurrent)

endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return
