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
564
565
566
567
568
569
570
571
572
573
//! Common Provider Test Utilities
//!
//! This module provides shared utilities, fixtures, and helper functions
//! for testing all provider implementations in the unified SCIM system.
//! These utilities ensure consistent testing patterns across different provider types.

use crate::common::{UnifiedTestHarness, create_multi_tenant_context, create_test_user};
use scim_server::ResourceProvider;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::sync::Arc;

// ============================================================================
// Test Data Builders and Fixtures
// ============================================================================

/// Builder for creating comprehensive test scenarios with multiple tenants
#[derive(Debug, Clone)]
pub struct MultiTenantScenarioBuilder {
    tenant_ids: Vec<String>,
    users_per_tenant: usize,
    groups_per_tenant: usize,
    include_enterprise_users: bool,
    include_complex_groups: bool,
}

impl MultiTenantScenarioBuilder {
    /// Create a new scenario builder
    pub fn new() -> Self {
        Self {
            tenant_ids: vec!["tenant_a".to_string(), "tenant_b".to_string()],
            users_per_tenant: 5,
            groups_per_tenant: 2,
            include_enterprise_users: false,
            include_complex_groups: false,
        }
    }

    /// Set the tenant IDs for the scenario
    pub fn with_tenants(mut self, tenant_ids: Vec<String>) -> Self {
        self.tenant_ids = tenant_ids;
        self
    }

    /// Set the number of users per tenant
    pub fn with_users_per_tenant(mut self, count: usize) -> Self {
        self.users_per_tenant = count;
        self
    }

    /// Set the number of groups per tenant
    pub fn with_groups_per_tenant(mut self, count: usize) -> Self {
        self.groups_per_tenant = count;
        self
    }

    /// Include enterprise user attributes
    pub fn with_enterprise_users(mut self, include: bool) -> Self {
        self.include_enterprise_users = include;
        self
    }

    /// Include complex group structures with nested memberships
    pub fn with_complex_groups(mut self, include: bool) -> Self {
        self.include_complex_groups = include;
        self
    }

    /// Build the scenario and populate the provider
    pub async fn build_and_populate<P>(
        self,
        provider: P,
    ) -> Result<UnifiedTestHarness<P>, Box<dyn std::error::Error + Send + Sync>>
    where
        P: ResourceProvider + Send + Sync + 'static,
        P::Error: std::error::Error + Send + Sync + 'static,
    {
        let tenant_refs: Vec<&str> = self.tenant_ids.iter().map(String::as_str).collect();
        let harness = UnifiedTestHarness::new_multi_tenant(provider, &tenant_refs);

        // Populate with test data
        for tenant_id in &self.tenant_ids {
            // Create users
            for i in 0..self.users_per_tenant {
                let username = if self.include_enterprise_users {
                    format!("enterprise_user_{}_{}", tenant_id, i)
                } else {
                    format!("user_{}_{}", tenant_id, i)
                };

                harness.create_user(Some(tenant_id), &username).await?;
            }

            // Create groups
            for i in 0..self.groups_per_tenant {
                let group_name = format!("group_{}_{}", tenant_id, i);
                harness.create_group(Some(tenant_id), &group_name).await?;
            }
        }

        Ok(harness)
    }
}

