From cd6a11e269a25345988f44570af3191192c6ec31 Mon Sep 17 00:00:00 2001 From: Max Peintner Date: Tue, 29 Oct 2024 10:44:03 +0100 Subject: [PATCH] fix tests --- apps/login/cypress/integration/register.cy.ts | 39 ++++- apps/login/cypress/integration/verify.cy.ts | 157 +++++++++++------- apps/login/mock/mocked-services.cfg | 1 + .../choose-authenticator-to-setup.tsx | 2 +- apps/login/src/components/verify-form.tsx | 3 - apps/login/src/lib/server/email.ts | 4 +- apps/login/src/lib/server/register.ts | 12 +- 7 files changed, 136 insertions(+), 82 deletions(-) diff --git a/apps/login/cypress/integration/register.cy.ts b/apps/login/cypress/integration/register.cy.ts index bf23fc40372..902c6a8061a 100644 --- a/apps/login/cypress/integration/register.cy.ts +++ b/apps/login/cypress/integration/register.cy.ts @@ -12,13 +12,40 @@ describe("register", () => { }); stub("zitadel.user.v2.UserService", "AddHumanUser", { data: { - userId: "123", - email: { - email: "john@zitadel.com", + userId: "221394658884845598", + }, + }); + stub("zitadel.session.v2.SessionService", "CreateSession", { + data: { + details: { + sequence: 859, + changeDate: new Date("2024-04-04T09:40:55.577Z"), + resourceOwner: "220516472055706145", }, - profile: { - givenName: "John", - familyName: "Doe", + sessionId: "221394658884845598", + sessionToken: + "SDMc7DlYXPgwRJ-Tb5NlLqynysHjEae3csWsKzoZWLplRji0AYY3HgAkrUEBqtLCvOayLJPMd0ax4Q", + challenges: undefined, + }, + }); + + stub("zitadel.session.v2.SessionService", "GetSession", { + data: { + session: { + id: "221394658884845598", + creationDate: new Date("2024-04-04T09:40:55.577Z"), + changeDate: new Date("2024-04-04T09:40:55.577Z"), + sequence: 859, + factors: { + user: { + id: "221394658884845598", + loginName: "john@zitadel.com", + }, + password: undefined, + webAuthN: undefined, + intent: undefined, + }, + metadata: {}, }, }, }); diff --git a/apps/login/cypress/integration/verify.cy.ts b/apps/login/cypress/integration/verify.cy.ts index d9688c631c5..7fe4f4afb6c 100644 --- a/apps/login/cypress/integration/verify.cy.ts +++ b/apps/login/cypress/integration/verify.cy.ts @@ -1,54 +1,89 @@ import { stub } from "../support/mock"; describe("verify invite", () => { - // beforeEach(() => { - // stub("zitadel.org.v2.OrganizationService", "ListOrganizations", { - // data: { - // details: { - // totalResult: 1, - // }, - // result: [{ id: "256088834543534543" }], - // }, - // }); + beforeEach(() => { + stub("zitadel.org.v2.OrganizationService", "ListOrganizations", { + data: { + details: { + totalResult: 1, + }, + result: [{ id: "256088834543534543" }], + }, + }); - // stub("zitadel.user.v2.UserService", "ListAuthenticationMethodTypes", { - // data: { - // authMethodTypes: [], - // }, - // }); + stub("zitadel.user.v2.UserService", "ListAuthenticationMethodTypes", { + data: { + authMethodTypes: [], + }, + }); - // stub("zitadel.user.v2.UserService", "GetUserById", { - // data: { - // user: { - // userId: "221394658884845598", - // state: 1, - // username: "john@zitadel.com", - // loginNames: ["john@zitadel.com"], - // preferredLoginName: "john@zitadel.com", - // human: { - // userId: "221394658884845598", - // state: 1, - // username: "john@zitadel.com", - // loginNames: ["john@zitadel.com"], - // preferredLoginName: "john@zitadel.com", - // profile: { - // givenName: "John", - // familyName: "Doe", - // avatarUrl: "https://zitadel.com/avatar.jpg", - // }, - // email: { - // email: "john@zitadel.com", - // isVerified: true, - // }, - // }, - // }, - // }, - // }); - // }); + stub("zitadel.user.v2.UserService", "GetUserByID", { + data: { + user: { + userId: "221394658884845598", + state: 1, + username: "john@zitadel.com", + loginNames: ["john@zitadel.com"], + preferredLoginName: "john@zitadel.com", + human: { + userId: "221394658884845598", + state: 1, + username: "john@zitadel.com", + loginNames: ["john@zitadel.com"], + preferredLoginName: "john@zitadel.com", + profile: { + givenName: "John", + familyName: "Doe", + avatarUrl: "https://zitadel.com/avatar.jpg", + }, + email: { + email: "john@zitadel.com", + isVerified: false, // email is not verified yet + }, + }, + }, + }, + }); + + stub("zitadel.session.v2.SessionService", "CreateSession", { + data: { + details: { + sequence: 859, + changeDate: new Date("2024-04-04T09:40:55.577Z"), + resourceOwner: "220516472055706145", + }, + sessionId: "221394658884845598", + sessionToken: + "SDMc7DlYXPgwRJ-Tb5NlLqynysHjEae3csWsKzoZWLplRji0AYY3HgAkrUEBqtLCvOayLJPMd0ax4Q", + challenges: undefined, + }, + }); + + stub("zitadel.session.v2.SessionService", "GetSession", { + data: { + session: { + id: "221394658884845598", + creationDate: new Date("2024-04-04T09:40:55.577Z"), + changeDate: new Date("2024-04-04T09:40:55.577Z"), + sequence: 859, + factors: { + user: { + id: "221394658884845598", + loginName: "john@zitadel.com", + }, + password: undefined, + webAuthN: undefined, + intent: undefined, + }, + metadata: {}, + }, + }, + }); + }); it.only("shows authenticators after successful invite verification", () => { stub("zitadel.user.v2.UserService", "VerifyInviteCode"); - cy.visit("/verify?userId=123&code=abc&invite=true"); + cy.visit("/verify?userId=221394658884845598&code=abc&invite=true"); cy.location("pathname", { timeout: 10_000 }).should( "eq", "/authenticator/set", @@ -62,32 +97,32 @@ describe("verify invite", () => { }); // TODO: Avoid uncaught exception in application cy.once("uncaught:exception", () => false); - cy.visit("/verify?userId=123&code=abc&invite=true"); + cy.visit("/verify?userId=221394658884845598&code=abc&invite=true"); cy.contains("Could not verify invite", { timeout: 10_000 }); }); }); describe("verify email", () => { - // beforeEach(() => { - // stub("zitadel.org.v2.OrganizationService", "ListOrganizations", { - // data: { - // details: { - // totalResult: 1, - // }, - // result: [{ id: "256088834543534543" }], - // }, - // }); + beforeEach(() => { + stub("zitadel.org.v2.OrganizationService", "ListOrganizations", { + data: { + details: { + totalResult: 1, + }, + result: [{ id: "256088834543534543" }], + }, + }); - // stub("zitadel.user.v2.UserService", "ListAuthenticationMethodTypes", { - // data: { - // authMethodTypes: [], - // }, - // }); - // }); + stub("zitadel.user.v2.UserService", "ListAuthenticationMethodTypes", { + data: { + authMethodTypes: [], + }, + }); + }); it("shows password and passkey method after successful invite verification", () => { stub("zitadel.user.v2.UserService", "VerifyEmail"); - cy.visit("/verify?userId=123&code=abc"); + cy.visit("/verify?userId=221394658884845598&code=abc"); cy.location("pathname", { timeout: 10_000 }).should( "eq", "/authenticator/set", @@ -101,7 +136,7 @@ describe("verify email", () => { }); // TODO: Avoid uncaught exception in application cy.once("uncaught:exception", () => false); - cy.visit("/verify?userId=123&code=abc&submit=true"); + cy.visit("/verify?userId=221394658884845598&code=abc&submit=true"); cy.contains("Could not verify email", { timeout: 10_000 }); }); }); diff --git a/apps/login/mock/mocked-services.cfg b/apps/login/mock/mocked-services.cfg index 72e4d987487..6a758ab8c12 100644 --- a/apps/login/mock/mocked-services.cfg +++ b/apps/login/mock/mocked-services.cfg @@ -1,4 +1,5 @@ zitadel/user/v2/user_service.proto +zitadel/org/v2/org_service.proto zitadel/session/v2/session_service.proto zitadel/settings/v2/settings_service.proto zitadel/management.proto diff --git a/apps/login/src/components/choose-authenticator-to-setup.tsx b/apps/login/src/components/choose-authenticator-to-setup.tsx index fe1f66132cc..25ca69e7768 100644 --- a/apps/login/src/components/choose-authenticator-to-setup.tsx +++ b/apps/login/src/components/choose-authenticator-to-setup.tsx @@ -25,7 +25,7 @@ export function ChooseAuthenticatorToSetup({ } else { return ( <> - {loginSettings.passkeysType === PasskeysType.ALLOWED && + {loginSettings.passkeysType === PasskeysType.NOT_ALLOWED && !loginSettings.allowUsernamePassword && ( {t("noMethodsAvailable")} )} diff --git a/apps/login/src/components/verify-form.tsx b/apps/login/src/components/verify-form.tsx index f0f60005b46..b43c0eaa42e 100644 --- a/apps/login/src/components/verify-form.tsx +++ b/apps/login/src/components/verify-form.tsx @@ -3,7 +3,6 @@ import { Alert } from "@/components/alert"; import { resendVerification, sendVerification } from "@/lib/server/email"; import { useTranslations } from "next-intl"; -import { useRouter } from "next/navigation"; import { useCallback, useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { Button, ButtonVariants } from "./button"; @@ -35,8 +34,6 @@ export function VerifyForm({ userId, code, isInvite, params }: Props) { const [loading, setLoading] = useState(false); - const router = useRouter(); - async function resendCode() { setError(""); setLoading(true); diff --git a/apps/login/src/lib/server/email.ts b/apps/login/src/lib/server/email.ts index f1925317a44..d2ab8496c34 100644 --- a/apps/login/src/lib/server/email.ts +++ b/apps/login/src/lib/server/email.ts @@ -74,7 +74,7 @@ export async function sendVerification(command: VerifyUserByEmailCommand) { if (session.factors?.user?.loginName) { params.set("loginName", session.factors?.user?.loginName); } - return redirect("/authenticator/set?" + params); + return redirect(`/authenticator/set?${params}`); } } @@ -134,6 +134,6 @@ export async function sendVerificationRedirectWithoutCheck(command: { if (session.factors?.user?.loginName) { params.set("loginName", session.factors?.user?.loginName); } - return redirect("/authenticator/set?" + params); + return redirect(`/authenticator/set?${params}`); } } diff --git a/apps/login/src/lib/server/register.ts b/apps/login/src/lib/server/register.ts index a3ad8a09272..a6d8e406437 100644 --- a/apps/login/src/lib/server/register.ts +++ b/apps/login/src/lib/server/register.ts @@ -54,13 +54,7 @@ export async function registerUser(command: RegisterUserCommand) { checks, undefined, command.authRequestId, - ).then((session) => { - return { - userId: human.userId, - sessionId: session.id, - factors: session.factors, - }; - }); + ); if (!session || !session.factors?.user) { return { error: "Could not create session" }; @@ -83,9 +77,9 @@ export async function registerUser(command: RegisterUserCommand) { organization: session.factors.user.organizationId, }); - if (command.authRequestId && session.userId) { + if (command.authRequestId && session.factors.user.id) { params.append("authRequest", command.authRequestId); - params.append("sessionId", session.sessionId); + params.append("sessionId", session.id); return redirect("/login?" + params); } else {