forked from chocolatey/ChocoCCM
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chocolatey#66) Adds Tests to ChocoCCM
Adds test covering public functions within ChocoCCM. Admittedly, currently only ~66% code coverage. This does, however, hit pretty much every _working_ command - most of the missing percentage are argument completers (which I am to improve), `if not session throw` blocks (which I aim to remove), and those commands that aren't working. Exceptions to be improved include: - Set-CCMDeploymentStep, which isn't implemented fully and should probably just be removed for now. - Remove-CCMStaleDeployment, which is currently broken - Import-PDQDeployPackage, because I couldn't find an example of an importable file.
- Loading branch information
Showing
37 changed files
with
3,764 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ gen/generated | |
.DS_Store | ||
Output/ | ||
.dotnet/ | ||
coverage.xml | ||
testResults.xml |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
Import-Module $PSScriptRoot\..\..\ChocoCCM.psd1 | ||
|
||
Describe "Connect-CCMServer" { | ||
BeforeAll { | ||
Mock Invoke-WebRequest -ModuleName ChocoCCM -MockWith { | ||
if ($SessionVariable -eq "Session") { | ||
$script:Session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() | ||
} | ||
} | ||
} | ||
|
||
Context "Logging into a HTTP Host" { | ||
BeforeAll { | ||
$TestValues = @{ | ||
Hostname = "New-Guid" | ||
Credential = [pscredential]::new( | ||
"$(New-Guid)", | ||
("$(New-Guid)" | ConvertTo-SecureString -AsPlainText -Force) | ||
) | ||
UseSSL = $false | ||
} | ||
$Result = Connect-CCMServer @TestValues | ||
} | ||
|
||
It "Returns Nothing" { | ||
$Result | Should -BeNullOrEmpty | ||
} | ||
|
||
It "Called /Account/Login using the provided Credentials" { | ||
Should -Invoke Invoke-WebRequest -ModuleName ChocoCCM -Scope Context -Times 1 -Exactly -ParameterFilter { | ||
$Method -eq "POST" -and | ||
$Uri -eq "http://$($TestValues.Hostname)/Account/Login" -and | ||
$ContentType -eq "application/x-www-form-urlencoded" -and | ||
$Body.usernameOrEmailAddress -eq $TestValues.Credential.UserName -and | ||
$Body.password -eq $TestValues.Credential.GetNetworkCredential().Password | ||
} | ||
} | ||
|
||
It "Sets the Protocol Variable" { | ||
InModuleScope ChocoCCM { $script:Protocol } | Should -Be "http" | ||
} | ||
|
||
It "Sets the Session Variable" -Skip { | ||
# TODO: Fix this test. | ||
InModuleScope ChocoCCM { $script:Session } | Should -Not -BeNullOrEmpty | ||
} | ||
|
||
It "Sets the Hostname Variable" { | ||
InModuleScope ChocoCCM { $script:Hostname } | Should -Be $TestValues.Hostname | ||
} | ||
} | ||
|
||
Context "Logging into a HTTPS Host" { | ||
BeforeAll { | ||
$TestValues = @{ | ||
Hostname = "New-Guid" | ||
Credential = [pscredential]::new( | ||
"$(New-Guid)", | ||
("$(New-Guid)" | ConvertTo-SecureString -AsPlainText -Force) | ||
) | ||
UseSSL = $true | ||
} | ||
$Result = Connect-CCMServer @TestValues | ||
} | ||
|
||
It "Returns Nothing" { | ||
$Result | Should -BeNullOrEmpty | ||
} | ||
|
||
It "Called /Account/Login using the provided Credentials" { | ||
Should -Invoke Invoke-WebRequest -ModuleName ChocoCCM -Scope Context -Times 1 -Exactly -ParameterFilter { | ||
$Method -eq "POST" -and | ||
$Uri -eq "https://$($TestValues.Hostname)/Account/Login" -and | ||
$ContentType -eq "application/x-www-form-urlencoded" -and | ||
$Body.usernameOrEmailAddress -eq $TestValues.Credential.UserName -and | ||
$Body.password -eq $TestValues.Credential.GetNetworkCredential().Password | ||
} | ||
} | ||
|
||
It "Sets the Protocol Variable" { | ||
InModuleScope ChocoCCM { $script:Protocol } | Should -Be "https" | ||
} | ||
|
||
It "Sets the Session Variable" -Skip { | ||
# TODO: Fix this test. | ||
InModuleScope ChocoCCM { $script:Session } | Should -Not -BeNullOrEmpty | ||
} | ||
|
||
It "Sets the Hostname Variable" { | ||
InModuleScope ChocoCCM { $script:Hostname } | Should -Be $TestValues.Hostname | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
Import-Module $PSScriptRoot\..\..\ChocoCCM.psd1 | ||
|
||
Describe "Disable-CCMDeployment" { | ||
BeforeAll { | ||
InModuleScope -ModuleName ChocoCCM { | ||
$script:Session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() | ||
$script:HostName = "localhost" | ||
$script:Protocol = "http" | ||
} | ||
Mock Get-CCMDeployment -ModuleName ChocoCCM { | ||
@{Id = $script:TestGuid} | ||
} | ||
Mock Invoke-RestMethod -ModuleName ChocoCCM | ||
} | ||
|
||
Context "Disabling a deployment" { | ||
BeforeAll { | ||
$script:TestGuid = "$(New-Guid)" | ||
$TestParams = @{ | ||
Deployment = "Your Deployment" | ||
} | ||
|
||
$Result = Disable-CCMDeployment @TestParams -Confirm:$false | ||
} | ||
|
||
It "Calls the API to disable the deployment" { | ||
Should -Invoke Invoke-RestMethod -ModuleName ChocoCCM -Scope Context -Times 1 -Exactly -ParameterFilter { | ||
if ($Body -is [string]) { | ||
$Body = $Body | ConvertFrom-Json | ||
} | ||
|
||
$Method -eq "POST" -and | ||
$Uri.AbsolutePath -eq "/api/services/app/DeploymentPlans/Archive" -and | ||
$ContentType -eq "application/json" -and | ||
$Body.id -eq $script:TestGuid | ||
} | ||
} | ||
|
||
It "Returns Nothing" { | ||
$Result | Should -BeNullOrEmpty | ||
} | ||
} | ||
|
||
Context "Disabling a non-existent deployment" -Skip { | ||
BeforeAll { | ||
Mock Get-CCMDeployment -ModuleName ChocoCCM { | ||
throw [Microsoft.PowerShell.Commands.HttpResponseException]::new("Response status code does not indicate success: 400 (Bad Request).") | ||
} | ||
Mock Invoke-RestMethod -ModuleName ChocoCCM { | ||
throw [System.Management.Automation.RuntimeException]::new("Response status code does not indicate success: 400 (Bad Request).") | ||
} | ||
} | ||
|
||
It "Throws an error when the deployment doesn't exist" { | ||
{Disable-CCMDeployment @TestParams -Confirm:$false} | Should -Throw | ||
} | ||
} | ||
|
||
Context "Failing to disable a deployment" { | ||
BeforeAll { | ||
Mock Invoke-RestMethod -ModuleName ChocoCCM { | ||
throw [System.Management.Automation.RuntimeException]::new("Response status code does not indicate success: 400 (Bad Request).") | ||
} | ||
} | ||
|
||
It "Throws an error when the API call fails" { | ||
{Disable-CCMDeployment @TestParams -Confirm:$false} | Should -Throw | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
Import-Module $PSScriptRoot\..\..\ChocoCCM.psd1 | ||
|
||
Describe "Export-CCMDeployment" { | ||
BeforeAll { | ||
InModuleScope -ModuleName ChocoCCM { | ||
$script:Session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() | ||
$script:HostName = "localhost" | ||
$script:Protocol = "http" | ||
} | ||
Mock Get-CCMDeployment -ModuleName ChocoCCM { | ||
@{ | ||
Name = $Name | ||
Description = "This is a deployment" | ||
DeploymentSteps = @( | ||
@{ | ||
Name = "Step 1" | ||
Description = "This is a step" | ||
Type = "Install" | ||
Package = @{ | ||
Name = "7zip" | ||
Version = "19.00" | ||
} | ||
} | ||
@{ | ||
Name = "Step 2" | ||
Description = "This is a step" | ||
Type = "Install" | ||
Package = @{ | ||
Name = "Firefox" | ||
Version = "21.00" | ||
} | ||
} | ||
) | ||
} | ||
} | ||
} | ||
|
||
Context "Exporting a Deployment" { | ||
BeforeAll { | ||
$TestParams = @{ | ||
Deployment = "$(New-Guid)" | ||
OutFile = Join-Path $TestDrive "TestFile.xml" | ||
} | ||
|
||
$Result = Export-CCMDeployment @TestParams | ||
} | ||
|
||
It "Calls the API to get the content of the specified deployment" { | ||
Should -Invoke Get-CCMDeployment -ModuleName ChocoCCM -Scope Context -Times 1 -Exactly -ParameterFilter { | ||
$Name -eq $TestParams.Deployment | ||
} | ||
} | ||
|
||
It "Exports the content to the specified file" { | ||
$TestParams.OutFile | Should -FileContentMatch "This is a deployment" | ||
$TestParams.OutFile | Should -FileContentMatch $TestParams.Deployment | ||
} | ||
|
||
It "Returns Nothing" { | ||
$Result | Should -BeNullOrEmpty | ||
} | ||
} | ||
|
||
Context "Exporting Deployment Steps Only" { | ||
BeforeAll { | ||
$TestParams = @{ | ||
Deployment = "$(New-Guid)" | ||
DeploymentStepsOnly = $true | ||
OutFile = Join-Path $TestDrive "TestFile.xml" | ||
} | ||
|
||
$Result = Export-CCMDeployment @TestParams | ||
} | ||
|
||
It "Calls the API to get the content of the specified deployment" { | ||
Should -Invoke Get-CCMDeployment -ModuleName ChocoCCM -Scope Context -Times 1 -Exactly -ParameterFilter { | ||
$Name -eq $TestParams.Deployment | ||
} | ||
} | ||
|
||
It "Exports the deployment steps to the specified file" { | ||
$TestParams.OutFile | Should -Not -FileContentMatch "This is a deployment" | ||
$TestParams.OutFile | Should -Not -FileContentMatch $TestParams.Deployment | ||
$TestParams.OutFile | Should -FileContentMatch "This is a step" | ||
} | ||
|
||
It "Returns Nothing" { | ||
$Result | Should -BeNullOrEmpty | ||
} | ||
} | ||
|
||
Context "Exporting to an existing file" { | ||
BeforeAll { | ||
$TestParams = @{ | ||
Deployment = "$(New-Guid)" | ||
OutFile = Join-Path $TestDrive "TestFile.xml" | ||
AllowClobber = $false | ||
} | ||
|
||
New-Item -Path $TestParams.OutFile -Value "This is an old deployment" | ||
|
||
$Result = Export-CCMDeployment @TestParams | ||
} | ||
|
||
It "Calls the API to get the content of the specified deployment" { | ||
Should -Invoke Get-CCMDeployment -ModuleName ChocoCCM -Scope Context -Times 1 -Exactly -ParameterFilter { | ||
$Name -eq $TestParams.Deployment | ||
} | ||
} | ||
|
||
It "Does not overwrite the deployment steps in the specified file" -Skip { | ||
# This currently fails because the file is overwritten regardless of the AllowClobber parameter | ||
$TestParams.OutFile | Should -FileContentMatch "This is an old deployment" | ||
$TestParams.OutFile | Should -Not -FileContentMatch "This is a deployment" | ||
} | ||
|
||
It "Returns Nothing" { | ||
$Result | Should -BeNullOrEmpty | ||
} | ||
} | ||
|
||
Context "Overwriting an existing file" { | ||
BeforeAll { | ||
$TestParams = @{ | ||
Deployment = "$(New-Guid)" | ||
OutFile = Join-Path $TestDrive "TestFile.xml" | ||
AllowClobber = $true | ||
} | ||
|
||
New-Item -Path $TestParams.OutFile -Value "This is an old deployment" | ||
|
||
$Result = Export-CCMDeployment @TestParams | ||
} | ||
|
||
It "Calls the API to get the content of the specified deployment" { | ||
Should -Invoke Get-CCMDeployment -ModuleName ChocoCCM -Scope Context -Times 1 -Exactly -ParameterFilter { | ||
$Name -eq $TestParams.Deployment | ||
} | ||
} | ||
|
||
It "Exports the deployment to the specified file" { | ||
$TestParams.OutFile | Should -Not -FileContentMatch "This is an old deployment" | ||
$TestParams.OutFile | Should -FileContentMatch "This is a deployment" | ||
} | ||
|
||
It "Returns Nothing" { | ||
$Result | Should -BeNullOrEmpty | ||
} | ||
} | ||
} |
Oops, something went wrong.