|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +<# |
| 5 | +.Synopsis |
| 6 | + Tests for GitHubEvents.ps1 module |
| 7 | +#> |
| 8 | + |
| 9 | +[String] $root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) |
| 10 | +. (Join-Path -Path $root -ChildPath 'Tests\Config\Settings.ps1') |
| 11 | +Import-Module -Name $root -Force |
| 12 | + |
| 13 | +function Initialize-AppVeyor |
| 14 | +{ |
| 15 | +<# |
| 16 | + .SYNOPSIS |
| 17 | + Configures the tests to run with the authentication information stored in AppVeyor |
| 18 | + (if that information exists in the environment). |
| 19 | +
|
| 20 | + .DESCRIPTION |
| 21 | + Configures the tests to run with the authentication information stored in AppVeyor |
| 22 | + (if that information exists in the environment). |
| 23 | +
|
| 24 | + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub |
| 25 | +
|
| 26 | + .NOTES |
| 27 | + Internal-only helper method. |
| 28 | +
|
| 29 | + The only reason this exists is so that we can leverage CodeAnalysis.SuppressMessageAttribute, |
| 30 | + which can only be applied to functions. |
| 31 | +
|
| 32 | + We call this immediately after the declaration so that AppVeyor initialization can heppen |
| 33 | + (if applicable). |
| 34 | +
|
| 35 | +#> |
| 36 | + [CmdletBinding()] |
| 37 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Justification="Needed to configure with the stored, encrypted string value in AppVeyor.")] |
| 38 | + param() |
| 39 | + |
| 40 | + if ($env:AppVeyor) |
| 41 | + { |
| 42 | + $secureString = $env:avAccessToken | ConvertTo-SecureString -AsPlainText -Force |
| 43 | + $cred = New-Object System.Management.Automation.PSCredential "<username is ignored>", $secureString |
| 44 | + Set-GitHubAuthentication -Credential $cred |
| 45 | + |
| 46 | + $script:ownerName = $env:avOwnerName |
| 47 | + $script:organizationName = $env:avOrganizationName |
| 48 | + |
| 49 | + $message = @( |
| 50 | + 'This run is executed in the AppVeyor environment.', |
| 51 | + 'The GitHub Api Token won''t be decrypted in PR runs causing some tests to fail.', |
| 52 | + '403 errors possible due to GitHub hourly limit for unauthenticated queries.', |
| 53 | + 'Use Set-GitHubAuthentication manually. modify the values in Tests\Config\Settings.ps1,', |
| 54 | + 'and run tests on your machine first.') |
| 55 | + Write-Warning -Message ($message -join [Environment]::NewLine) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +Initialize-AppVeyor |
| 60 | + |
| 61 | +$accessTokenConfigured = Test-GitHubAuthenticationConfigured |
| 62 | +if (-not $accessTokenConfigured) |
| 63 | +{ |
| 64 | + $message = @( |
| 65 | + 'GitHub API Token not defined, some of the tests will be skipped.', |
| 66 | + '403 errors possible due to GitHub hourly limit for unauthenticated queries.') |
| 67 | + Write-Warning -Message ($message -join [Environment]::NewLine) |
| 68 | +} |
| 69 | + |
| 70 | +# Backup the user's configuration before we begin, and ensure we're at a pure state before running |
| 71 | +# the tests. We'll restore it at the end. |
| 72 | +$configFile = New-TemporaryFile |
| 73 | + |
| 74 | +try |
| 75 | +{ |
| 76 | + Backup-GitHubConfiguration -Path $configFile |
| 77 | + Reset-GitHubConfiguration |
| 78 | + |
| 79 | + if ($accessTokenConfigured) |
| 80 | + { |
| 81 | + Describe 'Getting events from repository' { |
| 82 | + $repositoryName = [Guid]::NewGuid() |
| 83 | + $null = New-GitHubRepository -RepositoryName $repositoryName |
| 84 | + |
| 85 | + Context 'For getting events from a new repository' { |
| 86 | + $events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName) |
| 87 | + |
| 88 | + It 'Should have no events' { |
| 89 | + $events.Count | Should be 0 |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + $issue = New-GithubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Title "New Issue" |
| 94 | + Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State closed |
| 95 | + |
| 96 | + Context 'For getting events from a repository' { |
| 97 | + $events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName) |
| 98 | + |
| 99 | + It 'Should have an event from closing an issue' { |
| 100 | + $events.Count | Should be 1 |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName |
| 105 | + } |
| 106 | + |
| 107 | + Describe 'Getting events from an issue' { |
| 108 | + $repositoryName = [Guid]::NewGuid() |
| 109 | + $null = New-GitHubRepository -RepositoryName $repositoryName |
| 110 | + $issue = New-GithubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Title "New Issue" |
| 111 | + |
| 112 | + Context 'For getting events from a new issue' { |
| 113 | + $events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number) |
| 114 | + |
| 115 | + It 'Should have no events' { |
| 116 | + $events.Count | Should be 0 |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + Context 'For getting events from an issue' { |
| 121 | + Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State closed |
| 122 | + Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State open |
| 123 | + $events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName) |
| 124 | + |
| 125 | + It 'Should have two events from closing and opening the issue' { |
| 126 | + $events.Count | Should be 2 |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName |
| 131 | + } |
| 132 | + |
| 133 | + Describe 'Getting an event directly' { |
| 134 | + $repositoryName = [Guid]::NewGuid() |
| 135 | + $null = New-GitHubRepository -RepositoryName $repositoryName |
| 136 | + $issue = New-GithubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Title "New Issue" |
| 137 | + Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State closed |
| 138 | + Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -State open |
| 139 | + $events = @(Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName) |
| 140 | + |
| 141 | + Context 'For getting an event directly'{ |
| 142 | + $singleEvent = Get-GitHubEvent -OwnerName $ownerName -RepositoryName $repositoryName -EventID $events[0].id |
| 143 | + |
| 144 | + It 'Should have the correct event type'{ |
| 145 | + $singleEvent.event | Should be 'reopened' |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | +catch |
| 154 | +{ |
| 155 | + # Restore the user's configuration to its pre-test state |
| 156 | + Restore-GitHubConfiguration -Path $configFile |
| 157 | +} |
| 158 | + |
0 commit comments