Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions azuredevops-pihole-pester-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pool:
name: mmadeja-dt

variables:
testResultsFile: '$(System.DefaultWorkingDirectory)/TestResults/pester.xml'
- group: dns3
- name: testResultsFile
value: '$(System.DefaultWorkingDirectory)/TestResults/pester.xml'

steps:
- pwsh: |
Expand All @@ -30,15 +32,16 @@ steps:

- pwsh: |
$configContent = @"
`$PiHoleServer = [uri]'$env:PIHOLE_TEST_SERVER'
`$PiHoleToken = '$env:PIHOLE_TEST_TOKEN'
`$PiHoleIgnoreSsl = `$true
`$PiHoleServer = [uri]'$env:PIHOLE_SERVER'
`$PiHoleToken = '$env:PIHOLE_TOKEN'
`$PiHoleIgnoreSsl = $env:PIHOLE_IGNORE_SSL
"@
Set-Content -Path (Join-Path '$(System.DefaultWorkingDirectory)' 'tests/IntegrationConfig.local.ps1') -Value $configContent -Encoding utf8
displayName: 'Write integration test config'
env:
PIHOLE_TEST_SERVER: $(PiHoleTestServer)
PIHOLE_TEST_TOKEN: $(PiHoleTestToken)
PIHOLE_SERVER: $(PiHoleServer)
PIHOLE_TOKEN: $(PiHoleToken)
PIHOLE_IGNORE_SSL: $(PiHoleIgnoreSsl)

- pwsh: |
Import-Module Pester -MinimumVersion 5.0.0 -Force
Expand Down
4 changes: 4 additions & 0 deletions tests/Restart-PiHoleDnsService.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Describe 'Restart-PiHoleDnsService (Integration)' -Tag 'Integration' {
}

It 'returns the raw API response when RawOutput is set' -Skip:(-not $script:ConfigAvailable) {
# The previous test just restarted pihole-FTL; give it a moment to come back up before
# restarting it again, or this occasionally hits a transient connection failure.
Start-Sleep -Seconds 5

{ Restart-PiHoleDnsService -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -RawOutput $true } |
Should -Not -Throw
}
Expand Down
65 changes: 27 additions & 38 deletions tests/Set-PiHoleDnsBlocking.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,68 +1,57 @@

# Requires -Module Pester
Describe 'Set-PiHoleDnsBlocking' {
BeforeAll {
Import-Module .\PiHoleShell\PiHoleShell.psm1

Mock -CommandName Request-PiHoleAuth -MockWith { return 'mock-sid' }
Mock -CommandName Invoke-RestMethod -MockWith {
Import-Module .\PiHoleShell\PiHoleShell.psm1 -Force

# Request-PiHoleAuth and Format-PiHoleSecond are internal helpers that aren't exported, and
# every one of these is called from code running inside the module, so all need -ModuleName
# for the mock to actually intercept those internal calls.
Mock -CommandName Request-PiHoleAuth -ModuleName PiHoleShell -MockWith { return 'mock-sid' }
Mock -CommandName Remove-PiHoleCurrentAuthSession -ModuleName PiHoleShell
Mock -CommandName Format-PiHoleSecond -ModuleName PiHoleShell -MockWith {
return @{ TimeInSeconds = 60 }
}
Mock -CommandName Invoke-RestMethod -ModuleName PiHoleShell -MockWith {
return @{
blocking = 'false'
timer = 60
}
}
Mock -CommandName Remove-PiHoleCurrentAuthSession
Mock -CommandName Format-PiHoleSecond -MockWith {
return @{ TimeInSeconds = 60 }
}

# Sample input values
$server = [uri]'http://pihole.local'
$password = 'mock-password'
$sid = 'mock-session-id'

# Mock external functions
Mock -CommandName Request-PiHoleAuth -MockWith { 'mock-sid' }
Mock -CommandName Remove-PiHoleCurrentAuthSession
Mock -CommandName Format-PiHoleSecond -MockWith {
return @{ TimeInSeconds = 60 }
}

# Mock response from API
Mock -CommandName Invoke-RestMethod -MockWith {
return @{
blocking = 'false'
timer = 60
}
}
}

It 'should call Request-PiHoleAuth and send correct POST body' {
Set-PiHoleDnsBlocking -PiHoleServer $server -Password $password -Blocking 'False' -TimeInSeconds 60 | Out-Null

# Assert internal functions were called correctly
Assert-MockCalled Request-PiHoleAuth -Times 1 -Exactly -Scope It
Assert-MockCalled Invoke-RestMethod -Times 1 -Scope It
Assert-MockCalled Remove-PiHoleCurrentAuthSession -Times 1 -Scope It
Should -Invoke Request-PiHoleAuth -ModuleName PiHoleShell -Times 1 -Exactly -Scope It
Should -Invoke Invoke-RestMethod -ModuleName PiHoleShell -Times 1 -Exactly -Scope It
Should -Invoke Remove-PiHoleCurrentAuthSession -ModuleName PiHoleShell -Times 1 -Exactly -Scope It
}

It 'should return a formatted PSCustomObject if RawOutput is $false' {
$result = Set-PiHoleDnsBlocking -PiHoleServer $server -Password $password -Blocking 'False' -TimeInSeconds 60
$result | Should -BeOfType 'System.Object[]'
$result[0].Blocking | Should -Be 'false'
$result[0].TimeInSeconds | Should -Be 60

$result.Blocking | Should -Be 'false'
$result.TimeInSeconds | Should -Be 60
}

It 'should return raw response if RawOutput is $true' {
$result = Set-PiHoleDnsBlocking -PiHoleServer $server -Password $password -Blocking 'False' -TimeInSeconds 60 -RawOutput
$result | Should -HaveProperty 'blocking'
$result | Should -HaveProperty 'timer'
$result = Set-PiHoleDnsBlocking -PiHoleServer $server -Password $password -Blocking 'False' -TimeInSeconds 60 -RawOutput $true

$result.blocking | Should -Be 'false'
$result.timer | Should -Be 60
}

It 'should handle errors and output them' {
# Mock to simulate error
Mock -CommandName Invoke-RestMethod -MockWith { throw "Test error" } -ParameterFilter { $Body -like '*' }
Mock -CommandName Invoke-RestMethod -ModuleName PiHoleShell -MockWith { throw "Test error" }

Set-PiHoleDnsBlocking -PiHoleServer $server -Password $password -Blocking 'False' -TimeInSeconds 60 -ErrorVariable errOut -ErrorAction SilentlyContinue

{ Set-PiHoleDnsBlocking -PiHoleServer $server -Password $password -Blocking 'False' } |
Should -Throw -ErrorMessage 'Test error'
$errOut | Should -Not -BeNullOrEmpty
$errOut[0].Exception.Message | Should -Be 'Test error'
}
}
Loading