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
//! Tests for schema validation and registry functionality.
//!
//! This module contains comprehensive tests for schema loading, validation,
//! and all the various validation scenarios including edge cases and error conditions.

use super::registry::SchemaRegistry;
use super::types::AttributeType;
use super::validation::OperationContext;
use crate::error::ValidationError;
use serde_json::json;

#[test]
fn test_schema_registry_creation() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");
    assert_eq!(registry.get_schemas().len(), 2);
    assert!(
        registry
            .get_schema("urn:ietf:params:scim:schemas:core:2.0:User")
            .is_some()
    );
    assert!(
        registry
            .get_schema("urn:ietf:params:scim:schemas:core:2.0:Group")
            .is_some()
    );
}

#[test]
fn test_valid_user_validation() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");
    let user = json!({
        "userName": "testuser",
        "displayName": "Test User",
        "active": true,
        "emails": [
            {
                "value": "test@example.com",
                "type": "work",
                "primary": true
            }
        ]
    });

    assert!(
        registry
            .validate_resource(&registry.get_user_schema(), &user)
            .is_ok()
    );
}

#[test]
fn test_missing_required_attribute() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");
    let user = json!({
        "displayName": "Test User"
        // Missing required userName
    });

    let result = registry.validate_resource(&registry.get_user_schema(), &user);
    assert!(result.is_err());
    if let Err(ValidationError::MissingRequiredAttribute { attribute }) = result {
        assert_eq!(attribute, "userName");
    } else {
        panic!("Expected MissingRequiredAttribute error");
    }
}

#[test]
fn test_invalid_type_validation() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");
    let user = json!({
        "userName": "testuser",
        "active": "not_a_boolean"
    });

    let result = registry.validate_resource(&registry.get_user_schema(), &user);
    assert!(result.is_err());
}

#[test]
fn test_invalid_canonical_value() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");
    let user = json!({
        "userName": "testuser",
        "emails": [
            {
                "value": "test@example.com",
                "type": "invalid_type"
            }
        ]
    });

    let result = registry.validate_resource(&registry.get_user_schema(), &user);
    assert!(result.is_err());
}

#[test]
fn test_complex_attribute_validation() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");
    let user = json!({
        "userName": "testuser",
        "name": {
            "givenName": "John",
            "familyName": "Doe",
            "formatted": "John Doe"
        }
    });

    assert!(
        registry
            .validate_resource(&registry.get_user_schema(), &user)
            .is_ok()
    );
}

#[test]
fn test_id_validation() {
    // Test valid ID during Resource creation
    let valid_user = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "12345",
        "userName": "testuser@example.com",
        "meta": {
            "resourceType": "User"
        }
    });

    let result = crate::resource::core::Resource::from_json("User".to_string(), valid_user);
    assert!(
        result.is_ok(),
        "Valid user resource should be created successfully"
    );

    // Test missing ID (should be allowed, ID is optional)
    let missing_id_user = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "userName": "testuser@example.com",
        "meta": {
            "resourceType": "User"
        }
    });

    let result = crate::resource::core::Resource::from_json("User".to_string(), missing_id_user);
    assert!(
        result.is_ok(),
        "Resource creation should succeed without ID"
    );
    let resource = result.unwrap();
    assert!(
        resource.id.is_none(),
        "Resource should have no ID when not provided"
    );

    // Test empty ID - this should fail value object validation
    let empty_id_user = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "",
        "userName": "testuser@example.com",
        "meta": {
            "resourceType": "User"
        }
    });

    let result = crate::resource::core::Resource::from_json("User".to_string(), empty_id_user);
    assert!(
        result.is_err(),
        "Empty ID should cause resource creation to fail"
    );

    // Test invalid ID type
    let invalid_id_user = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": 12345,
        "userName": "testuser@example.com",
        "meta": {
            "resourceType": "User"
        }
    });

    let result = crate::resource::core::Resource::from_json("User".to_string(), invalid_id_user);
    assert!(
        result.is_err(),
        "Non-string ID should cause resource creation to fail"
    );
}

#[test]
fn test_external_id_validation() {
    // Test valid external ID during Resource creation
    let valid_external_id_user = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "12345",
        "userName": "testuser@example.com",
        "externalId": "ext-12345",
        "meta": {
            "resourceType": "User"
        }
    });

    let result =
        crate::resource::core::Resource::from_json("User".to_string(), valid_external_id_user);
    assert!(result.is_ok(), "Valid external ID should be accepted");
    let resource = result.unwrap();
    assert_eq!(resource.external_id.unwrap().as_str(), "ext-12345");

    // Test invalid external ID type - this should fail during resource creation
    let invalid_external_id_user = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "12345",
        "userName": "testuser@example.com",
        "externalId": 999,
        "meta": {
            "resourceType": "User"
        }
    });

    let result =
        crate::resource::core::Resource::from_json("User".to_string(), invalid_external_id_user);
    assert!(
        result.is_err(),
        "Non-string external ID should cause resource creation to fail"
    );

    // Test empty external ID - this should fail value object validation
    let empty_external_id_user = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "12345",
        "userName": "testuser@example.com",
        "externalId": "",
        "meta": {
            "resourceType": "User"
        }
    });

    let result =
        crate::resource::core::Resource::from_json("User".to_string(), empty_external_id_user);
    assert!(
        result.is_err(),
        "Empty external ID should cause resource creation to fail"
    );
}

