|
| 1 | +# Copyright 2023 Google LLC. |
| 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 | +"""E2E tests for the backend Python container.""" |
| 16 | + |
| 17 | +import json |
| 18 | +import os |
| 19 | +import urllib |
| 20 | + |
| 21 | + |
| 22 | +import google.auth.transport.requests |
| 23 | +import google.cloud.logging |
| 24 | +import google.oauth2.id_token |
| 25 | +import pytest |
| 26 | + |
| 27 | + |
| 28 | +_BACKEND_URL_ENV_VAR = 'BACKEND_URL' |
| 29 | + |
| 30 | +client = google.cloud.logging.Client() |
| 31 | +client.setup_logging() |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.systemtest |
| 35 | +def test_accessible_accounts(): |
| 36 | + auth_request = google.auth.transport.requests.Request() |
| 37 | + url = os.environ.get(_BACKEND_URL_ENV_VAR) |
| 38 | + |
| 39 | + id_token = google.oauth2.id_token.fetch_id_token(auth_request, url) |
| 40 | + |
| 41 | + assert id_token is not None, 'Could not fetch id token for get accounts.' |
| 42 | + |
| 43 | + request = urllib.request.Request(f'{url}/accessible_accounts') |
| 44 | + request.add_header('Authorization', f'Bearer {id_token}') |
| 45 | + |
| 46 | + with urllib.request.urlopen(request) as response: |
| 47 | + assert ( |
| 48 | + response.status == 200 |
| 49 | + ), 'Got non-200 response from /accessible_accounts' |
| 50 | + body = response.read() |
| 51 | + account_dict = json.loads(body) |
| 52 | + assert account_dict, 'Could not find accessible accounts' |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.systemtest |
| 56 | +@pytest.mark.skipif( |
| 57 | + os.getenv('TEST_FAILURE_ALERTING') != 'TRUE', |
| 58 | + reason='Test failure alerting is not set to TRUE.', |
| 59 | +) |
| 60 | +def test_that_always_fails(): |
| 61 | + """A deliberately failing test, to check alerts are sent when tests fail.""" |
| 62 | + assert False |
0 commit comments