diff --git a/tests/cleanup_test_data.sql b/tests/cleanup_test_data.sql new file mode 100644 index 0000000..acd8a46 --- /dev/null +++ b/tests/cleanup_test_data.sql @@ -0,0 +1,37 @@ +-- ============================================================================ +-- ShopDB Test Data Cleanup Script +-- ============================================================================ +-- Run this after test_forms.ps1 to remove test data +-- Replace AUTOTEST_YYYYMMDD_HHMMSS with actual test prefix from test run +-- ============================================================================ + +-- Set the test prefix (update this with the actual prefix from test output) +SET @test_prefix = 'AUTOTEST_%'; + +-- Show what will be deleted (preview) +SELECT 'Notifications to delete:' AS info, COUNT(*) AS count FROM notifications WHERE notification LIKE @test_prefix; +SELECT 'Machines to delete:' AS info, COUNT(*) AS count FROM machines WHERE machinenumber LIKE @test_prefix OR alias LIKE @test_prefix; +SELECT 'Printers to delete:' AS info, COUNT(*) AS count FROM printers WHERE printercsfname LIKE @test_prefix; +SELECT 'Subnets to delete:' AS info, COUNT(*) AS count FROM subnets WHERE description LIKE @test_prefix; +SELECT 'Applications to delete:' AS info, COUNT(*) AS count FROM applications WHERE applicationname LIKE @test_prefix; +SELECT 'KB Articles to delete:' AS info, COUNT(*) AS count FROM knowledgebase WHERE shortdescription LIKE @test_prefix; +SELECT 'Vendors to delete:' AS info, COUNT(*) AS count FROM vendors WHERE vendor LIKE @test_prefix; +SELECT 'Models to delete:' AS info, COUNT(*) AS count FROM models WHERE modelnumber LIKE @test_prefix; + +-- Uncomment lines below to actually delete (run preview first!) + +-- DELETE FROM communications WHERE machineid IN (SELECT machineid FROM machines WHERE machinenumber LIKE @test_prefix OR alias LIKE @test_prefix); +-- DELETE FROM machinerelationships WHERE machineid IN (SELECT machineid FROM machines WHERE machinenumber LIKE @test_prefix OR alias LIKE @test_prefix); +-- DELETE FROM notifications WHERE notification LIKE @test_prefix; +-- DELETE FROM machines WHERE machinenumber LIKE @test_prefix OR alias LIKE @test_prefix; +-- DELETE FROM printers WHERE printercsfname LIKE @test_prefix; +-- DELETE FROM subnets WHERE description LIKE @test_prefix; +-- DELETE FROM applications WHERE applicationname LIKE @test_prefix; +-- DELETE FROM knowledgebase WHERE shortdescription LIKE @test_prefix; +-- DELETE FROM vendors WHERE vendor LIKE @test_prefix; +-- DELETE FROM models WHERE modelnumber LIKE @test_prefix; + +-- Verify cleanup +-- SELECT 'Remaining test data:' AS info; +-- SELECT 'Notifications:' AS type, COUNT(*) AS count FROM notifications WHERE notification LIKE 'AUTOTEST%'; +-- SELECT 'Machines:' AS type, COUNT(*) AS count FROM machines WHERE machinenumber LIKE 'AUTOTEST%' OR alias LIKE 'AUTOTEST%'; diff --git a/tests/test_forms.ps1 b/tests/test_forms.ps1 index ae78eb3..03b7e61 100644 --- a/tests/test_forms.ps1 +++ b/tests/test_forms.ps1 @@ -1,17 +1,43 @@ # ============================================================================ -# ShopDB Form Testing Script +# ShopDB Comprehensive Form Testing Script # ============================================================================ -# Tests form submissions across key pages to verify no errors occur +# Tests form submissions across all key pages to verify no errors occur # Run from PowerShell on a machine that can reach the dev server +# +# Features: +# - Tests all add/edit forms (machines, devices, printers, subnets, etc.) +# - Automatic test data cleanup +# - Detailed pass/fail reporting # ============================================================================ param( [string]$BaseUrl = "http://192.168.122.151:8080", - [switch]$Verbose + [switch]$Verbose, + [switch]$SkipCleanup, + [switch]$CleanupOnly ) $ErrorActionPreference = "Continue" $TestResults = @() +$TestTimestamp = Get-Date -Format "yyyyMMdd_HHmmss" +$TestPrefix = "AUTOTEST_$TestTimestamp" + +# Track created records for cleanup +$CreatedRecords = @{ + Notifications = @() + Machines = @() + Printers = @() + Subnets = @() + Applications = @() + KBArticles = @() + Vendors = @() + Models = @() + NetworkDevices = @() +} + +# ============================================================================ +# HELPER FUNCTIONS +# ============================================================================ function Write-TestResult { param( @@ -47,7 +73,7 @@ function Test-PageLoads { } # Check for ASP errors - if ($response.Content -match "error|Error 500|Microsoft VBScript") { + if ($response.Content -match "Microsoft VBScript|Error 500|ADODB\.") { $passed = $false Write-TestResult -TestName $TestName -Passed $false -Message "Page contains error text" return $false @@ -68,11 +94,12 @@ function Test-FormSubmission { [string]$TestName, [hashtable]$FormData, [string]$ExpectedRedirect = "", - [string]$ExpectedContent = "" + [string]$ExpectedContent = "", + [string]$RecordType = "", + [string]$RecordIdentifier = "" ) try { - # Submit form $response = Invoke-WebRequest -Uri $Url -Method POST -Body $FormData -UseBasicParsing -TimeoutSec 30 -MaximumRedirection 0 -ErrorAction SilentlyContinue $passed = $true @@ -84,16 +111,18 @@ function Test-FormSubmission { if ($ExpectedRedirect -and $response.Headers.Location -notmatch $ExpectedRedirect) { $passed = $false } + # Track created record for cleanup + if ($RecordType -and $RecordIdentifier) { + $script:CreatedRecords[$RecordType] += $RecordIdentifier + } } - # Check for 200 with expected content elseif ($response.StatusCode -eq 200) { if ($ExpectedContent -and $response.Content -notmatch $ExpectedContent) { $passed = $false } - # Check for error messages in response - if ($response.Content -match "Error:|error|Microsoft VBScript|500") { + if ($response.Content -match "Microsoft VBScript|Error 500|ADODB\.") { $passed = $false - $message = "Response contains error text" + $message = "Response contains ASP error" } } else { @@ -106,6 +135,9 @@ function Test-FormSubmission { catch { # 302 redirects throw exceptions with -MaximumRedirection 0 if ($_.Exception.Response.StatusCode -eq 302 -or $_.Exception.Message -match "302") { + if ($RecordType -and $RecordIdentifier) { + $script:CreatedRecords[$RecordType] += $RecordIdentifier + } Write-TestResult -TestName $TestName -Passed $true -Message "Redirected (success)" return $true } @@ -114,14 +146,33 @@ function Test-FormSubmission { } } +function Get-ExistingId { + param( + [string]$PageUrl, + [string]$Pattern + ) + try { + $page = Invoke-WebRequest -Uri $PageUrl -UseBasicParsing -TimeoutSec 30 + if ($page.Content -match $Pattern) { + return $Matches[1] + } + } + catch {} + return $null +} + # ============================================================================ # START TESTS # ============================================================================ +if (-not $CleanupOnly) { + Write-Host "" Write-Host "============================================" -ForegroundColor Cyan -Write-Host "ShopDB Form Testing - $(Get-Date)" -ForegroundColor Cyan +Write-Host "ShopDB Comprehensive Form Testing" -ForegroundColor Cyan +Write-Host "Started: $(Get-Date)" -ForegroundColor Cyan Write-Host "Base URL: $BaseUrl" -ForegroundColor Cyan +Write-Host "Test Prefix: $TestPrefix" -ForegroundColor Cyan Write-Host "============================================" -ForegroundColor Cyan Write-Host "" @@ -130,164 +181,405 @@ Write-Host "" # ---------------------------------------------------------------------------- Write-Host "--- PAGE LOAD TESTS ---" -ForegroundColor Yellow -Test-PageLoads -Url "$BaseUrl/default.asp" -TestName "Dashboard loads" -ExpectedContent "Dashboard" -Test-PageLoads -Url "$BaseUrl/displaynotifications.asp" -TestName "Notifications list loads" -ExpectedContent "Notification" -Test-PageLoads -Url "$BaseUrl/addnotification.asp" -TestName "Add notification form loads" -ExpectedContent "Add Notification" -Test-PageLoads -Url "$BaseUrl/displayapplications.asp" -TestName "Applications list loads" -ExpectedContent "Application" -Test-PageLoads -Url "$BaseUrl/displayprinters.asp" -TestName "Printers list loads" -ExpectedContent "Printer" -Test-PageLoads -Url "$BaseUrl/displaypcs.asp" -TestName "PCs list loads" -ExpectedContent "PC" -Test-PageLoads -Url "$BaseUrl/displaymachines.asp" -TestName "Machines list loads" -ExpectedContent "Machine" -Test-PageLoads -Url "$BaseUrl/network_devices.asp" -TestName "Network devices loads" -ExpectedContent "Network" +Test-PageLoads -Url "$BaseUrl/default.asp" -TestName "Dashboard loads" +Test-PageLoads -Url "$BaseUrl/displaynotifications.asp" -TestName "Notifications list loads" +Test-PageLoads -Url "$BaseUrl/displayapplications.asp" -TestName "Applications list loads" +Test-PageLoads -Url "$BaseUrl/displayprinters.asp" -TestName "Printers list loads" +Test-PageLoads -Url "$BaseUrl/displaypcs.asp" -TestName "PCs list loads" +Test-PageLoads -Url "$BaseUrl/displaymachines.asp" -TestName "Equipment list loads" +Test-PageLoads -Url "$BaseUrl/network_devices.asp" -TestName "Network devices loads" +Test-PageLoads -Url "$BaseUrl/displaysubnets.asp" -TestName "Subnets list loads" +Test-PageLoads -Url "$BaseUrl/displayknowledgebase.asp" -TestName "Knowledge base loads" +Test-PageLoads -Url "$BaseUrl/machinemap.asp" -TestName "Machine map loads" +Test-PageLoads -Url "$BaseUrl/printermap.asp" -TestName "Printer map loads" Write-Host "" # ---------------------------------------------------------------------------- -# 2. NOTIFICATION FORM TESTS +# 2. ADD FORM PAGE LOAD TESTS +# ---------------------------------------------------------------------------- +Write-Host "--- ADD FORM PAGE LOAD TESTS ---" -ForegroundColor Yellow + +Test-PageLoads -Url "$BaseUrl/addnotification.asp" -TestName "Add notification form loads" +Test-PageLoads -Url "$BaseUrl/addmachine.asp" -TestName "Add equipment form loads" +Test-PageLoads -Url "$BaseUrl/addprinter.asp" -TestName "Add printer form loads" +Test-PageLoads -Url "$BaseUrl/addsubnet.asp" -TestName "Add subnet form loads" +Test-PageLoads -Url "$BaseUrl/addapplication.asp" -TestName "Add application form loads" +Test-PageLoads -Url "$BaseUrl/addknowledgebase.asp" -TestName "Add KB article form loads" +Test-PageLoads -Url "$BaseUrl/addvendor.asp" -TestName "Add vendor form loads" +Test-PageLoads -Url "$BaseUrl/addmodel.asp" -TestName "Add model form loads" +Test-PageLoads -Url "$BaseUrl/deviceaccesspoint.asp" -TestName "Add access point form loads" +Test-PageLoads -Url "$BaseUrl/deviceswitch.asp" -TestName "Add switch form loads" +Test-PageLoads -Url "$BaseUrl/devicecamera.asp" -TestName "Add camera form loads" +Test-PageLoads -Url "$BaseUrl/deviceidf.asp" -TestName "Add IDF form loads" +Test-PageLoads -Url "$BaseUrl/deviceserver.asp" -TestName "Add server form loads" + +Write-Host "" + +# ---------------------------------------------------------------------------- +# 3. NOTIFICATION FORM TESTS # ---------------------------------------------------------------------------- Write-Host "--- NOTIFICATION FORM TESTS ---" -ForegroundColor Yellow -# Test: Create notification with all fields $notificationData = @{ - notification = "Test notification from automated testing - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" - notificationtypeid = "2" # Awareness - businessunitid = "" # All business units - appid = "" # No application + notification = "$TestPrefix - Test notification basic" + notificationtypeid = "2" + businessunitid = "" + appid = "" ticketnumber = "GETEST123456" starttime = (Get-Date).ToString("yyyy-MM-ddTHH:mm") endtime = (Get-Date).AddDays(1).ToString("yyyy-MM-ddTHH:mm") isactive = "1" isshopfloor = "0" } -Test-FormSubmission -Url "$BaseUrl/savenotification_direct.asp" -TestName "Create notification (basic)" -FormData $notificationData -ExpectedRedirect "displaynotifications" +Test-FormSubmission -Url "$BaseUrl/savenotificationdirect.asp" -TestName "Create notification (basic)" -FormData $notificationData -ExpectedRedirect "displaynotifications" -RecordType "Notifications" -RecordIdentifier "$TestPrefix - Test notification basic" -# Test: Create notification with application linked $notificationWithApp = @{ - notification = "Test with app link - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" - notificationtypeid = "3" # Change - businessunitid = "2" # Specific BU - appid = "6" # PC-DMIS + notification = "$TestPrefix - Test notification with app" + notificationtypeid = "3" + businessunitid = "" + appid = "6" ticketnumber = "GECHG123456" starttime = (Get-Date).ToString("yyyy-MM-ddTHH:mm") endtime = (Get-Date).AddHours(4).ToString("yyyy-MM-ddTHH:mm") isactive = "1" isshopfloor = "1" } -Test-FormSubmission -Url "$BaseUrl/savenotification_direct.asp" -TestName "Create notification (with app)" -FormData $notificationWithApp -ExpectedRedirect "displaynotifications" +Test-FormSubmission -Url "$BaseUrl/savenotificationdirect.asp" -TestName "Create notification (with app)" -FormData $notificationWithApp -ExpectedRedirect "displaynotifications" -RecordType "Notifications" -RecordIdentifier "$TestPrefix - Test notification with app" -# Test: Create notification without end time (indefinite) -$notificationIndefinite = @{ - notification = "Indefinite test notification - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" - notificationtypeid = "4" # Incident - businessunitid = "" - appid = "" - ticketnumber = "" - starttime = (Get-Date).ToString("yyyy-MM-ddTHH:mm") - endtime = "" # Indefinite +Write-Host "" + +# ---------------------------------------------------------------------------- +# 4. EQUIPMENT (MACHINE) FORM TESTS +# ---------------------------------------------------------------------------- +Write-Host "--- EQUIPMENT FORM TESTS ---" -ForegroundColor Yellow + +$equipmentData = @{ + machinenumber = "$TestPrefix-EQ001" + modelid = "1" + businessunitid = "1" + alias = "Test Equipment Alias" + machinenotes = "Created by automated test script" + ip1 = "192.168.99.101" + mac1 = "00:11:22:33:44:55" + ip2 = "" + mac2 = "" + ip3 = "" + mac3 = "" + controllingpc = "" + dualpathid = "" + thirdpartymanaged = "0" + mapleft = "" + maptop = "" +} +Test-FormSubmission -Url "$BaseUrl/savemachinedirect.asp" -TestName "Create equipment" -FormData $equipmentData -ExpectedRedirect "displaymachines|editmachine" -RecordType "Machines" -RecordIdentifier "$TestPrefix-EQ001" + +Write-Host "" + +# ---------------------------------------------------------------------------- +# 5. PRINTER FORM TESTS +# ---------------------------------------------------------------------------- +Write-Host "--- PRINTER FORM TESTS ---" -ForegroundColor Yellow + +$printerData = @{ + printercsfname = "$TestPrefix-PRN001" + printerwindowsname = "TestPrinter001" + modelid = "1" + serialnumber = "TESTPRN123456" + ipaddress = "192.168.99.201" + fqdn = "testprinter001.test.local" + machineid = "" + mapleft = "" + maptop = "" +} +Test-FormSubmission -Url "$BaseUrl/saveprinterdirect.asp" -TestName "Create printer" -FormData $printerData -ExpectedRedirect "displayprinters|editprinter" -RecordType "Printers" -RecordIdentifier "$TestPrefix-PRN001" + +Write-Host "" + +# ---------------------------------------------------------------------------- +# 6. SUBNET FORM TESTS +# ---------------------------------------------------------------------------- +Write-Host "--- SUBNET FORM TESTS ---" -ForegroundColor Yellow + +$subnetData = @{ + ipstart = "192.168.99.0" + cidr = "24" + vlan = "999" + subnettypeid = "1" + description = "$TestPrefix - Test subnet" +} +Test-FormSubmission -Url "$BaseUrl/addsubnetbackenddirect.asp" -TestName "Create subnet" -FormData $subnetData -ExpectedRedirect "displaysubnets" -RecordType "Subnets" -RecordIdentifier "192.168.99.0" + +Write-Host "" + +# ---------------------------------------------------------------------------- +# 7. APPLICATION FORM TESTS +# ---------------------------------------------------------------------------- +Write-Host "--- APPLICATION FORM TESTS ---" -ForegroundColor Yellow + +$applicationData = @{ + applicationname = "$TestPrefix-App001" + appdescription = "Test application created by automated test" + supportteamid = "1" + appownerid = "" + skilllevelid = "" +} +Test-FormSubmission -Url "$BaseUrl/saveapplicationdirect.asp" -TestName "Create application" -FormData $applicationData -ExpectedRedirect "displayapplications|displayapplication" -RecordType "Applications" -RecordIdentifier "$TestPrefix-App001" + +Write-Host "" + +# ---------------------------------------------------------------------------- +# 8. KNOWLEDGE BASE FORM TESTS +# ---------------------------------------------------------------------------- +Write-Host "--- KNOWLEDGE BASE FORM TESTS ---" -ForegroundColor Yellow + +$kbData = @{ + shortdescription = "$TestPrefix - Test KB Article" + applicationid = "1" + keywords = "test automated testing" + linkurl = "https://example.com/test" + topicid = "1" +} +Test-FormSubmission -Url "$BaseUrl/addlinkdirect.asp" -TestName "Create KB article" -FormData $kbData -ExpectedRedirect "displayknowledgebase|displayknowledgearticle" -RecordType "KBArticles" -RecordIdentifier "$TestPrefix - Test KB Article" + +Write-Host "" + +# ---------------------------------------------------------------------------- +# 9. VENDOR FORM TESTS +# ---------------------------------------------------------------------------- +Write-Host "--- VENDOR FORM TESTS ---" -ForegroundColor Yellow + +$vendorData = @{ + vendor = "$TestPrefix-Vendor001" +} +Test-FormSubmission -Url "$BaseUrl/savevendordirect.asp" -TestName "Create vendor" -FormData $vendorData -ExpectedRedirect "addmodel|displayvendors" -RecordType "Vendors" -RecordIdentifier "$TestPrefix-Vendor001" + +Write-Host "" + +# ---------------------------------------------------------------------------- +# 10. MODEL FORM TESTS +# ---------------------------------------------------------------------------- +Write-Host "--- MODEL FORM TESTS ---" -ForegroundColor Yellow + +$modelData = @{ + modelnumber = "$TestPrefix-Model001" + vendorid = "1" + machinetypeid = "1" + notes = "Test model created by automated test" +} +Test-FormSubmission -Url "$BaseUrl/savemodeldirect.asp" -TestName "Create model" -FormData $modelData -ExpectedRedirect "addmachine|displaymodels" -RecordType "Models" -RecordIdentifier "$TestPrefix-Model001" + +Write-Host "" + +# ---------------------------------------------------------------------------- +# 11. NETWORK DEVICE FORM TESTS +# ---------------------------------------------------------------------------- +Write-Host "--- NETWORK DEVICE FORM TESTS ---" -ForegroundColor Yellow + +# Access Point +$apData = @{ + type = "new" + apname = "$TestPrefix-AP001" + serialnumber = "TESTAP123456" + ipaddress = "192.168.99.50" + fqdn = "testap001.test.local" + modelid = "" + description = "Test access point" isactive = "1" - isshopfloor = "0" + mapleft = "" + maptop = "" } -Test-FormSubmission -Url "$BaseUrl/savenotification_direct.asp" -TestName "Create notification (indefinite)" -FormData $notificationIndefinite -ExpectedRedirect "displaynotifications" +Test-FormSubmission -Url "$BaseUrl/deviceaccesspoint.asp" -TestName "Create access point" -FormData $apData -ExpectedRedirect "network_devices|deviceaccesspoint" -RecordType "NetworkDevices" -RecordIdentifier "$TestPrefix-AP001" + +# Switch +$switchData = @{ + type = "new" + alias = "$TestPrefix-SW001" + serialnumber = "TESTSW123456" + ipaddress = "192.168.99.51" + fqdn = "testsw001.test.local" + modelid = "" + machinenotes = "Test switch" + isactive = "1" + mapleft = "" + maptop = "" +} +Test-FormSubmission -Url "$BaseUrl/deviceswitch.asp" -TestName "Create switch" -FormData $switchData -ExpectedRedirect "network_devices|deviceswitch" -RecordType "NetworkDevices" -RecordIdentifier "$TestPrefix-SW001" + +# Camera +$cameraData = @{ + type = "new" + alias = "$TestPrefix-CAM001" + serialnumber = "TESTCAM123456" + ipaddress = "192.168.99.52" + fqdn = "testcam001.test.local" + modelid = "" + machinenotes = "Test camera" + isactive = "1" + mapleft = "" + maptop = "" +} +Test-FormSubmission -Url "$BaseUrl/devicecamera.asp" -TestName "Create camera" -FormData $cameraData -ExpectedRedirect "network_devices|devicecamera" -RecordType "NetworkDevices" -RecordIdentifier "$TestPrefix-CAM001" + +# IDF +$idfData = @{ + type = "new" + alias = "$TestPrefix-IDF001" + serialnumber = "TESTIDF123456" + ipaddress = "192.168.99.53" + fqdn = "testidf001.test.local" + modelid = "" + machinenotes = "Test IDF" + isactive = "1" + mapleft = "" + maptop = "" +} +Test-FormSubmission -Url "$BaseUrl/deviceidf.asp" -TestName "Create IDF" -FormData $idfData -ExpectedRedirect "network_devices|deviceidf" -RecordType "NetworkDevices" -RecordIdentifier "$TestPrefix-IDF001" + +# Server +$serverData = @{ + type = "new" + alias = "$TestPrefix-SRV001" + serialnumber = "TESTSRV123456" + fqdn = "testsrv001.test.local" + modelid = "" + machinenotes = "Test server" + isactive = "1" + mapleft = "" + maptop = "" +} +Test-FormSubmission -Url "$BaseUrl/deviceserver.asp" -TestName "Create server" -FormData $serverData -ExpectedRedirect "network_devices|deviceserver" -RecordType "NetworkDevices" -RecordIdentifier "$TestPrefix-SRV001" Write-Host "" # ---------------------------------------------------------------------------- -# 3. API ENDPOINT TESTS -# ---------------------------------------------------------------------------- -Write-Host "--- API ENDPOINT TESTS ---" -ForegroundColor Yellow - -# Test: API health check -Test-PageLoads -Url "$BaseUrl/api.asp?action=getDashboardData" -TestName "API getDashboardData" -ExpectedContent "success" - -# Test: API with POST data (updateCompleteAsset simulation) -$apiTestData = @{ - action = "updateCompleteAsset" - hostname = "TEST-PC-001" - serialNumber = "TESTSERIAL123" - model = "Test Model" - manufacturer = "Test Manufacturer" - macAddress = "00:11:22:33:44:55" - ipAddress = "192.168.1.100" - osVersion = "Windows 11 Pro" - lastUser = "testuser" - pcType = "Shopfloor" -} -# Note: This may fail if PC doesn't exist - that's expected -Test-FormSubmission -Url "$BaseUrl/api.asp" -TestName "API updateCompleteAsset" -FormData $apiTestData -ExpectedContent "success|error" - -Write-Host "" - -# ---------------------------------------------------------------------------- -# 4. EDIT FORM TESTS (requires existing records) +# 12. EDIT FORM TESTS # ---------------------------------------------------------------------------- Write-Host "--- EDIT FORM TESTS ---" -ForegroundColor Yellow -# Get a notification ID to test editing -try { - $notifPage = Invoke-WebRequest -Uri "$BaseUrl/displaynotifications.asp" -UseBasicParsing - if ($notifPage.Content -match 'editnotification\.asp\?notificationid=(\d+)') { - $testNotifId = $Matches[1] - - Test-PageLoads -Url "$BaseUrl/editnotification.asp?notificationid=$testNotifId" -TestName "Edit notification form loads" -ExpectedContent "Edit Notification" - - # Test updating a notification - $updateData = @{ - notificationid = $testNotifId - notification = "Updated by test script - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" - notificationtypeid = "2" - businessunitid = "" - appid = "" - ticketnumber = "GETEST-UPDATE" - starttime = (Get-Date).ToString("yyyy-MM-ddTHH:mm") - endtime = (Get-Date).AddDays(1).ToString("yyyy-MM-ddTHH:mm") - isactive = "1" - isactive_submitted = "1" - isshopfloor = "0" - isshopfloor_submitted = "1" - } - Test-FormSubmission -Url "$BaseUrl/updatenotification_direct.asp" -TestName "Update notification" -FormData $updateData -ExpectedRedirect "displaynotifications" - } - else { - Write-TestResult -TestName "Edit notification form loads" -Passed $false -Message "No notifications found to test" - } +# Test editing a notification +$testNotifId = Get-ExistingId -PageUrl "$BaseUrl/displaynotifications.asp" -Pattern 'editnotification\.asp\?notificationid=(\d+)' +if ($testNotifId) { + Test-PageLoads -Url "$BaseUrl/editnotification.asp?notificationid=$testNotifId" -TestName "Edit notification form loads" +} else { + Write-TestResult -TestName "Edit notification form loads" -Passed $false -Message "No notifications found" } -catch { - Write-TestResult -TestName "Edit notification tests" -Passed $false -Message $_.Exception.Message + +# Test editing equipment +$testMachineId = Get-ExistingId -PageUrl "$BaseUrl/displaymachines.asp" -Pattern 'editmachine\.asp\?machineid=(\d+)' +if ($testMachineId) { + Test-PageLoads -Url "$BaseUrl/editmachine.asp?machineid=$testMachineId" -TestName "Edit equipment form loads" +} else { + Write-TestResult -TestName "Edit equipment form loads" -Passed $false -Message "No equipment found" +} + +# Test editing PC +$testPCId = Get-ExistingId -PageUrl "$BaseUrl/displaypcs.asp" -Pattern 'editpc\.asp\?machineid=(\d+)' +if ($testPCId) { + Test-PageLoads -Url "$BaseUrl/editpc.asp?machineid=$testPCId" -TestName "Edit PC form loads" +} else { + Write-TestResult -TestName "Edit PC form loads" -Passed $false -Message "No PCs found" +} + +# Test editing printer +$testPrinterId = Get-ExistingId -PageUrl "$BaseUrl/displayprinters.asp" -Pattern 'editprinter\.asp\?printerid=(\d+)' +if ($testPrinterId) { + Test-PageLoads -Url "$BaseUrl/editprinter.asp?printerid=$testPrinterId" -TestName "Edit printer form loads" +} else { + Write-TestResult -TestName "Edit printer form loads" -Passed $false -Message "No printers found" +} + +# Test editing KB article +$testLinkId = Get-ExistingId -PageUrl "$BaseUrl/displayknowledgebase.asp" -Pattern 'editlink\.asp\?linkid=(\d+)' +if ($testLinkId) { + Test-PageLoads -Url "$BaseUrl/editlink.asp?linkid=$testLinkId" -TestName "Edit KB article form loads" +} else { + Write-TestResult -TestName "Edit KB article form loads" -Passed $false -Message "No KB articles found" } Write-Host "" # ---------------------------------------------------------------------------- -# 5. VALIDATION TESTS (should fail gracefully) +# 13. API ENDPOINT TESTS +# ---------------------------------------------------------------------------- +Write-Host "--- API ENDPOINT TESTS ---" -ForegroundColor Yellow + +Test-PageLoads -Url "$BaseUrl/api.asp?action=getDashboardData" -TestName "API getDashboardData" -ExpectedContent "success" +Test-PageLoads -Url "$BaseUrl/api.asp?action=getShopfloorPCs" -TestName "API getShopfloorPCs" -ExpectedContent "success" +Test-PageLoads -Url "$BaseUrl/apiprinters.asp" -TestName "API printers" -ExpectedContent "printerid" +Test-PageLoads -Url "$BaseUrl/apibusinessunits.asp" -TestName "API business units" -ExpectedContent "businessunit" + +# Test API POST +$apiTestData = @{ + action = "updateCompleteAsset" + hostname = "$TestPrefix-PC001" + serialNumber = "TESTSERIAL123" + model = "Test Model" + manufacturer = "Dell" + osVersion = "Windows 11 Pro" + pcType = "Standard" +} +Test-FormSubmission -Url "$BaseUrl/api.asp" -TestName "API updateCompleteAsset" -FormData $apiTestData -ExpectedContent "success" + +Write-Host "" + +# ---------------------------------------------------------------------------- +# 14. VALIDATION TESTS # ---------------------------------------------------------------------------- Write-Host "--- VALIDATION TESTS ---" -ForegroundColor Yellow -# Test: Missing required fields +# Test missing required fields on notification $invalidNotification = @{ - notification = "" # Required field empty + notification = "" notificationtypeid = "1" - starttime = "" # Required field empty + starttime = "" } -# This should NOT redirect to displaynotifications, it should show an error or stay on form try { - $response = Invoke-WebRequest -Uri "$BaseUrl/savenotification_direct.asp" -Method POST -Body $invalidNotification -UseBasicParsing -MaximumRedirection 0 -ErrorAction SilentlyContinue - $passed = $response.Content -match "Required|missing|error" -or $response.StatusCode -eq 200 - Write-TestResult -TestName "Reject empty required fields" -Passed $passed -Message "Response handled gracefully" + $response = Invoke-WebRequest -Uri "$BaseUrl/savenotificationdirect.asp" -Method POST -Body $invalidNotification -UseBasicParsing -MaximumRedirection 0 -ErrorAction SilentlyContinue + $passed = $response.Content -match "Required|missing|error" -or $response.StatusCode -ne 302 + Write-TestResult -TestName "Reject empty notification" -Passed $passed -Message "Handled gracefully" } catch { - # If it redirects anyway, that's a problem if ($_.Exception.Message -match "302") { - Write-TestResult -TestName "Reject empty required fields" -Passed $false -Message "Should not redirect with missing required fields" - } - else { - Write-TestResult -TestName "Reject empty required fields" -Passed $true -Message "Validation working" + Write-TestResult -TestName "Reject empty notification" -Passed $false -Message "Should not redirect with missing fields" + } else { + Write-TestResult -TestName "Reject empty notification" -Passed $true -Message "Validation working" } } Write-Host "" +} # End if not CleanupOnly + +# ============================================================================ +# CLEANUP +# ============================================================================ + +if (-not $SkipCleanup) { + Write-Host "--- CLEANUP ---" -ForegroundColor Yellow + Write-Host "Cleaning up test records with prefix: $TestPrefix" -ForegroundColor Gray + Write-Host "" + Write-Host "NOTE: Cleanup requires direct database access." -ForegroundColor Magenta + Write-Host "Run the following SQL to clean up test data:" -ForegroundColor Magenta + Write-Host "" + Write-Host "-- Cleanup SQL for test run: $TestPrefix" -ForegroundColor DarkGray + Write-Host "DELETE FROM notifications WHERE notification LIKE '$TestPrefix%';" -ForegroundColor DarkGray + Write-Host "DELETE FROM machines WHERE machinenumber LIKE '$TestPrefix%' OR alias LIKE '$TestPrefix%';" -ForegroundColor DarkGray + Write-Host "DELETE FROM printers WHERE printercsfname LIKE '$TestPrefix%';" -ForegroundColor DarkGray + Write-Host "DELETE FROM subnets WHERE description LIKE '$TestPrefix%';" -ForegroundColor DarkGray + Write-Host "DELETE FROM applications WHERE applicationname LIKE '$TestPrefix%';" -ForegroundColor DarkGray + Write-Host "DELETE FROM knowledgebase WHERE shortdescription LIKE '$TestPrefix%';" -ForegroundColor DarkGray + Write-Host "DELETE FROM vendors WHERE vendor LIKE '$TestPrefix%';" -ForegroundColor DarkGray + Write-Host "DELETE FROM models WHERE modelnumber LIKE '$TestPrefix%';" -ForegroundColor DarkGray + Write-Host "" +} + # ============================================================================ # SUMMARY # ============================================================================ + +if (-not $CleanupOnly) { + Write-Host "============================================" -ForegroundColor Cyan Write-Host "TEST SUMMARY" -ForegroundColor Cyan Write-Host "============================================" -ForegroundColor Cyan @@ -307,7 +599,16 @@ if ($failed -gt 0) { $TestResults | Where-Object { $_.Status -eq "FAIL" } | ForEach-Object { Write-Host " - $($_.Test): $($_.Message)" -ForegroundColor Red } + Write-Host "" } +# Export results to file +$resultsFile = "test_results_$TestTimestamp.csv" +$TestResults | Export-Csv -Path $resultsFile -NoTypeInformation +Write-Host "Results exported to: $resultsFile" -ForegroundColor Gray + Write-Host "" Write-Host "Testing completed at $(Get-Date)" -ForegroundColor Cyan +Write-Host "Test prefix for cleanup: $TestPrefix" -ForegroundColor Yellow + +} diff --git a/tests/test_forms.sh b/tests/test_forms.sh deleted file mode 100755 index 3a6f791..0000000 --- a/tests/test_forms.sh +++ /dev/null @@ -1,307 +0,0 @@ -#!/bin/bash -# ============================================================================ -# ShopDB Form Testing Script (Bash/curl version) -# ============================================================================ -# Tests form submissions across key pages to verify no errors occur -# Run from Linux: ./test_forms.sh -# ============================================================================ - -BASE_URL="${1:-http://192.168.122.151:8080}" -PASSED=0 -FAILED=0 -TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') - -# Colors -RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -CYAN='\033[0;36m' -NC='\033[0m' # No Color - -echo "" -echo -e "${CYAN}============================================${NC}" -echo -e "${CYAN}ShopDB Form Testing - $TIMESTAMP${NC}" -echo -e "${CYAN}Base URL: $BASE_URL${NC}" -echo -e "${CYAN}============================================${NC}" -echo "" - -# ---------------------------------------------------------------------------- -# Test Functions -# ---------------------------------------------------------------------------- - -test_page_loads() { - local url="$1" - local test_name="$2" - local expected="${3:-}" - - response=$(curl -s -w "\n%{http_code}" "$url" 2>/dev/null) - http_code=$(echo "$response" | tail -n1) - body=$(echo "$response" | sed '$d') - - # Check for HTTP 200 - if [ "$http_code" != "200" ]; then - echo -e "[${RED}FAIL${NC}] $test_name - HTTP $http_code" - ((FAILED++)) - return 1 - fi - - # Check for ASP errors in body - if echo "$body" | grep -qi "Microsoft VBScript\|Error 500\|Internal server error"; then - echo -e "[${RED}FAIL${NC}] $test_name - Contains server error" - ((FAILED++)) - return 1 - fi - - # Check for expected content if specified - if [ -n "$expected" ]; then - if ! echo "$body" | grep -qi "$expected"; then - echo -e "[${RED}FAIL${NC}] $test_name - Missing expected content: $expected" - ((FAILED++)) - return 1 - fi - fi - - echo -e "[${GREEN}PASS${NC}] $test_name" - ((PASSED++)) - return 0 -} - -test_form_submit() { - local url="$1" - local test_name="$2" - local data="$3" - local expect_redirect="${4:-displaynotifications}" - - # Submit form and capture response - response=$(curl -s -w "\n%{http_code}" -X POST -d "$data" -L "$url" 2>/dev/null) - http_code=$(echo "$response" | tail -n1) - body=$(echo "$response" | sed '$d') - - # Check for HTTP 200 (after redirects) - if [ "$http_code" != "200" ]; then - echo -e "[${RED}FAIL${NC}] $test_name - HTTP $http_code" - ((FAILED++)) - return 1 - fi - - # Check for ASP errors in body - if echo "$body" | grep -qi "Microsoft VBScript\|Error 500\|Internal server error"; then - echo -e "[${RED}FAIL${NC}] $test_name - Contains server error" - ((FAILED++)) - return 1 - fi - - # For form submissions, we typically get redirected back to a list page - # Check that we're on the expected page - if [ -n "$expect_redirect" ]; then - if ! echo "$body" | grep -qi "$expect_redirect\|Notification\|success"; then - echo -e "[${YELLOW}WARN${NC}] $test_name - May not have redirected properly" - fi - fi - - echo -e "[${GREEN}PASS${NC}] $test_name" - ((PASSED++)) - return 0 -} - -test_form_submit_no_redirect() { - local url="$1" - local test_name="$2" - local data="$3" - - # Submit form without following redirects - response=$(curl -s -w "\n%{http_code}" -X POST -d "$data" "$url" 2>/dev/null) - http_code=$(echo "$response" | tail -n1) - body=$(echo "$response" | sed '$d') - - # 302 redirect means success for most form submissions - if [ "$http_code" = "302" ]; then - echo -e "[${GREEN}PASS${NC}] $test_name (redirected)" - ((PASSED++)) - return 0 - fi - - # 200 might be OK if it contains success or validation message - if [ "$http_code" = "200" ]; then - if echo "$body" | grep -qi "Microsoft VBScript\|Error 500\|Internal server error"; then - echo -e "[${RED}FAIL${NC}] $test_name - Server error" - ((FAILED++)) - return 1 - fi - echo -e "[${GREEN}PASS${NC}] $test_name (200 OK)" - ((PASSED++)) - return 0 - fi - - echo -e "[${RED}FAIL${NC}] $test_name - HTTP $http_code" - ((FAILED++)) - return 1 -} - -# ---------------------------------------------------------------------------- -# 1. PAGE LOAD TESTS -# ---------------------------------------------------------------------------- -echo -e "${YELLOW}--- PAGE LOAD TESTS ---${NC}" - -test_page_loads "$BASE_URL/default.asp" "Dashboard loads" "Dashboard" -test_page_loads "$BASE_URL/displaynotifications.asp" "Notifications list loads" "Notification" -test_page_loads "$BASE_URL/addnotification.asp" "Add notification form loads" "Add Notification" -test_page_loads "$BASE_URL/displayapplications.asp" "Applications list loads" "Application" -test_page_loads "$BASE_URL/displayprinters.asp" "Printers list loads" "Printer" -test_page_loads "$BASE_URL/displaypcs.asp" "PCs list loads" -test_page_loads "$BASE_URL/displaymachines.asp" "Machines list loads" "Machine" -test_page_loads "$BASE_URL/network_devices.asp" "Network devices loads" "Network" -test_page_loads "$BASE_URL/displayinstalledapps.asp?appid=1" "Installed apps loads" - -echo "" - -# ---------------------------------------------------------------------------- -# 2. NOTIFICATION FORM TESTS -# ---------------------------------------------------------------------------- -echo -e "${YELLOW}--- NOTIFICATION FORM TESTS ---${NC}" - -NOW=$(date '+%Y-%m-%dT%H:%M') -TOMORROW=$(date -d '+1 day' '+%Y-%m-%dT%H:%M') -HOUR_LATER=$(date -d '+1 hour' '+%Y-%m-%dT%H:%M') - -# Test: Create notification with basic fields -test_form_submit_no_redirect \ - "$BASE_URL/savenotification_direct.asp" \ - "Create notification (basic)" \ - "notification=Test+from+bash+script+-+$TIMESTAMP¬ificationtypeid=2&businessunitid=&appid=&ticketnumber=GETEST001&starttime=$NOW&endtime=$TOMORROW&isactive=1&isshopfloor=0" - -# Test: Create notification with application linked -test_form_submit_no_redirect \ - "$BASE_URL/savenotification_direct.asp" \ - "Create notification (with app)" \ - "notification=Test+with+app+-+$TIMESTAMP¬ificationtypeid=3&businessunitid=2&appid=6&ticketnumber=GECHG002&starttime=$NOW&endtime=$HOUR_LATER&isactive=1&isshopfloor=1" - -# Test: Create notification without end time (indefinite) -test_form_submit_no_redirect \ - "$BASE_URL/savenotification_direct.asp" \ - "Create notification (indefinite)" \ - "notification=Indefinite+test+-+$TIMESTAMP¬ificationtypeid=4&businessunitid=&appid=&ticketnumber=&starttime=$NOW&endtime=&isactive=1&isshopfloor=0" - -# Test: Create notification with all fields filled -test_form_submit_no_redirect \ - "$BASE_URL/savenotification_direct.asp" \ - "Create notification (all fields)" \ - "notification=Full+test+-+$TIMESTAMP¬ificationtypeid=1&businessunitid=3&appid=21&ticketnumber=GETEST003&starttime=$NOW&endtime=$TOMORROW&isactive=1&isshopfloor=1" - -echo "" - -# ---------------------------------------------------------------------------- -# 3. EDIT NOTIFICATION TESTS -# ---------------------------------------------------------------------------- -echo -e "${YELLOW}--- EDIT NOTIFICATION TESTS ---${NC}" - -# Get a notification ID from the list page -NOTIF_ID=$(curl -s "$BASE_URL/displaynotifications.asp" | grep -oP 'editnotification\.asp\?notificationid=\K\d+' | head -1) - -if [ -n "$NOTIF_ID" ]; then - test_page_loads "$BASE_URL/editnotification.asp?notificationid=$NOTIF_ID" "Edit notification form loads" "Edit Notification" - - # Test updating the notification - test_form_submit_no_redirect \ - "$BASE_URL/updatenotification_direct.asp" \ - "Update notification" \ - "notificationid=$NOTIF_ID¬ification=Updated+by+test+-+$TIMESTAMP¬ificationtypeid=2&businessunitid=&appid=&ticketnumber=GEUPDATE&starttime=$NOW&endtime=$TOMORROW&isactive=1&isactive_submitted=1&isshopfloor=0&isshopfloor_submitted=1" -else - echo -e "[${YELLOW}SKIP${NC}] Edit notification tests - No notifications found" -fi - -echo "" - -# ---------------------------------------------------------------------------- -# 4. API ENDPOINT TESTS -# ---------------------------------------------------------------------------- -echo -e "${YELLOW}--- API ENDPOINT TESTS ---${NC}" - -test_page_loads "$BASE_URL/api.asp?action=getDashboardData" "API getDashboardData" "success" - -# Test API with POST -api_response=$(curl -s -X POST -d "action=getDashboardData" "$BASE_URL/api.asp") -if echo "$api_response" | grep -qi "success"; then - echo -e "[${GREEN}PASS${NC}] API POST getDashboardData" - ((PASSED++)) -else - echo -e "[${RED}FAIL${NC}] API POST getDashboardData" - ((FAILED++)) -fi - -echo "" - -# ---------------------------------------------------------------------------- -# 5. VALIDATION TESTS -# ---------------------------------------------------------------------------- -echo -e "${YELLOW}--- VALIDATION TESTS ---${NC}" - -# Test: Submit with missing required fields (should NOT create notification) -response=$(curl -s -w "\n%{http_code}" -X POST \ - -d "notification=¬ificationtypeid=1&starttime=" \ - "$BASE_URL/savenotification_direct.asp" 2>/dev/null) -http_code=$(echo "$response" | tail -n1) -body=$(echo "$response" | sed '$d') - -if [ "$http_code" = "200" ] && echo "$body" | grep -qi "required\|missing"; then - echo -e "[${GREEN}PASS${NC}] Validation - rejects empty required fields" - ((PASSED++)) -elif [ "$http_code" = "302" ]; then - echo -e "[${YELLOW}WARN${NC}] Validation - accepted empty fields (may need better validation)" - ((PASSED++)) -else - echo -e "[${GREEN}PASS${NC}] Validation - handled gracefully" - ((PASSED++)) -fi - -echo "" - -# ---------------------------------------------------------------------------- -# 6. SPECIAL CHARACTER TESTS -# ---------------------------------------------------------------------------- -echo -e "${YELLOW}--- SPECIAL CHARACTER TESTS ---${NC}" - -# Test: Notification with special characters (XSS test) -SPECIAL_MSG="Test+%3Cscript%3Ealert%28%27xss%27%29%3C%2Fscript%3E+and+%26+symbols" -test_form_submit_no_redirect \ - "$BASE_URL/savenotification_direct.asp" \ - "Create notification (special chars)" \ - "notification=$SPECIAL_MSG¬ificationtypeid=2&businessunitid=&appid=&ticketnumber=&starttime=$NOW&endtime=$TOMORROW&isactive=1&isshopfloor=0" - -# Verify the special characters are escaped in output -LATEST_PAGE=$(curl -s "$BASE_URL/displaynotifications.asp") -if echo "$LATEST_PAGE" | grep -q " - -