From 34fcb73d32b39ec17df7d0d3f66d103af60585e9 Mon Sep 17 00:00:00 2001 From: Raymond LaRose Date: Wed, 30 Apr 2025 14:59:51 -0400 Subject: [PATCH] Add unit tests for Get-GiteaReleases function to validate behavior and error handling --- .../Tests/Get-GiteaReleases.Tests.ps1 | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 PS-GiteaUtilities/Tests/Get-GiteaReleases.Tests.ps1 diff --git a/PS-GiteaUtilities/Tests/Get-GiteaReleases.Tests.ps1 b/PS-GiteaUtilities/Tests/Get-GiteaReleases.Tests.ps1 new file mode 100644 index 0000000..b072bdd --- /dev/null +++ b/PS-GiteaUtilities/Tests/Get-GiteaReleases.Tests.ps1 @@ -0,0 +1,117 @@ +# Get-GiteaReleases.Tests.ps1 + +# Import the module +$modulePath = Join-Path -Path $PSScriptRoot -ChildPath '..\PS-GiteaUtilities.psm1' +Import-Module -Name $modulePath -Force + +Describe 'Get-GiteaReleases' { + + BeforeEach { + # Mock configuration load + Mock -CommandName Get-GiteaConfiguration -ModuleName PS-GiteaUtilities -MockWith { + return @{ + giteaURL = 'https://mock.gitea.com' + defaultOwner = 'mockuser' + token = 'mocktoken' + } + } + + # Mock API response + Mock -CommandName Invoke-RestMethod -ModuleName PS-GiteaUtilities -MockWith { + return @( + @{ + id = 1 + tag_name = "v1.0" + target_commitish = "main" + name = "Release 1" + body = "Initial release" + url = "https://mock.gitea.com/api/v1/releases/1" + html_url = "https://mock.gitea.com/releases/v1.0" + tarball_url = "https://mock.gitea.com/releases/v1.0.tar.gz" + zipball_url = "https://mock.gitea.com/releases/v1.0.zip" + upload_url = "https://mock.gitea.com/releases/uploads" + draft = $false + prerelease = $false + created_at = "2024-01-01T00:00:00Z" + published_at = "2024-01-01T01:00:00Z" + author = @{ + id = 101 + username = "devuser" + email = "dev@norwichct.org" + avatar_url = "https://mock.gitea.com/avatar.jpg" + html_url = "https://mock.gitea.com/devuser" + } + assets = @( + @{ + id = 201 + name = "tool.exe" + size = 123456 + download_count = 42 + created_at = "2024-01-01T00:30:00Z" + uuid = "uuid-1234" + browser_download_url = "https://mock.gitea.com/releases/assets/tool.exe" + } + ) + } + ) + } + + Mock -CommandName Write-Error -ModuleName PS-GiteaUtilities -MockWith { } + } + + Context 'Basic usage' { + It 'Should return a list of release objects' { + $result = Get-GiteaReleases -repoOwner 'mockuser' -repoName 'mockrepo' -token 'mocktoken' + + $result | Should -Not -BeNullOrEmpty + $result[0].Name | Should -Be 'Release 1' + $result[0].Author.Username | Should -Be 'devuser' + $result[0].Assets.Count | Should -Be 1 + $result[0].Assets[0].Name | Should -Be 'tool.exe' + } + } + + Context 'Optional switches affect URL' { + It 'Should include draft=true and pre-release=true in URL if switches are set' { + $calledUrl = $null + + Mock -CommandName Invoke-RestMethod -ModuleName PS-GiteaUtilities -MockWith { + param($Uri) + $script:calledUrl = $Uri + return @() + } + + Get-GiteaReleases -repoOwner 'mockuser' -repoName 'mockrepo' -includeDrafts -includePreReleases -page 2 -limit 10 -token 'mocktoken' + + $script:calledUrl | Should -Match 'draft=true' + $script:calledUrl | Should -Match 'pre-release=true' + $script:calledUrl | Should -Match 'page=2' + $script:calledUrl | Should -Match 'limit=10' + } + } + + Context 'When required params are missing and no config' { + BeforeEach { + Mock -CommandName Get-GiteaConfiguration -ModuleName PS-GiteaUtilities -MockWith { return $null } + } + + It 'Should throw when required parameters are not supplied' { + { Get-GiteaReleases -repoName 'mockrepo' } | Should -Throw + } + } + + 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 catch and handle the error' { + $null = Get-GiteaReleases -repoOwner 'mockuser' -repoName 'mockrepo' -token 'mocktoken' + # Expect no throw, Write-Error should catch it + } + } +}