|
| 1 | +from collections.abc import Iterable |
| 2 | +from copy import deepcopy |
1 | 3 | from datetime import datetime |
2 | 4 | from test.unit.base import ClientBaseCase |
3 | 5 |
|
|
21 | 23 | ServiceTransfer, |
22 | 24 | StackScript, |
23 | 25 | User, |
| 26 | + UserGrants, |
24 | 27 | Volume, |
25 | 28 | get_obj_grants, |
26 | 29 | ) |
27 | 30 | from linode_api4.objects.account import ChildAccount |
| 31 | +from linode_api4.objects.vpc import VPC |
28 | 32 |
|
29 | 33 |
|
30 | 34 | class InvoiceTest(ClientBaseCase): |
@@ -204,22 +208,6 @@ def test_get_payment_method(self): |
204 | 208 | self.assertTrue(paymentMethod.is_default) |
205 | 209 | self.assertEqual(paymentMethod.type, "credit_card") |
206 | 210 |
|
207 | | - def test_get_user_grant(self): |
208 | | - """ |
209 | | - Tests that a user grant is loaded correctly |
210 | | - """ |
211 | | - grants = get_obj_grants() |
212 | | - |
213 | | - self.assertTrue(grants.count(("linode", Instance)) > 0) |
214 | | - self.assertTrue(grants.count(("domain", Domain)) > 0) |
215 | | - self.assertTrue(grants.count(("stackscript", StackScript)) > 0) |
216 | | - self.assertTrue(grants.count(("nodebalancer", NodeBalancer)) > 0) |
217 | | - self.assertTrue(grants.count(("volume", Volume)) > 0) |
218 | | - self.assertTrue(grants.count(("image", Image)) > 0) |
219 | | - self.assertTrue(grants.count(("longview", LongviewClient)) > 0) |
220 | | - self.assertTrue(grants.count(("database", Database)) > 0) |
221 | | - self.assertTrue(grants.count(("firewall", Firewall)) > 0) |
222 | | - |
223 | 211 | def test_payment_method_make_default(self): |
224 | 212 | """ |
225 | 213 | Tests that making a payment method default creates the correct api request. |
@@ -309,3 +297,89 @@ def test_child_account_create_token(self): |
309 | 297 | token = child_account.create_token() |
310 | 298 | self.assertEqual(token.token, "abcdefghijklmnop") |
311 | 299 | self.assertEqual(m.call_data, {}) |
| 300 | + |
| 301 | + |
| 302 | +def test_get_user_grant(): |
| 303 | + """ |
| 304 | + Tests that a user grant is loaded correctly |
| 305 | + """ |
| 306 | + grants = get_obj_grants() |
| 307 | + |
| 308 | + assert grants.count(("linode", Instance)) > 0 |
| 309 | + assert grants.count(("domain", Domain)) > 0 |
| 310 | + assert grants.count(("stackscript", StackScript)) > 0 |
| 311 | + assert grants.count(("nodebalancer", NodeBalancer)) > 0 |
| 312 | + assert grants.count(("volume", Volume)) > 0 |
| 313 | + assert grants.count(("image", Image)) > 0 |
| 314 | + assert grants.count(("longview", LongviewClient)) > 0 |
| 315 | + assert grants.count(("database", Database)) > 0 |
| 316 | + assert grants.count(("firewall", Firewall)) > 0 |
| 317 | + assert grants.count(("vpc", VPC)) > 0 |
| 318 | + |
| 319 | + |
| 320 | +def test_user_grants_serialization(): |
| 321 | + """ |
| 322 | + Tests that user grants from JSON is serialized correctly |
| 323 | + """ |
| 324 | + user_grants_json = { |
| 325 | + "database": [ |
| 326 | + {"id": 123, "label": "example-entity", "permissions": "read_only"} |
| 327 | + ], |
| 328 | + "domain": [ |
| 329 | + {"id": 123, "label": "example-entity", "permissions": "read_only"} |
| 330 | + ], |
| 331 | + "firewall": [ |
| 332 | + {"id": 123, "label": "example-entity", "permissions": "read_only"} |
| 333 | + ], |
| 334 | + "global": { |
| 335 | + "account_access": "read_only", |
| 336 | + "add_databases": True, |
| 337 | + "add_domains": True, |
| 338 | + "add_firewalls": True, |
| 339 | + "add_images": True, |
| 340 | + "add_linodes": True, |
| 341 | + "add_longview": True, |
| 342 | + "add_nodebalancers": True, |
| 343 | + "add_placement_groups": True, |
| 344 | + "add_stackscripts": True, |
| 345 | + "add_volumes": True, |
| 346 | + "add_vpcs": True, |
| 347 | + "cancel_account": False, |
| 348 | + "child_account_access": True, |
| 349 | + "longview_subscription": True, |
| 350 | + }, |
| 351 | + "image": [ |
| 352 | + {"id": 123, "label": "example-entity", "permissions": "read_only"} |
| 353 | + ], |
| 354 | + "linode": [ |
| 355 | + {"id": 123, "label": "example-entity", "permissions": "read_only"} |
| 356 | + ], |
| 357 | + "longview": [ |
| 358 | + {"id": 123, "label": "example-entity", "permissions": "read_only"} |
| 359 | + ], |
| 360 | + "nodebalancer": [ |
| 361 | + {"id": 123, "label": "example-entity", "permissions": "read_only"} |
| 362 | + ], |
| 363 | + "stackscript": [ |
| 364 | + {"id": 123, "label": "example-entity", "permissions": "read_only"} |
| 365 | + ], |
| 366 | + "volume": [ |
| 367 | + {"id": 123, "label": "example-entity", "permissions": "read_only"} |
| 368 | + ], |
| 369 | + "vpc": [ |
| 370 | + {"id": 123, "label": "example-entity", "permissions": "read_only"} |
| 371 | + ], |
| 372 | + } |
| 373 | + |
| 374 | + expected_serialized_grants = deepcopy(user_grants_json) |
| 375 | + |
| 376 | + for grants in expected_serialized_grants.values(): |
| 377 | + if isinstance(grants, Iterable): |
| 378 | + for grant in grants: |
| 379 | + if isinstance(grant, dict) and "label" in grant: |
| 380 | + del grant["label"] |
| 381 | + |
| 382 | + assert ( |
| 383 | + UserGrants(None, None, user_grants_json)._serialize() |
| 384 | + == expected_serialized_grants |
| 385 | + ) |
0 commit comments