Skip to content

Commit

Permalink
#210: Adds some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed Mar 31, 2019
1 parent 093f9b8 commit 8a8afbb
Show file tree
Hide file tree
Showing 2 changed files with 254 additions and 0 deletions.
236 changes: 236 additions & 0 deletions tests/unit/Tools/Middleware.Tests.ps1
Expand Up @@ -181,4 +181,240 @@ Describe 'Middleware' {
{ Middleware -HashTable @{ 'Logic' = { write-host 'middle2' } } -Name '@access' } | Should Throw 'already exists'
}
}
}

Describe 'Invoke-PodeMiddleware' {
It 'Returns true for no middleware' {
(Invoke-PodeMiddleware -WebEvent @{} -Middleware @()) | Should Be $true
}

It 'Runs the logic for a single middleware and returns true' {
Mock Invoke-ScriptBlock { return $true }
$WebEvent = @{ 'Middleware' = @{} }
$midware = @{
'Options' = @{};
'Logic' = { 'test' | Out-Null };
}

Invoke-PodeMiddleware -WebEvent $WebEvent -Middleware @($midware) | Should Be $true

Assert-MockCalled Invoke-ScriptBlock -Times 1 -Scope It
}

It 'Runs the logic for a single middleware mapped to a route' {
Mock Invoke-ScriptBlock { return $true }
$WebEvent = @{ 'Middleware' = @{} }
$midware = @{
'Options' = @{};
'Route' = '/';
'Logic' = { 'test' | Out-Null };
}

Invoke-PodeMiddleware -WebEvent $WebEvent -Middleware @($midware) -Route '/' | Should Be $true

Assert-MockCalled Invoke-ScriptBlock -Times 1 -Scope It
}

It 'Runs the logic for two middlewares and returns true' {
Mock Invoke-ScriptBlock { return $true }
$WebEvent = @{ 'Middleware' = @{} }

$midware1 = @{
'Options' = @{};
'Logic' = { 'test' | Out-Null };
}

$midware2 = @{
'Options' = @{};
'Logic' = { 'test2' | Out-Null };
}

Invoke-PodeMiddleware -WebEvent $WebEvent -Middleware @($midware1, $midware2) | Should Be $true

Assert-MockCalled Invoke-ScriptBlock -Times 2 -Scope It
}

It 'Runs the logic for a single middleware and returns false' {
Mock Invoke-ScriptBlock { return $false }
$WebEvent = @{ 'Middleware' = @{} }
$midware = @{
'Options' = @{};
'Logic' = { 'test' | Out-Null };
}

Invoke-PodeMiddleware -WebEvent $WebEvent -Middleware @($midware) | Should Be $false

Assert-MockCalled Invoke-ScriptBlock -Times 1 -Scope It
}

It 'Runs the logic for a single middleware and returns false after erroring' {
Mock Invoke-ScriptBlock { throw 'some error' }
Mock Out-Default { }
Mock Status { }

$WebEvent = @{ 'Middleware' = @{} }
$midware = @{
'Options' = @{};
'Logic' = { 'test' | Out-Null };
}

Invoke-PodeMiddleware -WebEvent $WebEvent -Middleware @($midware) | Should Be $false

Assert-MockCalled Invoke-ScriptBlock -Times 1 -Scope It
Assert-MockCalled Status -Times 1 -Scope It
}
}

Describe 'Get-PodeAccessMiddleware' {
Mock Get-PodeInbuiltMiddleware { return @{
'Name' = $Name;
'Logic' = $ScriptBlock;
} }

It 'Returns a ScriptBlock and invokes it as true' {
$r = Get-PodeAccessMiddleware
$r.Name | Should Be '@access'
$r.Logic | Should Not Be $null

Mock Test-PodeIPAccess { return $true }
(. $r.Logic @{
'Request' = @{ 'RemoteEndPoint' = @{ 'Address' = 'localhost' } }
}) | Should Be $true
}

It 'Returns a ScriptBlock and invokes it as false' {
$r = Get-PodeAccessMiddleware
$r.Name | Should Be '@access'
$r.Logic | Should Not Be $null

Mock Test-PodeIPAccess { return $false }
Mock Status { }
(. $r.Logic @{
'Request' = @{ 'RemoteEndPoint' = @{ 'Address' = 'localhost' } }
}) | Should Be $false
}
}

