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

feat: add re-introduce user.id fields #670

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
31 changes: 18 additions & 13 deletions e2e_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Deno.test("[e2e] GET /", async () => {

Deno.test("[e2e] GET /callback", async (test) => {
setupEnv();
const id = crypto.randomUUID();
const login = crypto.randomUUID();
const sessionId = crypto.randomUUID();

Expand All @@ -137,17 +138,19 @@ Deno.test("[e2e] GET /callback", async (test) => {
},
sessionId,
};
const id = crypto.randomUUID();
const handleCallbackStub = stub(
_internals,
"handleCallback",
returnsNext([Promise.resolve(handleCallbackResp)]),
);
const githubRespBody = {
id,
login,
email: crypto.randomUUID(),
};
const stripeRespBody: Partial<Stripe.Response<Stripe.Customer>> = { id };
const stripeRespBody: Partial<Stripe.Response<Stripe.Customer>> = {
id: crypto.randomUUID(),
};
const fetchStub = stub(
window,
"fetch",
Expand All @@ -161,7 +164,7 @@ Deno.test("[e2e] GET /callback", async (test) => {
handleCallbackStub.restore();
fetchStub.restore();

const user = await getUser(githubRespBody.login);
const user = await getUser(githubRespBody.id);
assert(user !== null);
assertEquals(user.sessionId, handleCallbackResp.sessionId);
});
Expand All @@ -175,17 +178,19 @@ Deno.test("[e2e] GET /callback", async (test) => {
},
sessionId: crypto.randomUUID(),
};
const id = crypto.randomUUID();
const handleCallbackStub = stub(
_internals,
"handleCallback",
returnsNext([Promise.resolve(handleCallbackResp)]),
);
const githubRespBody = {
id,
login,
email: crypto.randomUUID(),
};
const stripeRespBody: Partial<Stripe.Response<Stripe.Customer>> = { id };
const stripeRespBody: Partial<Stripe.Response<Stripe.Customer>> = {
id: crypto.randomUUID(),
};
const fetchStub = stub(
window,
"fetch",
Expand All @@ -199,7 +204,7 @@ Deno.test("[e2e] GET /callback", async (test) => {
handleCallbackStub.restore();
fetchStub.restore();

const user = await getUser(githubRespBody.login);
const user = await getUser(githubRespBody.id);
assert(user !== null);
assertNotEquals(user.sessionId, sessionId);
});
Expand Down Expand Up @@ -418,7 +423,7 @@ Deno.test("[e2e] POST /submit", async (test) => {
body,
}),
);
const items = await collectValues(listItemsByUser(user.login));
const items = await collectValues(listItemsByUser(user.id));

assertRedirect(resp, "/");
// Deep partial match since the item ID is a ULID generated at runtime
Expand Down Expand Up @@ -493,7 +498,7 @@ Deno.test("[e2e] GET /api/users/[login]/items", async (test) => {
const user = randomUser();
const item: Item = {
...randomItem(),
userLogin: user.login,
userId: user.id,
};
const req = new Request(`http://localhost/api/users/${user.login}/items`);

Expand Down Expand Up @@ -547,7 +552,7 @@ Deno.test("[e2e] POST /api/vote", async (test) => {
});

await test.step("creates a vote", async () => {
const item = { ...randomItem(), userLogin: user.login };
const item = { ...randomItem(), userId: user.id };
await createItem(item);
const resp = await handler(
new Request(url, {
Expand Down Expand Up @@ -700,7 +705,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {
constructEventAsyncStub.restore();

assertEquals(resp.status, STATUS_CODE.Created);
assertEquals(await getUser(user.login), { ...user, isSubscribed: true });
assertEquals(await getUser(user.id), { ...user, isSubscribed: true });
});

await test.step("serves not found response if the user is not found for subscription deletion", async () => {
Expand Down Expand Up @@ -750,7 +755,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {

constructEventAsyncStub.restore();

assertEquals(await getUser(user.login), { ...user, isSubscribed: false });
assertEquals(await getUser(user.id), { ...user, isSubscribed: false });
assertEquals(resp.status, STATUS_CODE.Accepted);
});

Expand Down Expand Up @@ -970,11 +975,11 @@ Deno.test("[e2e] GET /api/me/votes", async () => {
await createItem(item1);
await createItem(item2);
await createVote({
userLogin: user.login,
userId: user.id,
itemId: item1.id,
});
await createVote({
userLogin: user.login,
userId: user.id,
itemId: item2.id,
});
const resp = await handler(
Expand Down
6 changes: 3 additions & 3 deletions islands/ItemsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ function ItemSummary(props: ItemSummaryProps) {
</p>
<p class="text-gray-500">
<GitHubAvatarImg
login={props.item.userLogin}
login={props.item.id}
size={24}
class="mr-2"
/>
<a class="hover:underline" href={`/users/${props.item.userLogin}`}>
{props.item.userLogin}
<a class="hover:underline" href={`/users/${props.item.id}`}>
{props.item.userId}
</a>{" "}
{timeAgo(new Date(decodeTime(props.item.id)))}
</p>
Expand Down
3 changes: 2 additions & 1 deletion plugins/kv_oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ export default {
);

const githubUser = await getGitHubUser(tokens.accessToken);
const user = await getUser(githubUser.login);
const user = await getUser(githubUser.id);

if (user === null) {
const user: User = {
id: githubUser.id,
login: githubUser.login,
sessionId,
isSubscribed: false,
Expand Down
2 changes: 1 addition & 1 deletion routes/api/me/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SignedInState } from "@/plugins/session.ts";

export const handler: Handlers<undefined, SignedInState> = {
async GET(_req, ctx) {
const iter = listItemsVotedByUser(ctx.state.sessionUser.login);
const iter = listItemsVotedByUser(ctx.state.sessionUser.id);
const items = await collectValues(iter);
return Response.json(items);
},
Expand Down
4 changes: 2 additions & 2 deletions routes/api/users/[login]/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import type { Handlers } from "$fresh/server.ts";
import { getUser } from "@/utils/db.ts";
import { getUserByLogin } from "@/utils/db.ts";

export const handler: Handlers = {
async GET(_req, ctx) {
const user = await getUser(ctx.params.login);
const user = await getUserByLogin(ctx.params.login);
if (user === null) throw new Deno.errors.NotFound("User not found");
return Response.json(user);
},
Expand Down
6 changes: 3 additions & 3 deletions routes/api/users/[login]/items.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import type { Handlers } from "$fresh/server.ts";
import { collectValues, getUser, listItemsByUser } from "@/utils/db.ts";
import { collectValues, getUserByLogin, listItemsByUser } from "@/utils/db.ts";
import { getCursor } from "@/utils/http.ts";

export const handler: Handlers = {
async GET(req, ctx) {
const user = await getUser(ctx.params.login);
const user = await getUserByLogin(ctx.params.login);
if (user === null) throw new Deno.errors.NotFound("User not found");

const url = new URL(req.url);
const iter = listItemsByUser(ctx.params.login, {
const iter = listItemsByUser(user.id, {
cursor: getCursor(url),
limit: 10,
});
Expand Down
2 changes: 1 addition & 1 deletion routes/api/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const handler: Handlers<undefined, SignedInState> = {

await createVote({
itemId,
userLogin: ctx.state.sessionUser.login,
userId: ctx.state.sessionUser.id,
});

return new Response(null, { status: STATUS_CODE.Created });
Expand Down
2 changes: 1 addition & 1 deletion routes/submit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const handler: Handlers<undefined, SignedInState> = {

await createItem({
id: ulid(),
userLogin: ctx.state.sessionUser.login,
userId: ctx.state.sessionUser.id,
title,
url,
score: 0,
Expand Down
Loading
Loading