Skip to content

Latest commit

 

History

History
362 lines (269 loc) · 15.1 KB

README-dev.md

File metadata and controls

362 lines (269 loc) · 15.1 KB

Local development

Run the tests and the runnable examples in examples/ on your machine.

Install

git clone https://github.com/vgrem/office365-rest-python-client.git
cd office365-rest-python-client

uv venv
source .venv/bin/activate       # Windows: .venv\Scripts\activate
uv sync --all-extras

Using pip: python3 -m venv venv && source venv/bin/activate && pip install -e ".[examples,ntlm]" pytest

Run offline tests (no tenant)

uv run pytest -q

Offline is the default: a bare pytest cannot reach a tenant. Add --live to also run what the credentials in .env make reachable (slow, and it writes to the tenant).

From an IDE

An IDE's test runner is handed a path, not a command line, so a suite picked in the test tree runs offline however many times you click it. Two ways out, both once per checkout:

  • Switch the checkout on — put OFFICE365_LIVE=1 in .env. tests/settings.py gives .env to the environment, so every runner in this checkout signs in: the test tree, a debugger, a bare uv run pytest. --offline still wins for the run that must not, and CI has no .env, so nothing there changes.
  • Or give the runner the flag — VS Code: "python.testing.pytestArgs": ["tests", "--live"] in .vscode/settings.json; PyCharm: Run → Edit Configurations → Edit configuration templates… → Python tests → pytest → Additional arguments. A template covers every run created after it.

python -m tests.doctor ends its report with which of the two is in force.

That is the whole setup for most contributions. Continue only to run against a real tenant.

What an offline test is for

A live suite that calls an operation is that operation's acceptance — --offline skips it, it does not stand in for it. So an offline test earns its place on what a tenant call cannot show: a parser, a path builder, a declaration table, a converter's edge case. It does not earn it by restating what the live run already asserts — that an upload session accepts a 10 MiB chunk is the upload succeeding, and three such pins were deleted in one commit.

The question is narrower than "is this code exercised live": does the live run tell the two behaviours apart? For a chunk it does, because the upload is refused otherwise. It does not for a header no tenant reads, and it does not for a payload a modern farm accepts either way — SharePoint 2019 refuses the empty object the serialiser used to send, and there is no 2019 lab here. Both of those are pinned offline, and the docstring names the tenant that cannot show it.

Lint, format and git hooks

Install the git hooks once per clone:

uv run pre-commit install

That single command registers both stages:

  • commit — pyproject-fmt, uv lock, and ruff check --fix / ruff format on the files you staged (fast).
  • push — pyright and the offline test suite, uv run pytest (slower).

Run everything against the whole tree without committing:

uv run pre-commit run --all-files
uv run pre-commit run --hook-stage pre-push --all-files

The hooks invoke the tools from the locked environment, and CI installs the same lockfile, so the versions you run locally match the ones that gate the build.

One more gate runs on every pull request, and nothing commits it for you:

uv run python -m generator verify

It reads the rendered model source rather than the metadata, and checks that a served package declares its members, that every declaration names a name its getter reads, that every projected column declares a type, that a client object member is a navigation, and that a facet names one of the declared roles. Run it after touching @odata decorators, _schema-adjacent code or the generator's templates.


Keep the tree where the routing says it belongs

Placement descends: a type whose class name repeats the package it lives in belongs in the subpackage that consumes the name. New types follow that rule, and --place re-applies it to files written before their subpackage existed:

uv run python -m generator graph --place office365/intune               # what it would move
uv run python -m generator graph --place office365/intune --apply-place # move, rewrite imports

It reads the tree rather than the metadata, so it answers in a second; --apply-place moves the files and rewrites every module that imported them. Running it on a placed tree proposes nothing, and that is the invariant to re-run after a refresh -- so a new beta type that lands flat is caught by the command, not by a reviewer noticing. See generator/PLACEMENT.md for the rules it enforces and for why it will not lengthen a stem.


Set up live authentication

Credentials live in one .env at the repo root (loaded automatically, nothing to export). Verify what is configured at any time:

uv run python -m tests.doctor

1. Create .env

cp .env.example .env

2. Register app in Entra (manual)

Register an app, then fill in the tenant, client ID and an admin account:

