Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -0,0 +1,119 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { SERVICE_WORKER_VERSION } from "./serviceWorkerRecoveryBootstrap";
|
||||
import {
|
||||
bootstrapTelephonyPush,
|
||||
decodeVapidPublicKey,
|
||||
registerTelephonyPushSubscription,
|
||||
serializePushSubscription,
|
||||
telephonyPushRegistrationPath,
|
||||
} from "./telephonyPush";
|
||||
|
||||
const vapidPublicKey = Buffer.from(Uint8Array.from({ length: 65 }, (_, index) => (index === 0 ? 0x04 : index))).toString("base64url");
|
||||
const p256dhKey = Buffer.from(Uint8Array.from({ length: 65 }, (_, index) => (index === 0 ? 0x04 : index + 1))).toString("base64url");
|
||||
const authKey = Buffer.alloc(16, 7).toString("base64url");
|
||||
|
||||
function pushSubscription(endpoint = "https://push.example.test/subscriptions/device-1"): PushSubscription {
|
||||
return {
|
||||
endpoint,
|
||||
toJSON: () => ({
|
||||
endpoint,
|
||||
expirationTime: null,
|
||||
keys: { p256dh: p256dhKey, auth: authKey },
|
||||
}),
|
||||
} as PushSubscription;
|
||||
}
|
||||
|
||||
function serviceWorkerRegistration(options: { existing?: PushSubscription | null; created?: PushSubscription } = {}) {
|
||||
const subscribe = vi.fn().mockResolvedValue(options.created ?? pushSubscription());
|
||||
const getSubscription = vi.fn().mockResolvedValue(options.existing ?? null);
|
||||
return {
|
||||
registration: { pushManager: { getSubscription, subscribe } } as unknown as ServiceWorkerRegistration,
|
||||
getSubscription,
|
||||
subscribe,
|
||||
};
|
||||
}
|
||||
|
||||
describe("telephony Web Push bootstrap", () => {
|
||||
it("decodes the canonical uncompressed VAPID public key", () => {
|
||||
const decoded = decodeVapidPublicKey(vapidPublicKey);
|
||||
expect(decoded).toHaveLength(65);
|
||||
expect(decoded[0]).toBe(0x04);
|
||||
expect(() => decodeVapidPublicKey("invalid!" )).toThrow(/public key is invalid/i);
|
||||
});
|
||||
|
||||
it("does not subscribe or register before the user has granted notification permission", async () => {
|
||||
const registration = serviceWorkerRegistration();
|
||||
const fetcher = vi.fn();
|
||||
const navigatorRef = {
|
||||
serviceWorker: { ready: Promise.resolve(registration.registration) },
|
||||
} as unknown as Navigator;
|
||||
|
||||
await expect(
|
||||
bootstrapTelephonyPush({
|
||||
enabled: true,
|
||||
tenantSlug: "hivegrid-demo",
|
||||
vapidPublicKey,
|
||||
csrfToken: "csrf-token",
|
||||
notificationPermission: "default",
|
||||
navigatorRef,
|
||||
fetcher: fetcher as typeof fetch,
|
||||
}),
|
||||
).resolves.toBe("permission-not-granted");
|
||||
expect(registration.getSubscription).not.toHaveBeenCalled();
|
||||
expect(fetcher).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("creates a missing PushSubscription and registers the serialized token through the same-origin resource route", async () => {
|
||||
const created = pushSubscription();
|
||||
const registration = serviceWorkerRegistration({ created });
|
||||
const fetcher = vi.fn().mockResolvedValue(Response.json({ ok: true }));
|
||||
|
||||
await registerTelephonyPushSubscription({
|
||||
registration: registration.registration,
|
||||
endpoint: "/hivegrid-demo/telephony/push-subscription",
|
||||
vapidPublicKey,
|
||||
csrfToken: "csrf-token",
|
||||
fetcher: fetcher as typeof fetch,
|
||||
});
|
||||
|
||||
expect(registration.subscribe).toHaveBeenCalledWith({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: expect.any(Uint8Array),
|
||||
});
|
||||
expect(fetcher).toHaveBeenCalledTimes(1);
|
||||
const [url, init] = fetcher.mock.calls[0] as [string, RequestInit];
|
||||
expect(url).toBe("/hivegrid-demo/telephony/push-subscription");
|
||||
expect(init).toMatchObject({
|
||||
method: "POST",
|
||||
credentials: "same-origin",
|
||||
headers: { "content-type": "application/json", "x-csrf-token": "csrf-token" },
|
||||
});
|
||||
const body = JSON.parse(String(init.body));
|
||||
expect(body).toMatchObject({ platform: "web", pushProvider: "webpush", appVersion: SERVICE_WORKER_VERSION });
|
||||
expect(JSON.parse(body.token)).toEqual(created.toJSON());
|
||||
});
|
||||
|
||||
it("reuses an existing subscription on cold start and reports server rejection", async () => {
|
||||
const existing = pushSubscription("https://push.example.test/subscriptions/existing");
|
||||
const registration = serviceWorkerRegistration({ existing });
|
||||
const fetcher = vi.fn().mockResolvedValue(new Response(null, { status: 503 }));
|
||||
|
||||
await expect(
|
||||
registerTelephonyPushSubscription({
|
||||
registration: registration.registration,
|
||||
endpoint: "/hivegrid-demo/telephony/push-subscription",
|
||||
vapidPublicKey,
|
||||
csrfToken: "csrf-token",
|
||||
fetcher: fetcher as typeof fetch,
|
||||
}),
|
||||
).rejects.toThrow("status 503");
|
||||
expect(registration.subscribe).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects unsafe tenant paths and incomplete browser subscription payloads", () => {
|
||||
expect(telephonyPushRegistrationPath("hivegrid-demo")).toBe("/hivegrid-demo/telephony/push-subscription");
|
||||
expect(() => telephonyPushRegistrationPath("//attacker.example")).toThrow(/tenant slug/i);
|
||||
expect(() => serializePushSubscription(pushSubscription("http://push.example.test/device"))).toThrow(/incomplete/i);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user