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
//! Test helpers for PATCH integration tests
//!
//! This module provides common setup and helper functions for PATCH operation testing,
//! including proper server configuration with User and Group resource types and
//! patch capabilities enabled.

use scim_server::{
    RequestContext, ScimOperation, ScimServer,
    multi_tenant::TenantContext,
    provider_capabilities::{
        AuthenticationCapabilities, BulkCapabilities, ExtendedCapabilities, FilterCapabilities,
        PaginationCapabilities, ProviderCapabilities,
    },
    providers::StandardResourceProvider,
    resource_handlers::{create_group_resource_handler, create_user_resource_handler},
    schema::SchemaRegistry,
    storage::InMemoryStorage,
};

/// Create a fully configured SCIM server with User and Group support and patch capabilities
pub fn create_test_server_with_patch_support()
-> ScimServer<StandardResourceProvider<InMemoryStorage>> {
    let storage = InMemoryStorage::new();
    let provider = StandardResourceProvider::new(storage);
    let mut server = ScimServer::new(provider).expect("Failed to create server");

    // Register User resource type with full operations including Patch
    register_user_resource_type(&mut server).expect("Failed to register User resource type");

    // Register Group resource type with full operations including Patch
    register_group_resource_type(&mut server).expect("Failed to register Group resource type");

    server
}

/// Create a SCIM server with patch capabilities disabled
pub fn create_test_server_without_patch_support()
-> ScimServer<StandardResourceProvider<InMemoryStorage>> {
    let storage = InMemoryStorage::new();
    let provider = StandardResourceProvider::new(storage);
    let mut server = ScimServer::new(provider).expect("Failed to create server");

    // Register resource types without Patch operation
    register_user_resource_type_without_patch(&mut server)
        .expect("Failed to register User resource type");
    register_group_resource_type_without_patch(&mut server)
        .expect("Failed to register Group resource type");

    server
}

/// Register User resource type with full operations including Patch
fn register_user_resource_type(
    server: &mut ScimServer<StandardResourceProvider<InMemoryStorage>>,
) -> Result<(), Box<dyn std::error::Error>> {
    let registry = SchemaRegistry::new()?;
    let user_schema = registry.get_user_schema().clone();
    let user_handler = create_user_resource_handler(user_schema);

    server.register_resource_type(
        "User",
        user_handler,
        vec![
            ScimOperation::Create,
            ScimOperation::Read,
            ScimOperation::Update,
            ScimOperation::Patch,
            ScimOperation::Delete,
            ScimOperation::List,
            ScimOperation::Search,
        ],
    )?;

    Ok(())
}

/// Register Group resource type with full operations including Patch
fn register_group_resource_type(
    server: &mut ScimServer<StandardResourceProvider<InMemoryStorage>>,
) -> Result<(), Box<dyn std::error::Error>> {
    let registry = SchemaRegistry::new()?;
    let group_schema = registry.get_group_schema().clone();
    let group_handler = create_group_resource_handler(group_schema);

    server.register_resource_type(
        "Group",
        group_handler,
        vec![
            ScimOperation::Create,
            ScimOperation::Read,
            ScimOperation::Update,
            ScimOperation::Patch,
            ScimOperation::Delete,
            ScimOperation::List,
            ScimOperation::Search,
        ],
    )?;

    Ok(())
}

/// Register User resource type without Patch operation (for testing disabled patch)
fn register_user_resource_type_without_patch(
    server: &mut ScimServer<StandardResourceProvider<InMemoryStorage>>,
) -> Result<(), Box<dyn std::error::Error>> {
    let registry = SchemaRegistry::new()?;
    let user_schema = registry.get_user_schema().clone();
    let user_handler = create_user_resource_handler(user_schema);

    server.register_resource_type(
        "User",
        user_handler,
        vec![
            ScimOperation::Create,
            ScimOperation::Read,
            ScimOperation::Update,
            ScimOperation::Delete,
            ScimOperation::List,
            ScimOperation::Search,
        ],
    )?;

    Ok(())
}

/// Register Group resource type without Patch operation (for testing disabled patch)
fn register_group_resource_type_without_patch(
    server: &mut ScimServer<StandardResourceProvider<InMemoryStorage>>,
) -> Result<(), Box<dyn std::error::Error>> {
    let registry = SchemaRegistry::new()?;
    let group_schema = registry.get_group_schema().clone();
    let group_handler = create_group_resource_handler(group_schema);

    server.register_resource_type(
        "Group",
        group_handler,
        vec![
            ScimOperation::Create,
            ScimOperation::Read,
            ScimOperation::Update,
            ScimOperation::Delete,
            ScimOperation::List,
            ScimOperation::Search,
        ],
    )?;

    Ok(())
}

