scim-server 0.4.0

A comprehensive SCIM 2.0 server library for Rust with multi-tenant support and type-safe operations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
//! Multi-valued attribute validation tests.
//!
//! This module tests validation errors related to multi-valued attributes
//! and their structure in SCIM resources (Errors 33-38).

use serde_json::json;

// Import test utilities
use crate::common::{ValidationErrorCode, builders::UserBuilder, fixtures::rfc_examples};

// Import SCIM server types
use scim_server::error::ValidationError;
use scim_server::schema::{SchemaRegistry, validation::OperationContext};

/// Test Error #33: Single value provided for multi-valued attribute
#[test]
fn test_single_value_for_multi_valued() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // emails should be an array, not a single object
    let user_single_email = UserBuilder::new().with_single_value_emails().build();

    // Verify emails is not an array
    assert!(!user_single_email["emails"].is_array());
    assert!(user_single_email["emails"].is_object());

    // Actually validate the resource
    let result = registry.validate_json_resource_with_context(
        "User",
        &user_single_email,
        OperationContext::Update,
    );

    // Assert that validation fails with the expected error
    assert!(result.is_err());
    match result {
        Err(ValidationError::Custom { message }) => {
            assert!(message.contains("emails must be an array"));
        }
        Err(other) => panic!("Expected Custom error about emails array, got {:?}", other),
        Ok(_) => panic!("Expected validation to fail, but it passed"),
    }
}

/// Test Error #33: Single value for other multi-valued attributes
#[test]
fn test_single_value_for_multi_valued_addresses() {
    // addresses should be an array, not a single object
    let user_single_address = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "123",
        "userName": "test@example.com",
        "addresses": {
            "type": "work",
            "streetAddress": "123 Main St",
            "locality": "Anytown",
            "region": "CA",
            "postalCode": "12345",
            "country": "USA"
        },
        "meta": {
            "resourceType": "User"
        }
    });

    assert!(!user_single_address["addresses"].is_array());
    assert!(user_single_address["addresses"].is_object());
}

/// Test Error #34: Array provided for single-valued attribute
#[test]
fn test_array_for_single_valued() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // userName should be a string, not an array
    let user_array_username = UserBuilder::new().with_array_username().build();

    // Verify userName is an array
    assert!(user_array_username["userName"].is_array());
    assert!(!user_array_username["userName"].is_string());

    // Actually validate the resource
    let result = registry.validate_json_resource_with_context(
        "User",
        &user_array_username,
        OperationContext::Update,
    );

    // Assert that validation fails with the expected error
    assert!(result.is_err());
    match result {
        Err(ValidationError::Custom { message }) => {
            assert!(message.contains("userName must be a string"));
        }
        Err(other) => panic!(
            "Expected Custom error about userName string, got {:?}",
            other
        ),
        Ok(_) => panic!("Expected validation to fail, but it passed"),
    }
}

/// Test Error #34: Array for other single-valued attributes
#[test]
fn test_array_for_single_valued_display_name() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // displayName should be a string, not an array
    let user_array_display_name = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "123",
        "userName": "test@example.com",
        "displayName": ["John", "Doe"],
        "meta": {
            "resourceType": "User"
        }
    });

    // Verify displayName is an array
    assert!(user_array_display_name["displayName"].is_array());

    // Actually validate the resource
    let result = registry.validate_json_resource_with_context(
        "User",
        &user_array_display_name,
        OperationContext::Update,
    );

    // Assert that validation fails with the expected error
    assert!(result.is_err());
    match result {
        Err(ValidationError::InvalidAttributeType {
            attribute,
            expected,
            actual,
        }) => {
            assert_eq!(attribute, "displayName");
            assert_eq!(expected, "string");
            assert_eq!(actual, "array");
        }
        Err(other) => panic!("Expected InvalidAttributeType error, got {:?}", other),
        Ok(_) => panic!("Expected validation to fail, but it passed"),
    }
}

