2023-08-30 00:30:59 +03:00
|
|
|
import { openHomePage } from '@affine-test/kit/utils/load-page';
|
|
|
|
import { waitEditorLoad } from '@affine-test/kit/utils/page-logic';
|
|
|
|
import { clickSideBarCurrentWorkspaceBanner } from '@affine-test/kit/utils/sidebar';
|
2023-08-29 13:07:05 +03:00
|
|
|
import { faker } from '@faker-js/faker';
|
|
|
|
import { hash } from '@node-rs/argon2';
|
2023-08-30 00:30:59 +03:00
|
|
|
import type { BrowserContext, Cookie, Page } from '@playwright/test';
|
|
|
|
import { z } from 'zod';
|
2023-08-29 13:07:05 +03:00
|
|
|
|
|
|
|
export async function getLoginCookie(
|
|
|
|
context: BrowserContext
|
|
|
|
): Promise<Cookie | undefined> {
|
|
|
|
return (await context.cookies()).find(
|
|
|
|
c => c.name === 'next-auth.session-token'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-30 00:30:59 +03:00
|
|
|
const cloudUserSchema = z.object({
|
|
|
|
name: z.string(),
|
|
|
|
email: z.string().email(),
|
|
|
|
password: z.string(),
|
|
|
|
});
|
|
|
|
|
|
|
|
export type CloudUser = z.infer<typeof cloudUserSchema>;
|
|
|
|
|
|
|
|
export async function createRandomUser(): Promise<CloudUser> {
|
2023-08-29 13:07:05 +03:00
|
|
|
const user = {
|
|
|
|
name: faker.internet.userName(),
|
|
|
|
email: faker.internet.email().toLowerCase(),
|
|
|
|
password: '123456',
|
2023-08-30 00:30:59 +03:00
|
|
|
} satisfies CloudUser;
|
2023-08-29 13:07:05 +03:00
|
|
|
const {
|
|
|
|
PrismaClient,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
|
|
} = require('../../../apps/server/node_modules/@prisma/client');
|
|
|
|
const client = new PrismaClient();
|
|
|
|
await client.$connect();
|
|
|
|
await client.user.create({
|
|
|
|
data: {
|
|
|
|
...user,
|
|
|
|
emailVerified: new Date(),
|
|
|
|
password: await hash(user.password),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await client.$disconnect();
|
|
|
|
|
2023-08-30 00:30:59 +03:00
|
|
|
const result = await client.user.findUnique({
|
2023-08-29 13:07:05 +03:00
|
|
|
where: {
|
|
|
|
email: user.email,
|
|
|
|
},
|
|
|
|
});
|
2023-08-30 00:30:59 +03:00
|
|
|
cloudUserSchema.parse(result);
|
|
|
|
return result;
|
2023-08-29 13:07:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteUser(email: string) {
|
|
|
|
const {
|
|
|
|
PrismaClient,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
|
|
} = require('../../../apps/server/node_modules/@prisma/client');
|
|
|
|
const client = new PrismaClient();
|
|
|
|
await client.$connect();
|
|
|
|
await client.user.delete({
|
|
|
|
where: {
|
|
|
|
email,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await client.$disconnect();
|
|
|
|
}
|
2023-08-30 00:30:59 +03:00
|
|
|
|
|
|
|
export async function loginUser(
|
|
|
|
page: Page,
|
|
|
|
user: CloudUser,
|
|
|
|
config?: {
|
|
|
|
beforeLogin?: () => Promise<void>;
|
|
|
|
afterLogin?: () => Promise<void>;
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
await openHomePage(page);
|
|
|
|
await waitEditorLoad(page);
|
|
|
|
|
|
|
|
await clickSideBarCurrentWorkspaceBanner(page);
|
|
|
|
await page.getByTestId('cloud-signin-button').click({
|
|
|
|
delay: 200,
|
|
|
|
});
|
|
|
|
await page.getByPlaceholder('Enter your email address').type(user.email, {
|
|
|
|
delay: 50,
|
|
|
|
});
|
|
|
|
await page.getByTestId('continue-login-button').click({
|
|
|
|
delay: 200,
|
|
|
|
});
|
|
|
|
await page.getByTestId('sign-in-with-password').click({
|
|
|
|
delay: 200,
|
|
|
|
});
|
|
|
|
await page.getByTestId('password-input').type('123456', {
|
|
|
|
delay: 50,
|
|
|
|
});
|
|
|
|
if (config?.beforeLogin) {
|
|
|
|
await config.beforeLogin();
|
|
|
|
}
|
|
|
|
await page.waitForTimeout(200);
|
|
|
|
await page.getByTestId('sign-in-button').click();
|
|
|
|
await page.waitForTimeout(200);
|
|
|
|
if (config?.afterLogin) {
|
|
|
|
await config.afterLogin();
|
|
|
|
}
|
|
|
|
}
|