-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/auth package #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e2a0a95
fix(core): escape markdown source before rendering to block XSS
abdelkabirouadoukou 53e0365
Merge branch 'fix/markdown-xss-escaping'
abdelkabirouadoukou 776414b
fix(core): parameterize migration bookkeeping insert and run migratio…
abdelkabirouadoukou 97f33c0
Merge branch 'fix/postgres-migration-injection'
abdelkabirouadoukou 990a083
feat(auth): add @thexjs/auth package with defineAuth
abdelkabirouadoukou 2db2b57
fix(core): reject javascript:/data: links in renderMarkdown
abdelkabirouadoukou 4cf1ed7
fix(core): shield fenced code blocks from earlier markdown passes
abdelkabirouadoukou db45c58
test(core): cover runPostgresMigrations() rollback against real Postgres
abdelkabirouadoukou aebdcd7
Merge pull request #25 from abdelkabirouadoukou/fix/coderabbit-review…
abdelkabirouadoukou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,129 @@ | ||
| import { Database } from "bun:sqlite"; | ||
| import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | ||
| import { mkdirSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { runSQLiteMigrations } from "./migrate"; | ||
|
|
||
| const FIXTURE_DIR = join(import.meta.dir, "__fixtures__/migrations"); | ||
|
|
||
| function resetFixtures() { | ||
| rmSync(FIXTURE_DIR, { recursive: true, force: true }); | ||
| mkdirSync(FIXTURE_DIR, { recursive: true }); | ||
| } | ||
|
|
||
| function writeMigration(name: string, sql: string) { | ||
| writeFileSync(join(FIXTURE_DIR, name), sql); | ||
| } | ||
|
|
||
| function tableExists(db: Database, name: string): boolean { | ||
| const row = db | ||
| .query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?1") | ||
| .get(name) as { name: string } | undefined; | ||
| return row !== undefined; | ||
| } | ||
|
|
||
| function appliedNames(db: Database): string[] { | ||
| const rows = db.query("SELECT name FROM _x_migrations ORDER BY name").all() as { | ||
| name: string; | ||
| }[]; | ||
| return rows.map((r) => r.name); | ||
| } | ||
|
|
||
| beforeAll(() => { | ||
| resetFixtures(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| rmSync(FIXTURE_DIR, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe("runSQLiteMigrations", () => { | ||
| test("applies migrations in filename order", () => { | ||
| resetFixtures(); | ||
| writeMigration( | ||
| "001_create_users.sql", | ||
| "CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT NOT NULL);", | ||
| ); | ||
| writeMigration( | ||
| "002_add_profiles.sql", | ||
| "CREATE TABLE profiles (id INTEGER PRIMARY KEY, user_id INTEGER REFERENCES users(id));", | ||
| ); | ||
|
|
||
| const db = new Database(":memory:"); | ||
| const result = runSQLiteMigrations(db, FIXTURE_DIR); | ||
|
|
||
| expect(result.applied).toEqual(["001_create_users.sql", "002_add_profiles.sql"]); | ||
| expect(result.skipped).toEqual([]); | ||
| expect(tableExists(db, "users")).toBe(true); | ||
| expect(tableExists(db, "profiles")).toBe(true); | ||
| expect(appliedNames(db)).toEqual(["001_create_users.sql", "002_add_profiles.sql"]); | ||
| db.close(); | ||
| }); | ||
|
|
||
| test("skips migrations that were already applied", () => { | ||
| resetFixtures(); | ||
| writeMigration("001_create_users.sql", "CREATE TABLE users (id INTEGER PRIMARY KEY);"); | ||
|
|
||
| const db = new Database(":memory:"); | ||
| const first = runSQLiteMigrations(db, FIXTURE_DIR); | ||
| expect(first.applied).toEqual(["001_create_users.sql"]); | ||
|
|
||
| const second = runSQLiteMigrations(db, FIXTURE_DIR); | ||
| expect(second.applied).toEqual([]); | ||
| expect(second.skipped).toEqual(["001_create_users.sql"]); | ||
| db.close(); | ||
| }); | ||
|
|
||
| test("rolls back a migration that fails partway", () => { | ||
| resetFixtures(); | ||
| writeMigration( | ||
| "001_create_items.sql", | ||
| "CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT NOT NULL);", | ||
| ); | ||
| // The second statement violates the primary key — the whole file must | ||
| // fail as one unit inside its transaction: no partial rows and no | ||
| // bookkeeping record, so a retry replays it against a clean schema. | ||
| writeMigration( | ||
| "002_broken.sql", | ||
| "INSERT INTO items (id, name) VALUES (1, 'first');\nINSERT INTO items (id, name) VALUES (1, 'duplicate');", | ||
| ); | ||
|
|
||
| const db = new Database(":memory:"); | ||
| expect(() => runSQLiteMigrations(db, FIXTURE_DIR)).toThrow(); | ||
|
|
||
| // The earlier migration is still applied and its data is intact... | ||
| expect(appliedNames(db)).toEqual(["001_create_items.sql"]); | ||
| // ...but the failed migration left no trace: rows it inserted were rolled | ||
| // back and it was never recorded as applied. | ||
| const rows = db.query("SELECT COUNT(*) AS c FROM items").get() as { c: number }; | ||
| expect(rows.c).toBe(0); | ||
| db.close(); | ||
| }); | ||
|
|
||
| test("retries a previously failed migration after a fix", () => { | ||
| resetFixtures(); | ||
| writeMigration( | ||
| "001_create_items.sql", | ||
| "CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT NOT NULL);", | ||
| ); | ||
| writeMigration( | ||
| "002_broken.sql", | ||
| "INSERT INTO items (id, name) VALUES (1, 'first');\nINSERT INTO items (id, name) VALUES (1, 'duplicate');", | ||
| ); | ||
|
|
||
| const db = new Database(":memory:"); | ||
| expect(() => runSQLiteMigrations(db, FIXTURE_DIR)).toThrow(); | ||
|
|
||
| writeMigration( | ||
| "002_broken.sql", | ||
| "INSERT INTO items (id, name) VALUES (1, 'first');\nINSERT INTO items (id, name) VALUES (2, 'second');", | ||
| ); | ||
|
|
||
| const retry = runSQLiteMigrations(db, FIXTURE_DIR); | ||
| expect(retry.applied).toEqual(["002_broken.sql"]); | ||
| expect(appliedNames(db)).toEqual(["001_create_items.sql", "002_broken.sql"]); | ||
| const rows = db.query("SELECT name FROM items ORDER BY id").all() as { name: string }[]; | ||
| expect(rows.map((r) => r.name)).toEqual(["first", "second"]); | ||
| db.close(); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.