/// Test Error #35: Multiple primary values in multi-valued attribute
#[test]
fn test_multiple_primary_values() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // Only one email should have primary: true
    let user_multiple_primaries = UserBuilder::new().with_multiple_primary_emails().build();

    // Verify multiple emails have primary: true
    let emails = user_multiple_primaries["emails"].as_array().unwrap();
    let primary_count = emails
        .iter()
        .filter(|email| email["primary"] == true)
        .count();

    assert!(primary_count > 1, "Should have multiple primary emails");

    // Actually validate the resource
    let result = registry.validate_json_resource_with_context(
        "User",
        &user_multiple_primaries,
        OperationContext::Update,
    );

    // Assert that validation fails with the expected error
    assert!(result.is_err());
    match result {
        Err(ValidationError::MultiplePrimaryValues { attribute }) => {
            assert_eq!(attribute, "emails");
        }
        Err(other) => panic!("Expected MultiplePrimaryValues error, got {:?}", other),
        Ok(_) => panic!("Expected validation to fail, but it passed"),
    }
}

/// Test Error #35: Multiple primary values in addresses
#[test]
fn test_multiple_primary_addresses() {
    let user_multiple_primary_addresses = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "123",
        "userName": "test@example.com",
        "addresses": [
            {
                "type": "work",
                "streetAddress": "123 Work St",
                "primary": true
            },
            {
                "type": "home",
                "streetAddress": "456 Home St",
                "primary": true  // Multiple primaries not allowed
            }
        ],
        "meta": {
            "resourceType": "User"
        }
    });

    let addresses = user_multiple_primary_addresses["addresses"]
        .as_array()
        .unwrap();
    let primary_count = addresses
        .iter()
        .filter(|addr| addr["primary"] == true)
        .count();

    assert_eq!(primary_count, 2);
}

/// Test Error #36: Invalid multi-valued structure
#[test]
fn test_invalid_multi_valued_structure() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // Test emails with incorrect structure
    let user_invalid_email_structure = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "123",
        "userName": "test@example.com",
        "emails": [
            "plain-string-email", // Should be object with value, type, etc.
            {
                "value": "valid@example.com",
                "type": "work"
            }
        ],
        "meta": {
            "resourceType": "User"
        }
    });

    let emails = user_invalid_email_structure["emails"].as_array().unwrap();
    assert!(emails[0].is_string()); // First email is invalid structure
    assert!(emails[1].is_object()); // Second email is valid structure

    // Actually validate the resource
    let result = registry.validate_json_resource_with_context(
        "User",
        &user_invalid_email_structure,
        OperationContext::Update,
    );

    // Assert that validation fails with the expected error
    assert!(result.is_err());
    match result {
        Err(ValidationError::Custom { message }) => {
            assert!(message.contains("Invalid emails format"));
            assert!(message.contains("invalid type: string"));
        }
        Err(other) => panic!(
            "Expected Custom error about invalid emails format, got {:?}",
            other
        ),
        Ok(_) => panic!("Expected validation to fail, but it passed"),
    }
}

/// Test Error #36: Missing value in multi-valued complex attribute
#[test]
fn test_multi_valued_missing_value() {
    let user_email_without_value = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "123",
        "userName": "test@example.com",
        "emails": [
            {
                "type": "work",
                "primary": true
                // Missing "value" field
            }
        ],
        "meta": {
            "resourceType": "User"
        }
    });

    let email = &user_email_without_value["emails"][0];
    assert!(!email.as_object().unwrap().contains_key("value"));
    assert!(email.as_object().unwrap().contains_key("type"));
}

