Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🩹 [Patch]: Add tests for Organizations #243

Merged
merged 9 commits into from
Dec 28, 2024
6 changes: 3 additions & 3 deletions .github/workflows/Process-PSModule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ jobs:
TEST_USER_ORG_FG_PAT: ${{ secrets.TEST_USER_ORG_FG_PAT }}
TEST_USER_USER_FG_PAT: ${{ secrets.TEST_USER_USER_FG_PAT }}
TEST_USER_PAT: ${{ secrets.TEST_USER_PAT }}
with:
Debug: true
Verbose: true
# with:
# Debug: true
# Verbose: true
10 changes: 7 additions & 3 deletions src/functions/public/Auth/Connect-GitHubApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
Connects to GitHub as the user 'octocat' using the logged in GitHub App.

.EXAMPLE
Connect-GitHubApp -Organization 'psmodule'
Connect-GitHubApp -Organization 'psmodule' -Default

Connects to GitHub as the organization 'psmodule' using the logged in GitHub App.
Connects to GitHub as the organization 'psmodule' using the logged in GitHub App and sets it as the default context.

.EXAMPLE
Connect-GitHubApp -Enterprise 'msx'
Expand Down Expand Up @@ -65,6 +65,10 @@
[Parameter()]
[switch] $PassThru,

# Set as the default context.
[Parameter()]
[switch] $Default,

# The context to run the command in. Used to get the details for the API call.
# Can be either a string or a GitHubContext object.
[Parameter()]
Expand Down Expand Up @@ -133,7 +137,7 @@
}
Write-Verbose 'Logging in using a managed installation access token...'
Write-Verbose ($contextParams | Format-Table | Out-String)
$contextObj = [InstallationGitHubContext]::new((Set-GitHubContext -Context $contextParams.Clone() -PassThru))
$contextObj = [InstallationGitHubContext]::new((Set-GitHubContext -Context $contextParams.Clone() -PassThru -Default:$Default))
Write-Verbose ($contextObj | Format-List | Out-String)
if (-not $Silent) {
$name = $contextObj.name
Expand Down
35 changes: 20 additions & 15 deletions src/functions/public/Auth/Disconnect-GitHubAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,36 @@
# The context to run the command with.
# Can be either a string or a GitHubContext object.
[Parameter(ValueFromPipeline)]
[object] $Context = (Get-GitHubContext)
[object[]] $Context
)

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
$Context = Resolve-GitHubContext -Context $Context
}

