PowerShell 6 Core Support (#35)
## About This pull request reflects all changes done in the `linuxsupport` branch. ## Content - Enable PowerShell 6 Core support - Use PFX Certificate for encryption ( fixes #32 ) - Updates CI / CD pipeline ( fixes #31 ) - uses portable libressl ( fixes #34 ) - adds `-PassThru` switch for returning current `VIServer` session in `Connect-To` ( fixes #34 ) - adds git lfs for embedded libressl files - restructured internal functions into `Private` dir - added certificate related functions - adds travis build pipeline for tests
This commit is contained in:
@ -1,15 +1,17 @@
|
||||
$RepoRoot = (Get-GitDirectory).replace('\.git', '')
|
||||
$RepoRoot = (Get-Item -Path (Get-GitDirectory) -Force).Parent | Select-Object -ExpandProperty 'FullName'
|
||||
Write-Verbose -Message ('RepoRoot: {0}' -f $RepoRoot) -Verbose
|
||||
|
||||
$ManifestFilePath = Join-Path -Path $RepoRoot -ChildPath '/src/PSCredentialStore.psd1'
|
||||
Write-Verbose -Message ("ManifestFilePath: {0}" -f $ManifestFilePath) -Verbose
|
||||
Describe "Pre-Flight module tests" {
|
||||
$ManifestFilePath = "{0}\src\PSCredentialstore.psd1" -f $RepoRoot
|
||||
Context "Manifest file related" {
|
||||
It "Test the parsed file itsef" {
|
||||
{ Test-ModuleManifest -Path $ManifestFilePath } | Should -Not -Throw
|
||||
It "Test the parsed file itself" {
|
||||
{ Test-ModuleManifest -Path $ManifestFilePath -Verbose } | Should -Not -Throw
|
||||
}
|
||||
}
|
||||
Context "Module consistency tests" {
|
||||
IT "Importing should work" {
|
||||
{ Import-Module -Name $ManifestFilePath -Global -Force }| Should -Not -Throw
|
||||
It "Importing should work" {
|
||||
{ Import-Module -Name $ManifestFilePath -Global -Force -Verbose } | Should -Not -Throw
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,60 +0,0 @@
|
||||
#region HEADER
|
||||
$RepoRoot = (Get-GitDirectory).replace('\.git', '')
|
||||
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
|
||||
$sut = $sut -replace "\d{2}`_", ''
|
||||
$suthome = (Get-ChildItem -Path $RepoRoot -Exclude ".\tests\" -Filter $sut -Recurse).FullName
|
||||
# Skip try loading the source file if it doesn't exists.
|
||||
If ($suthome.Length -gt 0) {
|
||||
. $suthome
|
||||
}
|
||||
Else {
|
||||
Write-Warning ("Could not find source file {0}" -f $sut)
|
||||
}
|
||||
|
||||
# load additional functions defined in the repository. Replace the expression <FunctionName>.
|
||||
. (Get-ChildItem -Path $RepoRoot -Filter "Get-RandomKey.ps1" -Recurse).FullName
|
||||
|
||||
#endregion HEADER
|
||||
|
||||
Describe "Set-ChallengeFile" {
|
||||
Context "Tests with custom path" {
|
||||
It "Working dir and path not exist" {
|
||||
{Set-ChallengeFile -Path 'C:\PSCredentialStore\Challenge.bin'} | Should -Not -Throw
|
||||
}
|
||||
It "No parameter and non file should return true" {
|
||||
if (Test-Path -Path ("{0}\PSCredentialStore\Challenge.bin" -f $env:ProgramData)) {
|
||||
Remove-Item -Path ("{0}\PSCredentialStore\Challenge.bin" -f $env:ProgramData)
|
||||
}
|
||||
Set-ChallengeFile
|
||||
Test-Path -Path ("{0}\PSCredentialStore\Challenge.bin" -f $env:ProgramData) | Should -Be $true
|
||||
}
|
||||
It "Existing Credential file should return error" {
|
||||
{ Set-ChallengeFile } | Should -Throw
|
||||
Remove-Item -Path ("{0}\PSCredentialStore\Challenge.bin" -f $env:ProgramData)
|
||||
}
|
||||
It "Use -Force switch should create a new challenge file" {
|
||||
# prepare for test and clean up old data
|
||||
if (Test-Path -Path ("{0}\PSCredentialStore\Challenge.bin" -f $env:ProgramData)) {
|
||||
Remove-Item -Path ("{0}\PSCredentialStore\Challenge.bin" -f $env:ProgramData)
|
||||
}
|
||||
Set-ChallengeFile
|
||||
{ Set-ChallengeFile -Force } | Should -Not -Throw
|
||||
}
|
||||
It "Test directory creation for shared store" {
|
||||
if (Test-Path -Path ("{0}\PSCredentialStore" -f $env:ProgramData)) {
|
||||
Remove-Item -Path ("{0}\PSCredentialStore" -f $env:ProgramData) -Force -Recurse
|
||||
}
|
||||
Set-ChallengeFile
|
||||
Test-Path -Path ("{0}\PSCredentialStore" -f $env:ProgramData) | Should -Be $true
|
||||
}
|
||||
}
|
||||
Context "General Exception handling" {
|
||||
Mock New-Item {throw "foobar exception"}
|
||||
It "Test exception handling if the root directory could not be created" {
|
||||
if (Test-Path -Path ("{0}\PSCredentialStore" -f $env:ProgramData)) {
|
||||
Remove-Item -Path ("{0}\PSCredentialStore" -f $env:ProgramData) -Force -Recurse
|
||||
}
|
||||
{ Set-ChallengeFile } | Should -Throw "Could not create the parent data dir"
|
||||
}
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
#region HEADER
|
||||
$RepoRoot = (Get-GitDirectory).replace('\.git', '')
|
||||
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
|
||||
$sut = $sut -replace "\d{2}`_", ''
|
||||
$suthome = (Get-ChildItem -Path $RepoRoot -Exclude ".\tests\" -Filter $sut -Recurse).FullName
|
||||
# Skip try loading the source file if it doesn't exists.
|
||||
If ($suthome.Length -gt 0) {
|
||||
. $suthome
|
||||
}
|
||||
Else {
|
||||
Write-Warning ("Could not find source file {0}" -f $sut)
|
||||
}
|
||||
|
||||
# load additional functions defined in the repository. Replace the expression <FunctionName>.
|
||||
#. (Get-ChildItem -Path $RepoRoot -Filter "Test-ChallengeFile.ps1" -Recurse).FullName
|
||||
|
||||
#endregion HEADER
|
||||
|
||||
Describe "Test-ChallengeFile" {
|
||||
Context "Basic input tests" {
|
||||
Mock Test-Path {return $true}
|
||||
It "No parameter with existing challenge file" {
|
||||
{Test-ChallengeFile} | Should -Not -Throw
|
||||
}
|
||||
It "No parameter and existing file should return true" {
|
||||
Test-ChallengeFile | Should -Be $true
|
||||
}
|
||||
}
|
||||
Context "Execute with parameter" {
|
||||
$TestChFile = "{0}\resources\cs\Challenge.bin" -f $RepoRoot
|
||||
It "Provide valid path" {
|
||||
Test-ChallengeFile -Path $TestChFile | Should -Be $true
|
||||
}
|
||||
It "Provide fake path" {
|
||||
Test-ChallengeFile -Path "C:\notexisting.bin" | Should -Be $false
|
||||
}
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
#region HEADER
|
||||
$RepoRoot = (Get-GitDirectory).replace('\.git', '')
|
||||
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
|
||||
$sut = $sut -replace "\d{2}`_", ''
|
||||
$suthome = (Get-ChildItem -Path $RepoRoot -Exclude ".\tests\" -Filter $sut -Recurse).FullName
|
||||
# Skip try loading the source file if it doesn't exists.
|
||||
If ($suthome.Length -gt 0) {
|
||||
. $suthome
|
||||
}
|
||||
Else {
|
||||
Write-Warning ("Could not find source file {0}" -f $sut)
|
||||
}
|
||||
|
||||
# load additional functions defined in the repository. Replace the expression <FunctionName>.
|
||||
# . (Get-ChildItem -Path $RepoRoot -Filter "<FunctionName>.ps1" -Recurse).FullName
|
||||
|
||||
#endregion HEADER
|
||||
|
||||
Describe "Get-ModuleBase" {
|
||||
Context "Basic syntax check" {
|
||||
It "Test1: Should not throw" {
|
||||
{ Get-ModuleBase } | Should -Not -Throw
|
||||
}
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
#region HEADER
|
||||
$RepoRoot = (Get-GitDirectory).replace('\.git', '')
|
||||
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
|
||||
$sut = $sut -replace "\d{2}`_", ''
|
||||
$suthome = (Get-ChildItem -Path $RepoRoot -Exclude ".\tests\" -Filter $sut -Recurse).FullName
|
||||
# Skip try loading the source file if it doesn't exists.
|
||||
If ($suthome.Length -gt 0) {
|
||||
. $suthome
|
||||
}
|
||||
Else {
|
||||
Write-Warning ("Could not find source file {0}" -f $sut)
|
||||
}
|
||||
|
||||
# load additional functions defined in the repository. Replace the expression <FunctionName>.
|
||||
# . (Get-ChildItem -Path $RepoRoot -Filter "<FunctionName>.ps1" -Recurse).FullName
|
||||
|
||||
#endregion HEADER
|
||||
|
||||
Describe "Get-RandomKey" {
|
||||
Context "Basic input tests" {
|
||||
It "Test1: Should throw if wrong size is given" {
|
||||
{Get-RandomKey -size 43} | Should -Throw
|
||||
}
|
||||
}
|
||||
Context "Basic syntax check" {
|
||||
It "Test1: Should return a key with a length of 16" {
|
||||
$Key = Get-RandomKey -size 16
|
||||
$Key.length | Should -Be 16
|
||||
}
|
||||
It "Test2: Should return a key with a length of 24" {
|
||||
$Key = Get-RandomKey -size 24
|
||||
$Key.length | Should -Be 24
|
||||
}
|
||||
It "Test3: Should return a key with a length of 32" {
|
||||
$Key = Get-RandomKey -size 32
|
||||
$Key.length | Should -Be 32
|
||||
}
|
||||
}
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
#region HEADER
|
||||
$RepoRoot = (Get-GitDirectory).replace('\.git', '')
|
||||
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
|
||||
$sut = $sut -replace "\d{2}`_", ''
|
||||
$suthome = (Get-ChildItem -Path $RepoRoot -Exclude ".\tests\" -Filter $sut -Recurse).FullName
|
||||
# Skip try loading the source file if it doesn't exists.
|
||||
If ($suthome.Length -gt 0) {
|
||||
. $suthome
|
||||
}
|
||||
Else {
|
||||
Write-Warning ("Could not find source file {0}" -f $sut)
|
||||
}
|
||||
|
||||
# load additional functions defined in the repository. Replace the expression <FunctionName>.
|
||||
#. (Get-ChildItem -Path $RepoRoot -Filter "<FunctionName>.ps1" -Recurse).FullName
|
||||
|
||||
#endregion HEADER
|
||||
|
||||
Describe "Test-ModuleName" {
|
||||
Context "Basic input tests" {
|
||||
It "Testing standard module should not throw" {
|
||||
{ Test-Module -Name 'PowerShellGet' -Type Module } | Should -Not -Throw
|
||||
}
|
||||
It "Existing module should return true" {
|
||||
Test-Module -Name 'PowerShellGet' -Type Module | Should -Be $true
|
||||
}
|
||||
}
|
||||
Context "Custom Type tests" {
|
||||
It "Using custom type should throw" {
|
||||
{ Test-Module -Name "foobarr" -Type Custom} | Should -Throw
|
||||
}
|
||||
}
|
||||
Context "Working with PSSnapins" {
|
||||
It "Loading first PSSnaping should not throw " {
|
||||
$Snap = Get-PSSnapin -Registered | Select-Object -First 1
|
||||
{ Test-Module -Name $Snap.Name -Type PSSnapin } | Should -Not -Throw
|
||||
}
|
||||
It "Loading first PSSnaping should return true" {
|
||||
$Snap = Get-PSSnapin -Registered | Select-Object -First 1
|
||||
Test-Module -Name $Snap.Name -Type PSSnapin | Should -Be $true
|
||||
}
|
||||
It "Not existing PSSnaping should return false" {
|
||||
Test-Module -Name 'foobar2000' -Type PSSnapin | Should -Be $false
|
||||
}
|
||||
It "StopifFails switch should thrown an error" {
|
||||
{Test-Module -Name 'foobar2000' -Type PSSnapin -StopIfFails }| Should -Throw
|
||||
}
|
||||
}
|
||||
Context "Working with modules" {
|
||||
It "Loading first module should not throw " {
|
||||
$Mod = Get-Module -ListAvailable | Select-Object -First 1
|
||||
{ Test-Module -Name $Mod.Name -Type Module } | Should -Not -Throw
|
||||
}
|
||||
It "Loading first module should return true" {
|
||||
$Snap = Get-Module -ListAvailable | Select-Object -First 1
|
||||
Test-Module -Name $Snap.Name -Type Module | Should -Be $true
|
||||
}
|
||||
It "Not existing module should return false" {
|
||||
Test-Module -Name 'foobar2000' -Type Module | Should -Be $false
|
||||
}
|
||||
It "StopifFails switch should thrown an error" {
|
||||
{Test-Module -Name 'foobar2000' -Type Module -StopIfFails }| Should -Throw
|
||||
}
|
||||
}
|
||||
}
|
@ -1,113 +0,0 @@
|
||||
#region HEADER
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
# $RepoRoot = (Get-Item -Path $here).Parent.Parent.FullName
|
||||
$RepoRoot = (Get-GitDirectory).replace('\.git', '')
|
||||
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
|
||||
$sut = $sut -replace "\d{2}`_", ''
|
||||
$suthome = (Get-ChildItem -Path $RepoRoot -Exclude ".\tests\" -Filter $sut -Recurse).FullName
|
||||
# Skip try loading the source file if it doesn't exists.
|
||||
If ($suthome.Length -gt 0) {
|
||||
. $suthome
|
||||
}
|
||||
Else {
|
||||
Write-Warning ("Could not find source file {0}" -f $sut)
|
||||
}
|
||||
|
||||
# load additional functions defined in the repository. Replace the expression <FunctionName>.
|
||||
#. (Get-ChildItem -Path $RepoRoot -Filter "Test-CredentialStore.ps1" -Recurse).FullName
|
||||
#. (Get-ChildItem -Path $RepoRoot -Filter "New-CredentialStore.ps1" -Recurse).FullName
|
||||
#. (Get-ChildItem -Path $RepoRoot -Filter "Get-CredentialStore.ps1" -Recurse).FullName
|
||||
#. (Get-ChildItem -Path $RepoRoot -Filter "Get-CredentialStoreItem.ps1" -Recurse).FullName
|
||||
. (Get-ChildItem -Path $RepoRoot -Filter "Test-ChallengeFile.ps1" -Recurse).FullName
|
||||
. (Get-ChildItem -Path $RepoRoot -Filter "Get-ChallengeFile.ps1" -Recurse).FullName
|
||||
. (Get-ChildItem -Path $RepoRoot -Filter "Set-ChallengeFile.ps1" -Recurse).FullName
|
||||
. (Get-ChildItem -Path $RepoRoot -Filter "Get-RandomKey.ps1" -Recurse).FullName
|
||||
|
||||
#endregion HEADER
|
||||
|
||||
Describe "New-CredentialStoreItem" {
|
||||
Context "Private Credential Store tests" {
|
||||
It "Test1: Add entry to existing private store." {
|
||||
If (-not (Test-CredentialStore)) {
|
||||
New-CredentialStore
|
||||
}
|
||||
[String]$tmp = (65..90) + (97..122) | Get-Random -Count 5 | % {[char]$_}
|
||||
$tmp = $tmp.Replace(' ', '')
|
||||
$tmpUser = "MyUser"
|
||||
$tmpPwd = "fooobarysdfsfs" | ConvertTo-SecureString -AsPlainText -Force
|
||||
$creds = New-Object -TypeName PsCredential -ArgumentList $tmpUser, $tmpPwd
|
||||
New-CredentialStoreItem -RemoteHost $tmp -Credential $creds
|
||||
# Had to remove the `{ <exp> } | Shoud Not Throw` because the return would be empty.
|
||||
$content = Get-CredentialStoreItem -RemoteHost $tmp
|
||||
$content.UserName | Should Be "MyUser"
|
||||
#Cleanup Temp entry
|
||||
$CS = Get-CredentialStore
|
||||
$CS.PSObject.Properties.Remove($tmp)
|
||||
ConvertTo-Json -InputObject $CS | Out-File -FilePath ("{0}\CredentialStore.json" -f $env:AppData)
|
||||
}
|
||||
}
|
||||
Context "Test with new shared Credential Store" {
|
||||
It "Test2: Create new RemoteHost entry" {
|
||||
# prepare test environment
|
||||
$tmpCS = 'C:\CredentialStore.json'
|
||||
New-CredentialStore -Shared -Path $tmpCS
|
||||
|
||||
$UserName = "myuser"
|
||||
$Password = ConvertTo-SecureString -String "mypasswd" -AsPlainText -Force
|
||||
$mycreds = New-Object -TypeName PSCredential -ArgumentList $UserName, $Password
|
||||
$RemoteHost = "foobar"
|
||||
{ New-CredentialStoreItem -Path $tmpCS -RemoteHost $RemoteHost -Credential $mycreds -Shared } | Should Not Throw
|
||||
$tmpCS = Get-Content -Path $tmpCS -Raw | ConvertFrom-Json
|
||||
$res = Get-Member -InputObject $tmpCS -Name $RemoteHost -Membertype Properties
|
||||
$res.Name | Should Be $RemoteHost
|
||||
}
|
||||
It "Adds Item with identifier to shared store" {
|
||||
$tmpCS = 'C:\CredentialStore.json'
|
||||
$UserName = "myuser"
|
||||
$Password = ConvertTo-SecureString -String "mypasswd" -AsPlainText -Force
|
||||
$mycreds = New-Object -TypeName PSCredential -ArgumentList $UserName, $Password
|
||||
$RemoteHost = "foobar2"
|
||||
New-CredentialStoreItem -Path $tmpCS -RemoteHost $RemoteHost -Credential $mycreds -Identifier 'Foo'
|
||||
$writtenItem = Get-CredentialStoreItem -Path $tmpCS -RemoteHost $RemoteHost -Identifier 'Foo'
|
||||
($writtenItem.UserName -eq $UserName) -and ($writtenItem.Password.Length -gt 0) | Should -Be $true
|
||||
}
|
||||
}
|
||||
Context "Test optional parameter lookup" {
|
||||
Mock Get-Credential {
|
||||
$UserName = 'testuser'
|
||||
$Password = ConvertTo-SecureString -String "mypasswd" -AsPlainText -Force
|
||||
return [PSCredential]::new($UserName, $Password)
|
||||
|
||||
}
|
||||
It "Test missing Credential" {
|
||||
$tmpCS = 'C:\CredentialStore.json'
|
||||
New-CredentialStoreItem -Path $tmpCs -Shared -RemoteHost 'foobar3'
|
||||
$writtenItem = Get-CredentialStoreItem -Path $tmpCS -Shared -RemoteHost 'foobar3'
|
||||
$writtenItem.UserName | Should -Be "testuser"
|
||||
}
|
||||
}
|
||||
Context "General Exception handling" {
|
||||
Mock Test-CredentialStore {return $false}
|
||||
Mock Get-Credential {
|
||||
$UserName = 'myUser'
|
||||
$Password = ConvertTo-SecureString -String "mypasswd" -AsPlainText -Force
|
||||
return [PSCredential]::new($UserName, $Password)
|
||||
|
||||
}
|
||||
It "Missing CredentialStore should throw" {
|
||||
{ New-CredentialStoreItem -Path 'C:\missingStore.json' -RemoteHost 'notrelevant' } | Should -Throw "Could not add anything"
|
||||
}
|
||||
}
|
||||
Context "Testing pipeline paramter" {
|
||||
It "Add the item with credential value from pipe" {
|
||||
$UserName = 'pipeUser'
|
||||
$Password = ConvertTo-SecureString -String "pipePasswd" -AsPlainText -Force
|
||||
{ [PSCredential]::new($UserName, $Password) | New-CredentialStoreItem -RemoteHost 'PipeHost' } | Should -Not -Throw
|
||||
}
|
||||
|
||||
It "Testing written item" {
|
||||
(Get-CredentialStoreItem -RemoteHost 'PipeHost').UserName | Should -Be 'pipeUser'
|
||||
}
|
||||
}
|
||||
|
||||
}
|
85
tests/Item/03_New-CredentialStoreItem.Tests.ps1
Normal file
85
tests/Item/03_New-CredentialStoreItem.Tests.ps1
Normal file
@ -0,0 +1,85 @@
|
||||
Describe "New-CredentialStoreItem" {
|
||||
Context "Private Credential Store tests" {
|
||||
It "Test1: Add entry to existing private store." {
|
||||
# Creat a fresh CredentialStore first
|
||||
New-CredentialStore -Force
|
||||
|
||||
[String]$tmp = (65..90) + (97..122) | Get-Random -Count 5 | % {[char]$_}
|
||||
$tmp = $tmp.Replace(' ', '')
|
||||
$tmpUser = "MyUser"
|
||||
$tmpPwd = "fooobarysdfsfs" | ConvertTo-SecureString -AsPlainText -Force
|
||||
$creds = [PSCredential]::new($tmpUser, $tmpPwd)
|
||||
New-CredentialStoreItem -RemoteHost $tmp -Credential $creds
|
||||
# Had to remove the `{ <exp> } | Shoud Not Throw` because the return would be empty.
|
||||
$content = Get-CredentialStoreItem -RemoteHost $tmp
|
||||
$content.UserName | Should -Be "MyUser"
|
||||
#Cleanup Temp entry
|
||||
$CS = Get-CredentialStore
|
||||
$CS.PSObject.Properties.Remove($tmp)
|
||||
ConvertTo-Json -InputObject $CS | Out-File -FilePath (Get-DefaultCredentialStorePath)
|
||||
}
|
||||
}
|
||||
Context "Test with new shared Credential Store" {
|
||||
It "Test2: Create new RemoteHost entry" {
|
||||
# prepare test environment
|
||||
$tmpCS = Join-Path -Path (Get-TempDir) -ChildPath '/CredentialStore.json'
|
||||
New-CredentialStore -Shared -Path $tmpCS -Force
|
||||
|
||||
$UserName = "myuser"
|
||||
$Password = ConvertTo-SecureString -String "mypasswd" -AsPlainText -Force
|
||||
$mycreds = [PSCredential]::new($UserName, $Password)
|
||||
$RemoteHost = "foobar"
|
||||
{ New-CredentialStoreItem -Shared -Path $tmpCS -RemoteHost $RemoteHost -Credential $mycreds } | Should -Not -Throw
|
||||
$tmpCS = Get-Content -Path $tmpCS -Raw | ConvertFrom-Json
|
||||
$res = Get-Member -InputObject $tmpCS -Name $RemoteHost -Membertype Properties
|
||||
$res.Name | Should -Be $RemoteHost
|
||||
}
|
||||
It "Adds Item with identifier to shared store" {
|
||||
$tmpCS = Join-Path -Path (Get-TempDir) -ChildPath '/CredentialStore.json'
|
||||
New-CredentialStore -Shared -Path $tmpCS -Force
|
||||
|
||||
$UserName = "myuser"
|
||||
$Password = ConvertTo-SecureString -String "mypasswd" -AsPlainText -Force
|
||||
$mycreds = [PSCredential]::new($UserName, $Password)
|
||||
$RemoteHost = "foobar2"
|
||||
New-CredentialStoreItem -Shared -Path $tmpCS -RemoteHost $RemoteHost -Credential $mycreds -Identifier 'Foo'
|
||||
$writtenItem = Get-CredentialStoreItem -Shared -Path $tmpCS -RemoteHost $RemoteHost -Identifier 'Foo'
|
||||
($writtenItem.UserName -eq $UserName) -and ($writtenItem.Password.Length -gt 0) | Should -Be $true
|
||||
}
|
||||
}
|
||||
Context "Test optional parameter lookup" {
|
||||
|
||||
It "Test missing Credential" {
|
||||
function global:Get-Credential ([string]$Message) {
|
||||
$UserName = 'testuser'
|
||||
$Password = ConvertTo-SecureString -String "mypasswd" -AsPlainText -Force
|
||||
return [PSCredential]::new($UserName, $Password)
|
||||
}
|
||||
$tmpCS = Join-Path -Path (Get-TempDir) -ChildPath '/CredentialStore.json'
|
||||
New-CredentialStoreItem -Path $tmpCs -Shared -RemoteHost 'foobar3'
|
||||
$writtenItem = Get-CredentialStoreItem -Path $tmpCS -Shared -RemoteHost 'foobar3'
|
||||
$writtenItem.UserName | Should -Be "testuser"
|
||||
|
||||
Remove-Item -Path 'Function:\Get-Credential'
|
||||
}
|
||||
|
||||
}
|
||||
Context "General Exception handling" {
|
||||
Mock Test-CredentialStore {return $false}
|
||||
It "Missing CredentialStore should throw" {
|
||||
{ New-CredentialStoreItem -Shared -Path 'C:\missingStore.json' -RemoteHost 'notrelevant' } | Should -Throw "Could not add anything"
|
||||
}
|
||||
}
|
||||
Context "Testing pipeline paramter" {
|
||||
It "Add the item with credential value from pipe" {
|
||||
$UserName = 'pipeUser'
|
||||
$Password = ConvertTo-SecureString -String "pipePasswd" -AsPlainText -Force
|
||||
{ [PSCredential]::new($UserName, $Password) | New-CredentialStoreItem -RemoteHost 'PipeHost' } | Should -Not -Throw
|
||||
}
|
||||
|
||||
It "Testing written item" {
|
||||
(Get-CredentialStoreItem -RemoteHost 'PipeHost').UserName | Should -Be 'pipeUser'
|
||||
}
|
||||
}
|
||||
|
||||
}
|
49
tests/Private/01_Get-DefaultCredentialStorePath.Tests.ps1
Normal file
49
tests/Private/01_Get-DefaultCredentialStorePath.Tests.ps1
Normal file
@ -0,0 +1,49 @@
|
||||
Describe "Get-DefaultCredentialStorePath" {
|
||||
Context "Basic syntax test" {
|
||||
It "Test1: Should not throw" {
|
||||
{ Get-DefaultCredentialStorePath } | Should -Not -Throw
|
||||
}
|
||||
}
|
||||
|
||||
Context "Private Type" {
|
||||
It "Should return correct paths" {
|
||||
$Path = Get-DefaultCredentialStorePath
|
||||
#Write-Verbose -Message ('Delivered path is: {0}' -f $Path) -Verbose
|
||||
if ($Env:APPVEYOR) {
|
||||
$PathRef = Join-Path -Path $Env:APPDATA -ChildPath 'CredentialStore.json'
|
||||
$Path | Should -Be $PathRef
|
||||
}
|
||||
elseif ($ENV:TRAVIS) {
|
||||
if ($IsLinux) {
|
||||
$PathRef = Join-Path -Path $Env:HOME -ChildPath 'CredentialStore.json'
|
||||
$Path | Should -Be $PathRef
|
||||
}
|
||||
elseif ($IsMacOS) {
|
||||
$PathRef = Join-Path -Path $Env:HOME -ChildPath 'CredentialStore.json'
|
||||
$Path | Should -Be $PathRef
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Context "Shared Type" {
|
||||
It "Should return correct paths" {
|
||||
$Path = Get-DefaultCredentialStorePath -Shared
|
||||
#Write-Verbose -Message ('Delivered path is: {0}' -f $Path) -Verbose
|
||||
if ($Env:APPVEYOR) {
|
||||
$PathRef = Join-Path -Path $env:ProgramData -ChildPath 'PSCredentialStore/CredentialStore.json'
|
||||
$Path | Should -Be $PathRef
|
||||
}
|
||||
elseif ($ENV:TRAVIS) {
|
||||
if ($IsLinux) {
|
||||
$PathRef = Join-Path -Path '/var/opt' -ChildPath 'PSCredentialStore/CredentialStore.json'
|
||||
$Path | Should -Be $PathRef
|
||||
}
|
||||
elseif ($IsMacOS) {
|
||||
$PathRef = Join-Path -Path '/var/opt' -ChildPath 'PSCredentialStore/CredentialStore.json'
|
||||
$Path | Should -Be $PathRef
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
7
tests/Private/01_Get-ModuleBase.Tests.ps1
Normal file
7
tests/Private/01_Get-ModuleBase.Tests.ps1
Normal file
@ -0,0 +1,7 @@
|
||||
Describe "Get-ModuleBase" {
|
||||
Context "Basic syntax check" {
|
||||
It "Test1: Should not throw" {
|
||||
{ Get-ModuleBase } | Should -Not -Throw
|
||||
}
|
||||
}
|
||||
}
|
13
tests/Private/01_Get-RandomAESKey.Tests.ps1
Normal file
13
tests/Private/01_Get-RandomAESKey.Tests.ps1
Normal file
@ -0,0 +1,13 @@
|
||||
Describe "Get-RandomKey" {
|
||||
Context "Basic input tests" {
|
||||
It "Test1: Should not throw " {
|
||||
{Get-RandomAESKey} | Should -Not -Throw
|
||||
}
|
||||
}
|
||||
Context "Basic syntax check" {
|
||||
It "Test2: Should return a key with a length of 32 bytes" {
|
||||
$Key = Get-RandomAESKey
|
||||
$Key.length | Should -Be 32
|
||||
}
|
||||
}
|
||||
}
|
27
tests/Private/01_Get-TempDir.Tests.ps1
Normal file
27
tests/Private/01_Get-TempDir.Tests.ps1
Normal file
@ -0,0 +1,27 @@
|
||||
Describe "Get-TempDir" {
|
||||
Context "Basic tests" {
|
||||
It "Should not throw" {
|
||||
{Get-TempDir} | Should -Not -Throw
|
||||
}
|
||||
It "Should return the correct os tmp path" {
|
||||
$Path = Get-TempDir
|
||||
|
||||
if ($ENV:TRAVIS) {
|
||||
if ($IsLinux) {
|
||||
$RefPath = (Resolve-Path -Path '/tmp/').Path
|
||||
$Path | Should -Be $RefPath
|
||||
}
|
||||
if ($IsMacOS) {
|
||||
$RefPath = (Resolve-Path -Path '/tmp/').Path
|
||||
$Path | Should -Be $RefPath
|
||||
}
|
||||
}
|
||||
if ($Env:APPVEYOR) {
|
||||
if (($isWindows) -or ($PSVersionTable.PSVersion.Major -lt 6) -or ($PSVersionTable.PSEdition -eq 'Desktop')) {
|
||||
$RefPath = (Resolve-Path -Path $env:TEMP).Path
|
||||
$Path | Should -Be $RefPath
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,27 +1,6 @@
|
||||
#region HEADER
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
# $RepoRoot = (Get-Item -Path $here).Parent.Parent.FullName
|
||||
$RepoRoot = (Get-GitDirectory).replace('\.git', '')
|
||||
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
|
||||
$sut = $sut -replace "\d{2}`_", ''
|
||||
$suthome = (Get-ChildItem -Path $RepoRoot -Exclude ".\tests\" -Filter $sut -Recurse).FullName
|
||||
# Skip try loading the source file if it doesn't exists.
|
||||
If ($suthome.Length -gt 0) {
|
||||
. $suthome
|
||||
}
|
||||
Else {
|
||||
Write-Warning ("Could not find source file {0}" -f $sut)
|
||||
}
|
||||
|
||||
# load additional functions defined in the repository. Replace the expression <FunctionName>.
|
||||
. (Get-ChildItem -Path $RepoRoot -Filter "Get-ModuleBase.ps1" -Recurse).FullName
|
||||
. (Get-ChildItem -Path $RepoRoot -Filter "Test-Module.ps1" -Recurse).FullName
|
||||
|
||||
#endregion HEADER
|
||||
|
||||
Describe "Resolve-Dependency" {
|
||||
Context "Basic syntax check" {
|
||||
Mock Get-ModuleBase {return "{0}\resources" -f $PWD}
|
||||
Mock Get-ModuleBase {return (Join-Path -Path $PWD -ChildPath '/resources')}
|
||||
Mock Test-Module {return $true}
|
||||
It "Test1: Should not throw" {
|
||||
{ Resolve-Dependency -Name 'foobar2000' } | Should -Not -Throw
|
||||
@ -32,7 +11,10 @@ Describe "Resolve-Dependency" {
|
||||
}
|
||||
Context "Enforce Error" {
|
||||
# Return incorrect module base to enforce there is no config file.
|
||||
Mock Get-ModuleBase {return "C:\"}
|
||||
Mock Get-ModuleBase {
|
||||
if ($IsWindows) {return "C:\"}
|
||||
elseif ($isLinux) {return "/"}
|
||||
}
|
||||
It "Missing dependency file should not cause an error" {
|
||||
{ Resolve-Dependency -Name 'awesome'} | Should -Not -Throw
|
||||
}
|
||||
@ -42,7 +24,7 @@ Describe "Resolve-Dependency" {
|
||||
}
|
||||
}
|
||||
Context "Testing input variations" {
|
||||
Mock Get-ModuleBase {return "{0}\resources" -f $PWD}
|
||||
Mock Get-ModuleBase {return (Join-Path -Path $PWD -ChildPath '/resources')}
|
||||
It "Should return true if all given dependencies exist" {
|
||||
Resolve-Dependency -Name 'Existing' | Should -Be $true
|
||||
}
|
26
tests/Private/01_Test-Module.Tests.ps1
Normal file
26
tests/Private/01_Test-Module.Tests.ps1
Normal file
@ -0,0 +1,26 @@
|
||||
Describe "Test-ModuleName" {
|
||||
Context "Basic input tests" {
|
||||
It "Testing standard module should not throw" {
|
||||
{ Test-Module -Name 'PowerShellGet' } | Should -Not -Throw
|
||||
}
|
||||
It "Existing module should return true" {
|
||||
Test-Module -Name 'PowerShellGet' | Should -Be $true
|
||||
}
|
||||
}
|
||||
Context "Working with modules" {
|
||||
It "Loading first module should not throw " {
|
||||
$Mod = Get-Module -ListAvailable | Select-Object -First 1
|
||||
{ Test-Module -Name $Mod.Name } | Should -Not -Throw
|
||||
}
|
||||
It "Loading first module should return true" {
|
||||
$Snap = Get-Module -ListAvailable | Select-Object -First 1
|
||||
Test-Module -Name $Snap.Name | Should -Be $true
|
||||
}
|
||||
It "Not existing module should return false" {
|
||||
Test-Module -Name 'foobar2000' | Should -Be $false
|
||||
}
|
||||
It "StopifFails switch should thrown an error" {
|
||||
{Test-Module -Name 'foobar2000' -StopIfFails }| Should -Throw
|
||||
}
|
||||
}
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
#region HEADER
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
# $RepoRoot = (Get-Item -Path $here).Parent.Parent.FullName
|
||||
$RepoRoot = (Get-GitDirectory).replace('\.git', '')
|
||||
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
|
||||
$sut = $sut -replace "\d{2}`_", ''
|
||||
$suthome = (Get-ChildItem -Path $RepoRoot -Exclude ".\tests\" -Filter $sut -Recurse).FullName
|
||||
# Skip try loading the source file if it doesn't exists.
|
||||
If ($suthome.Length -gt 0) {
|
||||
. $suthome
|
||||
}
|
||||
Else {
|
||||
Write-Warning ("Could not find source file {0}" -f $sut)
|
||||
}
|
||||
|
||||
# load additional functions defined in the repository. Replace the expression <FunctionName>.
|
||||
#. (Get-ChildItem -Path $RepoRoot -Filter "Test-CredentialStore.ps1" -Recurse).FullName
|
||||
|
||||
#endregion HEADER
|
||||
|
||||
Describe "Get-CredentialStore" {
|
||||
Context "Basic logic tests" {
|
||||
$TestCredentialStore = Resolve-Path -Path ("{0}\resources\cs\CredentialStore.json" -f $RepoRoot)
|
||||
It "Test1: Read CS without params" {
|
||||
If (Test-Path -Path ("{0}\CredentialStore.json" -f $env:APPDATA)) {
|
||||
{Get-CredentialStore} | Should Not Throw
|
||||
}
|
||||
Else {
|
||||
Write-Warning "Default private Credential Store not found. Skipping..."
|
||||
}
|
||||
}
|
||||
It "Test2: Read Credential Store with testing data" {
|
||||
|
||||
{Get-CredentialStore -Path $TestCredentialStore} | Should Not Throw
|
||||
}
|
||||
It "Test3: Not existing path should return false" {
|
||||
{ Get-CredentialStore -Path 'C:\foobar\CredentialStore.json' -Shared }| Should -Throw "Could not find the CredentialStore."
|
||||
}
|
||||
}
|
||||
Context "Testing invalid json data" {
|
||||
Mock Test-CredentialStore {return $true}
|
||||
Mock Get-Content {return '"foo":"bar",'}
|
||||
It "Should throw with invalid CredentialStore" {
|
||||
{ Get-Credentialstore -Path "C:\dummy.json"} | Should -Throw "Unknown CredentialStore format. Invalid JSON file."
|
||||
}
|
||||
}
|
||||
}
|
@ -1,120 +0,0 @@
|
||||
#region HEADER
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
# $RepoRoot = (Get-Item -Path $here).Parent.Parent.FullName
|
||||
$RepoRoot = (Get-GitDirectory).replace('\.git', '')
|
||||
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
|
||||
$sut = $sut -replace "\d{2}`_", ''
|
||||
$suthome = (Get-ChildItem -Path $RepoRoot -Exclude ".\tests\" -Filter $sut -Recurse).FullName
|
||||
# Skip try loading the source file if it doesn't exists.
|
||||
If ($suthome.Length -gt 0) {
|
||||
. $suthome
|
||||
}
|
||||
Else {
|
||||
Write-Warning ("Could not find source file {0}" -f $sut)
|
||||
}
|
||||
|
||||
# load additional functions defined in the repository. Replace the expression <FunctionName>.
|
||||
#. (Get-ChildItem -Path $RepoRoot -Filter "Test-CredentialStore.ps1" -Recurse).FullName
|
||||
. (Get-ChildItem -Path $RepoRoot -Filter "Test-ChallengeFile.ps1" -Recurse).FullName
|
||||
. (Get-ChildItem -Path $RepoRoot -Filter "Set-ChallengeFile.ps1" -Recurse).FullName
|
||||
. (Get-ChildItem -Path $RepoRoot -Filter "Get-RandomKey.ps1" -Recurse).FullName
|
||||
|
||||
#endregion HEADER
|
||||
|
||||
|
||||
# Backup existing credential stores
|
||||
$VerbosePreference = "Continue"
|
||||
Write-Verbose "Backup private Credential Store..."
|
||||
$CSPath = ("{0}\CredentialStore.json" -f $env:APPDATA)
|
||||
$BackupFile = "{0}.back" -f $CSPath
|
||||
If (Test-Path -Path $CSPath) {
|
||||
Move-Item -Path $CSPath -Destination $BackupFile
|
||||
}
|
||||
Write-Verbose "Backup shared CredentialStore..."
|
||||
$CSShared = ("{0}\PSCredentialStore\CredentialStore.json" -f $env:ProgramData)
|
||||
$BackupSharedFile = "{0}.back" -f $CSShared
|
||||
If (Test-Path -Path $CSShared) {
|
||||
Move-Item -Path $CSShared -Destination $BackupSharedFile
|
||||
}
|
||||
Write-Verbose "Remove old CredentialStore in Temp dir"
|
||||
$CSTemp = "{0}\CredentialStore.json" -f $Env:TEMP
|
||||
If (Test-Path -Path $CSTemp) {
|
||||
Remove-Item -Path $CSTemp
|
||||
}
|
||||
$VerbosePreference = "SilentlyContinue"
|
||||
|
||||
Describe "New-CredentialStore" {
|
||||
Context "Private CS tests" {
|
||||
$pCS = Join-Path -Path $env:APPDATA -ChildPath "CredentialStore.json"
|
||||
It "Test1: Create new private CredentialStore" {
|
||||
New-CredentialStore
|
||||
$result = Test-Path -Path $pCS
|
||||
$CS = Get-Content -Path $pCS -Raw | ConvertFrom-Json
|
||||
($result -eq $True) -and ($CS.Type -eq "Private") | Should Be $True
|
||||
}
|
||||
It "Test2: Try to override private Store" {
|
||||
{New-CredentialStore} | Should Throw
|
||||
}
|
||||
It "Test3: Reset existing Credential Store" {
|
||||
$now = Get-Date
|
||||
$CS = Get-Content -Path $pCS -Raw | ConvertFrom-Json
|
||||
$CSCreation = [DateTime]$CS.Creation
|
||||
New-CredentialStore -Force
|
||||
$now -gt $csCreation | Should Be $True
|
||||
}
|
||||
}
|
||||
Context "Shared CS tests" {
|
||||
$pCS = Join-Path -Path $env:ProgramData -ChildPath "PSCredentialStore\CredentialStore.json"
|
||||
It "Test1: Create a new Shared Credential Store" {
|
||||
New-CredentialStore -Shared
|
||||
Test-Path -Path ("{0}\PSCredentialStore\CredentialStore.json" -f $env:ProgramData) | Should Be $True
|
||||
}
|
||||
It "Test2: Try to override existing shared CS" {
|
||||
{New-CredentialStore -Shared} | Should Throw
|
||||
}
|
||||
It "Test3: Reset shared CredentialStore" {
|
||||
$now = Get-Date
|
||||
$CS = Get-Content -Path $pCS -Raw | ConvertFrom-Json
|
||||
$CSCreation = [DateTime]$CS.Creation
|
||||
New-CredentialStore -Force -Shared
|
||||
$now -gt $csCreation | Should Be $True
|
||||
}
|
||||
}
|
||||
Context "Custom Shared CS tests" {
|
||||
$pCS = Join-Path -Path $env:TEMP -ChildPath "CredentialStore.json"
|
||||
It "Test1: Create new custom shared" {
|
||||
{New-CredentialStore -Path $pCS -Shared} | Should Not Throw
|
||||
}
|
||||
It "Test2: Try to override exiting one" {
|
||||
{New-CredentialStore -Path $pCS -Shared} | Should Throw
|
||||
}
|
||||
It "Test3: Reset existing custom CredentialStore" {
|
||||
{New-CredentialStore -Path $pCS -Shared -Force} | Should Not Throw
|
||||
}
|
||||
}
|
||||
Context "Test exception handling" {
|
||||
Mock Out-File {throw "foobar exception"}
|
||||
It "JSON Converstion should fail and throw" {
|
||||
{ New-CredentialStore -Path "C:\dummy.json"} | Should -Throw
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Cleanup test stores and restore existing ones.
|
||||
$VerbosePreference = "Continue"
|
||||
Write-Verbose "Restoring private CredentialStore"
|
||||
If (Test-Path -Path $BackupFile) {
|
||||
If (Test-Path -Path $CSPath) {
|
||||
Remove-Item -Path $CSPath
|
||||
Move-Item -Path $BackupFile -Destination $CSPath
|
||||
}
|
||||
}
|
||||
|
||||
Write-Verbose "Restoring shared CredentialStore"
|
||||
If (Test-Path -Path $BackupSharedFile) {
|
||||
If (Test-Path -Path $CSShared) {
|
||||
Remove-Item -Path $CSShared
|
||||
Move-Item -Path $BackupSharedFile -Destination $CSShared
|
||||
}
|
||||
}
|
||||
$VerbosePreference = "SilentlyContinue"
|
@ -1,49 +0,0 @@
|
||||
#region HEADER
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
# $RepoRoot = (Get-Item -Path $here).Parent.Parent.FullName
|
||||
$RepoRoot = (Get-GitDirectory).replace('\.git', '')
|
||||
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
|
||||
$sut = $sut -replace "\d{2}`_", ''
|
||||
$suthome = (Get-ChildItem -Path $RepoRoot -Exclude ".\tests\" -Filter $sut -Recurse).FullName
|
||||
# Skip try loading the source file if it doesn't exists.
|
||||
If ($suthome.Length -gt 0) {
|
||||
. $suthome
|
||||
}
|
||||
Else {
|
||||
Write-Warning ("Could not find source file {0}" -f $sut)
|
||||
}
|
||||
|
||||
# load additional functions defined in the repository. Replace the expression <FunctionName>.
|
||||
# . (Get-ChildItem -Path $RepoRoot -Filter "<Function-Name>.ps1" -Recurse).FullName
|
||||
|
||||
#endregion HEADER
|
||||
|
||||
Describe "Test-CredentialStore" {
|
||||
Context "Basic logic tests" {
|
||||
$TestCredentialStore = Resolve-Path -Path ("{0}\resources\cs\CredentialStore.json" -f $RepoRoot)
|
||||
It "Test1: Should Not Throw" {
|
||||
{ Test-CredentialStore -Path $TestCredentialStore } | Should Not Throw
|
||||
}
|
||||
It "Test2: Read valid CredentialStore" {
|
||||
$res = Test-CredentialStore -Path $TestCredentialStore
|
||||
$res | Should Be $True
|
||||
}
|
||||
It "Test3: Read a broken CredentialStore" {
|
||||
$BrokenCS = Resolve-Path -Path ("{0}\resources\cs\Broken_CS.json" -f $RepoRoot)
|
||||
$oWarningPreference = $WarningPreference
|
||||
$WarningPreference = 'SilentlyContinue'
|
||||
$res = Test-CredentialStore -Path $BrokenCS
|
||||
$res | Should Be $False
|
||||
$WarningPreference = $oWarningPreference
|
||||
}
|
||||
It "Test4: Not existing path should return false" {
|
||||
Test-CredentialStore -Path 'C:\foobar\CredentialStore.json' | Should -Be $false
|
||||
}
|
||||
It "Test5: testing private CredentialStore path" {
|
||||
if (Test-Path -Path ("{0}\CredentialStore.json" -f $env:APPDATA) ) {
|
||||
Remove-Item -Path ("{0}\CredentialStore.json" -f $env:APPDATA)
|
||||
}
|
||||
Test-CredentialStore | Should -Be $false
|
||||
}
|
||||
}
|
||||
}
|
96
tests/Store/02_New-CredentialStore.Tests.ps1
Normal file
96
tests/Store/02_New-CredentialStore.Tests.ps1
Normal file
@ -0,0 +1,96 @@
|
||||
# Backup existing credential stores
|
||||
$VerbosePreference = "Continue"
|
||||
Write-Verbose "Backup private Credential Store..."
|
||||
$CSPath = Get-DefaultCredentialStorePath
|
||||
$BackupFile = "{0}.back" -f $CSPath
|
||||
If (Test-Path -Path $CSPath) {
|
||||
Move-Item -Path $CSPath -Destination $BackupFile
|
||||
}
|
||||
Write-Verbose "Backup shared CredentialStore..."
|
||||
$CSShared = Get-DefaultCredentialStorePath -Shared
|
||||
$BackupSharedFile = "{0}.back" -f $CSShared
|
||||
If (Test-Path -Path $CSShared) {
|
||||
Move-Item -Path $CSShared -Destination $BackupSharedFile
|
||||
}
|
||||
Write-Verbose "Remove old CredentialStore in Temp dir"
|
||||
$CSTemp = Join-Path -Path (Get-TempDir) -ChildPath '/CredentialStore.json'
|
||||
If (Test-Path -Path $CSTemp) {
|
||||
Remove-Item -Path $CSTemp
|
||||
}
|
||||
$VerbosePreference = "SilentlyContinue"
|
||||
|
||||
Describe "New-CredentialStore" {
|
||||
Context "Private CS tests" {
|
||||
$pCS = Get-DefaultCredentialStorePath
|
||||
It "Test1: Create new private CredentialStore" {
|
||||
{ New-CredentialStore -Confirm:$false } | Should -Not -Throw
|
||||
$result = Test-Path -Path $pCS
|
||||
$CS = Get-Content -Path $pCS -Raw | ConvertFrom-Json
|
||||
($result -eq $true) -and ($CS.Type -eq "Private") | Should -Be $true
|
||||
}
|
||||
It "Test2: Try to override private Store" {
|
||||
{ New-CredentialStore -Confirm:$false } | Should -Throw
|
||||
}
|
||||
It "Test3: Reset existing Credential Store" {
|
||||
$now = Get-Date
|
||||
$CS = Get-Content -Path $pCS -Raw | ConvertFrom-Json
|
||||
$CSCreation = [DateTime]$CS.Created
|
||||
New-CredentialStore -Confirm:$false -Force
|
||||
$now -gt $csCreation | Should -Be $true
|
||||
}
|
||||
}
|
||||
Context "Shared CS tests" {
|
||||
$sCS = Get-DefaultCredentialStorePath -Shared
|
||||
It "Test1: Create a new Shared Credential Store" {
|
||||
{ New-CredentialStore -Confirm:$false -Shared } | Should -Not -Throw
|
||||
Test-Path -Path $sCS | Should -Be $true
|
||||
}
|
||||
It "Test2: Try to override existing shared CS" {
|
||||
{New-CredentialStore -Shared -Confirm:$false} | Should -Throw
|
||||
}
|
||||
It "Test3: Reset shared CredentialStore" {
|
||||
$now = Get-Date
|
||||
$CS = Get-Content -Path $sCS -Raw | ConvertFrom-Json
|
||||
$CSCreation = [DateTime]$CS.Created
|
||||
New-CredentialStore -Force -Shared -Confirm:$false
|
||||
$now -gt $csCreation | Should -Be $true
|
||||
}
|
||||
}
|
||||
Context "Custom Shared CS tests" {
|
||||
$cCS = Join-Path -Path (Get-TempDir) -ChildPath "CredentialStore.json"
|
||||
It "Test1: Create new custom shared" {
|
||||
{New-CredentialStore -Path $cCS -Shared -Confirm:$false} | Should -Not -Throw
|
||||
}
|
||||
It "Test2: Try to override exiting one" {
|
||||
{New-CredentialStore -Path $cCS -Shared -Confirm:$false} | Should -Throw
|
||||
}
|
||||
It "Test3: Reset existing custom CredentialStore" {
|
||||
{New-CredentialStore -Path $cCS -Shared -Force -Confirm:$false} | Should -Not -Throw
|
||||
}
|
||||
}
|
||||
Context "Test exception handling" {
|
||||
Mock Out-File {throw "foobar exception"}
|
||||
It "JSON Conversion should fail and throw" {
|
||||
{ New-CredentialStore -Path (Join-Path -Path (Get-TempDir) -ChildPath '/dummy.json') -Shared -Confirm:$false} | Should -Throw
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Cleanup test stores and restore existing ones.
|
||||
$VerbosePreference = "Continue"
|
||||
Write-Verbose "Restoring private CredentialStore"
|
||||
If (Test-Path -Path $BackupFile) {
|
||||
If (Test-Path -Path $CSPath) {
|
||||
Remove-Item -Path $CSPath
|
||||
Move-Item -Path $BackupFile -Destination $CSPath
|
||||
}
|
||||
}
|
||||
|
||||
Write-Verbose "Restoring shared CredentialStore"
|
||||
If (Test-Path -Path $BackupSharedFile) {
|
||||
If (Test-Path -Path $CSShared) {
|
||||
Remove-Item -Path $CSShared
|
||||
Move-Item -Path $BackupSharedFile -Destination $CSShared
|
||||
}
|
||||
}
|
||||
$VerbosePreference = "SilentlyContinue"
|
36
tests/Store/02_Test-CredentialStore.Tests.ps1
Normal file
36
tests/Store/02_Test-CredentialStore.Tests.ps1
Normal file
@ -0,0 +1,36 @@
|
||||
$RepoRoot = (Get-Item -Path (Get-GitDirectory) -Force).Parent | Select-Object -ExpandProperty 'FullName'
|
||||
|
||||
Describe "Test-CredentialStore" {
|
||||
Context "Basic logic tests" {
|
||||
$TestCredentialStore = Join-Path -Path $RepoRoot -ChildPath '/resources/cs/CredentialStore.json'
|
||||
It "Test1: Should Not Throw" {
|
||||
{ Test-CredentialStore -Shared -Path $TestCredentialStore } | Should -Not -Throw
|
||||
}
|
||||
It "Test2: Read valid CredentialStore" {
|
||||
$res = Test-CredentialStore -Shared -Path $TestCredentialStore
|
||||
$res | Should -Be $true
|
||||
}
|
||||
It "Test3: Read a broken CredentialStore" {
|
||||
$BrokenCS = Join-Path -Path $RepoRoot -ChildPath '{0}/resources/cs/Broken_CS.json'
|
||||
$oWarningPreference = $WarningPreference
|
||||
$WarningPreference = 'SilentlyContinue'
|
||||
$res = Test-CredentialStore -Shared -Path $BrokenCS
|
||||
$res | Should -Be $false
|
||||
$WarningPreference = $oWarningPreference
|
||||
}
|
||||
It "Test4: Not existing path should return false" {
|
||||
if ($isWindows -or ($PSVersionTable.PSVersion.Major -eq 5)) {
|
||||
Test-CredentialStore -Shared -Path 'C:\foobar\CredentialStore.json' | Should -Be $false
|
||||
}
|
||||
elseif ($isWindows -or $IsMacOS) {
|
||||
Test-CredentialStore -Shared -Path '/var/opt/foo.json' | Should -Be $false
|
||||
}
|
||||
}
|
||||
It "Test5: testing private CredentialStore path" {
|
||||
if (Test-Path -Path (Get-DefaultCredentialStorePath)) {
|
||||
Remove-Item -Path (Get-DefaultCredentialStorePath)
|
||||
}
|
||||
Test-CredentialStore | Should -Be $false
|
||||
}
|
||||
}
|
||||
}
|
32
tests/Store/03_Get-CredentialStore.Tests.ps1
Normal file
32
tests/Store/03_Get-CredentialStore.Tests.ps1
Normal file
@ -0,0 +1,32 @@
|
||||
$RepoRoot = (Get-Item -Path (Get-GitDirectory) -Force).Parent | Select-Object -ExpandProperty 'FullName'
|
||||
|
||||
Describe "Get-CredentialStore" {
|
||||
Context "Basic logic tests" {
|
||||
$TestCredentialStore = Join-Path -Path $RepoRoot -ChildPath 'resources/cs/CredentialStore.json'
|
||||
$TestPfxCert = Join-Path -Path $RepoRoot -ChildPath 'resources/cs/PSCredentialStore.pfx'
|
||||
'TestCredentialStore: {0}' -f $TestCredentialStore
|
||||
It "Test1: Read CS without params" {
|
||||
if (! (Test-Path -Path (Get-DefaultCredentialStorePath)) ) {
|
||||
{ New-CredentialStore -Force } | Should -Not -Throw
|
||||
|
||||
}
|
||||
{ Get-CredentialStore } | Should -Not -Throw
|
||||
}
|
||||
It "Test2: Read Credential Store with testing data" {
|
||||
{ Use-PfxCertificate -Shared -CredentialStore $TestCredentialStore -Path $TestPfxCert } | Should -Not -Throw
|
||||
{ Get-CredentialStore -Shared -Path $TestCredentialStore } | Should -Not -Throw
|
||||
}
|
||||
It "Test3: Not existing path should return false" {
|
||||
{ Get-CredentialStore -Shared -Path './CredentialStore.json' }| Should -Throw "Could not find the CredentialStore."
|
||||
}
|
||||
}
|
||||
Context "Testing invalid json data" {
|
||||
#Mock Test-CredentialStore {return $true}
|
||||
#Mock Get-Content {return '"foo":"bar",'}
|
||||
$BrokenCS = Join-Path -Path $RepoRoot -ChildPath 'resources/cs/Broken_CS.json'
|
||||
Write-Verbose -Message ('BrokenCS Path: {0}' -f $BrokenCS) -Verbose
|
||||
It "Should throw with invalid CredentialStore" {
|
||||
{ Get-CredentialStore -Path -Shared $BrokenCS } | Should -Throw
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user