github-bot-sdk 0.2.1

A comprehensive Rust SDK for GitHub App integration with authentication, webhooks, and API client
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
//! Tests for GitHub App token management.

use super::super::{GitHubAppId, KeyAlgorithm, PrivateKey};
use super::*;
use crate::error::{ApiError, CacheError, SecretError, SigningError, ValidationError};
use async_trait::async_trait;
use chrono::{Duration, Utc};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

// ============================================================================
// Mock Implementations
// ============================================================================

/// Mock secret provider for testing.
struct MockSecretProvider {
    app_id: GitHubAppId,
    private_key: PrivateKey,
    webhook_secret: String,
}

impl MockSecretProvider {
    fn new(app_id: u64) -> Self {
        Self {
            app_id: GitHubAppId::new(app_id),
            private_key: PrivateKey::new(b"mock-private-key-data".to_vec(), KeyAlgorithm::RS256),
            webhook_secret: "mock-webhook-secret".to_string(),
        }
    }
}

#[async_trait]
impl SecretProvider for MockSecretProvider {
    async fn get_private_key(&self) -> Result<PrivateKey, SecretError> {
        Ok(self.private_key.clone())
    }

    async fn get_app_id(&self) -> Result<GitHubAppId, SecretError> {
        Ok(self.app_id)
    }

    async fn get_webhook_secret(&self) -> Result<String, SecretError> {
        Ok(self.webhook_secret.clone())
    }

    fn cache_duration(&self) -> Duration {
        Duration::minutes(5)
    }
}

/// Mock JWT signer for testing.
struct MockJwtSigner {
    should_fail: bool,
}

impl MockJwtSigner {
    fn new() -> Self {
        Self { should_fail: false }
    }

    fn with_failure() -> Self {
        Self { should_fail: true }
    }
}

#[async_trait]
impl JwtSigner for MockJwtSigner {
    async fn sign_jwt(
        &self,
        claims: super::JwtClaims,
        _private_key: &PrivateKey,
    ) -> Result<JsonWebToken, SigningError> {
        if self.should_fail {
            return Err(SigningError::SigningFailed {
                message: "Mock signing failure".to_string(),
            });
        }

        let expires_at = Utc::now() + Duration::seconds(claims.exp - claims.iat);
        Ok(JsonWebToken::new(
            format!("mock.jwt.{}", claims.iss.as_u64()),
            claims.iss,
            expires_at,
        ))
    }

    fn validate_private_key(&self, _key: &PrivateKey) -> Result<(), ValidationError> {
        Ok(())
    }
}

/// Mock GitHub API client for testing.
struct MockGitHubApiClient {
    installation_tokens: Arc<Mutex<HashMap<InstallationId, InstallationToken>>>,
    installations: Vec<Installation>,
    should_fail: bool,
}

impl MockGitHubApiClient {
    fn new() -> Self {
        Self {
            installation_tokens: Arc::new(Mutex::new(HashMap::new())),
            installations: Vec::new(),
            should_fail: false,
        }
    }

    fn with_installation(mut self, installation: Installation) -> Self {
        self.installations.push(installation);
        self
    }

    fn with_failure() -> Self {
        Self {
            installation_tokens: Arc::new(Mutex::new(HashMap::new())),
            installations: Vec::new(),
            should_fail: true,
        }
    }
}

#[async_trait]
impl GitHubApiClient for MockGitHubApiClient {
    async fn create_installation_access_token(
        &self,
        installation_id: InstallationId,
        _jwt: &JsonWebToken,
    ) -> Result<InstallationToken, ApiError> {
        if self.should_fail {
            return Err(ApiError::HttpError {
                status: 500,
                message: "Mock API failure".to_string(),
            });
        }

        let token = InstallationToken::new(
            format!("ghs_mock_token_{}", installation_id.as_u64()),
            installation_id,
            Utc::now() + Duration::hours(1),
            super::super::InstallationPermissions::default(),
            vec![],
        );

        self.installation_tokens
            .lock()
            .unwrap()
            .insert(installation_id, token.clone());

        Ok(token)
    }

    async fn list_app_installations(
        &self,
        _jwt: &JsonWebToken,
    ) -> Result<Vec<Installation>, ApiError> {
        if self.should_fail {
            return Err(ApiError::HttpError {
                status: 500,
                message: "Mock API failure".to_string(),
            });
        }

        Ok(self.installations.clone())
    }

    async fn list_installation_repositories(
        &self,
        _installation_id: InstallationId,
        _token: &InstallationToken,
    ) -> Result<Vec<Repository>, ApiError> {
        if self.should_fail {
            return Err(ApiError::HttpError {
                status: 500,
                message: "Mock API failure".to_string(),
            });
        }

        Ok(vec![])
    }

