Skip to content

Commit

Permalink
added monthly_check tests and workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
charlottekostelic committed Aug 7, 2024
1 parent b39b34a commit 5241196
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
32 changes: 32 additions & 0 deletions .github/workflows/monthly-api-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: monthly API response check
on:
push:
branches:
- api-response-changes
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
WC_KEY: ${{ secrets.WC_KEY }}
WC_SECRET: ${{ secrets.WC_SECRET }}
WC_SCOPES: "WorldCatMetadataAPI"
jobs:
monthly-run:
name: Run monthly API response check
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version}}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version}}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r dev-requirements.txt
- name: Run monthly live tests
run: |
pytest -m "monthly_check"
2 changes: 1 addition & 1 deletion bookops_worldcat/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Union, Tuple, TYPE_CHECKING
import sys

from requests.models import PreparedRequest
from requests import PreparedRequest
from requests.exceptions import ConnectionError, HTTPError, Timeout, RetryError
from .errors import WorldcatRequestError

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ markers = [
"webtest: mark a test hitting live endpoints",
"holdings: mark holdings live endpoint tests",
"http_code: use to pass returned http code to 'mock_session_response' fixture that mocks 'requests.Session.send' method",
"monthly_check: mark a test that should be run with monthly API response check",
]

[tool.coverage.run]
Expand Down
44 changes: 44 additions & 0 deletions tests/test_authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,47 @@ def test_post_token_request_with_live_service_no_timeout(self, live_keys):
assert token.token_str.startswith("tk_")
assert token.is_expired() is False
assert isinstance(token.token_expires_at, datetime.datetime)


@pytest.mark.monthly_check
class TestMonthlyCheckWorldcatAccessToken:
"""Runs monthly tests against live service"""

def test_cred_in_env_variables(self):
assert os.getenv("WC_KEY") is not None
assert os.getenv("WC_SECRET") is not None
assert os.getenv("WC_SCOPES") == "WorldCatMetadataAPI"

def test_post_token_request_with_live_service(self):
token = WorldcatAccessToken(
key=os.getenv("WC_KEY"),
secret=os.getenv("WC_SECRET"),
scopes=os.getenv("WC_SCOPES"),
)

assert token.server_response.status_code == 200

# test presence of all returned parameters
response = token.server_response.json()
params = [
"access_token",
"expires_at",
"authenticating_institution_id",
"principalID",
"context_institution_id",
"scope",
"scopes",
"token_type",
"expires_in",
"principalIDNS",
]
for p in params:
assert p in response

# test if any new additions are present
assert sorted(params) == sorted(response.keys())

# test if token looks right
assert token.token_str.startswith("tk_")
assert token.is_expired() is False
assert isinstance(token.token_expires_at, datetime.datetime)
21 changes: 21 additions & 0 deletions tests/test_metadata_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,3 +1066,24 @@ def test_custom_retries(self, live_keys, stub_marc21):
str(exc.value)
)
assert session.adapters["https://"].max_retries.total == 3


@pytest.mark.monthly_check
class TestMonthlyCheckMetadataSession:
"""Runs rudimentary tests against live Metadata API for monthly checks"""

def test_bib_get(self):
token = WorldcatAccessToken(
key=os.getenv("WC_KEY"),
secret=os.getenv("WC_SECRET"),
scopes=os.getenv("WC_SCOPES"),
)

with MetadataSession(authorization=token) as session:
response = session.bib_get(41266045)

assert (
response.url
== "https://metadata.api.oclc.org/worldcat/manage/bibs/41266045"
)
assert response.status_code == 200

0 comments on commit 5241196

Please sign in to comment.