/// Create a test request context
pub fn create_test_context() -> RequestContext {
    RequestContext::with_generated_id()
}

/// Create a test request context with specific tenant
pub fn create_test_context_with_tenant(tenant_id: &str, client_id: &str) -> RequestContext {
    let tenant_context = TenantContext::new(tenant_id.to_string(), client_id.to_string());
    RequestContext::with_tenant_generated_id(tenant_context)
}

/// Create a provider with patch capabilities enabled
pub fn create_provider_with_patch_enabled() -> StandardResourceProvider<InMemoryStorage> {
    // For now, return default provider
    // In a real implementation, this would configure the provider to advertise patch support
    let storage = InMemoryStorage::new();
    StandardResourceProvider::new(storage)
}

/// Create a provider with patch capabilities disabled
pub fn create_provider_with_patch_disabled() -> StandardResourceProvider<InMemoryStorage> {
    // For now, return default provider
    // In a real implementation, this would configure the provider to not advertise patch support
    let storage = InMemoryStorage::new();
    StandardResourceProvider::new(storage)
}

/// Helper to create a user resource for testing
pub async fn create_test_user(
    server: &ScimServer<StandardResourceProvider<InMemoryStorage>>,
    context: &RequestContext,
) -> Result<scim_server::Resource, Box<dyn std::error::Error>> {
    let user_data = serde_json::json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
        "userName": "test.user",
        "displayName": "Test User",
        "emails": [
            {
                "value": "test@example.com",
                "primary": true,
                "type": "work"
            }
        ],
        "active": true
    });

    let created = server.create_resource("User", user_data, context).await?;
    Ok(created)
}

/// Helper to create a group resource for testing
pub async fn create_test_group(
    server: &ScimServer<StandardResourceProvider<InMemoryStorage>>,
    context: &RequestContext,
) -> Result<scim_server::Resource, Box<dyn std::error::Error>> {
    let group_data = serde_json::json!({
        "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
        "displayName": "Test Group",
        "members": []
    });

    let created = server.create_resource("Group", group_data, context).await?;
    Ok(created)
}

/// Assert that a server has patch support properly configured
pub fn assert_patch_support_enabled(
    server: &ScimServer<StandardResourceProvider<InMemoryStorage>>,
) {
    let config = server
        .get_service_provider_config()
        .expect("Should be able to get service provider config");

    assert!(config.patch_supported, "Patch support should be enabled");
}

/// Assert that a server has patch support disabled
pub fn assert_patch_support_disabled(
    server: &ScimServer<StandardResourceProvider<InMemoryStorage>>,
) {
    let config = server
        .get_service_provider_config()
        .expect("Should be able to get service provider config");

    assert!(!config.patch_supported, "Patch support should be disabled");
}

/// Helper to check if a resource type is supported
pub fn is_resource_type_supported(
    server: &ScimServer<StandardResourceProvider<InMemoryStorage>>,
    resource_type: &str,
) -> bool {
    server
        .get_supported_resource_types()
        .contains(&resource_type)
}

/// Assert that required resource types are registered
pub fn assert_required_resource_types_registered(
    server: &ScimServer<StandardResourceProvider<InMemoryStorage>>,
) {
    assert!(
        is_resource_type_supported(server, "User"),
        "User resource type should be registered"
    );
    assert!(
        is_resource_type_supported(server, "Group"),
        "Group resource type should be registered"
    );
}

/// Create provider capabilities with patch enabled
pub fn create_patch_enabled_capabilities() -> ProviderCapabilities {
    use std::collections::HashMap;

    let mut supported_operations = HashMap::new();
    supported_operations.insert(
        "User".to_string(),
        vec![
            scim_server::multi_tenant::ScimOperation::Create,
            scim_server::multi_tenant::ScimOperation::Read,
            scim_server::multi_tenant::ScimOperation::Update,
            scim_server::multi_tenant::ScimOperation::Patch,
            scim_server::multi_tenant::ScimOperation::Delete,
            scim_server::multi_tenant::ScimOperation::List,
        ],
    );
    supported_operations.insert(
        "Group".to_string(),
        vec![
            scim_server::multi_tenant::ScimOperation::Create,
            scim_server::multi_tenant::ScimOperation::Read,
            scim_server::multi_tenant::ScimOperation::Update,
            scim_server::multi_tenant::ScimOperation::Patch,
            scim_server::multi_tenant::ScimOperation::Delete,
            scim_server::multi_tenant::ScimOperation::List,
        ],
    );

    ProviderCapabilities {
        supported_operations,
        supported_schemas: vec![
            "urn:ietf:params:scim:schemas:core:2.0:User".to_string(),
            "urn:ietf:params:scim:schemas:core:2.0:Group".to_string(),
        ],
        supported_resource_types: vec!["User".to_string(), "Group".to_string()],
        bulk_capabilities: BulkCapabilities::default(),
        filter_capabilities: FilterCapabilities::default(),
        pagination_capabilities: PaginationCapabilities::default(),
        authentication_capabilities: AuthenticationCapabilities::default(),
        extended_capabilities: ExtendedCapabilities {
            patch_supported: true,
            etag_supported: true,
            change_password_supported: false,
            sort_supported: true,
            custom_capabilities: std::collections::HashMap::new(),
        },
    }
}

