|
| 1 | +use common::{ |
| 2 | + assert_obj, |
| 3 | + types::MemberId, |
| 4 | + value::ConvexValue, |
| 5 | +}; |
| 6 | +use keybroker::{ |
| 7 | + testing::TestUserIdentity, |
| 8 | + AdminIdentity, |
| 9 | + Identity, |
| 10 | + UserIdentity, |
| 11 | +}; |
| 12 | +use must_let::must_let; |
| 13 | +use runtime::testing::TestRuntime; |
| 14 | +use crate::test_helpers::UdfTest; |
| 15 | + |
| 16 | +#[convex_macro::test_runtime] |
| 17 | +async fn test_get_user_identity_debug_with_plaintext_user(rt: TestRuntime) -> anyhow::Result<()> { |
| 18 | + UdfTest::run_test_with_isolate2(rt, async move |t| { |
| 19 | + // Test that getUserIdentityDebug works with regular user identity |
| 20 | + let identity = Identity::user(UserIdentity::test()); |
| 21 | + let (result, outcome) = t |
| 22 | + .query_outcome("auth:getUserIdentityDebug", assert_obj!(), identity.clone()) |
| 23 | + .await?; |
| 24 | + |
| 25 | + // Should return the user identity, not an error |
| 26 | + must_let!(let ConvexValue::Object(obj) = result); |
| 27 | + assert!(obj.get("name").is_some()); |
| 28 | + assert!(outcome.observed_identity); |
| 29 | + |
| 30 | + // Test with PlaintextUser identity - should return null (no JWT to debug) |
| 31 | + let plaintext_identity = Identity::PlaintextUser("test-plaintext-token".to_string()); |
| 32 | + let (result, outcome) = t |
| 33 | + .query_outcome("auth:getUserIdentityDebug", assert_obj!(), plaintext_identity) |
| 34 | + .await?; |
| 35 | + |
| 36 | + assert_eq!(result, ConvexValue::Null); |
| 37 | + assert!(outcome.observed_identity); |
| 38 | + |
| 39 | + Ok(()) |
| 40 | + }) |
| 41 | + .await |
| 42 | +} |
| 43 | + |
| 44 | +#[convex_macro::test_runtime] |
| 45 | +async fn test_get_user_identity_insecure_with_different_identities(rt: TestRuntime) -> anyhow::Result<()> { |
| 46 | + UdfTest::run_test_with_isolate2(rt, async move |t| { |
| 47 | + // Test with PlaintextUser identity - should return the plaintext token |
| 48 | + let plaintext_token = "my-test-plaintext-token-12345"; |
| 49 | + let plaintext_identity = Identity::PlaintextUser(plaintext_token.to_string()); |
| 50 | + let (result, outcome) = t |
| 51 | + .query_outcome("auth:getUserIdentityInsecure", assert_obj!(), plaintext_identity) |
| 52 | + .await?; |
| 53 | + |
| 54 | + must_let!(let ConvexValue::String(token) = result); |
| 55 | + assert_eq!(&*token, plaintext_token); |
| 56 | + assert!(outcome.observed_identity == false); |
| 57 | + |
| 58 | + // Test with regular User identity - should return null |
| 59 | + let user_identity = Identity::user(UserIdentity::test()); |
| 60 | + let (result, outcome) = t |
| 61 | + .query_outcome("auth:getUserIdentityInsecure", assert_obj!(), user_identity) |
| 62 | + .await?; |
| 63 | + |
| 64 | + assert_eq!(result, ConvexValue::Null); |
| 65 | + assert!(outcome.observed_identity == false); |
| 66 | + |
| 67 | + // Test with System identity - should return null |
| 68 | + let system_identity = Identity::system(); |
| 69 | + let (result, outcome) = t |
| 70 | + .query_outcome("auth:getUserIdentityInsecure", assert_obj!(), system_identity) |
| 71 | + .await?; |
| 72 | + |
| 73 | + assert_eq!(result, ConvexValue::Null); |
| 74 | + assert!(outcome.observed_identity == false); |
| 75 | + |
| 76 | + // Test with Admin identity - should return null |
| 77 | + let admin_identity = Identity::InstanceAdmin(AdminIdentity::new_for_test_only( |
| 78 | + "test-admin-key".to_string(), |
| 79 | + MemberId(1), |
| 80 | + )); |
| 81 | + let (result, outcome) = t |
| 82 | + .query_outcome("auth:getUserIdentityInsecure", assert_obj!(), admin_identity) |
| 83 | + .await?; |
| 84 | + |
| 85 | + assert_eq!(result, ConvexValue::Null); |
| 86 | + assert!(outcome.observed_identity == false); |
| 87 | + |
| 88 | + // Test with Unknown identity - should return null |
| 89 | + let unknown_identity = Identity::Unknown(None); |
| 90 | + let (result, outcome) = t |
| 91 | + .query_outcome("auth:getUserIdentityInsecure", assert_obj!(), unknown_identity) |
| 92 | + .await?; |
| 93 | + |
| 94 | + assert_eq!(result, ConvexValue::Null); |
| 95 | + assert!(outcome.observed_identity == false); |
| 96 | + |
| 97 | + Ok(()) |
| 98 | + }) |
| 99 | + .await |
| 100 | +} |
| 101 | + |
| 102 | +#[convex_macro::test_runtime] |
| 103 | +async fn test_plaintext_user_admin_access_restriction(rt: TestRuntime) -> anyhow::Result<()> { |
| 104 | + UdfTest::run_test_with_isolate2(rt, async move |t| { |
| 105 | + // Test that PlaintextUser identity cannot access admin-protected functions |
| 106 | + let plaintext_identity = Identity::PlaintextUser("admin-wannabe-token".to_string()); |
| 107 | + |
| 108 | + // This test would verify that PlaintextUser identities are properly rejected |
| 109 | + // by the must_be_admin_internal function changes |
| 110 | + let (outcome, _token) = t |
| 111 | + .raw_query("auth:testAdminAccess", vec![ConvexValue::Object(assert_obj!())], plaintext_identity, None) |
| 112 | + .await?; |
| 113 | + |
| 114 | + // Should fail with admin access error |
| 115 | + assert!(outcome.result.is_err()); |
| 116 | + let error = outcome.result.unwrap_err(); |
| 117 | + let error_str = error.to_string(); |
| 118 | + assert!(error_str.contains("BadDeployKey") || error_str.contains("invalid")); |
| 119 | + |
| 120 | + // Compare with regular admin identity which should succeed |
| 121 | + let admin_identity = Identity::InstanceAdmin(AdminIdentity::new_for_test_only( |
| 122 | + "valid-admin-key".to_string(), |
| 123 | + MemberId(1), |
| 124 | + )); |
| 125 | + |
| 126 | + // This should succeed for admin identities |
| 127 | + let (admin_outcome, _token) = t |
| 128 | + .raw_query("auth:testAdminAccess", vec![ConvexValue::Object(assert_obj!())], admin_identity, None) |
| 129 | + .await?; |
| 130 | + |
| 131 | + // Admin should have access |
| 132 | + assert!(admin_outcome.result.is_ok()); |
| 133 | + |
| 134 | + Ok(()) |
| 135 | + }) |
| 136 | + .await |
| 137 | +} |
| 138 | + |
| 139 | +#[convex_macro::test_runtime] |
| 140 | +async fn test_plaintext_user_identity_creation_and_handling(rt: TestRuntime) -> anyhow::Result<()> { |
| 141 | + UdfTest::run_test_with_isolate2(rt, async move |t| { |
| 142 | + let test_token = "test-plaintext-auth-token-xyz"; |
| 143 | + let plaintext_identity = Identity::PlaintextUser(test_token.to_string()); |
| 144 | + |
| 145 | + // Test that PlaintextUser identity is properly handled in queries |
| 146 | + let (result, outcome) = t |
| 147 | + .query_outcome("auth:getIdentityType", assert_obj!(), plaintext_identity.clone()) |
| 148 | + .await?; |
| 149 | + |
| 150 | + // Should indicate it's a PlaintextUser identity |
| 151 | + must_let!(let ConvexValue::String(identity_type) = result); |
| 152 | + assert_eq!(&*identity_type, "PlaintextUser"); |
| 153 | + assert!(outcome.observed_identity); |
| 154 | + |
| 155 | + // Test that getUserIdentityInsecure returns the correct token |
| 156 | + let (token_result, _) = t |
| 157 | + .query_outcome("auth:getUserIdentityInsecure", assert_obj!(), plaintext_identity) |
| 158 | + .await?; |
| 159 | + |
| 160 | + must_let!(let ConvexValue::String(returned_token) = token_result); |
| 161 | + assert_eq!(&*returned_token, test_token); |
| 162 | + |
| 163 | + Ok(()) |
| 164 | + }) |
| 165 | + .await |
| 166 | +} |
| 167 | + |
| 168 | +#[convex_macro::test_runtime] |
| 169 | +async fn test_get_user_identity_debug_error_scenarios(rt: TestRuntime) -> anyhow::Result<()> { |
| 170 | + UdfTest::run_test_with_isolate2(rt, async move |t| { |
| 171 | + // Test getUserIdentityDebug with Unknown identity containing error |
| 172 | + let error_message = "JWT validation failed: token expired"; |
| 173 | + let unknown_identity_with_error = Identity::Unknown(Some( |
| 174 | + errors::ErrorMetadata::bad_request("InvalidJWT", error_message) |
| 175 | + )); |
| 176 | + |
| 177 | + let (result, outcome) = t |
| 178 | + .query_outcome("auth:getUserIdentityDebug", assert_obj!(), unknown_identity_with_error) |
| 179 | + .await?; |
| 180 | + |
| 181 | + // Should return structured error information |
| 182 | + must_let!(let ConvexValue::Object(error_obj) = result); |
| 183 | + assert!(error_obj.get("error").is_some()); |
| 184 | + must_let!(let ConvexValue::Object(error_obj_inner) = error_obj.get("error").unwrap()); |
| 185 | + assert!(error_obj_inner.get("code").is_some()); |
| 186 | + assert!(error_obj_inner.get("message").is_some()); |
| 187 | + assert!(outcome.observed_identity); |
| 188 | + |
| 189 | + // Test with Unknown identity without error - should return null |
| 190 | + let unknown_identity = Identity::Unknown(None); |
| 191 | + let (result, outcome) = t |
| 192 | + .query_outcome("auth:getUserIdentityDebug", assert_obj!(), unknown_identity) |
| 193 | + .await?; |
| 194 | + |
| 195 | + assert_eq!(result, ConvexValue::Null); |
| 196 | + assert!(outcome.observed_identity); |
| 197 | + |
| 198 | + Ok(()) |
| 199 | + }) |
| 200 | + .await |
| 201 | +} |
0 commit comments