From 9e875a097737fd10a4b8c8b50ff5811b2f6f5ad1 Mon Sep 17 00:00:00 2001 From: James Anderson Date: Fri, 24 Oct 2025 11:43:01 +0100 Subject: [PATCH 1/4] Fix request object for new sessions to handle the RemberMe value correctly --- NctApiClientLibrary/Classes/NctSessionManager.psm1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NctApiClientLibrary/Classes/NctSessionManager.psm1 b/NctApiClientLibrary/Classes/NctSessionManager.psm1 index 4f7478c..91d06d6 100644 --- a/NctApiClientLibrary/Classes/NctSessionManager.psm1 +++ b/NctApiClientLibrary/Classes/NctSessionManager.psm1 @@ -16,7 +16,7 @@ class NctSessionManager { [string]$Username [Microsoft.PowerShell.Commands.WebRequestSession]$Session [datetime]$SessionCreatedTime - [int]$SessionTimeoutMinutes = 10 + [int]$SessionTimeoutMinutes = 1 [bool]$SkipCertificateCheck # Constructor @@ -53,7 +53,7 @@ class NctSessionManager { $body = @{ "UserName" = $credentials.username "Password" = $credentials.Password - "RememberMe" = false + "RememberMe" = "false" "Meta" = @{ "OneTimePassword" = $oneTimePassword } From e46d59345861bf26ac26b0f96c555bdb821c7b93 Mon Sep 17 00:00:00 2001 From: James Anderson Date: Tue, 28 Oct 2025 10:52:43 +0000 Subject: [PATCH 2/4] Add CI script to README for tests --- NctApiClientLibrary/Tests/README.md | 60 ++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/NctApiClientLibrary/Tests/README.md b/NctApiClientLibrary/Tests/README.md index 7e5eaa8..7a33d5d 100644 --- a/NctApiClientLibrary/Tests/README.md +++ b/NctApiClientLibrary/Tests/README.md @@ -7,25 +7,65 @@ Pester is the default test framework for PowerShell. See the docs for details, b ### Authentication -A valid session is required for the tests to exectute successfully. You'll almost certainly want to create a persisted credential. See the README in the root of the module folder for more details on authenticating. +A valid session is required for the tests to exectute successfully. -When running locally you may be using a self signed certificate for your Hub. In this case you will want pass the `-SkipCertificateCheck` argument. +#### Local Testing +When testing locally, you'll almost certainly want to create a persisted credential. See the README in the root of the module folder for more details on authenticating. +You may also be using a self signed certificate for your Hub. In this case you will want pass the `-SkipCertificateCheck` argument. ```powershell New-NctSession -url "https://localhost/api" -user "admin" -SkipCertificateCheck ``` +#### Automated testing +For automated builds and CI runs pass a credential from a password store into the script below to ensure the test run is able to initialise sessions. + +```powershell +# Create a directory to store the encrypted password file in +$path = "$env:USERPROFILE\.nct client library" +New-Item -Path $path -ItemType Directory | Out-Null + +# File path to store the encrypted password +$file = "$path\$user.dat" + +# Get password +# This could be a pipeline variable passed in as a secure string or it could be obtained from a secret store. +# Example using Azure Key Vault: +$password = Get-AzKeyVaultSecret -VaultName "" -Name "MyPassword" + +# Convert to byte array +$passwordBytes = [System.Text.Encoding]::UTF8.GetBytes([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))) + +Remove-Variable -Name password + +# Encrypt using DPAPI +$encryptedBytes = Protect-Credential -Data $passwordBytes -Action Encrypt + +Remove-Variable -Name passwordBytes + +# Save encrypted bytes to file +[System.IO.File]::WriteAllBytes($file, $encryptedBytes) + +# Create session using the stored credential +New-NctSession -url $NctHubUrl -user $user -SkipCertificateCheck +``` + +The function `New-NctSession` will look in the `$env:USERPROFILE\.nct client library` directory for a .dat file who's name matches the value passed to the user parameter. +If it finds a matching file it will decrypt the file and use the password when initialising a session to the API. + ### Execution -To run a suite of tests just execute the file or files in this folder when a valid session is in place. +To run a suite of tests just execute the files in this folder with a valid session in place. -Example output: +Naviage to the Tests directory and invoke all tests with `Invoke-Pester`. -```powershell -Starting discovery in 1 files. -Discovery found 4 tests in 150ms. +Example output: +``` +Starting discovery in 2 files. +Discovery found 8 tests in 10.51s. Running tests. -[+] C:\Users\james\Desktop\NctApiClientLibrary\Tests\DevicesTests.ps1 2.89s (2.59s|157ms) -Tests completed in 2.9s -Tests Passed: 4, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0 +[+] C:\dev\change-tracker\NctApiClientLibrary\Tests\Credentials.Tests.ps1 9.66s (1.78s|327ms) +[+] C:\dev\change-tracker\NctApiClientLibrary\Tests\Devices.Tests.ps1 7.21s (4.21s|64ms) +Tests completed in 16.88s +Tests Passed: 8, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0 ``` \ No newline at end of file From 420cbfffbda1f98c2aaeab41a62b854ed5dcf149 Mon Sep 17 00:00:00 2001 From: James Anderson Date: Tue, 28 Oct 2025 10:58:52 +0000 Subject: [PATCH 3/4] Fix session timeout --- NctApiClientLibrary/Classes/NctSessionManager.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NctApiClientLibrary/Classes/NctSessionManager.psm1 b/NctApiClientLibrary/Classes/NctSessionManager.psm1 index 91d06d6..349319c 100644 --- a/NctApiClientLibrary/Classes/NctSessionManager.psm1 +++ b/NctApiClientLibrary/Classes/NctSessionManager.psm1 @@ -16,7 +16,7 @@ class NctSessionManager { [string]$Username [Microsoft.PowerShell.Commands.WebRequestSession]$Session [datetime]$SessionCreatedTime - [int]$SessionTimeoutMinutes = 1 + [int]$SessionTimeoutMinutes = 10 [bool]$SkipCertificateCheck # Constructor From acb92af2cb1a88c84c3daa503fc4711079b3d503 Mon Sep 17 00:00:00 2001 From: James Anderson Date: Tue, 28 Oct 2025 11:01:35 +0000 Subject: [PATCH 4/4] Fix CI script in README for tests --- NctApiClientLibrary/Tests/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NctApiClientLibrary/Tests/README.md b/NctApiClientLibrary/Tests/README.md index 7a33d5d..0888cac 100644 --- a/NctApiClientLibrary/Tests/README.md +++ b/NctApiClientLibrary/Tests/README.md @@ -21,6 +21,9 @@ New-NctSession -url "https://localhost/api" -user "admin" -SkipCertificateCheck For automated builds and CI runs pass a credential from a password store into the script below to ensure the test run is able to initialise sessions. ```powershell +# Pass the username in from a pipeline variable +$user = "MyUsername" + # Create a directory to store the encrypted password file in $path = "$env:USERPROFILE\.nct client library" New-Item -Path $path -ItemType Directory | Out-Null