Skip to content

Commit 5d83599

Browse files
committed
Making cors a property instead of having two separate methods.
1 parent 4d69bb3 commit 5d83599

2 files changed

Lines changed: 51 additions & 81 deletions

File tree

gcloud/storage/bucket.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
... print blob
3434
"""
3535

36+
import copy
3637
import os
3738
import six
3839

@@ -456,28 +457,30 @@ def upload_file_object(self, file_obj, blob_name=None):
456457
blob.upload_from_file(file_obj)
457458
return blob
458459

459-
def get_cors(self):
460+
@property
461+
def cors(self):
460462
"""Retrieve CORS policies configured for this bucket.
461463
462464
See: http://www.w3.org/TR/cors/ and
463465
https://cloud.google.com/storage/docs/json_api/v1/buckets
464466
465-
:rtype: list(dict)
467+
:rtype: list
466468
:returns: A sequence of mappings describing each CORS policy.
467469
"""
468-
return [policy.copy() for policy in self.properties.get('cors', ())]
470+
return [copy.deepcopy(policy)
471+
for policy in self.properties.get('cors', ())]
469472

470-
def update_cors(self, entries):
471-
"""Update CORS policies configured for this bucket.
473+
@cors.setter
474+
def cors(self, entries):
475+
"""Set CORS policies configured for this bucket.
472476
473477
See: http://www.w3.org/TR/cors/ and
474478
https://cloud.google.com/storage/docs/json_api/v1/buckets
475479
476-
:type entries: list(dict)
480+
:type entries: list of dictionaries
477481
:param entries: A sequence of mappings describing each CORS policy.
478482
"""
479483
self._patch_properties({'cors': entries})
480-
self.patch()
481484

482485
def get_default_object_acl(self):
483486
"""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'
@@ -745,6 +671,47 @@ def test_location_setter(self):
745671
self.assertEqual(kw[0]['data'], {'location': 'AS'})
746672
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
747673

674+
def test_cors_getter(self):
675+
NAME = 'name'
676+
CORS_ENTRY = {
677+
'maxAgeSeconds': 1234,
678+
'method': ['OPTIONS', 'GET'],
679+
'origin': ['127.0.0.1'],
680+
'responseHeader': ['Content-Type'],
681+
}
682+
properties = {'cors': [CORS_ENTRY, {}]}
683+
bucket = self._makeOne(NAME, properties=properties)
684+
entries = bucket.cors
685+
self.assertEqual(len(entries), 2)
686+
self.assertEqual(entries[0], CORS_ENTRY)
687+
self.assertEqual(entries[1], {})
688+
# Make sure it was a copy, not the same object.
689+
self.assertFalse(entries[0] is CORS_ENTRY)
690+
691+
def test_cors_setter(self):
692+
NAME = 'name'
693+
CORS_ENTRY = {
694+
'maxAgeSeconds': 1234,
695+
'method': ['OPTIONS', 'GET'],
696+
'origin': ['127.0.0.1'],
697+
'responseHeader': ['Content-Type'],
698+
}
699+
DATA = {'cors': [CORS_ENTRY]}
700+
connection = _Connection(DATA)
701+
bucket = self._makeOne(NAME, connection)
702+
703+
self.assertEqual(bucket.cors, [])
704+
705+
bucket.cors = [CORS_ENTRY]
706+
bucket.patch()
707+
self.assertEqual(bucket.cors, [CORS_ENTRY])
708+
kw = connection._requested
709+
self.assertEqual(len(kw), 1)
710+
self.assertEqual(kw[0]['method'], 'PATCH')
711+
self.assertEqual(kw[0]['path'], '/b/%s' % NAME)
712+
self.assertEqual(kw[0]['data'], DATA)
713+
self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
714+
748715
def test_get_logging_w_prefix(self):
749716
NAME = 'name'
750717
LOG_BUCKET = 'logs'

0 commit comments

Comments
 (0)