test/Pester.PSRepositoryTests.ps1

# This is a Pester test suite to validate Register-PSResourceRepository, Unregister-PSResourceRepository, Get-PSResourceRepository, and Set-PSResourceRepository.
#
# Copyright (c) Microsoft Corporation, 2019

# Import-Module "$PSScriptRoot\PSGetTestUtils.psm1" -WarningAction SilentlyContinue
import-module "C:\code\PowerShellGet\v3\PowerShellGet\bin\Debug\netcoreapp2.2\PowerShellGet.dll"

$PSGalleryName = 'PSGallery'
$PSGalleryLocation = 'https://www.powershellgallery.com/api/v2'

$TestRepoName = 'TestRepoName'
$TestRepoURL = 'https://www.poshtestgallery.com/api/v2'

$TestRepoName2 = "NuGet"
$TestRepoURL2 = 'https://api.nuget.org/v3/index.json'

$TestRepoLocalName = 'TestLocalRepo'
$tmpdir = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath $TestRepoLocalName




if (-not (Test-Path -LiteralPath $tmpdir)) {
    New-Item -Path $tmpdir -ItemType Directory > $null
}
Write-Host $tmpdir
$TestRepoLocalURL = $tmpdir



$TestRepoLocalName2 = "TestLocalRepoName2"
$tmpdir2 = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath $TestRepoLocalName2
if (-not (Test-Path -LiteralPath $tmpdir2)) {
    New-Item -Path $tmpdir2 -ItemType Directory > $null
}

Write-Host $tmpdir2
$TestRepoLocalURL2 = $tmpdir2

# remember to delete these files
# Remove-Item -LiteralPath $tmpdir -Force -Recurse
#}

$ErrorActionPreference = "SilentlyContinue"




#####################################
### Register-PSResourceRepository ###
#####################################

