-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial test coverage for saving a new custom emoji
This is mostly to show the fake storage in action, as well as setup the helpers required for writing these kinds of tests
- Loading branch information
1 parent
d5bfb8b
commit 4916908
Showing
5 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ assets/ | |
fedify-hollo-*.tgz | ||
*.jsonl | ||
node_modules/ | ||
tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { afterEach, beforeEach, describe, it } from "node:test"; | ||
import type { TestContext } from "node:test"; | ||
|
||
import { getFixtureFile, getLoginCookie } from "../../tests/helpers"; | ||
|
||
import db from "../db"; | ||
import { customEmojis } from "../schema"; | ||
|
||
import { drive } from "../storage"; | ||
import app from "./index"; | ||
|
||
const emojiFile = await getFixtureFile("emoji.png", "image/png"); | ||
|
||
describe("emojis", () => { | ||
beforeEach(async () => { | ||
await db.delete(customEmojis); | ||
}); | ||
|
||
afterEach(() => { | ||
drive.restore(); | ||
}); | ||
|
||
it("Successfully saves a new emoji", async (t: TestContext) => { | ||
const disk = drive.fake(); | ||
const testShortCode = ":test-emoji:"; | ||
|
||
const formData = new FormData(); | ||
formData.append("shortcode", testShortCode); | ||
formData.append("image", emojiFile); | ||
|
||
const cookie = await getLoginCookie(); | ||
|
||
const response = await app.request("/emojis", { | ||
method: "POST", | ||
body: formData, | ||
headers: { | ||
Cookie: cookie, | ||
}, | ||
}); | ||
|
||
t.assert.equal(response.status, 302); | ||
t.assert.equal(response.headers.get("Location"), "/emojis"); | ||
|
||
// Assert we uploaded the file: | ||
t.assert.doesNotThrow(() => disk.assertExists("emojis/test-emoji.png")); | ||
|
||
const emoji = await db.query.customEmojis.findFirst(); | ||
|
||
t.assert.ok(emoji, "Successfully saves the emoji"); | ||
t.assert.equal(emoji.category, null, "Defaults category to null"); | ||
t.assert.equal( | ||
emoji.url, | ||
"http://hollo.test/assets/emojis/test-emoji.png", | ||
"Sets the file URL correctly", | ||
); | ||
t.assert.equal(emoji.shortcode, "test-emoji"); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { readFile } from "node:fs/promises"; | ||
import { join } from "node:path"; | ||
import { after } from "node:test"; | ||
import { serializeSigned } from "hono/utils/cookie"; | ||
import db from "../src/db"; | ||
import { drive } from "../src/storage"; | ||
|
||
const fixtureFiles = join(import.meta.dirname, "fixtures", "files"); | ||
|
||
export async function getFixtureFile( | ||
name: string, | ||
type: string, | ||
): Promise<File> { | ||
const filePath = join(fixtureFiles, name); | ||
const data = await readFile(filePath); | ||
|
||
return new File([data], name, { | ||
type, | ||
}); | ||
} | ||
|
||
// biome-ignore lint/complexity/useLiteralKeys: tsc complains about this (TS4111) | ||
const SECRET_KEY = process.env["SECRET_KEY"]; | ||
|
||
export async function getLoginCookie() { | ||
// Same logic as in src/pages/login.tsx | ||
return serializeSigned("login", new Date().toISOString(), SECRET_KEY!, { | ||
path: "/", | ||
}); | ||
} | ||
|
||
// Automatically close the database and remove test file uploads | ||
// Without this the tests hang due to the database | ||
after(async () => { | ||
await db.$client.end({ timeout: 5 }); | ||
|
||
const disk = drive.fake(); | ||
await disk.deleteAll(); | ||
}); |