    async fn get_repository(
        &self,
        _repo_id: super::super::RepositoryId,
        _token: &InstallationToken,
    ) -> Result<Repository, ApiError> {
        unimplemented!("Not needed for token tests")
    }

    async fn get_rate_limit(
        &self,
        _token: &InstallationToken,
    ) -> Result<super::super::RateLimitInfo, ApiError> {
        unimplemented!("Not needed for token tests")
    }
}

/// Mock token cache for testing.
struct MockTokenCache {
    jwt_cache: Arc<Mutex<HashMap<GitHubAppId, JsonWebToken>>>,
    installation_cache: Arc<Mutex<HashMap<InstallationId, InstallationToken>>>,
    should_fail: bool,
}

impl MockTokenCache {
    fn new() -> Self {
        Self {
            jwt_cache: Arc::new(Mutex::new(HashMap::new())),
            installation_cache: Arc::new(Mutex::new(HashMap::new())),
            should_fail: false,
        }
    }

    fn with_failure() -> Self {
        Self {
            jwt_cache: Arc::new(Mutex::new(HashMap::new())),
            installation_cache: Arc::new(Mutex::new(HashMap::new())),
            should_fail: true,
        }
    }
}

#[async_trait]
impl TokenCache for MockTokenCache {
    async fn get_jwt(&self, app_id: GitHubAppId) -> Result<Option<JsonWebToken>, CacheError> {
        if self.should_fail {
            return Err(CacheError::OperationFailed {
                message: "Mock cache failure".to_string(),
            });
        }

        Ok(self.jwt_cache.lock().unwrap().get(&app_id).cloned())
    }

    async fn store_jwt(&self, jwt: JsonWebToken) -> Result<(), CacheError> {
        if self.should_fail {
            return Err(CacheError::OperationFailed {
                message: "Mock cache failure".to_string(),
            });
        }

        self.jwt_cache
            .lock()
            .unwrap()
            .insert(jwt.app_id(), jwt.clone());
        Ok(())
    }

    async fn get_installation_token(
        &self,
        installation_id: InstallationId,
    ) -> Result<Option<InstallationToken>, CacheError> {
        if self.should_fail {
            return Err(CacheError::OperationFailed {
                message: "Mock cache failure".to_string(),
            });
        }

        Ok(self
            .installation_cache
            .lock()
            .unwrap()
            .get(&installation_id)
            .cloned())
    }

    async fn store_installation_token(&self, token: InstallationToken) -> Result<(), CacheError> {
        if self.should_fail {
            return Err(CacheError::OperationFailed {
                message: "Mock cache failure".to_string(),
            });
        }

        self.installation_cache
            .lock()
            .unwrap()
            .insert(token.installation_id(), token.clone());
        Ok(())
    }

    async fn invalidate_installation_token(
        &self,
        installation_id: InstallationId,
    ) -> Result<(), CacheError> {
        if self.should_fail {
            return Err(CacheError::OperationFailed {
                message: "Mock cache failure".to_string(),
            });
        }

        self.installation_cache
            .lock()
            .unwrap()
            .remove(&installation_id);
        Ok(())
    }

    fn cleanup_expired_tokens(&self) {
        // No-op for mock
    }
}

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

fn create_test_auth(
) -> GitHubAppAuth<MockSecretProvider, MockJwtSigner, MockGitHubApiClient, MockTokenCache> {
    let config = AuthConfig::default();
    GitHubAppAuth::new(
        MockSecretProvider::new(12345),
        MockJwtSigner::new(),
        MockGitHubApiClient::new(),
        MockTokenCache::new(),
        config,
    )
}

fn create_mock_installation(id: u64) -> Installation {
    use super::super::{Account, GitHubAppId, RepositorySelection, TargetType};

    Installation {
        id: InstallationId::new(id),
        account: Account {
            id: super::super::UserId::new(1),
            login: "test-org".to_string(),
            account_type: TargetType::Organization,
            avatar_url: Some("https://github.com/test-org.png".to_string()),
            html_url: "https://github.com/test-org".to_string(),
        },
        access_tokens_url: format!(
            "https://api.github.com/app/installations/{}/access_tokens",
            id
        ),
        repositories_url: "https://api.github.com/installation/repositories".to_string(),
        html_url: format!("https://github.com/settings/installations/{}", id),
        app_id: GitHubAppId::new(123),
        target_type: TargetType::Organization,
        repository_selection: RepositorySelection::All,
        permissions: super::super::InstallationPermissions::default(),
        events: vec!["push".to_string(), "pull_request".to_string()],
        created_at: Utc::now(),
        updated_at: Utc::now(),
        single_file_name: None,
        has_multiple_single_files: false,
        suspended_at: None,
        suspended_by: None,
    }
}

