diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3da260c..e4145b0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,19 +26,24 @@ jobs: permissions: contents: read steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Check out the repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: extractions/setup-just@53165ef7e734c5c07cb06b3c8e7b647c5aa16db3 # v4.0.0 + - name: Install just + uses: extractions/setup-just@53165ef7e734c5c07cb06b3c8e7b647c5aa16db3 # v4.0.0 with: just-version: "1.55.0" # Keep in sync with minimum-version in justfile - - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 + - name: Install uv + uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: cache-suffix: lint - name: Check the lockfile is up to date run: uv lock --check - - run: just lint - - run: uv build + - name: Lint and check formatting + run: just lint + - name: Build the package + run: uv build - name: Check generated models match the spec run: | just codegen @@ -50,14 +55,16 @@ jobs: permissions: contents: read steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Check out the repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: zizmorcore/zizmor-action@45d7276c9ca790aabd705e31ca99c6185c3e76dd # v0.8.1 + - name: Audit the workflows with zizmor + uses: zizmorcore/zizmor-action@45d7276c9ca790aabd705e31ca99c6185c3e76dd # v0.8.1 with: advanced-security: false - test: + unit-tests-and-type-checks: name: Unit tests and type checks (Python ${{ matrix.python }}${{ matrix.lowest_direct && ', lowest direct dependencies' || '' }}) runs-on: ubuntu-latest permissions: @@ -70,21 +77,26 @@ jobs: lowest_direct: true - python: "3.14" steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Check out the repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: extractions/setup-just@53165ef7e734c5c07cb06b3c8e7b647c5aa16db3 # v4.0.0 + - name: Install just + uses: extractions/setup-just@53165ef7e734c5c07cb06b3c8e7b647c5aa16db3 # v4.0.0 with: just-version: "1.55.0" # Keep in sync with minimum-version in justfile - - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 + - name: Install uv + uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: cache-suffix: tests-py${{ matrix.python }}${{ matrix.lowest_direct && '-lowest-direct' || '' }} - - run: just test --python="$PYTHON" ${LOWEST_DIRECT:+--lowest-direct} ${UPGRADE:+--upgrade} + - name: Run the unit tests + run: just test --python="$PYTHON" ${LOWEST_DIRECT:+--lowest-direct} ${UPGRADE:+--upgrade} env: PYTHON: ${{ matrix.python }} LOWEST_DIRECT: ${{ matrix.lowest_direct && 'true' || '' }} UPGRADE: ${{ (github.event_name == 'schedule' || inputs.upgrade) && 'true' || '' }} - - run: just typecheck --python="$PYTHON" ${LOWEST_DIRECT:+--lowest-direct} + - name: Run the type checks + run: just typecheck --python="$PYTHON" ${LOWEST_DIRECT:+--lowest-direct} if: ${{ !cancelled() }} env: PYTHON: ${{ matrix.python }} @@ -96,10 +108,12 @@ jobs: permissions: contents: read steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Check out the repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Check out the PDC service repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false repository: PhilanthropyDataCommons/service @@ -110,13 +124,16 @@ jobs: working-directory: service run: docker compose -f compose-ci.yml up -d --wait timeout-minutes: 5 - - uses: extractions/setup-just@53165ef7e734c5c07cb06b3c8e7b647c5aa16db3 # v4.0.0 + - name: Install just + uses: extractions/setup-just@53165ef7e734c5c07cb06b3c8e7b647c5aa16db3 # v4.0.0 with: just-version: "1.55.0" # Keep in sync with minimum-version in justfile - - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 + - name: Install uv + uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: cache-suffix: integration - - run: just integration ${UPGRADE:+--upgrade} + - name: Run the integration tests + run: just integration ${UPGRADE:+--upgrade} timeout-minutes: 5 env: UPGRADE: ${{ (github.event_name == 'schedule' || inputs.upgrade) && 'true' || '' }} @@ -124,22 +141,24 @@ jobs: all-green: name: All green if: always() - needs: [lint, zizmor, test, integration] + needs: [lint, zizmor, unit-tests-and-type-checks, integration] runs-on: ubuntu-latest permissions: {} steps: - - uses: re-actors/alls-green@b5b5b37504aa4183270bd3d855c52a67f212be35 # v1.3.0 + - name: Check that every required job passed + uses: re-actors/alls-green@b5b5b37504aa4183270bd3d855c52a67f212be35 # v1.3.0 with: jobs: ${{ toJSON(needs) }} notify: name: Notify failed build - needs: [lint, zizmor, test, integration] + needs: [lint, zizmor, unit-tests-and-type-checks, integration] if: failure() && (github.event_name == 'push' || github.event_name == 'schedule') runs-on: ubuntu-latest permissions: issues: write steps: - - uses: drivendataorg/failed-build-issue-action@940568ce50eeef3f31a920614cefcc6102d2f930 # v1.3.0 + - name: Open an issue for the failed build + uses: drivendataorg/failed-build-issue-action@940568ce50eeef3f31a920614cefcc6102d2f930 # v1.3.0 with: github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 110d747..2854c58 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ coverage.xml htmlcov/ .pytest_cache/ .ruff_cache/ +.mypy_cache/ .DS_Store codegen/openapi.preprocessed.json local/ diff --git a/AGENTS.md b/AGENTS.md index 17b4061..60b6676 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -14,12 +14,12 @@ A framework-free Python client for the Philanthropy Data Commons (PDC) API. The - `src/pdc_api_client/_generated.py` is generated from `src/pdc_api_client/openapi.json` by `just codegen`. Never edit it by hand, and import models from `pdc_api_client.models`. - Generated code owns field shapes. `models.py` owns client concerns: bundle `pdc_endpoint` and `default_page_size` ClassVars, aliases, and write-only request models. `permission_grants.py` models the grant union by hand. - After `just update-api-spec`, record the spec's `info.version` and the download date in `CHANGELOG.md`; PDC changes the spec without bumping that version. -- When the spec disagrees with what PDC returns, add a rule to `codegen/preprocess.py` with a one-line docstring naming the mismatch, then regenerate. Delete a rule once PDC fixes the cause. CI fails if `_generated.py` drifts from the spec. +- When the spec disagrees with what PDC returns, add a rule to `codegen/preprocess.py` with a one-line comment naming the mismatch, then regenerate. Delete a rule once PDC fixes the cause. CI fails if `_generated.py` drifts from the spec. ## API conventions - `session` is the first positional argument; every other argument is keyword-only. The exception is the bundle type in `get_bundle_page` and `pdc_api_client.testing.add_page_response`, which names the endpoint. -- Domain errors end in `Error`. +- Errors end in `Error`, except the base class `PDCException`. A lookup's not-found error subclasses `PDCNotFoundError`. ## Commands @@ -34,5 +34,6 @@ A framework-free Python client for the Philanthropy Data Commons (PDC) API. The - Readability, simplicity, and elegance, in that order. - Write the test before the code. Assert transformations and branch behavior, not that a mock returned what it was given. - Every client function gets an integration test against PDC as well as unit tests, unless PDC's CI stack can't support it. +- Tests live in `Test` classes named for the function or class under test, with methods named `test_` in the present tense, such as `TestGetProposal.test_raises_proposal_not_found_on_404`. Integration tests that cover a workflow across several functions may group by area instead, such as `TestProposals`. - No docstrings, and comments only for a non-obvious why such as a PDC quirk, in one line. - Lower bounds only in `pyproject.toml`. `uv.lock` pins the versions that development and CI use. CI also tests the lowest allowed versions on the oldest supported Python, and a weekly run tests the newest versions released at least a week ago, so raise a bound when you use something newer, and fix breakage from new releases rather than pinning. diff --git a/CHANGELOG.md b/CHANGELOG.md index 532cccb..11eb532 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,22 @@ This project follows [Semantic Versioning](https://semver.org). Until 1.0, a min PDC does not bump its spec version for every API change, so each entry names the spec version and the date the spec was downloaded. +## Unreleased + +Breaking changes that make names consistent before a wider release: + +- `get_basefields` is now `get_base_fields`, `create_changemaker_proposal` is `create_proposal`, `create_changemaker_proposal_version` is `create_proposal_version`, and `register_file_upload` is `create_file`. +- `get_application_form` takes `application_form_id` instead of `form_id`. +- `list_permission_grants` filters by `changemaker_id`, `funder_short_code`, and `proposal_id` instead of `changemaker`, `funder`, and `proposal`. +- The permission grant create and update functions take `grant` instead of `spec`. +- `ProposalNotFoundError`, `ChangemakerNotFoundError`, `FunderNotFoundError`, and `ChangemakerFieldValueBatchNotFoundError` now subclass `PDCNotFoundError`, and their `.detail` names the missing resource. +- Seeding's `ensure_funder_for_kc_org`, `ensure_changemaker_for_kc_org`, and `ensure_data_provider_for_kc_org` are now `ensure_*_for_keycloak_org`, with a `keycloak_org` argument. +- Seeding drops its own `link_changemaker_to_proposal`; use the client function of the same name. + +Other changes: + +- `ChangemakerFieldValueBatchNotFoundError` and `TokenRequestError` can be imported from `pdc_api_client`. + ## 0.3.0 (2026-11-18) - The package is now type checked with mypy (including strict mode), pyrefly, and ty, so its `py.typed` marker is accurate. The testing helpers and seeding modules are fully annotated. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6f2a94e..f79b7c3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -24,7 +24,7 @@ just test --python 3.12 --lowest-direct -- -k token `just typecheck` takes `--python` and `--lowest-direct` too. CI type checks Python 3.12 with the lowest versions and Python 3.14 with `uv.lock`. -`uv.lock` pins the versions used for development and CI. Update it with `uv lock --upgrade`. CI also runs both suites weekly with `--upgrade`, and opens an issue when that run fails. +`uv.lock` pins the versions used for development and CI. Update it with `uv lock --upgrade`. CI also runs both suites weekly with `--upgrade`, and opens an issue when that run or a push to main fails. `just pdc up` downloads PDC's [`compose-ci.yml`](https://github.com/PhilanthropyDataCommons/service/blob/main/compose-ci.yml) and Keycloak realm from the service repo's main branch and starts the stack: the PDC API, Keycloak, Postgres, and S3. The integration tests reach Keycloak at `pdc-auth`, so add this line to `/etc/hosts`: diff --git a/README.md b/README.md index 3e7c85c..6f2526b 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ PDC applies the caller's permissions to every request. The same call can return There are two ways to read a list. -**Iterate everything.** `get_changemakers`, `get_funders`, `get_proposals`, `get_initiatives`, and the other `get_*` list functions are generators that walk every page for you: +**Iterate everything.** `get_changemakers`, `get_funders`, `get_proposals`, `get_initiatives`, and the other plural `get_*` functions are generators that walk every page for you. The exception is `get_base_fields`, which returns a list: ```python from pdc_api_client.client import get_changemaker_proposals @@ -103,11 +103,11 @@ Bundles for nested endpoints take path parameters, such as `get_bundle_page(sess The client covers the writes a proposal workflow needs: creating a proposal, adding a version with field values, linking a changemaker to it, and uploading files. ```python -from pdc_api_client.client import create_changemaker_proposal, create_changemaker_proposal_version +from pdc_api_client.client import create_proposal, create_proposal_version from pdc_api_client.models import NewProposalFieldValue, NewProposalVersion -proposal = create_changemaker_proposal(session, data={"opportunityId": 5, "externalId": "app-2026-001"}) -version = create_changemaker_proposal_version( +proposal = create_proposal(session, data={"opportunityId": 5, "externalId": "app-2026-001"}) +version = create_proposal_version( session, proposal_version=NewProposalVersion( proposal_id=proposal.id, @@ -122,7 +122,7 @@ A changemaker can also write its own profile values. `create_changemaker_source` `create_initiative` creates an initiative led by a changemaker, and `create_initiative_field_value` adds a value to it from a given source. -Uploading a file takes two steps. `register_file_upload` creates the file record and returns a presigned S3 POST, and `upload_file_to_s3_presigned_url` sends the bytes to it. +Uploading a file takes two steps. `create_file` creates the file record and returns a presigned S3 POST, and `upload_file_to_s3_presigned_url` sends the bytes to it. ## Permission grants @@ -134,7 +134,7 @@ from pdc_api_client.permission_grants import ChangemakerPermissionGrant, create_ grant = create_permission_grant( session, - spec=ChangemakerPermissionGrant( + grant=ChangemakerPermissionGrant( changemaker_id=7, grantee_type=PermissionGrantGranteeType.USER_GROUP, grantee_keycloak_organization_id="1f0c6e2a-7d4b-4c55-9a51-0d8c2f0b6a11", @@ -143,14 +143,14 @@ grant = create_permission_grant( ), ) -page = list_permission_grants(session, changemaker=7, verb=PermissionGrantVerb.VIEW) +page = list_permission_grants(session, changemaker_id=7, verb=PermissionGrantVerb.VIEW) ``` The models check that the grantee fields match `grantee_type` before anything is sent. Create, get, update, and delete functions exist for grants and for default permission grants. A default permission grant has no context entity; PDC copies it into a real grant on each new entity of its type as that entity is created. ## Errors -`APISession` raises a subclass of `pdc_api_client.PDCException` for every failed request. Each exception has `.code` (the HTTP status) and `.detail` (the parsed JSON body, or the raw text when the body isn't JSON). +`APISession` raises a subclass of `pdc_api_client.PDCException` for every failed request. Each exception has `.code` (the HTTP status) and `.detail` (the parsed JSON body, or the raw text when the body isn't JSON). Before raising, it retries a GET or HEAD up to three times on a 502, 503, or 504 or a dropped connection. | Status | Exception | | ------------- | ----------------------------- | @@ -163,7 +163,15 @@ The models check that the grantee fields match `grantee_type` before anything is | 5xx, timeout, or connection failure | `PDCServiceError` | | Any other non-2xx | `PDCException` | -A few lookups turn a 404 into an error named for the resource: `get_proposal` and `get_changemaker_proposal` raise `ProposalNotFoundError`, `get_changemaker` raises `ChangemakerNotFoundError`, `get_funder` raises `FunderNotFoundError`, `get_changemaker_field_value_batch` raises `ChangemakerFieldValueBatchNotFoundError`, and `get_application_form_by_name` raises `ApplicationFormNotFoundError`, a `PDCNotFoundError`, when no form has that name. `KeycloakClientCredentialsProvider` raises `TokenRequestError` when Keycloak rejects the grant. +A few lookups raise a subclass of `PDCNotFoundError` named for the resource: + +- `get_proposal` and `get_changemaker_proposal` raise `ProposalNotFoundError`. +- `get_changemaker` raises `ChangemakerNotFoundError`. +- `get_funder` raises `FunderNotFoundError`. +- `get_changemaker_field_value_batch` raises `ChangemakerFieldValueBatchNotFoundError`. +- `get_application_form_by_name` raises `ApplicationFormNotFoundError` when no form has that name. + +`KeycloakClientCredentialsProvider` raises `TokenRequestError` when Keycloak rejects the grant. ## Models @@ -177,8 +185,8 @@ The spec doesn't always match what PDC returns. Some fields it marks required ar The client covers most of the API but not all of it: -- **No client functions:** `/applicationFormFields`, `/sources`, `/tasks` (bulk uploads and base field copies), `/terminologySets`, and `/users`. Their models are generated, and `session.get`/`session.post` reach any endpoint directly. -- **Writes outside the proposal workflow:** creating funders, changemakers, opportunities, sources, and the like exists only as seeding helpers in `pdc_api_client.seeding`, which aren't meant for production data. +- **No client functions:** `/applicationFormFields`, `/sources` (apart from `create_changemaker_source`), `/tasks` (bulk uploads and base field copies), `/terminologySets`, and `/users`. Their models are generated, and `session.get`/`session.post` reach any endpoint directly. +- **Other writes:** creating funders, changemakers, opportunities, and funder or data-provider sources is possible only through the seeding helpers in `pdc_api_client.seeding`, which aren't meant for production data. - **Async:** the client is synchronous and built on `requests`. Issues and pull requests that fill these gaps are welcome. diff --git a/integration_tests/conftest.py b/integration_tests/conftest.py index c7fc794..7109923 100644 --- a/integration_tests/conftest.py +++ b/integration_tests/conftest.py @@ -1,4 +1,5 @@ import os +import uuid import pytest import requests @@ -21,8 +22,8 @@ KEYCLOAK_CLIENT_ID = os.environ.get("KEYCLOAK_CLIENT_ID", "pdc-dev-ingest") KEYCLOAK_CLIENT_SECRET = os.environ.get("KEYCLOAK_CLIENT_SECRET", "password") PDC_API_TIMEOUT = float(os.environ.get("PDC_API_TIMEOUT", "30")) -KC_ADMIN_USERNAME = os.environ.get("KEYCLOAK_ADMIN_USERNAME", "admin") -KC_ADMIN_PASSWORD = os.environ.get("KEYCLOAK_ADMIN_PASSWORD", "admin") +KEYCLOAK_ADMIN_USERNAME = os.environ.get("KEYCLOAK_ADMIN_USERNAME", "admin") +KEYCLOAK_ADMIN_PASSWORD = os.environ.get("KEYCLOAK_ADMIN_PASSWORD", "admin") @pytest.fixture(scope="session") @@ -55,8 +56,8 @@ def keycloak_admin() -> KeycloakAdmin: return KeycloakAdmin( keycloak_url=KEYCLOAK_SERVER_URL, realm=KEYCLOAK_REALM, - admin_username=KC_ADMIN_USERNAME, - admin_password=KC_ADMIN_PASSWORD, + admin_username=KEYCLOAK_ADMIN_USERNAME, + admin_password=KEYCLOAK_ADMIN_PASSWORD, ) @@ -116,3 +117,30 @@ def make(username: str, org_alias: str) -> APISession: @pytest.fixture(scope="session") def test_user_password() -> str: return TEST_USER_PASSWORD + + +@pytest.fixture +def suffix() -> str: + return uuid.uuid4().hex[:8] + + +@pytest.fixture +def fresh_keycloak_org(keycloak_admin, suffix): + alias = f"fresh-org-{suffix}" + return keycloak_admin.ensure_organization( + name=f"Fresh Org {suffix}", + alias=alias, + domain=f"{alias}.example.com", + ) + + +@pytest.fixture +def add_user_to_org(keycloak_admin, keycloak_client_id, test_user_password): + keycloak_admin.ensure_direct_grants_enabled(client_id=keycloak_client_id) + + def add(username: str, *orgs) -> None: + user = keycloak_admin.ensure_user(username=username, email=username, password=test_user_password) + for org in orgs: + keycloak_admin.ensure_user_in_organization(organization_id=org.id, user_id=user.id) + + return add diff --git a/integration_tests/pytest.ini b/integration_tests/pytest.ini index 706b0ff..6443df0 100644 --- a/integration_tests/pytest.ini +++ b/integration_tests/pytest.ini @@ -1,3 +1,3 @@ [pytest] -# A separate config so the unit suite's global `responses` interception does not apply. +# A separate config so the unit suite's testpaths and coverage options in pyproject.toml do not apply. testpaths = integration_tests diff --git a/integration_tests/test_application_form_input_type_contract.py b/integration_tests/test_application_form_input_type_contract.py index f339415..913ea04 100644 --- a/integration_tests/test_application_form_input_type_contract.py +++ b/integration_tests/test_application_form_input_type_contract.py @@ -59,7 +59,7 @@ def test_field_without_input_type_parses_as_none(self, api_session): base_field_short_codes=[base_field.short_code], ) - form = get_application_form(api_session, form_id=seeded.id) + form = get_application_form(api_session, application_form_id=seeded.id) assert isinstance(form, ChangemakerApplicationForm) assert form.fields[0].input_type is None diff --git a/integration_tests/test_default_permission_grants.py b/integration_tests/test_default_permission_grants.py index 4435ccc..561c4d5 100644 --- a/integration_tests/test_default_permission_grants.py +++ b/integration_tests/test_default_permission_grants.py @@ -41,7 +41,7 @@ class TestDefaultPermissionGrantsLifecycle: def test_create_get_update_delete_roundtrip(self, api_session, created_default_grant_ids): spec = _changemaker_view_spec() - created = create_default_permission_grant(api_session, spec=spec) + created = create_default_permission_grant(api_session, grant=spec) created_default_grant_ids.append(created.id) assert created.id is not None @@ -51,7 +51,7 @@ def test_create_get_update_delete_roundtrip(self, api_session, created_default_g assert get_default_permission_grant(api_session, grant_id=created.id) == created spec.verbs = [PermissionGrantVerb.VIEW, PermissionGrantVerb.EDIT] - updated = update_default_permission_grant(api_session, grant_id=created.id, spec=spec) + updated = update_default_permission_grant(api_session, grant_id=created.id, grant=spec) assert set(updated.verbs) == {PermissionGrantVerb.VIEW, PermissionGrantVerb.EDIT} delete_default_permission_grant(api_session, grant_id=created.id) @@ -61,7 +61,7 @@ def test_create_get_update_delete_roundtrip(self, api_session, created_default_g get_default_permission_grant(api_session, grant_id=created.id) def test_list_includes_created_grant(self, api_session, created_default_grant_ids): - created = create_default_permission_grant(api_session, spec=_changemaker_view_spec()) + created = create_default_permission_grant(api_session, grant=_changemaker_view_spec()) created_default_grant_ids.append(created.id) bundle = list_default_permission_grants(api_session, count_per_page=200) diff --git a/integration_tests/test_ensure_for_kc_org.py b/integration_tests/test_ensure_for_kc_org.py deleted file mode 100644 index 5526867..0000000 --- a/integration_tests/test_ensure_for_kc_org.py +++ /dev/null @@ -1,41 +0,0 @@ -import uuid - -import pytest - -from pdc_api_client.seeding.pdc_entities import ( - ensure_changemaker_for_kc_org, - ensure_data_provider_for_kc_org, - ensure_funder_for_kc_org, -) - - -@pytest.fixture -def fresh_kc_org(keycloak_admin): - suffix = uuid.uuid4().hex[:8] - alias = f"ensure-test-{suffix}" - return keycloak_admin.ensure_organization( - name=f"Ensure Test {suffix}", - alias=alias, - domain=f"{alias}.example.com", - ) - - -def test_ensure_funder_for_kc_org_is_idempotent(api_session, fresh_kc_org): - first = ensure_funder_for_kc_org(api_session, kc_org=fresh_kc_org, name="X Funder") - second = ensure_funder_for_kc_org(api_session, kc_org=fresh_kc_org, name="X Funder") - assert first.short_code == second.short_code - assert str(first.keycloak_organization_id) == fresh_kc_org.id - - -def test_ensure_changemaker_for_kc_org_is_idempotent(api_session, fresh_kc_org): - first = ensure_changemaker_for_kc_org(api_session, kc_org=fresh_kc_org, name="X Changemaker") - second = ensure_changemaker_for_kc_org(api_session, kc_org=fresh_kc_org, name="X Changemaker") - assert first.id == second.id - assert str(first.keycloak_organization_id) == fresh_kc_org.id - - -def test_ensure_data_provider_for_kc_org_is_idempotent(api_session, fresh_kc_org): - first = ensure_data_provider_for_kc_org(api_session, kc_org=fresh_kc_org, name="X Data Provider") - second = ensure_data_provider_for_kc_org(api_session, kc_org=fresh_kc_org, name="X Data Provider") - assert first.short_code == second.short_code - assert str(first.keycloak_organization_id) == fresh_kc_org.id diff --git a/integration_tests/test_ensure_for_keycloak_org.py b/integration_tests/test_ensure_for_keycloak_org.py new file mode 100644 index 0000000..cd7f967 --- /dev/null +++ b/integration_tests/test_ensure_for_keycloak_org.py @@ -0,0 +1,37 @@ +from pdc_api_client.seeding.pdc_entities import ( + ensure_changemaker_for_keycloak_org, + ensure_data_provider_for_keycloak_org, + ensure_funder_for_keycloak_org, +) + + +class TestEnsureFunderForKeycloakOrg: + def test_is_idempotent(self, api_session, fresh_keycloak_org): + first = ensure_funder_for_keycloak_org(api_session, keycloak_org=fresh_keycloak_org, name="Ensure Test Funder") + second = ensure_funder_for_keycloak_org(api_session, keycloak_org=fresh_keycloak_org, name="Ensure Test Funder") + assert first.short_code == second.short_code + assert str(first.keycloak_organization_id) == fresh_keycloak_org.id + + +class TestEnsureChangemakerForKeycloakOrg: + def test_is_idempotent(self, api_session, fresh_keycloak_org): + first = ensure_changemaker_for_keycloak_org( + api_session, keycloak_org=fresh_keycloak_org, name="Ensure Test Changemaker" + ) + second = ensure_changemaker_for_keycloak_org( + api_session, keycloak_org=fresh_keycloak_org, name="Ensure Test Changemaker" + ) + assert first.id == second.id + assert str(first.keycloak_organization_id) == fresh_keycloak_org.id + + +class TestEnsureDataProviderForKeycloakOrg: + def test_is_idempotent(self, api_session, fresh_keycloak_org): + first = ensure_data_provider_for_keycloak_org( + api_session, keycloak_org=fresh_keycloak_org, name="Ensure Test Data Provider" + ) + second = ensure_data_provider_for_keycloak_org( + api_session, keycloak_org=fresh_keycloak_org, name="Ensure Test Data Provider" + ) + assert first.short_code == second.short_code + assert str(first.keycloak_organization_id) == fresh_keycloak_org.id diff --git a/integration_tests/test_pdc_group_membership.py b/integration_tests/test_group_membership.py similarity index 63% rename from integration_tests/test_pdc_group_membership.py rename to integration_tests/test_group_membership.py index 1aa9135..dfb3305 100644 --- a/integration_tests/test_pdc_group_membership.py +++ b/integration_tests/test_group_membership.py @@ -20,9 +20,7 @@ def _can_manage(session: APISession, changemaker_id: int) -> bool: @pytest.fixture() -def user_in_two_orgs(keycloak_admin, keycloak_client_id, test_user_password): - suffix = uuid.uuid4().hex[:8] - keycloak_admin.ensure_direct_grants_enabled(client_id=keycloak_client_id) +def user_in_two_orgs(keycloak_admin, add_user_to_org, suffix): org_a, org_b = ( keycloak_admin.ensure_organization( name=f"Membership {suffix} {label}", @@ -32,9 +30,7 @@ def user_in_two_orgs(keycloak_admin, keycloak_client_id, test_user_password): for label in ("a", "b") ) username = f"membership-{suffix}@example.com" - user = keycloak_admin.ensure_user(username=username, email=username, password=test_user_password) - for org in (org_a, org_b): - keycloak_admin.ensure_user_in_organization(organization_id=org.id, user_id=user.id) + add_user_to_org(username, org_a, org_b) return username, org_a, org_b @@ -46,7 +42,7 @@ def create(org_id: str) -> int: changemaker = seed_changemaker(api_session) grant = create_permission_grant( api_session, - spec=ChangemakerPermissionGrant( + grant=ChangemakerPermissionGrant( changemaker_id=changemaker.id, grantee_type=PermissionGrantGranteeType.USER_GROUP, grantee_keycloak_organization_id=uuid.UUID(org_id), @@ -63,17 +59,20 @@ def create(org_id: str) -> int: delete_permission_grant(api_session, grant_id=grant_id) -@pytest.mark.xfail( - strict=True, - reason="PhilanthropyDataCommons/service#2617: a token's user-group grants leak into the user's other tokens", -) -def test_permissions_follow_only_the_current_tokens_org(user_in_two_orgs, changemaker_managed_by, session_narrowed_to): - username, org_a, org_b = user_in_two_orgs - changemaker_id = changemaker_managed_by(org_a.id) - as_b = session_narrowed_to(username, org_b.alias) +class TestPermissionsPerToken: + @pytest.mark.xfail( + strict=True, + reason="PhilanthropyDataCommons/service#2617: a token's user-group grants leak into the user's other tokens", + ) + def test_permissions_follow_only_the_current_tokens_org( + self, user_in_two_orgs, changemaker_managed_by, session_narrowed_to + ): + username, org_a, org_b = user_in_two_orgs + changemaker_id = changemaker_managed_by(org_a.id) + as_b = session_narrowed_to(username, org_b.alias) - assert not _can_manage(as_b, changemaker_id) + assert not _can_manage(as_b, changemaker_id) - _can_manage(session_narrowed_to(username, org_a.alias), changemaker_id) + _can_manage(session_narrowed_to(username, org_a.alias), changemaker_id) - assert not _can_manage(as_b, changemaker_id) + assert not _can_manage(as_b, changemaker_id) diff --git a/integration_tests/test_keycloak_admin.py b/integration_tests/test_keycloak_admin.py index 9544f98..4cffee2 100644 --- a/integration_tests/test_keycloak_admin.py +++ b/integration_tests/test_keycloak_admin.py @@ -1,22 +1,11 @@ -import uuid - -import pytest - from pdc_api_client.seeding.keycloak_admin import KeycloakOrganization, KeycloakUser -TEST_USER_PASSWORD = "integration-test-password" - - -@pytest.fixture -def suffix() -> str: - return uuid.uuid4().hex[:8] - -class TestOrganizations: - def test_ensure_organization_creates_then_finds_existing(self, keycloak_admin, suffix): - alias = f"pkg-org-{suffix}" +class TestEnsureOrganization: + def test_creates_then_finds_existing(self, keycloak_admin, suffix): + alias = f"keycloak-admin-test-org-{suffix}" created = keycloak_admin.ensure_organization( - name=f"Pkg Org {suffix}", alias=alias, domain=f"{alias}.example.com" + name=f"Keycloak Admin Test Org {suffix}", alias=alias, domain=f"{alias}.example.com" ) assert isinstance(created, KeycloakOrganization) assert created.alias == alias @@ -24,36 +13,40 @@ def test_ensure_organization_creates_then_finds_existing(self, keycloak_admin, s again = keycloak_admin.ensure_organization(name="ignored", alias=alias, domain=f"{alias}.example.com") assert again.id == created.id - def test_get_organization_returns_existing_and_none(self, keycloak_admin, suffix): - alias = f"pkg-getorg-{suffix}" + +class TestGetOrganization: + def test_returns_existing_and_none(self, keycloak_admin, suffix): + alias = f"keycloak-admin-test-get-org-{suffix}" assert keycloak_admin.get_organization(alias=alias) is None created = keycloak_admin.ensure_organization( - name=f"Pkg Get {suffix}", alias=alias, domain=f"{alias}.example.com" + name=f"Keycloak Admin Test Get Org {suffix}", alias=alias, domain=f"{alias}.example.com" ) found = keycloak_admin.get_organization(alias=alias) assert found is not None assert found.id == created.id -class TestUsers: - def test_ensure_user_creates_then_finds_existing(self, keycloak_admin, suffix): +class TestEnsureUser: + def test_creates_then_finds_existing(self, keycloak_admin, suffix, test_user_password): # registrationEmailAsUsername=true on the realm, so username == email. - email = f"pkg-user-{suffix}@example.com" - created = keycloak_admin.ensure_user(username=email, email=email, password=TEST_USER_PASSWORD) + email = f"keycloak-admin-test-user-{suffix}@example.com" + created = keycloak_admin.ensure_user(username=email, email=email, password=test_user_password) assert isinstance(created, KeycloakUser) assert created.id - again = keycloak_admin.ensure_user(username=email, email=email, password=TEST_USER_PASSWORD) + again = keycloak_admin.ensure_user(username=email, email=email, password=test_user_password) assert again.id == created.id -class TestMemberships: - def test_add_is_idempotent_and_remove_reports_change(self, keycloak_admin, suffix): - alias = f"pkg-mem-{suffix}" - org = keycloak_admin.ensure_organization(name=f"Pkg Mem {suffix}", alias=alias, domain=f"{alias}.example.com") - email = f"pkg-mem-user-{suffix}@example.com" - user = keycloak_admin.ensure_user(username=email, email=email, password=TEST_USER_PASSWORD) +class TestUserOrganizationMembership: + def test_add_is_idempotent_and_remove_reports_change(self, keycloak_admin, suffix, test_user_password): + alias = f"keycloak-admin-test-membership-org-{suffix}" + org = keycloak_admin.ensure_organization( + name=f"Keycloak Admin Test Membership Org {suffix}", alias=alias, domain=f"{alias}.example.com" + ) + email = f"keycloak-admin-test-member-{suffix}@example.com" + user = keycloak_admin.ensure_user(username=email, email=email, password=test_user_password) assert keycloak_admin.ensure_user_in_organization(organization_id=org.id, user_id=user.id) is True assert keycloak_admin.ensure_user_in_organization(organization_id=org.id, user_id=user.id) is False @@ -62,10 +55,10 @@ def test_add_is_idempotent_and_remove_reports_change(self, keycloak_admin, suffi assert keycloak_admin.remove_user_from_organization(organization_id=org.id, user_id=user.id) is False -class TestClientConfig: - def test_ensure_oidc_login_enabled_merges_redirect_uri(self, keycloak_admin, keycloak_client_id): +class TestEnsureOidcLoginEnabled: + def test_merges_redirect_uri(self, keycloak_admin, keycloak_client_id): # A fixed URI keeps repeated runs on a persistent stack from growing redirectUris. - uri = "http://localhost:8000/pkg-integration-test/callback" + uri = "http://localhost:8000/keycloak-admin-test/callback" keycloak_admin.ensure_oidc_login_enabled(client_id=keycloak_client_id, redirect_uris=[uri]) client = keycloak_admin._find_client(keycloak_client_id) @@ -75,18 +68,28 @@ def test_ensure_oidc_login_enabled_merges_redirect_uri(self, keycloak_admin, key keycloak_admin.ensure_oidc_login_enabled(client_id=keycloak_client_id, redirect_uris=[uri]) assert uri in keycloak_admin._find_client(keycloak_client_id).get("redirectUris", []) - def test_ensure_client_secret_sets_and_is_idempotent(self, keycloak_admin, keycloak_client_id): + +class TestEnsureClientSecret: + def test_sets_and_is_idempotent(self, keycloak_admin, keycloak_client_id): # The shared session fixtures authenticate with the original secret. target = keycloak_admin._find_client(keycloak_client_id) original = keycloak_admin._admin.get_client_secrets(target["id"])["value"] try: - assert keycloak_admin.ensure_client_secret(client_id=keycloak_client_id, secret="pkg-int-secret") is True - assert keycloak_admin._admin.get_client_secrets(target["id"])["value"] == "pkg-int-secret" - assert keycloak_admin.ensure_client_secret(client_id=keycloak_client_id, secret="pkg-int-secret") is False + assert ( + keycloak_admin.ensure_client_secret(client_id=keycloak_client_id, secret="keycloak-admin-test-secret") + is True + ) + assert keycloak_admin._admin.get_client_secrets(target["id"])["value"] == "keycloak-admin-test-secret" + assert ( + keycloak_admin.ensure_client_secret(client_id=keycloak_client_id, secret="keycloak-admin-test-secret") + is False + ) finally: keycloak_admin.ensure_client_secret(client_id=keycloak_client_id, secret=original) - def test_direct_grants_enable_is_idempotent(self, keycloak_admin, keycloak_client_id): + +class TestEnsureDirectGrantsEnabled: + def test_is_idempotent(self, keycloak_admin, keycloak_client_id): keycloak_admin.set_direct_grants_enabled(client_id=keycloak_client_id, enabled=False) assert keycloak_admin.is_direct_grants_enabled(client_id=keycloak_client_id) is False diff --git a/integration_tests/test_operations.py b/integration_tests/test_operations.py index 1ccf59c..89d90a7 100644 --- a/integration_tests/test_operations.py +++ b/integration_tests/test_operations.py @@ -8,14 +8,14 @@ from pdc_api_client.client import ( create_changemaker_field_value, create_changemaker_field_value_batch, - create_changemaker_proposal, - create_changemaker_proposal_version, create_changemaker_source, create_initiative, create_initiative_field_value, + create_proposal, + create_proposal_version, get_application_form, get_application_form_by_name, - get_basefields, + get_base_fields, get_bundle_page, get_changemaker, get_changemaker_field_values, @@ -81,16 +81,16 @@ def test_authenticated_request_succeeds(self, api_session): class TestBaseFields: def test_get_basefields_returns_list(self, api_session): - result = list(get_basefields(api_session)) + result = list(get_base_fields(api_session)) assert len(result) > 0 def test_seeded_base_field_exists(self, api_session, seeded_pdc_stack): - result = list(get_basefields(api_session)) + result = list(get_base_fields(api_session)) short_codes = [bf.short_code for bf in result] assert seeded_pdc_stack.application_form.fields[0]["baseFieldShortCode"] in short_codes def test_base_field_parses_as_pydantic_model(self, api_session): - result = list(get_basefields(api_session)) + result = list(get_base_fields(api_session)) bf = result[0] assert isinstance(bf, BaseField) assert bf.short_code @@ -152,8 +152,8 @@ def test_is_collaborative_filter_returns_only_collaboratives(self, api_session): assert regular.short_code not in short_codes assert all(entry.is_collaborative is True for entry in bundle.entries) - def test_search_combined_with_is_collaborative_filter(self, api_session): - marker = f"inttest-combo-{uuid.uuid4().hex[:8]}" + def test_search_combined_with_is_collaborative_filter(self, api_session, suffix): + marker = f"inttest-combo-{suffix}" matching_collab = seed_funder( api_session, name=f"{marker} Funders Collaborative", @@ -181,8 +181,8 @@ def test_search_combined_with_is_collaborative_filter(self, api_session): assert matching_regular.short_code not in short_codes assert all(entry.is_collaborative is True for entry in bundle.entries) - def test_pagination_with_is_collaborative_filter(self, api_session): - marker = f"inttest-page-{uuid.uuid4().hex[:8]}" + def test_pagination_with_is_collaborative_filter(self, api_session, suffix): + marker = f"inttest-page-{suffix}" collaboratives = [ seed_funder( api_session, @@ -542,22 +542,22 @@ def test_get_application_form_returns_seeded_form(self, api_session): base_field_short_codes=[base_field.short_code], ) - form = get_application_form(api_session, form_id=seeded_form.id) + form = get_application_form(api_session, application_form_id=seeded_form.id) assert isinstance(form, ChangemakerApplicationForm) assert form.id == seeded_form.id assert form.opportunity_id == opportunity.id assert {f.base_field.short_code for f in form.fields} == {base_field.short_code} - def test_get_application_form_by_name_finds_seeded_form(self, api_session): - name = f"Named form {uuid.uuid4().hex[:8]}" + def test_get_application_form_by_name_finds_seeded_form(self, api_session, suffix): + name = f"Named form {suffix}" seeded_form = seed_application_form(api_session, name=name) assert get_application_form_by_name(api_session, name=name).id == seeded_form.id class TestProposalCreationFlow: - def test_create_proposal_link_and_round_trip(self, api_session): + def test_create_proposal_link_and_round_trip(self, api_session, suffix): funder = seed_funder(api_session) opportunity = seed_opportunity(api_session, funder=funder) source = seed_source(api_session, funder_short_code=funder.short_code) @@ -570,15 +570,15 @@ def test_create_proposal_link_and_round_trip(self, api_session): application_form_field_id = application_form.fields[0]["id"] changemaker = seed_changemaker(api_session) - proposal = create_changemaker_proposal( + proposal = create_proposal( api_session, data={ "opportunityId": opportunity.id, - "externalId": f"inttest-{uuid.uuid4().hex[:8]}", + "externalId": f"inttest-{suffix}", }, ) - proposal_version = create_changemaker_proposal_version( + proposal_version = create_proposal_version( api_session, proposal_version=NewProposalVersion( proposal_id=proposal.id, diff --git a/integration_tests/test_permission_grants.py b/integration_tests/test_permission_grants.py index 237bcb5..3908e04 100644 --- a/integration_tests/test_permission_grants.py +++ b/integration_tests/test_permission_grants.py @@ -92,7 +92,7 @@ def test_create_get_update_delete_roundtrip(self, api_session, seeded_pdc_stack, grantee_org_id=grantee_org_id, ) - created = create_permission_grant(api_session, spec=spec) + created = create_permission_grant(api_session, grant=spec) created_grant_ids.append(created.id) assert isinstance(created, ChangemakerPermissionGrant) @@ -109,7 +109,7 @@ def test_create_get_update_delete_roundtrip(self, api_session, seeded_pdc_stack, grantee_org_id=grantee_org_id, ) update_spec.verbs = [PermissionGrantVerb.VIEW, PermissionGrantVerb.EDIT] - updated = update_permission_grant(api_session, grant_id=created.id, spec=update_spec) + updated = update_permission_grant(api_session, grant_id=created.id, grant=update_spec) assert set(updated.verbs) == {PermissionGrantVerb.VIEW, PermissionGrantVerb.EDIT} delete_permission_grant(api_session, grant_id=created.id) @@ -123,7 +123,7 @@ def test_list_includes_created_grant(self, api_session, seeded_pdc_stack, grante changemaker_id=seeded_pdc_stack.changemaker.id, grantee_org_id=grantee_org_id, ) - created = create_permission_grant(api_session, spec=spec) + created = create_permission_grant(api_session, grant=spec) created_grant_ids.append(created.id) bundle = list_permission_grants(api_session, count_per_page=200) @@ -141,7 +141,7 @@ def test_authenticated_users_changemaker_grant_roundtrips(self, api_session, see ], ) - created = create_permission_grant(api_session, spec=spec) + created = create_permission_grant(api_session, grant=spec) assert created.id is not None created_grant_ids.append(created.id) @@ -173,7 +173,7 @@ def test_proposal_context_grant_with_conditions( ), ) - created = create_permission_grant(api_session, spec=spec) + created = create_permission_grant(api_session, grant=spec) created_grant_ids.append(created.id) assert isinstance(created, ProposalPermissionGrant) @@ -189,21 +189,23 @@ def test_changemaker_filter_scopes_results_server_side(self, api_session, seeded match = create_permission_grant( api_session, - spec=_changemaker_view_spec(changemaker_id=changemaker_b.id, grantee_org_id=uuid.uuid4()), + grant=_changemaker_view_spec(changemaker_id=changemaker_b.id, grantee_org_id=uuid.uuid4()), ) created_grant_ids.append(match.id) other_changemaker = create_permission_grant( api_session, - spec=_changemaker_view_spec(changemaker_id=seeded_pdc_stack.changemaker.id, grantee_org_id=uuid.uuid4()), + grant=_changemaker_view_spec(changemaker_id=seeded_pdc_stack.changemaker.id, grantee_org_id=uuid.uuid4()), ) created_grant_ids.append(other_changemaker.id) non_changemaker = create_permission_grant( api_session, - spec=_funder_manage_spec(funder_short_code=seeded_pdc_stack.funder.short_code, grantee_org_id=uuid.uuid4()), + grant=_funder_manage_spec( + funder_short_code=seeded_pdc_stack.funder.short_code, grantee_org_id=uuid.uuid4() + ), ) created_grant_ids.append(non_changemaker.id) - bundle = list_permission_grants(api_session, changemaker=changemaker_b.id, count_per_page=200) + bundle = list_permission_grants(api_session, changemaker_id=changemaker_b.id, count_per_page=200) returned_ids = {entry.id for entry in bundle.entries} assert match.id in returned_ids @@ -219,21 +221,23 @@ def test_funder_filter_scopes_results_server_side(self, api_session, seeded_pdc_ match = create_permission_grant( api_session, - spec=_funder_manage_spec(funder_short_code=funder_b.short_code, grantee_org_id=uuid.uuid4()), + grant=_funder_manage_spec(funder_short_code=funder_b.short_code, grantee_org_id=uuid.uuid4()), ) created_grant_ids.append(match.id) other_funder = create_permission_grant( api_session, - spec=_funder_manage_spec(funder_short_code=seeded_pdc_stack.funder.short_code, grantee_org_id=uuid.uuid4()), + grant=_funder_manage_spec( + funder_short_code=seeded_pdc_stack.funder.short_code, grantee_org_id=uuid.uuid4() + ), ) created_grant_ids.append(other_funder.id) non_funder = create_permission_grant( api_session, - spec=_changemaker_view_spec(changemaker_id=seeded_pdc_stack.changemaker.id, grantee_org_id=uuid.uuid4()), + grant=_changemaker_view_spec(changemaker_id=seeded_pdc_stack.changemaker.id, grantee_org_id=uuid.uuid4()), ) created_grant_ids.append(non_funder.id) - bundle = list_permission_grants(api_session, funder=funder_b.short_code, count_per_page=200) + bundle = list_permission_grants(api_session, funder_short_code=funder_b.short_code, count_per_page=200) returned_ids = {entry.id for entry in bundle.entries} assert match.id in returned_ids @@ -249,21 +253,21 @@ def test_proposal_filter_scopes_results_server_side(self, api_session, seeded_pd match = create_permission_grant( api_session, - spec=_proposal_view_spec(proposal_id=proposal_b.id, grantee_org_id=uuid.uuid4()), + grant=_proposal_view_spec(proposal_id=proposal_b.id, grantee_org_id=uuid.uuid4()), ) created_grant_ids.append(match.id) other_proposal = create_permission_grant( api_session, - spec=_proposal_view_spec(proposal_id=seeded_pdc_stack.proposal.id, grantee_org_id=uuid.uuid4()), + grant=_proposal_view_spec(proposal_id=seeded_pdc_stack.proposal.id, grantee_org_id=uuid.uuid4()), ) created_grant_ids.append(other_proposal.id) non_proposal = create_permission_grant( api_session, - spec=_changemaker_view_spec(changemaker_id=seeded_pdc_stack.changemaker.id, grantee_org_id=uuid.uuid4()), + grant=_changemaker_view_spec(changemaker_id=seeded_pdc_stack.changemaker.id, grantee_org_id=uuid.uuid4()), ) created_grant_ids.append(non_proposal.id) - bundle = list_permission_grants(api_session, proposal=proposal_b.id, count_per_page=200) + bundle = list_permission_grants(api_session, proposal_id=proposal_b.id, count_per_page=200) returned_ids = {entry.id for entry in bundle.entries} assert match.id in returned_ids @@ -278,12 +282,12 @@ def test_verb_filter_scopes_results_server_side(self, api_session, seeded_pdc_st changemaker_id = seeded_pdc_stack.changemaker.id manage_grant = create_permission_grant( api_session, - spec=_changemaker_manage_spec(changemaker_id=changemaker_id, grantee_org_id=uuid.uuid4()), + grant=_changemaker_manage_spec(changemaker_id=changemaker_id, grantee_org_id=uuid.uuid4()), ) created_grant_ids.append(manage_grant.id) view_grant = create_permission_grant( api_session, - spec=_changemaker_view_spec(changemaker_id=changemaker_id, grantee_org_id=uuid.uuid4()), + grant=_changemaker_view_spec(changemaker_id=changemaker_id, grantee_org_id=uuid.uuid4()), ) created_grant_ids.append(view_grant.id) @@ -298,7 +302,7 @@ def test_grantee_type_filter_scopes_results_server_side(self, api_session, seede changemaker_id = seeded_pdc_stack.changemaker.id authenticated_users_grant = create_permission_grant( api_session, - spec=ChangemakerPermissionGrant( + grant=ChangemakerPermissionGrant( changemaker_id=changemaker_id, grantee_type=PermissionGrantGranteeType.AUTHENTICATED_USERS, verbs=[PermissionGrantVerb.VIEW], @@ -311,7 +315,7 @@ def test_grantee_type_filter_scopes_results_server_side(self, api_session, seede created_grant_ids.append(authenticated_users_grant.id) user_group_grant = create_permission_grant( api_session, - spec=_changemaker_view_spec(changemaker_id=changemaker_id, grantee_org_id=uuid.uuid4()), + grant=_changemaker_view_spec(changemaker_id=changemaker_id, grantee_org_id=uuid.uuid4()), ) created_grant_ids.append(user_group_grant.id) diff --git a/integration_tests/test_preprocess_rules.py b/integration_tests/test_preprocess_rules.py index c10d5d5..4ef0353 100644 --- a/integration_tests/test_preprocess_rules.py +++ b/integration_tests/test_preprocess_rules.py @@ -1,7 +1,3 @@ -import uuid - -import pytest - from pdc_api_client.client import get_changemaker, get_organization_by_keycloak_id from pdc_api_client.models import Source from pdc_api_client.seeding.pdc_entities import ( @@ -12,23 +8,11 @@ ) -@pytest.fixture -def fresh_kc_org(keycloak_admin): - suffix = uuid.uuid4().hex[:8] - alias = f"preprocess-{suffix}" - return keycloak_admin.ensure_organization( - name=f"Preprocess Test {suffix}", - alias=alias, - domain=f"{alias}.example.com", - ) - - -class TestOrganizationRoles: - # Covers codegen/preprocess.py::relax_organization_nullable_roles. - def test_single_role_org_has_null_unfilled_roles(self, api_session, fresh_kc_org): - seed_funder(api_session, keycloak_organization_id=fresh_kc_org.id) +class TestRelaxOrganizationNullableRoles: + def test_single_role_org_has_null_unfilled_roles(self, api_session, fresh_keycloak_org): + seed_funder(api_session, keycloak_organization_id=fresh_keycloak_org.id) - org = get_organization_by_keycloak_id(api_session, keycloak_id=fresh_kc_org.id) + org = get_organization_by_keycloak_id(api_session, keycloak_id=fresh_keycloak_org.id) assert org.funder is not None assert org.changemaker is None @@ -36,7 +20,6 @@ def test_single_role_org_has_null_unfilled_roles(self, api_session, fresh_kc_org class TestFlattenSource: - # Covers codegen/preprocess.py::flatten_source. def test_funder_source_exposes_flat_funder_field(self, api_session): funder = seed_funder(api_session) source = seed_source(api_session, funder_short_code=funder.short_code) @@ -55,17 +38,20 @@ def test_changemaker_source_exposes_flat_changemaker_fields(self, api_session): class TestDefaultArraysToEmpty: - # Covers codegen/preprocess.py::default_arrays_to_empty. def test_fiscal_sponsors_omitted_for_caller_without_view_is_empty( - self, api_session, keycloak_admin, keycloak_client_id, test_user_password, fresh_kc_org, session_narrowed_to + self, + api_session, + add_user_to_org, + fresh_keycloak_org, + session_narrowed_to, ): sponsee = seed_changemaker(api_session) seed_fiscal_sponsorship(api_session, sponsee=sponsee, sponsor=seed_changemaker(api_session)) - keycloak_admin.ensure_direct_grants_enabled(client_id=keycloak_client_id) - username = f"{fresh_kc_org.alias}@example.com" - user = keycloak_admin.ensure_user(username=username, email=username, password=test_user_password) - keycloak_admin.ensure_user_in_organization(organization_id=fresh_kc_org.id, user_id=user.id) + username = f"{fresh_keycloak_org.alias}@example.com" + add_user_to_org(username, fresh_keycloak_org) - changemaker = get_changemaker(session_narrowed_to(username, fresh_kc_org.alias), changemaker_id=sponsee.id) + changemaker = get_changemaker( + session_narrowed_to(username, fresh_keycloak_org.alias), changemaker_id=sponsee.id + ) assert changemaker.fiscal_sponsors == [] diff --git a/just/pdc.just b/just/pdc.just index 8ab9c9a..91a5b40 100644 --- a/just/pdc.just +++ b/just/pdc.just @@ -1,4 +1,4 @@ -# Runs upstream's CI stack (PDC API, Keycloak, Postgres, S3), refetched from PDC's main branch on every `up`. +# Runs PDC's CI stack (PDC API, Keycloak, Postgres, S3), refetched from PDC's main branch on every `up`. set working-directory := '..' @@ -9,7 +9,7 @@ compose := 'docker compose -f ' + cache + '/compose-ci.yml' _default: @just --list pdc --unsorted -# Fetch upstream's compose file and Keycloak realm +# Fetch PDC's compose file and Keycloak realm fetch: mkdir -p {{cache}}/keycloak curl -fsSL {{base}}/compose-ci.yml -o {{cache}}/compose-ci.yml @@ -21,13 +21,13 @@ up: fetch {{compose}} up -d --wait # Follow the stack's logs -logs: fetch +logs: {{compose}} logs -f # Stop the stack, keeping its data -down: fetch +down: {{compose}} down --remove-orphans # Stop the stack and delete its data -reset: fetch +reset: {{compose}} down --remove-orphans --volumes diff --git a/justfile b/justfile index 53f3ea8..3ce944b 100644 --- a/justfile +++ b/justfile @@ -45,52 +45,47 @@ typecheck python=python lowest_direct="false": typecheck-mypy python=python lowest_direct="false": #!/usr/bin/env bash status=0 - run="uv run --python={{python}} {{ if lowest_direct == "true" { "--resolution=lowest-direct" } else { "" } }} --isolated --no-dev --group tests --group typecheck --" - $run mypy --python-version={{python}} || status=1 - $run mypy --python-version={{python}} --strict src || status=1 + {{just_executable()}} _typecheck {{python}} {{lowest_direct}} mypy || status=1 + {{just_executable()}} _typecheck {{python}} {{lowest_direct}} mypy --strict src || status=1 exit $status -# Type check the package and tests with pyrefly (passes through args) +# Type check the package and tests with pyrefly [group('dev')] [arg("python", long)] [arg("lowest_direct", long="lowest-direct", value="true")] -typecheck-pyrefly python=python lowest_direct="false" *args: - uv run --python={{python}} {{ if lowest_direct == "true" { "--resolution=lowest-direct" } else { "" } }} --isolated --no-dev --group tests --group typecheck -- \ - pyrefly check --python-version={{python}} {{args}} +typecheck-pyrefly python=python lowest_direct="false" *args: (_typecheck python lowest_direct "pyrefly check" args) -# Type check the package and tests with ty (passes through args) +# Type check the package and tests with ty [group('dev')] [arg("python", long)] [arg("lowest_direct", long="lowest-direct", value="true")] -typecheck-ty python=python lowest_direct="false" *args: - uv run --python={{python}} {{ if lowest_direct == "true" { "--resolution=lowest-direct" } else { "" } }} --isolated --no-dev --group tests --group typecheck -- \ - ty check --python-version={{python}} {{args}} +typecheck-ty python=python lowest_direct="false" *args: (_typecheck python lowest_direct "ty check" args) # Run the unit tests against the installed package [group('test')] [arg("python", long)] [arg("lowest_direct", long="lowest-direct", value="true")] [arg("upgrade", long, value="true")] -test python=python lowest_direct="false" upgrade="false" *args: - @if [ "{{upgrade}}" = "true" ]; then uv lock --upgrade --dry-run; fi - uv run --python={{python}} \ - {{ if lowest_direct == "true" { "--resolution=lowest-direct" } else { "" } }} \ - {{ if upgrade == "true" { "--upgrade" } else { "" } }} \ - --isolated --no-editable --no-dev --group tests --reinstall -- \ - python -I -m pytest {{args}} +test python=python lowest_direct="false" upgrade="false" *args: (_pytest python lowest_direct upgrade args) # Run the integration tests against the installed package and a live PDC stack (run `just pdc up` first) [group('test')] [arg("python", long)] [arg("lowest_direct", long="lowest-direct", value="true")] [arg("upgrade", long, value="true")] -integration python=python lowest_direct="false" upgrade="false" *args: +integration python=python lowest_direct="false" upgrade="false" *args: (_pytest python lowest_direct upgrade "-c integration_tests/pytest.ini integration_tests/ " + args) + +_typecheck python lowest_direct tool *args: + uv run --python={{python}} {{ if lowest_direct == "true" { "--resolution=lowest-direct" } else { "" } }} --isolated --no-dev --group tests --group typecheck -- \ + {{tool}} --python-version={{python}} {{args}} + +_pytest python lowest_direct upgrade *args: @if [ "{{upgrade}}" = "true" ]; then uv lock --upgrade --dry-run; fi uv run --python={{python}} \ {{ if lowest_direct == "true" { "--resolution=lowest-direct" } else { "" } }} \ {{ if upgrade == "true" { "--upgrade" } else { "" } }} \ --isolated --no-editable --no-dev --group tests --reinstall -- \ - python -I -m pytest -c integration_tests/pytest.ini integration_tests/ {{args}} + python -I -m pytest {{args}} # Run lint, type checks, unit tests, and integration tests (run `just pdc up` first) [group('test')] diff --git a/src/pdc_api_client/__init__.py b/src/pdc_api_client/__init__.py index 5bc8289..2a6c68e 100644 --- a/src/pdc_api_client/__init__.py +++ b/src/pdc_api_client/__init__.py @@ -1,5 +1,6 @@ from pdc_api_client.errors import ( ApplicationFormNotFoundError, + ChangemakerFieldValueBatchNotFoundError, ChangemakerNotFoundError, FunderNotFoundError, PDCBadRequestError, @@ -11,6 +12,7 @@ PDCUnauthorizedError, PDCUnprocessableEntityError, ProposalNotFoundError, + TokenRequestError, ) from pdc_api_client.token import TokenProvider from pdc_api_client.transport import APISession @@ -18,6 +20,7 @@ __all__ = [ "APISession", "ApplicationFormNotFoundError", + "ChangemakerFieldValueBatchNotFoundError", "ChangemakerNotFoundError", "FunderNotFoundError", "PDCBadRequestError", @@ -30,4 +33,5 @@ "PDCUnprocessableEntityError", "ProposalNotFoundError", "TokenProvider", + "TokenRequestError", ] diff --git a/src/pdc_api_client/client.py b/src/pdc_api_client/client.py index d251532..4e6d089 100644 --- a/src/pdc_api_client/client.py +++ b/src/pdc_api_client/client.py @@ -56,28 +56,28 @@ def _iterate_pages( break -def get_basefields(session: APISession) -> list[BaseField]: +def get_base_fields(session: APISession) -> list[BaseField]: return [ BaseField.model_validate(entry) - for payload in _iterate_pages(session, endpoint="/baseFields", count_per_page=10_000) + for payload in _iterate_pages(session, endpoint="baseFields", count_per_page=10_000) for entry in payload["entries"] ] def get_changemakers(session: APISession) -> Generator[Changemaker, None, None]: - for payload in _iterate_pages(session, endpoint="/changemakers"): + for payload in _iterate_pages(session, endpoint="changemakers"): for entry in payload["entries"]: yield Changemaker.model_validate(entry) def get_funders(session: APISession) -> Generator[Funder, None, None]: - for payload in _iterate_pages(session, endpoint="/funders"): + for payload in _iterate_pages(session, endpoint="funders"): for entry in payload["entries"]: yield Funder.model_validate(entry) def get_data_providers(session: APISession) -> Generator[DataProvider, None, None]: - for payload in _iterate_pages(session, endpoint="/dataProviders"): + for payload in _iterate_pages(session, endpoint="dataProviders"): for entry in payload["entries"]: yield DataProvider.model_validate(entry) @@ -89,7 +89,7 @@ def get_changemaker_proposals( proposal_id: int | None = None, count_per_page: int | None = None, ) -> Generator[ChangemakerProposal, None, None]: - endpoint = "/changemakerProposals" + endpoint = "changemakerProposals" query_params = {} if changemaker_id is not None: query_params["changemaker"] = changemaker_id @@ -103,14 +103,14 @@ def get_changemaker_proposals( def get_proposals(session: APISession, *, count_per_page: int = 50) -> Generator[Proposal, None, None]: - for payload in _iterate_pages(session, endpoint="/proposals", count_per_page=count_per_page): + for payload in _iterate_pages(session, endpoint="proposals", count_per_page=count_per_page): for entry in payload["entries"]: yield Proposal.model_validate(entry) def get_initiatives(session: APISession, *, changemaker_id: int | None = None) -> Generator[Initiative, None, None]: params = {} if changemaker_id is None else {"changemaker": changemaker_id} - for payload in _iterate_pages(session, endpoint="/initiatives", params=params): + for payload in _iterate_pages(session, endpoint="initiatives", params=params): for entry in payload["entries"]: yield Initiative.model_validate(entry) @@ -123,7 +123,7 @@ def get_initiative(session: APISession, *, initiative_id: int) -> Initiative: def get_initiative_field_values( session: APISession, *, initiative_id: int ) -> Generator[InitiativeFieldValue, None, None]: - for payload in _iterate_pages(session, endpoint=f"/initiatives/{initiative_id}/fieldValues"): + for payload in _iterate_pages(session, endpoint=f"initiatives/{initiative_id}/fieldValues"): for entry in payload["entries"]: yield InitiativeFieldValue.model_validate(entry) @@ -167,27 +167,25 @@ def get_organization_by_keycloak_id(session: APISession, *, keycloak_id: str) -> return Organization.model_validate(response.json()) -def get_application_form(session: APISession, *, form_id: int) -> ChangemakerApplicationForm: - response = session.get(f"applicationForms/{form_id}") +def get_application_form(session: APISession, *, application_form_id: int) -> ChangemakerApplicationForm: + response = session.get(f"applicationForms/{application_form_id}") return ChangemakerApplicationForm.model_validate(response.json()) def get_application_form_by_name(session: APISession, *, name: str) -> ChangemakerApplicationForm: - for payload in _iterate_pages(session, endpoint="/applicationForms"): + for payload in _iterate_pages(session, endpoint="applicationForms"): for entry in payload["entries"]: if entry["name"] == name: return ChangemakerApplicationForm.model_validate(entry) raise errors.ApplicationFormNotFoundError(detail=f"No application form named {name!r}") -def create_changemaker_proposal(session: APISession, *, data: dict[str, Any]) -> Proposal: +def create_proposal(session: APISession, *, data: dict[str, Any]) -> Proposal: response = session.post("proposals", json=data) return Proposal.model_validate(response.json()) -def create_changemaker_proposal_version( - session: APISession, *, proposal_version: NewProposalVersion -) -> ProposalVersion: +def create_proposal_version(session: APISession, *, proposal_version: NewProposalVersion) -> ProposalVersion: response = session.post("proposalVersions", json=json.loads(proposal_version.model_dump_json(by_alias=True))) return ProposalVersion.model_validate(response.json()) @@ -202,14 +200,14 @@ def get_changemaker_proposal(session: APISession, *, proposal_id: int) -> Change try: return ChangemakerProposal.model_validate(response.json()["entries"][0]) except (KeyError, IndexError): - raise errors.ProposalNotFoundError + raise errors.ProposalNotFoundError(detail=f"No changemaker linked to proposal {proposal_id}") from None def get_proposal(session: APISession, *, proposal_id: int) -> Proposal: try: response = session.get(f"proposals/{proposal_id}") - except errors.PDCNotFoundError: - raise errors.ProposalNotFoundError + except errors.PDCNotFoundError as exc: + raise errors.ProposalNotFoundError(detail=f"No proposal with id {proposal_id}", code=exc.code) from exc return Proposal.model_validate(response.json()) @@ -217,8 +215,8 @@ def get_changemaker(session: APISession, *, changemaker_id: int) -> Changemaker: try: response = session.get(f"changemakers/{changemaker_id}") return Changemaker.model_validate(response.json()) - except errors.PDCNotFoundError: - raise errors.ChangemakerNotFoundError + except errors.PDCNotFoundError as exc: + raise errors.ChangemakerNotFoundError(detail=f"No changemaker with id {changemaker_id}", code=exc.code) from exc def get_changemaker_field_values( @@ -232,7 +230,7 @@ def get_changemaker_field_values( params["changemaker"] = changemaker_id if batch_id is not None: params["changemakerFieldValueBatch"] = batch_id - for payload in _iterate_pages(session, endpoint="/changemakerFieldValues", params=params): + for payload in _iterate_pages(session, endpoint="changemakerFieldValues", params=params): for entry in payload["entries"]: yield ChangemakerFieldValue.model_validate(entry) @@ -240,8 +238,10 @@ def get_changemaker_field_values( def get_changemaker_field_value_batch(session: APISession, *, batch_id: int) -> ChangemakerFieldValueBatch: try: response = session.get(f"changemakerFieldValueBatches/{batch_id}") - except errors.PDCNotFoundError: - raise errors.ChangemakerFieldValueBatchNotFoundError + except errors.PDCNotFoundError as exc: + raise errors.ChangemakerFieldValueBatchNotFoundError( + detail=f"No changemaker field value batch with id {batch_id}", code=exc.code + ) from exc return ChangemakerFieldValueBatch.model_validate(response.json()) @@ -283,7 +283,7 @@ def get_funder(session: APISession, *, short_code: str) -> Funder: try: response = session.get(f"funders/{short_code}") except errors.PDCNotFoundError as exc: - raise errors.FunderNotFoundError(short_code) from exc + raise errors.FunderNotFoundError(detail=f"No funder with short code {short_code!r}", code=exc.code) from exc return Funder.model_validate(response.json()) @@ -315,9 +315,9 @@ def get_bundle_page[BundleT: PageableBundle]( return bundle_type.model_validate(response.json()) -def register_file_upload(session: APISession, *, name: str, mime_type: str, size: int) -> File: +def create_file(session: APISession, *, name: str, mime_type: str, size: int) -> File: response = session.post("files", json={"name": name, "mimeType": mime_type, "size": size}) - return File(**response.json()) + return File.model_validate(response.json()) def upload_file_to_s3_presigned_url( @@ -329,4 +329,4 @@ def upload_file_to_s3_presigned_url( response = requests.post(presigned_post.url, data=fields, files=files, timeout=120) response.raise_for_status() except requests.RequestException as exc: - raise errors.PDCServiceError(detail=f"Failed to upload file to S3: {exc}") from exc + raise errors.PDCServiceError(detail=f"Could not upload file to S3: {exc}") from exc diff --git a/src/pdc_api_client/errors.py b/src/pdc_api_client/errors.py index fd11607..699e72c 100644 --- a/src/pdc_api_client/errors.py +++ b/src/pdc_api_client/errors.py @@ -41,19 +41,19 @@ class TokenRequestError(PDCException): pass -class ProposalNotFoundError(Exception): +class ProposalNotFoundError(PDCNotFoundError): pass -class ChangemakerNotFoundError(Exception): +class ChangemakerNotFoundError(PDCNotFoundError): pass -class ChangemakerFieldValueBatchNotFoundError(Exception): +class ChangemakerFieldValueBatchNotFoundError(PDCNotFoundError): pass -class FunderNotFoundError(Exception): +class FunderNotFoundError(PDCNotFoundError): pass diff --git a/src/pdc_api_client/permission_grants.py b/src/pdc_api_client/permission_grants.py index 00d4f05..fd9d722 100644 --- a/src/pdc_api_client/permission_grants.py +++ b/src/pdc_api_client/permission_grants.py @@ -225,9 +225,9 @@ def _validate_grant(payload: dict[str, Any]) -> PermissionGrant: return _PERMISSION_GRANT_ADAPTER.validate_python(payload) -def _grant_to_payload(spec: PermissionGrant | DefaultPermissionGrant) -> dict[str, Any]: +def _grant_to_payload(grant: PermissionGrant | DefaultPermissionGrant) -> dict[str, Any]: # PDC rejects server-managed fields and the unset grantee key in write bodies. - return spec.model_dump(mode="json", by_alias=True, exclude={"id", "created_by", "created_at"}, exclude_none=True) + return grant.model_dump(mode="json", by_alias=True, exclude={"id", "created_by", "created_at"}, exclude_none=True) def list_permission_grants( @@ -235,19 +235,19 @@ def list_permission_grants( *, page: int = 1, count_per_page: int | None = None, - changemaker: int | None = None, - funder: str | None = None, - proposal: int | None = None, + changemaker_id: int | None = None, + funder_short_code: str | None = None, + proposal_id: int | None = None, grantee_type: PermissionGrantGranteeType | None = None, verb: PermissionGrantVerb | None = None, ) -> PermissionGrantBundle: extra_params: dict[str, Any] = {} - if changemaker is not None: - extra_params["changemaker"] = changemaker - if funder is not None: - extra_params["funder"] = funder - if proposal is not None: - extra_params["proposal"] = proposal + if changemaker_id is not None: + extra_params["changemaker"] = changemaker_id + if funder_short_code is not None: + extra_params["funder"] = funder_short_code + if proposal_id is not None: + extra_params["proposal"] = proposal_id if grantee_type is not None: extra_params["granteeType"] = grantee_type.value if verb is not None: @@ -266,13 +266,13 @@ def get_permission_grant(session: APISession, *, grant_id: int) -> PermissionGra return _validate_grant(response.json()) -def create_permission_grant(session: APISession, *, spec: PermissionGrant) -> PermissionGrant: - response = session.post(_ENDPOINT, json=_grant_to_payload(spec)) +def create_permission_grant(session: APISession, *, grant: PermissionGrant) -> PermissionGrant: + response = session.post(_ENDPOINT, json=_grant_to_payload(grant)) return _validate_grant(response.json()) -def update_permission_grant(session: APISession, *, grant_id: int, spec: PermissionGrant) -> PermissionGrant: - response = session.put(f"{_ENDPOINT}/{grant_id}", json=_grant_to_payload(spec)) +def update_permission_grant(session: APISession, *, grant_id: int, grant: PermissionGrant) -> PermissionGrant: + response = session.put(f"{_ENDPOINT}/{grant_id}", json=_grant_to_payload(grant)) return _validate_grant(response.json()) @@ -291,15 +291,15 @@ def get_default_permission_grant(session: APISession, *, grant_id: int) -> Defau return DefaultPermissionGrant.model_validate(response.json()) -def create_default_permission_grant(session: APISession, *, spec: DefaultPermissionGrant) -> DefaultPermissionGrant: - response = session.post(_DEFAULT_ENDPOINT, json=_grant_to_payload(spec)) +def create_default_permission_grant(session: APISession, *, grant: DefaultPermissionGrant) -> DefaultPermissionGrant: + response = session.post(_DEFAULT_ENDPOINT, json=_grant_to_payload(grant)) return DefaultPermissionGrant.model_validate(response.json()) def update_default_permission_grant( - session: APISession, *, grant_id: int, spec: DefaultPermissionGrant + session: APISession, *, grant_id: int, grant: DefaultPermissionGrant ) -> DefaultPermissionGrant: - response = session.put(f"{_DEFAULT_ENDPOINT}/{grant_id}", json=_grant_to_payload(spec)) + response = session.put(f"{_DEFAULT_ENDPOINT}/{grant_id}", json=_grant_to_payload(grant)) return DefaultPermissionGrant.model_validate(response.json()) diff --git a/src/pdc_api_client/seeding/keycloak_admin.py b/src/pdc_api_client/seeding/keycloak_admin.py index 16e6967..88ae217 100644 --- a/src/pdc_api_client/seeding/keycloak_admin.py +++ b/src/pdc_api_client/seeding/keycloak_admin.py @@ -27,7 +27,7 @@ def __init__( realm: str, admin_username: str, admin_password: str, - ): + ) -> None: # The bootstrap admin lives in the master realm and can administer any realm. self._admin = _KeycloakAdminClient( server_url=keycloak_url.rstrip("/") + "/", @@ -40,9 +40,9 @@ def __init__( self.realm = realm def ensure_organization(self, *, name: str, alias: str, domain: str) -> KeycloakOrganization: - existing = self._find_org_by_alias(alias) + existing = self.get_organization(alias=alias) if existing is not None: - return KeycloakOrganization(id=existing["id"], alias=existing["alias"], name=existing["name"]) + return existing org_id = self._admin.create_organization( {"name": name, "alias": alias, "domains": [{"name": domain, "verified": False}]} ) @@ -132,7 +132,7 @@ def assign_realm_role(self, *, user_id: str, role_name: str) -> bool: role = self._admin.get_realm_role(role_name) except KeycloakGetError as exc: if exc.response_code == 404: - raise LookupError(f"Realm role {role_name!r} not found in realm {self.realm!r}") from exc + raise LookupError(f"Keycloak realm role {role_name!r} not found in realm {self.realm!r}") from exc raise self._admin.assign_realm_roles(user_id, [{"id": role["id"], "name": role["name"]}]) return True diff --git a/src/pdc_api_client/seeding/pdc_entities.py b/src/pdc_api_client/seeding/pdc_entities.py index 6ab9d71..1429ed8 100644 --- a/src/pdc_api_client/seeding/pdc_entities.py +++ b/src/pdc_api_client/seeding/pdc_entities.py @@ -14,6 +14,7 @@ get_bundle_page, get_changemaker_field_values, get_organization_by_keycloak_id, + link_changemaker_to_proposal, ) from pdc_api_client.errors import PDCNotFoundError from pdc_api_client.models import ( @@ -26,6 +27,7 @@ Initiative, InitiativeFieldValue, Opportunity, + Organization, Proposal, ProposalVersion, ShallowChangemaker, @@ -121,7 +123,7 @@ def seed_base_field( short_code = short_code or _token("field") label = label or f"Test Field {short_code}" response = session.put( - f"/baseFields/{short_code}", + f"baseFields/{short_code}", json={ "label": label, "description": f"Integration test base field: {short_code}", @@ -168,7 +170,7 @@ def seed_funder( body: dict[str, Any] = {"name": name, "isCollaborative": is_collaborative} if keycloak_organization_id is not None: body["keycloakOrganizationId"] = keycloak_organization_id - response = session.put(f"/funders/{short_code}", json=body) + response = session.put(f"funders/{short_code}", json=body) return Funder.model_validate(response.json()) @@ -179,7 +181,7 @@ def seed_funder_collaborative_member( member: Funder, ) -> dict[str, Any]: response = session.post( - f"/funders/{collaborative.short_code}/members/{member.short_code}", + f"funders/{collaborative.short_code}/members/{member.short_code}", ) return _json_object(response) @@ -238,7 +240,7 @@ def seed_changemaker( body: dict[str, Any] = {"name": name, "taxId": tax_id} if keycloak_organization_id is not None: body["keycloakOrganizationId"] = keycloak_organization_id - response = session.post("/changemakers", json=body) + response = session.post("changemakers", json=body) return ShallowChangemaker.model_validate(response.json()) @@ -266,7 +268,7 @@ def seed_opportunity( funder = seed_funder(session) title = title or f"{fake.catch_phrase()} Grant" response = session.post( - "/opportunities", + "opportunities", json={"title": title, "funderShortCode": funder.short_code}, ) return Opportunity.model_validate(response.json()) @@ -284,69 +286,67 @@ def seed_data_provider( body: dict[str, Any] = {"name": name} if keycloak_organization_id is not None: body["keycloakOrganizationId"] = keycloak_organization_id - response = session.put(f"/dataProviders/{short_code}", json=body) + response = session.put(f"dataProviders/{short_code}", json=body) return DataProvider.model_validate(response.json()) -def _kc_alias_slug(kc_org: KeycloakOrganization) -> str: +def _keycloak_alias_slug(keycloak_org: KeycloakOrganization) -> str: # Keycloak aliases are kebab-case; PDC short codes are snake_case. - return kc_org.alias.replace("-", "_") + return keycloak_org.alias.replace("-", "_") -def ensure_funder_for_kc_org( +def _find_organization(session: APISession, *, keycloak_org: KeycloakOrganization) -> Organization | None: + try: + return get_organization_by_keycloak_id(session, keycloak_id=keycloak_org.id) + except PDCNotFoundError: + return None + + +def ensure_funder_for_keycloak_org( session: APISession, *, - kc_org: KeycloakOrganization, + keycloak_org: KeycloakOrganization, name: str, is_collaborative: bool = False, ) -> Funder: - try: - org = get_organization_by_keycloak_id(session, keycloak_id=kc_org.id) - if org.funder is not None: - return org.funder - except PDCNotFoundError: - pass + org = _find_organization(session, keycloak_org=keycloak_org) + if org is not None and org.funder is not None: + return org.funder return seed_funder( session, - short_code=f"{_kc_alias_slug(kc_org)}_funder", + short_code=f"{_keycloak_alias_slug(keycloak_org)}_funder", name=name, is_collaborative=is_collaborative, - keycloak_organization_id=kc_org.id, + keycloak_organization_id=keycloak_org.id, ) -def ensure_changemaker_for_kc_org( +def ensure_changemaker_for_keycloak_org( session: APISession, *, - kc_org: KeycloakOrganization, + keycloak_org: KeycloakOrganization, name: str, ) -> ShallowChangemaker: - try: - org = get_organization_by_keycloak_id(session, keycloak_id=kc_org.id) - if org.changemaker is not None: - return org.changemaker - except PDCNotFoundError: - pass - return seed_changemaker(session, name=name, keycloak_organization_id=kc_org.id) + org = _find_organization(session, keycloak_org=keycloak_org) + if org is not None and org.changemaker is not None: + return org.changemaker + return seed_changemaker(session, name=name, keycloak_organization_id=keycloak_org.id) -def ensure_data_provider_for_kc_org( +def ensure_data_provider_for_keycloak_org( session: APISession, *, - kc_org: KeycloakOrganization, + keycloak_org: KeycloakOrganization, name: str, ) -> DataProvider: - try: - org = get_organization_by_keycloak_id(session, keycloak_id=kc_org.id) - if org.data_provider is not None: - return org.data_provider - except PDCNotFoundError: - pass + org = _find_organization(session, keycloak_org=keycloak_org) + if org is not None and org.data_provider is not None: + return org.data_provider return seed_data_provider( session, - short_code=f"{_kc_alias_slug(kc_org)}_data_provider", + short_code=f"{_keycloak_alias_slug(keycloak_org)}_data_provider", name=name, - keycloak_organization_id=kc_org.id, + keycloak_organization_id=keycloak_org.id, ) @@ -361,7 +361,7 @@ def seed_fiscal_sponsorship( if not_after is not None: body["notAfter"] = not_after response = session.put( - f"/changemakers/{sponsee.id}/fiscalSponsors/{sponsor.id}", + f"changemakers/{sponsee.id}/fiscalSponsors/{sponsor.id}", json=body, ) return _json_object(response) @@ -374,7 +374,7 @@ def seed_funder_collaborative_invitation( invited: Funder, ) -> dict[str, Any]: response = session.post( - f"/funders/{collaborative.short_code}/invitations/sent/{invited.short_code}", + f"funders/{collaborative.short_code}/invitations/sent/{invited.short_code}", # PDC rejects a missing body even though no fields are required. json={}, ) @@ -389,7 +389,7 @@ def respond_to_funder_collaborative_invitation( status: str, ) -> dict[str, Any]: response = session.patch( - f"/funders/{invited.short_code}/invitations/received/{collaborative.short_code}", + f"funders/{invited.short_code}/invitations/received/{collaborative.short_code}", json={"invitationStatus": status}, ) return _json_object(response) @@ -414,7 +414,7 @@ def seed_source( else: funder = seed_funder(session) payload["funderShortCode"] = funder.short_code - response = session.post("/sources", json=payload) + response = session.post("sources", json=payload) return Source.model_validate(response.json()) @@ -461,7 +461,7 @@ def seed_application_form( for i, sc in enumerate(base_field_short_codes) ] response = session.post( - "/applicationForms", + "applicationForms", json={ "opportunityId": opportunity.id, "name": name or f"Test Application Form {uuid.uuid4().hex[:8]}", @@ -486,7 +486,7 @@ def seed_proposal( opportunity = seed_opportunity(session) external_id = external_id or _token("proposal") response = session.post( - "/proposals", + "proposals", json={ "opportunityId": opportunity.id, "externalId": external_id, @@ -504,7 +504,7 @@ def seed_proposal_version( field_values: list[dict[str, Any]] | None = None, ) -> ProposalVersion: response = session.post( - "/proposalVersions", + "proposalVersions", json={ "proposalId": proposal.id, "sourceId": source.id, @@ -515,22 +515,6 @@ def seed_proposal_version( return ProposalVersion.model_validate(response.json()) -def link_changemaker_to_proposal( - session: APISession, - *, - changemaker: ShallowChangemaker, - proposal: Proposal, -) -> dict[str, Any]: - response = session.post( - "/changemakerProposals", - json={ - "changemakerId": changemaker.id, - "proposalId": proposal.id, - }, - ) - return _json_object(response) - - _CHANGEMAKER_PROFILE_FIELDS: tuple[tuple[str, str, str], ...] = ( ("organization_legal_name", "Legal name", "string"), ("organization_website", "Website", "url"), @@ -601,39 +585,19 @@ def seed_changemaker_profile_fields( return SeededChangemakerProfile(changemaker=changemaker, source=source, batch=batch, field_values=field_values) -def seed_proposal_bundle( +def _seed_linked_proposal( session: APISession, *, - field_seeds: list[tuple[str, str, str]] | None = None, - changemaker_name: str | None = None, - field_sensitivity_classification: str = "public", + changemaker: ShallowChangemaker, + funder: Funder, + opportunity: Opportunity, + source: Source, + application_form: SeededApplicationForm, + field_seeds: list[tuple[str, str, str]], ) -> SeededProposalBundle: - # Base fields upsert by short_code across the whole stack, so a non-public sensitivity needs unique short codes. - if field_seeds is None: - field_seeds = make_proposal_field_seeds() - seeded_fields = [ - seed_base_field( - session, short_code=short_code, label=label, sensitivity_classification=field_sensitivity_classification - ) - for short_code, label, _ in field_seeds - ] - funder = seed_funder(session) - changemaker = seed_changemaker(session, name=changemaker_name) - opportunity = seed_opportunity(session, funder=funder) - - source = seed_source( - session, - funder_short_code=funder.short_code, - ) - application_form = seed_application_form( - session, - opportunity=opportunity, - base_field_short_codes=[bf.short_code for bf in seeded_fields], - ) form_field_id_by_short_code = { form_field["baseFieldShortCode"]: form_field["id"] for form_field in application_form.fields } - proposal = seed_proposal(session, opportunity=opportunity) proposal_version = seed_proposal_version( session, @@ -650,13 +614,7 @@ def seed_proposal_bundle( for position, (short_code, _, value) in enumerate(field_seeds, start=1) ], ) - - link_changemaker_to_proposal( - session, - changemaker=changemaker, - proposal=proposal, - ) - + link_changemaker_to_proposal(session, changemaker_id=changemaker.id, proposal_id=proposal.id) return SeededProposalBundle( changemaker=changemaker, funder=funder, @@ -668,6 +626,37 @@ def seed_proposal_bundle( ) +def seed_proposal_bundle( + session: APISession, + *, + field_seeds: list[tuple[str, str, str]] | None = None, + changemaker_name: str | None = None, + field_sensitivity_classification: str = "public", +) -> SeededProposalBundle: + # Base fields upsert by short_code across the whole stack, so a non-public sensitivity needs unique short codes. + if field_seeds is None: + field_seeds = make_proposal_field_seeds() + seeded_fields = [ + seed_base_field( + session, short_code=short_code, label=label, sensitivity_classification=field_sensitivity_classification + ) + for short_code, label, _ in field_seeds + ] + funder = seed_funder(session) + opportunity = seed_opportunity(session, funder=funder) + return _seed_linked_proposal( + session, + changemaker=seed_changemaker(session, name=changemaker_name), + funder=funder, + opportunity=opportunity, + source=seed_source(session, funder_short_code=funder.short_code), + application_form=seed_application_form( + session, opportunity=opportunity, base_field_short_codes=[bf.short_code for bf in seeded_fields] + ), + field_seeds=field_seeds, + ) + + def seed_changemaker_with_proposals( session: APISession, *, @@ -678,55 +667,29 @@ def seed_changemaker_with_proposals( ) -> SeededChangemakerWithProposals: if changemaker is None: changemaker = seed_changemaker(session, name=changemaker_name) - field_labels = make_proposal_field_seeds() seeded_fields = [ - seed_base_field(session, short_code=short_code, label=label) for short_code, label, _ in field_labels + seed_base_field(session, short_code=short_code, label=label) + for short_code, label, _ in make_proposal_field_seeds() ] if funder is None: funder = seed_funder(session) opportunity = seed_opportunity(session, funder=funder) source = seed_source(session, funder_short_code=funder.short_code) application_form = seed_application_form( - session, - opportunity=opportunity, - base_field_short_codes=[bf.short_code for bf in seeded_fields], + session, opportunity=opportunity, base_field_short_codes=[bf.short_code for bf in seeded_fields] ) - form_field_id_by_short_code = { - form_field["baseFieldShortCode"]: form_field["id"] for form_field in application_form.fields - } - - bundles: list[SeededProposalBundle] = [] - for i in range(proposal_count): - field_seeds = make_proposal_field_seeds(include_numbers=i % 2 == 0) - proposal = seed_proposal(session, opportunity=opportunity) - proposal_version = seed_proposal_version( + bundles = [ + _seed_linked_proposal( session, - proposal=proposal, + changemaker=changemaker, + funder=funder, + opportunity=opportunity, source=source, application_form=application_form, - field_values=[ - { - "applicationFormFieldId": form_field_id_by_short_code[short_code], - "position": position, - "value": value, - "goodAsOf": None, - } - for position, (short_code, _, value) in enumerate(field_seeds, start=1) - ], + field_seeds=make_proposal_field_seeds(include_numbers=i % 2 == 0), ) - link_changemaker_to_proposal(session, changemaker=changemaker, proposal=proposal) - bundles.append( - SeededProposalBundle( - changemaker=changemaker, - funder=funder, - opportunity=opportunity, - source=source, - application_form=application_form, - proposal=proposal, - proposal_version=proposal_version, - ) - ) - + for i in range(proposal_count) + ] return SeededChangemakerWithProposals(changemaker=changemaker, bundles=bundles) diff --git a/src/pdc_api_client/token.py b/src/pdc_api_client/token.py index 23751e2..e3ef9cf 100644 --- a/src/pdc_api_client/token.py +++ b/src/pdc_api_client/token.py @@ -66,6 +66,4 @@ def _request_token(self) -> tuple[str, float]: payload = response.json() return payload["access_token"], time.monotonic() + payload["expires_in"] except (ValueError, KeyError) as exc: - raise TokenRequestError( - detail="Malformed token response from auth server", code=response.status_code - ) from exc + raise TokenRequestError(detail="Malformed token response from PDC auth", code=response.status_code) from exc diff --git a/src/pdc_api_client/transport.py b/src/pdc_api_client/transport.py index 48cf0c3..7a3d578 100644 --- a/src/pdc_api_client/transport.py +++ b/src/pdc_api_client/transport.py @@ -35,7 +35,7 @@ def __init__( self._request_kwargs = request_kwargs or {} self._session = requests.Session() self._session.headers["Accept"] = "application/json" - # Render's edge sometimes answers a healthy upstream with a one-off 502; only idempotent reads retry. + # A proxy in front of PDC can answer a healthy upstream with a one-off 502; only idempotent reads retry. retries = Retry( total=3, backoff_factor=0.5, diff --git a/tests/test_client.py b/tests/test_client.py index f24d37b..54139fc 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -3,7 +3,7 @@ from pdc_api_client import errors from pdc_api_client.client import ( _iterate_pages, - get_basefields, + get_base_fields, get_changemaker_field_value_batch, get_changemaker_field_values, get_changemaker_proposals, @@ -36,13 +36,13 @@ ) -class TestGetBasefields: +class TestGetBaseFields: # PDC sorts /baseFields only by created_at, which ties, so paging can repeat or skip fields. def test_fetches_every_field_in_one_page(self, session, add_json_response): payload = {"entries": [_basefield(f"f{i}") for i in range(75)], "total": 75} add_json_response(path="/baseFields", payload=payload, query_params={"_page": 1, "_count": 10_000}) - result = get_basefields(session) + result = get_base_fields(session) assert [b.short_code for b in result] == [f"f{i}" for i in range(75)] @@ -50,7 +50,7 @@ def test_validates_date_and_date_time_data_types(self, session, add_json_respons payload = {"entries": [_basefield("d", data_type="date"), _basefield("dt", data_type="date_time")], "total": 2} add_json_response(path="/baseFields", payload=payload, query_params={"_page": 1, "_count": 10_000}) - result = get_basefields(session) + result = get_base_fields(session) assert [b.data_type for b in result] == [BaseFieldDataType.DATE, BaseFieldDataType.DATE_TIME] @@ -193,9 +193,14 @@ def test_returns_validated_model(self, session, add_json_response): def test_raises_batch_not_found_on_404(self, session, add_json_response): add_json_response(path="changemakerFieldValueBatches/999", payload={"detail": "not found"}, status=404) - with pytest.raises(errors.ChangemakerFieldValueBatchNotFoundError): + with pytest.raises( + errors.ChangemakerFieldValueBatchNotFoundError, match="No changemaker field value batch with id 999" + ) as exc_info: get_changemaker_field_value_batch(session, batch_id=999) + assert isinstance(exc_info.value, errors.PDCNotFoundError) + assert exc_info.value.code == 404 + class TestIteratePages: def test_handles_empty_bundle(self, session, add_json_response): diff --git a/tests/test_client_files.py b/tests/test_client_files.py index ec498e5..37ddf5b 100644 --- a/tests/test_client_files.py +++ b/tests/test_client_files.py @@ -55,14 +55,15 @@ def test_raises_on_failure(self, presigned_post, stub_s3_upload): ) -def test_download_url_is_the_presigned_url_as_a_plain_string(): - url = ( - "https://my-bucket.s3.us-east-1.amazonaws.com/files/report%20%28final%29.pdf" - "?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=3600&X-Amz-Signature=abc123" - ) +class TestFile: + def test_keeps_presigned_download_url_as_plain_string(self): + url = ( + "https://my-bucket.s3.us-east-1.amazonaws.com/files/report%20%28final%29.pdf" + "?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=3600&X-Amz-Signature=abc123" + ) - file = File.model_validate({"id": 1, "name": "report (final).pdf", "downloadUrl": url}) + file = File.model_validate({"id": 1, "name": "report (final).pdf", "downloadUrl": url}) - assert isinstance(file.download_url, str) - assert file.download_url == url - assert json.loads(json.dumps(file.model_dump()))["download_url"] == url + assert isinstance(file.download_url, str) + assert file.download_url == url + assert json.loads(json.dumps(file.model_dump()))["download_url"] == url diff --git a/tests/test_client_operations.py b/tests/test_client_operations.py index 19995ee..95d9511 100644 --- a/tests/test_client_operations.py +++ b/tests/test_client_operations.py @@ -7,11 +7,12 @@ from pdc_api_client.client import ( create_changemaker_field_value, create_changemaker_field_value_batch, - create_changemaker_proposal, - create_changemaker_proposal_version, create_changemaker_source, + create_file, create_initiative, create_initiative_field_value, + create_proposal, + create_proposal_version, get_application_form, get_application_form_by_name, get_bundle_page, @@ -21,12 +22,12 @@ get_organization_by_keycloak_id, get_proposal, link_changemaker_to_proposal, - register_file_upload, ) from pdc_api_client.errors import ( ApplicationFormNotFoundError, ChangemakerNotFoundError, FunderNotFoundError, + PDCNotFoundError, ProposalNotFoundError, ) from pdc_api_client.models import ( @@ -66,28 +67,37 @@ def _sent_json(): class TestGetProposal: - def test_404_translates_to_proposal_not_found(self, session, add_proposal_response): + def test_raises_proposal_not_found_on_404(self, session, add_proposal_response): add_proposal_response(id=999, status=404, payload={}) - with pytest.raises(ProposalNotFoundError): + with pytest.raises(ProposalNotFoundError, match="No proposal with id 999") as exc_info: get_proposal(session, proposal_id=999) + assert isinstance(exc_info.value, PDCNotFoundError) + assert exc_info.value.code == 404 + class TestGetChangemaker: - def test_404_translates_to_changemaker_not_found(self, session, add_changemaker_response): + def test_raises_changemaker_not_found_on_404(self, session, add_changemaker_response): add_changemaker_response(id=999, status=404, payload={}) - with pytest.raises(ChangemakerNotFoundError): + with pytest.raises(ChangemakerNotFoundError, match="No changemaker with id 999") as exc_info: get_changemaker(session, changemaker_id=999) + assert isinstance(exc_info.value, PDCNotFoundError) + assert exc_info.value.code == 404 + class TestGetFunder: - def test_404_translates_to_funder_not_found(self, session, add_funder_response): + def test_raises_funder_not_found_on_404(self, session, add_funder_response): add_funder_response(short_code="missing", status=404, payload={}) - with pytest.raises(FunderNotFoundError): + with pytest.raises(FunderNotFoundError, match="No funder with short code 'missing'") as exc_info: get_funder(session, short_code="missing") + assert isinstance(exc_info.value, PDCNotFoundError) + assert exc_info.value.code == 404 + class TestGetChangemakerProposal: def test_returns_first_entry_as_model(self, session, add_json_response): @@ -102,12 +112,14 @@ def test_returns_first_entry_as_model(self, session, add_json_response): assert isinstance(result, ChangemakerProposal) assert result.id == 5 - def test_empty_bundle_translates_to_proposal_not_found(self, session, add_json_response): + def test_raises_proposal_not_found_on_empty_bundle(self, session, add_json_response): add_json_response(path="changemakerProposals", payload=bundle([]), query_params={"proposal": 100}) - with pytest.raises(ProposalNotFoundError): + with pytest.raises(ProposalNotFoundError, match="No changemaker linked to proposal 100") as exc_info: get_changemaker_proposal(session, proposal_id=100) + assert isinstance(exc_info.value, PDCNotFoundError) + class TestGetOrganizationByKeycloakId: def test_parses_organization_with_funder_facet(self, session, add_organization_response): @@ -125,7 +137,7 @@ class TestGetApplicationForm: def test_requests_form_by_id(self, session, add_application_form_response): add_application_form_response(id=7) - get_application_form(session, form_id=7) + get_application_form(session, application_form_id=7) assert responses.calls[-1].request.path_url == "/applicationForms/7" @@ -140,7 +152,7 @@ def test_returns_form_with_matching_name(self, session): assert get_application_form_by_name(session, name="Wanted").id == 2 - def test_raises_not_found_when_no_form_has_the_name(self, session): + def test_raises_not_found_on_unknown_name(self, session): self._add_forms("Other") with pytest.raises(ApplicationFormNotFoundError): @@ -151,7 +163,7 @@ class TestCreateChangemakerProposal: def test_sends_data_unchanged(self, session): _post("proposals", proposal_payload()) - create_changemaker_proposal(session, data={"opportunityId": 5, "externalId": "ext-1"}) + create_proposal(session, data={"opportunityId": 5, "externalId": "ext-1"}) assert _sent_json() == {"opportunityId": 5, "externalId": "ext-1"} @@ -166,7 +178,7 @@ def test_sends_camel_case_body(self, session): ) _post("proposalVersions", proposal_version_payload()) - create_changemaker_proposal_version(session, proposal_version=new_version) + create_proposal_version(session, proposal_version=new_version) assert _sent_json() == { "proposalId": 100, @@ -189,7 +201,7 @@ class TestRegisterFileUpload: def test_sends_file_metadata(self, session): _post("files", {"id": 3, "name": "doc.pdf"}) - register_file_upload(session, name="doc.pdf", mime_type="application/pdf", size=42) + create_file(session, name="doc.pdf", mime_type="application/pdf", size=42) assert _sent_json() == {"name": "doc.pdf", "mimeType": "application/pdf", "size": 42} diff --git a/tests/test_keycloak_admin.py b/tests/test_keycloak_admin.py index e9d45b7..bc65b05 100644 --- a/tests/test_keycloak_admin.py +++ b/tests/test_keycloak_admin.py @@ -7,14 +7,15 @@ ORGANIZATIONS_URL = f"{KEYCLOAK_URL}/admin/realms/pdc/organizations" -def test_ensure_organization_raises_when_keycloak_returns_no_id(): - responses.post( - f"{KEYCLOAK_URL}/realms/master/protocol/openid-connect/token", - json={"access_token": "token", "expires_in": 300, "refresh_token": "refresh", "refresh_expires_in": 1800}, - ) - responses.get(ORGANIZATIONS_URL, json=[]) - responses.post(ORGANIZATIONS_URL, status=201) - admin = KeycloakAdmin(keycloak_url=KEYCLOAK_URL, realm="pdc", admin_username="admin", admin_password="secret") +class TestKeycloakAdmin: + def test_ensure_organization_raises_lookup_error_on_missing_id(self): + responses.post( + f"{KEYCLOAK_URL}/realms/master/protocol/openid-connect/token", + json={"access_token": "token", "expires_in": 300, "refresh_token": "refresh", "refresh_expires_in": 1800}, + ) + responses.get(ORGANIZATIONS_URL, json=[]) + responses.post(ORGANIZATIONS_URL, status=201) + admin = KeycloakAdmin(keycloak_url=KEYCLOAK_URL, realm="pdc", admin_username="admin", admin_password="secret") - with pytest.raises(LookupError, match="acme"): - admin.ensure_organization(name="Acme", alias="acme", domain="acme.test") + with pytest.raises(LookupError, match="acme"): + admin.ensure_organization(name="Acme", alias="acme", domain="acme.test") diff --git a/tests/test_models.py b/tests/test_models.py index 61f2fac..363f03b 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -3,19 +3,22 @@ from pdc_api_client.testing import funder_payload, initiative_field_value_payload -def test_nullable_id_reference_is_a_plain_int(): - funder = Funder.model_validate(funder_payload() | {"defaultTerminologySetId": 17}) +class TestFunder: + def test_parses_nullable_id_reference_as_int(self): + funder = Funder.model_validate(funder_payload() | {"defaultTerminologySetId": 17}) - assert funder.default_terminology_set_id == 17 + assert funder.default_terminology_set_id == 17 -def test_field_value_with_null_is_valid_parses(): - field_value = InitiativeFieldValue.model_validate(initiative_field_value_payload() | {"isValid": None}) +class TestInitiativeFieldValue: + def test_accepts_null_is_valid(self): + field_value = InitiativeFieldValue.model_validate(initiative_field_value_payload() | {"isValid": None}) - assert field_value.is_valid is None + assert field_value.is_valid is None -def test_generated_models_leave_permission_grants_to_the_hand_written_union(): - generated = {name for name in vars(_generated) if "PermissionGrant" in name} +class TestGeneratedModels: + def test_leave_permission_grants_to_the_hand_written_union(self): + generated = {name for name in vars(_generated) if "PermissionGrant" in name} - assert generated == {"PermissionGrantEntityType", "PermissionGrantGranteeType", "PermissionGrantVerb"} + assert generated == {"PermissionGrantEntityType", "PermissionGrantGranteeType", "PermissionGrantVerb"} diff --git a/tests/test_permission_grants.py b/tests/test_permission_grants.py index ff2edd9..3ac5404 100644 --- a/tests/test_permission_grants.py +++ b/tests/test_permission_grants.py @@ -126,9 +126,9 @@ def test_filters_become_query_params(self, session, add_page_response): result = list_permission_grants( session, - changemaker=9, - funder="acme-foundation", - proposal=7, + changemaker_id=9, + funder_short_code="acme-foundation", + proposal_id=7, grantee_type=PermissionGrantGranteeType.USER_GROUP, verb=PermissionGrantVerb.MANAGE, ) diff --git a/tests/test_token.py b/tests/test_token.py index 386daf5..5043023 100644 --- a/tests/test_token.py +++ b/tests/test_token.py @@ -60,7 +60,7 @@ def test_refetches_after_expiry(self): assert provider.get_token() == "second" assert len(responses.calls) == 2 - def test_non_200_raises_token_request_error(self): + def test_raises_token_request_error_on_non_200(self): _add_token(status=401) provider = _provider() @@ -68,7 +68,7 @@ def test_non_200_raises_token_request_error(self): with pytest.raises(TokenRequestError): provider.get_token() - def test_non_json_error_body_still_raises_token_request_error(self): + def test_raises_token_request_error_on_non_json_error_body(self): responses.add(method=responses.POST, url=TOKEN_URL, body="502 Bad Gateway", status=502) provider = _provider() @@ -76,7 +76,7 @@ def test_non_json_error_body_still_raises_token_request_error(self): with pytest.raises(TokenRequestError): provider.get_token() - def test_non_json_success_body_raises_token_request_error(self): + def test_raises_token_request_error_on_non_json_success_body(self): responses.add(method=responses.POST, url=TOKEN_URL, body="not json", status=200) provider = _provider() @@ -84,7 +84,7 @@ def test_non_json_success_body_raises_token_request_error(self): with pytest.raises(TokenRequestError): provider.get_token() - def test_success_body_missing_access_token_raises_token_request_error(self): + def test_raises_token_request_error_on_missing_access_token(self): responses.add(method=responses.POST, url=TOKEN_URL, json={"expires_in": 300}, status=200) provider = _provider() @@ -92,7 +92,7 @@ def test_success_body_missing_access_token_raises_token_request_error(self): with pytest.raises(TokenRequestError): provider.get_token() - def test_connection_failure_raises_service_error(self): + def test_raises_service_error_on_connection_failure(self): provider = _provider() # no response registered -> ConnectionError with pytest.raises(PDCServiceError): diff --git a/tests/test_transport.py b/tests/test_transport.py index ad67da9..b8def22 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -18,119 +18,110 @@ URL = f"{BASE_URL}test-endpoint" -def test_pdc_exception_sets_detail_and_code(): - exc = PDCException(detail="something went wrong", code=400) - assert exc.detail == "something went wrong" - assert exc.code == 400 - - -@pytest.mark.parametrize( - ("status", "error_class"), - [ - (400, PDCBadRequestError), - (401, PDCUnauthorizedError), - (403, PDCForbiddenError), - (404, PDCNotFoundError), - (409, PDCConflictError), - (422, PDCUnprocessableEntityError), - (500, PDCServiceError), - (503, PDCServiceError), - (429, PDCException), - ], -) -def test_error_status_raises_mapped_exception(session, status, error_class): - responses.get(URL, json={"error": "test error"}, status=status) - with pytest.raises(PDCException) as exc_info: - session.get("/test-endpoint") - assert type(exc_info.value) is error_class - assert exc_info.value.code == status - assert exc_info.value.detail == {"error": "test error"} - - -def test_non_json_error_body_becomes_text_detail(session): - responses.get(URL, body="Service Unavailable", status=500) - with pytest.raises(PDCServiceError) as exc_info: - session.get("/test-endpoint") - assert exc_info.value.detail == "Service Unavailable" - - -@pytest.mark.parametrize("exception", [requests.Timeout, requests.ConnectionError]) -def test_network_failure_raises_service_error(session, exception): - responses.get(URL, body=exception()) - with pytest.raises(PDCServiceError): - session.get("/test-endpoint") - - -@pytest.mark.parametrize("status", [502, 503, 504]) -def test_get_retries_transient_gateway_error(session, status): - responses.get(URL, status=status) - responses.get(URL, json={"ok": True}) - - assert session.get("/test-endpoint").json() == {"ok": True} - assert len(responses.calls) == 2 - - -def test_get_that_exhausts_retries_raises_service_error(session): - responses.get(URL, json={"error": "bad gateway"}, status=502) - - with pytest.raises(PDCServiceError) as exc_info: - session.get("/test-endpoint") - - assert (exc_info.value.code, exc_info.value.detail) == (502, {"error": "bad gateway"}) - assert len(responses.calls) == 4 - - -def test_get_retries_with_keep_alive_disabled(): - session = APISession(base_url=BASE_URL, token_provider=FakeTokenProvider(), keep_alive=False) - responses.get(URL, status=502) - responses.get(URL, json={"ok": True}) - - assert session.get("/test-endpoint").json() == {"ok": True} - - -def test_post_does_not_retry(session): - responses.post(URL, status=502) - - with pytest.raises(PDCServiceError): - session.post("/test-endpoint") - - assert len(responses.calls) == 1 - - -def test_get_does_not_retry_server_error_outside_gateway_codes(session): - responses.get(URL, status=500) - - with pytest.raises(PDCServiceError): +class TestPDCException: + def test_sets_detail_and_code(self): + exc = PDCException(detail="something went wrong", code=400) + assert exc.detail == "something went wrong" + assert exc.code == 400 + + +class TestAPISession: + @pytest.mark.parametrize( + ("status", "error_class"), + [ + (400, PDCBadRequestError), + (401, PDCUnauthorizedError), + (403, PDCForbiddenError), + (404, PDCNotFoundError), + (409, PDCConflictError), + (422, PDCUnprocessableEntityError), + (500, PDCServiceError), + (503, PDCServiceError), + (429, PDCException), + ], + ) + def test_raises_mapped_exception_on_error_status(self, session, status, error_class): + responses.get(URL, json={"error": "test error"}, status=status) + with pytest.raises(PDCException) as exc_info: + session.get("/test-endpoint") + assert type(exc_info.value) is error_class + assert exc_info.value.code == status + assert exc_info.value.detail == {"error": "test error"} + + def test_uses_text_detail_on_non_json_error_body(self, session): + responses.get(URL, body="Service Unavailable", status=500) + with pytest.raises(PDCServiceError) as exc_info: + session.get("/test-endpoint") + assert exc_info.value.detail == "Service Unavailable" + + @pytest.mark.parametrize("exception", [requests.Timeout, requests.ConnectionError]) + def test_raises_service_error_on_network_failure(self, session, exception): + responses.get(URL, body=exception()) + with pytest.raises(PDCServiceError): + session.get("/test-endpoint") + + @pytest.mark.parametrize("status", [502, 503, 504]) + def test_get_retries_on_gateway_error(self, session, status): + responses.get(URL, status=status) + responses.get(URL, json={"ok": True}) + + assert session.get("/test-endpoint").json() == {"ok": True} + assert len(responses.calls) == 2 + + def test_get_raises_service_error_on_exhausted_retries(self, session): + responses.get(URL, json={"error": "bad gateway"}, status=502) + + with pytest.raises(PDCServiceError) as exc_info: + session.get("/test-endpoint") + + assert (exc_info.value.code, exc_info.value.detail) == (502, {"error": "bad gateway"}) + assert len(responses.calls) == 4 + + def test_get_retries_with_keep_alive_disabled(self): + session = APISession(base_url=BASE_URL, token_provider=FakeTokenProvider(), keep_alive=False) + responses.get(URL, status=502) + responses.get(URL, json={"ok": True}) + + assert session.get("/test-endpoint").json() == {"ok": True} + + def test_post_does_not_retry(self, session): + responses.post(URL, status=502) + + with pytest.raises(PDCServiceError): + session.post("/test-endpoint") + + assert len(responses.calls) == 1 + + def test_get_does_not_retry_on_other_server_errors(self, session): + responses.get(URL, status=500) + + with pytest.raises(PDCServiceError): + session.get("/test-endpoint") + + assert len(responses.calls) == 1 + + def test_sends_bearer_token(self, session): + responses.get(URL, json={}) session.get("/test-endpoint") - - assert len(responses.calls) == 1 - - -def test_request_sends_bearer_token(session): - responses.get(URL, json={}) - session.get("/test-endpoint") - assert responses.calls[0].request.headers["Authorization"] == "Bearer fake-token" - - -def test_keep_alive_default_does_not_force_connection_close(session): - assert session._session.headers.get("Connection") != "close" - - -def test_keep_alive_false_sends_connection_close(): - session = APISession(base_url=BASE_URL, token_provider=FakeTokenProvider(), keep_alive=False) - assert session._session.headers["Connection"] == "close" - - -@pytest.mark.parametrize( - ("base", "path", "expected"), - [ - ("https://host/api", "users", "https://host/api/users"), - ("https://host/api", "/users", "https://host/api/users"), - ("https://host/api/", "users", "https://host/api/users"), - ("https://host", "users", "https://host/users"), - ("https://host/", "/users", "https://host/users"), - ], -) -def test_make_api_url_preserves_base_path(base, path, expected): - session = APISession(base_url=base, token_provider=FakeTokenProvider()) - assert session._make_api_url(path) == expected + assert responses.calls[0].request.headers["Authorization"] == "Bearer fake-token" + + def test_keeps_connection_alive_by_default(self, session): + assert session._session.headers.get("Connection") != "close" + + def test_sends_connection_close_when_keep_alive_is_false(self): + session = APISession(base_url=BASE_URL, token_provider=FakeTokenProvider(), keep_alive=False) + assert session._session.headers["Connection"] == "close" + + @pytest.mark.parametrize( + ("base", "path", "expected"), + [ + ("https://host/api", "users", "https://host/api/users"), + ("https://host/api", "/users", "https://host/api/users"), + ("https://host/api/", "users", "https://host/api/users"), + ("https://host", "users", "https://host/users"), + ("https://host/", "/users", "https://host/users"), + ], + ) + def test_make_api_url_preserves_base_path(self, base, path, expected): + session = APISession(base_url=base, token_provider=FakeTokenProvider()) + assert session._make_api_url(path) == expected