|
| 1 | +//! JWT integration tests |
| 2 | +//! |
| 3 | +//! Tests for token creation and validation using mcpmux-gateway auth module. |
| 4 | +
|
| 5 | +use mcpmux_gateway::auth::{create_access_token, create_refresh_token, validate_token}; |
| 6 | + |
| 7 | +const TEST_SECRET: &[u8] = b"test_secret_key_that_is_32_bytes"; |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn test_create_access_token() { |
| 11 | + let token = create_access_token("client-123", Some("mcp read write"), 3600, TEST_SECRET); |
| 12 | + |
| 13 | + // Token should have two parts separated by '.' |
| 14 | + let parts: Vec<&str> = token.split('.').collect(); |
| 15 | + assert_eq!(parts.len(), 2, "Token should have payload.signature format"); |
| 16 | + |
| 17 | + // Both parts should be valid base64 |
| 18 | + assert!(!parts[0].is_empty()); |
| 19 | + assert!(!parts[1].is_empty()); |
| 20 | +} |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn test_create_refresh_token() { |
| 24 | + let token = create_refresh_token("client-123", Some("mcp"), TEST_SECRET); |
| 25 | + |
| 26 | + let parts: Vec<&str> = token.split('.').collect(); |
| 27 | + assert_eq!(parts.len(), 2); |
| 28 | +} |
| 29 | + |
| 30 | +#[test] |
| 31 | +fn test_validate_token_success() { |
| 32 | + let token = create_access_token("test-client", Some("read"), 3600, TEST_SECRET); |
| 33 | + |
| 34 | + let claims = validate_token(&token, TEST_SECRET); |
| 35 | + assert!(claims.is_some(), "Valid token should return claims"); |
| 36 | + |
| 37 | + let claims = claims.unwrap(); |
| 38 | + assert_eq!(claims.client_id, "test-client"); |
| 39 | + assert_eq!(claims.scope, Some("read".to_string())); |
| 40 | +} |
| 41 | + |
| 42 | +#[test] |
| 43 | +fn test_validate_token_wrong_secret() { |
| 44 | + let token = create_access_token("client", None, 3600, TEST_SECRET); |
| 45 | + let wrong_secret = b"different_secret_key_32_bytes!!!"; |
| 46 | + |
| 47 | + let claims = validate_token(&token, wrong_secret); |
| 48 | + assert!(claims.is_none(), "Token signed with different secret should fail"); |
| 49 | +} |
| 50 | + |
| 51 | +#[test] |
| 52 | +fn test_validate_expired_token() { |
| 53 | + // Create token that expired 1 hour ago |
| 54 | + let token = create_access_token("client", None, -3600, TEST_SECRET); |
| 55 | + |
| 56 | + let claims = validate_token(&token, TEST_SECRET); |
| 57 | + assert!(claims.is_none(), "Expired token should fail validation"); |
| 58 | +} |
| 59 | + |
| 60 | +#[test] |
| 61 | +fn test_validate_malformed_token() { |
| 62 | + let claims = validate_token("not.a.valid.token", TEST_SECRET); |
| 63 | + assert!(claims.is_none()); |
| 64 | + |
| 65 | + let claims = validate_token("", TEST_SECRET); |
| 66 | + assert!(claims.is_none()); |
| 67 | + |
| 68 | + let claims = validate_token("single_part_token", TEST_SECRET); |
| 69 | + assert!(claims.is_none()); |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn test_token_contains_timestamps() { |
| 74 | + let token = create_access_token("client", None, 3600, TEST_SECRET); |
| 75 | + |
| 76 | + let claims = validate_token(&token, TEST_SECRET).unwrap(); |
| 77 | + |
| 78 | + // iat should be recent (within last minute) |
| 79 | + let now = chrono::Utc::now().timestamp(); |
| 80 | + assert!(claims.iat >= now - 60); |
| 81 | + assert!(claims.iat <= now); |
| 82 | + |
| 83 | + // exp should be in the future |
| 84 | + assert!(claims.exp > now); |
| 85 | +} |
| 86 | + |
| 87 | +#[test] |
| 88 | +fn test_token_scope_optional() { |
| 89 | + // Token without scope |
| 90 | + let token = create_access_token("client", None, 3600, TEST_SECRET); |
| 91 | + let claims = validate_token(&token, TEST_SECRET).unwrap(); |
| 92 | + |
| 93 | + // Scope can be None when passed as null |
| 94 | + // The implementation may serialize None as null in JSON |
| 95 | + // Just verify token validates |
| 96 | + assert_eq!(claims.client_id, "client"); |
| 97 | +} |
| 98 | + |
| 99 | +#[test] |
| 100 | +fn test_different_clients_different_tokens() { |
| 101 | + let token1 = create_access_token("client-a", None, 3600, TEST_SECRET); |
| 102 | + let token2 = create_access_token("client-b", None, 3600, TEST_SECRET); |
| 103 | + |
| 104 | + assert_ne!(token1, token2); |
| 105 | + |
| 106 | + let claims1 = validate_token(&token1, TEST_SECRET).unwrap(); |
| 107 | + let claims2 = validate_token(&token2, TEST_SECRET).unwrap(); |
| 108 | + |
| 109 | + assert_eq!(claims1.client_id, "client-a"); |
| 110 | + assert_eq!(claims2.client_id, "client-b"); |
| 111 | +} |
0 commit comments