Skip to content

Commit

Permalink
(chocolatey#66) Adds Tests to ChocoCCM
Browse files Browse the repository at this point in the history
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
JPRuskin committed May 6, 2023
1 parent 74349e9 commit bc38a0c
Show file tree
Hide file tree
Showing 37 changed files with 3,764 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ gen/generated
.DS_Store
Output/
.dotnet/
coverage.xml
testResults.xml
414 changes: 414 additions & 0 deletions src/Tests/Public/Add-CCMGroup.Tests.ps1

Large diffs are not rendered by default.

420 changes: 420 additions & 0 deletions src/Tests/Public/Add-CCMGroupMember.Tests.ps1

Large diffs are not rendered by default.

93 changes: 93 additions & 0 deletions src/Tests/Public/Connect-CCMServer.Tests.ps1
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
}
}
}
70 changes: 70 additions & 0 deletions src/Tests/Public/Disable-CCMDeployment.Tests.ps1
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
}
}
}
150 changes: 150 additions & 0 deletions src/Tests/Public/Export-CCMDeployment.Tests.ps1
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
}
}
}
Loading

0 comments on commit bc38a0c

Please sign in to comment.