Files
zitadel/acceptance/tests/user.ts

172 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-11-15 13:48:42 +01:00
import { Page } from "@playwright/test";
import { registerWithPasskey } from "./register";
2024-11-28 09:57:20 +01:00
import { activateOTP, addTOTP, addUser, getUserByUsername, removeUser } from "./zitadel";
2024-10-28 19:44:50 +01:00
export interface userProps {
2024-11-15 13:48:42 +01:00
email: string;
firstName: string;
lastName: string;
organization: string;
password: string;
2024-11-20 14:22:22 +01:00
phone: string;
2024-10-28 19:44:50 +01:00
}
class User {
2024-11-15 13:48:42 +01:00
private readonly props: userProps;
private user: string;
constructor(userProps: userProps) {
this.props = userProps;
}
async ensure(page: Page) {
2024-11-28 09:57:20 +01:00
const response = await addUser(this.props);
2024-11-15 14:54:14 +01:00
2024-11-28 09:57:20 +01:00
this.setUserId(response.userId);
2024-11-15 13:48:42 +01:00
}
2024-11-28 14:38:13 +01:00
async cleanup() {
await removeUser(this.getUserId());
2024-11-15 13:48:42 +01:00
}
public setUserId(userId: string) {
this.user = userId;
}
public getUserId() {
return this.user;
}
public getUsername() {
return this.props.email;
}
public getPassword() {
return this.props.password;
}
public getFirstname() {
return this.props.firstName;
}
public getLastname() {
return this.props.lastName;
}
2024-11-20 14:22:22 +01:00
public getPhone() {
return this.props.phone;
}
2024-11-15 13:48:42 +01:00
public getFullName() {
2024-11-15 14:27:46 +01:00
return `${this.props.firstName} ${this.props.lastName}`;
2024-11-15 13:48:42 +01:00
}
2024-10-28 19:44:50 +01:00
}
2024-11-28 14:38:13 +01:00
export class PasswordUser extends User {
async ensure(page: Page) {
await super.ensure(page);
// wait for projection of user
await page.waitForTimeout(2000);
}
}
2024-10-28 19:44:50 +01:00
2024-11-13 19:48:25 +01:00
export enum OtpType {
2024-11-15 13:48:42 +01:00
sms = "sms",
email = "email",
}
export interface otpUserProps {
2024-11-15 13:48:42 +01:00
email: string;
firstName: string;
lastName: string;
organization: string;
password: string;
2024-11-20 14:22:22 +01:00
phone: string;
2024-11-15 13:48:42 +01:00
type: OtpType;
2024-10-28 19:44:50 +01:00
}
export class PasswordUserWithOTP extends User {
2024-11-15 13:48:42 +01:00
private type: OtpType;
constructor(props: otpUserProps) {
super({
email: props.email,
firstName: props.firstName,
lastName: props.lastName,
organization: props.organization,
password: props.password,
2024-11-20 14:22:22 +01:00
phone: props.phone,
2024-11-15 13:48:42 +01:00
});
this.type = props.type;
}
async ensure(page: Page) {
await super.ensure(page);
2024-11-28 09:57:20 +01:00
await activateOTP(this.getUserId(), this.type);
2024-11-15 13:48:42 +01:00
2024-11-28 09:57:20 +01:00
// wait for projection of user
await page.waitForTimeout(2000);
}
}
export class PasswordUserWithTOTP extends User {
private secret: string;
async ensure(page: Page) {
await super.ensure(page);
this.secret = await addTOTP(this.getUserId());
2024-11-15 14:54:14 +01:00
// wait for projection of user
2024-11-15 15:05:38 +01:00
await page.waitForTimeout(2000);
2024-11-15 13:48:42 +01:00
}
2024-11-28 09:57:20 +01:00
public getSecret(): string {
return this.secret;
}
}
export interface passkeyUserProps {
2024-11-15 13:48:42 +01:00
email: string;
firstName: string;
lastName: string;
organization: string;
2024-11-20 14:22:22 +01:00
phone: string;
}
export class PasskeyUser extends User {
2024-11-15 13:48:42 +01:00
private authenticatorId: string;
constructor(props: passkeyUserProps) {
super({
email: props.email,
firstName: props.firstName,
lastName: props.lastName,
organization: props.organization,
password: "",
2024-11-20 14:22:22 +01:00
phone: props.phone,
2024-11-15 13:48:42 +01:00
});
}
public async ensure(page: Page) {
const authId = await registerWithPasskey(page, this.getFirstname(), this.getLastname(), this.getUsername());
this.authenticatorId = authId;
2024-11-15 14:54:14 +01:00
// wait for projection of user
2024-11-15 15:05:38 +01:00
await page.waitForTimeout(2000);
2024-11-15 13:48:42 +01:00
}
2024-11-28 14:38:13 +01:00
async cleanup() {
const resp: any = await getUserByUsername(this.getUsername());
if (!resp || !resp.result || !resp.result[0]) {
return;
}
await removeUser(resp.result[0].userId);
}
2024-11-15 13:48:42 +01:00
public getAuthenticatorId(): string {
return this.authenticatorId;
}
}