! Macro for testing the HTTPAUTH command.
!
! NOTE1: This test requires HTTP basic authentication to be setup on web server.
!        rootURL1, rootURL2, and rootURL3 points to nextmidas.com
!        so this test is only run if /baseline switch is on.
! NOTE2: rootURL2 and rootURL3 must NOT be underneath (sub-directory) of rootUR1L,
!        but it could be a sibling directory.
! NOTE3: the username and password should be valid for rootURL1, rootURL2, and rootURL3.
!
! Dependencies: FILE command (COPY function)
!
! Author : Neon Ngo
!
startmacro S:rootURL1["http://nextmidas.com/authtest/basic1/"] S:user["testuser1"] S:pass["testpassword123"] &
           S:rootURL2["http://nextmidas.com/authtest/basic2/"] &
           S:rootURL3["http://nextmidas.com/authtest/basic3/"]
  switch BASELINE    baseline   GET DEF=FALSE SDEF=TRUE
  switch INTERACTIVE interAct   GET DEF=TRUE  SDEF=TRUE
  switch ALTTESTENV  alttestenv GET DEF=FALSE SDEF=TRUE


  call setDatDir

  set testfile1     "^{datDir}.test1000_ieee.prm"
  set testfile2     "^{datDir}.test1000_eeei.prm"
  set testurl1      "^{rootURL1}^{testfile1}"
  set testurl2      "^{rootURL2}^{testfile1}"
  set destfilename1 "ram:test_httpauth_destfile"
  set destfilename2 "ram:test_httpauth_destfile2"

  if alttestenv isTRUE then
    warn "SKIPPING ^reg.main.name because in alternate test environment"
  elseif baseline isTRUE then
    ! Enable (register with JVM) and Add bogus user/password entry into authentication database case (with shorter prefix URL)
    httpauth/enable add rootURL1 "bogusUsername_^{env.pid}" "aBogusPassword"
    ! Add valid entry with longer prefix URL (this should take precedence over the above)
    httpauth add testurl1 user pass

    ! use FILE copy function to retrieve file over HTTP
    file copy/f testurl1 destfilename1
    if ^{env.NMROOT} EQS "classpath:" AND interAct isFALSE AND destfilename1 nfexists then
      warn "SKIPPING ^reg.main.name Get HTTP BASIC auth protected file ASSUMING NO ACCESS to nextmidas.com"
    else
      assert/text="^reg.main.name: Get HTTP BASIC auth protected file with pre-registered credentials" destfilename1 fexists
    endif

    if interAct isTRUE then
      ! enable login prompt GUI for unreqistered URL
      httpauth/gui/title="HTTPAuth/GUI test: click CANCEL"
      info "!!!!! It is ok to see ERROR for opening ^{testurl2} TWICE !!!!!"
      assert/text="^reg.main.name: NFexists for HTTP BASIC auth protected file via login prompt GUI (cancelled)"   "^{testurl2}" nfexists

      httpauth/gui/title="HTTPAuth/GUI test: click OK without password"
      assert/text="^reg.main.name: NFexists for HTTP BASIC auth protected file via login prompt GUI (no password)" "^{testurl2}" nfexists

      ! NTN: we need to use a different one that rootURL1 and rootURL2 (so that this test case does NOT conflict with the above)
      ! change title message to enter user/password
      httpauth/gui/title="HTTPAuth/GUI test: use ^{user}/^{pass}"/status="false"
      file copy/f "^{rootURL3}^{testfile2}" destfilename2
      assert/text="^reg.main.name: Get HTTP BASIC auth protected file via login prompt GUI (VALID credentials)" destfilename2 fexists
    endif

    ! unregister HTTPAUTH from current JVM
    httpauth disable
  endif
endmacro

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return