// ============================================================================
// AuthConfig Tests
// ============================================================================

mod auth_config_tests {
    use super::*;

    /// Verify default AuthConfig has correct values.
    #[test]
    fn test_default_auth_config() {
        let config = AuthConfig::default();

        assert_eq!(config.jwt_expiration, Duration::minutes(10));
        assert_eq!(config.jwt_refresh_margin, Duration::minutes(2));
        assert_eq!(config.token_cache_ttl, Duration::minutes(55));
        assert_eq!(config.token_refresh_margin, Duration::minutes(5));
        assert_eq!(config.github_api_url, "https://api.github.com");
        assert_eq!(config.user_agent, "github-bot-sdk");
    }
}

// ============================================================================
// GitHubAppAuth Construction Tests
// ============================================================================

mod construction_tests {
    use super::*;

    /// Verify GitHubAppAuth can be constructed with all dependencies.
    #[test]
    fn test_create_github_app_auth() {
        let auth = create_test_auth();
        assert_eq!(auth.config().jwt_expiration, Duration::minutes(10));
    }

    /// Verify custom configuration is preserved.
    #[test]
    fn test_create_with_custom_config() {
        let config = AuthConfig {
            github_api_url: "https://github.enterprise.local/api/v3".to_string(),
            user_agent: "my-bot/1.0".to_string(),
            ..Default::default()
        };

        let auth = GitHubAppAuth::new(
            MockSecretProvider::new(12345),
            MockJwtSigner::new(),
            MockGitHubApiClient::new(),
            MockTokenCache::new(),
            config,
        );

        assert_eq!(
            auth.config().github_api_url,
            "https://github.enterprise.local/api/v3"
        );
        assert_eq!(auth.config().user_agent, "my-bot/1.0");
    }
}

// ============================================================================
// App Token (JWT) Tests
// ============================================================================

mod app_token_tests {
    use super::*;

    /// Verify app_token() generates valid JWT.
    ///
    /// Tests assertion #1: JWT Token Generation
    #[tokio::test]
    async fn test_app_token_generates_jwt() {
        let auth = create_test_auth();

        let jwt = auth.app_token().await.expect("Should generate JWT");

        assert_eq!(jwt.app_id(), GitHubAppId::new(12345));
        assert!(!jwt.is_expired());
        assert!(jwt.token().starts_with("mock.jwt."));
    }

    /// Verify app_token() uses cache for subsequent calls.
    #[tokio::test]
    async fn test_app_token_uses_cache() {
        let auth = create_test_auth();

        let jwt1 = auth.app_token().await.expect("First call should succeed");
        let jwt2 = auth.app_token().await.expect("Second call should succeed");

        // Should be the same token from cache
        assert_eq!(jwt1.token(), jwt2.token());
    }

    /// Verify app_token() refreshes expired JWT.
    #[tokio::test]
    async fn test_app_token_refreshes_expired() {
        let auth = create_test_auth();

        // Get initial token
        let jwt1 = auth.app_token().await.expect("Should generate JWT");

        // Simulate expiration by waiting (in real code, would mock time)
        // For now, test that fresh tokens are generated
        assert!(!jwt1.is_expired());
    }

    /// Verify app_token() handles signing failure.
    ///
    /// Tests assertion #3: JWT Token with Invalid Private Key
    #[tokio::test]
    async fn test_app_token_signing_failure() {
        let auth = GitHubAppAuth::new(
            MockSecretProvider::new(12345),
            MockJwtSigner::with_failure(),
            MockGitHubApiClient::new(),
            MockTokenCache::new(),
            AuthConfig::default(),
        );

        let result = auth.app_token().await;

        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), AuthError::SigningError(_)));
    }

    /// Verify app_token() handles cache failure gracefully.
    #[tokio::test]
    async fn test_app_token_cache_failure_fallback() {
        let auth = GitHubAppAuth::new(
            MockSecretProvider::new(12345),
            MockJwtSigner::new(),
            MockGitHubApiClient::new(),
            MockTokenCache::with_failure(),
            AuthConfig::default(),
        );

        // Should still generate token even if cache fails
        let result = auth.app_token().await;
        assert!(result.is_err() || result.unwrap().app_id() == GitHubAppId::new(12345));
    }
}

// ============================================================================
// Installation Token Tests
// ============================================================================

mod installation_token_tests {
    use super::*;

