Skip to content

Commit 970e1c0

Browse files
committed
Making cors a property instead of having two separate methods.
1 parent f65f7c7 commit 970e1c0

File tree

2 files changed

+50
-81
lines changed

2 files changed

+50
-81
lines changed

gcloud/storage/bucket.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,28 +457,30 @@ def upload_file_object(self, file_obj, blob_name=None):
457457
blob.upload_from_file(file_obj)
458458
return blob
459459

460-
def get_cors(self):
460+
@property
461+
def cors(self):
461462
"""Retrieve CORS policies configured for this bucket.
462463
463464
See: http://www.w3.org/TR/cors/ and
464465
https://cloud.google.com/storage/docs/json_api/v1/buckets
465466
466-
:rtype: list(dict)
467+
:rtype: list of dictionaries
467468
:returns: A sequence of mappings describing each CORS policy.
468469
"""
469-
return [policy.copy() for policy in self.properties.get('cors', ())]
470+
return [copy.deepcopy(policy)
471+
for policy in self.properties.get('cors', ())]
470472

471-
def update_cors(self, entries):
472-
"""Update CORS policies configured for this bucket.
473+
@cors.setter
474+
def cors(self, entries):
475+
"""Set CORS policies configured for this bucket.
473476
474477
See: http://www.w3.org/TR/cors/ and
475478
https://cloud.google.com/storage/docs/json_api/v1/buckets
476479
477-
:type entries: list(dict)
480+
:type entries: list of dictionaries
478481
:param entries: A sequence of mappings describing each CORS policy.
479482
"""
480483
self._patch_properties({'cors': entries})
481-
self.patch()
482484

483485
def get_default_object_acl(self):
484486
"""Get the current Default Object ACL rules.

gcloud/storage/test_bucket.py

Lines changed: 41 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -564,80 +564,6 @@ def upload_from_file(self, fh):
564564
self.assertEqual(found._name, BLOB_NAME)
565565
self.assertTrue(found._bucket is bucket)
566566

567-
def test_get_cors_eager(self):
568-
NAME = 'name'
569-
CORS_ENTRY = {
570-
'maxAgeSeconds': 1234,
571-
'method': ['OPTIONS', 'GET'],
572-
'origin': ['127.0.0.1'],
573-
'responseHeader': ['Content-Type'],
574-
}
575-
before = {'cors': [CORS_ENTRY, {}]}
576-
connection = _Connection()
577-
bucket = self._makeOne(NAME, connection, properties=before)
578-
entries = bucket.get_cors()
579-
self.assertEqual(len(entries), 2)
580-
self.assertEqual(entries[0]['maxAgeSeconds'],
581-
CORS_ENTRY['maxAgeSeconds'])
582-
self.assertEqual(entries[0]['method'],
583-
CORS_ENTRY['method'])
584-
self.assertEqual(entries[0]['origin'],
585-
CORS_ENTRY['origin'])
586-
self.assertEqual(entries[0]['responseHeader'],
587-
CORS_ENTRY['responseHeader'])
588-
self.assertEqual(entries[1], {})
589-
kw = connection._requested
590-
self.assertEqual(len(kw), 0)
591-
592-
def test_get_cors_lazy(self):
593-
NAME = 'name'
594-
CORS_ENTRY = {
595-
'maxAgeSeconds': 1234,
596-
'method': ['OPTIONS', 'GET'],
597-
'origin': ['127.0.0.1'],
598-
'responseHeader': ['Content-Type'],
599-
}
600-
after = {'cors': [CORS_ENTRY]}
601-
connection = _Connection(after)
602-
bucket = self._makeOne(NAME, connection)
603-
bucket._reload_properties()
604-
entries = bucket.get_cors()
605-
self.assertEqual(len(entries), 1)
606-
self.assertEqual(entries[0]['maxAgeSeconds'],
607-
CORS_ENTRY['maxAgeSeconds'])
608-
self.assertEqual(entries[0]['method'],
609-
CORS_ENTRY['method'])
610-
self.assertEqual(entries[0]['origin'],
611-
CORS_ENTRY['origin'])
612-
self.assertEqual(entries[0]['responseHeader'],
613-
CORS_ENTRY['responseHeader'])
614-
kw = connection._requested
615-
self.assertEqual(len(kw), 1)
616-
self.assertEqual(kw[0]['method'], 'GET')
617-
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
618-
self.assertEqual(kw[0]['query_params'], {'projection': 'noAcl'})
619-
620-
def test_update_cors(self):
621-
NAME = 'name'
622-
CORS_ENTRY = {
623-
'maxAgeSeconds': 1234,
624-
'method': ['OPTIONS', 'GET'],
625-
'origin': ['127.0.0.1'],
626-
'responseHeader': ['Content-Type'],
627-
}
628-
after = {'cors': [CORS_ENTRY, {}]}
629-
connection = _Connection(after)
630-
bucket = self._makeOne(NAME, connection)
631-
bucket.update_cors([CORS_ENTRY, {}])
632-
kw = connection._requested
633-
self.assertEqual(len(kw), 1)
634-
self.assertEqual(kw[0]['method'], 'PATCH')
635-
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
636-
self.assertEqual(kw[0]['data'], after)
637-
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
638-
entries = bucket.get_cors()
639-
self.assertEqual(entries, [CORS_ENTRY, {}])
640-
641567
def test_get_default_object_acl_lazy(self):
642568
from gcloud.storage.acl import BucketACL
643569
NAME = 'name'
@@ -727,6 +653,47 @@ def test_lifecycle_rules_setter(self):
727653
self.assertEqual(kw[0]['data'], after)
728654
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
729655

656+
def test_cors_getter(self):
657+
NAME = 'name'
658+
CORS_ENTRY = {
659+
'maxAgeSeconds': 1234,
660+
'method': ['OPTIONS', 'GET'],
661+
'origin': ['127.0.0.1'],
662+
'responseHeader': ['Content-Type'],
663+
}
664+
properties = {'cors': [CORS_ENTRY, {}]}
665+
bucket = self._makeOne(NAME, properties=properties)
666+
entries = bucket.cors
667+
self.assertEqual(len(entries), 2)
668+
self.assertEqual(entries[0], CORS_ENTRY)
669+
self.assertEqual(entries[1], {})
670+
# Make sure it was a copy, not the same object.
671+
self.assertFalse(entries[0] is CORS_ENTRY)
672+
673+
def test_cors_setter(self):
674+
NAME = 'name'
675+
CORS_ENTRY = {
676+
'maxAgeSeconds': 1234,
677+
'method': ['OPTIONS', 'GET'],
678+
'origin': ['127.0.0.1'],
679+
'responseHeader': ['Content-Type'],
680+
}
681+
DATA = {'cors': [CORS_ENTRY]}
682+
connection = _Connection(DATA)
683+
bucket = self._makeOne(NAME, connection)
684+
685+
self.assertEqual(bucket.cors, [])
686+
687+
bucket.cors = [CORS_ENTRY]
688+
bucket.patch()
689+
self.assertEqual(bucket.cors, [CORS_ENTRY])
690+
kw = connection._requested
691+
self.assertEqual(len(kw), 1)
692+
self.assertEqual(kw[0]['method'], 'PATCH')
693+
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
694+
self.assertEqual(kw[0]['data'], DATA)
695+
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
696+
730697
def test_get_logging_w_prefix(self):
731698
NAME = 'name'
732699
LOG_BUCKET = 'logs'

0 commit comments

Comments
 (0)