|
| 1 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( |
| 2 | + 'PSReviewUnusedParameter', '', |
| 3 | + Justification = 'Required for Pester tests' |
| 4 | +)] |
| 5 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( |
| 6 | + 'PSUseDeclaredVarsMoreThanAssignments', '', |
| 7 | + Justification = 'Required for Pester tests' |
| 8 | +)] |
| 9 | +[CmdletBinding()] |
| 10 | +param() |
| 11 | + |
| 12 | +Describe 'Domeneshop context and auth flow' { |
| 13 | + BeforeAll { |
| 14 | + $functionFiles = Get-ChildItem -Path "$PSScriptRoot\..\src\functions" -Filter '*.ps1' -Recurse -File | Sort-Object -Property FullName |
| 15 | + foreach ($file in $functionFiles) { |
| 16 | + . $file.FullName |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + It 'Connect-DomeneshopAccount stores credentials in Context and can set default' { |
| 21 | + Mock Set-Context {} |
| 22 | + Mock Get-DomeneshopConfig { [pscustomobject]@{ DefaultContext = $null } } |
| 23 | + Mock Set-DomeneshopDefaultContext {} |
| 24 | + Mock Get-DomeneshopContext { [pscustomobject]@{ ID = 'demo' } } |
| 25 | + |
| 26 | + Connect-DomeneshopAccount -Token 'token' -Secret 'secret' -Context 'demo' -PassThru | Should -Not -BeNullOrEmpty |
| 27 | + |
| 28 | + Should -Invoke Set-Context -Times 1 -Exactly -ParameterFilter { |
| 29 | + $ID -eq 'demo' -and |
| 30 | + $Vault -eq 'Domeneshop' -and |
| 31 | + $Context.Secret -is [securestring] |
| 32 | + } |
| 33 | + Should -Invoke Set-DomeneshopDefaultContext -Times 1 -Exactly |
| 34 | + } |
| 35 | + |
| 36 | + It 'Get-DomeneshopContext throws when default context is not configured' { |
| 37 | + Mock Get-DomeneshopConfig { [pscustomobject]@{ DefaultContext = '' } } |
| 38 | + |
| 39 | + { Get-DomeneshopContext } | Should -Throw |
| 40 | + } |
| 41 | + |
| 42 | + It 'Get-DomeneshopDomain calls domains endpoint with domain filter' { |
| 43 | + Mock Resolve-DomeneshopContext { |
| 44 | + [pscustomobject]@{ |
| 45 | + ID = 'demo' |
| 46 | + Token = 'token' |
| 47 | + Secret = ConvertTo-SecureString -AsPlainText 'secret' -Force |
| 48 | + ApiBaseUri = 'https://api.domeneshop.no/v0' |
| 49 | + } |
| 50 | + } |
| 51 | + Mock Invoke-DomeneshopApiRequest { @() } |
| 52 | + |
| 53 | + { Get-DomeneshopDomain -Context 'demo' -Domain '.no' } | Should -Not -Throw |
| 54 | + |
| 55 | + Should -Invoke Invoke-DomeneshopApiRequest -Times 1 -Exactly -ParameterFilter { |
| 56 | + $Method -eq 'Get' -and |
| 57 | + $Uri -eq 'https://api.domeneshop.no/v0/domains?domain=.no' -and |
| 58 | + $Context.ID -eq 'demo' |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + It 'Invoke-DomeneshopApiRequest uses basic authentication' { |
| 63 | + Mock Invoke-RestMethod { [pscustomobject]@{ ok = $true } } |
| 64 | + $contextObject = [pscustomobject]@{ |
| 65 | + Token = 'token' |
| 66 | + Secret = ConvertTo-SecureString -AsPlainText 'secret' -Force |
| 67 | + } |
| 68 | + |
| 69 | + $result = Invoke-DomeneshopApiRequest -Method Get -Uri 'https://api.domeneshop.no/v0/domains' -Context $contextObject |
| 70 | + |
| 71 | + $result.ok | Should -BeTrue |
| 72 | + Should -Invoke Invoke-RestMethod -Times 1 -Exactly -ParameterFilter { |
| 73 | + $Method -eq 'Get' -and |
| 74 | + $Authentication -eq 'Basic' -and |
| 75 | + $Credential.UserName -eq 'token' |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments