-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_tenants.py
992 lines (842 loc) · 33.5 KB
/
test_tenants.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
from uuid import uuid4
import pytest
from aries_cloudcontroller.acapy_client import AcaPyClient
from assertpy.assertpy import assert_that
from fastapi import HTTPException
import app.services.trust_registry.actors as trust_registry
from app.dependencies.acapy_clients import get_tenant_controller
from app.routes.admin.tenants import router
from app.services import acapy_wallet
from app.tests.util.regression_testing import TestMode
from app.util.did import ed25519_verkey_to_did_key
from shared import RichAsyncClient
TENANTS_BASE_PATH = router.prefix
skip_regression_test_reason = "Don't need to cover tenant tests in regression mode"
group_id = "TestGroup"
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_get_wallet_auth_token(tenant_admin_client: RichAsyncClient):
response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"image_url": "https://image.ca",
"wallet_label": uuid4().hex,
"group_id": group_id,
},
)
assert response.status_code == 200
tenant = response.json()
wallet_id = tenant["wallet_id"]
try:
# Attempting to access with incorrect wallet_id will fail with 404
with pytest.raises(HTTPException) as exc:
await tenant_admin_client.get(
f"{TENANTS_BASE_PATH}/bad_wallet_id/access-token"
)
assert exc.value.status_code == 404
# Attempting to access with incorrect group_id will fail with 404
with pytest.raises(HTTPException) as exc:
await tenant_admin_client.get(
f"{TENANTS_BASE_PATH}/{wallet_id}/access-token?group_id=wrong_group"
)
assert exc.value.status_code == 404
# Successfully get access-token with correct group_id
response = await tenant_admin_client.get(
f"{TENANTS_BASE_PATH}/{wallet_id}/access-token?group_id={group_id}"
)
assert response.status_code == 200
token = response.json()
assert token["access_token"]
assert token["access_token"].startswith("tenant.ey")
finally:
# Cleanup: Delete the created tenant even if test fails
delete_response = await tenant_admin_client.delete(
f"{TENANTS_BASE_PATH}/{wallet_id}"
)
assert delete_response.status_code == 200
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_create_tenant_member_wo_wallet_name(
tenant_admin_client: RichAsyncClient, tenant_admin_acapy_client: AcaPyClient
):
wallet_label = uuid4().hex
response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"image_url": "https://image.ca",
"wallet_label": wallet_label,
"group_id": group_id,
},
)
assert response.status_code == 200
tenant = response.json()
wallet_id = tenant["wallet_id"]
try:
wallet = await tenant_admin_acapy_client.multitenancy.get_wallet(
wallet_id=wallet_id
)
wallet_name = wallet.settings["wallet.name"]
assert wallet_id == wallet.wallet_id
assert tenant["group_id"] == group_id
assert tenant["wallet_label"] == wallet_label
assert tenant["created_at"] == wallet.created_at
assert tenant["updated_at"] == wallet.updated_at
assert tenant["wallet_name"] == wallet_name
assert_that(wallet_name).is_length(32)
finally:
# Cleanup: Delete the created tenant even if test fails
delete_response = await tenant_admin_client.delete(
f"{TENANTS_BASE_PATH}/{wallet_id}"
)
assert delete_response.status_code == 200
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_create_tenant_member_w_wallet_name(
tenant_admin_client: RichAsyncClient, tenant_admin_acapy_client: AcaPyClient
):
wallet_label = uuid4().hex
wallet_name = "TestWalletName"
create_tenant_payload = {
"image_url": "https://image.ca",
"wallet_label": wallet_label,
"group_id": group_id,
"wallet_name": wallet_name,
}
response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json=create_tenant_payload,
)
assert response.status_code == 200
tenant = response.json()
wallet_id = tenant["wallet_id"]
try:
wallet = await tenant_admin_acapy_client.multitenancy.get_wallet(
wallet_id=wallet_id
)
assert wallet_id == wallet.wallet_id
assert tenant["group_id"] == group_id
assert tenant["wallet_label"] == wallet_label
assert tenant["created_at"] == wallet.created_at
assert tenant["updated_at"] == wallet.updated_at
assert tenant["wallet_name"] == wallet_name
assert wallet.settings["wallet.name"] == wallet_name
with pytest.raises(HTTPException) as http_error:
await tenant_admin_client.post(
TENANTS_BASE_PATH,
json=create_tenant_payload,
)
assert http_error.value.status_code == 409
assert "already exists" in http_error.value.detail
finally:
# Cleanup: Delete the created tenant even if test fails
delete_response = await tenant_admin_client.delete(
f"{TENANTS_BASE_PATH}/{wallet_id}"
)
assert delete_response.status_code == 200
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_create_tenant_issuer(
tenant_admin_client: RichAsyncClient,
tenant_admin_acapy_client: AcaPyClient,
governance_acapy_client: AcaPyClient,
):
wallet_label = uuid4().hex
response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"image_url": "https://image.ca",
"wallet_label": wallet_label,
"roles": ["issuer"],
"group_id": group_id,
},
)
assert response.status_code == 200
tenant = response.json()
wallet_id = tenant["wallet_id"]
try:
actor = await trust_registry.fetch_actor_by_id(wallet_id)
if not actor:
raise Exception("Missing actor") # pylint: disable=W0719
endorser_did = await acapy_wallet.get_public_did(governance_acapy_client)
acapy_token: str = tenant["access_token"].split(".", 1)[1]
async with get_tenant_controller(acapy_token) as tenant_controller:
public_did = await acapy_wallet.get_public_did(tenant_controller)
connections = await tenant_controller.connection.get_connections()
connections = [
connection
for connection in connections.results
if connection.their_public_did == endorser_did.did
]
endorser_connection = connections[0]
# Connection with endorser
assert_that(endorser_connection).has_state("active")
assert_that(endorser_connection).has_their_public_did(endorser_did.did)
# Actor
assert_that(actor).has_name(tenant["wallet_label"])
assert_that(actor).has_did(f"did:sov:{public_did.did}")
assert_that(actor).has_roles(["issuer"])
# Tenant
wallet = await tenant_admin_acapy_client.multitenancy.get_wallet(
wallet_id=wallet_id
)
assert_that(tenant).has_wallet_id(wallet.wallet_id)
assert_that(tenant).has_wallet_label(wallet_label)
assert_that(tenant).has_created_at(wallet.created_at)
assert_that(tenant).has_updated_at(wallet.updated_at)
assert_that(wallet.settings["wallet.name"]).is_length(32)
# Assert that wallet_label cannot be re-used by plain tenants
with pytest.raises(HTTPException) as http_error:
await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={"wallet_label": wallet_label},
)
assert http_error.status_code == 409
assert "Can't create Tenant." in http_error.json()["details"]
finally:
# Cleanup: Delete the created tenant even if test fails
delete_response = await tenant_admin_client.delete(
f"{TENANTS_BASE_PATH}/{wallet_id}"
)
assert delete_response.status_code == 200
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_create_tenant_verifier(
tenant_admin_client: RichAsyncClient, tenant_admin_acapy_client: AcaPyClient
):
wallet_label = uuid4().hex
response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"image_url": "https://image.ca",
"wallet_label": wallet_label,
"roles": ["verifier"],
},
)
assert response.status_code == 200
tenant = response.json()
wallet_id = tenant["wallet_id"]
try:
wallet = await tenant_admin_acapy_client.multitenancy.get_wallet(
wallet_id=wallet_id
)
actor = await trust_registry.fetch_actor_by_id(wallet_id)
if not actor:
raise Exception("Missing actor") # pylint: disable=W0719
acapy_token: str = tenant["access_token"].split(".", 1)[1]
async with get_tenant_controller(acapy_token) as tenant_controller:
connections = await tenant_controller.connection.get_connections(
alias=f"Trust Registry {wallet_label}"
)
connection = connections.results[0]
# Connection invitation
assert_that(connection).has_state("invitation")
assert_that(actor).has_name(tenant["wallet_label"])
assert_that(actor).has_did(ed25519_verkey_to_did_key(connection.invitation_key))
assert_that(actor).has_roles(["verifier"])
# Tenant
assert_that(tenant).has_wallet_id(wallet.wallet_id)
assert_that(tenant).has_wallet_label(wallet_label)
assert_that(tenant).has_created_at(wallet.created_at)
assert_that(tenant).has_updated_at(wallet.updated_at)
assert_that(wallet.settings["wallet.name"]).is_length(32)
finally:
# Cleanup: Delete the created tenant even if test fails
delete_response = await tenant_admin_client.delete(
f"{TENANTS_BASE_PATH}/{wallet_id}"
)
assert delete_response.status_code == 200
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_update_tenant_verifier_to_issuer(
tenant_admin_client: RichAsyncClient,
tenant_admin_acapy_client: AcaPyClient,
governance_acapy_client: AcaPyClient,
):
wallet_label = uuid4().hex
image_url = "https://image.ca"
response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"image_url": image_url,
"wallet_label": wallet_label,
"roles": ["verifier"],
"group_id": group_id,
},
)
verifier_tenant = response.json()
verifier_wallet_id = verifier_tenant["wallet_id"]
try:
verifier_actor = await trust_registry.fetch_actor_by_id(verifier_wallet_id)
assert verifier_actor
assert_that(verifier_actor).has_name(wallet_label)
assert_that(verifier_actor).has_roles(["verifier"])
wallet = await tenant_admin_acapy_client.multitenancy.get_wallet(
wallet_id=verifier_wallet_id
)
assert_that(wallet.settings["wallet.name"]).is_length(32)
acapy_token: str = verifier_tenant["access_token"].split(".", 1)[1]
async with get_tenant_controller(acapy_token) as tenant_controller:
connections = await tenant_controller.connection.get_connections(
alias=f"Trust Registry {wallet_label}"
)
connection = connections.results[0]
# Connection invitation
assert_that(connection).has_state("invitation")
assert_that(verifier_actor).has_did(
ed25519_verkey_to_did_key(connection.invitation_key)
)
# Tenant
assert_that(verifier_tenant).has_wallet_id(wallet.wallet_id)
assert_that(verifier_tenant).has_image_url(image_url)
assert_that(verifier_tenant).has_wallet_label(wallet_label)
assert_that(verifier_tenant).has_created_at(wallet.created_at)
assert_that(verifier_tenant).has_updated_at(wallet.updated_at)
new_wallet_label = uuid4().hex
new_image_url = "https://some-ssi-site.org/image.png"
new_roles = ["issuer", "verifier"]
json_request = {
"image_url": new_image_url,
"wallet_label": new_wallet_label,
"roles": new_roles,
}
# Attempting to update with incorrect wallet_id will fail with 404
with pytest.raises(HTTPException) as exc:
await tenant_admin_client.put(
f"{TENANTS_BASE_PATH}/bad_wallet_id", json=json_request
)
assert exc.value.status_code == 404
# Attempting to update with incorrect group_id will fail with 404
with pytest.raises(HTTPException) as exc:
await tenant_admin_client.put(
f"{TENANTS_BASE_PATH}/{verifier_wallet_id}?group_id=wrong_group",
json=json_request,
)
assert exc.value.status_code == 404
# Successful update with correct group
update_response = await tenant_admin_client.put(
f"{TENANTS_BASE_PATH}/{verifier_wallet_id}?group_id={group_id}",
json=json_request,
)
new_tenant = update_response.json()
assert_that(new_tenant).has_wallet_id(wallet.wallet_id)
assert_that(new_tenant).has_image_url(new_image_url)
assert_that(new_tenant).has_wallet_label(new_wallet_label)
assert_that(new_tenant).has_created_at(wallet.created_at)
assert_that(new_tenant).has_group_id(group_id)
new_actor = await trust_registry.fetch_actor_by_id(verifier_wallet_id)
endorser_did = await acapy_wallet.get_public_did(governance_acapy_client)
acapy_token = (
(
await tenant_admin_client.get(
f"{TENANTS_BASE_PATH}/{verifier_wallet_id}/access-token"
)
)
.json()["access_token"]
.split(".", 1)[1]
)
async with get_tenant_controller(acapy_token) as tenant_controller:
public_did = await acapy_wallet.get_public_did(tenant_controller)
assert public_did
_connections = (
await tenant_controller.connection.get_connections()
).results
connections = [
connection
for connection in _connections
if connection.their_public_did == endorser_did.did
]
endorser_connection = connections[0]
# Connection invitation
assert_that(endorser_connection).has_state("active")
assert_that(endorser_connection).has_their_public_did(endorser_did.did)
assert new_actor
assert_that(new_actor).has_name(new_wallet_label)
assert_that(new_actor).has_did(new_actor.did)
assert_that(new_actor.roles).contains_only("issuer", "verifier")
assert new_actor.didcomm_invitation is not None
finally:
# Cleanup: Delete the created tenant even if test fails
delete_response = await tenant_admin_client.delete(
f"{TENANTS_BASE_PATH}/{verifier_wallet_id}"
)
assert delete_response.status_code == 200
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_get_tenants(tenant_admin_client: RichAsyncClient):
response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"image_url": "https://image.ca",
"wallet_label": uuid4().hex,
},
)
assert response.status_code == 200
created_tenant = response.json()
first_wallet_id = created_tenant["wallet_id"]
wallet_ids = [first_wallet_id]
try:
response = await tenant_admin_client.get(
f"{TENANTS_BASE_PATH}/{first_wallet_id}"
)
assert response.status_code == 200
retrieved_tenant = response.json()
created_tenant.pop("access_token")
assert created_tenant == retrieved_tenant
response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"image_url": "https://image.ca",
"wallet_label": uuid4().hex,
"group_id": group_id,
},
)
assert response.status_code == 200
last_wallet_id = response.json()["wallet_id"]
wallet_ids += (last_wallet_id,)
response = await tenant_admin_client.get(
TENANTS_BASE_PATH, params={"group_id": group_id}
)
assert response.status_code == 200
tenants = response.json()
assert len(tenants) >= 1
# Make sure created tenant is returned
assert_that(tenants).extracting("wallet_id").contains(last_wallet_id)
assert_that(tenants).extracting("group_id").contains(group_id)
finally:
# Cleanup: Delete the created tenant even if test fails
for wallet_id in wallet_ids:
delete_response = await tenant_admin_client.delete(
f"{TENANTS_BASE_PATH}/{wallet_id}"
)
assert delete_response.status_code == 200
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_get_tenants_by_group(tenant_admin_client: RichAsyncClient):
wallet_label = uuid4().hex
response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"image_url": "https://image.ca",
"wallet_label": wallet_label,
"group_id": group_id,
},
)
assert response.status_code == 200
created_tenant = response.json()
wallet_id = created_tenant["wallet_id"]
try:
response = await tenant_admin_client.get(
f"{TENANTS_BASE_PATH}?group_id={group_id}"
)
assert response.status_code == 200
tenants = response.json()
assert len(tenants) >= 1
# Make sure created tenant is returned
assert_that(tenants).extracting("wallet_id").contains(wallet_id)
assert_that(tenants).extracting("group_id").contains(group_id)
response = await tenant_admin_client.get(f"{TENANTS_BASE_PATH}?group_id=other")
assert response.status_code == 200
tenants = response.json()
assert len(tenants) == 0
assert tenants == []
finally:
# Cleanup: Delete the created tenant even if test fails
delete_response = await tenant_admin_client.delete(
f"{TENANTS_BASE_PATH}/{wallet_id}"
)
assert delete_response.status_code == 200
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_get_tenants_by_wallet_name(tenant_admin_client: RichAsyncClient):
wallet_name = uuid4().hex
response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"image_url": "https://image.ca",
"wallet_label": "abc",
"wallet_name": wallet_name,
"group_id": group_id,
},
)
assert response.status_code == 200
created_tenant = response.json()
wallet_id = created_tenant["wallet_id"]
try:
response = await tenant_admin_client.get(
f"{TENANTS_BASE_PATH}?wallet_name={wallet_name}"
)
assert response.status_code == 200
tenants = response.json()
assert len(tenants) == 1
# Make sure created tenant is returned
assert_that(tenants).extracting("wallet_id").contains(wallet_id)
assert_that(tenants).extracting("group_id").contains(group_id)
# Does not return when wallet_name = other
response = await tenant_admin_client.get(
f"{TENANTS_BASE_PATH}?wallet_name=other"
)
assert response.status_code == 200
tenants = response.json()
assert len(tenants) == 0
assert tenants == []
# Does not return when group_id = other
response = await tenant_admin_client.get(
f"{TENANTS_BASE_PATH}?wallet_name={wallet_name}&group_id=other"
)
assert response.status_code == 200
tenants = response.json()
assert len(tenants) == 0
assert tenants == []
finally:
# Cleanup: Delete the created tenant even if test fails
delete_response = await tenant_admin_client.delete(
f"{TENANTS_BASE_PATH}/{wallet_id}"
)
assert delete_response.status_code == 200
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_get_tenant(tenant_admin_client: RichAsyncClient):
wallet_name = uuid4().hex
wallet_label = "abc"
image_url = "https://image.ca"
create_response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"image_url": image_url,
"wallet_label": wallet_label,
"wallet_name": wallet_name,
"group_id": group_id,
},
)
assert create_response.status_code == 200
created_tenant = create_response.json()
wallet_id = created_tenant["wallet_id"]
try:
# Attempting to get with incorrect wallet_id will fail with 404
with pytest.raises(HTTPException) as exc:
await tenant_admin_client.get(f"{TENANTS_BASE_PATH}/bad_wallet_id")
assert exc.value.status_code == 404
# Attempting to get with incorrect group_id will fail with 404
with pytest.raises(HTTPException) as exc:
await tenant_admin_client.get(
f"{TENANTS_BASE_PATH}/{wallet_id}?group_id=wrong_group"
)
assert exc.value.status_code == 404
# Successful get with correct group_id
get_tenant_response = await tenant_admin_client.get(
f"{TENANTS_BASE_PATH}/{wallet_id}?group_id={group_id}"
)
assert get_tenant_response.status_code == 200
tenant = get_tenant_response.json()
assert tenant["wallet_id"] == wallet_id
assert tenant["wallet_label"] == wallet_label
assert tenant["wallet_name"] == wallet_name
assert tenant["image_url"] == image_url
assert tenant["group_id"] == group_id
assert tenant["created_at"] == created_tenant["created_at"]
assert tenant["updated_at"] == created_tenant["updated_at"]
finally:
# Cleanup: Delete the created tenant even if test fails
delete_response = await tenant_admin_client.delete(
f"{TENANTS_BASE_PATH}/{wallet_id}"
)
assert delete_response.status_code == 200
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_delete_tenant(
tenant_admin_client: RichAsyncClient, tenant_admin_acapy_client: AcaPyClient
):
wallet_label = uuid4().hex
response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"image_url": "https://image.ca",
"wallet_label": wallet_label,
"roles": ["verifier"],
"group_id": group_id,
},
)
assert response.status_code == 200
tenant = response.json()
wallet_id = tenant["wallet_id"]
# Actor exists
actor = await trust_registry.fetch_actor_by_id(wallet_id)
assert actor
# Attempting to delete with incorrect wallet_id will fail with 404
with pytest.raises(HTTPException) as exc:
await tenant_admin_client.delete(f"{TENANTS_BASE_PATH}/bad_wallet_id")
assert exc.value.status_code == 404
# Attempting to delete with incorrect group_id will fail with 404
with pytest.raises(HTTPException) as exc:
await tenant_admin_client.delete(
f"{TENANTS_BASE_PATH}/{wallet_id}?group_id=wrong_group"
)
assert exc.value.status_code == 404
# Successful delete with correct group_id:
response = await tenant_admin_client.delete(
f"{TENANTS_BASE_PATH}/{wallet_id}?group_id={group_id}"
)
assert response.status_code == 200
# Actor doesn't exist any more
actor = await trust_registry.fetch_actor_by_id(wallet_id)
assert not actor
with pytest.raises(Exception):
await tenant_admin_acapy_client.multitenancy.get_wallet(wallet_id=wallet_id)
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_extra_settings(
tenant_admin_client: RichAsyncClient, tenant_admin_acapy_client: AcaPyClient
):
# Create tenant with custom wallet settings
created_tenant_response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"wallet_label": uuid4().hex,
"extra_settings": {"ACAPY_AUTO_ACCEPT_INVITES": True},
},
)
assert created_tenant_response.status_code == 200
created_wallet_id = created_tenant_response.json()["wallet_id"]
try:
# Fetch wallet record and assert setting got passed through
wallet_record = await tenant_admin_acapy_client.multitenancy.get_wallet(
wallet_id=created_wallet_id
)
assert wallet_record.settings["debug.auto_accept_invites"] is True
# Test updating a wallet setting
update_tenant_response = await tenant_admin_client.put(
f"{TENANTS_BASE_PATH}/{created_wallet_id}",
json={
"wallet_label": "new_label",
"extra_settings": {"ACAPY_AUTO_ACCEPT_INVITES": False},
},
)
assert update_tenant_response.status_code == 200
# Fetch updated wallet record and assert setting got updated
updated_wallet_record = await tenant_admin_acapy_client.multitenancy.get_wallet(
wallet_id=created_wallet_id
)
assert updated_wallet_record.settings["debug.auto_accept_invites"] is False
# Assert bad request is raised for invalid extra_settings
with pytest.raises(Exception):
bad_response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"wallet_label": uuid4().hex,
"extra_settings": {"Bad_value": True},
},
)
assert bad_response.status_code == 422
finally:
# Cleanup: Delete the created tenant even if test fails
delete_response = await tenant_admin_client.delete(
f"{TENANTS_BASE_PATH}/{created_wallet_id}"
)
assert delete_response.status_code == 200
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_create_tenant_validation(tenant_admin_client: RichAsyncClient):
# Assert that 422 is raised when unacceptable special chars are used in wallet label or name
# The following chars are either reserved or unsafe to use in URIs without encoding
for char in [
"#",
"%",
"^",
"&",
"+",
"=",
"{",
"}",
"[",
"]",
"|",
"\\",
'"',
":",
";",
",",
"/",
"?",
]:
# Assert bad requests for wallet label
with pytest.raises(Exception):
bad_label_response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={"wallet_label": uuid4().hex + char},
)
assert bad_label_response.status_code == 422
assert "wallet_label" in bad_label_response.json()
# Assert bad requests for wallet name
with pytest.raises(Exception):
bad_name_response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={"wallet_label": uuid4().hex, "wallet_name": char},
)
assert bad_name_response.status_code == 422
assert "wallet_name" in bad_label_response.json()
# Assert bad requests for group_id
with pytest.raises(Exception):
bad_group_response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={"wallet_label": uuid4().hex, "group_id": char},
)
assert bad_group_response.status_code == 422
assert "group_id" in bad_group_response.json()
# Lastly, assert very long strings (> 100 chars) aren't allowed
very_long_string = 101 * "a"
# for wallet label
with pytest.raises(Exception):
bad_label_response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={"wallet_label": very_long_string},
)
assert bad_label_response.status_code == 422
assert "wallet_label" in bad_label_response.json()
# for wallet name
with pytest.raises(Exception):
bad_name_response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={"wallet_label": uuid4().hex, "wallet_name": very_long_string},
)
assert bad_name_response.status_code == 422
assert "wallet_name" in bad_label_response.json()
@pytest.mark.anyio
@pytest.mark.skipif(
TestMode.regression_run in TestMode.fixture_params,
reason=skip_regression_test_reason,
)
async def test_get_wallets_paginated(tenant_admin_client: RichAsyncClient):
num_wallets_to_test = 5
test_group = "TestPaginationGroup"
wallet_names = []
try:
# Create multiple wallets
for _ in range(num_wallets_to_test):
wallet_name = uuid4().hex
wallet_names.append(wallet_name)
response = await tenant_admin_client.post(
TENANTS_BASE_PATH,
json={
"image_url": "https://image.ca",
"wallet_name": wallet_name,
"wallet_label": "test_wallet",
"group_id": test_group,
},
)
# Test different limits
for limit in range(1, num_wallets_to_test + 2):
response = await tenant_admin_client.get(
TENANTS_BASE_PATH,
params={"limit": limit, "group_id": test_group},
)
wallets = response.json()
assert len(wallets) == min(limit, num_wallets_to_test)
# Test ascending order
response = await tenant_admin_client.get(
TENANTS_BASE_PATH,
params={
"limit": num_wallets_to_test,
"group_id": test_group,
"descending": False,
},
)
wallets_asc = response.json()
assert len(wallets_asc) == num_wallets_to_test
# Verify that the wallets are in ascending order based on created_at
assert wallets_asc == sorted(wallets_asc, key=lambda x: x["created_at"])
# Test descending order
response = await tenant_admin_client.get(
TENANTS_BASE_PATH,
params={
"limit": num_wallets_to_test,
"group_id": test_group,
"descending": True,
},
)
wallets_desc = response.json()
assert len(wallets_desc) == num_wallets_to_test
# Verify that the wallets are in descending order based on created_at
assert wallets_desc == sorted(
wallets_desc, key=lambda x: x["created_at"], reverse=True
)
# Compare ascending and descending order results
assert wallets_desc == sorted(
wallets_asc, key=lambda x: x["created_at"], reverse=True
)
# Test offset greater than number of records
response = await tenant_admin_client.get(
TENANTS_BASE_PATH,
params={"limit": 1, "offset": num_wallets_to_test, "group_id": test_group},
)
wallets = response.json()
assert len(wallets) == 0
# Test fetching unique records with pagination
prev_wallets = []
for offset in range(num_wallets_to_test):
response = await tenant_admin_client.get(
TENANTS_BASE_PATH,
params={"limit": 1, "offset": offset, "group_id": test_group},
)
wallets = response.json()
assert len(wallets) == 1
wallet = wallets[0]
assert wallet not in prev_wallets
prev_wallets.append(wallet)
# Test invalid limit and offset values
invalid_params = [
{"limit": -1}, # must be positive
{"offset": -1}, # must be positive
{"limit": 0}, # must be greater than 0
{"limit": 10001}, # must be less than or equal to max in ACA-Py: 10'000
]
for params in invalid_params:
with pytest.raises(HTTPException) as exc:
await tenant_admin_client.get(TENANTS_BASE_PATH, params=params)
assert exc.value.status_code == 422
finally:
# Cleanup: Delete the created tenants even if test fails
for wallet_name in wallet_names:
response = await tenant_admin_client.get(
f"{TENANTS_BASE_PATH}?wallet_name={wallet_name}&group_id={test_group}"
)
wallet_id = response.json()[0]["wallet_id"]
await tenant_admin_client.delete(f"{TENANTS_BASE_PATH}/{wallet_id}")