impl Default for MultiTenantScenarioBuilder {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Provider Testing Suite
// ============================================================================

/// Comprehensive testing suite for provider implementations
pub struct ProviderTestingSuite;

impl ProviderTestingSuite {
    /// Run the complete test suite for a provider
    pub async fn run_comprehensive_tests<P>(
        provider: P,
    ) -> Result<TestResults, Box<dyn std::error::Error + Send + Sync>>
    where
        P: ResourceProvider + Send + Sync + 'static,
        P::Error: std::error::Error + Send + Sync + 'static,
    {
        let provider = Arc::new(provider);
        let mut results = TestResults::new();

        // Test 1: Basic CRUD operations
        println!("🧪 Running basic CRUD tests...");
        let crud_result = Self::test_basic_crud(Arc::clone(&provider)).await;
        results.add_test("basic_crud", crud_result.is_ok());
        if let Err(e) = crud_result {
            results.add_error("basic_crud", e);
        }

        // Test 2: Multi-tenant isolation
        println!("🧪 Running multi-tenant isolation tests...");
        let isolation_result = Self::test_multi_tenant_isolation(Arc::clone(&provider)).await;
        results.add_test("multi_tenant_isolation", isolation_result.is_ok());
        if let Err(e) = isolation_result {
            results.add_error("multi_tenant_isolation", e);
        }

        // Test 3: Concurrent operations
        println!("🧪 Running concurrent operations tests...");
        let concurrent_result = Self::test_concurrent_operations(Arc::clone(&provider)).await;
        results.add_test("concurrent_operations", concurrent_result.is_ok());
        if let Err(e) = concurrent_result {
            results.add_error("concurrent_operations", e);
        }

        // Test 4: Data integrity
        println!("🧪 Running data integrity tests...");
        let integrity_result = Self::test_data_integrity(provider).await;
        results.add_test("data_integrity", integrity_result.is_ok());
        if let Err(e) = integrity_result {
            results.add_error("data_integrity", e);
        }

        Ok(results)
    }

