community/mock-preset-x

A service preset for aux4/mock that stands up X's (Twitter) OAuth2 auth dance on a running mock server with a single command: the token endpoint, the /2/users/me endpoint (bearer-gated), and X's real problem+json 401 envelope. Install it and aux4 mock preset x becomes available — the mock core stays preset-agnostic and knows nothing about X.

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

Installation

aux4 aux4 pkger install community/mock-preset-x

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

Usage

aux4 mock start --port 8080
aux4 mock preset x --port 8080
stub POST /2/oauth2/token -> 200
stub GET /2/users/me -> 200
stub GET /2/users/me -> 401

Confirm it is installed and visible to the mock:

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

What it stubs

| Method + Path | Status | Behavior | |---------------|--------|----------| | POST /2/oauth2/token | 200 | OAuth2 token response (access_token, refresh_token, scope, token_type: bearer) | | GET /2/users/me | 200 | Identity JSON (data.id, data.name, data.username) — only when Authorization: Bearer <token> is present | | GET /2/users/me | 401 | X problem+json envelope — fallback when no bearer token |

The /2/users/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:

# with a bearer token → 200 identity
curl -s http://localhost:8080/api/2/users/me -H 'Authorization: Bearer x'
# {"data":{"id":"1234567890","name":"Sally","username":"sally"}}

# without one → 401 problem+json envelope
curl -s http://localhost:8080/api/2/users/me
# {"status":401,"title":"Unauthorized","type":"about:blank"}

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 x --port 8080
aux4 mock stub --port 8080 --method POST \
  --path /2/tweets \
  --status 201 --body '{"data":{"id":"tweet_1","text":"hello"}}'

Overrides

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

aux4 mock preset x --port 8080 \
  --accessToken TOK123 --user "Sally"
  • --accessToken — token returned by POST /2/oauth2/token (default mock-access-token)
  • --user — display name (data.name) in the /2/users/me response (default Sally). The data.username handle is derived from it (lowercased, spaces removed) — e.g. --user "Sally" yields username: 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 X's real paths (/2/oauth2/token, /2/users/me). Point the code-under-test's base URL at http://localhost:<port>/api and let it 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-x

This package as a template

community/mock-preset-x 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.