Skip to content

Commit

Permalink
Issue okta#61 - Correct the UnitTests to match the public API
Browse files Browse the repository at this point in the history
  • Loading branch information
TriggerAu committed Nov 1, 2024
1 parent 66d2c05 commit 327d7a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
14 changes: 9 additions & 5 deletions tests/Internal/OktaApiClient.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ Context 'Invoke-OktaApiClient - 429 Responses' {
$Config.RequestTimeout = $null
$Now = Get-Date # Used as a reference for the test. Indicates when the request was executed
$ResetDate = $Now.AddSeconds(3) # Indicates when one should retry

$ResetDateEpoch = $ResetDate.ToUniversalTime().Subtract((New-Object DateTime(1970, 1, 1, 0, 0, 0, 0))).TotalSeconds

Mock -ModuleName Okta.PowerShell Get-OktaConfiguration { return $Config } -Verifiable

$Response = [PSCustomObject]@{
Content = "[]"
Headers = @{
"Date" = @($Now)
"Content-Type" = @('application/json; charset=utf-8')
"X-Rate-Limit-Reset" = @($ResetDate)
"x-rate-limit-reset" = @($ResetDateEpoch)
"X-Okta-Request-Id" = @("foo")
}
StatusCode = 429
Expand All @@ -33,6 +34,7 @@ Context 'Invoke-OktaApiClient - 429 Responses' {
$Config.RequestTimeout = 100000
$Now = Get-Date # Used as a reference for the test. Indicates when the request was executed
$ResetDate = $Now.AddSeconds(3) # Indicates when one should retry
$ResetDateEpoch = $ResetDate.ToUniversalTime().Subtract((New-Object DateTime(1970, 1, 1, 0, 0, 0, 0))).TotalSeconds

Mock -ModuleName Okta.PowerShell Get-OktaConfiguration { return $Config } -Verifiable

Expand All @@ -41,7 +43,7 @@ Context 'Invoke-OktaApiClient - 429 Responses' {
Headers = @{
"Date" = @($Now)
"Content-Type" = @('application/json; charset=utf-8')
"X-Rate-Limit-Reset" = @($ResetDate)
"x-rate-limit-reset" = @($ResetDateEpoch)
"X-Okta-Request-Id" = @("foo")
}
StatusCode = 429
Expand All @@ -61,6 +63,7 @@ Context 'Invoke-OktaApiClient - 429 Responses' {
$Config.RequestTimeout = 1000
$Now = Get-Date # Used as a reference for the test. Indicates when the request was executed
$ResetDate = $Now.AddSeconds(3) # Indicates when one should retry
$ResetDateEpoch = $ResetDate.ToUniversalTime().Subtract((New-Object DateTime(1970, 1, 1, 0, 0, 0, 0))).TotalSeconds

Mock -ModuleName Okta.PowerShell Get-OktaConfiguration { return $Config } -Verifiable

Expand All @@ -71,7 +74,7 @@ Context 'Invoke-OktaApiClient - 429 Responses' {
Headers = @{
"Date" = @($Now)
"Content-Type" = @('application/json; charset=utf-8')
"X-Rate-Limit-Reset" = @($ResetDate)
"x-rate-limit-reset" = @($ResetDateEpoch)
"X-Okta-Request-Id" = @("foo")
}
StatusCode = 429
Expand All @@ -92,6 +95,7 @@ Context 'Invoke-OktaApiClient - 429 Responses' {
$Config.RequestTimeout = 1000
$Now = Get-Date # Used as a reference for the test. Indicates when the request was executed
$ResetDate = $Now.AddSeconds(3) # Indicates when one should retry
$ResetDateEpoch = $ResetDate.ToUniversalTime().Subtract((New-Object DateTime(1970, 1, 1, 0, 0, 0, 0))).TotalSeconds

Mock -ModuleName Okta.PowerShell Get-OktaConfiguration { return $Config } -Verifiable

Expand All @@ -102,7 +106,7 @@ Context 'Invoke-OktaApiClient - 429 Responses' {
Headers = @{
"Date" = @($Now)
"Content-Type" = @('application/json; charset=utf-8')
"X-Rate-Limit-Reset" = @($ResetDate)
"x-rate-limit-reset" = @($ResetDateEpoch)
"X-Okta-Request-Id" = @("foo")
}
StatusCode = 400
Expand Down
17 changes: 10 additions & 7 deletions tests/Internal/OktaRetryApi.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,19 @@ Describe -tag 'Okta.PowerShell' -name 'OktaRetryApi' {

$Now = Get-Date # Used as a reference for the test. Indicates when the request was executed
$ResetDate = $Now.AddSeconds(3) # Indicates when one should retry

$ResetDateEpoch = $ResetDate.ToUniversalTime().Subtract((New-Object DateTime(1970, 1, 1, 0, 0, 0, 0))).TotalSeconds

Mock -ModuleName Okta.PowerShell Get-OktaConfiguration { return $Config } -Verifiable

$Headers = @{"X-Rate-Limit-Reset" = @($ResetDate); "Date" = @($Now)}
$Headers = @{"x-rate-limit-reset" = @($ResetDateEpoch); "Date" = @($Now)}

# $BackoffInMilliseconds = (New-TimeSpan -Start $RequestUtcDate -End $RetryAtUtcTime).Milliseconds + 1000 #delta
$Result = CalculateDelay -Headers $Headers

Assert-MockCalled -ModuleName Okta.PowerShell Get-OktaConfiguration -Times 1

$Result | Should -Be 4000
[int]$intResult = $result
$intResult | Should -Be 4000
}

It 'Should fail if mandatory headers are not provided' {
Expand All @@ -151,17 +153,18 @@ Describe -tag 'Okta.PowerShell' -name 'OktaRetryApi' {

$Now = Get-Date # Used as a reference for the test. Indicates when the request was executed
$ResetDate = $Now.AddSeconds(3) # Indicates when one should retry

$ResetDateEpoch = $ResetDate.ToUniversalTime().Subtract((New-Object DateTime(1970, 1, 1, 0, 0, 0, 0))).TotalSeconds

Mock -ModuleName Okta.PowerShell Get-OktaConfiguration { return $Config } -Verifiable

$Headers = @{"Date" = @($Now)} # "X-Rate-Limit-Reset" = $ResetDate; not included
$Headers = @{"Date" = @($Now)} # "x-rate-limit-reset" = $ResetDate; not included

# $BackoffInMilliseconds = (New-TimeSpan -Start $RequestUtcDate -End $RetryAtUtcTime).Milliseconds + 1000 #delta
{ CalculateDelay -Headers $Headers } | Should -Throw -ExpectedMessage "Error! The required header `X-Rate-Limit-Reset` missing when calling CalculateDelay."
{ CalculateDelay -Headers $Headers } | Should -Throw -ExpectedMessage "Error! The required header `x-rate-limit-reset` missing when calling CalculateDelay."

Assert-MockCalled -ModuleName Okta.PowerShell Get-OktaConfiguration -Times 1

$Headers = @{ "X-Rate-Limit-Reset" = @($ResetDate)} # "Date" = $Now; not included
$Headers = @{ "x-rate-limit-reset" = @($ResetDateEpoch)} # "Date" = $Now; not included

# $BackoffInMilliseconds = (New-TimeSpan -Start $RequestUtcDate -End $RetryAtUtcTime).Milliseconds + 1000 #delta
{ CalculateDelay -Headers $Headers } | Should -Throw -ExpectedMessage "Error! The required header `Date` missing when calling CalculateDelay."
Expand Down

0 comments on commit 327d7a0

Please sign in to comment.