Skip to content

Commit adc0029

Browse files
committed
Merge pull request googleapis#768 from dhermes/make-cors-property
Making cors a property instead of having two separate methods.
2 parents 4e64267 + 970e1c0 commit adc0029

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
@@ -452,28 +452,30 @@ def upload_file_object(self, file_obj, blob_name=None):
452452
blob.upload_from_file(file_obj)
453453
return blob
454454

455-
def get_cors(self):
455+
@property
456+
def cors(self):
456457
"""Retrieve CORS policies configured for this bucket.
457458
458459
See: http://www.w3.org/TR/cors/ and
459460
https://cloud.google.com/storage/docs/json_api/v1/buckets
460461
461-
:rtype: list(dict)
462+
:rtype: list of dictionaries
462463
:returns: A sequence of mappings describing each CORS policy.
463464
"""
464-
return [policy.copy() for policy in self.properties.get('cors', ())]
465+
return [copy.deepcopy(policy)
466+
for policy in self.properties.get('cors', ())]
465467

466-
def update_cors(self, entries):
467-
"""Update CORS policies configured for this bucket.
468+
@cors.setter
469+
def cors(self, entries):
470+
"""Set CORS policies configured for this bucket.
468471
469472
See: http://www.w3.org/TR/cors/ and
470473
https://cloud.google.com/storage/docs/json_api/v1/buckets
471474
472-
:type entries: list(dict)
475+
:type entries: list of dictionaries
473476
:param entries: A sequence of mappings describing each CORS policy.
474477
"""
475478
self._patch_properties({'cors': entries})
476-
self.patch()
477479

478480
def get_default_object_acl(self):
479481
"""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
@@ -568,80 +568,6 @@ def upload_from_file(self, fh):
568568
self.assertEqual(found._name, BLOB_NAME)
569569
self.assertTrue(found._bucket is bucket)
570570

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

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

0 commit comments

Comments
 (0)