@supatype/client 0.3.6 → 0.8.0
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/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +69 -30
- package/.turbo/turbo-typecheck.log +2 -2
- package/LICENSE +1 -1
- package/dist/auth-redirect.d.ts +48 -0
- package/dist/auth-redirect.d.ts.map +1 -0
- package/dist/auth-redirect.js +70 -0
- package/dist/auth-redirect.js.map +1 -0
- package/dist/auth.d.ts +86 -4
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +383 -275
- package/dist/auth.js.map +1 -1
- package/dist/bindings.d.ts +16 -0
- package/dist/bindings.d.ts.map +1 -0
- package/dist/bindings.js +13 -0
- package/dist/bindings.js.map +1 -0
- package/dist/caller-types.d.ts +18 -0
- package/dist/caller-types.d.ts.map +1 -0
- package/dist/caller-types.js +22 -0
- package/dist/caller-types.js.map +1 -0
- package/dist/draft-create.d.ts +12 -0
- package/dist/draft-create.d.ts.map +1 -0
- package/dist/draft-create.js +47 -0
- package/dist/draft-create.js.map +1 -0
- package/dist/draft-models-registry.d.ts +27 -0
- package/dist/draft-models-registry.d.ts.map +1 -0
- package/dist/draft-models-registry.js +61 -0
- package/dist/draft-models-registry.js.map +1 -0
- package/dist/error-codes-doc.d.ts +1 -1
- package/dist/error-codes-doc.d.ts.map +1 -1
- package/dist/error-codes-doc.js +42 -34
- package/dist/error-codes-doc.js.map +1 -1
- package/dist/errors.d.ts +102 -60
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +69 -28
- package/dist/errors.js.map +1 -1
- package/dist/exact-response.d.ts.map +1 -1
- package/dist/exact-response.js +2 -1
- package/dist/exact-response.js.map +1 -1
- package/dist/exact-values.d.ts +2 -0
- package/dist/exact-values.d.ts.map +1 -1
- package/dist/exact-values.js +12 -10
- package/dist/exact-values.js.map +1 -1
- package/dist/fetch-with-retry.d.ts +21 -6
- package/dist/fetch-with-retry.d.ts.map +1 -1
- package/dist/fetch-with-retry.js +67 -53
- package/dist/fetch-with-retry.js.map +1 -1
- package/dist/field-kinds-registry.d.ts +11 -3
- package/dist/field-kinds-registry.d.ts.map +1 -1
- package/dist/field-kinds-registry.js +10 -6
- package/dist/field-kinds-registry.js.map +1 -1
- package/dist/filters.d.ts +51 -0
- package/dist/filters.d.ts.map +1 -0
- package/dist/filters.js +117 -0
- package/dist/filters.js.map +1 -0
- package/dist/generated-client.d.ts +7 -0
- package/dist/generated-client.d.ts.map +1 -0
- package/dist/generated-client.js +7 -0
- package/dist/generated-client.js.map +1 -0
- package/dist/graphql-document.d.ts +39 -0
- package/dist/graphql-document.d.ts.map +1 -0
- package/dist/graphql-document.js +228 -0
- package/dist/graphql-document.js.map +1 -0
- package/dist/graphql-json.d.ts +56 -0
- package/dist/graphql-json.d.ts.map +1 -0
- package/dist/graphql-json.js +220 -0
- package/dist/graphql-json.js.map +1 -0
- package/dist/graphql-transport.d.ts +28 -0
- package/dist/graphql-transport.d.ts.map +1 -0
- package/dist/graphql-transport.js +77 -0
- package/dist/graphql-transport.js.map +1 -0
- package/dist/index.d.ts +103 -40
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +170 -165
- package/dist/index.js.map +1 -1
- package/dist/json.d.ts +61 -0
- package/dist/json.d.ts.map +1 -0
- package/dist/json.js +83 -0
- package/dist/json.js.map +1 -0
- package/dist/jwt.d.ts +16 -0
- package/dist/jwt.d.ts.map +1 -0
- package/dist/jwt.js +37 -0
- package/dist/jwt.js.map +1 -0
- package/dist/live-preview.d.ts +23 -0
- package/dist/live-preview.d.ts.map +1 -0
- package/dist/live-preview.js +28 -0
- package/dist/live-preview.js.map +1 -0
- package/dist/mutation-request.d.ts +17 -0
- package/dist/mutation-request.d.ts.map +1 -0
- package/dist/mutation-request.js +41 -0
- package/dist/mutation-request.js.map +1 -0
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +8 -6
- package/dist/preview.js.map +1 -1
- package/dist/query-cache.d.ts.map +1 -1
- package/dist/query-cache.js +19 -13
- package/dist/query-cache.js.map +1 -1
- package/dist/query.d.ts +108 -29
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +294 -224
- package/dist/query.js.map +1 -1
- package/dist/realtime.d.ts +57 -2
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +300 -88
- package/dist/realtime.js.map +1 -1
- package/dist/select-types.d.ts +99 -0
- package/dist/select-types.d.ts.map +1 -0
- package/dist/select-types.js +2 -0
- package/dist/select-types.js.map +1 -0
- package/dist/storage.d.ts +81 -11
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +137 -38
- package/dist/storage.js.map +1 -1
- package/dist/table-subscription.d.ts +69 -0
- package/dist/table-subscription.d.ts.map +1 -0
- package/dist/table-subscription.js +93 -0
- package/dist/table-subscription.js.map +1 -0
- package/dist/types.d.ts +98 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +6 -1
- package/dist/types.js.map +1 -1
- package/package.json +6 -2
- package/src/auth-redirect.ts +90 -0
- package/src/auth.ts +434 -281
- package/src/bindings.ts +22 -0
- package/src/caller-types.ts +27 -0
- package/src/draft-create.ts +51 -0
- package/src/draft-models-registry.ts +81 -0
- package/src/error-codes-doc.ts +44 -35
- package/src/errors.ts +72 -28
- package/src/exact-response.ts +2 -1
- package/src/exact-values.ts +16 -9
- package/src/fetch-with-retry.ts +80 -70
- package/src/field-kinds-registry.ts +22 -9
- package/src/filters.ts +133 -0
- package/src/generated-client.ts +6 -0
- package/src/graphql-document.ts +234 -0
- package/src/graphql-json.ts +243 -0
- package/src/graphql-transport.ts +97 -0
- package/src/index.ts +289 -208
- package/src/json.ts +117 -0
- package/src/jwt.ts +37 -0
- package/src/live-preview.ts +37 -0
- package/src/mutation-request.ts +66 -0
- package/src/preview.ts +9 -11
- package/src/query-cache.ts +19 -16
- package/src/query.ts +389 -270
- package/src/realtime.ts +333 -90
- package/src/select-types.ts +184 -0
- package/src/storage.ts +223 -44
- package/src/table-subscription.ts +160 -0
- package/src/types.ts +104 -5
- package/tests/auth-error-codes.test.ts +53 -0
- package/tests/auth-errors.test.ts +84 -0
- package/tests/auth-events.test.ts +136 -0
- package/tests/auth-url-session.test.ts +204 -0
- package/tests/auth.test.ts +187 -26
- package/tests/count.test.ts +60 -0
- package/tests/data-plane-auth.test.ts +21 -46
- package/tests/draft-insert.integration.test.ts +118 -0
- package/tests/draft-insert.test.ts +225 -0
- package/tests/draft-profile.test.ts +55 -84
- package/tests/draft-update.integration.test.ts +122 -0
- package/tests/draft-update.test.ts +174 -0
- package/tests/dynamic-headers.test.ts +45 -0
- package/tests/entry-points.test.ts +42 -0
- package/tests/exact-response.test.ts +4 -4
- package/tests/exact-values.test.ts +14 -25
- package/tests/field-kinds-registry.test.ts +3 -3
- package/tests/filters.test.ts +146 -0
- package/tests/graphql-json.test.ts +199 -0
- package/tests/helpers/mock-websocket.ts +44 -11
- package/tests/helpers/oauth-binding.ts +92 -0
- package/tests/helpers/signup-binding.ts +73 -0
- package/tests/helpers/socket-swap.ts +142 -0
- package/tests/mfa-totp-integration.test.ts +13 -20
- package/tests/postgrest-error-codes.test.ts +85 -0
- package/tests/query-cache.test.ts +116 -117
- package/tests/query.test.ts +29 -31
- package/tests/rate-limit-integration.test.ts +4 -12
- package/tests/realtime-ack-ref.test.ts +112 -0
- package/tests/realtime-exact-values.test.ts +2 -4
- package/tests/realtime-presence.test.ts +75 -0
- package/tests/realtime-session-auth.test.ts +2 -4
- package/tests/realtime-shared-channel.test.ts +209 -0
- package/tests/realtime-swap-status.test.ts +62 -0
- package/tests/retry-integration.test.ts +3 -8
- package/tests/retry-policy.test.ts +166 -0
- package/tests/rpc-resend.test.ts +24 -0
- package/tests/server-bodies.test.ts +187 -0
- package/tests/serverless.test.ts +2 -2
- package/tests/signed-url-absolute.test.ts +2 -5
- package/tests/storage-object-shape.test.ts +101 -0
- package/tests/storage-quota-integration.test.ts +7 -9
- package/tests/storage-reference-url.test.ts +51 -0
- package/tests/storage-upload-browser.test.ts +71 -0
- package/tests/storage-upload-limit-integration.test.ts +17 -31
- package/tests/storage-url-encoding.test.ts +52 -0
- package/tests/subscribe-frame.test.ts +5 -11
- package/tests/support/blob.ts +10 -0
- package/tests/support/fetch-mock.ts +64 -0
- package/tests/support/storage-object.ts +37 -0
- package/tests/table-subscribe.test.ts +41 -14
- package/tests/table-subscription.test.ts +105 -0
- package/tests/table-transport.test.ts +94 -0
- package/tests/tsconfig.json +1 -1
- package/tests/types.test.ts +153 -1
- package/tsconfig.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { vi, describe, it, expect, beforeEach, afterEach } from "vitest"
|
|
2
|
+
import type { AuthRedirectError, Session, SupatypeError } from "../../src/types.js"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What every framework binding's auth primitive owes an OAuth sign-in.
|
|
6
|
+
*
|
|
7
|
+
* Shared by the Vue, Svelte and Solid suites, because each must start the redirect and surface the
|
|
8
|
+
* server's refusal the same way; a copy per binding would let one drift. Not named `*.test.ts`, so
|
|
9
|
+
* vitest's default include does not collect it as a suite.
|
|
10
|
+
*/
|
|
11
|
+
type OAuthResult = { data: { url: string; provider: string }; error: SupatypeError | null }
|
|
12
|
+
|
|
13
|
+
export interface OAuthBindingHarness {
|
|
14
|
+
auth: {
|
|
15
|
+
initialize: () => Promise<{ error: AuthRedirectError | null }>
|
|
16
|
+
getSession: () => Promise<{ data: { session: Session | null }; error: null }>
|
|
17
|
+
onAuthStateChange: () => { data: { subscription: { unsubscribe(): void } } }
|
|
18
|
+
signInWithOAuth: ReturnType<typeof vi.fn<(opts: { provider: string }) => Promise<OAuthResult>>>
|
|
19
|
+
}
|
|
20
|
+
/** The refusal the next mount's `initialize()` reports. */
|
|
21
|
+
redirectError: AuthRedirectError | null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface OAuthBindingProbe {
|
|
25
|
+
error(): AuthRedirectError | null | undefined
|
|
26
|
+
signInWithOAuth(provider: string): Promise<{ error: SupatypeError | null }>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function createHarness(): OAuthBindingHarness {
|
|
30
|
+
const harness: OAuthBindingHarness = {
|
|
31
|
+
redirectError: null,
|
|
32
|
+
auth: {
|
|
33
|
+
initialize: () => Promise.resolve({ error: harness.redirectError }),
|
|
34
|
+
getSession: () => Promise.resolve({ data: { session: null }, error: null }),
|
|
35
|
+
onAuthStateChange: () => ({ data: { subscription: { unsubscribe: () => undefined } } }),
|
|
36
|
+
signInWithOAuth: vi.fn<(opts: { provider: string }) => Promise<OAuthResult>>(),
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
return harness
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const settle = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0))
|
|
43
|
+
|
|
44
|
+
export function describeOAuthBinding(
|
|
45
|
+
name: string,
|
|
46
|
+
mount: (harness: OAuthBindingHarness) => OAuthBindingProbe,
|
|
47
|
+
unmount: () => void,
|
|
48
|
+
): void {
|
|
49
|
+
describe(`${name} OAuth`, () => {
|
|
50
|
+
let harness: OAuthBindingHarness
|
|
51
|
+
let assign: ReturnType<typeof vi.fn<(url: string) => void>>
|
|
52
|
+
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
harness = createHarness()
|
|
55
|
+
assign = vi.fn<(url: string) => void>()
|
|
56
|
+
vi.stubGlobal("window", { location: { href: "https://app.example/", assign } })
|
|
57
|
+
})
|
|
58
|
+
afterEach(() => {
|
|
59
|
+
unmount()
|
|
60
|
+
vi.unstubAllGlobals()
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it("sends the browser to the provider", async () => {
|
|
64
|
+
const url = "http://localhost:9999/auth/v1/authorize?provider=github"
|
|
65
|
+
harness.auth.signInWithOAuth.mockResolvedValue({ data: { url, provider: "github" }, error: null })
|
|
66
|
+
|
|
67
|
+
const { error } = await mount(harness).signInWithOAuth("github")
|
|
68
|
+
|
|
69
|
+
expect(error).toBeNull()
|
|
70
|
+
expect(assign).toHaveBeenCalledWith(url)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it("stays on the page and returns the error when the provider is not enabled", async () => {
|
|
74
|
+
const refusal = { message: "Sign-in with github is not enabled for this project.", code: "provider_disabled" }
|
|
75
|
+
harness.auth.signInWithOAuth.mockResolvedValue({ data: { url: "", provider: "github" }, error: refusal })
|
|
76
|
+
|
|
77
|
+
const { error } = await mount(harness).signInWithOAuth("github")
|
|
78
|
+
|
|
79
|
+
expect(error).toEqual(refusal)
|
|
80
|
+
expect(assign).not.toHaveBeenCalled()
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it("exposes a refusal the server sent back in the URL", async () => {
|
|
84
|
+
harness.redirectError = { message: "Signups not allowed", code: "signup_disabled", provider: "github" }
|
|
85
|
+
|
|
86
|
+
const probe = mount(harness)
|
|
87
|
+
await settle()
|
|
88
|
+
|
|
89
|
+
expect(probe.error()).toEqual(harness.redirectError)
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { vi, describe, it, expect, beforeEach, afterEach } from "vitest"
|
|
2
|
+
import { AuthClient } from "../../src/auth.js"
|
|
3
|
+
import type { Session, SupatypeError, User } from "../../src/types.js"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What every framework binding's auth primitive owes a sign-up that waits for email confirmation.
|
|
7
|
+
*
|
|
8
|
+
* The auth server answers such a sign-up with the bare user and no tokens. The binding must stay
|
|
9
|
+
* signed out and hand the caller what it needs to say "check your email". Shared by the Vue,
|
|
10
|
+
* Svelte and Solid suites over a real AuthClient, so the client's answer is what is under test.
|
|
11
|
+
* Not named `*.test.ts`, so vitest's default include does not collect it as a suite.
|
|
12
|
+
*/
|
|
13
|
+
export type SignUpBindingResult = {
|
|
14
|
+
data: { session: Session | null; user: User | null }
|
|
15
|
+
error: SupatypeError | null
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface SignUpBindingProbe {
|
|
19
|
+
user(): User | null | undefined
|
|
20
|
+
signUp(email: string, password: string): Promise<SignUpBindingResult>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const PENDING_USER = {
|
|
24
|
+
id: "user-pending",
|
|
25
|
+
email: "ada@example.com",
|
|
26
|
+
confirmation_sent_at: "2024-01-01T00:00:00Z",
|
|
27
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
28
|
+
updated_at: "2024-01-01T00:00:00Z",
|
|
29
|
+
app_metadata: { provider: "email" },
|
|
30
|
+
user_metadata: {},
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const settle = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0))
|
|
34
|
+
|
|
35
|
+
export function describeSignUpBinding(
|
|
36
|
+
name: string,
|
|
37
|
+
mount: (auth: AuthClient) => SignUpBindingProbe,
|
|
38
|
+
unmount: () => void,
|
|
39
|
+
): void {
|
|
40
|
+
describe(`${name} sign-up waiting for email confirmation`, () => {
|
|
41
|
+
let probe: SignUpBindingProbe
|
|
42
|
+
|
|
43
|
+
beforeEach(async () => {
|
|
44
|
+
vi.stubGlobal("fetch", vi.fn().mockResolvedValue({
|
|
45
|
+
ok: true,
|
|
46
|
+
status: 200,
|
|
47
|
+
json: () => Promise.resolve(PENDING_USER),
|
|
48
|
+
headers: { get: () => null },
|
|
49
|
+
}))
|
|
50
|
+
probe = mount(new AuthClient("http://localhost:9999/auth/v1", { apikey: "anon" }))
|
|
51
|
+
await settle()
|
|
52
|
+
})
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
unmount()
|
|
55
|
+
vi.unstubAllGlobals()
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it("stays signed out", async () => {
|
|
59
|
+
await probe.signUp("ada@example.com", "secret")
|
|
60
|
+
await settle()
|
|
61
|
+
|
|
62
|
+
expect(probe.user()).toBeNull()
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it("returns the user and no session, so the app can ask for the confirmation", async () => {
|
|
66
|
+
const { data, error } = await probe.signUp("ada@example.com", "secret")
|
|
67
|
+
|
|
68
|
+
expect(error).toBeNull()
|
|
69
|
+
expect(data.session).toBeNull()
|
|
70
|
+
expect(data.user?.id).toBe("user-pending")
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { vi, describe, it, expect, beforeEach, afterEach } from "vitest"
|
|
2
|
+
import { RealtimeClient } from "../../src/realtime.js"
|
|
3
|
+
import {
|
|
4
|
+
createMockWebSocketClass,
|
|
5
|
+
simulateServerMessage,
|
|
6
|
+
subscribedChannels,
|
|
7
|
+
type MockWebSocketInstance,
|
|
8
|
+
} from "./mock-websocket.js"
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A realtime client on a stand-in socket, with the sign-in that swaps that socket for a new one.
|
|
12
|
+
*
|
|
13
|
+
* Shared by the client's suite and by every framework binding's, because each binding must show
|
|
14
|
+
* the same status sequence across the same swap. A copy per binding would let one of them drift
|
|
15
|
+
* into testing a socket that no longer behaves like the client's.
|
|
16
|
+
*
|
|
17
|
+
* Not named `*.test.ts`, so vitest's default include does not collect it as a suite.
|
|
18
|
+
*/
|
|
19
|
+
export interface SocketSwapHarness {
|
|
20
|
+
realtime: RealtimeClient
|
|
21
|
+
/** Every socket opened so far, oldest first. */
|
|
22
|
+
sockets: MockWebSocketInstance[]
|
|
23
|
+
/** The newest socket. Fails the test when none has been opened. */
|
|
24
|
+
socket(): MockWebSocketInstance
|
|
25
|
+
/** Let queued connects and opens run. */
|
|
26
|
+
settle(): Promise<void>
|
|
27
|
+
/** The server acknowledges every join sent on the newest socket. */
|
|
28
|
+
ackJoins(): void
|
|
29
|
+
/** The server refuses a join on the newest socket. */
|
|
30
|
+
refuseJoin(): void
|
|
31
|
+
/** Change identity, which replaces the socket, and let the replacement open. */
|
|
32
|
+
signIn(): Promise<void>
|
|
33
|
+
/** Put the real WebSocket back. */
|
|
34
|
+
restore(): void
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function newestOf(sockets: MockWebSocketInstance[]): MockWebSocketInstance {
|
|
38
|
+
const socket = sockets[sockets.length - 1]
|
|
39
|
+
if (socket === undefined) throw new Error("no socket has been opened")
|
|
40
|
+
return socket
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function createSocketSwapHarness(): SocketSwapHarness {
|
|
44
|
+
const mock = createMockWebSocketClass()
|
|
45
|
+
vi.stubGlobal("WebSocket", mock.MockWebSocket)
|
|
46
|
+
let token = "anon"
|
|
47
|
+
const realtime = new RealtimeClient("ws://localhost:4000/realtime", async () => ({ Authorization: `Bearer ${token}` }))
|
|
48
|
+
const settle = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 10))
|
|
49
|
+
const reply = (status: "ok" | "error", message: string): void =>
|
|
50
|
+
simulateServerMessage(newestOf(mock.instances), { type: "system", status, message })
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
realtime,
|
|
54
|
+
sockets: mock.instances,
|
|
55
|
+
socket: () => newestOf(mock.instances),
|
|
56
|
+
settle,
|
|
57
|
+
ackJoins: () => {
|
|
58
|
+
for (const channel of subscribedChannels(newestOf(mock.instances))) reply("ok", `subscribed to ${channel}`)
|
|
59
|
+
},
|
|
60
|
+
refuseJoin: () => reply("error", "permission denied"),
|
|
61
|
+
signIn: async () => {
|
|
62
|
+
token = "user"
|
|
63
|
+
await realtime.reauthenticate()
|
|
64
|
+
await settle()
|
|
65
|
+
},
|
|
66
|
+
restore: () => vi.unstubAllGlobals(),
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The status a subscription shows at each point of a sign-in: subscribed, joined, swapped, and
|
|
72
|
+
* then joined again on the new socket, or refused there.
|
|
73
|
+
*
|
|
74
|
+
* `read` is whatever the subject under test exposes, read as it stands at that moment.
|
|
75
|
+
*/
|
|
76
|
+
export async function statusesAcrossSwap(
|
|
77
|
+
harness: SocketSwapHarness,
|
|
78
|
+
read: () => string,
|
|
79
|
+
{ refuse = false }: { refuse?: boolean } = {},
|
|
80
|
+
): Promise<string[]> {
|
|
81
|
+
const seen: string[] = []
|
|
82
|
+
await harness.settle()
|
|
83
|
+
seen.push(read())
|
|
84
|
+
harness.ackJoins()
|
|
85
|
+
seen.push(read())
|
|
86
|
+
await harness.signIn()
|
|
87
|
+
seen.push(read())
|
|
88
|
+
if (refuse) harness.refuseJoin()
|
|
89
|
+
else harness.ackJoins()
|
|
90
|
+
seen.push(read())
|
|
91
|
+
return seen
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** What a binding under test exposes, read as it stands. */
|
|
95
|
+
export interface StatusProbe {
|
|
96
|
+
status(): string
|
|
97
|
+
/** The error it exposes, when it exposes one. */
|
|
98
|
+
error?(): unknown
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** What the Vue, Svelte and Solid bindings read at each point of `statusesAcrossSwap`. */
|
|
102
|
+
export const BINDING_STATES = {
|
|
103
|
+
swap: ["connecting", "connected", "connecting", "connected"],
|
|
104
|
+
refused: ["connecting", "connected", "connecting", "error"],
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The two cases every binding's suite asserts, written once.
|
|
109
|
+
*
|
|
110
|
+
* `mount` starts the subscription against the harness it is given. `unmount` runs the cleanup the
|
|
111
|
+
* binding registered. The framework mocks stay in each suite, because `vi.mock` is hoisted per file.
|
|
112
|
+
*/
|
|
113
|
+
export function describeStatusAcrossSignIn(
|
|
114
|
+
subject: string,
|
|
115
|
+
mount: (harness: SocketSwapHarness) => StatusProbe,
|
|
116
|
+
unmount: () => void,
|
|
117
|
+
expected: { swap: string[]; refused: string[] } = BINDING_STATES,
|
|
118
|
+
): void {
|
|
119
|
+
describe(`${subject} status across sign-in`, () => {
|
|
120
|
+
let harness: SocketSwapHarness
|
|
121
|
+
|
|
122
|
+
beforeEach(() => {
|
|
123
|
+
harness = createSocketSwapHarness()
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
afterEach(() => {
|
|
127
|
+
unmount()
|
|
128
|
+
harness.restore()
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it("reads connecting from the swap until the new socket acknowledges the join", async () => {
|
|
132
|
+
const probe = mount(harness)
|
|
133
|
+
expect(await statusesAcrossSwap(harness, probe.status)).toEqual(expected.swap)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it("reads error when the new socket refuses the join", async () => {
|
|
137
|
+
const probe = mount(harness)
|
|
138
|
+
expect(await statusesAcrossSwap(harness, probe.status, { refuse: true })).toEqual(expected.refused)
|
|
139
|
+
if (probe.error) expect(probe.error()).not.toBeNull()
|
|
140
|
+
})
|
|
141
|
+
})
|
|
142
|
+
}
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import { describe, it, expect, vi, beforeEach } from "vitest"
|
|
9
9
|
import { AuthClient } from "../src/auth.js"
|
|
10
|
+
import { fetchCall, fetchReturning, jsonBody, type FetchMock } from "./support/fetch-mock.js"
|
|
10
11
|
|
|
11
12
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
12
13
|
|
|
@@ -59,13 +60,8 @@ function makeTokenResponse(overrides?: Record<string, unknown>) {
|
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
62
|
|
|
62
|
-
function mockFetch(body: unknown, ok = true, status?: number):
|
|
63
|
-
return
|
|
64
|
-
ok,
|
|
65
|
-
status: status ?? (ok ? 200 : 400),
|
|
66
|
-
json: vi.fn().mockResolvedValue(body),
|
|
67
|
-
headers: { get: () => null },
|
|
68
|
-
})
|
|
63
|
+
function mockFetch(body: unknown, ok = true, status?: number): FetchMock {
|
|
64
|
+
return fetchReturning(body, status ?? (ok ? 200 : 400))
|
|
69
65
|
}
|
|
70
66
|
|
|
71
67
|
function freshClient(): AuthClient {
|
|
@@ -110,11 +106,10 @@ describe("Task 89: MFA TOTP integration", () => {
|
|
|
110
106
|
expect(data!.type).toBe("totp")
|
|
111
107
|
|
|
112
108
|
// Verify POST was sent to /factors
|
|
113
|
-
const
|
|
109
|
+
const { url, init } = fetchCall(enrollFetch)
|
|
114
110
|
expect(url).toBe(`${SUPATYPE_URL}/factors`)
|
|
115
|
-
expect(
|
|
116
|
-
|
|
117
|
-
expect(body["factor_type"]).toBe("totp")
|
|
111
|
+
expect(init.method).toBe("POST")
|
|
112
|
+
expect(jsonBody(init)["factor_type"]).toBe("totp")
|
|
118
113
|
})
|
|
119
114
|
|
|
120
115
|
it("returns QR code and secret for TOTP enrollment", async () => {
|
|
@@ -243,8 +238,8 @@ describe("Task 89: MFA TOTP integration", () => {
|
|
|
243
238
|
expect(data.session).not.toBeNull()
|
|
244
239
|
// Decode JWT to check AAL
|
|
245
240
|
const parts = data.session!.accessToken.split(".")
|
|
246
|
-
const payload = JSON.parse(Buffer.from(parts[1]!, "base64url").toString())
|
|
247
|
-
expect(payload
|
|
241
|
+
const payload = JSON.parse(Buffer.from(parts[1]!, "base64url").toString())
|
|
242
|
+
expect(payload).toHaveProperty("aal", "aal1")
|
|
248
243
|
})
|
|
249
244
|
|
|
250
245
|
it("second step: TOTP verification elevates to AAL2", async () => {
|
|
@@ -271,11 +266,9 @@ describe("Task 89: MFA TOTP integration", () => {
|
|
|
271
266
|
|
|
272
267
|
expect(data.session).not.toBeNull()
|
|
273
268
|
const parts = data.session!.accessToken.split(".")
|
|
274
|
-
const payload = JSON.parse(Buffer.from(parts[1]!, "base64url").toString())
|
|
275
|
-
expect(payload
|
|
276
|
-
expect(payload
|
|
277
|
-
expect.arrayContaining([{ method: "password" }, { method: "totp" }]),
|
|
278
|
-
)
|
|
269
|
+
const payload = JSON.parse(Buffer.from(parts[1]!, "base64url").toString())
|
|
270
|
+
expect(payload).toHaveProperty("aal", "aal2")
|
|
271
|
+
expect(payload).toHaveProperty("amr", expect.arrayContaining([{ method: "password" }, { method: "totp" }]))
|
|
279
272
|
})
|
|
280
273
|
})
|
|
281
274
|
|
|
@@ -336,9 +329,9 @@ describe("Task 89: MFA TOTP integration", () => {
|
|
|
336
329
|
expect(data).not.toBeNull()
|
|
337
330
|
expect(data!.id).toBe(FACTOR_ID)
|
|
338
331
|
|
|
339
|
-
const
|
|
332
|
+
const { url, init } = fetchCall(unenrollFetch)
|
|
340
333
|
expect(url).toBe(`${SUPATYPE_URL}/factors/${FACTOR_ID}`)
|
|
341
|
-
expect(
|
|
334
|
+
expect(init.method).toBe("DELETE")
|
|
342
335
|
})
|
|
343
336
|
|
|
344
337
|
it("after unenroll, listFactors returns empty", async () => {
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest"
|
|
2
|
+
import { getErrorDocumentation } from "../src/error-codes-doc.js"
|
|
3
|
+
import { PostgrestErrorCodes } from "../src/errors.js"
|
|
4
|
+
import { MutationBuilder, QueryBuilder } from "../src/query.js"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The codes PostgREST 14 answers with, recorded from a live `postgrest/postgrest:v14.18` against
|
|
8
|
+
* Postgres 17, and the constant a caller would compare each one with.
|
|
9
|
+
*
|
|
10
|
+
* Two of these moved in PostgREST 13: an expired token is `PGRST303` (it was `PGRST301`), and a
|
|
11
|
+
* missing table is `PGRST205` (it was the SQLSTATE `42P01`). A database error is passed through
|
|
12
|
+
* under its SQLSTATE, with no `PGRST` prefix, so a constant spelled `PGRST23505` never matched a
|
|
13
|
+
* unique violation in any release.
|
|
14
|
+
*/
|
|
15
|
+
const RECORDED: Array<{ case: string; status: number; code: string; constant: string }> = [
|
|
16
|
+
{ case: "expired JWT", status: 401, code: "PGRST303", constant: PostgrestErrorCodes.JWT_EXPIRED },
|
|
17
|
+
{ case: "JWT with a bad signature", status: 401, code: "PGRST301", constant: PostgrestErrorCodes.JWT_INVALID },
|
|
18
|
+
{ case: "missing table", status: 404, code: "PGRST205", constant: PostgrestErrorCodes.UNDEFINED_TABLE },
|
|
19
|
+
{ case: "insert into an unknown column", status: 400, code: "PGRST204", constant: PostgrestErrorCodes.SCHEMA_CACHE_MISS },
|
|
20
|
+
{ case: "single() with no rows", status: 406, code: "PGRST116", constant: PostgrestErrorCodes.NOT_FOUND },
|
|
21
|
+
{ case: "row-level security denial, signed in", status: 403, code: "42501", constant: PostgrestErrorCodes.PERMISSION_DENIED },
|
|
22
|
+
{ case: "foreign key violation", status: 409, code: "23503", constant: PostgrestErrorCodes.FK_VIOLATION },
|
|
23
|
+
{ case: "unique violation", status: 409, code: "23505", constant: PostgrestErrorCodes.UNIQUE_VIOLATION },
|
|
24
|
+
{ case: "not-null violation", status: 400, code: "23502", constant: PostgrestErrorCodes.NOT_NULL_VIOLATION },
|
|
25
|
+
{ case: "check violation", status: 400, code: "23514", constant: PostgrestErrorCodes.CHECK_VIOLATION },
|
|
26
|
+
{ case: "invalid input syntax", status: 400, code: "22P02", constant: PostgrestErrorCodes.INVALID_INPUT },
|
|
27
|
+
{ case: "filter on an unknown column", status: 400, code: "42703", constant: PostgrestErrorCodes.UNDEFINED_COLUMN },
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
/** PostgREST 14's message for a `vnd.pgrst.object+json` request that did not find one row. */
|
|
31
|
+
const SINGLE_ROW_MESSAGE = "Cannot coerce the result to a single JSON object"
|
|
32
|
+
|
|
33
|
+
describe("PostgREST error codes", () => {
|
|
34
|
+
it.each(RECORDED)("$case: the constant is the code PostgREST answers", ({ code, constant }) => {
|
|
35
|
+
expect(constant).toBe(code)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it.each(RECORDED)("$case: the documentation has an entry for the code", ({ code, status }) => {
|
|
39
|
+
const entry = getErrorDocumentation(code)
|
|
40
|
+
expect(entry?.category).toBe("postgrest")
|
|
41
|
+
expect(entry?.httpStatus).toBe(status)
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
describe("single() without exactly one row", () => {
|
|
46
|
+
const emptyArray = (): Response =>
|
|
47
|
+
new Response("[]", { status: 200, headers: { "Content-Type": "application/json" } })
|
|
48
|
+
|
|
49
|
+
it("a read words it as PostgREST does", async () => {
|
|
50
|
+
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(emptyArray()))
|
|
51
|
+
const { error } = await new QueryBuilder("http://x", "/rest/v1/t", {}).single()
|
|
52
|
+
vi.unstubAllGlobals()
|
|
53
|
+
expect(error).toEqual({ message: SINGLE_ROW_MESSAGE, code: "PGRST116" })
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it("a write words it as PostgREST does", async () => {
|
|
57
|
+
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(emptyArray()))
|
|
58
|
+
const { error } = await new MutationBuilder("http://x", "/rest/v1/t", {}, "PATCH", {}).single()
|
|
59
|
+
vi.unstubAllGlobals()
|
|
60
|
+
expect(error).toEqual({ message: SINGLE_ROW_MESSAGE, code: "PGRST116" })
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
describe("a field validator's refusal", () => {
|
|
65
|
+
// Recorded from the kitchen-sink stack: what the gateway answers when `validate-chat-body` refuses.
|
|
66
|
+
const refused = (): Response =>
|
|
67
|
+
new Response(
|
|
68
|
+
JSON.stringify({ field: "body", message: "That reads as shouting.", error: "That reads as shouting." }),
|
|
69
|
+
{ status: 422, headers: { "Content-Type": "application/json" } },
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
it("keeps the field on a read", async () => {
|
|
73
|
+
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(refused()))
|
|
74
|
+
const { error } = await new QueryBuilder("http://x", "/rest/v1/t", {})
|
|
75
|
+
vi.unstubAllGlobals()
|
|
76
|
+
expect(error).toEqual({ message: "That reads as shouting.", status: 422, field: "body" })
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it("keeps the field on a write, which is where a validator runs", async () => {
|
|
80
|
+
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(refused()))
|
|
81
|
+
const { error } = await new MutationBuilder("http://x", "/rest/v1/t", { body: "HELLO" }, "POST", {})
|
|
82
|
+
vi.unstubAllGlobals()
|
|
83
|
+
expect(error).toEqual({ message: "That reads as shouting.", status: 422, field: "body" })
|
|
84
|
+
})
|
|
85
|
+
})
|