From 79799e64dd3d6f1c61d66bfcc99d13983c5952a6 Mon Sep 17 00:00:00 2001 From: Nathan Kramer Date: Wed, 13 Nov 2024 16:57:05 +1100 Subject: [PATCH] Add mock prompt API for testing rendering of forms --- package.json | 2 +- src/mock/api/post-login.ts | 6 ++++++ src/mock/api/prompt.ts | 20 ++++++++++++++++++++ src/test/api/prompt.test.ts | 23 +++++++++++++++++++++++ src/types/api/post-login.d.ts | 4 ++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/mock/api/prompt.ts create mode 100644 src/test/api/prompt.test.ts diff --git a/package.json b/package.json index f3cb479..7230525 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kilterset/auth0-actions-testing", - "version": "0.3.1", + "version": "0.3.2", "description": "Test and develop Auth0 Actions or Okta CIC Actions locally.", "repository": "https://github.com/kilterset/auth0-actions-testing", "homepage": "https://github.com/kilterset/auth0-actions-testing#readme", diff --git a/src/mock/api/post-login.ts b/src/mock/api/post-login.ts index 808b520..153e74b 100644 --- a/src/mock/api/post-login.ts +++ b/src/mock/api/post-login.ts @@ -7,6 +7,7 @@ import { accessMock } from "./access"; import { authenticationMock, FactorList } from "./authentication"; import { idTokenMock } from "./id-token"; import { multifactorMock } from "./multifactor"; +import { promptMock } from "./prompt"; import { redirectMock } from "./redirect"; import { userMock } from "./user"; import { samlResponseMock } from "./saml-response"; @@ -103,6 +104,7 @@ export function postLogin({ }); const idToken = idTokenMock("PostLogin"); const multifactor = multifactorMock("PostLogin"); + const prompt = promptMock("PostLogin"); const redirect = redirectMock("PostLogin", { now, request: requestValue, @@ -151,6 +153,10 @@ export function postLogin({ return multifactor.build(api); }, + get prompt() { + return prompt.build(api); + }, + get redirect() { return redirect.build(api); }, diff --git a/src/mock/api/prompt.ts b/src/mock/api/prompt.ts new file mode 100644 index 0000000..d09ef89 --- /dev/null +++ b/src/mock/api/prompt.ts @@ -0,0 +1,20 @@ +import { MultifactorEnableOptions } from "../../types"; + +interface PromptState { + promptId: string; + promptOptions?: { [key: string]: unknown }; +} + +export function promptMock(flow: string) { + const state: { rendered: PromptState | null } = { + rendered: null, + }; + + const build = (api: T) => ({ + render: (promptId: string, promptOptions?: { [key: string]: unknown }) => { + state.rendered = { promptId, promptOptions }; + }, + }); + + return { state, build }; +} diff --git a/src/test/api/prompt.test.ts b/src/test/api/prompt.test.ts new file mode 100644 index 0000000..3b90385 --- /dev/null +++ b/src/test/api/prompt.test.ts @@ -0,0 +1,23 @@ +import { deepStrictEqual, ok, strictEqual } from "node:assert"; +import test from "node:test"; +import { promptMock } from "../../mock/api/prompt"; + +test("prompt", async (t) => { + const baseApi = Symbol("Base API"); + + await t.test("render", async (t) => { + const { build, state } = promptMock("Another Flow"); + const api = build(baseApi); + + strictEqual(state.rendered, null); + + api.render("prompt-id", { fields: { foo: "bar" } }); + + ok(Boolean(state.rendered), "Expected prompt to be rendered"); + + deepStrictEqual(state.rendered, { + promptId: "prompt-id", + promptOptions: { fields: { foo: "bar" } }, + }); + }); +}); diff --git a/src/types/api/post-login.d.ts b/src/types/api/post-login.d.ts index 313e16a..be32926 100644 --- a/src/types/api/post-login.d.ts +++ b/src/types/api/post-login.d.ts @@ -50,6 +50,10 @@ export interface PostLogin { enable(provider: string, options?: MultifactorEnableOptions): PostLogin; }; + readonly prompt: { + render(promptId: string, promptOptions?: { [key: string]: unknown }): void; + }; + readonly rules: { wasExecuted(ruleId: string): boolean; };