Describe 'Get-PodeLimitMiddleware' {
Mock Get-PodeInbuiltMiddleware { return @{
'Name' = $Name;
'Logic' = $ScriptBlock;
} }

It 'Returns a ScriptBlock and invokes it as true' {
$r = Get-PodeLimitMiddleware
$r.Name | Should Be '@limit'
$r.Logic | Should Not Be $null

Mock Test-PodeIPLimit { return $true }
(. $r.Logic @{
'Request' = @{ 'RemoteEndPoint' = @{ 'Address' = 'localhost' } }
}) | Should Be $true
}

It 'Returns a ScriptBlock and invokes it as false' {
$r = Get-PodeLimitMiddleware
$r.Name | Should Be '@limit'
$r.Logic | Should Not Be $null

Mock Test-PodeIPLimit { return $false }
Mock Status { }
(. $r.Logic @{
'Request' = @{ 'RemoteEndPoint' = @{ 'Address' = 'localhost' } }
}) | Should Be $false
}
}

Describe 'Get-PodeRouteValidateMiddleware' {
Mock Get-PodeInbuiltMiddleware { return @{
'Name' = $Name;
'Logic' = $ScriptBlock;
} }

It 'Returns a ScriptBlock and invokes it as true' {
$WebEvent = @{ 'Parameters' = @{} }

$r = Get-PodeRouteValidateMiddleware
$r.Name | Should Be '@route-valid'
$r.Logic | Should Not Be $null

Mock Get-PodeRoute { return @{ 'Parameters' = @{}; 'Logic' = { Write-Host 'hello' }; } }
(. $r.Logic @{
'Method' = 'GET';
'Path' = '/';
}) | Should Be $true
}

It 'Returns a ScriptBlock and invokes it as false' {
$r = Get-PodeRouteValidateMiddleware
$r.Name | Should Be '@route-valid'
$r.Logic | Should Not Be $null

Mock Get-PodeRoute { return $null }
Mock Status { }
(. $r.Logic @{
'Method' = 'GET';
'Path' = '/';
}) | Should Be $false
}
}

Describe 'Get-PodeBodyMiddleware' {
Mock Get-PodeInbuiltMiddleware { return @{
'Name' = $Name;
'Logic' = $ScriptBlock;
} }

It 'Returns a ScriptBlock and invokes it as true' {
$r = Get-PodeBodyMiddleware
$r.Name | Should Be '@body'
$r.Logic | Should Not Be $null

Mock ConvertFrom-PodeRequestContent { return @{ 'Data' = @{}; 'Files' = @{}; } }
(. $r.Logic @{
'Request' = 'value'
}) | Should Be $true
}

It 'Returns a ScriptBlock and invokes it as false' {
$r = Get-PodeBodyMiddleware
$r.Name | Should Be '@body'
$r.Logic | Should Not Be $null

Mock ConvertFrom-PodeRequestContent { throw 'error' }
Mock Status { }
(. $r.Logic @{
'Request' = 'value'
}) | Should Be $false
}
}

Describe 'Get-PodeQueryMiddleware' {
Mock Get-PodeInbuiltMiddleware { return @{
'Name' = $Name;
'Logic' = $ScriptBlock;
} }

It 'Returns a ScriptBlock and invokes it as true' {
$r = Get-PodeQueryMiddleware
$r.Name | Should Be '@query'
$r.Logic | Should Not Be $null

Mock ConvertFrom-PodeNameValueToHashTable { return 'string' }
(. $r.Logic @{
'Request' = @{ 'QueryString' = 'name=bob' }
}) | Should Be $true
}

It 'Returns a ScriptBlock and invokes it as false' {
$r = Get-PodeQueryMiddleware
$r.Name | Should Be '@query'
$r.Logic | Should Not Be $null

Mock ConvertFrom-PodeNameValueToHashTable { throw 'error' }
Mock Status { }
(. $r.Logic @{
'Request' = @{ 'QueryString' = 'name=bob' }
}) | Should Be $false
}
}
18 changes: 18 additions & 0 deletions tests/unit/Tools/Sessions.Tests.ps1
Expand Up @@ -201,4 +201,22 @@ Describe 'Set-PodeSessionCookieInMemClearDown' {
$PodeContext.Schedules.Count | Should Be 1
$PodeContext.Schedules.Contains('__pode_session_inmem_cleanup__') | Should Be $true
}
}

Describe 'Set-PodeSessionCookie' {
It 'Sets a new cookie on the response' {
Mock Set-PodeCookie { }
Mock Get-PodeSessionCookieExpiry { return ([datetime]::UtcNow) }

$session = @{
'Name' = 'name';
'Id' = 'sessionId';
'Cookie' = @{};
}

Set-PodeSessionCookie -Session $session

Assert-MockCalled Set-PodeCookie -Times 1 -Scope It
Assert-MockCalled Get-PodeSessionCookieExpiry -Times 1 -Scope It
}
}

0 comments on commit 8a8afbb

Please sign in to comment.