@@ -298,26 +298,23 @@ impl PrivateTowerServices for Arc<InternalAPI> {
298298                ) 
299299            } ) ?; 
300300
301-         let  appointments :  Vec < Appointment >  = self 
301+         let  mut  matching_appointments :  Vec < common_msgs :: AppointmentData >  = self 
302302            . watcher 
303303            . get_watcher_appointments_with_locator ( locator,  user_id) 
304304            . into_values ( ) 
305-             . map ( |appointment| appointment. inner ) 
306-             . collect ( ) ; 
307- 
308-         let  mut  matching_appointments:  Vec < common_msgs:: AppointmentData >  = appointments
309-             . into_iter ( ) 
310305            . map ( |appointment| common_msgs:: AppointmentData  { 
311306                appointment_data :  Some ( 
312-                     common_msgs:: appointment_data:: AppointmentData :: Appointment ( appointment. into ( ) ) , 
307+                     common_msgs:: appointment_data:: AppointmentData :: Appointment ( 
308+                         appointment. inner . into ( ) , 
309+                     ) , 
313310                ) , 
314311            } ) 
315312            . collect ( ) ; 
316313
317-         for  ( _ ,   tracker)  in  self 
314+         for  tracker in  self 
318315            . watcher 
319316            . get_responder_trackers_with_locator ( locator,  user_id) 
320-             . into_iter ( ) 
317+             . into_values ( ) 
321318        { 
322319            matching_appointments. push ( common_msgs:: AppointmentData  { 
323320                appointment_data :  Some ( common_msgs:: appointment_data:: AppointmentData :: Tracker ( 
@@ -445,6 +442,8 @@ mod tests_private_api {
445442    use  bitcoin:: hashes:: Hash ; 
446443    use  bitcoin:: Txid ; 
447444
445+     use  rand:: { self ,  thread_rng,  Rng } ; 
446+ 
448447    use  crate :: responder:: { ConfirmationStatus ,  TransactionTracker } ; 
449448    use  crate :: test_utils:: { 
450449        create_api,  generate_dummy_appointment,  generate_dummy_appointment_with_user, 
@@ -453,7 +452,7 @@ mod tests_private_api {
453452    use  crate :: watcher:: Breach ; 
454453
455454    use  teos_common:: cryptography:: { self ,  get_random_keypair} ; 
456-     use  teos_common:: test_utils:: get_random_user_id; 
455+     use  teos_common:: test_utils:: { get_random_locator ,   get_random_user_id} ; 
457456
458457    #[ tokio:: test]  
459458    async  fn  test_get_all_appointments ( )  { 
@@ -531,18 +530,33 @@ mod tests_private_api {
531530            . into_inner ( ) ; 
532531
533532        assert ! ( matches!( response,  msgs:: GetAppointmentsResponse  {  .. } ) ) ; 
533+ 
534+         let  user_id = get_random_user_id ( ) . to_vec ( ) ; 
535+         let  locator = get_random_locator ( ) . to_vec ( ) ; 
536+         let  response = internal_api
537+             . get_appointments ( Request :: new ( msgs:: GetAppointmentsRequest  { 
538+                 locator, 
539+                 user_id :  Some ( user_id) , 
540+             } ) ) 
541+             . await 
542+             . unwrap ( ) 
543+             . into_inner ( ) ; 
544+ 
545+         assert ! ( matches!( response,  msgs:: GetAppointmentsResponse  {  .. } ) ) ; 
534546    } 
535547
536548    #[ tokio:: test]  
537-     async  fn  test_get_appointments_watcher ( )  { 
549+     async  fn  test_get_appointments_watcher_without_userid ( )  { 
538550        let  ( internal_api,  _s)  = create_api ( ) . await ; 
539551
540552        for  i in  0 ..3  { 
541553            // Create a dispute tx to be used for creating different dummy appointments with the same locator. 
542554            let  dispute_txid = get_random_tx ( ) . txid ( ) ; 
555+             let  locator = Locator :: new ( dispute_txid) ; 
543556
544557            // The number of different appointments to create for this dispute tx. 
545-             let  appointments_to_create = 4  *  i + 7 ; 
558+             let  random_number = 4  *  i + 7 ; 
559+             let  appointments_to_create = random_number; 
546560
547561            // Add that many appointments to the watcher. 
548562            for  _ in  0 ..appointments_to_create { 
@@ -556,8 +570,6 @@ mod tests_private_api {
556570                    . unwrap ( ) ; 
557571            } 
558572
559-             let  locator = Locator :: new ( dispute_txid) ; 
560- 
561573            // Query for the current locator and assert it retrieves correct appointments. 
562574            let  response = internal_api
563575                . get_appointments ( Request :: new ( msgs:: GetAppointmentsRequest  { 
@@ -584,6 +596,62 @@ mod tests_private_api {
584596        } 
585597    } 
586598
599+     #[ tokio:: test]  
600+     async  fn  test_get_appointments_watcher_with_userid ( )  { 
601+         let  ( internal_api,  _s)  = create_api ( ) . await ; 
602+ 
603+         for  i in  0 ..3  { 
604+             // Create a dispute tx to be used for creating different dummy appointments with the same locator. 
605+             let  dispute_txid = get_random_tx ( ) . txid ( ) ; 
606+             let  locator = Locator :: new ( dispute_txid) ; 
607+ 
608+             // The number of different appointments to create for this dispute tx. 
609+             let  random_number = 4  *  i + 7 ; 
610+             let  appointments_to_create = random_number; 
611+ 
612+             let  mut  random_users_list = Vec :: new ( ) ; 
613+ 
614+             // Add that many appointments to the watcher. 
615+             for  _ in  0 ..appointments_to_create { 
616+                 let  ( user_sk,  user_pk)  = get_random_keypair ( ) ; 
617+                 let  user_id = UserId ( user_pk) ; 
618+                 internal_api. watcher . register ( user_id) . unwrap ( ) ; 
619+                 random_users_list. push ( user_id) ; 
620+                 let  appointment = generate_dummy_appointment ( Some ( & dispute_txid) ) . inner ; 
621+                 let  signature = cryptography:: sign ( & appointment. to_vec ( ) ,  & user_sk) . unwrap ( ) ; 
622+                 internal_api
623+                     . watcher 
624+                     . add_appointment ( appointment,  signature) 
625+                     . unwrap ( ) ; 
626+             } 
627+ 
628+             for  user_id in  random_users_list. into_iter ( )  { 
629+                 let  response = internal_api
630+                     . get_appointments ( Request :: new ( msgs:: GetAppointmentsRequest  { 
631+                         locator :  locator. to_vec ( ) , 
632+                         user_id :  Some ( user_id. to_vec ( ) ) , 
633+                     } ) ) 
634+                     . await 
635+                     . unwrap ( ) 
636+                     . into_inner ( ) ; 
637+ 
638+                 // Verify that only a single appointment is returned 
639+                 assert_eq ! ( response. appointments. len( ) ,  1 ) ; 
640+ 
641+                 // Verify that the appointment have the current locator 
642+                 assert ! ( matches!( 
643+                     response. appointments[ 0 ] . appointment_data, 
644+                     Some ( common_msgs:: appointment_data:: AppointmentData :: Appointment ( 
645+                         common_msgs:: Appointment  { 
646+                             locator:  ref app_loc, 
647+                             ..
648+                         } 
649+                     ) )  if  Locator :: from_slice( app_loc) . unwrap( )  == locator
650+                 ) ) ; 
651+             } 
652+         } 
653+     } 
654+ 
587655    #[ tokio:: test]  
588656    async  fn  test_get_appointments_responder ( )  { 
589657        let  ( internal_api,  _s)  = create_api ( ) . await ; 
@@ -596,11 +664,19 @@ mod tests_private_api {
596664            // The number of different trackers to create for this dispute tx. 
597665            let  trackers_to_create = 4  *  i + 7 ; 
598666
667+             let  random_tracker_num = thread_rng ( ) . gen_range ( 0 ..trackers_to_create) ; 
668+             let  random_user_id = get_random_user_id ( ) ; 
669+ 
599670            // Add that many trackers to the responder. 
600-             for  _ in  0 ..trackers_to_create { 
671+             for  i in  0 ..trackers_to_create { 
672+                 let  user_id = if  i == random_tracker_num { 
673+                     random_user_id
674+                 }  else  { 
675+                     get_random_user_id ( ) 
676+                 } ; 
601677                let  tracker = TransactionTracker :: new ( 
602678                    breach. clone ( ) , 
603-                     get_random_user_id ( ) , 
679+                     user_id , 
604680                    ConfirmationStatus :: ConfirmedIn ( 100 ) , 
605681                ) ; 
606682                internal_api
@@ -610,7 +686,7 @@ mod tests_private_api {
610686
611687            let  locator = Locator :: new ( dispute_tx. txid ( ) ) ; 
612688
613-             // Query for the current locator and assert it retrieves correct trackers . 
689+             // Query for the current locator without the optional user_id . 
614690            let  response = internal_api
615691                . get_appointments ( Request :: new ( msgs:: GetAppointmentsRequest  { 
616692                    locator :  locator. to_vec ( ) , 
@@ -620,7 +696,7 @@ mod tests_private_api {
620696                . unwrap ( ) 
621697                . into_inner ( ) ; 
622698
623-             // The  response should contain `trackers_to_create` trackers, all with dispute txid that matches with the locator of the current iteration. 
699+             // Verify that the  response should contain `trackers_to_create` trackers, all with dispute txid that matches with the locator of the current iteration. 
624700            assert_eq ! ( response. appointments. len( ) ,  trackers_to_create) ; 
625701            for  app_data in  response. appointments  { 
626702                assert ! ( matches!( 
@@ -633,6 +709,28 @@ mod tests_private_api {
633709                    ) )  if  Locator :: new( Txid :: from_slice( dispute_txid) . unwrap( ) )  == locator
634710                ) ) ; 
635711            } 
712+ 
713+             // Query for the current locator with the optional user_id present. 
714+             let  response = internal_api
715+                 . get_appointments ( Request :: new ( msgs:: GetAppointmentsRequest  { 
716+                     locator :  locator. to_vec ( ) , 
717+                     user_id :  Some ( random_user_id. to_vec ( ) ) , 
718+                 } ) ) 
719+                 . await 
720+                 . unwrap ( ) 
721+                 . into_inner ( ) ; 
722+ 
723+             // Verify that only a single appointment is returned and the correct locator is found 
724+             assert_eq ! ( response. appointments. len( ) ,  1 ) ; 
725+             assert ! ( matches!( 
726+                 response. appointments[ 0 ] . appointment_data, 
727+                 Some ( common_msgs:: appointment_data:: AppointmentData :: Tracker ( 
728+                     common_msgs:: Tracker  { 
729+                         ref dispute_txid, 
730+                         ..
731+                     } 
732+                 ) )  if  Locator :: new( Txid :: from_slice( dispute_txid) . unwrap( ) )  == locator
733+             ) ) ; 
636734        } 
637735    } 
638736
0 commit comments