|
| 1 | +#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '6.0.0'; MaximumVersion = '6.*' } |
| 2 | + |
| 3 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( |
| 4 | + 'PSReviewUnusedParameter', '', |
| 5 | + Justification = 'Required for Pester mock parameter filters.' |
| 6 | +)] |
| 7 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( |
| 8 | + 'PSUseDeclaredVarsMoreThanAssignments', '', |
| 9 | + Justification = 'Required for Pester test setup.' |
| 10 | +)] |
| 11 | +[CmdletBinding()] |
| 12 | +param() |
| 13 | + |
| 14 | +Describe 'Add-DomeneshopDnsRecord' { |
| 15 | + BeforeAll { |
| 16 | + . (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'Domeneshop.TestSetup.ps1') |
| 17 | + } |
| 18 | + |
| 19 | + BeforeEach { |
| 20 | + Mock Get-DomeneshopContext { $script:DomeneshopTestContext } |
| 21 | + Mock Invoke-DomeneshopApiRequest {} |
| 22 | + $script:Record = @{ host = 'www'; type = 'A'; data = '192.0.2.10' } |
| 23 | + } |
| 24 | + |
| 25 | + It 'posts the DNS record to the domain endpoint' { |
| 26 | + Add-DomeneshopDnsRecord -Context 'demo' -DomainID 42 -Record $script:Record |
| 27 | + |
| 28 | + Should -Invoke Invoke-DomeneshopApiRequest -Times 1 -Exactly -ParameterFilter { |
| 29 | + $Method -eq 'Post' -and |
| 30 | + $Uri -eq 'https://api.domeneshop.no/v0/domains/42/dns' -and |
| 31 | + $Body -eq $script:Record |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + It 'does not send a request when WhatIf is specified' { |
| 36 | + Add-DomeneshopDnsRecord -Context 'demo' -DomainID 42 -Record $script:Record -WhatIf |
| 37 | + |
| 38 | + Should -Invoke Invoke-DomeneshopApiRequest -Times 0 -Exactly |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +Describe 'Get-DomeneshopDnsRecord' { |
| 43 | + BeforeAll { |
| 44 | + . (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'Domeneshop.TestSetup.ps1') |
| 45 | + } |
| 46 | + |
| 47 | + BeforeEach { |
| 48 | + Mock Get-DomeneshopContext { $script:DomeneshopTestContext } |
| 49 | + Mock Invoke-DomeneshopApiRequest {} |
| 50 | + } |
| 51 | + |
| 52 | + It 'builds an escaped list query' { |
| 53 | + Get-DomeneshopDnsRecord -Context 'demo' -DomainID 42 -RecordHost 'home office' -Type 'A' -Data '192.0.2.10' |
| 54 | + |
| 55 | + Should -Invoke Invoke-DomeneshopApiRequest -Times 1 -Exactly -ParameterFilter { |
| 56 | + $Method -eq 'Get' -and |
| 57 | + $Uri -eq 'https://api.domeneshop.no/v0/domains/42/dns?host=home%20office&type=A&data=192.0.2.10' |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + It 'gets a DNS record by ID' { |
| 62 | + Get-DomeneshopDnsRecord -Context 'demo' -DomainID 42 -RecordID 7 |
| 63 | + |
| 64 | + Should -Invoke Invoke-DomeneshopApiRequest -Times 1 -Exactly -ParameterFilter { |
| 65 | + $Method -eq 'Get' -and |
| 66 | + $Uri -eq 'https://api.domeneshop.no/v0/domains/42/dns/7' |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + It 'rejects whitespace-only list filters' { |
| 71 | + { Get-DomeneshopDnsRecord -Context 'demo' -DomainID 42 -RecordHost ' ' } | Should -Throw |
| 72 | + { Get-DomeneshopDnsRecord -Context 'demo' -DomainID 42 -Type ' ' } | Should -Throw |
| 73 | + { Get-DomeneshopDnsRecord -Context 'demo' -DomainID 42 -Data ' ' } | Should -Throw |
| 74 | + |
| 75 | + Should -Invoke Invoke-DomeneshopApiRequest -Times 0 -Exactly |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +Describe 'Remove-DomeneshopDnsRecord' { |
| 80 | + BeforeAll { |
| 81 | + . (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'Domeneshop.TestSetup.ps1') |
| 82 | + } |
| 83 | + |
| 84 | + BeforeEach { |
| 85 | + Mock Get-DomeneshopContext { $script:DomeneshopTestContext } |
| 86 | + Mock Invoke-DomeneshopApiRequest {} |
| 87 | + } |
| 88 | + |
| 89 | + It 'deletes the DNS record endpoint' { |
| 90 | + Remove-DomeneshopDnsRecord -Context 'demo' -DomainID 42 -RecordID 7 -Confirm:$false |
| 91 | + |
| 92 | + Should -Invoke Invoke-DomeneshopApiRequest -Times 1 -Exactly -ParameterFilter { |
| 93 | + $Method -eq 'Delete' -and |
| 94 | + $Uri -eq 'https://api.domeneshop.no/v0/domains/42/dns/7' |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + It 'does not send a request when WhatIf is specified' { |
| 99 | + Remove-DomeneshopDnsRecord -Context 'demo' -DomainID 42 -RecordID 7 -WhatIf |
| 100 | + |
| 101 | + Should -Invoke Invoke-DomeneshopApiRequest -Times 0 -Exactly |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +Describe 'Set-DomeneshopDnsRecord' { |
| 106 | + BeforeAll { |
| 107 | + . (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'Domeneshop.TestSetup.ps1') |
| 108 | + } |
| 109 | + |
| 110 | + BeforeEach { |
| 111 | + Mock Get-DomeneshopContext { $script:DomeneshopTestContext } |
| 112 | + Mock Invoke-DomeneshopApiRequest {} |
| 113 | + $script:Record = @{ host = 'www'; type = 'A'; data = '192.0.2.20' } |
| 114 | + } |
| 115 | + |
| 116 | + It 'puts the replacement record to the record endpoint' { |
| 117 | + Set-DomeneshopDnsRecord -Context 'demo' -DomainID 42 -RecordID 7 -Record $script:Record |
| 118 | + |
| 119 | + Should -Invoke Invoke-DomeneshopApiRequest -Times 1 -Exactly -ParameterFilter { |
| 120 | + $Method -eq 'Put' -and |
| 121 | + $Uri -eq 'https://api.domeneshop.no/v0/domains/42/dns/7' -and |
| 122 | + $Body -eq $script:Record |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + It 'does not send a request when WhatIf is specified' { |
| 127 | + Set-DomeneshopDnsRecord -Context 'demo' -DomainID 42 -RecordID 7 -Record $script:Record -WhatIf |
| 128 | + |
| 129 | + Should -Invoke Invoke-DomeneshopApiRequest -Times 0 -Exactly |
| 130 | + } |
| 131 | +} |
0 commit comments