trigger:
  - main

pr:
  autoCancel: false
  branches:
    include:
      - main

pool:
  vmImage: "ubuntu-latest"

steps:
  - task: GoTool@0
    inputs:
      version: "1.22.3"
  - task: Go@0
    inputs:
      command: "get"
      arguments: "-d -v -t -d ./..."
      workingDirectory: "$(System.DefaultWorkingDirectory)"
    displayName: "Install dependencies"
  - task: Go@0
    inputs:
      command: "build"
      arguments: "./apps/..."
      workingDirectory: "$(System.DefaultWorkingDirectory)"
    displayName: "Build"
  - task: Go@0
    inputs:
      command: "test"
      arguments: "-race -short ./apps/cache/... ./apps/confidential/... ./apps/public/... ./apps/internal/... ./apps/managedidentity/..."
      workingDirectory: "$(System.DefaultWorkingDirectory)"
    displayName: "Run Unit Tests"
  - task: AzureKeyVault@2
    displayName: "Connect to Key Vault"
    inputs:
      azureSubscription: "AuthSdkResourceManager"
      KeyVaultName: "msidlabs"
      SecretsFilter: "LabAuth,IDLABS-APP-Confidential-Client-Cert-OnPrem"
  - task: Bash@3
    displayName: Installing certificate
    inputs:
      targetType: "inline"
      script: |
        echo $(LabAuth) | base64 -d > $(Build.SourcesDirectory)/cert.pfx
        OPENSSL_CONF=/dev/null openssl pkcs12 -in $(Build.SourcesDirectory)/cert.pfx -out $(Build.SourcesDirectory)/cert.pem -nodes -passin pass:'' -legacy
        echo "$(IDLABS-APP-Confidential-Client-Cert-OnPrem)" | base64 -d > $(Build.SourcesDirectory)/ccaCert.pfx
        OPENSSL_CONF=/dev/null openssl pkcs12 -in $(Build.SourcesDirectory)/ccaCert.pfx -out $(Build.SourcesDirectory)/ccaCert.pem -nodes -passin pass:'' -legacy

  - task: Go@0
    inputs:
      command: "test"
      arguments: "-race ./apps/tests/integration/..."
      workingDirectory: "$(System.DefaultWorkingDirectory)"
    displayName: "Run Integration Tests"
