reduce test code

This commit is contained in:
Elio Bischof
2023-07-05 19:06:21 +02:00
parent aa16fb0910
commit 1bd5ea4e2d
4 changed files with 37 additions and 67 deletions

View File

@@ -1,9 +1,8 @@
import { addStub, removeStub } from "../support/mock";
import { stub } from "../support/mock";
describe("login", () => {
beforeEach(() => {
removeStub("zitadel.session.v2alpha.SessionService", "CreateSession");
addStub("zitadel.session.v2alpha.SessionService", "CreateSession", {
stub("zitadel.session.v2alpha.SessionService", "CreateSession", {
data: {
details: {
sequence: 859,
@@ -17,8 +16,7 @@ describe("login", () => {
},
});
removeStub("zitadel.session.v2alpha.SessionService", "GetSession");
addStub("zitadel.session.v2alpha.SessionService", "GetSession", {
stub("zitadel.session.v2alpha.SessionService", "GetSession", {
data: {
session: {
id: "221394658884845598",
@@ -40,8 +38,7 @@ describe("login", () => {
},
});
removeStub("zitadel.settings.v2alpha.SessionService", "GetLoginSettings");
addStub("zitadel.settings.v2alpha.SettingsService", "GetLoginSettings", {
stub("zitadel.settings.v2alpha.SettingsService", "GetLoginSettings", {
data: {
settings: {
passkeysType: 1,
@@ -51,11 +48,7 @@ describe("login", () => {
});
describe("password login", () => {
beforeEach(() => {
removeStub(
"zitadel.user.v2alpha.UserService",
"ListAuthenticationMethodTypes"
);
addStub(
stub(
"zitadel.user.v2alpha.UserService",
"ListAuthenticationMethodTypes",
{
@@ -69,15 +62,33 @@ describe("login", () => {
cy.visit("/loginname?loginName=johndoe%40zitadel.com&submit=true");
cy.location("pathname", { timeout: 10_000 }).should("eq", "/password");
});
describe("with passkey prompt", () => {
beforeEach(() => {
stub("zitadel.session.v2alpha.SessionService", "SetSession", {
data: {
details: {
sequence: 859,
changeDate: "2023-07-04T07:58:20.126Z",
resourceOwner: "220516472055706145",
},
sessionToken:
"SDMc7DlYXPgwRJ-Tb5NlLqynysHjEae3csWsKzoZWLplRji0AYY3HgAkrUEBqtLCvOayLJPMd0ax4Q",
challenges: undefined,
},
});
});
it("should prompt a user to setup passwordless authentication if passkey is allowed in the login settings", () => {
cy.visit("/loginname?loginName=john%40zitadel.com&submit=true");
cy.location("pathname", { timeout: 10_000 }).should("eq", "/password");
cy.get('input[type="password"]').focus().type("MyStrongPassword!1");
cy.get('button[type="submit"]').click();
cy.location("pathname", { timeout: 10_000 }).should("eq", "/passkey/add");
});
});
});
describe("passkey login", () => {
beforeEach(() => {
removeStub(
"zitadel.user.v2alpha.UserService",
"ListAuthenticationMethodTypes"
);
addStub(
stub(
"zitadel.user.v2alpha.UserService",
"ListAuthenticationMethodTypes",
{
@@ -95,43 +106,4 @@ describe("login", () => {
);
});
});
describe("password login with passkey prompt", () => {
beforeEach(() => {
removeStub(
"zitadel.user.v2alpha.UserService",
"ListAuthenticationMethodTypes"
);
addStub(
"zitadel.user.v2alpha.UserService",
"ListAuthenticationMethodTypes",
{
data: {
authMethodTypes: [1], // 1 for password authentication
},
}
);
removeStub("zitadel.session.v2alpha.SessionService", "SetSession");
addStub("zitadel.session.v2alpha.SessionService", "SetSession", {
data: {
details: {
sequence: 859,
changeDate: "2023-07-04T07:58:20.126Z",
resourceOwner: "220516472055706145",
},
sessionToken:
"SDMc7DlYXPgwRJ-Tb5NlLqynysHjEae3csWsKzoZWLplRji0AYY3HgAkrUEBqtLCvOayLJPMd0ax4Q",
challenges: undefined,
},
});
});
it("should prompt a user to setup passwordless authentication if passkey is allowed in the login settings", () => {
cy.visit("/loginname?loginName=john%40zitadel.com&submit=true");
cy.location("pathname", { timeout: 10_000 }).should("eq", "/password");
cy.get('input[type="password"]').focus().type("MyStrongPassword!1");
cy.get('button[type="submit"]').click();
cy.location("pathname", { timeout: 10_000 }).should("eq", "/passkey/add");
});
});
});

View File

@@ -1,9 +1,8 @@
import { addStub, removeStub } from "../support/mock";
import { stub } from "../support/mock";
describe("register", () => {
beforeEach(() => {
removeStub("zitadel.user.v2alpha.UserService", "AddHumanUser");
addStub("zitadel.user.v2alpha.UserService", "AddHumanUser", {
stub("zitadel.user.v2alpha.UserService", "AddHumanUser", {
data: {
userId: "123",
},

View File

@@ -1,15 +1,13 @@
import { addStub, removeStub } from "../support/mock";
import { stub } from "../support/mock";
describe("/verify", () => {
it("redirects after successful email verification", () => {
removeStub("zitadel.user.v2alpha.UserService", "VerifyEmail");
addStub("zitadel.user.v2alpha.UserService", "VerifyEmail");
stub("zitadel.user.v2alpha.UserService", "VerifyEmail");
cy.visit("/verify?userID=123&code=abc&submit=true");
cy.location("pathname", { timeout: 10_000 }).should("eq", "/loginname");
});
it("shows an error if validation failed", () => {
removeStub("zitadel.user.v2alpha.UserService", "VerifyEmail");
addStub("zitadel.user.v2alpha.UserService", "VerifyEmail", {
stub("zitadel.user.v2alpha.UserService", "VerifyEmail", {
code: 3,
error: "error validating code",
});

View File

@@ -1,4 +1,4 @@
export function removeStub(service: string, method: string) {
function removeStub(service: string, method: string) {
return cy.request({
url: "http://localhost:22220/v1/stubs",
method: "DELETE",
@@ -9,7 +9,8 @@ export function removeStub(service: string, method: string) {
});
}
export function addStub(service: string, method: string, out?: any) {
export function stub(service: string, method: string, out?: any) {
removeStub(service, method)
return cy.request({
url: "http://localhost:22220/v1/stubs",
method: "POST",