community/mock-preset-microsoft

A service preset for aux4/mock that stands up Microsoft's Azure AD auth dance on a running mock server with a single command: the token endpoint, the Microsoft Graph /me endpoint (bearer-gated), and Graph's real InvalidAuthenticationToken 401 envelope. Install it and aux4 mock preset microsoft becomes available — the mock core stays preset-agnostic and knows nothing about Microsoft.

This is a pure .aux4 package (no binary, no bundle). It works entirely by contributing a microsoft command into aux4/mock's mock:preset profile via aux4's global.aux4 profile merge.

Installation

aux4 aux4 pkger install community/mock-preset-microsoft

Installing this package pulls in aux4/mock as a dependency.

Usage

aux4 mock start --port 8080
aux4 mock preset microsoft --port 8080
stub POST /common/oauth2/v2.0/token -> 200
stub GET /me -> 200
stub GET /me -> 401

Confirm it is installed and visible to the mock:

aux4 mock presets
Installed presets (apply with: aux4 mock preset <service> --port <port>):
  microsoft

What it stubs

| Method + Path | Status | Behavior | |---------------|--------|----------| | POST /common/oauth2/v2.0/token | 200 | Azure AD token response (access_token, ext_expires_in, refresh_token, scope, token_type) | | GET /me | 200 | Graph identity JSON — only when Authorization: Bearer <token> is present | | GET /me | 401 | Graph InvalidAuthenticationToken envelope — fallback when no bearer token |

The /me endpoint is registered as two stubs on the same path: a happy path gated on Authorization: Bearer * (any token) and a bare fallback returning the 401. A request with a bearer token gets the identity; one without falls through to Graph's real error:

# with a bearer token → 200 identity
curl -s http://localhost:8080/api/me -H 'Authorization: Bearer x'
# {"displayName":"Sally","id":"00000000-0000-0000-0000-000000000000","mail":"sally@example.com","userPrincipalName":"sally@example.com"}

# without one → 401 Graph error envelope
curl -s http://localhost:8080/api/me
# {"error":{"code":"InvalidAuthenticationToken","message":"Access token is empty."}}

The preset is additive — it only calls aux4 mock stub, so it never clears existing stubs. Layer the business endpoints your test exercises on top:

aux4 mock preset microsoft --port 8080
aux4 mock stub --port 8080 --method GET \
  --path /me/messages \
  --status 200 --body '{"value":[{"id":"msg_1","subject":"Hello"}]}'

Overrides

All optional, with sane defaults, so a test can assert a known identity:

aux4 mock preset microsoft --port 8080 \
  --accessToken TOK123 --email sally@corp.io --user "Sally"
  • --accessToken — token returned by POST /common/oauth2/v2.0/token (default mock-access-token)
  • --email — mail/userPrincipalName in the /me response (default sally@example.com)
  • --user — displayName in the /me response (default Sally)

Also accepts --name and --stateDir to address a server the same way every other aux4/mock command does (precedence: --stateDir > --name > --port).

Base URLs and paths

The preset stubs Microsoft's real paths (/common/oauth2/v2.0/token, /me). Azure AD token URLs embed the tenant (/{tenant}/oauth2/v2.0/token); this preset uses the concrete /common tenant segment, so point the code-under-test at the common tenant (or add your own tenant stub with aux4 mock stub). Point the base URL at http://localhost:<port>/api and let the client append the real path — aux4/mock strips the /api mount prefix before matching.

Using in tests

aux4/mock and this preset are plain CLIs, so they drop straight into an aux4/test .test.md. In CI, declare both packages on the test job:

- uses: aux4/action@v1
  with:
    command: test
    packages: aux4/mock,community/mock-preset-microsoft

This package as a template

community/mock-preset-microsoft follows the same pattern as community/mock-preset-google, the reference implementation for writing your own community/mock-preset-<service> package: depend on aux4/mock, re-declare the mock profile with a preset routing command, and add a <service> command under mock:preset whose execute is a sequence of aux4 mock stub calls.