@@ -147,7 +147,10 @@ pub async fn info(
147147 code : code. to_string ( ) ,
148148 file_name : file_name. to_string ( ) ,
149149 } ;
150- let meta = state. db . fetch ( & file_path) ?. to_result ( ) ?;
150+ let meta = state
151+ . db
152+ . fetch ( & file_path) ?
153+ . to_result ( & file_path. to_string ( ) ) ?;
151154 if let Some ( max) = meta. max_download {
152155 if meta. count_downloads >= max {
153156 state. db . delete ( file_path. clone ( ) ) . await ?;
@@ -168,7 +171,10 @@ pub async fn fetch(
168171 code : code. to_string ( ) ,
169172 file_name : file_name. to_string ( ) ,
170173 } ;
171- let meta_data = state. db . fetch ( & file_path) ?. to_result ( ) ?;
174+ let meta_data = state
175+ . db
176+ . fetch ( & file_path) ?
177+ . to_result ( & file_path. to_string ( ) ) ?;
172178 authorize_user ( secret, & meta_data. secret ) ?;
173179 if let Some ( max) = meta_data. max_download {
174180 if meta_data. count_downloads >= max {
@@ -245,6 +251,7 @@ mod tests {
245251 util:: { multipart:: create_multipart_request, test:: StateTestContext } ,
246252 } ;
247253
254+ use fake:: { Fake , Faker } ;
248255 use test_context:: test_context;
249256
250257 #[ test_context( StateTestContext ) ]
@@ -257,12 +264,132 @@ mod tests {
257264 delete_manually : Some ( false ) ,
258265 qr_code_format : None ,
259266 } ;
260- let multipart = create_multipart_request ( "file_name.txt" , "data" )
267+ let file_name = format ! ( "{}.txt" , Faker . fake:: <String >( ) ) ;
268+ let multipart = create_multipart_request ( & file_name, "data" ) . await . unwrap ( ) ;
269+ let ( file_path, _) = store ( & ctx. state , & param, None , multipart) . await . unwrap ( ) ;
270+ let result = delete ( & ctx. state , & file_path. code , & file_path. file_name , None ) . await ;
271+ assert_err ! ( result, |e: & ApiError | e. to_string( )
272+ == format!( "{}/{file_name} is not deletable" , file_path. code) ) ;
273+ }
274+
275+ #[ test_context( StateTestContext ) ]
276+ #[ tokio:: test]
277+ async fn test_max_download_file_error ( ctx : & mut StateTestContext ) {
278+ let param = UploadQueryParam {
279+ max_download : Some ( 1 ) ,
280+ code_length : None ,
281+ expire_secs : None ,
282+ delete_manually : Some ( false ) ,
283+ qr_code_format : None ,
284+ } ;
285+ let file_name = format ! ( "{}.txt" , Faker . fake:: <String >( ) ) ;
286+ let multipart = create_multipart_request ( & file_name, "data" ) . await . unwrap ( ) ;
287+ let ( file_path, _) = store ( & ctx. state , & param, None , multipart) . await . unwrap ( ) ;
288+ fetch ( & ctx. state , & file_path. code , & file_path. file_name , None )
289+ . await
290+ . unwrap ( ) ;
291+ let result = fetch ( & ctx. state , & file_path. code , & file_path. file_name , None ) . await ;
292+ assert_err ! ( result, |e: & ApiError | e. to_string( )
293+ == format!(
294+ "resource not found: {}/{file_name} not found" ,
295+ file_path. code
296+ ) ) ;
297+ }
298+
299+ #[ test_context( StateTestContext ) ]
300+ #[ tokio:: test]
301+ async fn test_authorization_header_required_error ( ctx : & mut StateTestContext ) {
302+ let secret = Secret :: new ( Faker . fake :: < String > ( ) ) ;
303+ let param = UploadQueryParam {
304+ max_download : None ,
305+ code_length : None ,
306+ expire_secs : None ,
307+ delete_manually : Some ( true ) ,
308+ qr_code_format : None ,
309+ } ;
310+ let file_name = format ! ( "{}.txt" , Faker . fake:: <String >( ) ) ;
311+ let multipart = create_multipart_request ( & file_name, "data" ) . await . unwrap ( ) ;
312+ let ( file_path, _) = store ( & ctx. state , & param, Some ( secret) , multipart)
261313 . await
262314 . unwrap ( ) ;
263- let ( file_path, _) = store ( & ctx. state , & param, None , multipart) . await . unwrap ( ) ;
264315 let result = delete ( & ctx. state , & file_path. code , & file_path. file_name , None ) . await ;
265316 assert_err ! ( result, |e: & ApiError | e. to_string( )
266- == format!( "{}/file_name.txt is not deletable" , file_path. code) ) ;
317+ == "Authorization header required." ) ;
318+ let result = fetch ( & ctx. state , & file_path. code , & file_path. file_name , None ) . await ;
319+ assert_err ! ( result, |e: & ApiError | e. to_string( )
320+ == "Authorization header required." ) ;
321+ }
322+
323+ #[ test_context( StateTestContext ) ]
324+ #[ tokio:: test]
325+ async fn test_secret_token_is_invalid_error ( ctx : & mut StateTestContext ) {
326+ let mut secret = Secret :: new ( Faker . fake :: < String > ( ) ) ;
327+ let param = UploadQueryParam {
328+ max_download : None ,
329+ code_length : None ,
330+ expire_secs : None ,
331+ delete_manually : Some ( true ) ,
332+ qr_code_format : None ,
333+ } ;
334+ let file_name = format ! ( "{}.txt" , Faker . fake:: <String >( ) ) ;
335+ let multipart = create_multipart_request ( & file_name, "data" ) . await . unwrap ( ) ;
336+ let ( file_path, _) = store ( & ctx. state , & param, Some ( secret) , multipart)
337+ . await
338+ . unwrap ( ) ;
339+ secret = Secret :: new ( Faker . fake :: < String > ( ) ) ;
340+ let result = delete (
341+ & ctx. state ,
342+ & file_path. code ,
343+ & file_path. file_name ,
344+ Some ( secret. clone ( ) ) ,
345+ )
346+ . await ;
347+ assert_err ! ( result, |e: & ApiError | e. to_string( )
348+ == "Secret token is invalid" ) ;
349+
350+ let result = fetch (
351+ & ctx. state ,
352+ & file_path. code ,
353+ & file_path. file_name ,
354+ Some ( secret) ,
355+ )
356+ . await ;
357+ assert_err ! ( result, |e: & ApiError | e. to_string( )
358+ == "Secret token is invalid" ) ;
359+ }
360+
361+ #[ test_context( StateTestContext ) ]
362+ #[ tokio:: test]
363+ async fn test_code_length ( ctx : & mut StateTestContext ) {
364+ let code_length = 100 ;
365+ let param = UploadQueryParam {
366+ max_download : None ,
367+ code_length : Some ( code_length) ,
368+ expire_secs : None ,
369+ delete_manually : Some ( false ) ,
370+ qr_code_format : None ,
371+ } ;
372+ let file_name = format ! ( "{}.txt" , Faker . fake:: <String >( ) ) ;
373+ let multipart = create_multipart_request ( & file_name, "data" ) . await . unwrap ( ) ;
374+ let ( file_path, _) = store ( & ctx. state , & param, None , multipart) . await . unwrap ( ) ;
375+ assert_eq ! ( file_path. code. len( ) , code_length) ;
376+ }
377+
378+ #[ test_context( StateTestContext ) ]
379+ #[ tokio:: test]
380+ async fn test_file_does_not_exist_error ( ctx : & mut StateTestContext ) {
381+ let file_path = Faker . fake :: < FilePath > ( ) ;
382+ let result = fetch ( & ctx. state , & file_path. code , & file_path. file_name , None ) . await ;
383+ assert_err ! ( result, |e: & ApiError | e. to_string( )
384+ == format!(
385+ "resource not found: {}/{} not found" ,
386+ file_path. code, file_path. file_name
387+ ) ) ;
388+ let result = info ( & ctx. state , & file_path. code , & file_path. file_name , None ) . await ;
389+ assert_err ! ( result, |e: & ApiError | e. to_string( )
390+ == format!(
391+ "resource not found: {}/{} not found" ,
392+ file_path. code, file_path. file_name
393+ ) ) ;
267394 }
268395}
0 commit comments