diff --git a/src/formats/GitHubContext.Format.ps1xml b/src/formats/GitHubContext.Format.ps1xml index 45756fbb5..6c80c2e1b 100644 --- a/src/formats/GitHubContext.Format.ps1xml +++ b/src/formats/GitHubContext.Format.ps1xml @@ -49,39 +49,55 @@ TokenType - - TokenExpiresAt - - if ($null -eq $_.TokenExpiresIn) { - return + if ($null -ne $_.TokenExpiresAt) { + return $_.TokenExpiresAt } - if ($_.TokenExpiresIn -le 0) { - $text = 'Expired' - } else { - $text = $_.TokenExpiresIn.ToString('hh\:mm\:ss') + $text = 'N/A' + if ($Host.UI.SupportsVirtualTerminal -and ($env:GITHUB_ACTIONS -ne 'true')) { + $gray = "`e[90m" + $reset = "`e[0m" + return "$gray$text$reset" } + return $text + + + + + if ($null -ne $_.TokenExpiresIn) { + if ($_.TokenExpiresIn -le 0) { + $text = 'Expired' + } else { + $text = $_.TokenExpiresIn.ToString('hh\:mm\:ss') + } - if ($Host.UI.SupportsVirtualTerminal -and - ($env:GITHUB_ACTIONS -ne 'true')) { - switch ($_.AuthType) { - 'UAT' { - $maxValue = [TimeSpan]::FromHours(8) - } - 'IAT' { - $maxValue = [TimeSpan]::FromHours(1) - } - 'APP' { - $maxValue = [TimeSpan]::FromMinutes(10) + if ($Host.UI.SupportsVirtualTerminal -and ($env:GITHUB_ACTIONS -ne 'true')) { + switch ($_.AuthType) { + 'UAT' { + $maxValue = [TimeSpan]::FromHours(8) + } + 'IAT' { + $maxValue = [TimeSpan]::FromHours(1) + } + 'APP' { + $maxValue = [TimeSpan]::FromMinutes(10) + } + } + $ratio = [Math]::Min(($_.TokenExpiresIn / $maxValue), 1) + return [GitHubFormatter]::FormatColorByRatio($ratio, $text) + } + return $text } + + $text = 'N/A' + if ($Host.UI.SupportsVirtualTerminal -and ($env:GITHUB_ACTIONS -ne 'true')) { + $gray = "`e[90m" + $reset = "`e[0m" + return "$gray$text$reset" } - $ratio = [Math]::Min(($_.TokenExpiresIn / $maxValue), 1) - [GitHubFormatter]::FormatColorByRatio($ratio, $text) - } else { - $text - } + return $text diff --git a/src/types/GitHubContext.Types.ps1xml b/src/types/GitHubContext.Types.ps1xml index d30c41a11..92f51f159 100644 --- a/src/types/GitHubContext.Types.ps1xml +++ b/src/types/GitHubContext.Types.ps1xml @@ -6,15 +6,17 @@ TokenExpiresIn - if ($null -eq $this.TokenExpiresAt) { return [TimeSpan]::Zero } - $this.TokenExpiresAt - [DateTime]::Now + if ($null -ne $this.TokenExpiresAt) { + return $this.TokenExpiresAt - [DateTime]::Now + } RefreshTokenExpiresIn - if ($null -eq $this.RefreshTokenExpiresAt) { return [TimeSpan]::Zero } - $this.RefreshTokenExpiresAt - [DateTime]::Now + if ($null -ne $this.RefreshTokenExpiresAt) { + return $this.RefreshTokenExpiresAt - [DateTime]::Now + } @@ -25,8 +27,9 @@ TokenExpiresIn - if ($null -eq $this.TokenExpiresAt) { return [TimeSpan]::Zero } - $this.TokenExpiresAt - [DateTime]::Now + if ($null -ne $this.TokenExpiresAt) { + return $this.TokenExpiresAt - [DateTime]::Now + } @@ -37,8 +40,9 @@ TokenExpiresIn - if ($null -eq $this.TokenExpiresAt) { return [TimeSpan]::Zero } - $this.TokenExpiresAt - [DateTime]::Now + if ($null -ne $this.TokenExpiresAt) { + return $this.TokenExpiresAt - [DateTime]::Now + } diff --git a/tests/Apps.Tests.ps1 b/tests/Apps.Tests.ps1 index 9e5697196..e9979095b 100644 --- a/tests/Apps.Tests.ps1 +++ b/tests/Apps.Tests.ps1 @@ -230,7 +230,7 @@ Describe 'Apps' { LogGroup 'Config' { Write-Host "$($config | Format-List | Out-String)" } - LogGroup "Installation" { + LogGroup 'Installation' { Write-Host "$($installation | Format-List | Out-String)" } LogGroup 'Permissions' { @@ -274,8 +274,11 @@ Describe 'Apps' { $installationContext.Events | Should -BeOfType 'string' } - It 'Connect-GitHubApp - TokenExpiresIn property should be calculated correctly' { + It 'Connect-GitHubApp - TokenExpiresAt and TokenExpiresIn properties should be calculated correctly' { + $installationContext.TokenExpiresAt | Should -BeOfType [DateTime] + $installationContext.TokenExpiresAt | Should -BeGreaterThan ([DateTime]::Now) $installationContext.TokenExpiresIn | Should -BeOfType [TimeSpan] + $installationContext.TokenExpiresIn.TotalSeconds | Should -BeGreaterThan 0 $installationContext.TokenExpiresIn.TotalMinutes | Should -BeGreaterThan 0 $installationContext.TokenExpiresIn.TotalMinutes | Should -BeLessOrEqual 60 } diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index 9a7c69a17..177d53235 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -23,13 +23,19 @@ Describe 'Auth' { $authCases = . "$PSScriptRoot/Data/AuthCases.ps1" Context 'As using on ' -ForEach $authCases { + BeforeAll { + $context = Connect-GitHubAccount @connectParams -PassThru -Silent + LogGroup 'Context' { + Write-Host ($context | Format-List | Out-String) + } + } + AfterAll { Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent Write-Host ('-' * 60) } It 'Connect-GitHubAccount - Connects using the provided credentials' { - $context = Connect-GitHubAccount @connectParams -PassThru -Silent LogGroup 'Context - Standard' { Write-Host ($context | Out-String) } @@ -45,6 +51,22 @@ Describe 'Auth' { $context | Should -Not -BeNullOrEmpty } + It 'Connect-GitHubAccount - TokenExpiresAt and TokenExpiresIn validation' { + if ($AuthType -eq 'APP') { + $context.TokenExpiresAt | Should -Not -BeNullOrEmpty + $context.TokenExpiresAt | Should -BeOfType [DateTime] + $context.TokenExpiresAt | Should -BeGreaterThan ([DateTime]::Now) + + $context.TokenExpiresIn | Should -Not -BeNullOrEmpty + $context.TokenExpiresIn | Should -BeOfType [TimeSpan] + $context.TokenExpiresIn.TotalMinutes | Should -BeGreaterThan 0 + $context.TokenExpiresIn.TotalMinutes | Should -BeLessOrEqual 10 + } else { + $context.TokenExpiresAt | Should -BeNullOrEmpty + $context.TokenExpiresIn | Should -BeNullOrEmpty + } + } + It 'Connect-GitHubAccount - Connects using the provided credentials - Double' { $context = Connect-GitHubAccount @connectParams -PassThru -Silent $context = Connect-GitHubAccount @connectParams -PassThru -Silent @@ -80,8 +102,6 @@ Describe 'Auth' { } $context | Should -Not -BeNullOrEmpty $context | Should -BeOfType [GitHubContext] - $context.TokenExpiresAt | Should -BeOfType [DateTime] - $context.TokenExpiresIn | Should -BeOfType [TimeSpan] } It 'Connect-GitHubApp - Connects as a GitHub App to ' -Skip:($AuthType -ne 'APP') { @@ -109,18 +129,18 @@ Describe 'Auth' { LogGroup 'Connect-GithubApp' { $context } - $context.TokenExpiresAt | Should -BeOfType [DateTime] - $context.TokenExpiresIn | Should -BeOfType [TimeSpan] LogGroup 'Context' { Write-Host ($context | Format-List | Out-String) } $context | Should -Not -BeNullOrEmpty } - # Tests for runners goes here - if ($Type -eq 'GitHub Actions') {} + It 'Connect-GitHubApp - Installation tokens (IAT) should have correct auth type' -Skip:($AuthType -ne 'APP') { + $appContextToUse = Get-GitHubContext -ListAvailable | Where-Object { $_.AuthType -eq 'App' } | Select-Object -First 1 + $appContext = Connect-GitHubApp @connectAppParams -PassThru -Silent -Context $appContextToUse + $appContext.AuthType | Should -Be 'IAT' + } - # Tests for IAT UAT and PAT goes here It 'Connect-GitHubAccount - Connects to GitHub CLI on runners' { [string]::IsNullOrEmpty($(gh auth token)) | Should -Be $false }