Tests/Import-OriAzBopPsModule.tests.ps1
. $PSScriptRoot\..\Public\Wait-OriAzBopModuleComplete.ps1 . $PSScriptRoot\..\Private\Invoke-ModuleInstall.ps1 . $PSScriptRoot\..\Private\Invoke-OriAzExrExceptionRetry.ps1 . $PSScriptRoot\..\Private\Test-GetModule.ps1 . $PSScriptRoot\..\Public\Register-OriAzBopRepository.ps1 . $PSScriptRoot\..\Public\Install-OriAzBopPrerequisity.ps1 . $PSScriptRoot\..\Public\Import-OriAzBopPsModule.ps1 Describe 'OriAzBopBoostrapOriPsgallery\Import-OriAzBopPsModule' { Context 'Succesfull excution Module is not yet installed, install with prefix' { Mock -CommandName Invoke-WebRequest -Verifiable -MockWith { # Some source for Invoke-Expression return ";" } $testPath = "TestDrive:\test.psd1" Set-Content $testPath -value "my test text." Mock -CommandName Install-OriAzBopPrerequisity -Verifiable Mock -CommandName Test-GetModule -Verifiable -MockWith { return $false } Mock -CommandName Register-OriAzBopRepository -Verifiable Mock -CommandName Invoke-ModuleInstall -Verifiable -MockWith { return [PSCustomObject]@{ InstalledLocation = "TestDrive:\test.psd1" } } Mock -CommandName Wait-OriAzBopModuleComplete -Verifiable Mock -CommandName Import-Module -Verifiable Mock -CommandName Write-Verbose -Verifiable it 'Should be succesfully processed' { { $password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force $RepositoryCredential = New-Object System.Management.Automation.PSCredential 'PackageManage@oriflame.net', $password Import-OriAzBopPsModule ` -Name OriAzEncEnvironmentConfiguration ` -RequiredVersion 1.0.48 ` -Credential $RepositoryCredential ` -Prefix XXX } | Should -Not -Throw } Assert-MockCalled Invoke-WebRequest -Times 1 -Exactly -ParameterFilter { $Uri -eq "https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1" } Assert-MockCalled Install-OriAzBopPrerequisity -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" -and $MinimumVersion -eq [Version]::new(2, 2, 5) } Assert-MockCalled Install-OriAzBopPrerequisity -Times 1 -Exactly -ParameterFilter { $Name -eq "PackageManagement" -and $MinimumVersion -eq [Version]::new(1, 4, 7) } Assert-MockCalled Test-GetModule -Times 1 -Exactly -ParameterFilter { $Name -eq "OriAzEncEnvironmentConfiguration" -and $RequiredVersion -eq [Version]::new(1, 0, 48) } Assert-MockCalled Register-OriAzBopRepository -Times 1 -Exactly -ParameterFilter { $PsProjectName.Contains("PackageManagementFeed") -and ` $NugetProjectName.Contains("DeploymentPackages") -and ` ![string]::IsNullOrEmpty($RepositoryCredential) } Assert-MockCalled Invoke-ModuleInstall -Times 1 -Exactly -ParameterFilter { $Name -eq "OriAzEncEnvironmentConfiguration" ` -and ![string]::IsNullOrEmpty($Credential) ` -and $Repository -eq "PackageManagementFeed" ` -and $RequiredVersion -eq [Version]::new(1, 0, 48) } Assert-MockCalled Wait-OriAzBopModuleComplete -Times 1 -Exactly Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter { $Prefix -eq "XXX" } Assert-MockCalled Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -eq "-- End of Import-OriAzBopPsModule --" } Assert-VerifiableMock } Context 'Succesfull excution Module is not yet installed, install without prefix' { Mock -CommandName Invoke-WebRequest -Verifiable -MockWith { # Some source for Invoke-Expression return ";" } $testPath = "TestDrive:\test.psd1" Set-Content $testPath -value "my test text." Mock -CommandName Install-OriAzBopPrerequisity -Verifiable Mock -CommandName Test-GetModule -Verifiable -MockWith { return $false } Mock -CommandName Register-OriAzBopRepository -Verifiable Mock -CommandName Invoke-ModuleInstall -Verifiable -MockWith { return [PSCustomObject]@{ InstalledLocation = "TestDrive:\test.psd1" } } Mock -CommandName Wait-OriAzBopModuleComplete -Verifiable Mock -CommandName Import-Module -Verifiable Mock -CommandName Write-Verbose -Verifiable it 'Should be succesfully processed' { { $password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force $RepositoryCredential = New-Object System.Management.Automation.PSCredential 'PackageManage@oriflame.net', $password Import-OriAzBopPsModule ` -Name OriAzEncEnvironmentConfiguration ` -RequiredVersion 1.0.48 ` -Credential $RepositoryCredential } | Should -Not -Throw } Assert-MockCalled Invoke-WebRequest -Times 1 -Exactly -ParameterFilter { $Uri -eq "https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1" } Assert-MockCalled Install-OriAzBopPrerequisity -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" -and $MinimumVersion -eq [Version]::new(2, 2, 5) } Assert-MockCalled Install-OriAzBopPrerequisity -Times 1 -Exactly -ParameterFilter { $Name -eq "PackageManagement" -and $MinimumVersion -eq [Version]::new(1, 4, 7) } Assert-MockCalled Test-GetModule -Times 1 -Exactly -ParameterFilter { $Name -eq "OriAzEncEnvironmentConfiguration" -and $RequiredVersion -eq [Version]::new(1, 0, 48) } Assert-MockCalled Register-OriAzBopRepository -Times 1 -Exactly -ParameterFilter { $PsProjectName.Contains("PackageManagementFeed") -and ` $NugetProjectName.Contains("DeploymentPackages") -and ` ![string]::IsNullOrEmpty($RepositoryCredential) } Assert-MockCalled Invoke-ModuleInstall -Times 1 -Exactly -ParameterFilter { $Name -eq "OriAzEncEnvironmentConfiguration" ` -and ![string]::IsNullOrEmpty($Credential) ` -and $Repository -eq "PackageManagementFeed" ` -and $RequiredVersion -eq [Version]::new(1, 0, 48) } Assert-MockCalled Wait-OriAzBopModuleComplete -Times 1 -Exactly Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter { [string]::IsNullOrEmpty($Prefix) } Assert-MockCalled Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -eq "-- End of Import-OriAzBopPsModule --" } Assert-VerifiableMock } Context 'Succesfull excution Module is not yet installed skip install credentail provider' { Mock -CommandName Invoke-WebRequest -MockWith { # Some source for Invoke-Expression return ";" } $testPath = "TestDrive:\test.psd1" Set-Content $testPath -value "my test text." Mock -CommandName Install-OriAzBopPrerequisity -Verifiable Mock -CommandName Test-GetModule -Verifiable -MockWith { return $false } Mock -CommandName Register-OriAzBopRepository -Verifiable Mock -CommandName Invoke-ModuleInstall -Verifiable -MockWith { return [PSCustomObject]@{ InstalledLocation = "TestDrive:\test.psd1" } } Mock -CommandName Wait-OriAzBopModuleComplete -Verifiable Mock -CommandName Import-Module -Verifiable Mock -CommandName Write-Verbose -Verifiable it 'Should be succesfully processed' { { $password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force $RepositoryCredential = New-Object System.Management.Automation.PSCredential 'PackageManage@oriflame.net', $password Import-OriAzBopPsModule ` -Name OriAzEncEnvironmentConfiguration ` -RequiredVersion 1.0.48 ` -Credential $RepositoryCredential ` -SkipInstallCredProvider } | Should -Not -Throw } Assert-MockCalled Invoke-WebRequest -Times 0 -Exactly -ParameterFilter { $Uri -eq "https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1" } Assert-MockCalled Install-OriAzBopPrerequisity -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" -and $MinimumVersion -eq [Version]::new(2, 2, 5) } Assert-MockCalled Install-OriAzBopPrerequisity -Times 1 -Exactly -ParameterFilter { $Name -eq "PackageManagement" -and $MinimumVersion -eq [Version]::new(1, 4, 7) } Assert-MockCalled Test-GetModule -Times 1 -Exactly -ParameterFilter { $Name -eq "OriAzEncEnvironmentConfiguration" -and $RequiredVersion -eq [Version]::new(1, 0, 48) } Assert-MockCalled Register-OriAzBopRepository -Times 1 -Exactly -ParameterFilter { $PsProjectName.Contains("PackageManagementFeed") -and ` $NugetProjectName.Contains("DeploymentPackages") -and ` ![string]::IsNullOrEmpty($RepositoryCredential) } Assert-MockCalled Invoke-ModuleInstall -Times 1 -Exactly -ParameterFilter { $Name -eq "OriAzEncEnvironmentConfiguration" ` -and ![string]::IsNullOrEmpty($Credential) ` -and $Repository -eq "PackageManagementFeed" ` -and $RequiredVersion -eq [Version]::new(1, 0, 48) } Assert-MockCalled Wait-OriAzBopModuleComplete -Times 1 -Exactly Assert-MockCalled Import-Module -Times 1 -Exactly Assert-MockCalled Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -eq "-- End of Import-OriAzBopPsModule --" } Assert-VerifiableMock } Context 'Succesfull excution Module is not yet installed with skip import' { Mock -CommandName Invoke-WebRequest -Verifiable -MockWith { # Some source for Invoke-Expression return ";" } $testPath = "TestDrive:\test.psd1" Set-Content $testPath -value "my test text." Mock -CommandName Install-OriAzBopPrerequisity -Verifiable Mock -CommandName Test-GetModule Mock -CommandName Register-OriAzBopRepository -Verifiable Mock -CommandName Invoke-ModuleInstall -Verifiable -MockWith { return [PSCustomObject]@{ InstalledLocation = "TestDrive:\test.psd1" } } Mock -CommandName Wait-OriAzBopModuleComplete Mock -CommandName Import-Module Mock -CommandName Write-Verbose -Verifiable it 'Should be succesfully processed' { { $password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force $RepositoryCredential = New-Object System.Management.Automation.PSCredential 'PackageManage@oriflame.net', $password Import-OriAzBopPsModule ` -Name OriAzEncEnvironmentConfiguration ` -RequiredVersion 1.0.48 ` -Credential $RepositoryCredential ` -SkipImport } | Should -Not -Throw } Assert-MockCalled Invoke-WebRequest -Times 1 -Exactly -ParameterFilter { $Uri -eq "https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1" } Assert-MockCalled Install-OriAzBopPrerequisity -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" -and $MinimumVersion -eq [Version]::new(2, 2, 5) } Assert-MockCalled Install-OriAzBopPrerequisity -Times 1 -Exactly -ParameterFilter { $Name -eq "PackageManagement" -and $MinimumVersion -eq [Version]::new(1, 4, 7) } Assert-MockCalled Test-GetModule -Times 0 -Exactly Assert-MockCalled Register-OriAzBopRepository -Times 1 -Exactly -ParameterFilter { $PsProjectName.Contains("PackageManagementFeed") -and ` $NugetProjectName.Contains("DeploymentPackages") -and ` ![string]::IsNullOrEmpty($RepositoryCredential) } Assert-MockCalled Invoke-ModuleInstall -Times 1 -Exactly -ParameterFilter { $Name -eq "OriAzEncEnvironmentConfiguration" ` -and ![string]::IsNullOrEmpty($Credential) ` -and $Repository -eq "PackageManagementFeed" ` -and $RequiredVersion -eq [Version]::new(1, 0, 48) } Assert-MockCalled Wait-OriAzBopModuleComplete -Times 0 -Exactly Assert-MockCalled Import-Module -Times 0 -Exactly Assert-MockCalled Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -eq "-- End of Import-OriAzBopPsModule --" } Assert-VerifiableMock } Context 'Succesfull excution Module is already installed' { Mock -CommandName Invoke-WebRequest -MockWith { # Some source for Invoke-Expression return ";" } $testPath = "TestDrive:\test.psd1" Set-Content $testPath -value "my test text." Mock -CommandName Install-OriAzBopPrerequisity Mock -CommandName Test-GetModule -Verifiable -MockWith { return $true } Mock -CommandName Register-OriAzBopRepository Mock -CommandName Invoke-ModuleInstall -MockWith { return [PSCustomObject]@{ InstalledLocation = "TestDrive:\test.psd1" } } Mock -CommandName Wait-OriAzBopModuleComplete Mock -CommandName Import-Module Mock -CommandName Write-Verbose -Verifiable it 'Should be succesfully processed' { { $password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force $RepositoryCredential = New-Object System.Management.Automation.PSCredential 'PackageManage@oriflame.net', $password Import-OriAzBopPsModule ` -Name OriAzEncEnvironmentConfiguration ` -RequiredVersion 1.0.48 ` -Credential $RepositoryCredential } | Should -Not -Throw } Assert-MockCalled Invoke-WebRequest -Times 0 -Exactly Assert-MockCalled Install-OriAzBopPrerequisity -Times 0 -Exactly Assert-MockCalled Test-GetModule -Times 1 -Exactly -ParameterFilter { $Name -eq "OriAzEncEnvironmentConfiguration" -and $RequiredVersion -eq [Version]::new(1, 0, 48) } Assert-MockCalled Register-OriAzBopRepository -Times 0 -Exactly Assert-MockCalled Invoke-ModuleInstall -Times 0 -Exactly Assert-MockCalled Wait-OriAzBopModuleComplete -Times 0 -Exactly Assert-MockCalled Import-Module -Times 0 -Exactly Assert-MockCalled Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -eq "Module OriAzEncEnvironmentConfiguration is already imported." } Assert-VerifiableMock } } |