-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathopen_fga_api_test.py
More file actions
2170 lines (2027 loc) · 82.3 KB
/
open_fga_api_test.py
File metadata and controls
2170 lines (2027 loc) · 82.3 KB
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
993
994
995
996
997
998
999
1000
"""
Python SDK for OpenFGA
API version: 1.x
Website: https://openfga.dev
Documentation: https://openfga.dev/docs
Support: https://openfga.dev/community
License: [Apache-2.0](https://github.com/openfga/python-sdk/blob/main/LICENSE)
NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
"""
import unittest
from datetime import datetime, timedelta, timezone
from unittest import IsolatedAsyncioTestCase
from unittest.mock import ANY, patch
import urllib3
import openfga_sdk
from openfga_sdk import rest
from openfga_sdk.api import open_fga_api
from openfga_sdk.credentials import CredentialConfiguration, Credentials
from openfga_sdk.exceptions import (
FGA_REQUEST_ID,
ApiValueError,
FgaValidationException,
NotFoundException,
RateLimitExceededError,
ServiceException,
ValidationException,
)
from openfga_sdk.models.assertion import Assertion
from openfga_sdk.models.authorization_model import AuthorizationModel
from openfga_sdk.models.check_request import CheckRequest
from openfga_sdk.models.check_response import CheckResponse
from openfga_sdk.models.create_store_request import CreateStoreRequest
from openfga_sdk.models.create_store_response import CreateStoreResponse
from openfga_sdk.models.error_code import ErrorCode
from openfga_sdk.models.expand_request import ExpandRequest
from openfga_sdk.models.expand_request_tuple_key import ExpandRequestTupleKey
from openfga_sdk.models.expand_response import ExpandResponse
from openfga_sdk.models.get_store_response import GetStoreResponse
from openfga_sdk.models.internal_error_code import InternalErrorCode
from openfga_sdk.models.internal_error_message_response import (
InternalErrorMessageResponse,
)
from openfga_sdk.models.leaf import Leaf
from openfga_sdk.models.list_objects_request import ListObjectsRequest
from openfga_sdk.models.list_objects_response import ListObjectsResponse
from openfga_sdk.models.list_stores_response import ListStoresResponse
from openfga_sdk.models.list_users_request import ListUsersRequest
from openfga_sdk.models.list_users_response import ListUsersResponse
from openfga_sdk.models.node import Node
from openfga_sdk.models.not_found_error_code import NotFoundErrorCode
from openfga_sdk.models.object_relation import ObjectRelation
from openfga_sdk.models.path_unknown_error_message_response import (
PathUnknownErrorMessageResponse,
)
from openfga_sdk.models.read_assertions_response import ReadAssertionsResponse
from openfga_sdk.models.read_authorization_model_response import (
ReadAuthorizationModelResponse,
)
from openfga_sdk.models.read_changes_response import ReadChangesResponse
from openfga_sdk.models.read_request import ReadRequest
from openfga_sdk.models.read_request_tuple_key import ReadRequestTupleKey
from openfga_sdk.models.read_response import ReadResponse
from openfga_sdk.models.store import Store
from openfga_sdk.models.tuple import Tuple
from openfga_sdk.models.tuple_change import TupleChange
from openfga_sdk.models.tuple_key import TupleKey
from openfga_sdk.models.tuple_key_without_condition import TupleKeyWithoutCondition
from openfga_sdk.models.tuple_operation import TupleOperation
from openfga_sdk.models.type_definition import TypeDefinition
from openfga_sdk.models.users import Users
from openfga_sdk.models.userset import Userset
from openfga_sdk.models.userset_tree import UsersetTree
from openfga_sdk.models.usersets import Usersets
from openfga_sdk.models.validation_error_message_response import (
ValidationErrorMessageResponse,
)
from openfga_sdk.models.write_assertions_request import WriteAssertionsRequest
from openfga_sdk.models.write_authorization_model_request import (
WriteAuthorizationModelRequest,
)
from openfga_sdk.models.write_authorization_model_response import (
WriteAuthorizationModelResponse,
)
from openfga_sdk.models.write_request import WriteRequest
from openfga_sdk.models.write_request_deletes import WriteRequestDeletes
from openfga_sdk.models.write_request_writes import WriteRequestWrites
store_id = "01H0H015178Y2V4CX10C2KGHF4"
request_id = "x1y2z3"
# Helper function to construct mock response
def http_mock_response(body, status, headers=None):
if headers is None:
headers = {}
default_headers = urllib3.response.HTTPHeaderDict(
{"content-type": "application/json", "Fga-Request-Id": request_id}
)
headers = {**default_headers, **headers}
return urllib3.HTTPResponse(
body.encode("utf-8"), headers, status, preload_content=False
)
def mock_response(body, status):
obj = http_mock_response(body, status)
return rest.RESTResponse(obj, obj.data)
class TestOpenFgaApi(IsolatedAsyncioTestCase):
"""OpenFgaApi unit test stubs"""
def setUp(self):
self.configuration = openfga_sdk.Configuration(
api_url="http://api.fga.example",
)
def tearDown(self):
pass
@patch.object(rest.RESTClientObject, "request")
async def test_check(self, mock_request):
"""Test case for check
Check whether a user is authorized to access an object
"""
# First, mock the response
response_body = '{"allowed": true, "resolution": "1234"}'
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
async with openfga_sdk.ApiClient(configuration) as api_client:
api_instance = open_fga_api.OpenFgaApi(api_client)
body = CheckRequest(
tuple_key=TupleKey(
object="document:2021-budget",
relation="reader",
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
),
authorization_model_id="01GXSA8YR785C4FYS3C0RTG7B1",
)
api_response = await api_instance.check(
body=body,
)
self.assertIsInstance(api_response, CheckResponse)
self.assertTrue(api_response.allowed)
# Make sure the API was called with the right data
mock_request.assert_called_once_with(
"POST",
"http://api.fga.example/stores/01H0H015178Y2V4CX10C2KGHF4/check",
headers=ANY,
query_params=[],
post_params=[],
body={
"tuple_key": {
"object": "document:2021-budget",
"relation": "reader",
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
},
"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1",
},
_preload_content=ANY,
_request_timeout=None,
)
await api_client.close()
@patch.object(rest.RESTClientObject, "request")
async def test_create_store(self, mock_request):
"""Test case for create_store
Create a store
"""
response_body = """{
"id": "01YCP46JKYM8FJCQ37NMBYHE5X",
"name": "test_store",
"created_at": "2022-07-25T17:41:26.607Z",
"updated_at": "2022-07-25T17:41:26.607Z"}
"""
mock_request.return_value = mock_response(response_body, 201)
configuration = self.configuration
async with openfga_sdk.ApiClient(configuration) as api_client:
api_instance = open_fga_api.OpenFgaApi(api_client)
body = CreateStoreRequest(
name="test-store",
)
api_response = await api_instance.create_store(
body=body,
)
self.assertIsInstance(api_response, CreateStoreResponse)
self.assertEqual(api_response.id, "01YCP46JKYM8FJCQ37NMBYHE5X")
mock_request.assert_called_once_with(
"POST",
"http://api.fga.example/stores",
headers=ANY,
query_params=[],
post_params=[],
body={"name": "test-store"},
_preload_content=ANY,
_request_timeout=None,
)
await api_client.close()
@patch.object(rest.RESTClientObject, "request")
async def test_delete_store(self, mock_request):
"""Test case for delete_store
Delete a store
"""
response_body = ""
mock_request.return_value = mock_response(response_body, 201)
configuration = self.configuration
configuration.store_id = store_id
async with openfga_sdk.ApiClient(configuration) as api_client:
api_instance = open_fga_api.OpenFgaApi(api_client)
await api_instance.delete_store()
mock_request.assert_called_once_with(
"DELETE",
"http://api.fga.example/stores/01H0H015178Y2V4CX10C2KGHF4",
headers=ANY,
body=None,
query_params=[],
post_params=[],
_preload_content=ANY,
_request_timeout=None,
)
await api_client.close()
@patch.object(rest.RESTClientObject, "request")
async def test_expand(self, mock_request):
"""Test case for expand
Expand all relationships in userset tree format, and following userset rewrite rules. Useful to reason about and debug a certain relationship
"""
response_body = """{
"tree": {"root": {"name": "document:budget#reader", "leaf": {"users": {"users": ["user:81684243-9356-4421-8fbf-a4f8d36aa31b"]}}}}}
"""
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
async with openfga_sdk.ApiClient(configuration) as api_client:
api_instance = open_fga_api.OpenFgaApi(api_client)
body = ExpandRequest(
tuple_key=ExpandRequestTupleKey(
object="document:budget",
relation="reader",
),
authorization_model_id="01GXSA8YR785C4FYS3C0RTG7B1",
)
api_response = await api_instance.expand(
body=body,
)
self.assertIsInstance(api_response, ExpandResponse)
cur_users = Users(users=["user:81684243-9356-4421-8fbf-a4f8d36aa31b"])
leaf = Leaf(users=cur_users)
node = Node(name="document:budget#reader", leaf=leaf)
userTree = UsersetTree(node)
expected_response = ExpandResponse(userTree)
self.assertEqual(api_response, expected_response)
mock_request.assert_called_once_with(
"POST",
"http://api.fga.example/stores/01H0H015178Y2V4CX10C2KGHF4/expand",
headers=ANY,
query_params=[],
post_params=[],
body={
"tuple_key": {"object": "document:budget", "relation": "reader"},
"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1",
},
_preload_content=ANY,
_request_timeout=None,
)
await api_client.close()
@patch.object(rest.RESTClientObject, "request")
async def test_get_store(self, mock_request):
"""Test case for get_store
Get a store
"""
response_body = """{
"id": "01H0H015178Y2V4CX10C2KGHF4",
"name": "test_store",
"created_at": "2022-07-25T20:45:10.485Z",
"updated_at": "2022-07-25T20:45:10.485Z"
}
"""
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
async with openfga_sdk.ApiClient(configuration) as api_client:
api_instance = open_fga_api.OpenFgaApi(api_client)
# Get a store
api_response = await api_instance.get_store()
self.assertIsInstance(api_response, GetStoreResponse)
self.assertEqual(api_response.id, "01H0H015178Y2V4CX10C2KGHF4")
self.assertEqual(api_response.name, "test_store")
mock_request.assert_called_once_with(
"GET",
"http://api.fga.example/stores/01H0H015178Y2V4CX10C2KGHF4",
headers=ANY,
body=None,
query_params=[],
post_params=[],
_preload_content=ANY,
_request_timeout=None,
)
await api_client.close()
@patch.object(rest.RESTClientObject, "request")
async def test_list_objects(self, mock_request):
"""Test case for list_objects
List objects
"""
response_body = """
{
"objects": [
"document:abcd1234"
]
}
"""
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
async with openfga_sdk.ApiClient(configuration) as api_client:
api_instance = open_fga_api.OpenFgaApi(api_client)
body = ListObjectsRequest(
authorization_model_id="01G5JAVJ41T49E9TT3SKVS7X1J",
type="document",
relation="reader",
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
)
# Get all stores
api_response = await api_instance.list_objects(body)
self.assertIsInstance(api_response, ListObjectsResponse)
self.assertEqual(api_response.objects, ["document:abcd1234"])
mock_request.assert_called_once_with(
"POST",
"http://api.fga.example/stores/01H0H015178Y2V4CX10C2KGHF4/list-objects",
headers=ANY,
query_params=[],
post_params=[],
body={
"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J",
"type": "document",
"relation": "reader",
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
},
_preload_content=ANY,
_request_timeout=None,
)
await api_client.close()
@patch.object(rest.RESTClientObject, "request")
async def test_list_stores(self, mock_request):
"""Test case for list_stores
Get all stores
"""
response_body = """
{
"stores": [
{
"id": "01YCP46JKYM8FJCQ37NMBYHE5X",
"name": "store1",
"created_at": "2022-07-25T21:15:37.524Z",
"updated_at": "2022-07-25T21:15:37.524Z",
"deleted_at": "2022-07-25T21:15:37.524Z"
},
{
"id": "01YCP46JKYM8FJCQ37NMBYHE6X",
"name": "store2",
"created_at": "2022-07-25T21:15:37.524Z",
"updated_at": "2022-07-25T21:15:37.524Z",
"deleted_at": "2022-07-25T21:15:37.524Z"
}
],
"continuation_token": "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ=="
}
"""
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
async with openfga_sdk.ApiClient(configuration) as api_client:
api_instance = open_fga_api.OpenFgaApi(api_client)
# Get all stores
api_response = await api_instance.list_stores(
page_size=1,
continuation_token="continuation_token_example",
)
self.assertIsInstance(api_response, ListStoresResponse)
self.assertEqual(
api_response.continuation_token,
"eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==",
)
store1 = Store(
id="01YCP46JKYM8FJCQ37NMBYHE5X",
name="store1",
created_at=datetime.fromisoformat("2022-07-25T21:15:37.524+00:00"),
updated_at=datetime.fromisoformat("2022-07-25T21:15:37.524+00:00"),
deleted_at=datetime.fromisoformat("2022-07-25T21:15:37.524+00:00"),
)
store2 = Store(
id="01YCP46JKYM8FJCQ37NMBYHE6X",
name="store2",
created_at=datetime.fromisoformat("2022-07-25T21:15:37.524+00:00"),
updated_at=datetime.fromisoformat("2022-07-25T21:15:37.524+00:00"),
deleted_at=datetime.fromisoformat("2022-07-25T21:15:37.524+00:00"),
)
stores = [store1, store2]
self.assertEqual(api_response.stores, stores)
mock_request.assert_called_once_with(
"GET",
"http://api.fga.example/stores",
headers=ANY,
body=None,
query_params=[
("page_size", 1),
("continuation_token", "continuation_token_example"),
],
post_params=[],
_preload_content=ANY,
_request_timeout=None,
)
await api_client.close()
@patch.object(rest.RESTClientObject, "request")
async def test_list_users(self, mock_request):
"""
Test case for list_users
"""
response_body = """{
"users": [
{
"object": {
"id": "81684243-9356-4421-8fbf-a4f8d36aa31b",
"type": "user"
}
},
{
"userset": {
"id": "fga",
"relation": "member",
"type": "team"
}
},
{
"wildcard": {
"type": "user"
}
}
]
}"""
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
async with openfga_sdk.ApiClient(configuration) as api_client:
api_instance = open_fga_api.OpenFgaApi(api_client)
request = ListUsersRequest(
authorization_model_id="01G5JAVJ41T49E9TT3SKVS7X1J",
object="document:2021-budget",
relation="can_read",
user_filters=[
{"type": "user"},
],
context={},
contextual_tuples=[
{
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
"relation": "editor",
"object": "folder:product",
},
{
"user": "folder:product",
"relation": "parent",
"object": "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
},
],
)
response = await api_instance.list_users(request)
self.assertIsInstance(response, ListUsersResponse)
self.assertEqual(response.users.__len__(), 3)
self.assertIsNotNone(response.users[0].object)
self.assertEqual(
response.users[0].object.id, "81684243-9356-4421-8fbf-a4f8d36aa31b"
)
self.assertEqual(response.users[0].object.type, "user")
self.assertIsNone(response.users[0].userset)
self.assertIsNone(response.users[0].wildcard)
self.assertIsNone(response.users[1].object)
self.assertIsNotNone(response.users[1].userset)
self.assertEqual(response.users[1].userset.id, "fga")
self.assertEqual(response.users[1].userset.relation, "member")
self.assertEqual(response.users[1].userset.type, "team")
self.assertIsNone(response.users[1].wildcard)
self.assertIsNone(response.users[2].object)
self.assertIsNone(response.users[2].userset)
self.assertIsNotNone(response.users[2].wildcard)
self.assertEqual(response.users[2].wildcard.type, "user")
mock_request.assert_called_once_with(
"POST",
"http://api.fga.example/stores/01H0H015178Y2V4CX10C2KGHF4/list-users",
headers=ANY,
query_params=[],
post_params=[],
body={
"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J",
"object": "document:2021-budget",
"relation": "can_read",
"user_filters": [
{"type": "user"},
],
"context": {},
"contextual_tuples": [
{
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
"relation": "editor",
"object": "folder:product",
},
{
"user": "folder:product",
"relation": "parent",
"object": "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
},
],
},
_preload_content=ANY,
_request_timeout=None,
)
await api_client.close()
@patch.object(rest.RESTClientObject, "request")
async def test_read(self, mock_request):
"""Test case for read
Get tuples from the store that matches a query, without following userset rewrite rules
"""
response_body = """
{
"tuples": [
{
"key": {
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
"relation": "reader",
"object": "document:2021-budget"
},
"timestamp": "2021-10-06T15:32:11.128Z"
}
],
"continuation_token": ""
}
"""
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
async with openfga_sdk.ApiClient(configuration) as api_client:
api_instance = open_fga_api.OpenFgaApi(api_client)
body = ReadRequest(
tuple_key=ReadRequestTupleKey(
object="document:2021-budget",
relation="reader",
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
),
page_size=50,
continuation_token="eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==",
)
api_response = await api_instance.read(
body=body,
)
self.assertIsInstance(api_response, ReadResponse)
key = TupleKey(
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation="reader",
object="document:2021-budget",
)
timestamp = datetime.fromisoformat("2021-10-06T15:32:11.128+00:00")
expected_data = ReadResponse(
tuples=[Tuple(key=key, timestamp=timestamp)], continuation_token=""
)
self.assertEqual(api_response, expected_data)
mock_request.assert_called_once_with(
"POST",
"http://api.fga.example/stores/01H0H015178Y2V4CX10C2KGHF4/read",
headers=ANY,
query_params=[],
post_params=[],
body={
"tuple_key": {
"object": "document:2021-budget",
"relation": "reader",
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
},
"page_size": 50,
"continuation_token": "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==",
},
_preload_content=ANY,
_request_timeout=None,
)
@patch.object(rest.RESTClientObject, "request")
async def test_read_assertions(self, mock_request):
"""Test case for read_assertions
Read assertions for an authorization model ID
"""
response_body = """
{
"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J",
"assertions": [
{
"tuple_key": {
"object": "document:2021-budget",
"relation": "reader",
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"
},
"expectation": true
}
]
}
"""
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
async with openfga_sdk.ApiClient(configuration) as api_client:
api_instance = open_fga_api.OpenFgaApi(api_client)
api_response = await api_instance.read_assertions(
"01G5JAVJ41T49E9TT3SKVS7X1J",
)
self.assertIsInstance(api_response, ReadAssertionsResponse)
self.assertEqual(
api_response.authorization_model_id, "01G5JAVJ41T49E9TT3SKVS7X1J"
)
assertion = Assertion(
tuple_key=TupleKeyWithoutCondition(
object="document:2021-budget",
relation="reader",
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
),
expectation=True,
)
self.assertEqual(api_response.assertions, [assertion])
mock_request.assert_called_once_with(
"GET",
"http://api.fga.example/stores/01H0H015178Y2V4CX10C2KGHF4/assertions/01G5JAVJ41T49E9TT3SKVS7X1J",
headers=ANY,
body=None,
query_params=[],
post_params=[],
_preload_content=ANY,
_request_timeout=None,
)
@patch.object(rest.RESTClientObject, "request")
async def test_read_authorization_model(self, mock_request):
"""Test case for read_authorization_model
Return a particular version of an authorization model
"""
response_body = """
{
"authorization_model": {
"id": "01G5JAVJ41T49E9TT3SKVS7X1J",
"schema_version":"1.1",
"type_definitions": [
{
"type": "document",
"relations": {
"reader": {
"union": {
"child": [
{
"this": {}
},
{
"computedUserset": {
"object": "",
"relation": "writer"
}
}
]
}
},
"writer": {
"this": {}
}
}
}
]
}
}
"""
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
async with openfga_sdk.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = open_fga_api.OpenFgaApi(api_client)
# Return a particular version of an authorization model
api_response = await api_instance.read_authorization_model(
"01G5JAVJ41T49E9TT3SKVS7X1J",
)
self.assertIsInstance(api_response, ReadAuthorizationModelResponse)
type_definitions = [
TypeDefinition(
type="document",
relations=dict(
reader=Userset(
union=Usersets(
child=[
Userset(this=dict()),
Userset(
computed_userset=ObjectRelation(
object="",
relation="writer",
)
),
],
),
),
writer=Userset(
this=dict(),
),
),
)
]
authorization_model = AuthorizationModel(
id="01G5JAVJ41T49E9TT3SKVS7X1J",
schema_version="1.1",
type_definitions=type_definitions,
)
self.assertEqual(api_response.authorization_model, authorization_model)
mock_request.assert_called_once_with(
"GET",
"http://api.fga.example/stores/01H0H015178Y2V4CX10C2KGHF4/authorization-models/01G5JAVJ41T49E9TT3SKVS7X1J",
headers=ANY,
body=None,
query_params=[],
post_params=[],
_preload_content=ANY,
_request_timeout=None,
)
@patch.object(rest.RESTClientObject, "request")
async def test_read_changes(self, mock_request):
"""Test case for read_changes
Return a list of all the tuple changes
"""
response_body = """
{
"changes": [
{
"tuple_key": {
"object": "document:2021-budget",
"relation": "reader",
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b"
},
"operation": "TUPLE_OPERATION_WRITE",
"timestamp": "2022-07-26T15:55:55.809Z"
}
],
"continuation_token": "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ=="
}
"""
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
async with openfga_sdk.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = open_fga_api.OpenFgaApi(api_client)
# Return a particular version of an authorization model
api_response = await api_instance.read_changes(
page_size=1,
continuation_token="abcdefg",
start_time="2022-01-01T00:00:00+00:00",
type="document",
)
self.assertIsInstance(api_response, ReadChangesResponse)
changes = TupleChange(
tuple_key=TupleKey(
object="document:2021-budget",
relation="reader",
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
),
operation=TupleOperation.WRITE,
timestamp=datetime.fromisoformat("2022-07-26T15:55:55.809+00:00"),
)
read_changes = ReadChangesResponse(
continuation_token="eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==",
changes=[changes],
)
self.assertEqual(api_response, read_changes)
mock_request.assert_called_once_with(
"GET",
"http://api.fga.example/stores/01H0H015178Y2V4CX10C2KGHF4/changes",
headers=ANY,
body=None,
query_params=[
("type", "document"),
("page_size", 1),
("continuation_token", "abcdefg"),
("start_time", "2022-01-01T00:00:00+00:00"),
],
post_params=[],
_preload_content=ANY,
_request_timeout=None,
)
@patch.object(rest.RESTClientObject, "request")
async def test_write(self, mock_request):
"""Test case for write
Add tuples from the store
"""
response_body = "{}"
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
async with openfga_sdk.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = open_fga_api.OpenFgaApi(api_client)
# example passing only required values which don't have defaults set
body = WriteRequest(
writes=WriteRequestWrites(
tuple_keys=[
TupleKey(
object="document:2021-budget",
relation="reader",
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
)
],
),
authorization_model_id="01G5JAVJ41T49E9TT3SKVS7X1J",
)
await api_instance.write(
body,
)
mock_request.assert_called_once_with(
"POST",
"http://api.fga.example/stores/01H0H015178Y2V4CX10C2KGHF4/write",
headers=ANY,
query_params=[],
post_params=[],
body={
"writes": {
"tuple_keys": [
{
"object": "document:2021-budget",
"relation": "reader",
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
}
]
},
"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J",
},
_preload_content=ANY,
_request_timeout=None,
)
@patch.object(rest.RESTClientObject, "request")
async def test_write_delete(self, mock_request):
"""Test case for write
Delete tuples from the store
"""
response_body = "{}"
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
async with openfga_sdk.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = open_fga_api.OpenFgaApi(api_client)
# example passing only required values which don't have defaults set
body = WriteRequest(
deletes=WriteRequestDeletes(
tuple_keys=[
TupleKey(
object="document:2021-budget",
relation="reader",
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
)
],
),
authorization_model_id="01G5JAVJ41T49E9TT3SKVS7X1J",
)
await api_instance.write(
body,
)
mock_request.assert_called_once_with(
"POST",
"http://api.fga.example/stores/01H0H015178Y2V4CX10C2KGHF4/write",
headers=ANY,
query_params=[],
post_params=[],
body={
"deletes": {
"tuple_keys": [
{
"object": "document:2021-budget",
"relation": "reader",
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
}
]
},
"authorization_model_id": "01G5JAVJ41T49E9TT3SKVS7X1J",
},
_preload_content=ANY,
_request_timeout=None,
)
@patch.object(rest.RESTClientObject, "request")
async def test_write_assertions(self, mock_request):
"""Test case for write_assertions
Upsert assertions for an authorization model ID
"""
response_body = ""
mock_request.return_value = mock_response(response_body, 204)
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
async with openfga_sdk.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = open_fga_api.OpenFgaApi(api_client)
# example passing only required values which don't have defaults set
body = WriteAssertionsRequest(
assertions=[
Assertion(
tuple_key=TupleKey(
object="document:2021-budget",
relation="reader",
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
),
expectation=True,
)
],
)
# Upsert assertions for an authorization model ID
await api_instance.write_assertions(
authorization_model_id="xyz0123",
body=body,
)
mock_request.assert_called_once_with(
"PUT",
"http://api.fga.example/stores/01H0H015178Y2V4CX10C2KGHF4/assertions/xyz0123",
headers=ANY,
query_params=[],
post_params=[],
body={
"assertions": [
{
"expectation": True,
"tuple_key": {
"object": "document:2021-budget",
"relation": "reader",
"user": "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
},
}
]
},
_preload_content=ANY,
_request_timeout=None,