mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-17 06:16:59 +08:00
test: cover basic collaborative (#4127)
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import { openHomePage } from '@affine-test/kit/utils/load-page';
|
||||
import { waitForEditorLoad } from '@affine-test/kit/utils/page-logic';
|
||||
import { clickSideBarCurrentWorkspaceBanner } from '@affine-test/kit/utils/sidebar';
|
||||
import {
|
||||
clickNewPageButton,
|
||||
waitForEditorLoad,
|
||||
} from '@affine-test/kit/utils/page-logic';
|
||||
import {
|
||||
clickSideBarCurrentWorkspaceBanner,
|
||||
clickSideBarSettingButton,
|
||||
} from '@affine-test/kit/utils/sidebar';
|
||||
import { faker } from '@faker-js/faker';
|
||||
import { hash } from '@node-rs/argon2';
|
||||
import type { BrowserContext, Cookie, Page } from '@playwright/test';
|
||||
@@ -15,61 +21,105 @@ export async function getLoginCookie(
|
||||
}
|
||||
|
||||
const cloudUserSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
email: z.string().email(),
|
||||
password: z.string(),
|
||||
});
|
||||
|
||||
export type CloudUser = z.infer<typeof cloudUserSchema>;
|
||||
export const runPrisma = async <T>(
|
||||
cb: (
|
||||
prisma: InstanceType<
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
||||
typeof import('../../../apps/server/node_modules/@prisma/client').PrismaClient
|
||||
>
|
||||
) => Promise<T>
|
||||
): Promise<T> => {
|
||||
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();
|
||||
try {
|
||||
return await cb(client);
|
||||
} finally {
|
||||
await client.$disconnect();
|
||||
}
|
||||
};
|
||||
|
||||
export async function createRandomUser(): Promise<CloudUser> {
|
||||
export async function addUserToWorkspace(
|
||||
workspaceId: string,
|
||||
userId: string,
|
||||
permission: number
|
||||
) {
|
||||
await runPrisma(async client => {
|
||||
const workspace = await client.workspace.findUnique({
|
||||
where: {
|
||||
id: workspaceId,
|
||||
},
|
||||
});
|
||||
if (workspace == null) {
|
||||
throw new Error(`workspace ${workspaceId} not found`);
|
||||
}
|
||||
await client.userWorkspacePermission.create({
|
||||
data: {
|
||||
workspaceId: workspace.id,
|
||||
subPageId: null,
|
||||
userId,
|
||||
accepted: true,
|
||||
type: permission,
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function createRandomUser(): Promise<{
|
||||
name: string;
|
||||
email: string;
|
||||
password: string;
|
||||
id: string;
|
||||
}> {
|
||||
const user = {
|
||||
name: faker.internet.userName(),
|
||||
email: faker.internet.email().toLowerCase(),
|
||||
password: '123456',
|
||||
} satisfies CloudUser;
|
||||
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();
|
||||
};
|
||||
const result = await runPrisma(async client => {
|
||||
await client.user.create({
|
||||
data: {
|
||||
...user,
|
||||
emailVerified: new Date(),
|
||||
password: await hash(user.password),
|
||||
},
|
||||
});
|
||||
|
||||
const result = await client.user.findUnique({
|
||||
where: {
|
||||
email: user.email,
|
||||
},
|
||||
return await client.user.findUnique({
|
||||
where: {
|
||||
email: user.email,
|
||||
},
|
||||
});
|
||||
});
|
||||
cloudUserSchema.parse(result);
|
||||
return result;
|
||||
return {
|
||||
...result,
|
||||
password: user.password,
|
||||
} as any;
|
||||
}
|
||||
|
||||
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 runPrisma(async client => {
|
||||
await client.user.delete({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
});
|
||||
});
|
||||
await client.$disconnect();
|
||||
}
|
||||
|
||||
export async function loginUser(
|
||||
page: Page,
|
||||
user: CloudUser,
|
||||
userEmail: string,
|
||||
config?: {
|
||||
beforeLogin?: () => Promise<void>;
|
||||
afterLogin?: () => Promise<void>;
|
||||
@@ -82,7 +132,7 @@ export async function loginUser(
|
||||
await page.getByTestId('cloud-signin-button').click({
|
||||
delay: 200,
|
||||
});
|
||||
await page.getByPlaceholder('Enter your email address').type(user.email, {
|
||||
await page.getByPlaceholder('Enter your email address').type(userEmail, {
|
||||
delay: 50,
|
||||
});
|
||||
await page.getByTestId('continue-login-button').click({
|
||||
@@ -104,3 +154,14 @@ export async function loginUser(
|
||||
await config.afterLogin();
|
||||
}
|
||||
}
|
||||
|
||||
export async function enableCloudWorkspace(page: Page) {
|
||||
await clickSideBarSettingButton(page);
|
||||
await page.getByTestId('current-workspace-label').click();
|
||||
await page.getByTestId('publish-enable-affine-cloud-button').click();
|
||||
await page.getByTestId('confirm-enable-affine-cloud-button').click();
|
||||
// wait for upload and delete local workspace
|
||||
await page.waitForTimeout(2000);
|
||||
await waitForEditorLoad(page);
|
||||
await clickNewPageButton(page);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user