server/tests/utils.ts

121 lines
3.3 KiB
TypeScript
Raw Normal View History

import { randomBytes } from "node:crypto";
import { inArray, like } from "drizzle-orm";
2024-04-14 02:46:33 +02:00
import { type Status, findManyStatuses } from "~database/entities/Status";
2024-04-11 15:52:44 +02:00
import {
type User,
type UserWithRelations,
createNewLocalUser,
2024-04-11 15:52:44 +02:00
} from "~database/entities/User";
import { db } from "~drizzle/db";
import { status, token, user } from "~drizzle/schema";
import { server } from "~index";
/**
* This allows us to send a test request to the server even when it isnt running
* CURRENTLY NOT WORKING, NEEDS TO BE FIXED
* @param req Request to send
* @returns Response from the server
*/
export async function sendTestRequest(req: Request) {
2024-04-11 14:12:16 +02:00
return server.fetch(req);
}
export function wrapRelativeUrl(url: string, base_url: string) {
2024-04-07 07:30:49 +02:00
return new URL(url, base_url);
}
2024-04-11 15:52:44 +02:00
export const deleteOldTestUsers = async () => {
// Deletes all users that match the test username (test-<32 random characters>)
await db.delete(user).where(like(user.username, "test-%"));
};
export const getTestUsers = async (count: number) => {
const users: UserWithRelations[] = [];
2024-04-14 02:46:33 +02:00
const passwords: string[] = [];
2024-04-11 15:52:44 +02:00
for (let i = 0; i < count; i++) {
2024-04-14 02:46:33 +02:00
const password = randomBytes(32).toString("hex");
2024-04-11 15:52:44 +02:00
const user = await createNewLocalUser({
username: `test-${randomBytes(32).toString("hex")}`,
email: `${randomBytes(32).toString("hex")}@test.com`,
2024-04-14 02:46:33 +02:00
password,
2024-04-11 15:52:44 +02:00
});
if (!user) {
throw new Error("Failed to create test user");
}
2024-04-14 02:46:33 +02:00
passwords.push(password);
2024-04-11 15:52:44 +02:00
users.push(user);
}
const tokens = await db
.insert(token)
.values(
users.map((u) => ({
accessToken: randomBytes(32).toString("hex"),
tokenType: "bearer",
userId: u.id,
applicationId: null,
code: randomBytes(32).toString("hex"),
scope: "read write follow push",
})),
)
.returning();
return {
users,
tokens,
2024-04-14 02:46:33 +02:00
passwords,
2024-04-11 15:52:44 +02:00
deleteUsers: async () => {
await db.delete(user).where(
inArray(
user.id,
users.map((u) => u.id),
),
);
},
};
};
export const getTestStatuses = async (
count: number,
user: User,
partial?: Partial<Status>,
) => {
const statuses: Status[] = [];
for (let i = 0; i < count; i++) {
const newStatus = (
await db
.insert(status)
.values({
content: `${i} ${randomBytes(32).toString("hex")}`,
authorId: user.id,
sensitive: false,
updatedAt: new Date().toISOString(),
visibility: "public",
...partial,
})
.returning()
)[0];
if (!newStatus) {
throw new Error("Failed to create test status");
}
statuses.push(newStatus);
}
2024-04-14 02:46:33 +02:00
const statusesWithRelations = await findManyStatuses({
where: (status, { inArray }) =>
inArray(
status.id,
statuses.map((s) => s.id),
),
orderBy: (status, { asc }) => asc(status.id),
});
return statusesWithRelations;
2024-04-11 15:52:44 +02:00
};