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