|
| 1 | +"""Tests for SCIM path validation utilities.""" |
| 2 | + |
| 3 | +from scim2_models.utils import validate_scim_path_syntax |
| 4 | +from scim2_models.utils import validate_scim_urn_syntax |
| 5 | + |
| 6 | + |
| 7 | +def test_validate_scim_path_syntax_valid_paths(): |
| 8 | + """Test that valid SCIM paths are accepted.""" |
| 9 | + valid_paths = [ |
| 10 | + "userName", |
| 11 | + "name.familyName", |
| 12 | + "emails.value", |
| 13 | + "groups.display", |
| 14 | + "urn:ietf:params:scim:schemas:core:2.0:User:userName", |
| 15 | + "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber", |
| 16 | + 'emails[type eq "work"].value', |
| 17 | + 'groups[display eq "Admin"]', |
| 18 | + "meta.lastModified", |
| 19 | + ] |
| 20 | + |
| 21 | + for path in valid_paths: |
| 22 | + assert validate_scim_path_syntax(path), f"Path should be valid: {path}" |
| 23 | + |
| 24 | + |
| 25 | +def test_validate_scim_path_syntax_invalid_paths(): |
| 26 | + """Test that invalid SCIM paths are rejected.""" |
| 27 | + invalid_paths = [ |
| 28 | + "", # Empty string |
| 29 | + " ", # Whitespace only |
| 30 | + "123invalid", # Starts with digit |
| 31 | + "invalid..path", # Double dots |
| 32 | + "invalid@path", # Invalid character |
| 33 | + "urn:invalid", # Invalid URN format |
| 34 | + "urn:too:short", # URN too short |
| 35 | + ] |
| 36 | + |
| 37 | + for path in invalid_paths: |
| 38 | + assert not validate_scim_path_syntax(path), f"Path should be invalid: {path}" |
| 39 | + |
| 40 | + |
| 41 | +def test_validate_scim_urn_syntax_valid_urns(): |
| 42 | + """Test that valid SCIM URN paths are accepted.""" |
| 43 | + valid_urns = [ |
| 44 | + "urn:ietf:params:scim:schemas:core:2.0:User:userName", |
| 45 | + "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber", |
| 46 | + "urn:custom:namespace:schema:1.0:Resource:attribute", |
| 47 | + "urn:example:extension:v2:MyResource:customField", |
| 48 | + ] |
| 49 | + |
| 50 | + for urn in valid_urns: |
| 51 | + assert validate_scim_urn_syntax(urn), f"URN should be valid: {urn}" |
| 52 | + |
| 53 | + |
| 54 | +def test_validate_scim_urn_syntax_invalid_urns(): |
| 55 | + """Test that invalid SCIM URN paths are rejected.""" |
| 56 | + invalid_urns = [ |
| 57 | + "not_an_urn", # Doesn't start with urn: |
| 58 | + "urn:too:short", # Not enough segments |
| 59 | + "urn:ietf:params:scim:schemas:core:2.0:User:", # Empty attribute |
| 60 | + "urn:ietf:params:scim:schemas:core:2.0:User:123invalid", # Attribute starts with digit |
| 61 | + "urn:invalid", # Too short |
| 62 | + "urn:only:two:attribute", # URN part too short |
| 63 | + ] |
| 64 | + |
| 65 | + for urn in invalid_urns: |
| 66 | + assert not validate_scim_urn_syntax(urn), f"URN should be invalid: {urn}" |
| 67 | + |
| 68 | + |
| 69 | +def test_validate_scim_path_syntax_edge_cases(): |
| 70 | + """Test edge cases for path validation.""" |
| 71 | + # Test None handling (shouldn't happen in practice but defensive) |
| 72 | + assert not validate_scim_path_syntax("") |
| 73 | + |
| 74 | + # Test borderline valid cases |
| 75 | + assert validate_scim_path_syntax("a") # Single character |
| 76 | + assert validate_scim_path_syntax("a.b") # Simple dotted |
| 77 | + assert validate_scim_path_syntax("a_b") # Underscore |
| 78 | + assert validate_scim_path_syntax("a-b") # Hyphen |
| 79 | + |
| 80 | + # Test borderline invalid cases |
| 81 | + assert not validate_scim_path_syntax("9invalid") # Starts with digit |
| 82 | + assert not validate_scim_path_syntax("a..b") # Double dots |
| 83 | + |
| 84 | + |
| 85 | +def test_validate_scim_urn_syntax_edge_cases(): |
| 86 | + """Test edge cases for URN validation.""" |
| 87 | + # Test minimal valid URN |
| 88 | + assert validate_scim_urn_syntax("urn:a:b:c:d") |
| 89 | + |
| 90 | + # Test boundary cases |
| 91 | + assert not validate_scim_urn_syntax("urn:a:b:c:") # Empty attribute |
| 92 | + assert not validate_scim_urn_syntax("urn:a:b:") # Missing resource |
| 93 | + assert not validate_scim_urn_syntax("urn:") # Just urn: |
0 commit comments