Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 4f1110c

Browse files
committed
refactor: use 'Client._get_path' in 'Blob.test_iam_permissions'
Toward #38.
1 parent 7b54756 commit 4f1110c

File tree

2 files changed

+47
-43
lines changed

2 files changed

+47
-43
lines changed

google/cloud/storage/blob.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2968,12 +2968,12 @@ def test_iam_permissions(
29682968
query_params["userProject"] = self.user_project
29692969

29702970
path = "%s/iam/testPermissions" % (self.path,)
2971-
resp = client._connection.api_request(
2972-
method="GET",
2973-
path=path,
2971+
resp = client._get_path(
2972+
path,
29742973
query_params=query_params,
29752974
timeout=timeout,
29762975
retry=retry,
2976+
_target_object=None,
29772977
)
29782978

29792979
return resp.get("permissions", [])

tests/unit/test_blob.py

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3351,71 +3351,75 @@ def test_set_iam_policy_w_user_project(self):
33513351
self.assertEqual(kw[0]["query_params"], {"userProject": USER_PROJECT})
33523352
self.assertEqual(kw[0]["data"], {"resourceId": PATH})
33533353

3354-
def test_test_iam_permissions(self):
3354+
def test_test_iam_permissions_defaults(self):
33553355
from google.cloud.storage.iam import STORAGE_OBJECTS_LIST
33563356
from google.cloud.storage.iam import STORAGE_BUCKETS_GET
33573357
from google.cloud.storage.iam import STORAGE_BUCKETS_UPDATE
33583358

3359-
BLOB_NAME = "blob-name"
3360-
PATH = "/b/name/o/%s" % (BLOB_NAME,)
3361-
PERMISSIONS = [
3359+
blob_name = "blob-name"
3360+
permissions = [
33623361
STORAGE_OBJECTS_LIST,
33633362
STORAGE_BUCKETS_GET,
33643363
STORAGE_BUCKETS_UPDATE,
33653364
]
3366-
ALLOWED = PERMISSIONS[1:]
3367-
RETURNED = {"permissions": ALLOWED}
3368-
after = ({"status": http_client.OK}, RETURNED)
3369-
connection = _Connection(after)
3370-
client = _Client(connection)
3365+
expected = permissions[1:]
3366+
api_response = {"permissions": expected}
3367+
client = mock.Mock(spec=["_get_path"])
3368+
client._get_path.return_value = api_response
33713369
bucket = _Bucket(client=client)
3372-
blob = self._make_one(BLOB_NAME, bucket=bucket)
3370+
blob = self._make_one(blob_name, bucket=bucket)
33733371

3374-
allowed = blob.test_iam_permissions(PERMISSIONS, timeout=42)
3372+
found = blob.test_iam_permissions(permissions)
33753373

3376-
self.assertEqual(allowed, ALLOWED)
3374+
self.assertEqual(found, expected)
33773375

3378-
kw = connection._requested
3379-
self.assertEqual(len(kw), 1)
3380-
self.assertEqual(kw[0]["method"], "GET")
3381-
self.assertEqual(kw[0]["path"], "%s/iam/testPermissions" % (PATH,))
3382-
self.assertEqual(kw[0]["query_params"], {"permissions": PERMISSIONS})
3383-
self.assertEqual(kw[0]["timeout"], 42)
3376+
expected_path = "/b/name/o/%s/iam/testPermissions" % (blob_name,)
3377+
expected_query_params = {"permissions": permissions}
3378+
client._get_path.assert_called_once_with(
3379+
expected_path,
3380+
query_params=expected_query_params,
3381+
timeout=self._get_default_timeout(),
3382+
retry=DEFAULT_RETRY,
3383+
_target_object=None,
3384+
)
33843385

3385-
def test_test_iam_permissions_w_user_project(self):
3386+
def test_test_iam_permissions_w_user_project_w_timeout_w_retry(self):
33863387
from google.cloud.storage.iam import STORAGE_OBJECTS_LIST
33873388
from google.cloud.storage.iam import STORAGE_BUCKETS_GET
33883389
from google.cloud.storage.iam import STORAGE_BUCKETS_UPDATE
33893390

3390-
BLOB_NAME = "blob-name"
3391-
USER_PROJECT = "user-project-123"
3392-
PATH = "/b/name/o/%s" % (BLOB_NAME,)
3393-
PERMISSIONS = [
3391+
blob_name = "blob-name"
3392+
user_project = "user-project-123"
3393+
timeout = 42
3394+
retry = mock.Mock(spec=[])
3395+
permissions = [
33943396
STORAGE_OBJECTS_LIST,
33953397
STORAGE_BUCKETS_GET,
33963398
STORAGE_BUCKETS_UPDATE,
33973399
]
3398-
ALLOWED = PERMISSIONS[1:]
3399-
RETURNED = {"permissions": ALLOWED}
3400-
after = ({"status": http_client.OK}, RETURNED)
3401-
connection = _Connection(after)
3402-
client = _Client(connection)
3403-
bucket = _Bucket(client=client, user_project=USER_PROJECT)
3404-
blob = self._make_one(BLOB_NAME, bucket=bucket)
3400+
expected = permissions[1:]
3401+
api_response = {"permissions": expected}
3402+
client = mock.Mock(spec=["_get_path"])
3403+
client._get_path.return_value = api_response
3404+
bucket = _Bucket(client=client, user_project=user_project)
3405+
blob = self._make_one(blob_name, bucket=bucket)
34053406

3406-
allowed = blob.test_iam_permissions(PERMISSIONS)
3407+
found = blob.test_iam_permissions(permissions, timeout=timeout, retry=retry)
34073408

3408-
self.assertEqual(allowed, ALLOWED)
3409+
self.assertEqual(found, expected)
34093410

3410-
kw = connection._requested
3411-
self.assertEqual(len(kw), 1)
3412-
self.assertEqual(kw[0]["method"], "GET")
3413-
self.assertEqual(kw[0]["path"], "%s/iam/testPermissions" % (PATH,))
3414-
self.assertEqual(
3415-
kw[0]["query_params"],
3416-
{"permissions": PERMISSIONS, "userProject": USER_PROJECT},
3411+
expected_path = "/b/name/o/%s/iam/testPermissions" % (blob_name,)
3412+
expected_query_params = {
3413+
"permissions": permissions,
3414+
"userProject": user_project,
3415+
}
3416+
client._get_path.assert_called_once_with(
3417+
expected_path,
3418+
query_params=expected_query_params,
3419+
timeout=timeout,
3420+
retry=retry,
3421+
_target_object=None,
34173422
)
3418-
self.assertEqual(kw[0]["timeout"], self._get_default_timeout())
34193423

34203424
def test_make_public(self):
34213425
from google.cloud.storage.acl import _ACLEntity

0 commit comments

Comments
 (0)