server/tests/utils.ts

160 lines
4.2 KiB
TypeScript
Raw Permalink Normal View History

import { generateChallenge } from "@/challenges";
import { randomString } from "@/math";
import { solveChallenge } from "altcha-lib";
import { asc, inArray, like } from "drizzle-orm";
import { appFactory } from "~/app";
import type { Status } from "~/classes/functions/status";
import { searchManager } from "~/classes/search/search-manager";
import { db } from "~/drizzle/db";
import { setupDatabase } from "~/drizzle/db";
import { Notes, Tokens, Users } from "~/drizzle/schema";
import { config } from "~/packages/config-manager";
import { Note } from "~/packages/database-interface/note";
import { User } from "~/packages/database-interface/user";
await setupDatabase();
if (config.sonic.enabled) {
await searchManager.connect();
}
const app = await appFactory();
/**
* This allows us to send a test request to the server even when it isnt running
* @param req Request to send
* @returns Response from the server
*/
export function sendTestRequest(req: Request): Promise<Response> {
// return fetch(req);
return Promise.resolve(app.fetch(req));
}
export function wrapRelativeUrl(url: string, baseUrl: string) {
return new URL(url, baseUrl);
}
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(Users).where(like(Users.username, "test-%"));
2024-04-11 15:52:44 +02:00
};
export const getTestUsers = async (count: number) => {
const users: User[] = [];
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++) {
const password = randomString(32, "hex");
2024-04-14 02:46:33 +02:00
const user = await User.fromDataLocal({
username: `test-${randomString(32, "hex")}`,
email: `${randomString(32, "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(Tokens)
2024-04-11 15:52:44 +02:00
.values(
users.map((u) => ({
accessToken: randomString(32, "hex"),
2024-04-11 15:52:44 +02:00
tokenType: "bearer",
userId: u.id,
applicationId: null,
code: randomString(32, "hex"),
2024-04-11 15:52:44 +02:00
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(Users).where(
2024-04-11 15:52:44 +02:00
inArray(
Users.id,
2024-04-11 15:52:44 +02:00
users.map((u) => u.id),
),
);
},
};
};
export const getTestStatuses = async (
count: number,
user: User,
partial?: Partial<Status>,
) => {
const statuses: Note[] = [];
2024-04-11 15:52:44 +02:00
for (let i = 0; i < count; i++) {
const newStatus = await Note.insert({
content: `${i} ${randomString(32, "hex")}`,
authorId: user.id,
sensitive: false,
updatedAt: new Date().toISOString(),
visibility: "public",
applicationId: null,
...partial,
});
2024-04-11 15:52:44 +02:00
if (!newStatus) {
throw new Error("Failed to create test status");
}
statuses.push(newStatus);
}
return (
await Note.manyFromSql(
2024-04-14 02:46:33 +02:00
inArray(
Notes.id,
2024-04-14 02:46:33 +02:00
statuses.map((s) => s.id),
),
asc(Notes.id),
undefined,
undefined,
user.id,
)
).map((n) => n.data);
2024-04-11 15:52:44 +02:00
};
/**
* Generates a solved challenge (with tiny difficulty)
*
* Only to be used in tests
* @returns Base64 encoded payload
*/
export const getSolvedChallenge = async () => {
const { challenge } = await generateChallenge(100);
const solution = await solveChallenge(
challenge.challenge,
challenge.salt,
challenge.algorithm,
challenge.maxnumber,
).promise;
if (!solution) {
throw new Error("Failed to solve challenge");
}
return Buffer.from(
JSON.stringify({
number: solution.number,
algorithm: challenge.algorithm,
challenge: challenge.challenge,
salt: challenge.salt,
signature: challenge.signature,
}),
).toString("base64");
};