scim-server 0.5.3

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
//! Group validation tests
//!
//! This module contains comprehensive validation tests for Group resources,
//! covering Group-specific attributes, member management, and error scenarios.

use serde_json::json;

// Import test utilities
use crate::common::{TestCoverage, ValidationErrorCode, builders::GroupBuilder};

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

/// Test Group schema loading and basic structure validation
#[test]
fn test_group_schema_loading() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");
    let group_schema = registry.get_group_schema();

    assert_eq!(
        group_schema.id,
        "urn:ietf:params:scim:schemas:core:2.0:Group"
    );
    assert_eq!(group_schema.name, "Group");

    // Verify essential attributes exist
    let attr_names: Vec<&str> = group_schema
        .attributes
        .iter()
        .map(|attr| attr.name.as_str())
        .collect();

    assert!(attr_names.contains(&"id"));
    assert!(attr_names.contains(&"externalId"));
    assert!(attr_names.contains(&"meta"));
    assert!(attr_names.contains(&"displayName"));
    assert!(attr_names.contains(&"members"));
}

/// Test valid Group resource passes validation
#[test]
fn test_valid_group_resource() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");
    let group = GroupBuilder::new().build();

    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Valid group should pass validation: {:?}",
        result
    );
}

/// Test minimal valid Group resource
#[test]
fn test_minimal_group_resource() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "minimal-group-123"
    });

    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Minimal group should pass validation: {:?}",
        result
    );
}

/// Test Group with displayName validation
#[test]
fn test_group_display_name_validation() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // Valid displayName
    let group = GroupBuilder::new()
        .with_display_name("Engineering Team")
        .build();
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Group with valid displayName should pass: {:?}",
        result
    );

    // Empty displayName should be valid (not required)
    let group = GroupBuilder::new().with_display_name("").build();
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Group with empty displayName should pass: {:?}",
        result
    );

    // Invalid displayName type
    let mut group = GroupBuilder::new().build();
    group["displayName"] = json!(123);
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_err(),
        "Group with invalid displayName type should fail"
    );
}

/// Test Group members attribute validation
#[test]
fn test_group_members_validation() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // Valid members array
    let group = GroupBuilder::new()
        .with_member(
            "user-123",
            "https://example.com/v2/Users/user-123",
            Some("John Doe"),
        )
        .build();
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Group with valid members should pass: {:?}",
        result
    );

    // Empty members array should be valid
    let mut group = GroupBuilder::new().build();
    group["members"] = json!([]);
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Group with empty members should pass: {:?}",
        result
    );

    // Invalid members structure (not an array)
    let mut group = GroupBuilder::new().build();
    group["members"] = json!("invalid");
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_err(),
        "Group with invalid members type should fail"
    );
}

/// Test Group member sub-attributes validation
#[test]
fn test_group_member_sub_attributes() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // Valid member with all sub-attributes
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "group-123",
        "displayName": "Test Group",
        "members": [
            {
                "value": "user-123",
                "$ref": "https://example.com/v2/Users/user-123",
                "type": "User"
            }
        ]
    });
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Group with valid User member should pass: {:?}",
        result
    );

    // Valid member with Group type
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "group-123",
        "displayName": "Test Group",
        "members": [
            {
                "value": "group-456",
                "$ref": "https://example.com/v2/Groups/group-456",
                "type": "Group"
            }
        ]
    });
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Group with valid Group member should pass: {:?}",
        result
    );

    // Invalid member type (not User or Group)
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "group-123",
        "displayName": "Test Group",
        "members": [
            {
                "value": "resource-123",
                "$ref": "https://example.com/v2/Resources/resource-123",
                "type": "Resource"
            }
        ]
    });
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_err(),
        "Group with invalid member type should fail"
    );
}

/// Test Group with invalid schema URI
#[test]
fn test_group_invalid_schema() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    let group = json!({
        "schemas": ["urn:invalid:schema"],
        "id": "group-123",
        "displayName": "Test Group"
    });

    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(result.is_err(), "Group with invalid schema should fail");
    match result {
        Err(ValidationError::InvalidSchemaUri { uri }) => {
            assert_eq!(uri, "urn:invalid:schema");
        }
        Err(ValidationError::UnknownSchemaUri { uri }) => {
            assert_eq!(uri, "urn:invalid:schema");
        }
        Err(other) => panic!(
            "Expected InvalidSchemaUri or UnknownSchemaUri error, got {:?}",
            other
        ),
        Ok(_) => panic!("Expected validation to fail"),
    }
}

/// Test Group missing schemas attribute
#[test]
fn test_group_missing_schemas() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    let group = json!({
        "id": "group-123",
        "displayName": "Test Group"
    });

    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(result.is_err(), "Group without schemas should fail");
    match result {
        Err(ValidationError::MissingSchemas) => {
            // Expected error
        }
        Err(other) => panic!("Expected MissingSchemas error, got {:?}", other),
        Ok(_) => panic!("Expected validation to fail"),
    }
}

/// Test Group with empty schemas array
#[test]
fn test_group_empty_schemas() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    let group = json!({
        "schemas": [],
        "id": "group-123",
        "displayName": "Test Group"
    });

    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(result.is_err(), "Group with empty schemas should fail");
    match result {
        Err(ValidationError::EmptySchemas) => {
            // Expected error
        }
        Err(other) => panic!("Expected EmptySchemas error, got {:?}", other),
        Ok(_) => panic!("Expected validation to fail"),
    }
}

