tests/New-AWSVaultAlias.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Describe "New-AWSVaultAlias" {
  Import-Module .\posh-awsvault.psd1

  $TestCases = @{
    "When called with only an alias name" = @( "somecommand" )
    "When called with an alias name and a command name" = @( "somealias", "somecommand" )
  }

  foreach($Case in $TestCases.Keys) {
    Context $Case {
      $TestState = @{
        "Alias" = $null
        "Function" = $null
      }

      $TestCaseArguments = $TestCases[$Case]

      $TestAliasName = $TestCaseArguments[0]
      $TestCommandName = $TestAliasName
      
      if($TestCaseArguments.Count -gt 1) {
        $TestCommandName = $TestCaseArguments[1]
      }

      New-AWSVaultAlias @TestCaseArguments

      It "Creates an alias named $TestAliasName" {
        $TestState["Alias"] = Get-Alias $TestAliasName
        $TestState["Alias"] | Should -Not -BeNull
        Write-Debug ($TestState["Alias"] | Out-String)
      }

      It "Points to a function" {
        $TestState["Function"] = Get-Item Function:\$($TestState["Alias"].Definition)
        $TestState["Function"] | Should -Not -BeNull
        Write-Debug ($TestState["Function"] | Out-String)
      }

      Mock Invoke-AWSVault -ModuleName $TestState["Alias"].Module { 
        Write-Debug "Invoke-AWSVault $($args[0]) $($args[1])"
      } 

      It "Calls Invoke-AWSVault for command $TestCommandName" {
        &($TestState["Function"].Name)

        Assert-MockCalled Invoke-AWSVault -ModuleName $TestState["Function"].Module -ParameterFilter {
          $CommandName -eq $TestCommandName
        }
      }

      Remove-Module $TestState["Function"].Module -ErrorAction SilentlyContinue
    }
  }
  
  Remove-Module posh-awsvault
}