diff --git a/.gitignore b/.gitignore index 5f122f278..a02478323 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ coverage.xml ._.DS_Store **/.DS_Store **/._.DS_Store +.gdn \ No newline at end of file diff --git a/Source/Classes/VSTeamPermissionInheritance.ps1 b/Source/Classes/VSTeamPermissionInheritance.ps1 index ac3b96be0..dd09de128 100644 --- a/Source/Classes/VSTeamPermissionInheritance.ps1 +++ b/Source/Classes/VSTeamPermissionInheritance.ps1 @@ -24,7 +24,7 @@ class VSTeamPermissionInheritance { $this.Token = "repoV2/$($this.ProjectID)/$repositoryID" - $this.Version = "$([VSTeamVersions]::Git)" + $this.Version = "$(_getApiVersion Git)" } "BuildDefinition" { @@ -39,7 +39,7 @@ class VSTeamPermissionInheritance { $this.Token = "$($this.ProjectID)/$buildDefinitionID" - $this.Version = "$([VSTeamVersions]::Build)" + $this.Version = "$(_getApiVersion Build)" } "ReleaseDefinition" { @@ -59,7 +59,7 @@ class VSTeamPermissionInheritance { $this.Token = "$($this.ProjectID)" + "$($releaseDefinition.path -replace "\\","/")" + "/$($releaseDefinition.id)" } - $this.Version = "$([VSTeamVersions]::Release)" + $this.Version = "$(_getApiVersion Release)" } } } diff --git a/Source/Classes/VSTeamVersions.ps1 b/Source/Classes/VSTeamVersions.ps1 index 95580b2ec..c7e1ac984 100644 --- a/Source/Classes/VSTeamVersions.ps1 +++ b/Source/Classes/VSTeamVersions.ps1 @@ -16,4 +16,5 @@ class VSTeamVersions { static [string] $ServiceFabricEndpoint = '' static [string] $ModuleVersion = $null static [string] $Graph = '' + static [string] $Policy = '' } \ No newline at end of file diff --git a/Source/Private/callMembershipAPI.ps1 b/Source/Private/callMembershipAPI.ps1 index a458b466d..a17c2845e 100644 --- a/Source/Private/callMembershipAPI.ps1 +++ b/Source/Private/callMembershipAPI.ps1 @@ -26,7 +26,7 @@ function _callMembershipAPI { -Id $Id ` -SubDomain "vssps" ` -Method $Method ` - -Version $([VSTeamVersions]::Graph) ` + -Version $(_getApiVersion Graph) ` -QueryString $query return $resp diff --git a/Source/Private/common.ps1 b/Source/Private/common.ps1 index 6949b303b..109fea36e 100644 --- a/Source/Private/common.ps1 +++ b/Source/Private/common.ps1 @@ -15,7 +15,7 @@ function _supportsGraph { } function _testGraphSupport { - if (-not [VSTeamVersions]::Graph) { + if (-not $(_getApiVersion Graph)) { return $false } @@ -30,7 +30,7 @@ function _supportsFeeds { } function _testFeedSupport { - if (-not [VSTeamVersions]::Packaging) { + if (-not $(_getApiVersion Packaging)) { return $false } @@ -46,7 +46,7 @@ function _supportsSecurityNamespace { function _supportsMemberEntitlementManagement { _hasAccount - if (-not [VSTeamVersions]::MemberEntitlementManagement) { + if (-not $(_getApiVersion MemberEntitlementManagement)) { throw 'This account does not support Member Entitlement.' } } @@ -56,6 +56,74 @@ function _testAdministrator { (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) } +# When you mock this in tests be sure to add a Parameter Filter that matches +# the Service that should be used. +# Mock _getApiVersion { return '1.0-gitUnitTests' } -ParameterFilter { $Service -eq 'Git' } +# Also test in the Assert-MockCalled that the correct version was used in the URL that was +# built for the API call. +function _getApiVersion { + [CmdletBinding(DefaultParameterSetName = 'Service')] + param ( + [parameter(ParameterSetName = 'Service', Mandatory = $true, Position = 0)] + [ValidateSet('Build', 'Release', 'Core', 'Git', 'DistributedTask', 'VariableGroups', 'Tfvc', 'Packaging', 'MemberEntitlementManagement', 'ExtensionsManagement', 'ServiceFabricEndpoint', 'Graph', 'TaskGroups', 'Policy')] + [string] $Service, + + [parameter(ParameterSetName = 'Target')] + [switch] $Target + ) + + if ($Target.IsPresent) { + return [VSTeamVersions]::Version + } + else { + + switch ($Service) { + 'Build' { + return [VSTeamVersions]::Build + } + 'Release' { + return [VSTeamVersions]::Release + } + 'Core' { + return [VSTeamVersions]::Core + } + 'Git' { + return [VSTeamVersions]::Git + } + 'DistributedTask' { + return [VSTeamVersions]::DistributedTask + } + 'VariableGroups' { + return [VSTeamVersions]::VariableGroups + } + 'Tfvc' { + return [VSTeamVersions]::Tfvc + } + 'Packaging' { + return [VSTeamVersions]::Packaging + } + 'MemberEntitlementManagement' { + return [VSTeamVersions]::MemberEntitlementManagement + } + 'ExtensionsManagement' { + return [VSTeamVersions]::ExtensionsManagement + } + 'ServiceFabricEndpoint' { + return [VSTeamVersions]::ServiceFabricEndpoint + } + 'Graph' { + return [VSTeamVersions]::Graph + } + 'TaskGroups' { + return [VSTeamVersions]::TaskGroups + } + 'Policy' { + return [VSTeamVersions]::Policy + } + } + } +} + function _getInstance { return [VSTeamVersions]::Account } @@ -76,7 +144,8 @@ function _buildRequestURI { [string]$version, [string]$subDomain, [object]$queryString, - [switch]$UseProjectId + [switch]$UseProjectId, + [switch]$NoProject ) DynamicParam { _buildProjectNameDynamicParam -Mandatory $false @@ -92,7 +161,12 @@ function _buildRequestURI { $sb.Append($(_addSubDomain -subDomain $subDomain -instance $(_getInstance))) | Out-Null - if ($ProjectName) { + # There are some APIs that must not have the project added to the URI. + # However, if they caller set the default project it will be passed in + # here and added to the URI by mistake. Functions that need the URI + # created without the project even if the default project is set needs + # to pass the -NoProject switch. + if ($ProjectName -and $NoProject.IsPresent -eq $false) { if ($UseProjectId.IsPresent) { $projectId = (Get-VSTeamProject -Name $ProjectName | Select-Object -ExpandProperty id) $sb.Append("/$projectId") | Out-Null @@ -278,7 +352,7 @@ function _getWorkItemTypes { # Call the REST API try { - $resp = _callAPI -ProjectName $ProjectName -area 'wit' -resource 'workitemtypes' -version $([VSTeamVersions]::Core) + $resp = _callAPI -ProjectName $ProjectName -area 'wit' -resource 'workitemtypes' -version $(_getApiVersion Core) # This call returns JSON with "": which causes the ConvertFrom-Json to fail. # To replace all the "": with "_end": @@ -294,6 +368,14 @@ function _getWorkItemTypes { } } +# When writing unit tests mock this and return false. +# This will prevent the dynamic project name parameter +# from trying to call the getProject function. +# Mock _hasProjectCacheExpired { return $false } +function _hasProjectCacheExpired { + return $([VSTeamProjectCache]::timestamp) -ne (Get-Date).Minute +} + function _getProjects { if (-not $(_getInstance)) { Write-Output @() @@ -302,7 +384,7 @@ function _getProjects { $resource = "/projects" $instance = $(_getInstance) - $version = [VSTeamVersions]::Core + $version = $(_getApiVersion Core) # Build the url to list the projects # You CANNOT use _buildRequestURI here or you will end up @@ -358,7 +440,7 @@ function _buildProjectNameDynamicParam { } # Generate and set the ValidateSet - if ($([VSTeamProjectCache]::timestamp) -ne (Get-Date).Minute) { + if (_hasProjectCacheExpired) { $arrSet = _getProjects [VSTeamProjectCache]::projects = $arrSet [VSTeamProjectCache]::timestamp = (Get-Date).Minute @@ -413,10 +495,10 @@ function _getProcesses { # Call the REST API try { - $query = @{} + $query = @{ } $query['stateFilter'] = 'All' $query['$top'] = '9999' - $resp = _callAPI -area 'process' -resource 'processes' -Version $([VSTeamVersions]::Core) -QueryString $query + $resp = _callAPI -area 'process' -resource 'processes' -Version $(_getApiVersion Core) -QueryString $query if ($resp.count -gt 0) { Write-Output ($resp.value).name @@ -620,7 +702,18 @@ function _callAPI { [string]$Url, [object]$QueryString, [hashtable]$AdditionalHeaders, - [switch]$UseProjectId + # Some API calls require the Project ID and not the project name. + # However, the dynamic project name parameter only shows you names + # and not the Project IDs. Using this flag the project name provided + # will be converted to the Project ID when building the URI for the API + # call. + [switch]$UseProjectId, + # This flag makes sure that even if a default project is set that it is + # not used to build the URI for the API call. Not all API require or + # allow the project to be used. Setting a default project would cause + # that project name to be used in building the URI that would lead to + # 404 because the URI would not be correct. + [switch]$NoProject ) # If the caller did not provide a Url build it. @@ -648,7 +741,7 @@ function _callAPI { if (_useWindowsAuthenticationOnPremise) { $params.Add('UseDefaultCredentials', $true) - $params.Add('Headers', @{}) + $params.Add('Headers', @{ }) } elseif (_useBearerToken) { $params.Add('Headers', @{Authorization = "Bearer $env:TEAM_TOKEN" }) @@ -664,7 +757,7 @@ function _callAPI { } # We have to remove any extra parameters not used by Invoke-RestMethod - $extra = 'UseProjectId', 'Area', 'Resource', 'SubDomain', 'Id', 'Version', 'JSON', 'ProjectName', 'Team', 'Url', 'QueryString', 'AdditionalHeaders' + $extra = 'NoProject', 'UseProjectId', 'Area', 'Resource', 'SubDomain', 'Id', 'Version', 'JSON', 'ProjectName', 'Team', 'Url', 'QueryString', 'AdditionalHeaders' foreach ($e in $extra) { $params.Remove($e) | Out-Null } try { @@ -737,7 +830,7 @@ function _trackServiceEndpointProgress { # Track status while (-not $isReady) { $statusTracking = _callAPI -ProjectName $projectName -Area 'distributedtask' -Resource 'serviceendpoints' -Id $resp.id ` - -Version $([VSTeamVersions]::DistributedTask) + -Version $(_getApiVersion DistributedTask) $isReady = $statusTracking.isReady; @@ -760,7 +853,7 @@ function _trackServiceEndpointProgress { } function _supportsServiceFabricEndpoint { - if (-not [VSTeamVersions]::ServiceFabricEndpoint) { + if (-not $(_getApiVersion ServiceFabricEndpoint)) { throw 'This account does not support Service Fabric endpoints.' } } diff --git a/Source/Public/Add-VSTeam.ps1 b/Source/Public/Add-VSTeam.ps1 index 527f3409f..4cc88abe9 100644 --- a/Source/Public/Add-VSTeam.ps1 +++ b/Source/Public/Add-VSTeam.ps1 @@ -18,8 +18,8 @@ function Add-VSTeam { $body = '{ "name": "' + $Name + '", "description": "' + $Description + '" }' # Call the REST API - $resp = _callAPI -Area 'projects' -Resource "$ProjectName/teams" ` - -Method Post -ContentType 'application/json' -Body $body -Version $([VSTeamVersions]::Core) + $resp = _callAPI -Area 'projects' -Resource "$ProjectName/teams" -NoProject ` + -Method Post -ContentType 'application/json' -Body $body -Version $(_getApiVersion Core) $team = [VSTeamTeam]::new($resp, $ProjectName) diff --git a/Source/Public/Add-VSTeamAccessControlEntry.ps1 b/Source/Public/Add-VSTeamAccessControlEntry.ps1 index 5a7ad37f6..5ebf4fb9d 100644 --- a/Source/Public/Add-VSTeamAccessControlEntry.ps1 +++ b/Source/Public/Add-VSTeamAccessControlEntry.ps1 @@ -5,15 +5,7 @@ function Add-VSTeamAccessControlEntry { [VSTeamSecurityNamespace] $SecurityNamespace, [Parameter(ParameterSetName = 'ByNamespaceId', Mandatory = $true)] - [ValidateScript({ - try { - [System.Guid]::Parse($_) | Out-Null - $true - } catch { - $false - } - })] - [string] $SecurityNamespaceId, + [guid] $SecurityNamespaceId, [Parameter(ParameterSetName = 'ByNamespace', Mandatory = $true)] [Parameter(ParameterSetName = 'ByNamespaceId', Mandatory = $true)] @@ -35,13 +27,12 @@ function Add-VSTeamAccessControlEntry { ) process { - if ($SecurityNamespace) - { + if ($SecurityNamespace) { $SecurityNamespaceId = $SecurityNamespace.ID } -$body = -@" + $body = + @" { "token": "$Token", "merge": true, @@ -57,11 +48,10 @@ $body = "@ # Call the REST API $resp = _callAPI -Area 'accesscontrolentries' -id $SecurityNamespaceId -method POST -body $body ` - -Version $([VSTeamVersions]::Core) ` + -Version $(_getApiVersion Core) -NoProject ` -ContentType "application/json" - if ($resp.count -ne 1) - { + if ($resp.count -ne 1) { throw "Expected 1 result, but got $($rep.count)" } diff --git a/Source/Public/Add-VSTeamBuild.ps1 b/Source/Public/Add-VSTeamBuild.ps1 index a6e4555c1..428456b6c 100644 --- a/Source/Public/Add-VSTeamBuild.ps1 +++ b/Source/Public/Add-VSTeamBuild.ps1 @@ -61,8 +61,8 @@ function Add-VSTeamBuild { else { # Find the BuildDefinition id from the name $id = Get-VSTeamBuildDefinition -ProjectName "$ProjectName" -Type All | - Where-Object { $_.name -eq $BuildDefinition } | - Select-Object -ExpandProperty id + Where-Object { $_.name -eq $BuildDefinition } | + Select-Object -ExpandProperty id } $body = @{ @@ -73,7 +73,7 @@ function Add-VSTeamBuild { if ($QueueName) { $queueId = Get-VSTeamQueue -ProjectName "$ProjectName" -queueName "$QueueName" | - Select-Object -ExpandProperty Id + Select-Object -ExpandProperty Id $body.Add('queue', @{ id = $queueId }) } @@ -89,7 +89,7 @@ function Add-VSTeamBuild { # Call the REST API $resp = _callAPI -ProjectName $ProjectName -Area 'build' -Resource 'builds' ` -Method Post -ContentType 'application/json' -Body ($body | ConvertTo-Json) ` - -Version $([VSTeamVersions]::Build) + -Version $(_getApiVersion Build) _applyTypesToBuild -item $resp diff --git a/Source/Public/Add-VSTeamBuildDefinition.ps1 b/Source/Public/Add-VSTeamBuildDefinition.ps1 index 33b69ec82..7d6557153 100644 --- a/Source/Public/Add-VSTeamBuildDefinition.ps1 +++ b/Source/Public/Add-VSTeamBuildDefinition.ps1 @@ -13,7 +13,7 @@ function Add-VSTeamBuildDefinition { # Bind the parameter to a friendly variable $ProjectName = $PSBoundParameters["ProjectName"] - $resp = _callAPI -Method Post -ProjectName $ProjectName -Area build -Resource definitions -Version $([VSTeamVersions]::Build) -infile $InFile -ContentType 'application/json' + $resp = _callAPI -Method Post -ProjectName $ProjectName -Area build -Resource definitions -Version $(_getApiVersion Build) -infile $InFile -ContentType 'application/json' return $resp } diff --git a/Source/Public/Add-VSTeamBuildTag.ps1 b/Source/Public/Add-VSTeamBuildTag.ps1 index 16ac325b4..2dde4817f 100644 --- a/Source/Public/Add-VSTeamBuildTag.ps1 +++ b/Source/Public/Add-VSTeamBuildTag.ps1 @@ -23,7 +23,7 @@ function Add-VSTeamBuildTag { foreach ($tag in $tags) { # Call the REST API _callAPI -ProjectName $projectName -Area 'build' -Resource "builds/$Id/tags" ` - -Method Put -Querystring @{tag = $tag} -Version $([VSTeamVersions]::Build) | Out-Null + -Method Put -Querystring @{tag = $tag} -Version $(_getApiVersion Build) | Out-Null } } } diff --git a/Source/Public/Add-VSTeamExtension.ps1 b/Source/Public/Add-VSTeamExtension.ps1 index eb30b1025..ffaf958d5 100644 --- a/Source/Public/Add-VSTeamExtension.ps1 +++ b/Source/Public/Add-VSTeamExtension.ps1 @@ -16,7 +16,7 @@ function Add-VSTeamExtension { $resource += '/' + $Version } - $resp = _callAPI -Method Post -SubDomain 'extmgmt' -Resource $resource -Version $([VSTeamVersions]::ExtensionsManagement) -ContentType "application/json" + $resp = _callAPI -Method Post -SubDomain 'extmgmt' -Resource $resource -Version $(_getApiVersion ExtensionsManagement) -ContentType "application/json" $item = [VSTeamExtension]::new($resp) diff --git a/Source/Public/Add-VSTeamFeed.ps1 b/Source/Public/Add-VSTeamFeed.ps1 index 7aa28f612..227f751cb 100644 --- a/Source/Public/Add-VSTeamFeed.ps1 +++ b/Source/Public/Add-VSTeamFeed.ps1 @@ -50,7 +50,7 @@ function Add-VSTeamFeed { # Call the REST API $resp = _callAPI -subDomain feeds -Area packaging -Resource feeds ` - -Method Post -ContentType 'application/json' -body $bodyAsJson -Version $([VSTeamVersions]::Packaging) + -Method Post -ContentType 'application/json' -body $bodyAsJson -Version $(_getApiVersion Packaging) return [VSTeamFeed]::new($resp) } diff --git a/Source/Public/Add-VSTeamGitRepository.ps1 b/Source/Public/Add-VSTeamGitRepository.ps1 index 8fd3d721e..174fc0c91 100644 --- a/Source/Public/Add-VSTeamGitRepository.ps1 +++ b/Source/Public/Add-VSTeamGitRepository.ps1 @@ -17,7 +17,7 @@ function Add-VSTeamGitRepository { try { # Call the REST API $resp = _callAPI -ProjectName $ProjectName -Area 'git' -Resource 'repositories' ` - -Method Post -ContentType 'application/json' -Body $body -Version $([VSTeamVersions]::Git) + -Method Post -ContentType 'application/json' -Body $body -Version $(_getApiVersion Git) # Storing the object before you return it cleaned up the pipeline. # When I just write the object from the constructor each property diff --git a/Source/Public/Add-VSTeamGitRepositoryPermission.ps1 b/Source/Public/Add-VSTeamGitRepositoryPermission.ps1 index f51bcb214..498229350 100644 --- a/Source/Public/Add-VSTeamGitRepositoryPermission.ps1 +++ b/Source/Public/Add-VSTeamGitRepositoryPermission.ps1 @@ -15,15 +15,7 @@ function Add-VSTeamGitRepositoryPermission { [parameter(Mandatory=$true,ParameterSetName="ByRepositoryIdAndGroup")] [parameter(Mandatory=$true,ParameterSetName="ByRepositoryIdAndUser")] [parameter(Mandatory=$true,ParameterSetName="ByRepositoryIdAndDescriptor")] - [ValidateScript({ - try { - [System.Guid]::Parse($_) | Out-Null - $true - } catch { - $false - } - })] - [string]$RepositoryId, + [guid]$RepositoryId, [parameter(Mandatory=$true,ParameterSetName="ByRepositoryNameAndGroup")] [parameter(Mandatory=$true,ParameterSetName="ByRepositoryNameAndUser")] diff --git a/Source/Public/Add-VSTeamPolicy.ps1 b/Source/Public/Add-VSTeamPolicy.ps1 index 330e4fd6d..718bee0be 100644 --- a/Source/Public/Add-VSTeamPolicy.ps1 +++ b/Source/Public/Add-VSTeamPolicy.ps1 @@ -31,7 +31,7 @@ function Add-VSTeamPolicy { try { # Call the REST API $resp = _callAPI -ProjectName $ProjectName -Area 'policy' -Resource 'configurations' ` - -Method Post -ContentType 'application/json' -Body $body -Version $([VSTeamVersions]::Git) + -Method Post -ContentType 'application/json' -Body $body -Version $(_getApiVersion Git) Write-Output $resp } diff --git a/Source/Public/Add-VSTeamProject.ps1 b/Source/Public/Add-VSTeamProject.ps1 index 841a0e441..01449539a 100644 --- a/Source/Public/Add-VSTeamProject.ps1 +++ b/Source/Public/Add-VSTeamProject.ps1 @@ -41,7 +41,7 @@ function Add-VSTeamProject { try { # Call the REST API $resp = _callAPI -Area 'projects' ` - -Method Post -ContentType 'application/json' -body $body -Version $([VSTeamVersions]::Core) + -Method Post -ContentType 'application/json' -body $body -Version $(_getApiVersion Core) _trackProjectProgress -resp $resp -title 'Creating team project' -msg "Name: $($ProjectName), Template: $($processTemplate), Src: $($srcCtrl)" diff --git a/Source/Public/Add-VSTeamPullRequest.ps1 b/Source/Public/Add-VSTeamPullRequest.ps1 index 6ae63c428..c637f529c 100644 --- a/Source/Public/Add-VSTeamPullRequest.ps1 +++ b/Source/Public/Add-VSTeamPullRequest.ps1 @@ -47,7 +47,7 @@ function Add-VSTeamPullRequest { try { Write-Debug 'Add-VSTeamPullRequest Call the REST API' $resp = _callAPI -ProjectName $ProjectName -Area 'git' -Resource 'repositories' -Id "$RepositoryId/pullrequests" ` - -Method Post -ContentType 'application/json;charset=utf-8' -Body $body -Version $([VSTeamVersions]::Release) + -Method Post -ContentType 'application/json;charset=utf-8' -Body $body -Version $(_getApiVersion Git) _applyTypesToPullRequests -item $resp diff --git a/Source/Public/Add-VSTeamRelease.ps1 b/Source/Public/Add-VSTeamRelease.ps1 index 984dc8a1b..b9127f86c 100644 --- a/Source/Public/Add-VSTeamRelease.ps1 +++ b/Source/Public/Add-VSTeamRelease.ps1 @@ -91,7 +91,7 @@ function Add-VSTeamRelease { try { Write-Debug 'Add-VSTeamRelease Call the REST API' $resp = _callAPI -SubDomain 'vsrm' -ProjectName $ProjectName -Area 'release' -Resource 'releases' ` - -Method Post -ContentType 'application/json' -Body $body -Version $([VSTeamVersions]::Release) + -Method Post -ContentType 'application/json' -Body $body -Version $(_getApiVersion Release) _applyTypesToRelease $resp diff --git a/Source/Public/Add-VSTeamReleaseDefinition.ps1 b/Source/Public/Add-VSTeamReleaseDefinition.ps1 index c9cecfb2e..5311e0399 100644 --- a/Source/Public/Add-VSTeamReleaseDefinition.ps1 +++ b/Source/Public/Add-VSTeamReleaseDefinition.ps1 @@ -16,7 +16,7 @@ function Add-VSTeamReleaseDefinition { $ProjectName = $PSBoundParameters["ProjectName"] $resp = _callAPI -Method Post -subDomain vsrm -Area release -Resource definitions -ProjectName $ProjectName ` - -Version $([VSTeamVersions]::Release) -inFile $inFile -ContentType 'application/json' + -Version $(_getApiVersion Release) -inFile $inFile -ContentType 'application/json' Write-Output $resp } diff --git a/Source/Public/Add-VSTeamServiceEndpoint.ps1 b/Source/Public/Add-VSTeamServiceEndpoint.ps1 index 3c8b78fd5..16374818e 100644 --- a/Source/Public/Add-VSTeamServiceEndpoint.ps1 +++ b/Source/Public/Add-VSTeamServiceEndpoint.ps1 @@ -26,7 +26,7 @@ function Add-VSTeamServiceEndpoint { # Call the REST API $resp = _callAPI -ProjectName $projectName -Area 'distributedtask' -Resource 'serviceendpoints' ` - -Method Post -ContentType 'application/json' -body $body -Version $([VSTeamVersions]::DistributedTask) + -Method Post -ContentType 'application/json' -body $body -Version $(_getApiVersion DistributedTask) _trackServiceEndpointProgress -projectName $projectName -resp $resp -title 'Creating Service Endpoint' -msg "Creating $endpointName" diff --git a/Source/Public/Add-VSTeamTaskGroup.ps1 b/Source/Public/Add-VSTeamTaskGroup.ps1 index 5fbc0d475..0788b9aa1 100644 --- a/Source/Public/Add-VSTeamTaskGroup.ps1 +++ b/Source/Public/Add-VSTeamTaskGroup.ps1 @@ -17,10 +17,10 @@ function Add-VSTeamTaskGroup { $ProjectName = $PSBoundParameters["ProjectName"] if ($InFile) { - $resp = _callAPI -Method Post -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $([VSTeamVersions]::TaskGroups) -InFile $InFile -ContentType 'application/json' + $resp = _callAPI -Method Post -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $(_getApiVersion TaskGroups) -InFile $InFile -ContentType 'application/json' } else { - $resp = _callAPI -Method Post -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $([VSTeamVersions]::TaskGroups) -ContentType 'application/json' -Body $Body + $resp = _callAPI -Method Post -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $(_getApiVersion TaskGroups) -ContentType 'application/json' -Body $Body } return $resp diff --git a/Source/Public/Add-VSTeamUserEntitlement.ps1 b/Source/Public/Add-VSTeamUserEntitlement.ps1 index dcd753e92..e654911ae 100644 --- a/Source/Public/Add-VSTeamUserEntitlement.ps1 +++ b/Source/Public/Add-VSTeamUserEntitlement.ps1 @@ -52,6 +52,6 @@ function Add-VSTeamUserEntitlement { $body = $obj | ConvertTo-Json # Call the REST API - _callAPI -Method Post -Body $body -SubDomain 'vsaex' -Resource 'userentitlements' -Version $([VSTeamVersions]::MemberEntitlementManagement) -ContentType "application/json" + _callAPI -Method Post -Body $body -SubDomain 'vsaex' -Resource 'userentitlements' -Version $(_getApiVersion MemberEntitlementManagement) -ContentType "application/json" } } \ No newline at end of file diff --git a/Source/Public/Add-VSTeamVariableGroup.ps1 b/Source/Public/Add-VSTeamVariableGroup.ps1 index cc0c76e75..6b1d14e5f 100644 --- a/Source/Public/Add-VSTeamVariableGroup.ps1 +++ b/Source/Public/Add-VSTeamVariableGroup.ps1 @@ -55,7 +55,7 @@ function Add-VSTeamVariableGroup { # Call the REST API $resp = _callAPI -ProjectName $projectName -Area 'distributedtask' -Resource 'variablegroups' ` - -Method Post -ContentType 'application/json' -body $body -Version $([VSTeamVersions]::VariableGroups) + -Method Post -ContentType 'application/json' -body $body -Version $(_getApiVersion VariableGroups) return Get-VSTeamVariableGroup -ProjectName $ProjectName -id $resp.id } diff --git a/Source/Public/Add-VSTeamWorkItem.ps1 b/Source/Public/Add-VSTeamWorkItem.ps1 index 459fd93e8..f2459cca2 100644 --- a/Source/Public/Add-VSTeamWorkItem.ps1 +++ b/Source/Public/Add-VSTeamWorkItem.ps1 @@ -115,7 +115,7 @@ function Add-VSTeamWorkItem { # Call the REST API $resp = _callAPI -ProjectName $ProjectName -Area 'wit' -Resource 'workitems' ` - -Version $([VSTeamVersions]::Core) -id $WorkItemType -Method Post ` + -Version $(_getApiVersion Core) -id $WorkItemType -Method Post ` -ContentType 'application/json-patch+json' -Body $json _applyTypesToWorkItem -item $resp diff --git a/Source/Public/Disable-VSTeamAgent.ps1 b/Source/Public/Disable-VSTeamAgent.ps1 index b2e6f6c92..cf048c75f 100644 --- a/Source/Public/Disable-VSTeamAgent.ps1 +++ b/Source/Public/Disable-VSTeamAgent.ps1 @@ -11,7 +11,7 @@ function Disable-VSTeamAgent { process { foreach ($item in $Id) { try { - _callAPI -Method Patch -Area "distributedtask/pools/$PoolId" -Resource agents -Id $item -Version $([VSTeamVersions]::DistributedTask) -ContentType "application/json" -Body "{'enabled':false,'id':$item,'maxParallelism':1}" | Out-Null + _callAPI -Method Patch -Area "distributedtask/pools/$PoolId" -NoProject -Resource agents -Id $item -Version $(_getApiVersion DistributedTask) -ContentType "application/json" -Body "{'enabled':false,'id':$item,'maxParallelism':1}" | Out-Null Write-Output "Disabled agent $item" } catch { diff --git a/Source/Public/Enable-VSTeamAgent.ps1 b/Source/Public/Enable-VSTeamAgent.ps1 index f162140bf..99c8df0f8 100644 --- a/Source/Public/Enable-VSTeamAgent.ps1 +++ b/Source/Public/Enable-VSTeamAgent.ps1 @@ -11,7 +11,7 @@ function Enable-VSTeamAgent { process { foreach ($item in $Id) { try { - _callAPI -Method Patch -Area "distributedtask/pools/$PoolId" -Resource agents -Id $item -Version $([VSTeamVersions]::DistributedTask) -ContentType "application/json" -Body "{'enabled':true,'id':$item,'maxParallelism':1}" | Out-Null + _callAPI -Method Patch -Area "distributedtask/pools/$PoolId" -NoProject -Resource agents -Id $item -Version $(_getApiVersion DistributedTask) -ContentType "application/json" -Body "{'enabled':true,'id':$item,'maxParallelism':1}" | Out-Null Write-Output "Enabled agent $item" } catch { diff --git a/Source/Public/Get-VSTeam.ps1 b/Source/Public/Get-VSTeam.ps1 index 4bf472a1e..79642b746 100644 --- a/Source/Public/Get-VSTeam.ps1 +++ b/Source/Public/Get-VSTeam.ps1 @@ -28,7 +28,7 @@ function Get-VSTeam { foreach ($item in $Id) { # Call the REST API $resp = _callAPI -Area 'projects' -Resource "$ProjectName/teams" -id $item ` - -Version $([VSTeamVersions]::Core) + -Version $(_getApiVersion Core) $team = [VSTeamTeam]::new($resp, $ProjectName) @@ -39,7 +39,7 @@ function Get-VSTeam { foreach ($item in $Name) { # Call the REST API $resp = _callAPI -Area 'projects' -Resource "$ProjectName/teams" -id $item ` - -Version $([VSTeamVersions]::Core) + -Version $(_getApiVersion Core) $team = [VSTeamTeam]::new($resp, $ProjectName) @@ -49,7 +49,7 @@ function Get-VSTeam { else { # Call the REST API $resp = _callAPI -Area 'projects' -Resource "$ProjectName/teams" ` - -Version $([VSTeamVersions]::Core) ` + -Version $(_getApiVersion Core) ` -QueryString @{ '$top' = $top '$skip' = $skip diff --git a/Source/Public/Get-VSTeamAPIVersion.ps1 b/Source/Public/Get-VSTeamAPIVersion.ps1 index e84066996..f9fb5dd53 100644 --- a/Source/Public/Get-VSTeamAPIVersion.ps1 +++ b/Source/Public/Get-VSTeamAPIVersion.ps1 @@ -12,11 +12,12 @@ function Get-VSTeamAPIVersion { DistributedTask = $([VSTeamVersions]::DistributedTask) VariableGroups = $([VSTeamVersions]::VariableGroups) Tfvc = $([VSTeamVersions]::Tfvc) - Packaging = $([VSTeamVersions]::Packaging) + Packaging = $(_getApiVersion Packaging) TaskGroups = $([VSTeamVersions]::TaskGroups) MemberEntitlementManagement = $([VSTeamVersions]::MemberEntitlementManagement) ExtensionsManagement = $([VSTeamVersions]::ExtensionsManagement) ServiceFabricEndpoint = $([VSTeamVersions]::ServiceFabricEndpoint) Graph = $([VSTeamVersions]::Graph) + Policy = $([VSTeamVersions]::Policy) } } \ No newline at end of file diff --git a/Source/Public/Get-VSTeamAccessControlList.ps1 b/Source/Public/Get-VSTeamAccessControlList.ps1 index 6baad16d5..dd0e0f912 100644 --- a/Source/Public/Get-VSTeamAccessControlList.ps1 +++ b/Source/Public/Get-VSTeamAccessControlList.ps1 @@ -6,15 +6,7 @@ function Get-VSTeamAccessControlList { [Parameter(ParameterSetName = 'ByNamespaceId', Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [Alias('ID')] - [ValidateScript({ - try { - [System.Guid]::Parse($_) | Out-Null - $true - } catch { - $false - } - })] - [string] $SecurityNamespaceId, + [guid] $SecurityNamespaceId, [Parameter(ParameterSetName = 'ByNamespace', Mandatory = $false)] [Parameter(ParameterSetName = 'ByNamespaceId', Mandatory = $false)] @@ -62,7 +54,7 @@ function Get-VSTeamAccessControlList { # Call the REST API $resp = _callAPI -Area 'accesscontrollists' -id $SecurityNamespaceId -method GET ` - -Version $([VSTeamVersions]::Core) ` + -Version $(_getApiVersion Core) -NoProject ` -QueryString $queryString try { diff --git a/Source/Public/Get-VSTeamAgent.ps1 b/Source/Public/Get-VSTeamAgent.ps1 index 7fee4b41a..0ed489316 100644 --- a/Source/Public/Get-VSTeamAgent.ps1 +++ b/Source/Public/Get-VSTeamAgent.ps1 @@ -12,8 +12,8 @@ function Get-VSTeamAgent { process { if ($id) { - $resp = _callAPI -Area "distributedtask/pools/$PoolId" -Resource agents -Id $id ` - -Body @{includeCapabilities = 'true'} -Version $([VSTeamVersions]::DistributedTask) + $resp = _callAPI -Area "distributedtask/pools/$PoolId" -Resource agents -Id $id -NoProject ` + -Body @{includeCapabilities = 'true'} -Version $(_getApiVersion DistributedTask) # Storing the object before you return it cleaned up the pipeline. # When I just write the object from the constructor each property @@ -23,8 +23,8 @@ function Get-VSTeamAgent { Write-Output $item } else { - $resp = _callAPI -Area "distributedtask/pools/$PoolId" -Resource agents ` - -Body @{includeCapabilities = 'true'} -Version $([VSTeamVersions]::DistributedTask) + $resp = _callAPI -Area "distributedtask/pools/$PoolId" -Resource agents -NoProject ` + -Body @{includeCapabilities = 'true'} -Version $(_getApiVersion DistributedTask) $objs = @() diff --git a/Source/Public/Get-VSTeamApproval.ps1 b/Source/Public/Get-VSTeamApproval.ps1 index 3815880e3..18857acc1 100644 --- a/Source/Public/Get-VSTeamApproval.ps1 +++ b/Source/Public/Get-VSTeamApproval.ps1 @@ -40,7 +40,7 @@ function Get-VSTeamApproval { } # Call the REST API - $resp = _callAPI -ProjectName $ProjectName -Area release -Resource approvals -SubDomain vsrm -Version $([VSTeamVersions]::Release) -QueryString $queryString + $resp = _callAPI -ProjectName $ProjectName -Area release -Resource approvals -SubDomain vsrm -Version $(_getApiVersion Release) -QueryString $queryString # Apply a Type Name so we can use custom format view and custom type extensions foreach ($item in $resp.value) { diff --git a/Source/Public/Get-VSTeamBuild.ps1 b/Source/Public/Get-VSTeamBuild.ps1 index 2e45bdcde..39c4968fd 100644 --- a/Source/Public/Get-VSTeamBuild.ps1 +++ b/Source/Public/Get-VSTeamBuild.ps1 @@ -52,7 +52,7 @@ function Get-VSTeamBuild { foreach ($item in $id) { # Build the url to return the single build $resp = _callAPI -ProjectName $projectName -Area 'build' -Resource 'builds' -id $item ` - -Version $([VSTeamVersions]::Build) + -Version $(_getApiVersion Build) _applyTypesToBuild -item $resp @@ -62,7 +62,7 @@ function Get-VSTeamBuild { else { # Build the url to list the builds $resp = _callAPI -ProjectName $projectName -Area 'build' -Resource 'builds' ` - -Version $([VSTeamVersions]::Build) ` + -Version $(_getApiVersion Build) ` -Querystring @{ '$top' = $top 'type' = $type diff --git a/Source/Public/Get-VSTeamBuildArtifact.ps1 b/Source/Public/Get-VSTeamBuildArtifact.ps1 index a67b24fb0..2f2f35e13 100644 --- a/Source/Public/Get-VSTeamBuildArtifact.ps1 +++ b/Source/Public/Get-VSTeamBuildArtifact.ps1 @@ -13,7 +13,7 @@ function Get-VSTeamBuildArtifact { $ProjectName = $PSBoundParameters["ProjectName"] $resp = _callAPI -ProjectName $projectName -Area 'build' -Resource "builds/$Id/artifacts" ` - -Version $([VSTeamVersions]::Build) + -Version $(_getApiVersion Build) foreach ($item in $resp.value) { _applyArtifactTypes -item $item diff --git a/Source/Public/Get-VSTeamBuildDefinition.ps1 b/Source/Public/Get-VSTeamBuildDefinition.ps1 index f649f9cd2..ddea8063b 100644 --- a/Source/Public/Get-VSTeamBuildDefinition.ps1 +++ b/Source/Public/Get-VSTeamBuildDefinition.ps1 @@ -36,7 +36,7 @@ function Get-VSTeamBuildDefinition { if ($id) { foreach ($item in $id) { - $resp = _callAPI -ProjectName $ProjectName -Id $item -Area build -Resource definitions -Version $([VSTeamVersions]::Build) ` + $resp = _callAPI -ProjectName $ProjectName -Id $item -Area build -Resource definitions -Version $(_getApiVersion Build) ` -QueryString @{revision = $revision } if ($JSON.IsPresent) { @@ -55,7 +55,7 @@ function Get-VSTeamBuildDefinition { } } else { - $resp = _callAPI -ProjectName $ProjectName -Area build -Resource definitions -Version $([VSTeamVersions]::Build) ` + $resp = _callAPI -ProjectName $ProjectName -Area build -Resource definitions -Version $(_getApiVersion Build) ` -QueryString @{type = $type; name = $filter; includeAllProperties = $true } $objs = @() diff --git a/Source/Public/Get-VSTeamBuildLog.ps1 b/Source/Public/Get-VSTeamBuildLog.ps1 index 85f4f2681..31858a0e0 100644 --- a/Source/Public/Get-VSTeamBuildLog.ps1 +++ b/Source/Public/Get-VSTeamBuildLog.ps1 @@ -20,7 +20,7 @@ function Get-VSTeamBuildLog { # Build the url to return the logs of the build # Call the REST API to get the number of logs for the build $resp = _callAPI -ProjectName $projectName -Area 'build' -Resource "builds/$item/logs" ` - -Version $([VSTeamVersions]::Build) + -Version $(_getApiVersion Build) $fullLogIndex = $($resp.count - 1) } @@ -32,7 +32,7 @@ function Get-VSTeamBuildLog { # Build the url to return the single build # Call the REST API to get the number of logs for the build $resp = _callAPI -ProjectName $projectName -Area 'build' -Resource "builds/$item/logs" -id $fullLogIndex ` - -Version $([VSTeamVersions]::Build) + -Version $(_getApiVersion Build) Write-Output $resp } diff --git a/Source/Public/Get-VSTeamBuildTag.ps1 b/Source/Public/Get-VSTeamBuildTag.ps1 index a82aca5f9..ee828741e 100644 --- a/Source/Public/Get-VSTeamBuildTag.ps1 +++ b/Source/Public/Get-VSTeamBuildTag.ps1 @@ -14,7 +14,7 @@ function Get-VSTeamBuildTag { # Call the REST API $resp = _callAPI -ProjectName $projectName -Area 'build' -Resource "builds/$Id/tags" ` - -Version $([VSTeamVersions]::Build) + -Version $(_getApiVersion Build) return $resp.value } diff --git a/Source/Public/Get-VSTeamClassificationNode.ps1 b/Source/Public/Get-VSTeamClassificationNode.ps1 index 961740c18..ff00dbee7 100644 --- a/Source/Public/Get-VSTeamClassificationNode.ps1 +++ b/Source/Public/Get-VSTeamClassificationNode.ps1 @@ -49,12 +49,12 @@ function Get-VSTeamClassificationNode { { # Call the REST API $resp = _callAPI -ProjectName $ProjectName -Area 'wit' -Resource "classificationnodes" -id $id ` - -Version $([VSTeamVersions]::Core) ` + -Version $(_getApiVersion Core) ` -QueryString $queryString } else { # Call the REST API $resp = _callAPI -ProjectName $ProjectName -Area 'wit' -Resource "classificationnodes" -id $id ` - -Version $([VSTeamVersions]::Core) ` + -Version $(_getApiVersion Core) ` } if ([bool]($resp.PSobject.Properties.name -match "value")) diff --git a/Source/Public/Get-VSTeamCloudSubscription.ps1 b/Source/Public/Get-VSTeamCloudSubscription.ps1 index e4fb30f2c..29451ddbe 100644 --- a/Source/Public/Get-VSTeamCloudSubscription.ps1 +++ b/Source/Public/Get-VSTeamCloudSubscription.ps1 @@ -4,7 +4,7 @@ function Get-VSTeamCloudSubscription { # Call the REST API $resp = _callAPI -Area 'distributedtask' -Resource 'serviceendpointproxy/azurermsubscriptions' ` - -Version $([VSTeamVersions]::DistributedTask) + -Version $(_getApiVersion DistributedTask) -NoProject # Apply a Type Name so we can use custom format view and custom type extensions foreach ($item in $resp.value) { diff --git a/Source/Public/Get-VSTeamDescriptor.ps1 b/Source/Public/Get-VSTeamDescriptor.ps1 index 557c7c26f..0a8e7aff4 100644 --- a/Source/Public/Get-VSTeamDescriptor.ps1 +++ b/Source/Public/Get-VSTeamDescriptor.ps1 @@ -11,8 +11,8 @@ function Get-VSTeamDescriptor { # Call the REST API $resp = _callAPI -Area 'graph' -Resource 'descriptors' -id $StorageKey ` - -Version $([VSTeamVersions]::Graph) ` - -SubDomain 'vssps' + -Version $(_getApiVersion Graph) ` + -SubDomain 'vssps' -NoProject # Storing the object before you return it cleaned up the pipeline. # When I just write the object from the constructor each property diff --git a/Source/Public/Get-VSTeamExtension.ps1 b/Source/Public/Get-VSTeamExtension.ps1 index bc93d3020..6f68c9edc 100644 --- a/Source/Public/Get-VSTeamExtension.ps1 +++ b/Source/Public/Get-VSTeamExtension.ps1 @@ -20,14 +20,14 @@ function Get-VSTeamExtension { if ($PublisherId -and $ExtensionId) { $resource = "extensionmanagement/installedextensionsbyname/$PublisherId/$ExtensionId" - $resp = _callAPI -SubDomain 'extmgmt' -Resource $resource -Version $([VSTeamVersions]::ExtensionsManagement) + $resp = _callAPI -SubDomain 'extmgmt' -Resource $resource -Version $(_getApiVersion ExtensionsManagement) $item = [VSTeamExtension]::new($resp) Write-Output $item } else { - $queryString = @{} + $queryString = @{ } if ($IncludeInstallationIssues.IsPresent) { $queryString.includeCapabilities = $true } @@ -40,7 +40,7 @@ function Get-VSTeamExtension { $queryString.includeErrors = $true } - $resp = _callAPI -SubDomain 'extmgmt' -Resource 'extensionmanagement/installedextensions' -QueryString $queryString -Version $([VSTeamVersions]::ExtensionsManagement) + $resp = _callAPI -SubDomain 'extmgmt' -Resource 'extensionmanagement/installedextensions' -QueryString $queryString -Version $(_getApiVersion ExtensionsManagement) $objs = @() diff --git a/Source/Public/Get-VSTeamFeed.ps1 b/Source/Public/Get-VSTeamFeed.ps1 index e3607ab5c..2548fd130 100644 --- a/Source/Public/Get-VSTeamFeed.ps1 +++ b/Source/Public/Get-VSTeamFeed.ps1 @@ -12,7 +12,7 @@ function Get-VSTeamFeed { if ($id) { foreach ($item in $id) { - $resp = _callAPI -subDomain feeds -Id $item -Area packaging -Resource feeds -Version $([VSTeamVersions]::Packaging) + $resp = _callAPI -NoProject -subDomain feeds -Id $item -Area packaging -Resource feeds -Version $(_getApiVersion Packaging) Write-Verbose $resp $item = [VSTeamFeed]::new($resp) @@ -21,7 +21,7 @@ function Get-VSTeamFeed { } } else { - $resp = _callAPI -subDomain feeds -Area packaging -Resource feeds -Version $([VSTeamVersions]::Packaging) + $resp = _callAPI -NoProject -subDomain feeds -Area packaging -Resource feeds -Version $(_getApiVersion Packaging) $objs = @() diff --git a/Source/Public/Get-VSTeamGitCommit.ps1 b/Source/Public/Get-VSTeamGitCommit.ps1 index 1dd53a095..466a2afe4 100644 --- a/Source/Public/Get-VSTeamGitCommit.ps1 +++ b/Source/Public/Get-VSTeamGitCommit.ps1 @@ -134,7 +134,7 @@ function Get-VSTeamGitCommit { 'searchCriteria.user' = $User } - $resp = _callAPI -ProjectName $ProjectName -Id "$RepositoryID/commits" -Area git -Resource repositories -Version $([VSTeamVersions]::Git) -QueryString $queryString + $resp = _callAPI -ProjectName $ProjectName -Id "$RepositoryID/commits" -Area git -Resource repositories -Version $(_getApiVersion Git) -QueryString $queryString $obj = @() diff --git a/Source/Public/Get-VSTeamGitRef.ps1 b/Source/Public/Get-VSTeamGitRef.ps1 index b542c2f0e..f04543290 100644 --- a/Source/Public/Get-VSTeamGitRef.ps1 +++ b/Source/Public/Get-VSTeamGitRef.ps1 @@ -31,7 +31,7 @@ function Get-VSTeamGitRef { 'continuationToken' = $continuationToken } - $url = _buildRequestURI -Area git -Resource repositories -Version $([VSTeamVersions]::Git) -ProjectName $ProjectName -Id "$RepositoryID/refs" + $url = _buildRequestURI -Area git -Resource repositories -Version $(_getApiVersion Git) -ProjectName $ProjectName -Id "$RepositoryID/refs" $resp = _callAPI -url $url -QueryString $queryString $obj = @() diff --git a/Source/Public/Get-VSTeamGitRepository.ps1 b/Source/Public/Get-VSTeamGitRepository.ps1 index 723448185..dda6af3d6 100644 --- a/Source/Public/Get-VSTeamGitRepository.ps1 +++ b/Source/Public/Get-VSTeamGitRepository.ps1 @@ -20,7 +20,7 @@ function Get-VSTeamGitRepository { if ($id) { foreach ($item in $id) { try { - $resp = _callAPI -ProjectName $ProjectName -Id $item -Area git -Resource repositories -Version $([VSTeamVersions]::Git) + $resp = _callAPI -ProjectName $ProjectName -Id $item -Area git -Resource repositories -Version $(_getApiVersion Git) # Storing the object before you return it cleaned up the pipeline. # When I just write the object from the constructor each property @@ -37,7 +37,7 @@ function Get-VSTeamGitRepository { elseif ($Name) { foreach ($item in $Name) { try { - $resp = _callAPI -ProjectName $ProjectName -Id $item -Area git -Resource repositories -Version $([VSTeamVersions]::Git) + $resp = _callAPI -ProjectName $ProjectName -Id $item -Area git -Resource repositories -Version $(_getApiVersion Git) # Storing the object before you return it cleaned up the pipeline. # When I just write the object from the constructor each property @@ -53,9 +53,9 @@ function Get-VSTeamGitRepository { } else { if($ProjectName) { - $resp = _callAPI -ProjectName $ProjectName -Area git -Resource repositories -Version $([VSTeamVersions]::Git) + $resp = _callAPI -ProjectName $ProjectName -Area git -Resource repositories -Version $(_getApiVersion Git) } else { - $resp = _callAPI -Area git -Resource repositories -Version $([VSTeamVersions]::Git) + $resp = _callAPI -Area git -Resource repositories -Version $(_getApiVersion Git) } $objs = @() diff --git a/Source/Public/Get-VSTeamGitStat.ps1 b/Source/Public/Get-VSTeamGitStat.ps1 index b369419c4..f2adb880b 100644 --- a/Source/Public/Get-VSTeamGitStat.ps1 +++ b/Source/Public/Get-VSTeamGitStat.ps1 @@ -42,7 +42,7 @@ function Get-VSTeamGitStat { 'baseVersionDescriptor.versionOptions' = $VersionOptions } - $resp = _callAPI -ProjectName $ProjectName -Id "$RepositoryID/stats/branches" -Area git -Resource repositories -Version $([VSTeamVersions]::Git) -QueryString $queryString + $resp = _callAPI -ProjectName $ProjectName -Id "$RepositoryID/stats/branches" -Area git -Resource repositories -Version $(_getApiVersion Git) -QueryString $queryString $hasValueProp = $resp.PSObject.Properties.Match('value') diff --git a/Source/Public/Get-VSTeamGroup.ps1 b/Source/Public/Get-VSTeamGroup.ps1 index e6f88a3bc..11dbbf5e9 100644 --- a/Source/Public/Get-VSTeamGroup.ps1 +++ b/Source/Public/Get-VSTeamGroup.ps1 @@ -30,8 +30,8 @@ function Get-VSTeamGroup { if ($Descriptor) { # Call the REST API - $resp = _callAPI -Area 'graph' -Resource 'groups' -id $Descriptor ` - -Version $([VSTeamVersions]::Graph) ` + $resp = _callAPI -NoProject -Area 'graph' -Resource 'groups' -id $Descriptor ` + -Version $(_getApiVersion Graph) ` -SubDomain 'vssps' # Storing the object before you return it cleaned up the pipeline. @@ -59,8 +59,8 @@ function Get-VSTeamGroup { try { # Call the REST API - $resp = _callAPI -Area 'graph' -id 'groups' ` - -Version $([VSTeamVersions]::Graph) ` + $resp = _callAPI -NoProject -Area 'graph' -id 'groups' ` + -Version $(_getApiVersion Graph) ` -QueryString $queryString ` -SubDomain 'vssps' diff --git a/Source/Public/Get-VSTeamJobRequest.ps1 b/Source/Public/Get-VSTeamJobRequest.ps1 index c2ec5c25c..0e0f271cd 100644 --- a/Source/Public/Get-VSTeamJobRequest.ps1 +++ b/Source/Public/Get-VSTeamJobRequest.ps1 @@ -24,7 +24,7 @@ function Get-VSTeamJobRequest { } $resp = _callAPI -Area "distributedtask/pools/$PoolId" -Resource "jobrequests" ` - -QueryString $body -Version $([VSTeamVersions]::DistributedTask) + -QueryString $body -Version $(_getApiVersion DistributedTask) $objs = @() diff --git a/Source/Public/Get-VSTeamMember.ps1 b/Source/Public/Get-VSTeamMember.ps1 index 7d9daeb9e..e2c44a52c 100644 --- a/Source/Public/Get-VSTeamMember.ps1 +++ b/Source/Public/Get-VSTeamMember.ps1 @@ -21,7 +21,7 @@ function Get-VSTeamMember { # Bind the parameter to a friendly variable $ProjectName = $PSBoundParameters["ProjectName"] - $resp = _callAPI -Id "$TeamId/members" -Area 'projects' -Resource "$ProjectName/teams" -Version $([VSTeamVersions]::Core) ` + $resp = _callAPI -Id "$TeamId/members" -Area 'projects' -Resource "$ProjectName/teams" -Version $(_getApiVersion Core) ` -QueryString @{'$top' = $top; '$skip' = $skip} # Apply a Type Name so we can use custom format view and custom type extensions diff --git a/Source/Public/Get-VSTeamPolicy.ps1 b/Source/Public/Get-VSTeamPolicy.ps1 index 666ff636e..3dffae7c6 100644 --- a/Source/Public/Get-VSTeamPolicy.ps1 +++ b/Source/Public/Get-VSTeamPolicy.ps1 @@ -15,7 +15,7 @@ function Get-VSTeamPolicy { if ($id) { foreach ($item in $id) { try { - $resp = _callAPI -ProjectName $ProjectName -Id $item -Area policy -Resource configurations -Version $([VSTeamVersions]::Git) + $resp = _callAPI -ProjectName $ProjectName -Id $item -Area policy -Resource configurations -Version $(_getApiVersion Git) _applyTypesToPolicy -item $resp @@ -28,7 +28,7 @@ function Get-VSTeamPolicy { } else { try { - $resp = _callAPI -ProjectName $ProjectName -Area policy -Resource configurations -Version $([VSTeamVersions]::Git) + $resp = _callAPI -ProjectName $ProjectName -Area policy -Resource configurations -Version $(_getApiVersion Git) # Apply a Type Name so we can use custom format view and custom type extensions foreach ($item in $resp.value) { diff --git a/Source/Public/Get-VSTeamPolicyType.ps1 b/Source/Public/Get-VSTeamPolicyType.ps1 index ec0a5ac39..d7f8e004a 100644 --- a/Source/Public/Get-VSTeamPolicyType.ps1 +++ b/Source/Public/Get-VSTeamPolicyType.ps1 @@ -16,7 +16,7 @@ function Get-VSTeamPolicyType { if ($id) { foreach ($item in $id) { try { - $resp = _callAPI -ProjectName $ProjectName -Id $item -Area policy -Resource types -Version $([VSTeamVersions]::Git) + $resp = _callAPI -ProjectName $ProjectName -Id $item -Area policy -Resource types -Version $(_getApiVersion Policy) _applyTypesToPolicyType -item $resp @@ -29,7 +29,7 @@ function Get-VSTeamPolicyType { } else { try { - $resp = _callAPI -ProjectName $ProjectName -Area policy -Resource types -Version $([VSTeamVersions]::Git) + $resp = _callAPI -ProjectName $ProjectName -Area policy -Resource types -Version $(_getApiVersion Policy) # Apply a Type Name so we can use custom format view and custom type extensions foreach ($item in $resp.value) { diff --git a/Source/Public/Get-VSTeamPool.ps1 b/Source/Public/Get-VSTeamPool.ps1 index 6f1bce9b8..02017e8ae 100644 --- a/Source/Public/Get-VSTeamPool.ps1 +++ b/Source/Public/Get-VSTeamPool.ps1 @@ -9,7 +9,7 @@ function Get-VSTeamPool { process { if ($id) { - $resp = _callAPI -Area distributedtask -Resource pools -Id $id -Version $([VSTeamVersions]::DistributedTask) + $resp = _callAPI -NoProject -Area distributedtask -Resource pools -Id $id -Version $(_getApiVersion DistributedTask) # Storing the object before you return it cleaned up the pipeline. # When I just write the object from the constructor each property @@ -19,7 +19,7 @@ function Get-VSTeamPool { Write-Output $item } else { - $resp = _callAPI -Area distributedtask -Resource pools -Version $([VSTeamVersions]::DistributedTask) + $resp = _callAPI -NoProject -Area distributedtask -Resource pools -Version $(_getApiVersion DistributedTask) $objs = @() diff --git a/Source/Public/Get-VSTeamProcess.ps1 b/Source/Public/Get-VSTeamProcess.ps1 index d5c5980ed..d78235c84 100644 --- a/Source/Public/Get-VSTeamProcess.ps1 +++ b/Source/Public/Get-VSTeamProcess.ps1 @@ -23,11 +23,11 @@ function Get-VSTeamProcess { $ProcessName = $PSBoundParameters["Name"] if ($id) { - $queryString = @{} + $queryString = @{ } # Call the REST API $resp = _callAPI -Area 'process/processes' -id $id ` - -Version $([VSTeamVersions]::Core) ` + -Version $(_getApiVersion Core) ` -QueryString $queryString $project = [VSTeamProcess]::new($resp) @@ -36,15 +36,14 @@ function Get-VSTeamProcess { } elseif ($ProcessName) { # Lookup Process ID by Name - Get-VSTeamProcess | where-object {$_.name -eq $ProcessName} - + Get-VSTeamProcess | where-object { $_.name -eq $ProcessName } } else { # Return list of processes try { # Call the REST API $resp = _callAPI -Area 'process/processes' ` - -Version $([VSTeamVersions]::Core) ` + -Version $(_getApiVersion Core) ` -QueryString @{ '$top' = $top '$skip' = $skip diff --git a/Source/Public/Get-VSTeamProject.ps1 b/Source/Public/Get-VSTeamProject.ps1 index ac521ab69..5e7940794 100644 --- a/Source/Public/Get-VSTeamProject.ps1 +++ b/Source/Public/Get-VSTeamProject.ps1 @@ -40,8 +40,8 @@ function Get-VSTeamProject { } # Call the REST API - $resp = _callAPI -Area 'projects' -id $ProjectName ` - -Version $([VSTeamVersions]::Core) ` + $resp = _callAPI -Area 'projects' -NoProject -id $ProjectName ` + -Version $(_getApiVersion Core) ` -QueryString $queryString # Storing the object before you return it cleaned up the pipeline. @@ -54,8 +54,8 @@ function Get-VSTeamProject { else { try { # Call the REST API - $resp = _callAPI -Area 'projects' ` - -Version $([VSTeamVersions]::Core) ` + $resp = _callAPI -Area 'projects' -NoProject ` + -Version $(_getApiVersion Core) ` -QueryString @{ stateFilter = $stateFilter '$top' = $top diff --git a/Source/Public/Get-VSTeamPullRequest.ps1 b/Source/Public/Get-VSTeamPullRequest.ps1 index df44155b8..af09cda73 100644 --- a/Source/Public/Get-VSTeamPullRequest.ps1 +++ b/Source/Public/Get-VSTeamPullRequest.ps1 @@ -4,36 +4,36 @@ function Get-VSTeamPullRequest { [Alias('PullRequestId')] [Parameter(ParameterSetName = "ById")] [string] $Id, - + [Parameter(ParameterSetName = "SearchCriteriaWithStatus")] [Parameter(ParameterSetName = "SearchCriteriaWithAll")] [Guid] $RepositoryId, - + [Parameter(ParameterSetName = "SearchCriteriaWithAll")] [Parameter(ParameterSetName = "SearchCriteriaWithStatus")] [Guid] $SourceRepositoryId, - + [Parameter(ParameterSetName = "SearchCriteriaWithAll")] [Parameter(ParameterSetName = "SearchCriteriaWithStatus")] [ValidatePattern('^refs/.*')] [string] $SourceBranchRef, - + [Parameter(ParameterSetName = "SearchCriteriaWithAll")] [Parameter(ParameterSetName = "SearchCriteriaWithStatus")] [ValidatePattern('^refs/.*')] [string] $TargetBranchRef, - + [Parameter(ParameterSetName = "SearchCriteriaWithStatus")] [ValidateSet("abandoned", "active", "all", "completed", "notSet")] [string] $Status, - + [Parameter(ParameterSetName = "SearchCriteriaWithAll")] [switch] $All, - + [Parameter(ParameterSetName = "SearchCriteriaWithAll")] [Parameter(ParameterSetName = "SearchCriteriaWithStatus")] [int] $Top, - + [Parameter(ParameterSetName = "SearchCriteriaWithAll")] [Parameter(ParameterSetName = "SearchCriteriaWithStatus")] [int] $Skip @@ -50,10 +50,10 @@ function Get-VSTeamPullRequest { try { if ($Id) { if ($ProjectName) { - $resp = _callAPI -ProjectName $ProjectName -Area git -Resource pullRequests -Version $([VSTeamVersions]::Git) -Id $Id + $resp = _callAPI -ProjectName $ProjectName -Area git -Resource pullRequests -Version $(_getApiVersion Git) -Id $Id } else { - $resp = _callAPI -Area git -Resource pullRequests -Version $([VSTeamVersions]::Git) -Id $Id + $resp = _callAPI -Area git -Resource pullRequests -Version $(_getApiVersion Git) -Id $Id } } else { @@ -68,18 +68,18 @@ function Get-VSTeamPullRequest { if ($RepositoryId) { if ($ProjectName) { - $resp = _callAPI -ProjectName $ProjectName -Id "$RepositoryId/pullRequests" -Area git -Resource repositories -Version $([VSTeamVersions]::Git) -QueryString $queryString + $resp = _callAPI -ProjectName $ProjectName -Id "$RepositoryId/pullRequests" -Area git -Resource repositories -Version $(_getApiVersion Git) -QueryString $queryString } else { - $resp = _callAPI -Id "$RepositoryId/pullRequests" -Area git -Resource repositories -Version $([VSTeamVersions]::Git) -QueryString $queryString + $resp = _callAPI -Id "$RepositoryId/pullRequests" -Area git -Resource repositories -Version $(_getApiVersion Git) -QueryString $queryString } } else { if ($ProjectName) { - $resp = _callAPI -ProjectName $ProjectName -Area git -Resource pullRequests -Version $([VSTeamVersions]::Git) -QueryString $queryString + $resp = _callAPI -ProjectName $ProjectName -Area git -Resource pullRequests -Version $(_getApiVersion Git) -QueryString $queryString } else { - $resp = _callAPI -Area git -Resource pullRequests -Version $([VSTeamVersions]::Git) -QueryString $queryString + $resp = _callAPI -Area git -Resource pullRequests -Version $(_getApiVersion Git) -QueryString $queryString } } } diff --git a/Source/Public/Get-VSTeamQueue.ps1 b/Source/Public/Get-VSTeamQueue.ps1 index 63f51bde1..d7823b950 100644 --- a/Source/Public/Get-VSTeamQueue.ps1 +++ b/Source/Public/Get-VSTeamQueue.ps1 @@ -21,7 +21,7 @@ function Get-VSTeamQueue { if ($id) { $resp = _callAPI -ProjectName $ProjectName -Id $id -Area distributedtask -Resource queues ` - -Version $([VSTeamVersions]::DistributedTask) + -Version $(_getApiVersion DistributedTask) $item = [VSTeamQueue]::new($resp, $ProjectName) @@ -29,7 +29,7 @@ function Get-VSTeamQueue { } else { $resp = _callAPI -ProjectName $projectName -Area distributedtask -Resource queues ` - -QueryString @{ queueName = $queueName; actionFilter = $actionFilter } -Version $([VSTeamVersions]::DistributedTask) + -QueryString @{ queueName = $queueName; actionFilter = $actionFilter } -Version $(_getApiVersion DistributedTask) $objs = @() diff --git a/Source/Public/Get-VSTeamRelease.ps1 b/Source/Public/Get-VSTeamRelease.ps1 index d79c5430d..46ea7e40f 100644 --- a/Source/Public/Get-VSTeamRelease.ps1 +++ b/Source/Public/Get-VSTeamRelease.ps1 @@ -55,7 +55,7 @@ function Get-VSTeamRelease { if ($id) { foreach ($item in $id) { - $resp = _callAPI -SubDomain vsrm -ProjectName $ProjectName -Area release -id $item -Resource releases -Version $([VSTeamVersions]::Release) + $resp = _callAPI -SubDomain vsrm -ProjectName $ProjectName -Area release -id $item -Resource releases -Version $(_getApiVersion Release) if ($JSON.IsPresent) { $resp | ConvertTo-Json -Depth 99 @@ -73,10 +73,10 @@ function Get-VSTeamRelease { } else { if ($ProjectName) { - $listurl = _buildRequestURI -SubDomain vsrm -ProjectName $ProjectName -Area release -Resource releases -Version $([VSTeamVersions]::Release) + $listurl = _buildRequestURI -SubDomain vsrm -ProjectName $ProjectName -Area release -Resource releases -Version $(_getApiVersion Release) } else { - $listurl = _buildRequestURI -SubDomain vsrm -Area release -Resource releases -Version $([VSTeamVersions]::Release) + $listurl = _buildRequestURI -SubDomain vsrm -Area release -Resource releases -Version $(_getApiVersion Release) } $QueryString = @{ diff --git a/Source/Public/Get-VSTeamReleaseDefinition.ps1 b/Source/Public/Get-VSTeamReleaseDefinition.ps1 index a3bc8cbe1..ea245de97 100644 --- a/Source/Public/Get-VSTeamReleaseDefinition.ps1 +++ b/Source/Public/Get-VSTeamReleaseDefinition.ps1 @@ -30,7 +30,7 @@ function Get-VSTeamReleaseDefinition { if ($id) { foreach ($item in $id) { - $resp = _callAPI -subDomain vsrm -Area release -resource definitions -Version $([VSTeamVersions]::Release) -projectName $projectName -id $item + $resp = _callAPI -subDomain vsrm -Area release -resource definitions -Version $(_getApiVersion Release) -projectName $projectName -id $item if ($JSON.IsPresent) { $resp | ConvertTo-Json -Depth 99 @@ -48,7 +48,7 @@ function Get-VSTeamReleaseDefinition { } } else { - $listurl = _buildRequestURI -subDomain vsrm -Area release -resource 'definitions' -Version $([VSTeamVersions]::Release) -projectName $ProjectName + $listurl = _buildRequestURI -subDomain vsrm -Area release -resource 'definitions' -Version $(_getApiVersion Release) -projectName $ProjectName if ($expand -ne 'none') { $listurl += "&`$expand=$($expand)" diff --git a/Source/Public/Get-VSTeamSecurityNamespace.ps1 b/Source/Public/Get-VSTeamSecurityNamespace.ps1 index fa850f475..41e8748cd 100644 --- a/Source/Public/Get-VSTeamSecurityNamespace.ps1 +++ b/Source/Public/Get-VSTeamSecurityNamespace.ps1 @@ -5,15 +5,7 @@ function Get-VSTeamSecurityNamespace { [string] $Name, [Parameter(ParameterSetName = 'ByNamespaceId', Mandatory = $true)] - [ValidateScript({ - try { - [System.Guid]::Parse($_) | Out-Null - $true - } catch { - $false - } - })] - [string] $Id, + [guid] $Id, [Parameter(ParameterSetName = 'List', Mandatory = $false)] [switch] $LocalOnly @@ -25,7 +17,7 @@ function Get-VSTeamSecurityNamespace { if ($Id) { # Call the REST API $resp = _callAPI -Area 'securitynamespaces' -id $Id ` - -Version $([VSTeamVersions]::Core) ` + -Version $(_getApiVersion Core) -NoProject ` } else { $queryString = @{} if ($LocalOnly.IsPresent) @@ -34,7 +26,7 @@ function Get-VSTeamSecurityNamespace { } $resp = _callAPI -Area 'securitynamespaces' ` - -Version $([VSTeamVersions]::Core) ` + -Version $(_getApiVersion Core) -NoProject ` -QueryString $queryString } diff --git a/Source/Public/Get-VSTeamServiceEndpoint.ps1 b/Source/Public/Get-VSTeamServiceEndpoint.ps1 index 38583af39..d35952a72 100644 --- a/Source/Public/Get-VSTeamServiceEndpoint.ps1 +++ b/Source/Public/Get-VSTeamServiceEndpoint.ps1 @@ -16,7 +16,7 @@ function Get-VSTeamServiceEndpoint { if ($id) { # Call the REST API $resp = _callAPI -Area 'distributedtask' -Resource 'serviceendpoints' -Id $id ` - -Version $([VSTeamVersions]::DistributedTask) -ProjectName $ProjectName + -Version $(_getApiVersion DistributedTask) -ProjectName $ProjectName _applyTypesToServiceEndpoint -item $resp @@ -25,7 +25,7 @@ function Get-VSTeamServiceEndpoint { else { # Call the REST API $resp = _callAPI -ProjectName $ProjectName -Area 'distributedtask' -Resource 'serviceendpoints' ` - -Version $([VSTeamVersions]::DistributedTask) + -Version $(_getApiVersion DistributedTask) # Apply a Type Name so we can use custom format view and custom type extensions foreach ($item in $resp.value) { diff --git a/Source/Public/Get-VSTeamServiceEndpointType.ps1 b/Source/Public/Get-VSTeamServiceEndpointType.ps1 index 86dbeb97d..e8e9afdc9 100644 --- a/Source/Public/Get-VSTeamServiceEndpointType.ps1 +++ b/Source/Public/Get-VSTeamServiceEndpointType.ps1 @@ -31,12 +31,12 @@ function Get-VSTeamServiceEndpointType { # Call the REST API $resp = _callAPI -Area 'distributedtask' -Resource 'serviceendpointtypes' ` - -Version $([VSTeamVersions]::DistributedTask) -body $body + -Version $(_getApiVersion DistributedTask) -body $body } else { # Call the REST API $resp = _callAPI -Area 'distributedtask' -Resource 'serviceendpointtypes' ` - -Version $([VSTeamVersions]::DistributedTask) + -Version $(_getApiVersion DistributedTask) } diff --git a/Source/Public/Get-VSTeamTaskGroup.ps1 b/Source/Public/Get-VSTeamTaskGroup.ps1 index 6f37aaa2e..1e955a4ca 100644 --- a/Source/Public/Get-VSTeamTaskGroup.ps1 +++ b/Source/Public/Get-VSTeamTaskGroup.ps1 @@ -18,12 +18,12 @@ function Get-VSTeamTaskGroup { $ProjectName = $PSBoundParameters["ProjectName"] if ($Id) { - $resp = _callAPI -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $([VSTeamVersions]::TaskGroups) -Id $Id -Method Get + $resp = _callAPI -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $(_getApiVersion TaskGroups) -Id $Id -Method Get Write-Output $resp.value } else { - $resp = _callAPI -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $([VSTeamVersions]::TaskGroups) -Method Get + $resp = _callAPI -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $(_getApiVersion TaskGroups) -Method Get if ($Name) { if ($resp.value) { diff --git a/Source/Public/Get-VSTeamTfvcBranch.ps1 b/Source/Public/Get-VSTeamTfvcBranch.ps1 index 2009c1bd9..237e27dc3 100644 --- a/Source/Public/Get-VSTeamTfvcBranch.ps1 +++ b/Source/Public/Get-VSTeamTfvcBranch.ps1 @@ -22,7 +22,7 @@ function Get-VSTeamTfvcBranch { includeDeleted = $IncludeDeleted; } - $resp = _callAPI -Area tfvc -Resource branches -Id $item -QueryString $queryString -Version $([VSTeamVersions]::Tfvc) + $resp = _callAPI -Area tfvc -Resource branches -Id $item -QueryString $queryString -Version $(_getApiVersion Tfvc) _applyTypesToTfvcBranch -item $resp diff --git a/Source/Public/Get-VSTeamTfvcRootBranch.ps1 b/Source/Public/Get-VSTeamTfvcRootBranch.ps1 index e21af1a52..d21cb6a94 100644 --- a/Source/Public/Get-VSTeamTfvcRootBranch.ps1 +++ b/Source/Public/Get-VSTeamTfvcRootBranch.ps1 @@ -14,7 +14,7 @@ function Get-VSTeamTfvcRootBranch { includeDeleted = $IncludeDeleted; } - $resp = _callAPI -Area tfvc -Resource branches -QueryString $queryString -Version $([VSTeamVersions]::Tfvc) + $resp = _callAPI -Area tfvc -Resource branches -QueryString $queryString -Version $(_getApiVersion Tfvc) if ($resp | Get-Member -Name value -MemberType Properties) { foreach ($item in $resp.value) { diff --git a/Source/Public/Get-VSTeamUser.ps1 b/Source/Public/Get-VSTeamUser.ps1 index 4786f432e..250fa517e 100644 --- a/Source/Public/Get-VSTeamUser.ps1 +++ b/Source/Public/Get-VSTeamUser.ps1 @@ -17,8 +17,8 @@ function Get-VSTeamUser { if ($Descriptor) { # Call the REST API $resp = _callAPI -Area 'graph' -Resource 'users' -id $Descriptor ` - -Version $([VSTeamVersions]::Graph) ` - -SubDomain 'vssps' + -Version $(_getApiVersion Graph) ` + -SubDomain 'vssps' -NoProject # Storing the object before you return it cleaned up the pipeline. # When I just write the object from the constructor each property @@ -37,9 +37,9 @@ function Get-VSTeamUser { try { # Call the REST API $resp = _callAPI -Area 'graph' -id 'users' ` - -Version $([VSTeamVersions]::Graph) ` + -Version $(_getApiVersion Graph) ` -QueryString $queryString ` - -SubDomain 'vssps' + -SubDomain 'vssps' -NoProject $objs = @() diff --git a/Source/Public/Get-VSTeamUserEntitlement.ps1 b/Source/Public/Get-VSTeamUserEntitlement.ps1 index c0768ff87..c396a6788 100644 --- a/Source/Public/Get-VSTeamUserEntitlement.ps1 +++ b/Source/Public/Get-VSTeamUserEntitlement.ps1 @@ -24,7 +24,7 @@ function Get-VSTeamUserEntitlement { foreach ($item in $Id) { # Build the url to return the single build # Call the REST API - $resp = _callAPI -SubDomain 'vsaex' -Version $([VSTeamVersions]::MemberEntitlementManagement) -Resource 'userentitlements' -id $item + $resp = _callAPI -SubDomain 'vsaex' -Version $(_getApiVersion MemberEntitlementManagement) -Resource 'userentitlements' -id $item _applyTypesToUser -item $resp @@ -35,7 +35,7 @@ function Get-VSTeamUserEntitlement { # Build the url to list the teams # $listurl = _buildUserURL $listurl = _buildRequestURI -SubDomain 'vsaex' -Resource 'userentitlements' ` - -Version $([VSTeamVersions]::MemberEntitlementManagement) + -Version $(_getApiVersion MemberEntitlementManagement) $listurl += _appendQueryString -name "top" -value $top -retainZero $listurl += _appendQueryString -name "skip" -value $skip -retainZero diff --git a/Source/Public/Get-VSTeamVariableGroup.ps1 b/Source/Public/Get-VSTeamVariableGroup.ps1 index 33643f8c2..39ff56daa 100644 --- a/Source/Public/Get-VSTeamVariableGroup.ps1 +++ b/Source/Public/Get-VSTeamVariableGroup.ps1 @@ -19,7 +19,7 @@ function Get-VSTeamVariableGroup { if ($Id) { # Call the REST API $resp = _callAPI -ProjectName $ProjectName -Area 'distributedtask' -Resource 'variablegroups' ` - -Version $([VSTeamVersions]::VariableGroups) -Id $Id + -Version $(_getApiVersion VariableGroups) -Id $Id _applyTypesToVariableGroup -item $resp @@ -27,8 +27,8 @@ function Get-VSTeamVariableGroup { } else { if ($Name) { - $resp = _callAPI -ProjectName $ProjectName -Area 'distributedtask' -Resource 'variablegroups' -Version $([VSTeamVersions]::VariableGroups) -Method Get ` - -QueryString @{groupName = $Name} + $resp = _callAPI -ProjectName $ProjectName -Area 'distributedtask' -Resource 'variablegroups' -Version $(_getApiVersion VariableGroups) -Method Get ` + -QueryString @{groupName = $Name } _applyTypesToVariableGroup -item $resp.value @@ -37,7 +37,7 @@ function Get-VSTeamVariableGroup { else { # Call the REST API $resp = _callAPI -ProjectName $ProjectName -Area 'distributedtask' -Resource 'variablegroups' ` - -Version $([VSTeamVersions]::VariableGroups) + -Version $(_getApiVersion VariableGroups) # Apply a Type Name so we can use custom format view and custom type extensions foreach ($item in $resp.value) { diff --git a/Source/Public/Get-VSTeamWiql.ps1 b/Source/Public/Get-VSTeamWiql.ps1 index 3603a018d..8506c6d6a 100644 --- a/Source/Public/Get-VSTeamWiql.ps1 +++ b/Source/Public/Get-VSTeamWiql.ps1 @@ -40,13 +40,13 @@ function Get-VSTeamWiql { $resp = _callAPI -ProjectName $ProjectName -Team $Team -Area 'wit' -Resource 'wiql' ` -method "POST" -ContentType "application/json" ` - -Version $([VSTeamVersions]::Core) ` + -Version $(_getApiVersion Core) ` -Querystring $QueryString ` -Body $body } else { $resp = _callAPI -ProjectName $ProjectName -Team $Team -Area 'wit' -Resource 'wiql' ` - -Version $([VSTeamVersions]::Core) -id "$Id" ` + -Version $(_getApiVersion Core) -id "$Id" ` -Querystring $QueryString } diff --git a/Source/Public/Get-VSTeamWorkItem.ps1 b/Source/Public/Get-VSTeamWorkItem.ps1 index 2bcae76b6..0f92c5b8c 100644 --- a/Source/Public/Get-VSTeamWorkItem.ps1 +++ b/Source/Public/Get-VSTeamWorkItem.ps1 @@ -17,8 +17,8 @@ function Get-VSTeamWorkItem { Process { # Call the REST API if ($Id.Length -gt 1) { - $resp = _callAPI -Area 'wit' -Resource 'workitems' ` - -Version $([VSTeamVersions]::Core) ` + $resp = _callAPI -NoProject -Area 'wit' -Resource 'workitems' ` + -Version $(_getApiVersion Core) ` -Querystring @{ '$Expand' = $Expand fields = ($Fields -join ',') @@ -34,8 +34,8 @@ function Get-VSTeamWorkItem { } else { $a = $Id[0] - $resp = _callAPI -Area 'wit' -Resource 'workitems' ` - -Version $([VSTeamVersions]::Core) -id "$a" ` + $resp = _callAPI -NoProject -Area 'wit' -Resource 'workitems' ` + -Version $(_getApiVersion Core) -id "$a" ` -Querystring @{ '$Expand' = $Expand fields = ($Fields -join ',') diff --git a/Source/Public/Get-VSTeamWorkItemType.ps1 b/Source/Public/Get-VSTeamWorkItemType.ps1 index a0938f301..62818e521 100644 --- a/Source/Public/Get-VSTeamWorkItemType.ps1 +++ b/Source/Public/Get-VSTeamWorkItemType.ps1 @@ -33,7 +33,7 @@ function Get-VSTeamWorkItemType { # Call the REST API if ($WorkItemType) { $resp = _callAPI -ProjectName $ProjectName -Area 'wit' -Resource 'workitemtypes' ` - -Version $([VSTeamVersions]::Core) -id $WorkItemType + -Version $(_getApiVersion Core) -id $WorkItemType # This call returns JSON with "": which causes the ConvertFrom-Json to fail. # To replace all the "": with "_end": @@ -45,7 +45,7 @@ function Get-VSTeamWorkItemType { } else { $resp = _callAPI -ProjectName $ProjectName -Area 'wit' -Resource 'workitemtypes' ` - -Version $([VSTeamVersions]::Core) + -Version $(_getApiVersion Core) # This call returns JSON with "": which causes the ConvertFrom-Json to fail. # To replace all the "": with "_end": diff --git a/Source/Public/Remove-VSTeam.ps1 b/Source/Public/Remove-VSTeam.ps1 index 1ac277344..8b90df0cd 100644 --- a/Source/Public/Remove-VSTeam.ps1 +++ b/Source/Public/Remove-VSTeam.ps1 @@ -18,7 +18,7 @@ function Remove-VSTeam { if ($Force -or $PSCmdlet.ShouldProcess($Id, "Delete team")) { # Call the REST API _callAPI -Area 'projects' -Resource "$ProjectName/teams" -Id $Id ` - -Method Delete -Version $([VSTeamVersions]::Core) | Out-Null + -Method Delete -Version $(_getApiVersion Core) | Out-Null Write-Output "Deleted team $Id" } diff --git a/Source/Public/Remove-VSTeamAccessControlList.ps1 b/Source/Public/Remove-VSTeamAccessControlList.ps1 index 71b0ce660..64001e89e 100644 --- a/Source/Public/Remove-VSTeamAccessControlList.ps1 +++ b/Source/Public/Remove-VSTeamAccessControlList.ps1 @@ -5,15 +5,7 @@ function Remove-VSTeamAccessControlList { [VSTeamSecurityNamespace] $SecurityNamespace, [Parameter(ParameterSetName = 'ByNamespaceId', Mandatory = $true)] - [ValidateScript({ - try { - [System.Guid]::Parse($_) | Out-Null - $true - } catch { - $false - } - })] - [string] $SecurityNamespaceId, + [guid] $SecurityNamespaceId, [Parameter(ParameterSetName = 'ByNamespace', Mandatory = $true)] [Parameter(ParameterSetName = 'ByNamespaceId', Mandatory = $true)] @@ -46,7 +38,7 @@ function Remove-VSTeamAccessControlList { if ($Force -or $pscmdlet.ShouldProcess($queryString.tokens, "Delete ACL")) { # Call the REST API $resp = _callAPI -Area 'accesscontrollists' -id $SecurityNamespaceId -method DELETE ` - -Version $([VSTeamVersions]::Core) ` + -Version $(_getApiVersion Core) ` -QueryString $queryString Write-Output $resp diff --git a/Source/Public/Remove-VSTeamAgent.ps1 b/Source/Public/Remove-VSTeamAgent.ps1 index dfebafe34..34a5a098d 100644 --- a/Source/Public/Remove-VSTeamAgent.ps1 +++ b/Source/Public/Remove-VSTeamAgent.ps1 @@ -16,7 +16,7 @@ function Remove-VSTeamAgent { foreach ($item in $Id) { if ($force -or $pscmdlet.ShouldProcess($item,"Delete agent")) { try { - _callAPI -Method Delete -Area "distributedtask/pools/$PoolId" -Resource agents -Id $item -Version $([VSTeamVersions]::DistributedTask) | Out-Null + _callAPI -Method Delete -Area "distributedtask/pools/$PoolId" -Resource agents -Id $item -Version $(_getApiVersion DistributedTask) | Out-Null Write-Output "Deleted agent $item" } catch { diff --git a/Source/Public/Remove-VSTeamBuild.ps1 b/Source/Public/Remove-VSTeamBuild.ps1 index 91e9924f6..fa5fce366 100644 --- a/Source/Public/Remove-VSTeamBuild.ps1 +++ b/Source/Public/Remove-VSTeamBuild.ps1 @@ -20,7 +20,7 @@ function Remove-VSTeamBuild { if ($Force -or $pscmdlet.ShouldProcess($item, "Delete Build")) { try { _callAPI -ProjectName $ProjectName -Area 'build' -Resource 'builds' -id $item ` - -Method Delete -Version $([VSTeamVersions]::Build) | Out-Null + -Method Delete -Version $(_getApiVersion Build) | Out-Null Write-Output "Deleted build $item" } diff --git a/Source/Public/Remove-VSTeamBuildDefinition.ps1 b/Source/Public/Remove-VSTeamBuildDefinition.ps1 index cb1f32168..cc34659ad 100644 --- a/Source/Public/Remove-VSTeamBuildDefinition.ps1 +++ b/Source/Public/Remove-VSTeamBuildDefinition.ps1 @@ -19,7 +19,7 @@ function Remove-VSTeamBuildDefinition { foreach ($item in $id) { if ($Force -or $pscmdlet.ShouldProcess($item, "Delete Build Definition")) { # Call the REST API - _callAPI -Method Delete -ProjectName $ProjectName -Area build -Resource definitions -Id $item -Version $([VSTeamVersions]::Build) | Out-Null + _callAPI -Method Delete -ProjectName $ProjectName -Area build -Resource definitions -Id $item -Version $(_getApiVersion Build) | Out-Null Write-Output "Deleted build definition $item" } diff --git a/Source/Public/Remove-VSTeamBuildTag.ps1 b/Source/Public/Remove-VSTeamBuildTag.ps1 index 62868a58b..be4d2f31f 100644 --- a/Source/Public/Remove-VSTeamBuildTag.ps1 +++ b/Source/Public/Remove-VSTeamBuildTag.ps1 @@ -23,7 +23,7 @@ function Remove-VSTeamBuildTag { foreach ($tag in $tags) { # Call the REST API _callAPI -ProjectName $projectName -Area 'build' -Resource "builds/$Id/tags" ` - -Method Delete -Querystring @{tag = $tag} -Version $([VSTeamVersions]::Build) | Out-Null + -Method Delete -Querystring @{tag = $tag} -Version $(_getApiVersion Build) | Out-Null } } } diff --git a/Source/Public/Remove-VSTeamExtension.ps1 b/Source/Public/Remove-VSTeamExtension.ps1 index cbc7649d3..a0a056d44 100644 --- a/Source/Public/Remove-VSTeamExtension.ps1 +++ b/Source/Public/Remove-VSTeamExtension.ps1 @@ -13,7 +13,7 @@ function Remove-VSTeamExtension { if ($Force -or $pscmdlet.ShouldProcess($ExtensionId, "Remove extension")) { $resource = "extensionmanagement/installedextensionsbyname/$PublisherId/$ExtensionId" - $resp = _callAPI -Method Delete -SubDomain 'extmgmt' -Resource $resource -Version $([VSTeamVersions]::ExtensionsManagement) + $resp = _callAPI -NoProject -Method Delete -SubDomain 'extmgmt' -Resource $resource -Version $(_getApiVersion ExtensionsManagement) Write-Output $resp } diff --git a/Source/Public/Remove-VSTeamFeed.ps1 b/Source/Public/Remove-VSTeamFeed.ps1 index f9deeaeda..ff4dd67d7 100644 --- a/Source/Public/Remove-VSTeamFeed.ps1 +++ b/Source/Public/Remove-VSTeamFeed.ps1 @@ -17,7 +17,7 @@ function Remove-VSTeamFeed { if ($Force -or $pscmdlet.ShouldProcess($item, "Delete Package Feed")) { # Call the REST API - _callAPI -subDomain feeds -Method Delete -Id $item -Area packaging -Resource feeds -Version $([VSTeamVersions]::Packaging) | Out-Null + _callAPI -NoProject -subDomain feeds -Method Delete -Id $item -Area packaging -Resource feeds -Version $(_getApiVersion Packaging) | Out-Null Write-Output "Deleted Feed $item" } diff --git a/Source/Public/Remove-VSTeamGitRepository.ps1 b/Source/Public/Remove-VSTeamGitRepository.ps1 index 73aa3f986..3fbdb5281 100644 --- a/Source/Public/Remove-VSTeamGitRepository.ps1 +++ b/Source/Public/Remove-VSTeamGitRepository.ps1 @@ -10,7 +10,7 @@ function Remove-VSTeamGitRepository { foreach ($item in $id) { if ($Force -or $pscmdlet.ShouldProcess($item, "Delete Repository")) { try { - _callAPI -Method Delete -Id $item -Area git -Resource repositories -Version $([VSTeamVersions]::Git) | Out-Null + _callAPI -Method Delete -Id $item -Area git -Resource repositories -Version $(_getApiVersion Git) | Out-Null Write-Output "Deleted repository $item" } diff --git a/Source/Public/Remove-VSTeamPolicy.ps1 b/Source/Public/Remove-VSTeamPolicy.ps1 index eaaf79b09..d1bb59100 100644 --- a/Source/Public/Remove-VSTeamPolicy.ps1 +++ b/Source/Public/Remove-VSTeamPolicy.ps1 @@ -17,7 +17,7 @@ function Remove-VSTeamPolicy { foreach ($item in $id) { if ($Force -or $pscmdlet.ShouldProcess($item, "Delete Policy")) { try { - _callAPI -ProjectName $ProjectName -Method Delete -Id $item -Area policy -Resource configurations -Version $([VSTeamVersions]::Git) | Out-Null + _callAPI -ProjectName $ProjectName -Method Delete -Id $item -Area policy -Resource configurations -Version $(_getApiVersion Git) | Out-Null Write-Output "Deleted policy $item" } diff --git a/Source/Public/Remove-VSTeamProject.ps1 b/Source/Public/Remove-VSTeamProject.ps1 index da7c86d5b..b4e918afd 100644 --- a/Source/Public/Remove-VSTeamProject.ps1 +++ b/Source/Public/Remove-VSTeamProject.ps1 @@ -15,7 +15,7 @@ function Remove-VSTeamProject { if ($Force -or $pscmdlet.ShouldProcess($ProjectName, "Delete Project")) { # Call the REST API $resp = _callAPI -Area 'projects' -Id (Get-VSTeamProject $ProjectName).id ` - -Method Delete -Version $([VSTeamVersions]::Core) + -Method Delete -Version $(_getApiVersion Core) _trackProjectProgress -resp $resp -title 'Deleting team project' -msg "Deleting $ProjectName" diff --git a/Source/Public/Remove-VSTeamRelease.ps1 b/Source/Public/Remove-VSTeamRelease.ps1 index 6f9714f8e..99922877a 100644 --- a/Source/Public/Remove-VSTeamRelease.ps1 +++ b/Source/Public/Remove-VSTeamRelease.ps1 @@ -24,7 +24,7 @@ function Remove-VSTeamRelease { try { # Call the REST API - _callAPI -Method Delete -SubDomain vsrm -Area release -Resource releases -ProjectName $ProjectName -id $item -Version $([VSTeamVersions]::Release) | Out-Null + _callAPI -Method Delete -SubDomain vsrm -Area release -Resource releases -ProjectName $ProjectName -id $item -Version $(_getApiVersion Release) | Out-Null Write-Output "Deleted release $item" } diff --git a/Source/Public/Remove-VSTeamReleaseDefinition.ps1 b/Source/Public/Remove-VSTeamReleaseDefinition.ps1 index af67a9ab1..39f10002a 100644 --- a/Source/Public/Remove-VSTeamReleaseDefinition.ps1 +++ b/Source/Public/Remove-VSTeamReleaseDefinition.ps1 @@ -20,7 +20,7 @@ function Remove-VSTeamReleaseDefinition { foreach ($item in $id) { if ($force -or $pscmdlet.ShouldProcess($item, "Delete Release Definition")) { - _callAPI -Method Delete -subDomain vsrm -Area release -Resource definitions -Version $([VSTeamVersions]::Release) -projectName $ProjectName -id $item | Out-Null + _callAPI -Method Delete -subDomain vsrm -Area release -Resource definitions -Version $(_getApiVersion Release) -projectName $ProjectName -id $item | Out-Null Write-Output "Deleted release definition $item" } diff --git a/Source/Public/Remove-VSTeamServiceEndpoint.ps1 b/Source/Public/Remove-VSTeamServiceEndpoint.ps1 index fd716c9f6..21f64daed 100644 --- a/Source/Public/Remove-VSTeamServiceEndpoint.ps1 +++ b/Source/Public/Remove-VSTeamServiceEndpoint.ps1 @@ -19,7 +19,7 @@ function Remove-VSTeamServiceEndpoint { if ($Force -or $pscmdlet.ShouldProcess($item, "Delete Service Endpoint")) { # Call the REST API _callAPI -projectName $projectName -Area 'distributedtask' -Resource 'serviceendpoints' -Id $item ` - -Method Delete -Version $([VSTeamVersions]::DistributedTask) | Out-Null + -Method Delete -Version $(_getApiVersion DistributedTask) | Out-Null Write-Output "Deleted service endpoint $item" } diff --git a/Source/Public/Remove-VSTeamTaskGroup.ps1 b/Source/Public/Remove-VSTeamTaskGroup.ps1 index 4cf4321fc..4a8cba9d1 100644 --- a/Source/Public/Remove-VSTeamTaskGroup.ps1 +++ b/Source/Public/Remove-VSTeamTaskGroup.ps1 @@ -18,7 +18,7 @@ function Remove-VSTeamTaskGroup { foreach ($item in $Id) { if ($Force -or $pscmdlet.ShouldProcess($item, "Delete Task Group")) { # Call the REST API - _callAPI -Method Delete -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $([VSTeamVersions]::TaskGroups) -Id $item | Out-Null + _callAPI -Method Delete -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $(_getApiVersion TaskGroups) -Id $item | Out-Null Write-Output "Deleted task group $item" } diff --git a/Source/Public/Remove-VSTeamUserEntitlement.ps1 b/Source/Public/Remove-VSTeamUserEntitlement.ps1 index e60a7f8bc..cf4a6466b 100644 --- a/Source/Public/Remove-VSTeamUserEntitlement.ps1 +++ b/Source/Public/Remove-VSTeamUserEntitlement.ps1 @@ -32,7 +32,7 @@ function Remove-VSTeamUserEntitlement { if ($Force -or $PSCmdlet.ShouldProcess("$($user.userName) ($($user.email))", "Delete user")) { # Call the REST API - _callAPI -Method Delete -SubDomain 'vsaex' -Resource 'userentitlements' -Id $Id -Version $([VSTeamVersions]::MemberEntitlementManagement) | Out-Null + _callAPI -Method Delete -SubDomain 'vsaex' -Resource 'userentitlements' -Id $Id -Version $(_getApiVersion MemberEntitlementManagement) | Out-Null Write-Output "Deleted user $($user.userName) ($($user.email))" } diff --git a/Source/Public/Remove-VSTeamVariableGroup.ps1 b/Source/Public/Remove-VSTeamVariableGroup.ps1 index 609f24fa3..999a11d6f 100644 --- a/Source/Public/Remove-VSTeamVariableGroup.ps1 +++ b/Source/Public/Remove-VSTeamVariableGroup.ps1 @@ -19,7 +19,7 @@ function Remove-VSTeamVariableGroup { if ($Force -or $pscmdlet.ShouldProcess($item, "Delete Variable Group")) { # Call the REST API _callAPI -projectName $projectName -Area 'distributedtask' -Resource 'variablegroups' -Id $item ` - -Method Delete -Version $([VSTeamVersions]::VariableGroups) | Out-Null + -Method Delete -Version $(_getApiVersion VariableGroups) | Out-Null Write-Output "Deleted variable group $item" } diff --git a/Source/Public/Remove-VSTeamWorkItem.ps1 b/Source/Public/Remove-VSTeamWorkItem.ps1 index e2b73cc7f..d449e1e0a 100644 --- a/Source/Public/Remove-VSTeamWorkItem.ps1 +++ b/Source/Public/Remove-VSTeamWorkItem.ps1 @@ -15,7 +15,7 @@ function Remove-VSTeamWorkItem { if ($Force -or $pscmdlet.ShouldProcess($item, "Delete Work Item")) { try { _callAPI -Method Delete -Area wit -Resource workitems ` - -Version $([VSTeamVersions]::Core) -id $item ` + -Version $(_getApiVersion Core) -id $item ` -Querystring @{ destroy = $Destroy } | Out-Null diff --git a/Source/Public/Set-VSTeamAPIVersion.ps1 b/Source/Public/Set-VSTeamAPIVersion.ps1 index e3bf5fb92..4175a6ace 100644 --- a/Source/Public/Set-VSTeamAPIVersion.ps1 +++ b/Source/Public/Set-VSTeamAPIVersion.ps1 @@ -6,7 +6,7 @@ function Set-VSTeamAPIVersion { [string] $Target = 'TFS2017', [parameter(ParameterSetName = 'Service', Mandatory = $true, Position = 0)] - [ValidateSet('Build', 'Release', 'Core', 'Git', 'DistributedTask', 'VariableGroups', 'Tfvc', 'Packaging', 'MemberEntitlementManagement', 'ExtensionsManagement', 'ServiceFabricEndpoint', 'Graph', 'TaskGroups')] + [ValidateSet('Build', 'Release', 'Core', 'Git', 'DistributedTask', 'VariableGroups', 'Tfvc', 'Packaging', 'MemberEntitlementManagement', 'ExtensionsManagement', 'ServiceFabricEndpoint', 'Graph', 'TaskGroups', 'Policy')] [string] $Service, [parameter(ParameterSetName = 'Service', Mandatory = $true, Position = 1)] @@ -57,6 +57,9 @@ function Set-VSTeamAPIVersion { 'TaskGroups' { [VSTeamVersions]::TaskGroups = $Version } + 'Policy' { + [VSTeamVersions]::Policy = $Version + } } } else { @@ -75,6 +78,7 @@ function Set-VSTeamAPIVersion { [VSTeamVersions]::ServiceFabricEndpoint = '5.0' [VSTeamVersions]::ExtensionsManagement = '5.0' [VSTeamVersions]::Graph = '' + [VSTeamVersions]::Policy = '5.0' } 'TFS2018' { [VSTeamVersions]::Version = 'TFS2018' @@ -91,6 +95,7 @@ function Set-VSTeamAPIVersion { [VSTeamVersions]::ServiceFabricEndpoint = '3.2' [VSTeamVersions]::ExtensionsManagement = '3.2-preview' [VSTeamVersions]::Graph = '' + [VSTeamVersions]::Policy = '' } 'TFS2017' { [VSTeamVersions]::Version = 'TFS2017' @@ -107,6 +112,7 @@ function Set-VSTeamAPIVersion { [VSTeamVersions]::ServiceFabricEndpoint = '' [VSTeamVersions]::ExtensionsManagement = '3.0-preview' [VSTeamVersions]::Graph = '' + [VSTeamVersions]::Policy = '' } Default { [VSTeamVersions]::Version = $Target @@ -126,6 +132,7 @@ function Set-VSTeamAPIVersion { [VSTeamVersions]::ServiceFabricEndpoint = '5.0-preview' [VSTeamVersions]::ExtensionsManagement = '5.1-preview' [VSTeamVersions]::Graph = '5.1-preview' + [VSTeamVersions]::Policy = '5.1' } } } @@ -139,10 +146,11 @@ function Set-VSTeamAPIVersion { Write-Verbose "DistributedTask: $([VSTeamVersions]::DistributedTask)" Write-Verbose "VariableGroups: $([VSTeamVersions]::VariableGroups)" Write-Verbose "Tfvc: $([VSTeamVersions]::Tfvc)" - Write-Verbose "Packaging: $([VSTeamVersions]::Packaging)" + Write-Verbose "Packaging: $(_getApiVersion Packaging)" Write-Verbose "TaskGroups: $([VSTeamVersions]::TaskGroups)" Write-Verbose "MemberEntitlementManagement: $([VSTeamVersions]::MemberEntitlementManagement)" Write-Verbose "ServiceFabricEndpoint: $([VSTeamVersions]::ServiceFabricEndpoint)" Write-Verbose "ExtensionsManagement: $([VSTeamVersions]::ExtensionsManagement)" Write-Verbose "Graph: $([VSTeamVersions]::Graph)" + Write-Verbose "Policy: $([VSTeamVersions]::Policy)" } \ No newline at end of file diff --git a/Source/Public/Set-VSTeamApproval.ps1 b/Source/Public/Set-VSTeamApproval.ps1 index 44084e846..a3c53a04f 100644 --- a/Source/Public/Set-VSTeamApproval.ps1 +++ b/Source/Public/Set-VSTeamApproval.ps1 @@ -36,7 +36,7 @@ function Set-VSTeamApproval { try { # Call the REST API _callAPI -Method Patch -SubDomain vsrm -ProjectName $ProjectName -Area release -Resource approvals ` - -Id $item -Version $([VSTeamVersions]::Release) -body $body -ContentType 'application/json' | Out-Null + -Id $item -Version $(_getApiVersion Release) -body $body -ContentType 'application/json' | Out-Null Write-Output "Approval $item status changed to $status" } diff --git a/Source/Public/Set-VSTeamEnvironmentStatus.ps1 b/Source/Public/Set-VSTeamEnvironmentStatus.ps1 index 1efd4c20e..9dd7967ff 100644 --- a/Source/Public/Set-VSTeamEnvironmentStatus.ps1 +++ b/Source/Public/Set-VSTeamEnvironmentStatus.ps1 @@ -40,7 +40,7 @@ function Set-VSTeamEnvironmentStatus { try { # Call the REST API _callAPI -Method Patch -SubDomain vsrm -Area release -Resource "releases/$ReleaseId/environments" -projectName $ProjectName -id $item ` - -body $body -ContentType 'application/json' -Version $([VSTeamVersions]::Release) | Out-Null + -body $body -ContentType 'application/json' -Version $(_getApiVersion Release) | Out-Null Write-Output "Environment $item status changed to $status" } diff --git a/Source/Public/Set-VSTeamPermissionInheritance.ps1 b/Source/Public/Set-VSTeamPermissionInheritance.ps1 index 77b476265..36f8700af 100644 --- a/Source/Public/Set-VSTeamPermissionInheritance.ps1 +++ b/Source/Public/Set-VSTeamPermissionInheritance.ps1 @@ -53,7 +53,7 @@ } "@ # Call the REST API to change the inheritance state - $resp = _callAPI -method POST -area "Contribution" -resource "HierarchyQuery" -id $projectID -Version $version -ContentType "application/json" -Body $body + $resp = _callAPI -NoProject -method POST -area "Contribution" -resource "HierarchyQuery" -id $projectID -Version $version -ContentType "application/json" -Body $body } Write-Verbose "Result: $(ConvertTo-Json -InputObject $resp -Depth 100)" diff --git a/Source/Public/Set-VSTeamReleaseStatus.ps1 b/Source/Public/Set-VSTeamReleaseStatus.ps1 index c4f864f70..41c2e8128 100644 --- a/Source/Public/Set-VSTeamReleaseStatus.ps1 +++ b/Source/Public/Set-VSTeamReleaseStatus.ps1 @@ -30,7 +30,7 @@ function Set-VSTeamReleaseStatus { try { # Call the REST API _callAPI -Method Patch -SubDomain vsrm -Area release -Resource releases -projectName $ProjectName -id $item ` - -body $body -ContentType 'application/json' -Version $([VSTeamVersions]::Release) | Out-Null + -body $body -ContentType 'application/json' -Version $(_getApiVersion Release) | Out-Null Write-Output "Release $item status changed to $status" } diff --git a/Source/Public/Test-VSTeamYamlPipeline.ps1 b/Source/Public/Test-VSTeamYamlPipeline.ps1 index eaf6bc585..555eb9f73 100644 --- a/Source/Public/Test-VSTeamYamlPipeline.ps1 +++ b/Source/Public/Test-VSTeamYamlPipeline.ps1 @@ -32,7 +32,7 @@ function Test-VSTeamYamlPipeline { # Call the REST API $resp = _callAPI -ProjectName $ProjectName -Area 'pipelines' -Resource "$PipelineId" -id "runs" ` -Method Post -ContentType 'application/json; charset=utf-8' -Body ($body | ConvertTo-Json) ` - -Version $([VSTeamVersions]::Build) + -Version $(_getApiVersion Build) } catch { if ($PSItem -match 'PipelineValidationException') { diff --git a/Source/Public/Update-VSTeam.ps1 b/Source/Public/Update-VSTeam.ps1 index 3772005bb..338b81ecd 100644 --- a/Source/Public/Update-VSTeam.ps1 +++ b/Source/Public/Update-VSTeam.ps1 @@ -33,7 +33,7 @@ function Update-VSTeam { # Call the REST API $resp = _callAPI -Area 'projects' -Resource "$ProjectName/teams" -Id $Name ` - -Method Patch -ContentType 'application/json' -Body $body -Version $([VSTeamVersions]::Core) + -Method Patch -ContentType 'application/json' -Body $body -Version $(_getApiVersion Core) # Storing the object before you return it cleaned up the pipeline. # When I just write the object from the constructor each property diff --git a/Source/Public/Update-VSTeamBuild.ps1 b/Source/Public/Update-VSTeamBuild.ps1 index 065b623cb..d96c62221 100644 --- a/Source/Public/Update-VSTeamBuild.ps1 +++ b/Source/Public/Update-VSTeamBuild.ps1 @@ -43,7 +43,7 @@ function Update-VSTeamBuild { # Call the REST API _callAPI -ProjectName $ProjectName -Area 'build' -Resource 'builds' -Id $Id ` - -Method Patch -ContentType 'application/json' -body $body -Version $([VSTeamVersions]::Build) | Out-Null + -Method Patch -ContentType 'application/json' -body $body -Version $(_getApiVersion Build) | Out-Null } } } \ No newline at end of file diff --git a/Source/Public/Update-VSTeamBuildDefinition.ps1 b/Source/Public/Update-VSTeamBuildDefinition.ps1 index bb3dfba4c..3864f156d 100644 --- a/Source/Public/Update-VSTeamBuildDefinition.ps1 +++ b/Source/Public/Update-VSTeamBuildDefinition.ps1 @@ -26,7 +26,7 @@ function Update-VSTeamBuildDefinition { # Call the REST API if ($InFile) { - _callAPI -Method Put -ProjectName $ProjectName -Area build -Resource definitions -Id $Id -Version $([VSTeamVersions]::Build) -InFile $InFile -ContentType 'application/json' | Out-Null + _callAPI -Method Put -ProjectName $ProjectName -Area build -Resource definitions -Id $Id -Version $(_getApiVersion Build) -InFile $InFile -ContentType 'application/json' | Out-Null } else { _callAPI -Method Put -ProjectName $ProjectName -Area build -Resource definitions -Id $Id -Version $([VSTeamVersions]::Build) -Body $BuildDefinition -ContentType 'application/json' | Out-Null diff --git a/Source/Public/Update-VSTeamPolicy.ps1 b/Source/Public/Update-VSTeamPolicy.ps1 index 7f4fc8d60..e80ec37c0 100644 --- a/Source/Public/Update-VSTeamPolicy.ps1 +++ b/Source/Public/Update-VSTeamPolicy.ps1 @@ -42,7 +42,7 @@ function Update-VSTeamPolicy { if ($Force -or $pscmdlet.ShouldProcess($id, "Update Policy")) { # Call the REST API $resp = _callAPI -ProjectName $ProjectName -Area 'policy' -id $id -Resource 'configurations' ` - -Method Put -ContentType 'application/json' -Body $body -Version $([VSTeamVersions]::Git) + -Method Put -ContentType 'application/json' -Body $body -Version $(_getApiVersion Git) Write-Output $resp } diff --git a/Source/Public/Update-VSTeamProject.ps1 b/Source/Public/Update-VSTeamProject.ps1 index 1fd4c7e18..d30fbf05e 100644 --- a/Source/Public/Update-VSTeamProject.ps1 +++ b/Source/Public/Update-VSTeamProject.ps1 @@ -51,8 +51,8 @@ function Update-VSTeamProject { } # Call the REST API - $resp = _callAPI -Area 'projects' -id $id ` - -Method Patch -ContentType 'application/json' -body $body -Version $([VSTeamVersions]::Core) + $resp = _callAPI -Area 'projects' -id $id -NoProject ` + -Method Patch -ContentType 'application/json' -body $body -Version $(_getApiVersion Core) _trackProjectProgress -resp $resp -title 'Updating team project' -msg $msg diff --git a/Source/Public/Update-VSTeamPullRequest.ps1 b/Source/Public/Update-VSTeamPullRequest.ps1 index adc6b8c7e..213e9ab3f 100644 --- a/Source/Public/Update-VSTeamPullRequest.ps1 +++ b/Source/Public/Update-VSTeamPullRequest.ps1 @@ -50,7 +50,7 @@ function Update-VSTeamPullRequest { # Call the REST API $resp = _callAPI -Area git -Resource repositories -iD "$RepositoryId/pullrequests/$PullRequestId" ` - -Method Patch -ContentType 'application/json' -body $body -Version $([VSTeamVersions]::Git) + -Method Patch -ContentType 'application/json' -body $body -Version $(_getApiVersion Git) _applyTypesToPullRequests -item $resp diff --git a/Source/Public/Update-VSTeamRelease.ps1 b/Source/Public/Update-VSTeamRelease.ps1 index e72685e15..e26aba8d5 100644 --- a/Source/Public/Update-VSTeamRelease.ps1 +++ b/Source/Public/Update-VSTeamRelease.ps1 @@ -23,7 +23,7 @@ function Update-VSTeamRelease { if ($Force -or $pscmdlet.ShouldProcess($Id, "Update Release")) { # Call the REST API $resp = _callAPI -ProjectName $projectName -SubDomain vsrm -Area release -Resource releases -Id $id ` - -Method Put -ContentType 'application/json' -body $body -Version $([VSTeamVersions]::Release) + -Method Put -ContentType 'application/json' -body $body -Version $(_getApiVersion Release) Write-Output $resp } diff --git a/Source/Public/Update-VSTeamServiceEndpoint.ps1 b/Source/Public/Update-VSTeamServiceEndpoint.ps1 index ef93be04b..1275cc5fa 100644 --- a/Source/Public/Update-VSTeamServiceEndpoint.ps1 +++ b/Source/Public/Update-VSTeamServiceEndpoint.ps1 @@ -23,7 +23,7 @@ function Update-VSTeamServiceEndpoint { if ($Force -or $pscmdlet.ShouldProcess($id, "Update Service Endpoint")) { # Call the REST API $resp = _callAPI -ProjectName $projectName -Area 'distributedtask' -Resource 'serviceendpoints' -Id $id ` - -Method Put -ContentType 'application/json' -body $body -Version $([VSTeamVersions]::DistributedTask) + -Method Put -ContentType 'application/json' -body $body -Version $(_getApiVersion DistributedTask) _trackServiceEndpointProgress -projectName $projectName -resp $resp -title 'Updating Service Endpoint' -msg "Updating $id" diff --git a/Source/Public/Update-VSTeamTaskGroup.ps1 b/Source/Public/Update-VSTeamTaskGroup.ps1 index 236105f90..f7dcf5b40 100644 --- a/Source/Public/Update-VSTeamTaskGroup.ps1 +++ b/Source/Public/Update-VSTeamTaskGroup.ps1 @@ -23,10 +23,10 @@ function Update-VSTeamTaskGroup { if ($Force -or $pscmdlet.ShouldProcess("Update Task Group")) { if ($InFile) { - $resp = _callAPI -Method Put -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $([VSTeamVersions]::TaskGroups) -InFile $InFile -ContentType 'application/json' -Id $Id + $resp = _callAPI -Method Put -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $(_getApiVersion TaskGroups) -InFile $InFile -ContentType 'application/json' -Id $Id } else { - $resp = _callAPI -Method Put -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $([VSTeamVersions]::TaskGroups) -Body $Body -ContentType 'application/json' -Id $Id + $resp = _callAPI -Method Put -ProjectName $ProjectName -Area distributedtask -Resource taskgroups -Version $(_getApiVersion TaskGroups) -Body $Body -ContentType 'application/json' -Id $Id } } diff --git a/Source/Public/Update-VSTeamUserEntitlement.ps1 b/Source/Public/Update-VSTeamUserEntitlement.ps1 index 75229e579..579ed3d9e 100644 --- a/Source/Public/Update-VSTeamUserEntitlement.ps1 +++ b/Source/Public/Update-VSTeamUserEntitlement.ps1 @@ -41,7 +41,7 @@ function Update-VSTeamUserEntitlement # We have to go find the id $user = Get-VSTeamUserEntitlement -Top 10000 | Where-Object email -eq $email - if (-not$user) + if (-not $user) { throw "Could not find user with an email equal to $email" } @@ -77,7 +77,7 @@ function Update-VSTeamUserEntitlement if ($Force -or $PSCmdlet.ShouldProcess("$( $user.userName ) ($( $user.email ))", "Update user")) { # Call the REST API - _callAPI -Method Patch -Body $body -SubDomain 'vsaex' -Resource 'userentitlements' -Id $id -Version $([VSTeamVersions]::MemberEntitlementManagement) -ContentType 'application/json-patch+json' | Out-Null + _callAPI -Method Patch -NoProject -Body $body -SubDomain 'vsaex' -Resource 'userentitlements' -Id $id -Version $(_getApiVersion MemberEntitlementManagement) -ContentType 'application/json-patch+json' | Out-Null Write-Output "Updated user license for $( $user.userName ) ($( $user.email )) from LicenseType: ($licenseTypeOriginal) to ($newLicenseType)" Write-Output "Updated user license for $( $user.userName ) ($( $user.email )) from LicenseSource: ($licenseSourceOriginal) to ($newLicenseSource)" diff --git a/Source/Public/Update-VSTeamVariableGroup.ps1 b/Source/Public/Update-VSTeamVariableGroup.ps1 index 85d7d9561..8af2e2ffa 100644 --- a/Source/Public/Update-VSTeamVariableGroup.ps1 +++ b/Source/Public/Update-VSTeamVariableGroup.ps1 @@ -22,7 +22,7 @@ function Update-VSTeamVariableGroup { DynamicParam { $dp = _buildProjectNameDynamicParam - if ([VSTeamVersions]::Version -ne "TFS2017" -and $PSCmdlet.ParameterSetName -eq "ByHashtable") { + if ($(_getApiVersion -Target) -ne "TFS2017" -and $PSCmdlet.ParameterSetName -eq "ByHashtable") { $ParameterName = 'Type' $rp = _buildDynamicParam -ParameterName $ParameterName -arrSet ('Vsts', 'AzureKeyVault') -Mandatory $true $dp.Add($ParameterName, $rp) @@ -47,7 +47,7 @@ function Update-VSTeamVariableGroup { description = $Description variables = $Variables } - if ([VSTeamVersions]::Version -ne "TFS2017") { + if ($(_getApiVersion -Target) -ne "TFS2017") { $Type = $PSBoundParameters['Type'] $bodyAsHashtable.Add("type", $Type) @@ -63,7 +63,7 @@ function Update-VSTeamVariableGroup { if ($Force -or $pscmdlet.ShouldProcess($Id, "Update Variable Group")) { # Call the REST API $resp = _callAPI -ProjectName $projectName -Area 'distributedtask' -Resource 'variablegroups' -Id $Id ` - -Method Put -ContentType 'application/json' -body $body -Version $([VSTeamVersions]::VariableGroups) + -Method Put -ContentType 'application/json' -body $body -Version $(_getApiVersion VariableGroups) Write-Verbose $resp diff --git a/Source/Public/Update-VSTeamWorkItem.ps1 b/Source/Public/Update-VSTeamWorkItem.ps1 index bab1366f1..a7d938c24 100644 --- a/Source/Public/Update-VSTeamWorkItem.ps1 +++ b/Source/Public/Update-VSTeamWorkItem.ps1 @@ -78,8 +78,8 @@ function Update-VSTeamWorkItem { # Call the REST API if ($Force -or $pscmdlet.ShouldProcess($Id, "Update-WorkItem")) { $resp = _callAPI -Area 'wit' -Resource 'workitems' ` - -Version $([VSTeamVersions]::Core) -id $Id -Method Patch ` - -ContentType 'application/json-patch+json' -Body $json + -Version $(_getApiVersion Core) -id $Id -Method Patch ` + -ContentType 'application/json-patch+json' -Body $json -NoProject _applyTypesToWorkItem -item $resp diff --git a/Source/formats/Team.ResourceArea.TableView.ps1xml b/Source/formats/Team.ResourceArea.TableView.ps1xml new file mode 100644 index 000000000..56ff1209f --- /dev/null +++ b/Source/formats/Team.ResourceArea.TableView.ps1xml @@ -0,0 +1,34 @@ + + + + + Team.ResourceArea.TableView + + Team.ResourceArea + VSTeamResourceArea + + + + + + + + + + + + + + + Name + + + locationUrl + + + + + + + + diff --git a/Source/formats/_formats.json b/Source/formats/_formats.json index 0a26e9a2d..cb480b8dd 100644 --- a/Source/formats/_formats.json +++ b/Source/formats/_formats.json @@ -97,6 +97,7 @@ "Team.WorkItemDeleted.ListView.ps1xml", "Team.JobRequest.TableView.ps1xml", "Team.GitCommitRef.TableView.ps1xml", - "Team.GitUserDate.TableView.ps1xml" + "Team.GitUserDate.TableView.ps1xml", + "Team.ResourceArea.TableView.ps1xml" ] } \ No newline at end of file diff --git a/build/credscan-supression.json b/build/credscan-supression.json index 5440f1af2..0c074e265 100644 --- a/build/credscan-supression.json +++ b/build/credscan-supression.json @@ -1,30 +1,33 @@ - { "tool": "Credential Scanner", "suppressions": [ - { - "file": "profile.Tests.ps1", - "_justification": "Fake credentials only used for unit tests." - }, - { - "file": "Set-VSTeamAccount.Tests.ps1", - "_justification": "Fake credentials only used for unit tests." - }, - { - "file": "serviceendpoints.Tests.ps1", - "_justification": "Fake credentials only used for unit tests." - }, - { - "file": "VSTeam-Help.xml", - "_justification": "Fake credentials which are part of PS documentation examples." - }, - { - "file": "Add-VSTeamServiceFabricEndpoint.md", - "_justification": "Fake credentials which are part of PS documentation examples." - }, - { - "file": "buildDefyaml.json", - "_justification": "Fake credentials only used for unit tests." - } + { + "placeholder": "00000000-0000-0000-0000-000000000000", + "_justification": "This is the secret place holder used by my team" + }, + { + "file": "Add-VSTeamServiceFabricEndpoint.md", + "_justification": "Fake credentials only used for unit tests." + }, + { + "file": "Set-VSTeamAccount.Tests.ps1", + "_justification": "Fake credentials only used for unit tests." + }, + { + "file": "Add-VSTeamNuGetEndpoint.Tests.ps1", + "_justification": "Fake credentials only used for unit tests." + }, + { + "file": "VSTeam-Help.xml", + "_justification": "Fake credentials which are part of PS documentation examples." + }, + { + "file": "Add-VSTeamServiceFabricEndpoint.md", + "_justification": "Fake credentials which are part of PS documentation examples." + }, + { + "file": "buildDefyaml.json", + "_justification": "Fake credentials only used for unit tests." + } ] - } \ No newline at end of file +} \ No newline at end of file diff --git a/unit/test/Add-VSTeam.Tests.ps1 b/unit/test/Add-VSTeam.Tests.ps1 new file mode 100644 index 000000000..b971cfbc1 --- /dev/null +++ b/unit/test/Add-VSTeam.Tests.ps1 @@ -0,0 +1,88 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamTeam.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + + +Describe "VSTeam" { + $singleResult = [PSCustomObject]@{ + id = '6f365a7143e492e911c341451a734401bcacadfd' + name = 'refs/heads/master' + description = 'team description' + } + + Context "Add-VSTeam" { + Context "Services" { + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Context 'Add-VSTeam with team name only' { + Mock Invoke-RestMethod { return $singleResult } + + It 'Should create a team' { + Add-VSTeam -ProjectName Test -TeamName "TestTeam" + + $expectedBody = '{ "name": "TestTeam", "description": "" }' + + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams?api-version=$(_getApiVersion Core)" -and + $Method -eq "Post" -and + $Body -eq $expectedBody + } + } + } + + Context 'Add-VSTeam with team name and description' { + Mock Invoke-RestMethod { return $singleResult } + + It 'Should create a team' { + Add-VSTeam -ProjectName Test -TeamName "TestTeam" -Description "Test Description" + + $expectedBody = '{ "name": "TestTeam", "description": "Test Description" }' + + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams?api-version=$(_getApiVersion Core)" -and + $Method -eq "Post" -and + $Body -eq $expectedBody + } + } + } + } + + Context "Server" { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Mock _useWindowsAuthenticationOnPremise { return $true } + + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + Mock Invoke-RestMethod { return $singleResult } + + It 'with team name only on TFS local Auth should create a team' { + Add-VSTeam -ProjectName Test -TeamName "TestTeam" + + $expectedBody = '{ "name": "TestTeam", "description": "" }' + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams?api-version=$(_getApiVersion Core)" -and + $Method -eq "Post" -and + $Body -eq $expectedBody + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamAccessControlEntry.Tests.ps1 b/unit/test/Add-VSTeamAccessControlEntry.Tests.ps1 index 54b7f7f9c..980a9aa1d 100644 --- a/unit/test/Add-VSTeamAccessControlEntry.Tests.ps1 +++ b/unit/test/Add-VSTeamAccessControlEntry.Tests.ps1 @@ -1,5 +1,8 @@ Set-StrictMode -Version Latest +#region include +Import-Module SHiPS + $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") @@ -9,104 +12,49 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Classes/VSTeamSecurityNamespace.ps1" . "$here/../../Source/Classes/VSTeamAccessControlEntry.ps1" . "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamDefaultProject.ps1" +. "$here/../../Source/Public/Get-VSTeamSecurityNamespace.ps1" . "$here/../../Source/Public/$sut" +#endregion -$accessControlEntryResult = -@" -{ - "count": 1, - "value": [ - { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", - "allow": 8, - "deny": 0, - "extendedInfo": {} - } - ] -} -"@ | ConvertFrom-Json - -$securityNamespace = -@" -{ - "count": 1, - "value": [ - { - "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531", - "name": "Analytics", - "displayName": "Analytics", - "separatorValue": "/", - "elementLength": -1, - "writePermission": 2, - "readPermission": 1, - "dataspaceCategory": "Default", - "actions": [ - { - "bit": 1, - "name": "Read", - "displayName": "View analytics", - "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" - }, - { - "bit": 2, - "name": "Administer", - "displayName": "Manage analytics permissions", - "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" - }, - { - "bit": 4, - "name": "Stage", - "displayName": "Push the data to staging area", - "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" - }, - { - "bit": 8, - "name": "ExecuteUnrestrictedQuery", - "displayName": "Execute query without any restrictions on the query form", - "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" - }, - { - "bit": 16, - "name": "ReadEuii", - "displayName": "Read EUII data", - "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" - } - ], - "structureValue": 1, - "extensionType": null, - "isRemotable": false, - "useTokenTranslator": false, - "systemBitMask": 30 - } - ] - } -"@ | ConvertFrom-Json - -$securityNamespaceObject = [VSTeamSecurityNamespace]::new($securityNamespace.value) - -Describe 'Add-VSTeamAccessControlEntry' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } - - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Core = '' - [VSTeamVersions]::Core = '5.0' - - Context 'Add-VSTeamAccessControlEntry by SecurityNamespaceId' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $accessControlEntryResult - } -Verifiable - - Add-VSTeamAccessControlEntry -SecurityNamespaceId 5a27515b-ccd7-42c9-84f1-54c998f03866 -Descriptor abc -Token xyz -AllowMask 12 -DenyMask 15 - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/5a27515b-ccd7-42c9-84f1-54c998f03866*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and +Describe 'VSTeamAccessControlEntry' { + Context 'Add-VSTeamAccessControlEntry' { + ## Arrange + # Load sample files you need for mocks below + $securityNamespace = Get-Content "$PSScriptRoot\sampleFiles\securityNamespace.json" -Raw | ConvertFrom-Json + $accessControlEntryResult = Get-Content "$PSScriptRoot\sampleFiles\accessControlEntryResult.json" -Raw | ConvertFrom-Json + + # Some of the functions return VSTeam classes so turn the PSCustomeObject + # into the correct type. + $securityNamespaceObject = [VSTeamSecurityNamespace]::new($securityNamespace.value[0]) + + ## Arrange + # This value being left around can cause other tests to fail. + AfterAll { $Global:PSDefaultParameterValues.Remove("*:projectName") } + + # Set the account to use for testing. A normal user would do this using the + # Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock _getApiVersion { return '5.0-unitTests' } -ParameterFilter { $Service -eq 'Core'} + + # This is only called when you need to test that the function can handle an + # exception. To make sure this mock is called make sure the descriptor in + # the body of your call has the value of 'boom'. + Mock Invoke-RestMethod { throw 'Error' } -ParameterFilter { $Body -like "*`"descriptor`": `"boom`",*" } + + Mock Invoke-RestMethod { return $accessControlEntryResult } + + It 'by SecurityNamespace (pipeline) should return ACEs' { + ## Act + $securityNamespaceObject | Add-VSTeamAccessControlEntry -Descriptor abc -Token xyz -AllowMask 12 -DenyMask 15 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + # The write-host below is great for seeing how many ways the mock is called. + # Write-Host "Assert Mock $Uri" + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/58450c49-b02d-465a-ab12-59ae512d6531*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and $Body -like "*`"token`": `"xyz`",*" -and $Body -like "*`"descriptor`": `"abc`",*" -and $Body -like "*`"allow`": 12,*" -and @@ -115,19 +63,23 @@ Describe 'Add-VSTeamAccessControlEntry' { $Method -eq "Post" } } - } - Context 'Add-VSTeamAccessControlEntry by SecurityNamespace' { - Mock Get-VSTeamSecurityNamespace { return $securityNamespaceObject } - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable + It 'by SecurityNamespaceId should return ACEs' { + # Even with a default set this URI should not have the project added. + # So set the default project to Testing here and test below that the + # project is NOT added to the Uri. + ## Arange + Set-VSTeamDefaultProject -Project Testing - $securityNamespace = Get-VSTeamSecurityNamespace -Id "58450c49-b02d-465a-ab12-59ae512d6531" - Add-VSTeamAccessControlEntry -SecurityNamespace $securityNamespace -Descriptor abc -Token xyz -AllowMask 12 -DenyMask 15 + ## Act + Add-VSTeamAccessControlEntry -SecurityNamespaceId 5a27515b-ccd7-42c9-84f1-54c998f03866 -Descriptor abc -Token xyz -AllowMask 12 -DenyMask 15 - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/58450c49-b02d-465a-ab12-59ae512d6531*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + # The write-host below is great for seeing how many ways the mock is called. + # Write-Host "Assert Mock $Uri" + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/5a27515b-ccd7-42c9-84f1-54c998f03866*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and $Body -like "*`"token`": `"xyz`",*" -and $Body -like "*`"descriptor`": `"abc`",*" -and $Body -like "*`"allow`": 12,*" -and @@ -136,19 +88,17 @@ Describe 'Add-VSTeamAccessControlEntry' { $Method -eq "Post" } } - } - - Context 'Add-VSTeamAccessControlEntry by SecurityNamespace (pipeline)' { - Mock Get-VSTeamSecurityNamespace { return $securityNamespaceObject } - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - Get-VSTeamSecurityNamespace -Id "58450c49-b02d-465a-ab12-59ae512d6531" | ` - Add-VSTeamAccessControlEntry -Descriptor abc -Token xyz -AllowMask 12 -DenyMask 15 + It 'by SecurityNamespace should return ACEs' { + ## Act + Add-VSTeamAccessControlEntry -SecurityNamespace $securityNamespaceObject -Descriptor abc -Token xyz -AllowMask 12 -DenyMask 15 - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + # The write-host below is great for seeing how many ways the mock is called. + # Write-Host "Assert Mock $Uri" $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/58450c49-b02d-465a-ab12-59ae512d6531*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and $Body -like "*`"token`": `"xyz`",*" -and $Body -like "*`"descriptor`": `"abc`",*" -and $Body -like "*`"allow`": 12,*" -and @@ -157,24 +107,15 @@ Describe 'Add-VSTeamAccessControlEntry' { $Method -eq "Post" } } - } - - Context 'Add-VSTeamAccessControlEntry by securityNamespaceId throws' { - Mock Invoke-RestMethod { throw 'Error' } - It 'Should throw' { - { Add-VSTeamAccessControlEntry -SecurityNamespaceId 5a27515b-ccd7-42c9-84f1-54c998f03866 -Descriptor abc -Token xyz -AllowMask 12 -DenyMask 15 } | Should Throw + It 'by securityNamespaceId throws should throw' { + ## Act / Assert + { Add-VSTeamAccessControlEntry -SecurityNamespaceId 5a27515b-ccd7-42c9-84f1-54c998f03866 -Descriptor boom -Token xyz -AllowMask 12 -DenyMask 15 } | Should Throw } - } - - Context 'Add-VSTeamAccessControlEntry by SecurityNamespace throws' { - Mock Get-VSTeamSecurityNamespace { return $securityNamespaceObject } - Mock Invoke-RestMethod { throw 'Error' } - - $securityNamespace = Get-VSTeamSecurityNamespace -Id "5a27515b-ccd7-42c9-84f1-54c998f03866" - It 'Should throw' { - { Add-VSTeamAccessControlEntry -SecurityNamespace $securityNamespace -Descriptor abc -Token xyz -AllowMask 12 -DenyMask 15 } | Should Throw + It 'by SecurityNamespace should throw' { + ## Act / Assert + { Add-VSTeamAccessControlEntry -SecurityNamespace $securityNamespaceObject -Descriptor boom -Token xyz -AllowMask 12 -DenyMask 15 } | Should Throw } } } \ No newline at end of file diff --git a/unit/test/Add-VSTeamBuild.Tests.ps1 b/unit/test/Add-VSTeamBuild.Tests.ps1 new file mode 100644 index 000000000..f29a81eae --- /dev/null +++ b/unit/test/Add-VSTeamBuild.Tests.ps1 @@ -0,0 +1,198 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinition.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamQueue.ps1" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" +. "$here/../../Source/Public/Get-VSTeamBuildDefinition.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamBuild' { + Context 'Add-VSTeamBuild' { + ## Arrange + $resultsVSTS = Get-Content "$PSScriptRoot\sampleFiles\buildDefvsts.json" -Raw | ConvertFrom-Json + $singleResult = Get-Content "$PSScriptRoot\sampleFiles\buildSingleResult.json" -Raw | ConvertFrom-Json + + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Build' } + + Context 'Services' { + ## Arrange + BeforeAll { + $Global:PSDefaultParameterValues.Remove("*:projectName") + } + + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + Mock Invoke-RestMethod { return $singleResult } + Mock Get-VSTeamBuildDefinition { return $resultsVSTS.value } + + It 'by name should add build' { + ## Act + Add-VSTeamBuild -ProjectName project -BuildDefinitionName 'aspdemo-CI' + + ## Assert + # Call to queue build. + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + ($Body | ConvertFrom-Json).definition.id -eq 699 -and + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds?api-version=$(_getApiVersion Build)" + } + } + + It 'by id should add build' { + ## Act + Add-VSTeamBuild -ProjectName project -BuildDefinitionId 2 + + ## Assert + # Call to queue build. + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + ($Body | ConvertFrom-Json).definition.id -eq 2 -and + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds?api-version=$(_getApiVersion Build)" + } + } + + It 'with source branch should add build' { + ## Act + Add-VSTeamBuild -ProjectName project -BuildDefinitionId 2 -SourceBranch 'refs/heads/dev' + + ## Assert + # Call to queue build. + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + ($Body | ConvertFrom-Json).definition.id -eq 2 -and + ($Body | ConvertFrom-Json).sourceBranch -eq 'refs/heads/dev' -and + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds?api-version=$(_getApiVersion Build)" + } + } + + It 'with parameters should add build' { + ## Act + Add-VSTeamBuild -ProjectName project -BuildDefinitionId 2 -BuildParameters @{'system.debug' = 'true' } + + ## Assert + # Call to queue build. + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + ($Body | ConvertFrom-Json).definition.id -eq 2 -and + (($Body | ConvertFrom-Json).parameters | ConvertFrom-Json).'system.debug' -eq 'true' -and + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds?api-version=$(_getApiVersion Build)" + } + } + } + + Context 'Server' { + ## Arrange + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable + + AfterAll { + $Global:PSDefaultParameterValues.Remove("*:projectName") + } + + Mock Invoke-RestMethod { + # Write-Host $args + return $singleResult + } + + Mock Get-VSTeamQueue { return [PSCustomObject]@{ + name = "MyQueue" + id = 3 + } + } + + Mock Get-VSTeamBuildDefinition { return @{ name = "MyBuildDef" } } + + It 'by id on TFS local Auth should add build' { + ## Arrange + $Global:PSDefaultParameterValues["*:projectName"] = 'Project' + + ## Act + Add-VSTeamBuild -projectName project -BuildDefinitionId 2 -QueueName MyQueue + + ## Assert + # Call to queue build. + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + # The write-host below is great for seeing how many ways the mock is called. + # Write-Host "Assert Mock $Uri" + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds?api-version=$(_getApiVersion Build)" -and + ($Body | ConvertFrom-Json).definition.id -eq 2 -and + ($Body | ConvertFrom-Json).queue.id -eq 3 + } + } + + It 'with parameters on TFS local Auth should add build' { + ## Arrange + $Global:PSDefaultParameterValues["*:projectName"] = 'Project' + + ## Act + Add-VSTeamBuild -projectName project -BuildDefinitionId 2 -QueueName MyQueue -BuildParameters @{'system.debug' = 'true' } + + ## Assert + # Call to queue build. + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + # The write-host below is great for seeing how many ways the mock is called. + # Write-Host "Assert Mock $Uri" + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds?api-version=$(_getApiVersion Build)" -and + ($Body | ConvertFrom-Json).definition.id -eq 2 -and + ($Body | ConvertFrom-Json).queue.id -eq 3 -and + $Body -like "*system.debug*" + } + } + + It 'with source branch on TFS local auth should add build' { + ## Arrange + $Global:PSDefaultParameterValues["*:projectName"] = 'Project' + + ## Act + Add-VSTeamBuild -projectName project -BuildDefinitionId 2 -QueueName MyQueue -SourceBranch refs/heads/dev + + ## Assert + # Call to queue build. + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + # The write-host below is great for seeing how many ways the mock is called. + # Write-Host "Assert Mock $Uri" + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds?api-version=$(_getApiVersion Build)" -and + ($Body | ConvertFrom-Json).definition.id -eq 2 -and + ($Body | ConvertFrom-Json).queue.id -eq 3 -and + ($Body | ConvertFrom-Json).sourceBranch -eq 'refs/heads/dev' + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamBuildDefinition.Tests.ps1 b/unit/test/Add-VSTeamBuildDefinition.Tests.ps1 new file mode 100644 index 000000000..42ffe628f --- /dev/null +++ b/unit/test/Add-VSTeamBuildDefinition.Tests.ps1 @@ -0,0 +1,58 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamBuildDefinition' { + Context 'Add-VSTeamBuildDefinition' { + ## Arrange + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + $resultsVSTS = Get-Content "$PSScriptRoot\sampleFiles\buildDefvsts.json" -Raw | ConvertFrom-Json + + Mock Invoke-RestMethod { return $resultsVSTS } + + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Build' } + + Context 'Services' { + ## Arrange + Mock _getInstance { return 'https://dev.azure.com/test' } + + it 'Should add build' { + ## Act + Add-VSTeamBuildDefinition -projectName project -inFile 'sampleFiles/builddef.json' + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Post' -and + $InFile -eq 'sampleFiles/builddef.json' -and + $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions?api-version=$(_getApiVersion Build)" + } + } + } + + Context 'Server' { + ## Arrange + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + it 'Should add build' { + ## Act + Add-VSTeamBuildDefinition -projectName project -inFile 'sampleFiles/builddef.json' + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Post' -and + $InFile -eq 'sampleFiles/builddef.json' -and + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/definitions?api-version=$(_getApiVersion Build)" + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamBuildTag.Tests.ps1 b/unit/test/Add-VSTeamBuildTag.Tests.ps1 new file mode 100644 index 000000000..2e5e8c997 --- /dev/null +++ b/unit/test/Add-VSTeamBuildTag.Tests.ps1 @@ -0,0 +1,67 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'Add-VSTeamBuildTag' { + Context 'Add-VSTeamBuildTag' { + ## Arrange + $inputTags = "Test1", "Test2", "Test3" + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Build' } + + Context 'Services' { + ## Arrange + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Invoke-RestMethod + + It 'should add tags to Build' { + ## Act + Add-VSTeamBuildTag -ProjectName project -id 2 -Tags $inputTags + + ## Assert + foreach ($inputTag in $inputTags) { + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Put' -and + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/2/tags?api-version=$(_getApiVersion Build)" + "&tag=$inputTag" + } + } + } + } + + Context 'Server' { + ## Arrange + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Mock _useWindowsAuthenticationOnPremise { return $true } + + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + Mock Invoke-RestMethod + + It 'should add tags to Build' { + ## Act + Add-VSTeamBuildTag -ProjectName project -id 2 -Tags $inputTags + + ## Assert + foreach ($inputTag in $inputTags) { + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Put' -and + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/2/tags?api-version=$(_getApiVersion Build)" + "&tag=$inputTag" + } + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamExtension.Tests.ps1 b/unit/test/Add-VSTeamExtension.Tests.ps1 new file mode 100644 index 000000000..21ac3cc5b --- /dev/null +++ b/unit/test/Add-VSTeamExtension.Tests.ps1 @@ -0,0 +1,77 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamInstallState.ps1" +. "$here/../../Source/Classes/VSTeamExtension.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamExtension' { + Context 'Add-VSTeamExtension' { + ## Arrange + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'ExtensionsManagement' } + + $singleResult = [PSCustomObject]@{ + extensionId = 'test' + extensionName = 'test' + publisherId = 'test' + publisherName = 'test' + version = '1.0.0' + registrationId = '12345678-9012-3456-7890-123456789012' + manifestVersion = 1 + baseUri = '' + fallbackBaseUri = '' + scopes = [PSCustomObject]@{ } + installState = [PSCustomObject]@{ + flags = 'none' + lastUpdated = '2018-10-09T11:26:47.187Z' + } + } + + BeforeAll { + $env:Team_TOKEN = '1234' + } + + AfterAll { + $env:TEAM_TOKEN = $null + } + + Mock _callAPI { return $singleResult } + + It 'Should add an extension without version' { + ## Act + Add-VSTeamExtension -PublisherId 'test' -ExtensionId 'test' + + ## Assert + Assert-MockCalled _callAPI -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Get' -and + $subDomain -eq 'extmgmt' -and + $version -eq $(_getApiVersion ExtensionsManagement) -and + $uri + $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensionsbyname/test/test*" + } + } + + It 'Should add an extension with version' { + ## Act + Add-VSTeamExtension -PublisherId 'test' -ExtensionId 'test' -Version '1.0.0' + + ## Assert + Assert-MockCalled _callAPI -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Get' -and + $subDomain -eq 'extmgmt' -and + $version -eq $(_getApiVersion ExtensionsManagement) + $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensionsbyname/test/test/1.0.0*" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamFeed.Tests.ps1 b/unit/test/Add-VSTeamFeed.Tests.ps1 new file mode 100644 index 000000000..1a47b4001 --- /dev/null +++ b/unit/test/Add-VSTeamFeed.Tests.ps1 @@ -0,0 +1,52 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamFeed.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamFeed' { + Context 'Add-VSTeamFeed' { + ## Arrange + $results = Get-Content "$PSScriptRoot\sampleFiles\feeds.json" -Raw | ConvertFrom-Json + Mock Invoke-RestMethod { return $results.value[0] } + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Packaging' } + + it 'with description should add feed' { + ## Act + Add-VSTeamFeed -Name 'module' -Description 'Test Module' + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://feeds.dev.azure.com/test/_apis/packaging/feeds?api-version=$(_getApiVersion Packaging)" -and + $Method -eq 'Post' -and + $ContentType -eq 'application/json' -and + $Body -like '*"name": *"module"*' + } + } + + it 'with upstream sources should add feed' { + ## Act + Add-VSTeamFeed -Name 'module' -EnableUpstreamSources -showDeletedPackageVersions + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://feeds.dev.azure.com/test/_apis/packaging/feeds?api-version=$(_getApiVersion packaging)" -and + $Method -eq 'Post' -and + $ContentType -eq 'application/json' -and + $Body -like '*"upstreamEnabled":*true*' -and + $Body -like '*"hideDeletedPackageVersions":*false*' + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamGitRepository.Tests.ps1 b/unit/test/Add-VSTeamGitRepository.Tests.ps1 new file mode 100644 index 000000000..d55bd1ca9 --- /dev/null +++ b/unit/test/Add-VSTeamGitRepository.Tests.ps1 @@ -0,0 +1,86 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinition.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamQueue.ps1" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" +. "$here/../../Source/Public/Get-VSTeamBuildDefinition.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe "VSTeamGitRepository" { + Context 'Add-VSTeamGitRepository' { + ## Arrange + $singleResult = [PSCustomObject]@{ + id = '' + url = '' + sshUrl = '' + remoteUrl = '' + defaultBranch = '' + size = 0 + name = 'testRepo' + project = [PSCustomObject]@{ + name = 'Project' + id = 1 + description = '' + url = '' + state = '' + revision = '' + visibility = '' + } + } + + Mock _hasProjectCacheExpired { return $false } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Mock Invoke-RestMethod { return $singleResult } + + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-gitUnitTests' } -ParameterFilter { $Service -eq 'Git' } + + It 'by name should add Git repo' { + Add-VSTeamGitRepository -Name 'testRepo' -ProjectName 'test' + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq "Post" -and + $Uri -eq "https://dev.azure.com/test/test/_apis/git/repositories?api-version=1.0-gitUnitTests" -and + $Body -like "*testRepo*" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamGitRepositoryPermission.Tests.ps1 b/unit/test/Add-VSTeamGitRepositoryPermission.Tests.ps1 new file mode 100644 index 000000000..54737e77a --- /dev/null +++ b/unit/test/Add-VSTeamGitRepositoryPermission.Tests.ps1 @@ -0,0 +1,221 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGroup.ps1" +. "$here/../../Source/Classes/VSTeamUser.ps1" +. "$here/../../Source/Classes/VSTeamSecurityNamespace.ps1" +. "$here/../../Source/Classes/VSTeamAccessControlEntry.ps1" +. "$here/../../Source/Classes/VSTeamGitRepositoryPermissions.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Add-VSTeamAccessControlEntry.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamGitRepositoryPermission' { + Context 'Add-VSTeamGitRepositoryPermission' { + ## Arrange + $userSingleResult = Get-Content "$PSScriptRoot\sampleFiles\users.single.json" -Raw | ConvertFrom-Json + $userSingleResultObject = [VSTeamUser]::new($userSingleResult) + + $groupSingleResult = Get-Content "$PSScriptRoot\sampleFiles\groupsSingle.json" -Raw | ConvertFrom-Json + $groupSingleResultObject = [VSTeamGroup]::new($groupSingleResult) + + $projectResult = Get-Content "$PSScriptRoot\sampleFiles\projectResult.json" -Raw | ConvertFrom-Json + $projectResultObject = [VSTeamProject]::new($projectResult) + + $accessControlEntryResult = Get-Content "$PSScriptRoot\sampleFiles\accessControlEntryResult.json" -Raw | ConvertFrom-Json + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + # You have to set the version or the api-version will not be added when versions = '' + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + # Write-Host $args + + return $accessControlEntryResult + } -Verifiable + + It 'by ProjectUser should return ACEs' { + ## Act + Add-VSTeamGitRepositoryPermission -Project $projectResultObject -User $userSingleResultObject -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and + $Body -like "*`"allow`": 34048,*" -and + $Body -like "*`"deny`": 2056,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by ProjectGroup should return ACEs' { + ## Act + Add-VSTeamGitRepositoryPermission -Project $projectResultObject -Group $groupSingleResultObject -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 34048,*" -and + $Body -like "*`"deny`": 2056,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by ProjectDescriptor should return ACEs' { + ## Act + # The S-1-9 number is on digit off from the calls above so the same mock can be used + # as above with the exactly 1 parameter. If you don't use different S-1-9 the count + # of calls will be off. + Add-VSTeamGitRepositoryPermission -Project $projectResultObject -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-2551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-2551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 34048,*" -and + $Body -like "*`"deny`": 2056,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by RepositoryUser should return ACEs' { + ## Act + Add-VSTeamGitRepositoryPermission -Project $projectResultObject -RepositoryId "12345678-1234-1234-1234-123456789012" -User $userSingleResultObject -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876/12345678-1234-1234-1234-123456789012`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and + $Body -like "*`"allow`": 34048,*" -and + $Body -like "*`"deny`": 2056,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by RepositoryGroup should return ACEs' { + ## Act + Add-VSTeamGitRepositoryPermission -Project $projectResultObject -RepositoryId "12345678-1234-1234-1234-123456789012" -Group $groupSingleResultObject -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876/12345678-1234-1234-1234-123456789012`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 34048,*" -and + $Body -like "*`"deny`": 2056,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by RepositoryDescriptor should return ACEs' { + ## Act + Add-VSTeamGitRepositoryPermission -Project $projectResultObject -RepositoryId "12345678-1234-1234-1234-123456789013" -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876/12345678-1234-1234-1234-123456789013`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 34048,*" -and + $Body -like "*`"deny`": 2056,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by RepositoryBranchUser should return ACEs' { + ## Act + Add-VSTeamGitRepositoryPermission -Project $projectResultObject -RepositoryId "12345678-1234-1234-1234-123456789012" -BranchName "master" -User $userSingleResultObject -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876/12345678-1234-1234-1234-123456789012/refs/heads/6d0061007300740065007200`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and + $Body -like "*`"allow`": 34048,*" -and + $Body -like "*`"deny`": 2056,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by RepositoryBranchGroup should return ACEs' { + ## Act + Add-VSTeamGitRepositoryPermission -Project $projectResultObject -RepositoryId "12345678-1234-1234-1234-123456789012" -BranchName "master" -Group $groupSingleResultObject -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876/12345678-1234-1234-1234-123456789012/refs/heads/6d0061007300740065007200`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 34048,*" -and + $Body -like "*`"deny`": 2056,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by RepositoryBranchDescriptor should return ACEs' { + ## Act + Add-VSTeamGitRepositoryPermission -Project $projectResultObject -RepositoryId "12345678-1234-1234-1234-123456789015" -BranchName "master" -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876/12345678-1234-1234-1234-123456789015/refs/heads/6d0061007300740065007200`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 34048,*" -and + $Body -like "*`"deny`": 2056,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamKubernetesEndpoint.Tests.ps1 b/unit/test/Add-VSTeamKubernetesEndpoint.Tests.ps1 new file mode 100644 index 000000000..7216b425b --- /dev/null +++ b/unit/test/Add-VSTeamKubernetesEndpoint.Tests.ps1 @@ -0,0 +1,73 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Add-VSTeamServiceEndpoint.ps1" +. "$here/../../Source/Public/Get-VSTeamServiceEndpoint.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamKubernetesEndpoint' { + Context 'Add-VSTeamKubernetesEndpoint' { + Mock _hasProjectCacheExpired { return $false } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'ServiceFabricEndpoint' } + + Mock Write-Progress + Mock Invoke-RestMethod { return @{id = '23233-2342' } } -ParameterFilter { $Method -eq 'Post' } + + Mock Invoke-RestMethod { + if ($iTracking -gt 9) { + return [PSCustomObject]@{ + isReady = $true + operationStatus = [PSCustomObject]@{state = 'Ready' } + } + } + + return [PSCustomObject]@{ + isReady = $false + createdBy = [PSCustomObject]@{ } + authorization = [PSCustomObject]@{ } + data = [PSCustomObject]@{ } + operationStatus = [PSCustomObject]@{state = 'InProgress' } + } + } + + It 'not accepting untrusted certs and not generating a pfx should create a new Kubernetes Serviceendpoint' { + Add-VSTeamKubernetesEndpoint -projectName 'project' -endpointName 'KubTest' ` + -kubernetesUrl 'http://myK8s.local' -clientKeyData '00000000-0000-0000-0000-000000000000' ` + -kubeconfig '{name: "myConfig"}' -clientCertificateData 'someClientCertData' + + # On PowerShell 5 the JSON has two spaces but on PowerShell 6 it only has one so + # test for both. + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + ($Body -like '*"acceptUntrustedCerts": false*' -or $Body -like '*"acceptUntrustedCerts": false*') -and + ($Body -like '*"generatePfx": false*' -or $Body -like '*"generatePfx": false*') + } + } + + It 'accepting untrusted certs and generating a pfx should create a new Kubernetes Serviceendpoint' { + Add-VSTeamKubernetesEndpoint -projectName 'project' -endpointName 'KubTest' ` + -kubernetesUrl 'http://myK8s.local' -clientKeyData '00000000-0000-0000-0000-000000000000' ` + -kubeconfig '{name: "myConfig"}' -clientCertificateData 'someClientCertData' -acceptUntrustedCerts -generatePfx + + # On PowerShell 5 the JSON has two spaces but on PowerShell 6 it only has one so + # test for both. + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + ($Body -like '*"acceptUntrustedCerts": true*' -or $Body -like '*"acceptUntrustedCerts": true*') -and + ($Body -like '*"generatePfx": true*' -or $Body -like '*"generatePfx": true*') + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamMembership.Tests.ps1 b/unit/test/Add-VSTeamMembership.Tests.ps1 new file mode 100644 index 000000000..a0fa178cc --- /dev/null +++ b/unit/test/Add-VSTeamMembership.Tests.ps1 @@ -0,0 +1,43 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/callMembershipAPI.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamMembership' { + ## Arrange + # You have to set the version or the api-version will not be added when [VSTeamVersions]::Graph = '' + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Graph' } + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Invoke-RestMethod + Mock _supportsGraph + + $UserDescriptor = 'aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj' + $GroupDescriptor = 'vssgp.Uy0xLTktMTU1MTM3NDI0NS04NTYwMDk3MjYtNDE5MzQ0MjExNy0yMzkwNzU2MTEwLTI3NDAxNjE4MjEtMC0wLTAtMC0x' + + Context 'Add-VSTeamMembership' { + It 'Should add membership' { + ## Act + $null = Add-VSTeamMembership -MemberDescriptor $UserDescriptor -ContainerDescriptor $GroupDescriptor + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq "Put" -and + $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/memberships/$UserDescriptor/$GroupDescriptor*" -and + $Uri -like "*api-version=$(_getApiVersion Graph)*" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamNuGetEndpoint.Tests.ps1 b/unit/test/Add-VSTeamNuGetEndpoint.Tests.ps1 new file mode 100644 index 000000000..4a12d2956 --- /dev/null +++ b/unit/test/Add-VSTeamNuGetEndpoint.Tests.ps1 @@ -0,0 +1,84 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Add-VSTeamServiceEndpoint.ps1" +. "$here/../../Source/Public/Get-VSTeamServiceEndpoint.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamNuGetEndpoint' { + Context 'Add-VSTeamNuGetEndpoint' { + Mock _hasProjectCacheExpired { return $false } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'DistributedTask' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'ServiceFabricEndpoint' } + + Mock Write-Progress + Mock Invoke-RestMethod { return @{id = '23233-2342' } } -ParameterFilter { $Method -eq 'Post' } + Mock Invoke-RestMethod { + # This $i is in the module. Because we use InModuleScope + # we can see it + if ($iTracking -gt 9) { + return [PSCustomObject]@{ + isReady = $true + operationStatus = [PSCustomObject]@{state = 'Ready' } + } + } + + return [PSCustomObject]@{ + isReady = $false + createdBy = [PSCustomObject]@{ } + authorization = [PSCustomObject]@{ } + data = [PSCustomObject]@{ } + operationStatus = [PSCustomObject]@{state = 'InProgress' } + } + } + + It 'with ApiKey should create a new NuGet Serviceendpoint' { + Add-VSTeamNuGetEndpoint -ProjectName 'project' -EndpointName 'PowerShell Gallery' -NuGetUrl 'https://www.powershellgallery.com/api/v2/package' -ApiKey '00000000-0000-0000-0000-000000000000' + Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/serviceendpoints?api-version=$(_getApiVersion DistributedTask)" -and + $Method -eq 'Post' -and + $ContentType -eq 'application/json' -and + $Body -like '*"nugetkey": *"00000000-0000-0000-0000-000000000000"*' -and + $Body -like '*"scheme": *"None"*' + } + } + + It 'with Username and Password should create a new NuGet Serviceendpoint' { + $password = '00000000-0000-0000-0000-000000000000' | ConvertTo-SecureString -AsPlainText -Force + Add-VSTeamNuGetEndpoint -ProjectName 'project' -EndpointName 'PowerShell Gallery' -NuGetUrl 'https://www.powershellgallery.com/api/v2/package' -Username 'testUser' -SecurePassword $password + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/serviceendpoints?api-version=$(_getApiVersion DistributedTask)" -and + $Method -eq 'Post' -and + $ContentType -eq 'application/json' -and + $Body -like '*"username": *"testUser"*' -and + $Body -like '*"password": *"00000000-0000-0000-0000-000000000000"*' -and + $Body -like '*"scheme": *"UsernamePassword"*' + } + } + + It 'with Token should create a new NuGet Serviceendpoint' { + Add-VSTeamNuGetEndpoint -ProjectName 'project' -EndpointName 'PowerShell Gallery' -NuGetUrl 'https://www.powershellgallery.com/api/v2/package' -PersonalAccessToken '00000000-0000-0000-0000-000000000000' + Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/serviceendpoints?api-version=$(_getApiVersion DistributedTask)" -and + $Method -eq 'Post' -and + $ContentType -eq 'application/json' -and + $Body -like '*"apitoken":*"00000000-0000-0000-0000-000000000000"*' -and + $Body -like '*"scheme":*"Token"*' + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamPolicy.Tests.ps1 b/unit/test/Add-VSTeamPolicy.Tests.ps1 new file mode 100644 index 000000000..9f402edc8 --- /dev/null +++ b/unit/test/Add-VSTeamPolicy.Tests.ps1 @@ -0,0 +1,75 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamPolicy' { + ## Arrange + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Invoke-RestMethod + Mock Invoke-RestMethod { throw 'Error' } -ParameterFilter { $Uri -like "*boom*" } + + Mock _getApiVersion { return '1.0-gitUnitTests' } -ParameterFilter { $Service -eq 'Git' } + + Context 'Add-VSTeamPolicy' { + It 'should add the policy' { + ## Act + Add-VSTeamPolicy -ProjectName Demo -type babcf51f-d853-43a2-9b05-4a64ca577be0 -enabled -blocking -settings @{ + MinimumApproverCount = 1 + scope = @( + @{ + refName = 'refs/heads/master' + matchKind = 'Exact' + repositoryId = '10000000-0000-0000-0000-0000000000001' + }) + } + + ## Assert + # With PowerShell core the order of the boty string is not the + # same from run to run! So instead of testing the entire string + # matches I have to search for the portions I expect but can't + # assume the order. + # The general string should look like this: + # '{"isBlocking":true,"isEnabled":true,"type":{"id":"babcf51f-d853-43a2-9b05-4a64ca577be0"},"settings":{"scope":[{"repositoryId":"10000000-0000-0000-0000-0000000000001","matchKind":"Exact","refName":"refs/heads/master"}],"MinimumApproverCount":1}}' + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Post' -and + $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/configurations?api-version=$(_getApiVersion Git)" -and + $Body -like '*"isBlocking":true*' -and + $Body -like '*"isEnabled":true*' -and + $Body -like '*"type":{"id":"babcf51f-d853-43a2-9b05-4a64ca577be0"}*' -and + $Body -like '*"MinimumApproverCount":1*' -and + $Body -like '*"settings":*' -and + $Body -like '*"scope":*' -and + $Body -like '*"repositoryId":"10000000-0000-0000-0000-0000000000001"*' -and + $Body -like '*"matchKind":"Exact"*' -and + $Body -like '*"refName":"refs/heads/master"*' + } + } + + It 'should throw' { + ## Act / Assert + { Add-VSTeamPolicy -ProjectName boom -type babcf51f-d853-43a2-9b05-4a64ca577be0 -enabled -blocking -settings @{ + MinimumApproverCount = 1 + scope = @( + @{ + refName = 'refs/heads/master' + matchKind = 'Exact' + repositoryId = '10000000-0000-0000-0000-0000000000001' + }) + } + } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamProfile.Tests.ps1 b/unit/test/Add-VSTeamProfile.Tests.ps1 new file mode 100644 index 000000000..d81ed6ad6 --- /dev/null +++ b/unit/test/Add-VSTeamProfile.Tests.ps1 @@ -0,0 +1,119 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamProfile" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamProfile' { + $expectedPath = "$HOME/vsteam_profiles.json" + + Mock Set-Content + + Context 'Add-VSTeamProfile' { + Mock Write-Error + Mock Get-VSTeamProfile + Mock _isOnWindows { return $true } + Mock _convertSecureStringTo_PlainText { return '' } + + It 'on Windows no data provided should save profile to disk' { + $emptySecureString = ConvertTo-SecureString 'does not matter because mock is going to return empty string' -AsPlainText -Force + + Add-VSTeamProfile -Account testing -SecurePersonalAccessToken $emptySecureString + + Assert-MockCalled Set-Content -Exactly -Scope It -Times 0 + Assert-MockCalled Write-Error -Exactly -Scope It -Times 1 -ParameterFilter { + $Message -eq 'Personal Access Token must be provided if you are not using Windows Authentication; please see the help.' + } + } + + It 'with PAT to empty file should save profile to disk' { + Add-VSTeamProfile -Account demos -PersonalAccessToken 12345 + + Assert-MockCalled Set-Content -Exactly -Scope It -Times 1 -ParameterFilter { + $Path -eq $expectedPath -and $Value -like "*https://dev.azure.com/demos*" -and $Value -like "*VSTS*" + } + } + + It 'with PAT to empty array should save profile to disk' { + Add-VSTeamProfile -Account demos -PersonalAccessToken 12345 -Version VSTS + + Assert-MockCalled Set-Content -Exactly -Scope It -Times 1 -ParameterFilter { + $Path -eq $expectedPath -and $Value -like "*https://dev.azure.com/demos*" -and $Value -like "*VSTS*" + } + } + + It 'with OAuth to empty array should save profile to disk' { + Add-VSTeamProfile -Account demos -PersonalAccessToken 12345 -Version VSTS -UseBearerToken + + Assert-MockCalled Set-Content -Exactly -Scope It -Times 1 -ParameterFilter { + $Path -eq $expectedPath -and $Value -like "*https://dev.azure.com/demos*" -and $Value -like "*VSTS*" + } + } + } + + Context 'Add-VSTeamProfile with PAT exisiting entry' { + Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } + + It 'Should save profile to disk' { + Add-VSTeamProfile -Account demos -PersonalAccessToken 12345 -Version TFS2018 + + Assert-MockCalled Set-Content -Exactly -Scope It -Times 1 -ParameterFilter { + $Path -eq $expectedPath -and $Value -like "*https://dev.azure.com/demos*" -and $Value -like "*https://dev.azure.com/test*" -and $Value -like "*TFS2018*" + } + } + } + + Context 'Add-VSTeamProfile TFS default to TFS2017 with Windows Auth' { + Mock _isOnWindows { return $true } + Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test/","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } + + It 'Should save profile to disk' { + Add-VSTeamProfile -Account http://localhost:8080/tfs/defaultcollection -UseWindowsAuthentication + + Assert-MockCalled Set-Content -Exactly -Scope It -Times 1 -ParameterFilter { + $Path -eq $expectedPath -and $Value -like "*OnPremise*" -and $Value -like "*http://localhost:8080/tfs/defaultcollection*" -and $Value -like "*TFS2017*" + } + } + } + + Context 'Add-VSTeamProfile TFS default to TFS2017' { + Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test/","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } + + It 'Should save profile to disk' { + Add-VSTeamProfile -Account http://localhost:8080/tfs/defaultcollection -PersonalAccessToken 678910 + + Assert-MockCalled Set-Content -Exactly -Scope It -Times 1 -ParameterFilter { + $Path -eq $expectedPath -and $Value -like "*OjY3ODkxMA==*" -and $Value -like "*http://localhost:8080/tfs/defaultcollection*" -and $Value -like "*TFS2017*" + } + } + } + + Context 'Add-VSTeamProfile with PAT replace exisiting entry' { + Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test/","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } + + It 'Should save profile to disk' { + Add-VSTeamProfile -Account test -PersonalAccessToken 678910 + Assert-MockCalled Set-Content -Exactly -Scope It -Times 1 -ParameterFilter { + $Path -eq $expectedPath -and $Value -like "*OjY3ODkxMA==*" -and $Value -like "*https://dev.azure.com/test*" -and $Value -like "*VSTS*" + } + } + } + + Context 'Add-VSTeamProfile with existing old URL' { + Mock Test-Path { return $true } + Mock Get-Content { return '[{"Name":"test","URL":"https://test.visualstudio.com","Type":"Pat","Pat":"12345","Version":"VSTS"}]' } + + It 'Should save profile with new URL to disk' { + Add-VSTeamProfile -Account test -PersonalAccessToken 678910 + Assert-MockCalled Set-Content -Exactly -Scope It -Times 1 -ParameterFilter { + $Path -eq $expectedPath -and $Value -like "*OjY3ODkxMA==*" -and $Value -like "*https://dev.azure.com/test*" -and $Value -like "*VSTS*" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamProject.Tests.ps1 b/unit/test/Add-VSTeamProject.Tests.ps1 new file mode 100644 index 000000000..bb68586ac --- /dev/null +++ b/unit/test/Add-VSTeamProject.Tests.ps1 @@ -0,0 +1,145 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamProcessCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinition.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamQueue.ps1" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" +. "$here/../../Source/Public/Get-VSTeamBuildDefinition.ps1" +. "$here/../../Source/Public/Get-VSTeamProcess.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamProject' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + . "$PSScriptRoot\mocks\mockProcessNameDynamicParam.ps1" + + $singleResult = [PSCustomObject]@{ + name = 'Test' + description = '' + url = '' + id = '123-5464-dee43' + state = '' + visibility = '' + revision = 0 + defaultTeam = [PSCustomObject]@{ } + _links = [PSCustomObject]@{ } + } + + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } + + Context 'Add-VSTeamProject' { + Mock Write-Progress + + # Add Project + Mock Invoke-RestMethod { return @{status = 'inProgress'; id = '123-5464-dee43'; url = 'https://someplace.com' } } -ParameterFilter { + $Method -eq 'Post' -and + $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$(_getApiVersion Core)" + } + + # Track Progress + Mock Invoke-RestMethod { + # This $i is in the module. Because we use InModuleScope + # we can see it + if ($i -gt 9) { + return @{status = 'succeeded' } + } + + return @{status = 'inProgress' } + } -ParameterFilter { + $Uri -eq 'https://someplace.com' + } + + # Get-VSTeamProject + Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$(_getApiVersion Core)" + } + + It 'with tfvc should create project with tfvc' { + Add-VSTeamProject -Name Test -tfvc + + Assert-MockCalled Invoke-RestMethod -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$(_getApiVersion Core)" + } + Assert-MockCalled Invoke-RestMethod -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Post' -and + $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$(_getApiVersion Core)" -and + $Body -eq '{"name": "Test", "description": "", "capabilities": {"versioncontrol": { "sourceControlType": "Tfvc"}, "processTemplate":{"templateTypeId": "6b724908-ef14-45cf-84f8-768b5384da45"}}}' + } + } + } + + Context 'Add-VSTeamProject with Agile' { + + Mock Invoke-RestMethod { return @{status = 'inProgress'; id = 1; url = 'https://someplace.com' } } -ParameterFilter { $Method -eq 'Post' -and $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$(_getApiVersion Core)" } + Mock _trackProjectProgress + Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$(_getApiVersion Core)" } + Mock Get-VSTeamProcess { return @{name = 'Agile'; id = 1 } } + + It 'Should create project with Agile' { + Add-VSTeamProject -ProjectName Test -processTemplate Agile + + Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$(_getApiVersion Core)" } + Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Method -eq 'Post' -and $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$(_getApiVersion Core)" } + } + } + + Context 'Add-VSTeamProject with CMMI' { + + Mock Invoke-RestMethod { return @{status = 'inProgress'; id = 1; url = 'https://someplace.com' } } -ParameterFilter { $Method -eq 'Post' -and $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$(_getApiVersion Core)" } + Mock _trackProjectProgress + Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$(_getApiVersion Core)" } + Mock Get-VSTeamProcess { return @{name = 'CMMI'; id = 1 } } + + It 'Should create project with CMMI' { + Add-VSTeamProject -ProjectName Test -processTemplate CMMI + + Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$(_getApiVersion Core)" } + Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Method -eq 'Post' -and $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$(_getApiVersion Core)" } + } + } + + Context 'Add-VSTeamProject throws error' { + + Mock Invoke-RestMethod { return @{status = 'inProgress'; id = 1; url = 'https://someplace.com' } } -ParameterFilter { $Method -eq 'Post' -and $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$(_getApiVersion Core)" } + Mock Write-Error + Mock _trackProjectProgress { throw 'Test error' } + Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$(_getApiVersion Core)" } + + It '_trackProjectProgress errors should throw' { { Add-VSTeamProject -projectName Test -processTemplate CMMI } | Should throw + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamProjectPermission.Tests.ps1 b/unit/test/Add-VSTeamProjectPermission.Tests.ps1 new file mode 100644 index 000000000..8e563c311 --- /dev/null +++ b/unit/test/Add-VSTeamProjectPermission.Tests.ps1 @@ -0,0 +1,132 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamGroup.ps1" +. "$here/../../Source/Classes/VSTeamUser.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamSecurityNamespace.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamProjectPermissions.ps1" +. "$here/../../Source/Classes/VSTeamAccessControlEntry.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Add-VSTeamAccessControlEntry.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamProjectPermission' { + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + $userSingleResult = Get-Content "$PSScriptRoot\sampleFiles\users.single.json" -Raw | ConvertFrom-Json + $userSingleResultObject = [VSTeamUser]::new($userSingleResult) + + $groupSingleResult = Get-Content "$PSScriptRoot\sampleFiles\groupsSingle.json" -Raw | ConvertFrom-Json + $groupSingleResultObject = [VSTeamGroup]::new($groupSingleResult) + + $projectResult = [PSCustomObject]@{ + name = 'Test Project Public' + description = '' + url = '' + id = '010d06f0-00d5-472a-bb47-58947c230876' + state = '' + visibility = '' + revision = 0 + defaultTeam = [PSCustomObject]@{ } + _links = [PSCustomObject]@{ } + } + + $projectResultObject = [VSTeamProject]::new($projectResult) + + $accessControlEntryResult = + @" +{ + "count": 1, + "value": [ + { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", + "allow": 8, + "deny": 0, + "extendedInfo": {} + } + ] +} +"@ | ConvertFrom-Json + + # You have to set the version or the api-version will not be added when versions = '' + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + Context 'Add-VSTeamProjectPermission by ProjectUser' { + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + # Write-Host $args + + return $accessControlEntryResult + } -Verifiable + + It 'by ProjectUser should return ACEs' { + Add-VSTeamProjectPermission -Project $projectResultObject -User $userSingleResultObject -Allow ([VSTeamProjectPermissions]'GENERIC_READ,GENERIC_WRITE,WORK_ITEM_DELETE,RENAME') -Deny ([VSTeamProjectPermissions]'CHANGE_PROCESS,VIEW_TEST_RESULTS') + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/52d39943-cb85-4d7f-8fa8-c6baac873819*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"`$PROJECT:vstfs:///Classification/TeamProject/010d06f0-00d5-472a-bb47-58947c230876`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and + $Body -like "*`"allow`": 73731,*" -and + $Body -like "*`"deny`": 8389120,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by ProjectGroup should return ACEs' { + Add-VSTeamProjectPermission -Project $projectResultObject -Group $groupSingleResultObject -Allow ([VSTeamProjectPermissions]'GENERIC_READ,GENERIC_WRITE,WORK_ITEM_DELETE,RENAME') -Deny ([VSTeamProjectPermissions]'CHANGE_PROCESS,VIEW_TEST_RESULTS') + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/52d39943-cb85-4d7f-8fa8-c6baac873819*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"`$PROJECT:vstfs:///Classification/TeamProject/010d06f0-00d5-472a-bb47-58947c230876`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 73731,*" -and + $Body -like "*`"deny`": 8389120,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by ProjectDescriptor should return ACEs' { + Add-VSTeamProjectPermission -Project $projectResultObject -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamProjectPermissions]'GENERIC_READ,GENERIC_WRITE,WORK_ITEM_DELETE,RENAME') -Deny ([VSTeamProjectPermissions]'CHANGE_PROCESS,VIEW_TEST_RESULTS') + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/52d39943-cb85-4d7f-8fa8-c6baac873819*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"`$PROJECT:vstfs:///Classification/TeamProject/010d06f0-00d5-472a-bb47-58947c230876`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 73731,*" -and + $Body -like "*`"deny`": 8389120,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamPullRequest.Tests.ps1 b/unit/test/Add-VSTeamPullRequest.Tests.ps1 index f4ff47585..1e31e6de9 100644 --- a/unit/test/Add-VSTeamPullRequest.Tests.ps1 +++ b/unit/test/Add-VSTeamPullRequest.Tests.ps1 @@ -1,70 +1,79 @@ Set-StrictMode -Version Latest -InModuleScope VSTeam { +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") - Describe 'Pull Requests' { - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +#endregion - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable +Describe 'VSTeamPullRequest' { + ## Arrange + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Core = '' - [VSTeamVersions]::Git = '5.1-preview' - [VSTeamVersions]::Graph = '5.0' + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - $result = Get-Content "$PSScriptRoot\sampleFiles\updatePullRequestResponse.json" -Raw | ConvertFrom-Json + # You have to set the version or the api-version will not be added when versions = '' + Mock _getApiVersion { return '1.0-gitUnitTests' } -ParameterFilter { $Service -eq 'Git' } - Context 'Add-VSTeamPullRequest' { + $result = Get-Content "$PSScriptRoot\sampleFiles\updatePullRequestResponse.json" -Raw | ConvertFrom-Json - It 'Add-VSTeamPullRequest as Draft' { - Mock Invoke-RestMethod { return $result } + Context 'Add-VSTeamPullRequest' { + Mock Invoke-RestMethod { return $result } - Add-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -ProjectName "Sandbox" ` - -Title "PR Title" -Description "PR Description" ` - -SourceRefName "refs/heads/test" -TargetRefName "refs/heads/master" ` - -Draft -Force + It 'Add-VSTeamPullRequest as Draft' { + ## Act + Add-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -ProjectName "Sandbox" ` + -Title "PR Title" -Description "PR Description" ` + -SourceRefName "refs/heads/test" -TargetRefName "refs/heads/master" ` + -Draft -Force - Assert-MockCalled Invoke-RestMethod -Scope It -ParameterFilter { - $Method -eq 'Post' -and - $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and - $Uri -like "*pullrequests*" -and - $Body -eq '{"sourceRefName": "refs/heads/test", "targetRefName": "refs/heads/master", "title": "PR Title", "description": "PR Description", "isDraft": true}' - } - } + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Post' -and + $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and + $Uri -like "*pullrequests*" -and + $Body -eq '{"sourceRefName": "refs/heads/test", "targetRefName": "refs/heads/master", "title": "PR Title", "description": "PR Description", "isDraft": true}' + } + } - It 'Add-VSTeamPullRequest as Published' { - Mock Invoke-RestMethod { return $result } + It 'Add-VSTeamPullRequest as Published' { + ## Act + Add-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -ProjectName "Sandbox" ` + -Title "PR Title" -Description "PR Description" ` + -SourceRefName "refs/heads/test" -TargetRefName "refs/heads/master" ` + -Force - Add-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -ProjectName "Sandbox" ` - -Title "PR Title" -Description "PR Description" ` - -SourceRefName "refs/heads/test" -TargetRefName "refs/heads/master" ` - -Force + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Post' -and + $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and + $Uri -like "*pullrequests*" -and + $Body -eq '{"sourceRefName": "refs/heads/test", "targetRefName": "refs/heads/master", "title": "PR Title", "description": "PR Description", "isDraft": false}' + } + } - Assert-MockCalled Invoke-RestMethod -Scope It -ParameterFilter { - $Method -eq 'Post' -and - $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and - $Uri -like "*pullrequests*" -and - $Body -eq '{"sourceRefName": "refs/heads/test", "targetRefName": "refs/heads/master", "title": "PR Title", "description": "PR Description", "isDraft": false}' - } - } + It 'Add-VSTeamPullRequest with wrong -SourceRefName throws' { + ## Act / Assert + { + Add-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -ProjectName "Sandbox" ` + -Title "PR Title" -Description "PR Description" ` + -SourceRefName "garbage" -TargetRefName "refs/heads/master" ` + -Draft -Force + } | Should Throw + } - It 'Add-VSTeamPullRequest with wrong -SourceRefName throws' { - { - Add-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -ProjectName "Sandbox" ` - -Title "PR Title" -Description "PR Description" ` - -SourceRefName "garbage" -TargetRefName "refs/heads/master" ` - -Draft -Force - } | Should Throw - } - - It 'Add-VSTeamPullRequest with wrong -TargetRefName throws' { - { - Add-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -ProjectName "Sandbox" ` - -Title "PR Title" -Description "PR Description" ` - -SourceRefName "refs/heads/test" -TargetRefName "garbage" ` - -Draft -Force - } | Should Throw - } - } - } + It 'Add-VSTeamPullRequest with wrong -TargetRefName throws' { + ## Act / Assert + { + Add-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -ProjectName "Sandbox" ` + -Title "PR Title" -Description "PR Description" ` + -SourceRefName "refs/heads/test" -TargetRefName "garbage" ` + -Draft -Force + } | Should Throw + } + } } \ No newline at end of file diff --git a/unit/test/Add-VSTeamRelease.Tests.ps1 b/unit/test/Add-VSTeamRelease.Tests.ps1 new file mode 100644 index 000000000..d8858aaba --- /dev/null +++ b/unit/test/Add-VSTeamRelease.Tests.ps1 @@ -0,0 +1,147 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamFeed.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamBuild.ps1" +. "$here/../../Source/Public/Get-VSTeamReleaseDefinition.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamRelease' { + ## Arrange + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Release' } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + + $singleResult = [PSCustomObject]@{ + environments = [PSCustomObject]@{ } + variables = [PSCustomObject]@{ + BrowserToUse = [PSCustomObject]@{ + value = "phantomjs" + } + } + _links = [PSCustomObject]@{ + self = [PSCustomObject]@{ } + web = [PSCustomObject]@{ } + } + } + + Mock _hasProjectCacheExpired { return $false } + + Context 'Add-VSTeamRelease' { + ## Arrange + BeforeAll { + $Global:PSDefaultParameterValues["*:projectName"] = 'project' + } + + AfterAll { + $Global:PSDefaultParameterValues.Remove("*:projectName") + } + + Mock Get-VSTeamReleaseDefinition { + $def1 = New-Object -TypeName PSObject -Prop @{name = 'Test1'; id = 1; artifacts = @(@{alias = 'drop' }) } + $def2 = New-Object -TypeName PSObject -Prop @{name = 'Tests'; id = 2; artifacts = @(@{alias = 'drop' }) } + return @( + $def1, + $def2 + ) + } + + Mock Get-VSTeamBuild { + $bld1 = New-Object -TypeName PSObject -Prop @{name = "Bld1"; id = 1 } + $bld2 = New-Object -TypeName PSObject -Prop @{name = "Bld2"; id = 2 } + + return @( + $bld1, + $bld2 + ) + } + + Mock Invoke-RestMethod { return $singleResult } + Mock Invoke-RestMethod { throw 'error' } -ParameterFilter { $Body -like "*101*" } + + Mock _buildDynamicParam { + param( + [string] $ParameterName = 'QueueName', + [array] $arrSet, + [bool] $Mandatory = $false, + [string] $ParameterSetName + ) + + # Create the collection of attributes + $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] + + # Create and set the parameters' attributes + $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute + $ParameterAttribute.Mandatory = $Mandatory + $ParameterAttribute.ValueFromPipelineByPropertyName = $true + + if ($ParameterSetName) { + $ParameterAttribute.ParameterSetName = $ParameterSetName + } + + # Add the attributes to the attributes collection + $AttributeCollection.Add($ParameterAttribute) + + if ($arrSet) { + # Generate and set the ValidateSet + $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) + + # Add the ValidateSet to the attributes collection + $AttributeCollection.Add($ValidateSetAttribute) + } + + # Create and return the dynamic parameter + return New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection) + } + + It 'by name should add a release' { + ## Act + Add-VSTeamRelease -ProjectName project -BuildNumber 'Bld1' -DefinitionName 'Test1' + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $Body -like '*"definitionId": 1*' -and + $Body -like '*"description": ""*' -and + $Body -like '*"alias": "drop"*' -and + $Body -like '*"id": "1"*' -and + $Body -like '*"sourceBranch": ""*' -and + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases?api-version=$(_getApiVersion Release)" + } + } + + It 'by Id should add a release' { + ## Act + Add-VSTeamRelease -ProjectName project -DefinitionId 1 -ArtifactAlias drop -BuildId 2 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $Body -like '*"definitionId": 1*' -and + $Body -like '*"description": ""*' -and + $Body -like '*"alias": "drop"*' -and + $Body -like '*"id": "2"*' -and + $Body -like '*"sourceBranch": ""*' -and + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases?api-version=$(_getApiVersion Release)" + } + } + + It 'should throw' { + ## Act / Assert + { Add-VSTeamRelease -ProjectName project -DefinitionId 101 -ArtifactAlias drop -BuildId 101 } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamReleaseDefinition.Tests.ps1 b/unit/test/Add-VSTeamReleaseDefinition.Tests.ps1 new file mode 100644 index 000000000..9206b620e --- /dev/null +++ b/unit/test/Add-VSTeamReleaseDefinition.Tests.ps1 @@ -0,0 +1,73 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamReleaseDefinition' { + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Release' } + + $results = [PSCustomObject]@{ + value = [PSCustomObject]@{ + queue = [PSCustomObject]@{ name = 'Default' } + _links = [PSCustomObject]@{ + self = [PSCustomObject]@{ } + web = [PSCustomObject]@{ } + } + retentionPolicy = [PSCustomObject]@{ } + lastRelease = [PSCustomObject]@{ } + artifacts = [PSCustomObject]@{ } + modifiedBy = [PSCustomObject]@{ name = 'project' } + createdBy = [PSCustomObject]@{ name = 'test' } + } + } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + Context 'Add-VSTeamReleaseDefinition' { + Mock Invoke-RestMethod { return $results } + + Context 'Services' { + Mock _getInstance { return 'https://dev.azure.com/test' } + + it 'should add release' { + ## Act + Add-VSTeamReleaseDefinition -projectName project -inFile 'Releasedef.json' + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $InFile -eq 'Releasedef.json' -and + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/definitions?api-version=$(_getApiVersion Release)" + } + } + } + + Context 'Server' { + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + it 'local Auth should add release' { + ## Act + Add-VSTeamReleaseDefinition -projectName project -inFile 'Releasedef.json' + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $InFile -eq 'Releasedef.json' -and + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/release/definitions?api-version=$(_getApiVersion Release)" + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamServiceFabricEndpoint.Tests.ps1 b/unit/test/Add-VSTeamServiceFabricEndpoint.Tests.ps1 new file mode 100644 index 000000000..5014f62c8 --- /dev/null +++ b/unit/test/Add-VSTeamServiceFabricEndpoint.Tests.ps1 @@ -0,0 +1,92 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Add-VSTeamServiceEndpoint.ps1" +. "$here/../../Source/Public/Get-VSTeamServiceEndpoint.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamServiceFabricEndpoint' { + Context 'Add-VSTeamServiceFabricEndpoint' { + Mock _hasProjectCacheExpired { return $false } + + Context 'Services' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'ServiceFabricEndpoint' } + + Mock Write-Progress + Mock Invoke-RestMethod { return @{id = '23233-2342' } } -ParameterFilter { $Method -eq 'Post' } + Mock Invoke-RestMethod { + # This $i is in the module. Because we use InModuleScope + # we can see it + if ($iTracking -gt 9) { + return [PSCustomObject]@{ + isReady = $true + operationStatus = [PSCustomObject]@{state = 'Ready' } + } + } + + return [PSCustomObject]@{ + isReady = $false + createdBy = [PSCustomObject]@{ } + authorization = [PSCustomObject]@{ } + data = [PSCustomObject]@{ } + operationStatus = [PSCustomObject]@{state = 'InProgress' } + } + } + + It 'should create a new Service Fabric Serviceendpoint' { + Add-VSTeamServiceFabricEndpoint -projectName 'project' -endpointName 'PM_DonovanBrown' -url "tcp://0.0.0.0:19000" -useWindowsSecurity $false + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } + } + + It 'with AzureAD authentication should create a new Service Fabric Serviceendpoint' { + $password = '00000000-0000-0000-0000-000000000000' | ConvertTo-SecureString -AsPlainText -Force + $username = "Test User" + $serverCertThumbprint = "0000000000000000000000000000000000000000" + Add-VSTeamServiceFabricEndpoint -projectName 'project' -endpointName 'PM_DonovanBrown' -url "tcp://0.0.0.0:19000" -username $username -password $password -serverCertThumbprint $serverCertThumbprint + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } + } + + It 'with Certificate authentication should create a new Service Fabric Serviceendpoint' { + $password = '00000000-0000-0000-0000-000000000000' | ConvertTo-SecureString -AsPlainText -Force + $base64Cert = "0000000000000000000000000000000000000000" + $serverCertThumbprint = "0000000000000000000000000000000000000000" + Add-VSTeamServiceFabricEndpoint -projectName 'project' -endpointName 'PM_DonovanBrown' -url "tcp://0.0.0.0:19000" -serverCertThumbprint $serverCertThumbprint -certificate $base64Cert -certificatePassword $password + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } + } + } + + Context 'Server' { + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + Mock _getApiVersion { return 'TFS2017' } + Mock _getApiVersion { return '' } -ParameterFilter { $Service -eq 'ServiceFabricEndpoint' } + + Context 'Add-VSTeamServiceFabricEndpoint' { + Mock ConvertTo-Json { throw 'Should not be called' } + + It 'Should throw' { + { Add-VSTeamServiceFabricEndpoint -projectName 'project' ` + -endpointName 'PM_DonovanBrown' -url "tcp://0.0.0.0:19000" ` + -useWindowsSecurity $false } | Should Throw + } + + It 'ConvertTo-Json should not be called' { + Assert-MockCalled ConvertTo-Json -Exactly -Times 0 -Scope Context + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamSonarQubeEndpoint.Tests.ps1 b/unit/test/Add-VSTeamSonarQubeEndpoint.Tests.ps1 new file mode 100644 index 000000000..058966a8b --- /dev/null +++ b/unit/test/Add-VSTeamSonarQubeEndpoint.Tests.ps1 @@ -0,0 +1,52 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Add-VSTeamServiceEndpoint.ps1" +. "$here/../../Source/Public/Get-VSTeamServiceEndpoint.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamSonarQubeEndpoint' { + Context 'Add-VSTeamSonarQubeEndpoint' { + Mock _hasProjectCacheExpired { return $false } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'ServiceFabricEndpoint' } + + Mock Write-Warning + Mock Write-Error -Verifiable + Mock Invoke-RestMethod { + $e = [System.Management.Automation.ErrorRecord]::new( + [System.Net.WebException]::new("Endpoint type couldn't be recognized 'sonarqube'", [System.Net.WebExceptionStatus]::ProtocolError), + "Endpoint type couldn't be recognized 'sonarqube'", + [System.Management.Automation.ErrorCategory]::ProtocolError, + $null) + + # The error message is different on TFS and VSTS + $msg = ConvertTo-Json @{ + '$id' = 1 + message = "Unable to find service endpoint type 'sonarqube' using authentication scheme 'UsernamePassword'." + } + + $e.ErrorDetails = [System.Management.Automation.ErrorDetails]::new($msg) + + $PSCmdlet.ThrowTerminatingError($e) + } + + It 'should not create SonarQube Serviceendpoint' { + Add-VSTeamSonarQubeEndpoint -projectName 'project' -endpointName 'PM_DonovanBrown' -sonarqubeUrl 'http://mysonarserver.local' -personalAccessToken '00000000-0000-0000-0000-000000000000' + + ## Verify that Write-Error was called + Assert-VerifiableMock + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamTaskGroup.Tests.ps1 b/unit/test/Add-VSTeamTaskGroup.Tests.ps1 new file mode 100644 index 000000000..2e6773f94 --- /dev/null +++ b/unit/test/Add-VSTeamTaskGroup.Tests.ps1 @@ -0,0 +1,59 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +$taskGroupsJson = "$PSScriptRoot\sampleFiles\taskGroups.json" +$taskGroupJson = "$PSScriptRoot\sampleFiles\taskGroup.json" + +Describe 'VSTeamTaskGroup' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'TaskGroups' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/project*" } + + BeforeAll { + $projectName = "project" + $taskGroupJsonAsString = Get-Content $taskGroupJson -Raw + } + + Context 'Add-VSTeamTaskGroup' { + Mock Invoke-RestMethod { + return Get-Content $taskGroupJson | ConvertFrom-Json + } + + It 'should create a task group using body param' { + Add-VSTeamTaskGroup -ProjectName $projectName -Body $taskGroupJsonAsString + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups?api-version=$(_getApiVersion TaskGroups)" -and + $Body -eq $taskGroupJsonAsString -and + $Method -eq "Post" + } + } + + It 'should create a task group using infile param' { + Add-VSTeamTaskGroup -ProjectName $projectName -InFile $taskGroupJson + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups?api-version=$(_getApiVersion TaskGroups)" -and + $InFile -eq $taskGroupJson -and + $Method -eq "Post" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamUserEntitlement.Tests.ps1 b/unit/test/Add-VSTeamUserEntitlement.Tests.ps1 new file mode 100644 index 000000000..a03915981 --- /dev/null +++ b/unit/test/Add-VSTeamUserEntitlement.Tests.ps1 @@ -0,0 +1,56 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamUserEntitlement.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe "VSTeamUserEntitlement" { + Context 'Add-VSTeamUserEntitlement' { + Mock _getApiVersion { return 'VSTS' } + # This will cause the call the _getProject to be skipped + Mock _hasProjectCacheExpired { return $false } + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'MemberEntitlementManagement' } + + $obj = @{ + accessLevel = @{ + accountLicenseType = 'earlyAdopter' + licensingSource = 'msdn' + msdnLicenseType = 'enterprise' + } + user = @{ + principalName = 'test@user.com' + subjectKind = 'user' + } + projectEntitlements = @{ + group = @{ + groupType = 'ProjectContributor' + } + projectRef = @{ + id = $null + } + } + } + + $expected = $obj | ConvertTo-Json + + Mock _callAPI -ParameterFilter { + $Method -eq 'Post' -and + $Body -eq $expected + } + + Add-VSTeamUserEntitlement -License earlyAdopter -LicensingSource msdn -MSDNLicenseType enterprise -Email 'test@user.com' + + It 'Should add a user' { + Assert-VerifiableMock + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamVariableGroup.Tests.ps1 b/unit/test/Add-VSTeamVariableGroup.Tests.ps1 new file mode 100644 index 000000000..4ac55e56c --- /dev/null +++ b/unit/test/Add-VSTeamVariableGroup.Tests.ps1 @@ -0,0 +1,104 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/Get-VSTeamVariableGroup.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamVariableGroup' { + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock _hasProjectCacheExpired { return $false } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'VariableGroups' } + + Context 'Add-VSTeamVariableGroup' { + Context 'Services' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + + $sampleFileVSTS = $(Get-Content "$PSScriptRoot\sampleFiles\variableGroupSamples.json" | ConvertFrom-Json) + + Mock _getInstance { return 'https://dev.azure.com/test' } + + BeforeAll { + Set-VSTeamAPIVersion -Target VSTS + } + + Mock Invoke-RestMethod { return $sampleFileVSTS.value[0] } + + It 'should create a new AzureRM Key Vault Variable Group' { + $testParameters = @{ + ProjectName = "project" + Name = "TestVariableGroup2" + Description = "A test variable group linked to an Azure KeyVault" + Type = "AzureKeyVault" + Variables = @{ + key3 = @{ + enabled = $true + contentType = "" + value = "" + isSecret = $true + } + } + ProviderData = @{ + serviceEndpointId = "0228e842-65a7-4c64-90f7-0f07f3aa4e10" + vault = "keyVaultName" + } + } + + Add-VSTeamVariableGroup @testParameters + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } + } + + It "should create a new var group when passing the json as the body" { + $body = $sampleFileVSTS + $projName = "project" + Add-VSTeamVariableGroup -Body $body -ProjectName $projName + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/$projName/_apis/distributedtask/variablegroups?api-version=$(_getApiVersion VariableGroups)" -and + $Method -eq 'Post' + } + } + } + + Context 'Server' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + $sampleFile2017 = $(Get-Content "$PSScriptRoot\sampleFiles\variableGroupSamples2017.json" | ConvertFrom-Json) + + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable + + BeforeAll { + Set-VSTeamAPIVersion -Target TFS2017 + } + + Mock Invoke-RestMethod { return $sampleFile2017.value[0] } + + It 'should create a new Variable Group' { + $testParameters = @{ + ProjectName = "project" + Name = "TestVariableGroup2" + Description = "A test variable group linked to an Azure KeyVault" + Variables = @{ + key1 = @{ + value = "value" + } + } + } + + Add-VSTeamVariableGroup @testParameters + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamWorkItem.Tests.ps1 b/unit/test/Add-VSTeamWorkItem.Tests.ps1 new file mode 100644 index 000000000..18aa16900 --- /dev/null +++ b/unit/test/Add-VSTeamWorkItem.Tests.ps1 @@ -0,0 +1,136 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamWorkItem' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + $obj = @{ + id = 47 + rev = 1 + url = "https://dev.azure.com/test/_apis/wit/workItems/47" + } + + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + # Write-Host $args + + return $obj + } + + Context 'Add-VSTeamWorkItem' { + It 'Without Default Project should add work item' { + $Global:PSDefaultParameterValues.Remove("*:projectName") + Add-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $Body -like '`[*' -and # Make sure the body is an array + $Body -like '*`]' -and # Make sure the body is an array + $ContentType -eq 'application/json-patch+json' -and + $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitems/`$Task?api-version=$(_getApiVersion Core)" + } + } + + It 'With Default Project should add work item' { + $Global:PSDefaultParameterValues["*:projectName"] = 'test' + Add-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test1 -Description Testing + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $Body -like '`[*' -and # Make sure the body is an array + $Body -like '*Test1*' -and + $Body -like '*Testing*' -and + $Body -like '*/fields/System.Title*' -and + $Body -like '*/fields/System.Description*' -and + $Body -like '*`]' -and # Make sure the body is an array + $ContentType -eq 'application/json-patch+json' -and + $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitems/`$Task?api-version=$(_getApiVersion Core)" + } + } + + It 'With Default Project should add work item with parent' { + $Global:PSDefaultParameterValues["*:projectName"] = 'test' + Add-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test1 -Description Testing -ParentId 25 + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $Body -like '`[*' -and # Make sure the body is an array + $Body -like '*Test1*' -and + $Body -like '*Testing*' -and + $Body -like '*/fields/System.Title*' -and + $Body -like '*/fields/System.Description*' -and + $Body -like '*/relations/-*' -and + $Body -like '*_apis/wit/workitems/25*' -and + $Body -like '*System.LinkTypes.Hierarchy-Reverse*' -and + $Body -like '*`]' -and # Make sure the body is an array + $ContentType -eq 'application/json-patch+json' -and + $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitems/`$Task?api-version=$(_getApiVersion Core)" + } + } + + It 'With Default Project should add work item only with additional properties and parent id' { + $Global:PSDefaultParameterValues["*:projectName"] = 'test' + + $additionalFields = @{"System.Tags" = "TestTag"; "System.AreaPath" = "Project\\MyPath" } + Add-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test1 -Description Testing -ParentId 25 -AdditionalFields $additionalFields + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $Body -like '`[*' -and # Make sure the body is an array + $Body -like '*Test1*' -and + $Body -like '*Testing*' -and + $Body -like '*/fields/System.Title*' -and + $Body -like '*/fields/System.Description*' -and + $Body -like '*/relations/-*' -and + $Body -like '*_apis/wit/workitems/25*' -and + $Body -like '*/fields/System.Tags*' -and + $Body -like '*/fields/System.AreaPath*' -and + $Body -like '*`]' -and # Make sure the body is an array + $ContentType -eq 'application/json-patch+json' -and + $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitems/`$Task?api-version=$(_getApiVersion Core)" + } + } + + It 'With Default Project should add work item only with additional properties' { + $Global:PSDefaultParameterValues["*:projectName"] = 'test' + + $additionalFields = @{"System.Tags" = "TestTag"; "System.AreaPath" = "Project\\MyPath" } + Add-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test1 -AdditionalFields $additionalFields + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $Body -like '`[*' -and # Make sure the body is an array + $Body -like '*Test1*' -and + $Body -like '*/fields/System.Title*' -and + $Body -like '*/fields/System.Tags*' -and + $Body -like '*/fields/System.AreaPath*' -and + $Body -like '*`]' -and # Make sure the body is an array + $ContentType -eq 'application/json-patch+json' -and + $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitems/`$Task?api-version=$(_getApiVersion Core)" + } + } + + It 'With Default Project should throw exception when adding existing parameters to additional properties and parent id' { + $Global:PSDefaultParameterValues["*:projectName"] = 'test' + + $additionalFields = @{"System.Title" = "Test1"; "System.AreaPath" = "Project\\TestPath" } + { Add-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test1 -Description Testing -ParentId 25 -AdditionalFields $additionalFields } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamWorkItemAreaPermission.Tests.ps1 b/unit/test/Add-VSTeamWorkItemAreaPermission.Tests.ps1 new file mode 100644 index 000000000..ab28e9e03 --- /dev/null +++ b/unit/test/Add-VSTeamWorkItemAreaPermission.Tests.ps1 @@ -0,0 +1,413 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamWorkItemAreaPermissions.ps1" +. "$here/../../Source/Classes/VSTeamClassificationNode.ps1" +. "$here/../../Source/Classes/VSTeamGroup.ps1" +. "$here/../../Source/Classes/VSTeamUser.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamAccessControlEntry.ps1" +. "$here/../../Source/Classes/VSTeamSecurityNamespace.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/Get-VSTeamClassificationNode.ps1" +. "$here/../../Source/Public/Add-VSTeamAccessControlEntry.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +$userSingleResult = Get-Content "$PSScriptRoot\sampleFiles\users.single.json" -Raw | ConvertFrom-Json +$userSingleResultObject = [VSTeamUser]::new($userSingleResult) + +$groupSingleResult = Get-Content "$PSScriptRoot\sampleFiles\groupsSingle.json" -Raw | ConvertFrom-Json +$groupSingleResultObject = [VSTeamGroup]::new($groupSingleResult) + +$projectResult = [PSCustomObject]@{ + name = 'Test Project Public' + description = '' + url = '' + id = '010d06f0-00d5-472a-bb47-58947c230876' + state = '' + visibility = '' + revision = 0 + defaultTeam = [PSCustomObject]@{ } + _links = [PSCustomObject]@{ } +} + +$projectResultObject = [VSTeamProject]::new($projectResult) + +$accessControlEntryResult = +@" +{ + "count": 1, + "value": [ + { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", + "allow": 8, + "deny": 0, + "extendedInfo": {} + } + ] +} +"@ | ConvertFrom-Json + +$classificationNodeById = +@" +{ + "count": 1, + "value": [ + { + "id": 44, + "identifier": "90aa2c42-de51-450a-bfb6-6e264e364d9a", + "name": "Child 1 Level 2", + "structureType": "area", + "hasChildren": false, + "path": "\\Demo Public\\Area\\Child 1 Level 1\\Child 1 Level 2", + "_links": { + "self": { + "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201/Child%201%20Level%202" + }, + "parent": { + "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201" + } + }, + "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201/Child%201%20Level%202" + } + ] + } +"@ | ConvertFrom-Json | Select-Object -ExpandProperty value + +$classificationNodeByIdObject = [VSTeamClassificationNode]::new($classificationNodeById, "test") + +$parentClassificationNode = +@" +{ + "count": 1, + "value": [ + { + "id": 43, + "identifier": "38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b", + "name": "Child 1 Level 1", + "structureType": "area", + "hasChildren": true, + "path": "\\Demo Public\\Area\\Child 1 Level 1", + "_links": { + "self": { + "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201" + }, + "parent": { + "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas" + } + }, + "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201" + } + ] + } +"@ | ConvertFrom-Json | Select-Object -ExpandProperty value + +$parentClassificationNodeObject = [VSTeamClassificationNode]::new($parentClassificationNode, "test") + +$areaRootNode = +@" +{ + "id": 24, + "identifier": "b33b12d7-6abb-4b7a-b9d6-2092d0933c99", + "name": "Demo Public", + "structureType": "area", + "hasChildren": true, + "path": "\\Demo Public\\Area", + "_links": { + "self": { + "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas" + } + }, + "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas" + } +"@ | ConvertFrom-Json + +$areaRootNodeObject = [VSTeamClassificationNode]::new($areaRootNode, "test") + +Describe 'VSTeamWorkItemAreaPermission' { + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + # You have to set the version or the api-version will not be added when versions = '' + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + Context 'Add-VSTeamWorkItemAreaPermission' { + # Mock _getProjects { return "Test Project Public" } + Mock _hasProjectCacheExpired { return $false } + Mock Get-VSTeamClassificationNode { return $areaRootNodeObject } + Mock Get-VSTeamClassificationNode { return $parentClassificationNodeObject } -ParameterFilter { $Path -eq "Child%201%20Level%201" } + Mock Get-VSTeamClassificationNode { return $classificationNodeByIdObject } -ParameterFilter { $Ids -eq 44 -or $Path -eq "Child 1 Level 1/Child 1 Level 2" } + + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + # Write-Host $args + return $accessControlEntryResult + } + + It 'by AreaID and User should return ACEs' { + Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaID 44 -User $userSingleResultObject -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/83e28ad4-2d72-4ceb-97b0-c7726d5502c3*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"vstfs:///Classification/Node/b33b12d7-6abb-4b7a-b9d6-2092d0933c99:vstfs:///Classification/Node/38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b:vstfs:///Classification/Node/90aa2c42-de51-450a-bfb6-6e264e364d9a`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and + $Body -like "*`"allow`": 65,*" -and + $Body -like "*`"deny`": 10,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by AreaID and Group should return ACEs' { + Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaID 44 -Group $groupSingleResultObject -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/83e28ad4-2d72-4ceb-97b0-c7726d5502c3*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"vstfs:///Classification/Node/b33b12d7-6abb-4b7a-b9d6-2092d0933c99:vstfs:///Classification/Node/38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b:vstfs:///Classification/Node/90aa2c42-de51-450a-bfb6-6e264e364d9a`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 65,*" -and + $Body -like "*`"deny`": 10,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by AreaID and Descriptor should return ACEs' { + Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaID 44 -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/83e28ad4-2d72-4ceb-97b0-c7726d5502c3*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"vstfs:///Classification/Node/b33b12d7-6abb-4b7a-b9d6-2092d0933c99:vstfs:///Classification/Node/38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b:vstfs:///Classification/Node/90aa2c42-de51-450a-bfb6-6e264e364d9a`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 65,*" -and + $Body -like "*`"deny`": 10,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by AreaPath and User should return ACEs' { + Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaPath "Child 1 Level 1/Child 1 Level 2" -User $userSingleResultObject -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/83e28ad4-2d72-4ceb-97b0-c7726d5502c3*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"vstfs:///Classification/Node/b33b12d7-6abb-4b7a-b9d6-2092d0933c99:vstfs:///Classification/Node/38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b:vstfs:///Classification/Node/90aa2c42-de51-450a-bfb6-6e264e364d9a`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and + $Body -like "*`"allow`": 65,*" -and + $Body -like "*`"deny`": 10,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by AreaPath and Group should return ACEs' { + Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaPath "Child 1 Level 1/Child 1 Level 2" -Group $groupSingleResultObject -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/83e28ad4-2d72-4ceb-97b0-c7726d5502c3*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"vstfs:///Classification/Node/b33b12d7-6abb-4b7a-b9d6-2092d0933c99:vstfs:///Classification/Node/38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b:vstfs:///Classification/Node/90aa2c42-de51-450a-bfb6-6e264e364d9a`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 65,*" -and + $Body -like "*`"deny`": 10,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by AreaPath and Descriptor should return ACEs' { + Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaPath "Child 1 Level 1/Child 1 Level 2" -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/83e28ad4-2d72-4ceb-97b0-c7726d5502c3*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"vstfs:///Classification/Node/b33b12d7-6abb-4b7a-b9d6-2092d0933c99:vstfs:///Classification/Node/38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b:vstfs:///Classification/Node/90aa2c42-de51-450a-bfb6-6e264e364d9a`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 65,*" -and + $Body -like "*`"deny`": 10,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + } + + # Context 'Add-VSTeamWorkItemIterationPermission by IterationID and User' { + # Mock _getProjects { return "Test Project Public" } -Verifiable + # Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Ids -eq 44 } -Verifiable + # Mock Get-VSTeamClassificationNode { return $iterationRootNodeObject } + + # Mock Invoke-RestMethod { + # # If this test fails uncomment the line below to see how the mock was called. + # # Write-Host $args + + # return $accessControlEntryResult + # } -Verifiable + + # Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationID 44 -User $userSingleResultObject -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') + + # It 'Should return ACEs' { + # Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + # $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and + # $Uri -like "*api-version=$(_getApiVersion Core)*" -and + # $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and + # $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and + # $Body -like "*`"allow`": 5,*" -and + # $Body -like "*`"deny`": 8,*" -and + # $ContentType -eq "application/json" -and + # $Method -eq "Post" + # } + # } + # } + + # Context 'Add-VSTeamWorkItemIterationPermission by IterationID and Group' { + # Mock _getProjects { return "Test Project Public" } -Verifiable + # Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Ids -eq 44 } -Verifiable + # Mock Get-VSTeamClassificationNode { return $iterationRootNodeObject } + # Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable + + # Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationID 44 -Group $groupSingleResultObject -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') + + # It 'Should return ACEs' { + # Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + # $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and + # $Uri -like "*api-version=$(_getApiVersion Core)*" -and + # $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and + # $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + # $Body -like "*`"allow`": 5,*" -and + # $Body -like "*`"deny`": 8,*" -and + # $ContentType -eq "application/json" -and + # $Method -eq "Post" + # } + # } + # } + + # Context 'Add-VSTeamWorkItemIterationPermission by IterationID and Descriptor' { + # Mock _getProjects { return "Test Project Public" } -Verifiable + # Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Ids -eq 44 } -Verifiable + # Mock Get-VSTeamClassificationNode { return $iterationRootNodeObject } + # Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable + + # Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationID 44 -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') + + # It 'Should return ACEs' { + # Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + # $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and + # $Uri -like "*api-version=$(_getApiVersion Core)*" -and + # $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and + # $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + # $Body -like "*`"allow`": 5,*" -and + # $Body -like "*`"deny`": 8,*" -and + # $ContentType -eq "application/json" -and + # $Method -eq "Post" + # } + # } + # } + + # Context 'Add-VSTeamWorkItemIterationPermission by IterationPath and User' { + # Mock _getProjects { return "Test Project Public" } -Verifiable + # Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Path -eq "Sprint 1" } -Verifiable + # Mock Get-VSTeamClassificationNode { return $iterationRootNodeObject } + + # Mock Invoke-RestMethod { + # # If this test fails uncomment the line below to see how the mock was called. + # # Write-Host $args + + # return $accessControlEntryResult + # } -Verifiable + + # Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationPath "Sprint 1" -User $userSingleResultObject -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') + + # It 'Should return ACEs' { + # Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + # $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and + # $Uri -like "*api-version=$(_getApiVersion Core)*" -and + # $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and + # $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and + # $Body -like "*`"allow`": 5,*" -and + # $Body -like "*`"deny`": 8,*" -and + # $ContentType -eq "application/json" -and + # $Method -eq "Post" + # } + # } + # } + + # Context 'Add-VSTeamWorkItemIterationPermission by IterationPath and Group' { + # Mock _getProjects { return "Test Project Public" } -Verifiable + # Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Path -eq "Sprint 1" } -Verifiable + # Mock Get-VSTeamClassificationNode { return $iterationRootNodeObject } + # Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable + + # Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationPath "Sprint 1" -Group $groupSingleResultObject -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') + + # It 'Should return ACEs' { + # Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + # $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and + # $Uri -like "*api-version=$(_getApiVersion Core)*" -and + # $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and + # $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + # $Body -like "*`"allow`": 5,*" -and + # $Body -like "*`"deny`": 8,*" -and + # $ContentType -eq "application/json" -and + # $Method -eq "Post" + # } + # } + # } + + # Context 'Add-VSTeamWorkItemIterationPermission by IterationPath and Descriptor' { + # Mock _getProjects { return "Test Project Public" } -Verifiable + # Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Path -eq "Sprint 1" } -Verifiable + # Mock Get-VSTeamClassificationNode { return $iterationRootNodeObject } + # Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable + + # Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationPath "Sprint 1" -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') + + # It 'Should return ACEs' { + # Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + # $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and + # $Uri -like "*api-version=$(_getApiVersion Core)*" -and + # $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and + # $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + # $Body -like "*`"allow`": 5,*" -and + # $Body -like "*`"deny`": 8,*" -and + # $ContentType -eq "application/json" -and + # $Method -eq "Post" + # } + # } + # } + + # Context 'Add-VSTeamWorkItemIterationPermission by IterationPath and Descritpor throws' { + # Mock Invoke-RestMethod { throw 'Error' } + + # It 'Should throw' { + # { Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationPath "Child 1 Level 1/Child 1 Level 2" -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') } | Should Throw + # } + # } +} \ No newline at end of file diff --git a/unit/test/Add-VSTeamWorkItemIterationPermission.Tests.ps1 b/unit/test/Add-VSTeamWorkItemIterationPermission.Tests.ps1 new file mode 100644 index 000000000..1bef0cbf5 --- /dev/null +++ b/unit/test/Add-VSTeamWorkItemIterationPermission.Tests.ps1 @@ -0,0 +1,233 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamWorkItemIterationPermissions.ps1" +. "$here/../../Source/Classes/VSTeamClassificationNode.ps1" +. "$here/../../Source/Classes/VSTeamGroup.ps1" +. "$here/../../Source/Classes/VSTeamUser.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamAccessControlEntry.ps1" +. "$here/../../Source/Classes/VSTeamSecurityNamespace.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/Get-VSTeamClassificationNode.ps1" +. "$here/../../Source/Public/Add-VSTeamAccessControlEntry.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +$userSingleResult = Get-Content "$PSScriptRoot\sampleFiles\users.single.json" -Raw | ConvertFrom-Json +$userSingleResultObject = [VSTeamUser]::new($userSingleResult) + +$groupSingleResult = Get-Content "$PSScriptRoot\sampleFiles\groupsSingle.json" -Raw | ConvertFrom-Json +$groupSingleResultObject = [VSTeamGroup]::new($groupSingleResult) + +$projectResult = [PSCustomObject]@{ + name = 'Test Project Public' + description = '' + url = '' + id = '010d06f0-00d5-472a-bb47-58947c230876' + state = '' + visibility = '' + revision = 0 + defaultTeam = [PSCustomObject]@{ } + _links = [PSCustomObject]@{ } +} + +$projectResultObject = [VSTeamProject]::new($projectResult) + +$accessControlEntryResult = +@" +{ + "count": 1, + "value": [ + { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", + "allow": 8, + "deny": 0, + "extendedInfo": {} + } + ] +} +"@ | ConvertFrom-Json + +$classificationNodeIterationId = +@" +{ + "count": 1, + "value": [ + { + "id": 20, + "identifier": "18e7998d-d0c5-4c01-b547-d7d4eb4c97c5", + "name": "Sprint 3", + "structureType": "iteration", + "hasChildren": false, + "path": "\\Demo Public\\Iteration\\Sprint 3", + "_links": { + "self": { + "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%203" + }, + "parent": { + "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations" + } + }, + "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%203" + } + ] + } +"@ | ConvertFrom-Json | Select-Object -ExpandProperty value + +$classificationNodeIterationIdObject = [VSTeamClassificationNode]::new($classificationNodeIterationId, "test") + +$iterationRootNode = +@" +{ + "id": 16, + "identifier": "dfa90792-403a-4119-a52b-bd142c08291b", + "name": "Demo Public", + "structureType": "iteration", + "hasChildren": true, + "path": "\\Demo Public\\Iteration", + "_links": { + "self": { + "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations" + } + }, + "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations" + } +"@ | ConvertFrom-Json + +$iterationRootNodeObject = [VSTeamClassificationNode]::new($iterationRootNode, "test") + +Describe 'VSTeamWorkItemIterationPermission' { + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + # You have to set the version or the api-version will not be added when versions = '' + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock _hasProjectCacheExpired { return $true } + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context 'Add-VSTeamWorkItemIterationPermission' { + # Mock _getProjects { return "Test Project Public" } + Mock Get-VSTeamClassificationNode { return $iterationRootNodeObject } + Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Ids -eq 44 -or $Path -eq "Sprint 1" } + + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + # Write-Host $args + return $accessControlEntryResult + } + + It 'by IterationID and User should return ACEs' { + Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationID 44 -User $userSingleResultObject -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and + $Body -like "*`"allow`": 5,*" -and + $Body -like "*`"deny`": 8,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by IterationID and Group should return ACEs' { + Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationID 44 -Group $groupSingleResultObject -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 5,*" -and + $Body -like "*`"deny`": 8,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by IterationID and Descriptor should return ACEs' { + Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationID 44 -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 5,*" -and + $Body -like "*`"deny`": 8,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by IterationPath and User should return ACEs' { + Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationPath "Sprint 1" -User $userSingleResultObject -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and + $Body -like "*`"allow`": 5,*" -and + $Body -like "*`"deny`": 8,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by IterationPath and Group should return ACEs' { + Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationPath "Sprint 1" -Group $groupSingleResultObject -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 5,*" -and + $Body -like "*`"deny`": 8,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + + It 'by IterationPath and Descriptor should return ACEs' { + Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationPath "Sprint 1" -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and + $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and + $Body -like "*`"allow`": 5,*" -and + $Body -like "*`"deny`": 8,*" -and + $ContentType -eq "application/json" -and + $Method -eq "Post" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Approvals.Tests.ps1 b/unit/test/Approvals.Tests.ps1 deleted file mode 100644 index b6e617e34..000000000 --- a/unit/test/Approvals.Tests.ps1 +++ /dev/null @@ -1,246 +0,0 @@ -Set-StrictMode -Version Latest - -# The InModuleScope command allows you to perform white-box unit testing on the -# internal (non-exported) code of a Script Module. -InModuleScope VSTeam { - Describe 'Approvals' -Tag 'unit', 'approvals' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - # Load the mocks to create the project name dynamic parameter - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - Context 'Get-VSTeamApproval' { - # Arrange - Mock _handleException -Verifiable - Mock Invoke-RestMethod { throw 'testing error handling' } - - It 'should pass exception to _handleException' { - # Act - Get-VSTeamApproval -ProjectName project - - # Assert - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamApproval' { - # Arrange - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return @{ - count = 1 - value = @( - @{ - id = 1 - revision = 1 - approver = @{ - id = 'c1f4b9a6-aee1-41f9-a2e0-070a79973ae9' - displayName = 'Test User' - } - } - ) - } - } - - It 'should return approvals' { - # Act - Get-VSTeamApproval -projectName project - - # Assert - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 ` - -ParameterFilter { - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/approvals?api-version=$([VSTeamVersions]::Release)" - } - } - } - - Context 'Get-VSTeamApproval with AssignedToFilter' { - # Arrange - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return @{ - count = 1 - value = @( - @{ - id = 1 - revision = 1 - approver = @{ - id = 'c1f4b9a6-aee1-41f9-a2e0-070a79973ae9' - displayName = 'Test User' - } - } - ) - } - } - - It 'should return approvals only for value passed into AssignedToFilter' { - # Act - Get-VSTeamApproval -projectName project -AssignedToFilter 'Chuck Reinhart' - - # Assert - # With PowerShell core the order of the query string is not the - # same from run to run! So instead of testing the entire string - # matches I have to search for the portions I expect but can't - # assume the order. - # The general string should look like this: - # "https://vsrm.dev.azure.com/test/project/_apis/release/approvals/?api-version=$([VSTeamVersions]::Release)&assignedtoFilter=Chuck%20Reinhart&includeMyGroupApprovals=true" - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 ` - -ParameterFilter { - $Uri -like "*https://vsrm.dev.azure.com/test/project/_apis/release/approvals*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Release)*" -and - $Uri -like "*assignedtoFilter=Chuck Reinhart*" -and - $Uri -like "*includeMyGroupApprovals=true*" - } - } - } - - # This makes sure the alias is working - Context 'Get-VSTeamApproval' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { return @{ - count = 1 - value = @( - @{ - id = 1 - revision = 1 - approver = @{ - id = 'c1f4b9a6-aee1-41f9-a2e0-070a79973ae9' - displayName = 'Test User' - } - } - ) - }} - - Get-VSTeamApproval -projectName project - - It 'should return approvals' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 ` - -ParameterFilter { - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/approvals?api-version=$([VSTeamVersions]::Release)" - } - } - } - - Context 'Set-VSTeamApproval' { - Mock Invoke-RestMethod { return @{ - id = 1 - revision = 1 - approver = @{ - id = 'c1f4b9a6-aee1-41f9-a2e0-070a79973ae9' - displayName = 'Test User' - } - }} - - Set-VSTeamApproval -projectName project -Id 1 -Status Rejected -Force - - It 'should set approval' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 ` - -ParameterFilter { - $Method -eq 'Patch' -and - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/approvals/1?api-version=$([VSTeamVersions]::Release)" - } - } - } - - Context 'Set-VSTeamApproval handles exception' { - Mock _handleException -Verifiable - Mock Invoke-RestMethod { throw 'testing error handling' } - - Set-VSTeamApproval -projectName project -Id 1 -Status Rejected -Force - - It 'should set approval' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 ` - -ParameterFilter { - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/approvals/1?api-version=$([VSTeamVersions]::Release)" - } - } - } - - Context 'Set-VSTeamApproval' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { return @{ - id = 1 - revision = 1 - approver = @{ - id = 'c1f4b9a6-aee1-41f9-a2e0-070a79973ae9' - displayName = 'Test User' - } - }} - - Set-VSTeamApproval -projectName project -Id 1 -Status Rejected -Force - - It 'should set approval' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 ` - -ParameterFilter { - $Method -eq 'Patch' -and - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/approvals/1?api-version=$([VSTeamVersions]::Release)" - } - } - } - - Context 'Show-VSTeamApproval' { - Mock Show-Browser -Verifiable - - Show-VSTeamApproval -projectName project -ReleaseDefinitionId 1 - - It 'should open in browser' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamApproval TFS' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } - - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return @{ - count = 1 - value = @( - @{ - id = 1 - revision = 1 - approver = @{ - id = 'c1f4b9a6-aee1-41f9-a2e0-070a79973ae9' - displayName = 'Test User' - } - } - ) - }} - - Get-VSTeamApproval -projectName project -ReleaseIdsFilter 1 -AssignedToFilter 'Test User' -StatusFilter Pending - - It 'should return approvals' { - # With PowerShell core the order of the query string is not the - # same from run to run! So instead of testing the entire string - # matches I have to search for the portions I expect but can't - # assume the order. - # The general string should look like this: - # "http://localhost:8080/tfs/defaultcollection/project/_apis/release/approvals/?api-version=$([VSTeamVersions]::Release)&statusFilter=Pending&assignedtoFilter=Test User&includeMyGroupApprovals=true&releaseIdsFilter=1" - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 ` - -ParameterFilter { - $Uri -like "*http://localhost:8080/tfs/defaultcollection/project/_apis/release/approvals*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Release)*" -and - $Uri -like "*statusFilter=Pending*" -and - $Uri -like "*assignedtoFilter=Test User*" -and - $Uri -like "*includeMyGroupApprovals=true*" -and - $Uri -like "*releaseIdsFilter=1*" - } - } - } - } -} \ No newline at end of file diff --git a/unit/test/Clear-VSTeamDefaultProject.Tests.ps1 b/unit/test/Clear-VSTeamDefaultProject.Tests.ps1 new file mode 100644 index 000000000..9a0d7013e --- /dev/null +++ b/unit/test/Clear-VSTeamDefaultProject.Tests.ps1 @@ -0,0 +1,100 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamProcessCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinition.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamQueue.ps1" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" +. "$here/../../Source/Public/Get-VSTeamBuildDefinition.ps1" +. "$here/../../Source/Public/Get-VSTeamProcess.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamProject' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + . "$PSScriptRoot\mocks\mockProcessNameDynamicParam.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + + Context 'Clear-VSTeamDefaultProject on Non Windows' { + AfterAll { + $Global:PSDefaultParameterValues.Remove("*:projectName") + } + + Mock _isOnWindows { return $false } + + It 'should clear default project' { + $Global:PSDefaultParameterValues['*:projectName'] = 'MyProject' + + Clear-VSTeamDefaultProject + + $Global:PSDefaultParameterValues['*:projectName'] | Should BeNullOrEmpty + } + } + + Context 'Clear-VSTeamDefaultProject as Non-Admin on Windows' { + AfterAll { + $Global:PSDefaultParameterValues.Remove("*:projectName") + } + + Mock _isOnWindows { return $true } + Mock _testAdministrator { return $false } + + It 'should clear default project' { + $Global:PSDefaultParameterValues['*:projectName'] = 'MyProject' + + Clear-VSTeamDefaultProject + + $Global:PSDefaultParameterValues['*:projectName'] | Should BeNullOrEmpty + } + } + + Context 'Clear-VSTeamDefaultProject as Admin on Windows' { + AfterAll { + $Global:PSDefaultParameterValues.Remove("*:projectName") + } + + Mock _isOnWindows { return $true } + Mock _testAdministrator { return $true } + + It 'should clear default project' { + $Global:PSDefaultParameterValues['*:projectName'] = 'MyProject' + + Clear-VSTeamDefaultProject + + $Global:PSDefaultParameterValues['*:projectName'] | Should BeNullOrEmpty + } + } +} \ No newline at end of file diff --git a/unit/test/Disable-VSTeamAgent.Tests.ps1 b/unit/test/Disable-VSTeamAgent.Tests.ps1 new file mode 100644 index 000000000..6b3f4b29c --- /dev/null +++ b/unit/test/Disable-VSTeamAgent.Tests.ps1 @@ -0,0 +1,41 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamAgent' { + Context 'Disable-VSTeamAgent' { + ## Arrange + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'DistributedTask' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod -ParameterFilter { $Uri -like "*950*" } + Mock Invoke-RestMethod { throw 'boom' } -ParameterFilter { $Uri -like "*101*" } + + It 'should throw' { + ## Act / Assert + { Disable-VSTeamAgent -Pool 36 -Id 101 } | Should Throw + } + + It 'by Id should disable the agent with passed in Id' { + ## Act + Disable-VSTeamAgent -Pool 36 -Id 950 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + # The write-host below is great for seeing how many ways the mock is called. + # Write-Host "Assert Mock $Uri" + $Method -eq 'Patch' -and + $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/36/agents/950?api-version=$(_getApiVersion DistributedTask)" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Enable-VSTeamAgent.Tests.ps1 b/unit/test/Enable-VSTeamAgent.Tests.ps1 new file mode 100644 index 000000000..b356c8bb2 --- /dev/null +++ b/unit/test/Enable-VSTeamAgent.Tests.ps1 @@ -0,0 +1,40 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamAgent' { + Context 'Enable-VSTeamAgent' { + ## Arrnage + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'DistributedTask' } + + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod -ParameterFilter { $Uri -like "*950*" } + Mock Invoke-RestMethod { throw 'boom' } -ParameterFilter { $Uri -like "*101*" } + + It 'by Id should enable the agent with passed in Id' { + ## Act + Enable-VSTeamAgent -Pool 36 -Id 950 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Patch' -and + $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/36/agents/950?api-version=$(_getApiVersion DistributedTask)" + } + } + + It 'should throw' { + ## Act / ## Assert + { Enable-VSTeamAgent -Pool 36 -Id 101 } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeam.Tests.ps1 b/unit/test/Get-VSTeam.Tests.ps1 new file mode 100644 index 000000000..18cf9e567 --- /dev/null +++ b/unit/test/Get-VSTeam.Tests.ps1 @@ -0,0 +1,226 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamTeam.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + + +$results = [PSCustomObject]@{ + value = [PSCustomObject]@{ + id = '6f365a7143e492e911c341451a734401bcacadfd' + name = 'refs/heads/master' + description = 'team description' + } +} + +$singleResult = [PSCustomObject]@{ + id = '6f365a7143e492e911c341451a734401bcacadfd' + name = 'refs/heads/master' + description = 'team description' +} + +Describe "VSTeam" { + Context "Get-VSTeam" { + Context "services" { + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Context 'Get-VSTeam with project name' { + Mock Invoke-RestMethod { return $results } + + It 'Should return teams' { + Get-VSTeam -ProjectName Test + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams?api-version=$(_getApiVersion Core)" + } + } + } + + Context 'Get-VSTeam with project name, with top' { + Mock Invoke-RestMethod { return $results } + + It 'Should return teams' { + Get-VSTeam -ProjectName Test -Top 10 + + # With PowerShell core the order of the query string is not the + # same from run to run! So instead of testing the entire string + # matches I have to search for the portions I expect but can't + # assume the order. + # The general string should look like this: + # "https://dev.azure.com/test/_apis/projects/Test/teams/?api-version=$(_getApiVersion Core)&`$top=10" + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/projects/Test/teams*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*`$top=10*" + } + } + } + + Context 'Get-VSTeam with project name, with skip' { + Mock Invoke-RestMethod { return $results } + + It 'Should return teams' { + Get-VSTeam -ProjectName Test -Skip 10 + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/projects/Test/teams*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*`$skip=10*" + } + } + } + + Context 'Get-VSTeam with project name, with top and skip' { + Mock Invoke-RestMethod { return $results } + + It 'Should return teams' { + Get-VSTeam -ProjectName Test -Top 10 -Skip 5 + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/projects/Test/teams*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*`$top=10*" -and + $Uri -like "*`$skip=5*" + } + } + } + + Context 'Get-VSTeam with specific project and specific team id' { + Mock Invoke-RestMethod { return $singleResult } + + It 'Should return teams' { + Get-VSTeam -ProjectName Test -TeamId TestTeamId + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams/TestTeamId?api-version=$(_getApiVersion Core)" + } + } + } + + Context 'Get-VSTeam with specific project and specific team Name' { + Mock Invoke-RestMethod { return $singleResult } + + It 'Should return teams' { + Get-VSTeam -ProjectName Test -TeamName TestTeamName + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams/TestTeamName?api-version=$(_getApiVersion Core)" + } + } + } + } + + + Context "Server" { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Mock _useWindowsAuthenticationOnPremise { return $true } + + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + Context 'Get-VSTeam with project name on TFS local Auth' { + Mock Invoke-RestMethod { return $results } + + It 'Should return teams' { + Get-VSTeam -ProjectName Test + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams?api-version=$(_getApiVersion Core)" + } + } + } + + Context 'Get-VSTeam with project name, with top on TFS local Auth' { + Mock Invoke-RestMethod { return $results } + + It 'Should return teams' { + Get-VSTeam -ProjectName Test -Top 10 + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -like "*http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams?*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*`$top=10*" + } + } + } + + Context 'Get-VSTeam with project name, with skip on TFS local Auth' { + Mock Invoke-RestMethod { return $results } + + It 'Should return teams' { + Get-VSTeam -ProjectName Test -Skip 10 + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -like "*http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*`$skip=10*" + } + } + } + + Context 'Get-VSTeam with project name, with top and skip on TFS local Auth' { + Mock Invoke-RestMethod { return $results } + + It 'Should return teams' { + Get-VSTeam -ProjectName Test -Top 10 -Skip 5 + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -like "*http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*`$top=10*" -and + $Uri -like "*`$skip=5*" + } + } + } + + Context 'Get-VSTeam with specific project and specific team Name on TFS local Auth' { + Mock Invoke-RestMethod { return $singleResult } + + It 'Should return teams' { + Get-VSTeam -ProjectName Test -Name TestTeamName + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams/TestTeamName?api-version=$(_getApiVersion Core)" + } + } + } + + Context 'Get-VSTeam with specific project and specific team ID on TFS local Auth' { + Mock Invoke-RestMethod { return $singleResult } + + It 'Should return teams' { + Get-VSTeam -ProjectName Test -TeamId TestTeamId + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams/TestTeamId?api-version=$(_getApiVersion Core)" + } + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamAccessControlList.Tests.ps1 b/unit/test/Get-VSTeamAccessControlList.Tests.ps1 index 608ac79c5..8ef9e6e3c 100644 --- a/unit/test/Get-VSTeamAccessControlList.Tests.ps1 +++ b/unit/test/Get-VSTeamAccessControlList.Tests.ps1 @@ -1,5 +1,8 @@ Set-StrictMode -Version Latest +#region include +Import-Module SHiPS + $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") @@ -10,178 +13,41 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Classes/VSTeamAccessControlEntry.ps1" . "$here/../../Source/Classes/VSTeamAccessControlList.ps1" . "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamDefaultProject.ps1" +. "$here/../../Source/Public/Get-VSTeamSecurityNamespace.ps1" . "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamAccessControlList' { + ## Arrange + # You have to set the version or the api-version will not be added when versions = '' + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + $accessControlListResult = Get-Content "$PSScriptRoot\sampleFiles\accessControlListResult.json" -Raw | ConvertFrom-Json + + $securityNamespace = Get-Content "$PSScriptRoot\sampleFiles\securityNamespace.json" -Raw | ConvertFrom-Json + $securityNamespaceObject = [VSTeamSecurityNamespace]::new($securityNamespace.value[0]) -$accessControlListResult = -@" -{ - "count": 5, - "value": [ - { - "inheritPermissions": true, - "token": "1ba198c0-7a12-46ed-a96b-f4e77554c6d4", - "acesDictionary": { - "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1": { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", - "allow": 31, - "deny": 0 - }, - "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-2": { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-2", - "allow": 31, - "deny": 0 - }, - "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-3": { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-3", - "allow": 1, - "deny": 0 - } - } - }, - { - "inheritPermissions": true, - "token": "1ba198c0-7a12-46ed-a96b-f4e77554c6d4\\846cd9c3-56ba-4158-b6d2-23a3a73244e5", - "acesDictionary": { - "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-1-2": { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-1-2", - "allow": 8, - "deny": 0 - } - } - }, - { - "inheritPermissions": true, - "token": "28b9bb88-a513-4115-9b5c-8be39ce1f1ba", - "acesDictionary": { - "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-2294004008-329585985-2606533603-2632053178-0-0-0-0-1": { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-2294004008-329585985-2606533603-2632053178-0-0-0-0-1", - "allow": 31, - "deny": 0 - }, - "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-2294004008-329585985-2606533603-2632053178-0-0-0-0-2": { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-2294004008-329585985-2606533603-2632053178-0-0-0-0-2", - "allow": 31, - "deny": 0 - }, - "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-2294004008-329585985-2606533603-2632053178-0-0-0-0-3": { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-2294004008-329585985-2606533603-2632053178-0-0-0-0-3", - "allow": 1, - "deny": 0 - } - } - }, - { - "inheritPermissions": false, - "token": "token1", - "acesDictionary": { - "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1": { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", - "allow": 31, - "deny": 0 - } - } - }, - { - "inheritPermissions": false, - "token": "token2", - "acesDictionary": { - "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1": { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", - "allow": 1, - "deny": 0 - }, - "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-2": { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-2", - "allow": 8, - "deny": 0 - } - } - } - ] - } -"@ | ConvertFrom-Json - -$securityNamespace = -@" -{ - "count": 1, - "value": [ - { - "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531", - "name": "Analytics", - "displayName": "Analytics", - "separatorValue": "/", - "elementLength": -1, - "writePermission": 2, - "readPermission": 1, - "dataspaceCategory": "Default", - "actions": [ - { - "bit": 1, - "name": "Read", - "displayName": "View analytics", - "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" - }, - { - "bit": 2, - "name": "Administer", - "displayName": "Manage analytics permissions", - "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" - }, - { - "bit": 4, - "name": "Stage", - "displayName": "Push the data to staging area", - "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" - }, - { - "bit": 8, - "name": "ExecuteUnrestrictedQuery", - "displayName": "Execute query without any restrictions on the query form", - "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" - }, - { - "bit": 16, - "name": "ReadEuii", - "displayName": "Read EUII data", - "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" - } - ], - "structureValue": 1, - "extensionType": null, - "isRemotable": false, - "useTokenTranslator": false, - "systemBitMask": 30 - } - ] - } -"@ | ConvertFrom-Json - -$securityNamespaceObject = [VSTeamSecurityNamespace]::new($securityNamespace.value) - -Describe 'Get-VSTeamAccessControlList' { # Set the account to use for testing. A normal user would do this # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Core = '' - [VSTeamVersions]::Core = '5.0' + Mock _getInstance { return 'https://dev.azure.com/test' } - Context 'Get-VSTeamAccessControlList by SecurityNamespaceId' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args + Context 'Get-VSTeamAccessControlList' { + Mock Invoke-RestMethod { return $accessControlListResult } + Mock Invoke-RestMethod { throw 'Error' } -ParameterFilter { $Uri -like "*token=boom*" } - return $accessControlListResult - } -Verifiable + It 'by SecurityNamespaceId should return ACLs' { + ## Arrange + # Even with a default set this URI should not have the project added. + Set-VSTeamDefaultProject -Project Testing - Get-VSTeamAccessControlList -SecurityNamespaceId 5a27515b-ccd7-42c9-84f1-54c998f03866 -Token "SomeToken" -Descriptors "SomeDescriptor" -IncludeExtendedInfo -Recurse + ## Act + Get-VSTeamAccessControlList -SecurityNamespaceId 5a27515b-ccd7-42c9-84f1-54c998f03866 -Token "SomeToken" -Descriptors "SomeDescriptor" -IncludeExtendedInfo -Recurse - It 'Should return ACLs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Uri -like "https://dev.azure.com/test/_apis/accesscontrollists/5a27515b-ccd7-42c9-84f1-54c998f03866*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and $Uri -like "*descriptors=SomeDescriptor*" -and $Uri -like "*includeExtendedInfo=True*" -and $Uri -like "*token=SomeToken*" -and @@ -189,61 +55,45 @@ Describe 'Get-VSTeamAccessControlList' { $Method -eq "Get" } } - } - Context 'Get-VSTeamAccessControlList by SecurityNamespace' { - Mock Get-VSTeamSecurityNamespace { return $securityNamespaceObject } - Mock Invoke-RestMethod { return $accessControlListResult } -Verifiable + It 'by SecurityNamespace should return ACLs' { + ## Act + # I use $securityNamespace.value[0] here because using securityNamespaceObject was leading to issues + Get-VSTeamAccessControlList -SecurityNamespace $($securityNamespace.value[0]) -Token "SomeToken" -Descriptors "SomeDescriptor" - $securityNamespace = Get-VSTeamSecurityNamespace -Id "58450c49-b02d-465a-ab12-59ae512d6531" - Get-VSTeamAccessControlList -SecurityNamespace $securityNamespace -Token "SomeToken" -Descriptors "SomeDescriptor" - - It 'Should return ACLs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Uri -like "https://dev.azure.com/test/_apis/accesscontrollists/58450c49-b02d-465a-ab12-59ae512d6531*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and $Uri -like "*descriptors=SomeDescriptor*" -and $Uri -like "*token=SomeToken*" -and $Method -eq "Get" } } - } - - Context 'Get-VSTeamAccessControlList by SecurityNamespace (pipeline)' { - Mock Get-VSTeamSecurityNamespace { return $securityNamespaceObject } - Mock Invoke-RestMethod { return $accessControlListResult } -Verifiable - Get-VSTeamSecurityNamespace -Id "58450c49-b02d-465a-ab12-59ae512d6531" | ` - Get-VSTeamAccessControlList -Token "SomeToken" -Descriptors "SomeDescriptor" + It 'by SecurityNamespace (pipeline) should return ACEs' { + ## Act + $securityNamespaceObject | Get-VSTeamAccessControlList -Token "AcesToken" -Descriptors "AcesDescriptor" - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Uri -like "https://dev.azure.com/test/_apis/accesscontrollists/58450c49-b02d-465a-ab12-59ae512d6531*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*descriptors=SomeDescriptor*" -and - $Uri -like "*token=SomeToken*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*descriptors=AcesDescriptor*" -and + $Uri -like "*token=AcesToken*" -and $Method -eq "Get" } } - } - - Context 'Get-VSTeamAccessControlList by securityNamespaceId throws' { - Mock Invoke-RestMethod { throw 'Error' } - It 'Should throw' { - { Get-VSTeamAccessControlList -SecurityNamespaceId 5a27515b-ccd7-42c9-84f1-54c998f03866 -Token "SomeToken" -Descriptors "SomeDescriptor" -IncludeExtendedInfo -Recurse } | Should Throw + It 'by SecurityNamespaceId should throw' { + ## Act / Assert + { Get-VSTeamAccessControlList -SecurityNamespaceId 5a27515b-ccd7-42c9-84f1-54c998f03866 -Token "boom" -Descriptors "SomeDescriptor" -IncludeExtendedInfo -Recurse } | Should Throw } - } - - Context 'Get-VSTeamAccessControlList by SecurityNamespace throws' { - Mock Get-VSTeamSecurityNamespace { return $securityNamespaceObject } - Mock Invoke-RestMethod { throw 'Error' } - - $securityNamespace = Get-VSTeamSecurityNamespace -Id "5a27515b-ccd7-42c9-84f1-54c998f03866" - It 'Should throw' { - { Get-VSTeamAccessControlList -SecurityNamespace $securityNamespace -Token "SomeToken" -Descriptors "SomeDescriptor" -IncludeExtendedInfo -Recurse } | Should Throw + It 'by SecurityNamespace should throw' { + ## Act / Assert + { Get-VSTeamAccessControlList -SecurityNamespace $securityNamespaceObject -Token "boom" -Descriptors "SomeDescriptor" -IncludeExtendedInfo -Recurse } | Should Throw } } } \ No newline at end of file diff --git a/unit/test/Get-VSTeamAgent.Tests.ps1 b/unit/test/Get-VSTeamAgent.Tests.ps1 new file mode 100644 index 000000000..818232090 --- /dev/null +++ b/unit/test/Get-VSTeamAgent.Tests.ps1 @@ -0,0 +1,70 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamAgent.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamDefaultProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamAgent' { + ## Arrange + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'DistributedTask' } + + $testAgent = Get-Content "$PSScriptRoot\sampleFiles\agentSingleResult.json" -Raw | ConvertFrom-Json + + Mock _getInstance { return 'https://dev.azure.com/test' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + # Even with a default set this URI should not have the project added. + Set-VSTeamDefaultProject -Project Testing + + Context 'Get-VSTeamAgent' { + Mock Invoke-RestMethod { return [PSCustomObject]@{ + count = 1 + value = $testAgent + } + } + Mock Invoke-RestMethod { return $testAgent } -ParameterFilter { $Uri -like "*101*"} + + it 'by pool id should return all the agents' { + ## Act + Get-VSTeamAgent -PoolId 1 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/1/agents?api-version=$(_getApiVersion DistributedTask)" + } + } + + it 'with agent id parameter should return on agent' { + ## Act + Get-VSTeamAgent -PoolId 1 -id 101 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/1/agents/101?api-version=$(_getApiVersion DistributedTask)" + } + } + + it 'PoolID from pipeline by value should return all the agents' { + ## Act + 1 | Get-VSTeamAgent + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/1/agents?api-version=$(_getApiVersion DistributedTask)" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamApproval.Tests.ps1 b/unit/test/Get-VSTeamApproval.Tests.ps1 new file mode 100644 index 000000000..d2c0fa924 --- /dev/null +++ b/unit/test/Get-VSTeamApproval.Tests.ps1 @@ -0,0 +1,117 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamApproval' -Tag 'unit', 'approvals' { + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Release' } + + Mock Invoke-RestMethod { throw 'error' } -ParameterFilter { $Uri -like "*boom*" } + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + # Write-Host $args + + return @{ + count = 1 + value = @( + @{ + id = 1 + revision = 1 + approver = @{ + id = 'c1f4b9a6-aee1-41f9-a2e0-070a79973ae9' + displayName = 'Test User' + } + } + ) + } + } + + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Context 'Get-VSTeamApproval' { + Context 'Services' { + # Arrange + Mock _handleException -Verifiable + + It 'should return approvals' { + # Act + Get-VSTeamApproval -projectName project + + # Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It ` + -ParameterFilter { + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/approvals?api-version=$(_getApiVersion Release)" + } + } + + It 'should pass exception to _handleException' { + # Act + Get-VSTeamApproval -ProjectName "boom" + + # Assert + Assert-VerifiableMock + } + + It 'with AssignedToFilter should return approvals only for value passed into AssignedToFilter' { + # Act + Get-VSTeamApproval -projectName project -AssignedToFilter 'Chuck Reinhart' + + # Assert + # With PowerShell core the order of the query string is not the + # same from run to run! So instead of testing the entire string + # matches I have to search for the portions I expect but can't + # assume the order. + # The general string should look like this: + # "https://vsrm.dev.azure.com/test/project/_apis/release/approvals/?api-version=$(_getApiVersion Release)&assignedtoFilter=Chuck%20Reinhart&includeMyGroupApprovals=true" + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It ` + -ParameterFilter { + $Uri -like "*https://vsrm.dev.azure.com/test/project/_apis/release/approvals*" -and + $Uri -like "*api-version=$(_getApiVersion Release)*" -and + $Uri -like "*assignedtoFilter=Chuck Reinhart*" -and + $Uri -like "*includeMyGroupApprovals=true*" + } + } + } + + Context 'Server' { + ## Arrange + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + It 'should return approvals' { + ## Act + Get-VSTeamApproval -projectName project -ReleaseIdsFilter 1 -AssignedToFilter 'Test User' -StatusFilter Pending + + ## Assert + # With PowerShell core the order of the query string is not the + # same from run to run! So instead of testing the entire string + # matches I have to search for the portions I expect but can't + # assume the order. + # The general string should look like this: + # "http://localhost:8080/tfs/defaultcollection/project/_apis/release/approvals/?api-version=$(_getApiVersion Release)&statusFilter=Pending&assignedtoFilter=Test User&includeMyGroupApprovals=true&releaseIdsFilter=1" + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It ` + -ParameterFilter { + $Uri -like "*http://localhost:8080/tfs/defaultcollection/project/_apis/release/approvals*" -and + $Uri -like "*api-version=$(_getApiVersion Release)*" -and + $Uri -like "*statusFilter=Pending*" -and + $Uri -like "*assignedtoFilter=Test User*" -and + $Uri -like "*includeMyGroupApprovals=true*" -and + $Uri -like "*releaseIdsFilter=1*" + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamBuild.Tests.ps1 b/unit/test/Get-VSTeamBuild.Tests.ps1 new file mode 100644 index 000000000..ec2f1d80e --- /dev/null +++ b/unit/test/Get-VSTeamBuild.Tests.ps1 @@ -0,0 +1,93 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" +#endregion + +Describe 'VSTeamBuild' { + ## Arrange + # Sample result of a single build + $singleResult = Get-Content "$PSScriptRoot\sampleFiles\buildSingleResult.json" -Raw | ConvertFrom-Json + + # Sample result for list of builds + $results = Get-Content "$PSScriptRoot\sampleFiles\buildResults.json" -Raw | ConvertFrom-Json + + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock Invoke-RestMethod { return $results } + Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -like "*101*" } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Build' } + + Context 'Get-VSTeamBuild' { + Context 'Services' { + ## Arrange + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + It 'with no parameters should return builds' { + ## Act + Get-VSTeamBuild -projectName project + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds?api-version=$(_getApiVersion Build)" + } + } + + It 'with Top parameter should return top builds' { + ## Act + Get-VSTeamBuild -projectName project -top 1 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds?api-version=$(_getApiVersion Build)&`$top=1" + } + } + + It 'by id should return top builds' { + ## Act + Get-VSTeamBuild -projectName project -id 101 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/101?api-version=$(_getApiVersion Build)" + } + } + } + + Context 'Server' { + ## Arrange + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable + + It 'with no parameters on TFS local Auth should return builds' { + ## Act + Get-VSTeamBuild -projectName project + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds?api-version=$(_getApiVersion Build)" + } + } + + It 'should return builds' { + ## Act + Get-VSTeamBuild -projectName project -id 101 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/101?api-version=$(_getApiVersion Build)" } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamBuildArtifact.Tests.ps1 b/unit/test/Get-VSTeamBuildArtifact.Tests.ps1 new file mode 100644 index 000000000..09b7198b3 --- /dev/null +++ b/unit/test/Get-VSTeamBuildArtifact.Tests.ps1 @@ -0,0 +1,92 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamBuildArtifact' { + Context "Get-VSTeamBuildArtifact" { + ## Arrange + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Build' } + + Mock Invoke-RestMethod { return [PSCustomObject]@{ + value = [PSCustomObject]@{ + id = 150 + name = "Drop" + resource = [PSCustomObject]@{ + type = "filepath" + data = "C:\Test" + properties = [PSCustomObject]@{ } + } + } + } + } -ParameterFilter { + $Uri -like "*1*" + } + + Mock Invoke-RestMethod { return [PSCustomObject]@{ + value = [PSCustomObject]@{ + id = 150 + name = "Drop" + resource = [PSCustomObject]@{ + type = "filepath" + data = "C:\Test" + } + } + } + } -ParameterFilter { + $Uri -like "*2*" + } + + Context "Services" { + It 'calls correct Url should return the build artifact data' { + ## Act + Get-VSTeamBuildArtifact -projectName project -id 1 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/1/artifacts?api-version=$(_getApiVersion Build)" + } + } + + It 'result without properties should return the build artifact data' { + ## Act + Get-VSTeamBuildArtifact -projectName project -id 2 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/2/artifacts?api-version=$(_getApiVersion Build)" + } + } + } + + Context 'Server' { + ## Arrange + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + It 'calls correct Url on TFS local Auth should return the build artifact data' { + ## Act + Get-VSTeamBuildArtifact -projectName project -id 1 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/1/artifacts?api-version=$(_getApiVersion Build)" + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamBuildDefinition.Tests.ps1 b/unit/test/Get-VSTeamBuildDefinition.Tests.ps1 index 8d27a693d..577f3ae28 100644 --- a/unit/test/Get-VSTeamBuildDefinition.Tests.ps1 +++ b/unit/test/Get-VSTeamBuildDefinition.Tests.ps1 @@ -1,256 +1,207 @@ Set-StrictMode -Version Latest -# Loading System.Web avoids issues finding System.Web.HttpUtility -Add-Type -AssemblyName 'System.Web' - -InModuleScope VSTeam { - $resultsVSTS = Get-Content "$PSScriptRoot\sampleFiles\buildDefvsts.json" -Raw | ConvertFrom-Json - $resultsAzD = Get-Content "$PSScriptRoot\sampleFiles\buildDefAzD.json" -Raw | ConvertFrom-Json - $results2017 = Get-Content "$PSScriptRoot\sampleFiles\buildDef2017.json" -Raw | ConvertFrom-Json - $results2018 = Get-Content "$PSScriptRoot\sampleFiles\buildDef2018.json" -Raw | ConvertFrom-Json - - Describe 'BuildDefinitions' { - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinition.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamBuildDefinition' { + Context 'Get-VSTeamBuildDefinition' { + $resultsAzD = Get-Content "$PSScriptRoot\sampleFiles\buildDefAzD.json" -Raw | ConvertFrom-Json + $resultsVSTS = Get-Content "$PSScriptRoot\sampleFiles\buildDefvsts.json" -Raw | ConvertFrom-Json + $results2017 = Get-Content "$PSScriptRoot\sampleFiles\buildDef2017.json" -Raw | ConvertFrom-Json + $results2018 = Get-Content "$PSScriptRoot\sampleFiles\buildDef2018.json" -Raw | ConvertFrom-Json . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - Context 'Get-VSTeamBuildDefinition with no parameters 2017' { - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Build' } - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - # Write-Host $([VSTeamVersions]::Build) - # Write-Host $([VSTeamVersions]::Account) + Mock Invoke-RestMethod { return $results2017 } -ParameterFilter { $Uri -like "*2017Project*" } + Mock Invoke-RestMethod { return $results2018 } -ParameterFilter { $Uri -like "*2018Project*" } + Mock Invoke-RestMethod { return $resultsAzD } -ParameterFilter { $Uri -like "*azd*" } + Mock Invoke-RestMethod { return $resultsVSTS } -ParameterFilter { $Uri -like "*vsts*" } + Mock Invoke-RestMethod { return $resultsVSTS.value } -ParameterFilter { $Uri -like "*101*" } - return $results2017 - } + Context 'Server' { + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } - It 'should return build definitions' { - Get-VSTeamBuildDefinition -projectName project + It 'TFS 2017 with no parameters should return build definitions' { + ## Act + Get-VSTeamBuildDefinition -projectName "2017Project" - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*http://localhost:8080/tfs/defaultcollection/project/_apis/build/definitions*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*http://localhost:8080/tfs/defaultcollection/2017Project/_apis/build/definitions*" -and + $Uri -like "*api-version=$(_getApiVersion Build)*" -and $Uri -like "*type=All*" } } - } - - Context 'Get-VSTeamBuildDefinition with no parameters 2018' { - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { - return $results2018 - } - It 'should return build definitions' { - Get-VSTeamBuildDefinition -projectName project + It 'TFS 2018 with no parameters should return build definitions' { + ## Act + Get-VSTeamBuildDefinition -projectName "2018Project" - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*http://localhost:8080/tfs/defaultcollection/project/_apis/build/definitions*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*http://localhost:8080/tfs/defaultcollection/2018Project/_apis/build/definitions*" -and + $Uri -like "*api-version=$(_getApiVersion Build)*" -and $Uri -like "*type=All*" } } - } - - Context 'Get-VSTeamBuildDefinition with no parameters AzD v5.0 of API' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - Mock Invoke-RestMethod { - return $resultsAzD - } - It 'should return build definitions' { - Get-VSTeamBuildDefinition -projectName project + It 'by ID local auth should return build definition' { + ## Act + Get-VSTeamBuildDefinition -projectName project -id 101 + ## Assert Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/project/_apis/build/definitions*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and - $Uri -like "*type=All*" + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/definitions/101?api-version=$(_getApiVersion Build)" } } } - Context 'Get-VSTeamBuildDefinition with no parameters VSTS' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - Mock Invoke-RestMethod { - return $resultsVSTS - } + Context 'Services' { + ## Arrange + Mock _getInstance { return 'https://dev.azure.com/test' } - It 'should return build definitions' { - Get-VSTeamBuildDefinition -projectName project + It 'AzD v5.0 of API with no parameters should return build definitions' { + ## Act + Get-VSTeamBuildDefinition -projectName azd - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/project/_apis/build/definitions*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/azd/_apis/build/definitions*" -and + $Uri -like "*api-version=$(_getApiVersion Build)*" -and $Uri -like "*type=All*" } } - } - - Context 'Get-VSTeamBuildDefinition with no parameters VSTS yaml ' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - Mock Invoke-RestMethod { - return $resultsVSTS - } It 'should return build definitions' { - Get-VSTeamBuildDefinition -projectName project + ## Act + Get-VSTeamBuildDefinition -projectName vsts - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/project/_apis/build/definitions*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/vsts/_apis/build/definitions*" -and + $Uri -like "*api-version=$(_getApiVersion Build)*" -and $Uri -like "*type=All*" } } - } - - Context 'Get-VSTeamBuildDefinition with type parameter' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - Mock Invoke-RestMethod { - return $resultsVSTS - } - It 'should return build definitions by type' { - Get-VSTeamBuildDefinition -projectName project -type build + It 'with type parameter should return build definitions by type' { + ## Act + Get-VSTeamBuildDefinition -projectName vsts -type build + ## Assert Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/project/_apis/build/definitions*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and + $Uri -like "*https://dev.azure.com/test/vsts/_apis/build/definitions*" -and + $Uri -like "*api-version=$(_getApiVersion Build)*" -and $Uri -like "*type=build*" } } - } - - Context 'Get-VSTeamBuildDefinition with filter parameter' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - Mock Invoke-RestMethod { return $resultsVSTS } - - It 'should return build definitions by filter' { - Get-VSTeamBuildDefinition -projectName project -filter 'click*' + It 'with filter parameter should return build definitions by filter' { + ## Act + Get-VSTeamBuildDefinition -projectName vsts -filter 'click*' + ## Assert Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/project/_apis/build/definitions*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and + $Uri -like "*https://dev.azure.com/test/vsts/_apis/build/definitions*" -and + $Uri -like "*api-version=$(_getApiVersion Build)*" -and $Uri -like "*name=click*" -and $Uri -like "*type=All*" } } - } - Context 'Get-VSTeamBuildDefinition with both parameters' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - Mock Invoke-RestMethod { return $resultsVSTS } - - It 'should return build definitions by filter' { - Get-VSTeamBuildDefinition -projectName project -filter 'click*' -type build + It 'with both parameters should return build definitions by filter' { + ## Act + Get-VSTeamBuildDefinition -projectName vsts -filter 'click*' -type build + ## Asset Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/project/_apis/build/definitions*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and + $Uri -like "*https://dev.azure.com/test/vsts/_apis/build/definitions*" -and + $Uri -like "*api-version=$(_getApiVersion Build)*" -and $Uri -like "*name=click*" -and $Uri -like "*type=build*" } } - } - - Context 'Get-VSTeamBuildDefinition by ID' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - Mock Invoke-RestMethod { return $resultsVSTS.value } - - It 'should return build definition' { - $b = Get-VSTeamBuildDefinition -projectName project -id 15 + It 'by id should return build definition' { + ## Act + $b = Get-VSTeamBuildDefinition -projectName project -id 101 + ## Assert $b | Get-Member | Select-Object -First 1 -ExpandProperty TypeName | Should be 'Team.BuildDefinition' Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions/15?api-version=$([VSTeamVersions]::Build)" + $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions/101?api-version=$(_getApiVersion Build)" } } - } - - Context 'Get-VSTeamBuildDefinition by ID -Raw' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - Mock Invoke-RestMethod { return $resultsVSTS.value } - It 'should return build definition' { - $raw = Get-VSTeamBuildDefinition -projectName project -id 15 -Raw + It 'by ID -Raw should return build definition' { + ## Act + $raw = Get-VSTeamBuildDefinition -projectName project -id 101 -Raw + ## Assert $raw | Get-Member | Select-Object -First 1 -ExpandProperty TypeName | Should be 'System.Management.Automation.PSCustomObject' Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions/15?api-version=$([VSTeamVersions]::Build)" + $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions/101?api-version=$(_getApiVersion Build)" } } - } - - Context 'Get-VSTeamBuildDefinition by ID -Json' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - Mock Invoke-RestMethod { return $resultsVSTS.value } - - It 'should return build definition' { - $b = Get-VSTeamBuildDefinition -projectName project -id 15 -Json + It 'by ID -Json should return build definition' { + ## Act + $b = Get-VSTeamBuildDefinition -projectName project -id 101 -Json + ## Assert $b | Get-Member | Select-Object -First 1 -ExpandProperty TypeName | Should be 'System.String' Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions/15?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Get-VSTeamBuildDefinition by ID local auth' { - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { return $resultsVSTS.value } - - It 'should return build definition' { - Get-VSTeamBuildDefinition -projectName project -id 15 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/definitions/15?api-version=$([VSTeamVersions]::Build)" + $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions/101?api-version=$(_getApiVersion Build)" } } - } - - Context 'Get-VSTeamBuildDefinition with revision parameter' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - Mock Invoke-RestMethod { return $resultsVSTS.value } - - It 'should return build definitions by revision' { - Get-VSTeamBuildDefinition -projectName project -id 16 -revision 1 + It 'with revision parameter should return build definitions by revision' { + ## Act + Get-VSTeamBuildDefinition -projectName project -id 101 -revision 1 + ## Assert Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions/16?api-version=$([VSTeamVersions]::Build)&revision=1" + $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions/101?api-version=$(_getApiVersion Build)&revision=1" } } } - - # Make sure these test run last as the need differnt - # [VSTeamVersions]::Account values - Context 'Get-VSTeamBuildDefinition with no account' { - [VSTeamVersions]::Account = $null - - It 'should return build definitions' { - { Get-VSTeamBuildDefinition -projectName project } | Should Throw - } - } } } \ No newline at end of file diff --git a/unit/test/Get-VSTeamBuildLog.Tests.ps1 b/unit/test/Get-VSTeamBuildLog.Tests.ps1 new file mode 100644 index 000000000..dfa32eefa --- /dev/null +++ b/unit/test/Get-VSTeamBuildLog.Tests.ps1 @@ -0,0 +1,89 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamBuildLog' { + Context 'Get-VSTeamBuildLog' { + ## Arrange + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock Invoke-RestMethod { return @{ + count = 4 + value = @{ } + } + } + + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Build' } + + Context 'Services' { + ## Arrange + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + It 'with build id should return full log' { + ## Act + Get-VSTeamBuildLog -projectName project -Id 1 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/1/logs?api-version=$(_getApiVersion Build)" + } + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/1/logs/3?api-version=$(_getApiVersion Build)" + } + } + + It 'with build id and index should return full log' { + ## Act + Get-VSTeamBuildLog -projectName project -Id 1 -Index 2 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/1/logs/2?api-version=$(_getApiVersion Build)" + } + } + } + + Context 'Server' { + ## Arrange + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + It 'with index on TFS local Auth Should return full log' { + ## Act + Get-VSTeamBuildLog -projectName project -Id 1 -Index 2 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/1/logs/2?api-version=$(_getApiVersion Build)" + } + } + + It 'on TFS local Auth should return full log' { + ## Act + Get-VSTeamBuildLog -projectName project -Id 1 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/1/logs?api-version=$(_getApiVersion Build)" + } + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/1/logs/3?api-version=$(_getApiVersion Build)" + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamBuildTag.Tests.ps1 b/unit/test/Get-VSTeamBuildTag.Tests.ps1 new file mode 100644 index 000000000..0c07bdbda --- /dev/null +++ b/unit/test/Get-VSTeamBuildTag.Tests.ps1 @@ -0,0 +1,59 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamBuildTag' { + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Build' } + + $tags = 'Tag1', 'Tag2' + Mock Invoke-RestMethod { + return @{ value = $tags } + } + + Context 'Get-VSTeamBuildTag' { + Context 'Services' { + $returndata = Get-VSTeamBuildTag -projectName project -id 2 + + It 'should create correct URL.' { + Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/2/tags?api-version=$(_getApiVersion Build)" + } + } + + It 'should return correct data.' { + Compare-Object $tags $returndata | Should Be $null + } + } + + Context 'Server' { + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + $returndata = Get-VSTeamBuildTag -projectName project -id 2 + + It 'should create correct URL.' { + Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/2/tags?api-version=$(_getApiVersion Build)" + } + } + + It 'should return correct data.' { + Compare-Object $tags $returndata | Should Be $null + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamClassificationNode.Tests.ps1 b/unit/test/Get-VSTeamClassificationNode.Tests.ps1 new file mode 100644 index 000000000..64e26e734 --- /dev/null +++ b/unit/test/Get-VSTeamClassificationNode.Tests.ps1 @@ -0,0 +1,92 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamClassificationNode.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'Get-VSTeamClassificationNode' { + $withoutChildNode = Get-Content "$PSScriptRoot\sampleFiles\withoutChildNode.json" -Raw | ConvertFrom-Json + $classificationNodeResult = Get-Content "$PSScriptRoot\sampleFiles\classificationNodeResult.json" -Raw | ConvertFrom-Json + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock _getApiVersion { return '5.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context 'simplest call' { + Mock Invoke-RestMethod { return $classificationNodeResult } + Mock Invoke-RestMethod { return $withoutChildNode } -ParameterFilter { $Uri -like "*Ids=43,44*" } + + It 'with StructureGroup should return Nodes' { + ## Act + Get-VSTeamClassificationNode -ProjectName "Public Demo" -StructureGroup "Iterations" + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/Public Demo/_apis/wit/classificationnodes/Iterations*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" + } + } + + It 'with depth should return Nodes' { + ## Act + Get-VSTeamClassificationNode -ProjectName "Public Demo" -StructureGroup "Iterations" -Depth 10 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/Public Demo/_apis/wit/classificationnodes/Iterations*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*`$Depth=10*" + } + } + + It 'by Path should return Nodes' { + ## Act + Get-VSTeamClassificationNode -ProjectName "Public Demo" -StructureGroup "Iterations" -Path "test/test/test" + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/Public Demo/_apis/wit/classificationnodes/Iterations/test/test/test*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" + } + } + + It 'by ids should return Nodes' { + ## Act + Get-VSTeamClassificationNode -ProjectName "Public Demo" -Ids @(1, 2, 3, 4) + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/Public Demo/_apis/wit/classificationnodes*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*Ids=1,2,3,4*" + } + } + + It 'should handle when there is no child nodes' { + ## Act + Get-VSTeamClassificationNode -ProjectName "Public Demo" -Ids @(43, 44) + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -like "https://dev.azure.com/test/Public Demo/_apis/wit/classificationnodes*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*Ids=43,44*" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamCloudSubscription.Tests.ps1 b/unit/test/Get-VSTeamCloudSubscription.Tests.ps1 new file mode 100644 index 000000000..c5f39c733 --- /dev/null +++ b/unit/test/Get-VSTeamCloudSubscription.Tests.ps1 @@ -0,0 +1,48 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamCloudSubscription' { + Context 'Get-VSTeamCloudSubscription' { + Mock Invoke-RestMethod { return @{value = 'subs' } } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'DistributedTask' } + + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context 'Services' { + Mock _getInstance { return 'https://dev.azure.com/test' } + + It 'should return all AzureRM Subscriptions' { + Get-VSTeamCloudSubscription + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/serviceendpointproxy/azurermsubscriptions?api-version=$(_getApiVersion DistributedTask)" + } + } + } + + Context 'Server' { + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + It 'should return all AzureRM Subscriptions' { + Get-VSTeamCloudSubscription + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/distributedtask/serviceendpointproxy/azurermsubscriptions?api-version=$(_getApiVersion DistributedTask)" + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamDescriptor.Tests.ps1 b/unit/test/Get-VSTeamDescriptor.Tests.ps1 new file mode 100644 index 000000000..7e9b2c208 --- /dev/null +++ b/unit/test/Get-VSTeamDescriptor.Tests.ps1 @@ -0,0 +1,69 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamDescriptor.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +#endregion + +Describe "VSTeamDescriptor" { + ## Arrange + $result = Get-Content "$PSScriptRoot\sampleFiles\descriptor.scope.TestProject.json" -Raw | ConvertFrom-Json + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + + Context 'Get-VSTeamDescriptor' { + Context 'Services' { + ## Arrange + # You have to set the version or the api-version will not be added when versions = '' + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Graph' } + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _supportsGraph + + Mock Invoke-RestMethod { return $result } + + It 'by StorageKey Should return groups' { + ## Act + Get-VSTeamDescriptor -StorageKey '010d06f0-00d5-472a-bb47-58947c230876' + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://vssps.dev.azure.com/test/_apis/graph/descriptors/010d06f0-00d5-472a-bb47-58947c230876?api-version=$(_getApiVersion Graph)" + } + } + } + + Context 'Server' { + ## Arrange + # TFS 2017 does not support this feature + Mock _callAPI { throw 'Should not be called' } -Verifiable + + # It is not supported on 2017 + Mock _getApiVersion { return 'TFS2017' } + Mock _getApiVersion { return '' } -ParameterFilter { $Service -eq 'Graph' } + It 'Should throw' { + ## Act / Assert + { Get-VSTeamDescriptor -StorageKey '010d06f0-00d5-472a-bb47-58947c230876' } | Should Throw + } + + It '_callAPI should not be called' { + Assert-MockCalled _callAPI -Exactly 0 + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamExtension.Tests.ps1 b/unit/test/Get-VSTeamExtension.Tests.ps1 new file mode 100644 index 000000000..19ddd28c3 --- /dev/null +++ b/unit/test/Get-VSTeamExtension.Tests.ps1 @@ -0,0 +1,80 @@ +Set-StrictMode -Version Latest + +#region inclucde +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamInstallState.ps1" +. "$here/../../Source/Classes/VSTeamExtension.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamExtension' { + ## Arrange + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'ExtensionsManagement' } + + $results = Get-Content "$PSScriptRoot\sampleFiles\extensionResults.json" -Raw | ConvertFrom-Json + $singleResult = Get-Content "$PSScriptRoot\sampleFiles\singleExtensionResult.json" -Raw | ConvertFrom-Json + + Context 'Get-VSTeamExtension' { + ## Arrange + BeforeAll { + $env:Team_TOKEN = '1234' + } + + AfterAll { + $env:TEAM_TOKEN = $null + } + + Mock _callAPI { return $results } + Mock _callAPI { return $singleResult } -ParameterFilter { $resource -eq "extensionmanagement/installedextensionsbyname/test/test" } + + It 'Should return extensions' { + ## Act + Get-VSTeamExtension + + ## Assert + Assert-MockCalled _callAPI -Exactly 1 -Scope It -ParameterFilter { + $Method -eq 'Get' -and + $subDomain -eq 'extmgmt' -and + $version -eq $(_getApiVersion ExtensionsManagement) + $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensions*" + } + } + + It 'Should return extensions with optional parameters' { + ## Act + Get-VSTeamExtension -IncludeInstallationIssues -IncludeDisabledExtensions -IncludeErrors + + ## Assert + Assert-MockCalled _callAPI -Exactly 1 -Scope It -ParameterFilter { + $Method -eq 'Get' -and + $subDomain -eq 'extmgmt' -and + $version -eq $(_getApiVersion ExtensionsManagement) + $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensions*" -and + $Url -like "*includeInstallationIssues*" -and + $Url -like "*includeDisabledExtensions*" -and + $Url -like "*includeErrors*" + } + } + + It 'Should return the extension' { + ## Act + Get-VSTeamExtension -PublisherId test -ExtensionId test + + ## Assert + Assert-MockCalled _callAPI -Exactly 1 -Scope It -ParameterFilter { + $Method -eq 'Get' -and + $subDomain -eq 'extmgmt' -and + $version -eq $(_getApiVersion ExtensionsManagement) + $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensionsbyname/test/test*" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamFeed.Tests.ps1 b/unit/test/Get-VSTeamFeed.Tests.ps1 new file mode 100644 index 000000000..a42f84f51 --- /dev/null +++ b/unit/test/Get-VSTeamFeed.Tests.ps1 @@ -0,0 +1,64 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamFeed.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamFeed' { + Context 'Get-VSTeamFeed' { + Context 'Services' { + ## Arrange + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Packaging' } + $results = Get-Content "$PSScriptRoot\sampleFiles\feeds.json" -Raw | ConvertFrom-Json + + Mock _supportsFeeds { return $true } + Mock Invoke-RestMethod { return $results } + Mock _getInstance { return 'https://dev.azure.com/test' } + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + Mock Invoke-RestMethod { return $results.value[0] } -ParameterFilter { $Uri -like "*00000000-0000-0000-0000-000000000000*" } + + it 'with no parameters should return all the Feeds' { + ## Act + Get-VSTeamFeed + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://feeds.dev.azure.com/test/_apis/packaging/feeds?api-version=$(_getApiVersion packaging)" + } + } + + it 'by id should return one feed' { + ## Act + Get-VSTeamFeed -id '00000000-0000-0000-0000-000000000000' + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://feeds.dev.azure.com/test/_apis/packaging/feeds/00000000-0000-0000-0000-000000000000?api-version=$(_getApiVersion packaging)" + } + } + } + + Context 'Server' { + ## Arrange + Mock _supportsFeeds { return $false } + Mock _getApiVersion { return 'TFS2017' } + Mock _getApiVersion { return '' } -ParameterFilter { $Service -eq 'Packaging' } + + it 'is not supported and should throw' { + ## Act / Assert + { Get-VSTeamFeed } | Should throw + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamGitCommit.Tests.ps1 b/unit/test/Get-VSTeamGitCommit.Tests.ps1 index 6df754692..55d1d7384 100644 --- a/unit/test/Get-VSTeamGitCommit.Tests.ps1 +++ b/unit/test/Get-VSTeamGitCommit.Tests.ps1 @@ -1,80 +1,51 @@ Set-StrictMode -Version Latest -InModuleScope VSTeam { - $results = [PSCustomObject]@{ - count = 2 - value = @( - [PSCustomObject]@{ - author = [PSCustomObject]@{ - date = '2020-02-19T15:12:01Z' - email = 'test@test.com' - name = 'Test User' - } - changeCounts = [PSCustomObject]@{ - Add = 2 - Delete = 0 - Edit = 1 - } - comment = 'Just a test commit' - commitId = '1234567890abcdef1234567890abcdef' - committer = [PSCustomObject]@{ - date = '2020-02-19T15:12:01Z' - email = 'test@test.com' - name = 'Test User' - } - remoteUrl = 'https://dev.azure.com/test/test/_git/test/commit/1234567890abcdef1234567890abcdef' - url = 'https://dev.azure.com/test/21AF684D-AFFB-4F9A-9D49-866EF24D6A4A/_apid/git/repositories/06E176BE-D3D2-41C2-AB34-5F4D79AEC86B/commits/1234567890abcdef1234567890abcdef' - }, - [PSCustomObject]@{ - author = [PSCustomObject]@{ - date = '2020-02-20T01:00:01Z' - email = 'eample@example.com' - name = 'Example User' - } - changeCounts = [PSCustomObject]@{ - Add = 8 - Delete = 1 - Edit = 0 - } - comment = 'Just another test commit' - commitId = 'abcdef1234567890abcdef1234567890' - committer = [PSCustomObject]@{ - date = '2020-02-20T01:00:01Z' - email = 'eample@example.com' - name = 'Example User' - } - remoteUrl = 'https://dev.azure.com/test/test/_git/test/commit/abcdef1234567890abcdef1234567890' - url = 'https://dev.azure.com/test/21AF684D-AFFB-4F9A-9D49-866EF24D6A4A/_apid/git/repositories/06E176BE-D3D2-41C2-AB34-5F4D79AEC86B/commits/abcdef1234567890abcdef1234567890' - } - ) - } +#region include +Import-Module SHiPS - Describe "Git VSTS" { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") - . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamGitUserDate.ps1" +. "$here/../../Source/Classes/VSTeamGitCommitRef.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion - Context 'Get-VSTeamGitCommit' { - Mock Invoke-RestMethod { return $results } -Verifiable -ParameterFilter { - $Uri -like "*repositories/06E176BE-D3D2-41C2-AB34-5F4D79AEC86B/commits*" - } +Describe "VSTeamGitCommit" { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" - Get-VSTeamGitCommit -ProjectName Test -RepositoryId 06E176BE-D3D2-41C2-AB34-5F4D79AEC86B + $results = Get-Content "$PSScriptRoot\sampleFiles\gitCommitResults.json" -Raw | ConvertFrom-Json - It 'Should return all commits for the repo' { - Assert-VerifiableMock + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Git' } + + Context 'Get-VSTeamGitCommit' { + Mock Invoke-RestMethod { return $results } + + It 'should return all commits for the repo' { + Get-VSTeamGitCommit -ProjectName Test -RepositoryId 06E176BE-D3D2-41C2-AB34-5F4D79AEC86B + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*repositories/06E176BE-D3D2-41C2-AB34-5F4D79AEC86B/commits*" } } - Context 'Get-VSTeamGitCommit with many Parameters' { - Mock Invoke-RestMethod { return $results } -Verifiable -ParameterFilter { + It 'with many Parameters should return all commits for the repo' { + Get-VSTeamGitCommit -ProjectName Test -RepositoryId '06E176BE-D3D2-41C2-AB34-5F4D79AEC86B' ` + -FromDate '2020-01-01' -ToDate '2020-03-01' ` + -ItemVersionVersionType 'commit' -ItemVersionVersion 'abcdef1234567890abcdef1234567890' -ItemVersionVersionOptions 'previousChange' ` + -CompareVersionVersionType 'commit' -CompareVersionVersion 'abcdef1234567890abcdef1234567890' -CompareVersionVersionOptions 'previousChange' ` + -FromCommitId 'abcdef' -ToCommitId 'fedcba' ` + -Author "Test" ` + -Top 100 -Skip 50 ` + -User "Test" + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*repositories/06E176BE-D3D2-41C2-AB34-5F4D79AEC86B/commits*" -and $Uri -like "*searchCriteria.fromDate=2020-01-01T00:00:00Z*" -and $Uri -like "*searchCriteria.toDate=2020-03-01T00:00:00Z*" -and @@ -91,23 +62,17 @@ InModuleScope VSTeam { $Uri -like "*searchCriteria.`$top=100*" -and $Uri -like "*searchCriteria.`$skip=50*" } + } + It 'with ItemPath parameters should return all commits for the repo' { Get-VSTeamGitCommit -ProjectName Test -RepositoryId '06E176BE-D3D2-41C2-AB34-5F4D79AEC86B' ` - -FromDate '2020-01-01' -ToDate '2020-03-01' ` - -ItemVersionVersionType 'commit' -ItemVersionVersion 'abcdef1234567890abcdef1234567890' -ItemVersionVersionOptions 'previousChange' ` - -CompareVersionVersionType 'commit' -CompareVersionVersion 'abcdef1234567890abcdef1234567890' -CompareVersionVersionOptions 'previousChange' ` - -FromCommitId 'abcdef' -ToCommitId 'fedcba' ` - -Author "Test" ` + -ItemPath 'test' ` + -ExcludeDeletes ` + -HistoryMode 'fullHistory' ` -Top 100 -Skip 50 ` -User "Test" - It 'Should return all commits for the repo' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitCommit with ItemPath parameters' { - Mock Invoke-RestMethod { return $results } -Verifiable -ParameterFilter { + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*repositories/06E176BE-D3D2-41C2-AB34-5F4D79AEC86B/commits*" -and $Uri -like "*searchCriteria.itemPath=test*" -and $Uri -like "*searchCriteria.excludeDeletes=true*" -and @@ -115,25 +80,6 @@ InModuleScope VSTeam { $Uri -like "*searchCriteria.`$top=100*" -and $Uri -like "*searchCriteria.`$skip=50*" } - - Get-VSTeamGitCommit -ProjectName Test -RepositoryId '06E176BE-D3D2-41C2-AB34-5F4D79AEC86B' ` - -ItemPath 'test' ` - -ExcludeDeletes ` - -HistoryMode 'fullHistory' ` - -Top 100 -Skip 50 ` - -User "Test" - - It 'Should return all commits for the repo' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitCommit by id throws' { - Mock Invoke-RestMethod { throw [System.Net.WebException] "Test Exception." } - - It 'Should throw' { - { Get-VSTeamGitCommit -ProjectName Test -RepositoryId 06E176BE-D3D2-41C2-AB34-5F4D79AEC86B } | Should Throw - } } } } \ No newline at end of file diff --git a/unit/test/Get-VSTeamGitRef.Tests.ps1 b/unit/test/Get-VSTeamGitRef.Tests.ps1 new file mode 100644 index 000000000..5975fa318 --- /dev/null +++ b/unit/test/Get-VSTeamGitRef.Tests.ps1 @@ -0,0 +1,116 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamRef.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe "VSTeamGitRef" { + ## Arrange + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Git' } + + $results = [PSCustomObject]@{ + value = [PSCustomObject]@{ + objectId = '6f365a7143e492e911c341451a734401bcacadfd' + name = 'refs/heads/master' + creator = [PSCustomObject]@{ + displayName = 'Microsoft.VisualStudio.Services.TFS' + id = '1' + uniqueName = 'some@email.com' + } + } + } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Mock Invoke-RestMethod { return $results } + Mock Invoke-RestMethod { throw [System.Net.WebException] "Test Exception." } -ParameterFilter { + $Uri -like "*00000000-0000-0000-0000-000000000001*" + } + + Context 'Get-VSTeamGitRef' { + It 'Should return a single ref by id' { + ## Act + Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/Test/_apis/git/repositories/00000000-0000-0000-0000-000000000000/refs?api-version=$(_getApiVersion Git)" + } + } + + It 'with Filter should return a single ref with filter' { + ## Act + Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -Filter "refs/heads" + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*filter=refs*" + } + } + + It 'with FilterContains should return a single ref' { + ## Act + Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -FilterContains "test" + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*filterContains=test" + } + } + + It 'Should return a single ref' { + ## Act + Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -Top 100 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*`$top=100" + } + } + + It 'with ContinuationToken should return a single ref' { + ## Act + Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -ContinuationToken "myToken" + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*continuationToken=myToken" + } + } + + It 'with Filter, FilterContains, Top should return a single ref' { + ## Act + Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -Filter "/refs/heads" -FilterContains "test" -Top 500 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*filter=/refs/heads*" -and + $Uri -like "*`$top=500*" -and + $Uri -like "*filterContains=test*" + } + } + + It 'by id throws should return a single repo by id' { + ## Act / Assert + { Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000001 } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamGitRepository.Tests.ps1 b/unit/test/Get-VSTeamGitRepository.Tests.ps1 new file mode 100644 index 000000000..715387cca --- /dev/null +++ b/unit/test/Get-VSTeamGitRepository.Tests.ps1 @@ -0,0 +1,200 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinition.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamQueue.ps1" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" +. "$here/../../Source/Public/Get-VSTeamBuildDefinition.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/Clear-VSTeamDefaultProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe "VSTeamGitRepository" { + $results = [PSCustomObject]@{ + value = [PSCustomObject]@{ + id = '' + url = '' + sshUrl = '' + remoteUrl = '' + defaultBranch = '' + size = 0 + name = '' + project = [PSCustomObject]@{ + name = 'Project' + id = 1 + description = '' + url = '' + state = '' + revision = '' + visibility = '' + } + } + } + + $singleResult = [PSCustomObject]@{ + id = '' + url = '' + sshUrl = '' + remoteUrl = '' + defaultBranch = '' + size = 0 + name = '' + project = [PSCustomObject]@{ + name = 'Project' + id = 1 + description = '' + url = '' + state = '' + revision = '' + visibility = '' + } + } + + ## Arrange + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Git' } + + ## If you don't call this and there is a default project in scope + ## these tests will fail. The API can be called with or without + ## a project and these tests are written to test without one. + Clear-VSTeamDefaultProject + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Mock Invoke-RestMethod { + # Write-Host "results $Uri" + return $results } + Mock Invoke-RestMethod { + # Write-Host "Single $Uri" + return $singleResult } -ParameterFilter { + $Uri -like "*00000000-0000-0000-0000-000000000000*" -or + $Uri -like "*testRepo*" + } + Mock Invoke-RestMethod { + # Write-Host "boom $Uri" + throw [System.Net.WebException] } -ParameterFilter { + $Uri -like "*00000000-0000-0000-0000-000000000101*" -or + $Uri -like "*boom*" + } + + Context 'Get-VSTeamGitRepository' { + Context 'Services' { + ## Arrange + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + It 'no parameters should return all repos for all projects' { + ## Act + Get-VSTeamGitRepository + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/git/repositories?api-version=$(_getApiVersion Git)" + } + } + + It 'by Id should return a single repo by id' { + ## Act + Get-VSTeamGitRepository -id 00000000-0000-0000-0000-000000000000 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/git/repositories/00000000-0000-0000-0000-000000000000?api-version=$(_getApiVersion Git)" + } + } + + It 'by name should return a single repo by name' { + ## Act + Get-VSTeamGitRepository -Name 'testRepo' + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/git/repositories/testRepo?api-version=$(_getApiVersion Git)" + } + } + + It 'by id should throw' { + ## Act / Assert + { Get-VSTeamGitRepository -id 00000000-0000-0000-0000-000000000101 } | Should Throw + } + + It 'by name should throw' { + ## Act / Assert + { Get-VSTeamGitRepository -Name 'boom' } | Should Throw + } + } + + Context 'Server' { + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + Mock _useWindowsAuthenticationOnPremise { return $true } + + It 'no parameters Should return Git repo' { + ## Act + Get-VSTeamGitRepository + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/git/repositories?api-version=$(_getApiVersion Git)" + } + } + + It 'by Id should return a single repo by id' { + ## Act + Get-VSTeamGitRepository -id 00000000-0000-0000-0000-000000000000 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/git/repositories/00000000-0000-0000-0000-000000000000?api-version=$(_getApiVersion Git)" + } + } + + It 'by name should return a single repo by name' { + ## Act + Get-VSTeamGitRepository -Name 'testRepo' + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/git/repositories/testRepo?api-version=$(_getApiVersion Git)" + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamGitStat.Tests.ps1 b/unit/test/Get-VSTeamGitStat.Tests.ps1 index 77f2f9372..e77e8b5a6 100644 --- a/unit/test/Get-VSTeamGitStat.Tests.ps1 +++ b/unit/test/Get-VSTeamGitStat.Tests.ps1 @@ -1,185 +1,86 @@ Set-StrictMode -Version Latest -InModuleScope VSTeam { - $singleResult = [PSCustomObject]@{ - commit = [PSCustomObject]@{ - commitId = '67cae2b029dff7eb3dc062b49403aaedca5bad8d' - author = [PSCustomObject]@{ - name = '"Chuck Reinhart' - email = 'fabrikamfiber3@hotmail.com' - date = '2014-01-29T23:52:56Z' - } - committer = [PSCustomObject]@{ - name = '"Chuck Reinhart' - email = 'fabrikamfiber3@hotmail.com' - date = '2014-01-29T23:52:56Z' - } - comment = 'home page' - url = 'https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/67cae2b029dff7eb3dc062b49403aaedca5bad8d' - } - name = 'develop' - aheadCount = 1 - behindCount = 17 - isBaseVersion = $false - } +#region include +Import-Module SHiPS - $multipleResults = [PSCustomObject]@{ - count = 3 - value = @( - [PSCustomObject]@{ - commit = [PSCustomObject]@{ - commitId = '67cae2b029dff7eb3dc062b49403aaedca5bad8d' - author = [PSCustomObject]@{ - name = '"Chuck Reinhart' - email = 'fabrikamfiber3@hotmail.com' - date = '2014-01-29T23:52:56Z' - } - committer = [PSCustomObject]@{ - name = '"Chuck Reinhart' - email = 'fabrikamfiber3@hotmail.com' - date = '2014-01-29T23:52:56Z' - } - comment = 'home page' - url = 'https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/67cae2b029dff7eb3dc062b49403aaedca5bad8d' - } - name = 'develop' - aheadCount = 1 - behindCount = 17 - isBaseVersion = $false - }, - [PSCustomObject]@{ - commit = [PSCustomObject]@{ - parents = @('fe17a84cc2dfe0ea3a2202ab4dbac0706058e41f') - treeId = '8263e7232a2331c563d737e4fc4e9c66a8286c63' - commitId = '23d0bc5b128a10056dc68afece360d8a0fabb014' - author = [PSCustomObject]@{ - name = 'Norman Paulk' - email = 'Fabrikamfiber16@hotmail.com' - date = '2014-06-30T18:10:55Z' - } - committer = [PSCustomObject]@{ - name = 'Norman Paulk' - email = 'Fabrikamfiber16@hotmail.com' - date = '2014-06-30T18:10:55Z' - } - comment = 'Better description for hello world\n' - url = 'https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/23d0bc5b128a10056dc68afece360d8a0fabb014' - } - name = 'master' - aheadCount = 0 - behindCount = 0 - isBaseVersion = $true - }, - [PSCustomObject]@{ - commit = [PSCustomObject]@{ - parents = @('fe17a84cc2dfe0ea3a2202ab4dbac0706058e41f') - treeId = '8263e7232a2331c563d737e4fc4e9c66a8286c63' - commitId = '23d0bc5b128a10056dc68afece360d8a0fabb014' - author = [PSCustomObject]@{ - name = 'Norman Paulk' - email = 'Fabrikamfiber16@hotmail.com' - date = '2014-06-30T18:10:55Z' - } - committer = [PSCustomObject]@{ - name = 'Norman Paulk' - email = 'Fabrikamfiber16@hotmail.com' - date = '2014-06-30T18:10:55Z' - } - comment = 'Better description for hello world\n' - url = 'https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/23d0bc5b128a10056dc68afece360d8a0fabb014' - } - name = 'npaulk/feature' - aheadCount = 0 - behindCount = 0 - isBaseVersion = $true - } - ) - } +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") - Describe "Git VSTS" { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamFeed.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +#endregion - . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" +Describe "TeamGitStat" { + ## Arrange + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" - Context 'Get-VSTeamGitStat' { - Mock Invoke-RestMethod { return $multipleResults } -Verifiable -ParameterFilter { - $Uri -like "*/Test/*" -and - $Uri -like "*repositories/00000000-0000-0000-0000-000000000000/stats/branches*" - } + $singleResult = Get-Content "$PSScriptRoot\sampleFiles\gitStatSingleResult.json" -Raw | ConvertFrom-Json - Get-VSTeamGitStat -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Invoke-RestMethod { return $singleResult } - It 'Should return multiple results' { - Assert-VerifiableMock + Context 'Get-VSTeamGitStat' { + It 'should return multiple results' { + ## Act + Get-VSTeamGitStat -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*/Test/*" -and + $Uri -like "*repositories/00000000-0000-0000-0000-000000000000/stats/branches*" } } - Context 'Get-VSTeamGitStat by name' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable -ParameterFilter { + It 'by branch name should return multiple results' { + ## Act + Get-VSTeamGitStat -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -BranchName develop + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*/Test/*" -and $Uri -like "*repositories/00000000-0000-0000-0000-000000000000/stats/branches*" -and $Uri -like "*name=develop*" } - - Get-VSTeamGitStat -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -BranchName develop - - It 'Should return multiple results' { - Assert-VerifiableMock - } } - Context 'Get-VSTeamGitStat by tag' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable -ParameterFilter { + It 'by tag should return multiple results' { + ## Act + Get-VSTeamGitStat -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -BranchName "develop" -VersionType "tag" -Version "test" + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*/Test/*" -and $Uri -like "*repositories/00000000-0000-0000-0000-000000000000/stats/branches*" -and $Uri -like "*baseVersionDescriptor.versionType=tag*" -and $Uri -like "*baseVersionDescriptor.version=test*" } - - Get-VSTeamGitStat -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -BranchName "develop" -VersionType "tag" -Version "test" - - It 'Should return multiple results' { - Assert-VerifiableMock - } } - Context 'Get-VSTeamGitStat by tag with options' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable -ParameterFilter { + It 'by tag with options should return multiple results' { + ## Act + Get-VSTeamGitStat -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -BranchName "develop" -VersionType "tag" -Version "test" -VersionOptions previousChange + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*/Test/*" -and $Uri -like "*repositories/00000000-0000-0000-0000-000000000000/stats/branches*" -and $Uri -like "*baseVersionDescriptor.versionType=tag*" -and $Uri -like "*baseVersionDescriptor.version=test*" -and $Uri -like "*baseVersionDescriptor.versionOptions=previousChange*" } - - Get-VSTeamGitStat -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -BranchName "develop" -VersionType "tag" -Version "test" -VersionOptions previousChange - - It 'Should return multiple results' { - Assert-VerifiableMock - } } - Context 'Get-VSTeamGitStat by commit throws' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should throw because of invalid parameters' { - { Get-VSTeamGitStat -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -VersionType commit -Version '' } | Should Throw - } - } - - Context 'Get-VSTeamGitStat by id throws' { - Mock Invoke-RestMethod { throw [System.Net.WebException] "Test Exception." } - - It 'Should return a single repo by id' { - { Get-VSTeamGitStat -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 } | Should Throw - } + It 'by commit should throw because of invalid parameters' { + ## Act / Assert + { Get-VSTeamGitStat -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -VersionType commit -Version '' } | Should Throw } } } \ No newline at end of file diff --git a/unit/test/Get-VSTeamGroup.Tests.ps1 b/unit/test/Get-VSTeamGroup.Tests.ps1 new file mode 100644 index 000000000..9677f4dc6 --- /dev/null +++ b/unit/test/Get-VSTeamGroup.Tests.ps1 @@ -0,0 +1,139 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamGroup.ps1" +. "$here/../../Source/Classes/VSTeamDescriptor.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/Get-VSTeamDescriptor.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe "VSTeamGroup" { + Context 'Get-VSTeamGroup' { + Context 'Services' { + $groupListResult = Get-Content "$PSScriptRoot\sampleFiles\groups.json" -Raw | ConvertFrom-Json + $projectResult = Get-Content "$PSScriptRoot\sampleFiles\projectResult.json" -Raw | ConvertFrom-Json + $groupSingleResult = Get-Content "$PSScriptRoot\sampleFiles\groupsSingle.json" -Raw | ConvertFrom-Json + $scopeResult = Get-Content "$PSScriptRoot\sampleFiles\descriptor.scope.TestProject.json" -Raw | ConvertFrom-Json + + # You have to set the version or the api-version will not be added when versions = '' + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Graph' } + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + Mock _supportsGraph + + Mock Invoke-RestMethod { return $groupListResult } + + It 'by project should return groups' { + Mock Get-VSTeamProject { return $projectResult } -Verifiable + Mock Get-VSTeamDescriptor { return [VSTeamDescriptor]::new($scopeResult) } -Verifiable + + Get-VSTeamGroup -ProjectName "Test Project Public" + + # With PowerShell core the order of the query string is not the + # same from run to run! So instead of testing the entire string + # matches I have to search for the portions I expect but can't + # assume the order. + # The general string should look like this: + # "https://vssps.dev.azure.com/test/_apis/graph/groups?api-version=$(_getApiVersion Graph)&scopeDescriptor=scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2" + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/groups*" -and + $Uri -like "*api-version=$(_getApiVersion Graph)*" -and + $Uri -like "*scopeDescriptor=scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2*" + } + } + + It 'by scopeDescriptor should return groups' { + Get-VSTeamGroup -ScopeDescriptor scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2 + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/groups*" -and + $Uri -like "*api-version=$(_getApiVersion Graph)*" -and + $Uri -like "*scopeDescriptor=scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2*" + } + } + + It 'by subjectTypes should return groups' { + Get-VSTeamGroup -SubjectTypes vssgp, aadgp + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/groups*" -and + $Uri -like "*api-version=$(_getApiVersion Graph)*" -and + $Uri -like "*subjectTypes=vssgp,aadgp*" + } + } + + It 'by subjectTypes and scopeDescriptor should return groups' { + Get-VSTeamGroup -ScopeDescriptor scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2 -SubjectTypes vssgp, aadgp + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/groups*" -and + $Uri -like "*api-version=$(_getApiVersion Graph)*" -and + $Uri -like "*subjectTypes=vssgp,aadgp*" -and + $Uri -like "*scopeDescriptor=scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2*" + } + } + + It 'by descriptor should return the group' { + Mock Invoke-RestMethod { return $groupSingleResult } -Verifiable + + Get-VSTeamGroup -GroupDescriptor 'vssgp.Uy0xLTktMTU1MTM3NDI0NS04NTYwMDk3MjYtNDE5MzQ0MjExNy0yMzkwNzU2MTEwLTI3NDAxNjE4MjEtMC0wLTAtMC0x' + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/groups/vssgp.Uy0xLTktMTU1MTM3NDI0NS04NTYwMDk3MjYtNDE5MzQ0MjExNy0yMzkwNzU2MTEwLTI3NDAxNjE4MjEtMC0wLTAtMC0x*" -and + $Uri -like "*api-version=$(_getApiVersion Graph)*" + } + } + + It 'Should throw' { + Mock Invoke-RestMethod { throw 'Error' } + { Get-VSTeamGroup -ProjectName Demo } | Should Throw + } + + It 'Should throw' { + Mock Invoke-RestMethod { throw 'Error' } + { Get-VSTeamGroup -GroupDescriptor } | Should Throw + } + } + } + + Context 'Server' { + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + Mock _callAPI + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + # if you don't _callAPI will be called for this an throw off the count + # below. + Mock _getProjects { return @() } + + Mock _getApiVersion { return 'TFS2017' } + Mock _getApiVersion { return '' } -ParameterFilter { $Service -eq 'Graph' } + + # The Graph API is not supported on TFS + Mock _supportsGraph { throw 'This account does not support the graph API.' } + + It 'Should throw' { + { Get-VSTeamGroup } | Should Throw + } + + It '_callAPI should not be called' { + Assert-MockCalled _callAPI -Exactly -Times 0 -Scope Context + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamInfo.Tests.ps1 b/unit/test/Get-VSTeamInfo.Tests.ps1 index fd0edbef3..c8ee60e92 100644 --- a/unit/test/Get-VSTeamInfo.Tests.ps1 +++ b/unit/test/Get-VSTeamInfo.Tests.ps1 @@ -1,5 +1,6 @@ Set-StrictMode -Version Latest +#region include $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") @@ -7,26 +8,25 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Classes/VSTeamProjectCache.ps1" . "$here/../../Source/Private/common.ps1" . "$here/../../Source/Public/$sut" +#endregion -Describe 'Get-VSTeamInfo' { - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - +Describe 'VSTeamInfo' { + ## Arrange . "$PSScriptRoot\mocks\mockProjectDynamicParamMandatoryFalse.ps1" Context 'Get-VSTeamInfo' { AfterAll { $Global:PSDefaultParameterValues.Remove("*:projectName") } - + It 'should return account and default project' { [VSTeamVersions]::Account = "mydemos" $Global:PSDefaultParameterValues['*:projectName'] = 'TestProject' + ## Act $info = Get-VSTeamInfo + ## Assert $info.Account | Should Be "mydemos" $info.DefaultProject | Should Be "TestProject" } diff --git a/unit/test/Get-VSTeamJobRequest.Tests.ps1 b/unit/test/Get-VSTeamJobRequest.Tests.ps1 index c8bbc3643..255255670 100644 --- a/unit/test/Get-VSTeamJobRequest.Tests.ps1 +++ b/unit/test/Get-VSTeamJobRequest.Tests.ps1 @@ -1,5 +1,6 @@ Import-Module SHiPS +#region include $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") @@ -9,34 +10,29 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Classes/VSTeamJobRequest.ps1" . "$here/../../Source/Private/common.ps1" . "$here/../../Source/Public/$sut" - -$resultsAzD = Get-Content "$PSScriptRoot/sampleFiles/jobrequestsAzD.json" -Raw | ConvertFrom-Json -$results2017 = Get-Content "$PSScriptRoot/sampleFiles/jobrequests2017.json" -Raw | ConvertFrom-Json +#endregion Describe "Get-VSTeamJobRequest" { - Context "2017" { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Remove-VSTeamAccount - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $results2017 - } + ## Arrange + $resultsAzD = Get-Content "$PSScriptRoot/sampleFiles/jobrequestsAzD.json" -Raw | ConvertFrom-Json + $results2017 = Get-Content "$PSScriptRoot/sampleFiles/jobrequests2017.json" -Raw | ConvertFrom-Json - It "return all jobs" { - # This should stop the call to cache projects - [VSTeamProjectCache]::timestamp = (Get-Date).Minute + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'DistributedTask' } + Context "Server" { + ## Arrnage + Mock Invoke-RestMethod { return $results2017 } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + It "return all jobs" { + ## Act Get-VSTeamJobRequest -PoolId 5 -AgentID 4 + ## Assert Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*http://localhost:8080/tfs/defaultcollection/_apis/distributedtask/pools/5/jobrequests*" -and - $Uri -like "*api-version=$([VSTeamVersions]::DistributedTask)*" -and - $Uri -like "*agentid=4*" + $Uri -like "*api-version=$(_getApiVersion DistributedTask)*" -and + $Uri -like "*agentid=4*" } } @@ -48,25 +44,16 @@ Describe "Get-VSTeamJobRequest" { Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*http://localhost:8080/tfs/defaultcollection/_apis/distributedtask/pools/5/jobrequests*" -and - $Uri -like "*api-version=$([VSTeamVersions]::DistributedTask)*" -and + $Uri -like "*api-version=$(_getApiVersion DistributedTask)*" -and $Uri -like "*agentid=4*" -and $Uri -like "*completedRequestCount=2*" } } } - Context "AzD" { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Remove-VSTeamAccount - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $resultsAzD - } + Context "Services" { + Mock Invoke-RestMethod { return $resultsAzD } + Mock _getInstance { return 'https://dev.azure.com/test' } It "return all jobs" { # This should stop the call to cache projects @@ -76,20 +63,20 @@ Describe "Get-VSTeamJobRequest" { Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*https://dev.azure.com/test/_apis/distributedtask/pools/5/jobrequests*" -and - $Uri -like "*api-version=$([VSTeamVersions]::DistributedTask)*" -and - $Uri -like "*agentid=4*" + $Uri -like "*api-version=$(_getApiVersion DistributedTask)*" -and + $Uri -like "*agentid=4*" } } It "return 2 jobs" { # This should stop the call to cache projects [VSTeamProjectCache]::timestamp = (Get-Date).Minute - + Get-VSTeamJobRequest -PoolId 5 -AgentID 4 -completedRequestCount 2 Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*https://dev.azure.com/test/_apis/distributedtask/pools/5/jobrequests*" -and - $Uri -like "*api-version=$([VSTeamVersions]::DistributedTask)*" -and + $Uri -like "*api-version=$(_getApiVersion DistributedTask)*" -and $Uri -like "*agentid=4*" -and $Uri -like "*completedRequestCount=2*" } diff --git a/unit/test/Get-VSTeamMember.Tests.ps1 b/unit/test/Get-VSTeamMember.Tests.ps1 new file mode 100644 index 000000000..a6eec9d01 --- /dev/null +++ b/unit/test/Get-VSTeamMember.Tests.ps1 @@ -0,0 +1,100 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe "VSTeamMember" { + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Context 'Get-VSTeamMember for specific project and team' { + Mock Invoke-RestMethod { return @{value = 'teams' } } + + It 'Should return teammembers' { + Get-VSTeamMember -ProjectName TestProject -TeamId TestTeam + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/TestProject/teams/TestTeam/members?api-version=$(_getApiVersion Core)" + } + } + } + + Context 'Get-VSTeamMember for specific project and team, with top' { + Mock Invoke-RestMethod { return @{value = 'teams' } } + + It 'Should return teammembers' { + Get-VSTeamMember -ProjectName TestProject -TeamId TestTeam -Top 10 + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/projects/TestProject/teams/TestTeam/members*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*`$top=10*" + } + } + } + + Context 'Get-VSTeamMember for specific project and team, with skip' { + Mock Invoke-RestMethod { return @{value = 'teams' } } + + It 'Should return teammembers' { + Get-VSTeamMember -ProjectName TestProject -TeamId TestTeam -Skip 5 + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/projects/TestProject/teams/TestTeam/members*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*`$skip=5*" + } + } + } + + Context 'Get-VSTeamMember for specific project and team, with top and skip' { + Mock Invoke-RestMethod { return @{value = 'teams' } } + + It 'Should return teammembers' { + Get-VSTeamMember -ProjectName TestProject -TeamId TestTeam -Top 10 -Skip 5 + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/projects/TestProject/teams/TestTeam/members*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*`$top=10*" -and + $Uri -like "*`$skip=5*" + } + } + } + + Context 'Get-VSTeamMember for specific team, fed through pipeline' { + Mock Invoke-RestMethod { return @{value = 'teammembers' } } + + It 'Should return teammembers' { + New-Object -TypeName PSObject -Prop @{projectname = "TestProject"; name = "TestTeam" } | Get-VSTeamMember + + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/TestProject/teams/TestTeam/members?api-version=$(_getApiVersion Core)" + } + } + } + + # Must be last because it sets [VSTeamVersions]::Account to $null + Context '_buildURL handles exception' { + + # Arrange + [VSTeamVersions]::Account = $null + + It 'should return approvals' { + + # Act + { _buildURL -ProjectName project -TeamId 1 } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamMembership.Tests.ps1 b/unit/test/Get-VSTeamMembership.Tests.ps1 new file mode 100644 index 000000000..66849226a --- /dev/null +++ b/unit/test/Get-VSTeamMembership.Tests.ps1 @@ -0,0 +1,56 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/callMembershipAPI.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamMembership' { + ## Arrange + # You have to set the version or the api-version will not be added when [VSTeamVersions]::Graph = '' + Mock _supportsGraph + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Graph' } + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Invoke-RestMethod + + $UserDescriptor = 'aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj' + $GroupDescriptor = 'vssgp.Uy0xLTktMTU1MTM3NDI0NS04NTYwMDk3MjYtNDE5MzQ0MjExNy0yMzkwNzU2MTEwLTI3NDAxNjE4MjEtMC0wLTAtMC0x' + + Context 'Get-VSTeamMembership' { + It "for member should get a container's members" { + ## Act + $null = Get-VSTeamMembership -MemberDescriptor $UserDescriptor + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq "Get" -and + $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/memberships/$MemberDescriptor*" -and + $Uri -like "*api-version=$(_getApiVersion Graph)*" + } + } + + It "for Group should get a container's members" { + ## Act + $null = Get-VSTeamMembership -ContainerDescriptor $GroupDescriptor + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq "Get" -and + $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/memberships/$GroupDescriptor*" -and + $Uri -like "*api-version=$(_getApiVersion Graph)*" -and + $Uri -like "*direction=Down*" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamOption.Tests.ps1 b/unit/test/Get-VSTeamOption.Tests.ps1 index ae5e5a3aa..fbf5133dd 100644 --- a/unit/test/Get-VSTeamOption.Tests.ps1 +++ b/unit/test/Get-VSTeamOption.Tests.ps1 @@ -1,5 +1,6 @@ Set-StrictMode -Version Latest +#region include $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") @@ -8,9 +9,11 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Private/common.ps1" . "$here/../../Source/Private/applyTypes.ps1" . "$here/../../Source/Public/$sut" +#endregion Describe 'Get-VSTeamOption' { Context 'Get-VSTeamOption' { + ## Arrange # Set the account to use for testing. A normal user would do this # using the Set-VSTeamAccount function. Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable @@ -28,14 +31,20 @@ Describe 'Get-VSTeamOption' { } It 'Should return all options' { + ## Act Get-VSTeamOption | Should Not Be $null + + ## Assert Assert-MockCalled Invoke-RestMethod -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis" } } It 'Should return release options' { + ## Act Get-VSTeamOption -subDomain vsrm | Should Not Be $null + + ## Assert Assert-MockCalled Invoke-RestMethod -ParameterFilter { $Uri -eq "https://vsrm.dev.azure.com/test/_apis" } diff --git a/unit/test/Get-VSTeamPermissionInheritance.Tests.ps1 b/unit/test/Get-VSTeamPermissionInheritance.Tests.ps1 index c7999c34b..54de5ff97 100644 --- a/unit/test/Get-VSTeamPermissionInheritance.Tests.ps1 +++ b/unit/test/Get-VSTeamPermissionInheritance.Tests.ps1 @@ -1,5 +1,6 @@ Set-StrictMode -Version Latest +#region include Import-Module SHiPS $here = Split-Path -Parent $MyInvocation.MyCommand.Path @@ -17,97 +18,74 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Public/Get-VSTeamGitRepository.ps1" . "$here/../../Source/Public/Get-VSTeamAccessControlList.ps1" . "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamPermissionInheritance' { + ## Arrange + $singleResult = Get-Content "$PSScriptRoot\sampleFiles\projectResult.json" -Raw | ConvertFrom-Json + $buildDefresults = Get-Content "$PSScriptRoot\sampleFiles\buildDefAzD.json" -Raw | ConvertFrom-Json + $gitRepoResult = Get-Content "$PSScriptRoot\sampleFiles\singleGitRepo.json" -Raw | ConvertFrom-Json + $releaseDefresults = Get-Content "$PSScriptRoot\sampleFiles\releaseDefAzD.json" -Raw | ConvertFrom-Json + $buildDefHierarchyResults = Get-Content "$PSScriptRoot\sampleFiles\buildDefHierarchyQuery.json" -Raw | ConvertFrom-Json + $accesscontrollistsResult = Get-Content "$PSScriptRoot\sampleFiles\repoAccesscontrollists.json" -Raw | ConvertFrom-Json + $releaseDefHierarchyResults = Get-Content "$PSScriptRoot\sampleFiles\releaseDefHierarchyQuery.json" -Raw | ConvertFrom-Json + + Context 'Get-VSTeamPermissionInheritance' { + ## Arrange + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { + $Service -eq 'Build' -or + $Service -eq 'Release' -or + $Service -eq 'Git' + } + Mock _hasProjectCacheExpired { return $false } -# Loading System.Web avoids issues finding System.Web.HttpUtility -Add-Type -AssemblyName 'System.Web' - -$buildDefresults = Get-Content "$PSScriptRoot\sampleFiles\buildDefAzD.json" -Raw | ConvertFrom-Json -$releaseDefresults = Get-Content "$PSScriptRoot\sampleFiles\releaseDefAzD.json" -Raw | ConvertFrom-Json -$gitRepoResult = Get-Content "$PSScriptRoot\sampleFiles\singleGitRepo.json" -Raw | ConvertFrom-Json -$buildDefHierarchyResults = Get-Content "$PSScriptRoot\sampleFiles\buildDefHierarchyQuery.json" -Raw | ConvertFrom-Json -$accesscontrollistsResult = Get-Content "$PSScriptRoot\sampleFiles\repoAccesscontrollists.json" -Raw | ConvertFrom-Json -$releaseDefHierarchyResults = Get-Content "$PSScriptRoot\sampleFiles\releaseDefHierarchyQuery.json" -Raw | ConvertFrom-Json - -$singleResult = [PSCustomObject]@{ - name = 'Project' - description = '' - url = '' - id = '123-5464-dee43' - state = '' - visibility = '' - revision = 0 - defaultTeam = [PSCustomObject]@{} - _links = [PSCustomObject]@{} -} - -Describe 'Get-VSTeamPermissionInheritance' { - Remove-VSTeamAccount - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Get-VSTeamPermissionInheritance buildDef' { - Mock _useWindowsAuthenticationOnPremise { return $true } Mock Get-VSTeamProject { return $singleResult } + Mock Get-VSTeamGitRepository { return $gitRepoResult } Mock Get-VSTeamBuildDefinition { return $buildDefresults.value } - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - # Write-Host $([VSTeamVersions]::Build) - - return $buildDefHierarchyResults + Mock Get-VSTeamReleaseDefinition { return $releaseDefresults.value } + Mock Get-VSTeamAccessControlList { return $accesscontrollistsResult.value } + Mock Invoke-RestMethod { return $releaseDefHierarchyResults } -ParameterFilter { + $Body -like '*c788c23e-1b46-4162-8f5e-d7585343b5de*' + } + Mock Invoke-RestMethod { return $buildDefHierarchyResults } -ParameterFilter { + $Body -like '*010d06f0-00d5-472a-bb47-58947c230876/1432*' } - It 'should return true' { - Get-VSTeamPermissionInheritance -projectName project -Name dynamTest-Docker-CI -resourceType BuildDefinition | Should be $true + It 'buildDef should return true' { + ## Act + $actual = Get-VSTeamPermissionInheritance -projectName Project -Name dynamTest-Docker-CI -resourceType BuildDefinition + ## Assert + $actual | Should be $true Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' -and - $Body -like '*123-5464-dee43/1432*' -and + $Body -like '*010d06f0-00d5-472a-bb47-58947c230876/1432*' -and $Body -like '*33344d9c-fc72-4d6f-aba5-fa317101a7e9*' -and - $Uri -like "*https://dev.azure.com/test/_apis/Contribution/HierarchyQuery/project/123-5464-dee43*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" + $Uri -like "*https://dev.azure.com/test/_apis/Contribution/HierarchyQuery/Project/010d06f0-00d5-472a-bb47-58947c230876*" -and + $Uri -like "*api-version=$(_getApiVersion Build)*" } } - } - Context 'Get-VSTeamPermissionInheritance releaseDef' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Get-VSTeamProject { return $singleResult } - Mock Get-VSTeamReleaseDefinition { return $releaseDefresults.value } - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - # Write-Host $([VSTeamVersions]::Release) - - return $releaseDefHierarchyResults - } - - It 'should return true' { + It 'releaseDef should return true' { + ## Act Get-VSTeamPermissionInheritance -projectName project -Name PTracker-CD -resourceType ReleaseDefinition | Should be $true + ## Assert Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' -and $Body -like '*c788c23e-1b46-4162-8f5e-d7585343b5de*' -and - $Body -like '*123-5464-dee43//2*' -and - $Uri -like "*https://dev.azure.com/test/_apis/Contribution/HierarchyQuery/project/123-5464-dee43*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Release)*" + $Body -like '*010d06f0-00d5-472a-bb47-58947c230876//2*' -and + $Uri -like "*https://dev.azure.com/test/_apis/Contribution/HierarchyQuery/project/010d06f0-00d5-472a-bb47-58947c230876*" -and + $Uri -like "*api-version=$(_getApiVersion Release)*" } } - } - - Context 'Get-VSTeamPermissionInheritance repository' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Get-VSTeamProject { return $singleResult } - Mock Get-VSTeamGitRepository { return $gitRepoResult } - Mock Get-VSTeamAccessControlList { return $accesscontrollistsResult.value } -Verifiable - - It 'should return true' { + + It 'repository should return true' { + ## Act Get-VSTeamPermissionInheritance -projectName project -Name project -resourceType Repository | Should be $true + ## Assert Assert-MockCalled Get-VSTeamAccessControlList -Exactly -Scope It -Times 1 } } diff --git a/unit/test/Get-VSTeamPolicy.Tests.ps1 b/unit/test/Get-VSTeamPolicy.Tests.ps1 new file mode 100644 index 000000000..94c53a172 --- /dev/null +++ b/unit/test/Get-VSTeamPolicy.Tests.ps1 @@ -0,0 +1,59 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamPolicy' { + ## Arrange + $results = [PSCustomObject]@{ + value = [PSCustomObject]@{ } + } + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Invoke-RestMethod { return $results } + Mock Invoke-RestMethod { throw 'Error' } -ParameterFilter { $Uri -like "*boom*" } + + Context 'Get-VSTeamPolicy' { + It 'by project should return policies' { + ## Act + Get-VSTeamPolicy -ProjectName Demo + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/configurations?api-version=$(_getApiVersion Git)" + } + } + + It 'by project and id should return the policy' { + ## Act + Get-VSTeamPolicy -ProjectName Demo -Id 4 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/configurations/4?api-version=$(_getApiVersion Git)" + } + } + + It 'by project should throw' { + ## Act / Assert + { Get-VSTeamPolicy -ProjectName boom } | Should Throw + } + + It 'by project and id throws should throw' { + ## Act / Assert + { Get-VSTeamPolicy -ProjectName boom -Id 4 } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamPolicyType.Tests.ps1 b/unit/test/Get-VSTeamPolicyType.Tests.ps1 new file mode 100644 index 000000000..4ec750786 --- /dev/null +++ b/unit/test/Get-VSTeamPolicyType.Tests.ps1 @@ -0,0 +1,63 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe "VSTeamPolicyType" { + ## Arrnage + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Policy' } + + $results = [PSCustomObject]@{ + value = [PSCustomObject]@{ } + } + + $singleResult = [PSCustomObject]@{ } + + Mock Invoke-RestMethod { return $results } + Mock Invoke-RestMethod { throw 'Error' } -ParameterFilter { $Uri -like "*boom*" } + Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -like "*90a51335-0c53-4a5f-b6ce-d9aff3ea60e0*" } + + Context 'Get-VSTeamPolicyType' { + It 'by project should return policies' { + ## Act + Get-VSTeamPolicyType -ProjectName Demo + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/types?api-version=$(_getApiVersion Policy)" + } + } + + It 'by project throws should throw' { + ## Act / Assert + { Get-VSTeamPolicyType -ProjectName boom } | Should Throw + } + + It 'Should return policies' { + ## Act + Get-VSTeamPolicyType -ProjectName Demo -id 90a51335-0c53-4a5f-b6ce-d9aff3ea60e0 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/types/90a51335-0c53-4a5f-b6ce-d9aff3ea60e0?api-version=$(_getApiVersion Policy)" + } + } + + It 'Should return policies' { + ## Act / Assert + { Get-VSTeamPolicyType -ProjectName boom -id 90a51335-0c53-4a5f-b6ce-d9aff3ea60e1 } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamPool.Tests.ps1 b/unit/test/Get-VSTeamPool.Tests.ps1 new file mode 100644 index 000000000..1202bd102 --- /dev/null +++ b/unit/test/Get-VSTeamPool.Tests.ps1 @@ -0,0 +1,94 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamPool' { + ## Arrange + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _hasProjectCacheExpired { return $false } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'DistributedTask' } + + + $hostedPool = [PSCustomObject]@{ + owner = [PSCustomObject]@{ + displayName = 'Test User' + id = '1' + uniqueName = 'test@email.com' + } + createdBy = [PSCustomObject]@{ + displayName = 'Test User' + id = '1' + uniqueName = 'test@email.com' + } + id = 1 + size = 1 + isHosted = $true + Name = 'Hosted' + } + + $privatePool = [PSCustomObject]@{ + owner = [PSCustomObject]@{ + displayName = 'Test User' + id = '1' + uniqueName = 'test@email.com' + } + createdBy = [PSCustomObject]@{ + displayName = 'Test User' + id = '1' + uniqueName = 'test@email.com' + } + id = 1 + size = 1 + isHosted = $false + Name = 'Default' + } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + Context 'Get-VSTeamPool with no parameters' { + Mock Invoke-RestMethod { return [PSCustomObject]@{ + count = 1 + value = $privatePool + } + } + + Mock Invoke-RestMethod { return $hostedPool } -ParameterFilter { $Uri -like "*101*" } + + it 'with no parameters should return all the pools' { + ## Act + Get-VSTeamPool + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools?api-version=$(_getApiVersion DistributedTask)" + } + } + + it 'with id parameter should return all the pools' { + ## Act + Get-VSTeamPool -id 101 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/101?api-version=$(_getApiVersion DistributedTask)" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamProcess.Tests.ps1 b/unit/test/Get-VSTeamProcess.Tests.ps1 index 43f723012..674c9a539 100644 --- a/unit/test/Get-VSTeamProcess.Tests.ps1 +++ b/unit/test/Get-VSTeamProcess.Tests.ps1 @@ -1,5 +1,6 @@ Set-StrictMode -Version Latest +#region include $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") @@ -9,11 +10,15 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Classes/VSTeamProcessCache.ps1" . "$here/../../Source/Private/common.ps1" . "$here/../../Source/Public/$sut" - -Describe 'Get-VSTeamProcess' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable +#endregion + +Describe 'VSTeamProcess' { . "$PSScriptRoot\mocks\mockProcessNameDynamicParam.ps1" + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + $results = [PSCustomObject]@{ value = [PSCustomObject]@{ name = 'Test' @@ -34,84 +39,68 @@ Describe 'Get-VSTeamProcess' { type = 'Agile' } - Context 'Get-VSTeamProcess with no parameters using BearerToken' { - - Mock Invoke-RestMethod { return $results } + Mock Invoke-RestMethod { return $results } + Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { + $Uri -like "*123-5464-dee43*" + } - It 'Should return process' { + Context 'Get-VSTeamProcess' { + It 'with no parameters using BearerToken should return process' { + ## Act Get-VSTeamProcess + ## Assert # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Uri -like "*https://dev.azure.com/test/_apis/process/processes*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and $Uri -like "*`$top=100*" } } - } - - Context 'Get-VSTeamProcess with top 10' { - - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - #Write-Host $args - return $results - } - It 'Should return top 10 process' { + It 'with top 10 should return top 10 process' { + ## Act Get-VSTeamProcess -top 10 + ## Assert # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Uri -like "*https://dev.azure.com/test/_apis/process/processes*" -and $Uri -like "*`$top=10*" } } - } - - Context 'Get-VSTeamProcess with skip 1' { - - Mock Invoke-RestMethod { return $results } - It 'Should skip first process' { + It 'with skip 1 should skip first process' { + ## Act Get-VSTeamProcess -skip 1 + ## Assert # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Uri -like "*https://dev.azure.com/test/_apis/process/processes*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and $Uri -like "*`$skip=1*" -and $Uri -like "*`$top=100*" } } - } - - Context 'Get-VSTeamProcess by Name' { - #Although this returns a single VSTeamProcess instance, the REST call returns multiple results - Mock Invoke-RestMethod { return $results } - It 'Should return Process by Name' { + It 'by Name should return Process by Name' { Get-VSTeamProcess -Name Agile # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Uri -like "*https://dev.azure.com/test/_apis/process/processes*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" + $Uri -like "*api-version=$(_getApiVersion Core)*" } } - } - - Context 'Get-VSTeamProcess by Id' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should return Process by Id' { + It 'by Id should return Process by Id' { Get-VSTeamProcess -Id '123-5464-dee43' # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/process/processes*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/process/processes/123-5464-dee43*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" } } } diff --git a/unit/test/Get-VSTeamProfile.Tests.ps1 b/unit/test/Get-VSTeamProfile.Tests.ps1 new file mode 100644 index 000000000..ae3e5c8e3 --- /dev/null +++ b/unit/test/Get-VSTeamProfile.Tests.ps1 @@ -0,0 +1,129 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamProfile' { + $contents = @" + [ + { + "Name": "http://localhost:8080/tfs/defaultcollection", + "URL": "http://localhost:8080/tfs/defaultcollection", + "Pat": "", + "Type": "OnPremise", + "Version": "TFS2017" + }, + { + "Name": "http://192.168.1.3:8080/tfs/defaultcollection", + "URL": "http://192.168.1.3:8080/tfs/defaultcollection", + "Pat": "OnE2cXpseHk0YXp3dHpz", + "Type": "Pat", + "Version": "TFS2017" + }, + { + "Name": "test", + "URL": "https://dev.azure.com/test", + "Pat": "OndrejR0ZHpwbDM3bXUycGt5c3hm", + "Type": "Pat", + "Version": "VSTS" + } + ] +"@ + + Context 'Get-VSTeamProfile' { + Mock Get-Content { return '' } + Mock Test-Path { return $true } + + It 'empty profiles file should return 0 profiles' { + $actual = Get-VSTeamProfile + $actual | Should BeNullOrEmpty + } + } + + Context 'Get-VSTeamProfile invalid profiles file' { + Mock Test-Path { return $true } + Mock Write-Error { } -Verifiable + Mock Get-Content { return 'Not Valid JSON. This might happen if someone touches the file.' } + + It 'invalid profiles file should return 0 profiles' { + $actual = Get-VSTeamProfile + $actual | Should BeNullOrEmpty + Assert-VerifiableMock + } + } + + Context 'Get-VSTeamProfile no profiles' { + Mock Test-Path { return $false } + + $actual = Get-VSTeamProfile + + It 'no profiles should return 0 profiles' { + $actual | Should BeNullOrEmpty + } + } + + Context 'Get-VSTeamProfile by name' { + Mock Test-Path { return $true } + Mock Get-Content { return $contents } + + $actual = Get-VSTeamProfile test + + It 'by name should return 1 profile' { + $actual.URL | Should be 'https://dev.azure.com/test' + } + + It 'by name profile Should by Pat' { + $actual.Type | Should be 'Pat' + } + + It 'by name token Should be empty string' { + # This is testing that the Token property is added + # to existing profiles loaded from file created before + # the bearer token support was added. + $actual.Token | Should be '' + } + } + + Context 'Get-VSTeamProfile' { + Mock Test-Path { return $true } + Mock Get-Content { return $contents } + + $actual = Get-VSTeamProfile + + It 'Should return 3 profiles' { + $actual.Length | Should be 3 + } + + It '1st profile Should by OnPremise' { + $actual[0].Type | Should be 'OnPremise' + } + } + + Context 'Get-VSTeamProfile with old URL' { + Mock Test-Path { return $true } + Mock Get-Content { return '[{"Name":"test","URL":"https://test.visualstudio.com","Type":"Pat","Pat":"12345","Version":"VSTS"}]' } + + $actual = Get-VSTeamProfile + + It 'Should return new URL' { + $actual.URL | Should Be "https://dev.azure.com/test" + } + } + + Context 'Get-VSTeamProfile with old URL and multiple entries' { + Mock Test-Path { return $true } + Mock Get-Content { return '[{"Name":"test","URL":"https://test.visualstudio.com","Type":"Pat","Pat":"12345","Version":"VSTS"},{"Name":"demo","URL":"https://demo.visualstudio.com","Type":"Pat","Pat":"12345","Version":"VSTS"}]' } + + $actual = Get-VSTeamProfile -Name "test" + + It 'Should return new URL' { + $actual.URL | Should Be "https://dev.azure.com/test" + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamProject.Tests.ps1 b/unit/test/Get-VSTeamProject.Tests.ps1 new file mode 100644 index 000000000..9e8dca3a5 --- /dev/null +++ b/unit/test/Get-VSTeamProject.Tests.ps1 @@ -0,0 +1,155 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinition.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamQueue.ps1" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" +. "$here/../../Source/Public/Get-VSTeamBuildDefinition.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamProject' { + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + . "$PSScriptRoot\mocks\mockProcessNameDynamicParam.ps1" + + $results = [PSCustomObject]@{ + value = [PSCustomObject]@{ + name = 'Test' + description = '' + url = '' + id = '123-5464-dee43' + state = '' + visibility = '' + revision = 0 + _links = [PSCustomObject]@{ } + } + } + + $singleResult = [PSCustomObject]@{ + name = 'Test' + description = '' + url = '' + id = '123-5464-dee43' + state = '' + visibility = '' + revision = 0 + defaultTeam = [PSCustomObject]@{ } + _links = [PSCustomObject]@{ } + } + + Context 'Get-VSTeamProject' { + + BeforeAll { + $env:TEAM_TOKEN = '1234' + } + + AfterAll { + $env:TEAM_TOKEN = $null + } + + Mock Invoke-RestMethod { return $results } + Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -like "*TestProject*" } + + It 'with no parameters using BearerToken should return projects' { + Get-VSTeamProject + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/projects*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*`$top=100*" -and + $Uri -like "*stateFilter=WellFormed*" + } + } + + It 'with top 10 should return top 10 projects' { + Get-VSTeamProject -top 10 + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/projects*" -and + $Uri -like "*`$top=10*" -and + $Uri -like "*stateFilter=WellFormed*" + } + } + + It 'with skip 1 should skip first project' { + Get-VSTeamProject -skip 1 + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/projects*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*`$skip=1*" -and + $Uri -like "*`$top=100*" -and + $Uri -like "*stateFilter=WellFormed*" + } + } + + It 'with stateFilter All should return All projects' { + Get-VSTeamProject -stateFilter 'All' + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/projects*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*`$top=100*" -and + $Uri -like "*stateFilter=All*" + } + } + + It 'with no Capabilities by name should return the project' { + Get-VSTeamProject -Name TestProject + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/TestProject?api-version=$(_getApiVersion Core)" + } + } + + It 'with Capabilities by name should return the project with capabilities' { + Get-VSTeamProject -projectId TestProject -includeCapabilities + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/projects/TestProject*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*includeCapabilities=True*" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamPullRequest.Tests.ps1 b/unit/test/Get-VSTeamPullRequest.Tests.ps1 new file mode 100644 index 000000000..390a05ce3 --- /dev/null +++ b/unit/test/Get-VSTeamPullRequest.Tests.ps1 @@ -0,0 +1,205 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProcess.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamProcessCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamPullRequest' { + # You have to manually load the type file so the property reviewStatus + # can be tested. + Update-TypeData -AppendPath "$here/../../Source/types/Team.PullRequest.ps1xml" -ErrorAction Ignore + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Git' } + + # You have to set the version or the api-version will not be added when versions = '' + [VSTeamVersions]::Git = '5.1-preview' + + $singleResult = @{ + pullRequestId = 1 + repositoryName = "testreponame" + repository = @{ + project = @{ + name = "testproject" + } + } + reviewers = @{ + vote = 0 + } + } + + $collection = @{ + value = @($singleResult) + } + + Context 'Get-VSTeamPullRequest' { + Mock Invoke-RestMethod { return $collection } + Mock Invoke-RestMethod { + $result = $singleResult + $result.reviewers.vote = 0 + return $result + } -ParameterFilter { + $Uri -like "*101*" + } + + Mock Invoke-RestMethod { + $result = $singleResult + $result.reviewers.vote = 10 + return $result + } -ParameterFilter { + $Uri -like "*110*" + } + + Mock Invoke-RestMethod { + $result = $singleResult + $result.reviewers.vote = -10 + return $result + } -ParameterFilter { + $Uri -like "*100*" + } + + It 'with no parameters' { + $Global:PSDefaultParameterValues.Remove("*:ProjectName") + Get-VSTeamPullRequest + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/git/pullRequests?api-version=$(_getApiVersion Git)" + } + } + + It 'with default project name' { + $Global:PSDefaultParameterValues["*:ProjectName"] = 'testproject' + Get-VSTeamPullRequest -ProjectName testproject + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/testproject/_apis/git/pullRequests?api-version=$(_getApiVersion Git)" + } + } + + It 'By ProjectName' { + $Global:PSDefaultParameterValues.Remove("*:ProjectName") + Get-VSTeamPullRequest -ProjectName testproject + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/testproject/_apis/git/pullRequests?api-version=$(_getApiVersion Git)" + } + } + + It 'By ID' { + Get-VSTeamPullRequest -Id 101 + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/git/pullRequests/101?api-version=$(_getApiVersion Git)" + } + } + + It 'with All' { + Get-VSTeamPullRequest -ProjectName Test -All + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*api-version=$(_getApiVersion Git)*" -and + $Uri -like "*Test/_apis/git*" -and + $Uri -like "*status=all*" + } + } + + It 'with Status abandoned' { + Get-VSTeamPullRequest -ProjectName Test -Status abandoned + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*api-version=$(_getApiVersion Git)*" -and + $Uri -like "*Test/_apis/git*" -and + $Uri -like "*status=abandoned*" + } + } + + It 'with source branch' { + Get-VSTeamPullRequest -ProjectName Test -SourceBranchRef "refs/heads/mybranch" + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*api-version=$(_getApiVersion Git)*" -and + $Uri -like "*Test/_apis/git*" -and + $Uri -like "*searchCriteria.sourceRefName=refs/heads/mybranch*" + } + } + + It 'with target branch' { + Get-VSTeamPullRequest -ProjectName Test -TargetBranchRef "refs/heads/mybranch" + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*api-version=$(_getApiVersion Git)*" -and + $Uri -like "*Test/_apis/git*" -and + $Uri -like "*searchCriteria.targetRefName=refs/heads/mybranch*" + } + } + + It 'with repository id' { + Get-VSTeamPullRequest -ProjectName Test -RepositoryId "93BBA613-2729-4158-9217-751E952AB4AF" + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*api-version=$(_getApiVersion Git)*" -and + $Uri -like "*Test/_apis/git*" -and + $Uri -like "*repositories/93BBA613-2729-4158-9217-751E952AB4AF*" + } + } + + It 'with source repository id' { + Get-VSTeamPullRequest -ProjectName Test -SourceRepositoryId "93BBA613-2729-4158-9217-751E952AB4AF" + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*api-version=$(_getApiVersion Git)*" -and + $Uri -like "*Test/_apis/git*" -and + $Uri -like "*searchCriteria.sourceRepositoryId=93BBA613-2729-4158-9217-751E952AB4AF*" + } + } + + It 'with top and skip' { + Get-VSTeamPullRequest -ProjectName Test -SourceRepositoryId "93BBA613-2729-4158-9217-751E952AB4AF" -Top 100 -Skip 200 + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*api-version=$(_getApiVersion Git)*" -and + $Uri -like "*Test/_apis/git*" -and + $Uri -like "*searchCriteria.sourceRepositoryId=93BBA613-2729-4158-9217-751E952AB4AF*" -and + $Uri -like "*`$top=100*" -and + $Uri -like "*`$skip=200*" + } + } + + It 'with source branch in wrong format throws' { + { Get-VSTeamPullRequest -ProjectName Test -SourceBranchRef "garbage" } | should throw + } + + It 'with target branch in wrong format throws' { + { Get-VSTeamPullRequest -ProjectName Test -TargetBranchRef "garbage" } | should throw + } + + It 'No Votes should be Pending Status' { + $pr = Get-VSTeamPullRequest -Id 101 + + $pr.reviewStatus | Should be "Pending" + } + + It 'Postivite Votes should be Approved Status' { + $pr = Get-VSTeamPullRequest -Id 110 + + $pr.reviewStatus | Should be "Approved" + } + + It 'Negative Votes should be Rejected Status' { + $pr = Get-VSTeamPullRequest -Id 100 + + $pr.reviewStatus | Should be "Rejected" + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamQueue.Tests.ps1 b/unit/test/Get-VSTeamQueue.Tests.ps1 new file mode 100644 index 000000000..9304dc49a --- /dev/null +++ b/unit/test/Get-VSTeamQueue.Tests.ps1 @@ -0,0 +1,91 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamQueue' { + ## Arrange + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'DistributedTask' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Mock Invoke-RestMethod { return @{ value = @{ id = 3; name = 'Hosted'; pool = @{ } } } } + Mock Invoke-RestMethod { return @{ id = 101; name = 'Hosted'; pool = @{ } } } -ParameterFilter { + $Uri -like "*101*" + } + + Context 'Get-VSTeamQueue' { + It 'should return requested queue' { + ## Act + Get-VSTeamQueue -projectName project -queueId 101 + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/queues/101?api-version=$(_getApiVersion DistributedTask)" + } + } + + it 'with actionFilter & queueName parameter should return all the queues' { + ## Act + Get-VSTeamQueue -projectName project -actionFilter 'None' -queueName 'Hosted' + + # With PowerShell core the order of the query string is not the + # same from run to run! So instead of testing the entire string + # matches I have to search for the portions I expect but can't + # assume the order. + # The general string should look like this: + # "https://dev.azure.com/test/project/_apis/distributedtask/queues/?api-version=$(_getApiVersion DistributedTask)&actionFilter=None&queueName=Hosted" + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/project/_apis/distributedtask/queues*" -and + $Uri -like "*api-version=$(_getApiVersion DistributedTask)*" -and + $Uri -like "*actionFilter=None*" -and + $Uri -like "*queueName=Hosted*" + } + } + + it 'with no parameters should return all the queues' { + ## Act + Get-VSTeamQueue -ProjectName project + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/queues?api-version=$(_getApiVersion DistributedTask)" + } + } + + it 'with queueName parameter should return all the queues' { + ## Act + Get-VSTeamQueue -projectName project -queueName 'Hosted' + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/queues?api-version=$(_getApiVersion DistributedTask)&queueName=Hosted" + } + } + + it 'with actionFilter parameter should return all the queues' { + ## Act + Get-VSTeamQueue -projectName project -actionFilter 'None' + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/queues?api-version=$(_getApiVersion DistributedTask)&actionFilter=None" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamRelease.Tests.ps1 b/unit/test/Get-VSTeamRelease.Tests.ps1 index c39135c52..399bb746d 100644 --- a/unit/test/Get-VSTeamRelease.Tests.ps1 +++ b/unit/test/Get-VSTeamRelease.Tests.ps1 @@ -1,136 +1,100 @@ Set-StrictMode -Version Latest -# Loading System.Web avoids issues finding System.Web.HttpUtility -Add-Type -AssemblyName 'System.Web' - -InModuleScope VSTeam { - $results = [PSCustomObject]@{ - value = [PSCustomObject]@{ - environments = [PSCustomObject]@{} - _links = [PSCustomObject]@{ - self = [PSCustomObject]@{} - web = [PSCustomObject]@{} - } - } - } - - $singleResult = [PSCustomObject]@{ - environments = [PSCustomObject]@{} - variables = [PSCustomObject]@{ - BrowserToUse = [PSCustomObject]@{ - value = "phantomjs" - } - } - _links = [PSCustomObject]@{ - self = [PSCustomObject]@{} - web = [PSCustomObject]@{} - } - } - - Describe 'Releases' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - [VSTeamVersions]::Release = '1.0-unittest' +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamFeed.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamRelease' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + [VSTeamVersions]::Release = '1.0-unittest' + $results = Get-Content "$PSScriptRoot\sampleFiles\releaseResults.json" -Raw | ConvertFrom-Json + $singleResult = Get-Content "$PSScriptRoot\sampleFiles\releaseSingleReult.json" -Raw | ConvertFrom-Json + + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Release' } - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + Mock Invoke-RestMethod { return $results } + Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { + $Uri -like "*15*" + } - Context 'Get-VSTeamRelease by ID -Raw' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { - return $singleResult - } + Context 'Get-VSTeamRelease' { - It 'should return release as Raw' { - $raw = Get-VSTeamRelease -ProjectName project -Id 15 -Raw + It 'by Id -Raw should return release as Raw' { + ## Act + $raw = Get-VSTeamRelease -ProjectName project -Id 15 -Raw - $raw | Get-Member | Select-Object -First 1 -ExpandProperty TypeName | Should be 'System.Management.Automation.PSCustomObject' + ## Assert + $raw | Get-Member | Select-Object -First 1 -ExpandProperty TypeName | Should be 'System.Management.Automation.PSCustomObject' - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/15?api-version=$([VSTeamVersions]::Release)" - } + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/15?api-version=$(_getApiVersion Release)" } } - Context 'Get-VSTeamRelease by ID' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { - return $singleResult - } - - It 'should return release as Object' { - $r = Get-VSTeamRelease -ProjectName project -Id 15 + It 'by Id should return release as Object' { + ## Act + $r = Get-VSTeamRelease -ProjectName project -Id 15 - $r | Get-Member | Select-Object -First 1 -ExpandProperty TypeName | Should be 'Team.Release' + ## Assert + $r | Get-Member | Select-Object -First 1 -ExpandProperty TypeName | Should be 'Team.Release' - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/15?api-version=$([VSTeamVersions]::Release)" - } + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/15?api-version=$(_getApiVersion Release)" } } - Context 'Get-VSTeamRelease by ID -JSON' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { - return $singleResult - } - - It 'should return release as JSON' { - $r = Get-VSTeamRelease -ProjectName project -Id 15 -JSON + It 'by Id -JSON should return release as JSON' { + ## Act + $r = Get-VSTeamRelease -ProjectName project -Id 15 -JSON - $r | Get-Member | Select-Object -First 1 -ExpandProperty TypeName | Should be 'System.String' + ## Assert + $r | Get-Member | Select-Object -First 1 -ExpandProperty TypeName | Should be 'System.String' - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/15?api-version=$([VSTeamVersions]::Release)" - } + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/15?api-version=$(_getApiVersion Release)" } } - - Context 'Get-VSTeamRelease with no parameters' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { - return $results - } - It 'should return releases' { - Get-VSTeamRelease -projectName project + It 'with no parameters should return releases' { + ## Act + Get-VSTeamRelease -projectName project - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases?api-version=$([VSTeamVersions]::Release)" - } + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases?api-version=$(_getApiVersion Release)" } } - Context 'Get-VSTeamRelease with expand environments' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { - return $results - } - - It 'should return releases' { - Get-VSTeamRelease -projectName project -expand environments + It 'with expand environments should return releases' { + ## Act + Get-VSTeamRelease -projectName project -expand environments - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases?api-version=$([VSTeamVersions]::Release)&`$expand=environments" - } + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases?api-version=$(_getApiVersion Release)&`$expand=environments" } } - Context 'Get-VSTeamRelease with no parameters & no project' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { - return $results - } - - It 'should return releases' { - Get-VSTeamRelease + It 'with no parameters & no project should return releases' { + ## Act + Get-VSTeamRelease - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://vsrm.dev.azure.com/test/_apis/release/releases?api-version=$([VSTeamVersions]::Release)" - } + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://vsrm.dev.azure.com/test/_apis/release/releases?api-version=$(_getApiVersion Release)" } } } diff --git a/unit/test/Get-VSTeamReleaseDefinition.Tests.ps1 b/unit/test/Get-VSTeamReleaseDefinition.Tests.ps1 index 40bb9e760..7d23a9b04 100644 --- a/unit/test/Get-VSTeamReleaseDefinition.Tests.ps1 +++ b/unit/test/Get-VSTeamReleaseDefinition.Tests.ps1 @@ -1,5 +1,6 @@ Set-StrictMode -Version Latest +#region include $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") @@ -11,72 +12,49 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Private/common.ps1" . "$here/../../Source/Private/common.ps1" . "$here/../../Source/Public/$sut" +#endregion -# Loading System.Web avoids issues finding System.Web.HttpUtility -Add-Type -AssemblyName 'System.Web' - -$results = Get-Content "$PSScriptRoot\sampleFiles\releaseDefAzD.json" -Raw | ConvertFrom-Json - -Describe 'Get-VSTeamReleaseDefinition' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - [VSTeamVersions]::Release = '1.0-unittest' - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - +Describe 'VSTeamReleaseDefinition' { . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - Context 'no parameters' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { - return $results - } + [VSTeamVersions]::Release = '1.0-unittest' - It 'should return Release definitions' { + $results = Get-Content "$PSScriptRoot\sampleFiles\releaseDefAzD.json" -Raw | ConvertFrom-Json + + Mock Invoke-RestMethod { return $results } + Mock Invoke-RestMethod { return $results.value[0] } -ParameterFilter { $Uri -like "*15*" } + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Release' } + + Context 'Get-VSTeamReleaseDefinition' { + It 'no parameters should return Release definitions' { + ## Act Get-VSTeamReleaseDefinition -projectName project + ## Assert Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/definitions?api-version=$([VSTeamVersions]::Release)" + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/definitions?api-version=$(_getApiVersion Release)" } } - } - - Context 'expand environments' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { - return $results - } - It 'should return Release definitions' { + It 'expand environments should return Release definitions' { + ## Act Get-VSTeamReleaseDefinition -projectName project -expand environments + ## Assert Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/definitions?api-version=$([VSTeamVersions]::Release)&`$expand=environments" + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/definitions?api-version=$(_getApiVersion Release)&`$expand=environments" } } - } - - Context 'by ID' { - Mock Invoke-RestMethod { return $results.value[0] } - It 'should return Release definition' { + It 'by Id should return Release definition' { + ## Act Get-VSTeamReleaseDefinition -projectName project -id 15 + ## Assert Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/definitions/15?api-version=$([VSTeamVersions]::Release)" + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/definitions/15?api-version=$(_getApiVersion Release)" } } } - - # Make sure these test run last as the need differnt - # [VSTeamVersions]::Account values - Context 'no account' { - [VSTeamVersions]::Account = $null - - It 'should return Release definitions' { - { Get-VSTeamReleaseDefinition -projectName project } | Should Throw - } - } } \ No newline at end of file diff --git a/unit/test/Get-VSTeamResourceArea.Tests.ps1 b/unit/test/Get-VSTeamResourceArea.Tests.ps1 index edb950306..6864db3e4 100644 --- a/unit/test/Get-VSTeamResourceArea.Tests.ps1 +++ b/unit/test/Get-VSTeamResourceArea.Tests.ps1 @@ -1,5 +1,6 @@ Set-StrictMode -Version Latest +#region include $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") @@ -8,17 +9,18 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Private/common.ps1" . "$here/../../Source/Private/applyTypes.ps1" . "$here/../../Source/Public/$sut" +#endregion -Describe 'Get-VSTeamResourceArea' { +Describe 'VSTeamResourceArea' { Context 'Get-VSTeamResourceArea' { - Mock _callAPI { return @{ - value = @{ } - } - } - - $actual = Get-VSTeamResourceArea + ## Arrange + Mock _callAPI { return @{ value = @{ } } } It 'Should return resources' { + ## Act + $actual = Get-VSTeamResourceArea + + ## Assert $actual | Should Not Be $null } } diff --git a/unit/test/Get-VSTeamSecurityNamespace.Tests.ps1 b/unit/test/Get-VSTeamSecurityNamespace.Tests.ps1 new file mode 100644 index 000000000..a9ab8742b --- /dev/null +++ b/unit/test/Get-VSTeamSecurityNamespace.Tests.ps1 @@ -0,0 +1,105 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamSecurityNamespace.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe "VSTeamSecurityNamespace" { + Context "Get-VSTeamSecurityNamespace" { + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + # to avoid the + # Cannot validate argument on parameter 'ProjectName'. + # error when using test project names that do not really exist. + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context 'Services' { + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _supportsSecurityNamespace { return $true } + + $securityNamespaceListResult = Get-Content "$PSScriptRoot\sampleFiles\securityNamespaces.json" -Raw | ConvertFrom-Json + $securityNamespaceSingleResult = Get-Content "$PSScriptRoot\sampleFiles\securityNamespace.single.json" -Raw | ConvertFrom-Json + + # You have to set the version or the api-version will not be added when versions = '' + Mock _getApiVersion { return 'AzD' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + Mock Invoke-RestMethod { return $securityNamespaceListResult } + Mock Invoke-RestMethod { return $securityNamespaceSingleResult } -ParameterFilter { $Uri -like "*58450c49-b02d-465a-ab12-59ae512d6531*" } + + It 'list should return namespaces' { + Get-VSTeamSecurityNamespace + # With PowerShell core the order of the query string is not the + # same from run to run! So instead of testing the entire string + # matches I have to search for the portions I expect but can't + # assume the order. + # The general string should look like this: + # "https://vssps.dev.azure.com/test/_apis/graph/groups?api-version=$(_getApiVersion Graph)&scopeDescriptor=scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2" + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/securitynamespaces*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" + } + } + + It 'by id should return a single namespace' { + Get-VSTeamSecurityNamespace -Id 58450c49-b02d-465a-ab12-59ae512d6531 + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/securitynamespaces/58450c49-b02d-465a-ab12-59ae512d6531*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" + } + } + + It 'by name should return namespace' { + Get-VSTeamSecurityNamespace -Name "WorkItemTracking" + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/securitynamespaces*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" + } + } + + It 'by list and localOnly should return namespaces' { + Get-VSTeamSecurityNamespace -LocalOnly + # With PowerShell core the order of the query string is not the + # same from run to run! So instead of testing the entire string + # matches I have to search for the portions I expect but can't + # assume the order. + # The general string should look like this: + # "https://vssps.dev.azure.com/test/_apis/graph/groups?api-version=$(_getApiVersion Graph)&scopeDescriptor=scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2" + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://dev.azure.com/test/_apis/securitynamespaces*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*localOnly=true*" + } + } + } + + Context "Server" { + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + Mock _callAPI { throw 'Should not be called' } -Verifiable + Mock _getApiVersion { return 'TFS2017' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + It 'should throw' { + { Get-VSTeamSecurityNamespace } | Should Throw + } + + It '_callAPI should not be called' { + Assert-MockCalled _callAPI -Exactly 0 + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamServiceEndpointType.Tests.ps1 b/unit/test/Get-VSTeamServiceEndpointType.Tests.ps1 new file mode 100644 index 000000000..a68e15fbe --- /dev/null +++ b/unit/test/Get-VSTeamServiceEndpointType.Tests.ps1 @@ -0,0 +1,82 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamServiceEndpointType' { + $sampleFile = "$PSScriptRoot\sampleFiles\serviceEndpointTypeSample.json" + + Mock _getInstance { return 'https://dev.azure.com/test' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context 'Get-VSTeamServiceEndpointType' { + Mock Invoke-RestMethod { + return Get-Content $sampleFile | ConvertFrom-Json + } + + It 'Should return all service endpoints types' { + Get-VSTeamServiceEndpointType + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/serviceendpointtypes?api-version=$(_getApiVersion DistributedTask)" + } + } + } + + Context 'Get-VSTeamServiceEndpointType by Type' { + Mock Invoke-RestMethod { + return Get-Content $sampleFile | ConvertFrom-Json + } + + It 'Should return all service endpoints types' { + Get-VSTeamServiceEndpointType -Type azurerm + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/serviceendpointtypes?api-version=$(_getApiVersion DistributedTask)" -and + $Body.type -eq 'azurerm' + } + } + } + + Context 'Get-VSTeamServiceEndpointType by Type and scheme' { + Mock Invoke-RestMethod { + return Get-Content $sampleFile | ConvertFrom-Json + } + + It 'Should return all service endpoints types' { + Get-VSTeamServiceEndpointType -Type azurerm -Scheme Basic + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/serviceendpointtypes?api-version=$(_getApiVersion DistributedTask)" -and + $Body.type -eq 'azurerm' -and + $Body.scheme -eq 'Basic' + } + } + } + + Context 'Get-VSTeamServiceEndpointType by scheme' { + Mock Invoke-RestMethod { + return Get-Content $sampleFile | ConvertFrom-Json + } + + It 'Should return all service endpoints types' { + Get-VSTeamServiceEndpointType -Scheme Basic + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/serviceendpointtypes?api-version=$(_getApiVersion DistributedTask)" -and + $Body.scheme -eq 'Basic' + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamTaskGroup.Tests.ps1 b/unit/test/Get-VSTeamTaskGroup.Tests.ps1 new file mode 100644 index 000000000..1d8453071 --- /dev/null +++ b/unit/test/Get-VSTeamTaskGroup.Tests.ps1 @@ -0,0 +1,81 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +$taskGroupsJson = "$PSScriptRoot\sampleFiles\taskGroups.json" +$taskGroupJson = "$PSScriptRoot\sampleFiles\taskGroup.json" + +Describe 'VSTeamTaskGroup' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'TaskGroups' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/project*" } + + BeforeAll { + $projectName = "project" + $taskGroupJsonAsString = Get-Content $taskGroupJson -Raw + } + + Context 'Get-VSTeamTaskGroup list' { + Mock Invoke-RestMethod { + return Get-Content $taskGroupsJson | ConvertFrom-Json + } + + It 'Should return all task groups' { + Get-VSTeamTaskGroup -projectName $projectName + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups?api-version=$(_getApiVersion TaskGroups)" + } + } + } + + Context 'Get-VSTeamTaskGroup Id' { + Mock Invoke-RestMethod { + return Get-Content $taskGroupJson | ConvertFrom-Json + } + + It 'Should return one task group' { + $projectID = "d30f8b85-6b13-41a9-bb77-2e1a9c611def" + Get-VSTeamTaskGroup -projectName $projectName -id $projectID + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups/$($projectID)?api-version=$(_getApiVersion TaskGroups)" + } + } + } + + Context 'Get-VSTeamTaskGroup Name' { + Mock Invoke-RestMethod { + # Return multiple task groups, because the function filters by name after getting the list from the server. + return Get-Content $taskGroupsJson | ConvertFrom-Json + } + + It 'Should return one task group' { + $taskGroupName = "For Unit Tests 2" + $taskGroup = Get-VSTeamTaskGroup -projectName $projectName -Name $taskGroupName + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups?api-version=$(_getApiVersion TaskGroups)" + } + + # Ensure that we only have one task group, in other words, that the name filter was applied. + $taskGroup.name | Should Be $taskGroupName + } + } +} \ No newline at end of file diff --git a/unit/test/tfvc.Tests.ps1 b/unit/test/Get-VSTeamTfvcBranch.Tests.ps1 similarity index 51% rename from unit/test/tfvc.Tests.ps1 rename to unit/test/Get-VSTeamTfvcBranch.Tests.ps1 index 5e6e635b4..5594644c3 100644 --- a/unit/test/tfvc.Tests.ps1 +++ b/unit/test/Get-VSTeamTfvcBranch.Tests.ps1 @@ -1,6 +1,18 @@ Set-StrictMode -Version Latest -InModuleScope VSTeam { +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamTfvcBranch' -Tag 'unit', 'tfvc' { $singleResult = [PSCustomObject]@{ path = "$/TfvcProject/Master"; @@ -23,171 +35,9 @@ InModuleScope VSTeam { ) } - Describe 'Get-VSTeamTfvcRootBranch VSTS' -Tag 'unit', 'tfvc', 'vsts' { - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - Context 'Get-VSTeamTfvcRootBranch with no parameters and single result' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $singleResult - } -Verifiable - - $res = Get-VSTeamTfvcRootBranch - - It 'should get 1 branch' { - $res.path | Should -Be $singleResult.path - } - - It 'should call the REST endpoint with correct parameters' { - Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches?api-version=$([VSTeamVersions]::Tfvc)" - } - } - } - - Context 'Get-VSTeamTfvcRootBranch with no parameters and multiple results' { - Mock Invoke-RestMethod { return $multipleResults } -Verifiable - - $res = Get-VSTeamTfvcRootBranch - - It 'should get 2 branches' { - $res.Count | Should -Be 2 - $multipleResults.value[0] | Should -BeIn $res - $multipleResults.value[1] | Should -BeIn $res - } - - It 'should call the REST endpoint with correct parameters' { - Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches?api-version=$([VSTeamVersions]::Tfvc)" - } - } - } - - Context 'Get-VSTeamTfvcRootBranch with IncludeChildren' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable - - Get-VSTeamTfvcRootBranch -IncludeChildren - - It 'should call the REST endpoint with correct parameters' { - Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches?api-version=$([VSTeamVersions]::Tfvc)&includeChildren=True" - } - } - } - - Context 'Get-VSTeamTfvcRootBranch with IncludeDeleted' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable - - Get-VSTeamTfvcRootBranch -IncludeDeleted - - It 'should call the REST endpoint with correct parameters' { - Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches?api-version=$([VSTeamVersions]::Tfvc)&includeDeleted=True" - } - } - } - - Context 'Get-VSTeamTfvcRootBranch with all switches' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable - - Get-VSTeamTfvcRootBranch -IncludeChildren -IncludeDeleted - - It 'should call the REST endpoint with correct parameters' { - Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches?api-version=$([VSTeamVersions]::Tfvc)&includeChildren=True&includeDeleted=True" - } - } - } - } - - Describe 'Get-VSTeamTfvcRootBranch TFS' -Tag 'unit', 'tfvc', 'tfs' { - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } - Mock _useWindowsAuthenticationOnPremise { return $true } - - Context 'Get-VSTeamTfvcRootBranch with no parameters and single result' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable - - $res = Get-VSTeamTfvcRootBranch - - It 'should get 1 branch' { - $res.path | Should -Be $singleResult.path - } - - It 'should call the REST endpoint with correct parameters' { - Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches?api-version=$([VSTeamVersions]::Tfvc)" - } - } - } - - Context 'Get-VSTeamTfvcRootBranch with no parameters and multiple results' { - Mock Invoke-RestMethod { return $multipleResults } -Verifiable - - $res = Get-VSTeamTfvcRootBranch - - It 'should get 2 branches' { - $res.Count | Should -Be 2 - $multipleResults.value[0] | Should -BeIn $res - $multipleResults.value[1] | Should -BeIn $res - } - - It 'should call the REST endpoint with correct parameters' { - Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches?api-version=$([VSTeamVersions]::Tfvc)" - } - } - } - - Context 'Get-VSTeamTfvcRootBranch with IncludeChildren' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable - - Get-VSTeamTfvcRootBranch -IncludeChildren - - It 'should call the REST endpoint with correct parameters' { - Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches?api-version=$([VSTeamVersions]::Tfvc)&includeChildren=True" - } - } - } - - Context 'Get-VSTeamTfvcRootBranch with IncludeDeleted' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable - - Get-VSTeamTfvcRootBranch -IncludeDeleted - - It 'should call the REST endpoint with correct parameters' { - Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches?api-version=$([VSTeamVersions]::Tfvc)&includeDeleted=True" - } - } - } - - Context 'Get-VSTeamTfvcRootBranch with all switches' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable - - Get-VSTeamTfvcRootBranch -IncludeChildren -IncludeDeleted - - It 'should call the REST endpoint with correct parameters' { - Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches?api-version=$([VSTeamVersions]::Tfvc)&includeChildren=True&includeDeleted=True" - } - } - } - } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Tfvc' } - Describe 'Get-VSTeamTfvcBranch' -Tag 'unit', 'multi' { + Describe 'Get-VSTeamTfvcBranch' { # Mock the call to Get-Projects by the dynamic parameter for ProjectName Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" @@ -213,12 +63,12 @@ InModuleScope VSTeam { Get-VSTeamTfvcBranch -Path $/TfvcProject/Master Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "$a/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)" + $Uri -eq "$a/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)" } } } - Describe 'Get-VSTeamTfvcBranch VSTS' -Tag 'unit', 'tfvc', 'vsts' { + Describe 'Get-VSTeamTfvcBranch VSTS' { # Mock the call to Get-Projects by the dynamic parameter for ProjectName Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" @@ -233,7 +83,7 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)" + $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)" } } } @@ -245,7 +95,7 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)" + $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)" } } } @@ -257,10 +107,10 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)" + $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)" } Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Feature?api-version=$([VSTeamVersions]::Tfvc)" + $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Feature?api-version=$(_getApiVersion Tfvc)" } } } @@ -272,7 +122,7 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)&includeChildren=True" + $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)&includeChildren=True" } } } @@ -284,7 +134,7 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)&includeParent=True" + $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)&includeParent=True" } } } @@ -296,7 +146,7 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)&includeDeleted=True" + $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)&includeDeleted=True" } } } @@ -308,13 +158,13 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)&includeChildren=True&includeParent=True&includeDeleted=True" + $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)&includeChildren=True&includeParent=True&includeDeleted=True" } } } } - Describe 'Get-VSTeamTfvcBranch TFS' -Tag 'unit', 'tfvc', 'tfs' { + Describe 'Get-VSTeamTfvcBranch TFS' { # Mock the call to Get-Projects by the dynamic parameter for ProjectName Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" @@ -330,7 +180,7 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)" + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)" } } } @@ -342,7 +192,7 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)" + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)" } } } @@ -354,10 +204,10 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)" + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)" } Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Feature?api-version=$([VSTeamVersions]::Tfvc)" + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Feature?api-version=$(_getApiVersion Tfvc)" } } } @@ -369,7 +219,7 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)&includeChildren=True" + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)&includeChildren=True" } } } @@ -381,7 +231,7 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)&includeParent=True" + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)&includeParent=True" } } } @@ -393,7 +243,7 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)&includeDeleted=True" + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)&includeDeleted=True" } } } @@ -405,7 +255,7 @@ InModuleScope VSTeam { It 'should call the REST endpoint with correct parameters' { Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$([VSTeamVersions]::Tfvc)&includeChildren=True&includeParent=True&includeDeleted=True" + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches/$/TfvcProject/Master?api-version=$(_getApiVersion Tfvc)&includeChildren=True&includeParent=True&includeDeleted=True" } } } diff --git a/unit/test/Get-VSTeamTfvcRootBranch.Tests.ps1 b/unit/test/Get-VSTeamTfvcRootBranch.Tests.ps1 new file mode 100644 index 000000000..e82db21d6 --- /dev/null +++ b/unit/test/Get-VSTeamTfvcRootBranch.Tests.ps1 @@ -0,0 +1,202 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamTfvcRootBranch' -Tag 'unit', 'tfvc' { + $singleResult = [PSCustomObject]@{ + path = "$/TfvcProject/Master"; + description = 'desc'; + children = @(); + } + + $multipleResults = [PSCustomObject]@{ + value = @( + [PSCustomObject]@{ + path = '$/TfvcProject/Master'; + description = 'desc'; + children = @(); + }, + [PSCustomObject]@{ + path = '$/AnotherProject/Master'; + description = 'desc'; + children = @(); + } + ) + } + + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Tfvc' } + + Context 'Get-VSTeamTfvcRootBranch' { + Context 'Services' { + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + Context 'Get-VSTeamTfvcRootBranch with no parameters and single result' { + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + # Write-Host $args + + return $singleResult + } -Verifiable + + $res = Get-VSTeamTfvcRootBranch + + It 'should get 1 branch' { + $res.path | Should -Be $singleResult.path + } + + It 'should call the REST endpoint with correct parameters' { + Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches?api-version=$(_getApiVersion Tfvc)" + } + } + } + + Context 'Get-VSTeamTfvcRootBranch with no parameters and multiple results' { + Mock Invoke-RestMethod { return $multipleResults } -Verifiable + + $res = Get-VSTeamTfvcRootBranch + + It 'should get 2 branches' { + $res.Count | Should -Be 2 + $multipleResults.value[0] | Should -BeIn $res + $multipleResults.value[1] | Should -BeIn $res + } + + It 'should call the REST endpoint with correct parameters' { + Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches?api-version=$(_getApiVersion Tfvc)" + } + } + } + + Context 'Get-VSTeamTfvcRootBranch with IncludeChildren' { + Mock Invoke-RestMethod { return $singleResult } -Verifiable + + Get-VSTeamTfvcRootBranch -IncludeChildren + + It 'should call the REST endpoint with correct parameters' { + Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches?api-version=$(_getApiVersion Tfvc)&includeChildren=True" + } + } + } + + Context 'Get-VSTeamTfvcRootBranch with IncludeDeleted' { + Mock Invoke-RestMethod { return $singleResult } -Verifiable + + Get-VSTeamTfvcRootBranch -IncludeDeleted + + It 'should call the REST endpoint with correct parameters' { + Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches?api-version=$(_getApiVersion Tfvc)&includeDeleted=True" + } + } + } + + Context 'Get-VSTeamTfvcRootBranch with all switches' { + Mock Invoke-RestMethod { return $singleResult } -Verifiable + + Get-VSTeamTfvcRootBranch -IncludeChildren -IncludeDeleted + + It 'should call the REST endpoint with correct parameters' { + Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/tfvc/branches?api-version=$(_getApiVersion Tfvc)&includeChildren=True&includeDeleted=True" + } + } + } + } + + Context 'Server' { + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + Mock _useWindowsAuthenticationOnPremise { return $true } + + Context 'Get-VSTeamTfvcRootBranch with no parameters and single result' { + Mock Invoke-RestMethod { return $singleResult } -Verifiable + + $res = Get-VSTeamTfvcRootBranch + + It 'should get 1 branch' { + $res.path | Should -Be $singleResult.path + } + + It 'should call the REST endpoint with correct parameters' { + Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches?api-version=$(_getApiVersion Tfvc)" + } + } + } + + Context 'Get-VSTeamTfvcRootBranch with no parameters and multiple results' { + Mock Invoke-RestMethod { return $multipleResults } -Verifiable + + $res = Get-VSTeamTfvcRootBranch + + It 'should get 2 branches' { + $res.Count | Should -Be 2 + $multipleResults.value[0] | Should -BeIn $res + $multipleResults.value[1] | Should -BeIn $res + } + + It 'should call the REST endpoint with correct parameters' { + Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches?api-version=$(_getApiVersion Tfvc)" + } + } + } + + Context 'Get-VSTeamTfvcRootBranch with IncludeChildren' { + Mock Invoke-RestMethod { return $singleResult } -Verifiable + + Get-VSTeamTfvcRootBranch -IncludeChildren + + It 'should call the REST endpoint with correct parameters' { + Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches?api-version=$(_getApiVersion Tfvc)&includeChildren=True" + } + } + } + + Context 'Get-VSTeamTfvcRootBranch with IncludeDeleted' { + Mock Invoke-RestMethod { return $singleResult } -Verifiable + + Get-VSTeamTfvcRootBranch -IncludeDeleted + + It 'should call the REST endpoint with correct parameters' { + Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches?api-version=$(_getApiVersion Tfvc)&includeDeleted=True" + } + } + } + + Context 'Get-VSTeamTfvcRootBranch with all switches' { + Mock Invoke-RestMethod { return $singleResult } -Verifiable + + Get-VSTeamTfvcRootBranch -IncludeChildren -IncludeDeleted + + It 'should call the REST endpoint with correct parameters' { + Assert-MockCalled Invoke-RestMethod -Scope Context -Exactly -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/tfvc/branches?api-version=$(_getApiVersion Tfvc)&includeChildren=True&includeDeleted=True" + } + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamUser.Tests.ps1 b/unit/test/Get-VSTeamUser.Tests.ps1 new file mode 100644 index 000000000..e6bb39294 --- /dev/null +++ b/unit/test/Get-VSTeamUser.Tests.ps1 @@ -0,0 +1,119 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamUser.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamUser' { + Context "Get-VSTeamUser" { + $userListResult = Get-Content "$PSScriptRoot\sampleFiles\users.json" -Raw | ConvertFrom-Json + $userSingleResult = Get-Content "$PSScriptRoot\sampleFiles\users.single.json" -Raw | ConvertFrom-Json + + # The Graph API is not supported on TFS + Context "Server" { + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + Context 'Get-VSTeamUser' { + Mock _callAPI { throw 'Should not be called' } + Mock _getApiVersion { return 'TFS2017' } + Mock _getApiVersion { return '' } -ParameterFilter { $Service -eq 'Graph' } + + It 'Should throw' { + { Get-VSTeamUser } | Should Throw + } + } + } + + Context 'Services' { + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + # You have to set the version or the api-version will not be added when [VSTeamVersions]::Graph = '' + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Graph' } + + Context 'Get-VSTeamUser list' { + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + # Write-Host $args + + return $userListResult + } + + Get-VSTeamUser + + It 'Should return users' { + # With PowerShell core the order of the query string is not the + # same from run to run! So instead of testing the entire string + # matches I have to search for the portions I expect but can't + # assume the order. + # The general string should look like this: + # "https://vssps.dev.azure.com/test/_apis/graph/users?api-version=$(_getApiVersion Graph)" + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/users*" -and + $Uri -like "*api-version=$(_getApiVersion Graph)*" + } + } + } + + Context 'Get-VSTeamUser by subjectTypes' { + Mock Invoke-RestMethod { return $userListResult } -Verifiable + + Get-VSTeamUser -SubjectTypes vss, aad + + It 'Should return users' { + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/users*" -and + $Uri -like "*api-version=$(_getApiVersion Graph)*" -and + $Uri -like "*subjectTypes=vss,aad*" + } + } + } + + Context 'Get-VSTeamUser by descriptor' { + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + # Write-Host $args + return $userSingleResult + } + + It 'Should return the user' { + Get-VSTeamUser -UserDescriptor 'aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj' + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/users/aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj*" -and + $Uri -like "*api-version=$(_getApiVersion Graph)*" + } + } + } + + Context 'Get-VSTeamUser list throws' { + Mock Invoke-RestMethod { throw 'Error' } + + It 'Should throw' { + { Get-VSTeamUser } | Should Throw + } + } + + Context 'Get-VSTeamUser by descriptor throws' { + Mock Invoke-RestMethod { throw 'Error' } + + It 'Should throw' { + { Get-VSTeamUser -UserDescriptor } | Should Throw + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamUserEntitlement.Tests.ps1 b/unit/test/Get-VSTeamUserEntitlement.Tests.ps1 new file mode 100644 index 000000000..f78602be1 --- /dev/null +++ b/unit/test/Get-VSTeamUserEntitlement.Tests.ps1 @@ -0,0 +1,78 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe "VSTeamUserEntitlement" { + Context "Get-VSTeamUserEntitlement" { + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context "Server" { + Mock _getApiVersion { return 'TFS2017' } + Mock _getApiVersion { return '' } -ParameterFilter { $Service -eq 'MemberEntitlementManagement' } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + Context 'Get-VSTeamUserEntitlement' { + Mock _callAPI { throw 'Should not be called' } + + It 'not supported should throw' { + { Get-VSTeamUserEntitlement } | Should Throw + } + + It '_callAPI should not be called' { + Assert-MockCalled _callAPI -Exactly -Times 0 -Scope Context + } + } + } + + Context "Services" { + Mock _getApiVersion { return 'VSTS' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'MemberEntitlementManagement' } + Mock _getInstance { return 'https://dev.azure.com/test' } + + . "$PSScriptRoot\mocks\mockProjectDynamicParamMandatoryFalse.ps1" + + Context 'Get-VSTeamUserEntitlement' { + Mock Invoke-RestMethod { return [PSCustomObject]@{ members = [PSCustomObject]@{ accessLevel = [PSCustomObject]@{ } } } } + Mock Invoke-RestMethod { return [PSCustomObject]@{ accessLevel = [PSCustomObject]@{ }; email = 'fake@email.com' } } -ParameterFilter { + $Uri -like "*00000000-0000-0000-0000-000000000000*" + } + Mock Invoke-RestMethod { return [PSCustomObject]@{ members = [PSCustomObject]@{ accessLevel = [PSCustomObject]@{ }; email = 'fake@email.com' } } } -ParameterFilter { + $Uri -like "*select=Projects*" + } + + It 'no parameters should return users' { + Get-VSTeamUserEntitlement + + # Make sure it was called with the correct URI + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://vsaex.dev.azure.com/test/_apis/userentitlements?api-version=$(_getApiVersion MemberEntitlementManagement)&top=100&skip=0" + } + } + + It 'by Id should return users with projects' { + Get-VSTeamUserEntitlement -Id '00000000-0000-0000-0000-000000000000' + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://vsaex.dev.azure.com/test/_apis/userentitlements/00000000-0000-0000-0000-000000000000?api-version=$(_getApiVersion MemberEntitlementManagement)" + } + } + + It 'with select for projects should return users with projects' { + Get-VSTeamUserEntitlement -Select Projects + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Uri -eq "https://vsaex.dev.azure.com/test/_apis/userentitlements?api-version=$(_getApiVersion MemberEntitlementManagement)&top=100&skip=0&select=Projects" + } + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamVariableGroup.Tests.ps1 b/unit/test/Get-VSTeamVariableGroup.Tests.ps1 new file mode 100644 index 000000000..cccd39003 --- /dev/null +++ b/unit/test/Get-VSTeamVariableGroup.Tests.ps1 @@ -0,0 +1,90 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamVariableGroup' { + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/project*" } + + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'VariableGroups' } + + Context 'Get-VSTeamVariableGroup' { + Context 'Services' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + $sampleFileVSTS = $(Get-Content "$PSScriptRoot\sampleFiles\variableGroupSamples.json" | ConvertFrom-Json) + + Mock _getApiVersion { return 'VSTS' } + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Invoke-RestMethod { return $sampleFileVSTS } + Mock Invoke-RestMethod { return $sampleFileVSTS.value[0] } -ParameterFilter { $Uri -like "*101*" } + + It 'list should return all variable groups' { + Get-VSTeamVariableGroup -projectName project + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/variablegroups?api-version=$(_getApiVersion VariableGroups)" + } + } + + + It 'by Id should return one variable group' { + $projectID = 101 + Get-VSTeamVariableGroup -projectName project -id $projectID + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/variablegroups/$($projectID)?api-version=$(_getApiVersion VariableGroups)" + } + } + } + + Context 'Server' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + $sampleFile2017 = $(Get-Content "$PSScriptRoot\sampleFiles\variableGroupSamples2017.json" | ConvertFrom-Json) + + Mock _getApiVersion { return 'TFS2017' } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable + + Mock Invoke-RestMethod { return $sampleFile2017 } + Mock Invoke-RestMethod { return $sampleFile2017.value[0] } -ParameterFilter { $Uri -like "*101*" } + + It 'list should return all variable groups' { + Get-VSTeamVariableGroup -projectName project + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/distributedtask/variablegroups?api-version=$(_getApiVersion VariableGroups)" + } + } + + It 'by Id should return one variable group' { + $projectID = 101 + Get-VSTeamVariableGroup -projectName project -id $projectID + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/distributedtask/variablegroups/$($projectID)?api-version=$(_getApiVersion VariableGroups)" + } + } + + It 'by name should return one variable group' { + $varGroupName = "TestVariableGroup1" + Get-VSTeamVariableGroup -projectName project -Name $varGroupName + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/distributedtask/variablegroups?api-version=$(_getApiVersion VariableGroups)&groupName=$varGroupName" + } + } + } + } +} diff --git a/unit/test/Get-VSTeamWiql.Tests.ps1 b/unit/test/Get-VSTeamWiql.Tests.ps1 new file mode 100644 index 000000000..88122f939 --- /dev/null +++ b/unit/test/Get-VSTeamWiql.Tests.ps1 @@ -0,0 +1,174 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamWorkItem.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamWiql' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + $workItem = @{ + id = 47 + url = "https://dev.azure.com/test/_apis/wit/workItems/47" + } + + $column = @{ + referenceName = "System.Id" + name = "ID" + url = "https://dev.azure.com/razorspoint-test/_apis/wit/fields/System.Id" + } + + $sortColumn = @{ + field = $column + descending = $false + } + + $wiqlResult = @{ + querytype = "flat" + queryTypeResult = "worItem" + asOf = "2019-10-03T18:35:09.117Z" + columns = @($column) + sortColumns = @($sortColumn) + workItems = @($workItem, $workItem) + } + + $expandedWorkItems = @{ + count = 1 + value = @($workItem, $workItem) + } + + Context 'Get-VSTeamWiql' { + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + # Write-Host $args + + return $wiqlResult + } + + # function is mocked because it is used when switch 'Expanded' is being used. + Mock Get-VSTeamWorkItem { + # If this test fails uncomment the line below to see how the mock was called. + # Write-Host $args + + return $expandedWorkItems + } + + It 'Get work items with custom WIQL query' { + $Global:PSDefaultParameterValues.Remove("*:projectName") + $wiqlQuery = "Select [System.Id], [System.Title], [System.State] From WorkItems" + Get-VSTeamWiql -ProjectName "test" -Team "test team" -Query $wiqlQuery + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $Body -like '`{*' -and # Make sure the body is an object + $Body -like "*[System.Id]*" -and + $Body -like "*[System.Title]*" -and + $Body -like "*[System.State]*" -and + $Body -like '*`}' -and # Make sure the body is an object + $ContentType -eq 'application/json' -and + $Uri -eq "https://dev.azure.com/test/test/test team/_apis/wit/wiql?api-version=$(_getApiVersion Core)&`$top=100" + } + } + + It 'Get work items with custom WIQL query with -Top 250' { + $Global:PSDefaultParameterValues.Remove("*:projectName") + $wiqlQuery = "Select [System.Id], [System.Title], [System.State] From WorkItems" + Get-VSTeamWiql -ProjectName "test" -Team "test team" -Query $wiqlQuery -Top 250 + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $Body -like '`{*' -and # Make sure the body is an object + $Body -like "*[System.Id]*" -and + $Body -like "*[System.Title]*" -and + $Body -like "*[System.State]*" -and + $Body -like '*`}' -and # Make sure the body is an object + $ContentType -eq 'application/json' -and + $Uri -eq "https://dev.azure.com/test/test/test team/_apis/wit/wiql?api-version=$(_getApiVersion Core)&`$top=250" + } + } + + It 'Get work items with custom WIQL query with -Top 0' { + $Global:PSDefaultParameterValues.Remove("*:projectName") + $wiqlQuery = "Select [System.Id], [System.Title], [System.State] From WorkItems" + Get-VSTeamWiql -ProjectName "test" -Team "test team" -Query $wiqlQuery -Top 0 + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $Body -like '`{*' -and # Make sure the body is an object + $Body -like "*[System.Id]*" -and + $Body -like "*[System.Title]*" -and + $Body -like "*[System.State]*" -and + $Body -like '*`}' -and # Make sure the body is an object + $ContentType -eq 'application/json' -and + $Uri -eq "https://dev.azure.com/test/test/test team/_apis/wit/wiql?api-version=$(_getApiVersion Core)" + } + } + + It 'Get work items with custom WIQL query with expanded work items' { + $Global:PSDefaultParameterValues.Remove("*:projectName") + $wiqlQuery = "Select [System.Id], [System.Title], [System.State] From WorkItems" + Get-VSTeamWiql -ProjectName "test" -Team "test team" -Query $wiqlQuery -Expand + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $Body -like '`{*' -and # Make sure the body is an object + $Body -like "*[System.Id]*" -and + $Body -like "*[System.Title]*" -and + $Body -like "*[System.State]*" -and + $Body -like '*`}' -and # Make sure the body is an object + $ContentType -eq 'application/json' -and + $Uri -eq "https://dev.azure.com/test/test/test team/_apis/wit/wiql?api-version=$(_getApiVersion Core)&`$top=100" + } + } + + It 'Get work items with custom WIQL query with time precision' { + $Global:PSDefaultParameterValues.Remove("*:projectName") + $wiqlQuery = "Select [System.Id], [System.Title], [System.State] From WorkItems" + Get-VSTeamWiql -ProjectName "test" -Team "test team" -Query $wiqlQuery -TimePrecision + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Post' -and + $Body -like '`{*' -and # Make sure the body is an object + $Body -like "*[System.Id]*" -and + $Body -like "*[System.Title]*" -and + $Body -like "*[System.State]*" -and + $Body -like '*`}' -and # Make sure the body is an object + $ContentType -eq 'application/json' -and + $Uri -like "*timePrecision=True*" + $Uri -like "*`$top=100*" + $Uri -like "https://dev.azure.com/test/test/test team/_apis/wit/wiql?api-version=$(_getApiVersion Core)*" + } + } + + It 'Get work items with query ID query' { + $Global:PSDefaultParameterValues.Remove("*:projectName") + Get-VSTeamWiql -ProjectName "test" -Team "test team" -Id 1 + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/test/test team/_apis/wit/wiql/1?api-version=$(_getApiVersion Core)&`$top=100" + } + } + + It 'Get work items with query ID query with expanded work items' { + $Global:PSDefaultParameterValues.Remove("*:projectName") + Get-VSTeamWiql -ProjectName "test" -Team "test team" -Id 1 -Expand + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/test/test team/_apis/wit/wiql/1?api-version=$(_getApiVersion Core)&`$top=100" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamWorkItem.Tests.ps1 b/unit/test/Get-VSTeamWorkItem.Tests.ps1 new file mode 100644 index 000000000..b5901d1b9 --- /dev/null +++ b/unit/test/Get-VSTeamWorkItem.Tests.ps1 @@ -0,0 +1,65 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamWorkItem' { + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + $obj = @{ + id = 47 + rev = 1 + url = "https://dev.azure.com/test/_apis/wit/workItems/47" + } + + $collection = @{ + count = 1 + value = @($obj) + } + + Context 'Get-VSTeamWorkItem' { + Mock Invoke-RestMethod { return $obj } + Mock Invoke-RestMethod { return $collection } -ParameterFilter { $Uri -like "*ids=47,48*" } + + It 'by id should return work items' { + Get-VSTeamWorkItem -Id 47, 48 + + # With PowerShell core the order of the query string is not the + # same from run to run! So instead of testing the entire string + # matches I have to search for the portions I expect but can't + # assume the order. + # The general string should look like this: + # https://dev.azure.com/test/test/_apis/wit/workitems/?api-version=$(_getApiVersion Core)&ids=47,48&`$Expand=None&errorPolicy=omit + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/wit/workitems*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*ids=47,48*" -and + $Uri -like "*`$Expand=None*" -and + $Uri -like "*errorPolicy=omit*" + } + } + + It 'by Id with Default Project should return single work item' { + Get-VSTeamWorkItem -Id 47 + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/wit/workitems/47?api-version=$(_getApiVersion Core)&`$Expand=None" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Get-VSTeamWorkItemType.Tests.ps1 b/unit/test/Get-VSTeamWorkItemType.Tests.ps1 new file mode 100644 index 000000000..2c68d871b --- /dev/null +++ b/unit/test/Get-VSTeamWorkItemType.Tests.ps1 @@ -0,0 +1,82 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamWorkItemType' { + ## Arrange + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + + Context 'Get-VSTeamWorkItemType' { + $testSuite = @{ + count = 1 + value = @{ + name = "Test Suite" + referenceName = "Microsoft.VSTS.WorkItemTypes.TestSuite" + description = "Tracks test activites for a specific feature, requirement, or user story." + color = "004B50" + } + } + + $bug = @{ + name = "Bug" + referenceName = "Microsoft.VSTS.WorkItemTypes.Bug" + description = "Describes a divergence between required and actual behavior, and tracks the work done to correct the defect and verify the correction." + color = "CC293D" + } + + Mock Invoke-RestMethod { return ConvertTo-Json $testSuite } + Mock Invoke-RestMethod { return ConvertTo-Json $bug } -ParameterFilter{ $Uri -like "*bug*" } + + It 'with project only should return all work item types' { + ## Act + Get-VSTeamWorkItemType -ProjectName test + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitemtypes?api-version=$(_getApiVersion Core)" + } + } + + It 'by type with default project should return 1 work item type' { + ## Arrange + $Global:PSDefaultParameterValues["*:projectName"] = 'test' + + ## Act + Get-VSTeamWorkItemType -WorkItemType bug + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitemtypes/bug?api-version=$(_getApiVersion Core)" + } + } + + It 'by type with explicit project should return 1 work item type' { + ## Arrange + $Global:PSDefaultParameterValues.Remove("*:projectName") + + ## Act + Get-VSTeamWorkItemType -ProjectName test -WorkItemType bug + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitemtypes/bug?api-version=$(_getApiVersion Core)" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Invoke-VSTeamRequest.Tests.ps1 b/unit/test/Invoke-VSTeamRequest.Tests.ps1 index 2a20cdd32..536c1480f 100644 --- a/unit/test/Invoke-VSTeamRequest.Tests.ps1 +++ b/unit/test/Invoke-VSTeamRequest.Tests.ps1 @@ -1,77 +1,59 @@ Set-StrictMode -Version Latest +#region include $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Classes/VSTeamVersions.ps1" . "$here/../../Source/Classes/VSTeamProjectCache.ps1" . "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" . "$here/../../Source/Public/$sut" +#endregion Describe 'Invoke-VSTeamRequest' { - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { - return @() - } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Mock Write-Host - + Mock _hasProjectCacheExpired { return $false } + Context 'Invoke-VSTeamRequest Options' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - Mock Invoke-RestMethod { - # Write-Host $args - } - - Invoke-VSTeamRequest -Method Options - - It 'Should call API' { - Assert-VerifiableMock - } - } + Mock Invoke-RestMethod - Context 'Invoke-VSTeamRequest Release' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - Mock Invoke-RestMethod { - # Write-Host $args - } -Verifiable + Mock _getInstance { return 'https://dev.azure.com/test' } - Invoke-VSTeamRequest -Area release -Resource releases -Id 1 -SubDomain vsrm -Version '4.1-preview' -ProjectName testproject -JSON + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } - It 'Should call API' { - Assert-VerifiableMock + # Called to convert from ProjectName to ProjectID + Mock Get-VSTeamProject { return [PSCustomObject]@{ id = '00000000-0000-0000-0000-000000000000' } } -Verifiable + + It 'options should call API' { + Invoke-VSTeamRequest -Method Options + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq "Options" -and + $Uri -eq "https://dev.azure.com/test/_apis" + } } - } - Context 'Invoke-VSTeamRequest AdditionalHeaders' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - Mock Invoke-RestMethod { return @() } -Verifiable -ParameterFilter { - $Headers["Test"] -eq 'Test' + It 'release should call API' { + Invoke-VSTeamRequest -Area release -Resource releases -Id 1 -SubDomain vsrm -Version '4.1-preview' -ProjectName testproject -JSON + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://vsrm.dev.azure.com/test/testproject/_apis/release/releases/1?api-version=4.1-preview" + } } - Invoke-VSTeamRequest -Area release -Resource releases -Id 1 -SubDomain vsrm -Version '4.1-preview' -ProjectName testproject -JSON -AdditionalHeaders @{Test = "Test" } - - It 'Should call API' { - Assert-VerifiableMock - } - } - - Context 'Invoke-VSTeamRequest By Product ID' { - # Called to convert from ProjectName to ProjectID - Mock Get-VSTeamProject { - return [PSCustomObject]@{ - id = '00000000-0000-0000-0000-000000000000' - } } -Verifiable - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - Mock Invoke-RestMethod { return @() } -Verifiable -ParameterFilter { - $Uri -like "*https://vsrm.dev.azure.com/test/00000000-0000-0000-0000-000000000000*" + It 'AdditionalHeaders should call API' { + Invoke-VSTeamRequest -Area release -Resource releases -Id 1 -SubDomain vsrm -Version '4.1-preview' -ProjectName testproject -JSON -AdditionalHeaders @{Test = "Test" } + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Headers["Test"] -eq 'Test' -and + $Uri -eq "https://vsrm.dev.azure.com/test/testproject/_apis/release/releases/1?api-version=4.1-preview" + } } - Invoke-VSTeamRequest -ProjectName testproject -UseProjectId -Area release -Resource releases -Id 1 -SubDomain vsrm -Version '4.1-preview' + It 'by Product Id should call API with product id instead of name' { + Invoke-VSTeamRequest -ProjectName testproject -UseProjectId -Area release -Resource releases -Id 1 -SubDomain vsrm -Version '4.1-preview' - It 'Should call API' { - Assert-VerifiableMock + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://vsrm.dev.azure.com/test/00000000-0000-0000-0000-000000000000/_apis/release/releases/1?api-version=4.1-preview" + } } } } \ No newline at end of file diff --git a/unit/test/Remove-VSTeam.Tests.ps1 b/unit/test/Remove-VSTeam.Tests.ps1 new file mode 100644 index 000000000..832814a67 --- /dev/null +++ b/unit/test/Remove-VSTeam.Tests.ps1 @@ -0,0 +1,92 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamTeam.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeam.ps1" +. "$here/../../Source/Public/$sut" +#endregion + + +$results = [PSCustomObject]@{ + value = [PSCustomObject]@{ + id = '6f365a7143e492e911c341451a734401bcacadfd' + name = 'refs/heads/master' + description = 'team description' + } +} + +$singleResult = [PSCustomObject]@{ + id = '6f365a7143e492e911c341451a734401bcacadfd' + name = 'refs/heads/master' + description = 'team description' +} + +Describe "VSTeam" { + Context "Get-VSTeam" { + Context "services" { + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Context 'Remove-VSTeam' { + Mock Invoke-RestMethod { return $singleResult } + + It 'Should remove the team' { + Remove-VSTeam -ProjectName Test -TeamId "TestTeam" -Force + + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams/TestTeam?api-version=$(_getApiVersion Core)" -and + $Method -eq "Delete" + } + } + } + + Context 'Remove-VSTeam, fed through pipeline' { + Mock Get-VSTeam { return New-Object -TypeName PSObject -Prop @{projectname = "TestProject"; name = "TestTeam" } } + Mock Invoke-RestMethod { return $singleResult } + + It 'Should remove the team' { + Get-VSTeam -ProjectName TestProject -TeamId "TestTeam" | Remove-VSTeam -Force + + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/TestProject/teams/TestTeam?api-version=$(_getApiVersion Core)" -and + $Method -eq "Delete" + } + } + } + } + + + Context "Server" { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Mock _useWindowsAuthenticationOnPremise { return $true } + + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + Context 'Remove-VSTeam on TFS local Auth' { + Mock Invoke-RestMethod { return $singleResult } + + It 'Should remove the team' { + Remove-VSTeam -ProjectName Test -TeamId "TestTeam" -Force + + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams/TestTeam?api-version=$(_getApiVersion Core)" -and + $Method -eq "Delete" + } + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamAccessControlEntry.Tests.ps1 b/unit/test/Remove-VSTeamAccessControlEntry.Tests.ps1 index 2d43ca7e1..b53d67d92 100644 --- a/unit/test/Remove-VSTeamAccessControlEntry.Tests.ps1 +++ b/unit/test/Remove-VSTeamAccessControlEntry.Tests.ps1 @@ -1,8 +1,22 @@ Set-StrictMode -Version Latest -InModuleScope VSTeam { - $securityNamespace = - @" +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamSecurityNamespace.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamSecurityNamespace.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +$securityNamespace = +@" { "namespaceId": "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87", "name": "Git Repositories", @@ -102,68 +116,66 @@ InModuleScope VSTeam { } "@ | ConvertFrom-Json - Describe 'AccessControlEntry VSTS' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable +Describe 'VSTeamSecurityNamespace' { + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } - # You have to set the version or the api-version will not be Removed when - # [VSTeamVersions]::Core = '' - [VSTeamVersions]::Core = '5.1' - + # You have to set the version or the api-version will not be Removed when + # versions = '' + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } - Mock Get-VSTeamSecurityNamespace { return $securityNamespace } + Mock Get-VSTeamSecurityNamespace { return $securityNamespace } - Context 'Remove-VSTeamAccessControlEntry by SecurityNamespaceId' { - It 'Should succeed with a properly formatted descriptor if descriptor is on ACL' { - Mock _callAPI { return $true } -Verifiable - Remove-VSTeamAccessControlEntry -SecurityNamespaceId "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" -Descriptor @("vssgp.Uy0xLTktMTU1MTM3NDI0NS0yMTkxNDc4NTk1LTU1MDM1MzIxOC0yNDM3MjM2NDgzLTQyMjkyNzUyNDktMC0wLTAtOC04") -Token xyz -confirm:$false | Should be "Removal of ACE from ACL succeeded." - } - It 'Should fail with a properly formatted descriptor if descriptor is not on ACL already' { - Mock _callAPI { return $false } -Verifiable - Remove-VSTeamAccessControlEntry -SecurityNamespaceId "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" -Descriptor @("vssgp.Uy0xLTktMTU1MTM3NDI0NS0yMTkxNDc4NTk1LTU1MDM1MzIxOC0yNDM3MjM2NDgzLTQyMjkyNzUyNDktMC0wLTAtOC04") -Token xyz -confirm:$false -ErrorVariable err -ErrorAction SilentlyContinue - $err.count | should be 1 - $err[0].Exception.Message | Should Be "Removal of ACE from ACL failed. Ensure descriptor and token are correct." - $err - } - It 'Should fail with an improperly formatted descriptor' { - Remove-VSTeamAccessControlEntry -SecurityNamespaceId "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" -Descriptor @("vssgp.NotARealDescriptor") -Token xyz -confirm:$false -ErrorVariable err -ErrorAction SilentlyContinue - $err.count | should be 2 - $err[1].Exception.Message | Should Be "Could not convert base64 string to string." - } - It 'Should fail if the REST API gives a non true/false response' { - Mock _callAPI { return "Not a valid return" } -Verifiable - Remove-VSTeamAccessControlEntry -SecurityNamespaceId "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" -Descriptor @("vssgp.Uy0xLTktMTU1MTM3NDI0NS0yMTkxNDc4NTk1LTU1MDM1MzIxOC0yNDM3MjM2NDgzLTQyMjkyNzUyNDktMC0wLTAtOC04") -Token xyz -confirm:$false -ErrorVariable err -ErrorAction SilentlyContinue - $err.count | should be 1 - $err[0].Exception.Message | Should Be "Unexpected response from REST API." - } + Context 'Remove-VSTeamAccessControlEntry by SecurityNamespaceId' { + It 'Should succeed with a properly formatted descriptor if descriptor is on ACL' { + Mock _callAPI { return $true } -Verifiable + Remove-VSTeamAccessControlEntry -SecurityNamespaceId "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" -Descriptor @("vssgp.Uy0xLTktMTU1MTM3NDI0NS0yMTkxNDc4NTk1LTU1MDM1MzIxOC0yNDM3MjM2NDgzLTQyMjkyNzUyNDktMC0wLTAtOC04") -Token xyz -confirm:$false | Should be "Removal of ACE from ACL succeeded." + } + It 'Should fail with a properly formatted descriptor if descriptor is not on ACL already' { + Mock _callAPI { return $false } -Verifiable + Remove-VSTeamAccessControlEntry -SecurityNamespaceId "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" -Descriptor @("vssgp.Uy0xLTktMTU1MTM3NDI0NS0yMTkxNDc4NTk1LTU1MDM1MzIxOC0yNDM3MjM2NDgzLTQyMjkyNzUyNDktMC0wLTAtOC04") -Token xyz -confirm:$false -ErrorVariable err -ErrorAction SilentlyContinue + $err.count | should be 1 + $err[0].Exception.Message | Should Be "Removal of ACE from ACL failed. Ensure descriptor and token are correct." + $err + } + It 'Should fail with an improperly formatted descriptor' { + Remove-VSTeamAccessControlEntry -SecurityNamespaceId "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" -Descriptor @("vssgp.NotARealDescriptor") -Token xyz -confirm:$false -ErrorVariable err -ErrorAction SilentlyContinue + $err.count | should be 2 + $err[1].Exception.Message | Should Be "Could not convert base64 string to string." } + It 'Should fail if the REST API gives a non true/false response' { + Mock _callAPI { return "Not a valid return" } -Verifiable + Remove-VSTeamAccessControlEntry -SecurityNamespaceId "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" -Descriptor @("vssgp.Uy0xLTktMTU1MTM3NDI0NS0yMTkxNDc4NTk1LTU1MDM1MzIxOC0yNDM3MjM2NDgzLTQyMjkyNzUyNDktMC0wLTAtOC04") -Token xyz -confirm:$false -ErrorVariable err -ErrorAction SilentlyContinue + $err.count | should be 1 + $err[0].Exception.Message | Should Be "Unexpected response from REST API." + } + } - Context 'Remove-VSTeamAccessControlEntry by SecurityNamespace' { - It 'Should succeed with a properly formatted descriptor if descriptor is on ACL' { - $securityNamespace = Get-VSTeamSecurityNamespace -Id "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" - Mock _callAPI { return $true } -Verifiable - Remove-VSTeamAccessControlEntry -SecurityNamespace $securityNamespace -Descriptor @("vssgp.Uy0xLTktMTU1MTM3NDI0NS0yMTkxNDc4NTk1LTU1MDM1MzIxOC0yNDM3MjM2NDgzLTQyMjkyNzUyNDktMC0wLTAtOC04") -Token xyz -confirm:$false | Should be "Removal of ACE from ACL succeeded." - } - It 'Should fail with a properly formatted descriptor if descriptor is not on ACL already' { - $securityNamespace = Get-VSTeamSecurityNamespace -Id "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" - Mock _callAPI { return $false } -Verifiable - Remove-VSTeamAccessControlEntry -SecurityNamespace $securityNamespace -Descriptor @("vssgp.Uy0xLTktMTU1MTM3NDI0NS0yMTkxNDc4NTk1LTU1MDM1MzIxOC0yNDM3MjM2NDgzLTQyMjkyNzUyNDktMC0wLTAtOC04") -Token xyz -confirm:$false -ErrorVariable err -ErrorAction SilentlyContinue - $err.count | should be 1 - $err[0].Exception.Message | Should Be "Removal of ACE from ACL failed. Ensure descriptor and token are correct." - } - It 'Should fail with an improperly formatted descriptor' { - $securityNamespace = Get-VSTeamSecurityNamespace -Id "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" - Remove-VSTeamAccessControlEntry -SecurityNamespace $securityNamespace -Descriptor @("vssgp.NotARealDescriptor") -Token xyz -confirm:$false -ErrorVariable err -ErrorAction SilentlyContinue - $err.count | should be 2 - $err[1].Exception.Message | Should Be "Could not convert base64 string to string." - } - It 'Should fail if the REST API gives a non true/false response' { - Mock _callAPI { return "Not a valid return" } -Verifiable - Remove-VSTeamAccessControlEntry -SecurityNamespaceId "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" -Descriptor @("vssgp.Uy0xLTktMTU1MTM3NDI0NS0yMTkxNDc4NTk1LTU1MDM1MzIxOC0yNDM3MjM2NDgzLTQyMjkyNzUyNDktMC0wLTAtOC04") -Token xyz -confirm:$false -ErrorVariable err -ErrorAction SilentlyContinue - $err.count | should be 1 - $err[0].Exception.Message | Should Be "Unexpected response from REST API." - } + Context 'Remove-VSTeamAccessControlEntry by SecurityNamespace' { + It 'Should succeed with a properly formatted descriptor if descriptor is on ACL' { + $securityNamespace = Get-VSTeamSecurityNamespace -Id "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" + Mock _callAPI { return $true } -Verifiable + Remove-VSTeamAccessControlEntry -SecurityNamespace $securityNamespace -Descriptor @("vssgp.Uy0xLTktMTU1MTM3NDI0NS0yMTkxNDc4NTk1LTU1MDM1MzIxOC0yNDM3MjM2NDgzLTQyMjkyNzUyNDktMC0wLTAtOC04") -Token xyz -confirm:$false | Should be "Removal of ACE from ACL succeeded." + } + It 'Should fail with a properly formatted descriptor if descriptor is not on ACL already' { + $securityNamespace = Get-VSTeamSecurityNamespace -Id "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" + Mock _callAPI { return $false } -Verifiable + Remove-VSTeamAccessControlEntry -SecurityNamespace $securityNamespace -Descriptor @("vssgp.Uy0xLTktMTU1MTM3NDI0NS0yMTkxNDc4NTk1LTU1MDM1MzIxOC0yNDM3MjM2NDgzLTQyMjkyNzUyNDktMC0wLTAtOC04") -Token xyz -confirm:$false -ErrorVariable err -ErrorAction SilentlyContinue + $err.count | should be 1 + $err[0].Exception.Message | Should Be "Removal of ACE from ACL failed. Ensure descriptor and token are correct." + } + It 'Should fail with an improperly formatted descriptor' { + $securityNamespace = Get-VSTeamSecurityNamespace -Id "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" + Remove-VSTeamAccessControlEntry -SecurityNamespace $securityNamespace -Descriptor @("vssgp.NotARealDescriptor") -Token xyz -confirm:$false -ErrorVariable err -ErrorAction SilentlyContinue + $err.count | should be 2 + $err[1].Exception.Message | Should Be "Could not convert base64 string to string." + } + It 'Should fail if the REST API gives a non true/false response' { + Mock _callAPI { return "Not a valid return" } -Verifiable + Remove-VSTeamAccessControlEntry -SecurityNamespaceId "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87" -Descriptor @("vssgp.Uy0xLTktMTU1MTM3NDI0NS0yMTkxNDc4NTk1LTU1MDM1MzIxOC0yNDM3MjM2NDgzLTQyMjkyNzUyNDktMC0wLTAtOC04") -Token xyz -confirm:$false -ErrorVariable err -ErrorAction SilentlyContinue + $err.count | should be 1 + $err[0].Exception.Message | Should Be "Unexpected response from REST API." } } } \ No newline at end of file diff --git a/unit/test/Remove-VSTeamAccount.Tests.ps1 b/unit/test/Remove-VSTeamAccount.Tests.ps1 index 12f3bcbe6..8216101ac 100644 --- a/unit/test/Remove-VSTeamAccount.Tests.ps1 +++ b/unit/test/Remove-VSTeamAccount.Tests.ps1 @@ -1,5 +1,6 @@ Set-StrictMode -Version Latest +#region include $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") @@ -7,6 +8,7 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Classes/VSTeamProjectCache.ps1" . "$here/../../Source/Private/common.ps1" . "$here/../../Source/Public/$sut" +#endregion Describe 'Remove-VSTeamAccount' { # Mock the call to Get-Projects by the dynamic parameter for ProjectName diff --git a/unit/test/Remove-VSTeamAgent.Tests.ps1 b/unit/test/Remove-VSTeamAgent.Tests.ps1 new file mode 100644 index 000000000..c6dd78c32 --- /dev/null +++ b/unit/test/Remove-VSTeamAgent.Tests.ps1 @@ -0,0 +1,40 @@ +Set-StrictMode -Version Latest + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" + +Describe 'Remove-VSTeamAgent' { + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + [VSTeamVersions]::DistributedTask = '1.0-unitTest' + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + Context 'Remove-VSTeamAgent by ID' { + Mock Invoke-RestMethod + + It 'should remove the agent with passed in Id' { + Remove-VSTeamAgent -Pool 36 -Id 950 -Force + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/36/agents/950?api-version=$(_getApiVersion DistributedTask)" + } + } + } + + Context 'Remove-VSTeamAgent throws' { + Mock Invoke-RestMethod { throw 'boom' } + + It 'should remove the agent with passed in Id' { + { Remove-VSTeamAgent -Pool 36 -Id 950 -Force } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamBuild.Tests.ps1 b/unit/test/Remove-VSTeamBuild.Tests.ps1 new file mode 100644 index 000000000..98fa15e23 --- /dev/null +++ b/unit/test/Remove-VSTeamBuild.Tests.ps1 @@ -0,0 +1,74 @@ +Set-StrictMode -Version Latest + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" + +Describe 'Remove-VSTeamBuild' { + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + Context 'Service' { + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + # Arrange + Mock Invoke-RestMethod + + # Act + Remove-VSTeamBuild -projectName project -id 2 -Force + + It 'should delete build' { + + # Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/2?api-version=$(_getApiVersion Build)" + } + } + } + + Context 'Server local Auth' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Mock _useWindowsAuthenticationOnPremise { return $true } + + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable + Mock Invoke-RestMethod + + It 'should delete build' { + Remove-VSTeamBuild -projectName project -id 2 -Force + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/2?api-version=$(_getApiVersion Build)" + } + } + } + + Context 'Server local Auth handles exception' { + + # Arrange + Mock _handleException -Verifiable + Mock Invoke-RestMethod { throw 'Testing error handling.' } + + # Act + Remove-VSTeamBuild -ProjectName project -id 2 -Force + + It 'should add tags to Build' { + + # Assert + Assert-VerifiableMock + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamBuildDefinition.Tests.ps1 b/unit/test/Remove-VSTeamBuildDefinition.Tests.ps1 new file mode 100644 index 000000000..ae125ea67 --- /dev/null +++ b/unit/test/Remove-VSTeamBuildDefinition.Tests.ps1 @@ -0,0 +1,49 @@ +Set-StrictMode -Version Latest + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" + +$resultsVSTS = Get-Content "$PSScriptRoot\sampleFiles\buildDefvsts.json" -Raw | ConvertFrom-Json + +Describe 'Remove-VSTeamBuildDefinition' { + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Context 'Succeeds' { + Mock Invoke-RestMethod { return $resultsVSTS } + + It 'should delete build definition' { + Remove-VSTeamBuildDefinition -projectName project -id 2 -Force + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions/2?api-version=$(_getApiVersion Build)" + } + } + } + + Context 'Succeeds on TFS local Auth' { + Mock Invoke-RestMethod { return $resultsVSTS } + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable + + Remove-VSTeamBuildDefinition -projectName project -id 2 -Force + + It 'should delete build definition' { + Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/definitions/2?api-version=$(_getApiVersion Build)" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamBuildTag.Tests.ps1 b/unit/test/Remove-VSTeamBuildTag.Tests.ps1 new file mode 100644 index 000000000..54bf019b2 --- /dev/null +++ b/unit/test/Remove-VSTeamBuildTag.Tests.ps1 @@ -0,0 +1,71 @@ +Set-StrictMode -Version Latest + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" + +Describe 'Remove-VSTeamBuildTag' { + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + Context 'Remove-VSTeamBuildTag' { + Mock Invoke-RestMethod { + return @{ value = $null } + } + [string[]] $inputTags = "Test1", "Test2", "Test3" + + It 'should add tags to Build' { + Remove-VSTeamBuildTag -ProjectName project -id 2 -Tags $inputTags + + foreach ($inputTag in $inputTags) { + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/2/tags?api-version=$(_getApiVersion Build)" + "&tag=$inputTag" + } + } + } + } +} + +Describe 'Remove-VSTeamBuildTag' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Mock _useWindowsAuthenticationOnPremise { return $true } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable + + Context 'Remove-VSTeamBuildTag' { + Mock Invoke-RestMethod { + return @{ value = $null } + } + [string[]] $inputTags = "Test1", "Test2", "Test3" + + It 'should add tags to Build' { + Remove-VSTeamBuildTag -ProjectName project -id 2 -Tags $inputTags + + foreach ($inputTag in $inputTags) { + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/2/tags?api-version=$(_getApiVersion Build)" + "&tag=$inputTag" + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamExtension.Tests.ps1 b/unit/test/Remove-VSTeamExtension.Tests.ps1 new file mode 100644 index 000000000..a8c5ebd4c --- /dev/null +++ b/unit/test/Remove-VSTeamExtension.Tests.ps1 @@ -0,0 +1,42 @@ +Set-StrictMode -Version Latest + +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamInstallState.ps1" +. "$here/../../Source/Classes/VSTeamExtension.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" + +$singleResult = Get-Content "$PSScriptRoot\sampleFiles\singleExtensionResult.json" -Raw | ConvertFrom-Json + +Describe 'VSTeamExtension' { + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + Context 'Remove-VSTeamExtension' { + BeforeAll { + $env:Team_TOKEN = '1234' + } + + AfterAll { + $env:TEAM_TOKEN = $null + } + + Mock _callAPI { return $singleResult } + + It 'Should remove an extension' { + Remove-VSTeamExtension -PublisherId 'test' -ExtensionId 'test' -Force + + Assert-MockCalled _callAPI -Exactly 1 -Scope It -ParameterFilter { + $Method -eq 'Delete' -and + $subDomain -eq 'extmgmt' -and + $version -eq [VSTeamVersions]::ExtensionsManagement + $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensionsbyname/test/test*" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamFeed.Tests.ps1 b/unit/test/Remove-VSTeamFeed.Tests.ps1 new file mode 100644 index 000000000..f0f3cd67b --- /dev/null +++ b/unit/test/Remove-VSTeamFeed.Tests.ps1 @@ -0,0 +1,33 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'Feeds' { + Mock _supportsFeeds { return $true } + Mock _getApiVersion { return '1.0-unitTests' } + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Invoke-RestMethod + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context 'Remove-VSTeamFeed' { + It 'should delete feed' { + Remove-VSTeamFeed -id '00000000-0000-0000-0000-000000000000' -Force + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "https://feeds.dev.azure.com/test/_apis/packaging/feeds/00000000-0000-0000-0000-000000000000?api-version=$(_getApiVersion Packaging)" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamGitRepository.Tests.ps1 b/unit/test/Remove-VSTeamGitRepository.Tests.ps1 new file mode 100644 index 000000000..4ab1a06a8 --- /dev/null +++ b/unit/test/Remove-VSTeamGitRepository.Tests.ps1 @@ -0,0 +1,122 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinition.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamQueue.ps1" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" +. "$here/../../Source/Public/Get-VSTeamBuildDefinition.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +$singleResult = [PSCustomObject]@{ + id = '' + url = '' + sshUrl = '' + remoteUrl = '' + defaultBranch = '' + size = 0 + name = '' + project = [PSCustomObject]@{ + name = 'Project' + id = 1 + description = '' + url = '' + state = '' + revision = '' + visibility = '' + } +} + +Describe "VSTeamGitRepository" { + ## Arrange + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Mock Invoke-RestMethod { + # Write-Host "Single $Uri" + return $singleResult } -ParameterFilter { + $Uri -like "*00000000-0000-0000-0000-000000000000*" -or + $Uri -like "*testRepo*" + } + Mock Invoke-RestMethod { + # Write-Host "boom $Uri" + throw [System.Net.WebException] } -ParameterFilter { + $Uri -like "*00000000-0000-0000-0000-000000000101*" -or + $Uri -like "*boom*" + } + + Context 'Remove-VSTeamGitRepository' { + Context 'Services' { + ## Arrange + Mock _getInstance { return 'https://dev.azure.com/test' } + + It 'by id should remove Git repo' { + ## Act + Remove-VSTeamGitRepository -id 00000000-0000-0000-0000-000000000000 -Force + + ## Assert + Assert-MockCalled Invoke-RestMethod -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "https://dev.azure.com/test/_apis/git/repositories/00000000-0000-0000-0000-000000000000?api-version=$(_getApiVersion Git)" + } + } + + It 'by Id should throw' { + { Remove-VSTeamGitRepository -id 00000000-0000-0000-0000-000000000101 -Force } | Should Throw + } + } + + Context 'Server' { + ## Arrange + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + It 'by id should remove Git repo' { + ## Act + Remove-VSTeamGitRepository -id 00000000-0000-0000-0000-000000000000 -Force + + ## Assert + Assert-MockCalled Invoke-RestMethod -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/git/repositories/00000000-0000-0000-0000-000000000000?api-version=$(_getApiVersion Git)" + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamMembership.Tests.ps1 b/unit/test/Remove-VSTeamMembership.Tests.ps1 new file mode 100644 index 000000000..c79bd38c7 --- /dev/null +++ b/unit/test/Remove-VSTeamMembership.Tests.ps1 @@ -0,0 +1,43 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/callMembershipAPI.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamMembership' { + ## Arrange + # You have to set the version or the api-version will not be added when [VSTeamVersions]::Graph = '' + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Graph' } + Mock _supportsGraph + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Invoke-RestMethod + + $UserDescriptor = 'aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj' + $GroupDescriptor = 'vssgp.Uy0xLTktMTU1MTM3NDI0NS04NTYwMDk3MjYtNDE5MzQ0MjExNy0yMzkwNzU2MTEwLTI3NDAxNjE4MjEtMC0wLTAtMC0x' + + Context 'Remove-VSTeamMembership' { + It 'Should remove a membership' { + ## Act + $null = Remove-VSTeamMembership -MemberDescriptor $UserDescriptor -ContainerDescriptor $GroupDescriptor -Force + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq "Delete" -and + $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/memberships/$UserDescriptor/$GroupDescriptor*" -and + $Uri -like "*api-version=$(_getApiVersion Graph)*" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamPolicy.Tests.ps1 b/unit/test/Remove-VSTeamPolicy.Tests.ps1 new file mode 100644 index 000000000..92b55c340 --- /dev/null +++ b/unit/test/Remove-VSTeamPolicy.Tests.ps1 @@ -0,0 +1,42 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamPolicy' { + ## Arrange + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Invoke-RestMethod + Mock Invoke-RestMethod { throw 'Error' } -ParameterFilter { $Uri -like "*boom*" } + + Context 'Remove-VSTeamPolicy' { + + It 'by id should delete the policy' { + ## Act + Remove-VSTeamPolicy -ProjectName Demo -id 4 -Force + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/configurations/4?api-version=$(_getApiVersion Git)" + } + } + + It 'Should throw' { + ## Act / Assert + { Remove-VSTeamPolicy -ProjectName boom -id 4 -Force } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamProfile.Tests.ps1 b/unit/test/Remove-VSTeamProfile.Tests.ps1 new file mode 100644 index 000000000..c158eaaf9 --- /dev/null +++ b/unit/test/Remove-VSTeamProfile.Tests.ps1 @@ -0,0 +1,41 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamProfile.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamProfile' { + ## Arrange + $expectedPath = "$HOME/vsteam_profiles.json" + + Mock Set-Content { } + Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } + + Context 'Remove-VSTeamProfile' { + It 'should save remaining profiles to disk' { + ## Act + Remove-VSTeamProfile test + + ## Assert + Assert-MockCalled Set-Content -Exactly -Times 1 -Scope It -ParameterFilter { + $Path -eq $expectedPath -and ([string]$Value -eq '') + } + } + + It 'entry does not exist should save original profiles to disk' { + ## Act + Remove-VSTeamProfile demos + + ## Assert + Assert-MockCalled Set-Content -Exactly -Times 1 -Scope It -ParameterFilter { + $Path -eq $expectedPath -and $Value -like "*https://dev.azure.com/test*" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamProject.Tests.ps1 b/unit/test/Remove-VSTeamProject.Tests.ps1 new file mode 100644 index 000000000..598127790 --- /dev/null +++ b/unit/test/Remove-VSTeamProject.Tests.ps1 @@ -0,0 +1,76 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinition.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamQueue.ps1" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" +. "$here/../../Source/Public/Get-VSTeamBuildDefinition.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamProject' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + . "$PSScriptRoot\mocks\mockProcessNameDynamicParam.ps1" + + $singleResult = [PSCustomObject]@{ + name = 'Test' + description = '' + url = '' + id = '123-5464-dee43' + state = '' + visibility = '' + revision = 0 + defaultTeam = [PSCustomObject]@{ } + _links = [PSCustomObject]@{ } + } + + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Write-Progress + Mock _trackProjectProgress + Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$(_getApiVersion Core)" } + Mock Invoke-RestMethod { return @{status = 'inProgress'; url = 'https://someplace.com' } } -ParameterFilter { $Method -eq 'Delete' -and $Uri -eq "https://dev.azure.com/test/_apis/projects/123-5464-dee43?api-version=$(_getApiVersion Core)" } + + Context 'Remove-VSTeamProject with Force' { + It 'Should not call Invoke-RestMethod' { + ## Act + Remove-VSTeamProject -ProjectName Test -Force + + ## Assert + Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$(_getApiVersion Core)" } + Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Method -eq 'Delete' -and $Uri -eq "https://dev.azure.com/test/_apis/projects/123-5464-dee43?api-version=$(_getApiVersion Core)" } + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamRelease.Tests.ps1 b/unit/test/Remove-VSTeamRelease.Tests.ps1 new file mode 100644 index 000000000..e5db48470 --- /dev/null +++ b/unit/test/Remove-VSTeamRelease.Tests.ps1 @@ -0,0 +1,51 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamFeed.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamBuild.ps1" +. "$here/../../Source/Public/Get-VSTeamReleaseDefinition.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamRelease' { + ## Arrange + [VSTeamVersions]::Release = '1.0-unittest' + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context 'Remove-VSTeamRelease' { + Mock Invoke-RestMethod + Mock Invoke-RestMethod { throw 'error' } -ParameterFilter { $Uri -like "*101*" } + + It 'by Id should remove release' { + ## Act + Remove-VSTeamRelease -ProjectName project -Id 15 -Force + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/15?api-version=$(_getApiVersion Release)" + } + } + + It 'by Id should throw' { + ## Act / Assert + { Remove-VSTeamRelease -ProjectName project -Id 101 -Force } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamReleaseDefinition.Tests.ps1 b/unit/test/Remove-VSTeamReleaseDefinition.Tests.ps1 new file mode 100644 index 000000000..88256164f --- /dev/null +++ b/unit/test/Remove-VSTeamReleaseDefinition.Tests.ps1 @@ -0,0 +1,72 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamReleaseDefinition' { + ## Arrange + [VSTeamVersions]::Release = '1.0-unittest' + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + $results = [PSCustomObject]@{ + value = [PSCustomObject]@{ + queue = [PSCustomObject]@{ name = 'Default' } + _links = [PSCustomObject]@{ + self = [PSCustomObject]@{ } + web = [PSCustomObject]@{ } + } + retentionPolicy = [PSCustomObject]@{ } + lastRelease = [PSCustomObject]@{ } + artifacts = [PSCustomObject]@{ } + modifiedBy = [PSCustomObject]@{ name = 'project' } + createdBy = [PSCustomObject]@{ name = 'test' } + } + } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context 'Remove-VSTeamReleaseDefinition' { + Mock Invoke-RestMethod { return $results } + + Context 'Services' { + Mock _getInstance { return 'https://dev.azure.com/test' } + + It 'should delete release definition' { + ## Act + Remove-VSTeamReleaseDefinition -projectName project -id 2 -Force + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/definitions/2?api-version=$(_getApiVersion Release)" + } + } + } + + Context 'Server' { + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + It 'local Auth should delete release definition' { + ## Act + Remove-VSTeamReleaseDefinition -projectName project -id 2 -Force + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/release/definitions/2?api-version=$(_getApiVersion Release)" + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamTaskGroup.Tests.ps1 b/unit/test/Remove-VSTeamTaskGroup.Tests.ps1 new file mode 100644 index 000000000..99a28295b --- /dev/null +++ b/unit/test/Remove-VSTeamTaskGroup.Tests.ps1 @@ -0,0 +1,48 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +$taskGroupsJson = "$PSScriptRoot\sampleFiles\taskGroups.json" +$taskGroupJson = "$PSScriptRoot\sampleFiles\taskGroup.json" + +Describe 'VSTeamTaskGroup' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'TaskGroups' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/project*" } + + BeforeAll { + $projectName = "project" + $taskGroupJsonAsString = Get-Content $taskGroupJson -Raw + } + + Context 'Remove-VSTeamTaskGroup' { + Mock Invoke-RestMethod + + It 'should delete Task group' { + $projectID = "d30f8b85-6b13-41a9-bb77-2e1a9c611def" + + Remove-VSTeamTaskGroup -projectName $projectName -Id $projectID -Force + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups/$($projectID)?api-version=$(_getApiVersion TaskGroups)" -and + $Method -eq 'Delete' + } + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamUserEntitlement.Tests.ps1 b/unit/test/Remove-VSTeamUserEntitlement.Tests.ps1 new file mode 100644 index 000000000..f4180b88a --- /dev/null +++ b/unit/test/Remove-VSTeamUserEntitlement.Tests.ps1 @@ -0,0 +1,76 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamUserEntitlement.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe "VSTeamUserEntitlement" { + Context "Get-VSTeamUserEntitlement" { + Mock _getApiVersion { return 'VSTS' } + # This will cause the call the _getProject to be skipped + Mock _hasProjectCacheExpired { return $false } + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'MemberEntitlementManagement' } + + Context 'Remove-VSTeamUserEntitlement' { + # Get-VSTeamUserEntitlement by Id + Mock _callAPI { + return [PSCustomObject]@{ + accessLevel = [PSCustomObject]@{ } + email = 'test@user.com' + userName = 'Test User' + id = '00000000-0000-0000-0000-000000000000' + } + } -ParameterFilter { $Id -eq '00000000-0000-0000-0000-000000000000' } + + # Get-VSTeamUserEntitlement by email + Mock _callAPI { + return [PSCustomObject]@{ + members = [PSCustomObject]@{ + accessLevel = [PSCustomObject]@{ } + email = 'test@user.com' + userName = 'Test User' + id = '00000000-0000-0000-0000-000000000000' + } + } + } + + # Remove Call + Mock _callAPI -ParameterFilter { $Method -eq 'Delete' } + + It 'by Id should remove user' { + Remove-VSTeamUserEntitlement -UserId '00000000-0000-0000-0000-000000000000' -Force + Assert-MockCalled _callAPI -Exactly -Times 1 -Scope It -ParameterFilter { + $subDomain -eq 'vsaex' -and + $id -eq '00000000-0000-0000-0000-000000000000' -and + $resource -eq 'userentitlements' -and + $method -eq 'Delete' -and + $version -eq $(_getApiVersion MemberEntitlementManagement) + } + } + + It 'bye email should remove user' { + Remove-VSTeamUserEntitlement -Email 'test@user.com' -Force + Assert-MockCalled _callAPI -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Delete' -and + $subDomain -eq 'vsaex' -and + $id -eq '00000000-0000-0000-0000-000000000000' -and + $resource -eq 'userentitlements' -and + $version -eq $(_getApiVersion MemberEntitlementManagement) + } + } + + It 'by invalid email should throw' { + { Remove-VSTeamUserEntitlement -Email 'not@found.com' -Force } | Should Throw + } + } + } +} \ No newline at end of file diff --git a/unit/test/Remove-VSTeamVariableGroup.Tests.ps1 b/unit/test/Remove-VSTeamVariableGroup.Tests.ps1 new file mode 100644 index 000000000..fab421193 --- /dev/null +++ b/unit/test/Remove-VSTeamVariableGroup.Tests.ps1 @@ -0,0 +1,68 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamVariableGroup' { + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/project*" } + + Context 'Remove-VSTeamVariableGroup' { + Context 'Services' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + [VSTeamVersions]::VariableGroups = '5.0-preview.1' + + Mock _getInstance { return 'https://dev.azure.com/test' } + + BeforeAll { + Set-VSTeamAPIVersion -Target VSTS + } + + Mock Invoke-RestMethod + + It 'should delete variable group' { + $projectID = 1 + Remove-VSTeamVariableGroup -projectName project -Id $projectID -Force + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/variablegroups/$($projectID)?api-version=$(_getApiVersion VariableGroups)" -and + $Method -eq 'Delete' + } + } + } + + Context 'Server' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + [VSTeamVersions]::VariableGroups = '3.2-preview.1' + + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable + + BeforeAll { + Set-VSTeamAPIVersion -Target TFS2017 + } + + Mock Invoke-RestMethod + + It 'should delete variable group' { + $projectID = 1 + Remove-VSTeamVariableGroup -projectName project -id $projectID -Force + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/distributedtask/variablegroups/$($projectID)?api-version=$(_getApiVersion VariableGroups)" -and + $Method -eq 'Delete' + } + } + } + } +} diff --git a/unit/test/Remove-VSTeamWorkItem.Tests.ps1 b/unit/test/Remove-VSTeamWorkItem.Tests.ps1 index 7e8635546..76f32e73e 100644 --- a/unit/test/Remove-VSTeamWorkItem.Tests.ps1 +++ b/unit/test/Remove-VSTeamWorkItem.Tests.ps1 @@ -1,92 +1,100 @@ Set-StrictMode -Version Latest -InModuleScope VSTeam { +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion - Describe 'workitems' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable +Describe 'VSTeamWorkItem' { + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - $obj = @{ - id = 47 - rev = 1 - url = "https://dev.azure.com/test/_apis/wit/workItems/47" - } + $obj = @{ + id = 47 + rev = 1 + url = "https://dev.azure.com/test/_apis/wit/workItems/47" + } - $objDeleted = @{ - id = 47 - name = "Test Work Item 47" - deletedBy = "Theobald Test " - deletedDate = "10/19/2019 9:08:48 PM" - code = 200 - resource = $obj - } + $objDeleted = @{ + id = 47 + name = "Test Work Item 47" + deletedBy = "Theobald Test " + deletedDate = "10/19/2019 9:08:48 PM" + code = 200 + resource = $obj + } - $collectionDeleted = @( - $objDeleted - ) + $collectionDeleted = @( + $objDeleted + ) - Context 'Remove-WorkItem' { + Context 'Remove-WorkItem' { - It 'Should delete single work item' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - #Write-Host $args + It 'Should delete single work item' { + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + #Write-Host $args - return $collectionDeleted - } + return $collectionDeleted + } - Remove-VSTeamWorkItem -Id 47 -Force + Remove-VSTeamWorkItem -Id 47 -Force - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/wit/workitems*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*workitems/47*" - } + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/wit/workitems*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + $Uri -like "*workitems/47*" } + } - It 'Should throw single work item with id equals $null' { - { Remove-VSTeamWorkItem -Id $null } | Should -Throw - } + It 'Should throw single work item with id equals $null' { + { Remove-VSTeamWorkItem -Id $null } | Should -Throw + } - It 'Should delete multipe work items' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - #Write-Host $args + It 'Should delete multipe work items' { + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + #Write-Host $args - return $collectionDeleted - } + return $collectionDeleted + } - Remove-VSTeamWorkItem -Id 47, 48 -Force + Remove-VSTeamWorkItem -Id 47, 48 -Force - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 2 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/wit/workitems*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - ($Uri -like "*workitems/47*" -or $Uri -like "*workitems/48*") - } + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 2 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/wit/workitems*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + ($Uri -like "*workitems/47*" -or $Uri -like "*workitems/48*") } + } - It 'Single Work Item Should be deleted permanently' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - #Write-Host $args + It 'Single Work Item Should be deleted permanently' { + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + #Write-Host $args - return $collectionDeleted - } + return $collectionDeleted + } - Remove-VSTeamWorkItem -Id 47, 48 -Destroy -Force + Remove-VSTeamWorkItem -Id 47, 48 -Destroy -Force - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 2 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/wit/workitems*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - ($Uri -like "*workitems/47*" -or $Uri -like "*workitems/48*") -and - $Uri -like "*destroy=True*" - } + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 2 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/_apis/wit/workitems*" -and + $Uri -like "*api-version=$(_getApiVersion Core)*" -and + ($Uri -like "*workitems/47*" -or $Uri -like "*workitems/48*") -and + $Uri -like "*destroy=True*" } } } diff --git a/unit/test/Set-VSTeamAPIVersion.Tests.ps1 b/unit/test/Set-VSTeamAPIVersion.Tests.ps1 index afce47c1b..c5293cce2 100644 --- a/unit/test/Set-VSTeamAPIVersion.Tests.ps1 +++ b/unit/test/Set-VSTeamAPIVersion.Tests.ps1 @@ -1,5 +1,6 @@ Set-StrictMode -Version Latest +#region include $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") @@ -7,14 +8,10 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Classes/VSTeamProjectCache.ps1" . "$here/../../Source/Private/common.ps1" . "$here/../../Source/Public/$sut" +#endregion -Describe 'Set-VSTeamAPIVersion' { - +Describe 'VSTeamAPIVersion' { Context 'Set-VSTeamAPIVersion' { - BeforeEach { - [VSTeamVersions]::Version = '' - } - It 'Should default to TFS2017' { Set-VSTeamAPIVersion [VSTeamVersions]::Version | Should Be 'TFS2017' diff --git a/unit/test/Set-VSTeamAccount.Tests.ps1 b/unit/test/Set-VSTeamAccount.Tests.ps1 index 6f7ac6bd5..4d47cc3c9 100644 --- a/unit/test/Set-VSTeamAccount.Tests.ps1 +++ b/unit/test/Set-VSTeamAccount.Tests.ps1 @@ -1,8 +1,22 @@ Set-StrictMode -Version Latest -InModuleScope VSTeam { - Describe 'Set-VSTeamAccount' { - $contents = @" +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Clear-VSTeamDefaultProject.ps1" +. "$here/../../Source/Public/Get-VSTeamProfile.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamAccount' { + $contents = @" [ { "Name": "http://localhost:8080/tfs/defaultcollection", @@ -31,286 +45,285 @@ InModuleScope VSTeam { ] "@ - AfterAll { - Remove-VSTeamAccount - } + AfterAll { + Remove-VSTeamAccount + } - Context 'You cannot use -UseWindowsAuthentication with Azd' { - # This is only supported on a Windows machine. So we have - # to Mock the call to _isOnWindows so you can develop on a - # Mac or Linux machine. - Mock _isOnWindows { return $true } + Context 'You cannot use -UseWindowsAuthentication with Azd' { + # This is only supported on a Windows machine. So we have + # to Mock the call to _isOnWindows so you can develop on a + # Mac or Linux machine. + Mock _isOnWindows { return $true } - # Have to Mock this because you can't call - # [Security.Principal.WindowsIdentity]::GetCurrent() - # on Mac and Linux - Mock _testAdministrator { return $false } + # Have to Mock this because you can't call + # [Security.Principal.WindowsIdentity]::GetCurrent() + # on Mac and Linux + Mock _testAdministrator { return $false } - Mock Write-Error { } -Verifiable + Mock Write-Error { } -Verifiable - It 'Should return error' { - Set-VSTeamAccount -Account test -UseWindowsAuthentication - Assert-VerifiableMock - } + It 'Should return error' { + Set-VSTeamAccount -Account test -UseWindowsAuthentication + Assert-VerifiableMock } + } - Context 'Set-VSTeamAccount invalid profile' { - Mock _isOnWindows { return $false } - Mock Write-Error -Verifiable - Mock Get-VSTeamProfile { return "[]" | ConvertFrom-Json | ForEach-Object { $_ } } + Context 'Set-VSTeamAccount invalid profile' { + Mock _isOnWindows { return $false } + Mock Write-Error -Verifiable + Mock Get-VSTeamProfile { return "[]" | ConvertFrom-Json | ForEach-Object { $_ } } - Set-VSTeamAccount -Profile notFound + Set-VSTeamAccount -Profile notFound - It 'should write error' { - Assert-VerifiableMock - } + It 'should write error' { + Assert-VerifiableMock } + } - Context 'Set-VSTeamAccount profile' { - Mock _isOnWindows { return $false } - Mock _setEnvironmentVariables - Mock Set-VSTeamAPIVersion - Mock Get-VSTeamProfile { return $contents | ConvertFrom-Json | ForEach-Object { $_ } } + Context 'Set-VSTeamAccount profile' { + Mock _isOnWindows { return $false } + Mock _setEnvironmentVariables + Mock Set-VSTeamAPIVersion + Mock Get-VSTeamProfile { return $contents | ConvertFrom-Json | ForEach-Object { $_ } } - It 'should set env at process level' { - Set-VSTeamAccount -Profile mydemos + It 'should set env at process level' { + Set-VSTeamAccount -Profile mydemos - Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { - $Target -eq 'VSTS' - } + Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { + $Target -eq 'VSTS' + } - # Make sure set env vars was called with the correct parameters - Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { - $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' - } + # Make sure set env vars was called with the correct parameters + Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { + $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' } } + } - Context 'Set-VSTeamAccount profile and drive' { - Mock _isOnWindows { return $false } - Mock _setEnvironmentVariables - Mock Set-VSTeamAPIVersion - Mock Write-Output -Verifiable - Mock Get-VSTeamProfile { return $contents | ConvertFrom-Json | ForEach-Object { $_ } } + Context 'Set-VSTeamAccount profile and drive' { + Mock _isOnWindows { return $false } + Mock _setEnvironmentVariables + Mock Set-VSTeamAPIVersion + Mock Write-Output -Verifiable + Mock Get-VSTeamProfile { return $contents | ConvertFrom-Json | ForEach-Object { $_ } } - It 'should set env at process level' { - Set-VSTeamAccount -Profile mydemos -Drive mydemos + It 'should set env at process level' { + Set-VSTeamAccount -Profile mydemos -Drive mydemos - Assert-VerifiableMock + Assert-VerifiableMock - Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { - $Target -eq 'VSTS' - } + Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { + $Target -eq 'VSTS' + } - # Make sure set env vars was called with the correct parameters - Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { - $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' - } + # Make sure set env vars was called with the correct parameters + Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { + $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' } } + } - Context 'Set-VSTeamAccount vsts' { - Mock _isOnWindows { return $false } - Mock _setEnvironmentVariables - Mock Set-VSTeamAPIVersion + Context 'Set-VSTeamAccount vsts' { + Mock _isOnWindows { return $false } + Mock _setEnvironmentVariables + Mock Set-VSTeamAPIVersion - It 'should set env at process level' { - Set-VSTeamAccount -a mydemos -pe 12345 -Version VSTS + It 'should set env at process level' { + Set-VSTeamAccount -a mydemos -pe 12345 -Version VSTS - Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { - $Target -eq 'VSTS' - } + Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { + $Target -eq 'VSTS' + } - # Make sure set env vars was called with the correct parameters - Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { - $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' - } + # Make sure set env vars was called with the correct parameters + Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { + $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' } } + } - Context 'Set-VSTeamAccount vsts (old URL)' { - Mock _isOnWindows { return $false } - Mock _setEnvironmentVariables - Mock Set-VSTeamAPIVersion + Context 'Set-VSTeamAccount vsts (old URL)' { + Mock _isOnWindows { return $false } + Mock _setEnvironmentVariables + Mock Set-VSTeamAPIVersion - It 'should set env at process level' { - Set-VSTeamAccount -a https://mydemos.visualstudio.com -pe 12345 -Version VSTS + It 'should set env at process level' { + Set-VSTeamAccount -a https://mydemos.visualstudio.com -pe 12345 -Version VSTS - Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { - $Target -eq 'VSTS' - } + Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { + $Target -eq 'VSTS' + } - # Make sure set env vars was called with the correct parameters - Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { - $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' - } + # Make sure set env vars was called with the correct parameters + Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { + $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' } } + } - Context 'Set-VSTeamAccount vsts OAuth' { - Mock _isOnWindows { return $false } - Mock _setEnvironmentVariables - Mock Set-VSTeamAPIVersion + Context 'Set-VSTeamAccount vsts OAuth' { + Mock _isOnWindows { return $false } + Mock _setEnvironmentVariables + Mock Set-VSTeamAPIVersion - It 'should set env at process level' { - Set-VSTeamAccount -a mydemos -pe 12345 -Version VSTS -UseBearerToken + It 'should set env at process level' { + Set-VSTeamAccount -a mydemos -pe 12345 -Version VSTS -UseBearerToken - Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { - $Target -eq 'VSTS' - } + Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { + $Target -eq 'VSTS' + } - # Make sure set env vars was called with the correct parameters - Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { - $Level -eq 'Process' -and $Pat -eq '' -and $BearerToken -eq 12345 -and $Acct -eq 'https://dev.azure.com/mydemos' - } + # Make sure set env vars was called with the correct parameters + Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { + $Level -eq 'Process' -and $Pat -eq '' -and $BearerToken -eq 12345 -and $Acct -eq 'https://dev.azure.com/mydemos' } } + } - Context 'Set-VSTeamAccount vsts with securePersonalAccessToken' { - Mock _isOnWindows { return $false } - Mock _setEnvironmentVariables - Mock Set-VSTeamAPIVersion + Context 'Set-VSTeamAccount vsts with securePersonalAccessToken' { + Mock _isOnWindows { return $false } + Mock _setEnvironmentVariables + Mock Set-VSTeamAPIVersion - It 'should set env at process level' { - $password = '12345' | ConvertTo-SecureString -AsPlainText -Force + It 'should set env at process level' { + $password = '12345' | ConvertTo-SecureString -AsPlainText -Force - Set-VSTeamAccount -a mydemos -SecurePersonalAccessToken $password -Version VSTS + Set-VSTeamAccount -a mydemos -SecurePersonalAccessToken $password -Version VSTS - Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { - $Target -eq 'VSTS' - } + Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { + $Target -eq 'VSTS' + } - # Make sure set env vars was called with the correct parameters - Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { - $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' - } + # Make sure set env vars was called with the correct parameters + Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { + $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' } } + } - Context 'Set-VSTeamAccount run as administrator' { - # I have to write both test just in case the actually - # start the PowerShell window as Admin or not. If I - # don't write both of these I will get different code - # coverage depending on if I started the PowerShell session - # as admin or not. - Mock _isOnWindows { return $true } - Mock _testAdministrator { return $true } - Mock Set-VSTeamAPIVersion - Mock _setEnvironmentVariables - - It 'should set env at process level' { - Set-VSTeamAccount -a mydemos -pe 12345 - - Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { - $Target -eq 'VSTS' - } - - # Make sure set env vars was called with the correct parameters - Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { - $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' - } + Context 'Set-VSTeamAccount run as administrator' { + # I have to write both test just in case the actually + # start the PowerShell window as Admin or not. If I + # don't write both of these I will get different code + # coverage depending on if I started the PowerShell session + # as admin or not. + Mock _isOnWindows { return $true } + Mock _testAdministrator { return $true } + Mock Set-VSTeamAPIVersion + Mock _setEnvironmentVariables + + It 'should set env at process level' { + Set-VSTeamAccount -a mydemos -pe 12345 + + Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { + $Target -eq 'VSTS' + } + + # Make sure set env vars was called with the correct parameters + Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { + $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' } } + } + + Context 'Set-VSTeamAccount run as normal user' { + # I have to write both test just in case the actually + # start the PowerShell window as Admin or not. If I + # don't write both of these I will get different code + # coverage depending on if I started the PowerShell session + # as admin or not. + Mock _isOnWindows { return $false } + Mock _testAdministrator { return $false } + Mock Set-VSTeamAPIVersion + Mock _setEnvironmentVariables + + It 'should set env at process level' { + Set-VSTeamAccount -a mydemos -pe 12345 + + Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { + $Target -eq 'VSTS' + } - Context 'Set-VSTeamAccount run as normal user' { - # I have to write both test just in case the actually - # start the PowerShell window as Admin or not. If I - # don't write both of these I will get different code - # coverage depending on if I started the PowerShell session - # as admin or not. - Mock _isOnWindows { return $false } - Mock _testAdministrator { return $false } - Mock Set-VSTeamAPIVersion - Mock _setEnvironmentVariables - - It 'should set env at process level' { - Set-VSTeamAccount -a mydemos -pe 12345 - - Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { - $Target -eq 'VSTS' - } - - # Make sure set env vars was called with the correct parameters - Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { - $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' - } + # Make sure set env vars was called with the correct parameters + Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { + $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' } } + } + + Context 'Set-VSTeamAccount TFS from windows' { + # I have to write both test just in case the actually + # start the PowerShell window as Admin or not. If I + # don't write both of these I will get different code + # coverage depending on if I started the PowerShell session + # as admin or not. + Mock _isOnWindows { return $true } + Mock _testAdministrator { return $false } + Mock Set-VSTeamAPIVersion + Mock _setEnvironmentVariables + + It 'should set env at process level' { + Set-VSTeamAccount -a http://localhost:8080/tfs/defaultcollection -UseWindowsAuthentication + + Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { + $Target -eq 'TFS2017' + } - Context 'Set-VSTeamAccount TFS from windows' { - # I have to write both test just in case the actually - # start the PowerShell window as Admin or not. If I - # don't write both of these I will get different code - # coverage depending on if I started the PowerShell session - # as admin or not. - Mock _isOnWindows { return $true } - Mock _testAdministrator { return $false } - Mock Set-VSTeamAPIVersion - Mock _setEnvironmentVariables - - It 'should set env at process level' { - Set-VSTeamAccount -a http://localhost:8080/tfs/defaultcollection -UseWindowsAuthentication - - Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { - $Target -eq 'TFS2017' - } - - # Make sure set env vars was called with the correct parameters - Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { - $Level -eq 'Process' -and $Acct -eq 'http://localhost:8080/tfs/defaultcollection' - } + # Make sure set env vars was called with the correct parameters + Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { + $Level -eq 'Process' -and $Acct -eq 'http://localhost:8080/tfs/defaultcollection' } } + } - Context 'Set-VSTeamAccount TFS' { - # I have to write both test just in case the actually - # start the PowerShell window as Admin or not. If I - # don't write both of these I will get different code - # coverage depending on if I started the PowerShell session - # as admin or not. - Mock _isOnWindows { return $false } - Mock _testAdministrator { return $false } - Mock Set-VSTeamAPIVersion - Mock _setEnvironmentVariables - - It 'should set env at process level' { - Set-VSTeamAccount -a http://localhost:8080/tfs/defaultcollection -pe 12345 - - Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { - $Target -eq 'TFS2017' - } - - # Make sure set env vars was called with the correct parameters - Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { - $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'http://localhost:8080/tfs/defaultcollection' - } + Context 'Set-VSTeamAccount TFS' { + # I have to write both test just in case the actually + # start the PowerShell window as Admin or not. If I + # don't write both of these I will get different code + # coverage depending on if I started the PowerShell session + # as admin or not. + Mock _isOnWindows { return $false } + Mock _testAdministrator { return $false } + Mock Set-VSTeamAPIVersion + Mock _setEnvironmentVariables + + It 'should set env at process level' { + Set-VSTeamAccount -a http://localhost:8080/tfs/defaultcollection -pe 12345 + + Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { + $Target -eq 'TFS2017' + } + + # Make sure set env vars was called with the correct parameters + Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { + $Level -eq 'Process' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'http://localhost:8080/tfs/defaultcollection' } } + } + + Context 'Set-VSTeamAccount at user level on Windows machine' { + # This is only supported on a Windows machine. So we have + # to Mock the call to _isOnWindows so you can develop on a + # Mac or Linux machine. + Mock _isOnWindows { return $true } + + # Have to Mock this because you can't call + # [Security.Principal.WindowsIdentity]::GetCurrent() + # on Mac and Linux + Mock _testAdministrator { return $false } + Mock _setEnvironmentVariables + Mock Set-VSTeamAPIVersion + + It 'should set env at user level' { + Set-VSTeamAccount -a mydemos -pe 12345 -Level User + + Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { + $Target -eq 'VSTS' + } - Context 'Set-VSTeamAccount at user level on Windows machine' { - # This is only supported on a Windows machine. So we have - # to Mock the call to _isOnWindows so you can develop on a - # Mac or Linux machine. - Mock _isOnWindows { return $true } - - # Have to Mock this because you can't call - # [Security.Principal.WindowsIdentity]::GetCurrent() - # on Mac and Linux - Mock _testAdministrator { return $false } - Mock _setEnvironmentVariables - Mock Set-VSTeamAPIVersion - - It 'should set env at user level' { - Set-VSTeamAccount -a mydemos -pe 12345 -Level User - - Assert-MockCalled Set-VSTeamAPIVersion -Exactly -Scope It -Times 1 -ParameterFilter { - $Target -eq 'VSTS' - } - - # Make sure set env vars was called with the correct parameters - Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { - $Level -eq 'User' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' - } + # Make sure set env vars was called with the correct parameters + Assert-MockCalled _setEnvironmentVariables -Exactly -Scope It -Times 1 -ParameterFilter { + $Level -eq 'User' -and $Pat -eq 'OjEyMzQ1' -and $Acct -eq 'https://dev.azure.com/mydemos' } } } diff --git a/unit/test/Set-VSTeamApproval.Tests.ps1 b/unit/test/Set-VSTeamApproval.Tests.ps1 new file mode 100644 index 000000000..70531dbb8 --- /dev/null +++ b/unit/test/Set-VSTeamApproval.Tests.ps1 @@ -0,0 +1,80 @@ +Set-StrictMode -Version Latest + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" + +Describe 'Set-VSTeamApproval' -Tag 'unit', 'approvals' { + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Context 'Set-VSTeamApproval' { + Mock Invoke-RestMethod { return @{ + id = 1 + revision = 1 + approver = @{ + id = 'c1f4b9a6-aee1-41f9-a2e0-070a79973ae9' + displayName = 'Test User' + } + } } + + Set-VSTeamApproval -projectName project -Id 1 -Status Rejected -Force + + It 'should set approval' { + Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 ` + -ParameterFilter { + $Method -eq 'Patch' -and + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/approvals/1?api-version=$(_getApiVersion Release)" + } + } + } + + Context 'Set-VSTeamApproval handles exception' { + Mock _handleException -Verifiable + Mock Invoke-RestMethod { throw 'testing error handling' } + + Set-VSTeamApproval -projectName project -Id 1 -Status Rejected -Force + + It 'should set approval' { + Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 ` + -ParameterFilter { + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/approvals/1?api-version=$(_getApiVersion Release)" + } + } + } + + Context 'Set-VSTeamApproval' { + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock Invoke-RestMethod { return @{ + id = 1 + revision = 1 + approver = @{ + id = 'c1f4b9a6-aee1-41f9-a2e0-070a79973ae9' + displayName = 'Test User' + } + } } + + Set-VSTeamApproval -projectName project -Id 1 -Status Rejected -Force + + It 'should set approval' { + Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 ` + -ParameterFilter { + $Method -eq 'Patch' -and + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/approvals/1?api-version=$(_getApiVersion Release)" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Set-VSTeamDefaultProject.Tests.ps1 b/unit/test/Set-VSTeamDefaultProject.Tests.ps1 new file mode 100644 index 000000000..5789c066c --- /dev/null +++ b/unit/test/Set-VSTeamDefaultProject.Tests.ps1 @@ -0,0 +1,101 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamProcessCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinition.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamQueue.ps1" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" +. "$here/../../Source/Public/Get-VSTeamBuildDefinition.ps1" +. "$here/../../Source/Public/Get-VSTeamProcess.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamProject' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + . "$PSScriptRoot\mocks\mockProcessNameDynamicParam.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + + Context 'Set-VSTeamDefaultProject' { + AfterAll { + $Global:PSDefaultParameterValues.Remove("*:projectName") + } + + It 'should set default project' { + Set-VSTeamDefaultProject 'DefaultProject' + + $Global:PSDefaultParameterValues['*:projectName'] | Should be 'DefaultProject' + } + + It 'should update default project' { + $Global:PSDefaultParameterValues['*:projectName'] = 'DefaultProject' + + Set-VSTeamDefaultProject -Project 'NextProject' + + $Global:PSDefaultParameterValues['*:projectName'] | Should be 'NextProject' + } + } + + Context 'Set-VSTeamDefaultProject on Non Windows' { + AfterAll { + $Global:PSDefaultParameterValues.Remove("*:projectName") + } + + Mock _isOnWindows { return $false } -Verifiable + + It 'should set default project' { + Set-VSTeamDefaultProject 'MyProject' + + Assert-VerifiableMock + $Global:PSDefaultParameterValues['*:projectName'] | Should be 'MyProject' + } + } + + Context 'Set-VSTeamDefaultProject As Admin on Windows' { + AfterAll { + $Global:PSDefaultParameterValues.Remove("*:projectName") + } + + Mock _isOnWindows { return $true } + Mock _testAdministrator { return $true } -Verifiable + + It 'should set default project' { + Set-VSTeamDefaultProject 'MyProject' + + Assert-VerifiableMock + $Global:PSDefaultParameterValues['*:projectName'] | Should be 'MyProject' + } + } +} \ No newline at end of file diff --git a/unit/test/Set-VSTeamEnvironmentStatus.Tests.ps1 b/unit/test/Set-VSTeamEnvironmentStatus.Tests.ps1 new file mode 100644 index 000000000..6cb2d9711 --- /dev/null +++ b/unit/test/Set-VSTeamEnvironmentStatus.Tests.ps1 @@ -0,0 +1,55 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamFeed.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamBuild.ps1" +. "$here/../../Source/Public/Get-VSTeamReleaseDefinition.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamReleaseStatus' { + ## Arrange + [VSTeamVersions]::Release = '1.0-unittest' + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context 'Set-VSTeamEnvironmentStatus by ID' { + Mock _useWindowsAuthenticationOnPremise { return $false } + Mock Invoke-RestMethod + Mock Invoke-RestMethod { throw 'error' } -ParameterFilter { $Uri -like "*101*" } + + $expectedBody = ConvertTo-Json ([PSCustomObject]@{status = 'inProgress'; comment = ''; scheduledDeploymentTime = $null }) + + It 'should set environments' { + ## Act + Set-VSTeamEnvironmentStatus -ProjectName project -ReleaseId 1 -Id 15 -Status inProgress -Force + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Patch' -and + $Body -eq $expectedBody -and + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/1/environments/15?api-version=$(_getApiVersion Release)" + } + } + + It 'by Id should throw' { + ## Act / Assert + { Set-VSTeamEnvironmentStatus -ProjectName project -ReleaseId 101 -Id 101 -Status inProgress -Force } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Set-VSTeamPermissionInheritance.Tests.ps1 b/unit/test/Set-VSTeamPermissionInheritance.Tests.ps1 index 5c55b79ae..57739d199 100644 --- a/unit/test/Set-VSTeamPermissionInheritance.Tests.ps1 +++ b/unit/test/Set-VSTeamPermissionInheritance.Tests.ps1 @@ -18,18 +18,13 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Public/Get-VSTeamAccessControlList.ps1" . "$here/../../Source/Public/$sut" -# Loading System.Web avoids issues finding System.Web.HttpUtility -Add-Type -AssemblyName 'System.Web' - Describe 'Set-VSTeamPermissionInheritance' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - + $gitRepoResult = Get-Content "$PSScriptRoot\sampleFiles\singleGitRepo.json" -Raw | ConvertFrom-Json $buildDefresults = Get-Content "$PSScriptRoot\sampleFiles\buildDefAzD.json" -Raw | ConvertFrom-Json $releaseDefresults = Get-Content "$PSScriptRoot\sampleFiles\releaseDefAzD.json" -Raw | ConvertFrom-Json - $gitRepoResult = Get-Content "$PSScriptRoot\sampleFiles\singleGitRepo.json" -Raw | ConvertFrom-Json + $accesscontrollistsResult = Get-Content "$PSScriptRoot\sampleFiles\repoAccesscontrollists.json" -Raw | ConvertFrom-Json $gitRepoHierarchyUpdateResults = Get-Content "$PSScriptRoot\sampleFiles\gitReopHierarchyQuery_Update.json" -Raw | ConvertFrom-Json $buildDefHierarchyUpdateResults = Get-Content "$PSScriptRoot\sampleFiles\buildDefHierarchyQuery_Update.json" -Raw | ConvertFrom-Json - $accesscontrollistsResult = Get-Content "$PSScriptRoot\sampleFiles\repoAccesscontrollists.json" -Raw | ConvertFrom-Json $releaseDefHierarchyUpdateResults = Get-Content "$PSScriptRoot\sampleFiles\releaseDefHierarchyQuery_Update.json" -Raw | ConvertFrom-Json $singleResult = [PSCustomObject]@{ @@ -40,24 +35,24 @@ Describe 'Set-VSTeamPermissionInheritance' { state = '' visibility = '' revision = 0 - defaultTeam = [PSCustomObject]@{} - _links = [PSCustomObject]@{} + defaultTeam = [PSCustomObject]@{ } + _links = [PSCustomObject]@{ } } + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Build' -or $Service -eq 'Release' -or $Service -eq 'Git' } # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } Context 'Set-VSTeamPermissionInheritance buildDef' { - Mock _useWindowsAuthenticationOnPremise { return $true } Mock Get-VSTeamProject { return $singleResult } + Mock _useWindowsAuthenticationOnPremise { return $true } Mock Get-VSTeamBuildDefinition { return $buildDefresults.value } Mock Invoke-RestMethod { # If this test fails uncomment the line below to see how the mock was called. # Write-Host $args - # Write-Host $([VSTeamVersions]::Build) + # Write-Host $(_getApiVersion Build) return $buildDefHierarchyUpdateResults } @@ -70,7 +65,7 @@ Describe 'Set-VSTeamPermissionInheritance' { $Body -like '*123-5464-dee43/1432*' -and $Body -like '*33344d9c-fc72-4d6f-aba5-fa317101a7e9*' -and $Uri -like "*https://dev.azure.com/test/_apis/Contribution/HierarchyQuery/123-5464-dee43*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" + $Uri -like "*api-version=$(_getApiVersion Build)*" } } } @@ -82,7 +77,7 @@ Describe 'Set-VSTeamPermissionInheritance' { Mock Invoke-RestMethod { # If this test fails uncomment the line below to see how the mock was called. # Write-Host $args - # Write-Host $([VSTeamVersions]::Release) + # Write-Host $(_getApiVersion Release) return $releaseDefHierarchyUpdateResults } @@ -95,7 +90,7 @@ Describe 'Set-VSTeamPermissionInheritance' { $Body -like '*c788c23e-1b46-4162-8f5e-d7585343b5de*' -and $Body -like '*123-5464-dee43//2*' -and $Uri -like "*https://dev.azure.com/test/_apis/Contribution/HierarchyQuery/123-5464-dee43*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Release)*" + $Uri -like "*api-version=$(_getApiVersion Release)*" } } } @@ -104,12 +99,12 @@ Describe 'Set-VSTeamPermissionInheritance' { Mock _useWindowsAuthenticationOnPremise { return $true } Mock Get-VSTeamProject { return $singleResult } Mock Get-VSTeamGitRepository { return $gitRepoResult } - Mock Get-VSTeamAccessControlList { return $accesscontrollistsResult.value } -Verifiable + Mock Get-VSTeamAccessControlList { return $accesscontrollistsResult.value } Mock Invoke-RestMethod { # If this test fails uncomment the line below to see how the mock was called. #Write-Host $args - #Write-Host $([VSTeamVersions]::Git) + #Write-Host $(_getApiVersion Git) return $gitRepoHierarchyUpdateResults } @@ -122,7 +117,7 @@ Describe 'Set-VSTeamPermissionInheritance' { $Body -like '*2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*' -and $Body -like '*repoV2/123-5464-dee43/00000000-0000-0000-0000-000000000001*' -and $Uri -like "*https://dev.azure.com/test/_apis/Contribution/HierarchyQuery/123-5464-dee43*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Git)*" + $Uri -like "*api-version=$(_getApiVersion Git)*" } } } diff --git a/unit/test/Set-VSTeamReleaseStatus.Tests.ps1 b/unit/test/Set-VSTeamReleaseStatus.Tests.ps1 new file mode 100644 index 000000000..9f44727a0 --- /dev/null +++ b/unit/test/Set-VSTeamReleaseStatus.Tests.ps1 @@ -0,0 +1,54 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamFeed.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamBuild.ps1" +. "$here/../../Source/Public/Get-VSTeamReleaseDefinition.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamReleaseStatus' { + ## Arrange + [VSTeamVersions]::Release = '1.0-unittest' + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context 'Set-VSTeamReleaseStatus' { + ## Arrange + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock Invoke-RestMethod + Mock Invoke-RestMethod { throw 'error' } -ParameterFilter { $Uri -like "*101*" } + + It 'by Id should set release status' { + ## Act + Set-VSTeamReleaseStatus -ProjectName project -Id 15 -Status Abandoned -Force + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Patch' -and + $Body -eq '{ "id": 15, "status": "Abandoned" }' -and + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/15?api-version=$(_getApiVersion Release)" + } + } + + It 'by Id should throw' { + ## Act / Assert + { Set-VSTeamReleaseStatus -ProjectName project -Id 101 -Status Abandoned -Force } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Show-VSTeamApproval.Tests.ps1 b/unit/test/Show-VSTeamApproval.Tests.ps1 new file mode 100644 index 000000000..d10d86fdc --- /dev/null +++ b/unit/test/Show-VSTeamApproval.Tests.ps1 @@ -0,0 +1,33 @@ +Set-StrictMode -Version Latest + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" + +Describe 'Show-VSTeamApproval' -Tag 'unit', 'approvals' { + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Context 'Succeeds' { + Mock Show-Browser -Verifiable + + Show-VSTeamApproval -projectName project -ReleaseDefinitionId 1 + + It 'should open in browser' { + Assert-VerifiableMock + } + } +} \ No newline at end of file diff --git a/unit/test/Show-VSTeamBuild.Tests.ps1 b/unit/test/Show-VSTeamBuild.Tests.ps1 new file mode 100644 index 000000000..1f182df38 --- /dev/null +++ b/unit/test/Show-VSTeamBuild.Tests.ps1 @@ -0,0 +1,31 @@ +Set-StrictMode -Version Latest + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" + +Describe 'Show-VSTeamBuild' { + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + Context 'By ID' { + Mock Show-Browser { } + + it 'should return url for mine' { + Show-VSTeamBuild -projectName project -Id 15 + + Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { $url -eq 'https://dev.azure.com/test/project/_build/index?buildId=15' } + } + } +} \ No newline at end of file diff --git a/unit/test/Show-VSTeamBuildDefinition.Tests.ps1 b/unit/test/Show-VSTeamBuildDefinition.Tests.ps1 new file mode 100644 index 000000000..d64c1dc74 --- /dev/null +++ b/unit/test/Show-VSTeamBuildDefinition.Tests.ps1 @@ -0,0 +1,90 @@ +Set-StrictMode -Version Latest + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" + +Describe 'Show-VSTeamBuildDefinition' { + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Context 'Show-VSTeamBuildDefinition by ID' { + Mock Show-Browser { } + + it 'should return url for mine' { + Show-VSTeamBuildDefinition -projectName project -Id 15 + + Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { + $url -eq 'https://dev.azure.com/test/project/_build/index?definitionId=15' + } + } + } + + Context 'Show-VSTeamBuildDefinition Mine' { + Mock Show-Browser { } + + it 'should return url for mine' { + Show-VSTeamBuildDefinition -projectName project -Type Mine + + Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { + $url -eq 'https://dev.azure.com/test/project/_build/index?_a=mine&path=%5c' + } + } + } + + Context 'Show-VSTeamBuildDefinition XAML' { + Mock Show-Browser { } + + it 'should return url for XAML' { + Show-VSTeamBuildDefinition -projectName project -Type XAML + + Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { + $url -eq 'https://dev.azure.com/test/project/_build/xaml&path=%5c' + } + } + } + + Context 'Show-VSTeamBuildDefinition Queued' { + Mock Show-Browser { } + + it 'should return url for Queued' { + Show-VSTeamBuildDefinition -projectName project -Type Queued + + Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { + $url -eq 'https://dev.azure.com/test/project/_build/index?_a=queued&path=%5c' + } + } + } + + Context 'Show-VSTeamBuildDefinition Mine with path' { + Mock Show-Browser { } + + it 'should return url for mine' { + Show-VSTeamBuildDefinition -projectName project -path '\test' + + Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { + $url -like 'https://dev.azure.com/test/project/_Build/index?_a=allDefinitions&path=%5Ctest' + } + } + } + + Context 'Show-VSTeamBuildDefinition Mine with path missing \' { + Mock Show-Browser { } + + it 'should return url for mine with \ added' { + Show-VSTeamBuildDefinition -projectName project -path 'test' + + Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { + $url -like 'https://dev.azure.com/test/project/_Build/index?_a=allDefinitions&path=%5Ctest' + } + } + } +} \ No newline at end of file diff --git a/unit/test/Show-VSTeamFeed.Tests.ps1 b/unit/test/Show-VSTeamFeed.Tests.ps1 new file mode 100644 index 000000000..b0c08cd8f --- /dev/null +++ b/unit/test/Show-VSTeamFeed.Tests.ps1 @@ -0,0 +1,40 @@ +Set-StrictMode -Version Latest + +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" + +Describe 'Feeds' { + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + Context 'Show-VSTeamFeed by name' { + Mock Show-Browser + + It 'Show call start' { + Show-VSTeamFeed -Name module + + Assert-MockCalled Show-Browser + } + } + + Context 'Show-VSTeamFeed by id' { + Mock Show-Browser + + It 'Show call start' { + Show-VSTeamFeed -Id '00000000-0000-0000-0000-000000000000' + + Assert-MockCalled Show-Browser + } + } +} \ No newline at end of file diff --git a/unit/test/Show-VSTeamGitRepository.Tests.ps1 b/unit/test/Show-VSTeamGitRepository.Tests.ps1 new file mode 100644 index 000000000..96654369b --- /dev/null +++ b/unit/test/Show-VSTeamGitRepository.Tests.ps1 @@ -0,0 +1,72 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinition.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamQueue.ps1" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" +. "$here/../../Source/Public/Get-VSTeamBuildDefinition.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe "VSTeamGitRepository" { + ## Arrange + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Context 'Show-VSTeamGitRepository' { + Mock Show-Browser + Mock _getInstance { return 'https://dev.azure.com/test' } + + it 'by project should return url for mine' { + Show-VSTeamGitRepository -projectName project + + Assert-MockCalled Show-Browser -Exactly -Times 1 -Scope It -ParameterFilter { + $url -eq 'https://dev.azure.com/test/_git/project' + } + } + + it 'by remote url should return url for mine' { + Show-VSTeamGitRepository -RemoteUrl 'https://dev.azure.com/test/_git/project' + Assert-MockCalled Show-Browser -Exactly -Times 1 -Scope It -ParameterFilter { + $url -eq 'https://dev.azure.com/test/_git/project' + } + } + } +} \ No newline at end of file diff --git a/unit/test/Show-VSTeamProject.Tests.ps1 b/unit/test/Show-VSTeamProject.Tests.ps1 new file mode 100644 index 000000000..01c012817 --- /dev/null +++ b/unit/test/Show-VSTeamProject.Tests.ps1 @@ -0,0 +1,39 @@ +Set-StrictMode -Version Latest + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" + +Describe 'VSTeamProject' { + Mock _getInstance { return 'https://dev.azure.com/test' } + + Context 'Show-VSTeamProject' { + Mock Show-Browser + + It 'by ID should call start' { + Show-VSTeamProject -Id 123456 + + Assert-MockCalled Show-Browser -Exactly -Times 1 -Scope It -ParameterFilter { + $Url -eq "https://dev.azure.com/test/123456" + } + } + + It 'by nameed ProjectName parameter should call open' { + Show-VSTeamProject -ProjectName ShowProject + + Assert-MockCalled Show-Browser -Exactly -Times 1 -Scope It -ParameterFilter { + $Url -eq "https://dev.azure.com/test/ShowProject" + } + } + + It 'by postion projectName parameter should call open' { + Show-VSTeamProject ShowProject + + Assert-MockCalled Show-Browser -Exactly -Times 1 -Scope It -ParameterFilter { + $Url -eq "https://dev.azure.com/test/ShowProject" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Show-VSTeamPullRequest.Tests.ps1 b/unit/test/Show-VSTeamPullRequest.Tests.ps1 new file mode 100644 index 000000000..f6d4bdc70 --- /dev/null +++ b/unit/test/Show-VSTeamPullRequest.Tests.ps1 @@ -0,0 +1,57 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProcess.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamProcessCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamPullRequest.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamPullRequest' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + + # You have to set the version or the api-version will not be added when versions = '' + [VSTeamVersions]::Git = '5.1-preview' + + $singleResult = @{ + pullRequestId = 1 + repositoryName = "testreponame" + repository = @{ + project = @{ + name = "testproject" + } + } + reviewers = @{ + vote = 0 + } + } + + Context 'Show-VSTeamPullRequest' { + It 'Show-VSTeamPullRequest by Id' { + Mock Invoke-RestMethod { return $singleResult } + Mock Show-Browser + + Show-VSTeamPullRequest -Id 1 + + Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { + $url -eq "https://dev.azure.com/test/testproject/_git/testreponame/pullrequest/1" + } + } + + It 'Show-VSTeamPullRequest with invalid ID' { + Mock Invoke-RestMethod { return $singleResult } + Mock Show-Browser { throw } + + { Show-VSTeamPullRequest -Id 999999 } | Should throw + } + } +} \ No newline at end of file diff --git a/unit/test/Show-VSTeamRelease.Tests.ps1 b/unit/test/Show-VSTeamRelease.Tests.ps1 new file mode 100644 index 000000000..19361e19f --- /dev/null +++ b/unit/test/Show-VSTeamRelease.Tests.ps1 @@ -0,0 +1,50 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamFeed.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamBuild.ps1" +. "$here/../../Source/Public/Get-VSTeamReleaseDefinition.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamRelease' { + ## Arrange + [VSTeamVersions]::Release = '1.0-unittest' + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context 'Show-VSTeamRelease' { + ## Arrange + Mock Show-Browser { } + + it 'by Id should show release' { + ## Act + Show-VSTeamRelease -projectName project -Id 15 + + ## Assert + Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { + $url -eq 'https://dev.azure.com/test/project/_release?releaseId=15' + } + } + + ## Act / Assert + it 'with invalid Id should throw' { + { Show-VSTeamRelease -projectName project -Id 0 } | Should throw + } + } +} \ No newline at end of file diff --git a/unit/test/Show-VSTeamReleaseDefinition.Tests.ps1 b/unit/test/Show-VSTeamReleaseDefinition.Tests.ps1 new file mode 100644 index 000000000..c0b34b482 --- /dev/null +++ b/unit/test/Show-VSTeamReleaseDefinition.Tests.ps1 @@ -0,0 +1,35 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamReleaseDefinition' { + ## Arrange + [VSTeamVersions]::Release = '1.0-unittest' + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context 'Show-VSTeamReleaseDefinition' { + Mock Show-Browser { } + Mock _getInstance { return 'https://dev.azure.com/test' } + + it 'by Id should return release definitions' { + Show-VSTeamReleaseDefinition -projectName project -Id 15 + + Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { + $url -eq 'https://dev.azure.com/test/project/_release?definitionId=15' + } + } + } +} \ No newline at end of file diff --git a/unit/test/Show-VSTeamWorkItem.Tests.ps1 b/unit/test/Show-VSTeamWorkItem.Tests.ps1 new file mode 100644 index 000000000..8910a7c43 --- /dev/null +++ b/unit/test/Show-VSTeamWorkItem.Tests.ps1 @@ -0,0 +1,29 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamWorkItem' { + Mock _getInstance { return 'https://dev.azure.com/test' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context 'Show-VSTeamWorkItem' { + Mock Show-Browser { } + + it 'should return url for mine' { + Show-VSTeamWorkItem -projectName project -Id 15 + + Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { $url -eq 'https://dev.azure.com/test/project/_workitems/edit/15' } + } + } +} \ No newline at end of file diff --git a/unit/test/Test-VSTeamMembership.Tests.ps1 b/unit/test/Test-VSTeamMembership.Tests.ps1 new file mode 100644 index 000000000..8a85cc3c1 --- /dev/null +++ b/unit/test/Test-VSTeamMembership.Tests.ps1 @@ -0,0 +1,45 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/callMembershipAPI.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamMembership' { + ## Arrange + # You have to set the version or the api-version will not be added when [VSTeamVersions]::Graph = '' + [VSTeamVersions]::Graph = '5.0' + Mock _supportsGraph + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Invoke-RestMethod + + $UserDescriptor = 'aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj' + $GroupDescriptor = 'vssgp.Uy0xLTktMTU1MTM3NDI0NS04NTYwMDk3MjYtNDE5MzQ0MjExNy0yMzkwNzU2MTEwLTI3NDAxNjE4MjEtMC0wLTAtMC0x' + + Context 'Test-VSTeamMembership' { + It 'Should test membership' { + ## Act + $result = Test-VSTeamMembership -MemberDescriptor $UserDescriptor -ContainerDescriptor $GroupDescriptor + + ## Assert + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq "Head" -and + $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/memberships/$UserDescriptor/$GroupDescriptor*" -and + $Uri -like "*api-version=$(_getApiVersion Graph)*" + } + + $result | Should Be $true + } + } +} \ No newline at end of file diff --git a/unit/test/Test-VSTeamYamlPipeline.Tests.ps1 b/unit/test/Test-VSTeamYamlPipeline.Tests.ps1 index cdcaa48f7..b59b28d2b 100644 --- a/unit/test/Test-VSTeamYamlPipeline.Tests.ps1 +++ b/unit/test/Test-VSTeamYamlPipeline.Tests.ps1 @@ -38,7 +38,7 @@ Describe 'Test-VSTeamYamlPipeline' { Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*https://dev.azure.com/test/project/_apis/pipelines/24/runs*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and + $Uri -like "*api-version=$(_getApiVersion Build)*" -and $Body -like '*"PreviewRun":*true*' -and $Body -notlike '*YamlOverride*' } @@ -51,7 +51,7 @@ Describe 'Test-VSTeamYamlPipeline' { Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*https://dev.azure.com/test/project/_apis/pipelines/24/runs*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and + $Uri -like "*api-version=$(_getApiVersion Build)*" -and $Body -like '*"PreviewRun":*true*' -and $Body -like '*YamlOverride*' } @@ -65,7 +65,7 @@ Describe 'Test-VSTeamYamlPipeline' { Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*https://dev.azure.com/test/project/_apis/pipelines/24/runs*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and + $Uri -like "*api-version=$(_getApiVersion Build)*" -and $Body -like '*"PreviewRun":*true*' -and $Body -like '*YamlOverride*' } diff --git a/unit/test/Update-VSTeam.Tests.ps1 b/unit/test/Update-VSTeam.Tests.ps1 new file mode 100644 index 000000000..144f79098 --- /dev/null +++ b/unit/test/Update-VSTeam.Tests.ps1 @@ -0,0 +1,139 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamTeam.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeam.ps1" +. "$here/../../Source/Public/$sut" +#endregion + + +$results = [PSCustomObject]@{ + value = [PSCustomObject]@{ + id = '6f365a7143e492e911c341451a734401bcacadfd' + name = 'refs/heads/master' + description = 'team description' + } +} + +$singleResult = [PSCustomObject]@{ + id = '6f365a7143e492e911c341451a734401bcacadfd' + name = 'refs/heads/master' + description = 'team description' +} + +Describe "VSTeam" { + Context "Get-VSTeam" { + Context "services" { + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Core' } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Context 'Update-VSTeam without name or description' { + It 'Should throw' { + { Update-VSTeam -ProjectName Test -TeamToUpdate "OldTeamName" } | Should Throw + } + } + + Context 'Update-VSTeam with new team name' { + Mock Invoke-RestMethod { return $singleResult } + + It 'Should update the team' { + Update-VSTeam -ProjectName Test -TeamToUpdate "OldTeamName" -NewTeamName "NewTeamName" + + $expectedBody = '{ "name": "NewTeamName" }' + + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams/OldTeamName?api-version=$(_getApiVersion Core)" -and + $Method -eq "Patch" -and + $Body -eq $expectedBody + } + } + } + + Context 'Update-VSTeam with new description' { + Mock Invoke-RestMethod { return $singleResult } + + It 'Should update the team' { + Update-VSTeam -ProjectName Test -TeamToUpdate "OldTeamName" -Description "New Description" + + $expectedBody = '{"description": "New Description" }' + + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams/OldTeamName?api-version=$(_getApiVersion Core)" -and + $Method -eq "Patch" -and + $Body -eq $expectedBody + } + } + } + + Context 'Update-VSTeam with new team name and description' { + Mock Invoke-RestMethod { return $singleResult } + + It 'Should update the team' { + Update-VSTeam -ProjectName Test -TeamToUpdate "OldTeamName" -NewTeamName "NewTeamName" -Description "New Description" + + $expectedBody = '{ "name": "NewTeamName", "description": "New Description" }' + + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams/OldTeamName?api-version=$(_getApiVersion Core)" -and + $Method -eq "Patch" -and + $Body -eq $expectedBody + } + } + } + + Context 'Update-VSTeam, fed through pipeline' { + Mock Get-VSTeam { return New-Object -TypeName PSObject -Prop @{projectname = "TestProject"; name = "OldTeamName" } } + Mock Invoke-RestMethod { return $singleResult } + + It 'Should update the team' { + Get-VSTeam -ProjectName TestProject -TeamId "OldTeamName" | Update-VSTeam -NewTeamName "NewTeamName" -Description "New Description" + + $expectedBody = '{ "name": "NewTeamName", "description": "New Description" }' + + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/_apis/projects/TestProject/teams/OldTeamName?api-version=$(_getApiVersion Core)" -and + $Method -eq "Patch" -and + $Body -eq $expectedBody + } + } + } + } + + + Context "Server" { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Mock _useWindowsAuthenticationOnPremise { return $true } + + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } + + Context 'Update-VSTeam with new team name on TFS local Auth' { + Mock Invoke-RestMethod { return $singleResult } + + It 'Should update the team' { + Update-VSTeam -ProjectName Test -TeamToUpdate "OldTeamName" -NewTeamName "NewTeamName" + + $expectedBody = '{ "name": "NewTeamName" }' + + Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams/OldTeamName?api-version=$(_getApiVersion Core)" -and + $Method -eq "Patch" -and + $Body -eq $expectedBody + } + } + } + } + } +} \ No newline at end of file diff --git a/unit/test/Update-VSTeamAgent.Tests.ps1 b/unit/test/Update-VSTeamAgent.Tests.ps1 index 46973644f..7e4a8b84b 100644 --- a/unit/test/Update-VSTeamAgent.Tests.ps1 +++ b/unit/test/Update-VSTeamAgent.Tests.ps1 @@ -9,7 +9,7 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Public/$sut" Describe 'Update-VSTeamAgent' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + Mock _getInstance { return 'https://dev.azure.com/test' } [VSTeamVersions]::DistributedTask = '1.0-unitTest' Context 'Update-VSTeamAgent by ID' { @@ -24,7 +24,7 @@ Describe 'Update-VSTeamAgent' { Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' -and $Uri -like "*https://dev.azure.com/test/_apis/distributedtask/pools/36/messages*" -and - $Uri -like "*api-version=$([VSTeamVersions]::DistributedTask)*" -and + $Uri -like "*api-version=$(_getApiVersion DistributedTask)*" -and $Uri -like "*agentId=950*" } } diff --git a/unit/test/Update-VSTeamBuild.Tests.ps1 b/unit/test/Update-VSTeamBuild.Tests.ps1 new file mode 100644 index 000000000..3746cbc48 --- /dev/null +++ b/unit/test/Update-VSTeamBuild.Tests.ps1 @@ -0,0 +1,53 @@ +Set-StrictMode -Version Latest + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" + +Describe 'Update-VSTeamBuild' { + Mock Invoke-RestMethod + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + Context 'Update Build keep forever' { + # Load the mocks to create the project name dynamic parameter + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + Update-VSTeamBuild -projectName project -id 1 -KeepForever $true -Force + + It 'should post changes' { + Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { + $Method -eq 'Patch' -and + $Body -eq '{"keepForever": true}' -and + $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/1?api-version=$(_getApiVersion Build)" } + } + } + + Context 'Update Build number' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable + + Mock _useWindowsAuthenticationOnPremise { return $true } + + Update-VSTeamBuild -projectName project -id 1 -BuildNumber 'TestNumber' -KeepForever $true -Force + + It 'should post changes' { + Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { + $Method -eq 'Patch' -and + $Body -eq '{"keepForever": true, "buildNumber": "TestNumber"}' -and + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/1?api-version=$(_getApiVersion Build)" } + } + } +} \ No newline at end of file diff --git a/unit/test/Update-VSTeamBuildDefinition.Tests.ps1 b/unit/test/Update-VSTeamBuildDefinition.Tests.ps1 index c7b257890..8a57792ba 100644 --- a/unit/test/Update-VSTeamBuildDefinition.Tests.ps1 +++ b/unit/test/Update-VSTeamBuildDefinition.Tests.ps1 @@ -6,15 +6,18 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") . "$here/../../Source/Classes/VSTeamVersions.ps1" . "$here/../../Source/Classes/VSTeamProjectCache.ps1" . "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" . "$here/../../Source/Public/$sut" $resultsAzD = Get-Content "$PSScriptRoot\sampleFiles\buildDefvsts.json" -Raw | ConvertFrom-Json Describe "Update-VSTeamBuildDefinition" { + Mock _hasProjectCacheExpired { return $false } + Context "AzD" { # Set the account to use for testing. A normal user would do this # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + Mock _getInstance { return 'https://dev.azure.com/test' } Mock Invoke-RestMethod { # If this test fails uncomment the line below to see how the mock was called. @@ -32,7 +35,7 @@ Describe "Update-VSTeamBuildDefinition" { Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Put' -and $Uri -like "*https://dev.azure.com/test/Demo/_apis/build/definitions/23*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Build)*" + $Uri -like "*api-version=$(_getApiVersion Build)*" } } @@ -45,7 +48,7 @@ Describe "Update-VSTeamBuildDefinition" { Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Put' -and $InFile -eq 'sampleFiles/builddef.json' -and - $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions/2?api-version=$([VSTeamVersions]::Build)" + $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions/2?api-version=$(_getApiVersion Build)" } } } @@ -54,7 +57,7 @@ Describe "Update-VSTeamBuildDefinition" { # Set the account to use for testing. A normal user would do this # using the Set-VSTeamAccount function. Remove-VSTeamAccount - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } Mock _useWindowsAuthenticationOnPremise { return $true } @@ -71,7 +74,7 @@ Describe "Update-VSTeamBuildDefinition" { Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { $Method -eq 'Put' -and $InFile -eq 'sampleFiles/builddef.json' -and - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/definitions/2?api-version=$([VSTeamVersions]::Build)" + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/definitions/2?api-version=$(_getApiVersion Build)" } } } diff --git a/unit/test/Update-VSTeamExtension.Tests.ps1 b/unit/test/Update-VSTeamExtension.Tests.ps1 new file mode 100644 index 000000000..8453f164a --- /dev/null +++ b/unit/test/Update-VSTeamExtension.Tests.ps1 @@ -0,0 +1,57 @@ +Set-StrictMode -Version Latest + +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamInstallState.ps1" +. "$here/../../Source/Classes/VSTeamExtension.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" + +Describe 'VSTeamExtension' { + Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable + + $singleResult = [PSCustomObject]@{ + extensionId = 'test' + extensionName = 'test' + publisherId = 'test' + publisherName = 'test' + version = '1.0.0' + registrationId = '12345678-9012-3456-7890-123456789012' + manifestVersion = 1 + baseUri = '' + fallbackBaseUri = '' + scopes = [PSCustomObject]@{ } + installState = [PSCustomObject]@{ + flags = 'none' + lastUpdated = '2018-10-09T11:26:47.187Z' + } + } + + Context 'Update-VSTeamExtension' { + BeforeAll { + $env:Team_TOKEN = '1234' + } + + AfterAll { + $env:TEAM_TOKEN = $null + } + + Mock _callAPI { return $singleResult } + + It 'Should update an extension' { + Update-VSTeamExtension -PublisherId 'test' -ExtensionId 'test' -ExtensionState disabled -Force + + Assert-MockCalled _callAPI -Exactly 1 -Scope It -ParameterFilter { + $Method -eq 'Post' -and + $subDomain -eq 'extmgmt' -and + $version -eq [VSTeamVersions]::ExtensionsManagement + $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensionsbyname/test/test*" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Update-VSTeamPolicy.Tests.ps1 b/unit/test/Update-VSTeamPolicy.Tests.ps1 new file mode 100644 index 000000000..677d3578c --- /dev/null +++ b/unit/test/Update-VSTeamPolicy.Tests.ps1 @@ -0,0 +1,110 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamPolicy.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamPolicy' { + ## Arrange + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Invoke-RestMethod + Mock Invoke-RestMethod { throw 'Error' } -ParameterFilter { $Uri -like "*boom*" } + Mock Get-VSTeamPolicy { return @{ type = @{ id = 'babcf51f-d853-43a2-9b05-4a64ca577be0' } } } + + Context 'Update-VSTeamPolicy' { + It 'with type should add the policy' { + ## Act + Update-VSTeamPolicy -ProjectName Demo -id 1 -type babcf51f-d853-43a2-9b05-4a64ca577be0 -enabled -blocking -settings @{ + MinimumApproverCount = 1 + scope = @( + @{ + refName = 'refs/heads/release' + matchKind = 'Exact' + repositoryId = '20000000-0000-0000-0000-0000000000002' + }) + } + + ## Assert + # With PowerShell core the order of the boty string is not the + # same from run to run! So instead of testing the entire string + # matches I have to search for the portions I expect but can't + # assume the order. + # The general string should look like this: + #'{"isBlocking":true,"isEnabled":true,"type":{"id":"babcf51f-d853-43a2-9b05-4a64ca577be0"},"settings":{"scope":[{"repositoryId":"20000000-0000-0000-0000-0000000000002","matchKind":"Exact","refName":"refs/heads/release"}],"MinimumApproverCount":1}}' + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Put' -and + $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/configurations/1?api-version=$(_getApiVersion Git)" -and + $Body -like '*"isBlocking":true*' -and + $Body -like '*"isEnabled":true*' -and + $Body -like '*"type":*' -and + $Body -like '*"id":"babcf51f-d853-43a2-9b05-4a64ca577be0"*' -and + $Body -like '*"settings":*' -and + $Body -like '*"scope":*' -and + $Body -like '*"repositoryId":"20000000-0000-0000-0000-0000000000002"*' -and + $Body -like '*"matchKind":"Exact"*' -and + $Body -like '*"refName":"refs/heads/release"*' -and + $Body -like '*"MinimumApproverCount":1*' + } + } + + It 'without type should add the policy' { + ## Act + Update-VSTeamPolicy -ProjectName Demo -id 1 -enabled -blocking -settings @{ + MinimumApproverCount = 1 + scope = @( + @{ + refName = 'refs/heads/release' + matchKind = 'Exact' + repositoryId = '20000000-0000-0000-0000-0000000000002' + }) + } + + ## Assert + # With PowerShell core the order of the boty string is not the + # same from run to run! So instead of testing the entire string + # matches I have to search for the portions I expect but can't + # assume the order. + # The general string should look like this: + #'{"isBlocking":true,"isEnabled":true,"type":{"id":"babcf51f-d853-43a2-9b05-4a64ca577be0"},"settings":{"scope":[{"repositoryId":"20000000-0000-0000-0000-0000000000002","matchKind":"Exact","refName":"refs/heads/release"}],"MinimumApproverCount":1}}' + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Put' -and + $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/configurations/1?api-version=$(_getApiVersion Git)" -and + $Body -like '*"isBlocking":true*' -and + $Body -like '*"isEnabled":true*' -and + $Body -like '*"type":*' -and + $Body -like '*"id":"babcf51f-d853-43a2-9b05-4a64ca577be0"*' -and + $Body -like '*"settings":*' -and + $Body -like '*"scope":*' -and + $Body -like '*"repositoryId":"20000000-0000-0000-0000-0000000000002"*' -and + $Body -like '*"matchKind":"Exact"*' -and + $Body -like '*"refName":"refs/heads/release"*' -and + $Body -like '*"MinimumApproverCount":1*' + } + } + + It 'should throw' { + ## Act / Assert + { Update-VSTeamPolicy -ProjectName boom -id 1 -type babcf51f-d853-43a2-9b05-4a64ca577be0 -enabled -blocking -settings @{ + MinimumApproverCount = 1 + scope = @( + @{ + refName = 'refs/heads/release' + matchKind = 'Exact' + repositoryId = '20000000-0000-0000-0000-0000000000002' + }) + } + } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/Update-VSTeamProfile.Tests.ps1 b/unit/test/Update-VSTeamProfile.Tests.ps1 new file mode 100644 index 000000000..f86616d69 --- /dev/null +++ b/unit/test/Update-VSTeamProfile.Tests.ps1 @@ -0,0 +1,77 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamProfile.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamProfile' { + $expectedPath = "$HOME/vsteam_profiles.json" + + Mock Get-Content { return '' } + Mock Test-Path { return $true } + + Context 'Update-VSTeamProfile entry does not exist' { + Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } + + It 'Should throw' { + { Update-VSTeamProfile -Name Testing -PersonalAccessToken 678910 } | Should -Throw + } + } + + Context 'Update-VSTeamProfile with PAT to empty file' { + Mock Set-Content { } + Mock Write-Warning -Verifiable + Mock Get-VSTeamProfile { } + + Update-VSTeamProfile -name demos -PersonalAccessToken 12345 + + It 'Should save profile to disk' { + Assert-VerifiableMock + } + } + + Context 'Update-VSTeamProfile with securePersonalAccessToken' { + Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*OjY3ODkxMA==*" -and $Value -like "*https://dev.azure.com/test*" -and $Value -like "*VSTS*" } + Mock Set-Content { } + Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test/","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } + + $password = '678910' | ConvertTo-SecureString -AsPlainText -Force + + Update-VSTeamProfile -Name test -SecurePersonalAccessToken $password + + It 'Should update profile' { + Assert-VerifiableMock + } + } + + Context 'Update-VSTeamProfile with PAT' { + Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*OjY3ODkxMA==*" -and $Value -like "*https://dev.azure.com/test*" -and $Value -like "*VSTS*" } + Mock Set-Content { } + Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test/","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } + + Update-VSTeamProfile -Name test -PersonalAccessToken 678910 + + It 'Should update profile' { + Assert-VerifiableMock + } + } + + Context 'Update-VSTeamProfile with old URL' { + Mock Test-Path { return $true } + Mock Get-Content { return '[{"Name":"test","URL":"https://test.visualstudio.com","Type":"Pat","Pat":"12345","Version":"VSTS"}]' } + Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*OjY3ODkxMA==*" -and $Value -like "*https://dev.azure.com/test*" -and $Value -like "*VSTS*" } + Mock Set-Content { } + + Update-VSTeamProfile -Name test -PersonalAccessToken 678910 + + It 'Should update profile with new URL' { + Assert-VerifiableMock + } + } +} \ No newline at end of file diff --git a/unit/test/Update-VSTeamProject.Tests.ps1 b/unit/test/Update-VSTeamProject.Tests.ps1 new file mode 100644 index 000000000..b7d1d6e74 --- /dev/null +++ b/unit/test/Update-VSTeamProject.Tests.ps1 @@ -0,0 +1,109 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinition.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Public/Get-VSTeamQueue.ps1" +. "$here/../../Source/Public/Remove-VSTeamAccount.ps1" +. "$here/../../Source/Public/Get-VSTeamBuildDefinition.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamProject' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" + . "$PSScriptRoot\mocks\mockProcessNameDynamicParam.ps1" + + Mock _getApiVersion { return '1.0-unitTests' } + Mock _getInstance { return 'https://dev.azure.com/test' } + + $singleResult = [PSCustomObject]@{ + name = 'Test' + description = '' + url = '' + id = '123-5464-dee43' + state = '' + visibility = '' + revision = 0 + defaultTeam = [PSCustomObject]@{ } + _links = [PSCustomObject]@{ } + } + + Context 'Update-VSTeamProject' { + Mock Invoke-RestMethod { + # Write-Host $args + return $singleResult + } + Mock Invoke-RestMethod { + # Write-Host "single result $args" + return $singleResult + } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$(_getApiVersion Core)" } + Mock Invoke-RestMethod { + # Write-Host "in progress $args" + return @{status = 'inProgress'; url = 'https://someplace.com' } + } -ParameterFilter { $Method -eq 'Patch' } + Mock _trackProjectProgress { + # Write-Host "in progress $args" + } + Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Testing123?api-version=$(_getApiVersion Core)" } + + It 'with no op by id should not call Invoke-RestMethod' { + Update-VSTeamProject -id '123-5464-dee43' + + Assert-MockCalled Invoke-RestMethod -Exactly 0 + } + + It 'with newName should change name' { + Update-VSTeamProject -ProjectName Test -newName Testing123 -Force + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$(_getApiVersion Core)" } + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Method -eq 'Patch' -and $Body -eq '{"name": "Testing123"}' } + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Testing123?api-version=$(_getApiVersion Core)" } + } + + It 'with newDescription should change description' { + Update-VSTeamProject -ProjectName Test -newDescription Testing123 -Force + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 2 -Scope It -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$(_getApiVersion Core)" } + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Method -eq 'Patch' -and $Body -eq '{"description": "Testing123"}' } + } + + It 'with new name and description should not call Invoke-RestMethod' { + Update-VSTeamProject -ProjectName Test -newName Testing123 -newDescription Testing123 -Force + + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$(_getApiVersion Core)" } + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Method -eq 'Patch' } + Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Testing123?api-version=$(_getApiVersion Core)" } + } + } +} \ No newline at end of file diff --git a/unit/test/Update-VSTeamPullRequest.Tests.ps1 b/unit/test/Update-VSTeamPullRequest.Tests.ps1 index 6693bdc73..37674888b 100644 --- a/unit/test/Update-VSTeamPullRequest.Tests.ps1 +++ b/unit/test/Update-VSTeamPullRequest.Tests.ps1 @@ -1,87 +1,96 @@ Set-StrictMode -Version Latest -InModuleScope VSTeam { +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") - Describe 'Pull Requests' { - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamUser.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamUser.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/$sut" +#endregion - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable +Describe 'Pull Requests' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Core = '' - [VSTeamVersions]::Git = '5.1-preview' - [VSTeamVersions]::Graph = '5.0' + Mock _getInstance { return 'https://dev.azure.com/test' } - $result = Get-Content "$PSScriptRoot\sampleFiles\updatePullRequestResponse.json" -Raw | ConvertFrom-Json - $userSingleResult = Get-Content "$PSScriptRoot\sampleFiles\users.single.json" -Raw | ConvertFrom-Json + # You have to set the version or the api-version will not be added when versions = '' + Mock _getApiVersion { return '1.0-unitTest' } -ParameterFilter { $Service -eq 'Git' -or $Service -eq 'Graph' } - Context 'Update-VSTeamPullRequest' { + $result = Get-Content "$PSScriptRoot\sampleFiles\updatePullRequestResponse.json" -Raw | ConvertFrom-Json + $userSingleResult = Get-Content "$PSScriptRoot\sampleFiles\users.single.json" -Raw | ConvertFrom-Json - It 'Update-VSTeamPullRequest to Draft' { - Mock Invoke-RestMethod { return $result } + Context 'Update-VSTeamPullRequest' { - Update-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -PullRequestId 19543 -Draft -Force + It 'Update-VSTeamPullRequest to Draft' { + Mock Invoke-RestMethod { return $result } - Assert-MockCalled Invoke-RestMethod -Scope It -ParameterFilter { - $Method -eq 'Patch' -and - $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and - $Uri -like "*pullrequests/19543*" -and - $Body -eq '{"isDraft": true }' - } + Update-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -PullRequestId 19543 -Draft -Force + + Assert-MockCalled Invoke-RestMethod -Scope It -ParameterFilter { + $Method -eq 'Patch' -and + $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and + $Uri -like "*pullrequests/19543*" -and + $Body -eq '{"isDraft": true }' } + } - It 'Update-VSTeamPullRequest to Published' { - Mock Invoke-RestMethod { return $result } + It 'Update-VSTeamPullRequest to Published' { + Mock Invoke-RestMethod { return $result } - Update-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -PullRequestId 19543 -Force + Update-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -PullRequestId 19543 -Force - Assert-MockCalled Invoke-RestMethod -Scope It -ParameterFilter { - $Method -eq 'Patch' -and - $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and - $Uri -like "*pullrequests/19543*" -and - $Body -eq '{"isDraft": false }' - } + Assert-MockCalled Invoke-RestMethod -Scope It -ParameterFilter { + $Method -eq 'Patch' -and + $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and + $Uri -like "*pullrequests/19543*" -and + $Body -eq '{"isDraft": false }' } + } - It 'Update-VSTeamPullRequest to set status to abandoned' { - Mock Invoke-RestMethod { return $result } + It 'Update-VSTeamPullRequest to set status to abandoned' { + Mock Invoke-RestMethod { return $result } - Update-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -PullRequestId 19543 -Status abandoned -Force + Update-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -PullRequestId 19543 -Status abandoned -Force - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Patch' -and - $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and - $Uri -like "*pullrequests/19543*" -and - $Body -eq '{"status": "abandoned"}' - } + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Patch' -and + $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and + $Uri -like "*pullrequests/19543*" -and + $Body -eq '{"status": "abandoned"}' } + } - It 'Update-VSTeamPullRequest to set to enable auto complete' { - Mock Invoke-RestMethod { return $userSingleResult } + It 'Update-VSTeamPullRequest to set to enable auto complete' { + Mock Invoke-RestMethod { return $userSingleResult } - $user = Get-VSTeamUser -Descriptor "aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj" + $user = Get-VSTeamUser -Descriptor "aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj" - Mock Invoke-RestMethod { return $result } - Update-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -PullRequestId 19543 -EnableAutoComplete -AutoCompleteIdentity $user -Force + Mock Invoke-RestMethod { return $result } + Update-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -PullRequestId 19543 -EnableAutoComplete -AutoCompleteIdentity $user -Force - Assert-MockCalled Invoke-RestMethod -Scope It -ParameterFilter { - $Method -eq 'Patch' -and - $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and - $Uri -like "*pullrequests/19543*" -and - $Body -eq '{"autoCompleteSetBy": "aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj"}' - } + Assert-MockCalled Invoke-RestMethod -Scope It -ParameterFilter { + $Method -eq 'Patch' -and + $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and + $Uri -like "*pullrequests/19543*" -and + $Body -eq '{"autoCompleteSetBy": "aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj"}' } + } - It 'Update-VSTeamPullRequest to set to disable auto complete' { - Mock Invoke-RestMethod { return $result } - Update-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -PullRequestId 19543 -DisableAutoComplete -Force + It 'Update-VSTeamPullRequest to set to disable auto complete' { + Mock Invoke-RestMethod { return $result } + Update-VSTeamPullRequest -RepositoryId "45df2d67-e709-4557-a7f9-c6812b449277" -PullRequestId 19543 -DisableAutoComplete -Force - Assert-MockCalled Invoke-RestMethod -Scope It -ParameterFilter { - $Method -eq 'Patch' -and - $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and - $Uri -like "*pullrequests/19543*" -and - $Body -eq '{"autoCompleteSetBy": null}' - } + Assert-MockCalled Invoke-RestMethod -Scope It -ParameterFilter { + $Method -eq 'Patch' -and + $Uri -like "*repositories/45df2d67-e709-4557-a7f9-c6812b449277/*" -and + $Uri -like "*pullrequests/19543*" -and + $Body -eq '{"autoCompleteSetBy": null}' } } } diff --git a/unit/test/Update-VSTeamRelease.Tests.ps1 b/unit/test/Update-VSTeamRelease.Tests.ps1 index bb22b8d96..079a1fa53 100644 --- a/unit/test/Update-VSTeamRelease.Tests.ps1 +++ b/unit/test/Update-VSTeamRelease.Tests.ps1 @@ -1,52 +1,59 @@ Set-StrictMode -Version Latest -# Loading System.Web avoids issues finding System.Web.HttpUtility -Add-Type -AssemblyName 'System.Web' - -InModuleScope VSTeam { - Describe 'Releases' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - [VSTeamVersions]::Release = '1.0-unittest' +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamRelease.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamRelease' { + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unittest' } -ParameterFilter { $Service -eq 'Release' } - $singleResult = [PSCustomObject]@{ - environments = [PSCustomObject]@{} - variables = [PSCustomObject]@{ - BrowserToUse = [PSCustomObject]@{ - value = "phantomjs" - } - } - _links = [PSCustomObject]@{ - self = [PSCustomObject]@{} - web = [PSCustomObject]@{} + $singleResult = [PSCustomObject]@{ + environments = [PSCustomObject]@{ } + variables = [PSCustomObject]@{ + BrowserToUse = [PSCustomObject]@{ + value = "phantomjs" } } - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" + _links = [PSCustomObject]@{ + self = [PSCustomObject]@{ } + web = [PSCustomObject]@{ } } + } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - Context 'Update-VSTeamRelease' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { - return $singleResult - } + Context 'Update-VSTeamRelease' { + Mock _useWindowsAuthenticationOnPremise { return $true } + Mock Invoke-RestMethod { + return $singleResult + } - It 'should return releases' { - $r = Get-VSTeamRelease -ProjectName project -Id 15 + It 'should return releases' { + $r = Get-VSTeamRelease -ProjectName project -Id 15 - $r.variables | Add-Member NoteProperty temp(@{value = 'temp'}) + $r.variables | Add-Member NoteProperty temp(@{value = 'temp' }) - Update-VSTeamRelease -ProjectName project -Id 15 -Release $r + Update-VSTeamRelease -ProjectName project -Id 15 -Release $r - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Put' -and - $Body -ne $null -and - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/15?api-version=$([VSTeamVersions]::Release)" - } - } - } + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Put' -and + $Body -ne $null -and + $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/15?api-version=$(_getApiVersion Release)" + } + } } } \ No newline at end of file diff --git a/unit/test/Update-VSTeamServiceEndpoint.Tests.ps1 b/unit/test/Update-VSTeamServiceEndpoint.Tests.ps1 new file mode 100644 index 000000000..1ac6521a3 --- /dev/null +++ b/unit/test/Update-VSTeamServiceEndpoint.Tests.ps1 @@ -0,0 +1,57 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Add-VSTeamServiceEndpoint.ps1" +. "$here/../../Source/Public/Get-VSTeamServiceEndpoint.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamServiceEndpoint' { + Context 'Update-VSTeamServiceEndpoint' { + Mock _hasProjectCacheExpired { return $false } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'ServiceFabricEndpoint' } + + Mock Write-Progress + Mock Invoke-RestMethod { return @{id = '23233-2342' } } -ParameterFilter { $Method -eq 'Get' } + Mock Invoke-RestMethod { return @{id = '23233-2342' } } -ParameterFilter { $Method -eq 'Put' } + Mock Invoke-RestMethod { + + # This $i is in the module. Because we use InModuleScope + # we can see it + if ($iTracking -gt 9) { + return [PSCustomObject]@{ + isReady = $true + operationStatus = [PSCustomObject]@{state = 'Ready' } + } + } + + return [PSCustomObject]@{ + isReady = $false + createdBy = [PSCustomObject]@{ } + authorization = [PSCustomObject]@{ } + data = [PSCustomObject]@{ } + operationStatus = [PSCustomObject]@{state = 'InProgress' } + } + } + + It 'should update Serviceendpoint' { + Update-VSTeamServiceEndpoint -projectName 'project' -id '23233-2342' ` + -object @{ key = 'value' } + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Put' + } + } + } +} \ No newline at end of file diff --git a/unit/test/Update-VSTeamTaskGroup.Tests.ps1 b/unit/test/Update-VSTeamTaskGroup.Tests.ps1 new file mode 100644 index 000000000..d9c7fd797 --- /dev/null +++ b/unit/test/Update-VSTeamTaskGroup.Tests.ps1 @@ -0,0 +1,63 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamTaskGroup.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +$taskGroupsJson = "$PSScriptRoot\sampleFiles\taskGroups.json" +$taskGroupJson = "$PSScriptRoot\sampleFiles\taskGroup.json" + +Describe 'VSTeamTaskGroup' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + # Set the account to use for testing. A normal user would do this + # using the Set-VSTeamAccount function. + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'TaskGroups' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/project*" } + + BeforeAll { + $projectName = "project" + $taskGroupJsonAsString = Get-Content $taskGroupJson -Raw + } + + Context 'Update-VSTeamTaskGroup' { + Mock Invoke-RestMethod { + return Get-Content $taskGroupJson | ConvertFrom-Json + } + + It 'should update a task group using body param' { + $taskGroupToUpdate = Get-VSTeamTaskGroup -Name "For Unit Tests" -ProjectName $projectName + + Update-VSTeamTaskGroup -ProjectName $projectName -Body $taskGroupJsonAsString -Id $taskGroupToUpdate.id + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups/$($taskGroupToUpdate.id)?api-version=$(_getApiVersion TaskGroups)" -and + $Body -eq $taskGroupJsonAsString -and + $Method -eq "Put" + } + } + + It 'should update a task group using infile param' { + $taskGroupToUpdate = Get-VSTeamTaskGroup -Name "For Unit Tests" -ProjectName $projectName + + Update-VSTeamTaskGroup -ProjectName $projectName -InFile $taskGroupJson -Id $taskGroupToUpdate.id + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups/$($taskGroupToUpdate.id)?api-version=$(_getApiVersion TaskGroups)" -and + $InFile -eq $taskGroupJson -and + $Method -eq "Put" + } + } + } +} \ No newline at end of file diff --git a/unit/test/Update-VSTeamUserEntitlement.Tests.ps1 b/unit/test/Update-VSTeamUserEntitlement.Tests.ps1 new file mode 100644 index 000000000..535d38f60 --- /dev/null +++ b/unit/test/Update-VSTeamUserEntitlement.Tests.ps1 @@ -0,0 +1,97 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamUserEntitlement.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe "VSTeamUserEntitlement" { + Context 'Update-VSTeamUserEntitlement' { + Mock _getApiVersion { return 'VSTS' } + # This will cause the call the _getProject to be skipped + Mock _hasProjectCacheExpired { return $false } + Mock _getInstance { return 'https://dev.azure.com/test' } + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'MemberEntitlementManagement' } + + # Update Call + Mock _callAPI -ParameterFilter { $Method -eq 'Patch' } + + # Get-VSTeamUserEntitlement by email + Mock _callAPI { + return [PSCustomObject]@{ + members = [PSCustomObject]@{ + accessLevel = [PSCustomObject]@{ + accountLicenseType = "Stakeholder" + licensingSource = "msdn" + msdnLicenseType = "enterprise" + } + email = 'test@user.com' + userName = 'Test User' + id = '00000000-0000-0000-0000-000000000000' + } + } + } + + It 'Should update a user' { + Update-VSTeamUserEntitlement -License 'Stakeholder' -LicensingSource msdn -MSDNLicenseType enterprise -Email 'test@user.com' -Force + + Assert-MockCalled _callAPI -Exactly -Times 1 -Scope It -ParameterFilter { + $Method -eq 'Patch' -and + $subDomain -eq 'vsaex' -and + $id -eq '00000000-0000-0000-0000-000000000000' -and + $resource -eq 'userentitlements' -and + $version -eq $(_getApiVersion MemberEntitlementManagement) + } + } + + It 'update user with invalid email should throw' { + { Update-VSTeamUserEntitlement -Email 'not@found.com' -License 'Express' -Force } | Should Throw + } + + It 'update user with invalid id should throw' { + { Update-VSTeamUserEntitlement -Id '11111111-0000-0000-0000-000000000000' -License 'Express' -Force } | Should Throw + } + + # Context 'Add-VSTeamUserEntitlement' { + # $obj = @{ + # accessLevel = @{ + # accountLicenseType = 'earlyAdopter' + # licensingSource = 'msdn' + # msdnLicenseType = 'enterprise' + # } + # user = @{ + # principalName = 'test@user.com' + # subjectKind = 'user' + # } + # projectEntitlements = @{ + # group = @{ + # groupType = 'ProjectContributor' + # } + # projectRef = @{ + # id = $null + # } + # } + # } + + # $expected = $obj | ConvertTo-Json + + # Mock _callAPI -ParameterFilter { + # $Method -eq 'Post' -and + # $Body -eq $expected + # } + + # Add-VSTeamUserEntitlement -License earlyAdopter -LicensingSource msdn -MSDNLicenseType enterprise -Email 'test@user.com' + + # It 'Should add a user' { + # Assert-VerifiableMock + # } + # } + } +} \ No newline at end of file diff --git a/unit/test/Update-VSTeamVariableGroup.Tests.ps1 b/unit/test/Update-VSTeamVariableGroup.Tests.ps1 new file mode 100644 index 000000000..9c29d0f9a --- /dev/null +++ b/unit/test/Update-VSTeamVariableGroup.Tests.ps1 @@ -0,0 +1,109 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Set-VSTeamAPIVersion.ps1" +. "$here/../../Source/Public/Get-VSTeamVariableGroup.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamVariableGroup' { + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/project*" } + + Context 'Update-VSTeamVariableGroup' { + Context 'Services' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + $sampleFileVSTS = $(Get-Content "$PSScriptRoot\sampleFiles\variableGroupSamples.json" | ConvertFrom-Json) + + Mock _getApiVersion { return 'VSTS' } + Mock _getApiVersion { return '5.0-preview.1-unitTests' } -ParameterFilter { $Service -eq 'VariableGroups' } + + Mock _getInstance { return 'https://dev.azure.com/test' } + + Mock Invoke-RestMethod { return $sampleFileVSTS.value[0] } + + It 'should update an exisiting Variable Group' { + $testParameters = @{ + ProjectName = "project" + Id = 1 + Name = "TestVariableGroup1" + Description = "A test variable group" + Type = "Vsts" + Variables = @{ + key1 = @{ + value = "value" + } + key2 = @{ + value = "" + isSecret = $true + } + } + } + + Update-VSTeamVariableGroup @testParameters + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/variablegroups/$($testParameters.id)?api-version=$(_getApiVersion VariableGroups)" -and + $Method -eq 'Put' + } + } + + It "should update an existing var group when passing the json as the body" { + $body = $sampleFileVSTS + $projName = "project" + $id = "1" + Update-VSTeamVariableGroup -Body $body -ProjectName $projName -Id $id + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "https://dev.azure.com/test/$projName/_apis/distributedtask/variablegroups/$($id)?api-version=$(_getApiVersion VariableGroups)" -and + $Method -eq 'Put' + } + } + } + + Context 'Server' { + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + $sampleFile2017 = $(Get-Content "$PSScriptRoot\sampleFiles\variableGroupSamples2017.json" | ConvertFrom-Json) + + Mock _getApiVersion { return 'TFS2017' } + Mock _getApiVersion { return '3.2-preview.1-unitTests' } + Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable + + Mock Invoke-RestMethod { return $sampleFile2017.value[0] } + + It 'should update an exisiting Variable Group' { + $testParameters = @{ + ProjectName = "project" + id = 1 + Name = "TestVariableGroup1" + Description = "A test variable group" + Variables = @{ + key1 = @{ + value = "value" + } + key2 = @{ + value = "" + isSecret = $true + } + } + } + + Update-VSTeamVariableGroup @testParameters + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/distributedtask/variablegroups/$($testParameters.id)?api-version=$(_getApiVersion VariableGroups)" -and + $Method -eq 'Put' + } + } + } + } +} diff --git a/unit/test/Update-VSTeamWorkItem.Tests.ps1 b/unit/test/Update-VSTeamWorkItem.Tests.ps1 new file mode 100644 index 000000000..9e1cf94b0 --- /dev/null +++ b/unit/test/Update-VSTeamWorkItem.Tests.ps1 @@ -0,0 +1,132 @@ +Set-StrictMode -Version Latest + +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/$sut" +#endregion + +Describe 'VSTeamWorkItem' { + Mock _getInstance { return 'https://dev.azure.com/test' } + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { + $Uri -like "*_apis/projects*" + } + + . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" + + $obj = @{ + id = 47 + rev = 1 + url = "https://dev.azure.com/test/_apis/wit/workItems/47" + } + + Context 'Update-VSTeamWorkItem' { + Mock Invoke-RestMethod { + # If this test fails uncomment the line below to see how the mock was called. + # Write-Host $args + + return $obj + } + + It 'Without Default Project should update work item' { + $Global:PSDefaultParameterValues.Remove("*:projectName") + Update-VSTeamWorkItem -Id 1 -Title Test -Force + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Patch' -and + $Body -like '`[*' -and # Make sure the body is an array + $Body -like '*`]' -and # Make sure the body is an array + $ContentType -eq 'application/json-patch+json' -and + $Uri -eq "https://dev.azure.com/test/_apis/wit/workitems/1?api-version=$(_getApiVersion Core)" + } + } + + It 'With Default Project should update work item' { + $Global:PSDefaultParameterValues["*:projectName"] = 'test' + Update-VSTeamWorkItem 1 -Title Test1 -Description Testing -Force + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Patch' -and + $Body -like '`[*' -and # Make sure the body is an array + $Body -like '*Test1*' -and + $Body -like '*Testing*' -and + $Body -like '*/fields/System.Title*' -and + $Body -like '*/fields/System.Description*' -and + $Body -like '*`]' -and # Make sure the body is an array + $ContentType -eq 'application/json-patch+json' -and + $Uri -eq "https://dev.azure.com/test/_apis/wit/workitems/1?api-version=$(_getApiVersion Core)" + } + } + + It 'With Default Project should update work item with 2 parameters and additional properties' { + $Global:PSDefaultParameterValues["*:projectName"] = 'test' + + $additionalFields = @{"System.Tags" = "TestTag"; "System.AreaPath" = "Project\\MyPath" } + Update-VSTeamWorkItem 1 -Title Test1 -Description Testing -AdditionalFields $additionalFields + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Patch' -and + $Body -like '`[*' -and # Make sure the body is an array + $Body -like '*Test1*' -and + $Body -like '*Testing*' -and + $Body -like '*/fields/System.Title*' -and + $Body -like '*/fields/System.Description*' -and + $Body -like '*/fields/System.Tags*' -and + $Body -like '*/fields/System.AreaPath*' -and + $Body -like '*`]' -and # Make sure the body is an array + $ContentType -eq 'application/json-patch+json' -and + $Uri -eq "https://dev.azure.com/test/_apis/wit/workitems/1?api-version=$(_getApiVersion Core)" + } + } + + It 'With Default Project should update work item only with 1 parameter and additional properties' { + $Global:PSDefaultParameterValues["*:projectName"] = 'test' + + $additionalFields = @{"System.Tags" = "TestTag"; "System.AreaPath" = "Project\\MyPath" } + Update-VSTeamWorkItem 1 -Title Test1 -AdditionalFields $additionalFields + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Patch' -and + $Body -like '`[*' -and # Make sure the body is an array + $Body -like '*Test1*' -and + $Body -like '*/fields/System.Title*' -and + $Body -like '*/fields/System.Tags*' -and + $Body -like '*/fields/System.AreaPath*' -and + $Body -like '*`]' -and # Make sure the body is an array + $ContentType -eq 'application/json-patch+json' -and + $Uri -eq "https://dev.azure.com/test/_apis/wit/workitems/1?api-version=$(_getApiVersion Core)" + } + } + + It 'With Default Project should update work item only with additional properties' { + $Global:PSDefaultParameterValues["*:projectName"] = 'test' + + $additionalFields = @{"System.Tags" = "TestTag"; "System.AreaPath" = "Project\\MyPath" } + Update-VSTeamWorkItem 1 -AdditionalFields $additionalFields + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Patch' -and + $Body -like '`[*' -and # Make sure the body is an array + $Body -like '*/fields/System.Tags*' -and + $Body -like '*/fields/System.AreaPath*' -and + $Body -like '*`]' -and # Make sure the body is an array + $ContentType -eq 'application/json-patch+json' -and + $Uri -eq "https://dev.azure.com/test/_apis/wit/workitems/1?api-version=$(_getApiVersion Core)" + } + } + + It 'With Default Project should throw exception when adding existing parameters to additional properties' { + $Global:PSDefaultParameterValues["*:projectName"] = 'test' + + $additionalFields = @{"System.Title" = "Test1"; "System.AreaPath" = "Project\\TestPath" } + { Update-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test1 -Description Testing -AdditionalFields $additionalFields } | Should Throw + } + } +} \ No newline at end of file diff --git a/unit/test/VSTeamAccount.Tests.ps1 b/unit/test/VSTeamAccount.Tests.ps1 new file mode 100644 index 000000000..c9e4611da --- /dev/null +++ b/unit/test/VSTeamAccount.Tests.ps1 @@ -0,0 +1,109 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamPools.ps1" +. "$here/../../Source/Classes/VSTeamExtensions.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamFeeds.ps1" +. "$here/../../Source/Classes/VSTeamGroups.ps1" +. "$here/../../Source/Classes/VSTeamUsers.ps1" +. "$here/../../Source/Classes/VSTeamPermissions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamPool.ps1" +. "$here/../../Source/Public/Get-VSTeamProject.ps1" +. "$here/../../Source/Classes/$sut" +#endregion + +Describe "VSTeamAccount" { + Context 'Constructor' { + Mock _getApiVersion { return '1.0-unitTests' } -ParameterFilter { $Service -eq 'Graph' -or $Service -eq 'Packaging' } + Mock Get-VSTeamProject { return [VSTeamProject]::new([PSCustomObject]@{ + name = 'TestProject' + description = '' + url = '' + id = '123 - 5464-dee43' + state = '' + visibility = '' + revision = 0 + defaultTeam = [PSCustomObject]@{ } + _links = [PSCustomObject]@{ } + } + ) + } + + Mock Get-VSTeamPool { + return [PSCustomObject]@{ + name = 'Default' + id = 1 + size = 1 + createdBy = [PSCustomObject]@{ } + administratorsGroup = [PSCustomObject]@{ } + serviceAccountsGroup = [PSCustomObject]@{ } + } + } + + $account = [VSTeamAccount]::new('TestAccount') + + It 'Should create VSTeamAccount' { + $account | Should Not Be $null + } + + # Skip 0 because that will be Agent Pools + # Skip 1 because that will be Extensions + # Skip 2 because that will be Feeds + # Skip 3 because that will be Permissions + $project = $account.GetChildItem()[4] + + It 'Should return projects' { + $project | Should Not Be $null + } + + $actual = $project.GetChildItem() + + It 'Should return builds, releases, repositories and teams' { + $actual | Should Not Be $null + $actual[0].Name | Should Be 'Build Definitions' + $actual[0].ProjectName | Should Be 'TestProject' + $actual[1].Name | Should Be 'Builds' + $actual[1].ProjectName | Should Be 'TestProject' + $actual[2].Name | Should Be 'Queues' + $actual[2].ProjectName | Should Be 'TestProject' + $actual[3].Name | Should Be 'Release Definitions' + $actual[3].ProjectName | Should Be 'TestProject' + $actual[4].Name | Should Be 'Releases' + $actual[4].ProjectName | Should Be 'TestProject' + $actual[5].Name | Should Be 'Repositories' + $actual[5].ProjectName | Should Be 'TestProject' + $actual[6].Name | Should Be 'Teams' + $actual[6].ProjectName | Should Be 'TestProject' + } + } +} \ No newline at end of file diff --git a/unit/test/VSTeamBuildDefinitions.Tests.ps1 b/unit/test/VSTeamBuildDefinitions.Tests.ps1 new file mode 100644 index 000000000..c5ff3d307 --- /dev/null +++ b/unit/test/VSTeamBuildDefinitions.Tests.ps1 @@ -0,0 +1,161 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/$sut" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinition.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamBuildDefinition.ps1" +#endregion + +Describe "VSTeamBuildDefinitions" { + $buildDefResultsAzD = Get-Content "$PSScriptRoot\sampleFiles\buildDefAzD.json" -Raw | ConvertFrom-Json + $buildDefResults2017 = Get-Content "$PSScriptRoot\sampleFiles\buildDef2017.json" -Raw | ConvertFrom-Json + $buildDefResults2018 = Get-Content "$PSScriptRoot\sampleFiles\buildDef2018.json" -Raw | ConvertFrom-Json + $buildDefResultsVSTS = Get-Content "$PSScriptRoot\sampleFiles\buildDefvsts.json" -Raw | ConvertFrom-Json + $buildDefResultsyaml = Get-Content "$PSScriptRoot\sampleFiles\buildDefyaml.json" -Raw | ConvertFrom-Json + + # Mock the call to Get-Projects by the dynamic parameter for ProjectName + Mock Invoke-RestMethod { return @() } -ParameterFilter { $Uri -like "*_apis/projects*" } + + Context "Server 2017 Constructor" { + Mock Get-VSTeamBuildDefinition { return @([VSTeamBuildDefinition]::new($buildDefResults2017.value[0], 'TestProject')) } + + $buildDefinitions = [VSTeamBuildDefinitions]::new('Build Definitions', 'TestProject') + + It 'Should create Build definitions' { + $buildDefinitions | Should Not be $null + } + + $hasSteps = $buildDefinitions.GetChildItem()[0] + + It 'Should have Steps' { + $hasSteps | Should Not Be $null + } + + $steps = $hasSteps.GetChildItem() + + It 'Should parse steps' { + $steps.Length | Should Be 10 + } + } + + Context 'Server 2018 Constructor' { + Mock Get-VSTeamBuildDefinition { return @([VSTeamBuildDefinition]::new($buildDefResults2018.value[0], 'TestProject')) } + + $buildDefinitions = [VSTeamBuildDefinitions]::new('Build Definitions', 'TestProject') + + It 'Should create Build definitions' { + $buildDefinitions | Should Not be $null + } + + $hasSteps = $buildDefinitions.GetChildItem()[0] + + It 'Should have Steps' { + $hasSteps | Should Not Be $null + } + + $steps = $hasSteps.GetChildItem() + + It 'Should parse steps' { + $steps.Length | Should Be 9 + } + } + + Context 'Services' { + Mock Get-VSTeamBuildDefinition { + return @( + [VSTeamBuildDefinition]::new($buildDefResultsVSTS.value[0], 'TestProject'), + [VSTeamBuildDefinition]::new($buildDefResultsyaml.value[0], 'TestProject'), + [VSTeamBuildDefinition]::new($buildDefResultsAzD.value[0], 'TestProject') + ) + } + + $buildDefinitions = [VSTeamBuildDefinitions]::new('Build Definitions', 'TestProject') + + It 'Should create Build definitions' { + $buildDefinitions | Should Not be $null + } + + $VSTeamBuildDefinitionWithPhases = $buildDefinitions.GetChildItem()[0] + + It 'Should parse phases' { + $VSTeamBuildDefinitionWithPhases.Process.Phases.Length | Should Be 1 + } + + It 'Should show steps in tostring' { + $VSTeamBuildDefinitionWithPhases.Process.ToString() | Should Be 'Number of phases: 1' + } + + $process = $VSTeamBuildDefinitionWithPhases.GetChildItem()[0] + + It 'Should return process' { + $process | Should Not Be $null + } + + $steps = $process.GetChildItem() + + It 'Should parse steps' { + $steps.Length | Should Be 9 + } + + $yamlBuild = $buildDefinitions.GetChildItem()[1] + $yamlFile = $yamlBuild.GetChildItem() + + It 'Should have yamlFilename' { + $yamlFile | Should Be '.vsts-ci.yml' + } + + $version5Build = $buildDefinitions.GetChildItem()[2] + + It 'Should have jobCancelTimeoutInMinutes' { + $version5Build.JobCancelTimeoutInMinutes | Should Be '5' + } + } + + Context 'Build Definitions' { + Mock Get-VSTeamBuildDefinition { return @( + [VSTeamBuildDefinition]::new(@{ }, 'TestProject'), + [VSTeamBuildDefinition]::new(@{ }, 'TestProject') + ) + } + + $buildDefinitions = [VSTeamBuildDefinitions]::new('Build Definitions', 'TestProject') + + It 'Should create Build definitions' { + $buildDefinitions | Should Not be $null + } + + # $VSTeamBuildDefinitionWithPhases = $buildDefinitions.GetChildItem()[0] + + # $yamlBuild = $buildDefinitions.GetChildItem()[1] + } +} \ No newline at end of file diff --git a/unit/test/VSTeamBuilds.Tests.ps1 b/unit/test/VSTeamBuilds.Tests.ps1 new file mode 100644 index 000000000..77a0eb648 --- /dev/null +++ b/unit/test/VSTeamBuilds.Tests.ps1 @@ -0,0 +1,68 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamBuild.ps1" +. "$here/../../Source/Classes/$sut" +#endregion + +Describe "VSTeamBuilds" { + Context "Constructor" { + Mock Get-VSTeamBuild { return @([PSCustomObject]@{ + id = 1 + description = '' + buildNumber = '1' + status = 'completed' + result = 'succeeded' + startTime = Get-Date + lastChangedBy = [PSCustomObject]@{ + id = '' + displayName = 'Test User' + uniqueName = 'test@email.com' + } + requestedBy = [PSCustomObject]@{ + id = '' + displayName = 'Test User' + uniqueName = 'test@email.com' + } + requestedFor = [PSCustomObject]@{ + id = '' + displayName = 'Test User' + uniqueName = 'test@email.com' + } + definition = [PSCustomObject]@{ + name = 'Test CI' + fullname = 'Test CI' + } + project = [PSCustomObject]@{ + name = 'Test Project' + } + } + ) + } + + $builds = [VSTeamBuilds]::new('TestBuild', 'TestProject') + + It 'Should create Builds' { + $builds | Should Not Be $null + } + + $build = $builds.GetChildItem() + + It 'Should return build' { + $build | Should Not Be $null + } + } +} \ No newline at end of file diff --git a/unit/test/VSTeamFeeds.Tests.ps1 b/unit/test/VSTeamFeeds.Tests.ps1 new file mode 100644 index 000000000..63c64f649 --- /dev/null +++ b/unit/test/VSTeamFeeds.Tests.ps1 @@ -0,0 +1,40 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamFeed.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamFeed.ps1" +. "$here/../../Source/Classes/$sut" +#endregion + +Describe "VSTeamFeeds" { + Context 'Constructor' { + $feedResults = Get-Content "$PSScriptRoot\sampleFiles\feeds.json" -Raw | ConvertFrom-Json + $singleResult = $feedResults.value[0] + + Mock Get-VSTeamFeed { + return [VSTeamFeed]::new($singleResult) + } + + $target = [VSTeamFeeds]::new('Feeds') + + It 'Should create Feeds' { + $target | Should Not Be $null + } + + $feed = $target.GetChildItem()[0] + + It 'Should return feed' { + $feed | Should Not Be $null + } + } +} \ No newline at end of file diff --git a/unit/test/VSTeamPermissions.Tests.ps1 b/unit/test/VSTeamPermissions.Tests.ps1 new file mode 100644 index 000000000..7406b309b --- /dev/null +++ b/unit/test/VSTeamPermissions.Tests.ps1 @@ -0,0 +1,44 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamGroups.ps1" +. "$here/../../Source/Classes/VSTeamUsers.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamUser.ps1" +. "$here/../../Source/Public/Get-VSTeamGroup.ps1" +. "$here/../../Source/Classes/$sut" +#endregion + +Describe "VSTeamPermissions" { + Context "Constructor" { + Mock Get-VSTeamUser { return [VSTeamGroup]::new(@{ }) } + Mock Get-VSTeamGroup { return [VSTeamGroup]::new(@{ }) } + + $permissions = [VSTeamPermissions]::new('Permissions') + + It 'Should create Permissions' { + $permissions | Should Not Be $null + $permissions.GetChildItem().Count | Should Be 2 + } + + $groups = $permissions.GetChildItem()[0] + $users = $permissions.GetChildItem()[1] + + It 'Should return groups' { + $groups | Should Not Be $null + } + + It 'Should return users' { + $users | Should Not Be $null + } + } +} \ No newline at end of file diff --git a/unit/test/VSTeamPools.Tests.ps1 b/unit/test/VSTeamPools.Tests.ps1 new file mode 100644 index 000000000..dbcf51ba8 --- /dev/null +++ b/unit/test/VSTeamPools.Tests.ps1 @@ -0,0 +1,77 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamAgent.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamAgent.ps1" +. "$here/../../Source/Public/Get-VSTeamPool.ps1" +. "$here/../../Source/Classes/$sut" +#endregion + +Describe "VSTeamPools" { + Context 'Constructor' { + Mock Get-VSTeamPool { return [VSTeamPool]::new(@{ + owner = [PSCustomObject]@{ + displayName = 'Test User' + id = '1' + uniqueName = 'test@email.com' + } + createdBy = [PSCustomObject]@{ + displayName = 'Test User' + id = '1' + uniqueName = 'test@email.com' + } + id = 1 + size = 1 + isHosted = $false + Name = 'Default' + } + ) + } + + Mock Get-VSTeamAgent { return [VSTeamAgent]::new(@{ + _links = [PSCustomObject]@{ } + createdOn = '2018-03-28T16:48:58.317Z' + maxParallelism = 1 + id = 102 + enabled = $false + status = 'Online' + version = '1.336.1' + osDescription = 'Linux' + name = 'Test_Agent' + systemCapabilities = [PSCustomObject]@{ } + }, 1 + ) + } + + $target = [VSTeamPools]::new('Agent Pools') + + It 'Should create Agent Pools' { + $target | Should Not Be $null + } + + $pool = $target.GetChildItem()[0] + + It 'Should return pool' { + $pool | Should Not Be $null + } + + $agent = $pool.GetChildItem()[0] + + It 'Should return agent' { + $agent | Should Not Be $null + } + } +} \ No newline at end of file diff --git a/unit/test/VSTeamProject.Tests.ps1 b/unit/test/VSTeamProject.Tests.ps1 new file mode 100644 index 000000000..58a8368ab --- /dev/null +++ b/unit/test/VSTeamProject.Tests.ps1 @@ -0,0 +1,48 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Classes/$sut" +#endregion + +Describe 'VSTeamProject' -Tag 'Classes', 'Unit' { + Context 'ToString' { + $obj = @{ + name = 'Test Project' + id = 1 + description = '' + url = '' + state = '' + revision = '' + visibility = '' + } + + $target = [VSTeamProject]::new($obj) + + It 'should return name' { + $target.ToString() | Should Be 'Test Project' + } + } +} \ No newline at end of file diff --git a/unit/test/VSTeamReleases.Tests.ps1 b/unit/test/VSTeamReleases.Tests.ps1 new file mode 100644 index 000000000..ebc120a32 --- /dev/null +++ b/unit/test/VSTeamReleases.Tests.ps1 @@ -0,0 +1,113 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamRelease.ps1" +. "$here/../../Source/Classes/$sut" +#endregion + +Describe "VSTeamReleases" { + Context 'Constructor' { + Mock Get-VSTeamRelease { return [PSCustomObject]@{ + id = 1 + name = 'Release - 007' + status = 'active' + createdBy = [PSCustomObject]@{ + displayname = 'Test User' + uniqueName = 'test@email.com' + id = '1' + } + modifiedBy = [PSCustomObject]@{ + displayname = 'Test User' + uniqueName = 'test@email.com' + id = '1' + } + requestedFor = [PSCustomObject]@{ + displayname = '' + uniqueName = '' + id = '' + } + createdOn = Get-Date + releaseDefinition = [PSCustomObject]@{ + name = 'Test Release Def' + } + environments = @([PSCustomObject]@{ + id = 1 + name = 'Dev' + status = 'Succeeded' + deploySteps = @([PSCustomObject]@{ + id = 963 + deploymentId = 350 + attempt = 1 + reason = 'automated' + status = 'succeeded' + releaseDeployPhases = @([PSCustomObject]@{ + deploymentJobs = @([PSCustomObject]@{ + tasks = @([PSCustomObject]@{ + name = 'Initialize Job' + status = 'succeeded' + id = 1 + logUrl = '' + } + ) + } + ) + } + ) + } + ) + } + ) + } + } + + $releases = [VSTeamReleases]::new('Releases', 'TestProject') + + It 'Should create Releases directory' { + $releases | Should Not Be $null + } + + It 'Should be named Releases' { + $releases.Name | Should Be 'Releases' + } + + $release = $releases.GetChildItem()[0] + + It 'Should return releases' { + $release | Should Not Be $null + } + + $env = $release.GetChildItem()[0] + + It 'Should return environments' { + $env | Should Not Be $null + } + + $attempt = $env.GetChildItem()[0] + + It 'Should return attempts' { + $attempt | Should Not Be $null + } + + $task = $attempt.GetChildItem()[0] + + It 'Should return tasks' { + $task | Should Not Be $null + } + } +} \ No newline at end of file diff --git a/unit/test/VSTeamRepositories.Tests.ps1 b/unit/test/VSTeamRepositories.Tests.ps1 new file mode 100644 index 000000000..4b1b3709c --- /dev/null +++ b/unit/test/VSTeamRepositories.Tests.ps1 @@ -0,0 +1,95 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamRef.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhaseStep.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcessPhase.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitionProcess.ps1" +. "$here/../../Source/Classes/VSTeamUserEntitlement.ps1" +. "$here/../../Source/Classes/VSTeamPool.ps1" +. "$here/../../Source/Classes/VSTeamQueue.ps1" +. "$here/../../Source/Classes/VSTeamQueues.ps1" +. "$here/../../Source/Classes/VSTeamRepositories.ps1" +. "$here/../../Source/Classes/VSTeamTeams.ps1" +. "$here/../../Source/Classes/VSTeamReleaseDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamBuild.ps1" +. "$here/../../Source/Classes/VSTeamBuilds.ps1" +. "$here/../../Source/Classes/VSTeamTask.ps1" +. "$here/../../Source/Classes/VSTeamAttempt.ps1" +. "$here/../../Source/Classes/VSTeamEnvironment.ps1" +. "$here/../../Source/Classes/VSTeamRelease.ps1" +. "$here/../../Source/Classes/VSTeamReleases.ps1" +. "$here/../../Source/Classes/VSTeamBuildDefinitions.ps1" +. "$here/../../Source/Classes/VSTeamProject.ps1" +. "$here/../../Source/Classes/VSTeamGitRepository.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeamGitRepository.ps1" +. "$here/../../Source/Public/Get-VSTeamGitRef.ps1" +. "$here/../../Source/Classes/$sut" +#endregion + +Describe "VSTeamRepositories" { + Context 'Constructor' { + Mock Get-VSTeamGitRepository { return [VSTeamGitRepository]::new(@{ + id = "fa7b6ac1-0d4c-46cb-8565-8fd49e2031ad" + name = '' + url = '' + defaultBranch = '' + size = '' + remoteUrl = '' + sshUrl = '' + project = [PSCustomObject]@{ + name = 'TestProject' + description = '' + url = '' + id = '123 - 5464-dee43' + state = '' + visibility = '' + revision = 0 + defaultTeam = [PSCustomObject]@{ } + _links = [PSCustomObject]@{ } + } + }, 'TestProject') + } + + Mock Get-VSTeamGitRef { return [VSTeamRef]::new([PSCustomObject]@{ + objectId = '6f365a7143e492e911c341451a734401bcacadfd' + name = 'refs/heads/master' + creator = [PSCustomObject]@{ + displayName = 'Microsoft.VisualStudio.Services.TFS' + id = '1' + uniqueName = 'some@email.com' + } + }, 'TestProject') + } + + $repositories = [VSTeamRepositories]::new('Repositories', 'TestProject') + + It 'Should create Repositories' { + $repositories | Should Not Be $null + } + + $repository = $repositories.GetChildItem()[0] + + It 'Should return repository' { + $repository | Should Not Be $null + } + + $ref = $repository.GetChildItem()[0] + + It 'Should return ref' { + $ref | Should Not Be $null + } + } +} \ No newline at end of file diff --git a/unit/test/VSTeamTeams.Tests.ps1 b/unit/test/VSTeamTeams.Tests.ps1 new file mode 100644 index 000000000..a46e61acc --- /dev/null +++ b/unit/test/VSTeamTeams.Tests.ps1 @@ -0,0 +1,41 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamDirectory.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Classes/VSTeamTeam.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Public/Get-VSTeam.ps1" +. "$here/../../Source/Classes/$sut" +#endregion + +Describe "VSTeamTeams" { + Context "Constructor" { + Mock Get-VSTeam { return [VSTeamTeam]::new(@{ + name = '' + ProjectName = '' + description = '' + id = 1 + }, 'TestProject') } + + $teams = [VSTeamTeams]::new('Teams', 'TestProject') + + It 'Should create Teams' { + $teams | Should Not Be $null + } + + $team = $teams.GetChildItem()[0] + + It 'Should return team' { + $team | Should Not Be $null + } + } +} \ No newline at end of file diff --git a/unit/test/VSTeamUserEntitlement.Tests.ps1 b/unit/test/VSTeamUserEntitlement.Tests.ps1 new file mode 100644 index 000000000..0ccbd4bc8 --- /dev/null +++ b/unit/test/VSTeamUserEntitlement.Tests.ps1 @@ -0,0 +1,30 @@ +Set-StrictMode -Version Latest + +#region include +Import-Module SHiPS + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamLeaf.ps1" +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Private/applyTypes.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Classes/$sut" +#endregion + +Describe 'VSTeamUserEntitlement' -Tag 'Classes', 'Unit' { + Context 'ToString' { + $obj = [PSCustomObject]@{ + displayName = 'Test User' + id = '1' + uniqueName = 'test@email.com' + } + + $target = [VSTeamUserEntitlement]::new($obj, 'Test Project') + + It 'should return displayname' { + $target.ToString() | Should Be 'Test User' + } + } +} \ No newline at end of file diff --git a/unit/test/agents.Tests.ps1 b/unit/test/agents.Tests.ps1 deleted file mode 100644 index eeee864ec..000000000 --- a/unit/test/agents.Tests.ps1 +++ /dev/null @@ -1,134 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - $testAgent = [PSCustomObject]@{ - _links = [PSCustomObject]@{} - createdOn = '2018-03-28T16:48:58.317Z' - maxParallelism = 1 - id = 102 - status = 'Online' - version = '1.336.1' - enabled = $true - osDescription = 'Linux' - name = 'Test_Agent' - authorization = [PSCustomObject]@{} - systemCapabilities = [PSCustomObject]@{} - } - - Describe 'agents' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - [VSTeamVersions]::DistributedTask = '1.0-unitTest' - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Get-VSTeamAgents' { - Mock Invoke-RestMethod { return [PSCustomObject]@{ - count = 1 - value = $testAgent - } - } - - it 'Should return all the pools' { - Get-VSTeamAgent -PoolId 1 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/1/agents?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - - Context 'Get-VSTeamAgents PoolID from pipeline by value' { - Mock Invoke-RestMethod { return [PSCustomObject]@{ - count = 1 - value = $testAgent - } - } - - it 'Should return all the pools' { - 1 | Get-VSTeamAgent - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/1/agents?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - - Context 'Get-VSTeamPool with id parameter' { - Mock Invoke-RestMethod { return $testAgent } - - it 'Should return all the pools' { - Get-VSTeamAgent -PoolId 1 -id 1 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/1/agents/1?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - - Context 'Remove-VSTeamAgent by ID' { - Mock Invoke-RestMethod - - It 'should remove the agent with passed in Id' { - Remove-VSTeamAgent -Pool 36 -Id 950 -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Delete' -and - $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/36/agents/950?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - - Context 'Remove-VSTeamAgent throws' { - Mock Invoke-RestMethod { throw 'boom' } - - It 'should remove the agent with passed in Id' { - { Remove-VSTeamAgent -Pool 36 -Id 950 -Force } | Should Throw - } - } - - Context 'Enable-VSTeamAgent by ID' { - Mock Invoke-RestMethod - - It 'should enable the agent with passed in Id' { - Enable-VSTeamAgent -Pool 36 -Id 950 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Patch' -and - $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/36/agents/950?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - - Context 'Enable-VSTeamAgent throws' { - Mock Invoke-RestMethod { throw 'boom' } - - It 'should enable the agent with passed in Id' { - { Enable-VSTeamAgent -Pool 36 -Id 950 } | Should Throw - } - } - - Context 'Disable-VSTeamAgent by ID' { - Mock Invoke-RestMethod - - It 'should disable the agent with passed in Id' { - Disable-VSTeamAgent -Pool 36 -Id 950 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Patch' -and - $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/36/agents/950?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - - Context 'Disable-VSTeamAgent throws' { - Mock Invoke-RestMethod { throw 'boom' } - - It 'should disable the agent with passed in Id' { - { Disable-VSTeamAgent -Pool 36 -Id 950 } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/buildDefinitions.Tests.ps1 b/unit/test/buildDefinitions.Tests.ps1 deleted file mode 100644 index 99d8fd559..000000000 --- a/unit/test/buildDefinitions.Tests.ps1 +++ /dev/null @@ -1,149 +0,0 @@ -Set-StrictMode -Version Latest - -# Loading System.Web avoids issues finding System.Web.HttpUtility -Add-Type -AssemblyName 'System.Web' - -InModuleScope VSTeam { - $resultsVSTS = Get-Content "$PSScriptRoot\sampleFiles\buildDefvsts.json" -Raw | ConvertFrom-Json - - Describe 'BuildDefinitions' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - Context 'Show-VSTeamBuildDefinition by ID' { - Mock Show-Browser { } - - it 'should return url for mine' { - Show-VSTeamBuildDefinition -projectName project -Id 15 - - Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { - $url -eq 'https://dev.azure.com/test/project/_build/index?definitionId=15' - } - } - } - - Context 'Show-VSTeamBuildDefinition Mine' { - Mock Show-Browser { } - - it 'should return url for mine' { - Show-VSTeamBuildDefinition -projectName project -Type Mine - - Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { - $url -eq 'https://dev.azure.com/test/project/_build/index?_a=mine&path=%5c' - } - } - } - - Context 'Show-VSTeamBuildDefinition XAML' { - Mock Show-Browser { } - - it 'should return url for XAML' { - Show-VSTeamBuildDefinition -projectName project -Type XAML - - Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { - $url -eq 'https://dev.azure.com/test/project/_build/xaml&path=%5c' - } - } - } - - Context 'Show-VSTeamBuildDefinition Queued' { - Mock Show-Browser { } - - it 'should return url for Queued' { - Show-VSTeamBuildDefinition -projectName project -Type Queued - - Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { - $url -eq 'https://dev.azure.com/test/project/_build/index?_a=queued&path=%5c' - } - } - } - - Context 'Show-VSTeamBuildDefinition Mine with path' { - Mock Show-Browser { } - - it 'should return url for mine' { - Show-VSTeamBuildDefinition -projectName project -path '\test' - - Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { - $url -like 'https://dev.azure.com/test/project/_Build/index?_a=allDefinitions&path=%5Ctest' - } - } - } - - Context 'Show-VSTeamBuildDefinition Mine with path missing \' { - Mock Show-Browser { } - - it 'should return url for mine with \ added' { - Show-VSTeamBuildDefinition -projectName project -path 'test' - - Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { - $url -like 'https://dev.azure.com/test/project/_Build/index?_a=allDefinitions&path=%5Ctest' - } - } - } - - Context 'Add-VSTeamBuildDefinition' { - Mock Invoke-RestMethod { return $resultsVSTS } - - it 'Should add build' { - Add-VSTeamBuildDefinition -projectName project -inFile 'sampleFiles/builddef.json' - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $InFile -eq 'sampleFiles/builddef.json' -and - $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Remove-VSTeamBuildDefinition' { - Mock Invoke-RestMethod { return $resultsVSTS } - - It 'should delete build definition' { - Remove-VSTeamBuildDefinition -projectName project -id 2 -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Delete' -and - $Uri -eq "https://dev.azure.com/test/project/_apis/build/definitions/2?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Add-VSTeamBuildDefinition on TFS local Auth' { - Mock Invoke-RestMethod { return $resultsVSTS } - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - it 'Should add build' { - Add-VSTeamBuildDefinition -projectName project -inFile 'sampleFiles/builddef.json' - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $InFile -eq 'sampleFiles/builddef.json' -and - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/definitions?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Remove-VSTeamBuildDefinition on TFS local Auth' { - Mock Invoke-RestMethod { return $resultsVSTS } - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - Remove-VSTeamBuildDefinition -projectName project -id 2 -Force - - It 'should delete build definition' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { - $Method -eq 'Delete' -and - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/definitions/2?api-version=$([VSTeamVersions]::Build)" - } - } - } - } -} \ No newline at end of file diff --git a/unit/test/builds.Tests.ps1 b/unit/test/builds.Tests.ps1 deleted file mode 100644 index cf048cb68..000000000 --- a/unit/test/builds.Tests.ps1 +++ /dev/null @@ -1,618 +0,0 @@ -Set-StrictMode -Version Latest - -# The InModuleScope command allows you to perform white-box unit testing on the -# internal (non-exported) code of a Script Module. -InModuleScope VSTeam { - - # Just in case it was loaded. If we don't do - # this some test may fail - Remove-VSTeamAccount | Out-Null - - $resultsVSTS = Get-Content "$PSScriptRoot\sampleFiles\buildDefvsts.json" -Raw | ConvertFrom-Json - - # Sample result of a single build - $singleResult = [PSCustomObject]@{ - logs = [PSCustomObject]@{} - queue = [PSCustomObject]@{} - _links = [PSCustomObject]@{} - project = [PSCustomObject]@{} - repository = [PSCustomObject]@{} - requestedFor = [PSCustomObject]@{} - orchestrationPlan = [PSCustomObject]@{} - definition = [PSCustomObject]@{} - lastChangedBy = [PSCustomObject]@{} - requestedBy = [PSCustomObject]@{} - } - - # Sample result for list of builds - $results = [PSCustomObject]@{ - value = [PSCustomObject]@{ - logs = [PSCustomObject]@{} - queue = [PSCustomObject]@{} - _links = [PSCustomObject]@{} - project = [PSCustomObject]@{} - repository = [PSCustomObject]@{} - requestedFor = [PSCustomObject]@{} - orchestrationPlan = [PSCustomObject]@{} - definition = [PSCustomObject]@{} - lastChangedBy = [PSCustomObject]@{} - requestedBy = [PSCustomObject]@{} - } - } - - # Just a shell for the nested dynamic parameters - # Used as Mock for calls below. We can't use normal - # Mock because the module where it lives is not loaded. - function Get-VSTeamBuildDefinition { - return new-object psobject -Property @{ - id = 2 - name = 'MyBuildDef' - fullName = 'folder\MyBuildDef' - } - } - - Describe 'Builds VSTS' { - # Load the mocks to create the project name dynamic parameter - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Update Build keep forever' { - Mock Invoke-RestMethod - - Update-VSTeamBuild -projectName project -id 1 -KeepForever $true -Force - - It 'should post changes' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { - $Method -eq 'Patch' -and - $Body -eq '{"keepForever": true}' -and - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/1?api-version=$([VSTeamVersions]::Build)" } - } - } - - Context 'Show-VSTeamBuild by ID' { - Mock Show-Browser { } - - it 'should return url for mine' { - Show-VSTeamBuild -projectName project -Id 15 - - Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { $url -eq 'https://dev.azure.com/test/project/_build/index?buildId=15' } - } - } - - Context 'Get Build Log with build id' { - Mock Invoke-RestMethod { return @{ count = 4 } } -Verifiable -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/1/logs?api-version=$([VSTeamVersions]::Build)" - } - Mock Invoke-RestMethod { return @{ value = @{} } } -Verifiable -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/1/logs/3?api-version=$([VSTeamVersions]::Build)" - } - Mock Invoke-RestMethod { throw 'Invoke-RestMethod called with wrong URL' } - - Get-VSTeamBuildLog -projectName project -Id 1 - - It 'Should return full log' { - Assert-VerifiableMock - } - } - - Context 'Get Build Log with build id and index' { - Mock Invoke-RestMethod { return @{ value = @{} } } -Verifiable -ParameterFilter { $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/1/logs/2?api-version=$([VSTeamVersions]::Build)" } - Mock Invoke-RestMethod { throw 'Invoke-RestMethod called with wrong URL' } - - Get-VSTeamBuildLog -projectName project -Id 1 -Index 2 - - It 'Should return full log' { - Assert-VerifiableMock - } - } - - Context 'Get Builds with no parameters' { - Mock Invoke-RestMethod { return $results } - - It 'should return builds' { - Get-VSTeamBuild -projectName project - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Get Builds with Top parameter' { - Mock Invoke-RestMethod { return $results } - - It 'should return top builds' { - Get-VSTeamBuild -projectName project -top 1 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds?api-version=$([VSTeamVersions]::Build)&`$top=1" - } - } - } - - Context 'Get Build build by id' { - Mock Invoke-RestMethod { return $singleResult } - - Get-VSTeamBuild -projectName project -id 1 - - It 'should return top builds' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/1?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Add-VSTeamBuild by name' { - Mock Invoke-RestMethod { return $singleResult } - Mock Get-VSTeamBuildDefinition { return [VSTeamBuildDefinition]::new($resultsVSTS.value[0], 'project') } - - It 'should add build' { - Add-VSTeamBuild -ProjectName project -BuildDefinitionName 'aspdemo-CI' - - # Call to queue build. - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - ($Body | ConvertFrom-Json).definition.id -eq 699 -and - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Add-VSTeamBuild by id' { - Mock Invoke-RestMethod { - return $singleResult - } - - It 'should add build' { - Add-VSTeamBuild -ProjectName project -BuildDefinitionId 2 - - # Call to queue build. - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - ($Body | ConvertFrom-Json).definition.id -eq 2 -and - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Add-VSTeamBuild with source branch' { - Mock Invoke-RestMethod { - return $singleResult - } - - It 'should add build' { - Add-VSTeamBuild -ProjectName project -BuildDefinitionId 2 -SourceBranch 'refs/heads/dev' - - # Call to queue build. - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - ($Body | ConvertFrom-Json).definition.id -eq 2 -and - ($Body | ConvertFrom-Json).sourceBranch -eq 'refs/heads/dev' -and - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Add-VSTeamBuild with parameters' { - Mock Invoke-RestMethod { - return $singleResult - } - - It 'should add build' { - Add-VSTeamBuild -ProjectName project -BuildDefinitionId 2 -BuildParameters @{'system.debug' = 'true'} - - # Call to queue build. - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - ($Body | ConvertFrom-Json).definition.id -eq 2 -and - (($Body | ConvertFrom-Json).parameters | ConvertFrom-Json).'system.debug' -eq 'true' -and - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Remove-VSTeamBuild' { - - # Arrange - Mock Invoke-RestMethod - - # Act - Remove-VSTeamBuild -projectName project -id 2 -Force - - It 'should delete build' { - - # Assert - Assert-MockCalled Invoke-RestMethod -Exactly -Times 1 -ParameterFilter { - $Method -eq 'Delete' -and - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/2?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Add-VSTeamBuildTag' { - Mock Invoke-RestMethod - $inputTags = "Test1", "Test2", "Test3" - - It 'should add tags to Build' { - Add-VSTeamBuildTag -ProjectName project -id 2 -Tags $inputTags - - foreach ($inputTag in $inputTags) { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Put' -and - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/2/tags?api-version=$([VSTeamVersions]::Build)" + "&tag=$inputTag" - } - } - } - } - - Context 'Remove-VSTeamBuildTag' { - Mock Invoke-RestMethod { - return @{ value = $null } - } - [string[]] $inputTags = "Test1", "Test2", "Test3" - - It 'should add tags to Build' { - Remove-VSTeamBuildTag -ProjectName project -id 2 -Tags $inputTags - - foreach ($inputTag in $inputTags) { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Delete' -and - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/2/tags?api-version=$([VSTeamVersions]::Build)" + "&tag=$inputTag" - } - } - } - } - - Context 'Get-VSTeamBuildTag calls correct Url' { - Mock Invoke-RestMethod { - return @{ value = 'Tag1', 'Tag2'} - } - - It 'should get all Build Tags for the Build.' { - Get-VSTeamBuildTag -projectName project -id 2 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/2/tags?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Get-VSTeamBuildTag returns correct data' { - $tags = 'Tag1', 'Tag2' - Mock Invoke-RestMethod { - return @{ value = $tags} - } - - It 'should get all Build Tags for the Build.' { - $returndata = Get-VSTeamBuildTag -projectName project -id 2 - - Compare-Object $tags $returndata | - Should Be $null - } - } - - Context "Get-VSTeamBuildArtifact calls correct Url" { - Mock Invoke-RestMethod { return [PSCustomObject]@{ - value = [PSCustomObject]@{ - id = 150 - name = "Drop" - resource = [PSCustomObject]@{ - type = "filepath" - data = "C:\Test" - properties = [PSCustomObject]@{ } - } - } - } - } - - Get-VSTeamBuildArtifact -projectName project -id 2 - - It 'should return the build artifact data' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/2/artifacts?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context "Get-VSTeamBuildArtifact result without properties" { - Mock Invoke-RestMethod { return [PSCustomObject]@{ - value = [PSCustomObject]@{ - id = 150 - name = "Drop" - resource = [PSCustomObject]@{ - type = "filepath" - data = "C:\Test" - } - } - } - } - - Get-VSTeamBuildArtifact -projectName project -id 2 - - It 'should return the build artifact data' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/build/builds/2/artifacts?api-version=$([VSTeamVersions]::Build)" - } - } - } - } - - Describe 'Builds TFS' { - . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" - - Mock _useWindowsAuthenticationOnPremise { return $true } - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - # Remove any previously loaded accounts - Remove-VSTeamAccount - - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - Context 'Get Build Log with index on TFS local Auth' { - Mock Invoke-RestMethod { return @{ value = @{} } } -Verifiable -ParameterFilter { $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/1/logs/2?api-version=$([VSTeamVersions]::Build)" } - Mock Invoke-RestMethod { throw 'Invoke-RestMethod called with wrong URL' } - - Get-VSTeamBuildLog -projectName project -Id 1 -Index 2 - - It 'Should return full log' { - Assert-VerifiableMock - } - } - - Context 'Get Builds with no parameters on TFS local Auth' { - Mock Invoke-RestMethod { return $results } - - It 'should return builds' { - Get-VSTeamBuild -projectName project - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Get Build by id on TFS local Auth' { - Mock Invoke-RestMethod { return $singleResult } - - It 'should return builds' { - Get-VSTeamBuild -projectName project -id 2 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/2?api-version=$([VSTeamVersions]::Build)" } - } - } - - Context 'Get Build Log on TFS local Auth' { - Mock Invoke-RestMethod { return @{ count = 4 } } -Verifiable -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/1/logs?api-version=$([VSTeamVersions]::Build)" - } - Mock Invoke-RestMethod { return @{ value = @{} } } -Verifiable -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/1/logs/3?api-version=$([VSTeamVersions]::Build)" - } - Mock Invoke-RestMethod { throw 'Invoke-RestMethod called with wrong URL' } - - Get-VSTeamBuildLog -projectName project -Id 1 - - It 'Should return full log' { - Assert-VerifiableMock - } - } - - Context "Get-VSTeamBuildArtifact calls correct Url on TFS local Auth" { - Mock Invoke-RestMethod { - return [PSCustomObject]@{ - value = [PSCustomObject]@{ - id = 150 - name = "Drop" - resource = [PSCustomObject]@{ - type = "filepath" - data = "C:\Test" - properties = [PSCustomObject]@{ } - } - } - } - } - - Get-VSTeamBuildArtifact -projectName project -id 2 - - It 'should return the build artifact data' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/2/artifacts?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Get-VSTeamBuildTag returns correct data on TFS local Auth' { - $tags = 'Tag1', 'Tag2' - Mock Invoke-RestMethod { - return @{ value = $tags} - } - - It 'should get all Build Tags for the Build.' { - $returndata = Get-VSTeamBuildTag -projectName project -id 2 - - Compare-Object $tags $returndata | - Should Be $null - } - } - - Context 'Add-VSTeamBuildTag on TFS local Auth' { - Mock Invoke-RestMethod - $inputTags = "Test1", "Test2", "Test3" - - It 'should add tags to Build' { - Add-VSTeamBuildTag -ProjectName project -id 2 -Tags $inputTags - - foreach ($inputTag in $inputTags) { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Put' -and - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/2/tags?api-version=$([VSTeamVersions]::Build)" + "&tag=$inputTag" - } - } - } - } - - Context 'Remove-VSTeamBuild on TFS local Auth' { - Mock Invoke-RestMethod - - It 'should delete build' { - Remove-VSTeamBuild -projectName project -id 2 -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Delete' -and - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/2?api-version=$([VSTeamVersions]::Build)" - } - } - } - - Context 'Add-VSTeamBuild by id on TFS local Auth' { - AfterAll { - $Global:PSDefaultParameterValues.Remove("*:projectName") - } - - Mock Get-VSTeamQueue { - return [PSCustomObject]@{ - name = "MyQueue" - id = 3 - } - } - - Mock Get-VSTeamBuildDefinition { - return @{ name = "MyBuildDef" } - } - - Mock Invoke-RestMethod { return $singleResult } -Verifiable -ParameterFilter { - ($Body | ConvertFrom-Json).definition.id -eq 2 -and - ($Body | ConvertFrom-Json).queue.id -eq 3 -and - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds?api-version=$([VSTeamVersions]::Build)" - } - - Mock Invoke-RestMethod { throw 'Invoke-RestMethod called with wrong URL' } - - $Global:PSDefaultParameterValues["*:projectName"] = 'Project' - - Add-VSTeamBuild -projectName project -BuildDefinitionId 2 -QueueName MyQueue - - It 'should add build' { - # Call to queue build. - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamBuild with parameters on TFS local Auth' { - AfterAll { - $Global:PSDefaultParameterValues.Remove("*:projectName") - } - - Mock Get-VSTeamQueue { return [PSCustomObject]@{ - name = "MyQueue" - id = 3 - } - } - Mock Get-VSTeamBuildDefinition { return @{ name = "MyBuildDef" } } - - Mock Invoke-RestMethod { return $singleResult } -Verifiable -ParameterFilter { - ($Body | ConvertFrom-Json).definition.id -eq 2 -and - ($Body | ConvertFrom-Json).queue.id -eq 3 -and - (($Body | ConvertFrom-Json).parameters | ConvertFrom-Json).'system.debug' -eq 'true' -and - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds?api-version=$([VSTeamVersions]::Build)" - } - - Mock Invoke-RestMethod { throw 'Invoke-RestMethod called with wrong URL' } - - $Global:PSDefaultParameterValues["*:projectName"] = 'Project' - - Add-VSTeamBuild -projectName project -BuildDefinitionId 2 -QueueName MyQueue -BuildParameters @{'system.debug' = 'true'} - - It 'should add build' { - # Call to queue build. - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamBuild with source branch on TFS local auth' { - AfterAll { - $Global:PSDefaultParameterValues.Remove("*:projectName") - } - - Mock Get-VSTeamQueue { return [PSCustomObject]@{ - name = "MyQueue" - id = 3 - } - } - - Mock Get-VSTeamBuildDefinition { return @{ name = "MyBuildDef" } } - - Mock Invoke-RestMethod { return $singleResult } -Verifiable -ParameterFilter { - ($Body | ConvertFrom-Json).definition.id -eq 2 -and - ($Body | ConvertFrom-Json).queue.id -eq 3 -and - ($Body | ConvertFrom-Json).sourceBranch -eq 'refs/heads/dev' -and - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds?api-version=$([VSTeamVersions]::Build)" - } - - Mock Invoke-RestMethod { throw 'Invoke-RestMethod called with wrong URL' } - - $Global:PSDefaultParameterValues["*:projectName"] = 'Project' - - Add-VSTeamBuild -projectName project -BuildDefinitionId 2 -QueueName MyQueue -SourceBranch refs/heads/dev - - It 'should add build' { - # Call to queue build. - Assert-VerifiableMock - } - } - - Context 'Remove-VSTeamBuildTag' { - Mock Invoke-RestMethod { - return @{ value = $null } - } - [string[]] $inputTags = "Test1", "Test2", "Test3" - - It 'should add tags to Build' { - Remove-VSTeamBuildTag -ProjectName project -id 2 -Tags $inputTags - - foreach ($inputTag in $inputTags) { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Delete' -and - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/2/tags?api-version=$([VSTeamVersions]::Build)" + "&tag=$inputTag" - } - } - } - } - - Context 'Remove-VSTeamBuild on TFS local Auth handles exception' { - - # Arrange - Mock _handleException -Verifiable - Mock Invoke-RestMethod { throw 'Testing error handling.' } - - [string[]] $inputTags = "Test1", "Test2", "Test3" - - # Act - Remove-VSTeamBuild -ProjectName project -id 2 -Force - - It 'should add tags to Build' { - - # Assert - Assert-VerifiableMock - } - } - - Context 'Update Build number' { - Mock Invoke-RestMethod - - Update-VSTeamBuild -projectName project -id 1 -BuildNumber 'TestNumber' -KeepForever $true -Force - - It 'should post changes' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { $Method -eq 'Patch' -and $Body -eq '{"keepForever": true, "buildNumber": "TestNumber"}' -and $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/build/builds/1?api-version=$([VSTeamVersions]::Build)" } - } - } - } -} \ No newline at end of file diff --git a/unit/test/classificationNodes.Tests.ps1 b/unit/test/classificationNodes.Tests.ps1 deleted file mode 100644 index bb9cdb2ab..000000000 --- a/unit/test/classificationNodes.Tests.ps1 +++ /dev/null @@ -1,226 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - - $classificationNodeResult = - @" -{ - "id": 16, - "identifier": "dfa90792-403a-4119-a52b-bd142c08291b", - "name": "Demo Public", - "structureType": "iteration", - "hasChildren": true, - "children": [ - { - "id": 18, - "identifier": "3d98fd5b-fd0a-4ee0-b7b2-cfdf467014d0", - "name": "Sprint 1", - "structureType": "iteration", - "hasChildren": false, - "path": "\\Demo Public\\Iteration\\Sprint 1", - "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%201" - }, - { - "id": 19, - "identifier": "eee04655-55c2-4712-afa1-9c800c1f1345", - "name": "Sprint 2", - "structureType": "iteration", - "hasChildren": false, - "path": "\\Demo Public\\Iteration\\Sprint 2", - "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%202" - }, - { - "id": 20, - "identifier": "18e7998d-d0c5-4c01-b547-d7d4eb4c97c5", - "name": "Sprint 3", - "structureType": "iteration", - "hasChildren": false, - "path": "\\Demo Public\\Iteration\\Sprint 3", - "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%203" - }, - { - "id": 21, - "identifier": "4e07ee24-8653-4af6-9df2-2aadc0a1a3d3", - "name": "Sprint 4", - "structureType": "iteration", - "hasChildren": false, - "path": "\\Demo Public\\Iteration\\Sprint 4", - "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%204" - }, - { - "id": 22, - "identifier": "b4be112e-8b7d-4def-a37d-7c1e1dea01e1", - "name": "Sprint 5", - "structureType": "iteration", - "hasChildren": false, - "path": "\\Demo Public\\Iteration\\Sprint 5", - "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%205" - }, - { - "id": 23, - "identifier": "7ee87b38-6c81-4fdf-8fbd-dc996f040823", - "name": "Sprint 6", - "structureType": "iteration", - "hasChildren": false, - "path": "\\Demo Public\\Iteration\\Sprint 6", - "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%206" - } - ], - "path": "\\Demo Public\\Iteration", - "_links": { - "self": { - "href": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations" - } - }, - "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations" - } -"@ | ConvertFrom-Json - - - $withoutChildNode = - @" -{ - "count": 2, - "value": [ - { - "id": 44, - "identifier": "90aa2c42-de51-450a-bfb6-6e264e364d9a", - "name": "Child 1 Level 2", - "structureType": "area", - "hasChildren": true, - "path": "\\Demo Public\\Area\\Child 1 Level 1\\Child 1 Level 2", - "_links": { - "self": { - "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201/Child%201%20Level%202" - }, - "parent": { - "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201" - } - }, - "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201/Child%201%20Level%202" - }, - { - "id": 43, - "identifier": "38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b", - "name": "Child 1 Level 1", - "structureType": "area", - "hasChildren": true, - "path": "\\Demo Public\\Area\\Child 1 Level 1", - "_links": { - "self": { - "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201" - }, - "parent": { - "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas" - } - }, - "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201" - } - ] - } -"@ | ConvertFrom-Json - - Describe 'ClassificationNodes VSTS' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Core = '' - [VSTeamVersions]::Core = '5.0' - - Context 'Get-VSTeamClassificationNode' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $classificationNodeResult - } -Verifiable - - Get-VSTeamClassificationNode -ProjectName "Public Demo" -StructureGroup "Iterations" - - It 'Should return Nodes' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/Public Demo/_apis/wit/classificationnodes/Iterations*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" - } - } - } - - Context 'Get-VSTeamClassificationNode with Depth' { - Mock Invoke-RestMethod { return $classificationNodeResult } -Verifiable - - Get-VSTeamClassificationNode -ProjectName "Public Demo" -StructureGroup "Iterations" -Depth 10 - - It 'Should return Nodes' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/Public Demo/_apis/wit/classificationnodes/Iterations*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*`$Depth=10*" - } - } - } - - Context 'Get-VSTeamClassificationNode by Path' { - Mock Invoke-RestMethod { return $classificationNodeResult } -Verifiable - - Get-VSTeamClassificationNode -ProjectName "Public Demo" -StructureGroup "Iterations" -Path "test/test/test" - - It 'Should return Nodes' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/Public Demo/_apis/wit/classificationnodes/Iterations/test/test/test*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" - } - } - } - - Context 'Get-VSTeamClassificationNode by Ids' { - Mock Invoke-RestMethod { return $classificationNodeResult } -Verifiable - - Get-VSTeamClassificationNode -ProjectName "Public Demo" -Ids @(1, 2, 3, 4) - - It 'Should return Nodes' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/Public Demo/_apis/wit/classificationnodes*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*Ids=1,2,3,4*" - } - } - } - - Context 'Get-VSTeamClassificationNode by Ids returns no child node' { - Mock Invoke-RestMethod { return $withoutChildNode } -Verifiable - - Get-VSTeamClassificationNode -ProjectName "Public Demo" -Ids @(43, 44) - - It 'Should return Nodes' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/Public Demo/_apis/wit/classificationnodes*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*Ids=43,44*" - } - } - } - - Context 'Get-VSTeamClassificationNode throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Get-VSTeamClassificationNode -ProjectName "test" -StructureGroup "Areas" } | Should Throw - } - } - - Context 'Get-VSTeamClassificationNode with Depth throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Get-VSTeamClassificationNode -ProjectName "test" -StructureGroup "Areas" -Depth 12 } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/cloudSubscriptions.Tests.ps1 b/unit/test/cloudSubscriptions.Tests.ps1 deleted file mode 100644 index 890ffe046..000000000 --- a/unit/test/cloudSubscriptions.Tests.ps1 +++ /dev/null @@ -1,62 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - - Describe 'CloudSubscriptions vsts' { - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - Context 'Get-VSTeamCloudSubscription' { - Mock Invoke-RestMethod { - return @{value = 'subs'} - } - - It 'should return all AzureRM Subscriptions' { - Get-VSTeamCloudSubscription - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/serviceendpointproxy/azurermsubscriptions?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - } - - Describe 'CloudSubscriptions TFS' { - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - Context 'Get-VSTeamCloudSubscription' { - Mock Invoke-RestMethod { return @{value = 'subs'}} - - It 'should return all AzureRM Subscriptions' { - Get-VSTeamCloudSubscription - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/distributedtask/serviceendpointproxy/azurermsubscriptions?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - - # Must be last because it sets [VSTeamVersions]::Account to $null - Context '_buildURL handles exception' { - - # Arrange - [VSTeamVersions]::Account = $null - - It 'should return approvals' { - - # Act - { _buildURL } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/common.Tests.ps1 b/unit/test/common.Tests.ps1 index 4c296bbb0..24170a3c7 100644 --- a/unit/test/common.Tests.ps1 +++ b/unit/test/common.Tests.ps1 @@ -1,241 +1,244 @@ -#Set-StrictMode -Version Latest +Set-StrictMode -Version Latest -InModuleScope VSTeam { - Describe 'Common' { - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } +#region include +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") + +. "$here/../../Source/Classes/VSTeamVersions.ps1" +. "$here/../../Source/Classes/VSTeamProjectCache.ps1" +. "$here/../../Source/Private/common.ps1" +. "$here/../../Source/Private/$sut" +#endregion - Context '_convertSecureStringTo_PlainText' { - $emptySecureString = ConvertTo-SecureString 'Test String' -AsPlainText -Force +Describe 'Common' { + Context '_convertSecureStringTo_PlainText' { + $emptySecureString = ConvertTo-SecureString 'Test String' -AsPlainText -Force - $actual = _convertSecureStringTo_PlainText -SecureString $emptySecureString + $actual = _convertSecureStringTo_PlainText -SecureString $emptySecureString - It 'Should return plain text' { - $actual | Should Be 'Test String' - } + It 'Should return plain text' { + $actual | Should Be 'Test String' } + } - Context '_buildProjectNameDynamicParam set Alias' { - Mock _getProjects + Context '_buildProjectNameDynamicParam set Alias' { + Mock _getProjects - $actual = _buildProjectNameDynamicParam -AliasName TestAlias + $actual = _buildProjectNameDynamicParam -AliasName TestAlias - It 'Should set the alias of dynamic parameter' { - $actual["ProjectName"].Attributes[1].AliasNames | Should Be 'TestAlias' - } + It 'Should set the alias of dynamic parameter' { + $actual["ProjectName"].Attributes[1].AliasNames | Should Be 'TestAlias' } + } - Context '_getUserAgent on Mac' { - Mock Get-OperatingSystem { return 'macOS' } - [VSTeamVersions]::ModuleVersion = '0.0.0' + Context '_getUserAgent on Mac' { + Mock Get-OperatingSystem { return 'macOS' } + [VSTeamVersions]::ModuleVersion = '0.0.0' - $actual = _getUserAgent + $actual = _getUserAgent - It 'Should return User Agent for macOS' { - $actual | Should BeLike '*macOS*' - } + It 'Should return User Agent for macOS' { + $actual | Should BeLike '*macOS*' + } - It 'Should return User Agent for Module Version' { - $actual | Should BeLike '*0.0.0*' - } + It 'Should return User Agent for Module Version' { + $actual | Should BeLike '*0.0.0*' + } - It 'Should return User Agent for PowerShell Version' { - $actual | Should BeLike "*$($PSVersionTable.PSVersion.ToString())*" - } + It 'Should return User Agent for PowerShell Version' { + $actual | Should BeLike "*$($PSVersionTable.PSVersion.ToString())*" } + } - Context '_getUserAgent on Linux' { - Mock Get-OperatingSystem { return 'Linux' } - [VSTeamVersions]::ModuleVersion = '0.0.0' + Context '_getUserAgent on Linux' { + Mock Get-OperatingSystem { return 'Linux' } + [VSTeamVersions]::ModuleVersion = '0.0.0' - $actual = _getUserAgent + $actual = _getUserAgent - It 'Should return User Agent for Linux' { - $actual | Should BeLike '*Linux*' - } + It 'Should return User Agent for Linux' { + $actual | Should BeLike '*Linux*' + } - It 'Should return User Agent for Module Version' { - $actual | Should BeLike '*0.0.0*' - } + It 'Should return User Agent for Module Version' { + $actual | Should BeLike '*0.0.0*' + } - It 'Should return User Agent for PowerShell Version' { - $actual | Should BeLike "*$($PSVersionTable.PSVersion.ToString())*" - } + It 'Should return User Agent for PowerShell Version' { + $actual | Should BeLike "*$($PSVersionTable.PSVersion.ToString())*" } + } - Context '_buildProjectNameDynamicParam' { - Mock _getProjects { return ConvertFrom-Json '["Demo", "Universal"]' } + Context '_buildProjectNameDynamicParam' { + Mock _getProjects { return ConvertFrom-Json '["Demo", "Universal"]' } - It 'should return dynamic parameter' { - _buildProjectNameDynamicParam | Should Not BeNullOrEmpty - } + It 'should return dynamic parameter' { + _buildProjectNameDynamicParam | Should Not BeNullOrEmpty } + } - Context '_buildDynamicParam no defaults' { - Mock _getProjects { return ConvertFrom-Json '["Demo", "Universal"]' } - - $testParams = @{ - ParameterName = 'TestParam' - arrSet = @(@{A = 'A'}, @{B = 'B'}) - Mandatory = $true - ParameterSetName = "NewTest" - Position = 0 - ParameterType = ([hashtable]) - ValueFromPipelineByPropertyName = $false - AliasName = "TestAlieas" - HelpMessage = "Test Help Message" - } - $param = (_buildDynamicParam @testParams) + Context '_buildDynamicParam no defaults' { + Mock _getProjects { return ConvertFrom-Json '["Demo", "Universal"]' } + + $testParams = @{ + ParameterName = 'TestParam' + arrSet = @(@{A = 'A' }, @{B = 'B' }) + Mandatory = $true + ParameterSetName = "NewTest" + Position = 0 + ParameterType = ([hashtable]) + ValueFromPipelineByPropertyName = $false + AliasName = "TestAlieas" + HelpMessage = "Test Help Message" + } + $param = (_buildDynamicParam @testParams) - It 'should return dynamic parameter' { - $param | Should Not BeNullOrEmpty - } + It 'should return dynamic parameter' { + $param | Should Not BeNullOrEmpty + } - It 'should return dynamic parameter name' { - $param.Name | Should Be $testParams.ParameterName - } + It 'should return dynamic parameter name' { + $param.Name | Should Be $testParams.ParameterName + } - It 'should return dynamic parameter type' { - $param.ParameterType.FullName | Should Be $testParams.ParameterType.FullName - } + It 'should return dynamic parameter type' { + $param.ParameterType.FullName | Should Be $testParams.ParameterType.FullName + } - It 'Should set the basic attributes of the dynamic parameter' { - $param.Attributes[0].Position | Should Be $testParams.Position - $param.Attributes[0].Mandatory | Should Be $testParams.Mandatory - $param.Attributes[0].ParameterSetName | Should Be $testParams.ParameterSetName - $param.Attributes[0].ValueFromPipelineByPropertyName | Should Be $testParams.ValueFromPipelineByPropertyName - $param.Attributes[0].HelpMessage | Should Be $testParams.HelpMessage - } + It 'Should set the basic attributes of the dynamic parameter' { + $param.Attributes[0].Position | Should Be $testParams.Position + $param.Attributes[0].Mandatory | Should Be $testParams.Mandatory + $param.Attributes[0].ParameterSetName | Should Be $testParams.ParameterSetName + $param.Attributes[0].ValueFromPipelineByPropertyName | Should Be $testParams.ValueFromPipelineByPropertyName + $param.Attributes[0].HelpMessage | Should Be $testParams.HelpMessage + } - It 'Should set the alias attributes of the dynamic parameter' { - $param.Attributes[1].AliasNames | Should Be $testParams.AliasName - } + It 'Should set the alias attributes of the dynamic parameter' { + $param.Attributes[1].AliasNames | Should Be $testParams.AliasName + } - It 'Should set the possible vaule attributes of the dynamic parameter' { - (Compare-Object -ReferenceObject $param.Attributes[2].ValidValues -DifferenceObject $testParams.arrSet) | Should BeNullOrEmpty - } + It 'Should set the possible vaule attributes of the dynamic parameter' { + (Compare-Object -ReferenceObject $param.Attributes[2].ValidValues -DifferenceObject $testParams.arrSet) | Should BeNullOrEmpty } + } - Context '_buildDynamicParam defaults' { - Mock _getProjects { return ConvertFrom-Json '["Demo", "Universal"]' } + Context '_buildDynamicParam defaults' { + Mock _getProjects { return ConvertFrom-Json '["Demo", "Universal"]' } - $param = (_buildDynamicParam) + $param = (_buildDynamicParam) - It 'should return dynamic parameter' { - $param | Should Not BeNullOrEmpty - } + It 'should return dynamic parameter' { + $param | Should Not BeNullOrEmpty + } - It 'should return dynamic parameter name' { - $param.Name | Should Be 'QueueName' - } + It 'should return dynamic parameter name' { + $param.Name | Should Be 'QueueName' + } - It 'should return dynamic parameter type' { - $param.ParameterType.FullName | Should Be ([string]).FullName - } + It 'should return dynamic parameter type' { + $param.ParameterType.FullName | Should Be ([string]).FullName + } - It 'Should set the basic attributes of the dynamic parameter' { - ($param.Attributes[0].Position -lt 0) | Should Be ($true) - $param.Attributes[0].Mandatory | Should Be $false - $param.Attributes[0].ParameterSetName | Should Be '__AllParameterSets' - $param.Attributes[0].ValueFromPipelineByPropertyName | Should Be $true - $param.Attributes[0].HelpMessage | Should BeNullOrEmpty - } + It 'Should set the basic attributes of the dynamic parameter' { + ($param.Attributes[0].Position -lt 0) | Should Be ($true) + $param.Attributes[0].Mandatory | Should Be $false + $param.Attributes[0].ParameterSetName | Should Be '__AllParameterSets' + $param.Attributes[0].ValueFromPipelineByPropertyName | Should Be $true + $param.Attributes[0].HelpMessage | Should BeNullOrEmpty + } - It 'Should have no additoinal attributes of the dynamic parameter' { - $param.Attributes.Count | Should Be 1 - } + It 'Should have no additoinal attributes of the dynamic parameter' { + $param.Attributes.Count | Should Be 1 } + } - Context '_getWorkItemTypes' { - Mock _getInstance { return $null } -Verifiable + Context '_getWorkItemTypes' { + Mock _getInstance { return $null } -Verifiable - It 'should return empty array' { - _getWorkItemTypes -ProjectName test | Should be @() - Assert-VerifiableMock - } + It 'should return empty array' { + _getWorkItemTypes -ProjectName test | Should be @() + Assert-VerifiableMock } + } - Context '_handleException' { - # Build a proper error - $obj = "{Value: {Message: 'Top Message'}, Exception: {Message: 'Test Exception', Response: { StatusCode: '401'}}}" + Context '_handleException' { + # Build a proper error + $obj = "{Value: {Message: 'Top Message'}, Exception: {Message: 'Test Exception', Response: { StatusCode: '401'}}}" - if ($PSVersionTable.PSEdition -ne 'Core') { - $r = [System.Net.HttpWebResponse]::new() - $e = [System.Net.WebException]::new("Test Exception", $null, [System.Net.WebExceptionStatus]::ProtocolError, $r) - } - else { - $r = [System.Net.Http.HttpResponseMessage]::new([System.Net.HttpStatusCode]::Unauthorized) - $e = [Microsoft.PowerShell.Commands.HttpResponseException]::new("Test Exception", $r) - } - $ex = Write-Error -Exception $e 2>&1 -ErrorAction Continue - $ex.ErrorDetails = [System.Management.Automation.ErrorDetails]::new($obj) + if ($PSVersionTable.PSEdition -ne 'Core') { + $r = [System.Net.HttpWebResponse]::new() + $e = [System.Net.WebException]::new("Test Exception", $null, [System.Net.WebExceptionStatus]::ProtocolError, $r) + } + else { + $r = [System.Net.Http.HttpResponseMessage]::new([System.Net.HttpStatusCode]::Unauthorized) + $e = [Microsoft.PowerShell.Commands.HttpResponseException]::new("Test Exception", $r) + } + $ex = Write-Error -Exception $e 2>&1 -ErrorAction Continue + $ex.ErrorDetails = [System.Management.Automation.ErrorDetails]::new($obj) - It 'Should Write two warnings' { - Mock Write-Warning -ParameterFilter { $Message -eq 'An error occurred: Test Exception'} -Verifiable - Mock Write-Warning -ParameterFilter { $Message -eq 'Top Message' } -Verifiable + It 'Should Write two warnings' { + Mock Write-Warning -ParameterFilter { $Message -eq 'An error occurred: Test Exception' } -Verifiable + Mock Write-Warning -ParameterFilter { $Message -eq 'Top Message' } -Verifiable - _handleException $ex + _handleException $ex - Assert-VerifiableMock - } + Assert-VerifiableMock } + } - Context '_handleException should re-throw' { - $e = [System.Management.Automation.RuntimeException]::new('You must call Set-VSTeamAccount before calling any other functions in this module.') - $ex = Write-Error -Exception $e 2>&1 -ErrorAction Continue + Context '_handleException should re-throw' { + $e = [System.Management.Automation.RuntimeException]::new('You must call Set-VSTeamAccount before calling any other functions in this module.') + $ex = Write-Error -Exception $e 2>&1 -ErrorAction Continue - It 'Should throw' { + It 'Should throw' { - { _handleException $ex } | Should Throw - } + { _handleException $ex } | Should Throw } + } - Context '_handleException message only' { - # Build a proper error - $obj = "{Value: {Message: 'Test Exception'}, Exception: {Message: 'Test Exception', Response: { StatusCode: '400'}}}" + Context '_handleException message only' { + # Build a proper error + $obj = "{Value: {Message: 'Test Exception'}, Exception: {Message: 'Test Exception', Response: { StatusCode: '400'}}}" - if ($PSVersionTable.PSEdition -ne 'Core') { - $e = [System.Net.WebException]::new("Test Exception", $null) - } - else { - $r = [System.Net.Http.HttpResponseMessage]::new([System.Net.HttpStatusCode]::BadRequest) - $e = [Microsoft.PowerShell.Commands.HttpResponseException]::new("Test Exception", $r) - } + if ($PSVersionTable.PSEdition -ne 'Core') { + $e = [System.Net.WebException]::new("Test Exception", $null) + } + else { + $r = [System.Net.Http.HttpResponseMessage]::new([System.Net.HttpStatusCode]::BadRequest) + $e = [Microsoft.PowerShell.Commands.HttpResponseException]::new("Test Exception", $r) + } - $ex = Write-Error -Exception $e 2>&1 -ErrorAction Continue - $ex.ErrorDetails = [System.Management.Automation.ErrorDetails]::new($obj) + $ex = Write-Error -Exception $e 2>&1 -ErrorAction Continue + $ex.ErrorDetails = [System.Management.Automation.ErrorDetails]::new($obj) - It 'Should Write one warnings' { - Mock Write-Warning -ParameterFilter { $Message -eq 'Test Exception' } -Verifiable + It 'Should Write one warnings' { + Mock Write-Warning -ParameterFilter { $Message -eq 'Test Exception' } -Verifiable - _handleException $ex + _handleException $ex - Assert-VerifiableMock - } + Assert-VerifiableMock } + } - Context '_isVSTS' { - It '.visualstudio.com should return true' { - _isVSTS 'https://dev.azure.com/test' | Should Be $true - } + Context '_isVSTS' { + It '.visualstudio.com should return true' { + _isVSTS 'https://dev.azure.com/test' | Should Be $true + } - It '.visualstudio.com with / should return true' { - _isVSTS 'https://dev.azure.com/test/' | Should Be $true - } + It '.visualstudio.com with / should return true' { + _isVSTS 'https://dev.azure.com/test/' | Should Be $true + } - It 'https://dev.azure.com should return true' { - _isVSTS 'https://dev.azure.com/test' | Should Be $true - } + It 'https://dev.azure.com should return true' { + _isVSTS 'https://dev.azure.com/test' | Should Be $true + } - It 'https://dev.azure.com with / should return true' { - _isVSTS 'https://dev.azure.com/test/' | Should Be $true - } + It 'https://dev.azure.com with / should return true' { + _isVSTS 'https://dev.azure.com/test/' | Should Be $true + } - It 'should return false' { - _isVSTS 'http://localhost:8080/tfs/defaultcollection' | Should Be $false - } + It 'should return false' { + _isVSTS 'http://localhost:8080/tfs/defaultcollection' | Should Be $false } } } \ No newline at end of file diff --git a/unit/test/descriptor.Tests.ps1 b/unit/test/descriptor.Tests.ps1 deleted file mode 100644 index 2db845911..000000000 --- a/unit/test/descriptor.Tests.ps1 +++ /dev/null @@ -1,57 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - - $result = Get-Content "$PSScriptRoot\sampleFiles\descriptor.scope.TestProject.json" -Raw | ConvertFrom-Json - - Describe "Groups TFS Errors" { - Context 'Get-VSTeamDescriptor' { - Mock _callAPI { throw 'Should not be called' } -Verifiable - - It 'Should throw' { - Set-VSTeamAPIVersion TFS2017 - - { Get-VSTeamDescriptor -StorageKey '010d06f0-00d5-472a-bb47-58947c230876' } | Should Throw - } - - It '_callAPI should not be called' { - Assert-MockCalled _callAPI -Exactly 0 - } - } - } - - Describe 'Descriptor VSTS' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Graph = '' - [VSTeamVersions]::Graph = '5.0' - - Context 'Get-VSTeamDescriptor by StorageKey' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $result - } -Verifiable - - Get-VSTeamDescriptor -StorageKey '010d06f0-00d5-472a-bb47-58947c230876' - - It 'Should return groups' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://vssps.dev.azure.com/test/_apis/graph/descriptors/010d06f0-00d5-472a-bb47-58947c230876?api-version=$([VSTeamVersions]::Graph)" - } - } - } - - Context 'Get-VSTeamDescriptor by StorageKey throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Get-VSTeamDescriptor -StorageKey Demo } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/extensions.tests.ps1 b/unit/test/extensions.tests.ps1 deleted file mode 100644 index a9a28d5d6..000000000 --- a/unit/test/extensions.tests.ps1 +++ /dev/null @@ -1,189 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe 'Extension' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - $results = [PSCustomObject]@{ - count = 1 - value = [PSCustomObject]@{ - extensionId = 'test' - extensionName = 'test' - publisherId = 'test' - publisherName = 'test' - version = '1.0.0' - registrationId = '12345678-9012-3456-7890-123456789012' - manifestVersion = 1 - baseUri = '' - fallbackBaseUri = '' - scopes = [PSCustomObject]@{} - installState = [PSCustomObject]@{ - flags = 'none' - lastUpdated = '2018-10-09T11:26:47.187Z' - } - } - } - - $singleResult = [PSCustomObject]@{ - extensionId = 'test' - extensionName = 'test' - publisherId = 'test' - publisherName = 'test' - version = '1.0.0' - registrationId = '12345678-9012-3456-7890-123456789012' - manifestVersion = 1 - baseUri = '' - fallbackBaseUri = '' - scopes = [PSCustomObject]@{} - installState = [PSCustomObject]@{ - flags = 'none' - lastUpdated = '2018-10-09T11:26:47.187Z' - } - } - - Context 'Get-VSTeamExtension' { - BeforeAll { - $env:Team_TOKEN = '1234' - } - - AfterAll { - $env:TEAM_TOKEN = $null - } - - It 'Should return extensions' { - Mock _callAPI { return $results } - - Get-VSTeamExtension - - Assert-MockCalled _callAPI -Exactly 1 -Scope It -ParameterFilter { - $Method -eq 'Get' -and - $subDomain -eq 'extmgmt' -and - $version -eq [VSTeamVersions]::ExtensionsManagement - $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensions*" - } - } - - It 'Should return extensions with optional parameters' { - Mock _callAPI { return $results } - - Get-VSTeamExtension -IncludeInstallationIssues -IncludeDisabledExtensions -IncludeErrors - - Assert-MockCalled _callAPI -Exactly 1 -Scope It -ParameterFilter { - $Method -eq 'Get' -and - $subDomain -eq 'extmgmt' -and - $version -eq [VSTeamVersions]::ExtensionsManagement - $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensions*" -and - $Url -like "*includeInstallationIssues*" -and - $Url -like "*includeDisabledExtensions*" -and - $Url -like "*includeErrors*" - } - } - - It 'Should return the extension' { - Mock _callAPI { return $singleResult } - - Get-VSTeamExtension -PublisherId test -ExtensionId test - - Assert-MockCalled _callAPI -Exactly 1 -Scope It -ParameterFilter { - $Method -eq 'Get' -and - $subDomain -eq 'extmgmt' -and - $version -eq [VSTeamVersions]::ExtensionsManagement - $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensionsbyname/test/test*" - } - } - } - - Context 'Add-VSTeamExtension without version' { - BeforeAll { - $env:Team_TOKEN = '1234' - } - - AfterAll { - $env:TEAM_TOKEN = $null - } - - Mock _callAPI { return $singleResult } - - It 'Should add an extension without version' { - Add-VSTeamExtension -PublisherId 'test' -ExtensionId 'test' - - Assert-MockCalled _callAPI -Exactly 1 -Scope It -ParameterFilter { - $Method -eq 'Get' -and - $subDomain -eq 'extmgmt' -and - $version -eq [VSTeamVersions]::ExtensionsManagement -and - $uri - $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensionsbyname/test/test*" - } - } - } - - Context 'Add-VSTeamExtension with version' { - BeforeAll { - $env:Team_TOKEN = '1234' - } - - AfterAll { - $env:TEAM_TOKEN = $null - } - - Mock _callAPI { return $singleResult } - - It 'Should add an extension with version' { - Add-VSTeamExtension -PublisherId 'test' -ExtensionId 'test' -Version '1.0.0' - - Assert-MockCalled _callAPI -Exactly 1 -Scope It -ParameterFilter { - $Method -eq 'Get' -and - $subDomain -eq 'extmgmt' -and - $version -eq [VSTeamVersions]::ExtensionsManagement - $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensionsbyname/test/test/1.0.0*" - } - } - } - - Context 'Update-VSTeamExtension' { - BeforeAll { - $env:Team_TOKEN = '1234' - } - - AfterAll { - $env:TEAM_TOKEN = $null - } - - Mock _callAPI { return $singleResult } - - It 'Should add an extension without version' { - Update-VSTeamExtension -PublisherId 'test' -ExtensionId 'test' -ExtensionState disabled -Force - - Assert-MockCalled _callAPI -Exactly 1 -Scope It -ParameterFilter { - $Method -eq 'Post' -and - $subDomain -eq 'extmgmt' -and - $version -eq [VSTeamVersions]::ExtensionsManagement - $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensionsbyname/test/test*" - } - } - } - - Context 'Remove-VSTeamExtension' { - BeforeAll { - $env:Team_TOKEN = '1234' - } - - AfterAll { - $env:TEAM_TOKEN = $null - } - - Mock _callAPI { return $singleResult } - - It 'Should remove an extension' { - Remove-VSTeamExtension -PublisherId 'test' -ExtensionId 'test' -Force - - Assert-MockCalled _callAPI -Exactly 1 -Scope It -ParameterFilter { - $Method -eq 'Delete' -and - $subDomain -eq 'extmgmt' -and - $version -eq [VSTeamVersions]::ExtensionsManagement - $Url -like "*https://extmgmt.dev.azure.com/test/_apis/_apis/extensionmanagement/installedextensionsbyname/test/test*" - } - } - } - } -} \ No newline at end of file diff --git a/unit/test/feeds.Tests.ps1 b/unit/test/feeds.Tests.ps1 deleted file mode 100644 index ea6465463..000000000 --- a/unit/test/feeds.Tests.ps1 +++ /dev/null @@ -1,121 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - $results = Get-Content "$PSScriptRoot\sampleFiles\feeds.json" -Raw | ConvertFrom-Json - - Describe 'Feeds' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Remove-VSTeamFeed' { - [VSTeamVersions]::Packaging = '4.0' - Mock Invoke-RestMethod - - It 'should delete feed' { - Remove-VSTeamFeed -id '00000000-0000-0000-0000-000000000000' -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Delete' -and - $Uri -eq "https://feeds.dev.azure.com/test/_apis/packaging/feeds/00000000-0000-0000-0000-000000000000?api-version=$([VSTeamVersions]::packaging)" - } - } - } - - Context 'Get-VSTeamFeed with no parameters' { - [VSTeamVersions]::Packaging = '4.0' - Mock Invoke-RestMethod { return $results } - - it 'Should return all the Feeds' { - Get-VSTeamFeed - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://feeds.dev.azure.com/test/_apis/packaging/feeds?api-version=$([VSTeamVersions]::packaging)" - } - } - } - - Context 'Get-VSTeamFeed with id parameter' { - [VSTeamVersions]::Packaging = '4.0' - Mock Invoke-RestMethod { return $results.value[0] } - - it 'Should return one feed' { - Get-VSTeamFeed -id '00000000-0000-0000-0000-000000000000' - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://feeds.dev.azure.com/test/_apis/packaging/feeds/00000000-0000-0000-0000-000000000000?api-version=$([VSTeamVersions]::packaging)" - } - } - } - - Context 'Add-VSTeamFeed with description' { - [VSTeamVersions]::Packaging = '4.0' - Mock Invoke-RestMethod { - # Write-Host "$args" - return $results.value[0] - } - - it 'Should add Feed' { - Add-VSTeamFeed -Name 'module' -Description 'Test Module' - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://feeds.dev.azure.com/test/_apis/packaging/feeds?api-version=$([VSTeamVersions]::packaging)" -and - $Method -eq 'Post' -and - $ContentType -eq 'application/json' -and - $Body -like '*"name": *"module"*' - } - } - } - - Context 'Add-VSTeamFeed with upstream sources' { - [VSTeamVersions]::Packaging = '4.0' - Mock Invoke-RestMethod { - # Write-Host "$args" - return $results.value[0] - } - - it 'Should add Feed' { - Add-VSTeamFeed -Name 'module' -EnableUpstreamSources -showDeletedPackageVersions - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://feeds.dev.azure.com/test/_apis/packaging/feeds?api-version=$([VSTeamVersions]::packaging)" -and - $Method -eq 'Post' -and - $ContentType -eq 'application/json' -and - $Body -like '*"upstreamEnabled":*true*' -and - $Body -like '*"hideDeletedPackageVersions":*false*' - } - } - } - - Context 'Show-VSTeamFeed by name' { - Mock Show-Browser - - It 'Show call start' { - Show-VSTeamFeed -Name module - - Assert-MockCalled Show-Browser - } - } - - Context 'Show-VSTeamFeed by id' { - Mock Show-Browser - - It 'Show call start' { - Show-VSTeamFeed -Id '00000000-0000-0000-0000-000000000000' - - Assert-MockCalled Show-Browser - } - } - - Context 'Get-VSTeamFeed on TFS'{ - [VSTeamVersions]::Packaging = '' - - it 'Should throw' { - { Get-VSTeamFeed } | Should throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/gitRepositoryPermissions.Tests.ps1 b/unit/test/gitRepositoryPermissions.Tests.ps1 deleted file mode 100644 index 3055e8f96..000000000 --- a/unit/test/gitRepositoryPermissions.Tests.ps1 +++ /dev/null @@ -1,232 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - $userSingleResult = Get-Content "$PSScriptRoot\sampleFiles\users.single.json" -Raw | ConvertFrom-Json - $userSingleResultObject = [VSTeamUser]::new($userSingleResult) - - $groupSingleResult = Get-Content "$PSScriptRoot\sampleFiles\groupsSingle.json" -Raw | ConvertFrom-Json - $groupSingleResultObject = [VSTeamGroup]::new($groupSingleResult) - - $projectResult = [PSCustomObject]@{ - name = 'Test Project Public' - description = '' - url = '' - id = '010d06f0-00d5-472a-bb47-58947c230876' - state = '' - visibility = '' - revision = 0 - defaultTeam = [PSCustomObject]@{} - _links = [PSCustomObject]@{} - } - - $projectResultObject = [VSTeamProject]::new($projectResult) - - $accessControlEntryResult = - @" -{ - "count": 1, - "value": [ - { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", - "allow": 8, - "deny": 0, - "extendedInfo": {} - } - ] -} -"@ | ConvertFrom-Json - - Describe 'GitRepositoryPermissions VSTS' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Core = '' - [VSTeamVersions]::Core = '5.0' - - Context 'Add-VSTeamGitRepositoryPermission by ProjectUser' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $accessControlEntryResult - } -Verifiable - - Add-VSTeamGitRepositoryPermission -Project $projectResultObject -User $userSingleResultObject -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and - $Body -like "*`"allow`": 34048,*" -and - $Body -like "*`"deny`": 2056,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamGitRepositoryPermission by ProjectGroup' { - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamGitRepositoryPermission -Project $projectResultObject -Group $groupSingleResultObject -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 34048,*" -and - $Body -like "*`"deny`": 2056,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamGitRepositoryPermission by ProjectDescriptor' { - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamGitRepositoryPermission -Project $projectResultObject -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 34048,*" -and - $Body -like "*`"deny`": 2056,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamGitRepositoryPermission by RepositoryUser' { - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamGitRepositoryPermission -Project $projectResultObject -RepositoryId "12345678-1234-1234-1234-123456789012" -User $userSingleResultObject -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876/12345678-1234-1234-1234-123456789012`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and - $Body -like "*`"allow`": 34048,*" -and - $Body -like "*`"deny`": 2056,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamGitRepositoryPermission by RepositoryGroup' { - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamGitRepositoryPermission -Project $projectResultObject -RepositoryId "12345678-1234-1234-1234-123456789012" -Group $groupSingleResultObject -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876/12345678-1234-1234-1234-123456789012`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 34048,*" -and - $Body -like "*`"deny`": 2056,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamGitRepositoryPermission by RepositoryDescriptor' { - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamGitRepositoryPermission -Project $projectResultObject -RepositoryId "12345678-1234-1234-1234-123456789012" -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876/12345678-1234-1234-1234-123456789012`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 34048,*" -and - $Body -like "*`"deny`": 2056,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamGitRepositoryPermission by RepositoryBranchUser' { - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamGitRepositoryPermission -Project $projectResultObject -RepositoryId "12345678-1234-1234-1234-123456789012" -BranchName "master" -User $userSingleResultObject -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876/12345678-1234-1234-1234-123456789012/refs/heads/6d0061007300740065007200`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and - $Body -like "*`"allow`": 34048,*" -and - $Body -like "*`"deny`": 2056,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamGitRepositoryPermission by RepositoryBranchGroup' { - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamGitRepositoryPermission -Project $projectResultObject -RepositoryId "12345678-1234-1234-1234-123456789012" -BranchName "master" -Group $groupSingleResultObject -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876/12345678-1234-1234-1234-123456789012/refs/heads/6d0061007300740065007200`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 34048,*" -and - $Body -like "*`"deny`": 2056,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamGitRepositoryPermission by RepositoryBranchDescriptor' { - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamGitRepositoryPermission -Project $projectResultObject -RepositoryId "12345678-1234-1234-1234-123456789012" -BranchName "master" -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"repoV2/010d06f0-00d5-472a-bb47-58947c230876/12345678-1234-1234-1234-123456789012/refs/heads/6d0061007300740065007200`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 34048,*" -and - $Body -like "*`"deny`": 2056,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamGitRepositoryPermission by Project throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Add-VSTeamGitRepositoryPermission -Project $projectResultObject -RepositoryId "12345678-1234-1234-1234-123456789012" -User $userSingleResultObject -Allow ([VSTeamGitRepositoryPermissions]'CreateRepository,RenameRepository,PullRequestBypassPolicy') -Deny ([VSTeamGitRepositoryPermissions]'EditPolicies,ForcePush') } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/groups.Tests.ps1 b/unit/test/groups.Tests.ps1 deleted file mode 100644 index 5f28d611f..000000000 --- a/unit/test/groups.Tests.ps1 +++ /dev/null @@ -1,149 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - $projectResult = [PSCustomObject]@{ - name = 'Test Project Public' - description = '' - url = '' - id = '010d06f0-00d5-472a-bb47-58947c230876' - state = '' - visibility = '' - revision = 0 - defaultTeam = [PSCustomObject]@{} - _links = [PSCustomObject]@{} - } - - $scopeResult = Get-Content "$PSScriptRoot\sampleFiles\descriptor.scope.TestProject.json" -Raw | ConvertFrom-Json - $groupListResult = Get-Content "$PSScriptRoot\sampleFiles\groups.json" -Raw | ConvertFrom-Json - $groupSingleResult = Get-Content "$PSScriptRoot\sampleFiles\groupsSingle.json" -Raw | ConvertFrom-Json - - # The Graph API is not supported on TFS - Describe "Groups TFS Errors" { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - Context 'Get-VSTeamGroup' { - Mock _callAPI { throw 'Should not be called' } -Verifiable - - It 'Should throw' { - Set-VSTeamAPIVersion TFS2017 - - { Get-VSTeamGroup } | Should Throw - } - - It '_callAPI should be called once to get projects' { - Assert-MockCalled _callAPI -Exactly 1 - } - } - } - - Describe 'Groups VSTS' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Graph = '' - [VSTeamVersions]::Graph = '5.0' - - Context 'Get-VSTeamGroup by project' { - Mock Get-VSTeamProject { return $projectResult } -Verifiable - Mock Get-VSTeamDescriptor { return [VSTeamDescriptor]::new($scopeResult) } -Verifiable - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $groupListResult - } -Verifiable - - Get-VSTeamGroup -ProjectName "Test Project Public" - - It 'Should return groups' { - # With PowerShell core the order of the query string is not the - # same from run to run! So instead of testing the entire string - # matches I have to search for the portions I expect but can't - # assume the order. - # The general string should look like this: - # "https://vssps.dev.azure.com/test/_apis/graph/groups?api-version=$([VSTeamVersions]::Graph)&scopeDescriptor=scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2" - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/groups*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Graph)*" -and - $Uri -like "*scopeDescriptor=scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2*" - } - } - } - - Context 'Get-VSTeamGroup by scopeDescriptor' { - Mock Invoke-RestMethod { return $groupListResult } -Verifiable - - Get-VSTeamGroup -ScopeDescriptor scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2 - - It 'Should return groups' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/groups*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Graph)*" -and - $Uri -like "*scopeDescriptor=scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2*" - } - } - } - - Context 'Get-VSTeamGroup by subjectTypes' { - Mock Invoke-RestMethod { return $groupListResult } -Verifiable - - Get-VSTeamGroup -SubjectTypes vssgp, aadgp - - It 'Should return groups' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/groups*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Graph)*" -and - $Uri -like "*subjectTypes=vssgp,aadgp*" - } - } - } - - Context 'Get-VSTeamGroup by subjectTypes and scopeDescriptor' { - Mock Invoke-RestMethod { return $groupListResult } -Verifiable - - Get-VSTeamGroup -ScopeDescriptor scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2 -SubjectTypes vssgp, aadgp - - It 'Should return groups' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/groups*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Graph)*" -and - $Uri -like "*subjectTypes=vssgp,aadgp*" -and - $Uri -like "*scopeDescriptor=scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2*" - } - } - } - - Context 'Get-VSTeamGroup by descriptor' { - Mock Invoke-RestMethod { return $groupSingleResult } -Verifiable - - Get-VSTeamGroup -GroupDescriptor 'vssgp.Uy0xLTktMTU1MTM3NDI0NS04NTYwMDk3MjYtNDE5MzQ0MjExNy0yMzkwNzU2MTEwLTI3NDAxNjE4MjEtMC0wLTAtMC0x' - - It 'Should return the group' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/groups/vssgp.Uy0xLTktMTU1MTM3NDI0NS04NTYwMDk3MjYtNDE5MzQ0MjExNy0yMzkwNzU2MTEwLTI3NDAxNjE4MjEtMC0wLTAtMC0x*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Graph)*" - } - } - } - - Context 'Get-VSTeamGroup by project throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Get-VSTeamGroup -ProjectName Demo } | Should Throw - } - } - - Context 'Get-VSTeamGroup by descriptor throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Get-VSTeamGroup -GroupDescriptor } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/membership.Tests.ps1 b/unit/test/membership.Tests.ps1 deleted file mode 100644 index 28dc90128..000000000 --- a/unit/test/membership.Tests.ps1 +++ /dev/null @@ -1,99 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe 'Users VSTS' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Graph = '' - [VSTeamVersions]::Graph = '5.0' - - Context 'Test-VSTeamMembership' { - Mock Invoke-RestMethod { } -Verifiable - - $UserDescriptor = 'aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj' - $GroupDescriptor = 'vssgp.Uy0xLTktMTU1MTM3NDI0NS04NTYwMDk3MjYtNDE5MzQ0MjExNy0yMzkwNzU2MTEwLTI3NDAxNjE4MjEtMC0wLTAtMC0x' - - $result = Test-VSTeamMembership -MemberDescriptor $UserDescriptor -ContainerDescriptor $GroupDescriptor - It 'Should test membership' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Method -eq "Head" -and - $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/memberships/$UserDescriptor/$GroupDescriptor*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Graph)*" - } - $result | Should Be $true - } - } - - Context 'Add-VSTeamMembership' { - Mock Invoke-RestMethod { } -Verifiable - - $UserDescriptor = 'aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj' - $GroupDescriptor = 'vssgp.Uy0xLTktMTU1MTM3NDI0NS04NTYwMDk3MjYtNDE5MzQ0MjExNy0yMzkwNzU2MTEwLTI3NDAxNjE4MjEtMC0wLTAtMC0x' - - $null = Add-VSTeamMembership -MemberDescriptor $UserDescriptor -ContainerDescriptor $GroupDescriptor - It 'Should add membership' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Method -eq "Put" -and - $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/memberships/$UserDescriptor/$GroupDescriptor*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Graph)*" - } - } - } - - Context 'Remove-VSTeamMembership' { - Mock Invoke-RestMethod { } -Verifiable - - $UserDescriptor = 'aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj' - $GroupDescriptor = 'vssgp.Uy0xLTktMTU1MTM3NDI0NS04NTYwMDk3MjYtNDE5MzQ0MjExNy0yMzkwNzU2MTEwLTI3NDAxNjE4MjEtMC0wLTAtMC0x' - - $null = Remove-VSTeamMembership -MemberDescriptor $UserDescriptor -ContainerDescriptor $GroupDescriptor -Force - It 'Should remove a membership' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Method -eq "Delete" -and - $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/memberships/$UserDescriptor/$GroupDescriptor*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Graph)*" - } - } - } - - Context 'Get-VSTeamMembership for Member' { - Mock Invoke-RestMethod { } -Verifiable - - $UserDescriptor = 'aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj' - - $null = Get-VSTeamMembership -MemberDescriptor $UserDescriptor - It "Should get a container's members" { - # This should stop the call to cache projects - [VSTeamProjectCache]::timestamp = (Get-Date).Minute - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Method -eq "Get" -and - $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/memberships/$MemberDescriptor*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Graph)*" - } - } - } - - Context 'Get-VSTeamMembership for Group' { - Mock Invoke-RestMethod { } -Verifiable - - $GroupDescriptor = 'vssgp.Uy0xLTktMTU1MTM3NDI0NS04NTYwMDk3MjYtNDE5MzQ0MjExNy0yMzkwNzU2MTEwLTI3NDAxNjE4MjEtMC0wLTAtMC0x' - - $null = Get-VSTeamMembership -ContainerDescriptor $GroupDescriptor - It "Should get a container's members" { - # This should stop the call to cache projects - [VSTeamProjectCache]::timestamp = (Get-Date).Minute - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Method -eq "Get" -and - $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/memberships/$GroupDescriptor*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Graph)*" -and - $Uri -like "*direction=Down*" - } - } - } - } -} diff --git a/unit/test/permissions.Test.ps1 b/unit/test/permissions.Test.ps1 deleted file mode 100644 index e69de29bb..000000000 diff --git a/unit/test/policies.Tests.ps1 b/unit/test/policies.Tests.ps1 deleted file mode 100644 index 1f0322238..000000000 --- a/unit/test/policies.Tests.ps1 +++ /dev/null @@ -1,233 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe 'Policies VSTS' { - $results = [PSCustomObject]@{ - value = [PSCustomObject]@{ } - } - - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Get-VSTeamPolicy by project' { - Mock Invoke-RestMethod { return $results } -Verifiable - - Get-VSTeamPolicy -ProjectName Demo - - It 'Should return policies' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/configurations?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Get-VSTeamPolicy by project and id' { - Mock Invoke-RestMethod { return $results } -Verifiable - - Get-VSTeamPolicy -ProjectName Demo -Id 4 - - It 'Should return the policy' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/configurations/4?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Get-VSTeamPolicy by project throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Get-VSTeamPolicy -ProjectName Demo } | Should Throw - } - } - - Context 'Get-VSTeamPolicy by project and id throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Get-VSTeamPolicy -ProjectName Demo -Id 4 } | Should Throw - } - } - - Context 'Remove-VSTeamPolicy by id' { - Mock Invoke-RestMethod - - Remove-VSTeamPolicy -ProjectName Demo -id 4 -Force - - It 'Should delete the policy' { - Assert-MockCalled Invoke-RestMethod -ParameterFilter { - $Method -eq 'Delete' -and - $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/configurations/4?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Remove-VSTeamPolicy by id throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Remove-VSTeamPolicy -ProjectName Demo -id 4 -Force } | Should Throw - } - } - - Context 'Add-VSTeamPolicy' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - #Write-Host $args - } - - Add-VSTeamPolicy -ProjectName Demo -type babcf51f-d853-43a2-9b05-4a64ca577be0 -enabled -blocking -settings @{ - MinimumApproverCount = 1 - scope = @( - @{ - refName = 'refs/heads/master' - matchKind = 'Exact' - repositoryId = '10000000-0000-0000-0000-0000000000001' - }) - } - - It 'Should add the policy' { - # With PowerShell core the order of the boty string is not the - # same from run to run! So instead of testing the entire string - # matches I have to search for the portions I expect but can't - # assume the order. - # The general string should look like this: - # '{"isBlocking":true,"isEnabled":true,"type":{"id":"babcf51f-d853-43a2-9b05-4a64ca577be0"},"settings":{"scope":[{"repositoryId":"10000000-0000-0000-0000-0000000000001","matchKind":"Exact","refName":"refs/heads/master"}],"MinimumApproverCount":1}}' - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/configurations?api-version=$([VSTeamVersions]::Core)" -and - $Body -like '*"isBlocking":true*' -and - $Body -like '*"isEnabled":true*' -and - $Body -like '*"type":{"id":"babcf51f-d853-43a2-9b05-4a64ca577be0"}*' -and - $Body -like '*"MinimumApproverCount":1*' -and - $Body -like '*"settings":*' -and - $Body -like '*"scope":*' -and - $Body -like '*"repositoryId":"10000000-0000-0000-0000-0000000000001"*' -and - $Body -like '*"matchKind":"Exact"*' -and - $Body -like '*"refName":"refs/heads/master"*' - } - } - } - - Context 'Add-VSTeamPolicy throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Add-VSTeamPolicy -ProjectName Demo -type babcf51f-d853-43a2-9b05-4a64ca577be0 -enabled -blocking -settings @{ - MinimumApproverCount = 1 - scope = @( - @{ - refName = 'refs/heads/master' - matchKind = 'Exact' - repositoryId = '10000000-0000-0000-0000-0000000000001' - }) - } - } | Should Throw - } - } - - Context 'Update-VSTeamPolicy with type' { - Mock Invoke-RestMethod - - Update-VSTeamPolicy -ProjectName Demo -id 1 -type babcf51f-d853-43a2-9b05-4a64ca577be0 -enabled -blocking -settings @{ - MinimumApproverCount = 1 - scope = @( - @{ - refName = 'refs/heads/release' - matchKind = 'Exact' - repositoryId = '20000000-0000-0000-0000-0000000000002' - }) - } - - It 'Should add the policy' { - # With PowerShell core the order of the boty string is not the - # same from run to run! So instead of testing the entire string - # matches I have to search for the portions I expect but can't - # assume the order. - # The general string should look like this: - #'{"isBlocking":true,"isEnabled":true,"type":{"id":"babcf51f-d853-43a2-9b05-4a64ca577be0"},"settings":{"scope":[{"repositoryId":"20000000-0000-0000-0000-0000000000002","matchKind":"Exact","refName":"refs/heads/release"}],"MinimumApproverCount":1}}' - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { - $Method -eq 'Put' -and - $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/configurations/1?api-version=$([VSTeamVersions]::Core)" -and - $Body -like '*"isBlocking":true*' -and - $Body -like '*"isEnabled":true*' -and - $Body -like '*"type":*' -and - $Body -like '*"id":"babcf51f-d853-43a2-9b05-4a64ca577be0"*' -and - $Body -like '*"settings":*' -and - $Body -like '*"scope":*' -and - $Body -like '*"repositoryId":"20000000-0000-0000-0000-0000000000002"*' -and - $Body -like '*"matchKind":"Exact"*' -and - $Body -like '*"refName":"refs/heads/release"*' -and - $Body -like '*"MinimumApproverCount":1*' - } - } - } - - Context 'Update-VSTeamPolicy without type' { - Mock Invoke-RestMethod - Mock Get-VSTeamPolicy { - return @{ - type = @{ - id = 'babcf51f-d853-43a2-9b05-4a64ca577be0' - } - } - } - - Update-VSTeamPolicy -ProjectName Demo -id 1 -enabled -blocking -settings @{ - MinimumApproverCount = 1 - scope = @( - @{ - refName = 'refs/heads/release' - matchKind = 'Exact' - repositoryId = '20000000-0000-0000-0000-0000000000002' - }) - } - - It 'Should add the policy' { - # With PowerShell core the order of the boty string is not the - # same from run to run! So instead of testing the entire string - # matches I have to search for the portions I expect but can't - # assume the order. - # The general string should look like this: - #'{"isBlocking":true,"isEnabled":true,"type":{"id":"babcf51f-d853-43a2-9b05-4a64ca577be0"},"settings":{"scope":[{"repositoryId":"20000000-0000-0000-0000-0000000000002","matchKind":"Exact","refName":"refs/heads/release"}],"MinimumApproverCount":1}}' - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { - $Method -eq 'Put' -and - $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/configurations/1?api-version=$([VSTeamVersions]::Core)" -and - $Body -like '*"isBlocking":true*' -and - $Body -like '*"isEnabled":true*' -and - $Body -like '*"type":*' -and - $Body -like '*"id":"babcf51f-d853-43a2-9b05-4a64ca577be0"*' -and - $Body -like '*"settings":*' -and - $Body -like '*"scope":*' -and - $Body -like '*"repositoryId":"20000000-0000-0000-0000-0000000000002"*' -and - $Body -like '*"matchKind":"Exact"*' -and - $Body -like '*"refName":"refs/heads/release"*' -and - $Body -like '*"MinimumApproverCount":1*' - } - } - } - - Context 'Update-VSTeamPolicy throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should add the policy' { - { Update-VSTeamPolicy -ProjectName Demo -id 1 -type babcf51f-d853-43a2-9b05-4a64ca577be0 -enabled -blocking -settings @{ - MinimumApproverCount = 1 - scope = @( - @{ - refName = 'refs/heads/release' - matchKind = 'Exact' - repositoryId = '20000000-0000-0000-0000-0000000000002' - }) - } - } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/policyTypes.Tests.ps1 b/unit/test/policyTypes.Tests.ps1 deleted file mode 100644 index b3e798bac..000000000 --- a/unit/test/policyTypes.Tests.ps1 +++ /dev/null @@ -1,60 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe "Policies VSTS" { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - $results = [PSCustomObject]@{ - value = [PSCustomObject]@{ } - } - - $singleResult = [PSCustomObject]@{ } - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Get-VSTeamPolicyType by project' { - Mock Invoke-RestMethod { return $results } -Verifiable - - Get-VSTeamPolicyType -ProjectName Demo - - It 'Should return policies' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/types?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Get-VSTeamPolicyType by project throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Get-VSTeamPolicyType -ProjectName Demo } | Should Throw - } - } - - Context 'Get-VSTeamPolicyType by id' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable - - Get-VSTeamPolicyType -ProjectName Demo -id 90a51335-0c53-4a5f-b6ce-d9aff3ea60e0 - - It 'Should return policies' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/Demo/_apis/policy/types/90a51335-0c53-4a5f-b6ce-d9aff3ea60e0?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Get-VSTeamPolicyType by id throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should return policies' { - { Get-VSTeamPolicyType -ProjectName Demo -id 90a51335-0c53-4a5f-b6ce-d9aff3ea60e0 } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/pools.Tests.ps1 b/unit/test/pools.Tests.ps1 deleted file mode 100644 index e0d8bc902..000000000 --- a/unit/test/pools.Tests.ps1 +++ /dev/null @@ -1,75 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe 'pools' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - [VSTeamVersions]::DistributedTask = '1.0-unitTest' - - $hostedPool = [PSCustomObject]@{ - owner = [PSCustomObject]@{ - displayName = 'Test User' - id = '1' - uniqueName = 'test@email.com' - } - createdBy = [PSCustomObject]@{ - displayName = 'Test User' - id = '1' - uniqueName = 'test@email.com' - } - id = 1 - size = 1 - isHosted = $true - Name = 'Hosted' - } - - $privatePool = [PSCustomObject]@{ - owner = [PSCustomObject]@{ - displayName = 'Test User' - id = '1' - uniqueName = 'test@email.com' - } - createdBy = [PSCustomObject]@{ - displayName = 'Test User' - id = '1' - uniqueName = 'test@email.com' - } - id = 1 - size = 1 - isHosted = $false - Name = 'Default' - } - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Get-VSTeamPool with no parameters' { - Mock Invoke-RestMethod { return [PSCustomObject]@{ - count = 1 - value = $privatePool - } - } - - it 'Should return all the pools' { - Get-VSTeamPool - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - - Context 'Get-VSTeamPool with id parameter' { - Mock Invoke-RestMethod { return $hostedPool } - - it 'Should return all the pools' { - Get-VSTeamPool -id '1' - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/pools/1?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - } -} \ No newline at end of file diff --git a/unit/test/profile.Tests.ps1 b/unit/test/profile.Tests.ps1 deleted file mode 100644 index 05867fa7e..000000000 --- a/unit/test/profile.Tests.ps1 +++ /dev/null @@ -1,329 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe 'Profile' { - $expectedPath = "$HOME/vsteam_profiles.json" - $contents = @" - [ - { - "Name": "http://localhost:8080/tfs/defaultcollection", - "URL": "http://localhost:8080/tfs/defaultcollection", - "Pat": "", - "Type": "OnPremise", - "Version": "TFS2017" - }, - { - "Name": "http://192.168.1.3:8080/tfs/defaultcollection", - "URL": "http://192.168.1.3:8080/tfs/defaultcollection", - "Pat": "OnE2cXpseHk0YXp3dHpz", - "Type": "Pat", - "Version": "TFS2017" - }, - { - "Name": "test", - "URL": "https://dev.azure.com/test", - "Pat": "OndrejR0ZHpwbDM3bXUycGt5c3hm", - "Type": "Pat", - "Version": "VSTS" - } - ] -"@ - - Context 'Remove-VSTeamProfile' { - Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and ([string]$Value -eq '')} - Mock Set-Content { } - Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } - - Remove-VSTeamProfile test - - It 'Should save profile to disk' { - Assert-VerifiableMock - } - } - - Context 'Remove-VSTeamProfile entry does not exist' { - Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*https://dev.azure.com/test*" } - Mock Set-Content { } - Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } - - Remove-VSTeamProfile demos - - It 'Should save profile to disk' { - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamProfile on Windows no data provided' { - Mock Set-Content { } - Mock Write-Error { } - Mock Get-VSTeamProfile { } - Mock _isOnWindows { return $true } - Mock _convertSecureStringTo_PlainText { return '' } - - $emptySecureString = ConvertTo-SecureString 'does not matter because mock is going to return empty string' -AsPlainText -Force - - Add-VSTeamProfile -Account testing -SecurePersonalAccessToken $emptySecureString - - It 'Should save profile to disk' { - Assert-MockCalled Set-Content -Exactly -Scope Context -Times 0 - Assert-MockCalled Write-Error -Exactly -Scope Context -Times 1 ` - -ParameterFilter { - $Message -eq 'Personal Access Token must be provided if you are not using Windows Authentication; please see the help.' - } - } - } - - Context 'Add-VSTeamProfile with PAT to empty file' { - Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*https://dev.azure.com/demos*" -and $Value -like "*VSTS*" } - Mock Set-Content { } - Mock Get-VSTeamProfile { } - - Add-VSTeamProfile -Account demos -PersonalAccessToken 12345 - - It 'Should save profile to disk' { - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamProfile with PAT to empty array' { - Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*https://dev.azure.com/demos*" -and $Value -like "*VSTS*" } - Mock Set-Content { } - Mock Get-VSTeamProfile { } - - Add-VSTeamProfile -Account demos -PersonalAccessToken 12345 -Version VSTS - - It 'Should save profile to disk' { - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamProfile with OAuth to empty array' { - Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*https://dev.azure.com/demos*" -and $Value -like "*VSTS*" } - Mock Set-Content { } - Mock Get-VSTeamProfile { } - - Add-VSTeamProfile -Account demos -PersonalAccessToken 12345 -Version VSTS -UseBearerToken - - It 'Should save profile to disk' { - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamProfile with PAT exisiting entry' { - Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*https://dev.azure.com/demos*" -and $Value -like "*https://dev.azure.com/test*" -and $Value -like "*TFS2018*" } - Mock Set-Content { } - Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } - - Add-VSTeamProfile -Account demos -PersonalAccessToken 12345 -Version TFS2018 - - It 'Should save profile to disk' { - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamProfile TFS default to TFS2017 with Windows Auth' { - Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*OnPremise*" -and $Value -like "*http://localhost:8080/tfs/defaultcollection*" -and $Value -like "*TFS2017*" } - Mock Set-Content { } - Mock _isOnWindows { return $true } - Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test/","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } - - Add-VSTeamProfile -Account http://localhost:8080/tfs/defaultcollection -UseWindowsAuthentication - - It 'Should save profile to disk' { - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamProfile TFS default to TFS2017' { - Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*OjY3ODkxMA==*" -and $Value -like "*http://localhost:8080/tfs/defaultcollection*" -and $Value -like "*TFS2017*" } - Mock Set-Content { } - Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test/","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } - - Add-VSTeamProfile -Account http://localhost:8080/tfs/defaultcollection -PersonalAccessToken 678910 - - It 'Should save profile to disk' { - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamProfile with PAT replace exisiting entry' { - Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*OjY3ODkxMA==*" -and $Value -like "*https://dev.azure.com/test*" -and $Value -like "*VSTS*" } - Mock Set-Content { } - Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test/","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } - - Add-VSTeamProfile -Account test -PersonalAccessToken 678910 - - It 'Should save profile to disk' { - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamProfile with existing old URL' { - Mock Test-Path { return $true } - Mock Get-Content { return '[{"Name":"test","URL":"https://test.visualstudio.com","Type":"Pat","Pat":"12345","Version":"VSTS"}]' } - Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*OjY3ODkxMA==*" -and $Value -like "*https://dev.azure.com/test*" -and $Value -like "*VSTS*" } - Mock Set-Content { } - - Add-VSTeamProfile -Account test -PersonalAccessToken 678910 - - It 'Should save profile with new URL to disk' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamProfile empty profiles file' { - Mock Test-Path { return $true } - Mock Get-Content { return '' } - - $actual = Get-VSTeamProfile - - It 'Should return 0 profiles' { - $actual | Should BeNullOrEmpty - } - } - - Context 'Get-VSTeamProfile invalid profiles file' { - Mock Test-Path { return $true } - Mock Get-Content { return 'Not Valid JSON. This might happen if someone touches the file.' } - Mock Write-Error { } -Verifiable - - $actual = Get-VSTeamProfile - - It 'Should return 0 profiles' { - $actual | Should BeNullOrEmpty - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamProfile no profiles' { - Mock Test-Path { return $false } - - $actual = Get-VSTeamProfile - - It 'Should return 0 profiles' { - $actual | Should BeNullOrEmpty - } - } - - Context 'Get-VSTeamProfile by name' { - - Mock Test-Path { return $true } - Mock Get-Content { return $contents } - - $actual = Get-VSTeamProfile test - - It 'Should return 1 profile' { - $actual.URL | Should be 'https://dev.azure.com/test' - } - - It 'Profile Should by Pat' { - $actual.Type | Should be 'Pat' - } - - It 'Token Should be empty string' { - # This is testing that the Token property is added - # to existing profiles loaded from file created before - # the bearer token support was added. - $actual.Token | Should be '' - } - } - - Context 'Get-VSTeamProfile' { - Mock Test-Path { return $true } - Mock Get-Content { return $contents } - - $actual = Get-VSTeamProfile - - It 'Should return 3 profiles' { - $actual.Length | Should be 3 - } - - It '1st profile Should by OnPremise' { - $actual[0].Type | Should be 'OnPremise' - } - } - - Context 'Get-VSTeamProfile with old URL' { - Mock Test-Path { return $true } - Mock Get-Content { return '[{"Name":"test","URL":"https://test.visualstudio.com","Type":"Pat","Pat":"12345","Version":"VSTS"}]' } - - $actual = Get-VSTeamProfile - - It 'Should return new URL' { - $actual.URL | Should Be "https://dev.azure.com/test" - } - } - - Context 'Get-VSTeamProfile with old URL and multiple entries' { - Mock Test-Path { return $true } - Mock Get-Content { return '[{"Name":"test","URL":"https://test.visualstudio.com","Type":"Pat","Pat":"12345","Version":"VSTS"},{"Name":"demo","URL":"https://demo.visualstudio.com","Type":"Pat","Pat":"12345","Version":"VSTS"}]' } - - $actual = Get-VSTeamProfile -Name "test" - - It 'Should return new URL' { - $actual.URL | Should Be "https://dev.azure.com/test" - } - } - - Context 'Update-VSTeamProfile entry does not exist' { - Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } - - It 'Should throw' { - { Update-VSTeamProfile -Name Testing -PersonalAccessToken 678910 } | Should -Throw - } - } - - Context 'Update-VSTeamProfile with PAT to empty file' { - Mock Set-Content { } - Mock Write-Warning -Verifiable - Mock Get-VSTeamProfile { } - - Update-VSTeamProfile -name demos -PersonalAccessToken 12345 - - It 'Should save profile to disk' { - Assert-VerifiableMock - } - } - - Context 'Update-VSTeamProfile with securePersonalAccessToken' { - Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*OjY3ODkxMA==*" -and $Value -like "*https://dev.azure.com/test*" -and $Value -like "*VSTS*" } - Mock Set-Content { } - Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test/","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } - - $password = '678910' | ConvertTo-SecureString -AsPlainText -Force - - Update-VSTeamProfile -Name test -SecurePersonalAccessToken $password - - It 'Should update profile' { - Assert-VerifiableMock - } - } - - Context 'Update-VSTeamProfile with PAT' { - Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*OjY3ODkxMA==*" -and $Value -like "*https://dev.azure.com/test*" -and $Value -like "*VSTS*" } - Mock Set-Content { } - Mock Get-VSTeamProfile { return '[{"Name":"test","URL":"https://dev.azure.com/test/","Type":"Pat","Pat":"12345","Version":"VSTS"}]' | ConvertFrom-Json | ForEach-Object { $_ } } - - Update-VSTeamProfile -Name test -PersonalAccessToken 678910 - - It 'Should update profile' { - Assert-VerifiableMock - } - } - - Context 'Update-VSTeamProfile with old URL' { - Mock Test-Path { return $true } - Mock Get-Content { return '[{"Name":"test","URL":"https://test.visualstudio.com","Type":"Pat","Pat":"12345","Version":"VSTS"}]' } - Mock Set-Content { } -Verifiable -ParameterFilter { $Path -eq $expectedPath -and $Value -like "*OjY3ODkxMA==*" -and $Value -like "*https://dev.azure.com/test*" -and $Value -like "*VSTS*" } - Mock Set-Content { } - - Update-VSTeamProfile -Name test -PersonalAccessToken 678910 - - It 'Should update profile with new URL' { - Assert-VerifiableMock - } - } - - - } -} \ No newline at end of file diff --git a/unit/test/projectPermissions.Tests.ps1 b/unit/test/projectPermissions.Tests.ps1 deleted file mode 100644 index 16b80d774..000000000 --- a/unit/test/projectPermissions.Tests.ps1 +++ /dev/null @@ -1,118 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe 'ProjectPermissions VSTS' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - $userSingleResult = Get-Content "$PSScriptRoot\sampleFiles\users.single.json" -Raw | ConvertFrom-Json - $userSingleResultObject = [VSTeamUser]::new($userSingleResult) - - $groupSingleResult = Get-Content "$PSScriptRoot\sampleFiles\groupsSingle.json" -Raw | ConvertFrom-Json - $groupSingleResultObject = [VSTeamGroup]::new($groupSingleResult) - - $projectResult = [PSCustomObject]@{ - name = 'Test Project Public' - description = '' - url = '' - id = '010d06f0-00d5-472a-bb47-58947c230876' - state = '' - visibility = '' - revision = 0 - defaultTeam = [PSCustomObject]@{} - _links = [PSCustomObject]@{} - } - - $projectResultObject = [VSTeamProject]::new($projectResult) - - $accessControlEntryResult = - @" -{ - "count": 1, - "value": [ - { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", - "allow": 8, - "deny": 0, - "extendedInfo": {} - } - ] -} -"@ | ConvertFrom-Json - - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Core = '' - [VSTeamVersions]::Core = '5.0' - - Context 'Add-VSTeamProjectPermission by ProjectUser' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $accessControlEntryResult - } -Verifiable - - Add-VSTeamProjectPermission -Project $projectResultObject -User $userSingleResultObject -Allow ([VSTeamProjectPermissions]'GENERIC_READ,GENERIC_WRITE,WORK_ITEM_DELETE,RENAME') -Deny ([VSTeamProjectPermissions]'CHANGE_PROCESS,VIEW_TEST_RESULTS') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/52d39943-cb85-4d7f-8fa8-c6baac873819*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"`$PROJECT:vstfs:///Classification/TeamProject/010d06f0-00d5-472a-bb47-58947c230876`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and - $Body -like "*`"allow`": 73731,*" -and - $Body -like "*`"deny`": 8389120,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamProjectPermission by ProjectGroup' { - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamProjectPermission -Project $projectResultObject -Group $groupSingleResultObject -Allow ([VSTeamProjectPermissions]'GENERIC_READ,GENERIC_WRITE,WORK_ITEM_DELETE,RENAME') -Deny ([VSTeamProjectPermissions]'CHANGE_PROCESS,VIEW_TEST_RESULTS') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/52d39943-cb85-4d7f-8fa8-c6baac873819*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"`$PROJECT:vstfs:///Classification/TeamProject/010d06f0-00d5-472a-bb47-58947c230876`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 73731,*" -and - $Body -like "*`"deny`": 8389120,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamProjectPermission by ProjectDescriptor' { - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamProjectPermission -Project $projectResultObject -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamProjectPermissions]'GENERIC_READ,GENERIC_WRITE,WORK_ITEM_DELETE,RENAME') -Deny ([VSTeamProjectPermissions]'CHANGE_PROCESS,VIEW_TEST_RESULTS') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/52d39943-cb85-4d7f-8fa8-c6baac873819*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"`$PROJECT:vstfs:///Classification/TeamProject/010d06f0-00d5-472a-bb47-58947c230876`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 73731,*" -and - $Body -like "*`"deny`": 8389120,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamProjectPermission by Project throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Add-VSTeamProjectPermission -Project $projectResultObject -User $userSingleResultObject -Allow ([VSTeamProjectPermissions]'GENERIC_READ,GENERIC_WRITE,WORK_ITEM_DELETE,RENAME') -Deny ([VSTeamProjectPermissions]'CHANGE_PROCESS,VIEW_TEST_RESULTS') } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/projects.Tests.ps1 b/unit/test/projects.Tests.ps1 deleted file mode 100644 index 46c0cfb2e..000000000 --- a/unit/test/projects.Tests.ps1 +++ /dev/null @@ -1,433 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe 'Project' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" - . "$PSScriptRoot\mocks\mockProcessNameDynamicParam.ps1" - - Context 'Show-VSTeamProject by ID' { - Mock Show-Browser - - It 'Show call start' { - Show-VSTeamProject -Id 123456 - - Assert-MockCalled Show-Browser - } - } - - Context 'Show-VSTeamProject by ProjectName' { - Mock Show-Browser - - It 'Show call open' { - Show-VSTeamProject -ProjectName ShowProject - - Assert-MockCalled Show-Browser - } - } - - Context 'Show-VSTeamProject by default parameter' { - Mock Show-Browser - - It 'Show call open' { - Show-VSTeamProject ShowProject - - Assert-MockCalled Show-Browser - } - } - - $results = [PSCustomObject]@{ - value = [PSCustomObject]@{ - name = 'Test' - description = '' - url = '' - id = '123-5464-dee43' - state = '' - visibility = '' - revision = 0 - _links = [PSCustomObject]@{} - } - } - - $singleResult = [PSCustomObject]@{ - name = 'Test' - description = '' - url = '' - id = '123-5464-dee43' - state = '' - visibility = '' - revision = 0 - defaultTeam = [PSCustomObject]@{} - _links = [PSCustomObject]@{} - } - - Context 'Get-VSTeamProject with no parameters using BearerToken' { - - BeforeAll { - $env:TEAM_TOKEN = '1234' - } - - AfterAll { - $env:TEAM_TOKEN = $null - } - - Mock Invoke-RestMethod { return $results } - - It 'Should return projects' { - Get-VSTeamProject - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/projects*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*`$top=100*" -and - $Uri -like "*stateFilter=WellFormed*" - } - } - } - - Context 'Get-VSTeamProject with top 10' { - - Mock Invoke-RestMethod { return $results } - - It 'Should return top 10 projects' { - Get-VSTeamProject -top 10 - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/projects*" -and - $Uri -like "*`$top=10*" -and - $Uri -like "*stateFilter=WellFormed*" - } - } - } - - Context 'Get-VSTeamProject with skip 1' { - - Mock Invoke-RestMethod { return $results } - - It 'Should skip first project' { - Get-VSTeamProject -skip 1 - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/projects*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*`$skip=1*" -and - $Uri -like "*`$top=100*" -and - $Uri -like "*stateFilter=WellFormed*" - } - } - } - - Context 'Get-VSTeamProject with stateFilter All' { - - Mock Invoke-RestMethod { return $results } - - It 'Should return All projects' { - Get-VSTeamProject -stateFilter 'All' - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/projects*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*`$top=100*" -and - $Uri -like "*stateFilter=All*" - } - } - } - - Context 'Get-VSTeamProject with no Capabilities' { - - Mock Invoke-RestMethod { return $singleResult } - - It 'Should return the project' { - Get-VSTeamProject -Name Test - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Get-VSTeamProject with Capabilities' { - - Mock Invoke-RestMethod { return $singleResult } - - It 'Should return the project with capabilities' { - Get-VSTeamProject -projectId Test -includeCapabilities - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/projects/Test*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*includeCapabilities=True*" - } - } - } - - Context 'Get-VSTeamProject with no parameters throws' { - - Mock Invoke-RestMethod { throw 'error' } - - It 'Should return projects' { - { Get-VSTeamProject } | Should Throw - } - } - - Context 'Update-VSTeamProject with no op by id' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should not call Invoke-RestMethod' { - Update-VSTeamProject -id '123-5464-dee43' - - Assert-MockCalled Invoke-RestMethod -Exactly 0 - } - } - - Context 'Update-VSTeamProject with newName' { - - Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" } - Mock Invoke-RestMethod { return @{status = 'inProgress'; url = 'https://someplace.com'} } -ParameterFilter { $Method -eq 'Patch'} - Mock _trackProjectProgress - Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Testing123?api-version=$([VSTeamVersions]::Core)" } - - It 'Should change name' { - Update-VSTeamProject -ProjectName Test -newName Testing123 -Force - - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" } - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Method -eq 'Patch' -and $Body -eq '{"name": "Testing123"}'} - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Testing123?api-version=$([VSTeamVersions]::Core)" } - } - } - - Context 'Update-VSTeamProject with newDescription' { - - Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" } - Mock Invoke-RestMethod { return @{status = 'inProgress'; url = 'https://someplace.com'} } -ParameterFilter { $Method -eq 'Patch'} - Mock _trackProjectProgress - - It 'Should change description' { - Update-VSTeamProject -ProjectName Test -newDescription Testing123 -Force - - Assert-MockCalled Invoke-RestMethod -Times 2 -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" } - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Method -eq 'Patch' -and $Body -eq '{"description": "Testing123"}' } - } - } - - Context 'Update-VSTeamProject with new name and description' { - - Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" } - Mock Invoke-RestMethod { return @{status = 'inProgress'; url = 'https://someplace.com'} } -ParameterFilter { $Method -eq 'Patch'} - Mock _trackProjectProgress - Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Testing123?api-version=$([VSTeamVersions]::Core)" } - - It 'Should not call Invoke-RestMethod' { - Update-VSTeamProject -ProjectName Test -newName Testing123 -newDescription Testing123 -Force - - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" } - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Method -eq 'Patch'} - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Testing123?api-version=$([VSTeamVersions]::Core)" } - } - } - - Context 'Remove-VSTeamProject with Force' { - - Mock Write-Progress - Mock _trackProjectProgress - Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" } - Mock Invoke-RestMethod { return @{status = 'inProgress'; url = 'https://someplace.com'} } -ParameterFilter { $Method -eq 'Delete' -and $Uri -eq "https://dev.azure.com/test/_apis/projects/123-5464-dee43?api-version=$([VSTeamVersions]::Core)"} - - It 'Should not call Invoke-RestMethod' { - Remove-VSTeamProject -ProjectName Test -Force - - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" } - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Method -eq 'Delete' -and $Uri -eq "https://dev.azure.com/test/_apis/projects/123-5464-dee43?api-version=$([VSTeamVersions]::Core)"} - } - } - - Context 'Add-VSTeamProject with tfvc' { - Mock Write-Progress - # Add Project - Mock Invoke-RestMethod { return @{status = 'inProgress'; id = '123-5464-dee43'; url = 'https://someplace.com'} } -ParameterFilter { - $Method -eq 'Post' -and - $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$([VSTeamVersions]::Core)" - } - - # Track Progress - Mock Invoke-RestMethod { - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($i -gt 9) { - return @{status = 'succeeded'} - } - - return @{status = 'inProgress' } - } -ParameterFilter { - $Uri -eq 'https://someplace.com' - } - - # Get-VSTeamProject - Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" - } - - It 'Should create project with tfvc' { - Add-VSTeamProject -Name Test -tfvc - - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" - } - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$([VSTeamVersions]::Core)" -and - $Body -eq '{"name": "Test", "description": "", "capabilities": {"versioncontrol": { "sourceControlType": "Tfvc"}, "processTemplate":{"templateTypeId": "6b724908-ef14-45cf-84f8-768b5384da45"}}}' - } - } - } - - Context 'Add-VSTeamProject with Agile' { - - Mock Invoke-RestMethod { return @{status = 'inProgress'; id = 1; url = 'https://someplace.com'} } -ParameterFilter { $Method -eq 'Post' -and $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$([VSTeamVersions]::Core)"} - Mock _trackProjectProgress - Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" } - Mock Get-VSTeamProcess { return @{name = 'Agile'; id = 1}} - - It 'Should create project with Agile' { - Add-VSTeamProject -ProjectName Test -processTemplate Agile - - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" } - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Method -eq 'Post' -and $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$([VSTeamVersions]::Core)"} - } - } - - Context 'Add-VSTeamProject with CMMI' { - - Mock Invoke-RestMethod { return @{status = 'inProgress'; id = 1; url = 'https://someplace.com'} } -ParameterFilter { $Method -eq 'Post' -and $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$([VSTeamVersions]::Core)"} - Mock _trackProjectProgress - Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" } - Mock Get-VSTeamProcess { return @{name = 'CMMI'; id = 1}} - - It 'Should create project with CMMI' { - Add-VSTeamProject -ProjectName Test -processTemplate CMMI - - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" } - Assert-MockCalled Invoke-RestMethod -Times 1 -ParameterFilter { $Method -eq 'Post' -and $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$([VSTeamVersions]::Core)"} - } - } - - Context 'Add-VSTeamProject throws error' { - - Mock Invoke-RestMethod { return @{status = 'inProgress'; id = 1; url = 'https://someplace.com'} } -ParameterFilter { $Method -eq 'Post' -and $Uri -eq "https://dev.azure.com/test/_apis/projects?api-version=$([VSTeamVersions]::Core)"} - Mock Write-Error - Mock _trackProjectProgress { throw 'Test error' } - Mock Invoke-RestMethod { return $singleResult } -ParameterFilter { $Uri -eq "https://dev.azure.com/test/_apis/projects/Test?api-version=$([VSTeamVersions]::Core)" } - - It 'Should throw' { { Add-VSTeamProject -projectName Test -processTemplate CMMI } | Should throw - } - } - - Context 'Set-VSTeamDefaultProject' { - AfterAll { - $Global:PSDefaultParameterValues.Remove("*:projectName") - } - - It 'should set default project' { - Set-VSTeamDefaultProject 'DefaultProject' - - $Global:PSDefaultParameterValues['*:projectName'] | Should be 'DefaultProject' - } - - It 'should update default project' { - $Global:PSDefaultParameterValues['*:projectName'] = 'DefaultProject' - - Set-VSTeamDefaultProject -Project 'NextProject' - - $Global:PSDefaultParameterValues['*:projectName'] | Should be 'NextProject' - } - } - - Context 'Set-VSTeamDefaultProject on Non Windows' { - AfterAll { - $Global:PSDefaultParameterValues.Remove("*:projectName") - } - - Mock _isOnWindows { return $false } -Verifiable - - It 'should set default project' { - Set-VSTeamDefaultProject 'MyProject' - - Assert-VerifiableMock - $Global:PSDefaultParameterValues['*:projectName'] | Should be 'MyProject' - } - } - - Context 'Set-VSTeamDefaultProject As Admin on Windows' { - AfterAll { - $Global:PSDefaultParameterValues.Remove("*:projectName") - } - - Mock _isOnWindows { return $true } - Mock _testAdministrator { return $true } -Verifiable - - It 'should set default project' { - Set-VSTeamDefaultProject 'MyProject' - - Assert-VerifiableMock - $Global:PSDefaultParameterValues['*:projectName'] | Should be 'MyProject' - } - } - - Context 'Clear-VSTeamDefaultProject on Non Windows' { - AfterAll { - $Global:PSDefaultParameterValues.Remove("*:projectName") - } - - Mock _isOnWindows { return $false } -Verifiable - - It 'should clear default project' { - $Global:PSDefaultParameterValues['*:projectName'] = 'MyProject' - - Clear-VSTeamDefaultProject - - $Global:PSDefaultParameterValues['*:projectName'] | Should BeNullOrEmpty - } - } - - Context 'Clear-VSTeamDefaultProject as Non-Admin on Windows' { - AfterAll { - $Global:PSDefaultParameterValues.Remove("*:projectName") - } - - Mock _isOnWindows { return $true } - Mock _testAdministrator { return $false } - - It 'should clear default project' { - $Global:PSDefaultParameterValues['*:projectName'] = 'MyProject' - - Clear-VSTeamDefaultProject - - $Global:PSDefaultParameterValues['*:projectName'] | Should BeNullOrEmpty - } - } - - Context 'Clear-VSTeamDefaultProject as Admin on Windows' { - AfterAll { - $Global:PSDefaultParameterValues.Remove("*:projectName") - } - - Mock _isOnWindows { return $true } - Mock _testAdministrator { return $true } -Verifiable - - It 'should clear default project' { - $Global:PSDefaultParameterValues['*:projectName'] = 'MyProject' - - Clear-VSTeamDefaultProject - - $Global:PSDefaultParameterValues['*:projectName'] | Should BeNullOrEmpty - } - } - } -} \ No newline at end of file diff --git a/unit/test/pullrequest.Tests.ps1 b/unit/test/pullrequest.Tests.ps1 deleted file mode 100644 index e43b98c54..000000000 --- a/unit/test/pullrequest.Tests.ps1 +++ /dev/null @@ -1,228 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - - Describe 'Pull Requests' { - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Core = '' - [VSTeamVersions]::Git = '5.1-preview' - - $singleResult = @{ - pullRequestId = 1 - repositoryName = "testreponame" - repository = @{ - project = @{ - name = "testproject" - } - } - reviewers = @{ - vote = 0 - } - } - - $collection = @{ - value = @($singleResult) - } - - Context 'Show-VSTeamPullRequest' { - It 'Show-VSTeamPullRequest by Id' { - Mock Invoke-RestMethod { return $singleResult } - Mock Show-Browser - - Show-VSTeamPullRequest -Id 1 - - Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { - $url -eq "https://dev.azure.com/test/testproject/_git/testreponame/pullrequest/1" - } - } - - It 'Show-VSTeamPullRequest with invalid ID' { - Mock Invoke-RestMethod { return $singleResult } - Mock Show-Browser { throw } - - { Show-VSTeamPullRequest -Id 999999 } | Should throw - } - } - - Context 'Get-VSTeamPullRequest' { - - It 'Get-VSTeamPullRequest with no parameters' { - Mock Invoke-RestMethod { return $collection } - - $Global:PSDefaultParameterValues.Remove("*:ProjectName") - Get-VSTeamPullRequest - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/git/pullRequests?api-version=$([VSTeamVersions]::Git)" - } - } - - It 'Get-VSTeamPullRequest with default project name' { - Mock Invoke-RestMethod { return $collection } - - $Global:PSDefaultParameterValues["*:ProjectName"] = 'testproject' - Get-VSTeamPullRequest -ProjectName testproject - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/testproject/_apis/git/pullRequests?api-version=$([VSTeamVersions]::Git)" - } - } - - It 'Get-VSTeamPullRequest By ProjectName' { - Mock Invoke-RestMethod { return $collection } - - $Global:PSDefaultParameterValues.Remove("*:ProjectName") - Get-VSTeamPullRequest -ProjectName testproject - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/testproject/_apis/git/pullRequests?api-version=$([VSTeamVersions]::Git)" - } - } - - It 'Get-VSTeamPullRequest By ID' { - Mock Invoke-RestMethod { return $singleResult } - - Get-VSTeamPullRequest -Id 1 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/git/pullRequests/1?api-version=$([VSTeamVersions]::Git)" - } - } - - It 'Get-VSTeamPullRequest with All' { - Mock Invoke-RestMethod { return $singleResult } - - Get-VSTeamPullRequest -ProjectName Test -All - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and - $Uri -like "*Test/_apis/git*" -and - $Uri -like "*status=all*" - } - } - - It 'Get-VSTeamPullRequest with Status abandoned' { - Mock Invoke-RestMethod { return $singleResult } - - Get-VSTeamPullRequest -ProjectName Test -Status abandoned - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and - $Uri -like "*Test/_apis/git*" -and - $Uri -like "*status=abandoned*" - } - } - - It 'Get-VSTeamPullRequest with source branch' { - Mock Invoke-RestMethod { return $singleResult } - - Get-VSTeamPullRequest -ProjectName Test -SourceBranchRef "refs/heads/mybranch" - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and - $Uri -like "*Test/_apis/git*" -and - $Uri -like "*searchCriteria.sourceRefName=refs/heads/mybranch*" - } - } - - It 'Get-VSTeamPullRequest with target branch' { - Mock Invoke-RestMethod { return $singleResult } - - Get-VSTeamPullRequest -ProjectName Test -TargetBranchRef "refs/heads/mybranch" - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and - $Uri -like "*Test/_apis/git*" -and - $Uri -like "*searchCriteria.targetRefName=refs/heads/mybranch*" - } - } - - It 'Get-VSTeamPullRequest with repository id' { - Mock Invoke-RestMethod { return $singleResult } - - Get-VSTeamPullRequest -ProjectName Test -RepositoryId "93BBA613-2729-4158-9217-751E952AB4AF" - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and - $Uri -like "*Test/_apis/git*" -and - $Uri -like "*repositories/93BBA613-2729-4158-9217-751E952AB4AF*" - } - } - - - It 'Get-VSTeamPullRequest with source repository id' { - Mock Invoke-RestMethod { return $singleResult } - - Get-VSTeamPullRequest -ProjectName Test -SourceRepositoryId "93BBA613-2729-4158-9217-751E952AB4AF" - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and - $Uri -like "*Test/_apis/git*" -and - $Uri -like "*searchCriteria.sourceRepositoryId=93BBA613-2729-4158-9217-751E952AB4AF*" - } - } - - It 'Get-VSTeamPullRequest with top and skip' { - Mock Invoke-RestMethod { return $singleResult } - - Get-VSTeamPullRequest -ProjectName Test -SourceRepositoryId "93BBA613-2729-4158-9217-751E952AB4AF" -Top 100 -Skip 200 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and - $Uri -like "*Test/_apis/git*" -and - $Uri -like "*searchCriteria.sourceRepositoryId=93BBA613-2729-4158-9217-751E952AB4AF*" -and - $Uri -like "*`$top=100*" -and - $Uri -like "*`$skip=200*" - } - } - - It 'Get-VSTeamPullRequest with source branch in wrong format throws' { - Mock Invoke-RestMethod { return $singleResult } - - { Get-VSTeamPullRequest -ProjectName Test -SourceBranchRef "garbage" } | should throw - } - - It 'Get-VSTeamPullRequest with target branch in wrong format throws' { - Mock Invoke-RestMethod { return $singleResult } - - { Get-VSTeamPullRequest -ProjectName Test -TargetBranchRef "garbage" } | should throw - } - - It 'Get-VSTeamPullRequest No Votes should be Pending Status' { - Mock Invoke-RestMethod { return $singleResult } - - $pr = Get-VSTeamPullRequest -Id 1 - - $pr.reviewStatus | Should be "Pending" - } - - It 'Get-VSTeamPullRequest Postivite Votes should be Approved Status' { - Mock Invoke-RestMethod { - $result = $singleResult - $result.reviewers.vote = 10 - return $result - } - - $pr = Get-VSTeamPullRequest -Id 1 - - $pr.reviewStatus | Should be "Approved" - } - - It 'Get-VSTeamPullRequest Negative Votes should be Rejected Status' { - Mock Invoke-RestMethod { - $result = $singleResult - $result.reviewers.vote = -10 - return $result - } - - $pr = Get-VSTeamPullRequest -Id 1 - - $pr.reviewStatus | Should be "Rejected" - } - } - } -} \ No newline at end of file diff --git a/unit/test/queues.Tests.ps1 b/unit/test/queues.Tests.ps1 deleted file mode 100644 index 5f13cbf32..000000000 --- a/unit/test/queues.Tests.ps1 +++ /dev/null @@ -1,116 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe 'Queues' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - Context 'Get-VSTeamQueue with no parameters' { - Mock Invoke-RestMethod { return @{ - value = @{ - id = 3 - name = 'Hosted' - pool = @{} - } - }} - - it 'Should return all the queues' { - Get-VSTeamQueue -ProjectName project - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/queues?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - - Context 'Get-VSTeamQueue with queueName parameter' { - Mock Invoke-RestMethod { return @{ - value = @{ - id = 3 - name = 'Hosted' - pool = @{} - } - }} - - it 'Should return all the queues' { - Get-VSTeamQueue -projectName project -queueName 'Hosted' - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/queues?api-version=$([VSTeamVersions]::DistributedTask)&queueName=Hosted" - } - } - } - - Context 'Get-VSTeamQueue with actionFilter parameter' { - Mock Invoke-RestMethod { return @{ - value = @{ - id = 3 - name = 'Hosted' - pool = @{} - } - }} - - it 'Should return all the queues' { - Get-VSTeamQueue -projectName project -actionFilter 'None' - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/queues?api-version=$([VSTeamVersions]::DistributedTask)&actionFilter=None" - } - } - } - - Context 'Get-VSTeamQueue with actionFilter & queueName parameter' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return @{ - value = @{ - id = 3 - name = 'Hosted' - pool = @{} - } - }} - - it 'Should return all the queues' { - Get-VSTeamQueue -projectName project -actionFilter 'None' -queueName 'Hosted' - - # With PowerShell core the order of the query string is not the - # same from run to run! So instead of testing the entire string - # matches I have to search for the portions I expect but can't - # assume the order. - # The general string should look like this: - # "https://dev.azure.com/test/project/_apis/distributedtask/queues/?api-version=$([VSTeamVersions]::DistributedTask)&actionFilter=None&queueName=Hosted" - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/project/_apis/distributedtask/queues*" -and - $Uri -like "*api-version=$([VSTeamVersions]::DistributedTask)*" -and - $Uri -like "*actionFilter=None*" -and - $Uri -like "*queueName=Hosted*" - } - } - } - - Context 'Get-VSTeamQueue' { - Mock Invoke-RestMethod { return @{ - id = 3 - name = 'Hosted' - pool = @{} - }} - - It 'should return requested queue' { - Get-VSTeamQueue -projectName project -queueId 3 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/queues/3?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - } -} \ No newline at end of file diff --git a/unit/test/readme.md b/unit/test/readme.md index 5512c66c3..b0a838196 100644 --- a/unit/test/readme.md +++ b/unit/test/readme.md @@ -1,3 +1,14 @@ -Helpful links +# Helpful links + +## Test Structure + +Describe "Noun" + Context "Verb-Noun" + Context "Services" + Context "Server" + +## Mocking + +## Helpful Links [testing script modules with pester](https://blogs.technet.microsoft.com/heyscriptingguy/2015/12/17/testing-script-modules-with-pester/) diff --git a/unit/test/refs.Tests.ps1 b/unit/test/refs.Tests.ps1 deleted file mode 100644 index 424a7f389..000000000 --- a/unit/test/refs.Tests.ps1 +++ /dev/null @@ -1,108 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe "Git VSTS" { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - $results = [PSCustomObject]@{ - value = [PSCustomObject]@{ - objectId = '6f365a7143e492e911c341451a734401bcacadfd' - name = 'refs/heads/master' - creator = [PSCustomObject]@{ - displayName = 'Microsoft.VisualStudio.Services.TFS' - id = '1' - uniqueName = 'some@email.com' - } - } - } - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" - - Context 'Get-VSTeamGitRef' { - Mock Invoke-RestMethod { return $results } -Verifiable - - Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 - - It 'Should return a single ref by id' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitRef with Filter' { - Mock Invoke-RestMethod { return $results } -Verifiable -ParameterFilter { - $Uri -like "*filter=refs*" - } - - Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -Filter "refs/heads" - - It 'Should return a single ref with filter' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitRef with FilterContains' { - Mock Invoke-RestMethod { return $results } -Verifiable -ParameterFilter { - $Uri -like "*filterContains=test" - } - - Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -FilterContains "test" - - It 'Should return a single ref' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitRef with Top' { - Mock Invoke-RestMethod { return $results } -Verifiable -ParameterFilter { - $Uri -like "*`$top=100" - } - - Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -Top 100 - - It 'Should return a single ref' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitRef with ContinuationToken' { - Mock Invoke-RestMethod { return $results } -Verifiable -ParameterFilter { - $Uri -like "*continuationToken=myToken" - } - - Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -ContinuationToken "myToken" - - It 'Should return a single ref' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitRef with Filter, FilterContains, Top' { - Mock Invoke-RestMethod { return $results } -Verifiable -ParameterFilter { - $Uri -like "*filter=/refs/heads*" -and - $Uri -like "*`$top=500*" -and - $Uri -like "*filterContains=test*" - } - - Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 -Filter "/refs/heads" -FilterContains "test" -Top 500 - - It 'Should return a single ref' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitRef by id throws' { - Mock Invoke-RestMethod { throw [System.Net.WebException] "Test Exception." } - - It 'Should return a single repo by id' { - { Get-VSTeamGitRef -ProjectName Test -RepositoryId 00000000-0000-0000-0000-000000000000 } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/release.Tests.ps1 b/unit/test/release.Tests.ps1 deleted file mode 100644 index c1750445e..000000000 --- a/unit/test/release.Tests.ps1 +++ /dev/null @@ -1,230 +0,0 @@ -Set-StrictMode -Version Latest - -# Loading System.Web avoids issues finding System.Web.HttpUtility -Add-Type -AssemblyName 'System.Web' - -InModuleScope VSTeam { - Describe 'Releases' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - [VSTeamVersions]::Release = '1.0-unittest' - - $singleResult = [PSCustomObject]@{ - environments = [PSCustomObject]@{} - variables = [PSCustomObject]@{ - BrowserToUse = [PSCustomObject]@{ - value = "phantomjs" - } - } - _links = [PSCustomObject]@{ - self = [PSCustomObject]@{} - web = [PSCustomObject]@{} - } - } - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - Context 'Show-VSTeamRelease by ID' { - Mock Show-Browser { } - - it 'should show release' { - Show-VSTeamRelease -projectName project -Id 15 - - Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { - $url -eq 'https://dev.azure.com/test/project/_release?releaseId=15' - } - } - } - - Context 'Show-VSTeamRelease with invalid ID' { - it 'should show release' { - { Show-VSTeamRelease -projectName project -Id 0 } | Should throw - } - } - - Context 'Remove-VSTeamRelease by ID' { - Mock Invoke-RestMethod - - It 'should return releases' { - Remove-VSTeamRelease -ProjectName project -Id 15 -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Delete' -and - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/15?api-version=$([VSTeamVersions]::Release)" - } - } - } - - Context 'Remove-VSTeamRelease by ID throws' { - Mock Invoke-RestMethod { throw 'error'} - - It 'should return releases' { - { Remove-VSTeamRelease -ProjectName project -Id 150000 -Force } | Should Throw - } - } - - Context 'Set-VSTeamReleaseStatus by ID' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod - - It 'should set release status' { - Set-VSTeamReleaseStatus -ProjectName project -Id 15 -Status Abandoned -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Patch' -and - $Body -eq '{ "id": 15, "status": "Abandoned" }' -and - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/15?api-version=$([VSTeamVersions]::Release)" - } - } - } - - Context 'Set-VSTeamReleaseStatus by ID throws' { - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock Invoke-RestMethod { throw 'error' } - - It 'should set release status' { - { Set-VSTeamReleaseStatus -ProjectName project -Id 15 -Status Abandoned -Force } | Should Throw - } - } - - Context 'Set-VSTeamEnvironmentStatus by ID' { - Mock _useWindowsAuthenticationOnPremise { return $false } - Mock Invoke-RestMethod - - $expectedBody = ConvertTo-Json ([PSCustomObject]@{status = 'inProgress'; comment = ''; scheduledDeploymentTime = $null}) - - It 'should set environments' { - Set-VSTeamEnvironmentStatus -ProjectName project -ReleaseId 1 -Id 15 -Status inProgress -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Patch' -and - $Body -eq $expectedBody -and - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases/1/environments/15?api-version=$([VSTeamVersions]::Release)" - } - } - } - - Context 'Set-VSTeamEnvironmentStatus by ID throws' { - Mock _useWindowsAuthenticationOnPremise { return $false } - Mock Invoke-RestMethod { throw 'error' } - - It 'should set environments' { - { Set-VSTeamEnvironmentStatus -ProjectName project -ReleaseId 1 -Id 15 -Status inProgress -Force } | Should Throw - } - } - - Context 'Add-VSTeamRelease by ID' { - Mock Invoke-RestMethod { - return $singleResult - } - - It 'should add a release' { - Add-VSTeamRelease -ProjectName project -DefinitionId 1 -ArtifactAlias drop -BuildId 2 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Body -like '*"definitionId": 1*' -and - $Body -like '*"description": ""*' -and - $Body -like '*"alias": "drop"*' -and - $Body -like '*"id": "2"*' -and - $Body -like '*"sourceBranch": ""*' -and - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases?api-version=$([VSTeamVersions]::Release)" - } - } - } - - Context 'Add-VSTeamRelease by name' { - BeforeAll { - $Global:PSDefaultParameterValues["*:projectName"] = 'project' - } - - AfterAll { - $Global:PSDefaultParameterValues.Remove("*:projectName") - } - - Mock Get-VSTeamReleaseDefinition { - $def1 = New-Object -TypeName PSObject -Prop @{name = 'Test1'; id = 1; artifacts = @(@{alias = 'drop'})} - $def2 = New-Object -TypeName PSObject -Prop @{name = 'Tests'; id = 2; artifacts = @(@{alias = 'drop'})} - return @( - $def1, - $def2 - ) - } - - Mock Get-VSTeamBuild { - $bld1 = New-Object -TypeName PSObject -Prop @{name = "Bld1"; id = 1} - $bld2 = New-Object -TypeName PSObject -Prop @{name = "Bld2"; id = 2} - - return @( - $bld1, - $bld2 - ) - } - - Mock Invoke-RestMethod { - return $singleResult - } - - Mock _buildDynamicParam { - param( - [string] $ParameterName = 'QueueName', - [array] $arrSet, - [bool] $Mandatory = $false, - [string] $ParameterSetName - ) - - # Create the collection of attributes - $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] - - # Create and set the parameters' attributes - $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute - $ParameterAttribute.Mandatory = $Mandatory - $ParameterAttribute.ValueFromPipelineByPropertyName = $true - - if ($ParameterSetName) { - $ParameterAttribute.ParameterSetName = $ParameterSetName - } - - # Add the attributes to the attributes collection - $AttributeCollection.Add($ParameterAttribute) - - if ($arrSet) { - # Generate and set the ValidateSet - $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) - - # Add the ValidateSet to the attributes collection - $AttributeCollection.Add($ValidateSetAttribute) - } - - # Create and return the dynamic parameter - return New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection) - } - - It 'should add a release' { - Add-VSTeamRelease -ProjectName project -BuildNumber 'Bld1' -DefinitionName 'Test1' - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Body -like '*"definitionId": 1*' -and - $Body -like '*"description": ""*' -and - $Body -like '*"alias": "drop"*' -and - $Body -like '*"id": "1"*' -and - $Body -like '*"sourceBranch": ""*' -and - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/releases?api-version=$([VSTeamVersions]::Release)" - } - } - } - - Context 'Add-VSTeamRelease throws' { - Mock Invoke-RestMethod { throw 'error' } - - It 'should add a release' { - { Add-VSTeamRelease -ProjectName project -DefinitionId 1 -ArtifactAlias drop -BuildId 2 } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/releaseDefinitions.Tests.ps1 b/unit/test/releaseDefinitions.Tests.ps1 deleted file mode 100644 index 776b47a41..000000000 --- a/unit/test/releaseDefinitions.Tests.ps1 +++ /dev/null @@ -1,105 +0,0 @@ -Set-StrictMode -Version Latest - -# Loading System.Web avoids issues finding System.Web.HttpUtility -Add-Type -AssemblyName 'System.Web' - -InModuleScope VSTeam { - Describe 'ReleaseDefinitions' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - [VSTeamVersions]::Release = '1.0-unittest' - - $results = [PSCustomObject]@{ - value = [PSCustomObject]@{ - queue = [PSCustomObject]@{ name = 'Default' } - _links = [PSCustomObject]@{ - self = [PSCustomObject]@{} - web = [PSCustomObject]@{} - } - retentionPolicy = [PSCustomObject]@{} - lastRelease = [PSCustomObject]@{} - artifacts = [PSCustomObject]@{} - modifiedBy = [PSCustomObject]@{ name = 'project' } - createdBy = [PSCustomObject]@{ name = 'test'} - } - } - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - Context 'Show-VSTeamReleaseDefinition by ID' { - Mock Show-Browser { } - - it 'should return Release definitions' { - Show-VSTeamReleaseDefinition -projectName project -Id 15 - - Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { - $url -eq 'https://dev.azure.com/test/project/_release?definitionId=15' - } - } - } - - Context 'Add-VSTeamReleaseDefinition' { - Mock Invoke-RestMethod { - return $results - } - - it 'Should add Release' { - Add-VSTeamReleaseDefinition -projectName project -inFile 'Releasedef.json' - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $InFile -eq 'Releasedef.json' -and - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/definitions?api-version=$([VSTeamVersions]::Release)" - } - } - } - - Context 'Remove-VSTeamReleaseDefinition' { - Mock Invoke-RestMethod { return $results } - - It 'should delete Release definition' { - Remove-VSTeamReleaseDefinition -projectName project -id 2 -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Delete' -and - $Uri -eq "https://vsrm.dev.azure.com/test/project/_apis/release/definitions/2?api-version=$([VSTeamVersions]::Release)" - } - } - } - - Context 'Add-VSTeamReleaseDefinition on TFS local Auth' { - Mock Invoke-RestMethod { return $results } - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - it 'Should add Release' { - Add-VSTeamReleaseDefinition -projectName project -inFile 'Releasedef.json' - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $InFile -eq 'Releasedef.json' -and - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/release/definitions?api-version=$([VSTeamVersions]::Release)" - } - } - } - - Context 'Remove-VSTeamReleaseDefinition on TFS local Auth' { - Mock Invoke-RestMethod { return $results } - Mock _useWindowsAuthenticationOnPremise { return $true } - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - Remove-VSTeamReleaseDefinition -projectName project -id 2 -Force - - It 'should delete Release definition' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { - $Method -eq 'Delete' -and - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/release/definitions/2?api-version=$([VSTeamVersions]::Release)" - } - } - } - } -} \ No newline at end of file diff --git a/unit/test/repositories.Tests.ps1 b/unit/test/repositories.Tests.ps1 deleted file mode 100644 index 20f35caa9..000000000 --- a/unit/test/repositories.Tests.ps1 +++ /dev/null @@ -1,212 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - $results = [PSCustomObject]@{ - value = [PSCustomObject]@{ - id = '' - url = '' - sshUrl = '' - remoteUrl = '' - defaultBranch = '' - size = 0 - name = '' - project = [PSCustomObject]@{ - name = 'Project' - id = 1 - description = '' - url = '' - state = '' - revision = '' - visibility = '' - } - } - } - - $singleResult = [PSCustomObject]@{ - id = '' - url = '' - sshUrl = '' - remoteUrl = '' - defaultBranch = '' - size = 0 - name = '' - project = [PSCustomObject]@{ - name = 'Project' - id = 1 - description = '' - url = '' - state = '' - revision = '' - visibility = '' - } - } - - Describe "Git VSTS" { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" - - Context 'Show-VSTeamGitRepository by project' { - Mock Show-Browser -Verifiable -ParameterFilter { $url -eq 'https://dev.azure.com/test/_git/project' } - Mock Show-Browser { throw "Show-Browser called incorrectly: $Args" } - - Show-VSTeamGitRepository -projectName project - - it 'should return url for mine' { - Assert-VerifiableMock - } - } - - Context 'Show-VSTeamGitRepository by remote url' { - Mock Show-Browser -Verifiable -ParameterFilter { $url -eq 'https://dev.azure.com/test/_git/project' } - Mock Show-Browser { throw "Show-Browser called incorrectly: $Args" } - - Show-VSTeamGitRepository -RemoteUrl 'https://dev.azure.com/test/_git/project' - - it 'should return url for mine' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitRepository no parameters' { - Mock Invoke-RestMethod { return $results } -Verifiable - - Get-VSTeamGitRepository - - It 'Should return all repos for all projects' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitRepository no parameters throws' { - Mock Invoke-RestMethod { throw [System.Net.WebException] "Test Exception." } - - It 'Should return all repos for all projects' { - { Get-VSTeamGitRepository } | Should Throw - } - } - - Context 'Get-VSTeamGitRepository by id' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable - - Get-VSTeamGitRepository -id 00000000-0000-0000-0000-000000000000 - - It 'Should return a single repo by id' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitRepository by id throws' { - Mock Invoke-RestMethod { throw [System.Net.WebException] "Test Exception." } - - It 'Should return a single repo by id' { - { Get-VSTeamGitRepository -id 00000000-0000-0000-0000-000000000000 } | Should Throw - } - } - - Context 'Get-VSTeamGitRepository by name' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable - - Get-VSTeamGitRepository -Name 'test' - - It 'Should return a single repo by name' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitRepository by name throws' { - Mock Invoke-RestMethod { throw [System.Net.WebException] "Test Exception." } - - It 'Should return a single repo by name' { - { Get-VSTeamGitRepository -Name 'test' } | Should Throw - } - } - - Context 'Remove-VSTeamGitRepository by id' { - Mock Invoke-RestMethod - - Remove-VSTeamGitRepository -id 00000000-0000-0000-0000-000000000000 -Force - - It 'Should remove Git repo' { - Assert-MockCalled Invoke-RestMethod -ParameterFilter { - $Method -eq 'Delete' -and - $Uri -eq "https://dev.azure.com/test/_apis/git/repositories/00000000-0000-0000-0000-000000000000?api-version=$([VSTeamVersions]::Git)" - } - } - } - - Context 'Remove-VSTeamGitRepository throws ' { - Mock Invoke-RestMethod { throw [System.Net.WebException] "Test Exception." } - - It 'Should throw trying to remove Git repo' { - {Remove-VSTeamGitRepository -id 00000000-0000-0000-0000-000000000000 -Force} | Should Throw - } - } - } - - Describe "Git TFS" { - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Mock _useWindowsAuthenticationOnPremise { return $true } - - Context 'Get-VSTeamGitRepository no parameters' { - Mock Invoke-RestMethod { return $results } -Verifiable - - Get-VSTeamGitRepository - - It 'Should return Git repo' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitRepository by id' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable - - Get-VSTeamGitRepository -id 00000000-0000-0000-0000-000000000000 - - It 'Should return Git repo' { - Assert-VerifiableMock - } - } - - Context 'Get-VSTeamGitRepository by name' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable - - Get-VSTeamGitRepository -Name 'test' - - It 'Should return Git repo' { - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamGitRepository by name' { - Mock Invoke-RestMethod { return $singleResult } -Verifiable - - Add-VSTeamGitRepository -Name 'test' -ProjectName 'test' - - It 'Should add Git repo' { - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamGitRepository throws' { - Mock Invoke-RestMethod { throw [System.Net.WebException] "Test Exception." } - - It 'Should add Git repo' { - { Add-VSTeamGitRepository -Name 'test' -ProjectName 'test' } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/sampleFiles/accessControlEntryResult.json b/unit/test/sampleFiles/accessControlEntryResult.json new file mode 100644 index 000000000..d774be4a5 --- /dev/null +++ b/unit/test/sampleFiles/accessControlEntryResult.json @@ -0,0 +1,11 @@ +{ + "count": 1, + "value": [ + { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", + "allow": 8, + "deny": 0, + "extendedInfo": {} + } + ] +} \ No newline at end of file diff --git a/unit/test/sampleFiles/accessControlListResult.json b/unit/test/sampleFiles/accessControlListResult.json new file mode 100644 index 000000000..47c4be158 --- /dev/null +++ b/unit/test/sampleFiles/accessControlListResult.json @@ -0,0 +1,85 @@ +{ + "count": 5, + "value": [ + { + "inheritPermissions": true, + "token": "1ba198c0-7a12-46ed-a96b-f4e77554c6d4", + "acesDictionary": { + "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1": { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", + "allow": 31, + "deny": 0 + }, + "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-2": { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-2", + "allow": 31, + "deny": 0 + }, + "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-3": { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-3", + "allow": 1, + "deny": 0 + } + } + }, + { + "inheritPermissions": true, + "token": "1ba198c0-7a12-46ed-a96b-f4e77554c6d4\\846cd9c3-56ba-4158-b6d2-23a3a73244e5", + "acesDictionary": { + "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-1-2": { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-1-2", + "allow": 8, + "deny": 0 + } + } + }, + { + "inheritPermissions": true, + "token": "28b9bb88-a513-4115-9b5c-8be39ce1f1ba", + "acesDictionary": { + "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-2294004008-329585985-2606533603-2632053178-0-0-0-0-1": { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-2294004008-329585985-2606533603-2632053178-0-0-0-0-1", + "allow": 31, + "deny": 0 + }, + "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-2294004008-329585985-2606533603-2632053178-0-0-0-0-2": { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-2294004008-329585985-2606533603-2632053178-0-0-0-0-2", + "allow": 31, + "deny": 0 + }, + "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-2294004008-329585985-2606533603-2632053178-0-0-0-0-3": { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-2294004008-329585985-2606533603-2632053178-0-0-0-0-3", + "allow": 1, + "deny": 0 + } + } + }, + { + "inheritPermissions": false, + "token": "token1", + "acesDictionary": { + "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1": { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", + "allow": 31, + "deny": 0 + } + } + }, + { + "inheritPermissions": false, + "token": "token2", + "acesDictionary": { + "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1": { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", + "allow": 1, + "deny": 0 + }, + "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-2": { + "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-2", + "allow": 8, + "deny": 0 + } + } + } + ] +} \ No newline at end of file diff --git a/unit/test/sampleFiles/agentSingleResult.json b/unit/test/sampleFiles/agentSingleResult.json new file mode 100644 index 000000000..d91be3ec5 --- /dev/null +++ b/unit/test/sampleFiles/agentSingleResult.json @@ -0,0 +1,13 @@ +{ + "_links": {}, + "createdOn": "2018-03-28T16:48:58.317Z", + "maxParallelism": 1, + "id": 102, + "status": "Online", + "version": "1.336.1", + "enabled": true, + "osDescription": "Linux", + "name": "Test_Agent", + "authorization": {}, + "systemCapabilities": {} +} \ No newline at end of file diff --git a/unit/test/sampleFiles/buildResults.json b/unit/test/sampleFiles/buildResults.json new file mode 100644 index 000000000..db2215180 --- /dev/null +++ b/unit/test/sampleFiles/buildResults.json @@ -0,0 +1,14 @@ +{ + "value": { + "logs": {}, + "queue": {}, + "_links": {}, + "project": {}, + "repository": {}, + "requestedFor": {}, + "orchestrationPlan": {}, + "definition": {}, + "lastChangedBy": {}, + "requestedBy": {} + } + } \ No newline at end of file diff --git a/unit/test/sampleFiles/buildSingleResult.json b/unit/test/sampleFiles/buildSingleResult.json new file mode 100644 index 000000000..d1a04d9f8 --- /dev/null +++ b/unit/test/sampleFiles/buildSingleResult.json @@ -0,0 +1,12 @@ +{ + "logs": {}, + "queue": {}, + "_links": {}, + "project": {}, + "repository": {}, + "requestedFor": {}, + "orchestrationPlan": {}, + "definition": {}, + "lastChangedBy": {}, + "requestedBy": {} +} \ No newline at end of file diff --git a/unit/test/sampleFiles/classificationNodeResult.json b/unit/test/sampleFiles/classificationNodeResult.json new file mode 100644 index 000000000..e48f49ec9 --- /dev/null +++ b/unit/test/sampleFiles/classificationNodeResult.json @@ -0,0 +1,70 @@ +{ + "id": 16, + "identifier": "dfa90792-403a-4119-a52b-bd142c08291b", + "name": "Demo Public", + "structureType": "iteration", + "hasChildren": true, + "children": [ + { + "id": 18, + "identifier": "3d98fd5b-fd0a-4ee0-b7b2-cfdf467014d0", + "name": "Sprint 1", + "structureType": "iteration", + "hasChildren": false, + "path": "\\Demo Public\\Iteration\\Sprint 1", + "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%201" + }, + { + "id": 19, + "identifier": "eee04655-55c2-4712-afa1-9c800c1f1345", + "name": "Sprint 2", + "structureType": "iteration", + "hasChildren": false, + "path": "\\Demo Public\\Iteration\\Sprint 2", + "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%202" + }, + { + "id": 20, + "identifier": "18e7998d-d0c5-4c01-b547-d7d4eb4c97c5", + "name": "Sprint 3", + "structureType": "iteration", + "hasChildren": false, + "path": "\\Demo Public\\Iteration\\Sprint 3", + "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%203" + }, + { + "id": 21, + "identifier": "4e07ee24-8653-4af6-9df2-2aadc0a1a3d3", + "name": "Sprint 4", + "structureType": "iteration", + "hasChildren": false, + "path": "\\Demo Public\\Iteration\\Sprint 4", + "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%204" + }, + { + "id": 22, + "identifier": "b4be112e-8b7d-4def-a37d-7c1e1dea01e1", + "name": "Sprint 5", + "structureType": "iteration", + "hasChildren": false, + "path": "\\Demo Public\\Iteration\\Sprint 5", + "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%205" + }, + { + "id": 23, + "identifier": "7ee87b38-6c81-4fdf-8fbd-dc996f040823", + "name": "Sprint 6", + "structureType": "iteration", + "hasChildren": false, + "path": "\\Demo Public\\Iteration\\Sprint 6", + "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%206" + } + ], + "path": "\\Demo Public\\Iteration", + "_links": { + "self": { + "href": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations" + } + }, + "url": "https://dev.azure.com/test/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations" +} \ No newline at end of file diff --git a/unit/test/sampleFiles/extensionResults.json b/unit/test/sampleFiles/extensionResults.json new file mode 100644 index 000000000..c2abd8f1e --- /dev/null +++ b/unit/test/sampleFiles/extensionResults.json @@ -0,0 +1,19 @@ +{ + "count": 1, + "value": { + "extensionId": "test", + "extensionName": "test", + "publisherId": "test", + "publisherName": "test", + "version": "1.0.0", + "registrationId": "12345678-9012-3456-7890-123456789012", + "manifestVersion": 1, + "baseUri": "", + "fallbackBaseUri": "", + "scopes": {}, + "installState": { + "flags": "none", + "lastUpdated": "2018-10-09T11:26:47.187Z" + } + } +} \ No newline at end of file diff --git a/unit/test/sampleFiles/gitCommitResults.json b/unit/test/sampleFiles/gitCommitResults.json new file mode 100644 index 000000000..433e370e7 --- /dev/null +++ b/unit/test/sampleFiles/gitCommitResults.json @@ -0,0 +1,47 @@ +{ + "count": 2, + "value": [ + { + "author": { + "date": "2020-02-19T15:12:01Z", + "email": "test@test.com", + "name": "Test User" + }, + "changeCounts": { + "Add": 2, + "Delete": 0, + "Edit": 1 + }, + "comment": "Just a test commit", + "commitId": "1234567890abcdef1234567890abcdef", + "committer": { + "date": "2020-02-19T15:12:01Z", + "email": "test@test.com", + "name": "Test User" + }, + "remoteUrl": "https://dev.azure.com/test/test/_git/test/commit/1234567890abcdef1234567890abcdef", + "url": "https://dev.azure.com/test/21AF684D-AFFB-4F9A-9D49-866EF24D6A4A/_apid/git/repositories/06E176BE-D3D2-41C2-AB34-5F4D79AEC86B/commits/1234567890abcdef1234567890abcdef" + }, + { + "author": { + "date": "2020-02-20T01:00:01Z", + "email": "eample@example.com", + "name": "Example User" + }, + "changeCounts": { + "Add": 8, + "Delete": 1, + "Edit": 0 + }, + "comment": "Just another test commit", + "commitId": "abcdef1234567890abcdef1234567890", + "committer": { + "date": "2020-02-20T01:00:01Z", + "email": "eample@example.com", + "name": "Example User" + }, + "remoteUrl": "https://dev.azure.com/test/test/_git/test/commit/abcdef1234567890abcdef1234567890", + "url": "https://dev.azure.com/test/21AF684D-AFFB-4F9A-9D49-866EF24D6A4A/_apid/git/repositories/06E176BE-D3D2-41C2-AB34-5F4D79AEC86B/commits/abcdef1234567890abcdef1234567890" + } + ] +} \ No newline at end of file diff --git a/unit/test/sampleFiles/gitStatMultipleResults.json b/unit/test/sampleFiles/gitStatMultipleResults.json new file mode 100644 index 000000000..1e8228d56 --- /dev/null +++ b/unit/test/sampleFiles/gitStatMultipleResults.json @@ -0,0 +1,76 @@ +{ + "count": 3, + "value": [ + { + "commit": { + "commitId": "67cae2b029dff7eb3dc062b49403aaedca5bad8d", + "author": { + "name": "\"Chuck Reinhart", + "email": "fabrikamfiber3@hotmail.com", + "date": "2014-01-29T23:52:56Z" + }, + "committer": { + "name": "\"Chuck Reinhart", + "email": "fabrikamfiber3@hotmail.com", + "date": "2014-01-29T23:52:56Z" + }, + "comment": "home page", + "url": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/67cae2b029dff7eb3dc062b49403aaedca5bad8d" + }, + "name": "develop", + "aheadCount": 1, + "behindCount": 17, + "isBaseVersion": false + }, + { + "commit": { + "parents": [ + "fe17a84cc2dfe0ea3a2202ab4dbac0706058e41f" + ], + "treeId": "8263e7232a2331c563d737e4fc4e9c66a8286c63", + "commitId": "23d0bc5b128a10056dc68afece360d8a0fabb014", + "author": { + "name": "Norman Paulk", + "email": "Fabrikamfiber16@hotmail.com", + "date": "2014-06-30T18:10:55Z" + }, + "committer": { + "name": "Norman Paulk", + "email": "Fabrikamfiber16@hotmail.com", + "date": "2014-06-30T18:10:55Z" + }, + "comment": "Better description for hello world\\n", + "url": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/23d0bc5b128a10056dc68afece360d8a0fabb014" + }, + "name": "master", + "aheadCount": 0, + "behindCount": 0, + "isBaseVersion": true + }, + { + "commit": { + "parents": [ + "fe17a84cc2dfe0ea3a2202ab4dbac0706058e41f" + ], + "treeId": "8263e7232a2331c563d737e4fc4e9c66a8286c63", + "commitId": "23d0bc5b128a10056dc68afece360d8a0fabb014", + "author": { + "name": "Norman Paulk", + "email": "Fabrikamfiber16@hotmail.com", + "date": "2014-06-30T18:10:55Z" + }, + "committer": { + "name": "Norman Paulk", + "email": "Fabrikamfiber16@hotmail.com", + "date": "2014-06-30T18:10:55Z" + }, + "comment": "Better description for hello world\\n", + "url": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/23d0bc5b128a10056dc68afece360d8a0fabb014" + }, + "name": "npaulk/feature", + "aheadCount": 0, + "behindCount": 0, + "isBaseVersion": true + } + ] +} \ No newline at end of file diff --git a/unit/test/sampleFiles/gitStatSingleResult.json b/unit/test/sampleFiles/gitStatSingleResult.json new file mode 100644 index 000000000..43cd83df1 --- /dev/null +++ b/unit/test/sampleFiles/gitStatSingleResult.json @@ -0,0 +1,21 @@ +{ + "commit": { + "commitId": "67cae2b029dff7eb3dc062b49403aaedca5bad8d", + "author": { + "name": "\"Chuck Reinhart", + "email": "fabrikamfiber3@hotmail.com", + "date": "2014-01-29T23:52:56Z" + }, + "committer": { + "name": "\"Chuck Reinhart", + "email": "fabrikamfiber3@hotmail.com", + "date": "2014-01-29T23:52:56Z" + }, + "comment": "home page", + "url": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/67cae2b029dff7eb3dc062b49403aaedca5bad8d" + }, + "name": "develop", + "aheadCount": 1, + "behindCount": 17, + "isBaseVersion": false +} \ No newline at end of file diff --git a/unit/test/sampleFiles/projectResult.json b/unit/test/sampleFiles/projectResult.json new file mode 100644 index 000000000..e03399f21 --- /dev/null +++ b/unit/test/sampleFiles/projectResult.json @@ -0,0 +1,11 @@ +{ + "name": "Test Project Public", + "description": "", + "url": "", + "id": "010d06f0-00d5-472a-bb47-58947c230876", + "state": "", + "visibility": "", + "revision": 0, + "defaultTeam": {}, + "_links": {} +} \ No newline at end of file diff --git a/unit/test/sampleFiles/releaseResults.json b/unit/test/sampleFiles/releaseResults.json new file mode 100644 index 000000000..be4b479fb --- /dev/null +++ b/unit/test/sampleFiles/releaseResults.json @@ -0,0 +1,9 @@ +{ + "value": { + "environments": {}, + "_links": { + "self": {}, + "web": {} + } + } + } \ No newline at end of file diff --git a/unit/test/sampleFiles/releaseSingleReult.json b/unit/test/sampleFiles/releaseSingleReult.json new file mode 100644 index 000000000..c2b7e8882 --- /dev/null +++ b/unit/test/sampleFiles/releaseSingleReult.json @@ -0,0 +1,12 @@ +{ + "environments": {}, + "variables": { + "BrowserToUse": { + "value": "phantomjs" + } + }, + "_links": { + "self": {}, + "web": {} + } +} \ No newline at end of file diff --git a/unit/test/sampleFiles/securityNamespace.json b/unit/test/sampleFiles/securityNamespace.json new file mode 100644 index 000000000..2c1e39cce --- /dev/null +++ b/unit/test/sampleFiles/securityNamespace.json @@ -0,0 +1,52 @@ +{ + "count": 1, + "value": [ + { + "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531", + "name": "Analytics", + "displayName": "Analytics", + "separatorValue": "/", + "elementLength": -1, + "writePermission": 2, + "readPermission": 1, + "dataspaceCategory": "Default", + "actions": [ + { + "bit": 1, + "name": "Read", + "displayName": "View analytics", + "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" + }, + { + "bit": 2, + "name": "Administer", + "displayName": "Manage analytics permissions", + "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" + }, + { + "bit": 4, + "name": "Stage", + "displayName": "Push the data to staging area", + "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" + }, + { + "bit": 8, + "name": "ExecuteUnrestrictedQuery", + "displayName": "Execute query without any restrictions on the query form", + "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" + }, + { + "bit": 16, + "name": "ReadEuii", + "displayName": "Read EUII data", + "namespaceId": "58450c49-b02d-465a-ab12-59ae512d6531" + } + ], + "structureValue": 1, + "extensionType": null, + "isRemotable": false, + "useTokenTranslator": false, + "systemBitMask": 30 + } + ] +} \ No newline at end of file diff --git a/unit/test/sampleFiles/singleExtensionResult.json b/unit/test/sampleFiles/singleExtensionResult.json new file mode 100644 index 000000000..17c26be44 --- /dev/null +++ b/unit/test/sampleFiles/singleExtensionResult.json @@ -0,0 +1,16 @@ +{ + "extensionId": "test", + "extensionName": "test", + "publisherId": "test", + "publisherName": "test", + "version": "1.0.0", + "registrationId": "12345678-9012-3456-7890-123456789012", + "manifestVersion": 1, + "baseUri": "", + "fallbackBaseUri": "", + "scopes": {}, + "installState": { + "flags": "none", + "lastUpdated": "2018-10-09T11:26:47.187Z" + } +} \ No newline at end of file diff --git a/unit/test/sampleFiles/variableGroupSamples2017.json b/unit/test/sampleFiles/variableGroupSamples2017.json index 32698d0d1..6da45ebd3 100644 --- a/unit/test/sampleFiles/variableGroupSamples2017.json +++ b/unit/test/sampleFiles/variableGroupSamples2017.json @@ -11,7 +11,7 @@ "isSecret": true } }, - "id": 1, + "id": 101, "name": "TestVariableGroup1", "description": "A test variable group", "createdBy": { diff --git a/unit/test/sampleFiles/withoutChildNode.json b/unit/test/sampleFiles/withoutChildNode.json new file mode 100644 index 000000000..422b3109c --- /dev/null +++ b/unit/test/sampleFiles/withoutChildNode.json @@ -0,0 +1,39 @@ +{ + "count": 2, + "value": [ + { + "id": 44, + "identifier": "90aa2c42-de51-450a-bfb6-6e264e364d9a", + "name": "Child 1 Level 2", + "structureType": "area", + "hasChildren": true, + "path": "\\Demo Public\\Area\\Child 1 Level 1\\Child 1 Level 2", + "_links": { + "self": { + "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201/Child%201%20Level%202" + }, + "parent": { + "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201" + } + }, + "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201/Child%201%20Level%202" + }, + { + "id": 43, + "identifier": "38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b", + "name": "Child 1 Level 1", + "structureType": "area", + "hasChildren": true, + "path": "\\Demo Public\\Area\\Child 1 Level 1", + "_links": { + "self": { + "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201" + }, + "parent": { + "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas" + } + }, + "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201" + } + ] +} \ No newline at end of file diff --git a/unit/test/securityNamespace.Tests.ps1 b/unit/test/securityNamespace.Tests.ps1 deleted file mode 100644 index 444d814b5..000000000 --- a/unit/test/securityNamespace.Tests.ps1 +++ /dev/null @@ -1,141 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe "SecurityNamespace TFS Errors" { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Get-VSTeamSecurityNamespace' { - Mock _callAPI { throw 'Should not be called' } -Verifiable - - It 'Should throw' { - Set-VSTeamAPIVersion TFS2017 - - { Get-VSTeamSecurityNamespace } | Should Throw - } - - It '_callAPI should not be called' { - Assert-MockCalled _callAPI -Exactly 0 - } - } - } - - Describe 'SecurityNamespace VSTS' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - $securityNamespaceListResult = Get-Content "$PSScriptRoot\sampleFiles\securityNamespaces.json" -Raw | ConvertFrom-Json - $securityNamespaceSingleResult = Get-Content "$PSScriptRoot\sampleFiles\securityNamespace.single.json" -Raw | ConvertFrom-Json - - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Core = '' - Set-VSTeamAPIVersion AzD - [VSTeamVersions]::Core = '5.0' - - Context 'Get-VSTeamSecurityNamespace list' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $securityNamespaceListResult - } -Verifiable - - Get-VSTeamSecurityNamespace - - It 'Should return namespaces' { - # With PowerShell core the order of the query string is not the - # same from run to run! So instead of testing the entire string - # matches I have to search for the portions I expect but can't - # assume the order. - # The general string should look like this: - # "https://vssps.dev.azure.com/test/_apis/graph/groups?api-version=$([VSTeamVersions]::Graph)&scopeDescriptor=scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2" - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/securitynamespaces*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" - } - } - } - - Context 'Get-VSTeamSecurityNamespace by id' { - Mock Invoke-RestMethod { return $securityNamespaceSingleResult } -Verifiable - - Get-VSTeamSecurityNamespace -Id 58450c49-b02d-465a-ab12-59ae512d6531 - - It 'Should return namespaces' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/securitynamespaces/58450c49-b02d-465a-ab12-59ae512d6531*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" - } - } - } - - Context 'Get-VSTeamSecurityNamespace by name' { - Mock Invoke-RestMethod { return $securityNamespaceListResult } -Verifiable - - Get-VSTeamSecurityNamespace -Name "WorkItemTracking" - - It 'Should return namespaces' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/securitynamespaces*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" - } - } - } - - Context 'Get-VSTeamSecurityNamespace by list and localOnly' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $securityNamespaceListResult - } -Verifiable - - Get-VSTeamSecurityNamespace -LocalOnly - - It 'Should return namespaces' { - # With PowerShell core the order of the query string is not the - # same from run to run! So instead of testing the entire string - # matches I have to search for the portions I expect but can't - # assume the order. - # The general string should look like this: - # "https://vssps.dev.azure.com/test/_apis/graph/groups?api-version=$([VSTeamVersions]::Graph)&scopeDescriptor=scp.ZGU5ODYwOWEtZjRiMC00YWEzLTgzOTEtODI4ZDU2MDI0MjU2" - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/securitynamespaces*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*localOnly=true*" - } - } - } - - Context 'Get-VSTeamSecurityNamespace throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Get-VSTeamSecurityNamespace } | Should Throw - } - } - - Context 'Get-VSTeamSecurityNamespace by id throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Get-VSTeamSecurityNamespace -Id 58450c49-b02d-465a-ab12-59ae512d6531 } | Should Throw - } - } - - Context 'Get-VSTeamSecurityNamespace by name throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Get-VSTeamSecurityNamespace -Name "WorkItemTracking" } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/serviceendpoints.Tests.ps1 b/unit/test/serviceendpoints.Tests.ps1 deleted file mode 100644 index 0cb3a6112..000000000 --- a/unit/test/serviceendpoints.Tests.ps1 +++ /dev/null @@ -1,600 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe 'ServiceEndpoints TFS2017 throws' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Add-VSTeamServiceFabricEndpoint' { - Mock ConvertTo-Json { throw 'Should not be called' } -Verifiable - - It 'Should throw' { - Set-VSTeamAPIVersion TFS2017 - - { Add-VSTeamServiceFabricEndpoint -projectName 'project' ` - -endpointName 'PM_DonovanBrown' -url "tcp://0.0.0.0:19000" ` - -useWindowsSecurity $false } | Should Throw - } - - It 'ConvertTo-Json should not be called' { - Assert-MockCalled ConvertTo-Json -Exactly 0 - } - } - } - - Describe 'ServiceEndpoints TFS' { - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - Context 'Get-VSTeamServiceEndpoint' { - Mock Write-Verbose - Mock Invoke-RestMethod { - return [PSCustomObject]@{ - value = [PSCustomObject]@{ - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{ - state = 'Failed' - statusMessage = 'Bad things!' - } - } - }} - - It 'Should return all service endpoints' { - Get-VSTeamServiceEndpoint -projectName project -Verbose - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/distributedtask/serviceendpoints?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - - Context 'Remove-VSTeamServiceEndpoint' { - Mock Invoke-RestMethod - - It 'should delete service endpoint' { - Remove-VSTeamServiceEndpoint -projectName project -id 5 -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/distributedtask/serviceendpoints/5?api-version=$([VSTeamVersions]::DistributedTask)" -and - $Method -eq 'Delete' - } - } - } - - Context 'Add-VSTeamAzureRMServiceEndpoint' { - Mock Write-Progress - Mock Invoke-RestMethod { return @{id = '23233-2342'} } -ParameterFilter { $Method -eq 'Post'} - Mock Invoke-RestMethod { - - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($iTracking -gt 9) { - return [PSCustomObject]@{ - isReady = $true - operationStatus = [PSCustomObject]@{state = 'Ready'} - } - } - - return [PSCustomObject]@{ - isReady = $false - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{state = 'InProgress'} - } - } - - It 'should create a new AzureRM Serviceendpoint' { - Add-VSTeamAzureRMServiceEndpoint -projectName 'project' -displayName 'PM_DonovanBrown' -subscriptionId '00000000-0000-0000-0000-000000000000' -subscriptionTenantId '00000000-0000-0000-0000-000000000000' - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } - } - } - - Context 'Add-VSTeamSonarQubeEndpoint' { - Mock Write-Progress - Mock Invoke-RestMethod { return @{id = '23233-2342'} } -ParameterFilter { $Method -eq 'Post'} - Mock Invoke-RestMethod { - - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($iTracking -gt 9) { - return [PSCustomObject]@{ - isReady = $true - operationStatus = [PSCustomObject]@{state = 'Ready'} - } - } - - return [PSCustomObject]@{ - isReady = $false - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{state = 'InProgress'} - } - } - - It 'should create a new SonarQube Serviceendpoint' { - Add-VSTeamSonarQubeEndpoint -projectName 'project' -endpointName 'PM_DonovanBrown' -sonarqubeUrl 'http://mysonarserver.local' -personalAccessToken '00000000-0000-0000-0000-000000000000' - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } - } - } - - Context 'Add-VSTeamSonarQubeEndpoint throws on TFS' { - Mock Write-Warning - Mock Write-Error -Verifiable - Mock Invoke-RestMethod { - $e = [System.Management.Automation.ErrorRecord]::new( - [System.Net.WebException]::new("Endpoint type couldn't be recognized 'sonarqube'", [System.Net.WebExceptionStatus]::ProtocolError), - "Endpoint type couldn't be recognized 'sonarqube'", - [System.Management.Automation.ErrorCategory]::ProtocolError, - $null) - - # The error message is different on TFS and VSTS - $msg = ConvertTo-Json @{ - '$id' = 1 - message = "Endpoint type couldn't be recognized 'sonarqube'`r`nParameter name: endpoint.Type" - } - - $e.ErrorDetails = [System.Management.Automation.ErrorDetails]::new($msg) - - $PSCmdlet.ThrowTerminatingError($e) - } - - It 'should create a new SonarQube Serviceendpoint' { - Add-VSTeamSonarQubeEndpoint -projectName 'project' -endpointName 'PM_DonovanBrown' ` - -sonarqubeUrl 'http://mysonarserver.local' ` - -personalAccessToken '00000000-0000-0000-0000-000000000000' - - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamSonarQubeEndpoint with securePersonalAccessToken' { - Mock Write-Progress - Mock Invoke-RestMethod { return @{id = '23233-2342'} } -ParameterFilter { $Method -eq 'Post'} - Mock Invoke-RestMethod { - - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($iTracking -gt 9) { - return [PSCustomObject]@{ - isReady = $true - operationStatus = [PSCustomObject]@{state = 'Ready'} - } - } - - return [PSCustomObject]@{ - isReady = $false - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{state = 'InProgress'} - } - } - - It 'should create a new SonarQube Serviceendpoint' { - $password = '00000000-0000-0000-0000-000000000000' | ConvertTo-SecureString -AsPlainText -Force - - Add-VSTeamSonarQubeEndpoint -projectName 'project' -endpointName 'PM_DonovanBrown' -sonarqubeUrl 'http://mysonarserver.local' -securePersonalAccessToken $password - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } - } - } - - Context 'Add-VSTeamAzureRMServiceEndpoint-With-Failure' { - Mock Write-Progress - Mock Invoke-RestMethod { return @{id = '23233-2342'} } -ParameterFilter { $Method -eq 'Post'} - Mock Invoke-RestMethod { - - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($iTracking -gt 9) { - return [PSCustomObject]@{ - isReady = $false - operationStatus = [PSCustomObject]@{ - state = 'Failed' - statusMessage = 'Simulated failed request' - } - } - } - - return [PSCustomObject]@{ - isReady = $false - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{state = 'InProgress'} - } - } - - It 'should not create a new AzureRM Serviceendpoint' { - { - Add-VSTeamAzureRMServiceEndpoint -projectName 'project' ` - -displayName 'PM_DonovanBrown' -subscriptionId '00000000-0000-0000-0000-000000000000' ` - -subscriptionTenantId '00000000-0000-0000-0000-000000000000' -servicePrincipalKey '00000000-0000-0000-0000-000000000000' -servicePrincipalId '00000000-0000-0000-0000-000000000000' - } | Should Throw - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } - } - } - } - - Describe 'ServiceEndpoints VSTS' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - [VSTeamVersions]::ServiceFabricEndpoint = '4.1-preview' - - Context 'Add-VSTeamServiceFabricEndpoint' { - Mock Write-Progress - Mock Invoke-RestMethod { return @{id = '23233-2342'} } -ParameterFilter { $Method -eq 'Post'} - Mock Invoke-RestMethod { - - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($iTracking -gt 9) { - return [PSCustomObject]@{ - isReady = $true - operationStatus = [PSCustomObject]@{state = 'Ready'} - } - } - - return [PSCustomObject]@{ - isReady = $false - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{state = 'InProgress'} - } - } - - It 'should create a new Service Fabric Serviceendpoint' { - Add-VSTeamServiceFabricEndpoint -projectName 'project' -endpointName 'PM_DonovanBrown' -url "tcp://0.0.0.0:19000" -useWindowsSecurity $false - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } - } - } - - Context 'Add-VSTeamSonarQubeEndpoint throws on VSTS' { - Mock Write-Warning - Mock Write-Error -Verifiable - Mock Invoke-RestMethod { - $e = [System.Management.Automation.ErrorRecord]::new( - [System.Net.WebException]::new("Endpoint type couldn't be recognized 'sonarqube'", [System.Net.WebExceptionStatus]::ProtocolError), - "Endpoint type couldn't be recognized 'sonarqube'", - [System.Management.Automation.ErrorCategory]::ProtocolError, - $null) - - # The error message is different on TFS and VSTS - $msg = ConvertTo-Json @{ - '$id' = 1 - message = "Unable to find service endpoint type 'sonarqube' using authentication scheme 'UsernamePassword'." - } - - $e.ErrorDetails = [System.Management.Automation.ErrorDetails]::new($msg) - - $PSCmdlet.ThrowTerminatingError($e) - } - - It 'should create a new SonarQube Serviceendpoint' { - Add-VSTeamSonarQubeEndpoint -projectName 'project' -endpointName 'PM_DonovanBrown' -sonarqubeUrl 'http://mysonarserver.local' -personalAccessToken '00000000-0000-0000-0000-000000000000' - - Assert-VerifiableMock - } - } - - Context 'Add-VSTeamServiceFabricEndpoint with AzureAD authentication' { - Mock Write-Progress - Mock Invoke-RestMethod { return @{id = '23233-2342'} } -ParameterFilter { $Method -eq 'Post'} - Mock Invoke-RestMethod { - - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($iTracking -gt 9) { - return [PSCustomObject]@{ - isReady = $true - operationStatus = [PSCustomObject]@{state = 'Ready'} - } - } - - return [PSCustomObject]@{ - isReady = $false - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{state = 'InProgress'} - } - } - - It 'should create a new Service Fabric Serviceendpoint' { - $password = '00000000-0000-0000-0000-000000000000' | ConvertTo-SecureString -AsPlainText -Force - $username = "Test User" - $serverCertThumbprint = "0000000000000000000000000000000000000000" - Add-VSTeamServiceFabricEndpoint -projectName 'project' -endpointName 'PM_DonovanBrown' -url "tcp://0.0.0.0:19000" -username $username -password $password -serverCertThumbprint $serverCertThumbprint - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } - } - } - - Context 'Add-VSTeamServiceFabricEndpoint with Certificate authentication' { - Mock Write-Progress - Mock Invoke-RestMethod { return @{id = '23233-2342'} } -ParameterFilter { $Method -eq 'Post'} - Mock Invoke-RestMethod { - - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($iTracking -gt 9) { - return [PSCustomObject]@{ - isReady = $true - operationStatus = [PSCustomObject]@{state = 'Ready'} - } - } - - return [PSCustomObject]@{ - isReady = $false - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{state = 'InProgress'} - } - } - - It 'should create a new Service Fabric Serviceendpoint' { - $password = '00000000-0000-0000-0000-000000000000' | ConvertTo-SecureString -AsPlainText -Force - $base64Cert = "0000000000000000000000000000000000000000" - $serverCertThumbprint = "0000000000000000000000000000000000000000" - Add-VSTeamServiceFabricEndpoint -projectName 'project' -endpointName 'PM_DonovanBrown' -url "tcp://0.0.0.0:19000" -serverCertThumbprint $serverCertThumbprint -certificate $base64Cert -certificatePassword $password - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } - } - } - - Context 'Add-VSTeamKubernetesEndpoint not accepting untrusted certs and not generating a pfx' { - Mock Write-Progress - Mock Invoke-RestMethod { - return @{id = '23233-2342'} - } -ParameterFilter { $Method -eq 'Post'} - - Mock Invoke-RestMethod { - - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($iTracking -gt 9) { - return [PSCustomObject]@{ - isReady = $true - operationStatus = [PSCustomObject]@{state = 'Ready'} - } - } - - return [PSCustomObject]@{ - isReady = $false - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{state = 'InProgress'} - } - } - - It 'should create a new Kubernetes Serviceendpoint' { - Add-VSTeamKubernetesEndpoint -projectName 'project' -endpointName 'KubTest' ` - -kubernetesUrl 'http://myK8s.local' -clientKeyData '00000000-0000-0000-0000-000000000000' ` - -kubeconfig '{name: "myConfig"}' -clientCertificateData 'someClientCertData' - - # On PowerShell 5 the JSON has two spaces but on PowerShell 6 it only has one so - # test for both. - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - ($Body -like '*"acceptUntrustedCerts": false*' -or $Body -like '*"acceptUntrustedCerts": false*') -and - ($Body -like '*"generatePfx": false*' -or $Body -like '*"generatePfx": false*') - } - } - } - - Context 'Add-VSTeamKubernetesEndpoint accepting untrusted certs and generating a pfx' { - Mock Write-Progress - Mock Invoke-RestMethod { - return @{id = '23233-2342'} - } -ParameterFilter { $Method -eq 'Post'} - Mock Invoke-RestMethod { - - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($iTracking -gt 9) { - return [PSCustomObject]@{ - isReady = $true - operationStatus = [PSCustomObject]@{state = 'Ready'} - } - } - - return [PSCustomObject]@{ - isReady = $false - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{state = 'InProgress'} - } - } - - It 'should create a new Kubernetes Serviceendpoint' { - Add-VSTeamKubernetesEndpoint -projectName 'project' -endpointName 'KubTest' ` - -kubernetesUrl 'http://myK8s.local' -clientKeyData '00000000-0000-0000-0000-000000000000' ` - -kubeconfig '{name: "myConfig"}' -clientCertificateData 'someClientCertData' -acceptUntrustedCerts -generatePfx - - # On PowerShell 5 the JSON has two spaces but on PowerShell 6 it only has one so - # test for both. - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - ($Body -like '*"acceptUntrustedCerts": true*' -or $Body -like '*"acceptUntrustedCerts": true*') -and - ($Body -like '*"generatePfx": true*' -or $Body -like '*"generatePfx": true*') - } - } - } - - Context 'Add-VSTeamNuGetEndpoint with ApiKey' { - Mock Write-Progress - Mock Invoke-RestMethod { - return @{id = '23233-2342'} - } -ParameterFilter { $Method -eq 'Post'} - Mock Invoke-RestMethod { - - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($iTracking -gt 9) { - return [PSCustomObject]@{ - isReady = $true - operationStatus = [PSCustomObject]@{state = 'Ready'} - } - } - - return [PSCustomObject]@{ - isReady = $false - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{state = 'InProgress'} - } - } - - Add-VSTeamNuGetEndpoint -ProjectName 'project' -EndpointName 'PowerShell Gallery' -NuGetUrl 'https://www.powershellgallery.com/api/v2/package' -ApiKey '00000000-0000-0000-0000-000000000000' - - It 'should create a new NuGet Serviceendpoint' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/serviceendpoints?api-version=$([VSTeamVersions]::DistributedTask)" -and - $Method -eq 'Post' -and - $ContentType -eq 'application/json' -and - $Body -like '*"nugetkey": *"00000000-0000-0000-0000-000000000000"*' -and - $Body -like '*"scheme": *"None"*' - } - } - } - - Context 'Add-VSTeamNuGetEndpoint with Username and Password' { - Mock Write-Progress - Mock Invoke-RestMethod { - return @{id = '23233-2342'} - } -ParameterFilter { $Method -eq 'Post'} - Mock Invoke-RestMethod { - - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($iTracking -gt 9) { - return [PSCustomObject]@{ - isReady = $true - operationStatus = [PSCustomObject]@{state = 'Ready'} - } - } - - return [PSCustomObject]@{ - isReady = $false - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{state = 'InProgress'} - } - } - - $password = '00000000-0000-0000-0000-000000000000' | ConvertTo-SecureString -AsPlainText -Force - Add-VSTeamNuGetEndpoint -ProjectName 'project' -EndpointName 'PowerShell Gallery' -NuGetUrl 'https://www.powershellgallery.com/api/v2/package' -Username 'testUser' -SecurePassword $password - - It 'should create a new NuGet Serviceendpoint' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/serviceendpoints?api-version=$([VSTeamVersions]::DistributedTask)" -and - $Method -eq 'Post' -and - $ContentType -eq 'application/json' -and - $Body -like '*"username": *"testUser"*' -and - $Body -like '*"password": *"00000000-0000-0000-0000-000000000000"*' -and - $Body -like '*"scheme": *"UsernamePassword"*' - } - } - } - - Context 'Add-VSTeamNuGetEndpoint with Token' { - Mock Write-Progress - - Mock Invoke-RestMethod { - # Write-Host "$args" - return @{id = '23233-2342'} - } -ParameterFilter { $Method -eq 'Post'} - - Mock Invoke-RestMethod { - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($iTracking -gt 9) { - return [PSCustomObject]@{ - isReady = $true - operationStatus = [PSCustomObject]@{state = 'Ready'} - } - } - - return [PSCustomObject]@{ - isReady = $false - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{state = 'InProgress'} - } - } - - Add-VSTeamNuGetEndpoint -ProjectName 'project' -EndpointName 'PowerShell Gallery' -NuGetUrl 'https://www.powershellgallery.com/api/v2/package' -PersonalAccessToken '00000000-0000-0000-0000-000000000000' - - It 'should create a new NuGet Serviceendpoint' { - Assert-MockCalled Invoke-RestMethod -Exactly -Scope Context -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/serviceendpoints?api-version=$([VSTeamVersions]::DistributedTask)" -and - $Method -eq 'Post' -and - $ContentType -eq 'application/json' -and - $Body -like '*"apitoken":*"00000000-0000-0000-0000-000000000000"*' -and - $Body -like '*"scheme":*"Token"*' - } - } - } - - Context 'Update-VSTeamServiceEndpoint' { - Mock Write-Progress - Mock Invoke-RestMethod { return @{id = '23233-2342'} } -ParameterFilter { $Method -eq 'Get'} - Mock Invoke-RestMethod { return @{id = '23233-2342'} } -ParameterFilter { $Method -eq 'Put'} - Mock Invoke-RestMethod { - - # This $i is in the module. Because we use InModuleScope - # we can see it - if ($iTracking -gt 9) { - return [PSCustomObject]@{ - isReady = $true - operationStatus = [PSCustomObject]@{state = 'Ready'} - } - } - - return [PSCustomObject]@{ - isReady = $false - createdBy = [PSCustomObject]@{} - authorization = [PSCustomObject]@{} - data = [PSCustomObject]@{} - operationStatus = [PSCustomObject]@{state = 'InProgress'} - } - } - - It 'should update Serviceendpoint' { - Update-VSTeamServiceEndpoint -projectName 'project' -id '23233-2342' ` - -object @{ key = 'value' } - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Put' - } - } - } - } -} \ No newline at end of file diff --git a/unit/test/serviceendpointsTypes.Tests.ps1 b/unit/test/serviceendpointsTypes.Tests.ps1 deleted file mode 100644 index 4aae94c28..000000000 --- a/unit/test/serviceendpointsTypes.Tests.ps1 +++ /dev/null @@ -1,74 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe 'serviceendpointTypes' { - $sampleFile = "$PSScriptRoot\sampleFiles\serviceEndpointTypeSample.json" - - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Get-VSTeamServiceEndpointTypes' { - Mock Invoke-RestMethod { - return Get-Content $sampleFile | ConvertFrom-Json - } - - It 'Should return all service endpoints types' { - Get-VSTeamServiceEndpointType - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/serviceendpointtypes?api-version=$([VSTeamVersions]::DistributedTask)" - } - } - } - - Context 'Get-VSTeamServiceEndpointTypes by Type' { - Mock Invoke-RestMethod { - return Get-Content $sampleFile | ConvertFrom-Json - } - - It 'Should return all service endpoints types' { - Get-VSTeamServiceEndpointType -Type azurerm - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/serviceendpointtypes?api-version=$([VSTeamVersions]::DistributedTask)" -and - $Body.type -eq 'azurerm' - } - } - } - - Context 'Get-VSTeamServiceEndpointTypes by Type and scheme' { - Mock Invoke-RestMethod { - return Get-Content $sampleFile | ConvertFrom-Json - } - - It 'Should return all service endpoints types' { - Get-VSTeamServiceEndpointType -Type azurerm -Scheme Basic - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/serviceendpointtypes?api-version=$([VSTeamVersions]::DistributedTask)" -and - $Body.type -eq 'azurerm' -and - $Body.scheme -eq 'Basic' - } - } - } - - Context 'Get-VSTeamServiceEndpointTypes by scheme' { - Mock Invoke-RestMethod { - return Get-Content $sampleFile | ConvertFrom-Json - } - - It 'Should return all service endpoints types' { - Get-VSTeamServiceEndpointType -Scheme Basic - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/distributedtask/serviceendpointtypes?api-version=$([VSTeamVersions]::DistributedTask)" -and - $Body.scheme -eq 'Basic' - } - } - } - } -} \ No newline at end of file diff --git a/unit/test/taskGroups.Tests.ps1 b/unit/test/taskGroups.Tests.ps1 deleted file mode 100644 index 59c3bbb43..000000000 --- a/unit/test/taskGroups.Tests.ps1 +++ /dev/null @@ -1,144 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - $taskGroupsJson = "$PSScriptRoot\sampleFiles\taskGroups.json" - $taskGroupJson = "$PSScriptRoot\sampleFiles\taskGroup.json" - - Describe 'Task Groups VSTS' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/project*" - } - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - BeforeAll { - Set-VSTeamAPIVersion -Target VSTS - $projectName = "project" - $taskGroupJsonAsString = Get-Content $taskGroupJson -Raw - } - - Context 'Get-VSTeamTaskGroup list' { - Mock Invoke-RestMethod { - return Get-Content $taskGroupsJson | ConvertFrom-Json - } - - It 'Should return all task groups' { - Get-VSTeamTaskGroup -projectName $projectName - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups?api-version=$([VSTeamVersions]::TaskGroups)" - } - } - } - - Context 'Get-VSTeamTaskGroup Id' { - Mock Invoke-RestMethod { - return Get-Content $taskGroupJson | ConvertFrom-Json - } - - It 'Should return one task group' { - $projectID = "d30f8b85-6b13-41a9-bb77-2e1a9c611def" - Get-VSTeamTaskGroup -projectName $projectName -id $projectID - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups/$($projectID)?api-version=$([VSTeamVersions]::TaskGroups)" - } - } - } - - Context 'Get-VSTeamTaskGroup Name' { - Mock Invoke-RestMethod { - # Return multiple task groups, because the function filters by name after getting the list from the server. - return Get-Content $taskGroupsJson | ConvertFrom-Json - } - - It 'Should return one task group' { - $taskGroupName = "For Unit Tests 2" - $taskGroup = Get-VSTeamTaskGroup -projectName $projectName -Name $taskGroupName - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups?api-version=$([VSTeamVersions]::TaskGroups)" - } - - # Ensure that we only have one task group, in other words, that the name filter was applied. - $taskGroup.name | Should Be $taskGroupName - } - } - - Context 'Remove-VSTeamTaskGroup' { - Mock Invoke-RestMethod - - It 'should delete Task group' { - $projectID = "d30f8b85-6b13-41a9-bb77-2e1a9c611def" - - Remove-VSTeamTaskGroup -projectName $projectName -Id $projectID -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups/$($projectID)?api-version=$([VSTeamVersions]::TaskGroups)" -and - $Method -eq 'Delete' - } - } - } - - Context 'Add-VSTeamTaskGroup' { - Mock Invoke-RestMethod { - return Get-Content $taskGroupJson | ConvertFrom-Json - } - - It 'should create a task group using body param' { - Add-VSTeamTaskGroup -ProjectName $projectName -Body $taskGroupJsonAsString - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups?api-version=$([VSTeamVersions]::TaskGroups)" -and - $Body -eq $taskGroupJsonAsString -and - $Method -eq "Post" - } - } - - It 'should create a task group using infile param' { - Add-VSTeamTaskGroup -ProjectName $projectName -InFile $taskGroupJson - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups?api-version=$([VSTeamVersions]::TaskGroups)" -and - $InFile -eq $taskGroupJson -and - $Method -eq "Post" - } - } - } - - Context 'Update-VSTeamTaskGroup' { - Mock Invoke-RestMethod { - return Get-Content $taskGroupJson | ConvertFrom-Json - } - - It 'should update a task group using body param' { - $taskGroupToUpdate = Get-VSTeamTaskGroup -Name "For Unit Tests" -ProjectName $projectName - - Update-VSTeamTaskGroup -ProjectName $projectName -Body $taskGroupJsonAsString -Id $taskGroupToUpdate.id - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups/$($taskGroupToUpdate.id)?api-version=$([VSTeamVersions]::TaskGroups)" -and - $Body -eq $taskGroupJsonAsString -and - $Method -eq "Put" - } - } - - It 'should update a task group using infile param' { - $taskGroupToUpdate = Get-VSTeamTaskGroup -Name "For Unit Tests" -ProjectName $projectName - - Update-VSTeamTaskGroup -ProjectName $projectName -InFile $taskGroupJson -Id $taskGroupToUpdate.id - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/$projectName/_apis/distributedtask/taskgroups/$($taskGroupToUpdate.id)?api-version=$([VSTeamVersions]::TaskGroups)" -and - $InFile -eq $taskGroupJson -and - $Method -eq "Put" - } - } - } - } -} \ No newline at end of file diff --git a/unit/test/teammembers.Tests.ps1 b/unit/test/teammembers.Tests.ps1 deleted file mode 100644 index 32642e942..000000000 --- a/unit/test/teammembers.Tests.ps1 +++ /dev/null @@ -1,89 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe "TeamMembers" { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" - - Context 'Get-VSTeamMember for specific project and team' { - Mock Invoke-RestMethod { return @{value = 'teams'}} - - It 'Should return teammembers' { - Get-VSTeamMember -ProjectName TestProject -TeamId TestTeam - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/TestProject/teams/TestTeam/members?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Get-VSTeamMember for specific project and team, with top' { - Mock Invoke-RestMethod { return @{value = 'teams'}} - - It 'Should return teammembers' { - Get-VSTeamMember -ProjectName TestProject -TeamId TestTeam -Top 10 - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/projects/TestProject/teams/TestTeam/members*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*`$top=10*" - } - } - } - - Context 'Get-VSTeamMember for specific project and team, with skip' { - Mock Invoke-RestMethod { return @{value = 'teams'}} - - It 'Should return teammembers' { - Get-VSTeamMember -ProjectName TestProject -TeamId TestTeam -Skip 5 - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/projects/TestProject/teams/TestTeam/members*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*`$skip=5*" - } - } - } - - Context 'Get-VSTeamMember for specific project and team, with top and skip' { - Mock Invoke-RestMethod { return @{value = 'teams'}} - - It 'Should return teammembers' { - Get-VSTeamMember -ProjectName TestProject -TeamId TestTeam -Top 10 -Skip 5 - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/projects/TestProject/teams/TestTeam/members*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*`$top=10*" -and - $Uri -like "*`$skip=5*" - } - } - } - - Context 'Get-VSTeamMember for specific team, fed through pipeline' { - Mock Invoke-RestMethod { return @{value = 'teammembers'}} - - It 'Should return teammembers' { - New-Object -TypeName PSObject -Prop @{projectname = "TestProject"; name = "TestTeam"} | Get-VSTeamMember - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/TestProject/teams/TestTeam/members?api-version=$([VSTeamVersions]::Core)" - } - } - } - - # Must be last because it sets [VSTeamVersions]::Account to $null - Context '_buildURL handles exception' { - - # Arrange - [VSTeamVersions]::Account = $null - - It 'should return approvals' { - - # Act - { _buildURL -ProjectName project -TeamId 1 } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/teams.Tests.ps1 b/unit/test/teams.Tests.ps1 deleted file mode 100644 index 396ceb3b7..000000000 --- a/unit/test/teams.Tests.ps1 +++ /dev/null @@ -1,394 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - $results = [PSCustomObject]@{ - value = [PSCustomObject]@{ - id = '6f365a7143e492e911c341451a734401bcacadfd' - name = 'refs/heads/master' - description = 'team description' - } - } - - $singleResult = [PSCustomObject]@{ - id = '6f365a7143e492e911c341451a734401bcacadfd' - name = 'refs/heads/master' - description = 'team description' - } - - Describe "Teams VSTS" { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" - - Context 'Get-VSTeam with project name' { - Mock Invoke-RestMethod { return $results } - - It 'Should return teams' { - Get-VSTeam -ProjectName Test - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Get-VSTeam with project name, with top' { - Mock Invoke-RestMethod { return $results } - - It 'Should return teams' { - Get-VSTeam -ProjectName Test -Top 10 - - # With PowerShell core the order of the query string is not the - # same from run to run! So instead of testing the entire string - # matches I have to search for the portions I expect but can't - # assume the order. - # The general string should look like this: - # "https://dev.azure.com/test/_apis/projects/Test/teams/?api-version=$([VSTeamVersions]::Core)&`$top=10" - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/projects/Test/teams*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*`$top=10*" - } - } - } - - Context 'Get-VSTeam with project name, with skip' { - Mock Invoke-RestMethod { return $results } - - It 'Should return teams' { - Get-VSTeam -ProjectName Test -Skip 10 - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/projects/Test/teams*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*`$skip=10*" - } - } - } - - Context 'Get-VSTeam with project name, with top and skip' { - Mock Invoke-RestMethod { return $results } - - It 'Should return teams' { - Get-VSTeam -ProjectName Test -Top 10 -Skip 5 - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/projects/Test/teams*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*`$top=10*" -and - $Uri -like "*`$skip=5*" - } - } - } - - Context 'Get-VSTeam with specific project and specific team id' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should return teams' { - Get-VSTeam -ProjectName Test -TeamId TestTeamId - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams/TestTeamId?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Get-VSTeam with specific project and specific team Name' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should return teams' { - Get-VSTeam -ProjectName Test -TeamName TestTeamName - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams/TestTeamName?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Add-VSTeam with team name only' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should create a team' { - Add-VSTeam -ProjectName Test -TeamName "TestTeam" - - $expectedBody = '{ "name": "TestTeam", "description": "" }' - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams?api-version=$([VSTeamVersions]::Core)" -and - $Method -eq "Post" -and - $Body -eq $expectedBody - } - } - } - - Context 'Add-VSTeam with team name and description' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should create a team' { - Add-VSTeam -ProjectName Test -TeamName "TestTeam" -Description "Test Description" - - $expectedBody = '{ "name": "TestTeam", "description": "Test Description" }' - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams?api-version=$([VSTeamVersions]::Core)" -and - $Method -eq "Post" -and - $Body -eq $expectedBody - } - } - } - - Context 'Update-VSTeam without name or description' { - It 'Should throw' { - { Update-VSTeam -ProjectName Test -TeamToUpdate "OldTeamName" } | Should Throw - } - } - - Context 'Update-VSTeam with new team name' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should update the team' { - Update-VSTeam -ProjectName Test -TeamToUpdate "OldTeamName" -NewTeamName "NewTeamName" - - $expectedBody = '{ "name": "NewTeamName" }' - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams/OldTeamName?api-version=$([VSTeamVersions]::Core)" -and - $Method -eq "Patch" -and - $Body -eq $expectedBody - } - } - } - - Context 'Update-VSTeam with new description' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should update the team' { - Update-VSTeam -ProjectName Test -TeamToUpdate "OldTeamName" -Description "New Description" - - $expectedBody = '{"description": "New Description" }' - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams/OldTeamName?api-version=$([VSTeamVersions]::Core)" -and - $Method -eq "Patch" -and - $Body -eq $expectedBody - } - } - } - - Context 'Update-VSTeam with new team name and description' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should update the team' { - Update-VSTeam -ProjectName Test -TeamToUpdate "OldTeamName" -NewTeamName "NewTeamName" -Description "New Description" - - $expectedBody = '{ "name": "NewTeamName", "description": "New Description" }' - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams/OldTeamName?api-version=$([VSTeamVersions]::Core)" -and - $Method -eq "Patch" -and - $Body -eq $expectedBody - } - } - } - - Context 'Update-VSTeam, fed through pipeline' { - Mock Get-VSTeam { return New-Object -TypeName PSObject -Prop @{projectname = "TestProject"; name = "OldTeamName"} } - Mock Invoke-RestMethod { return $singleResult } - - It 'Should update the team' { - Get-VSTeam -ProjectName TestProject -TeamId "OldTeamName" | Update-VSTeam -NewTeamName "NewTeamName" -Description "New Description" - - $expectedBody = '{ "name": "NewTeamName", "description": "New Description" }' - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/TestProject/teams/OldTeamName?api-version=$([VSTeamVersions]::Core)" -and - $Method -eq "Patch" -and - $Body -eq $expectedBody - } - } - } - - Context 'Remove-VSTeam' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should remove the team' { - Remove-VSTeam -ProjectName Test -TeamId "TestTeam" -Force - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/Test/teams/TestTeam?api-version=$([VSTeamVersions]::Core)" -and - $Method -eq "Delete" - } - } - } - - Context 'Remove-VSTeam, fed through pipeline' { - Mock Get-VSTeam { return New-Object -TypeName PSObject -Prop @{projectname = "TestProject"; name = "TestTeam"} } - Mock Invoke-RestMethod { return $singleResult } - - It 'Should remove the team' { - Get-VSTeam -ProjectName TestProject -TeamId "TestTeam" | Remove-VSTeam -Force - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/projects/TestProject/teams/TestTeam?api-version=$([VSTeamVersions]::Core)" -and - $Method -eq "Delete" - } - } - } - } - - Describe "Teams TFS" { - . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" - - Mock _useWindowsAuthenticationOnPremise { return $true } - - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - Context 'Get-VSTeam with project name on TFS local Auth' { - Mock Invoke-RestMethod { return $results } - - It 'Should return teams' { - Get-VSTeam -ProjectName Test - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Get-VSTeam with project name, with top on TFS local Auth' { - Mock Invoke-RestMethod { return $results } - - It 'Should return teams' { - Get-VSTeam -ProjectName Test -Top 10 - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams?*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*`$top=10*" - } - } - } - - Context 'Get-VSTeam with project name, with skip on TFS local Auth' { - Mock Invoke-RestMethod { return $results } - - It 'Should return teams' { - Get-VSTeam -ProjectName Test -Skip 10 - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*`$skip=10*" - } - } - } - - Context 'Get-VSTeam with project name, with top and skip on TFS local Auth' { - Mock Invoke-RestMethod { return $results } - - It 'Should return teams' { - Get-VSTeam -ProjectName Test -Top 10 -Skip 5 - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "*http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*`$top=10*" -and - $Uri -like "*`$skip=5*" - } - } - } - - Context 'Get-VSTeam with specific project and specific team Name on TFS local Auth' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should return teams' { - Get-VSTeam -ProjectName Test -Name TestTeamName - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams/TestTeamName?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Get-VSTeam with specific project and specific team ID on TFS local Auth' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should return teams' { - Get-VSTeam -ProjectName Test -TeamId TestTeamId - - # Make sure it was called with the correct URI - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams/TestTeamId?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Add-VSTeam with team name only on TFS local Auth' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should create a team' { - Add-VSTeam -ProjectName Test -TeamName "TestTeam" - - $expectedBody = '{ "name": "TestTeam", "description": "" }' - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams?api-version=$([VSTeamVersions]::Core)" -and - $Method -eq "Post" -and - $Body -eq $expectedBody - } - } - } - - Context 'Update-VSTeam with new team name on TFS local Auth' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should update the team' { - Update-VSTeam -ProjectName Test -TeamToUpdate "OldTeamName" -NewTeamName "NewTeamName" - - $expectedBody = '{ "name": "NewTeamName" }' - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams/OldTeamName?api-version=$([VSTeamVersions]::Core)" -and - $Method -eq "Patch" -and - $Body -eq $expectedBody - } - } - } - - Context 'Remove-VSTeam on TFS local Auth' { - Mock Invoke-RestMethod { return $singleResult } - - It 'Should remove the team' { - Remove-VSTeam -ProjectName Test -TeamId "TestTeam" -Force - - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/_apis/projects/Test/teams/TestTeam?api-version=$([VSTeamVersions]::Core)" -and - $Method -eq "Delete" - } - } - } - - # Must be last because it sets [VSTeamVersions]::Account to $null - Context '_buildURL handles exception' { - - # Arrange - [VSTeamVersions]::Account = $null - - It 'should return approvals' { - - # Act - { _buildURL -ProjectName project } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/teamspsdrive.Tests.ps1 b/unit/test/teamspsdrive.Tests.ps1 deleted file mode 100644 index f804d6e97..000000000 --- a/unit/test/teamspsdrive.Tests.ps1 +++ /dev/null @@ -1,569 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe 'VSTeam Classes' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - Context 'VSTeamUserEntitlement ToString' { - $obj = [PSCustomObject]@{ - displayName = 'Test User' - id = '1' - uniqueName = 'test@email.com' - } - - $target = [VSTeamUserEntitlement]::new($obj, 'Test Project') - - It 'should return displayname' { - $target.ToString() | Should Be 'Test User' - } - } - - Context 'VSTeamProject ToString' { - $obj = @{ - name = 'Test Project' - id = 1 - description = '' - url = '' - state = '' - revision = '' - visibility = '' - } - - $target = [VSTeamProject]::new($obj) - - It 'should return name' { - $target.ToString() | Should Be 'Test Project' - } - } - } - - $buildDefResultsVSTS = Get-Content "$PSScriptRoot\sampleFiles\buildDefvsts.json" -Raw | ConvertFrom-Json - $buildDefResults2017 = Get-Content "$PSScriptRoot\sampleFiles\buildDef2017.json" -Raw | ConvertFrom-Json - $buildDefResults2018 = Get-Content "$PSScriptRoot\sampleFiles\buildDef2018.json" -Raw | ConvertFrom-Json - $buildDefResultsyaml = Get-Content "$PSScriptRoot\sampleFiles\buildDefyaml.json" -Raw | ConvertFrom-Json - $buildDefResultsAzD = Get-Content "$PSScriptRoot\sampleFiles\buildDefAzD.json" -Raw | ConvertFrom-Json - - Describe 'TFS 2017 Build Definition' { - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Build Definitions' { - Mock Get-VSTeamBuildDefinition { - return @( - [VSTeamBuildDefinition]::new($buildDefResults2017.value[0], 'TestProject') - ) - } - - $buildDefinitions = [VSTeamBuildDefinitions]::new('Build Definitions', 'TestProject') - - It 'Should create Build definitions' { - $buildDefinitions | Should Not be $null - } - - $hasSteps = $buildDefinitions.GetChildItem()[0] - - It 'Should have Steps' { - $hasSteps | Should Not Be $null - } - - $steps = $hasSteps.GetChildItem() - - It 'Should parse steps' { - $steps.Length | Should Be 10 - } - } - } - - Describe 'TFS 2018 Build Definition' { - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Build Definitions' { - Mock Get-VSTeamBuildDefinition { - return @( - [VSTeamBuildDefinition]::new($buildDefResults2018.value[0], 'TestProject') - ) - } - - $buildDefinitions = [VSTeamBuildDefinitions]::new('Build Definitions', 'TestProject') - - It 'Should create Build definitions' { - $buildDefinitions | Should Not be $null - } - - $hasSteps = $buildDefinitions.GetChildItem()[0] - - It 'Should have Steps' { - $hasSteps | Should Not Be $null - } - - $steps = $hasSteps.GetChildItem() - - It 'Should parse steps' { - $steps.Length | Should Be 9 - } - } - } - - Describe 'VSTS Build Definition' { - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Build Definitions' { - Mock Get-VSTeamBuildDefinition { - return @( - [VSTeamBuildDefinition]::new($buildDefResultsVSTS.value[0], 'TestProject'), - [VSTeamBuildDefinition]::new($buildDefResultsyaml.value[0], 'TestProject'), - [VSTeamBuildDefinition]::new($buildDefResultsAzD.value[0], 'TestProject') - ) - } - - $buildDefinitions = [VSTeamBuildDefinitions]::new('Build Definitions', 'TestProject') - - It 'Should create Build definitions' { - $buildDefinitions | Should Not be $null - } - - $VSTeamBuildDefinitionWithPhases = $buildDefinitions.GetChildItem()[0] - - It 'Should parse phases' { - $VSTeamBuildDefinitionWithPhases.Process.Phases.Length | Should Be 1 - } - - It 'Should show steps in tostring' { - $VSTeamBuildDefinitionWithPhases.Process.ToString() | Should Be 'Number of phases: 1' - } - - $process = $VSTeamBuildDefinitionWithPhases.GetChildItem()[0] - - It 'Should return process' { - $process | Should Not Be $null - } - - $steps = $process.GetChildItem() - - It 'Should parse steps' { - $steps.Length | Should Be 9 - } - - $yamlBuild = $buildDefinitions.GetChildItem()[1] - $yamlFile = $yamlBuild.GetChildItem() - - It 'Should have yamlFilename' { - $yamlFile | Should Be '.vsts-ci.yml' - } - - $version5Build = $buildDefinitions.GetChildItem()[2] - - It 'Should have jobCancelTimeoutInMinutes' { - $version5Build.JobCancelTimeoutInMinutes | Should Be '5' - } - } - } - Describe 'TeamsPSDrive' { - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'VSTeamAccount & Projects' { - Mock Get-VSTeamProject { return [VSTeamProject]::new([PSCustomObject]@{ - name = 'TestProject' - description = '' - url = '' - id = '123 - 5464-dee43' - state = '' - visibility = '' - revision = 0 - defaultTeam = [PSCustomObject]@{} - _links = [PSCustomObject]@{} - } - ) - } - - Mock Get-VSTeamPool { - return [PSCustomObject]@{ - name = 'Default' - id = 1 - size = 1 - createdBy = [PSCustomObject]@{} - administratorsGroup = [PSCustomObject]@{} - serviceAccountsGroup = [PSCustomObject]@{} - } - } - - $account = [VSTeamAccount]::new('TestAccount') - - It 'Should create VSTeamAccount' { - $account | Should Not Be $null - } - - # Skip 0 because that will be Agent Pools - # Skip 1 because that will be Extensions - # Skip 2 because that will be Feeds - # Skip 3 because that will be Permissions - $project = $account.GetChildItem()[4] - - It 'Should return projects' { - $project | Should Not Be $null - } - - $actual = $project.GetChildItem() - - It 'Should return builds, releases, repositories and teams' { - $actual | Should Not Be $null - $actual[0].Name | Should Be 'Build Definitions' - $actual[0].ProjectName | Should Be 'TestProject' - $actual[1].Name | Should Be 'Builds' - $actual[1].ProjectName | Should Be 'TestProject' - $actual[2].Name | Should Be 'Queues' - $actual[2].ProjectName | Should Be 'TestProject' - $actual[3].Name | Should Be 'Release Definitions' - $actual[3].ProjectName | Should Be 'TestProject' - $actual[4].Name | Should Be 'Releases' - $actual[4].ProjectName | Should Be 'TestProject' - $actual[5].Name | Should Be 'Repositories' - $actual[5].ProjectName | Should Be 'TestProject' - $actual[6].Name | Should Be 'Teams' - $actual[6].ProjectName | Should Be 'TestProject' - } - } - - Context 'Agent Pools' { - Mock Get-VSTeamPool { return [VSTeamPool]::new(@{ - owner = [PSCustomObject]@{ - displayName = 'Test User' - id = '1' - uniqueName = 'test@email.com' - } - createdBy = [PSCustomObject]@{ - displayName = 'Test User' - id = '1' - uniqueName = 'test@email.com' - } - id = 1 - size = 1 - isHosted = $false - Name = 'Default' - } - ) - } - - Mock Get-VSTeamAgent { return [VSTeamAgent]::new(@{ - _links = [PSCustomObject]@{} - createdOn = '2018-03-28T16:48:58.317Z' - maxParallelism = 1 - id = 102 - enabled = $false - status = 'Online' - version = '1.336.1' - osDescription = 'Linux' - name = 'Test_Agent' - systemCapabilities = [PSCustomObject]@{} - }, 1 - ) - } - - $target = [VSTeamPools]::new('Agent Pools') - - It 'Should create Agent Pools' { - $target | Should Not Be $null - } - - $pool = $target.GetChildItem()[0] - - It 'Should return pool' { - $pool | Should Not Be $null - } - - $agent = $pool.GetChildItem()[0] - - It 'Should return agent' { - $agent | Should Not Be $null - } - } - - Context 'Feeds' { - $feedResults = Get-Content "$PSScriptRoot\sampleFiles\feeds.json" -Raw | ConvertFrom-Json - $singleResult = $feedResults.value[0] - - Mock Get-VSTeamFeed { - return [VSTeamFeed]::new($singleResult) - } - - $target = [VSTeamFeeds]::new('Feeds') - - It 'Should create Feeds' { - $target | Should Not Be $null - } - - $feed = $target.GetChildItem()[0] - - It 'Should return feed' { - $feed | Should Not Be $null - } - } - - Context 'Builds' { - Mock Get-VSTeamBuild { return @([PSCustomObject]@{ - id = 1 - description = '' - buildNumber = '1' - status = 'completed' - result = 'succeeded' - startTime = Get-Date - lastChangedBy = [PSCustomObject]@{ - id = '' - displayName = 'Test User' - uniqueName = 'test@email.com' - } - requestedBy = [PSCustomObject]@{ - id = '' - displayName = 'Test User' - uniqueName = 'test@email.com' - } - requestedFor = [PSCustomObject]@{ - id = '' - displayName = 'Test User' - uniqueName = 'test@email.com' - } - definition = [PSCustomObject]@{ - name = 'Test CI' - fullname = 'Test CI' - } - project = [PSCustomObject]@{ - name = 'Test Project' - } - } - ) - } - - $builds = [VSTeamBuilds]::new('TestBuild', 'TestProject') - - It 'Should create Builds' { - $builds | Should Not Be $null - } - - $build = $builds.GetChildItem() - - It 'Should return build' { - $build | Should Not Be $null - } - } - - Context 'Build Definitions' { - Mock Get-VSTeamBuildDefinition { return @( - [VSTeamBuildDefinition]::new(@{}, 'TestProject'), - [VSTeamBuildDefinition]::new(@{}, 'TestProject') - ) - } - - $buildDefinitions = [VSTeamBuildDefinitions]::new('Build Definitions', 'TestProject') - - It 'Should create Build definitions' { - $buildDefinitions | Should Not be $null - } - - # $VSTeamBuildDefinitionWithPhases = $buildDefinitions.GetChildItem()[0] - - # $yamlBuild = $buildDefinitions.GetChildItem()[1] - } - - Context 'Releases' { - Mock Get-VSTeamRelease { return [PSCustomObject]@{ - id = 1 - name = 'Release - 007' - status = 'active' - createdBy = [PSCustomObject]@{ - displayname = 'Test User' - uniqueName = 'test@email.com' - id = '1' - } - modifiedBy = [PSCustomObject]@{ - displayname = 'Test User' - uniqueName = 'test@email.com' - id = '1' - } - requestedFor = [PSCustomObject]@{ - displayname = '' - uniqueName = '' - id = '' - } - createdOn = Get-Date - releaseDefinition = [PSCustomObject]@{ - name = 'Test Release Def' - } - environments = @([PSCustomObject]@{ - id = 1 - name = 'Dev' - status = 'Succeeded' - deploySteps = @([PSCustomObject]@{ - id = 963 - deploymentId = 350 - attempt = 1 - reason = 'automated' - status = 'succeeded' - releaseDeployPhases = @([PSCustomObject]@{ - deploymentJobs = @([PSCustomObject]@{ - tasks = @([PSCustomObject]@{ - name = 'Initialize Job' - status = 'succeeded' - id = 1 - logUrl = '' - } - ) - } - ) - } - ) - } - ) - } - ) - } - } - - $releases = [VSTeamReleases]::new('Releases', 'TestProject') - - It 'Should create Releases directory' { - $releases | Should Not Be $null - } - - It 'Should be named Releases' { - $releases.Name | Should Be 'Releases' - } - - $release = $releases.GetChildItem()[0] - - It 'Should return releases' { - $release | Should Not Be $null - } - - $env = $release.GetChildItem()[0] - - It 'Should return environments' { - $env | Should Not Be $null - } - - $attempt = $env.GetChildItem()[0] - - It 'Should return attempts' { - $attempt | Should Not Be $null - } - - $task = $attempt.GetChildItem()[0] - - It 'Should return tasks' { - $task | Should Not Be $null - } - } - - Context 'Repositories' { - Mock Get-VSTeamGitRepository { return [VSTeamGitRepository]::new(@{ - id = "fa7b6ac1-0d4c-46cb-8565-8fd49e2031ad" - name = '' - url = '' - defaultBranch = '' - size = '' - remoteUrl = '' - sshUrl = '' - project = [PSCustomObject]@{ - name = 'TestProject' - description = '' - url = '' - id = '123 - 5464-dee43' - state = '' - visibility = '' - revision = 0 - defaultTeam = [PSCustomObject]@{} - _links = [PSCustomObject]@{} - } - }, 'TestProject') - } - - Mock Get-VSTeamGitRef { return [VSTeamRef]::new([PSCustomObject]@{ - objectId = '6f365a7143e492e911c341451a734401bcacadfd' - name = 'refs/heads/master' - creator = [PSCustomObject]@{ - displayName = 'Microsoft.VisualStudio.Services.TFS' - id = '1' - uniqueName = 'some@email.com' - } - }, 'TestProject') - } - - $repositories = [VSTeamRepositories]::new('Repositories', 'TestProject') - - It 'Should create Repositories' { - $repositories | Should Not Be $null - } - - $repository = $repositories.GetChildItem()[0] - - It 'Should return repository' { - $repository | Should Not Be $null - } - - $ref = $repository.GetChildItem()[0] - - It 'Should return ref' { - $ref | Should Not Be $null - } - } - - Context 'Teams' { - Mock Get-VSTeam { return [VSTeamTeam]::new(@{ - name = '' - ProjectName = '' - description = '' - id = 1 - }, 'TestProject') } - - $teams = [VSTeamTeams]::new('Teams', 'TestProject') - - It 'Should create Teams' { - $teams | Should Not Be $null - } - - $team = $teams.GetChildItem()[0] - - It 'Should return team' { - $team | Should Not Be $null - } - } - - Context 'Permissions' { - Set-StrictMode -Version Latest - Mock Get-VSTeamGroup { return [VSTeamGroup]::new(@{})} - Mock Get-VSTeamUser { return [VSTeamGroup]::new(@{})} - - $permissions = [VSTeamPermissions]::new('Permissions') - - It 'Should create Permissions' { - $permissions | Should Not Be $null - $permissions.GetChildItem().Count | Should Be 2 - } - - $groups = $permissions.GetChildItem()[0] - $users = $permissions.GetChildItem()[1] - - It 'Should return groups' { - $groups | Should Not Be $null - } - - It 'Should return users' { - $users | Should Not Be $null - } - } - } -} \ No newline at end of file diff --git a/unit/test/users.Tests.ps1 b/unit/test/users.Tests.ps1 deleted file mode 100644 index 5bff92de5..000000000 --- a/unit/test/users.Tests.ps1 +++ /dev/null @@ -1,99 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - - $userListResult = Get-Content "$PSScriptRoot\sampleFiles\users.json" -Raw | ConvertFrom-Json - $userSingleResult = Get-Content "$PSScriptRoot\sampleFiles\users.single.json" -Raw | ConvertFrom-Json - - # The Graph API is not supported on TFS - Describe "Users TFS Errors" { - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - Context 'Get-VSTeamUser' { - Mock _callAPI { throw 'Should not be called' } -Verifiable - - It 'Should throw' { - Set-VSTeamAPIVersion TFS2017 - - { Get-VSTeamUser } | Should Throw - } - } - } - - Describe 'Users VSTS' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Graph = '' - [VSTeamVersions]::Graph = '5.0' - - Context 'Get-VSTeamUser list' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $userListResult - } -Verifiable - - Get-VSTeamUser - - It 'Should return users' { - # With PowerShell core the order of the query string is not the - # same from run to run! So instead of testing the entire string - # matches I have to search for the portions I expect but can't - # assume the order. - # The general string should look like this: - # "https://vssps.dev.azure.com/test/_apis/graph/users?api-version=$([VSTeamVersions]::Graph)" - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/users*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Graph)*" - } - } - } - - Context 'Get-VSTeamUser by subjectTypes' { - Mock Invoke-RestMethod { return $userListResult } -Verifiable - - Get-VSTeamUser -SubjectTypes vss, aad - - It 'Should return users' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/users*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Graph)*" -and - $Uri -like "*subjectTypes=vss,aad*" - } - } - } - - Context 'Get-VSTeamUser by descriptor' { - Mock Invoke-RestMethod { return $userSingleResult } -Verifiable - - Get-VSTeamUser -UserDescriptor 'aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj' - - It 'Should return the user' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://vssps.dev.azure.com/test/_apis/graph/users/aad.OTcyOTJkNzYtMjc3Yi03OTgxLWIzNDMtNTkzYmM3ODZkYjlj*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Graph)*" - } - } - } - - Context 'Get-VSTeamUser list throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Get-VSTeamUser } | Should Throw - } - } - - Context 'Get-VSTeamUser by descriptor throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Get-VSTeamUser -UserDescriptor } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/usersentitlement.Tests.ps1 b/unit/test/usersentitlement.Tests.ps1 deleted file mode 100644 index fd2dbfd67..000000000 --- a/unit/test/usersentitlement.Tests.ps1 +++ /dev/null @@ -1,269 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe "Users TFS Errors" { - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - Context 'Get-VSTeamUserEntitlement' { - Mock _callAPI { throw 'Should not be called' } -Verifiable - - It 'Should throw' { - Set-VSTeamAPIVersion TFS2017 - - { Get-VSTeamUserEntitlement } | Should Throw - } - - It '_callAPI should not be called' { - Assert-MockCalled _callAPI -Exactly 0 - } - } - } - - Describe "Users VSTS" { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - . "$PSScriptRoot\mocks\mockProjectDynamicParamMandatoryFalse.ps1" - - # Must be defined or call will throw error - [VSTeamVersions]::MemberEntitlementManagement = '4.1-preview' - - Context 'Get-VSTeamUserEntitlement no parameters' { - Mock _callAPI { return [PSCustomObject]@{ - members = [PSCustomObject]@{ accessLevel = [PSCustomObject]@{ } } - } - } - - It 'Should return users' { - Get-VSTeamUserEntitlement - - # Make sure it was called with the correct URI - Assert-MockCalled _callAPI -Exactly 1 -ParameterFilter { - $url -eq "https://vsaex.dev.azure.com/test/_apis/userentitlements?api-version=$([VSTeamVersions]::MemberEntitlementManagement)&top=100&skip=0" - } - } - } - - Context 'Get-VSTeamUserEntitlement By ID' { - Mock _callAPI { - return [PSCustomObject]@{ - accessLevel = [PSCustomObject]@{ } - email = 'fake@email.com' - } - } - - It 'Should return users with projects' { - Get-VSTeamUserEntitlement -Id '00000000-0000-0000-0000-000000000000' - - # Make sure it was called with the correct URI - Assert-MockCalled _callAPI -Exactly 1 -ParameterFilter { - $subDomain -eq 'vsaex' -and - $id -eq '00000000-0000-0000-0000-000000000000' -and - $resource -eq 'userentitlements' - } - } - } - - Context 'Get-VSTeamUserEntitlement with select for projects' { - Mock _callAPI { - return [PSCustomObject]@{ - members = [PSCustomObject]@{ - accessLevel = [PSCustomObject]@{ } - email = 'fake@email.com' - } - } - } - - It 'Should return users with projects' { - Get-VSTeamUserEntitlement -Select Projects - - # Make sure it was called with the correct URI - Assert-MockCalled _callAPI -Exactly 1 -ParameterFilter { - $url -eq "https://vsaex.dev.azure.com/test/_apis/userentitlements?api-version=$([VSTeamVersions]::MemberEntitlementManagement)&top=100&skip=0&Select=Projects" - } - } - } - - Context 'Remove-VSTeamUserEntitlement by Id' { - Mock _callAPI -ParameterFilter { - $Method -eq 'Delete' -and - $subDomain -eq 'vsaex' -and - $id -eq '00000000-0000-0000-0000-000000000000' -and - $resource -eq 'userentitlements' -and - $version -eq [VSTeamVersions]::MemberEntitlementManagement - } - - Mock _callAPI { - return [PSCustomObject]@{ - accessLevel = [PSCustomObject]@{ } - email = 'test@user.com' - userName = 'Test User' - id = '00000000-0000-0000-0000-000000000000' - } - } - - Remove-VSTeamUserEntitlement -UserId '00000000-0000-0000-0000-000000000000' -Force - - It 'Should remove user' { - Assert-MockCalled _callAPI -Exactly 1 -ParameterFilter { - $subDomain -eq 'vsaex' -and - $id -eq '00000000-0000-0000-0000-000000000000' -and - $resource -eq 'userentitlements' -and - $method -eq 'Delete' -and - $version -eq [VSTeamVersions]::MemberEntitlementManagement - } - } - } - - Context 'Remove-VSTeamUserEntitlement by email' { - Mock _callAPI -ParameterFilter { - $Method -eq 'Delete' -and - $subDomain -eq 'vsaex' -and - $id -eq '00000000-0000-0000-0000-000000000000' -and - $resource -eq 'userentitlements' -and - $version -eq [VSTeamVersions]::MemberEntitlementManagement - } - - Mock _callAPI { - return [PSCustomObject]@{ - members = [PSCustomObject]@{ - accessLevel = [PSCustomObject]@{ } - email = 'test@user.com' - userName = 'Test User' - id = '00000000-0000-0000-0000-000000000000' - } - } - } - - Remove-VSTeamUserEntitlement -Email 'test@user.com' -Force - - It 'Should remmove user' { - Assert-MockCalled _callAPI -Exactly 1 -ParameterFilter { - $Method -eq 'Delete' -and - $subDomain -eq 'vsaex' -and - $id -eq '00000000-0000-0000-0000-000000000000' -and - $resource -eq 'userentitlements' -and - $version -eq [VSTeamVersions]::MemberEntitlementManagement - } - } - } - - Context 'Remove-VSTeamUserEntitlement by invalid email' { - Mock _callAPI { return [PSCustomObject]@{ - members = [PSCustomObject]@{ - accessLevel = [PSCustomObject]@{ } - email = 'test@user.com' - id = '00000000-0000-0000-0000-000000000000' - } - } - } - - It 'Should throw' { - { Remove-VSTeamUserEntitlement -Email 'not@found.com' -Force } | Should Throw - } - } - - Context 'Update-VSTeamUserEntitlement by invalid email' { - Mock _callAPI { return [PSCustomObject]@{ - members = [PSCustomObject]@{ - accessLevel = [PSCustomObject]@{ } - email = 'test@user.com' - id = '00000000-0000-0000-0000-000000000000' - } - } - } - - It 'Update User with invalid email should throw' { - { Update-VSTeamUserEntitlement -Email 'not@found.com' -License 'Express' -Force } | Should Throw - } - } - - Context 'Update-VSTeamUserEntitlement by invalid id' { - Mock _callAPI { return [PSCustomObject]@{ - members = [PSCustomObject]@{ - accessLevel = [PSCustomObject]@{ } - email = 'test@user.com' - id = '00000000-0000-0000-0000-000000000000' - } - } - } - - It 'Update User with invalid id should throw' { - { Update-VSTeamUserEntitlement -Id '11111111-0000-0000-0000-000000000000' -License 'Express' -Force } | Should Throw - } - } - - Context 'Add-VSTeamUserEntitlement' { - $obj = @{ - accessLevel = @{ - accountLicenseType = 'earlyAdopter' - licensingSource = 'msdn' - msdnLicenseType = 'enterprise' - } - user = @{ - principalName = 'test@user.com' - subjectKind = 'user' - } - projectEntitlements = @{ - group = @{ - groupType = 'ProjectContributor' - } - projectRef = @{ - id = $null - } - } - } - - $expected = $obj | ConvertTo-Json - - Mock _callAPI -Verifiable -ParameterFilter { - $Method -eq 'Post' -and - $Body -eq $expected - } - - Add-VSTeamUserEntitlement -License earlyAdopter -LicensingSource msdn -MSDNLicenseType enterprise -Email 'test@user.com' - - It 'Should add a user' { - Assert-VerifiableMock - } - } - - Context 'Update user should update' { - - Mock _callAPI { return [PSCustomObject]@{ - members = [PSCustomObject]@{ - accessLevel = [PSCustomObject]@{ - accountLicenseType = "Stakeholder" - licensingSource = "msdn" - msdnLicenseType = "enterprise" - } - email = 'test@user.com' - id = '00000000-0000-0000-0000-000000000000' - } - } - } - - Update-VSTeamUserEntitlement -License 'Stakeholder' -LicensingSource msdn -MSDNLicenseType enterprise -Email 'test@user.com' -Force - - It 'Should update a user' { - Assert-MockCalled _callAPI -Exactly 1 -ParameterFilter { - $Method -eq 'Patch' -and - $subDomain -eq 'vsaex' -and - $id -eq '00000000-0000-0000-0000-000000000000' -and - $resource -eq 'userentitlements' -and - $version -eq [VSTeamVersions]::MemberEntitlementManagement - } - - } - } - } -} \ No newline at end of file diff --git a/unit/test/variableGroups.Tests.ps1 b/unit/test/variableGroups.Tests.ps1 deleted file mode 100644 index c73f9542b..000000000 --- a/unit/test/variableGroups.Tests.ps1 +++ /dev/null @@ -1,302 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - $sampleFile2017 = "$PSScriptRoot\sampleFiles\variableGroupSamples2017.json" - - Describe 'Variable Groups 2017' { - Mock _getInstance { return 'http://localhost:8080/tfs/defaultcollection' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/project*" - } - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - [VSTeamVersions]::VariableGroups = '3.2-preview.1' - - BeforeAll { - Set-VSTeamAPIVersion -Target TFS2017 - } - - Context 'Get-VSTeamVariableGroup list' { - Mock Invoke-RestMethod { - return Get-Content $sampleFile2017 | ConvertFrom-Json - } - - It 'Should return all variable groups' { - Get-VSTeamVariableGroup -projectName project - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/distributedtask/variablegroups?api-version=$([VSTeamVersions]::VariableGroups)" - } - } - } - - Context 'Get-VSTeamVariableGroup Id' { - Mock Invoke-RestMethod { - #Write-Host $args - - $collection = Get-Content $sampleFile2017 | ConvertFrom-Json - return $collection.value | Where-Object {$_.id -eq 1} - } - - It 'Should return one variable group' { - $projectID = 1 - Get-VSTeamVariableGroup -projectName project -id $projectID - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/distributedtask/variablegroups/$($projectID)?api-version=$([VSTeamVersions]::VariableGroups)" - } - } - } - - Context 'Get-VSTeamVariableGroup Name' { - Mock Invoke-RestMethod { - - $collection = Get-Content $sampleFile2017 | ConvertFrom-Json - return $collection | Where-Object {$_.value.name -eq "TestVariableGroup1"} - } - - It 'Should return one variable group' { - $varGroupName = "TestVariableGroup1" - Get-VSTeamVariableGroup -projectName project -Name $varGroupName - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/distributedtask/variablegroups?api-version=$([VSTeamVersions]::VariableGroups)&groupName=$varGroupName" - } - } - } - - Context 'Remove-VSTeamVariableGroup' { - Mock Invoke-RestMethod - - It 'should delete variable group' { - $projectID = 1 - Remove-VSTeamVariableGroup -projectName project -id $projectID -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/distributedtask/variablegroups/$($projectID)?api-version=$([VSTeamVersions]::VariableGroups)" -and - $Method -eq 'Delete' - } - } - } - - Context 'Add-VSTeamVariableGroup' { - Mock Invoke-RestMethod { - #Write-Host $args - - $collection = Get-Content $sampleFile2017 | ConvertFrom-Json - return $collection.value | Where-Object {$_.id -eq 1} - } -Verifiable - - It 'should create a new Variable Group' { - $testParameters = @{ - ProjectName = "project" - Name = "TestVariableGroup2" - Description = "A test variable group linked to an Azure KeyVault" - Variables = @{ - key1 = @{ - value = "value" - } - } - } - - Add-VSTeamVariableGroup @testParameters - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } - } - } - - Context 'Update-VSTeamVariableGroup' { - Mock Invoke-RestMethod { - #Write-Host $args - - $collection = Get-Content $sampleFile2017 | ConvertFrom-Json - return $collection.value | Where-Object {$_.id -eq 1} - } -Verifiable - - It 'should update an exisiting Variable Group' { - $testParameters = @{ - ProjectName = "project" - id = 1 - Name = "TestVariableGroup1" - Description = "A test variable group" - Variables = @{ - key1 = @{ - value = "value" - } - key2 = @{ - value = "" - isSecret = $true - } - } - } - - Update-VSTeamVariableGroup @testParameters - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "http://localhost:8080/tfs/defaultcollection/project/_apis/distributedtask/variablegroups/$($testParameters.id)?api-version=$([VSTeamVersions]::VariableGroups)" -and - $Method -eq 'Put' - } - } - } - } - - $sampleFileVSTS = "$PSScriptRoot\sampleFiles\variableGroupSamples.json" - Describe 'Variable Groups VSTS' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/project*" - } - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - [VSTeamVersions]::VariableGroups = '5.0-preview.1' - - BeforeAll { - Set-VSTeamAPIVersion -Target VSTS - } - - Context 'Get-VSTeamVariableGroup list' { - Mock Invoke-RestMethod { - return Get-Content $sampleFileVSTS | ConvertFrom-Json - } - - It 'Should return all variable groups' { - Get-VSTeamVariableGroup -projectName project - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/variablegroups?api-version=$([VSTeamVersions]::VariableGroups)" - } - } - } - - Context 'Get-VSTeamVariableGroup Id' { - Mock Invoke-RestMethod { - #Write-Host $args - - $collection = Get-Content $sampleFileVSTS | ConvertFrom-Json - return $collection.value | Where-Object {$_.id -eq 1} - } - - It 'Should return one variable group' { - $projectID = 1 - Get-VSTeamVariableGroup -projectName project -id $projectID - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/variablegroups/$($projectID)?api-version=$([VSTeamVersions]::VariableGroups)" - } - } - } - - Context 'Remove-VSTeamVariableGroup' { - Mock Invoke-RestMethod - - It 'should delete variable group' { - $projectID = 1 - Remove-VSTeamVariableGroup -projectName project -Id $projectID -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/variablegroups/$($projectID)?api-version=$([VSTeamVersions]::VariableGroups)" -and - $Method -eq 'Delete' - } - } - } - - Context 'Add-VSTeamVariableGroup' { - Mock Invoke-RestMethod { - #Write-Host $args - - $collection = Get-Content $sampleFileVSTS | ConvertFrom-Json - return $collection.value | Where-Object {$_.id -eq 2} - } -Verifiable - - It 'should create a new AzureRM Key Vault Variable Group' { - $testParameters = @{ - ProjectName = "project" - Name = "TestVariableGroup2" - Description = "A test variable group linked to an Azure KeyVault" - Type = "AzureKeyVault" - Variables = @{ - key3 = @{ - enabled = $true - contentType = "" - value = "" - isSecret = $true - } - } - ProviderData = @{ - serviceEndpointId = "0228e842-65a7-4c64-90f7-0f07f3aa4e10" - vault = "keyVaultName" - } - } - - Add-VSTeamVariableGroup @testParameters - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Method -eq 'Post' } - } - - It "should create a new var group when passing the json as the body" { - $body = Get-Content $sampleFileVSTS -Raw - $projName = "project" - Add-VSTeamVariableGroup -Body $body -ProjectName $projName - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/$projName/_apis/distributedtask/variablegroups?api-version=$([VSTeamVersions]::VariableGroups)" -and - $Method -eq 'Post' } - } - } - - Context 'Update-VSTeamVariableGroup' { - Mock Invoke-RestMethod { - #Write-Host $args - - $collection = Get-Content $sampleFileVSTS | ConvertFrom-Json - return $collection.value | Where-Object {$_.id -eq 1} - } -Verifiable - - It 'should update an exisiting Variable Group' { - $testParameters = @{ - ProjectName = "project" - Id = 1 - Name = "TestVariableGroup1" - Description = "A test variable group" - Type = "Vsts" - Variables = @{ - key1 = @{ - value = "value" - } - key2 = @{ - value = "" - isSecret = $true - } - } - } - - Update-VSTeamVariableGroup @testParameters - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/project/_apis/distributedtask/variablegroups/$($testParameters.id)?api-version=$([VSTeamVersions]::VariableGroups)" -and - $Method -eq 'Put' - } - } - - It "should update an existing var group when passing the json as the body" { - $body = Get-Content $sampleFileVSTS -Raw - $projName = "project" - $id = "1" - Update-VSTeamVariableGroup -Body $body -ProjectName $projName -Id $id - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/$projName/_apis/distributedtask/variablegroups/$($id)?api-version=$([VSTeamVersions]::VariableGroups)" -and - $Method -eq 'Put' - } - } - } - } -} diff --git a/unit/test/wiql.Tests.ps1 b/unit/test/wiql.Tests.ps1 deleted file mode 100644 index f522cfe4e..000000000 --- a/unit/test/wiql.Tests.ps1 +++ /dev/null @@ -1,166 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe 'wiql' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - $workItem = @{ - id = 47 - url = "https://dev.azure.com/test/_apis/wit/workItems/47" - } - - $column = @{ - referenceName = "System.Id" - name = "ID" - url = "https://dev.azure.com/razorspoint-test/_apis/wit/fields/System.Id" - } - - $sortColumn = @{ - field = $column - descending = $false - } - - $wiqlResult = @{ - querytype = "flat" - queryTypeResult = "worItem" - asOf = "2019-10-03T18:35:09.117Z" - columns = @($column) - sortColumns = @($sortColumn) - workItems = @($workItem, $workItem) - } - - $expandedWorkItems = @{ - count = 1 - value = @($workItem, $workItem) - } - - Context 'Get-Wiql' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $wiqlResult - } - - # function is mocked because it is used when switch 'Expanded' is being used. - Mock Get-VSTeamWorkItem { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $expandedWorkItems - } - - It 'Get work items with custom WIQL query' { - $Global:PSDefaultParameterValues.Remove("*:projectName") - $wiqlQuery = "Select [System.Id], [System.Title], [System.State] From WorkItems" - Get-VSTeamWiql -ProjectName "test" -Team "test team" -Query $wiqlQuery - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Body -like '`{*' -and # Make sure the body is an object - $Body -like "*[System.Id]*" -and - $Body -like "*[System.Title]*" -and - $Body -like "*[System.State]*" -and - $Body -like '*`}' -and # Make sure the body is an object - $ContentType -eq 'application/json' -and - $Uri -eq "https://dev.azure.com/test/test/test team/_apis/wit/wiql?api-version=$([VSTeamVersions]::Core)&`$top=100" - } - } - - It 'Get work items with custom WIQL query with -Top 250' { - $Global:PSDefaultParameterValues.Remove("*:projectName") - $wiqlQuery = "Select [System.Id], [System.Title], [System.State] From WorkItems" - Get-VSTeamWiql -ProjectName "test" -Team "test team" -Query $wiqlQuery -Top 250 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Body -like '`{*' -and # Make sure the body is an object - $Body -like "*[System.Id]*" -and - $Body -like "*[System.Title]*" -and - $Body -like "*[System.State]*" -and - $Body -like '*`}' -and # Make sure the body is an object - $ContentType -eq 'application/json' -and - $Uri -eq "https://dev.azure.com/test/test/test team/_apis/wit/wiql?api-version=$([VSTeamVersions]::Core)&`$top=250" - } - } - - It 'Get work items with custom WIQL query with -Top 0' { - $Global:PSDefaultParameterValues.Remove("*:projectName") - $wiqlQuery = "Select [System.Id], [System.Title], [System.State] From WorkItems" - Get-VSTeamWiql -ProjectName "test" -Team "test team" -Query $wiqlQuery -Top 0 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Body -like '`{*' -and # Make sure the body is an object - $Body -like "*[System.Id]*" -and - $Body -like "*[System.Title]*" -and - $Body -like "*[System.State]*" -and - $Body -like '*`}' -and # Make sure the body is an object - $ContentType -eq 'application/json' -and - $Uri -eq "https://dev.azure.com/test/test/test team/_apis/wit/wiql?api-version=$([VSTeamVersions]::Core)" - } - } - - It 'Get work items with custom WIQL query with expanded work items' { - $Global:PSDefaultParameterValues.Remove("*:projectName") - $wiqlQuery = "Select [System.Id], [System.Title], [System.State] From WorkItems" - Get-VSTeamWiql -ProjectName "test" -Team "test team" -Query $wiqlQuery -Expand - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Body -like '`{*' -and # Make sure the body is an object - $Body -like "*[System.Id]*" -and - $Body -like "*[System.Title]*" -and - $Body -like "*[System.State]*" -and - $Body -like '*`}' -and # Make sure the body is an object - $ContentType -eq 'application/json' -and - $Uri -eq "https://dev.azure.com/test/test/test team/_apis/wit/wiql?api-version=$([VSTeamVersions]::Core)&`$top=100" - } - } - - It 'Get work items with custom WIQL query with time precision' { - $Global:PSDefaultParameterValues.Remove("*:projectName") - $wiqlQuery = "Select [System.Id], [System.Title], [System.State] From WorkItems" - Get-VSTeamWiql -ProjectName "test" -Team "test team" -Query $wiqlQuery -TimePrecision - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Body -like '`{*' -and # Make sure the body is an object - $Body -like "*[System.Id]*" -and - $Body -like "*[System.Title]*" -and - $Body -like "*[System.State]*" -and - $Body -like '*`}' -and # Make sure the body is an object - $ContentType -eq 'application/json' -and - $Uri -like "*timePrecision=True*" - $Uri -like "*`$top=100*" - $Uri -like "https://dev.azure.com/test/test/test team/_apis/wit/wiql?api-version=$([VSTeamVersions]::Core)*" - } - } - - It 'Get work items with query ID query' { - $Global:PSDefaultParameterValues.Remove("*:projectName") - Get-VSTeamWiql -ProjectName "test" -Team "test team" -Id 1 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/test/test team/_apis/wit/wiql/1?api-version=$([VSTeamVersions]::Core)&`$top=100" - } - } - - It 'Get work items with query ID query with expanded work items' { - $Global:PSDefaultParameterValues.Remove("*:projectName") - Get-VSTeamWiql -ProjectName "test" -Team "test team" -Id 1 -Expand - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/test/test team/_apis/wit/wiql/1?api-version=$([VSTeamVersions]::Core)&`$top=100" - } - } - } - } -} \ No newline at end of file diff --git a/unit/test/workItemPermissions.Tests.ps1 b/unit/test/workItemPermissions.Tests.ps1 deleted file mode 100644 index bed2bb275..000000000 --- a/unit/test/workItemPermissions.Tests.ps1 +++ /dev/null @@ -1,494 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - $userSingleResult = Get-Content "$PSScriptRoot\sampleFiles\users.single.json" -Raw | ConvertFrom-Json - $userSingleResultObject = [VSTeamUser]::new($userSingleResult) - - $groupSingleResult = Get-Content "$PSScriptRoot\sampleFiles\groupsSingle.json" -Raw | ConvertFrom-Json - $groupSingleResultObject = [VSTeamGroup]::new($groupSingleResult) - - $projectResult = [PSCustomObject]@{ - name = 'Test Project Public' - description = '' - url = '' - id = '010d06f0-00d5-472a-bb47-58947c230876' - state = '' - visibility = '' - revision = 0 - defaultTeam = [PSCustomObject]@{} - _links = [PSCustomObject]@{} - } - - $projectResultObject = [VSTeamProject]::new($projectResult) - - $accessControlEntryResult = - @" -{ - "count": 1, - "value": [ - { - "descriptor": "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-1204400969-2402986413-2179408616-0-0-0-0-1", - "allow": 8, - "deny": 0, - "extendedInfo": {} - } - ] -} -"@ | ConvertFrom-Json - - - - $classificationNodeById = - @" -{ - "count": 1, - "value": [ - { - "id": 44, - "identifier": "90aa2c42-de51-450a-bfb6-6e264e364d9a", - "name": "Child 1 Level 2", - "structureType": "area", - "hasChildren": false, - "path": "\\Demo Public\\Area\\Child 1 Level 1\\Child 1 Level 2", - "_links": { - "self": { - "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201/Child%201%20Level%202" - }, - "parent": { - "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201" - } - }, - "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201/Child%201%20Level%202" - } - ] - } -"@ | ConvertFrom-Json | Select-Object -ExpandProperty value - - $classificationNodeByIdObject = [VSTeamClassificationNode]::new($classificationNodeById, "test") - - $parentClassificationNode = - @" -{ - "count": 1, - "value": [ - { - "id": 43, - "identifier": "38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b", - "name": "Child 1 Level 1", - "structureType": "area", - "hasChildren": true, - "path": "\\Demo Public\\Area\\Child 1 Level 1", - "_links": { - "self": { - "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201" - }, - "parent": { - "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas" - } - }, - "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas/Child%201%20Level%201" - } - ] - } -"@ | ConvertFrom-Json | Select-Object -ExpandProperty value - - $parentClassificationNodeObject = [VSTeamClassificationNode]::new($parentClassificationNode, "test") - - - $classificationNodeIterationId = - @" -{ - "count": 1, - "value": [ - { - "id": 20, - "identifier": "18e7998d-d0c5-4c01-b547-d7d4eb4c97c5", - "name": "Sprint 3", - "structureType": "iteration", - "hasChildren": false, - "path": "\\Demo Public\\Iteration\\Sprint 3", - "_links": { - "self": { - "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%203" - }, - "parent": { - "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations" - } - }, - "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations/Sprint%203" - } - ] - } -"@ | ConvertFrom-Json | Select-Object -ExpandProperty value - - $classificationNodeIterationIdObject = [VSTeamClassificationNode]::new($classificationNodeIterationId, "test") - - - $areaRootNode = - @" -{ - "id": 24, - "identifier": "b33b12d7-6abb-4b7a-b9d6-2092d0933c99", - "name": "Demo Public", - "structureType": "area", - "hasChildren": true, - "path": "\\Demo Public\\Area", - "_links": { - "self": { - "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas" - } - }, - "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Areas" - } -"@ | ConvertFrom-Json - - $areaRootNodeObject = [VSTeamClassificationNode]::new($areaRootNode, "test") - - $iterationRootNode = - @" -{ - "id": 16, - "identifier": "dfa90792-403a-4119-a52b-bd142c08291b", - "name": "Demo Public", - "structureType": "iteration", - "hasChildren": true, - "path": "\\Demo Public\\Iteration", - "_links": { - "self": { - "href": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations" - } - }, - "url": "https://dev.azure.com/vsteampsmoduletest/53e2997d-3723-4c1c-aa62-a0194cb65a29/_apis/wit/classificationNodes/Iterations" - } -"@ | ConvertFrom-Json - - $iterationRootNodeObject = [VSTeamClassificationNode]::new($iterationRootNode, "test") - - Describe 'WorkItem Area/Iteration Permissions VSTS' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # You have to set the version or the api-version will not be added when - # [VSTeamVersions]::Core = '' - [VSTeamVersions]::Core = '5.0' - - Context 'Add-VSTeamWorkItemAreaPermission by AreaID and User' { - Mock _getProjects { return "Test Project Public" } -Verifiable - Mock Get-VSTeamClassificationNode { return $parentClassificationNodeObject } -ParameterFilter { $Path -eq "Child%201%20Level%201"} -Verifiable - Mock Get-VSTeamClassificationNode { return $classificationNodeByIdObject } -ParameterFilter { $Ids -eq 44 } -Verifiable - Mock Get-VSTeamClassificationNode { return $areaRootNodeObject } - - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $accessControlEntryResult - } -Verifiable - - Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaID 44 -User $userSingleResultObject -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/83e28ad4-2d72-4ceb-97b0-c7726d5502c3*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"vstfs:///Classification/Node/b33b12d7-6abb-4b7a-b9d6-2092d0933c99:vstfs:///Classification/Node/38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b:vstfs:///Classification/Node/90aa2c42-de51-450a-bfb6-6e264e364d9a`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and - $Body -like "*`"allow`": 65,*" -and - $Body -like "*`"deny`": 10,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamWorkItemAreaPermission by AreaID and Group' { - Mock _getProjects { return "Test Project Public" } -Verifiable - Mock Get-VSTeamClassificationNode { return $parentClassificationNodeObject } -ParameterFilter { $Path -eq "Child%201%20Level%201"} -Verifiable - Mock Get-VSTeamClassificationNode { return $classificationNodeByIdObject } -ParameterFilter { $Ids -eq 44 } -Verifiable - Mock Get-VSTeamClassificationNode { return $areaRootNodeObject } - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaID 44 -Group $groupSingleResultObject -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/83e28ad4-2d72-4ceb-97b0-c7726d5502c3*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"vstfs:///Classification/Node/b33b12d7-6abb-4b7a-b9d6-2092d0933c99:vstfs:///Classification/Node/38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b:vstfs:///Classification/Node/90aa2c42-de51-450a-bfb6-6e264e364d9a`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 65,*" -and - $Body -like "*`"deny`": 10,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamWorkItemAreaPermission by AreaID and Descriptor' { - Mock _getProjects { return "Test Project Public" } -Verifiable - Mock Get-VSTeamClassificationNode { return $parentClassificationNodeObject } -ParameterFilter { $Path -eq "Child%201%20Level%201"} -Verifiable - Mock Get-VSTeamClassificationNode { return $classificationNodeByIdObject } -ParameterFilter { $Ids -eq 44 } -Verifiable - Mock Get-VSTeamClassificationNode { return $areaRootNodeObject } - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaID 44 -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/83e28ad4-2d72-4ceb-97b0-c7726d5502c3*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"vstfs:///Classification/Node/b33b12d7-6abb-4b7a-b9d6-2092d0933c99:vstfs:///Classification/Node/38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b:vstfs:///Classification/Node/90aa2c42-de51-450a-bfb6-6e264e364d9a`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 65,*" -and - $Body -like "*`"deny`": 10,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamWorkItemAreaPermission by AreaPath and User' { - Mock _getProjects { return "Test Project Public" } -Verifiable - Mock Get-VSTeamClassificationNode { return $parentClassificationNodeObject } -ParameterFilter { $Path -eq "Child%201%20Level%201"} -Verifiable - Mock Get-VSTeamClassificationNode { return $classificationNodeByIdObject } -ParameterFilter { $Path -eq "Child 1 Level 1/Child 1 Level 2" } -Verifiable - Mock Get-VSTeamClassificationNode { return $areaRootNodeObject } - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $accessControlEntryResult - } -Verifiable - - Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaPath "Child 1 Level 1/Child 1 Level 2" -User $userSingleResultObject -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/83e28ad4-2d72-4ceb-97b0-c7726d5502c3*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"vstfs:///Classification/Node/b33b12d7-6abb-4b7a-b9d6-2092d0933c99:vstfs:///Classification/Node/38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b:vstfs:///Classification/Node/90aa2c42-de51-450a-bfb6-6e264e364d9a`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and - $Body -like "*`"allow`": 65,*" -and - $Body -like "*`"deny`": 10,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamWorkItemAreaPermission by AreaPath and Group' { - Mock _getProjects { return "Test Project Public" } -Verifiable - Mock Get-VSTeamClassificationNode { return $parentClassificationNodeObject } -ParameterFilter { $Path -eq "Child%201%20Level%201"} -Verifiable - Mock Get-VSTeamClassificationNode { return $classificationNodeByIdObject } -ParameterFilter { $Path -eq "Child 1 Level 1/Child 1 Level 2" } -Verifiable - Mock Get-VSTeamClassificationNode { return $areaRootNodeObject } - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaPath "Child 1 Level 1/Child 1 Level 2" -Group $groupSingleResultObject -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/83e28ad4-2d72-4ceb-97b0-c7726d5502c3*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"vstfs:///Classification/Node/b33b12d7-6abb-4b7a-b9d6-2092d0933c99:vstfs:///Classification/Node/38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b:vstfs:///Classification/Node/90aa2c42-de51-450a-bfb6-6e264e364d9a`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 65,*" -and - $Body -like "*`"deny`": 10,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamWorkItemAreaPermission by AreaPath and Descriptor' { - Mock _getProjects { return "Test Project Public" } -Verifiable - Mock Get-VSTeamClassificationNode { return $parentClassificationNodeObject } -ParameterFilter { $Path -eq "Child%201%20Level%201"} -Verifiable - Mock Get-VSTeamClassificationNode { return $classificationNodeByIdObject } -ParameterFilter { $Path -eq "Child 1 Level 1/Child 1 Level 2" } -Verifiable - Mock Get-VSTeamClassificationNode { return $areaRootNodeObject } - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaPath "Child 1 Level 1/Child 1 Level 2" -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/83e28ad4-2d72-4ceb-97b0-c7726d5502c3*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"vstfs:///Classification/Node/b33b12d7-6abb-4b7a-b9d6-2092d0933c99:vstfs:///Classification/Node/38de1ce0-0b1b-45f2-b4f9-f32e3a72b78b:vstfs:///Classification/Node/90aa2c42-de51-450a-bfb6-6e264e364d9a`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 65,*" -and - $Body -like "*`"deny`": 10,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamWorkItemAreaPermission by AreaPath and Descritpor throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaPath "Child 1 Level 1/Child 1 Level 2" -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') } | Should Throw - } - } - - Context 'Add-VSTeamWorkItemAreaPermission by AreaID and Descriptor Throws on Iteration ID' { - Mock _getProjects { return "Test Project Public" } -Verifiable - Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Ids -eq 20 } -Verifiable - - It 'Should throw' { - { Add-VSTeamWorkItemAreaPermission -Project $projectResultObject -AreaID 20 -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemAreaPermissions]'GENERIC_READ,MANAGE_TEST_PLANS') -Deny ([VSTeamWorkItemAreaPermissions]'GENERIC_WRITE,DELETE') } - } - } - - Context 'Add-VSTeamWorkItemIterationPermission by IterationID and User' { - Mock _getProjects { return "Test Project Public" } -Verifiable - Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Ids -eq 44 } -Verifiable - Mock Get-VSTeamClassificationNode { return $iterationRootNodeObject } - - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $accessControlEntryResult - } -Verifiable - - Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationID 44 -User $userSingleResultObject -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and - $Body -like "*`"allow`": 5,*" -and - $Body -like "*`"deny`": 8,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamWorkItemIterationPermission by IterationID and Group' { - Mock _getProjects { return "Test Project Public" } -Verifiable - Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Ids -eq 44 } -Verifiable - Mock Get-VSTeamClassificationNode { return $iterationRootNodeObject } - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationID 44 -Group $groupSingleResultObject -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 5,*" -and - $Body -like "*`"deny`": 8,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamWorkItemIterationPermission by IterationID and Descriptor' { - Mock _getProjects { return "Test Project Public" } -Verifiable - Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Ids -eq 44 } -Verifiable - Mock Get-VSTeamClassificationNode { return $iterationRootNodeObject } - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationID 44 -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 5,*" -and - $Body -like "*`"deny`": 8,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamWorkItemIterationPermission by IterationPath and User' { - Mock _getProjects { return "Test Project Public" } -Verifiable - Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Path -eq "Sprint 1" } -Verifiable - Mock Get-VSTeamClassificationNode { return $iterationRootNodeObject } - - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $accessControlEntryResult - } -Verifiable - - Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationPath "Sprint 1" -User $userSingleResultObject -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.IdentityModel.Claims.ClaimsIdentity;788df857-dcd8-444d-885e-bff359bc1982\\test@testuser.com`",*" -and - $Body -like "*`"allow`": 5,*" -and - $Body -like "*`"deny`": 8,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamWorkItemIterationPermission by IterationPath and Group' { - Mock _getProjects { return "Test Project Public" } -Verifiable - Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Path -eq "Sprint 1" } -Verifiable - Mock Get-VSTeamClassificationNode { return $iterationRootNodeObject } - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationPath "Sprint 1" -Group $groupSingleResultObject -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 5,*" -and - $Body -like "*`"deny`": 8,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamWorkItemIterationPermission by IterationPath and Descriptor' { - Mock _getProjects { return "Test Project Public" } -Verifiable - Mock Get-VSTeamClassificationNode { return $classificationNodeIterationIdObject } -ParameterFilter { $Path -eq "Sprint 1" } -Verifiable - Mock Get-VSTeamClassificationNode { return $iterationRootNodeObject } - Mock Invoke-RestMethod { return $accessControlEntryResult } -Verifiable - - Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationPath "Sprint 1" -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') - - It 'Should return ACEs' { - Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { - $Uri -like "https://dev.azure.com/test/_apis/accesscontrolentries/bf7bfa03-b2b7-47db-8113-fa2e002cc5b1*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Body -like "*`"token`": `"vstfs:///Classification/Node/dfa90792-403a-4119-a52b-bd142c08291b:vstfs:///Classification/Node/18e7998d-d0c5-4c01-b547-d7d4eb4c97c5`",*" -and - $Body -like "*`"descriptor`": `"Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1`",*" -and - $Body -like "*`"allow`": 5,*" -and - $Body -like "*`"deny`": 8,*" -and - $ContentType -eq "application/json" -and - $Method -eq "Post" - } - } - } - - Context 'Add-VSTeamWorkItemIterationPermission by IterationPath and Descritpor throws' { - Mock Invoke-RestMethod { throw 'Error' } - - It 'Should throw' { - { Add-VSTeamWorkItemIterationPermission -Project $projectResultObject -IterationPath "Child 1 Level 1/Child 1 Level 2" -Descriptor "Microsoft.TeamFoundation.Identity;S-1-9-1551374245-856009726-4193442117-2390756110-2740161821-0-0-0-0-1" -Allow ([VSTeamWorkItemIterationPermissions]'GENERIC_READ,CREATE_CHILDREN') -Deny ([VSTeamWorkItemIterationPermissions]'DELETE') } | Should Throw - } - } - } -} \ No newline at end of file diff --git a/unit/test/workitem.Tests.ps1 b/unit/test/workitem.Tests.ps1 deleted file mode 100644 index 5b9dd50de..000000000 --- a/unit/test/workitem.Tests.ps1 +++ /dev/null @@ -1,289 +0,0 @@ -Set-StrictMode -Version Latest - -InModuleScope VSTeam { - Describe 'workitems' { - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - . "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1" - - $obj = @{ - id = 47 - rev = 1 - url = "https://dev.azure.com/test/_apis/wit/workItems/47" - } - - $collection = @{ - count = 1 - value = @($obj) - } - - Context 'Add-WorkItem' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $obj - } - - It 'Without Default Project should add work item' { - $Global:PSDefaultParameterValues.Remove("*:projectName") - Add-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Body -like '`[*' -and # Make sure the body is an array - $Body -like '*`]' -and # Make sure the body is an array - $ContentType -eq 'application/json-patch+json' -and - $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitems/`$Task?api-version=$([VSTeamVersions]::Core)" - } - } - - It 'With Default Project should add work item' { - $Global:PSDefaultParameterValues["*:projectName"] = 'test' - Add-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test1 -Description Testing - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Body -like '`[*' -and # Make sure the body is an array - $Body -like '*Test1*' -and - $Body -like '*Testing*' -and - $Body -like '*/fields/System.Title*' -and - $Body -like '*/fields/System.Description*' -and - $Body -like '*`]' -and # Make sure the body is an array - $ContentType -eq 'application/json-patch+json' -and - $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitems/`$Task?api-version=$([VSTeamVersions]::Core)" - } - } - - It 'With Default Project should add work item with parent' { - $Global:PSDefaultParameterValues["*:projectName"] = 'test' - Add-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test1 -Description Testing -ParentId 25 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Body -like '`[*' -and # Make sure the body is an array - $Body -like '*Test1*' -and - $Body -like '*Testing*' -and - $Body -like '*/fields/System.Title*' -and - $Body -like '*/fields/System.Description*' -and - $Body -like '*/relations/-*' -and - $Body -like '*_apis/wit/workitems/25*' -and - $Body -like '*System.LinkTypes.Hierarchy-Reverse*' -and - $Body -like '*`]' -and # Make sure the body is an array - $ContentType -eq 'application/json-patch+json' -and - $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitems/`$Task?api-version=$([VSTeamVersions]::Core)" - } - } - - It 'With Default Project should add work item only with additional properties and parent id' { - $Global:PSDefaultParameterValues["*:projectName"] = 'test' - - $additionalFields = @{"System.Tags" = "TestTag"; "System.AreaPath" = "Project\\MyPath" } - Add-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test1 -Description Testing -ParentId 25 -AdditionalFields $additionalFields - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Body -like '`[*' -and # Make sure the body is an array - $Body -like '*Test1*' -and - $Body -like '*Testing*' -and - $Body -like '*/fields/System.Title*' -and - $Body -like '*/fields/System.Description*' -and - $Body -like '*/relations/-*' -and - $Body -like '*_apis/wit/workitems/25*' -and - $Body -like '*/fields/System.Tags*' -and - $Body -like '*/fields/System.AreaPath*' -and - $Body -like '*`]' -and # Make sure the body is an array - $ContentType -eq 'application/json-patch+json' -and - $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitems/`$Task?api-version=$([VSTeamVersions]::Core)" - } - } - - It 'With Default Project should add work item only with additional properties' { - $Global:PSDefaultParameterValues["*:projectName"] = 'test' - - $additionalFields = @{"System.Tags" = "TestTag"; "System.AreaPath" = "Project\\MyPath" } - Add-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test1 -AdditionalFields $additionalFields - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Post' -and - $Body -like '`[*' -and # Make sure the body is an array - $Body -like '*Test1*' -and - $Body -like '*/fields/System.Title*' -and - $Body -like '*/fields/System.Tags*' -and - $Body -like '*/fields/System.AreaPath*' -and - $Body -like '*`]' -and # Make sure the body is an array - $ContentType -eq 'application/json-patch+json' -and - $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitems/`$Task?api-version=$([VSTeamVersions]::Core)" - } - } - - It 'With Default Project should throw exception when adding existing parameters to additional properties and parent id' { - $Global:PSDefaultParameterValues["*:projectName"] = 'test' - - $additionalFields = @{"System.Title" = "Test1"; "System.AreaPath" = "Project\\TestPath" } - { Add-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test1 -Description Testing -ParentId 25 -AdditionalFields $additionalFields } | Should Throw - } - } - - Context 'Update-WorkItem' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $obj - } - - It 'Without Default Project should update work item' { - $Global:PSDefaultParameterValues.Remove("*:projectName") - Update-VSTeamWorkItem -Id 1 -Title Test -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Patch' -and - $Body -like '`[*' -and # Make sure the body is an array - $Body -like '*`]' -and # Make sure the body is an array - $ContentType -eq 'application/json-patch+json' -and - $Uri -eq "https://dev.azure.com/test/_apis/wit/workitems/1?api-version=$([VSTeamVersions]::Core)" - } - } - - It 'With Default Project should update work item' { - $Global:PSDefaultParameterValues["*:projectName"] = 'test' - Update-VSTeamWorkItem 1 -Title Test1 -Description Testing -Force - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Patch' -and - $Body -like '`[*' -and # Make sure the body is an array - $Body -like '*Test1*' -and - $Body -like '*Testing*' -and - $Body -like '*/fields/System.Title*' -and - $Body -like '*/fields/System.Description*' -and - $Body -like '*`]' -and # Make sure the body is an array - $ContentType -eq 'application/json-patch+json' -and - $Uri -eq "https://dev.azure.com/test/_apis/wit/workitems/1?api-version=$([VSTeamVersions]::Core)" - } - } - - It 'With Default Project should update work item with 2 parameters and additional properties' { - $Global:PSDefaultParameterValues["*:projectName"] = 'test' - - $additionalFields = @{"System.Tags" = "TestTag"; "System.AreaPath" = "Project\\MyPath" } - Update-VSTeamWorkItem 1 -Title Test1 -Description Testing -AdditionalFields $additionalFields - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Patch' -and - $Body -like '`[*' -and # Make sure the body is an array - $Body -like '*Test1*' -and - $Body -like '*Testing*' -and - $Body -like '*/fields/System.Title*' -and - $Body -like '*/fields/System.Description*' -and - $Body -like '*/fields/System.Tags*' -and - $Body -like '*/fields/System.AreaPath*' -and - $Body -like '*`]' -and # Make sure the body is an array - $ContentType -eq 'application/json-patch+json' -and - $Uri -eq "https://dev.azure.com/test/_apis/wit/workitems/1?api-version=$([VSTeamVersions]::Core)" - } - } - - It 'With Default Project should update work item only with 1 parameter and additional properties' { - $Global:PSDefaultParameterValues["*:projectName"] = 'test' - - $additionalFields = @{"System.Tags" = "TestTag"; "System.AreaPath" = "Project\\MyPath" } - Update-VSTeamWorkItem 1 -Title Test1 -AdditionalFields $additionalFields - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Patch' -and - $Body -like '`[*' -and # Make sure the body is an array - $Body -like '*Test1*' -and - $Body -like '*/fields/System.Title*' -and - $Body -like '*/fields/System.Tags*' -and - $Body -like '*/fields/System.AreaPath*' -and - $Body -like '*`]' -and # Make sure the body is an array - $ContentType -eq 'application/json-patch+json' -and - $Uri -eq "https://dev.azure.com/test/_apis/wit/workitems/1?api-version=$([VSTeamVersions]::Core)" - } - } - - It 'With Default Project should update work item only with additional properties' { - $Global:PSDefaultParameterValues["*:projectName"] = 'test' - - $additionalFields = @{"System.Tags" = "TestTag"; "System.AreaPath" = "Project\\MyPath" } - Update-VSTeamWorkItem 1 -AdditionalFields $additionalFields - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Method -eq 'Patch' -and - $Body -like '`[*' -and # Make sure the body is an array - $Body -like '*/fields/System.Tags*' -and - $Body -like '*/fields/System.AreaPath*' -and - $Body -like '*`]' -and # Make sure the body is an array - $ContentType -eq 'application/json-patch+json' -and - $Uri -eq "https://dev.azure.com/test/_apis/wit/workitems/1?api-version=$([VSTeamVersions]::Core)" - } - } - - It 'With Default Project should throw exception when adding existing parameters to additional properties' { - $Global:PSDefaultParameterValues["*:projectName"] = 'test' - - $additionalFields = @{"System.Title" = "Test1"; "System.AreaPath" = "Project\\TestPath" } - { Update-VSTeamWorkItem -ProjectName test -WorkItemType Task -Title Test1 -Description Testing -AdditionalFields $additionalFields } | Should Throw - } - } - - Context 'Show-VSTeamWorkItem' { - Mock Show-Browser { } - - it 'should return url for mine' { - Show-VSTeamWorkItem -projectName project -Id 15 - - Assert-MockCalled Show-Browser -Exactly -Scope It -Times 1 -ParameterFilter { $url -eq 'https://dev.azure.com/test/project/_workitems/edit/15' } - } - } - - Context 'Get-WorkItem' { - - It 'Without Default Project should add work item' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - - return $collection - } - - Get-VSTeamWorkItem -Id 47, 48 - - # With PowerShell core the order of the query string is not the - # same from run to run! So instead of testing the entire string - # matches I have to search for the portions I expect but can't - # assume the order. - # The general string should look like this: - # https://dev.azure.com/test/test/_apis/wit/workitems/?api-version=$([VSTeamVersions]::Core)&ids=47,48&`$Expand=None&errorPolicy=omit - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -like "*https://dev.azure.com/test/_apis/wit/workitems*" -and - $Uri -like "*api-version=$([VSTeamVersions]::Core)*" -and - $Uri -like "*ids=47,48*" -and - $Uri -like "*`$Expand=None*" -and - $Uri -like "*errorPolicy=omit*" - } - } - - It 'With Default Project should add work item' { - Mock Invoke-RestMethod { - # If this test fails uncomment the line below to see how the mock was called. - # Write-Host $args - return $obj - } - - Get-VSTeamWorkItem -Id 47 - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/_apis/wit/workitems/47?api-version=$([VSTeamVersions]::Core)&`$Expand=None" - } - } - } - } -} \ No newline at end of file diff --git a/unit/test/workitemTypes.Tests.ps1 b/unit/test/workitemTypes.Tests.ps1 deleted file mode 100644 index bb1b9c4eb..000000000 --- a/unit/test/workitemTypes.Tests.ps1 +++ /dev/null @@ -1,87 +0,0 @@ -Set-StrictMode -Version Latest - -# The InModuleScope command allows you to perform white-box unit testing on the -# internal (non-exported) code of a Script Module. -InModuleScope VSTeam { - Describe 'workitemTypes' { - # Set the account to use for testing. A normal user would do this - # using the Set-VSTeamAccount function. - Mock _getInstance { return 'https://dev.azure.com/test' } -Verifiable - - # Mock the call to Get-Projects by the dynamic parameter for ProjectName - Mock Invoke-RestMethod { return @() } -ParameterFilter { - $Uri -like "*_apis/projects*" - } - - # Load the mocks to create the project name dynamic parameter - . "$PSScriptRoot\mocks\mockProjectNameDynamicParam.ps1" - - Context 'Get-WorkItemTypes' { - $obj = @{ - count = 1 - value = @{ - name = "Test Suite" - referenceName = "Microsoft.VSTS.WorkItemTypes.TestSuite" - description = "Tracks test activites for a specific feature, requirement, or user story." - color = "004B50" - } - } - - Mock Invoke-RestMethod { - return ConvertTo-Json $obj - } - - It 'Should return all work item types' { - Get-VSTeamWorkItemType -ProjectName test - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 2 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitemtypes?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Get-WorkItemTypes By Type with Default Project' { - $obj = @{ - name = "Bug" - referenceName = "Microsoft.VSTS.WorkItemTypes.Bug" - description = "Describes a divergence between required and actual behavior, and tracks the work done to correct the defect and verify the correction." - color = "CC293D" - } - - Mock Invoke-RestMethod { - return ConvertTo-Json $obj - } - - It 'Should return 1 work item type' { - $Global:PSDefaultParameterValues["*:projectName"] = 'test' - Get-VSTeamWorkItemType -ProjectName test -WorkItemType bug - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitemtypes/bug?api-version=$([VSTeamVersions]::Core)" - } - } - } - - Context 'Get-WorkItemTypes By Type without Default Project' { - $obj = @{ - name = "Bug" - referenceName = "Microsoft.VSTS.WorkItemTypes.Bug" - description = "Describes a divergence between required and actual behavior, and tracks the work done to correct the defect and verify the correction." - color = "CC293D" - } - - Mock Invoke-RestMethod { - return ConvertTo-Json $obj - } - - It 'Should return 1 work item type' { - $Global:PSDefaultParameterValues.Remove("*:projectName") - Get-VSTeamWorkItemType -ProjectName test -WorkItemType bug - - Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { - $Uri -eq "https://dev.azure.com/test/test/_apis/wit/workitemtypes/bug?api-version=$([VSTeamVersions]::Core)" - } - } - } - } -} \ No newline at end of file