forked from microsoft/PowerShellForGitHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitHubDeployments.tests.ps1
217 lines (185 loc) · 9.19 KB
/
GitHubDeployments.tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
<#
.Synopsis
Tests for GitHubDeployments.ps1 module
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '',
Justification = 'Suppress false positives in Pester code blocks')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingCmdletAliases', '',
Justification = 'Using Set-GitHubDeploymentEnvironment the way a user would.')]
param()
Set-StrictMode -Version 1.0
# This is common test code setup logic for all Pester test files
BeforeAll {
$moduleRootPath = Split-Path -Path $PSScriptRoot -Parent
. (Join-Path -Path $moduleRootPath -ChildPath 'Tests\Common.ps1')
$repoName = ([Guid]::NewGuid().Guid)
$newGitHubRepositoryParms = @{
RepositoryName = $repoName
OrganizationName = $script:organizationName
Private = $false
}
$repo = New-GitHubRepository @newGitHubRepositoryParms
$team1Name = [Guid]::NewGuid().Guid
$team2Name = [Guid]::NewGuid().Guid
$description = 'Team Description'
$privacy = 'closed'
$maintainerName = $script:ownerName
$newGithubTeamParms = @{
OrganizationName = $script:organizationName
Description = $description
Privacy = $privacy
MaintainerName = $MaintainerName
}
$reviewerTeam1 = New-GitHubTeam @newGithubTeamParms -TeamName $team1Name
$reviewerTeam2 = New-GitHubTeam @newGithubTeamParms -TeamName $team2Name
$reviewerTeamId = $reviewerTeam1.TeamId, $reviewerTeam2.TeamId
$reviewerUser = Get-GitHubUser -UserName $script:ownerName
$repo | Set-GitHubRepositoryTeamPermission -TeamSlug $reviewerTeam1.TeamSlug -Permission Push
$repo | Set-GitHubRepositoryTeamPermission -TeamSlug $reviewerTeam2.TeamSlug -Permission Push
}
Describe 'GitHubDeployments\New-GitHubDeploymentEnvironment' {
Context -Name 'When creating a new deployment environment' -Fixture {
BeforeAll -ScriptBlock {
$environmentName = [Guid]::NewGuid().Guid
$waitTimer = 50
$deploymentBranchPolicy = 'ProtectedBranches'
$newGitHubDeploymentEnvironmentParms = @{
EnvironmentName = $environmentName
WaitTimer = $waitTimer
DeploymentBranchPolicy = $deploymentBranchPolicy
ReviewerTeamId = $reviewerTeamId
ReviewerUserId = $reviewerUser.UserId
}
$environment = $repo | New-GitHubDeploymentEnvironment @newGitHubDeploymentEnvironmentParms
}
It 'Should return an object of the correct type' {
$environment.PSObject.TypeNames[0] | Should -Be 'GitHub.DeploymentEnvironment'
}
It 'Should return the correct properties' {
$environment.name | Should -Be $environmentName
$environment.RepositoryUrl | Should -Be $repo.RepositoryUrl
$environment.EnvironmentName | Should -Be $environmentName
$environment.ReviewerUser[0].UserName | Should -Be $reviewerUser.UserName
$environment.ReviewerUser[0].UserId | Should -Be $reviewerUser.UserId
$environment.ReviewerTeam.count | Should -Be $reviewerTeamId.count
$environment.ReviewerTeam[0].TeamName | Should -Be $reviewerTeam1.TeamName
$environment.ReviewerTeam[0].TeamId | Should -Be $reviewerTeam1.TeamId
$environment.ReviewerTeam[1].TeamName | Should -Be $reviewerTeam2.TeamName
$environment.ReviewerTeam[1].TeamId | Should -Be $reviewerTeam2.TeamId
$environment.WaitTimer | Should -Be $waitTimer
$environment.DeploymentBranchPolicy | Should -Be $deploymentBranchPolicy
}
}
}
Describe 'GitHubDeployments\Set-GitHubDeploymentEnvironment' {
Context -Name 'When updating a deployment environment' -Fixture {
BeforeAll -ScriptBlock {
$environmentName = [Guid]::NewGuid().Guid
$waitTimer = 50
$deploymentBranchPolicy = 'ProtectedBranches'
$environment = $repo | New-GitHubDeploymentEnvironment -EnvironmentName $environmentName
$setGitHubDeploymentEnvironmentParms = @{
EnvironmentName = $environmentName
WaitTimer = $waitTimer
DeploymentBranchPolicy = $deploymentBranchPolicy
ReviewerTeamId = $reviewerTeam1.TeamId, $reviewerTeam2.TeamId
ReviewerUserId = $reviewerUser.UserId
PassThru = $true
}
$updatedEnvironment = $environment | Set-GitHubDeploymentEnvironment @setGitHubDeploymentEnvironmentParms
}
It 'Should return an object of the correct type' {
$updatedEnvironment.PSObject.TypeNames[0] | Should -Be 'GitHub.DeploymentEnvironment'
}
It 'Should return the correct properties' {
$updatedEnvironment.name | Should -Be $environmentName
$updatedEnvironment.RepositoryUrl | Should -Be $repo.RepositoryUrl
$updatedEnvironment.EnvironmentName | Should -Be $environmentName
$updatedenvironment.ReviewerUser[0].UserName | Should -Be $reviewerUser.UserName
$updatedenvironment.ReviewerUser[0].UserId | Should -Be $reviewerUser.UserId
$updatedenvironment.ReviewerTeam.count | Should -Be $reviewerTeamId.count
$updatedenvironment.ReviewerTeam[0].TeamName | Should -Be $reviewerTeam1.TeamName
$updatedenvironment.ReviewerTeam[0].TeamId | Should -Be $reviewerTeam1.TeamId
$updatedenvironment.ReviewerTeam[1].TeamName | Should -Be $reviewerTeam2.TeamName
$updatedenvironment.ReviewerTeam[1].TeamId | Should -Be $reviewerTeam2.TeamId
$updatedenvironment.WaitTimer | Should -Be $waitTimer
$updatedenvironment.DeploymentBranchPolicy | Should -Be $deploymentBranchPolicy
}
}
}
Describe 'GitHubDeployments\Get-GitHubDeploymentEnvironment' {
Context -Name 'When getting a deployment environment' -Fixture {
BeforeAll -ScriptBlock {
$environmentName = [Guid]::NewGuid().Guid
$waitTimer = 50
$deploymentBranchPolicy = 'ProtectedBranches'
$newGitHubDeploymentEnvironmentParms = @{
EnvironmentName = $environmentName
WaitTimer = $waitTimer
DeploymentBranchPolicy = $deploymentBranchPolicy
ReviewerTeamId = $reviewerTeamId
ReviewerUserId = $reviewerUser.UserId
}
$repo | New-GitHubDeploymentEnvironment @newGitHubDeploymentEnvironmentParms | Out-Null
$environment = $repo | Get-GitHubDeploymentEnvironment -EnvironmentName $environmentName
}
It 'Should return an object of the correct type' {
$environment.PSObject.TypeNames[0] | Should -Be 'GitHub.DeploymentEnvironment'
}
It 'Should return the correct properties' {
$environment.name | Should -Be $environmentName
$environment.RepositoryUrl | Should -Be $repo.RepositoryUrl
$environment.EnvironmentName | Should -Be $environmentName
$environment.ReviewerUser[0].UserName | Should -Be $reviewerUser.UserName
$environment.ReviewerUser[0].UserId | Should -Be $reviewerUser.UserId
$environment.ReviewerTeam.count | Should -Be $reviewerTeamId.count
$environment.ReviewerTeam[0].TeamName | Should -Be $reviewerTeam1.TeamName
$environment.ReviewerTeam[0].TeamId | Should -Be $reviewerTeam1.TeamId
$environment.ReviewerTeam[1].TeamName | Should -Be $reviewerTeam2.TeamName
$environment.ReviewerTeam[1].TeamId | Should -Be $reviewerTeam2.TeamId
$environment.WaitTimer | Should -Be $waitTimer
$environment.DeploymentBranchPolicy | Should -Be $deploymentBranchPolicy
}
}
}
Describe 'GitHubDeployments\Remove-GitHubDeploymentEnvironment' {
Context -Name 'When removing a deployment environment' -Fixture {
BeforeAll -ScriptBlock {
$environmentName = [Guid]::NewGuid().Guid
$waitTimer = 50
$deploymentBranchPolicy = 'ProtectedBranches'
$newGitHubDeploymentEnvironmentParms = @{
EnvironmentName = $environmentName
WaitTimer = $waitTimer
DeploymentBranchPolicy = $deploymentBranchPolicy
ReviewerTeamId = $reviewerTeam1.id
ReviewerUserId = $reviewerUser.UserId
}
$environment = $repo | New-GitHubDeploymentEnvironment @newGitHubDeploymentEnvironmentParms
}
It 'Should not throw an exception' {
{ $environment | Remove-GitHubDeploymentEnvironment -Confirm:$false } | Should -Not -Throw
}
It 'Should have removed the deployment environment' {
{ $repo | Get-GitHubDeploymentEnvironment -EnvironmentName $environmentName } | `
Should -Throw '*Not Found*'
}
}
}
AfterAll -ScriptBlock {
if ($repo)
{
$repo | Remove-GitHubRepository -Confirm:$false
}
if ($reviewerTeam1)
{
$reviewerTeam1 | Remove-GitHubTeam -Confirm:$false
}
if ($reviewerTeam2)
{
$reviewerTeam2 | Remove-GitHubTeam -Confirm:$false
}
}