Repository navigation
Expand file tree
/
Copy pathGitHubEvents.tests.ps1
More file actions
160 lines (126 loc) · 6.48 KB
/
Copy pathGitHubEvents.tests.ps1
File metadata and controls
160 lines (126 loc) · 6.48 KB
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
<#
.Synopsis
Tests for GitHubEvents.ps1 module
#>
[String] $root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
. (Join-Path -Path $root -ChildPath 'Tests\Config\Settings.ps1')
Import-Module -Name $root -Force
function Initialize-AppVeyor
{
<#
.SYNOPSIS
Configures the tests to run with the authentication information stored in AppVeyor
(if that information exists in the environment).
.DESCRIPTION
Configures the tests to run with the authentication information stored in AppVeyor
(if that information exists in the environment).
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.NOTES
Internal-only helper method.
The only reason this exists is so that we can leverage CodeAnalysis.SuppressMessageAttribute,
which can only be applied to functions.
We call this immediately after the declaration so that AppVeyor initialization can heppen
(if applicable).
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Justification="Needed to configure with the stored, encrypted string value in AppVeyor.")]
param()
if ($env:AppVeyor)
{
$secureString = $env:avAccessToken | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential "<username is ignored>", $secureString
Set-GitHubAuthentication -Credential $cred
$script:ownerName = $env:avOwnerName
$script:organizationName = $env:avOrganizationName
$message = @(
'This run is executed in the AppVeyor environment.',
'The GitHub Api Token won''t be decrypted in PR runs causing some tests to fail.',
'403 errors possible due to GitHub hourly limit for unauthenticated queries.',
'Use Set-GitHubAuthentication manually. modify the values in Tests\Config\Settings.ps1,',
'and run tests on your machine first.')
Write-Warning -Message ($message -join [Environment]::NewLine)
}
}
Initialize-AppVeyor
$accessTokenConfigured = Test-GitHubAuthenticationConfigured
if (-not $accessTokenConfigured)
{
$message = @(
'GitHub API Token not defined, some of the tests will be skipped.',
'403 errors possible due to GitHub hourly limit for unauthenticated queries.')
Write-Warning -Message ($message -join [Environment]::NewLine)
}
# Backup the user's configuration before we begin, and ensure we're at a pure state before running
# the tests. We'll restore it at the end.
$configFile = New-TemporaryFile
try
{
Backup-GitHubConfiguration -Path $configFile
Reset-GitHubConfiguration
Set-GitHubConfiguration -DisableTelemetry # We don't want UT's to impact telemetry
Set-GitHubConfiguration -LogRequestBody # Make it easier to debug UT failures
if ($accessTokenConfigured)
{
Describe 'Getting events from repository' {
$repositoryName = [Guid]::NewGuid()
$null = New-GitHubRepository -RepositoryName $repositoryName
Context 'For getting events from a new repository' {
$events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName)
It 'Should have no events' {
$events.Count | Should be 0
}
}
$issue = New-GithubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Title "New Issue"
Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State Closed
Context 'For getting events from a repository' {
$events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName)
It 'Should have an event from closing an issue' {
$events.Count | Should be 1
}
}
$null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName
}
Describe 'Getting events from an issue' {
$repositoryName = [Guid]::NewGuid()
$null = New-GitHubRepository -RepositoryName $repositoryName
$issue = New-GithubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Title "New Issue"
Context 'For getting events from a new issue' {
$events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number)
It 'Should have no events' {
$events.Count | Should be 0
}
}
Context 'For getting events from an issue' {
Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State Closed
Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State Open
$events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName)
It 'Should have two events from closing and opening the issue' {
$events.Count | Should be 2
}
}
$null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName
}
Describe 'Getting an event directly' {
$repositoryName = [Guid]::NewGuid()
$null = New-GitHubRepository -RepositoryName $repositoryName
$issue = New-GithubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Title "New Issue"
Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State Closed
Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State Open
$events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName)
Context 'For getting an event directly'{
$singleEvent = Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName -EventID $events[0].id
It 'Should have the correct event type'{
$singleEvent.event | Should be 'reopened'
}
}
$null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName
}
}
}
catch
{
# Restore the user's configuration to its pre-test state
Restore-GitHubConfiguration -Path $configFile
}