@owlmeans/test-auth 0.1.18-rc.47 → 0.1.18-rc.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,16 +1,124 @@
1
1
  # @owlmeans/test-auth
2
2
 
3
- The **only** OwlMeans package that ships authentication/authorization mocks. Tests in any other package may import from here when they need a fake authenticated identity, a deterministic Ed25519 keypair, or an in-memory `TRUSTED` resource. No other mocks (database, network, sibling-package services) belong in test code — those packages get integration tests instead. See the `auth-protocol` skill for the protocol this mock implements.
3
+ The **only** OwlMeans package that ships authentication/authorization mocks. Tests in any other package may import from here when they need a fake authenticated identity, a deterministic Ed25519 keypair, or an in-memory `TRUSTED` resource. No other mocks (database, network, sibling-package services) belong in test code — those packages get integration tests instead (`@owlmeans/test-integration`). It is a dev dependency of category-B packages (the auth/authz family); a category-A package that seems to need it is testing behaviour that belongs in an auth-aware sibling. The package's skill is `testing-auth-unit`; the protocol these mocks stand in for is described by the `auth-protocol` skill.
4
4
 
5
- Helpers exported here:
5
+ ## Installation
6
6
 
7
- - `makeFixtureKeyPair(seed?)` — deterministic Ed25519 `KeyPairModel`.
8
- - `makeMemoryTrustedResource(records?)` — `Resource<TrustedRecord>` satisfying `trust()` lookups against the `TRUSTED` config resource.
9
- - `makeMockGuard({ alias?, auth?, allow? })` — `GuardService` that resolves to a chosen `Auth`. Implements `match`, `handle`, `authenticated`.
10
- - `appendMockGuard(ctx, auth, alias?)` — convenience that registers a mock guard with a chosen `Auth` on the context.
11
- - `signMockEnvelope(msg, type, kind?, kp?)` — wraps `makeEnvelopeModel` with a fixture keypair to produce a signed envelope.
12
- - `makeBearer(auth, kp?)` — `ED25519-BASIC-TOKEN <encoded>` header value for unit tests of header parsing.
13
- - Canonical fixtures: `SUPERUSER`, `USER`, `SERVICE` `Auth` payloads.
7
+ ```bash
8
+ bun add -d @owlmeans/test-auth@^0.1.18-rc.49
9
+ ```
10
+
11
+ It depends on `@owlmeans/test`, so the env gates and fixture loader are available alongside.
12
+
13
+ ## Concepts
14
+
15
+ - **Fixture keypair** — `makeFixtureKeyPair(seed)`: an Ed25519 `KeyPairModel` derived from a seed,
16
+ so signatures and trusted records are stable run to run. No seed gives a random pair.
17
+ - **Trusted resource** — `makeMemoryTrustedResource(records, alias?)`: a `Resource<TrustedRecord>`
18
+ registered under `TRUSTED` that answers the `trust()` lookups of `@owlmeans/auth-common`.
19
+ - **Mock guard** — `makeMockGuard(opts)`: a real `GuardService` shape (`match`, `handle`,
20
+ `authenticated`) that resolves a chosen `Auth` without cryptography or trusted-record lookups.
21
+ Defaults to the `DEFAULT_GUARD` alias, so it takes the place a real guard would occupy.
22
+ - **Canonical identities** — `SUPERUSER`, `USER`, `SERVICE`: `Auth` payloads with `scopes: ['*']`.
23
+ - **Last registration wins** — `registerService` overwrites an alias, so a mock guard registered
24
+ before a real one under the same alias is silently replaced.
25
+
26
+ ## Usage
27
+
28
+ ### Build the suite's context
29
+
30
+ ```typescript
31
+ // tests/context.ts
32
+ import { AppType, makeBasicContext } from '@owlmeans/context'
33
+ import type { BasicConfig } from '@owlmeans/context'
34
+ import type { TrustedRecord } from '@owlmeans/auth-common'
35
+ import { USER, appendMockGuard, makeFixtureKeyPair, makeMemoryTrustedResource } from '@owlmeans/test-auth'
36
+
37
+ export const authKey = makeFixtureKeyPair('auth-service')
38
+
39
+ export const authRecord: TrustedRecord = {
40
+ id: authKey.exportAddress(),
41
+ name: 'auth-service', // trust() looks up by name by default — seed both fields
42
+ credential: authKey.exportPublic(), // add `secret: authKey.export()` when the code must sign
43
+ scopes: ['*'],
44
+ }
45
+
46
+ export const makeTestCtx = () => {
47
+ const ctx = makeBasicContext<BasicConfig>({
48
+ ready: false, service: 'my-pkg-tests', type: AppType.Backend,
49
+ })
50
+ ctx.registerResource(makeMemoryTrustedResource([authRecord]))
51
+ appendMockGuard(ctx, USER) // after anything that registers a real guard
52
+
53
+ return ctx
54
+ }
55
+ ```
56
+
57
+ ### Control what the guard matches
58
+
59
+ ```typescript
60
+ import { SERVICE, makeMockGuard } from '@owlmeans/test-auth'
61
+
62
+ const guard = makeMockGuard({
63
+ alias: 'service-guard',
64
+ auth: SERVICE,
65
+ allow: req => req.headers['x-service'] != null, // `match` reports this predicate
66
+ token: 'token-service', // what client-side `authenticated()` answers
67
+ })
68
+ ctx.registerService(guard)
69
+ ```
70
+
71
+ ### Sign envelopes and bearers
72
+
73
+ ```typescript
74
+ import { EnvelopeKind } from '@owlmeans/basic-envelope'
75
+ import { USER, makeBearer, makeFixtureKeyPair, signMockEnvelope } from '@owlmeans/test-auth'
76
+
77
+ const key = makeFixtureKeyPair('alice')
78
+ const token = await signMockEnvelope({ userId: 'u-1' }, 'ed25519-basic-token', EnvelopeKind.Token, key)
79
+
80
+ const header = await makeBearer(USER, key) // 'ED25519-BASIC-TOKEN <encoded>'
81
+ ```
82
+
83
+ ## API
84
+
85
+ | Symbol | Kind | Purpose |
86
+ |---|---|---|
87
+ | `makeFixtureKeyPair(seed?)` | function | Deterministic Ed25519 `KeyPairModel`; random without a seed |
88
+ | `makeMemoryTrustedResource(records?, alias?)` | function | `Resource<TrustedRecord>` satisfying `trust()` lookups against the `TRUSTED` config resource; `alias` defaults to `'TRUSTED'` |
89
+ | `makeMockGuard(opts?)` | function | `GuardService` that resolves to a chosen `Auth`; implements `match`, `handle`, `authenticated` |
90
+ | `appendMockGuard(ctx, auth, alias?)` | function | Registers a mock guard resolving to `auth` on the context and returns it |
91
+ | `signMockEnvelope(msg, type, kind?, kp?)` | function | Wraps `makeEnvelopeModel` with a fixture keypair to produce a signed envelope; `kind` defaults to `EnvelopeKind.Token` |
92
+ | `makeBearer(auth, kp?)` | function | `ED25519-BASIC-TOKEN <encoded>` header value for unit tests of header parsing and guard `match` |
93
+ | `SUPERUSER`, `USER`, `SERVICE` | const | Canonical `Auth` payloads (`Superuser` / `User` / `Service` roles) |
94
+ | `MockGuardOptions` | type | `{ alias?, auth?, allow?, token? }` |
95
+
96
+ ### The in-memory trusted resource
97
+
98
+ - Implements only `load`, `save` and `create`; every other method throws.
99
+ - A read takes an id, or a criteria over exactly one of `id` / `name` with a string value; anything
100
+ wider throws `UnsupportedArgumentError('test-auth:trusted:where')`.
101
+ - Every record needs an `id` — a record without one is rejected on construction, and `create` refuses
102
+ an id already held.
103
+
104
+ ## Common pitfalls
105
+
106
+ - Registering the mock guard first and a real guard (through an `append*` mixin or a context factory)
107
+ afterwards under the same alias — the real one wins and the spec fails on a trusted-record lookup.
108
+ - Using random keypairs where a signature or record must be stable — pass a seed.
109
+ - Seeding a trusted record with `id` only — `trust()` looks up by `name`.
110
+ - Adding a new auth mock to a package's own `tests/` — it belongs here, documented in the
111
+ `testing-auth-unit` skill.
112
+ - Mocking anything other than authentication/authorization — write an integration test instead.
113
+
114
+ ## Related packages
115
+
116
+ - [`@owlmeans/test`](../test) — env gates and fixture loader this package builds on
117
+ - [`@owlmeans/test-integration`](../test-integration) — the place for tests that would need any other mock
118
+ - [`@owlmeans/auth-common`](../auth-common) — `trust()`, `TrustedRecord`, `DEFAULT_GUARD`
119
+ - [`@owlmeans/basic-keys`](../basic-keys) — `KeyPairModel`
120
+ - [`@owlmeans/basic-envelope`](../basic-envelope) — `makeEnvelopeModel`, `EnvelopeKind`
121
+ - [`@owlmeans/auth`](../auth) — the `Auth` type and roles
14
122
 
