|
| 1 | +# Copyright 2016 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 unittest2 |
| 16 | + |
| 17 | + |
| 18 | +class TestPolicy(unittest2.TestCase): |
| 19 | + |
| 20 | + def _getTargetClass(self): |
| 21 | + from gcloud.pubsub.iam import Policy |
| 22 | + return Policy |
| 23 | + |
| 24 | + def _makeOne(self, *args, **kw): |
| 25 | + return self._getTargetClass()(*args, **kw) |
| 26 | + |
| 27 | + def test_ctor_defaults(self): |
| 28 | + policy = self._makeOne() |
| 29 | + self.assertEqual(policy.etag, None) |
| 30 | + self.assertEqual(policy.version, None) |
| 31 | + self.assertEqual(list(policy.owners), []) |
| 32 | + self.assertEqual(list(policy.writers), []) |
| 33 | + self.assertEqual(list(policy.readers), []) |
| 34 | + |
| 35 | + def test_ctor_explicit(self): |
| 36 | + VERSION = 17 |
| 37 | + ETAG = 'ETAG' |
| 38 | + policy = self._makeOne(ETAG, VERSION) |
| 39 | + self.assertEqual(policy.etag, ETAG) |
| 40 | + self.assertEqual(policy.version, VERSION) |
| 41 | + self.assertEqual(list(policy.owners), []) |
| 42 | + self.assertEqual(list(policy.writers), []) |
| 43 | + self.assertEqual(list(policy.readers), []) |
| 44 | + |
| 45 | + def test_user(self): |
| 46 | + |
| 47 | + MEMBER = 'user:%s' % (EMAIL,) |
| 48 | + policy = self._makeOne() |
| 49 | + self.assertEqual(policy.user(EMAIL), MEMBER) |
| 50 | + |
| 51 | + def test_service_account(self): |
| 52 | + |
| 53 | + MEMBER = 'serviceAccount:%s' % (EMAIL,) |
| 54 | + policy = self._makeOne() |
| 55 | + self.assertEqual(policy.service_account(EMAIL), MEMBER) |
| 56 | + |
| 57 | + def test_group(self): |
| 58 | + |
| 59 | + MEMBER = 'group:%s' % (EMAIL,) |
| 60 | + policy = self._makeOne() |
| 61 | + self.assertEqual(policy.group(EMAIL), MEMBER) |
| 62 | + |
| 63 | + def test_domain(self): |
| 64 | + DOMAIN = 'example.com' |
| 65 | + MEMBER = 'domain:%s' % (DOMAIN,) |
| 66 | + policy = self._makeOne() |
| 67 | + self.assertEqual(policy.domain(DOMAIN), MEMBER) |
| 68 | + |
| 69 | + def test_all_users(self): |
| 70 | + policy = self._makeOne() |
| 71 | + self.assertEqual(policy.all_users(), 'allUsers') |
| 72 | + |
| 73 | + def test_authenticated_users(self): |
| 74 | + policy = self._makeOne() |
| 75 | + self.assertEqual(policy.authenticated_users(), 'allAuthenticatedUsers') |
| 76 | + |
| 77 | + def test_from_api_repr_only_etag(self): |
| 78 | + RESOURCE = { |
| 79 | + 'etag': 'ACAB', |
| 80 | + } |
| 81 | + klass = self._getTargetClass() |
| 82 | + policy = klass.from_api_repr(RESOURCE) |
| 83 | + self.assertEqual(policy.etag, 'ACAB') |
| 84 | + self.assertEqual(policy.version, None) |
| 85 | + self.assertEqual(list(policy.owners), []) |
| 86 | + self.assertEqual(list(policy.writers), []) |
| 87 | + self.assertEqual(list(policy.readers), []) |
| 88 | + |
| 89 | + def test_from_api_repr_complete(self): |
| 90 | + from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE |
| 91 | + |
| 92 | + OWNER2 = 'group:[email protected]' |
| 93 | + WRITER1 = 'domain:google.com' |
| 94 | + WRITER2 = 'user:[email protected]' |
| 95 | + READER1 = 'serviceAccount:[email protected]' |
| 96 | + READER2 = 'user:[email protected]' |
| 97 | + RESOURCE = { |
| 98 | + 'etag': 'DEADBEEF', |
| 99 | + 'version': 17, |
| 100 | + 'bindings': [ |
| 101 | + {'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, |
| 102 | + {'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, |
| 103 | + {'role': _READER_ROLE, 'members': [READER1, READER2]}, |
| 104 | + ], |
| 105 | + } |
| 106 | + klass = self._getTargetClass() |
| 107 | + policy = klass.from_api_repr(RESOURCE) |
| 108 | + self.assertEqual(policy.etag, 'DEADBEEF') |
| 109 | + self.assertEqual(policy.version, 17) |
| 110 | + self.assertEqual(sorted(policy.owners), [OWNER2, OWNER1]) |
| 111 | + self.assertEqual(sorted(policy.writers), [WRITER1, WRITER2]) |
| 112 | + self.assertEqual(sorted(policy.readers), [READER1, READER2]) |
| 113 | + |
| 114 | + def test_from_api_repr_bad_role(self): |
| 115 | + |
| 116 | + BOGUS2 = 'group:[email protected]' |
| 117 | + RESOURCE = { |
| 118 | + 'etag': 'DEADBEEF', |
| 119 | + 'version': 17, |
| 120 | + 'bindings': [ |
| 121 | + {'role': 'nonesuch', 'members': [BOGUS1, BOGUS2]}, |
| 122 | + ], |
| 123 | + } |
| 124 | + klass = self._getTargetClass() |
| 125 | + with self.assertRaises(ValueError): |
| 126 | + klass.from_api_repr(RESOURCE) |
| 127 | + |
| 128 | + def test_to_api_repr_defaults(self): |
| 129 | + policy = self._makeOne() |
| 130 | + self.assertEqual(policy.to_api_repr(), {}) |
| 131 | + |
| 132 | + def test_to_api_repr_only_etag(self): |
| 133 | + policy = self._makeOne('DEADBEEF') |
| 134 | + self.assertEqual(policy.to_api_repr(), {'etag': 'DEADBEEF'}) |
| 135 | + |
| 136 | + def test_to_api_repr_full(self): |
| 137 | + from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE |
| 138 | + OWNER1 = 'group:[email protected]' |
| 139 | + |
| 140 | + WRITER1 = 'domain:google.com' |
| 141 | + WRITER2 = 'user:[email protected]' |
| 142 | + READER1 = 'serviceAccount:[email protected]' |
| 143 | + READER2 = 'user:[email protected]' |
| 144 | + EXPECTED = { |
| 145 | + 'etag': 'DEADBEEF', |
| 146 | + 'version': 17, |
| 147 | + 'bindings': [ |
| 148 | + {'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, |
| 149 | + {'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, |
| 150 | + {'role': _READER_ROLE, 'members': [READER1, READER2]}, |
| 151 | + ], |
| 152 | + } |
| 153 | + policy = self._makeOne('DEADBEEF', 17) |
| 154 | + policy.owners.add(OWNER1) |
| 155 | + policy.owners.add(OWNER2) |
| 156 | + policy.writers.add(WRITER1) |
| 157 | + policy.writers.add(WRITER2) |
| 158 | + policy.readers.add(READER1) |
| 159 | + policy.readers.add(READER2) |
| 160 | + self.assertEqual(policy.to_api_repr(), EXPECTED) |
0 commit comments