Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 78 additions & 2 deletions web/src/flow/FlowExecutor.browser.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
import "#elements/messages/MessageContainer";
import { FlowExecutor } from "#flow/FlowExecutor";

import { ChallengeTypes, FlowsApi } from "@goauthentik/api";
import {
ChallengeTypes,
ContextualFlowInfoLayoutEnum,
FlowMessageLevelEnum,
FlowsApi,
} from "@goauthentik/api";

import { afterEach, expect, it, vi } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";

const mounted: HTMLElement[] = [];

function emailChallengeOf(...messages: string[]): ChallengeTypes {
return {
component: "ak-stage-email",
flowInfo: {
cancelUrl: "/flows/-/cancel/",
layout: ContextualFlowInfoLayoutEnum.Stacked,
messages: messages.map((message) => ({ level: FlowMessageLevelEnum.Success, message })),
},
};
}

async function createExecutor(): Promise<FlowExecutor> {
const container = document.createElement("ak-message-container");
const executor = new FlowExecutor();

vi.spyOn(executor, "refresh").mockResolvedValue(true);

executor.flowSlug = "test-flow";
executor.challenge = emailChallengeOf();

document.body.append(container, executor);
mounted.push(container, executor);

await executor.updateComplete;

return executor;
}

function shownMessages(): string[] {
return document.querySelector("ak-message-container")!.messages.map(({ message }) => message);
}

afterEach(() => {
document.querySelectorAll("form[data-test-autosubmit]").forEach((form) => form.remove());
vi.restoreAllMocks();

for (const element of mounted.splice(0)) {
element.remove();
}
});

it("preserves and disables the current stage during autosubmit navigation", async () => {
Expand Down Expand Up @@ -37,3 +81,35 @@ it("preserves and disables the current stage during autosubmit navigation", asyn
expect(executor.challenge).toBe(currentChallenge);
expect(executor.inert).toBe(true);
});

describe("FlowExecutor messages", () => {
it("shows the messages of every challenge returned by a submission", async () => {
vi.spyOn(FlowsApi.prototype, "flowsExecutorSolve").mockImplementation(async () =>
emailChallengeOf("Email Successfully sent."),
);

const executor = await createExecutor();

await executor.submit({ component: "ak-stage-email" });
await executor.submit({ component: "ak-stage-email" });
await executor.updateComplete;

expect(shownMessages()).toEqual(["Email Successfully sent.", "Email Successfully sent."]);
});

it("does not show the messages again when the theme changes", async () => {
vi.spyOn(FlowsApi.prototype, "flowsExecutorSolve").mockResolvedValue(
emailChallengeOf("Email Successfully sent."),
);

const executor = await createExecutor();

await executor.submit({ component: "ak-stage-email" });
await executor.updateComplete;

executor.activeTheme = executor.activeTheme === "dark" ? "light" : "dark";
await executor.updateComplete;

expect(shownMessages()).toEqual(["Email Successfully sent."]);
});
});
13 changes: 8 additions & 5 deletions web/src/flow/FlowExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,6 @@ export class FlowExecutor extends WithBrandConfig(Interface) implements StageHos
: this.ownerDocument.body;

applyBackgroundImageProperty(background, { target });

for (const message of flowMessages(this.challenge?.flowInfo?.messages)) {
showMessage(message);
}
}

//#region Listeners
Expand Down Expand Up @@ -273,7 +269,14 @@ export class FlowExecutor extends WithBrandConfig(Interface) implements StageHos
this.layout = this.challenge?.flowInfo?.layout || FlowExecutor.DefaultLayout;
}

if (changedProperties.has("flowInfo") || changedProperties.has("activeTheme")) {
// Shown once per challenge, so a theme change doesn't repeat them.
if (changedProperties.has("challenge")) {
for (const message of flowMessages(this.challenge?.flowInfo?.messages)) {
showMessage(message);
}
}

if (changedProperties.has("challenge") || changedProperties.has("activeTheme")) {
this.#synchronizeFlowInfo();
}
}
Expand Down
Loading