    /// Test basic CRUD operations
    async fn test_basic_crud<P>(
        provider: Arc<P>,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
    where
        P: ResourceProvider + Send + Sync + 'static,
        P::Error: std::error::Error + Send + Sync + 'static,
    {
        let harness = UnifiedTestHarness::from_arc_single_tenant(provider);
        let context = harness.default_context();

        // Create
        let user = harness.create_user(None, "crud_test_user").await?;
        let user_id = user.get_id().unwrap();

        // Read
        let retrieved = harness
            .provider
            .get_resource("User", user_id, context)
            .await
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?
            .ok_or("User should be retrievable")?;

        assert_eq!(retrieved.resource().get_id(), user.get_id());

        // Update
        let update_data = json!({
            "userName": "updated_crud_user",
            "active": false
        });

        let updated = harness
            .provider
            .update_resource("User", user_id, update_data, None, context)
            .await
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;

        assert_eq!(
            updated.resource().get_username().unwrap(),
            "updated_crud_user"
        );

        // Delete
        harness
            .provider
            .delete_resource("User", user_id, None, context)
            .await
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;

        // Verify deletion
        let deleted = harness
            .provider
            .get_resource("User", user_id, context)
            .await
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;

        if deleted.is_some() {
            return Err("User should be deleted".into());
        }

        Ok(())
    }

    /// Test multi-tenant isolation
    async fn test_multi_tenant_isolation<P>(
        provider: Arc<P>,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
    where
        P: ResourceProvider + Send + Sync + 'static,
        P::Error: std::error::Error + Send + Sync + 'static,
    {
        let harness =
            UnifiedTestHarness::from_arc_multi_tenant(provider, &["tenant_a", "tenant_b"]);

        // Create users in different tenants
        let user_a = harness
            .create_user(Some("tenant_a"), "isolation_user_a")
            .await?;
        let user_b = harness
            .create_user(Some("tenant_b"), "isolation_user_b")
            .await?;

        // Verify isolation
        harness
            .verify_tenant_isolation("tenant_a", "User", &user_a)
            .await?;
        harness
            .verify_tenant_isolation("tenant_b", "User", &user_b)
            .await?;

        Ok(())
    }

    /// Test concurrent operations
    async fn test_concurrent_operations<P>(
        provider: Arc<P>,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
    where
        P: ResourceProvider + Send + Sync + 'static,
        P::Error: std::error::Error + Send + Sync + 'static,
    {
        let tenant_count = 3;
        let operations_per_tenant = 5;

        // Create contexts for multiple tenants
        let mut contexts = HashMap::new();
        for i in 0..tenant_count {
            let tenant_id = format!("concurrent_tenant_{}", i);
            contexts.insert(tenant_id.clone(), create_multi_tenant_context(&tenant_id));
        }

        // Sequential operations for now (to avoid Send issues)
        let mut total_operations = 0;
        for (tenant_id, context) in &contexts {
            for i in 0..operations_per_tenant {
                let username = format!("concurrent_user_{}_{}", tenant_id, i);
                let user_data = create_test_user(&username);

                provider
                    .create_resource("User", user_data, context)
                    .await
                    .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;

                total_operations += 1;
            }
        }

        // Verify all operations completed
        if total_operations != tenant_count * operations_per_tenant {
            return Err("Not all concurrent operations completed successfully".into());
        }

        Ok(())
    }

    /// Test data integrity
    async fn test_data_integrity<P>(
        provider: Arc<P>,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
    where
        P: ResourceProvider + Send + Sync + 'static,
        P::Error: std::error::Error + Send + Sync + 'static,
    {
        let harness = UnifiedTestHarness::from_arc_single_tenant(provider);
        let context = harness.default_context();

        // Test with various data types and edge cases
        let test_cases = vec![
            ("user_with_unicode", "测试用户"),
            ("user_with_symbols", "user@domain.com"),
            ("user_with_spaces", "User With Spaces"),
            ("user_with_numbers", "user123456"),
        ];

        for (test_name, username) in test_cases {
            let user_data = create_test_user(username);

            let created = harness
                .provider
                .create_resource("User", user_data, context)
                .await
                .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;

            // Verify data integrity
            if created.resource().get_username().unwrap() != username {
                return Err(format!("Data integrity failed for test case: {}", test_name).into());
            }

            // Verify resource has proper structure
            if created.resource().get_id().is_none() {
                return Err(format!("Resource ID missing for test case: {}", test_name).into());
            }

            if created.resource().get_schemas().is_empty() {
                return Err(format!("Schemas missing for test case: {}", test_name).into());
            }
        }

        Ok(())
    }
}

// ============================================================================
// Test Results and Reporting
// ============================================================================

/// Container for test results
#[derive(Debug)]
pub struct TestResults {
    tests: HashMap<String, bool>,
    errors: HashMap<String, Box<dyn std::error::Error + Send + Sync>>,
    passed: usize,
    failed: usize,
}

impl TestResults {
    /// Create new test results container
    pub fn new() -> Self {
        Self {
            tests: HashMap::new(),
            errors: HashMap::new(),
            passed: 0,
            failed: 0,
        }
    }

    /// Add a test result
    pub fn add_test(&mut self, name: &str, passed: bool) {
        self.tests.insert(name.to_string(), passed);
        if passed {
            self.passed += 1;
        } else {
            self.failed += 1;
        }
    }

    /// Add an error for a test
    pub fn add_error(&mut self, name: &str, error: Box<dyn std::error::Error + Send + Sync>) {
        self.errors.insert(name.to_string(), error);
    }

    /// Get summary of results
    pub fn summary(&self) -> String {
        format!(
            "Test Results: {} passed, {} failed, {} total",
            self.passed,
            self.failed,
            self.passed + self.failed
        )
    }

    /// Check if all tests passed
    pub fn all_passed(&self) -> bool {
        self.failed == 0
    }

    /// Get list of failed tests
    pub fn failed_tests(&self) -> Vec<String> {
        self.tests
            .iter()
            .filter_map(|(name, &passed)| if !passed { Some(name.clone()) } else { None })
            .collect()
    }
}

impl Default for TestResults {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Create an enterprise user with additional attributes
fn create_enterprise_user(username: &str) -> Value {
    json!({
        "schemas": [
            "urn:ietf:params:scim:schemas:core:2.0:User",
            "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
        ],
        "userName": username,
        "displayName": format!("{} (Enterprise)", username),
        "active": true,
        "emails": [{
            "value": format!("{}@enterprise.com", username),
            "type": "work",
            "primary": true
        }],
        "name": {
            "formatted": format!("Enterprise User {}", username),
            "familyName": "User",
            "givenName": "Enterprise"
        },
        "title": "Senior Developer",
        "department": "Engineering",
        "organization": "Enterprise Corp",
        "manager": {
            "value": "manager@enterprise.com",
            "displayName": "Manager User"
        }
    })
}

/// Provider configuration for testing
#[derive(Debug, Clone)]
pub struct ProviderTestConfig {
    pub enable_concurrent_tests: bool,
    pub max_concurrent_operations: usize,
    pub test_data_size: TestDataSize,
    pub enable_performance_tests: bool,
}

impl ProviderTestConfig {
    /// Create default test configuration
    pub fn default() -> Self {
        Self {
            enable_concurrent_tests: true,
            max_concurrent_operations: 100,
            test_data_size: TestDataSize::Small,
            enable_performance_tests: false,
        }
    }

    /// Create configuration for performance testing
    pub fn for_performance() -> Self {
        Self {
            enable_concurrent_tests: true,
            max_concurrent_operations: 1000,
            test_data_size: TestDataSize::Large,
            enable_performance_tests: true,
        }
    }

    /// Create minimal configuration for quick tests
    pub fn minimal() -> Self {
        Self {
            enable_concurrent_tests: false,
            max_concurrent_operations: 10,
            test_data_size: TestDataSize::Minimal,
            enable_performance_tests: false,
        }
    }
}

/// Test data size configurations
#[derive(Debug, Clone)]
pub enum TestDataSize {
    Minimal, // 1-2 items per category
    Small,   // 5-10 items per category
    Medium,  // 50-100 items per category
    Large,   // 500-1000 items per category
}

impl TestDataSize {
    /// Get the number of items for this size
    pub fn item_count(&self) -> usize {
        match self {
            TestDataSize::Minimal => 2,
            TestDataSize::Small => 10,
            TestDataSize::Medium => 100,
            TestDataSize::Large => 1000,
        }
    }
}

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

    #[test]
    fn test_scenario_builder() {
        let builder = MultiTenantScenarioBuilder::new()
            .with_tenants(vec!["test1".to_string(), "test2".to_string()])
            .with_users_per_tenant(3)
            .with_groups_per_tenant(1);

        assert_eq!(builder.tenant_ids.len(), 2);
        assert_eq!(builder.users_per_tenant, 3);
        assert_eq!(builder.groups_per_tenant, 1);
    }

    #[test]
    fn test_results_tracking() {
        let mut results = TestResults::new();

        results.add_test("test1", true);
        results.add_test("test2", false);

        assert_eq!(results.passed, 1);
        assert_eq!(results.failed, 1);
        assert!(!results.all_passed());

        let failed = results.failed_tests();
        assert_eq!(failed.len(), 1);
        assert!(failed.contains(&"test2".to_string()));
    }

    #[test]
    fn test_enterprise_user_creation() {
        let user = create_enterprise_user("test_enterprise");
        assert_eq!(user["userName"], "test_enterprise");
        assert!(user["schemas"].as_array().unwrap().len() >= 2);
        assert!(user.get("title").is_some());
        assert!(user.get("department").is_some());
    }

    #[test]
    fn test_provider_config() {
        let config = ProviderTestConfig::default();
        assert!(config.enable_concurrent_tests);

        let perf_config = ProviderTestConfig::for_performance();
        assert!(perf_config.enable_performance_tests);
        assert_eq!(perf_config.max_concurrent_operations, 1000);

        let minimal_config = ProviderTestConfig::minimal();
        assert!(!minimal_config.enable_concurrent_tests);
    }

    #[test]
    fn test_data_size_configs() {
        assert_eq!(TestDataSize::Minimal.item_count(), 2);
        assert_eq!(TestDataSize::Small.item_count(), 10);
        assert_eq!(TestDataSize::Medium.item_count(), 100);
        assert_eq!(TestDataSize::Large.item_count(), 1000);
    }
}