From fa57db18ec3bd826ff3bd76b761c5c7c8f72ee15 Mon Sep 17 00:00:00 2001 From: Naoyuki Tai Date: Mon, 20 May 2024 16:58:19 -0400 Subject: [PATCH 1/2] (1) Not select the with_op by default as you may not have 1password CLI (2) Make the test to run from the arxiv-base directory --- gcp/service_auth/tests/test_get_token.py | 29 ++++++++++++++++++------ pytest.ini | 4 ++++ 2 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 pytest.ini diff --git a/gcp/service_auth/tests/test_get_token.py b/gcp/service_auth/tests/test_get_token.py index 366b0a37..0e911199 100644 --- a/gcp/service_auth/tests/test_get_token.py +++ b/gcp/service_auth/tests/test_get_token.py @@ -1,4 +1,5 @@ import os +import sys import subprocess import time from typing import Generator @@ -7,29 +8,42 @@ import logging import datetime from pathlib import Path +import json -from gcp_service_auth import GcpIdentityToken +try: + from gcp_service_auth import GcpIdentityToken +except ModuleNotFoundError: + sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + from gcp_service_auth import GcpIdentityToken + pass + -TEST_URL = os.environ.get('TEST_URL') @pytest.fixture(scope="module") #@pytest.mark.with_op -def gcp_browse_cred() -> Generator[str, str, str]: +def gcp_browse_cred() -> (Generator[str, str, str], str): logging.basicConfig(level=logging.DEBUG) cred_file = os.path.join(Path(__file__).parent, "browse-local.json") cred_file = cred_file if not os.path.exists(cred_file): subprocess.run(["op", "document", "get", "4feibaz4tzn6iwk5c7ggvb7xwi", "-o", cred_file]) os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = cred_file - yield cred_file + op = subprocess.run(["op", "item", "get", "4feibaz4tzn6iwk5c7ggvb7xwi", "--format", "json"], stdout=subprocess.PIPE) + test_meta = json.loads(op.stdout) + test_url = "" + for field in test_meta['fields']: + if field["id"] == "heltf7phky3h6rnlvd7zi3542u": + test_url = field["value"] + break + yield (cred_file, test_url) os.remove(cred_file) return "" @pytest.mark.with_op -def test_get_token(gcp_browse_cred: str) -> None: - os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = gcp_browse_cred +def test_get_token(gcp_browse_cred: (str, str)) -> None: + os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = gcp_browse_cred[0] logger = logging.getLogger("test") - idt = GcpIdentityToken(TEST_URL, logger=logger, + idt = GcpIdentityToken(gcp_browse_cred[0][1], logger=logger, expiration=datetime.timedelta(seconds=1)) token0 = idt.token time.sleep(2) @@ -37,3 +51,4 @@ def test_get_token(gcp_browse_cred: str) -> None: assert token0 is not None assert token1 is not None assert token0 != token1 + diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..5c872f0b --- /dev/null +++ b/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +markers = + with_op: marks tests to run with 1password CLI +addopts = -m "not with_op" From 99a6957936f96c8e0c19db71aeb46084da6ef430 Mon Sep 17 00:00:00 2001 From: Naoyuki Tai Date: Tue, 21 May 2024 12:08:27 -0400 Subject: [PATCH 2/2] Fix the bad type annotation --- gcp/service_auth/tests/test_get_token.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/gcp/service_auth/tests/test_get_token.py b/gcp/service_auth/tests/test_get_token.py index 0e911199..1c7c1443 100644 --- a/gcp/service_auth/tests/test_get_token.py +++ b/gcp/service_auth/tests/test_get_token.py @@ -2,7 +2,7 @@ import sys import subprocess import time -from typing import Generator +from typing import Generator, Tuple import pytest import logging @@ -21,10 +21,16 @@ @pytest.fixture(scope="module") #@pytest.mark.with_op -def gcp_browse_cred() -> (Generator[str, str, str], str): +def gcp_browse_cred() -> Generator[Tuple[str, str], None, None]: + """The fixture sets up + 1. the path to the credentials file + 2. the URL to the service. + """ logging.basicConfig(level=logging.DEBUG) cred_file = os.path.join(Path(__file__).parent, "browse-local.json") cred_file = cred_file + # the magic numbers are assigned by the 1password. + # You'd find the value by running `op item list`, etc. if not os.path.exists(cred_file): subprocess.run(["op", "document", "get", "4feibaz4tzn6iwk5c7ggvb7xwi", "-o", cred_file]) os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = cred_file @@ -32,15 +38,16 @@ def gcp_browse_cred() -> (Generator[str, str, str], str): test_meta = json.loads(op.stdout) test_url = "" for field in test_meta['fields']: + # the magic number is assigned by 1p. This is a URL to test against if field["id"] == "heltf7phky3h6rnlvd7zi3542u": test_url = field["value"] break - yield (cred_file, test_url) + yield cred_file, test_url os.remove(cred_file) - return "" + return @pytest.mark.with_op -def test_get_token(gcp_browse_cred: (str, str)) -> None: +def test_get_token(gcp_browse_cred: Tuple[str, str]) -> None: os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = gcp_browse_cred[0] logger = logging.getLogger("test") idt = GcpIdentityToken(gcp_browse_cred[0][1], logger=logger,