103 lines
3.4 KiB
TypeScript
103 lines
3.4 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { DatabaseSessionStore } from "../modules/database/src/sessions.ts";
|
|
import { createSqliteDriver } from "../modules/database/src/adapters/sqlite.ts";
|
|
import { App } from "../core/app.ts";
|
|
import { authIntegration } from "../integrations/auth.ts";
|
|
import { Controller } from "../decorators/Controller.ts";
|
|
import { Route } from "../decorators/Route.ts";
|
|
import { Auth } from "../decorators/Auth.ts";
|
|
import { Module } from "../decorators/Module.ts";
|
|
import { resolveConfig } from "../core/config.ts";
|
|
import { formatSource } from "../core/format-source.ts";
|
|
import plugin from "../modules/prettier-plugin/index.ts";
|
|
import prettier from "prettier";
|
|
import { compileCss } from "../views/css/index.ts";
|
|
|
|
test("database sessions authenticate across app instances, revoke and consume atomically", async () => {
|
|
const driver = createSqliteDriver({ path: ":memory:" });
|
|
const store = await DatabaseSessionStore.create(driver);
|
|
@Controller("/")
|
|
class PrivateController {
|
|
@Route("/private") @Auth() private() {
|
|
return "protected";
|
|
}
|
|
}
|
|
@Module({ controllers: [PrivateController] })
|
|
class Root {}
|
|
const create = () =>
|
|
App.create({
|
|
config: resolveConfig(),
|
|
modules: [Root],
|
|
integrations: [authIntegration({ store, login: () => ({ id: "ada" }) })],
|
|
});
|
|
try {
|
|
const first = await create();
|
|
const second = await create();
|
|
const login = await first.fetch(
|
|
new Request("https://app.test/auth/login", {
|
|
method: "POST",
|
|
headers: {
|
|
origin: "https://app.test",
|
|
"content-type": "application/json",
|
|
},
|
|
body: JSON.stringify({ email: "ada", password: "secret" }),
|
|
}),
|
|
);
|
|
const cookie = login.headers.get("set-cookie")!;
|
|
expect(
|
|
(
|
|
await second.fetch(
|
|
new Request("https://app.test/private", { headers: { cookie } }),
|
|
)
|
|
).status,
|
|
).toBe(200);
|
|
await first.fetch(
|
|
new Request("https://app.test/auth/logout", {
|
|
method: "POST",
|
|
headers: { origin: "https://app.test", cookie },
|
|
}),
|
|
);
|
|
expect(
|
|
(
|
|
await second.fetch(
|
|
new Request("https://app.test/private", { headers: { cookie } }),
|
|
)
|
|
).status,
|
|
).toBe(401);
|
|
await store.set("transaction", {
|
|
value: { verifier: "once" },
|
|
expires: Date.now() + 10000,
|
|
});
|
|
expect(
|
|
(
|
|
await Promise.all([
|
|
store.consume("transaction"),
|
|
store.consume("transaction"),
|
|
])
|
|
).filter(Boolean),
|
|
).toHaveLength(1);
|
|
await store.set("expired", { value: {}, expires: 1 });
|
|
expect(await store.get("expired")).toBeUndefined();
|
|
} finally {
|
|
await driver.close();
|
|
}
|
|
});
|
|
|
|
test("Prettier delegates to muddle formatting and preserves compiled STCSS", async () => {
|
|
const source =
|
|
".card{padding:1rem;@predict mobile max-height; &:hover{opacity:0.8;}}";
|
|
const formatted = await formatSource(source, "stcss");
|
|
expect(
|
|
await prettier.format(source, { parser: "stcss", plugins: [plugin] }),
|
|
).toBe(formatted);
|
|
expect(await formatSource(formatted, "stcss")).toBe(formatted);
|
|
expect(compileCss(formatted)).toBe(compileCss(source));
|
|
const spug =
|
|
'javascript!\nconst title={text:"Hello"}\nend\ndiv\n p= title.text\n';
|
|
const next = await formatSource(spug, "spug");
|
|
expect(next).toContain('const title = { text: "Hello" };');
|
|
expect(await formatSource(next, "spug")).toBe(next);
|
|
const raw =
|
|
'script.\n const text = "a b";\n\n console.log(text);\np End\n';
|
|
expect(await formatSource(raw, "spug")).toBe(raw);
|
|
});
|