2022-12-06 13:34:52 -05:00
|
|
|
|
|
2021-09-10 17:38:00 +02:00
|
|
|
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
|
|
|
|
|
|
param
|
|
|
|
|
|
(
|
|
|
|
|
|
)
|
2018-05-25 15:11:59 -04:00
|
|
|
|
Import-Module Pester
|
|
|
|
|
|
Remove-Module NetboxPS -Force -ErrorAction SilentlyContinue
|
|
|
|
|
|
|
|
|
|
|
|
$ModulePath = "$PSScriptRoot\..\dist\NetboxPS.psd1"
|
|
|
|
|
|
|
|
|
|
|
|
if (Test-Path $ModulePath) {
|
|
|
|
|
|
Import-Module $ModulePath -ErrorAction Stop
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Describe -Name "DCIM Platforms Tests" -Tag 'DCIM', 'platforms' -Fixture {
|
|
|
|
|
|
Mock -CommandName 'CheckNetboxIsConnected' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
|
|
|
|
|
return $true
|
|
|
|
|
|
}
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
Mock -CommandName 'Invoke-RestMethod' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
|
|
|
|
|
# Return a hashtable of the items we would normally pass to Invoke-RestMethod
|
|
|
|
|
|
return [ordered]@{
|
2021-09-10 17:38:00 +02:00
|
|
|
|
'Method' = $Method
|
|
|
|
|
|
'Uri' = $Uri
|
|
|
|
|
|
'Headers' = $Headers
|
|
|
|
|
|
'Timeout' = $Timeout
|
2018-05-25 15:11:59 -04:00
|
|
|
|
'ContentType' = $ContentType
|
2021-09-10 17:38:00 +02:00
|
|
|
|
'Body' = $Body
|
2018-05-25 15:11:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
|
|
|
|
|
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
|
|
|
|
|
|
}
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
Mock -CommandName 'Get-NetboxHostname' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
|
|
|
|
|
return 'netbox.domain.com'
|
|
|
|
|
|
}
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
|
|
|
|
|
|
Context -Name "Get-NetboxDCIMPlatform" -Fixture {
|
|
|
|
|
|
It "Should request the default number of platforms" {
|
|
|
|
|
|
$Result = Get-NetboxDCIMPlatform
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
Assert-VerifiableMock
|
|
|
|
|
|
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
$Result.Method | Should -Be 'GET'
|
|
|
|
|
|
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/platforms/'
|
|
|
|
|
|
$Result.Headers.Keys.Count | Should -BeExactly 1
|
|
|
|
|
|
}
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
It "Should request with a limit and offset" {
|
|
|
|
|
|
$Result = Get-NetboxDCIMPlatform -Limit 10 -Offset 100
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
Assert-VerifiableMock
|
|
|
|
|
|
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
$Result.Method | Should -Be 'GET'
|
|
|
|
|
|
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/platforms/?offset=100&limit=10'
|
|
|
|
|
|
$Result.Headers.Keys.Count | Should -BeExactly 1
|
|
|
|
|
|
}
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
It "Should request with a platform name" {
|
|
|
|
|
|
$Result = Get-NetboxDCIMPlatform -Name "Windows Server 2016"
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
Assert-VerifiableMock
|
|
|
|
|
|
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
$Result.Method | Should -Be 'GET'
|
|
|
|
|
|
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/platforms/?name=Windows+Server+2016'
|
|
|
|
|
|
$Result.Headers.Keys.Count | Should -BeExactly 1
|
|
|
|
|
|
}
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
It "Should request a platform by manufacturer" {
|
|
|
|
|
|
$Result = Get-NetboxDCIMPlatform -Manufacturer 'Cisco'
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
Assert-VerifiableMock
|
|
|
|
|
|
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
$Result.Method | Should -Be 'GET'
|
|
|
|
|
|
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/platforms/?manufacturer=Cisco'
|
|
|
|
|
|
$Result.Headers.Keys.Count | Should -BeExactly 1
|
|
|
|
|
|
}
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
It "Should request a platform by ID" {
|
|
|
|
|
|
$Result = Get-NetboxDCIMPlatform -Id 10
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
Assert-VerifiableMock
|
|
|
|
|
|
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
$Result.Method | Should -Be 'GET'
|
|
|
|
|
|
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/platforms/10/'
|
|
|
|
|
|
$Result.Headers.Keys.Count | Should -BeExactly 1
|
|
|
|
|
|
}
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
It "Should request multiple platforms by ID" {
|
|
|
|
|
|
$Result = Get-NetboxDCIMPlatform -Id 10, 20
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
Assert-VerifiableMock
|
|
|
|
|
|
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 2 -Scope 'It' -Exactly
|
2021-07-23 22:06:42 +02:00
|
|
|
|
|
2018-05-25 15:11:59 -04:00
|
|
|
|
$Result.Method | Should -Be 'GET', 'GET'
|
|
|
|
|
|
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/platforms/10/', 'https://netbox.domain.com/api/dcim/platforms/20/'
|
|
|
|
|
|
$Result.Headers.Keys.Count | Should -BeExactly 2
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|