OFFICE365_TENANT=contoso.onmicrosoft.com
OFFICE365_CLIENT_ID=00000000-0000-0000-0000-000000000000
OFFICE365_ADMIN_USERNAME=admin@contoso.onmicrosoft.com

The admin needs Global Administrator, Privileged Role Administrator or Application Administrator.

3. Grant permissions

Once .env exists, tests.preflight can grant everything the live suites declare and the tenant lacks — it reads the requirement matrix, prints the plan, asks, grants, and re-reads the tenant (see Grant what the suites need):

uv run python -m tests.preflight --verify    # what is missing, without changing anything
uv run python -m tests.preflight --grant     # the same, then grant it

For a single permission, the two scripts below take a name and apply admin consent:

uv run python examples/entraid/applications/grant_application_perms.py   # app-only roles
uv run python examples/entraid/applications/grant_delegated_perms.py     # delegated scopes

Each prompts for the permission name, e.g. Sites.FullControl.All or User.Read.All.

4. User context (ROPC / delegated)

OFFICE365_USERNAME is the primary test user and the ROPC account; the optional OFFICE365_USERNAME_ALT adds a second account for sharing/delegation tests.

OFFICE365_USERNAME=user1@contoso.onmicrosoft.com
OFFICE365_PASSWORD=your-password
# OFFICE365_USERNAME_ALT=user2@contoso.onmicrosoft.com

Needs MFA off for the ROPC user. For interactive examples, also add a redirect URI and the Allow public client flows toggle:

uv run python examples/entraid/applications/redirect_uris.py

5. App-only

Two values a tenant reveals once and never returns again are the certificate thumbprint and a client secret. Both come from the scripts that create them, not from a wizard:

uv run python examples/entraid/applications/rotate_cert.py     # prints OFFICE365_CERT_THUMBPRINT
uv run python examples/entraid/applications/rotate_secret.py   # prints CLIENT_SECRET, once

Those two lines, the URLs, usernames and mailboxes the comments in .env.example ask for, and the app registration from step 2 are the whole file. Nothing writes it — it holds an app secret and the admin account, and a program that can rewrite it can also truncate it.

SharePoint has an end-to-end variant that provisions the app, attaches the certificate the live suites read (tests/selfsigncert.crt), grants Sites.Selected plus one role per site, and prints the same block:

uv run python examples/sharepoint/getting-started/setup_sharepoint_app.py --site <url>

A client secret signs in Graph but not the SharePoint REST API v1, which wants a certificate — that is why both exist.

Then run tests and examples — missing credentials are skipped, not failed:

uv run pytest -q
uv run python examples/auth/with_client_secret.py

Check what the live suites need

A live test states its precondition beside itself and skips when the tenant does not grant it. Prefer the derived form, which reads the permission from the model method the test exercises instead of restating it:

@requires_call(Message.send, bypass_roles=["Exchange Administrator"])
@requires_delegated("Mail.Read")          # a scope, by name
@requires_application("Sites.Read.All")   # an app role, by name

bypass_roles is the weaker alternative: it is answered from the tenant's directory roles, which is not the same fact as a scope in the token. Graph mints a token from consented scopes alone, so Teams Administrator does not carry VirtualEvent.Read — measured, the virtual-events suite skipped on the scope once the role was taken away, because every call had answered 403 with the role still declared.

Name only permissions the API publishes. A directory records whatever string a grant names, so a tenant can hold BackupRestore.Read.All — a name Microsoft Graph never minted — and mint a token carrying it, while every request that declared the name answers 403. published_permissions(client, resource).publishes(name, kind) is the check: the service principal is read once and cached, and a requirement that names something unpublished is reported as unmet however the tenant's grants read. Where the vocabulary cannot be read at all — a delegated token without Application.Read.All — the answer is "unknown" and the grants decide alone, because an unreadable answer is not a refusal.

Add flow="application" to @requires_call for an app-only suite. The declared requirements are read off the test tree, so what a tenant must grant is known before a run and never kept in a second list. A requirement names the API it is granted on, so a SharePoint test and a Graph test never compare one API's grants against the other's:

