Skip to content

Conversation

@JanZachmann
Copy link
Contributor

Summary

Status: Work in Progress - PR #3 of 14 in the test coverage improvement plan
Depends on: #70 (test infrastructure)

Add comprehensive unit tests for TokenManager service covering token creation, verification, and edge cases.

Changes

TokenManager Tests (6 new tests, 9 total)

Existing tests (3):

  • test_create_and_verify_token
  • test_verify_invalid_token
  • test_verify_token_wrong_secret

New tests (6):

  • test_token_contains_required_subject - Validates subject claim is correct
  • test_token_has_expiration - Validates expiration timestamp is set
  • test_token_manager_is_clonable - Validates Arc-based sharing works across clones
  • test_multiple_tokens_from_same_manager - Validates multiple token creation
  • test_verify_malformed_token - Tests rejection of various malformed formats
  • test_token_format - Validates JWT structure (header.payload.signature)

Coverage

Tests validate all TokenManager functionality:

  • ✅ Token creation with correct claims
  • ✅ Token verification (signature, expiration, subject)
  • ✅ Invalid token rejection (empty, malformed, wrong secret)
  • ✅ Token structure validation (3-part JWT format)
  • ✅ Required claims presence (subject, expiration)
  • ✅ Thread-safety via Arc cloning

Test Results

✅ All 34 unique tests pass (27 original + 6 new + 1 doc)

test services::auth::token::tests::test_create_and_verify_token ... ok
test services::auth::token::tests::test_verify_invalid_token ... ok
test services::auth::token::tests::test_verify_token_wrong_secret ... ok
test services::auth::token::tests::test_token_contains_required_subject ... ok
test services::auth::token::tests::test_token_has_expiration ... ok
test services::auth::token::tests::test_token_manager_is_clonable ... ok
test services::auth::token::tests::test_multiple_tokens_from_same_manager ... ok
test services::auth::token::tests::test_verify_malformed_token ... ok
test services::auth::token::tests::test_token_format ... ok

Test Plan

  • All token creation scenarios tested
  • All verification scenarios tested
  • All error paths tested
  • All existing tests still pass
  • Token structure validated

Note

This PR focuses on TokenManager unit tests only. Integration tests for the /api/token endpoint will be added in a future PR if needed, as the token functionality is already tested through middleware integration tests.

Related

Signed-off-by: Jan Zachmann [email protected]

- Copy latest upstream/main src/ contents to src/backend/src/
- Rename vue/ to src/ui/ (previously src/frontend/)
- Update Dockerfile to reference src/ui paths
- Update build scripts for new structure
- Maintain workspace structure for future Crux integration

Signed-off-by: Jan Zachmann <[email protected]>
Add comprehensive test infrastructure to support upcoming test coverage improvements:

- common/mocks.rs: Reusable mock constructors for DeviceServiceClient and SingleSignOnProvider
- common/utils.rs: Test utilities for creating test apps, requests, and loading fixtures
- fixtures/: Test fixture files (tokens, certificates) for consistent test data
- tests/README.md: Documentation for using the test infrastructure
- TEST_COVERAGE_ANALYSIS.md: Comprehensive analysis of current test coverage and implementation plan

This infrastructure enables the planned 14 PRs to systematically increase test coverage from 1% to 85-90% across 4 phases:
- Phase 1: Security & Stability (1% → 13%)
- Phase 2: Core Device Operations (13% → 50%)
- Phase 3: API Coverage (50% → 72%)
- Phase 4: Frontend & E2E (72% → 85-90%)

All existing tests (27) pass with new infrastructure.

Signed-off-by: Jan Zachmann <[email protected]>
Add 6 additional unit tests for TokenManager service (total now 9):

New tests:
- test_token_contains_required_subject - Validates subject claim
- test_token_has_expiration - Validates expiration is set
- test_token_manager_is_clonable - Validates Arc<> sharing works
- test_multiple_tokens_from_same_manager - Validates multiple token creation
- test_verify_malformed_token - Tests various malformed token formats
- test_token_format - Validates JWT structure (3-part format)

Coverage:
- ✅ Token creation and verification
- ✅ Invalid/malformed token handling
- ✅ Wrong secret detection
- ✅ Token structure validation
- ✅ Required claims (subject, expiration)
- ✅ Manager clonability for thread-safety

All 34 unique tests pass (27 original + 6 new + 1 doc)

Signed-off-by: Jan Zachmann <[email protected]>
@JanZachmann JanZachmann deleted the test/token-and-login branch December 18, 2025 08:41
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