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
//! Integration tests for value object integration with Resource creation and validation.
//!
//! This module tests the Phase 2 clean break implementation where value objects are
//! core members of the Resource struct and validation happens during Resource construction.

use scim_server::resource::Resource;
use scim_server::schema::registry::SchemaRegistry;
use serde_json::json;

/// Test successful Resource creation with valid value objects
#[test]
fn test_resource_creation_with_valid_value_objects() {
    // Create a valid user resource JSON
    let user_data = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "2819c223-7f76-453a-919d-413861904646",
        "userName": "bjensen@example.com",
        "externalId": "701984",
        "displayName": "Barbara Jensen",
        "emails": [{
            "value": "bjensen@example.com",
            "type": "work",
            "primary": true
        }]
    });

    // Test Resource creation with value object validation
    let result = Resource::from_json("User".to_string(), user_data);
    assert!(
        result.is_ok(),
        "Resource creation should succeed with valid data"
    );

    let resource = result.unwrap();

    // Verify core value objects are properly extracted
    assert!(resource.id.is_some(), "Resource ID should be extracted");
    assert_eq!(
        resource.id.as_ref().unwrap().as_str(),
        "2819c223-7f76-453a-919d-413861904646"
    );

    assert!(resource.user_name.is_some(), "Username should be extracted");
    assert_eq!(
        resource.user_name.as_ref().unwrap().as_str(),
        "bjensen@example.com"
    );

    assert!(
        resource.external_id.is_some(),
        "External ID should be extracted"
    );
    assert_eq!(resource.external_id.as_ref().unwrap().as_str(), "701984");

    assert_eq!(resource.schemas.len(), 1);
    assert_eq!(
        resource.schemas[0].as_str(),
        "urn:ietf:params:scim:schemas:core:2.0:User"
    );
}

/// Test Resource creation fails with invalid value objects
#[test]
fn test_resource_creation_with_invalid_value_objects() {
    // Test invalid resource ID (empty string)
    let invalid_id_data = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "",
        "userName": "bjensen@example.com"
    });

    let result = Resource::from_json("User".to_string(), invalid_id_data);
    assert!(
        result.is_err(),
        "Resource creation should fail with empty ID"
    );

    // Test invalid username (empty string)
    let invalid_username_data = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "valid-id",
        "userName": ""
    });

    let result = Resource::from_json("User".to_string(), invalid_username_data);
    assert!(
        result.is_err(),
        "Resource creation should fail with empty username"
    );

    // Test invalid schema URI (empty string)
    let invalid_schema_data = json!({
        "schemas": [""],
        "id": "valid-id",
        "userName": "valid@example.com"
    });

    let result = Resource::from_json("User".to_string(), invalid_schema_data);
    assert!(
        result.is_err(),
        "Resource creation should fail with empty schema URI"
    );
}

/// Test Resource creation with missing required core fields
#[test]
fn test_resource_creation_with_missing_core_fields() {
    // Missing schemas (should get default)
    let no_schemas_data = json!({
        "id": "valid-id",
        "userName": "bjensen@example.com"
    });

    let result = Resource::from_json("User".to_string(), no_schemas_data);
    assert!(
        result.is_ok(),
        "Resource creation should succeed without explicit schemas"
    );

    let resource = result.unwrap();
    assert_eq!(resource.schemas.len(), 1);
    assert_eq!(
        resource.schemas[0].as_str(),
        "urn:ietf:params:scim:schemas:core:2.0:User"
    );

    // Missing userName (should be None for User resources)
    let no_username_data = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "valid-id",
        "displayName": "Test User"
    });

    let result = Resource::from_json("User".to_string(), no_username_data);
    assert!(
        result.is_ok(),
        "Resource creation should succeed without username"
    );

    let resource = result.unwrap();
    assert!(
        resource.user_name.is_none(),
        "Username should be None when not provided"
    );
}

/// Test Resource creation with various external ID formats
#[test]
fn test_resource_creation_with_external_ids() {
    // Numeric external ID
    let numeric_external_id = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "userName": "user1@example.com",
        "externalId": "12345"
    });

    let result = Resource::from_json("User".to_string(), numeric_external_id);
    assert!(result.is_ok(), "Should handle numeric external ID");
    assert_eq!(result.unwrap().external_id.unwrap().as_str(), "12345");

    // Alphanumeric external ID
    let alpha_external_id = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "userName": "user2@example.com",
        "externalId": "EXT-ABC-123"
    });

    let result = Resource::from_json("User".to_string(), alpha_external_id);
    assert!(result.is_ok(), "Should handle alphanumeric external ID");
    assert_eq!(result.unwrap().external_id.unwrap().as_str(), "EXT-ABC-123");

    // UUID external ID
    let uuid_external_id = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "userName": "user3@example.com",
        "externalId": "550e8400-e29b-41d4-a716-446655440000"
    });

    let result = Resource::from_json("User".to_string(), uuid_external_id);
    assert!(result.is_ok(), "Should handle UUID external ID");
    assert_eq!(
        result.unwrap().external_id.unwrap().as_str(),
        "550e8400-e29b-41d4-a716-446655440000"
    );
}

