| OLD | NEW |
| 1 # -*- coding: utf-8 -*- |
| 1 # Copyright 2013 Google Inc. All Rights Reserved. | 2 # Copyright 2013 Google Inc. All Rights Reserved. |
| 2 # | 3 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 4 # 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 not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at | 6 # You may obtain a copy of the License at |
| 6 # | 7 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 9 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 10 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 11 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and | 13 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. | 14 # limitations under the License. |
| 15 """Integration tests for the defacl command.""" |
| 14 | 16 |
| 17 from __future__ import absolute_import |
| 18 |
| 19 import re |
| 15 import gslib.tests.testcase as case | 20 import gslib.tests.testcase as case |
| 21 from gslib.tests.testcase.integration_testcase import SkipForS3 |
| 16 from gslib.tests.util import ObjectToURI as suri | 22 from gslib.tests.util import ObjectToURI as suri |
| 17 | 23 |
| 24 PUBLIC_READ_JSON_ACL_TEXT = '"entity":"allUsers","role":"READER"' |
| 18 | 25 |
| 26 |
| 27 @SkipForS3('S3 does not support default object ACLs.') |
| 19 class TestDefacl(case.GsUtilIntegrationTestCase): | 28 class TestDefacl(case.GsUtilIntegrationTestCase): |
| 20 """Tests gslib.commands.defacl.""" | 29 """Integration tests for the defacl command.""" |
| 21 | 30 |
| 22 _defacl_ch_prefix = ['defacl', 'ch'] | 31 _defacl_ch_prefix = ['defacl', 'ch'] |
| 23 _defacl_get_prefix = ['defacl', 'get'] | 32 _defacl_get_prefix = ['defacl', 'get'] |
| 24 _defacl_set_prefix = ['defacl', 'set'] | 33 _defacl_set_prefix = ['defacl', 'set'] |
| 25 | 34 |
| 26 def _MakeScopeRegex(self, scope_type, email_address, perm): | 35 def _MakeScopeRegex(self, role, entity_type, email_address): |
| 27 template_regex = ( | 36 template_regex = (r'\{.*"entity":\s*"%s-%s".*"role":\s*"%s".*\}' % |
| 28 r'<Scope type="{0}">\s*<EmailAddress>\s*{1}\s*</EmailAddress>\s*' | 37 (entity_type, email_address, role)) |
| 29 r'</Scope>\s*<Permission>\s*{2}\s*</Permission>') | 38 return re.compile(template_regex, flags=re.DOTALL) |
| 30 return template_regex.format(scope_type, email_address, perm) | |
| 31 | 39 |
| 32 def testChangeDefaultAcl(self): | 40 def testChangeDefaultAcl(self): |
| 41 """Tests defacl ch.""" |
| 33 bucket = self.CreateBucket() | 42 bucket = self.CreateBucket() |
| 34 | 43 |
| 35 test_regex = self._MakeScopeRegex( | 44 test_regex = self._MakeScopeRegex( |
| 36 'GroupByEmail', self.GROUP_TEST_ADDRESS, 'READ') | 45 'OWNER', 'group', self.GROUP_TEST_ADDRESS) |
| 37 xml = self.RunGsUtil(self._defacl_get_prefix + | 46 test_regex2 = self._MakeScopeRegex( |
| 38 [suri(bucket)], return_stdout=True) | 47 'READER', 'group', self.GROUP_TEST_ADDRESS) |
| 39 self.assertNotRegexpMatches(xml, test_regex) | 48 json_text = self.RunGsUtil(self._defacl_get_prefix + |
| 49 [suri(bucket)], return_stdout=True) |
| 50 self.assertNotRegexpMatches(json_text, test_regex) |
| 40 | 51 |
| 41 self.RunGsUtil(self._defacl_ch_prefix + | 52 self.RunGsUtil(self._defacl_ch_prefix + |
| 42 ['-g', self.GROUP_TEST_ADDRESS+':READ', suri(bucket)]) | 53 ['-g', self.GROUP_TEST_ADDRESS+':FC', suri(bucket)]) |
| 43 xml = self.RunGsUtil(self._defacl_get_prefix + | 54 json_text2 = self.RunGsUtil(self._defacl_get_prefix + |
| 44 [suri(bucket)], return_stdout=True) | 55 [suri(bucket)], return_stdout=True) |
| 45 self.assertRegexpMatches(xml, test_regex) | 56 self.assertRegexpMatches(json_text2, test_regex) |
| 57 |
| 58 self.RunGsUtil(self._defacl_ch_prefix + |
| 59 ['-g', self.GROUP_TEST_ADDRESS+':READ', suri(bucket)]) |
| 60 json_text3 = self.RunGsUtil(self._defacl_get_prefix + |
| 61 [suri(bucket)], return_stdout=True) |
| 62 self.assertRegexpMatches(json_text3, test_regex2) |
| 63 |
| 64 stderr = self.RunGsUtil(self._defacl_ch_prefix + |
| 65 ['-g', self.GROUP_TEST_ADDRESS+':WRITE', |
| 66 suri(bucket)], |
| 67 return_stderr=True, expected_status=1) |
| 68 self.assertIn('WRITER cannot be set as a default object ACL', stderr) |
| 46 | 69 |
| 47 def testChangeMultipleBuckets(self): | 70 def testChangeMultipleBuckets(self): |
| 71 """Tests defacl ch on multiple buckets.""" |
| 48 bucket1 = self.CreateBucket() | 72 bucket1 = self.CreateBucket() |
| 49 bucket2 = self.CreateBucket() | 73 bucket2 = self.CreateBucket() |
| 50 | 74 |
| 51 test_regex = self._MakeScopeRegex( | 75 test_regex = self._MakeScopeRegex( |
| 52 'GroupByEmail', self.GROUP_TEST_ADDRESS, 'READ') | 76 'READER', 'group', self.GROUP_TEST_ADDRESS) |
| 53 xml = self.RunGsUtil(self._defacl_get_prefix + [suri(bucket1)], | 77 json_text = self.RunGsUtil(self._defacl_get_prefix + [suri(bucket1)], |
| 54 return_stdout=True) | 78 return_stdout=True) |
| 55 self.assertNotRegexpMatches(xml, test_regex) | 79 self.assertNotRegexpMatches(json_text, test_regex) |
| 56 xml = self.RunGsUtil(self._defacl_get_prefix + [suri(bucket2)], | 80 json_text = self.RunGsUtil(self._defacl_get_prefix + [suri(bucket2)], |
| 57 return_stdout=True) | 81 return_stdout=True) |
| 58 self.assertNotRegexpMatches(xml, test_regex) | 82 self.assertNotRegexpMatches(json_text, test_regex) |
| 59 | 83 |
| 60 self.RunGsUtil(self._defacl_ch_prefix + | 84 self.RunGsUtil(self._defacl_ch_prefix + |
| 61 ['-g', self.GROUP_TEST_ADDRESS+':READ', | 85 ['-g', self.GROUP_TEST_ADDRESS+':READ', |
| 62 suri(bucket1), suri(bucket2)]) | 86 suri(bucket1), suri(bucket2)]) |
| 63 xml = self.RunGsUtil(self._defacl_get_prefix + [suri(bucket1)], | 87 json_text = self.RunGsUtil(self._defacl_get_prefix + [suri(bucket1)], |
| 64 return_stdout=True) | 88 return_stdout=True) |
| 65 self.assertRegexpMatches(xml, test_regex) | 89 self.assertRegexpMatches(json_text, test_regex) |
| 66 xml = self.RunGsUtil(self._defacl_get_prefix + [suri(bucket2)], | 90 json_text = self.RunGsUtil(self._defacl_get_prefix + [suri(bucket2)], |
| 67 return_stdout=True) | 91 return_stdout=True) |
| 68 self.assertRegexpMatches(xml, test_regex) | 92 self.assertRegexpMatches(json_text, test_regex) |
| 69 | 93 |
| 70 def testChangeMultipleAcls(self): | 94 def testChangeMultipleAcls(self): |
| 95 """Tests defacl ch with multiple ACL entries.""" |
| 71 bucket = self.CreateBucket() | 96 bucket = self.CreateBucket() |
| 72 | 97 |
| 73 test_regex_group = self._MakeScopeRegex( | 98 test_regex_group = self._MakeScopeRegex( |
| 74 'GroupByEmail', self.GROUP_TEST_ADDRESS, 'READ') | 99 'READER', 'group', self.GROUP_TEST_ADDRESS) |
| 75 test_regex_user = self._MakeScopeRegex( | 100 test_regex_user = self._MakeScopeRegex( |
| 76 'UserByEmail', self.USER_TEST_ADDRESS, 'FULL_CONTROL') | 101 'OWNER', 'user', self.USER_TEST_ADDRESS) |
| 77 xml = self.RunGsUtil(self._defacl_get_prefix + [suri(bucket)], | 102 json_text = self.RunGsUtil(self._defacl_get_prefix + [suri(bucket)], |
| 78 return_stdout=True) | 103 return_stdout=True) |
| 79 self.assertNotRegexpMatches(xml, test_regex_group) | 104 self.assertNotRegexpMatches(json_text, test_regex_group) |
| 80 self.assertNotRegexpMatches(xml, test_regex_user) | 105 self.assertNotRegexpMatches(json_text, test_regex_user) |
| 81 | 106 |
| 82 self.RunGsUtil(self._defacl_ch_prefix + | 107 self.RunGsUtil(self._defacl_ch_prefix + |
| 83 ['-g', self.GROUP_TEST_ADDRESS+':READ', | 108 ['-g', self.GROUP_TEST_ADDRESS+':READ', |
| 84 '-u', self.USER_TEST_ADDRESS+':fc', suri(bucket)]) | 109 '-u', self.USER_TEST_ADDRESS+':fc', suri(bucket)]) |
| 85 xml = self.RunGsUtil(self._defacl_get_prefix + [suri(bucket)], | 110 json_text = self.RunGsUtil(self._defacl_get_prefix + [suri(bucket)], |
| 86 return_stdout=True) | 111 return_stdout=True) |
| 87 self.assertRegexpMatches(xml, test_regex_group) | 112 self.assertRegexpMatches(json_text, test_regex_group) |
| 88 self.assertRegexpMatches(xml, test_regex_user) | 113 self.assertRegexpMatches(json_text, test_regex_user) |
| 89 | 114 |
| 90 def testEmptyDefAcl(self): | 115 def testEmptyDefAcl(self): |
| 91 bucket = self.CreateBucket() | 116 bucket = self.CreateBucket() |
| 92 self.RunGsUtil(self._defacl_set_prefix + ['private', suri(bucket)]) | 117 self.RunGsUtil(self._defacl_set_prefix + ['private', suri(bucket)]) |
| 118 stdout = self.RunGsUtil(self._defacl_get_prefix + [suri(bucket)], |
| 119 return_stdout=True) |
| 120 self.assertEquals(stdout.rstrip(), '[]') |
| 93 self.RunGsUtil(self._defacl_ch_prefix + | 121 self.RunGsUtil(self._defacl_ch_prefix + |
| 94 ['-u', self.USER_TEST_ADDRESS+':fc', suri(bucket)]) | 122 ['-u', self.USER_TEST_ADDRESS+':fc', suri(bucket)]) |
| 95 | 123 |
| 96 def testDeletePermissionsWithCh(self): | 124 def testDeletePermissionsWithCh(self): |
| 125 """Tests removing permissions with defacl ch.""" |
| 97 bucket = self.CreateBucket() | 126 bucket = self.CreateBucket() |
| 98 | 127 |
| 99 test_regex = self._MakeScopeRegex( | 128 test_regex = self._MakeScopeRegex( |
| 100 'UserByEmail', self.USER_TEST_ADDRESS, 'FULL_CONTROL') | 129 'OWNER', 'user', self.USER_TEST_ADDRESS) |
| 101 xml = self.RunGsUtil( | 130 json_text = self.RunGsUtil( |
| 102 self._defacl_get_prefix + [suri(bucket)], return_stdout=True) | 131 self._defacl_get_prefix + [suri(bucket)], return_stdout=True) |
| 103 self.assertNotRegexpMatches(xml, test_regex) | 132 self.assertNotRegexpMatches(json_text, test_regex) |
| 104 | 133 |
| 105 self.RunGsUtil(self._defacl_ch_prefix + | 134 self.RunGsUtil(self._defacl_ch_prefix + |
| 106 ['-u', self.USER_TEST_ADDRESS+':fc', suri(bucket)]) | 135 ['-u', self.USER_TEST_ADDRESS+':fc', suri(bucket)]) |
| 107 xml = self.RunGsUtil( | 136 json_text = self.RunGsUtil( |
| 108 self._defacl_get_prefix + [suri(bucket)], return_stdout=True) | 137 self._defacl_get_prefix + [suri(bucket)], return_stdout=True) |
| 109 self.assertRegexpMatches(xml, test_regex) | 138 self.assertRegexpMatches(json_text, test_regex) |
| 110 | 139 |
| 111 self.RunGsUtil(self._defacl_ch_prefix + | 140 self.RunGsUtil(self._defacl_ch_prefix + |
| 112 ['-d', self.USER_TEST_ADDRESS, suri(bucket)]) | 141 ['-d', self.USER_TEST_ADDRESS, suri(bucket)]) |
| 113 xml = self.RunGsUtil( | 142 json_text = self.RunGsUtil( |
| 114 self._defacl_get_prefix + [suri(bucket)], return_stdout=True) | 143 self._defacl_get_prefix + [suri(bucket)], return_stdout=True) |
| 115 self.assertNotRegexpMatches(xml, test_regex) | 144 self.assertNotRegexpMatches(json_text, test_regex) |
| 116 | 145 |
| 117 def testTooFewArgumentsFails(self): | 146 def testTooFewArgumentsFails(self): |
| 147 """Tests calling defacl with insufficient number of arguments.""" |
| 118 # No arguments for get, but valid subcommand. | 148 # No arguments for get, but valid subcommand. |
| 119 stderr = self.RunGsUtil(self._defacl_get_prefix, return_stderr=True, | 149 stderr = self.RunGsUtil(self._defacl_get_prefix, return_stderr=True, |
| 120 expected_status=1) | 150 expected_status=1) |
| 121 self.assertIn('command requires at least', stderr) | 151 self.assertIn('command requires at least', stderr) |
| 122 | 152 |
| 123 # No arguments for set, but valid subcommand. | 153 # No arguments for set, but valid subcommand. |
| 124 stderr = self.RunGsUtil(self._defacl_set_prefix, return_stderr=True, | 154 stderr = self.RunGsUtil(self._defacl_set_prefix, return_stderr=True, |
| 125 expected_status=1) | 155 expected_status=1) |
| 126 self.assertIn('command requires at least', stderr) | 156 self.assertIn('command requires at least', stderr) |
| 127 | 157 |
| 128 # No arguments for ch, but valid subcommand. | 158 # No arguments for ch, but valid subcommand. |
| 129 stderr = self.RunGsUtil(self._defacl_ch_prefix, return_stderr=True, | 159 stderr = self.RunGsUtil(self._defacl_ch_prefix, return_stderr=True, |
| 130 expected_status=1) | 160 expected_status=1) |
| 131 self.assertIn('command requires at least', stderr) | 161 self.assertIn('command requires at least', stderr) |
| 132 | 162 |
| 133 # Neither arguments nor subcommand. | 163 # Neither arguments nor subcommand. |
| 134 stderr = self.RunGsUtil(['defacl'], return_stderr=True, expected_status=1) | 164 stderr = self.RunGsUtil(['defacl'], return_stderr=True, expected_status=1) |
| 135 self.assertIn('command requires at least', stderr) | 165 self.assertIn('command requires at least', stderr) |
| 136 | 166 |
| 167 |
| 137 class TestDefaclOldAlias(TestDefacl): | 168 class TestDefaclOldAlias(TestDefacl): |
| 138 _defacl_ch_prefix= ['chdefacl'] | 169 _defacl_ch_prefix = ['chdefacl'] |
| 139 _defacl_get_prefix = ['getdefacl'] | 170 _defacl_get_prefix = ['getdefacl'] |
| 140 _defacl_set_prefix = ['setdefacl'] | 171 _defacl_set_prefix = ['setdefacl'] |
| OLD | NEW |