|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +from descope import AuthException, DescopeClient |
| 4 | +from descope.common import DEFAULT_TIMEOUT_SECONDS |
| 5 | +from descope.management.common import MgmtV1 |
| 6 | + |
| 7 | +from .. import common |
| 8 | + |
| 9 | + |
| 10 | +class TestFGA(common.DescopeTest): |
| 11 | + def setUp(self) -> None: |
| 12 | + super().setUp() |
| 13 | + self.dummy_project_id = "dummy" |
| 14 | + self.dummy_management_key = "key" |
| 15 | + self.public_key_dict = { |
| 16 | + "alg": "ES384", |
| 17 | + "crv": "P-384", |
| 18 | + "kid": "P2CtzUhdqpIF2ys9gg7ms06UvtC4", |
| 19 | + "kty": "EC", |
| 20 | + "use": "sig", |
| 21 | + "x": "pX1l7nT2turcK5_Cdzos8SKIhpLh1Wy9jmKAVyMFiOCURoj-WQX1J0OUQqMsQO0s", |
| 22 | + "y": "B0_nWAv2pmG_PzoH3-bSYZZzLNKUA0RoE2SH7DaS0KV4rtfWZhYd0MEr0xfdGKx0", |
| 23 | + } |
| 24 | + |
| 25 | + def test_save_schema(self): |
| 26 | + client = DescopeClient( |
| 27 | + self.dummy_project_id, |
| 28 | + self.public_key_dict, |
| 29 | + False, |
| 30 | + self.dummy_management_key, |
| 31 | + ) |
| 32 | + |
| 33 | + # Test failed save_schema |
| 34 | + with patch("requests.post") as mock_post: |
| 35 | + mock_post.return_value.ok = False |
| 36 | + self.assertRaises(AuthException, client.mgmt.fga.save_schema, "") |
| 37 | + |
| 38 | + # Test success flow |
| 39 | + with patch("requests.post") as mock_post: |
| 40 | + mock_post.return_value.ok = True |
| 41 | + self.assertIsNone(client.mgmt.fgs.save_schema("model AuthZ 1.0")) |
| 42 | + mock_post.assert_called_with( |
| 43 | + f"{common.DEFAULT_BASE_URL}{MgmtV1.fga_save_schema}", |
| 44 | + headers={ |
| 45 | + **common.default_headers, |
| 46 | + "Authorization": f"Bearer {self.dummy_project_id}:{self.dummy_management_key}", |
| 47 | + }, |
| 48 | + params=None, |
| 49 | + json={"dsl": "model AuthZ 1.0"}, |
| 50 | + allow_redirects=False, |
| 51 | + verify=True, |
| 52 | + timeout=DEFAULT_TIMEOUT_SECONDS, |
| 53 | + ) |
| 54 | + |
| 55 | + def test_create_relations(self): |
| 56 | + client = DescopeClient( |
| 57 | + self.dummy_project_id, |
| 58 | + self.public_key_dict, |
| 59 | + False, |
| 60 | + self.dummy_management_key, |
| 61 | + ) |
| 62 | + |
| 63 | + # Test failed create_relations |
| 64 | + with patch("requests.post") as mock_post: |
| 65 | + mock_post.return_value.ok = False |
| 66 | + self.assertRaises(AuthException, client.mgmt.fga.create_relations, []) |
| 67 | + |
| 68 | + # Test success flow |
| 69 | + with patch("requests.post") as mock_post: |
| 70 | + mock_post.return_value.ok = True |
| 71 | + self.assertIsNone( |
| 72 | + client.mgmt.fga.create_relations( |
| 73 | + [ |
| 74 | + { |
| 75 | + "resource": "r", |
| 76 | + "resourceType": "rt", |
| 77 | + "relation": "rel", |
| 78 | + "target": "u", |
| 79 | + "targetType": "ty" |
| 80 | + } |
| 81 | + ] |
| 82 | + ) |
| 83 | + ) |
| 84 | + mock_post.assert_called_with( |
| 85 | + f"{common.DEFAULT_BASE_URL}{MgmtV1.fga_create_relations}", |
| 86 | + headers={ |
| 87 | + **common.default_headers, |
| 88 | + "Authorization": f"Bearer {self.dummy_project_id}:{self.dummy_management_key}", |
| 89 | + }, |
| 90 | + params=None, |
| 91 | + json={ |
| 92 | + "tuples": [ |
| 93 | + { |
| 94 | + "resource": "r", |
| 95 | + "resourceType": "rt", |
| 96 | + "relation": "rel", |
| 97 | + "target": "u", |
| 98 | + "targetType": "ty" |
| 99 | + } |
| 100 | + ] |
| 101 | + }, |
| 102 | + allow_redirects=False, |
| 103 | + verify=True, |
| 104 | + timeout=DEFAULT_TIMEOUT_SECONDS, |
| 105 | + ) |
| 106 | + |
| 107 | + def test_delete_relations(self): |
| 108 | + client = DescopeClient( |
| 109 | + self.dummy_project_id, |
| 110 | + self.public_key_dict, |
| 111 | + False, |
| 112 | + self.dummy_management_key, |
| 113 | + ) |
| 114 | + |
| 115 | + # Test failed delete_relations |
| 116 | + with patch("requests.post") as mock_post: |
| 117 | + mock_post.return_value.ok = False |
| 118 | + self.assertRaises(AuthException, client.mgmt.fga.delete_relations, []) |
| 119 | + |
| 120 | + # Test success flow |
| 121 | + with patch("requests.post") as mock_post: |
| 122 | + mock_post.return_value.ok = True |
| 123 | + self.assertIsNone( |
| 124 | + client.mgmt.authz.delete_relations( |
| 125 | + [ |
| 126 | + { |
| 127 | + "resource": "r", |
| 128 | + "resourceType": "rt", |
| 129 | + "relation": "rel", |
| 130 | + "target": "u", |
| 131 | + "targetType": "ty" |
| 132 | + } |
| 133 | + ] |
| 134 | + ) |
| 135 | + ) |
| 136 | + mock_post.assert_called_with( |
| 137 | + f"{common.DEFAULT_BASE_URL}{MgmtV1.fga_delete_relations}", |
| 138 | + headers={ |
| 139 | + **common.default_headers, |
| 140 | + "Authorization": f"Bearer {self.dummy_project_id}:{self.dummy_management_key}", |
| 141 | + }, |
| 142 | + params=None, |
| 143 | + json={ |
| 144 | + "tuples": [ |
| 145 | + { |
| 146 | + "resource": "r", |
| 147 | + "resourceType": "rt", |
| 148 | + "relation": "rel", |
| 149 | + "target": "u", |
| 150 | + "targetType": "ty" |
| 151 | + } |
| 152 | + ] |
| 153 | + }, |
| 154 | + allow_redirects=False, |
| 155 | + verify=True, |
| 156 | + timeout=DEFAULT_TIMEOUT_SECONDS, |
| 157 | + ) |
| 158 | + |
| 159 | + def test_check(self): |
| 160 | + client = DescopeClient( |
| 161 | + self.dummy_project_id, |
| 162 | + self.public_key_dict, |
| 163 | + False, |
| 164 | + self.dummy_management_key, |
| 165 | + ) |
| 166 | + |
| 167 | + # Test failed has_relations |
| 168 | + with patch("requests.post") as mock_post: |
| 169 | + mock_post.return_value.ok = False |
| 170 | + self.assertRaises(AuthException, client.mgmt.fga.check, []) |
| 171 | + |
| 172 | + # Test success flow |
| 173 | + with patch("requests.post") as mock_post: |
| 174 | + mock_post.return_value.ok = True |
| 175 | + self.assertIsNotNone( |
| 176 | + client.mgmt.fga.check( |
| 177 | + [ |
| 178 | + { |
| 179 | + "resource": "r", |
| 180 | + "resourceType": "rt", |
| 181 | + "relation": "rel", |
| 182 | + "target": "u", |
| 183 | + "targetType": "ty" |
| 184 | + } |
| 185 | + ] |
| 186 | + ) |
| 187 | + ) |
| 188 | + mock_post.assert_called_with( |
| 189 | + f"{common.DEFAULT_BASE_URL}{MgmtV1.fga_check}", |
| 190 | + headers={ |
| 191 | + **common.default_headers, |
| 192 | + "Authorization": f"Bearer {self.dummy_project_id}:{self.dummy_management_key}", |
| 193 | + }, |
| 194 | + params=None, |
| 195 | + json={ |
| 196 | + "relationQueries": [ |
| 197 | + { |
| 198 | + "resource": "r", |
| 199 | + "relationDefinition": "rd", |
| 200 | + "namespace": "ns", |
| 201 | + "target": "u", |
| 202 | + } |
| 203 | + ] |
| 204 | + }, |
| 205 | + allow_redirects=False, |
| 206 | + verify=True, |
| 207 | + timeout=DEFAULT_TIMEOUT_SECONDS, |
| 208 | + ) |
0 commit comments