/// Test Error #37: Missing required sub-attribute in multi-valued attribute
#[test]
fn test_missing_required_sub_attribute() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // Test emails missing required "value" sub-attribute
    let user_email_missing_value = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "123",
        "userName": "test@example.com",
        "emails": [
            {
                "type": "work",
                "primary": true
                // Missing "value" field - this is required
            }
        ],
        "meta": {
            "resourceType": "User"
        }
    });

    // Verify the email object doesn't have a "value" field
    let email = &user_email_missing_value["emails"][0];
    assert!(!email.as_object().unwrap().contains_key("value"));
    assert!(email.as_object().unwrap().contains_key("type"));

    // Actually validate the resource
    let result = registry.validate_json_resource_with_context(
        "User",
        &user_email_missing_value,
        OperationContext::Update,
    );

    // Assert that validation fails with the expected error
    assert!(result.is_err());
    match result {
        Err(ValidationError::MissingRequiredSubAttribute {
            attribute,
            sub_attribute,
        }) => {
            assert_eq!(attribute, "emails");
            assert_eq!(sub_attribute, "value");
        }
        Err(other) => panic!(
            "Expected MissingRequiredSubAttribute error, got {:?}",
            other
        ),
        Ok(_) => panic!("Expected validation to fail, but it passed"),
    }
}

/// Test Error #37: Missing required sub-attribute in addresses
#[test]
fn test_missing_required_address_sub_attribute() {
    // Test address missing required sub-attributes
    let user_incomplete_address = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "123",
        "userName": "test@example.com",
        "addresses": [
            {
                "type": "work"
                // Missing other required fields like locality, region, etc.
                // depending on schema requirements
            }
        ],
        "meta": {
            "resourceType": "User"
        }
    });

    let address = &user_incomplete_address["addresses"][0];
    assert_eq!(address["type"], "work");
    assert!(!address.as_object().unwrap().contains_key("streetAddress"));
}

/// Test Error #38: Invalid canonical value in multi-valued attribute
#[test]
fn test_invalid_canonical_value() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // Test invalid "type" values that don't match canonical values
    let user_invalid_email_type = UserBuilder::new().with_invalid_email_type().build();

    // Verify the invalid type is present
    let emails = user_invalid_email_type["emails"].as_array().unwrap();
    let invalid_email = emails.iter().find(|email| email["type"] == "invalid-type");
    assert!(invalid_email.is_some());

    // Actually validate the resource
    let result = registry.validate_json_resource_with_context(
        "User",
        &user_invalid_email_type,
        OperationContext::Update,
    );

    // Assert that validation fails with the expected error
    assert!(result.is_err());
    match result {
        Err(ValidationError::InvalidCanonicalValue {
            attribute,
            value,
            allowed,
        }) => {
            assert_eq!(attribute, "emails.type");
            assert_eq!(value, "invalid-type");
            assert!(allowed.contains(&"work".to_string()));
            assert!(allowed.contains(&"home".to_string()));
            assert!(allowed.contains(&"other".to_string()));
        }
        Err(other) => panic!("Expected InvalidCanonicalValue error, got {:?}", other),
        Ok(_) => panic!("Expected validation to fail, but it passed"),
    }
}

/// Test Error #38: Invalid canonical values in phone numbers
#[test]
fn test_invalid_phone_canonical_values() {
    let user_invalid_phone_types = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "123",
        "userName": "test@example.com",
        "phoneNumbers": [
            {
                "value": "555-1234",
                "type": "invalid-phone-type" // Should be work, home, mobile, fax, pager, other
            },
            {
                "value": "555-5678",
                "type": "work" // Valid type
            }
        ],
        "meta": {
            "resourceType": "User"
        }
    });

    let phones = user_invalid_phone_types["phoneNumbers"].as_array().unwrap();
    assert_eq!(phones[0]["type"], "invalid-phone-type");
    assert_eq!(phones[1]["type"], "work");
}

/// Test valid multi-valued attributes to ensure no false positives
#[test]
fn test_valid_multi_valued_attributes() {
    // Test valid emails structure
    let valid_user = rfc_examples::user_full();

    // Verify emails is properly structured
    let emails = valid_user["emails"].as_array().unwrap();
    assert!(emails.len() >= 1);

    for email in emails {
        assert!(email["value"].is_string());
        assert!(email["type"].is_string());

        // Check that only one has primary: true
        if email["primary"] == true {
            // This is the primary email
            assert_eq!(email["primary"], true);
        }
    }

    // Count primary emails (should be 0 or 1)
    let primary_count = emails
        .iter()
        .filter(|email| email["primary"] == true)
        .count();
    assert!(primary_count <= 1, "Should have at most one primary email");
}

