fix(tests): Enhance CrowdSecConfig with new input fields and improve accessibility
- Added IDs to input fields in CrowdSecConfig for better accessibility. - Updated labels to use <label> elements for checkboxes and inputs. - Improved error handling and user feedback in the CrowdSecConfig tests. - Enhanced test coverage for console enrollment and banned IP functionalities. fix: Update SecurityHeaders to include aria-label for delete button - Added aria-label to the delete button for better screen reader support. test: Add comprehensive tests for proxyHostsHelpers and validation utilities - Implemented tests for formatting and help text functions in proxyHostsHelpers. - Added validation tests for email and IP address formats. chore: Update vitest configuration for dynamic coverage thresholds - Adjusted coverage thresholds to be dynamic based on environment variables. - Included additional coverage reporters. chore: Update frontend-test-coverage script to reflect new coverage threshold - Increased minimum coverage requirement from 85% to 87.5%. fix: Ensure tests pass with consistent data in passwd file - Updated tests/etc/passwd to ensure consistent content.
This commit is contained in:
89
frontend/src/utils/__tests__/validation.test.ts
Normal file
89
frontend/src/utils/__tests__/validation.test.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import {
|
||||
isValidEmail,
|
||||
isIPv4,
|
||||
isPrivateOrDockerIP,
|
||||
isLikelyDockerContainerIP,
|
||||
} from '../validation'
|
||||
|
||||
describe('validation utils', () => {
|
||||
describe('isValidEmail', () => {
|
||||
it('returns true for valid emails', () => {
|
||||
expect(isValidEmail('test@example.com')).toBe(true)
|
||||
expect(isValidEmail('user.name@domain.co.uk')).toBe(true)
|
||||
expect(isValidEmail('user+regex@domain.com')).toBe(true)
|
||||
})
|
||||
|
||||
it('returns false for invalid emails', () => {
|
||||
expect(isValidEmail('invalid')).toBe(false)
|
||||
expect(isValidEmail('invalid@')).toBe(false)
|
||||
expect(isValidEmail('@domain.com')).toBe(false)
|
||||
expect(isValidEmail('user@domain')).toBe(false)
|
||||
expect(isValidEmail('')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isIPv4', () => {
|
||||
it('returns true for valid IPv4 addresses', () => {
|
||||
expect(isIPv4('192.168.1.1')).toBe(true)
|
||||
expect(isIPv4('10.0.0.1')).toBe(true)
|
||||
expect(isIPv4('0.0.0.0')).toBe(true)
|
||||
expect(isIPv4('255.255.255.255')).toBe(true)
|
||||
})
|
||||
|
||||
it('returns false for invalid IPv4 addresses', () => {
|
||||
expect(isIPv4('256.0.0.1')).toBe(false)
|
||||
expect(isIPv4('1.2.3')).toBe(false)
|
||||
expect(isIPv4('1.2.3.4.5')).toBe(false)
|
||||
expect(isIPv4('1.2.3.4.')).toBe(false)
|
||||
expect(isIPv4('abc')).toBe(false)
|
||||
expect(isIPv4('192.168.1.a')).toBe(false)
|
||||
expect(isIPv4('')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isPrivateOrDockerIP', () => {
|
||||
it('returns true for private IP ranges', () => {
|
||||
expect(isPrivateOrDockerIP('10.0.0.1')).toBe(true)
|
||||
expect(isPrivateOrDockerIP('10.255.255.255')).toBe(true)
|
||||
expect(isPrivateOrDockerIP('192.168.0.1')).toBe(true)
|
||||
expect(isPrivateOrDockerIP('192.168.255.255')).toBe(true)
|
||||
expect(isPrivateOrDockerIP('172.16.0.1')).toBe(true) // Start of 172.16.x.x
|
||||
expect(isPrivateOrDockerIP('172.31.255.255')).toBe(true) // End of 172.31.x.x
|
||||
})
|
||||
|
||||
it('returns false for public or non-private IP ranges', () => {
|
||||
expect(isPrivateOrDockerIP('8.8.8.8')).toBe(false)
|
||||
expect(isPrivateOrDockerIP('1.1.1.1')).toBe(false)
|
||||
expect(isPrivateOrDockerIP('172.15.0.1')).toBe(false) // Below 172.16...
|
||||
expect(isPrivateOrDockerIP('172.32.0.1')).toBe(false) // Above 172.31...
|
||||
expect(isPrivateOrDockerIP('192.167.1.1')).toBe(false)
|
||||
expect(isPrivateOrDockerIP('192.169.1.1')).toBe(false)
|
||||
})
|
||||
|
||||
it('returns false for invalid IPs', () => {
|
||||
expect(isPrivateOrDockerIP('invalid')).toBe(false)
|
||||
expect(isPrivateOrDockerIP('999.999.999.999')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isLikelyDockerContainerIP', () => {
|
||||
it('returns true for likely Docker IPs', () => {
|
||||
// Docker default bridge: 172.17.x.x
|
||||
expect(isLikelyDockerContainerIP('172.17.0.2')).toBe(true)
|
||||
// Docker user-defined: 172.18.x.x - 172.31.x.x
|
||||
expect(isLikelyDockerContainerIP('172.18.0.1')).toBe(true)
|
||||
expect(isLikelyDockerContainerIP('172.31.255.255')).toBe(true)
|
||||
})
|
||||
|
||||
it('returns false for non-Docker IPs', () => {
|
||||
expect(isLikelyDockerContainerIP('172.16.0.1')).toBe(false) // Private but often not Docker default
|
||||
expect(isLikelyDockerContainerIP('192.168.1.1')).toBe(false)
|
||||
expect(isLikelyDockerContainerIP('10.0.0.1')).toBe(false)
|
||||
expect(isLikelyDockerContainerIP('8.8.8.8')).toBe(false)
|
||||
})
|
||||
|
||||
it('returns false for invalid IPs', () => {
|
||||
expect(isLikelyDockerContainerIP('invalid')).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user