    /// Verify installation_token() exchanges JWT for installation token.
    ///
    /// Tests assertion #4: Installation Token Retrieval
    #[tokio::test]
    async fn test_installation_token_exchange() {
        let auth = create_test_auth();
        let installation_id = InstallationId::new(54321);

        let token = auth
            .installation_token(installation_id)
            .await
            .expect("Should get installation token");

        assert_eq!(token.installation_id(), installation_id);
        assert!(!token.is_expired());
        assert!(token.token().contains("ghs_mock_token"));
    }

    /// Verify installation_token() caches tokens.
    #[tokio::test]
    async fn test_installation_token_caching() {
        let auth = create_test_auth();
        let installation_id = InstallationId::new(54321);

        let token1 = auth
            .installation_token(installation_id)
            .await
            .expect("First call");
        let token2 = auth
            .installation_token(installation_id)
            .await
            .expect("Second call");

        // Should be same cached token
        assert_eq!(token1.token(), token2.token());
    }

    /// Verify installation_token() handles non-existent installation.
    ///
    /// Tests assertion #6: Installation Token for Non-Existent Installation
    #[tokio::test]
    async fn test_installation_token_not_found() {
        let auth = GitHubAppAuth::new(
            MockSecretProvider::new(12345),
            MockJwtSigner::new(),
            MockGitHubApiClient::with_failure(),
            MockTokenCache::new(),
            AuthConfig::default(),
        );

        let result = auth.installation_token(InstallationId::new(99999)).await;

        assert!(result.is_err());
        // In real implementation, should be InstallationNotFound error
    }

    /// Verify installation_token() refreshes expired tokens.
    ///
    /// Tests assertion #7: Token Cache Expiry Handling
    #[tokio::test]
    async fn test_installation_token_refresh_expired() {
        let auth = create_test_auth();
        let installation_id = InstallationId::new(54321);

        // Get initial token
        let token1 = auth
            .installation_token(installation_id)
            .await
            .expect("Should get token");

        assert!(!token1.is_expired());
    }
}

// ============================================================================
// Refresh Token Tests
// ============================================================================

mod refresh_token_tests {
    use super::*;

    /// Verify refresh_installation_token() bypasses cache.
    #[tokio::test]
    async fn test_refresh_bypasses_cache() {
        let auth = create_test_auth();
        let installation_id = InstallationId::new(54321);

        // Get cached token
        let _token1 = auth.installation_token(installation_id).await.unwrap();

        // Force refresh
        let token2 = auth
            .refresh_installation_token(installation_id)
            .await
            .unwrap();

        assert_eq!(token2.installation_id(), installation_id);
    }
}

// ============================================================================
// List Installations Tests
// ============================================================================

mod list_installations_tests {
    use super::*;

    /// Verify list_installations() returns installations.
    ///
    /// Tests assertion #2: App-Level API Operations
    #[tokio::test]
    async fn test_list_installations() {
        let installation = create_mock_installation(123);

        let auth = GitHubAppAuth::new(
            MockSecretProvider::new(12345),
            MockJwtSigner::new(),
            MockGitHubApiClient::new().with_installation(installation.clone()),
            MockTokenCache::new(),
            AuthConfig::default(),
        );

        let installations = auth
            .list_installations()
            .await
            .expect("Should list installations");

        assert_eq!(installations.len(), 1);
        assert_eq!(installations[0].id, installation.id);
    }

    /// Verify list_installations() handles API failures.
    #[tokio::test]
    async fn test_list_installations_api_failure() {
        let auth = GitHubAppAuth::new(
            MockSecretProvider::new(12345),
            MockJwtSigner::new(),
            MockGitHubApiClient::with_failure(),
            MockTokenCache::new(),
            AuthConfig::default(),
        );

        let result = auth.list_installations().await;

        assert!(result.is_err());
    }
}

// ============================================================================
// Get Installation Repositories Tests
// ============================================================================

mod get_repositories_tests {
    use super::*;

    /// Verify get_installation_repositories() returns repositories.
    ///
    /// Tests assertion #5: Installation-Level API Operations
    #[tokio::test]
    async fn test_get_installation_repositories() {
        let auth = create_test_auth();
        let installation_id = InstallationId::new(54321);

        let repos = auth
            .get_installation_repositories(installation_id)
            .await
            .expect("Should get repositories");

        // Mock returns empty list
        assert_eq!(repos.len(), 0);
    }

    /// Verify get_installation_repositories() handles API failures.
    #[tokio::test]
    async fn test_get_installation_repositories_failure() {
        let auth = GitHubAppAuth::new(
            MockSecretProvider::new(12345),
            MockJwtSigner::new(),
            MockGitHubApiClient::with_failure(),
            MockTokenCache::new(),
            AuthConfig::default(),
        );

        let result = auth
            .get_installation_repositories(InstallationId::new(123))
            .await;

        assert!(result.is_err());
    }
}