@owlmeans/test-ui 0.1.19-rc.0 → 0.1.19

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.
@@ -0,0 +1,105 @@
1
+ import { describe, expect, test } from 'bun:test'
2
+ import { AppType, makeBasicContext } from '@owlmeans/context'
3
+ import type { BasicConfig } from '@owlmeans/context'
4
+ import { makeBasicEd25519Guard } from '@owlmeans/auth-common'
5
+ import type { TrustedRecord } from '@owlmeans/auth-common'
6
+ import type { Auth } from '@owlmeans/auth'
7
+ import type { AbstractRequest, AbstractResponse } from '@owlmeans/entrypoint'
8
+ import { makeFixtureKeyPair, makeMemoryTrustedResource } from '@owlmeans/test-auth'
9
+ import { makeEmailTechClient } from '../src/email.js'
10
+
11
+ const TRUSTED = 'trusted'
12
+
13
+ /** A guard over one trusted record, the way a deployment knows its signers. */
14
+ const fixture = () => {
15
+ const key = makeFixtureKeyPair('test-ui-email-tech')
16
+ const record: TrustedRecord = {
17
+ id: key.exportAddress(), name: 'shared-key', credential: key.exportPublic(), scopes: ['*'],
18
+ }
19
+ const ctx = makeBasicContext<BasicConfig>({ ready: false, service: 'test-ui-tests', type: AppType.Backend })
20
+ ctx.registerResource(makeMemoryTrustedResource([record], TRUSTED))
21
+ const guard = makeBasicEd25519Guard(TRUSTED)
22
+ ctx.registerService(guard)
23
+
24
+ return { key, record, guard }
25
+ }
26
+
27
+ interface Sent { url: string, headers: Record<string, string>, body: unknown }
28
+
29
+ /** A `fetch` that records what was sent and answers `answer`. */
30
+ const recorder = (answer: unknown, status = 200) => {
31
+ const sent: Sent[] = []
32
+ const fetchImpl = (async (url: string, init: RequestInit) => {
33
+ sent.push({ url, headers: init.headers as Record<string, string>, body: JSON.parse(init.body as string) })
34
+ return new Response(JSON.stringify(answer), { status, headers: { 'content-type': 'application/json' } })
35
+ }) as unknown as typeof fetch
36
+
37
+ return { sent, fetchImpl }
38
+ }
39
+
40
+ /** Run the guard over a recorded request, as the server would; resolves the caller it saw. */
41
+ const verify = async (guard: ReturnType<typeof makeBasicEd25519Guard>, sent: Sent): Promise<Auth | undefined> => {
42
+ let auth: Auth | undefined
43
+ const request = {
44
+ alias: 'tech', body: sent.body, headers: sent.headers, params: {}, query: {}, path: new URL(sent.url).pathname,
45
+ } satisfies AbstractRequest
46
+ const response: AbstractResponse<Auth> = { resolve: value => { auth = value }, reject: error => { throw error } }
47
+ expect(await guard.match(request)).toBe(true)
48
+ await guard.handle(request, response)
49
+
50
+ return auth
51
+ }
52
+
53
+ describe('makeEmailTechClient', () => {
54
+ test('signs the waiver request the way GUARD_ED25519 verifies it, as the key\'s own record', async () => {
55
+ const { key, record, guard } = fixture()
56
+ const { sent, fetchImpl } = recorder({ waiver: 'signed-waiver', expiresAt: new Date().toISOString() })
57
+ const client = makeEmailTechClient({ apiUrl: 'https://app.test/api/', privateKey: key.export(), fetchImpl })
58
+
59
+ expect(await client.waiver()).toBe('signed-waiver')
60
+ expect(sent[0].url).toBe('https://app.test/api/auth/email/tech/waiver')
61
+ expect(sent[0].body).toEqual({})
62
+ const auth = await verify(guard, sent[0])
63
+ // No credentialId given: the record is found by the key's address, the id OwlMeans deployments use.
64
+ expect(auth?.userId).toBe(record.id)
65
+ expect(auth?.scopes).toEqual(['shared-key'])
66
+ })
67
+
68
+ test('rewrites a challenge\'s PIN — the given one, or six random digits — on its own path', async () => {
69
+ const { key, guard } = fixture()
70
+ const { sent, fetchImpl } = recorder({ ok: true })
71
+ const client = makeEmailTechClient({
72
+ apiUrl: 'https://app.test', privateKey: key.export(), path: 'login/email/', fetchImpl,
73
+ })
74
+
75
+ expect(await client.rewritePin('challenge-1', '012345')).toBe('012345')
76
+ const random = await client.rewritePin('challenge-2')
77
+ expect(random).toMatch(/^[0-9]{6}$/)
78
+
79
+ expect(sent[0].url).toBe('https://app.test/login/email/tech/pin')
80
+ expect(sent[0].body).toEqual({ challenge: 'challenge-1', pin: '012345' })
81
+ expect(sent[1].body).toEqual({ challenge: 'challenge-2', pin: random })
82
+ expect((await verify(guard, sent[1]))?.userId).toBe(key.exportAddress())
83
+ })
84
+
85
+ test('refuses a malformed PIN before sending, and reports a refusal or a PIN nothing was rewritten for', async () => {
86
+ const { key } = fixture()
87
+ const refused = recorder({}, 404)
88
+ await expect(makeEmailTechClient({ apiUrl: 'https://a.test', privateKey: key.export(), fetchImpl: refused.fetchImpl })
89
+ .waiver()).rejects.toThrow('-> 404')
90
+
91
+ const noop = recorder({ ok: false })
92
+ const client = makeEmailTechClient({ apiUrl: 'https://a.test', privateKey: key.export(), fetchImpl: noop.fetchImpl })
93
+ await expect(client.rewritePin('challenge', '12ab56')).rejects.toBeInstanceOf(SyntaxError)
94
+ expect(noop.sent).toHaveLength(0)
95
+ await expect(client.rewritePin('challenge')).rejects.toThrow('not rewritten')
96
+ })
97
+
98
+ test('an explicit credentialId names the record instead of the key\'s address', async () => {
99
+ const { key } = fixture()
100
+ const { sent, fetchImpl } = recorder({ waiver: 'w', expiresAt: '' })
101
+ await makeEmailTechClient({ apiUrl: 'https://a.test', privateKey: key.export(), credentialId: 'did:custom', fetchImpl }).waiver()
102
+
103
+ expect(sent[0].headers.authorization).toContain('Credential=did:custom ')
104
+ })
105
+ })
package/tsconfig.json CHANGED
@@ -11,6 +11,7 @@
11
11
  "./dist/**/*",
12
12
  "./build/**/*",
13
13
  "./harness/**/*",
14
+ "./tests/**/*",
14
15
  "./*.ts"
15
16
  ]
16
17
  }