Tests/Invoke-OriAzBopUseAzCredential.tests.ps1

. $PSScriptRoot\..\Private\Get-InstalledModuleForAzureToken.ps1
. $PSScriptRoot\..\Public\Install-OriAzBopPrerequisity.ps1
. $PSScriptRoot\..\Private\Test-IsAzConnected.ps1
. $PSScriptRoot\..\Private\Test-IsRmConnected.ps1
. $PSScriptRoot\..\Private\Test-IsRmOldConnected.ps1
#. $PSScriptRoot\..\Private\Connect-AzAccount.ps1
#. $PSScriptRoot\..\Private\Connect-AzureRmAccount.ps1
. $PSScriptRoot\..\Private\Get-AzCredential.ps1
. $PSScriptRoot\..\Private\Get-AzureRmCredential.ps1
. $PSScriptRoot\..\Private\Get-AzureRmOldCredential.ps1
. $PSScriptRoot\..\Public\Invoke-OriAzBopUseAzCredential.ps1
#Import-Module -Name PowerShellGet -MinimumVersion 2.2.3

if ($null -eq (Get-Command Connect-AzAccount -ErrorAction SilentlyContinue)) {
  function Connect-AzAccount {     
    [CmdletBinding()]
    param
        ()
  }
}

if ($null -eq (Get-Command Connect-AzureRmAccount -ErrorAction SilentlyContinue)) {
  function Connect-AzureRmAccount {     
    [CmdletBinding()]
    param
        ()
  }
}

