Skip to content

Conversation

@JanZachmann
Copy link
Contributor

Summary

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

Add comprehensive unit tests for the AuthorizationService to cover all authorization scenarios.

Changes

Authorization Service Tests (10 new tests)

Happy Path:

  • test_fleet_administrator_with_valid_tenant - FleetAdmin can access with valid tenant
  • test_fleet_operator_with_valid_fleet - FleetOperator can access fleet in their list

Error Paths:

  • test_fleet_administrator_with_invalid_tenant - Rejects invalid tenant
  • test_fleet_operator_with_invalid_fleet - Rejects fleet not in operator's list
  • test_fleet_operator_without_fleet_list - Rejects operator without fleet_list claim
  • test_invalid_role - Rejects insufficient role (FleetObserver)
  • test_missing_tenant_list - Rejects token without tenant_list
  • test_missing_roles - Rejects token without roles
  • test_sso_token_verification_error - Handles SSO verification failures
  • test_device_service_fleet_id_error - Handles device service errors

Coverage

Tests validate all authorization rules:

  • ✅ Tenant validation (must be in tenant_list)
  • ✅ Role-based access (FleetAdministrator, FleetOperator)
  • ✅ Fleet validation for operators
  • ✅ Error handling for missing/invalid claims
  • ✅ Integration with mocked dependencies

Test Results

✅ All 38 tests pass (27 existing + 10 new authorization + 1 doc test)

test services::auth::authorization::tests::test_fleet_administrator_with_valid_tenant ... ok
test services::auth::authorization::tests::test_fleet_administrator_with_invalid_tenant ... ok
test services::auth::authorization::tests::test_fleet_operator_with_valid_fleet ... ok
test services::auth::authorization::tests::test_fleet_operator_with_invalid_fleet ... ok
test services::auth::authorization::tests::test_fleet_operator_without_fleet_list ... ok
test services::auth::authorization::tests::test_invalid_role ... ok
test services::auth::authorization::tests::test_missing_tenant_list ... ok
test services::auth::authorization::tests::test_missing_roles ... ok
test services::auth::authorization::tests::test_sso_token_verification_error ... ok
test services::auth::authorization::tests::test_device_service_fleet_id_error ... ok

Note on Keycloak Provider

KeycloakProvider unit tests were skipped as:

  • verify_token() requires mocking HTTP client (complex)
  • Already tested indirectly through AuthorizationService tests
  • Already tested in integration test (validate_portal_token.rs)
  • create_frontend_config_file() requires AppConfig setup

Future PR could add these if needed.

Test Plan

  • All authorization rules tested
  • All error paths tested
  • All existing tests still pass
  • Uses mockall framework via test infrastructure

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 10 unit tests for authorization service covering all authorization scenarios:

Happy path tests:
- test_fleet_administrator_with_valid_tenant
- test_fleet_operator_with_valid_fleet

Error path tests:
- test_fleet_administrator_with_invalid_tenant
- test_fleet_operator_with_invalid_fleet
- test_fleet_operator_without_fleet_list
- test_invalid_role (FleetObserver)
- test_missing_tenant_list
- test_missing_roles
- test_sso_token_verification_error
- test_device_service_fleet_id_error

Test coverage:
- All authorization rules (tenant, role, fleet validation)
- All error paths (missing claims, invalid permissions)
- Integration with mocked SSO provider and device service client

All 38 tests pass (27 existing + 10 new authorization + 1 doc test)

Signed-off-by: Jan Zachmann <[email protected]>
Add detailed comment explaining the rationale for not including unit tests
for KeycloakProvider in this PR:

- verify_token() requires complex HTTP client mocking (reqwest)
- Already tested indirectly via AuthorizationService tests
- Already tested in integration tests (validate_portal_token.rs)
- create_frontend_config_file() requires AppConfig with env vars
- Core logic delegated to well-tested jwt-simple library

Provides suggestions for future testing approaches if needed.

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