@@ -3,6 +3,8 @@ use firestore_db_and_auth::{documents, dto, errors, sessions, Credentials, Fireb
3
3
use firestore_db_and_auth:: documents:: WriteResult ;
4
4
use serde:: { Deserialize , Serialize } ;
5
5
6
+ use futures:: stream:: StreamExt ;
7
+
6
8
mod utils;
7
9
8
10
#[ derive( Debug , Serialize , Deserialize ) ]
@@ -21,7 +23,7 @@ struct DemoDTOPartial {
21
23
an_int : u32 ,
22
24
}
23
25
24
- fn write_document ( session : & mut ServiceSession , doc_id : & str ) -> errors:: Result < WriteResult > {
26
+ async fn write_document ( session : & mut ServiceSession , doc_id : & str ) -> errors:: Result < WriteResult > {
25
27
println ! ( "Write document" ) ;
26
28
27
29
let obj = DemoDTO {
@@ -30,10 +32,10 @@ fn write_document(session: &mut ServiceSession, doc_id: &str) -> errors::Result<
30
32
a_timestamp : chrono:: Utc :: now ( ) . to_rfc3339_opts ( chrono:: SecondsFormat :: Nanos , true ) ,
31
33
} ;
32
34
33
- documents:: write ( session, "tests" , Some ( doc_id) , & obj, documents:: WriteOptions :: default ( ) )
35
+ documents:: write ( session, "tests" , Some ( doc_id) , & obj, documents:: WriteOptions :: default ( ) ) . await
34
36
}
35
37
36
- fn write_partial_document ( session : & mut ServiceSession , doc_id : & str ) -> errors:: Result < WriteResult > {
38
+ async fn write_partial_document ( session : & mut ServiceSession , doc_id : & str ) -> errors:: Result < WriteResult > {
37
39
println ! ( "Partial write document" ) ;
38
40
39
41
let obj = DemoDTOPartial {
@@ -48,6 +50,7 @@ fn write_partial_document(session: &mut ServiceSession, doc_id: &str) -> errors:
48
50
& obj,
49
51
documents:: WriteOptions { merge : true } ,
50
52
)
53
+ . await
51
54
}
52
55
53
56
fn check_write ( result : WriteResult , doc_id : & str ) {
@@ -62,25 +65,25 @@ fn check_write(result: WriteResult, doc_id: &str) {
62
65
) ;
63
66
}
64
67
65
- fn service_account_session ( cred : Credentials ) -> errors:: Result < ( ) > {
66
- let mut session = ServiceSession :: new ( cred) . unwrap ( ) ;
67
- let b = session. access_token ( ) . to_owned ( ) ;
68
+ async fn service_account_session ( cred : Credentials ) -> errors:: Result < ( ) > {
69
+ let mut session = ServiceSession :: new ( cred) . await . unwrap ( ) ;
70
+ let b = session. access_token ( ) . await . to_owned ( ) ;
68
71
69
72
let doc_id = "service_test" ;
70
- check_write ( write_document ( & mut session, doc_id) ?, doc_id) ;
73
+ check_write ( write_document ( & mut session, doc_id) . await ?, doc_id) ;
71
74
72
75
// Check if cached value is used
73
- assert_eq ! ( session. access_token( ) , b) ;
76
+ assert_eq ! ( session. access_token( ) . await , b) ;
74
77
75
78
println ! ( "Read and compare document" ) ;
76
- let read: DemoDTO = documents:: read ( & mut session, "tests" , doc_id) ?;
79
+ let read: DemoDTO = documents:: read ( & mut session, "tests" , doc_id) . await ?;
77
80
78
81
assert_eq ! ( read. a_string, "abcd" ) ;
79
82
assert_eq ! ( read. an_int, 14 ) ;
80
83
81
- check_write ( write_partial_document ( & mut session, doc_id) ?, doc_id) ;
84
+ check_write ( write_partial_document ( & mut session, doc_id) . await ?, doc_id) ;
82
85
println ! ( "Read and compare document" ) ;
83
- let read: DemoDTOPartial = documents:: read ( & mut session, "tests" , doc_id) ?;
86
+ let read: DemoDTOPartial = documents:: read ( & mut session, "tests" , doc_id) . await ?;
84
87
85
88
// Should be updated
86
89
assert_eq ! ( read. an_int, 16 ) ;
@@ -90,14 +93,15 @@ fn service_account_session(cred: Credentials) -> errors::Result<()> {
90
93
Ok ( ( ) )
91
94
}
92
95
93
- fn user_account_session ( cred : Credentials ) -> errors:: Result < ( ) > {
94
- let user_session = utils:: user_session_with_cached_refresh_token ( & cred) ?;
96
+ async fn user_account_session ( cred : Credentials ) -> errors:: Result < ( ) > {
97
+ let user_session = utils:: user_session_with_cached_refresh_token ( & cred) . await ?;
95
98
96
99
assert_eq ! ( user_session. user_id, utils:: TEST_USER_ID ) ;
97
100
assert_eq ! ( user_session. project_id( ) , cred. project_id) ;
98
101
99
102
println ! ( "user::Session::by_access_token" ) ;
100
- let user_session = sessions:: user:: Session :: by_access_token ( & cred, & user_session. access_token_unchecked ( ) ) ?;
103
+ let user_session =
104
+ sessions:: user:: Session :: by_access_token ( & cred, & user_session. access_token_unchecked ( ) . await ) . await ?;
101
105
102
106
assert_eq ! ( user_session. user_id, utils:: TEST_USER_ID ) ;
103
107
@@ -117,13 +121,14 @@ fn user_account_session(cred: Credentials) -> errors::Result<()> {
117
121
Some ( doc_id) ,
118
122
& obj,
119
123
documents:: WriteOptions :: default ( ) ,
120
- ) ?,
124
+ )
125
+ . await ?,
121
126
doc_id,
122
127
) ;
123
128
124
129
// Test reading
125
130
println ! ( "user::Session documents::read" ) ;
126
- let read: DemoDTO = documents:: read ( & user_session, "tests" , doc_id) ?;
131
+ let read: DemoDTO = documents:: read ( & user_session, "tests" , doc_id) . await ?;
127
132
128
133
assert_eq ! ( read. a_string, "abc" ) ;
129
134
assert_eq ! ( read. an_int, 12 ) ;
@@ -135,22 +140,25 @@ fn user_account_session(cred: Credentials) -> errors::Result<()> {
135
140
"abc" . into ( ) ,
136
141
dto:: FieldOperator :: EQUAL ,
137
142
"a_string" ,
138
- ) ?
143
+ )
144
+ . await ?
139
145
. collect ( ) ;
140
146
assert_eq ! ( results. len( ) , 1 ) ;
141
- let doc: DemoDTO = documents:: read_by_name ( & user_session, & results. get ( 0 ) . unwrap ( ) . name ) ?;
147
+ let doc: DemoDTO = documents:: read_by_name ( & user_session, & results. get ( 0 ) . unwrap ( ) . name ) . await ?;
142
148
assert_eq ! ( doc. a_string, "abc" ) ;
143
149
144
150
let mut count = 0 ;
145
- let list_it: documents:: List < DemoDTO , _ > = documents:: list ( & user_session, "tests" . to_owned ( ) ) ;
151
+ let list_it = documents:: list ( & user_session, "tests" . to_owned ( ) )
152
+ . collect :: < Vec < errors:: Result < ( DemoDTO , _ ) > > > ( )
153
+ . await ;
146
154
for _doc in list_it {
147
155
count += 1 ;
148
156
}
149
157
assert_eq ! ( count, 2 ) ;
150
158
151
159
// test if the call fails for a non existing document
152
160
println ! ( "user::Session documents::delete" ) ;
153
- let r = documents:: delete ( & user_session, "tests/non_existing" , true ) ;
161
+ let r = documents:: delete ( & user_session, "tests/non_existing" , true ) . await ;
154
162
assert ! ( r. is_err( ) ) ;
155
163
match r. err ( ) . unwrap ( ) {
156
164
errors:: FirebaseError :: APIError ( code, message, context) => {
@@ -161,7 +169,7 @@ fn user_account_session(cred: Credentials) -> errors::Result<()> {
161
169
_ => panic ! ( "Expected an APIError" ) ,
162
170
} ;
163
171
164
- documents:: delete ( & user_session, & ( "tests/" . to_owned ( ) + doc_id) , false ) ?;
172
+ documents:: delete ( & user_session, & ( "tests/" . to_owned ( ) + doc_id) , false ) . await ?;
165
173
166
174
// Check if document is indeed removed
167
175
println ! ( "user::Session documents::query" ) ;
@@ -171,71 +179,76 @@ fn user_account_session(cred: Credentials) -> errors::Result<()> {
171
179
"abc" . into ( ) ,
172
180
dto:: FieldOperator :: EQUAL ,
173
181
"a_string" ,
174
- ) ?
182
+ )
183
+ . await ?
175
184
. count ( ) ;
176
185
assert_eq ! ( count, 0 ) ;
177
186
178
187
println ! ( "user::Session documents::query for f64" ) ;
179
188
let f: f64 = 13.37 ;
180
- let count = documents:: query ( & user_session, "tests" , f. into ( ) , dto:: FieldOperator :: EQUAL , "a_float" ) ?. count ( ) ;
189
+
190
+ let count = documents:: query ( & user_session, "tests" , f. into ( ) , dto:: FieldOperator :: EQUAL , "a_float" ) . await ?;
191
+
192
+ let count = count. count ( ) ;
181
193
assert_eq ! ( count, 0 ) ;
182
194
183
195
Ok ( ( ) )
184
196
}
185
197
186
- fn main ( ) -> errors:: Result < ( ) > {
198
+ #[ tokio:: main]
199
+ async fn main ( ) -> errors:: Result < ( ) > {
187
200
// Search for a credentials file in the root directory
188
201
use std:: path:: PathBuf ;
189
202
let mut credential_file = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
190
203
credential_file. push ( "firebase-service-account.json" ) ;
191
- let mut cred = Credentials :: from_file ( credential_file. to_str ( ) . unwrap ( ) ) ?;
204
+ let cred = Credentials :: from_file ( credential_file. to_str ( ) . unwrap ( ) ) . await ?;
192
205
193
206
// Only download the public keys once, and cache them.
194
- let jwkset = utils:: from_cache_file ( credential_file. with_file_name ( "cached_jwks.jwks" ) . as_path ( ) , & cred) ?;
207
+ let jwkset = utils:: from_cache_file ( credential_file. with_file_name ( "cached_jwks.jwks" ) . as_path ( ) , & cred) . await ?;
195
208
cred. add_jwks_public_keys ( & jwkset) ;
196
- cred. verify ( ) ?;
209
+ cred. verify ( ) . await ?;
197
210
198
211
// Perform some db operations via a service account session
199
- service_account_session ( cred. clone ( ) ) ?;
212
+ service_account_session ( cred. clone ( ) ) . await ?;
200
213
201
214
// Perform some db operations via a firebase user session
202
- user_account_session ( cred) ?;
215
+ user_account_session ( cred) . await ?;
203
216
204
217
Ok ( ( ) )
205
218
}
206
219
207
220
/// For integration tests and doc code snippets: Create a Credentials instance.
208
221
/// Necessary public jwk sets are downloaded or re-used if already present.
209
222
#[ cfg( test) ]
210
- fn valid_test_credentials ( ) -> errors:: Result < Credentials > {
223
+ async fn valid_test_credentials ( ) -> errors:: Result < Credentials > {
211
224
use std:: path:: PathBuf ;
212
225
let mut jwks_path = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
213
226
jwks_path. push ( "firebase-service-account.jwks" ) ;
214
227
215
- let mut cred: Credentials = Credentials :: new ( include_str ! ( "../firebase- service-account.json" ) ) ?;
228
+ let mut cred: Credentials = Credentials :: new ( include_str ! ( "../tests/ service-account-test .json" ) ) ?;
216
229
217
230
// Only download the public keys once, and cache them.
218
- let jwkset = utils:: from_cache_file ( jwks_path. as_path ( ) , & cred) ?;
231
+ let jwkset = utils:: from_cache_file ( jwks_path. as_path ( ) , & cred) . await ?;
219
232
cred. add_jwks_public_keys ( & jwkset) ;
220
233
cred. verify ( ) ?;
221
234
222
235
Ok ( cred)
223
236
}
224
237
225
- #[ test]
226
- fn valid_test_credentials_test ( ) -> errors:: Result < ( ) > {
227
- valid_test_credentials ( ) ?;
238
+ #[ tokio :: test]
239
+ async fn valid_test_credentials_test ( ) -> errors:: Result < ( ) > {
240
+ valid_test_credentials ( ) . await ?;
228
241
Ok ( ( ) )
229
242
}
230
243
231
- #[ test]
232
- fn service_account_session_test ( ) -> errors:: Result < ( ) > {
233
- service_account_session ( valid_test_credentials ( ) ? ) ?;
244
+ #[ tokio :: test]
245
+ async fn service_account_session_test ( ) -> errors:: Result < ( ) > {
246
+ service_account_session ( valid_test_credentials ( ) . await ? ) . await ?;
234
247
Ok ( ( ) )
235
248
}
236
249
237
- #[ test]
238
- fn user_account_session_test ( ) -> errors:: Result < ( ) > {
239
- user_account_session ( valid_test_credentials ( ) ? ) ?;
250
+ #[ tokio :: test]
251
+ async fn user_account_session_test ( ) -> errors:: Result < ( ) > {
252
+ user_account_session ( valid_test_credentials ( ) . await ? ) . await ?;
240
253
Ok ( ( ) )
241
254
}
0 commit comments