Skip to content

Commit

Permalink
#1251: merge with develop for pester 5.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed Mar 27, 2024
2 parents bc3bd8e + f68042c commit b7377c5
Show file tree
Hide file tree
Showing 34 changed files with 4,792 additions and 3,639 deletions.
2 changes: 1 addition & 1 deletion pode.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ param(
#>

$Versions = @{
Pester = '4.8.0'
Pester = '5.5.0'
MkDocs = '1.5.3'
PSCoveralls = '1.0.0'
SevenZip = '18.5.0.20180730'
Expand Down
74 changes: 40 additions & 34 deletions tests/integration/Authentication.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
$path = $MyInvocation.MyCommand.Path
$src = (Split-Path -Parent -Path $path) -ireplace '[\\/]tests[\\/]integration', '/src/'
Get-ChildItem "$($src)/*.ps1" -Recurse | Resolve-Path | ForEach-Object { . $_ }
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseUsingScopeModifierInNewRunspaces', '')]
param()

BeforeAll {
$path = $PSCommandPath
$src = (Split-Path -Parent -Path $path) -ireplace '[\\/]tests[\\/]integration', '/src/'
Get-ChildItem "$($src)/*.ps1" -Recurse | Resolve-Path | ForEach-Object { . $_ }
}

Describe 'Authentication Requests' {

BeforeAll {
$Port = 50000
$Endpoint = "http://localhost:$($Port)"
$Endpoint = "http://127.0.0.1:$($Port)"

Start-Job -Name 'Pode' -ErrorAction Stop -ScriptBlock {
Import-Module -Name "$($using:PSScriptRoot)\..\..\src\Pode.psm1"

Start-PodeServer {
Start-PodeServer -Quiet -ScriptBlock {
Add-PodeEndpoint -Address localhost -Port $using:Port -Protocol Http

New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
Expand All @@ -24,7 +30,7 @@ Describe 'Authentication Requests' {
param($username, $password)

if (($username -eq 'morty') -and ($password -eq 'pickle')) {
return @{ User = @{ ID ='M0R7Y302' } }
return @{ User = @{ ID = 'M0R7Y302' } }
}

return @{ Message = 'Invalid details supplied' }
Expand All @@ -40,7 +46,7 @@ Describe 'Authentication Requests' {

if ($token -ieq 'test-token') {
return @{
User = @{ ID ='M0R7Y302' }
User = @{ ID = 'M0R7Y302' }
Scope = 'write'
}
}
Expand All @@ -58,7 +64,7 @@ Describe 'Authentication Requests' {

if ($key -ieq 'test-key') {
return @{
User = @{ ID ='M0R7Y302' }
User = @{ ID = 'M0R7Y302' }
}
}

Expand All @@ -75,7 +81,7 @@ Describe 'Authentication Requests' {

if ($jwt.username -ieq 'morty') {
return @{
User = @{ ID ='M0R7Y302' }
User = @{ ID = 'M0R7Y302' }
}
}

Expand All @@ -92,7 +98,7 @@ Describe 'Authentication Requests' {

if ($jwt.username -ieq 'morty') {
return @{
User = @{ ID ='M0R7Y302' }
User = @{ ID = 'M0R7Y302' }
}
}

Expand Down Expand Up @@ -120,45 +126,45 @@ Describe 'Authentication Requests' {
# BASIC
It 'basic - returns ok for valid creds' {
$result = Invoke-RestMethod -Uri "$($Endpoint)/auth/basic" -Method Post -Headers @{ Authorization = 'Basic bW9ydHk6cGlja2xl' }
$result.Result | Should Be 'OK'
$result.Result | Should -Be 'OK'
}

It 'basic - returns 401 for invalid creds' {
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/basic" -Method Post -Headers @{ Authorization = 'Basic cmljazpwaWNrbGU=' } -ErrorAction Stop } | Should Throw '401'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/basic" -Method Post -Headers @{ Authorization = 'Basic cmljazpwaWNrbGU=' } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*401*'
}

It 'basic - returns 400 for invalid base64' {
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/basic" -Method Post -Headers @{ Authorization = 'Basic cmlazpwaNrbGU' } -ErrorAction Stop } | Should Throw '400'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/basic" -Method Post -Headers @{ Authorization = 'Basic cmlazpwaNrbGU' } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*400*'
}


# BEARER
It 'bearer - returns ok for valid token' {
$result = Invoke-RestMethod -Uri "$($Endpoint)/auth/bearer" -Method Get -Headers @{ Authorization = 'Bearer test-token' }
$result.Result | Should Be 'OK'
$result.Result | Should -Be 'OK'
}

It 'bearer - returns 401 for invalid token' {
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/bearer" -Method Get -Headers @{ Authorization = 'Bearer fake-token' } -ErrorAction Stop } | Should Throw '401'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/bearer" -Method Get -Headers @{ Authorization = 'Bearer fake-token' } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*401*'
}

It 'bearer - returns 400 for no token' {
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/bearer" -Method Get -Headers @{ Authorization = 'Bearer' } -ErrorAction Stop } | Should Throw '400'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/bearer" -Method Get -Headers @{ Authorization = 'Bearer' } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*400*'
}


# API KEY
It 'apikey - returns ok for valid key' {
$result = Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey" -Method Get -Headers @{ 'X-API-KEY' = 'test-key' }
$result.Result | Should Be 'OK'
$result.Result | Should -Be 'OK'
}

It 'apikey - returns 401 for invalid key' {
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey" -Method Get -Headers @{ 'X-API-KEY' = 'fake-key' } -ErrorAction Stop } | Should Throw '401'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey" -Method Get -Headers @{ 'X-API-KEY' = 'fake-key' } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*401*'
}

It 'apikey - returns 400 for no key' {
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey" -Method Get -ErrorAction Stop } | Should Throw '400'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey" -Method Get -ErrorAction Stop } | Should -Throw -ExpectedMessage '*400*'
}


Expand All @@ -169,39 +175,39 @@ Describe 'Authentication Requests' {
$jwt = ConvertTo-PodeJwt -Header $header -Payload $payload

$result = Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/notsigned" -Method Get -Headers @{ 'X-API-KEY' = $jwt }
$result.Result | Should Be 'OK'
$result.Result | Should -Be 'OK'
}

It 'apikey - jwt not signed - returns 400 for invalid key - invalid base64' {
$header = @{ alg = 'none' }
$payload = @{ sub = '123'; username = 'morty' }
$jwt = ConvertTo-PodeJwt -Header $header -Payload $payload

{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/notsigned" -Method Get -Headers @{ 'X-API-KEY' = "hh$($jwt)" } -ErrorAction Stop } | Should Throw '400'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/notsigned" -Method Get -Headers @{ 'X-API-KEY' = "hh$($jwt)" } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*400*'
}

It 'apikey - jwt not signed - returns 401 for invalid key - invalid username' {
$header = @{ alg = 'none' }
$payload = @{ sub = '123'; username = 'rick' }
$jwt = ConvertTo-PodeJwt -Header $header -Payload $payload

{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/notsigned" -Method Get -Headers @{ 'X-API-KEY' = $jwt } -ErrorAction Stop } | Should Throw '401'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/notsigned" -Method Get -Headers @{ 'X-API-KEY' = $jwt } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*401*'
}

It 'apikey - jwt not signed - returns 400 for invalid key - expired' {
$header = @{ alg = 'none' }
$payload = @{ sub = '123'; username = 'morty'; exp = 100 }
$jwt = ConvertTo-PodeJwt -Header $header -Payload $payload

{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/notsigned" -Method Get -Headers @{ 'X-API-KEY' = $jwt } -ErrorAction Stop } | Should Throw '400'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/notsigned" -Method Get -Headers @{ 'X-API-KEY' = $jwt } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*400*'
}

It 'apikey - jwt not signed - returns 400 for invalid key - not started' {
$header = @{ alg = 'none' }
$payload = @{ sub = '123'; username = 'morty'; nbf = ([System.DateTimeOffset]::Now.AddYears(1).ToUnixTimeSeconds()) }
$jwt = ConvertTo-PodeJwt -Header $header -Payload $payload

{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/notsigned" -Method Get -Headers @{ 'X-API-KEY' = $jwt } -ErrorAction Stop } | Should Throw '400'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/notsigned" -Method Get -Headers @{ 'X-API-KEY' = $jwt } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*400*'
}


Expand All @@ -212,60 +218,60 @@ Describe 'Authentication Requests' {
$jwt = ConvertTo-PodeJwt -Header $header -Payload $payload -Secret 'secret'

$result = Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/signed" -Method Get -Headers @{ 'X-API-KEY' = $jwt }
$result.Result | Should Be 'OK'
$result.Result | Should -Be 'OK'
}

It 'apikey - jwt signed - returns ok for valid key - valid exp/nbf' {
$header = @{ alg = 'hs256' }
$payload = @{
sub = '123'
sub = '123'
username = 'morty'
nbf = ([System.DateTimeOffset]::Now.AddDays(-1).ToUnixTimeSeconds())
exp = ([System.DateTimeOffset]::Now.AddDays(1).ToUnixTimeSeconds())
nbf = ([System.DateTimeOffset]::Now.AddDays(-1).ToUnixTimeSeconds())
exp = ([System.DateTimeOffset]::Now.AddDays(1).ToUnixTimeSeconds())
}
$jwt = ConvertTo-PodeJwt -Header $header -Payload $payload -Secret 'secret'

$result = Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/signed" -Method Get -Headers @{ 'X-API-KEY' = $jwt }
$result.Result | Should Be 'OK'
$result.Result | Should -Be 'OK'
}

It 'apikey - jwt signed - returns 400 for invalid key - invalid base64' {
$header = @{ alg = 'hs256' }
$payload = @{ sub = '123'; username = 'morty' }
$jwt = ConvertTo-PodeJwt -Header $header -Payload $payload -Secret 'secret'

{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/signed" -Method Get -Headers @{ 'X-API-KEY' = "hh$($jwt)" } -ErrorAction Stop } | Should Throw '400'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/signed" -Method Get -Headers @{ 'X-API-KEY' = "hh$($jwt)" } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*400*'
}

It 'apikey - jwt signed - returns 400 for invalid key - invalid signature' {
$header = @{ alg = 'hs256' }
$payload = @{ sub = '123'; username = 'morty' }
$jwt = ConvertTo-PodeJwt -Header $header -Payload $payload -Secret 'secret'

{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/signed" -Method Get -Headers @{ 'X-API-KEY' = "$($jwt)hh" } -ErrorAction Stop } | Should Throw '400'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/signed" -Method Get -Headers @{ 'X-API-KEY' = "$($jwt)hh" } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*400*'
}

It 'apikey - jwt signed - returns 400 for invalid key - invalid secret' {
$header = @{ alg = 'hs256' }
$payload = @{ sub = '123'; username = 'morty' }
$jwt = ConvertTo-PodeJwt -Header $header -Payload $payload -Secret 'fake'

{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/signed" -Method Get -Headers @{ 'X-API-KEY' = $jwt } -ErrorAction Stop } | Should Throw '400'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/signed" -Method Get -Headers @{ 'X-API-KEY' = $jwt } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*400*'
}

It 'apikey - jwt signed - returns 400 for invalid key - none algorithm' {
$header = @{ alg = 'none' }
$payload = @{ sub = '123'; username = 'morty' }
$jwt = ConvertTo-PodeJwt -Header $header -Payload $payload

{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/signed" -Method Get -Headers @{ 'X-API-KEY' = $jwt } -ErrorAction Stop } | Should Throw '400'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/signed" -Method Get -Headers @{ 'X-API-KEY' = $jwt } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*400*'
}

It 'apikey - jwt signed - returns 401 for invalid key - invalid username' {
$header = @{ alg = 'hs256' }
$payload = @{ sub = '123'; username = 'rick' }
$jwt = ConvertTo-PodeJwt -Header $header -Payload $payload -Secret 'secret'

{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/signed" -Method Get -Headers @{ 'X-API-KEY' = $jwt } -ErrorAction Stop } | Should Throw '401'
{ Invoke-RestMethod -Uri "$($Endpoint)/auth/apikey/jwt/signed" -Method Get -Headers @{ 'X-API-KEY' = $jwt } -ErrorAction Stop } | Should -Throw -ExpectedMessage '*401*'
}
}
23 changes: 13 additions & 10 deletions tests/integration/Endpoints.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
param()

Describe 'Endpoint Requests' {

BeforeAll {
$Port1 = 50000
$Endpoint1 = "http://localhost:$($Port1)"
$Endpoint1 = "http://127.0.0.1:$($Port1)"

$Port2 = 50001
$Endpoint2 = "http://localhost:$($Port2)"
$Endpoint2 = "http://127.0.0.1:$($Port2)"

Start-Job -Name 'Pode' -ErrorAction Stop -ScriptBlock {
Import-Module -Name "$($using:PSScriptRoot)\..\..\src\Pode.psm1"

Start-PodeServer -RootPath $using:PSScriptRoot {
Start-PodeServer -RootPath $using:PSScriptRoot -Quiet -ScriptBlock {
Add-PodeEndpoint -Address localhost -Port $using:Port1 -Protocol Http -Name 'Endpoint1'
Add-PodeEndpoint -Address localhost -Port $using:Port2 -Protocol Http -Name 'Endpoint2'

New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
Add-PodeRoute -Method Get -Path '/close' -ScriptBlock {
Add-PodeRoute -Method Get -Path '/close' -ScriptBlock {
Close-PodeServer
}

Expand Down Expand Up @@ -45,26 +48,26 @@ Describe 'Endpoint Requests' {

It 'responds back with pong1' {
$result = Invoke-RestMethod -Uri "$($Endpoint1)/ping-1" -Method Get
$result.Result | Should Be 'Pong1'
$result.Result | Should -Be 'Pong1'
}

It 'fails pong1 on second endpoint' {
{ Invoke-RestMethod -Uri "$($Endpoint2)/ping-1" -Method Get -ErrorAction Stop } | Should Throw '404'
{ Invoke-RestMethod -Uri "$($Endpoint2)/ping-1" -Method Get -ErrorAction Stop } | Should -Throw -ExpectedMessage '*404*'
}

It 'responds back with pong2' {
$result = Invoke-RestMethod -Uri "$($Endpoint2)/ping-2" -Method Get
$result.Result | Should Be 'Pong2'
$result.Result | Should -Be 'Pong2'
}

It 'fails pong2 on first endpoint' {
{ Invoke-RestMethod -Uri "$($Endpoint1)/ping-2" -Method Get -ErrorAction Stop } | Should Throw '404'
{ Invoke-RestMethod -Uri "$($Endpoint1)/ping-2" -Method Get -ErrorAction Stop } | Should -Throw -ExpectedMessage '*404*'
}

It 'responds back with pong all' {
$result = Invoke-RestMethod -Uri "$($Endpoint1)/ping-all" -Method Get
$result.Result | Should Be 'PongAll'
$result.Result | Should -Be 'PongAll'
$result = Invoke-RestMethod -Uri "$($Endpoint2)/ping-all" -Method Get
$result.Result | Should Be 'PongAll'
$result.Result | Should -Be 'PongAll'
}
}
Loading

0 comments on commit b7377c5

Please sign in to comment.