Describe 'OriAzBopBoostrapOriPsgallery\Invoke-OriAzBopUseAzCredential' {
    
  Context 'Succesfull excution no module for connect into azure is installed with InstallModuleWhenRequired' { 
    Mock -CommandName Get-InstalledModuleForAzureToken -Verifiable -MockWith {
      return 'none'
    }  
    Mock -CommandName Install-OriAzBopPrerequisity -Verifiable
    Mock -CommandName Test-IsAzConnected -Verifiable -MockWith {
      return $true
    }
    Mock -CommandName Get-AzCredential -MockWith {
      $password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force
      $RepositoryCredential = New-Object System.Management.Automation.PSCredential 'feafeafae@mydomain.net', $password 
      return $RepositoryCredential
    }
    Mock -CommandName Write-Debug -Verifiable

    It 'Should be succesfully processed' {
      {
        $Credential = Invoke-OriAzBopGetAzCredential -InstallModuleWhenRequired 
        $Credential.UserName | Should -BeExactly 'feafeafae@mydomain.net'
        $Credential.GetNetworkCredential().Password | Should -BeExactly 'xbchuuuuhaaaatest'
      } | Should -Not -Throw
    }  

    Assert-MockCalled Get-InstalledModuleForAzureToken -Times 1 -Exactly
    Assert-MockCalled Install-OriAzBopPrerequisity -Times 1 -Exactly
    Assert-MockCalled Test-IsAzConnected -Times 1 -Exactly
    Assert-MockCalled Get-AzCredential -Times 1 -Exactly
    Assert-MockCalled Write-Debug -Times 1 -Exactly -ParameterFilter {
      $Message -ilike "-- End of Invoke-OriAzBopUseAzCredential --"
    }
     
    Assert-VerifiableMock
  }
  
  Context 'Succesfull excution no module for connect into azure is installed without InstallModuleWhenRequired' { 
    Mock -CommandName Get-InstalledModuleForAzureToken -Verifiable -MockWith {
      return 'none'
    }  
    Mock -CommandName Install-OriAzBopPrerequisity 
    Mock -CommandName Test-IsAzConnected 
    Mock -CommandName Get-AzCredential 
    Mock -CommandName Write-Debug 

    It 'Should be succesfully processed' {
      {
        Invoke-OriAzBopGetAzCredential 
      } | Should -Throw
    }  

    Assert-MockCalled Get-InstalledModuleForAzureToken -Times 1 -Exactly
    Assert-MockCalled Install-OriAzBopPrerequisity -Times 0 -Exactly
    Assert-MockCalled Test-IsAzConnected -Times 0 -Exactly
    Assert-MockCalled Get-AzCredential -Times 0 -Exactly
    Assert-MockCalled Write-Debug -Times 0 -Exactly -ParameterFilter {
      $Message -ilike "-- End of Invoke-OriAzBopUseAzCredential --"
    }
     
    Assert-VerifiableMock
  }
  
  Context 'Succesfull excution module Az.Account installed and profile exists' {
    Mock -CommandName Get-InstalledModuleForAzureToken -Verifiable -MockWith {
      return 'Az'
    }  
    Mock -CommandName Install-OriAzBopPrerequisity 
    Mock -CommandName Test-IsAzConnected -Verifiable -MockWith {
      return $true
    }
    #Mock -CommandName Connect-AzAccount -Verifiable
    Mock -CommandName Get-AzCredential -MockWith {
      $password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force
      $RepositoryCredential = New-Object System.Management.Automation.PSCredential 'PackageManage@oriflame.net', $password 
      return $RepositoryCredential
    }
    Mock -CommandName Write-Debug -Verifiable

    It 'Should be succesfully processed' {
      {
        $Credential = Invoke-OriAzBopGetAzCredential  
        $Credential.UserName | Should -BeExactly 'PackageManage@oriflame.net'
        $Credential.GetNetworkCredential().Password | Should -BeExactly 'xbchuuuuhaaaatest'
      } | Should -Not -Throw
    }  

    Assert-MockCalled Get-InstalledModuleForAzureToken -Times 1 -Exactly
    Assert-MockCalled Install-OriAzBopPrerequisity -Times 0 -Exactly
    Assert-MockCalled Test-IsAzConnected -Times 1 -Exactly
    #Assert-MockCalled Connect-AzAccount -Times 1 -Exactly
    Assert-MockCalled Get-AzCredential -Times 1 -Exactly
    Assert-MockCalled Write-Debug -Times 1 -Exactly -ParameterFilter {
      $Message -ilike "-- End of Invoke-OriAzBopUseAzCredential --"
    }
     
    Assert-VerifiableMock  
  }
  
  Context 'Succesfull excution module Az.Account installed, but no account profile exists without ConnectAzureWhenRequired' { 
    Mock -CommandName Get-InstalledModuleForAzureToken -Verifiable -MockWith {
      return 'Az'
    }   
    Mock -CommandName Install-OriAzBopPrerequisity 
    Mock -CommandName Test-IsAzConnected -Verifiable -MockWith {
      return $false
    }
    Mock -CommandName Connect-AzAccount 
    Mock -CommandName Get-AzCredential 
    Mock -CommandName Write-Debug 

    It 'Should be succesfully processed' {
      {
        $Credential = Invoke-OriAzBopGetAzCredential  
        $Credential.UserName | Should -BeExactly 'PackageManage@oriflame.net'
        $Credential.GetNetworkCredential().Password | Should -BeExactly 'xbchuuuuhaaaatest'
      } | Should -Throw
    }  

    Assert-MockCalled Get-InstalledModuleForAzureToken -Times 1 -Exactly
    Assert-MockCalled Install-OriAzBopPrerequisity -Times 0 -Exactly
    Assert-MockCalled Test-IsAzConnected -Times 1 -Exactly
    Assert-MockCalled Connect-AzAccount -Times 0 -Exactly
    Assert-MockCalled Get-AzCredential -Times 0 -Exactly
    Assert-MockCalled Write-Debug -Times 0 -Exactly -ParameterFilter {
      $Message -ilike "-- End of Invoke-OriAzBopUseAzCredential --"
    }
     
    Assert-VerifiableMock      
  }

  Context 'Succesfull excution module Az.Account installed, but no account profile exists with ConnectAzureWhenRequired' { 
    Mock -CommandName Get-InstalledModuleForAzureToken -Verifiable -MockWith {
      return 'Az'
    }   
    Mock -CommandName Install-OriAzBopPrerequisity 
    Mock -CommandName Test-IsAzConnected -Verifiable -MockWith {
      return $false
    }
    Mock -CommandName Connect-AzAccount -Verifiable
    Mock -CommandName Get-AzCredential -MockWith {
      $password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force
      $RepositoryCredential = New-Object System.Management.Automation.PSCredential 'PackageManage@oriflame.net', $password 
      return $RepositoryCredential
    }
    Mock -CommandName Write-Debug -Verifiable

    It 'Should be succesfully processed' {
      {
        $Credential = Invoke-OriAzBopGetAzCredential -ConnectAzureWhenRequired
        $Credential.UserName | Should -BeExactly 'PackageManage@oriflame.net'
        $Credential.GetNetworkCredential().Password | Should -BeExactly 'xbchuuuuhaaaatest'
      } | Should -Not -Throw
    }  

    Assert-MockCalled Get-InstalledModuleForAzureToken -Times 1 -Exactly
    Assert-MockCalled Install-OriAzBopPrerequisity -Times 0 -Exactly
    Assert-MockCalled Test-IsAzConnected -Times 1 -Exactly
    Assert-MockCalled Connect-AzAccount -Times 1 -Exactly
    Assert-MockCalled Get-AzCredential -Times 1 -Exactly
    Assert-MockCalled Write-Debug -Times 1 -Exactly -ParameterFilter {
      $Message -ilike "-- End of Invoke-OriAzBopUseAzCredential --"
    }
     
    Assert-VerifiableMock      
  }
  
  Context 'Succesfull excution module AzureRM >=3.0 installed and profile exists' {   
    Mock -CommandName Get-InstalledModuleForAzureToken -Verifiable -MockWith {
      return 'Rm3'
    }   
    Mock -CommandName Install-OriAzBopPrerequisity 
    Mock -CommandName Test-IsRmConnected -Verifiable -MockWith {
      return $true
    }
    Mock -CommandName Connect-AzureRmAccount 
    Mock -CommandName Get-AzureRmCredential -MockWith {
      $password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force
      $RepositoryCredential = New-Object System.Management.Automation.PSCredential 'PackageManage@oriflame.net', $password 
      return $RepositoryCredential
    }
    Mock -CommandName Write-Debug -Verifiable

    It 'Should be succesfully processed' {
      {
        $Credential = Invoke-OriAzBopGetAzCredential -ConnectAzureWhenRequired
        $Credential.UserName | Should -BeExactly 'PackageManage@oriflame.net'
        $Credential.GetNetworkCredential().Password | Should -BeExactly 'xbchuuuuhaaaatest'
      } | Should -Not -Throw
    }  

    Assert-MockCalled Get-InstalledModuleForAzureToken -Times 1 -Exactly
    Assert-MockCalled Install-OriAzBopPrerequisity -Times 0 -Exactly
    Assert-MockCalled Test-IsRmConnected -Times 1 -Exactly
    Assert-MockCalled Connect-AzureRmAccount -Times 0 -Exactly
    Assert-MockCalled Get-AzureRmCredential -Times 1 -Exactly
    Assert-MockCalled Write-Debug -Times 1 -Exactly -ParameterFilter {
      $Message -ilike "-- End of Invoke-OriAzBopUseAzCredential --"
    }
     
    Assert-VerifiableMock        
  }
  
  Context 'Succesfull excution module AzureRM >=3.0 installed, but no account profile exists with ConnectAzureWhenRequired' {
    Mock -CommandName Get-InstalledModuleForAzureToken -Verifiable -MockWith {
      return 'Rm3'
    }      
    Mock -CommandName Install-OriAzBopPrerequisity 
    Mock -CommandName Test-IsRmConnected -Verifiable -MockWith {
      return $false
    }
    Mock -CommandName Connect-AzureRmAccount -Verifiable
    Mock -CommandName Get-AzureRmCredential -MockWith {
      $password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force
      $RepositoryCredential = New-Object System.Management.Automation.PSCredential 'PackageManage@oriflame.net', $password 
      return $RepositoryCredential
    }
    Mock -CommandName Write-Debug -Verifiable

    It 'Should be succesfully processed' {
      {
        $Credential = Invoke-OriAzBopGetAzCredential -ConnectAzureWhenRequired
        $Credential.UserName | Should -BeExactly 'PackageManage@oriflame.net'
        $Credential.GetNetworkCredential().Password | Should -BeExactly 'xbchuuuuhaaaatest'
      } | Should -Not -Throw
    }  

    Assert-MockCalled Get-InstalledModuleForAzureToken -Times 1 -Exactly
    Assert-MockCalled Install-OriAzBopPrerequisity -Times 0 -Exactly
    Assert-MockCalled Test-IsRmConnected -Times 1 -Exactly
    Assert-MockCalled Connect-AzureRmAccount -Times 1 -Exactly
    Assert-MockCalled Get-AzureRmCredential -Times 1 -Exactly
    Assert-MockCalled Write-Debug -Times 1 -Exactly -ParameterFilter {
      $Message -ilike "-- End of Invoke-OriAzBopUseAzCredential --"
    }
     
    Assert-VerifiableMock    
  }
  Context 'Succesfull excution module AzureRM >=3.0 installed, but no account profile exists without ConnectAzureWhenRequired' {
    Mock -CommandName Get-InstalledModuleForAzureToken -Verifiable -MockWith {
      return 'Rm3'
    }      
    Mock -CommandName Install-OriAzBopPrerequisity 
    Mock -CommandName Test-IsRmConnected -Verifiable -MockWith {
      return $false
    }
    Mock -CommandName Connect-AzureRmAccount
    Mock -CommandName Get-AzureRmCredential
    Mock -CommandName Write-Debug 

    It 'Should be succesfully processed' {
      {
        Invoke-OriAzBopGetAzCredential 
      } | Should -Throw
    }  

    Assert-MockCalled Get-InstalledModuleForAzureToken -Times 1 -Exactly
    Assert-MockCalled Install-OriAzBopPrerequisity -Times 0 -Exactly
    Assert-MockCalled Test-IsRmConnected -Times 1 -Exactly
    Assert-MockCalled Connect-AzureRmAccount -Times 0 -Exactly
    Assert-MockCalled Get-AzureRmCredential -Times 0 -Exactly
    Assert-MockCalled Write-Debug -Times 0 -Exactly -ParameterFilter {
      $Message -ilike "-- End of Invoke-OriAzBopUseAzCredential --"
    }
     
    Assert-VerifiableMock   
  }
  
  Context 'Succesfull excution module AzureRM <3.0 installed and profile exists' {   
    Mock -CommandName Get-InstalledModuleForAzureToken -Verifiable -MockWith {
      return 'RmOld'
    }     
    Mock -CommandName Install-OriAzBopPrerequisity 
    Mock -CommandName Test-IsRmOldConnected -Verifiable -MockWith {
      return $true
    }
    Mock -CommandName Connect-AzureRmAccount 
    Mock -CommandName Get-AzureRmOldCredential -MockWith {
      $password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force
      $RepositoryCredential = New-Object System.Management.Automation.PSCredential 'PackageManage@oriflame.net', $password 
      return $RepositoryCredential
    }
    Mock -CommandName Write-Debug -Verifiable

    It 'Should be succesfully processed' {
      {
        $Credential = Invoke-OriAzBopGetAzCredential -ConnectAzureWhenRequired
        $Credential.UserName | Should -BeExactly 'PackageManage@oriflame.net'
        $Credential.GetNetworkCredential().Password | Should -BeExactly 'xbchuuuuhaaaatest'
      } | Should -Not -Throw
    }  

    Assert-MockCalled Get-InstalledModuleForAzureToken -Times 1 -Exactly
    Assert-MockCalled Install-OriAzBopPrerequisity -Times 0 -Exactly
    Assert-MockCalled Test-IsRmOldConnected -Times 1 -Exactly
    Assert-MockCalled Connect-AzureRmAccount -Times 0 -Exactly
    Assert-MockCalled Get-AzureRmOldCredential -Times 1 -Exactly
    Assert-MockCalled Write-Debug -Times 1 -Exactly -ParameterFilter {
      $Message -ilike "-- End of Invoke-OriAzBopUseAzCredential --"
    }
     
    Assert-VerifiableMock  
  }
  
  Context 'Succesfull excution module AzureRM <3.0 installed, but no account profile exists with ConnectAzureWhenRequired' {   
    Mock -CommandName Get-InstalledModuleForAzureToken -Verifiable -MockWith {
      return 'RmOld'
    }     
    Mock -CommandName Install-OriAzBopPrerequisity 
    Mock -CommandName Test-IsRmOldConnected -Verifiable -MockWith {
      return $false
    }
    Mock -CommandName Connect-AzureRmAccount -Verifiable
    Mock -CommandName Get-AzureRmOldCredential -MockWith {
      $password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force
      $RepositoryCredential = New-Object System.Management.Automation.PSCredential 'PackageManage@oriflame.net', $password 
      return $RepositoryCredential
    }
    Mock -CommandName Write-Debug -Verifiable

    It 'Should be succesfully processed' {
      {
        $Credential = Invoke-OriAzBopGetAzCredential -ConnectAzureWhenRequired
        $Credential.UserName | Should -BeExactly 'PackageManage@oriflame.net'
        $Credential.GetNetworkCredential().Password | Should -BeExactly 'xbchuuuuhaaaatest'
      } | Should -Not -Throw
    }  

    Assert-MockCalled Get-InstalledModuleForAzureToken -Times 1 -Exactly
    Assert-MockCalled Install-OriAzBopPrerequisity -Times 0 -Exactly
    Assert-MockCalled Test-IsRmOldConnected -Times 1 -Exactly
    Assert-MockCalled Connect-AzureRmAccount -Times 1 -Exactly
    Assert-MockCalled Get-AzureRmOldCredential -Times 1 -Exactly
    Assert-MockCalled Write-Debug -Times 1 -Exactly -ParameterFilter {
      $Message -ilike "-- End of Invoke-OriAzBopUseAzCredential --"
    }
     
    Assert-VerifiableMock 
  }
  
  Context 'Succesfull excution module AzureRM <3.0 installed, but no account profile exists without ConnectAzureWhenRequired' {   
    Mock -CommandName Get-InstalledModuleForAzureToken -Verifiable -MockWith {
      return 'RmOld'
    }     
    Mock -CommandName Install-OriAzBopPrerequisity 
    Mock -CommandName Test-IsRmOldConnected -Verifiable -MockWith {
      return $false
    }
    Mock -CommandName Connect-AzureRmAccount
    Mock -CommandName Get-AzureRmOldCredential
    Mock -CommandName Write-Debug 

    It 'Should be succesfully processed' {
      {
        Invoke-OriAzBopGetAzCredential 
      } | Should -Throw
    }  

    Assert-MockCalled Get-InstalledModuleForAzureToken -Times 1 -Exactly
    Assert-MockCalled Install-OriAzBopPrerequisity -Times 0 -Exactly
    Assert-MockCalled Test-IsRmOldConnected -Times 1 -Exactly
    Assert-MockCalled Connect-AzureRmAccount -Times 0 -Exactly
    Assert-MockCalled Get-AzureRmOldCredential -Times 0 -Exactly
    Assert-MockCalled Write-Debug -Times 0 -Exactly -ParameterFilter {
      $Message -ilike "-- End of Invoke-OriAzBopUseAzCredential --"
    }
     
    Assert-VerifiableMock   
  }


}