/// Test Resource schema validation integration
#[test]
fn test_resource_with_schema_validation() {
    let registry = SchemaRegistry::new().expect("Failed to create registry");

    // Create resource with valid data
    let valid_data = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "userName": "testuser@example.com",
        "name": {
            "givenName": "Test",
            "familyName": "User"
        },
        "emails": [{
            "value": "test@example.com",
            "type": "work"
        }]
    });

    let resource = Resource::from_json("User".to_string(), valid_data)
        .expect("Resource creation should succeed");

    // Test schema validation using the new hybrid approach
    let validation_result = registry.validate_resource_hybrid(&resource);
    assert!(
        validation_result.is_ok(),
        "Schema validation should pass for valid resource"
    );
}

/// Test Resource email extraction functionality
#[test]
fn test_resource_email_extraction() {
    let email_data = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "userName": "multi@example.com",
        "emails": [
            {
                "value": "work@example.com",
                "type": "work",
                "primary": true
            },
            {
                "value": "home@example.com",
                "type": "home",
                "primary": false
            }
        ]
    });

    let resource = Resource::from_json("User".to_string(), email_data)
        .expect("Resource creation should succeed");

    let emails = resource.get_emails();
    let emails = emails.expect("Should have emails");
    assert_eq!(emails.len(), 2, "Should extract both email addresses");

    let email0 = emails.get(0).expect("Should have first email");
    assert_eq!(email0.value(), "work@example.com");

    let email1 = emails.get(1).expect("Should have second email");
    assert_eq!(email1.value(), "home@example.com");
}

/// Test Resource active status handling
#[test]
fn test_resource_active_status() {
    // Explicitly active user
    let active_data = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "userName": "active@example.com",
        "active": true
    });

    let resource = Resource::from_json("User".to_string(), active_data)
        .expect("Resource creation should succeed");
    assert!(
        resource.is_active(),
        "User should be active when explicitly set"
    );

    // Explicitly inactive user
    let inactive_data = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "userName": "inactive@example.com",
        "active": false
    });

    let resource = Resource::from_json("User".to_string(), inactive_data)
        .expect("Resource creation should succeed");
    assert!(
        !resource.is_active(),
        "User should be inactive when explicitly set"
    );

    // User without active field (should default to true)
    let default_data = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "userName": "default@example.com"
    });

    let resource = Resource::from_json("User".to_string(), default_data)
        .expect("Resource creation should succeed");
    assert!(resource.is_active(), "User should default to active");
}

/// Test Resource attribute access
#[test]
fn test_resource_attribute_access() {
    let user_data = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "userName": "testuser@example.com",
        "displayName": "Test User",
        "title": "Software Engineer",
        "department": "Engineering"
    });

    let mut resource = Resource::from_json("User".to_string(), user_data)
        .expect("Resource creation should succeed");

    // Test getting attributes
    assert_eq!(
        resource
            .get_attribute("displayName")
            .and_then(|v| v.as_str()),
        Some("Test User")
    );
    assert_eq!(
        resource.get_attribute("title").and_then(|v| v.as_str()),
        Some("Software Engineer")
    );

    // Test setting attributes
    resource.set_attribute("title".to_string(), json!("Senior Software Engineer"));
    assert_eq!(
        resource.get_attribute("title").and_then(|v| v.as_str()),
        Some("Senior Software Engineer")
    );

    // Test core fields are not in attributes (they're separate fields)
    assert!(
        resource.get_attribute("userName").is_none(),
        "userName should not be in attributes"
    );
    assert!(
        resource.get_attribute("id").is_none(),
        "id should not be in attributes"
    );
    assert!(
        resource.get_attribute("schemas").is_none(),
        "schemas should not be in attributes"
    );
}

/// Test Group resource creation (to test resource type flexibility)
#[test]
fn test_group_resource_creation() {
    let group_data = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "displayName": "Engineering Team",
        "members": [
            {
                "value": "user1",
                "type": "User"
            }
        ]
    });

    let resource = Resource::from_json("Group".to_string(), group_data)
        .expect("Group resource creation should succeed");

    assert_eq!(resource.resource_type, "Group");
    assert_eq!(resource.schemas.len(), 1);
    assert_eq!(
        resource.schemas[0].as_str(),
        "urn:ietf:params:scim:schemas:core:2.0:Group"
    );

    // Group resources should not have username
    assert!(
        resource.user_name.is_none(),
        "Groups should not have usernames"
    );

    // But should have displayName in attributes
    assert_eq!(
        resource
            .get_attribute("displayName")
            .and_then(|v| v.as_str()),
        Some("Engineering Team")
    );
}

/// Test Resource serialization round-trip
#[test]
fn test_resource_serialization_round_trip() {
    let original_data = json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "id": "test-id-123",
        "userName": "roundtrip@example.com",
        "externalId": "EXT-123",
        "displayName": "Round Trip User",
        "emails": [{
            "value": "rt@example.com",
            "type": "work",
            "primary": true
        }]
    });

    // Create resource from JSON
    let resource = Resource::from_json("User".to_string(), original_data.clone())
        .expect("Resource creation should succeed");

    // Serialize back to JSON
    let serialized = resource.to_json().unwrap();

    // Verify core fields are preserved
    assert_eq!(
        serialized["schemas"][0],
        "urn:ietf:params:scim:schemas:core:2.0:User"
    );
    assert_eq!(serialized["id"], "test-id-123");
    assert_eq!(serialized["userName"], "roundtrip@example.com");
    assert_eq!(serialized["externalId"], "EXT-123");
    assert_eq!(serialized["displayName"], "Round Trip User");
    assert_eq!(serialized["emails"][0]["value"], "rt@example.com");
}