Skip to content

Commit 1bf46db

Browse files
committed
Add integration tests
1 parent 50ac0cf commit 1bf46db

1 file changed

Lines changed: 173 additions & 14 deletions

File tree

Tests/Integration.Tests.ps1

Lines changed: 173 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -391,22 +391,25 @@ Describe "Live Integration Tests" -Tag 'Integration', 'Live' -Skip:(-not $script
391391
# Track created resources for cleanup (order matters - delete dependents first)
392392
$script:CreatedResources = @{
393393
# Level 1: Most dependent resources (delete first)
394-
Devices = [System.Collections.ArrayList]::new()
395-
VMs = [System.Collections.ArrayList]::new()
396-
Interfaces = [System.Collections.ArrayList]::new()
394+
Devices = [System.Collections.ArrayList]::new()
395+
ContactAssignments = [System.Collections.ArrayList]::new()
396+
VMs = [System.Collections.ArrayList]::new()
397+
Interfaces = [System.Collections.ArrayList]::new()
397398
# Level 2: Mid-level resources
398-
Addresses = [System.Collections.ArrayList]::new()
399-
Prefixes = [System.Collections.ArrayList]::new()
400-
VLANs = [System.Collections.ArrayList]::new()
399+
Addresses = [System.Collections.ArrayList]::new()
400+
Prefixes = [System.Collections.ArrayList]::new()
401+
VLANs = [System.Collections.ArrayList]::new()
401402
# Level 3: Reference data
402-
DeviceTypes = [System.Collections.ArrayList]::new()
403-
DeviceRoles = [System.Collections.ArrayList]::new()
404-
Manufacturers = [System.Collections.ArrayList]::new()
405-
Clusters = [System.Collections.ArrayList]::new()
406-
ClusterTypes = [System.Collections.ArrayList]::new()
407-
Sites = [System.Collections.ArrayList]::new()
408-
Tenants = [System.Collections.ArrayList]::new()
409-
Tags = [System.Collections.ArrayList]::new()
403+
DeviceTypes = [System.Collections.ArrayList]::new()
404+
DeviceRoles = [System.Collections.ArrayList]::new()
405+
Manufacturers = [System.Collections.ArrayList]::new()
406+
Clusters = [System.Collections.ArrayList]::new()
407+
ClusterTypes = [System.Collections.ArrayList]::new()
408+
Sites = [System.Collections.ArrayList]::new()
409+
Tenants = [System.Collections.ArrayList]::new()
410+
Tags = [System.Collections.ArrayList]::new()
411+
Contacts = [System.Collections.ArrayList]::new()
412+
ContactRoles = [System.Collections.ArrayList]::new()
410413
}
411414

412415
Write-Host "Test Run ID: $script:TestRunId" -ForegroundColor Cyan
@@ -441,6 +444,9 @@ Describe "Live Integration Tests" -Tag 'Integration', 'Live' -Skip:(-not $script
441444
foreach ($id in $script:CreatedResources.Interfaces) {
442445
Remove-TestResource -ResourceType 'Interface' -Id $id -RemoveCommand { param($Id, $Confirm, $ErrorAction) Remove-NBDCIMInterface -Id $Id -Confirm:$Confirm -ErrorAction $ErrorAction }
443446
}
447+
foreach ($id in $script:CreatedResources.ContactAssignments) {
448+
Remove-TestResource -ResourceType 'ContactAssignment' -Id $id -RemoveCommand { param($Id, $Confirm, $ErrorAction) Remove-NBContactAssignment -Id $Id -Confirm:$Confirm -ErrorAction $ErrorAction }
449+
}
444450

445451
# Level 2: Mid-level resources
446452
foreach ($id in $script:CreatedResources.Addresses) {
@@ -478,6 +484,12 @@ Describe "Live Integration Tests" -Tag 'Integration', 'Live' -Skip:(-not $script
478484
foreach ($id in $script:CreatedResources.Tags) {
479485
Remove-TestResource -ResourceType 'Tag' -Id $id -RemoveCommand { param($Id, $Confirm, $ErrorAction) Remove-NBTag -Id $Id -Confirm:$Confirm -ErrorAction $ErrorAction }
480486
}
487+
foreach ($id in $script:CreatedResources.Contacts) {
488+
Remove-TestResource -ResourceType 'Contact' -Id $id -RemoveCommand { param($Id, $Confirm, $ErrorAction) Remove-NBContact -Id $Id -Confirm:$Confirm -ErrorAction $ErrorAction }
489+
}
490+
foreach ($id in $script:CreatedResources.ContactRoles) {
491+
Remove-TestResource -ResourceType 'ContactRole' -Id $id -RemoveCommand { param($Id, $Confirm, $ErrorAction) Remove-NBContactRole -Id $Id -Confirm:$Confirm -ErrorAction $ErrorAction }
492+
}
481493

482494
# Report any cleanup errors
483495
if ($cleanupErrors.Count -gt 0) {
@@ -1191,6 +1203,153 @@ Describe "Live Integration Tests" -Tag 'Integration', 'Live' -Skip:(-not $script
11911203
}
11921204
}
11931205

1206+
Context "Contact Role CRUD" {
1207+
BeforeAll {
1208+
$script:TestContactRoleName = "$($script:TestPrefix)-ContactRole"
1209+
$script:TestContactRoleSlug = $script:TestContactRoleName.ToLower() -replace '[^a-z0-9-]', '-'
1210+
}
1211+
1212+
It "Should create a contact role" {
1213+
$role = New-NBContactRole -Name $script:TestContactRoleName -Slug $script:TestContactRoleSlug
1214+
1215+
$role | Should -Not -BeNullOrEmpty
1216+
$role.name | Should -Be $script:TestContactRoleName
1217+
1218+
$script:TestContactRoleId = $role.id
1219+
[void]$script:CreatedResources.ContactRoles.Add($role.id)
1220+
1221+
Write-Host " Created contact role: $($role.name) (ID: $($role.id))" -ForegroundColor Green
1222+
}
1223+
1224+
It "Should get contact role by ID" {
1225+
$role = Get-NBContactRole -Id $script:TestContactRoleId
1226+
1227+
$role | Should -Not -BeNullOrEmpty
1228+
$role.id | Should -Be $script:TestContactRoleId
1229+
$role.name | Should -Be $script:TestContactRoleName
1230+
}
1231+
1232+
It "Should update contact role" {
1233+
$role = Set-NBContactRole -Id $script:TestContactRoleId -Description "$script:TestPrefix - Updated Contact Role"
1234+
1235+
$role.description | Should -BeLike "*Updated*"
1236+
}
1237+
}
1238+
1239+
Context "Contact CRUD" {
1240+
BeforeAll {
1241+
$script:TestContactName = "$($script:TestPrefix)-Contact"
1242+
$script:TestContactSlug = $script:TestContactName.ToLower() -replace '[^a-z0-9-]', '-'
1243+
}
1244+
1245+
It "Should create a contact" {
1246+
$contact = New-NBContact -Name $script:TestContactName
1247+
1248+
$contact | Should -Not -BeNullOrEmpty
1249+
$contact.name | Should -Be $script:TestContactName
1250+
1251+
$script:TestContactId = $contact.id
1252+
[void]$script:CreatedResources.Contacts.Add($contact.id)
1253+
1254+
Write-Host " Created contact: $($contact.name) (ID: $($contact.id))" -ForegroundColor Green
1255+
}
1256+
1257+
It "Should get contact by ID" {
1258+
$contact = Get-NBContact -Id $script:TestContactId
1259+
1260+
$contact | Should -Not -BeNullOrEmpty
1261+
$contact.id | Should -Be $script:TestContactId
1262+
$contact.name | Should -Be $script:TestContactName
1263+
}
1264+
1265+
It "Should update contact" {
1266+
$contact = Set-NBContact -Id $script:TestContactId -Description "$script:TestPrefix - Updated Contact"
1267+
1268+
$contact.description | Should -BeLike "*Updated*"
1269+
}
1270+
}
1271+
1272+
Context "Contact Assignment CRUD" {
1273+
BeforeAll {}
1274+
1275+
It "Should create a contact assignment to a site" {
1276+
$assignment = New-NBContactAssignment -Object_Type 'dcim.site' -Object_Id $script:TestSiteId -Contact $script:TestContactId -Role $script:TestContactRoleId
1277+
1278+
$assignment | Should -Not -BeNullOrEmpty
1279+
$assignment.contact.id | Should -Be $script:TestContactId
1280+
$assignment.role.id | Should -Be $script:TestContactRoleId
1281+
$assignment.priority | Should -BeNullOrEmpty
1282+
1283+
$script:TestContactAssignmentId = $assignment.id
1284+
[void]$script:CreatedResources.ContactAssignments.Add($assignment.id)
1285+
1286+
Write-Host " Created contact assignment (ID: $($assignment.id))" -ForegroundColor Green
1287+
}
1288+
1289+
It "Should get contact assignment by ID" {
1290+
$assignment = Get-NBContactAssignment -Id $script:TestContactAssignmentId
1291+
1292+
$assignment | Should -Not -BeNullOrEmpty
1293+
$assignment.id | Should -Be $script:TestContactAssignmentId
1294+
$assignment.contact.id | Should -Be $script:TestContactId
1295+
$assignment.role.id | Should -Be $script:TestContactRoleId
1296+
}
1297+
1298+
It "Should get contact assignment by Contact_ID" {
1299+
$assignment = Get-NBContactAssignment -Contact_Id $script:TestContactId
1300+
1301+
$assignment | Should -Not -BeNullOrEmpty
1302+
$assignment.id | Should -Be $script:TestContactAssignmentId
1303+
$assignment.contact.id | Should -Be $script:TestContactId
1304+
$assignment.role.id | Should -Be $script:TestContactRoleId
1305+
}
1306+
1307+
It "Should get contact assignment by Object_ID" {
1308+
$assignment = Get-NBContactAssignment -Object_Id $script:TestSiteId
1309+
1310+
$assignment | Should -Not -BeNullOrEmpty
1311+
$assignment.id | Should -Be $script:TestContactAssignmentId
1312+
$assignment.contact.id | Should -Be $script:TestContactId
1313+
$assignment.role.id | Should -Be $script:TestContactRoleId
1314+
}
1315+
1316+
It "Should get contact assignment by Object_Type_ID" {
1317+
$objectType = Get-NBObjectType -App_Label 'dcim' -model 'site'
1318+
$assignment = Get-NBContactAssignment -Object_Type_Id $objectType.id
1319+
1320+
$assignment | Should -Not -BeNullOrEmpty
1321+
$assignment.id | Should -Be $script:TestContactAssignmentId
1322+
$assignment.contact.id | Should -Be $script:TestContactId
1323+
$assignment.role.id | Should -Be $script:TestContactRoleId
1324+
}
1325+
1326+
It "Should get contact assignment by Object_Type" {
1327+
$assignment = Get-NBContactAssignment -Object_Type 'dcim.site'
1328+
1329+
$assignment | Should -Not -BeNullOrEmpty
1330+
$assignment.id | Should -Be $script:TestContactAssignmentId
1331+
$assignment.contact.id | Should -Be $script:TestContactId
1332+
$assignment.role.id | Should -Be $script:TestContactRoleId
1333+
}
1334+
1335+
It "Should update contact assignment" {
1336+
$splat = @{
1337+
Id = $script:TestContactAssignmentId
1338+
Priority = 'secondary'
1339+
Content_Type = 'dcim.site'
1340+
}
1341+
$assignment = Set-NBContactAssignment @splat
1342+
1343+
$assignment.priority.value | Should -Be 'secondary'
1344+
}
1345+
1346+
It "Should delete contact assignment" {
1347+
{ Remove-NBContactAssignment -Id $script:TestContactAssignmentId -Confirm:$false } | Should -Not -Throw
1348+
1349+
$script:CreatedResources.ContactAssignments.Remove($script:TestContactAssignmentId)
1350+
}
1351+
}
1352+
11941353
Context "Extras Operations (Netbox 4.x)" {
11951354
It "Should query tags without error" {
11961355
{ Get-NBTag -Limit 1 } | Should -Not -Throw

0 commit comments

Comments
 (0)