Files
documenso/packages/prisma/seed/users.ts
David Nguyen 56c550c9d2 fix: refactor tests (#1066)
## Changes Made

- Refactor/optimise tests
- Reduce flakiness
- Add parallel tests (if there's enough CPU capacity)
- Removed explicit worker count when running parallel tests. Defaults to
50% of CPU capacity.

Might want to consider sharding the test across runners in the future as
our tests grows.
2024-04-03 16:13:35 +07:00

69 lines
1.3 KiB
TypeScript

import { customAlphabet } from 'nanoid';
import { hashSync } from '@documenso/lib/server-only/auth/hash';
import { prisma } from '..';
export const seedTestEmail = () => `user-${Date.now()}@test.documenso.com`;
type SeedUserOptions = {
name?: string;
email?: string;
password?: string;
verified?: boolean;
};
const nanoid = customAlphabet('1234567890abcdef', 10);
export const seedUser = async ({
name,
email,
password = 'password',
verified = true,
}: SeedUserOptions = {}) => {
if (!name) {
name = nanoid();
}
if (!email) {
email = `${nanoid()}@test.documenso.com`;
}
return await prisma.user.create({
data: {
name,
email,
password: hashSync(password),
emailVerified: verified ? new Date() : undefined,
url: name,
},
});
};
export const unseedUser = async (userId: number) => {
await prisma.user.delete({
where: {
id: userId,
},
});
};
export const unseedUserByEmail = async (email: string) => {
await prisma.user.delete({
where: {
email,
},
});
};
export const extractUserVerificationToken = async (email: string) => {
return await prisma.verificationToken.findFirstOrThrow({
where: {
identifier: 'confirmation-email',
user: {
email,
},
},
});
};