8
8
9
9
from faker import Faker
10
10
import requests
11
- from typing import cast , Any , Dict , Iterator , Tuple
11
+ from typing import cast , Any , List , Dict , Iterator , Tuple
12
12
13
13
import tankersdk
14
14
from tankersdk import Tanker
@@ -339,43 +339,60 @@ async def test_postponed_share(tmp_path: Path, app: Dict[str, str]) -> None:
339
339
340
340
341
341
async def check_share_with_group_works (
342
- alice : User , group_id : str , bob : User , charlie : User
342
+ alice : User , group_id : str , members : List [ User ], non_members : List [ User ] = []
343
343
) -> None :
344
344
message = b"Hi, guys"
345
345
encrypted = await alice .session .encrypt (
346
346
message , EncryptionOptions (share_with_groups = [group_id ])
347
347
)
348
- decrypted = await charlie .session .decrypt (encrypted )
349
- assert decrypted == message
350
- decrypted = await bob .session .decrypt (encrypted )
351
- assert decrypted == message
348
+ for m in members :
349
+ decrypted = await m .session .decrypt (encrypted )
350
+ assert decrypted == message
351
+ for n in non_members :
352
+ with pytest .raises (error .InvalidArgument ):
353
+ await n .session .decrypt (encrypted )
352
354
353
355
354
356
@pytest .mark .asyncio
355
357
async def test_create_group (tmp_path : Path , app : Dict [str , str ]) -> None :
356
358
alice = await create_user_session (tmp_path , app )
357
359
bob = await create_user_session (tmp_path , app )
358
360
charlie = await create_user_session (tmp_path , app )
361
+ tom = await create_user_session (tmp_path , app )
359
362
360
363
group_id = await alice .session .create_group (
361
- [bob .public_identity , charlie .public_identity ]
364
+ [bob .public_identity , charlie .public_identity , tom . public_identity ]
362
365
)
363
- await check_share_with_group_works (alice , group_id , bob , charlie )
366
+ await check_share_with_group_works (alice , group_id , [ bob , charlie , tom ] )
364
367
365
368
366
369
@pytest .mark .asyncio
367
370
async def test_update_group (tmp_path : Path , app : Dict [str , str ]) -> None :
368
371
alice = await create_user_session (tmp_path , app )
369
372
bob = await create_user_session (tmp_path , app )
370
373
charlie = await create_user_session (tmp_path , app )
374
+ tom = await create_user_session (tmp_path , app )
371
375
372
376
group_id = await alice .session .create_group (
373
- [alice .public_identity , bob .public_identity ]
377
+ [alice .public_identity , tom . public_identity , bob .public_identity ]
374
378
)
375
379
await alice .session .update_group_members (
376
- group_id , users_to_add = [charlie .public_identity ]
380
+ group_id ,
381
+ users_to_add = [charlie .public_identity ],
382
+ users_to_remove = [tom .public_identity ],
377
383
)
378
- await check_share_with_group_works (alice , group_id , bob , charlie )
384
+ await check_share_with_group_works (alice , group_id , [bob , charlie ], [tom ])
385
+
386
+
387
+ @pytest .mark .asyncio
388
+ async def test_update_group_empty (tmp_path : Path , app : Dict [str , str ]) -> None :
389
+ alice = await create_user_session (tmp_path , app )
390
+
391
+ group_id = await alice .session .create_group ([alice .public_identity ])
392
+ with pytest .raises (error .InvalidArgument ):
393
+ await alice .session .update_group_members (
394
+ group_id , users_to_add = [], users_to_remove = [],
395
+ )
379
396
380
397
381
398
@pytest .mark .asyncio
@@ -721,7 +738,7 @@ async def test_bad_verification_code(tmp_path: Path, app: Dict[str, str]) -> Non
721
738
)
722
739
723
740
724
- async def set_up_preshare (tmp_path : Path , app : Dict [str , str ]) -> Tuple [ User , PreUser ] :
741
+ async def create_pre_user (tmp_path : Path , app : Dict [str , str ]) -> PreUser :
725
742
fake = Faker ()
726
743
bob_email = fake .email (domain = "tanker.io" )
727
744
bob_provisional_identity = tankersdk_identity .create_provisional_identity (
@@ -730,7 +747,6 @@ async def set_up_preshare(tmp_path: Path, app: Dict[str, str]) -> Tuple[User, Pr
730
747
bob_public_provisional_identity = tankersdk_identity .get_public_identity (
731
748
bob_provisional_identity
732
749
)
733
- alice = await create_user_session (tmp_path , app )
734
750
bob = await create_user_session (tmp_path , app )
735
751
pre_bob = PreUser (
736
752
session = bob .session ,
@@ -741,7 +757,12 @@ async def set_up_preshare(tmp_path: Path, app: Dict[str, str]) -> Tuple[User, Pr
741
757
email = bob_email ,
742
758
verification_code = get_verification_code_email (app , bob_email ),
743
759
)
744
- return alice , pre_bob
760
+ return pre_bob
761
+
762
+
763
+ async def set_up_preshare (tmp_path : Path , app : Dict [str , str ]) -> Tuple [User , PreUser ]:
764
+ alice = await create_user_session (tmp_path , app )
765
+ return alice , await create_pre_user (tmp_path , app )
745
766
746
767
747
768
@pytest .mark .asyncio
@@ -899,7 +920,9 @@ async def test_create_group_with_prov_id(tmp_path: Path, app: Dict[str, str]) ->
899
920
900
921
901
922
@pytest .mark .asyncio
902
- async def test_add_to_group_with_prov_id (tmp_path : Path , app : Dict [str , str ]) -> None :
923
+ async def test_add_group_members_with_prov_id (
924
+ tmp_path : Path , app : Dict [str , str ]
925
+ ) -> None :
903
926
alice , bob = await set_up_preshare (tmp_path , app )
904
927
message = b"Hi, this is for a group"
905
928
group_id = await alice .session .create_group ([alice .public_identity ])
@@ -917,6 +940,31 @@ async def test_add_to_group_with_prov_id(tmp_path: Path, app: Dict[str, str]) ->
917
940
assert decrypted == message
918
941
919
942
943
+ @pytest .mark .asyncio
944
+ async def test_remove_group_members_with_prov_id (
945
+ tmp_path : Path , app : Dict [str , str ]
946
+ ) -> None :
947
+ alice , bob = await set_up_preshare (tmp_path , app )
948
+ message = b"Hi, this is for a group"
949
+ group_id = await alice .session .create_group (
950
+ [alice .public_identity , bob .public_provisional_identity ]
951
+ )
952
+
953
+ await bob .session .attach_provisional_identity (bob .private_provisional_identity )
954
+ await bob .session .verify_provisional_identity (
955
+ EmailVerification (bob .email , bob .verification_code )
956
+ )
957
+
958
+ encrypted = await alice .session .encrypt (
959
+ message , EncryptionOptions (share_with_groups = [group_id ])
960
+ )
961
+ await alice .session .update_group_members (
962
+ group_id , users_to_remove = [bob .public_identity ]
963
+ )
964
+ with pytest .raises (error .InvalidArgument ):
965
+ await bob .session .decrypt (encrypted )
966
+
967
+
920
968
@pytest .mark .asyncio
921
969
async def test_user_not_found (tmp_path : Path , app : Dict [str , str ]) -> None :
922
970
user_id = encode ("*" * 32 )
0 commit comments