Skip to content

Commit 6b9ddf4

Browse files
authored
Prevent users from setting ApiVersion to 'Unknown' in Set-PSResourceRepository and Register-PSResourceRepository (#1892)
1 parent 895cc6f commit 6b9ddf4

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

src/code/RegisterPSResourceRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class RegisterPSResourceRepository : PSCmdlet, IDynamicParameters
9191
/// Specifies the Api version of the repository to be set.
9292
/// </summary>
9393
[Parameter(ParameterSetName = NameParameterSet)]
94+
[ValidateSet("V2", "V3", "Local", "NugetServer", "ContainerRegistry")]
9495
public PSRepositoryInfo.APIVersion ApiVersion { get; set; }
9596

9697
/// <summary>

src/code/SetPSResourceRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public SwitchParameter Trusted
8888
/// Specifies the Api version of the repository to be set.
8989
/// </summary>
9090
[Parameter(ParameterSetName = NameParameterSet)]
91+
[ValidateSet("V2", "V3", "Local", "NugetServer", "ContainerRegistry")]
9192
public PSRepositoryInfo.APIVersion ApiVersion { get; set; }
9293

9394
/// <summary>

test/ResourceRepositoryTests/RegisterPSResourceRepository.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,14 @@ Describe "Test Register-PSResourceRepository" -tags 'CI' {
404404
$res.ApiVersion | Should -Be 'v2'
405405
}
406406

407+
It "should throw error when trying to register repository with ApiVersion unknown" {
408+
{Register-PSResourceRepository -Name $TestRepoName1 -Uri $tmpDir1Path -ApiVersion "unknown" -ErrorAction Stop} | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository"
409+
410+
# Verify the repository was not created
411+
$repo = Get-PSResourceRepository $TestRepoName1 -ErrorAction SilentlyContinue
412+
$repo | Should -BeNullOrEmpty
413+
}
414+
407415
It "should register container registry repository with correct ApiVersion" {
408416
$ContainerRegistryName = "ACRRepo"
409417
$ContainerRegistryUri = "https://psresourcegettest.azurecr.io/"

test/ResourceRepositoryTests/SetPSResourceRepository.Tests.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,16 @@ Describe "Test Set-PSResourceRepository" -tags 'CI' {
345345
$repo.Priority | Should -Be 25
346346
}
347347

348-
It "should not change ApiVersion of repository if -ApiVersion parameter was not used" {
348+
It "should throw error when trying to set ApiVersion to unknown" {
349349
Register-PSResourceRepository -Name $TestRepoName1 -Uri $tmpDir1Path
350350
$repo = Get-PSResourceRepository $TestRepoName1
351351
$repoApiVersion = $repo.ApiVersion
352352
$repoApiVersion | Should -Be "local"
353353

354-
Set-PSResourceRepository -Name $TestRepoName1 -ApiVersion "unknown" -ErrorVariable err -ErrorAction SilentlyContinue
354+
{Set-PSResourceRepository -Name $TestRepoName1 -ApiVersion "unknown" -ErrorAction Stop} | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.PSResourceGet.Cmdlets.SetPSResourceRepository"
355+
356+
# Verify the repository ApiVersion was not changed
355357
$repo = Get-PSResourceRepository $TestRepoName1
356-
$repo.ApiVersion | Should -Be "unknown"
357-
$err.Count | Should -Be 0
358+
$repo.ApiVersion | Should -Be "local"
358359
}
359360
}

0 commit comments

Comments
 (0)