diff --git a/.github/workflows/monthly-api-tests.yaml b/.github/workflows/monthly-api-tests.yaml new file mode 100644 index 0000000..b88fa02 --- /dev/null +++ b/.github/workflows/monthly-api-tests.yaml @@ -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" \ No newline at end of file diff --git a/bookops_worldcat/query.py b/bookops_worldcat/query.py index cac785a..568ebb4 100644 --- a/bookops_worldcat/query.py +++ b/bookops_worldcat/query.py @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 8db391e..0b3c99f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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] diff --git a/tests/test_authorize.py b/tests/test_authorize.py index 84a7f1c..f819e46 100644 --- a/tests/test_authorize.py +++ b/tests/test_authorize.py @@ -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) diff --git a/tests/test_metadata_api.py b/tests/test_metadata_api.py index 9edb8a6..cd4bcac 100644 --- a/tests/test_metadata_api.py +++ b/tests/test_metadata_api.py @@ -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