mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-12 01:08:29 +03:00
experimental: misc ergonomic checking
This commit is contained in:
@@ -9,43 +9,48 @@ export function RequireAuthPerms(
|
||||
perms: APIPermissions | Array<APIPermissions>,
|
||||
method: "DELETE" | "GET" | "PATCH" | "POST" | "PUT" = "GET"
|
||||
) {
|
||||
t.test(`Testing permissions for ${method} ${url} [${perms}]`, async (t) => {
|
||||
const m = method.toLowerCase() as Lowercase<typeof method>;
|
||||
return t.test(
|
||||
`Testing permissions for ${method} ${url} [${
|
||||
Array.isArray(perms) ? perms.join(", ") : perms
|
||||
}]`,
|
||||
async (t) => {
|
||||
const m = method.toLowerCase() as Lowercase<typeof method>;
|
||||
|
||||
const res = await mockApi[m](url);
|
||||
const res = await mockApi[m](url);
|
||||
|
||||
// 401 if no auth given
|
||||
t.equal(res.statusCode, 401);
|
||||
// 401 if no auth given
|
||||
t.equal(res.statusCode, 401);
|
||||
|
||||
await db["api-tokens"].insert({
|
||||
identifier: "temp_auth_perms",
|
||||
permissions: {},
|
||||
token: "temp_auth",
|
||||
userID: 1,
|
||||
fromAPIClient: null,
|
||||
});
|
||||
await db["api-tokens"].insert({
|
||||
identifier: "temp_auth_perms",
|
||||
permissions: {},
|
||||
token: "temp_auth",
|
||||
userID: 1,
|
||||
fromAPIClient: null,
|
||||
});
|
||||
|
||||
const resAuth = await mockApi[m](url).set("Authorization", "Bearer temp_auth");
|
||||
const resAuth = await mockApi[m](url).set("Authorization", "Bearer temp_auth");
|
||||
|
||||
t.equal(resAuth.statusCode, 403);
|
||||
t.equal(resAuth.statusCode, 403);
|
||||
|
||||
const prm = Array.isArray(perms) ? perms : [perms];
|
||||
const prm = Array.isArray(perms) ? perms : [perms];
|
||||
|
||||
await db["api-tokens"].insert({
|
||||
identifier: "temp_auth_perms2",
|
||||
permissions: Object.fromEntries(prm.map((e) => [e, true])),
|
||||
token: "temp_auth2",
|
||||
userID: 1,
|
||||
fromAPIClient: null,
|
||||
});
|
||||
await db["api-tokens"].insert({
|
||||
identifier: "temp_auth_perms2",
|
||||
permissions: Object.fromEntries(prm.map((e) => [e, true])),
|
||||
token: "temp_auth2",
|
||||
userID: 1,
|
||||
fromAPIClient: null,
|
||||
});
|
||||
|
||||
const resAuthed = await mockApi[m](url).set("Authorization", "Bearer temp_auth2");
|
||||
const resAuthed = await mockApi[m](url).set("Authorization", "Bearer temp_auth2");
|
||||
|
||||
t.not(resAuthed.statusCode, 401);
|
||||
t.not(resAuthed.statusCode, 403);
|
||||
t.not(resAuthed.statusCode, 401);
|
||||
t.not(resAuthed.statusCode, 403);
|
||||
|
||||
await ResetDBState();
|
||||
await ResetDBState();
|
||||
|
||||
t.end();
|
||||
});
|
||||
t.end();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ export function isApproximately(
|
||||
|
||||
export function prAssert(
|
||||
t: Tap.Test,
|
||||
obj: Record<string, unknown> | unknown,
|
||||
obj: unknown,
|
||||
schema: PrudenceSchema,
|
||||
message = "Unnamed Prudence Assertion"
|
||||
) {
|
||||
|
||||
@@ -6,7 +6,7 @@ import { CloseRedisPubSub } from "external/redis/redis-IPC";
|
||||
import { CloseScoreImportQueue } from "lib/score-import/worker/queue";
|
||||
|
||||
export async function CleanUpAfterTests() {
|
||||
if (process.env.TAP_SNAPSHOT) {
|
||||
if (process.env.TAP_SNAPSHOT !== "" && process.env.TAP_SNAPSHOT !== undefined) {
|
||||
WriteSnapshotData();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,16 @@ logger.verbose("Connecting to Supertest...");
|
||||
const mockApi = supertest(connection);
|
||||
|
||||
export function CloseServerConnection() {
|
||||
return connection.close();
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
connection.close((err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default mockApi;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
export function MockMulterFile(buffer: Buffer, originalname: string) {
|
||||
return {
|
||||
const mockFile: Express.Multer.File = {
|
||||
originalname,
|
||||
buffer,
|
||||
} as Express.Multer.File;
|
||||
};
|
||||
|
||||
return mockFile;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user