Skip to content

Commit f32ed30

Browse files
Add tests for context credential workflow
Load function scripts in test runtime and cover credential storage, default context behavior, domain query construction, and basic auth request execution. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 6bc7a36 commit f32ed30

3 files changed

Lines changed: 86 additions & 3 deletions

File tree

src/functions/public/Get-PSModuleTest.ps1

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#Requires -Modules Utilities
2-
3-
function Get-PSModuleTest {
1+
function Get-PSModuleTest {
42
<#
53
.SYNOPSIS
64
Performs tests on a module.

tests/DomeneshopContext.Tests.ps1

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

tests/PSModuleTest.Tests.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
param()
1111

1212
Describe 'Module' {
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+
1320
It 'Function: Get-PSModuleTest' {
1421
Get-PSModuleTest -Name 'World' | Should -Be 'Hello, World!'
1522
}

0 commit comments

Comments
 (0)