Skip to content

Commit

Permalink
Merge pull request #276 from arXiv/ntai/fix-gcp-auth-token-tests
Browse files Browse the repository at this point in the history
(1) Not select the with_op by default as you may not have 1password CLI
  • Loading branch information
ntai-arxiv committed May 23, 2024
2 parents 94f5243 + 99a6957 commit f555170
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
40 changes: 31 additions & 9 deletions gcp/service_auth/tests/test_get_token.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,61 @@
import os
import sys
import subprocess
import time
from typing import Generator
from typing import Generator, Tuple

import pytest
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[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
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']:
# 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
os.remove(cred_file)
return ""
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: Tuple[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)
token1 = idt.token
assert token0 is not None
assert token1 is not None
assert token0 != token1

4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
markers =
with_op: marks tests to run with 1password CLI
addopts = -m "not with_op"

0 comments on commit f555170

Please sign in to comment.