OLD | NEW |
(Empty) | |
| 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 from __future__ import absolute_import |
| 16 |
| 17 import base64 |
| 18 import datetime |
| 19 import unittest2 |
| 20 from expects import be_none, be_true, expect, equal, raise_error |
| 21 |
| 22 from google.api.control import label_descriptor, messages, report_request |
| 23 |
| 24 _KNOWN = label_descriptor.KnownLabels |
| 25 ValueType = label_descriptor.ValueType |
| 26 |
| 27 class KnownLabelsBase(object): |
| 28 SUBJECT = None |
| 29 GIVEN_INFO = report_request.Info( |
| 30 api_method = 'dummy_method', |
| 31 api_version = 'dummy_version', |
| 32 location = 'dummy_location', |
| 33 referer = 'dummy_referer' |
| 34 ) |
| 35 WANTED_LABEL_DICT = {} |
| 36 |
| 37 def _matching_descriptor(self, hide_default=False): |
| 38 res = messages.LabelDescriptor( |
| 39 key=self.SUBJECT.label_name, |
| 40 valueType=self.SUBJECT.value_type) |
| 41 if res.valueType == ValueType.STRING and hide_default: |
| 42 res.valueType = None |
| 43 return res |
| 44 |
| 45 def _not_matched(self): |
| 46 d = self._matching_descriptor() |
| 47 d.valueType = ValueType.INT64 # no known labels have this type |
| 48 return d |
| 49 |
| 50 def test_should_be_supported(self): |
| 51 expect(_KNOWN.is_supported(self._matching_descriptor())).to(be_true) |
| 52 expect(_KNOWN.is_supported( |
| 53 self._matching_descriptor(hide_default=True))).to(be_true) |
| 54 expect(_KNOWN.is_supported(self._not_matched())).not_to(be_true) |
| 55 |
| 56 def test_should_be_matched_correctly(self): |
| 57 expect(self.SUBJECT.matches(self._matching_descriptor())).to(be_true) |
| 58 expect(self.SUBJECT.matches( |
| 59 self._matching_descriptor(hide_default=True))).to(be_true) |
| 60 expect(self.SUBJECT.matches(self._not_matched())).not_to(be_true) |
| 61 |
| 62 def test_should_update_request_info(self): |
| 63 given_dict = {} |
| 64 self.SUBJECT.do_labels_update(self.GIVEN_INFO, given_dict) |
| 65 expect(given_dict).to(equal(self.WANTED_LABEL_DICT)) |
| 66 |
| 67 |
| 68 class TestCredentialIdWithNoCreds(KnownLabelsBase, unittest2.TestCase): |
| 69 SUBJECT = _KNOWN.CREDENTIAL_ID |
| 70 |
| 71 |
| 72 class TestCredentialIdWithApiKey(KnownLabelsBase, unittest2.TestCase): |
| 73 SUBJECT = _KNOWN.CREDENTIAL_ID |
| 74 GIVEN_INFO = report_request.Info( |
| 75 api_key = 'dummy_api_key', |
| 76 ) |
| 77 WANTED_LABEL_DICT = {SUBJECT.label_name: 'apiKey:dummy_api_key'} |
| 78 |
| 79 |
| 80 class TestCredentialIdWithAuthIssuer(KnownLabelsBase, unittest2.TestCase): |
| 81 SUBJECT = _KNOWN.CREDENTIAL_ID |
| 82 GIVEN_INFO = report_request.Info( |
| 83 auth_issuer = 'dummy_issuer', |
| 84 auth_audience = 'dummy_audience' |
| 85 ) |
| 86 WANTED_VALUE = 'jwtAuth:issuer=' + base64.urlsafe_b64encode('dummy_issuer') |
| 87 WANTED_VALUE += '&audience=' + base64.urlsafe_b64encode('dummy_audience') |
| 88 WANTED_LABEL_DICT = {SUBJECT.label_name: WANTED_VALUE} |
| 89 |
| 90 |
| 91 class EndUser(KnownLabelsBase, unittest2.TestCase): |
| 92 SUBJECT = _KNOWN.END_USER |
| 93 |
| 94 |
| 95 class EndUserCountry(KnownLabelsBase, unittest2.TestCase): |
| 96 SUBJECT = _KNOWN.END_USER_COUNTRY |
| 97 |
| 98 |
| 99 class ErrorType(KnownLabelsBase, unittest2.TestCase): |
| 100 SUBJECT = _KNOWN.ERROR_TYPE |
| 101 WANTED_LABEL_DICT = {SUBJECT.label_name: '2xx'} |
| 102 |
| 103 |
| 104 class Protocol(KnownLabelsBase, unittest2.TestCase): |
| 105 SUBJECT = _KNOWN.PROTOCOL |
| 106 WANTED_LABEL_DICT = { |
| 107 SUBJECT.label_name: report_request.ReportedProtocols.UNKNOWN.name |
| 108 } |
| 109 |
| 110 |
| 111 class Referer(KnownLabelsBase, unittest2.TestCase): |
| 112 SUBJECT = _KNOWN.REFERER |
| 113 WANTED_LABEL_DICT = {SUBJECT.label_name: 'dummy_referer'} |
| 114 |
| 115 |
| 116 class ResponseCode(KnownLabelsBase, unittest2.TestCase): |
| 117 SUBJECT = _KNOWN.RESPONSE_CODE |
| 118 WANTED_LABEL_DICT = {SUBJECT.label_name: '200'} |
| 119 |
| 120 |
| 121 class ResponseCodeClass(KnownLabelsBase, unittest2.TestCase): |
| 122 SUBJECT = _KNOWN.RESPONSE_CODE_CLASS |
| 123 WANTED_LABEL_DICT = {SUBJECT.label_name: '2xx'} |
| 124 |
| 125 |
| 126 class StatusCodeWithOkStatus(KnownLabelsBase, unittest2.TestCase): |
| 127 SUBJECT = _KNOWN.STATUS_CODE |
| 128 WANTED_LABEL_DICT = {SUBJECT.label_name: '0'} |
| 129 |
| 130 |
| 131 class StatusCodeWithKnown4XXStatus(KnownLabelsBase, unittest2.TestCase): |
| 132 SUBJECT = _KNOWN.STATUS_CODE |
| 133 GIVEN_INFO = report_request.Info( |
| 134 response_code = 401, |
| 135 ) |
| 136 WANTED_LABEL_DICT = {SUBJECT.label_name: '16'} |
| 137 |
| 138 |
| 139 class StatusCodeWithUnknown4XXStatus(KnownLabelsBase, unittest2.TestCase): |
| 140 SUBJECT = _KNOWN.STATUS_CODE |
| 141 GIVEN_INFO = report_request.Info( |
| 142 response_code = 477, |
| 143 ) |
| 144 WANTED_LABEL_DICT = {SUBJECT.label_name: '9'} |
| 145 |
| 146 |
| 147 class StatusCodeWithKnown5XXStatus(KnownLabelsBase, unittest2.TestCase): |
| 148 SUBJECT = _KNOWN.STATUS_CODE |
| 149 GIVEN_INFO = report_request.Info( |
| 150 response_code = 501, |
| 151 ) |
| 152 WANTED_LABEL_DICT = {SUBJECT.label_name: '12'} |
| 153 |
| 154 |
| 155 class StatusCodeWithUnknown5XXStatus(KnownLabelsBase, unittest2.TestCase): |
| 156 SUBJECT = _KNOWN.STATUS_CODE |
| 157 GIVEN_INFO = report_request.Info( |
| 158 response_code = 577, |
| 159 ) |
| 160 WANTED_LABEL_DICT = {SUBJECT.label_name: '13'} |
| 161 |
| 162 |
| 163 class StatusCodeWithUnknownStatus(KnownLabelsBase, unittest2.TestCase): |
| 164 SUBJECT = _KNOWN.STATUS_CODE |
| 165 GIVEN_INFO = report_request.Info( |
| 166 response_code = 777, |
| 167 ) |
| 168 WANTED_LABEL_DICT = {SUBJECT.label_name: '2'} |
| 169 |
| 170 |
| 171 class GaeCloneId(KnownLabelsBase, unittest2.TestCase): |
| 172 SUBJECT = _KNOWN.GAE_CLONE_ID |
| 173 |
| 174 |
| 175 class GaeModuleId(KnownLabelsBase, unittest2.TestCase): |
| 176 SUBJECT = _KNOWN.GAE_MODULE_ID |
| 177 |
| 178 |
| 179 class GaeReplicaIndex(KnownLabelsBase, unittest2.TestCase): |
| 180 SUBJECT = _KNOWN.GAE_REPLICA_INDEX |
| 181 |
| 182 |
| 183 class GaeVersionId(KnownLabelsBase, unittest2.TestCase): |
| 184 SUBJECT = _KNOWN.GAE_VERSION_ID |
| 185 |
| 186 |
| 187 class GcpLocation(KnownLabelsBase, unittest2.TestCase): |
| 188 SUBJECT = _KNOWN.GCP_LOCATION |
| 189 WANTED_LABEL_DICT = {SUBJECT.label_name: 'dummy_location'} |
| 190 |
| 191 |
| 192 class GcpProject(KnownLabelsBase, unittest2.TestCase): |
| 193 SUBJECT = _KNOWN.GCP_PROJECT |
| 194 |
| 195 |
| 196 class GcpRegion(KnownLabelsBase, unittest2.TestCase): |
| 197 SUBJECT = _KNOWN.GCP_REGION |
| 198 |
| 199 |
| 200 class GcpResourceId(KnownLabelsBase, unittest2.TestCase): |
| 201 SUBJECT = _KNOWN.GCP_RESOURCE_ID |
| 202 |
| 203 |
| 204 class GcpResourceType(KnownLabelsBase, unittest2.TestCase): |
| 205 SUBJECT = _KNOWN.GCP_RESOURCE_TYPE |
| 206 |
| 207 |
| 208 class GcpService(KnownLabelsBase, unittest2.TestCase): |
| 209 SUBJECT = _KNOWN.GCP_SERVICE |
| 210 |
| 211 |
| 212 class GcpZone(KnownLabelsBase, unittest2.TestCase): |
| 213 SUBJECT = _KNOWN.GCP_ZONE |
| 214 |
| 215 |
| 216 class GcpUid(KnownLabelsBase, unittest2.TestCase): |
| 217 SUBJECT = _KNOWN.GCP_UID |
| 218 |
| 219 |
| 220 class GcpApiMethod(KnownLabelsBase, unittest2.TestCase): |
| 221 SUBJECT = _KNOWN.GCP_API_METHOD |
| 222 WANTED_LABEL_DICT = {SUBJECT.label_name: 'dummy_method'} |
| 223 |
| 224 |
| 225 class GcpApiVersion(KnownLabelsBase, unittest2.TestCase): |
| 226 SUBJECT = _KNOWN.GCP_API_VERSION |
| 227 WANTED_LABEL_DICT = {SUBJECT.label_name: 'dummy_version'} |
| 228 |
| 229 |
| 230 class SccCallerIp(KnownLabelsBase, unittest2.TestCase): |
| 231 SUBJECT = _KNOWN.SCC_CALLER_IP |
| 232 |
| 233 |
| 234 class SccPlatform(KnownLabelsBase, unittest2.TestCase): |
| 235 SUBJECT = _KNOWN.SCC_PLATFORM |
| 236 WANTED_LABEL_DICT = { |
| 237 SUBJECT.label_name: report_request.ReportedPlatforms.UNKNOWN.name |
| 238 } |
| 239 |
| 240 |
| 241 class SccReferer(KnownLabelsBase, unittest2.TestCase): |
| 242 SUBJECT = _KNOWN.SCC_REFERER |
| 243 |
| 244 |
| 245 class SccServiceAgent(KnownLabelsBase, unittest2.TestCase): |
| 246 SUBJECT = _KNOWN.SCC_SERVICE_AGENT |
| 247 WANTED_LABEL_DICT = {SUBJECT.label_name: label_descriptor.SERVICE_AGENT} |
| 248 |
| 249 |
| 250 class SccUserAgent(KnownLabelsBase, unittest2.TestCase): |
| 251 SUBJECT = _KNOWN.SCC_USER_AGENT |
| 252 WANTED_LABEL_DICT = {SUBJECT.label_name: label_descriptor.USER_AGENT} |
OLD | NEW |