uv run python -m tests.preflight                      # what is blocking the most tests
uv run python -m tests.preflight --verify             # ... and what this tenant grants
uv run python -m tests.preflight --all                # every requirement, not just the blockers

--verify signs in once per configured flow — including the certificate — and asks each app about its own grants, so the answer is about the identity that will run the tests.

The default view is the screen, not the matrix: the counts, only the flows that are not ready, and the ten requirements that unblock the most tests — one row each, printed as 54 delegated: any of Sites.Read.All, Sites.ReadWrite.All (or Global Administrator, SharePoint Administrator). --all lists every requirement instead, met ones included, and still ranks them by the tests they unblock.

The model-method provenance behind @requires_call remains on the declarations. The console summary focuses on grants that unblock the most tests rather than exporting a per-suite matrix. --strict exits non-zero when a flow is not configured or a declared requirement is not met.

A skipped test run says the same thing in its summary — 2 tests skipped: application: any of Domain.Read.All — so a green-but-skipped run names what it did not exercise rather than just counting.

Grant what the suites need

tests.preflight answers "what is missing"; --grant closes the gap, because it is the one program that already signs in as an administrator:

uv run python -m tests.preflight --verify            # the plan, nothing granted
uv run python -m tests.preflight --grant             # asks, grants, re-reads the tenant
uv run python -m tests.preflight --grant -y           # same, no prompt (scripted)

The plan is the smallest set that would unblock the tree — one name per any-of group, shared names asked for once — so it does not request Mail.ReadWrite when Mail.Read is enough. It prints the plan and waits for confirmation, then signs in by device code as an account holding Global Administrator or Privileged Role Administrator: delegated consent and application-role assignment are tenant-wide changes. Grants are idempotent — an already-granted permission is skipped, a refused one is reported and the rest are still attempted — and it re-reads the tenant afterwards, so "done" means the answer changed, not that the call returned.

Two kinds of requirement are reported but not granted, because they are not permissions on your app: a directory role is assigned to the test user (and needs Entra ID P1/P2), and a license has to be bought before it can be assigned. The plan names them and says so.

Adding a model method

A method that queues a request (add_query, execute_query, execute_batch) says which permission it needs, or tests/unit/test_permissions.py fails: PENDING there counts the undeclared builders per service package, and both directions are ratcheted — a new undeclared builder raises the count, and so does a declaration that disappears. Declaring is one line beside the request:

@require_permission(
    delegated=("Mail.Read",),
    application=("Mail.Read",),
    resource=ResourceName.Graph,
)
def get(self) -> Message: ...

resource is the API the names are granted on, defaulting to Microsoft Graph; a model under office365/sharepoint names ResourceName.SharePoint, because the same name is a different grant on each API.

A SharePoint method says one word instead, because its eight names are a ladder — AllSites.Read, .Write, .Manage, .FullControl and the app-only Sites.*.All beside them — where each rung does what the ones below it do:

from office365.sharepoint.permissions import SiteLevel, site_access


@site_access(SiteLevel.WRITE)
def add_item(self, creation_information: ListItemCreationInformation) -> ListItem: ...

Name the weakest rung that works: a read-only endpoint that claims MANAGE makes a live test skip on a tenant that could have run it.

A name has to be one the API actually publishes, and nothing offline can say which those are: the answer changes with the service, so it is read from the service principal that owns it and cached for the run (published_permissions). tests/graph/test_declared_permissions.py walks every declaration and compares it with that answer, so a misspelled or retired name fails a test rather than a call that answers 403.

Publish a release

Publishing is a tag push; .github/workflows/release.yml builds and uploads the wheels. The tag must match project.version in pyproject.toml — the workflow refuses a mismatch:

git tag -a v3.3.0 -m "3.3.0"
git push origin v3.3.0

The next release is 3.4.0: the Python floor raise under [Unreleased] in CHANGELOG.md is a breaking change, so no 3.3.x can carry it. Drop this line when 3.4.0 ships.

The GitHub release is then created by hand on the Releases page: pick the v3.3.0 tag, title it v3.3.0, and write the body from the matching CHANGELOG.md section — a short summary, the ### ✨ Highlights, the ### ⚠️ Breaking changes block, the fixes worth calling out, and the changelog + install footer. CHANGELOG.md stays the exhaustive record; the release page is the curated read.