process {
try {
Remove-GitHubContext -Context $Context
$isDefaultContext = $Context.Name -eq $script:GitHub.Config.DefaultContext
if ($isDefaultContext) {
Remove-GitHubConfig -Name 'DefaultContext'
Write-Warning 'There is no longer a default context!'
Write-Warning "Please set a new default context using 'Set-GitHubDefaultContext -Name <context>'"
}
if (-not $Context) {
$Context = Get-GitHubContext
}
foreach ($contextItem in $Context) {
$contextItem = Resolve-GitHubContext -Context $contextItem
try {
Remove-GitHubContext -Context $contextItem
$isDefaultContext = $contextItem.Name -eq $script:GitHub.Config.DefaultContext
if ($isDefaultContext) {
Remove-GitHubConfig -Name 'DefaultContext'
Write-Warning 'There is no longer a default context!'
Write-Warning "Please set a new default context using 'Set-GitHubDefaultContext -Name <context>'"
}

if (-not $Silent) {
Write-Host '✓ ' -ForegroundColor Green -NoNewline
Write-Host "Logged out of GitHub! [$Context]"
if (-not $Silent) {
Write-Host '✓ ' -ForegroundColor Green -NoNewline
Write-Host "Logged out of GitHub! [$contextItem]"
}
} catch {
throw $_
}
} catch {
throw $_
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
.NOTES
[List organizations](https://docs.github.com/rest/orgs/orgs)
#>
#SkipTest:FunctionTest:Will add a test for this function in a future PR
[OutputType([pscustomobject])]
[CmdletBinding(DefaultParameterSetName = '__DefaultSet')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', 'All', Justification = 'Required for parameter set')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
.NOTES
[List organization members](https://docs.github.com/en/rest/orgs/members?apiVersion=2022-11-28#list-organization-members)
#>
#SkipTest:FunctionTest:Will add a test for this function in a future PR
[OutputType([pscustomobject])]
[CmdletBinding()]
param(
Expand Down
72 changes: 66 additions & 6 deletions tests/GitHub.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Describe 'GitHub' {
{ Connect-GitHubAccount @params -AutoloadInstallations } | Should -Not -Throw
$contexts = Get-GitHubContextInfo -Verbose:$false
Write-Verbose ($contexts | Out-String) -Verbose
($contexts).Count | Should -Be 6
($contexts).Count | Should -Be 7
}
It 'Connect-GitHubAccount - Connects a GitHub App from an enterprise' {
$params = @{
Expand All @@ -98,7 +98,7 @@ Describe 'GitHub' {
{ Connect-GitHubAccount @params } | Should -Not -Throw
$contexts = Get-GitHubContextInfo -Verbose:$false
Write-Verbose ($contexts | Out-String) -Verbose
($contexts).Count | Should -Be 7
($contexts).Count | Should -Be 8
}
It 'Connect-GitHubAccount - Connects all of a (ent) GitHub Apps installations' {
$params = @{
Expand All @@ -108,13 +108,13 @@ Describe 'GitHub' {
{ Connect-GitHubAccount @params -AutoloadInstallations } | Should -Not -Throw
$contexts = Get-GitHubContextInfo -Verbose:$false
Write-Verbose ($contexts | Out-String) -Verbose
($contexts).Count | Should -Be 10
($contexts).Count | Should -Be 12
}
It 'Disconnect-GitHubAccount - Disconnects a specific context' {
{ Disconnect-GitHubAccount -Context 'github.com/psmodule-enterprise-app/Organization/PSModule' -Silent } | Should -Not -Throw
$contexts = Get-GitHubContextInfo -Name 'github.com/psmodule-enterprise-app/*' -Verbose:$false
Write-Verbose ($contexts | Out-String) -Verbose
($contexts).Count | Should -Be 2
($contexts).Count | Should -Be 3
}
It 'Set-GitHubDefaultContext - Can swap context to another' {
{ Set-GitHubDefaultContext -Context 'github.com/github-actions/Organization/PSModule' } | Should -Not -Throw
Expand Down Expand Up @@ -519,6 +519,21 @@ Describe 'As a user - Fine-grained PAT token - organization account access (ORG_
{ Get-GitHubUser -Username 'Octocat' } | Should -Not -Throw
}
}
Context 'Organization' {
It 'Get-GitHubOrganization - Gets the organizations for the authenticated user (ORG_FG_PAT)' {
{ Get-GitHubOrganization } | Should -Not -Throw
}
It 'Get-GitHubOrganization - Gets a specific organization (ORG_FG_PAT)' {
{ Get-GitHubOrganization -Organization 'psmodule-test-org2' } | Should -Not -Throw
}
It "Get-GitHubOrganization - List public organizations for the user 'psmodule-user'. (ORG_FG_PAT)" {
{ Get-GitHubOrganization -Username 'psmodule-user' } | Should -Not -Throw
}
It 'Get-GitHubOrganizationMember - Gets the members of a specific organization (ORG_FG_PAT)' {
$members = Get-GitHubOrganizationMember -Organization 'psmodule-test-org2'
$members.login | Should -Contain 'psmodule-user'
}
}
}

Describe 'As a user - Classic PAT token (PAT)' {
Expand Down Expand Up @@ -656,6 +671,21 @@ Describe 'As a user - Classic PAT token (PAT)' {
}
}
}
Context 'Organization' {
It 'Get-GitHubOrganization - Gets the organizations for the authenticated user (PAT)' {
{ Get-GitHubOrganization } | Should -Not -Throw
}
It 'Get-GitHubOrganization - Gets a specific organization (PAT)' {
{ Get-GitHubOrganization -Organization 'psmodule-test-org2' } | Should -Not -Throw
}
It "Get-GitHubOrganization - List public organizations for the user 'psmodule-user'. (PAT)" {
{ Get-GitHubOrganization -Username 'psmodule-user' } | Should -Not -Throw
}
It 'Get-GitHubOrganizationMember - Gets the members of a specific organization (PAT)' {
$members = Get-GitHubOrganizationMember -Organization 'psmodule-test-org2'
$members.login | Should -Contain 'psmodule-user'
}
}
}

Describe 'As GitHub Actions (GHA)' {
Expand Down Expand Up @@ -794,7 +824,7 @@ Describe 'As a GitHub App - Enterprise (APP_ENT)' {
}
It 'Connect-GitHubApp - Connects all installations for the authenticated GitHub App (APP_ENT)' {
{ Connect-GitHubApp } | Should -Not -Throw
Get-GitHubContext -ListAvailable | Should -HaveCount 4
Get-GitHubContext -ListAvailable | Should -HaveCount 5
}
}
Context 'App' {
Expand All @@ -804,6 +834,21 @@ Describe 'As a GitHub App - Enterprise (APP_ENT)' {
$app | Should -Not -BeNullOrEmpty
}
}
Context 'Organization' {
BeforeAll {
Connect-GitHubApp -Organization 'psmodule-test-org3' -Default
}
AfterAll {
Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount
}
It 'Get-GitHubOrganization - Gets a specific organization (APP_ENT)' {
{ Get-GitHubOrganization -Organization 'psmodule-test-org3' } | Should -Not -Throw
}
It 'Get-GitHubOrganizationMember - Gets the members of a specific organization (APP_ENT)' {
$members = Get-GitHubOrganizationMember -Organization 'psmodule-test-org3'
$members.login | Should -Contain 'MariusStorhaug'
}
}
}

Describe 'As a GitHub App - Organization (APP_ORG)' {
Expand All @@ -826,7 +871,7 @@ Describe 'As a GitHub App - Organization (APP_ORG)' {
}
It 'Connect-GitHubApp - Connects all installations for the authenticated GitHub App (APP_ORG)' {
{ Connect-GitHubApp } | Should -Not -Throw
Get-GitHubContext -ListAvailable | Should -HaveCount 4
Get-GitHubContext -ListAvailable | Should -HaveCount 5
}
}
Context 'Apps' {
Expand Down Expand Up @@ -884,4 +929,19 @@ Describe 'As a GitHub App - Organization (APP_ORG)' {
} | Should -Not -Throw
}
}
Context 'Organization' {
BeforeAll {
Connect-GitHubApp -Organization 'psmodule-test-org' -Default
}
AfterAll {
Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount
}
It 'Get-GitHubOrganization - Gets a specific organization (APP_ORG)' {
{ Get-GitHubOrganization -Organization 'psmodule-test-org' } | Should -Not -Throw
}
It 'Get-GitHubOrganizationMember - Gets the members of a specific organization (APP_ORG)' {
$members = Get-GitHubOrganizationMember -Organization 'psmodule-test-org'
$members.login | Should -Contain 'MariusStorhaug'
}
}
}
Loading