/// Create provider capabilities with patch disabled
pub fn create_patch_disabled_capabilities() -> ProviderCapabilities {
    use std::collections::HashMap;

    let mut supported_operations = HashMap::new();
    supported_operations.insert(
        "User".to_string(),
        vec![
            scim_server::multi_tenant::ScimOperation::Create,
            scim_server::multi_tenant::ScimOperation::Read,
            scim_server::multi_tenant::ScimOperation::Update,
            scim_server::multi_tenant::ScimOperation::Delete,
            scim_server::multi_tenant::ScimOperation::List,
        ],
    );
    supported_operations.insert(
        "Group".to_string(),
        vec![
            scim_server::multi_tenant::ScimOperation::Create,
            scim_server::multi_tenant::ScimOperation::Read,
            scim_server::multi_tenant::ScimOperation::Update,
            scim_server::multi_tenant::ScimOperation::Delete,
            scim_server::multi_tenant::ScimOperation::List,
        ],
    );

    ProviderCapabilities {
        supported_operations,
        supported_schemas: vec![
            "urn:ietf:params:scim:schemas:core:2.0:User".to_string(),
            "urn:ietf:params:scim:schemas:core:2.0:Group".to_string(),
        ],
        supported_resource_types: vec!["User".to_string(), "Group".to_string()],
        bulk_capabilities: BulkCapabilities::default(),
        filter_capabilities: FilterCapabilities::default(),
        pagination_capabilities: PaginationCapabilities::default(),
        authentication_capabilities: AuthenticationCapabilities::default(),
        extended_capabilities: ExtendedCapabilities {
            patch_supported: false,
            etag_supported: true,
            change_password_supported: false,
            sort_supported: true,
            custom_capabilities: std::collections::HashMap::new(),
        },
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_create_server_with_patch_support() {
        let server = create_test_server_with_patch_support();
        assert_required_resource_types_registered(&server);

        // Check that Patch operation is supported for User
        let user_operations = server.get_supported_operations("User").unwrap();
        assert!(user_operations.contains(&ScimOperation::Patch));

        // Check that Patch operation is supported for Group
        let group_operations = server.get_supported_operations("Group").unwrap();
        assert!(group_operations.contains(&ScimOperation::Patch));
    }

    #[test]
    fn test_create_server_without_patch_support() {
        let server = create_test_server_without_patch_support();
        assert_required_resource_types_registered(&server);

        // Check that Patch operation is NOT supported for User
        let user_operations = server.get_supported_operations("User").unwrap();
        assert!(!user_operations.contains(&ScimOperation::Patch));

        // Check that Patch operation is NOT supported for Group
        let group_operations = server.get_supported_operations("Group").unwrap();
        assert!(!group_operations.contains(&ScimOperation::Patch));
    }

    #[test]
    fn test_context_creation() {
        let context = create_test_context();
        assert!(!context.request_id.is_empty());

        let tenant_context = create_test_context_with_tenant("test-tenant", "test-client");
        assert_eq!(tenant_context.tenant_id(), Some("test-tenant"));
        assert_eq!(tenant_context.client_id(), Some("test-client"));
    }

    #[tokio::test]
    async fn test_resource_creation_helpers() {
        let server = create_test_server_with_patch_support();
        let context = create_test_context();

        // Test user creation
        let user = create_test_user(&server, &context).await;
        assert!(user.is_ok(), "Should be able to create test user");
        let user_resource = user.unwrap();
        assert!(user_resource.get_id().is_some(), "User should have ID");

        // Test group creation
        let group = create_test_group(&server, &context).await;
        assert!(group.is_ok(), "Should be able to create test group");
        let group_resource = group.unwrap();
        assert!(group_resource.get_id().is_some(), "Group should have ID");
    }
}