|
| 1 | +# Copyright 2022 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 | +import os |
| 15 | +import re |
| 16 | + |
| 17 | +from _pytest.capture import CaptureFixture |
| 18 | +import google.auth.transport.requests |
| 19 | +from google.cloud.api_keys_v2 import Key |
| 20 | +import pytest |
| 21 | + |
| 22 | +import authenticate_with_api_key |
| 23 | +import create_api_key |
| 24 | +import delete_api_key |
| 25 | +import lookup_api_key |
| 26 | +import restrict_api_key_android |
| 27 | +import restrict_api_key_api |
| 28 | +import restrict_api_key_http |
| 29 | +import restrict_api_key_ios |
| 30 | +import restrict_api_key_server |
| 31 | + |
| 32 | +CREDENTIALS, PROJECT = google.auth.default() |
| 33 | +SERVICE_ACCOUNT_FILE = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture(scope="module") |
| 37 | +def api_key(): |
| 38 | + api_key = create_api_key.create_api_key(PROJECT) |
| 39 | + yield api_key |
| 40 | + delete_api_key.delete_api_key(PROJECT, get_key_id(api_key.name)) |
| 41 | + |
| 42 | + |
| 43 | +def get_key_id(api_key_name: str): |
| 44 | + return api_key_name.rsplit("/")[-1] |
| 45 | + |
| 46 | + |
| 47 | +def test_authenticate_with_api_key(api_key: Key, capsys: CaptureFixture): |
| 48 | + authenticate_with_api_key.authenticate_with_api_key(PROJECT, api_key.key_string) |
| 49 | + out, err = capsys.readouterr() |
| 50 | + assert re.search("Successfully authenticated using the API key", out) |
| 51 | + |
| 52 | + |
| 53 | +def test_lookup_api_key(api_key: Key, capsys: CaptureFixture): |
| 54 | + lookup_api_key.lookup_api_key(api_key.key_string) |
| 55 | + out, err = capsys.readouterr() |
| 56 | + assert re.search(f"Successfully retrieved the API key name: {api_key.name}", out) |
| 57 | + |
| 58 | + |
| 59 | +def test_restrict_api_key_android(api_key: Key, capsys: CaptureFixture): |
| 60 | + restrict_api_key_android.restrict_api_key_android(PROJECT, get_key_id(api_key.name)) |
| 61 | + out, err = capsys.readouterr() |
| 62 | + assert re.search(f"Successfully updated the API key: {api_key.name}", out) |
| 63 | + |
| 64 | + |
| 65 | +def test_restrict_api_key_api(api_key: Key, capsys: CaptureFixture): |
| 66 | + restrict_api_key_api.restrict_api_key_api(PROJECT, get_key_id(api_key.name)) |
| 67 | + out, err = capsys.readouterr() |
| 68 | + assert re.search(f"Successfully updated the API key: {api_key.name}", out) |
| 69 | + |
| 70 | + |
| 71 | +def test_restrict_api_key_http(api_key: Key, capsys: CaptureFixture): |
| 72 | + restrict_api_key_http.restrict_api_key_http(PROJECT, get_key_id(api_key.name)) |
| 73 | + out, err = capsys.readouterr() |
| 74 | + assert re.search(f"Successfully updated the API key: {api_key.name}", out) |
| 75 | + |
| 76 | + |
| 77 | +def test_restrict_api_key_ios(api_key: Key, capsys: CaptureFixture): |
| 78 | + restrict_api_key_ios.restrict_api_key_ios(PROJECT, get_key_id(api_key.name)) |
| 79 | + out, err = capsys.readouterr() |
| 80 | + assert re.search(f"Successfully updated the API key: {api_key.name}", out) |
| 81 | + |
| 82 | + |
| 83 | +def test_restrict_api_key_server(api_key: Key, capsys: CaptureFixture): |
| 84 | + restrict_api_key_server.restrict_api_key_server(PROJECT, get_key_id(api_key.name)) |
| 85 | + out, err = capsys.readouterr() |
| 86 | + assert re.search(f"Successfully updated the API key: {api_key.name}", out) |
0 commit comments