/// Test valid single-valued attributes
#[test]
fn test_valid_single_valued_attributes() {
    let valid_user = rfc_examples::user_minimal();

    // userName should be a string
    assert!(valid_user["userName"].is_string());
    assert_eq!(valid_user["userName"], "bjensen@example.com");

    // id should be a string
    assert!(valid_user["id"].is_string());

    // displayName should be a string if present
    let full_user = rfc_examples::user_full();
    assert!(full_user["displayName"].is_string());
    assert_eq!(full_user["displayName"], "Babs Jensen");
}

/// Test canonical values for multi-valued attributes
#[test]
fn test_valid_canonical_values() {
    let user = rfc_examples::user_full();

    // Test valid email types
    let emails = user["emails"].as_array().unwrap();
    for email in emails {
        let email_type = email["type"].as_str().unwrap();
        let valid_email_types = ["work", "home", "other"];
        assert!(
            valid_email_types.contains(&email_type),
            "Invalid email type: {}",
            email_type
        );
    }

    // Test valid address types
    if let Some(addresses) = user["addresses"].as_array() {
        for address in addresses {
            let addr_type = address["type"].as_str().unwrap();
            let valid_address_types = ["work", "home", "other"];
            assert!(
                valid_address_types.contains(&addr_type),
                "Invalid address type: {}",
                addr_type
            );
        }
    }
}

/// Test complex multi-valued attribute validation
#[test]
fn test_complex_multi_valued_validation() {
    // Test group members structure
    let valid_group = rfc_examples::group_basic();
    let members = valid_group["members"].as_array().unwrap();

    for member in members {
        // Each member should have required attributes
        assert!(member["value"].is_string());
        assert!(member["$ref"].is_string());
        assert!(member["display"].is_string());

        // $ref should be a valid URI
        let ref_uri = member["$ref"].as_str().unwrap();
        assert!(ref_uri.starts_with("https://"));
        assert!(ref_uri.contains("/Users/"));
    }
}

/// Test edge cases in multi-valued attribute validation
#[test]
fn test_multi_valued_edge_cases() {
    // Test empty multi-valued array (should be valid)
    let user_empty_emails = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "123",
        "userName": "test@example.com",
        "emails": [], // Empty array should be valid
        "meta": {
            "resourceType": "User"
        }
    });

    assert!(user_empty_emails["emails"].is_array());
    assert_eq!(user_empty_emails["emails"].as_array().unwrap().len(), 0);

    // Test single item in multi-valued array (should be valid)
    let user_single_email_array = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "123",
        "userName": "test@example.com",
        "emails": [
            {
                "value": "test@example.com",
                "type": "work",
                "primary": true
            }
        ],
        "meta": {
            "resourceType": "User"
        }
    });

    assert!(user_single_email_array["emails"].is_array());
    assert_eq!(
        user_single_email_array["emails"].as_array().unwrap().len(),
        1
    );
}

/// Test multi-valued attribute with null values
#[test]
fn test_multi_valued_with_null_values() {
    let user_with_nulls = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "123",
        "userName": "test@example.com",
        "emails": [
            null, // Null item in array
            {
                "value": "test@example.com",
                "type": "work"
            }
        ],
        "meta": {
            "resourceType": "User"
        }
    });

    let emails = user_with_nulls["emails"].as_array().unwrap();
    assert!(emails[0].is_null());
    assert!(emails[1].is_object());
}

