Files
zitadel/acceptance/tests/user.ts

217 lines
4.7 KiB
TypeScript
Raw Normal View History

2024-11-15 13:48:42 +01:00
import { Page } from "@playwright/test";
2024-11-15 14:27:46 +01:00
import axios from "axios";
2024-11-15 13:48:42 +01:00
import { registerWithPasskey } from "./register";
import { 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-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) {
await this.remove();
const body = {
username: this.props.email,
organization: {
orgId: this.props.organization,
},
profile: {
givenName: this.props.firstName,
familyName: this.props.lastName,
},
email: {
email: this.props.email,
isVerified: true,
},
password: {
password: this.props.password!,
},
};
2024-11-15 14:27:46 +01:00
try {
const response = await axios.post(`${process.env.ZITADEL_API_URL}/v2/users/human`, body, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.ZITADEL_SERVICE_USER_TOKEN}`,
},
});
if (response.status >= 400 && response.status !== 409) {
const error = `HTTP Error: ${response.status} - ${response.statusText}`;
console.error(error);
throw new Error(error);
}
} catch (error) {
console.error("Error making request:", error);
throw error;
2024-11-15 13:48:42 +01:00
}
2024-11-15 14:54:14 +01:00
// wait for projection of user
2024-11-15 15:05:38 +01:00
await page.waitForTimeout(3000);
2024-11-15 13:48:42 +01:00
}
async remove() {
2024-11-15 14:27:46 +01:00
const resp: any = await getUserByUsername(this.getUsername());
2024-11-15 13:48:42 +01:00
if (!resp || !resp.result || !resp.result[0]) {
return;
}
await removeUser(resp.result[0].userId);
}
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;
}
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-15 13:48:42 +01:00
export class PasswordUser extends User {}
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;
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;
private code: string;
constructor(props: otpUserProps) {
super({
email: props.email,
firstName: props.firstName,
lastName: props.lastName,
organization: props.organization,
password: props.password,
});
this.type = props.type;
}
async ensure(page: Page) {
await super.ensure(page);
let url = "otp_";
switch (this.type) {
case OtpType.sms:
url = url + "sms";
2024-11-15 14:27:46 +01:00
break;
2024-11-15 13:48:42 +01:00
case OtpType.email:
url = url + "email";
2024-11-15 14:27:46 +01:00
break;
2024-11-15 13:48:42 +01:00
}
2024-11-15 14:27:46 +01:00
try {
const response = await axios.post(
`${process.env.ZITADEL_API_URL}/v2/users/${this.getUserId()}/${url}`,
{},
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.ZITADEL_SERVICE_USER_TOKEN}`,
},
},
);
if (response.status >= 400 && response.status !== 409) {
const error = `HTTP Error: ${response.status} - ${response.statusText}`;
console.error(error);
throw new Error(error);
}
// TODO: get code from SMS or Email provider
this.code = "";
} catch (error) {
console.error("Error making request:", error);
throw error;
2024-11-15 13:48:42 +01:00
}
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
}
public getCode() {
return this.code;
}
}
export interface passkeyUserProps {
2024-11-15 13:48:42 +01:00
email: string;
firstName: string;
lastName: string;
organization: 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: "",
});
}
public async ensure(page: Page) {
await this.remove();
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
}
public async remove() {
await super.remove();
}
public getAuthenticatorId(): string {
return this.authenticatorId;
}
}