forked from sergiodxa/remix-utils
-
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.
Add 100% coverage on server-side code (sergiodxa#8)
* Move redirectBack test to server/responses.test.ts * Add tests for all Response helpers * Change CSRF token verification error messages * Add tests for CSRF server-side code * Create tests for bodyParser methods * Add test for typed JSON function * Remove file with old tests * Change import paths in tests * Ignore coverage folder * Update describe title to use function to get the name
- Loading branch information
Showing
6 changed files
with
312 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
node_modules | ||
|
||
/browser | ||
/build | ||
/build | ||
/coverage |
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 was deleted.
Oops, something went wrong.
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,43 @@ | ||
import { bodyParser } from "../../src"; | ||
|
||
describe("Body Parser", () => { | ||
describe(bodyParser.toString, () => { | ||
test("should return string", async () => { | ||
let request = new Request("/", { | ||
method: "POST", | ||
body: new URLSearchParams("a=1&b=2"), | ||
}); | ||
|
||
let body = await bodyParser.toString(request); | ||
|
||
expect(body).toBe("a=1&b=2"); | ||
}); | ||
}); | ||
|
||
describe(bodyParser.toSearchParams, () => { | ||
test("should return URLSearchParams", async () => { | ||
let request = new Request("/", { | ||
method: "POST", | ||
body: new URLSearchParams("a=1&b=2"), | ||
}); | ||
|
||
let params = await bodyParser.toSearchParams(request); | ||
|
||
expect(params.get("a")).toBe("1"); | ||
expect(params.get("b")).toBe("2"); | ||
}); | ||
}); | ||
|
||
describe(bodyParser.toJSON, () => { | ||
test("should return JS object", async () => { | ||
let request = new Request("/", { | ||
method: "POST", | ||
body: new URLSearchParams("a=1&b=2"), | ||
}); | ||
|
||
let body = (await bodyParser.toJSON(request)) as { a: 1; b: 2 }; | ||
|
||
expect(body).toEqual({ a: "1", b: "2" }); | ||
}); | ||
}); | ||
}); |
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,91 @@ | ||
import { createCookieSessionStorage } from "remix"; | ||
import { createAuthenticityToken, verifyAuthenticityToken } from "../../src/"; | ||
|
||
describe("CSRF Server", () => { | ||
let sessionStorage = createCookieSessionStorage({ | ||
cookie: { name: "session", secrets: ["s3cr3t"] }, | ||
}); | ||
|
||
describe(createAuthenticityToken, () => { | ||
test("should return a random string", async () => { | ||
let session = await sessionStorage.getSession(); | ||
const token = createAuthenticityToken(session); | ||
expect(token).toBeDefined(); | ||
expect(token).toHaveLength(136); | ||
}); | ||
|
||
test("the returned token should be stored in the session as csrf", async () => { | ||
let session = await sessionStorage.getSession(); | ||
const token = createAuthenticityToken(session); | ||
expect(session.get("csrf")).toBe(token); | ||
}); | ||
|
||
test("should be able to change the key used to store the token in the session", async () => { | ||
let session = await sessionStorage.getSession(); | ||
const token = createAuthenticityToken(session, "newKey"); | ||
expect(session.get("newKey")).toBe(token); | ||
}); | ||
}); | ||
|
||
describe(verifyAuthenticityToken, () => { | ||
test("should throw Unprocessable Entity if the csrf is not in the session", async () => { | ||
let session = await sessionStorage.getSession(); | ||
let cookie = await sessionStorage.commitSession(session); | ||
let request = new Request("/", { | ||
method: "POST", | ||
headers: { cookie }, | ||
}); | ||
|
||
try { | ||
await verifyAuthenticityToken(request, session); | ||
} catch (error) { | ||
if (!(error instanceof Response)) throw error; | ||
expect(error.status).toBe(422); | ||
expect(await error.json()).toEqual({ | ||
message: "Can't find CSRF token in session.", | ||
}); | ||
} | ||
}); | ||
|
||
test("should throw Unprocessable Entity if csrf is not in the body", async () => { | ||
let session = await sessionStorage.getSession(); | ||
session.set("csrf", "token"); | ||
let cookie = await sessionStorage.commitSession(session); | ||
let request = new Request("/", { | ||
method: "POST", | ||
headers: { cookie }, | ||
}); | ||
|
||
try { | ||
await verifyAuthenticityToken(request, session); | ||
} catch (error) { | ||
if (!(error instanceof Response)) throw error; | ||
expect(error.status).toBe(422); | ||
expect(await error.json()).toEqual({ | ||
message: "Can't find CSRF token in body.", | ||
}); | ||
} | ||
}); | ||
|
||
test("should throw Unprocessable Entity if session and body csrf don't match", async () => { | ||
let session = await sessionStorage.getSession(); | ||
session.set("csrf", "token"); | ||
let cookie = await sessionStorage.commitSession(session); | ||
let request = new Request("/", { | ||
method: "POST", | ||
headers: { cookie }, | ||
body: new URLSearchParams({ csrf: "wrong token" }), | ||
}); | ||
|
||
try { | ||
await verifyAuthenticityToken(request, session); | ||
} catch (error) { | ||
if (!(error instanceof Response)) throw error; | ||
expect(error.status).toBe(422); | ||
expect(await error.json()).toEqual({ | ||
message: "Can't verify CSRF token authenticity.", | ||
}); | ||
} | ||
}); | ||
}); | ||
}); |
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