Describe 'Test Register-PSResourceRepository' -tags 'BVT' {

    BeforeAll {

    }
    AfterAll {
        #Unregister-PSResourceRepository -Name $PSGalleryName -ErrorAction SilentlyContinue
        #Unregister-PSResourceRepository -Name $TestRepoName -ErrorAction SilentlyContinue
        #Unregister-PSResourceRepository -Name $TestRepoName2 -ErrorAction SilentlyContinue
        #Unregister-PSResourceRepository -Name $TestRepoLocalName -ErrorAction SilentlyContinue
        #Unregister-PSResourceRepository -Name $TestRepoLocalName2 -ErrorAction SilentlyContinue
    }

    BeforeEach {
       # Unregister-PSResourceRepository -Name $PSGalleryName -ErrorAction SilentlyContinue
       # Unregister-PSResourceRepository -Name $TestRepoName -ErrorAction SilentlyContinue
       # Unregister-PSResourceRepository -Name $TestRepoName2 -ErrorAction SilentlyContinue
       # Unregister-PSResourceRepository -Name $TestRepoLocalName -ErrorAction SilentlyContinue
       # Unregister-PSResourceRepository -Name $TestRepoLocalName2 -ErrorAction SilentlyContinue
    }

    AfterEach {
    }
    ### Registering the PowerShell Gallery
    It 'Should register the default PSGallery' {
        Register-PSResourceRepository -PSGallery

        $repo = Get-PSResourceRepository $PSGalleryName
        $repo | Should -Not -BeNullOrEmpty
        $repo.URL | Should be $PSGalleryLocation
        $repo.Trusted | Should be false
        $repo.Priority | Should be 50
    }
    
    It 'Should register PSGallery with installation policy trusted' {
        Unregister-PSResourceRepository $PSGalleryName
        Register-PSResourceRepository -PSGallery -Trusted

        $repo = Get-PSResourceRepository $PSGalleryName
        $repo.Name | Should be $PSGalleryName
        $repo.Trusted | Should be true
    }
    
    <#################################################################
    It 'Should fail to reregister PSGallery' {
        Unregister-PSResourceRepository $PSGalleryName
        Register-PSResourceRepository -PSGallery
        (Register-PSResourceRepository -PSGallery -ErrorVariable ev -ErrorAction SilentlyContinue) | should throw

       # $ev[0].FullyQualifiedErrorId | Should be "The PSResource Repository 'PSGallery' already exists."
    }
    #>



    <#################################################################
    It 'Should fail to register PSGallery when manually providing URL' {
        Unregister-PSResourceRepository $PSGalleryName
        {Register-PSResourceRepository $PSGalleryName -URL $PSGalleryLocation -ErrorVariable ev -ErrorAction SilentlyContinue} | should throw

        $ev[0].FullyQualifiedErrorId | Should be "Use 'Register-PSResourceRepository -Default' to register the PSGallery repository."
    }
    #>


    ### Registering an online URL
    It 'Should register the test repository with online -URL' {
        Register-PSResourceRepository $TestRepoName -URL $TestRepoURL

        $repo = Get-PSResourceRepository $TestRepoName
        $repo.Name | should be $TestRepoName
        $repo.URL | should be $TestRepoURL
        $repo.Trusted | should be false
    }

    It 'Should register the test repository when -URL is a website and installation policy is trusted' {
        Unregister-PSResourceRepository $TestRepoName
        Register-PSResourceRepository $TestRepoName -URL $TestRepoURL -Trusted

        $repo = Get-PSResourceRepository $TestRepoName
        $repo.Name | should be $TestRepoName
        $repo.URL | should be $TestRepoURL
        $repo.Trusted | should be true
    }

    It 'Should register the test repository when -URL is a website and priority is set' {
        Unregister-PSResourceRepository $TestRepoName
        Register-PSResourceRepository $TestRepoName -URL $TestRepoURL -Priority 2

        $repo = Get-PSResourceRepository $TestRepoName
        $repo.Name | should be $TestRepoName
        $repo.URL | should be $TestRepoURL
        $repo.Trusted | should be $true
        $repo.Priority | should be 2
    }

    <#################################################################
    It 'Should fail to reregister the repository when the -Name is already registered' {
        Register-PSResourceRepository $TestRepoName -URL $TestRepoURL
        Register-PSResourceRepository $TestRepoName -URL $TestRepoURL2 -ErrorVariable ev -ErrorAction SilentlyContinue

        $ev[0].FullyQualifiedErrorId | Should be "The PSResource Repository '$($TestRepoName)' exists."
    }
    #>


    <#################################################################
    It 'Should fail to reregister the repository when the -URL is already registered' {
        Register-PSResourceRepository $TestRepoName -URL $TestRepoURL
        Register-PSResourceRepository $TestRepoName2 -URL $TestRepoURL -ErrorVariable ev -ErrorAction SilentlyContinue

        $ev[0].FullyQualifiedErrorId | Should be "The repository could not be registered because there exists a registered repository with Name '$($TestRepoName)' and URL '$($TestRepoURL)'. To register another repository with Name '$($TestRepoName2)', please unregister the existing repository using the Unregister-PSResourceRepository cmdlet."
    }
    #>


    ### Registering a fileshare URL
    It 'Should register the test repository when -URL is a fileshare' {
        Write-Host $TestRepoLocalURL
        Register-PSResourceRepository $TestRepoLocalName -URL $TestRepoLocalURL

        $repo = Get-PSResourceRepository $TestRepoLocalName
        $repo.Name | should be $TestRepoLocalName

        $repoModifiedURL = $repo.URL.replace("/","\")
        $repoModifiedURL | should be ("file:\\\" + $TestRepoLocalURL)
        $repo.Trusted | should be false
    }

    It 'Should register the test repository when -URL is a fileshare and installation policy is trusted and priority is set' {
        Write-Host $TestRepoLocalURL
        Unregister-PSResourceRepository $TestRepoLocalName
        Register-PSResourceRepository $TestRepoLocalName -URL $TestRepoLocalURL -Trusted -Priority 2

        $repo = Get-PSResourceRepository $TestRepoLocalName
        $repo.Name | should be $TestRepoLocalName

        $repoModifiedURL = $repo.URL.replace("/","\")
        $repoModifiedURL | should be ("file:\\\" + $TestRepoLocalURL)
        $repo.Trusted | should be true
        $repo.Priority | should be 2
    }

    <#################################################################
    It 'Should fail to reregister the repository when the -Name is already registered' {
        Register-PSResourceRepository $TestRepoLocalName -URL $TestRepoLocalURL
        Register-PSResourceRepository $TestRepoLocalName -URL $TestRepoLocalURL2 -ErrorVariable ev -ErrorAction SilentlyContinue

        $ev[0].FullyQualifiedErrorId | Should be "The PSResource Repository '$($TestRepoName)' exists."
    }
    #>


    <#################################################################
    It 'Should fail to reregister the repository when the fileshare -URL is already registered' {
        Register-PSResourceRepository $TestRepoLocalName -URL $TestRepoLocalURL
        Register-PSResourceRepository 'NewTestName' -URL $TestRepoLocalURL2 -ErrorVariable ev -ErrorAction SilentlyContinue

        $ev[0].FullyQualifiedErrorId | Should be "The repository could not be registered because there exists a registered repository with Name '$($TestRepoName)' and URL '$($TestRepoURL)'. To register another repository with Name '$($TestRepoName2)', please unregister the existing repository using the Unregister-PSResourceRepository cmdlet."
    }
    #>


    It 'Register PSResourceRepository File system location with special chars' {
        $tmpdir = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath 'ps repo testing [$!@^&test(;)]'
        if (-not (Test-Path -LiteralPath $tmpdir)) {
            New-Item -Path $tmpdir -ItemType Directory > $null
        }
        try {
            Register-PSResourceRepository -Name 'Test Repository' -URL $tmpdir
            try {
                Write-Host $tmpdir

                $repo = Get-PSResourceRepository -Name 'Test Repository'
                $repo.Name | should be 'Test Repository'
        
                $repoModifiedURL = $repo.URL.replace("/","\")
                $repoModifiedURL | should be ("file:\\\" + $tmpdir)
                #$repo.URL | should be $tmpdir
            }
            finally {
                Unregister-PSResourceRepository -Name 'Test Repository' -ErrorAction SilentlyContinue
            }
        }
        finally {
            Remove-Item -LiteralPath $tmpdir -Force -Recurse
        }
    }
}



Describe 'Registering Repositories with Hashtable Parameters' -tags 'BVT', 'InnerLoop' {

    AfterAll {
      # Unregister-PSResourceRepository -Name $PSGalleryName -ErrorAction SilentlyContinue
      # Unregister-PSResourceRepository -Name $TestRepoName -ErrorAction SilentlyContinue
      # Unregister-PSResourceRepository -Name $TestRepoName2 -ErrorAction SilentlyContinue
      # Unregister-PSResourceRepository -Name $TestRepoLocalName -ErrorAction SilentlyContinue
      # Unregister-PSResourceRepository -Name $TestRepoLocalName2 -ErrorAction SilentlyContinue
    }

    BeforeEach {
      # Unregister-PSResourceRepository -Name $PSGalleryName -ErrorAction SilentlyContinue
      # Unregister-PSResourceRepository -Name $TestRepoName -ErrorAction SilentlyContinue
      # Unregister-PSResourceRepository -Name $TestRepoName2 -ErrorAction SilentlyContinue
      # Unregister-PSResourceRepository -Name $TestRepoLocalName -ErrorAction SilentlyContinue
      # Unregister-PSResourceRepository -Name $TestRepoLocalName2 -ErrorAction SilentlyContinue
    }

    It 'Should register a repository with parameters as a hashtable' {
        Unregister-PSResourceRepository -Name $TestRepoName -ErrorAction SilentlyContinue
        
        $paramRegisterPSResourceRepository = @{
            Name     = $TestRepoName
            URL      = $TestRepoURL
            Trusted  = $False
            Priority = 1
        }

        { Register-PSResourceRepository @paramRegisterPSResourceRepository } | Should not Throw

        $repo = Get-PSResourceRepository -Name $TestRepoName
        $repo.URL | Should be $TestRepoURL
        $repo.Trusted | Should be $True
        $repo.Priority | Should be 1
    }

    It 'Should register multiple repositories' {
        Unregister-PSResourceRepository -Name $TestRepoName -ErrorAction SilentlyContinue
        Unregister-PSResourceRepository -Name $TestRepoLocalName -ErrorAction SilentlyContinue
        Unregister-PSResourceRepository -Name $PSGalleryName

        Register-PSResourceRepository -Repositories @(
            @{ Name = $TestRepoName; URL = $TestRepoURL; Priority = 15 }
            @{ Name = $TestRepoLocalName; URL = $TestRepoLocalURL }
            @{ PSGallery = $true; Trusted = $true }
        )

        $repos = Get-PSResourceRepository
        $repos.Count | Should be 3
        $repo1 = Get-PSResourceRepository $TestRepoName
        $repo1.URL | Should be $TestRepoURL
        $repo1.Priority | Should be 15


        $repo2 = Get-PSResourceRepository $TestRepoLocalName

        Write-Host $repo2.URL
        $repo2ModifiedURL = $repo2.URL.replace("/","\")

        Write-Host $repo2.URL
        Write-Host $repo2ModifiedURL
        WRite-Host $TestRepoLocalURL
        $repo2ModifiedURL | should be ("file:\\\" + $TestRepoLocalURL)
        $repo2.Priority | Should be 50

        $repo3 = Get-PSResourceRepository $PSGalleryName
        $repo3.URL | Should be $PSGalleryLocation
        $repo3.Priority | Should be 50
    }
}





    
################################
### Set-PSResourceRepository ###
################################
Describe 'Test Set-PSResourceRepository' -tags 'BVT', 'InnerLoop' {

    BeforeAll {

        #Unregister-PSResourceRepository -Name $PSGalleryName -ErrorAction SilentlyContinue
        #Unregister-PSResourceRepository -Name $TestRepoName -ErrorAction SilentlyContinue

        #Register-PSResourceRepository -PSGallery -ErrorAction SilentlyContinue
        #Register-PSResourceRepository -Name $PSGalleryName -ErrorAction SilentlyContinue

        #Unregister-PSResourceRepository -Name $PSGalleryName -ErrorAction SilentlyContinue
        #Unregister-PSResourceRepository -Name $TestRepoName -ErrorAction SilentlyContinue
        #Unregister-PSResourceRepository -Name $TestRepoName2 -ErrorAction SilentlyContinue
        #Unregister-PSResourceRepository -Name $TestRepoLocalName -ErrorAction SilentlyContinue
        #Unregister-PSResourceRepository -Name $TestRepoLocalName2 -ErrorAction SilentlyContinue
    }

    AfterAll {

    }

    BeforeEach {
    }

    AfterEach {
    }

    It 'Should set PSGallery to a trusted installation policy and a non-zero priority' {
        Unregister-PSResourceRepository -Name $PSGalleryName
        Register-PSResourceRepository -PSGallery -Trusted:$False -Priority 0
        Set-PSResourceRepository $PSGalleryName -Trusted -Priority 2

        $repo = Get-PSResourceRepository $PSGalleryName
        $repo.URL | should be $PSGalleryLocation
        $repo.Trusted | should be $true
        $repo.Priority | should be 2
    }

    It 'Should set PSGallery to an untrusted installation policy' {
        Unregister-PSResourceRepository -Name $PSGalleryName
        Register-PSResourceRepository -PSGallery -Trusted
        Set-PSResourceRepository -Name $PSGalleryName -Trusted:$False

        $repo = Get-PSResourceRepository $PSGalleryName
        $repo.Trusted | should be false
        $repo.Priority | should be 50
    }

    <#################################################################
    It 'Should fail to set PSGallery to a different URL' {
        Set-PSResourceRepository $PSGalleryName -URL $TestRepoURL -ErrorVariable ev -ErrorAction SilentlyContinue

        $ev[0].FullyQualifiedErrorId | Should be "The PSGallery repository has pre-defined locations. Setting the 'URL' parameter is not allowed, try again after removing the 'URL' parameter."
    }
    #>

}



Describe 'Test Set-PSResourceRepository with hashtable parameters' -tags 'BVT', 'InnerLoop' {

    AfterAll {
    }
    BeforeAll {
    }

    BeforeEach {
   
    }


    # if repo isn't registered it shouldnt be set'
    It 'Should set repository with given hashtable parameters' {
        Unregister-PSResourceRepository -Name $TestRepoName
        Unregister-PSResourceRepository -Name $TestRepoLocalName
        Unregister-PSResourceRepository -Name $PSGalleryName

        Register-PSResourceRepository $TestRepoName -URL $TestRepoURL

        $paramSetPSResourceRepository = @{
            Name     = $TestRepoName
            URL      = $TestRepoURL2
            Trusted  = $False
            Priority = 1
        }

        { Set-PSResourceRepository -Repositories $paramSetPSResourceRepository } | Should not Throw

        $repo = Get-PSResourceRepository -Name $TestRepoName
        $repo.URL | Should be $TestRepoURL2
        $repo.Trusted | Should be false
        $repo.Priority | Should be 1
    }

    It 'Should set multiple repositories' {
        Unregister-PSResourceRepository -Name $TestRepoName

        Register-PSResourceRepository $TestRepoName -URL $TestRepoURL
        Register-PSResourceRepository $TestRepoLocalName -URL $TestRepoLocalURL -Priority 0
        Register-PSResourceRepository -PSGallery

        Write-Host $TestRepoURL
        $repositories = @(
            @{ Name = $TestRepoName; URL = $TestRepoURL2; Priority = 9 },
            @{ Name = $TestRepoLocalName; URL = $TestRepoLocalURL2; Trusted =$True }
            #@{ Name = $PSGalleryName; Trusted = $True /*****************************/
        )

        { Set-PSResourceRepository -Repositories $repositories } | Should not Throw

        $repos = Get-PSResourceRepository
        $repos.Count | Should be 3

        $repo1 = Get-PSResourceRepository $TestRepoName
        $repo1.URL | Should be $TestRepoURL2
        $repo1.Trusted | Should be false
        $repo1.Priority | Should be 9

        $repo2 = Get-PSResourceRepository $TestRepoLocalName
        $repo2ModifiedURL = $repo2.URL.replace("/","\")
        $repo2ModifiedURL | should be ("file:\\\" + $TestRepoLocalURL2)
        #$repo2.URL | Should be $TestRepoLocalURL2
        $repo2.Trusted | Should be true
        $repo2.Priority | Should be 50

        $repo3 = Get-PSResourceRepository $PSGalleryName
        $repo3.URL | Should be $PSGalleryLocation
       # $repo3.Trusted | Should be true
        $repo3.Priority | Should be 50
    }

}






################################
### Get-PSResourceRepository ###
################################
Describe 'Test Get-PSResourceRepository' -tags 'BVT', 'InnerLoop' {

    BeforeAll {

        #Register-PSResourceRepository -PSGallery -Trusted -ErrorAction SilentlyContinue
        #Register-PSResourceRepository -Name $TestRepoName -URL $TestRepoURL -Trusted -Priority 2 -ErrorAction SilentlyContinue
        #Register-PSResourceRepository -Name $TestRepoName2 -URL $TestRepoURL2 -Priority 15 -ErrorAction SilentlyContinue
        ##Register-PSResourceRepository -Name $TestRepoLocalName -URL $TestRepoLocalURL -ErrorAction SilentlyContinue
        ##Register-PSResourceRepository -Name $TestRepoLocalName2 -URL $TestRepoLocalURL2 -ErrorAction SilentlyContinue
    }

    AfterAll {
        #Unregister-PSResourceRepository -Name $PSGalleryName -ErrorAction SilentlyContinue
        #Unregister-PSResourceRepository -Name $TestRepoName -ErrorAction SilentlyContinue
        #Unregister-PSResourceRepository -Name $TestRepoName2 -ErrorAction SilentlyContinue
       # Unregister-PSResourceRepository -Name $TestRepoLocalName -ErrorAction SilentlyContinue
       # Unregister-PSResourceRepository -Name $TestRepoLocalName2 -ErrorAction SilentlyContinue
    }

    BeforeEach {
 
    }

    It 'Should get PSGallery repository' {
        Unregister-PSResourceRepository -Name $PSGalleryName 
        Register-PSResourceRepository -PSGallery -Trusted -ErrorAction SilentlyContinue

        $repo = Get-PSResourceRepository $PSGalleryName
        $repo.URL | should be $PSGalleryLocation
        $repo.Trusted | should be $true
        $repo.Priority | should be 50
    }

    It 'Should get test repository' {
        Unregister-PSResourceRepository -Name $TestRepoName 
        Register-PSResourceRepository -Name $TestRepoName -URL $TestRepoURL -Trusted -Priority 2 -ErrorAction SilentlyContinue

        $repo = Get-PSResourceRepository $TestRepoName
        $repo.URL | should be $TestRepoURL
        $repo.Trusted | should be $true
        $repo.Priority | should be 2
    }



    It 'Should get multiple repositories' {
        get-PSResourceRepository
        Unregister-PSResourceRepository -Name $TestRepoLocalName
        Register-PSResourceRepository -Name $TestRepoName2 -URL $TestRepoURL2 -Priority 15 -ErrorAction SilentlyContinue
        Register-PSResourceRepository -Name $TestRepoLocalName2 -URL $TestRepoLocalURL2 -ErrorAction SilentlyContinue

        $repos = Get-PSResourceRepository $PSGalleryName, $TestRepoName2, $TestRepoLocalName2

        $repos.Count | Should be 3

        $repos.Name | should contain $PSGalleryName
        $repos.Name | should contain $TestRepoName2
        $repos.Name | should contain $TestRepoLocalName2

        
        $repos.URL | should contain $PSGalleryLocation
        $repos.URL | should contain $TestRepoURL2

        $repoModifiedURL = "file:///" + ($TestRepoLocalURL2.replace("\", "/"));
        $repos.URL | should contain $repoModifiedURL

        $repos.Priority | should contain 15
        $repos.Priority | should contain 50

   }

    It 'Should get all repositories' {
        Register-PSResourceRepository $TestRepoLocalName -URL $TestRepoLocalURL

        $repos = Get-PSResourceRepository

        $repos.Count | Should be 5        

        $repos.Name | should contain $PSGalleryName
        $repos.Name | should contain $TestRepoName
        $repos.Name | should contain $TestRepoName2
        $repos.Name | should contain $TestRepoLocalName
        $repos.Name | should contain $TestRepoLocalName2

        $repos.URL | should contain $PSGalleryLocation
        $repos.URL | should contain $TestRepoURL
        $repos.URL | should contain $TestRepoURL2

        $repoModifiedURL = "file:///" + ($TestRepoLocalURL.replace("\", "/"));
        $repos.URL | should contain $repoModifiedURL

        $repoModifiedURL2 = "file:///" + ($TestRepoLocalURL2.replace("\", "/"));
        $repos.URL | should contain $repoModifiedURL2

        $repos.Priority | should contain 2
        $repos.Priority | should contain 50
        $repos.Priority | should contain 15

    }
}






#######################################
### Unregister-PSResourceRepository ###
#######################################

Describe 'Test Unregister-PSResourceRepository' -tags 'BVT' {

    BeforeAll {

    }
    AfterAll {
       # Unregister-PSResourceRepository -Name $PSGalleryName -ErrorAction SilentlyContinue
       # Unregister-PSResourceRepository -Name $TestRepoName -ErrorAction SilentlyContinue
       # Unregister-PSResourceRepository -Name $TestRepoName2 -ErrorAction SilentlyContinue
      # Unregister-PSResourceRepository -Name $TestRepoLocalName -ErrorAction SilentlyContinue
      # Unregister-PSResourceRepository -Name $TestRepoLocalName2 -ErrorAction SilentlyContinue
    }

    BeforeEach {
      
    }

    ### Unregistering the PowerShell Gallery
    It 'Should unregister the default PSGallery' { 
        Unregister-PSResourceRepository $PSGalleryName -ErrorVariable ev -ErrorAction SilentlyContinue

        $repo = Get-PSResourceRepository $PSGalleryName
        $repo | Should -BeNullOrEmpty
    }

    ### Unregistering any repository
    It 'Should unregister a given repository' { 
        Unregister-PSResourceRepository $TestRepoName -ErrorVariable ev -ErrorAction SilentlyContinue

        $repo = Get-PSResourceRepository $TestRepoName
        $repo | Should -BeNullOrEmpty
    }
    
}


<#
    It 'Should unregister multiple repositories' {
        Unregister-PSResourceRepository $TestRepoName, $TestRepoName2, $TestRepoLocalName

        $repos = Get-PSResourceRepository $TestRepoName, $TestRepoName2, $TestRepoLocalName -ErrorVariable ev -ErrorAction SilentlyContinue
        $repos | Should -BeNullOrEmpty
        $ev[0].FullyQualifiedErrorId | Should be "Unable to find repository 'PSGallery'. Use Get-PSResourceRepository to see all available repositories."

    }

}

#>