1
1
use crate :: blockchain:: bill:: BillBlockchain ;
2
2
use crate :: data:: {
3
- File ,
4
3
bill:: {
5
4
BillCombinedBitcoinKey , BillKeys , BillsBalanceOverview , BillsFilterRole , BitcreditBill ,
6
5
BitcreditBillResult , Endorsement , LightBitcreditBillResult , PastEndorsee ,
@@ -83,20 +82,10 @@ pub trait BillServiceApi: ServiceTraitBounds {
83
82
async fn open_and_decrypt_attached_file (
84
83
& self ,
85
84
bill_id : & str ,
86
- file_name : & str ,
85
+ file : & bcr_ebill_core :: File ,
87
86
bill_private_key : & str ,
88
87
) -> Result < Vec < u8 > > ;
89
88
90
- /// encrypts and saves the given uploaded file, returning the file name, as well as the hash of
91
- /// the unencrypted file
92
- async fn encrypt_and_save_uploaded_file (
93
- & self ,
94
- file_name : & str ,
95
- file_bytes : & [ u8 ] ,
96
- bill_id : & str ,
97
- bill_public_key : & str ,
98
- ) -> Result < File > ;
99
-
100
89
/// issues a new bill
101
90
async fn issue_new_bill ( & self , data : BillIssueData ) -> Result < BitcreditBill > ;
102
91
@@ -235,7 +224,7 @@ pub mod tests {
235
224
util,
236
225
} ;
237
226
use bcr_ebill_core:: {
238
- ValidationError ,
227
+ File , ValidationError ,
239
228
bill:: {
240
229
BillAcceptanceStatus , BillCurrentWaitingState , BillPaymentStatus , BillRecourseStatus ,
241
230
BillSellStatus , BillWaitingForPaymentState , PastPaymentStatus , RecourseReason ,
@@ -259,9 +248,11 @@ pub mod tests {
259
248
util:: date:: DateTimeUtc ,
260
249
} ;
261
250
use cashu:: nut02 as cdk02;
262
- use core:: str;
263
251
use mockall:: predicate:: { always, eq, function} ;
264
- use std:: collections:: { HashMap , HashSet } ;
252
+ use std:: {
253
+ collections:: { HashMap , HashSet } ,
254
+ str:: FromStr ,
255
+ } ;
265
256
use test_utils:: {
266
257
accept_block, get_baseline_bill, get_baseline_cached_bill, get_baseline_identity, get_ctx,
267
258
get_genesis_chain, get_service, offer_to_sell_block, recourse_block, reject_accept_block,
@@ -480,9 +471,12 @@ pub mod tests {
480
471
ctx. file_upload_store
481
472
. expect_remove_temp_upload_folder ( )
482
473
. returning ( |_| Ok ( ( ) ) ) ;
483
- ctx. file_upload_store
484
- . expect_save_attached_file ( )
485
- . returning ( move |_, _, _| Ok ( ( ) ) ) ;
474
+ ctx. file_upload_client . expect_upload ( ) . returning ( |_, _| {
475
+ Ok ( nostr:: hashes:: sha256:: Hash :: from_str (
476
+ "d277fe40da2609ca08215cdfbeac44835d4371a72f1416a63c87efd67ee24bfa" ,
477
+ )
478
+ . unwrap ( ) )
479
+ } ) ;
486
480
ctx. bill_store . expect_save_keys ( ) . returning ( |_, _| Ok ( ( ) ) ) ;
487
481
ctx. bill_store
488
482
. expect_save_bill_to_cache ( )
@@ -543,9 +537,12 @@ pub mod tests {
543
537
ctx. file_upload_store
544
538
. expect_remove_temp_upload_folder ( )
545
539
. returning ( |_| Ok ( ( ) ) ) ;
546
- ctx. file_upload_store
547
- . expect_save_attached_file ( )
548
- . returning ( move |_, _, _| Ok ( ( ) ) ) ;
540
+ ctx. file_upload_client . expect_upload ( ) . returning ( |_, _| {
541
+ Ok ( nostr:: hashes:: sha256:: Hash :: from_str (
542
+ "d277fe40da2609ca08215cdfbeac44835d4371a72f1416a63c87efd67ee24bfa" ,
543
+ )
544
+ . unwrap ( ) )
545
+ } ) ;
549
546
ctx. bill_store . expect_save_keys ( ) . returning ( |_, _| Ok ( ( ) ) ) ;
550
547
ctx. bill_store
551
548
. expect_save_bill_to_cache ( )
@@ -686,9 +683,12 @@ pub mod tests {
686
683
ctx. file_upload_store
687
684
. expect_remove_temp_upload_folder ( )
688
685
. returning ( |_| Ok ( ( ) ) ) ;
689
- ctx. file_upload_store
690
- . expect_save_attached_file ( )
691
- . returning ( move |_, _, _| Ok ( ( ) ) ) ;
686
+ ctx. file_upload_client . expect_upload ( ) . returning ( |_, _| {
687
+ Ok ( nostr:: hashes:: sha256:: Hash :: from_str (
688
+ "d277fe40da2609ca08215cdfbeac44835d4371a72f1416a63c87efd67ee24bfa" ,
689
+ )
690
+ . unwrap ( ) )
691
+ } ) ;
692
692
ctx. bill_store . expect_save_keys ( ) . returning ( |_, _| Ok ( ( ) ) ) ;
693
693
ctx. bill_store
694
694
. expect_save_bill_to_cache ( )
@@ -734,71 +734,26 @@ pub mod tests {
734
734
}
735
735
736
736
#[ tokio:: test]
737
- async fn save_encrypt_open_decrypt_compare_hashes ( ) {
738
- let mut ctx = get_ctx ( ) ;
739
- let bill_id = "test_bill_id" ;
740
- let file_name = "invoice_00000000-0000-0000-0000-000000000000.pdf" ;
741
- let file_bytes = String :: from ( "hello world" ) . as_bytes ( ) . to_vec ( ) ;
742
- let expected_encrypted =
743
- util:: crypto:: encrypt_ecies ( & file_bytes, TEST_PUB_KEY_SECP ) . unwrap ( ) ;
744
-
745
- ctx. file_upload_store
746
- . expect_save_attached_file ( )
747
- . with ( always ( ) , eq ( bill_id) , eq ( file_name) )
748
- . times ( 1 )
749
- . returning ( |_, _, _| Ok ( ( ) ) ) ;
750
-
751
- ctx. file_upload_store
752
- . expect_open_attached_file ( )
753
- . with ( eq ( bill_id) , eq ( file_name) )
754
- . times ( 1 )
755
- . returning ( move |_, _| Ok ( expected_encrypted. clone ( ) ) ) ;
756
- let service = get_service ( ctx) ;
757
-
758
- let bill_file = service
759
- . encrypt_and_save_uploaded_file ( file_name, & file_bytes, bill_id, TEST_PUB_KEY_SECP )
760
- . await
761
- . unwrap ( ) ;
762
- assert_eq ! (
763
- bill_file. hash,
764
- String :: from( "DULfJyE3WQqNxy3ymuhAChyNR3yufT88pmqvAazKFMG4" )
765
- ) ;
766
- assert_eq ! ( bill_file. name, String :: from( file_name) ) ;
767
-
768
- let decrypted = service
769
- . open_and_decrypt_attached_file ( bill_id, file_name, TEST_PRIVATE_KEY_SECP )
770
- . await
771
- . unwrap ( ) ;
772
- assert_eq ! ( str :: from_utf8( & decrypted) . unwrap( ) , "hello world" ) ;
773
- }
774
-
775
- #[ tokio:: test]
776
- async fn save_encrypt_propagates_write_file_error ( ) {
777
- let mut ctx = get_ctx ( ) ;
778
- ctx. file_upload_store
779
- . expect_save_attached_file ( )
780
- . returning ( |_, _, _| Err ( persistence:: Error :: Io ( std:: io:: Error :: other ( "test error" ) ) ) ) ;
781
- let service = get_service ( ctx) ;
782
-
783
- assert ! (
784
- service
785
- . encrypt_and_save_uploaded_file( "file_name" , & [ ] , "test" , TEST_PUB_KEY_SECP )
786
- . await
787
- . is_err( )
788
- ) ;
789
- }
790
-
791
- #[ tokio:: test]
792
- async fn open_decrypt_propagates_read_file_error ( ) {
737
+ async fn open_decrypt_propagates_download_error ( ) {
793
738
let mut ctx = get_ctx ( ) ;
794
- ctx. file_upload_store
795
- . expect_open_attached_file ( )
796
- . returning ( |_, _| Err ( persistence:: Error :: Io ( std:: io:: Error :: other ( "test error" ) ) ) ) ;
739
+ ctx. file_upload_client . expect_download ( ) . returning ( |_, _| {
740
+ Err ( crate :: external:: Error :: ExternalFileStorageApi (
741
+ crate :: external:: file_storage:: Error :: InvalidRelayUrl ,
742
+ ) )
743
+ } ) ;
797
744
let service = get_service ( ctx) ;
798
745
799
746
assert ! (
800
747
service
801
- . open_and_decrypt_attached_file( "test" , "test" , TEST_PRIVATE_KEY_SECP )
748
+ . open_and_decrypt_attached_file(
749
+ "test" ,
750
+ & File {
751
+ name: "some_file" . into( ) ,
752
+ hash: "" . into( ) ,
753
+ nostr_hash: "" . into( )
754
+ } ,
755
+ TEST_PRIVATE_KEY_SECP
756
+ )
802
757
. await
803
758
. is_err( )
804
759
) ;
@@ -6033,7 +5988,6 @@ pub mod tests {
6033
5988
1731593930 ,
6034
5989
)
6035
5990
. await ;
6036
- println ! ( "{res:?}" ) ;
6037
5991
assert ! ( res. is_ok( ) ) ;
6038
5992
}
6039
5993
0 commit comments