Tests/Send-LogAnalyticsData.tests.ps1

Describe 'New-LogSignature Tests' {
  BeforeAll {
    Import-Module "$PSScriptRoot\..\CIE.PSTools.psm1" -Force
  }
  It 'Returns the correct authorization header for given inputs' {
      # Arrange
      $customerId = 'test-customer'
      $sharedKey = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes('test-shared-key'))
      $date = 'Wed, 08 Nov 2023 12:34:56 GMT'
      $contentLength = '256'
      $method = 'GET'
      $contentType = 'application/json'
      $resource = '/test/resource'

      $expectedAuthorizationPattern = 'SharedKey test-customer:.+'

      # Act
      $authorization = New-LogSignature -customerId $customerId -sharedKey $sharedKey -date $date -contentLength $contentLength -method $method -contentType $contentType -resource $resource

      # Assert
      $authorization | Should -Match $expectedAuthorizationPattern
  }
}


Describe 'Send-LogAnalyticsData Tests' {
  BeforeAll {
    Remove-Module Write-LogAnalyticsData -Force -ErrorAction SilentlyContinue
    Import-Module "$PSScriptRoot\..\CIE.PSTools.psm1"  -Force
    Mock Invoke-WebRequest {
      return @{
        StatusCode = 200
      }
    } -ModuleName CIE.PSTools
  }

  It 'Returns status code 200 for a valid request' {
    # Arrange
    $customerId = 'dummyId'
    $sharedKey = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes('test-shared-key'))
    $logType = 'dummyLogType'
    $script:TimeStampField = Get-Date

    $postData = [PSCustomObject]@{
      Hostname = $env:computername
      State = 'Patching started'
    }

    $json = $postData | ConvertTo-Json -Depth 100
    $body = ([System.Text.Encoding]::UTF8.GetBytes($json))

    # Act
    $result = Send-LogAnalyticsData -customerId $customerId -sharedKey $sharedKey -body $body -logType $logType

    # Assert
    $result | Should -Be 200
  }
}