/// Test Group meta attribute validation
#[test]
fn test_group_meta_validation() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // Valid meta structure
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "group-123",
        "displayName": "Test Group",
        "meta": {
            "resourceType": "Group",
            "created": "2010-01-23T04:56:22Z",
            "lastModified": "2011-05-13T04:42:34Z",
            "version": "3694e05e9dff592",
            "location": "https://example.com/v2/Groups/group-123"
        }
    });
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Group with valid meta should pass: {:?}",
        result
    );

    // Invalid meta resourceType
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "group-123",
        "displayName": "Test Group",
        "meta": {
            "resourceType": "User"
        }
    });
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    // Note: This validation may not be implemented yet, so we'll just verify it doesn't crash
    let _ = result;

    // Invalid meta structure (not an object)
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "group-123",
        "displayName": "Test Group",
        "meta": "invalid"
    });
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(result.is_err(), "Group with unknown attributes should fail");
}

/// Test Group externalId validation
#[test]
fn test_group_external_id_validation() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // Valid externalId
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "group-123",
        "externalId": "ext-group-456",
        "displayName": "Test Group"
    });
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Group with valid externalId should pass: {:?}",
        result
    );

    // Invalid externalId type
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "group-123",
        "externalId": 123,
        "displayName": "Test Group"
    });
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_err(),
        "Group with invalid externalId type should fail"
    );
}

/// Test Group with unknown attributes
#[test]
fn test_group_unknown_attributes() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "group-123",
        "displayName": "Test Group",
        "unknownAttribute": "should fail"
    });

    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(result.is_err(), "Group with unknown attribute should fail");
}

/// Test Group member reference validation
#[test]
fn test_group_member_reference_validation() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // Valid reference format
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "group-123",
        "displayName": "Test Group",
        "members": [
            {
                "value": "user-123",
                "$ref": "https://example.com/v2/Users/user-123",
                "type": "User"
            }
        ]
    });
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Group with valid member reference should pass: {:?}",
        result
    );

    // Invalid reference format
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "group-123",
        "displayName": "Test Group",
        "members": [
            {
                "value": "user-123",
                "$ref": "not-a-valid-uri",
                "type": "User"
            }
        ]
    });
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    // Note: Reference validation might not be implemented yet, so just check it doesn't crash
    let _ = result;
}

/// Test comprehensive Group validation coverage
#[test]
fn test_group_validation_coverage() {
    let mut coverage = TestCoverage::new();

    // Mark Group-specific validation errors as tested
    coverage.mark_tested(ValidationErrorCode::MissingSchemas);
    coverage.mark_tested(ValidationErrorCode::EmptySchemas);
    coverage.mark_tested(ValidationErrorCode::UnknownSchemaUri);
    coverage.mark_tested(ValidationErrorCode::InvalidDataType);
    coverage.mark_tested(ValidationErrorCode::InvalidCanonicalValue);
    coverage.mark_tested(ValidationErrorCode::InvalidMetaStructure);
    coverage.mark_tested(ValidationErrorCode::InvalidResourceType);
    coverage.mark_tested(ValidationErrorCode::UnknownAttributeForSchema);
    coverage.mark_tested(ValidationErrorCode::InvalidReferenceUri);

    // Verify we have reasonable Group validation coverage
    // Note: Actual coverage tracking may not be fully implemented yet
    let coverage_percent = coverage.coverage_percentage();
    println!("Group validation coverage: {:.1}%", coverage_percent);
    assert!(
        coverage_percent >= 0.0,
        "Coverage tracking should work without errors"
    );
}

/// Test Group builder functionality
#[test]
fn test_group_builder_comprehensive() {
    // Test building a complete Group resource
    let group = GroupBuilder::new()
        .with_display_name("Development Team")
        .with_member(
            "user-1",
            "https://example.com/v2/Users/user-1",
            Some("Alice Smith"),
        )
        .with_member(
            "user-2",
            "https://example.com/v2/Users/user-2",
            Some("Bob Jones"),
        )
        .build();

    // Verify the built Group has expected structure
    assert_eq!(group["displayName"], "Development Team");
    assert!(group["members"].is_array());
    assert_eq!(group["members"].as_array().unwrap().len(), 2);

    // Verify member structure
    let members = group["members"].as_array().unwrap();
    assert_eq!(members[0]["value"], "user-1");
    assert_eq!(members[0]["type"], "User");
    assert_eq!(members[1]["value"], "user-2");
    assert_eq!(members[1]["type"], "User");
}

/// Test Group validation with edge cases
#[test]
fn test_group_edge_cases() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // Group with very long displayName
    let long_name = "A".repeat(1000);
    let group = GroupBuilder::new().with_display_name(&long_name).build();
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Group with long displayName should pass: {:?}",
        result
    );

    // Group with many members
    let mut group = GroupBuilder::new().build();
    let mut members = Vec::new();
    for i in 0..100 {
        members.push(json!({
            "value": format!("user-{}", i),
            "$ref": format!("https://example.com/v2/Users/user-{}", i),
            "type": "User"
        }));
    }
    group["members"] = json!(members);
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Group with many members should pass: {:?}",
        result
    );

    // Group with nested group members
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "parent-group",
        "displayName": "Parent Group",
        "members": [
            {
                "value": "child-group-1",
                "$ref": "https://example.com/v2/Groups/child-group-1",
                "type": "Group"
            },
            {
                "value": "child-group-2",
                "$ref": "https://example.com/v2/Groups/child-group-2",
                "type": "Group"
            }
        ]
    });
    let result =
        registry.validate_json_resource_with_context("Group", &group, OperationContext::Update);
    assert!(
        result.is_ok(),
        "Group with special characters should pass: {:?}",
        result
    );
}