Skip to content
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

test(http): basic endpoint tests #325

Merged
merged 6 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ coverage:
informational: true
patch:
default:
informational: true
informational: true
ignore:
- "components"
- "islands"
- "routes"
161 changes: 161 additions & 0 deletions tests/http_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.

import { createHandler } from "$fresh/server.ts";
import manifest from "@/fresh.gen.ts";
import {
assert,
assertEquals,
assertFalse,
assertInstanceOf,
assertStringIncludes,
} from "std/testing/asserts.ts";
import type { ConnInfo } from "std/http/server.ts";

const CONN_INFO: ConnInfo = {
localAddr: { hostname: "localhost", port: 8000, transport: "tcp" },
remoteAddr: { hostname: "localhost", port: 53496, transport: "tcp" },
};

Deno.test("[http]", async (test) => {
const handler = await createHandler(manifest);

await test.step("[get] /", async () => {
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
const response = await handler(new Request("http://localhost"), CONN_INFO);

assert(response.ok);
assertInstanceOf(response.body, ReadableStream);
assertEquals(
response.headers.get("content-type"),
"text/html; charset=utf-8",
);
assertEquals(response.status, 200);
});

await test.step("[get] /account", async () => {
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
const response = await handler(
new Request("http://localhost/account"),
CONN_INFO,
);

assertFalse(response.ok);
assertFalse(response.body);
assertEquals(
response.headers.get("location"),
"/signin?from=http://localhost/account",
);
assertEquals(response.status, 303);
});

await test.step("[get] /callback", async () => {
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
const response = await handler(
new Request("http://localhost/callback"),
CONN_INFO,
);

assertFalse(response.ok);
assertInstanceOf(response.body, ReadableStream);
assertEquals(
response.headers.get("content-type"),
"text/html; charset=utf-8",
);
assertEquals(response.status, 500);
});

await test.step("[get] /blog", async () => {
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
const response = await handler(
new Request("http://localhost/blog"),
CONN_INFO,
);

assert(response.ok);
assertInstanceOf(response.body, ReadableStream);
assertEquals(
response.headers.get("content-type"),
"text/html; charset=utf-8",
);
assertEquals(response.status, 200);
});

await test.step("[get] /pricing", async () => {
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
const response = await handler(
new Request("http://localhost/pricing"),
CONN_INFO,
);

assertFalse(response.ok);
assertInstanceOf(response.body, ReadableStream);
assertEquals(
response.headers.get("content-type"),
"text/html; charset=utf-8",
);
assertEquals(response.status, 404);
});

await test.step("[get] /signin", async () => {
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
const response = await handler(
new Request("http://localhost/signin"),
CONN_INFO,
);

assertFalse(response.ok);
assertFalse(response.body);
assertStringIncludes(
response.headers.get("location")!,
"https://github.com/login/oauth/authorize",
);
assertEquals(response.status, 302);
});

await test.step("[get] /signout", async () => {
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
const response = await handler(
new Request("http://localhost/signout"),
CONN_INFO,
);

assertFalse(response.ok);
assertFalse(response.body);
assertEquals(response.headers.get("location"), "/");
assertEquals(response.status, 302);
});

await test.step("[get] /stats", async () => {
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
const response = await handler(
new Request("http://localhost/stats"),
CONN_INFO,
);

assert(response.ok);
assertInstanceOf(response.body, ReadableStream);
assertEquals(
response.headers.get("content-type"),
"text/html; charset=utf-8",
);
assertEquals(response.status, 200);
});

await test.step("[get] /submit", async () => {
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
const response = await handler(
new Request("http://localhost/submit"),
CONN_INFO,
);

assertFalse(response.ok);
assertFalse(response.body);
assertEquals(
response.headers.get("location"),
"/signin?from=http://localhost/submit",
);
assertEquals(response.status, 303);
});

await test.step("[post] /submit", async () => {
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
const response = await handler(
new Request("http://localhost/submit", { method: "POST" }),
CONN_INFO,
);

assertFalse(response.ok);
assertFalse(response.body);
assertEquals(response.status, 401);
});
});
25 changes: 18 additions & 7 deletions utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,6 @@ export async function createVote(vote: Vote) {
export async function deleteVote(vote: Vote) {
vote.item.score--;

const itemKey = ["items", vote.item.id];
const itemsByTimeKey = [
"items_by_time",
vote.item.createdAt.getTime(),
vote.item.id,
];
const itemsByUserKey = ["items_by_user", vote.item.userId, vote.item.id];
const votedItemsByUserKey = [
"voted_items_by_user",
vote.user.id,
Expand All @@ -286,11 +279,29 @@ export async function deleteVote(vote: Vote) {
vote.user.id,
];

const [votedItemsByUserRes, votedUsersByItemRes] = await kv.getMany([
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is something i stumbled upon by accident.
if we compare createVote and deleteVote, we were missing a check for the votedItemsByUserKey and votedUsersByItemKey results.

as the deletion for a non-existent key doesn't throw (anymore?), the mutation on the vote.item.score would've been abused via api calls.

this is why an explicit check for these keys and their respective results has been introduced.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Though, next time, let's keep the objectives per PR to 1 🙂

votedItemsByUserKey,
votedUsersByItemKey,
]);

if (!votedItemsByUserRes.value || !votedUsersByItemRes.value) {
throw new Error(`Failed to delete vote: ${vote}`);
}

const itemKey = ["items", vote.item.id];
const itemsByTimeKey = [
"items_by_time",
vote.item.createdAt.getTime(),
vote.item.id,
];
const itemsByUserKey = ["items_by_user", vote.item.userId, vote.item.id];

const [itemRes, itemsByTimeRes, itemsByUserRes] = await kv.getMany([
itemKey,
itemsByTimeKey,
itemsByUserKey,
]);

const res = await kv.atomic()
.check(itemRes)
.check(itemsByTimeRes)
Expand Down