상위 디렉터리 및 카테고리의 소유권을 검증하지 않던 문제 등 버그 수정 및 Vitest 기반 테스트 추가

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
staticandClaude Opus 5.5 committed 2026-10-11 23:45:56 +09:00
1 parent 67989f4741
commit ed732be999
31 files changed
+2057 -42

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
import { sql } from "kysely";
import db from "#lib/server/db/kysely.js";
export const resetDatabase = async () => {
const { rows } = await sql<{ tablename: string }>`
SELECT tablename FROM pg_tables
WHERE schemaname = 'public' AND tablename NOT LIKE 'kysely_%'
`.execute(db);
await sql`TRUNCATE ${sql.join(rows.map(({ tablename }) => sql.table(tablename)))} RESTART IDENTITY CASCADE`.execute(
db,
);
};
let sequence = 0;
const nextSequence = () => ++sequence;
export const createUser = async () => {
const n = nextSequence();
const { id: userId } = await db
.insertInto("user")
.values({ email: `user${n}@example.com`, nickname: `user${n}`, password: "password" })
.returning("id")
.executeTakeFirstOrThrow();
const { id: clientId } = await db
.insertInto("client")
.values({ encryption_public_key: `enc${n}`, signature_public_key: `sig${n}` })
.returning("id")
.executeTakeFirstOrThrow();
await db
.insertInto("user_client")
.values({ user_id: userId, client_id: clientId, state: "active" })
.execute();
await db
.insertInto("master_encryption_key")
.values({ user_id: userId, version: 1, state: "active" })
.execute();
return { userId, clientId };
};
export const ciphertext = (value = "ciphertext") => ({ ciphertext: value, iv: "iv" });
export const createDirectory = async (userId: number, parentId: DirectoryId = "root") => {
const n = nextSequence();
const { id } = await db
.insertInto("directory")
.values({
parent_id: parentId === "root" ? null : parentId,
user_id: userId,
master_encryption_key_version: 1,
encrypted_data_encryption_key: `dek${n}`,
data_encryption_key_version: new Date(),
encrypted_name: ciphertext(),
})
.returning("id")
.executeTakeFirstOrThrow();
return id;
};
export const createCategory = async (userId: number, parentId: CategoryId = "root") => {
const n = nextSequence();
const { id } = await db
.insertInto("category")
.values({
parent_id: parentId === "root" ? null : parentId,
user_id: userId,
master_encryption_key_version: 1,
encrypted_data_encryption_key: `dek${n}`,
data_encryption_key_version: new Date(),
encrypted_name: ciphertext(),
})
.returning("id")
.executeTakeFirstOrThrow();
return id;
};
export const newEntryParams = (userId: number, parentId: number | "root") => ({
userId,
parentId,
mekVersion: 1,
encDek: `dek${nextSequence()}`,
dekVersion: new Date(),
encName: ciphertext(),
});