Add unit tests for Set-GiteaConfiguration, Get-GiteaConfiguration, and Get-GiteaFileContent functions
This commit is contained in:
119
PS-GiteaUtilities/Tests/Get-GiteaFileContent.Tests.ps1
Normal file
119
PS-GiteaUtilities/Tests/Get-GiteaFileContent.Tests.ps1
Normal file
@@ -0,0 +1,119 @@
|
||||
# Get-GiteaFileContent.Tests.ps1
|
||||
|
||||
# Import the module under test
|
||||
$modulePath = Join-Path -Path $PSScriptRoot -ChildPath '..\PS-GiteaUtilities.psm1'
|
||||
Import-Module -Name $modulePath -Force
|
||||
|
||||
Describe 'Get-GiteaFileContent' {
|
||||
|
||||
Context 'When all parameters are provided' {
|
||||
BeforeEach {
|
||||
Mock -CommandName Invoke-RestMethod -ModuleName PS-GiteaUtilities -MockWith {
|
||||
return @{
|
||||
content = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('Hello World'))
|
||||
size = 11
|
||||
sha = 'fake-sha'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
It 'Should return file content without decoding by default' {
|
||||
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'README.md' -token 'abc123'
|
||||
|
||||
$result | Should -Not -BeNullOrEmpty
|
||||
$result.Success | Should -Be $true
|
||||
$result.Content | Should -Be ([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('Hello World')))
|
||||
}
|
||||
|
||||
It 'Should decode content when -decode is used' {
|
||||
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'README.md' -token 'abc123' -decode
|
||||
|
||||
$result | Should -Not -BeNullOrEmpty
|
||||
$result.Success | Should -Be $true
|
||||
$result.Content | Should -Be 'Hello World'
|
||||
}
|
||||
}
|
||||
|
||||
Context 'When missing parameters and config is loaded' {
|
||||
BeforeEach {
|
||||
Mock -CommandName Get-GiteaConfiguration -ModuleName PS-GiteaUtilities -MockWith {
|
||||
return @{
|
||||
giteaURL = 'https://mock.gitea.com'
|
||||
defaultOwner = 'mockuser'
|
||||
defaultRepo = 'mockrepo'
|
||||
defaultBranch = 'main'
|
||||
token = 'mocktoken'
|
||||
}
|
||||
}
|
||||
|
||||
Mock -CommandName Invoke-RestMethod -ModuleName PS-GiteaUtilities -MockWith {
|
||||
return @{
|
||||
content = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('Mock Config Content'))
|
||||
size = 20
|
||||
sha = 'mock-sha'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
It 'Should load configuration from Get-GiteaConfiguration if not all parameters are given' {
|
||||
$result = Get-GiteaFileContent -filePath 'test.ps1'
|
||||
|
||||
$result | Should -Not -BeNullOrEmpty
|
||||
$result.Path | Should -Be 'test.ps1'
|
||||
$result.Content | Should -Be ([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('Mock Config Content')))
|
||||
}
|
||||
}
|
||||
|
||||
Context 'When API call fails' {
|
||||
BeforeEach {
|
||||
Mock -CommandName Invoke-RestMethod -ModuleName PS-GiteaUtilities -MockWith { throw "API call failed" }
|
||||
Mock -CommandName Write-Error -ModuleName PS-GiteaUtilities -MockWith { }
|
||||
}
|
||||
|
||||
It 'Should capture error and mark result as unsuccessful' {
|
||||
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'badfile.ps1' -token 'abc123'
|
||||
|
||||
$result.Success | Should -Be $false
|
||||
$result.Error | Should -Match 'API call failed'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Context 'Parameter validation' {
|
||||
It 'Should throw if required parameters are missing and no config' {
|
||||
Mock -CommandName Get-GiteaConfiguration -ModuleName PS-GiteaUtilities -MockWith { return $null }
|
||||
|
||||
{ Get-GiteaFileContent -filePath 'something.ps1' } | Should -Throw
|
||||
}
|
||||
}
|
||||
|
||||
Context 'When decoding file content' {
|
||||
BeforeEach {
|
||||
# Simulate API returning Base64 content
|
||||
Mock -CommandName Invoke-RestMethod -ModuleName PS-GiteaUtilities -MockWith {
|
||||
return @{
|
||||
content = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('Hello from API!'))
|
||||
size = 15
|
||||
sha = 'fake-sha-for-decode'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
It 'Should return base64 encoded content by default (no decode switch)' {
|
||||
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'testfile.txt' -token 'abc123'
|
||||
|
||||
$result | Should -Not -BeNullOrEmpty
|
||||
$result.Success | Should -Be $true
|
||||
$result.Content | Should -Be ([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('Hello from API!')))
|
||||
}
|
||||
|
||||
It 'Should decode base64 content into plain text when -decode is used' {
|
||||
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'testfile.txt' -token 'abc123' -decode
|
||||
|
||||
$result | Should -Not -BeNullOrEmpty
|
||||
$result.Success | Should -Be $true
|
||||
$result.Content | Should -Be 'Hello from API!'
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user