#[test]
fn test_schema_validation_integration() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // Test 1: Valid resource passes Resource creation and schema validation
    let valid_user = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "valid-id",
        "userName": "testuser@example.com",
        "externalId": "valid-external-id",
        "meta": {
            "resourceType": "User"
        }
    });

    // Resource creation should succeed
    let resource_result =
        crate::resource::core::Resource::from_json("User".to_string(), valid_user);
    assert!(
        resource_result.is_ok(),
        "Valid resource should be created successfully"
    );

    let resource = resource_result.unwrap();

    // Schema validation should also pass
    let schema_result = registry.validate_resource_hybrid(&resource);
    assert!(
        schema_result.is_ok(),
        "Schema validation should pass for valid resource"
    );

    // Test 2: Resource creation catches value object validation errors
    let invalid_user_empty_id = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "",  // Empty ID should fail
        "userName": "testuser@example.com",
        "meta": {
            "resourceType": "User"
        }
    });

    let result =
        crate::resource::core::Resource::from_json("User".to_string(), invalid_user_empty_id);
    assert!(
        result.is_err(),
        "Empty ID should cause resource creation to fail"
    );

    // Test 3: External ID validation is integrated in Resource creation
    let invalid_external_id = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "valid-id",
        "userName": "testuser@example.com",
        "externalId": "",  // Empty external ID should fail
        "meta": {
            "resourceType": "User"
        }
    });

    let result =
        crate::resource::core::Resource::from_json("User".to_string(), invalid_external_id);
    assert!(
        result.is_err(),
        "Empty external ID should cause resource creation to fail"
    );

    // Test 4: Missing ID is now allowed (ID is optional)
    let missing_id_user = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "userName": "testuser@example.com",
        "meta": {
            "resourceType": "User"
        }
    });

    let result = crate::resource::core::Resource::from_json("User".to_string(), missing_id_user);
    assert!(
        result.is_ok(),
        "Missing ID should be allowed in resource creation"
    );

    // Test 5: Schema validation integration with value objects
    let schema_validation_user = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "valid-id",
        "userName": "testuser@example.com",
        "invalidAttribute": "this should be caught by schema validation"
    });

    let resource_result =
        crate::resource::core::Resource::from_json("User".to_string(), schema_validation_user);
    assert!(
        resource_result.is_ok(),
        "Resource creation should succeed even with extra attributes"
    );

    let resource = resource_result.unwrap();

    // Schema validation should detect invalid attributes
    let schema_result = registry.validate_resource_hybrid(&resource);
    // Note: This might pass if the schema allows additional attributes
    // The test verifies that schema validation is properly integrated
    assert!(
        schema_result.is_ok() || schema_result.is_err(),
        "Schema validation should run without errors"
    );
}

#[test]
fn test_valid_group_validation() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "e9e30dba-f08f-4109-8486-d5c6a331660a",
        "displayName": "Tour Guides",
        "meta": {
            "resourceType": "Group",
            "created": "2010-01-23T04:56:22Z",
            "lastModified": "2011-05-13T04:42:34Z",
            "version": "W/\"3694e05e9dff592\"",
            "location": "https://example.com/v2/Groups/e9e30dba-f08f-4109-8486-d5c6a331660a"
        }
    });

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

#[test]
fn test_group_missing_display_name() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "e9e30dba-f08f-4109-8486-d5c6a331660a",
        "meta": {
            "resourceType": "Group"
        }
    });

    let result =
        registry.validate_json_resource_with_context("User", &group, OperationContext::Update);
    // Group schema allows displayName to be optional according to the schema
    assert!(
        result.is_ok(),
        "Group without displayName should be valid: {:?}",
        result
    );
}

#[test]
fn test_group_with_members() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");
    let group = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "id": "e9e30dba-f08f-4109-8486-d5c6a331660a",
        "displayName": "Tour Guides",
        "members": [
            {
                "value": "2819c223-7f76-453a-919d-413861904646",
                "$ref": "https://example.com/v2/Users/2819c223-7f76-453a-919d-413861904646",
                "type": "User"
            }
        ],
        "meta": {
            "resourceType": "Group"
        }
    });

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

#[test]
fn test_group_schema_retrieval() {
    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");
    assert!(!group_schema.attributes.is_empty());

    // Check that displayName attribute exists
    let display_name_attr = group_schema
        .attributes
        .iter()
        .find(|attr| attr.name == "displayName");
    assert!(
        display_name_attr.is_some(),
        "Group schema should have displayName attribute"
    );

    // Check that members attribute exists and is complex
    let members_attr = group_schema
        .attributes
        .iter()
        .find(|attr| attr.name == "members");
    assert!(
        members_attr.is_some(),
        "Group schema should have members attribute"
    );
    if let Some(attr) = members_attr {
        assert!(matches!(attr.data_type, AttributeType::Complex));
        assert!(attr.multi_valued);
    }
}