Skip to content

Fix key validation and align failing test expectations#3

Open
Cadtastic wants to merge 1 commit into
fix/mongodb-driver-security-updatefrom
fix/keyhelper-validation
Open

Fix key validation and align failing test expectations#3
Cadtastic wants to merge 1 commit into
fix/mongodb-driver-security-updatefrom
fix/keyhelper-validation

Conversation

@Cadtastic
Copy link
Copy Markdown
Contributor

Stacked on top of #2 (Mongo driver fix) — master can't restore without that PR, so this branch targets fix/mongodb-driver-security-update instead of master. Rebase onto master after #2 merges.

Summary

  • KeyHelper.ValidateKey was broken. The check if (string.IsNullOrWhiteSpace(key)) { ArgumentNullException.ThrowIfNull(key, ...); } only ever threw when key was null — empty and whitespace keys passed validation silently. Replaced with ArgumentNullException.ThrowIfNull(key) followed by an explicit IsNullOrWhiteSpace check that throws ArgumentException with nameof(key). XML doc now lists both exception types.
  • Tests in StateStoreTests and NullReferenceTests disagreed on what should be thrown for a null key (ArgumentException vs. ArgumentNullException). xUnit Throws<T> requires exact match, so they couldn't both pass. Resolved by following the .NET BCL convention: ArgumentNullException for null, ArgumentException for empty/whitespace. The two affected StateStoreTests cases were renamed and switched to expect ArgumentNullException.
  • NullReferenceTests.UpsertAsync_NullKey_ThrowsAsync was named "NullKey" but passed "". Corrected to pass null! so input matches intent.
  • EdgeCaseTests.Get_NonExistentKey_ThrowsAsync asserted KeyNotFoundException that StateStoreImplementation.GetAsync has never thrown — missing keys return default, as GetAsync_ReturnsDefault_WhenKeyDoesNotExist_Async already verifies. Renamed to Get_NonExistentKey_ReturnsDefault_Async and rewritten to assert default(int) == 0, preserving the value-type-specific edge case.

Test plan

  • dotnet test72/72 passing on net10.0 (was 66/72 before this PR; the 6 failures listed above are fixed).
  • dotnet build — 0 warnings, 0 errors across net8.0 / net9.0 / net10.0.

🤖 Generated with Claude Code

KeyHelper.ValidateKey wrapped ArgumentNullException.ThrowIfNull inside an
IsNullOrWhiteSpace check, so empty and whitespace keys were silently
accepted while null keys threw the wrong exception type. Split the
validation: null now throws ArgumentNullException and empty/whitespace
throws ArgumentException with nameof(key) -- matching the .NET BCL
convention -- and the XML documentation now lists both exception types.

Test fixes:
- StateStoreTests: two null-key cases now expect ArgumentNullException
  (and are renamed to reflect that), matching NullReferenceTests and the
  new contract.
- NullReferenceTests.UpsertAsync_NullKey_ThrowsAsync passed "" instead of
  null despite its name; corrected to null! so the assertion matches the
  test's intent.
- EdgeCaseTests.Get_NonExistentKey_ThrowsAsync asserted a KeyNotFoundException
  that StateStoreImplementation.GetAsync has never thrown -- missing keys
  return default, as verified by GetAsync_ReturnsDefault_WhenKeyDoesNotExist.
  Renamed and rewritten to verify default(int) is returned, preserving the
  value-type-specific coverage.

All 72 tests pass on net10.0.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant