Skip to content

Latest commit

 

History

History
236 lines (188 loc) · 7.51 KB

File metadata and controls

236 lines (188 loc) · 7.51 KB

Test Suite for VPN Configuration Features

Overview

Comprehensive test suite covering all VPN configuration enhancements including network address auto-calculation, FQDN support, DNS configuration, and dynamic filename generation.

Test Files Created

1. VPN Configuration Model Tests

File: spec/models/vpn_configuration_spec.rb

Test Coverage:

  • Attribute Validation: Ensures wg_fqdn, dns_servers, wg_ip_range, and server_vpn_ip_address can be saved and retrieved
  • Configuration Creation: Tests VpnConfiguration.get_vpn_configuration method for creating and retrieving configurations
  • FQDN Functionality: Tests saving, updating, and handling nil/empty FQDN values
  • Network Configuration: Tests network range and server IP address persistence
  • DNS Configuration: Tests custom DNS servers, multiple formats, and nil handling
  • Complete Scenarios: Tests both FQDN-based and IP-only setups
  • Associations: Tests relationship with network_addresses and dependent destruction

Key Test Cases:

# FQDN handling
it 'saves wg_fqdn correctly'
it 'can update wg_fqdn'
it 'allows nil wg_fqdn'

# Network configuration
it 'saves network range correctly'
it 'saves server VPN IP correctly'

# DNS configuration
it 'allows custom DNS servers'
it 'allows multiple DNS formats'

2. WireGuard Config Generator Tests

File: spec/lib/wireguard_config_generator_spec.rb

Test Coverage:

  • Server Config Generation: Tests default values and key generation
  • Client Config Generation: Tests with and without FQDN
  • Endpoint Priority: Tests FQDN priority over IP address
  • DNS Fallback Logic: Tests custom DNS vs. default fallback
  • Network Address Integration: Tests inclusion of additional network routes
  • Keep Alive Configuration: Tests optional persistent keep alive settings
  • Complete Scenarios: Tests professional FQDN setup vs. basic IP setup

Key Test Cases:

# Endpoint selection
it 'uses FQDN as endpoint'
it 'uses IP address as endpoint'
it 'prioritizes FQDN over IP address'

# DNS handling
it 'uses custom DNS servers'
it 'falls back to default DNS servers'
it 'uses default DNS servers for empty string'

# Complete configurations
it 'generates complete professional configuration'
it 'generates basic IP-based configuration'

3. Controller Download Tests

File: spec/controllers/vpn_devices_controller_spec.rb

Test Coverage:

  • Dynamic Filename Generation: Tests FQDN-based, IP-based, and fallback filenames
  • Content Generation: Tests correct configuration content in downloads
  • Priority Logic: Tests filename priority (FQDN > IP > default)
  • Special Character Handling: Tests dot-to-underscore conversion for IP addresses
  • Error Handling: Tests non-existent device handling
  • Various Formats: Tests different IP and FQDN formats

Key Test Cases:

# Filename generation
it 'downloads config with FQDN filename'
it 'downloads config with IP-based filename'
it 'falls back to default filename'

# Priority testing
it 'prioritizes FQDN over IP for filename'

# Format handling
it 'replaces dots with underscores in filename'
it 'handles complex FQDN correctly'

4. Feature Tests for Network Calculation Logic

File: spec/system/network_address_calculation_spec.rb

Test Coverage:

  • IP Calculation Logic: Tests expected behavior for automatic server IP calculation
  • Network Address Correction: Tests expected auto-correction logic for invalid network addresses
  • CIDR Handling: Tests various subnet sizes (/16, /24, /30, etc.)
  • Default Behavior: Tests /24 default when no CIDR specified
  • Edge Cases: Tests /31, /32 networks and expected handling
  • Logic Validation: Tests the underlying calculation expectations

Key Test Cases:

# Calculation logic
it 'provides correct calculation logic for /24 network'
it 'handles network address correction logic'
it 'defaults to /24 when no CIDR specified'

# Edge cases
it 'handles /31 networks'
it 'handles /32 networks'
it 'validates network correction expectations'

Test Scenarios Covered

1. Network Address Auto-calculation

# Input: '10.42.5.0/24' → Server IP: '10.42.5.254'
# Input: '10.89.90.9' → Auto-corrected to: '10.89.90.0', Server IP: '10.89.90.254'
# Input: '192.168.1.0/16' → Server IP: '192.168.255.254'

2. FQDN vs IP Priority

# FQDN present: Uses 'vpn.example.com:51820'
# IP only: Uses '203.0.113.10:51820'
# Both present: Prioritizes FQDN

3. DNS Configuration

# Custom DNS: Uses configured servers
# No DNS: Falls back to '8.8.8.8, 8.8.4.4'
# Empty DNS: Falls back to defaults

4. Filename Generation

# FQDN: 'vpn.example.com.conf'
# IP: '192_168_1_100.conf' (dots → underscores)
# None: 'gate_vpn_config.conf' (fallback)

Running the Tests

Individual Test Files

# Model tests
bundle exec rspec spec/models/vpn_configuration_spec.rb

# Library tests
bundle exec rspec spec/lib/wireguard_config_generator_spec.rb

# Controller tests
bundle exec rspec spec/controllers/vpn_devices_controller_spec.rb

# Feature tests (logic validation)
bundle exec rspec spec/system/network_address_calculation_spec.rb

All Tests

# Run all VPN-related tests
bundle exec rspec spec/models/vpn_configuration_spec.rb spec/lib/wireguard_config_generator_spec.rb spec/controllers/vpn_devices_controller_spec.rb spec/system/network_address_calculation_spec.rb

# Run with verbose output
bundle exec rspec spec/models/vpn_configuration_spec.rb -v

Test Dependencies

Required Gems

  • rspec-rails - Rails testing framework

Database Setup

# Ensure test database is set up
RAILS_ENV=test bundle exec rails db:create
RAILS_ENV=test bundle exec rails db:migrate

Prerequisites

  • Database migration must be run to add wg_fqdn column
  • VPN configuration must exist for some tests
  • User authentication mocking for controller tests

Expected Test Results

Model Tests

  • ✅ Attribute saving and retrieval
  • ✅ FQDN handling (nil, empty, valid values)
  • ✅ Network configuration persistence
  • ✅ DNS configuration flexibility
  • ✅ Association behavior

Generator Tests

  • ✅ Correct endpoint selection logic
  • ✅ DNS fallback behavior
  • ✅ Configuration completeness
  • ✅ Network address inclusion

Controller Tests

  • ✅ Dynamic filename generation
  • ✅ Priority logic (FQDN > IP > default)
  • ✅ Content correctness
  • ✅ Special character handling

Feature Tests

  • ✅ Network calculation logic validation
  • ✅ CIDR handling expectations
  • ✅ Edge case logic verification
  • ✅ Expected behavior validation

Troubleshooting

Common Issues

  1. Database Connection: Ensure test database is created and migrated
  2. Authentication: Controller tests mock authentication - ensure mocking is correct
  3. Missing Gems: Install required testing gems

Debug Steps

  1. Run individual test files to isolate issues
  2. Check test database schema includes wg_fqdn column
  3. Verify authentication helper methods are available

Coverage Summary

The test suite provides comprehensive coverage of:

  • Model Layer: VPN configuration persistence and validation
  • Business Logic: WireGuard configuration generation
  • Controller Layer: Download functionality and filename generation
  • Logic Validation: Network calculation and auto-correction expectations
  • Integration: Expected behavior and edge case handling

This ensures all features work correctly individually and together as a complete system.