-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
380 additions
and
11 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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: "3" | ||
services: | ||
db: | ||
image: mysql:8.0 | ||
command: mysqld --default-authentication-plugin=mysql_native_password | ||
ports: | ||
- "${MYSQL_PORT:-3306}:3306" | ||
environment: | ||
- MYSQL_DATABASE=test | ||
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-password_root} |
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,20 @@ | ||
//import { WithCsrf } from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { LogoutForm, PageProps, WithCsrf } from "plantation/mod.ts"; | ||
//import { PageProps } from "../../types.ts"; | ||
|
||
export default function Home(props: PageProps<unknown, WithCsrf>) { | ||
return ( | ||
<div class="px-4 py-8 mx-auto bg-[#86efac]"> | ||
<div class="max-w-screen-md mx-auto flex flex-col items-center justify-center"> | ||
MUST LOGIN | ||
<LogoutForm | ||
actionPath="/user/logout" | ||
csrfToken={props.state.csrf.getTokenStr()} | ||
class="px-2 py-1 border-gray-500 border-2 rounded bg-white hover:bg-gray-200 transition-colors" | ||
> | ||
<p>Logout</p> | ||
</LogoutForm> | ||
</div> | ||
</div> | ||
); | ||
} |
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,20 @@ | ||
//import { WithCsrf } from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { LogoutForm, PageProps, WithCsrf } from "plantation/mod.ts"; | ||
//import { PageProps } from "../../types.ts"; | ||
|
||
export default function Home(props: PageProps<unknown, WithCsrf>) { | ||
return ( | ||
<div class="px-4 py-8 mx-auto bg-[#86efac]"> | ||
<div class="max-w-screen-md mx-auto flex flex-col items-center justify-center"> | ||
MUST LOGIN | ||
<LogoutForm | ||
actionPath="/user/logout" | ||
csrfToken={props.state.csrf.getTokenStr()} | ||
class="px-2 py-1 border-gray-500 border-2 rounded bg-white hover:bg-gray-200 transition-colors" | ||
> | ||
<p>Logout</p> | ||
</LogoutForm> | ||
</div> | ||
</div> | ||
); | ||
} |
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
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,35 @@ | ||
/// <reference lib="deno.unstable" /> | ||
import { defineConfig } from "$fresh/server.ts"; | ||
import { testPlugin } from "../plugins/test_plugin.ts"; | ||
|
||
import { getPlantationWithCsrfPlugins } from "../../mod.ts"; | ||
import { auth, connectionPool } from "../utils/auth.ts"; | ||
export { connectionPool }; | ||
import { z } from "../../deps.ts"; | ||
|
||
const testEmailSchema = z.coerce.string().email(); | ||
const testPasswordSchema = z.coerce.string().trim().min(8); | ||
|
||
export default defineConfig({ | ||
plugins: [ | ||
...(await getPlantationWithCsrfPlugins( | ||
{ | ||
csrf: { | ||
kv: await Deno.openKv(":memory:"), | ||
}, | ||
plantationParams: { | ||
setupRootPath: "/", | ||
auth: auth, | ||
allowNoSessionPaths: [], | ||
resourceName: "user", | ||
resourceIdentifierName: "email", | ||
loginAfterPath: "/must_login", | ||
logoutAfterPath: "/", | ||
identifierSchema: testEmailSchema, | ||
passwordSchema: testPasswordSchema, | ||
}, | ||
}, | ||
)), | ||
testPlugin, | ||
], | ||
}); |
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,13 @@ | ||
import { PageProps, Plugin } from "$fresh/server.ts"; | ||
import TestComponent from "../routes/test_route.tsx"; | ||
import { ComponentType } from "preact"; | ||
|
||
export const testPlugin: Plugin = { | ||
name: "TestPlugin", | ||
routes: [ | ||
{ | ||
component: TestComponent as ComponentType<PageProps>, | ||
path: "/must_login", | ||
}, | ||
], | ||
}; |
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,18 @@ | ||
import { LogoutForm, PageProps, WithCsrf } from "../../mod.ts"; | ||
|
||
export default function MustLogin(props: PageProps<unknown, WithCsrf>) { | ||
return ( | ||
<div class="px-4 py-8 mx-auto bg-[#86efac]"> | ||
<div class="max-w-screen-md mx-auto flex flex-col items-center justify-center"> | ||
MUST LOGIN | ||
<LogoutForm | ||
actionPath="/user/logout" | ||
csrfToken={props.state.csrf.getTokenStr()} | ||
class="px-2 py-1 border-gray-500 border-2 rounded bg-white hover:bg-gray-200 transition-colors" | ||
> | ||
<p>Logout</p> | ||
</LogoutForm> | ||
</div> | ||
</div> | ||
); | ||
} |
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,182 @@ | ||
import { createHandler, ServeHandlerInfo } from "$fresh/server.ts"; | ||
import manifest from "./work/fresh.gen.ts"; | ||
import config, { connectionPool } from "./config/test_fresh.config.ts"; | ||
import { expect } from "./test_deps.ts"; | ||
|
||
const CONN_INFO: ServeHandlerInfo = { | ||
remoteAddr: { hostname: "127.0.0.1", port: 53496, transport: "tcp" }, | ||
}; | ||
|
||
Deno.test("Response Test", async (t) => { | ||
// const handler = await createHandler(manifest, config); | ||
|
||
await t.step("No login => Redirect", async () => { | ||
const handler = await createHandler(manifest, config); | ||
let resp = await handler( | ||
new Request("http://127.0.0.1/"), | ||
CONN_INFO, | ||
); | ||
|
||
expect(resp.status).toBe(302); | ||
expect(resp.headers.get("location")).toBe("/user/login"); | ||
}); | ||
|
||
await t.step("/user/login has /user/create link", async () => { | ||
const handler = await createHandler(manifest, config); | ||
let resp = await handler( | ||
new Request("http://127.0.0.1/user/login"), | ||
CONN_INFO, | ||
); | ||
|
||
expect(resp.status).toBe(200); | ||
const text = await resp.text(); | ||
expect(text.includes('href="/user/create"')).toBe(true); | ||
}); | ||
|
||
await t.step("/user/create has /user/login link", async () => { | ||
const handler = await createHandler(manifest, config); | ||
let resp = await handler( | ||
new Request("http://127.0.0.1/user/create"), | ||
CONN_INFO, | ||
); | ||
|
||
expect(resp.status).toBe(200); | ||
const text = await resp.text(); | ||
expect(text.includes('href="/user/login"')).toBe(true); | ||
}); | ||
// | ||
// | ||
// await t.step("Not Work Session(incorrect cookie)", async () => { | ||
// let resp = await handler( | ||
// new Request("http://127.0.0.1/session"), | ||
// CONN_INFO, | ||
// ); | ||
// assertEquals(resp.status, 200); | ||
// | ||
// let text = await resp.text(); | ||
// assertEquals(text.includes("<p>count:0</p>"), true); | ||
// | ||
// const sessionKey = | ||
// (resp.headers.get("set-cookie")!).split("session=")[1].split(";")[0]; | ||
// | ||
// resp = await handler( | ||
// new Request("http://127.0.0.1/session", { | ||
// headers: { cookie: `session=${sessionKey}AA` }, | ||
// }), | ||
// CONN_INFO, | ||
// ); | ||
// assertEquals(resp.status, 200); | ||
// text = await resp.text(); | ||
// assertEquals(text.includes("<p>count:0</p>"), true); | ||
// }); | ||
}); | ||
|
||
Deno.test( | ||
{ | ||
name: "Login test", | ||
async fn(t) { | ||
const handler = await createHandler(manifest, config); | ||
let csrfCookieToken = ""; | ||
let csrfToken = ""; | ||
let authSession = ""; | ||
|
||
await t.step("Create account", async () => { | ||
let resp = await handler( | ||
new Request("http://127.0.0.1/user/create"), | ||
CONN_INFO, | ||
); | ||
|
||
const text = await resp.text(); | ||
csrfCookieToken = resp.headers | ||
.get("set-cookie")! | ||
.split("csrf_token=")[1] | ||
.split(";")[0]; | ||
csrfToken = text | ||
.split('<input type="hidden" name="csrf" value="')[1] | ||
.split('"/')[0]; | ||
|
||
const formData = new FormData(); | ||
formData.append("csrf", csrfToken); | ||
formData.append("email", "[email protected]"); | ||
formData.append("password", "password"); | ||
|
||
const headers = new Headers(); | ||
headers.set("cookie", `csrf_token=${csrfCookieToken}`); | ||
|
||
resp = await handler( | ||
new Request("http://127.0.0.1/user/create", { | ||
headers, | ||
method: "POST", | ||
body: formData, | ||
}), | ||
CONN_INFO, | ||
); | ||
|
||
expect(resp.status).toBe(302); | ||
expect(resp.headers.get("location")).toBe("/must_login"); | ||
}); | ||
await t.step("Login", async () => { | ||
const formData = new FormData(); | ||
formData.append("csrf", csrfToken); | ||
formData.append("email", "[email protected]"); | ||
formData.append("password", "password"); | ||
|
||
const headers = new Headers(); | ||
headers.set("cookie", `csrf_token=${csrfCookieToken}`); | ||
|
||
const resp = await handler( | ||
new Request("http://127.0.0.1/user/login", { | ||
headers, | ||
method: "POST", | ||
body: formData, | ||
}), | ||
CONN_INFO, | ||
); | ||
|
||
expect(resp.status).toBe(302); | ||
expect(resp.headers.get("location")).toBe("/must_login"); | ||
authSession = | ||
resp.headers.get("set-cookie")!.split("auth_session=")[1].split( | ||
";", | ||
)[0]; | ||
}); | ||
|
||
await t.step("Logout", async () => { | ||
// const formData = new FormData(); | ||
// formData.append("csrf", csrfToken); | ||
// formData.append("email", "[email protected]"); | ||
// formData.append("password", "password"); | ||
|
||
const headers = new Headers(); | ||
headers.set("cookie", `auth_session=${authSession}`); | ||
|
||
let resp = await handler( | ||
new Request("http://127.0.0.1/must_login", { | ||
headers, | ||
}), | ||
CONN_INFO, | ||
); | ||
|
||
expect(resp.status).toBe(200); | ||
const text = await resp.text(); | ||
expect(text.includes("MUST LOGIN")).toBe(true); | ||
|
||
const formData = new FormData(); | ||
formData.append("csrf", csrfToken); | ||
|
||
resp = await handler( | ||
new Request("http://127.0.0.1/user/logout", { | ||
headers, | ||
method: "POST", | ||
body: formData, | ||
}), | ||
CONN_INFO, | ||
); | ||
expect(resp.status).toBe(302); | ||
expect(resp.headers.get("location")).toBe("/user/login"); | ||
}); | ||
}, | ||
sanitizeOps: false, | ||
sanitizeResources: false, | ||
}, | ||
); |
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,2 @@ | ||
export { expect } from "https://deno.land/[email protected]/expect/mod.ts"; | ||
export { FakeTime } from "https://deno.land/[email protected]/testing/time.ts"; |
Oops, something went wrong.