/// Test multiple validation errors in multi-valued attributes
#[test]
fn test_multiple_multi_valued_errors() {
    let user_multiple_errors = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "123",
        "userName": ["array", "username"], // Error #34: Array for single-valued
        "emails": {                        // Error #33: Single value for multi-valued
            "value": "test@example.com",
            "type": "work",
            "primary": true
        },
        "phoneNumbers": [
            {
                "value": "555-1234",
                "type": "invalid-type", // Error #38: Invalid canonical value
                "primary": true
            },
            {
                "value": "555-5678",
                "type": "work",
                "primary": true         // Error #35: Multiple primary values
            }
        ],
        "meta": {
            "resourceType": "User"
        }
    });

    // Verify multiple error conditions
    assert!(user_multiple_errors["userName"].is_array()); // Should be string
    assert!(user_multiple_errors["emails"].is_object()); // Should be array

    let phones = user_multiple_errors["phoneNumbers"].as_array().unwrap();
    assert_eq!(phones[0]["type"], "invalid-type");

    let primary_count = phones
        .iter()
        .filter(|phone| phone["primary"] == true)
        .count();
    assert_eq!(primary_count, 2); // Multiple primaries
}

#[cfg(test)]
mod coverage_tests {
    use super::*;
    use crate::common::TestCoverage;

    #[test]
    fn test_multi_valued_error_coverage() {
        // Verify all multi-valued attribute errors (33-38) are covered by our tests
        let mut coverage = TestCoverage::new();

        // Mark errors as tested based on our test functions
        coverage.mark_tested(ValidationErrorCode::SingleValueForMultiValued); // Error #33
        coverage.mark_tested(ValidationErrorCode::ArrayForSingleValued); // Error #34
        coverage.mark_tested(ValidationErrorCode::MultiplePrimaryValues); // Error #35
        coverage.mark_tested(ValidationErrorCode::InvalidMultiValuedStructure); // Error #36
        coverage.mark_tested(ValidationErrorCode::MissingRequiredSubAttribute); // Error #37
        coverage.mark_tested(ValidationErrorCode::InvalidCanonicalValue); // Error #38

        // Verify we've covered all multi-valued attribute errors
        let multi_valued_errors = [
            ValidationErrorCode::SingleValueForMultiValued,
            ValidationErrorCode::ArrayForSingleValued,
            ValidationErrorCode::MultiplePrimaryValues,
            ValidationErrorCode::InvalidMultiValuedStructure,
            ValidationErrorCode::MissingRequiredSubAttribute,
            ValidationErrorCode::InvalidCanonicalValue,
        ];

        for error in &multi_valued_errors {
            assert!(
                coverage.is_tested(error),
                "Error {:?} not covered by tests",
                error
            );
        }
    }

    #[test]
    fn test_multi_valued_test_scenarios() {
        // Document the different scenarios we test for multi-valued attributes

        let test_scenarios = [
            // Structure validation
            "single_value_for_multi_valued",
            "array_for_single_valued",
            "invalid_multi_valued_structure",
            // Content validation
            "multiple_primary_values",
            "missing_required_sub_attributes",
            "invalid_canonical_values",
            // Edge cases
            "empty_arrays",
            "single_item_arrays",
            "null_values_in_arrays",
            // Valid cases
            "valid_multi_valued_structure",
            "valid_canonical_values",
            "valid_single_valued_structure",
        ];

        assert!(
            test_scenarios.len() >= 10,
            "Should have comprehensive test scenarios"
        );

        // Verify we test multiple attribute types
        let tested_attributes = [
            "emails",
            "addresses",
            "phoneNumbers",
            "groups",
            "userName",
            "displayName",
        ];

        assert!(
            tested_attributes.len() >= 6,
            "Should test multiple attribute types"
        );
    }

    #[test]
    fn test_canonical_value_coverage() {
        // Verify we test canonical values for different attribute types

        let canonical_value_tests = [
            ("emails", vec!["work", "home", "other"]),
            ("addresses", vec!["work", "home", "other"]),
            (
                "phoneNumbers",
                vec!["work", "home", "mobile", "fax", "pager", "other"],
            ),
            (
                "ims",
                vec!["aim", "gtalk", "icq", "xmpp", "msn", "skype", "qq", "yahoo"],
            ),
        ];

        for (attr_type, expected_values) in canonical_value_tests {
            assert!(
                expected_values.len() >= 3,
                "Should test multiple canonical values for {}",
                attr_type
            );
        }
    }
}