# Copyright 2013 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import posixpath from xml.dom.minidom import parseString import gslib.tests.testcase as testcase from gslib.tests.util import ObjectToURI as suri class TestSetCors(testcase.GsUtilIntegrationTestCase): """Integration tests for setcors command.""" empty_doc1 = parseString('').toprettyxml(indent=' ') empty_doc2 = parseString( '').toprettyxml(indent=' ') empty_doc3 = parseString( '').toprettyxml(indent=' ') empty_doc4 = parseString( '').toprettyxml(indent=' ') cors_bad1 = ('GET' '') cors_bad2 = ('GET' '') cors_bad3 = ('GET' '') cors_bad4 = ('GET' '') cors_doc = parseString( '' 'http://origin1.example.com' 'http://origin2.example.com' 'GET' 'PUTPOST' 'foo' 'bar' '3600' 'http://origin3.example.com' 'GETDELETE' 'foo2' 'bar2' '').toprettyxml(indent=' ') def test_default_cors(self): bucket_uri = self.CreateBucket() stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) self.assertEqual(stdout, self.empty_doc1) def test_set_empty_cors1(self): bucket_uri = self.CreateBucket() fpath = self.CreateTempFile(contents=self.empty_doc1) self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) self.assertEqual(stdout, self.empty_doc1) def test_set_empty_cors2(self): bucket_uri = self.CreateBucket() fpath = self.CreateTempFile(contents=self.empty_doc2) self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) self.assertEqual(stdout, self.empty_doc1) def test_set_empty_cors3(self): bucket_uri = self.CreateBucket() fpath = self.CreateTempFile(contents=self.empty_doc3) self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) self.assertEqual(stdout, self.empty_doc3) def test_set_empty_cors4(self): bucket_uri = self.CreateBucket() fpath = self.CreateTempFile(contents=self.empty_doc4) self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) self.assertEqual(stdout, self.empty_doc3) def test_non_null_cors(self): bucket_uri = self.CreateBucket() fpath = self.CreateTempFile(contents=self.cors_doc) self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) self.assertEqual(stdout, self.cors_doc) def test_bad_cors1(self): bucket_uri = self.CreateBucket() fpath = self.CreateTempFile(contents=self.cors_bad1) self.RunGsUtil(['setcors', fpath, suri(bucket_uri)], expected_status=1) def test_bad_cors2(self): bucket_uri = self.CreateBucket() fpath = self.CreateTempFile(contents=self.cors_bad2) self.RunGsUtil(['setcors', fpath, suri(bucket_uri)], expected_status=1) def test_bad_cors3(self): bucket_uri = self.CreateBucket() fpath = self.CreateTempFile(contents=self.cors_bad3) self.RunGsUtil(['setcors', fpath, suri(bucket_uri)], expected_status=1) def test_bad_cors4(self): bucket_uri = self.CreateBucket() fpath = self.CreateTempFile(contents=self.cors_bad4) self.RunGsUtil(['setcors', fpath, suri(bucket_uri)], expected_status=1) def set_cors_and_reset(self): bucket_uri = self.CreateBucket() tmpdir = self.CreateTempDir() fpath = self.CreateTempFile(tmpdir=tmpdir, contents=self.cors_doc) self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) self.assertEqual(stdout, self.cors_doc) fpath = self.CreateTempFile(tmpdir=tmpdir, contents=self.empty_doc1) self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) self.assertEqual(stdout, self.empty_doc1) def set_multi_non_null_cors(self): bucket1_uri = self.CreateBucket() bucket2_uri = self.CreateBucket() fpath = self.CreateTempFile(contents=self.cors_doc) self.RunGsUtil(['setcors', fpath, suri(bucket1_uri), suri(bucket2_uri)]) stdout = self.RunGsUtil(['getcors', suri(bucket1_uri)], return_stdout=True) self.assertEqual(stdout, self.cors_doc) stdout = self.RunGsUtil(['getcors', suri(bucket2_uri)], return_stdout=True) self.assertEqual(stdout, self.cors_doc) def test_set_wildcard_non_null_cors(self): bucket1_uri = self.CreateBucket() bucket2_uri = self.CreateBucket() # This just double checks that the common prefix of the two buckets is what # we think it should be (based on implementation detail of CreateBucket). # We want to be careful when setting a wildcard on buckets to make sure we # don't step outside the test buckets to effect other buckets. common_prefix = posixpath.commonprefix([suri(bucket1_uri), suri(bucket2_uri)]) self.assertEqual(common_prefix, 'gs://gsutil-test-test_set_wildcard_non_null_cors-bucket-') wildcard = '%s*' % common_prefix fpath = self.CreateTempFile(contents=self.cors_doc) stdout = self.RunGsUtil(['setcors', fpath, wildcard], return_stdout=True) self.assertIn('Setting CORS on %s/...' % suri(bucket1_uri), stdout) self.assertIn('Setting CORS on %s/...' % suri(bucket2_uri), stdout) self.assertEqual(stdout.count('Setting CORS'), 2) stdout = self.RunGsUtil(['getcors', suri(bucket1_uri)], return_stdout=True) self.assertEqual(stdout, self.cors_doc) stdout = self.RunGsUtil(['getcors', suri(bucket2_uri)], return_stdout=True) self.assertEqual(stdout, self.cors_doc)