OLD | NEW |
(Empty) | |
| 1 # Copyright 2013 Google Inc. All Rights Reserved. |
| 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at |
| 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. |
| 14 |
| 15 import posixpath |
| 16 from xml.dom.minidom import parseString |
| 17 |
| 18 import gslib.tests.testcase as testcase |
| 19 from gslib.tests.util import ObjectToURI as suri |
| 20 |
| 21 |
| 22 class TestSetCors(testcase.GsUtilIntegrationTestCase): |
| 23 """Integration tests for setcors command.""" |
| 24 |
| 25 empty_doc1 = parseString('<CorsConfig/>').toprettyxml(indent=' ') |
| 26 |
| 27 empty_doc2 = parseString( |
| 28 '<CorsConfig></CorsConfig>').toprettyxml(indent=' ') |
| 29 |
| 30 empty_doc3 = parseString( |
| 31 '<CorsConfig><Cors/></CorsConfig>').toprettyxml(indent=' ') |
| 32 |
| 33 empty_doc4 = parseString( |
| 34 '<CorsConfig><Cors></Cors></CorsConfig>').toprettyxml(indent=' ') |
| 35 |
| 36 cors_bad1 = ('<?xml version="1.0" ?><CorsConfig><Cors><Methods><Method>GET' |
| 37 '</ResponseHeader></Methods></Cors></CorsConfig>') |
| 38 |
| 39 cors_bad2 = ('<?xml version="1.0" ?><CorsConfig><Cors><Methods><Cors>GET' |
| 40 '</Cors></Methods></Cors></CorsConfig>') |
| 41 |
| 42 cors_bad3 = ('<?xml version="1.0" ?><CorsConfig><Methods><Method>GET' |
| 43 '</Method></Methods></Cors></CorsConfig>') |
| 44 |
| 45 cors_bad4 = ('<?xml version="1.0" ?><CorsConfig><Cors><Method>GET' |
| 46 '</Method></Cors></CorsConfig>') |
| 47 |
| 48 cors_doc = parseString( |
| 49 '<CorsConfig><Cors><Origins>' |
| 50 '<Origin>http://origin1.example.com</Origin>' |
| 51 '<Origin>http://origin2.example.com</Origin>' |
| 52 '</Origins><Methods><Method>GET</Method>' |
| 53 '<Method>PUT</Method><Method>POST</Method></Methods>' |
| 54 '<ResponseHeaders><ResponseHeader>foo</ResponseHeader>' |
| 55 '<ResponseHeader>bar</ResponseHeader></ResponseHeaders>' |
| 56 '<MaxAgeSec>3600</MaxAgeSec></Cors>' |
| 57 '<Cors><Origins><Origin>http://origin3.example.com</Origin></Origins>' |
| 58 '<Methods><Method>GET</Method><Method>DELETE</Method></Methods>' |
| 59 '<ResponseHeaders><ResponseHeader>foo2</ResponseHeader>' |
| 60 '<ResponseHeader>bar2</ResponseHeader></ResponseHeaders>' |
| 61 '</Cors></CorsConfig>').toprettyxml(indent=' ') |
| 62 |
| 63 def test_default_cors(self): |
| 64 bucket_uri = self.CreateBucket() |
| 65 stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) |
| 66 self.assertEqual(stdout, self.empty_doc1) |
| 67 |
| 68 def test_set_empty_cors1(self): |
| 69 bucket_uri = self.CreateBucket() |
| 70 fpath = self.CreateTempFile(contents=self.empty_doc1) |
| 71 self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) |
| 72 stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) |
| 73 self.assertEqual(stdout, self.empty_doc1) |
| 74 |
| 75 def test_set_empty_cors2(self): |
| 76 bucket_uri = self.CreateBucket() |
| 77 fpath = self.CreateTempFile(contents=self.empty_doc2) |
| 78 self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) |
| 79 stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) |
| 80 self.assertEqual(stdout, self.empty_doc1) |
| 81 |
| 82 def test_set_empty_cors3(self): |
| 83 bucket_uri = self.CreateBucket() |
| 84 fpath = self.CreateTempFile(contents=self.empty_doc3) |
| 85 self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) |
| 86 stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) |
| 87 self.assertEqual(stdout, self.empty_doc3) |
| 88 |
| 89 def test_set_empty_cors4(self): |
| 90 bucket_uri = self.CreateBucket() |
| 91 fpath = self.CreateTempFile(contents=self.empty_doc4) |
| 92 self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) |
| 93 stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) |
| 94 self.assertEqual(stdout, self.empty_doc3) |
| 95 |
| 96 def test_non_null_cors(self): |
| 97 bucket_uri = self.CreateBucket() |
| 98 fpath = self.CreateTempFile(contents=self.cors_doc) |
| 99 self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) |
| 100 stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) |
| 101 self.assertEqual(stdout, self.cors_doc) |
| 102 |
| 103 def test_bad_cors1(self): |
| 104 bucket_uri = self.CreateBucket() |
| 105 fpath = self.CreateTempFile(contents=self.cors_bad1) |
| 106 self.RunGsUtil(['setcors', fpath, suri(bucket_uri)], expected_status=1) |
| 107 |
| 108 def test_bad_cors2(self): |
| 109 bucket_uri = self.CreateBucket() |
| 110 fpath = self.CreateTempFile(contents=self.cors_bad2) |
| 111 self.RunGsUtil(['setcors', fpath, suri(bucket_uri)], expected_status=1) |
| 112 |
| 113 def test_bad_cors3(self): |
| 114 bucket_uri = self.CreateBucket() |
| 115 fpath = self.CreateTempFile(contents=self.cors_bad3) |
| 116 self.RunGsUtil(['setcors', fpath, suri(bucket_uri)], expected_status=1) |
| 117 |
| 118 def test_bad_cors4(self): |
| 119 bucket_uri = self.CreateBucket() |
| 120 fpath = self.CreateTempFile(contents=self.cors_bad4) |
| 121 self.RunGsUtil(['setcors', fpath, suri(bucket_uri)], expected_status=1) |
| 122 |
| 123 def set_cors_and_reset(self): |
| 124 bucket_uri = self.CreateBucket() |
| 125 tmpdir = self.CreateTempDir() |
| 126 fpath = self.CreateTempFile(tmpdir=tmpdir, contents=self.cors_doc) |
| 127 self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) |
| 128 stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) |
| 129 self.assertEqual(stdout, self.cors_doc) |
| 130 |
| 131 fpath = self.CreateTempFile(tmpdir=tmpdir, contents=self.empty_doc1) |
| 132 self.RunGsUtil(['setcors', fpath, suri(bucket_uri)]) |
| 133 stdout = self.RunGsUtil(['getcors', suri(bucket_uri)], return_stdout=True) |
| 134 self.assertEqual(stdout, self.empty_doc1) |
| 135 |
| 136 def set_multi_non_null_cors(self): |
| 137 bucket1_uri = self.CreateBucket() |
| 138 bucket2_uri = self.CreateBucket() |
| 139 fpath = self.CreateTempFile(contents=self.cors_doc) |
| 140 self.RunGsUtil(['setcors', fpath, suri(bucket1_uri), suri(bucket2_uri)]) |
| 141 stdout = self.RunGsUtil(['getcors', suri(bucket1_uri)], return_stdout=True) |
| 142 self.assertEqual(stdout, self.cors_doc) |
| 143 stdout = self.RunGsUtil(['getcors', suri(bucket2_uri)], return_stdout=True) |
| 144 self.assertEqual(stdout, self.cors_doc) |
| 145 |
| 146 def test_set_wildcard_non_null_cors(self): |
| 147 bucket1_uri = self.CreateBucket() |
| 148 bucket2_uri = self.CreateBucket() |
| 149 # This just double checks that the common prefix of the two buckets is what |
| 150 # we think it should be (based on implementation detail of CreateBucket). |
| 151 # We want to be careful when setting a wildcard on buckets to make sure we |
| 152 # don't step outside the test buckets to effect other buckets. |
| 153 common_prefix = posixpath.commonprefix([suri(bucket1_uri), |
| 154 suri(bucket2_uri)]) |
| 155 self.assertEqual(common_prefix, |
| 156 'gs://gsutil-test-test_set_wildcard_non_null_cors-bucket-') |
| 157 wildcard = '%s*' % common_prefix |
| 158 |
| 159 fpath = self.CreateTempFile(contents=self.cors_doc) |
| 160 stdout = self.RunGsUtil(['setcors', fpath, wildcard], return_stdout=True) |
| 161 self.assertIn('Setting CORS on %s/...' % suri(bucket1_uri), stdout) |
| 162 self.assertIn('Setting CORS on %s/...' % suri(bucket2_uri), stdout) |
| 163 self.assertEqual(stdout.count('Setting CORS'), 2) |
| 164 |
| 165 stdout = self.RunGsUtil(['getcors', suri(bucket1_uri)], return_stdout=True) |
| 166 self.assertEqual(stdout, self.cors_doc) |
| 167 stdout = self.RunGsUtil(['getcors', suri(bucket2_uri)], return_stdout=True) |
| 168 self.assertEqual(stdout, self.cors_doc) |
OLD | NEW |