tests/Configuration.Dashboard.Tests.ps1

Describe 'Sync-NRDashboardConfiguration' {
  BeforeAll {
    Import-Module '.\NewRelicPS.Configuration.Dashboard.psm1' -Force
    Import-Module '.\NewRelicPS.Dashboards.psm1' -Force
    $apiKey = 'Fake-Test-Key'
  }

  Context 'No dashboards configured for sync' {
    BeforeAll {
      Mock Copy-NRDashboard -ModuleName 'NewRelicPS.Configuration.Dashboard' {}
    }

    It 'Does nothing' {
      Sync-NRDashboardConfiguration -APIKey $apiKey -DefinedDashboards @()
      Assert-MockCalled Copy-NRDashboard -ModuleName 'NewRelicPS.Configuration.Dashboard' -Exactly 0
    }
  }

  Context 'Dashboards configured for sync' {
    BeforeAll {
      $DefinedDashboards = @(
        [PSCustomObject] @{
          sourceDashboardName = 'fake1'
          sourceDashboardId = '1234'
          updateWidgetAccountIds = $true
          destinationAccounts = @(
            @{
              id = '0123456789'
              name = 'fakeAccount1'
            }
          )
        }
      )
      Mock Copy-NRDashboard -ModuleName 'NewRelicPS.Configuration.Dashboard' {}
      Mock Get-NRDashboard -ModuleName 'NewRelicPS.Configuration.Dashboard' {}
      Mock Get-NRDashboardList -ModuleName 'NewRelicPS.Configuration.Dashboard' {}
    }

    It 'Copies the dashboard when a single source and destination provided' {
      Sync-NRDashboardConfiguration -APIKey $apiKey -DefinedDashboards $DefinedDashboards
      Assert-MockCalled Copy-NRDashboard -ModuleName 'NewRelicPS.Configuration.Dashboard' -Exactly 1 -ParameterFilter {
        $DashboardId -eq '1234' -and $DestinationAccountId -eq '0123456789'
      }
     }

    It 'Updates widget account Ids when specified' {
      Sync-NRDashboardConfiguration -APIKey $apiKey -DefinedDashboards $DefinedDashboards
      Assert-MockCalled Copy-NRDashboard -ModuleName 'NewRelicPS.Configuration.Dashboard' -Exactly 1 -ParameterFilter {
        $UpdateWidgetAccountIds -eq $true
      }
    }

    It 'Syncs the dashboard to all specified destination accounts' {
      $DefinedDashboards[0].destinationAccounts += @{
        id = '9876543210'
        name = 'fakeAccount2'
      }
      Sync-NRDashboardConfiguration -APIKey $apiKey -DefinedDashboards $DefinedDashboards
      Assert-MockCalled Copy-NRDashboard -ModuleName 'NewRelicPS.Configuration.Dashboard' -Exactly 2 -ParameterFilter {
        $DashboardId -eq '1234' -and ($DestinationAccountId -eq '0123456789' -or $DestinationAccountId -eq '9876543210')
      }
    }

    It 'Syncs all specified source dashboards' {
      $DefinedDashboards += [PSCustomObject] @{
        sourceDashboardName = 'fake2'
        sourceDashboardId = '5678'
        destinationAccounts = @(
          @{
            id = '0123456789'
            name = 'fakeAccount1'
            updateWidgetAccountIds = $true
          }
        )
      }
      Sync-NRDashboardConfiguration -APIKey $apiKey -DefinedDashboards $DefinedDashboards
      Assert-MockCalled Copy-NRDashboard -ModuleName 'NewRelicPS.Configuration.Dashboard' -Exactly 2 -ParameterFilter {
        ($DashboardId -eq '1234' -or $DashboardId -eq '5678') -and $DestinationAccountId -eq '0123456789'
      }
    }
  }
}