15
123
  <!-- owlmeans:agent-guidance:start -->
16
124
  ## Agent guidance
@@ -20,7 +128,7 @@ This package ships embedded agent skills under `agent-meta/`. After installing y
20
128
  your project's skill store (`.agents/skills/`):
21
129
 
22
130
  ```sh
23
- npx @owlmeans/agent-skills@^0.1.18-rc.51
131
+ npx @owlmeans/agent-skills@^0.1.18-rc.54
24
132
  ```
25
133
 
26
134
  The embedded files are version-matched to this package release. Do not edit them
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "schemaVersion": 2,
3
3
  "package": "@owlmeans/test-auth",
4
- "version": "0.1.18-rc.47",
5
- "generatedAt": "2026-10-06T11:45:57.654Z",
4
+ "version": "0.1.18-rc.49",
5
+ "generatedAt": "2026-10-15T10:20:01.289Z",
6
6
  "canonicalRepo": "https://github.com/owlmeans/common",
7
7
  "entries": [
8
8
  {
@@ -6,7 +6,7 @@ description: Category-B unit tests for OwlMeans Common — auth/authz packages w
6
6
 
7
7
  # Auth Unit Tests — Category B
8
8
 
9
- **Install:** `"@owlmeans/test-auth": "^0.1.18-rc.47"` in `devDependencies`
9
+ **Install:** `"@owlmeans/test-auth": "^0.1.18-rc.49"` in `devDependencies`
10
10
 
11
11
  Category B applies to the auth/authz packages: `auth`, `auth-common`, `auth-otp`, `basic-envelope`, `basic-keys`, `client-auth`, `client-did`, `client-iam`, `client-payment`, `did`, `mui-oidc-rp`, `oidc`, `server-auth`, `server-auth-otp`, `server-oidc-provider`, `server-oidc-rp`, `web-auth`, `web-oidc-provider`, `web-oidc-rp`, `wled`. These tests run under `bun test` (no Playwright even when there are React bits) and are the **only** place mocks are allowed — and only for authentication/authorization, via `@owlmeans/test-auth`.
12
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owlmeans/test-auth",
3
- "version": "0.1.18-rc.47",
3
+ "version": "0.1.18-rc.49",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -21,14 +21,14 @@
21
21
  }
22
22
  },
23
23
  "dependencies": {
24
- "@owlmeans/auth": "^0.1.18-rc.40",
25
- "@owlmeans/auth-common": "^0.1.18-rc.48",
26
- "@owlmeans/basic-envelope": "^0.1.18-rc.47",
27
- "@owlmeans/basic-keys": "^0.1.18-rc.49",
24
+ "@owlmeans/auth": "^0.1.18-rc.41",
25
+ "@owlmeans/auth-common": "^0.1.18-rc.50",
26
+ "@owlmeans/basic-envelope": "^0.1.18-rc.49",
27
+ "@owlmeans/basic-keys": "^0.1.18-rc.51",
28
28
  "@owlmeans/context": "^0.1.18-rc.34",
29
29
  "@owlmeans/entrypoint": "^0.1.18-rc.41",
30
30
  "@owlmeans/resource": "^0.1.18-rc.40",
31
- "@owlmeans/test": "^0.1.18-rc.35"
31
+ "@owlmeans/test": "^0.1.18-rc.36"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@owlmeans/dep-config": "workspace:*",