Skip to content
Merged
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
48 changes: 13 additions & 35 deletions e2e_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Deno.test("[e2e] GET /callback", async (test) => {
sessionId,
};
const id = crypto.randomUUID();
const handleCallbackStub = stub(
using _handleCallbackStub = stub(
_internals,
"handleCallback",
returnsNext([Promise.resolve(handleCallbackResp)]),
Expand All @@ -148,7 +148,7 @@ Deno.test("[e2e] GET /callback", async (test) => {
email: crypto.randomUUID(),
};
const stripeRespBody: Partial<Stripe.Response<Stripe.Customer>> = { id };
const fetchStub = stub(
using _fetchStub = stub(
globalThis,
"fetch",
returnsNext([
Expand All @@ -158,8 +158,6 @@ Deno.test("[e2e] GET /callback", async (test) => {
);
const req = new Request("http://localhost/callback");
await handler(req);
handleCallbackStub.restore();
fetchStub.restore();

const user = await getUser(githubRespBody.login);
assert(user !== null);
Expand All @@ -176,7 +174,7 @@ Deno.test("[e2e] GET /callback", async (test) => {
sessionId: crypto.randomUUID(),
};
const id = crypto.randomUUID();
const handleCallbackStub = stub(
using _handleCallbackStub = stub(
_internals,
"handleCallback",
returnsNext([Promise.resolve(handleCallbackResp)]),
Expand All @@ -186,7 +184,7 @@ Deno.test("[e2e] GET /callback", async (test) => {
email: crypto.randomUUID(),
};
const stripeRespBody: Partial<Stripe.Response<Stripe.Customer>> = { id };
const fetchStub = stub(
using _fetchStub = stub(
globalThis,
"fetch",
returnsNext([
Expand All @@ -196,8 +194,6 @@ Deno.test("[e2e] GET /callback", async (test) => {
);
const req = new Request("http://localhost/callback");
await handler(req);
handleCallbackStub.restore();
fetchStub.restore();

const user = await getUser(githubRespBody.login);
assert(user !== null);
Expand Down Expand Up @@ -653,7 +649,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {
});

await test.step("serves not found response if the user is not found for subscription creation", async () => {
const constructEventAsyncStub = stub(
using _constructEventAsyncStub = stub(
stripe.webhooks,
"constructEventAsync",
returnsNext([
Expand All @@ -668,8 +664,6 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {
}),
);

constructEventAsyncStub.restore();

assertEquals(resp.status, STATUS_CODE.NotFound);
assertText(resp);
assertEquals(await resp.text(), "User not found");
Expand All @@ -679,7 +673,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {
const user = randomUser();
await createUser(user);

const constructEventAsyncStub = stub(
using _constructEventAsyncStub = stub(
stripe.webhooks,
"constructEventAsync",
returnsNext([
Expand All @@ -697,14 +691,12 @@ 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 });
});

await test.step("serves not found response if the user is not found for subscription deletion", async () => {
const constructEventAsyncStub = stub(
using _constructEventAsyncStub = stub(
stripe.webhooks,
"constructEventAsync",
returnsNext([
Expand All @@ -719,8 +711,6 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {
}),
);

constructEventAsyncStub.restore();

assertEquals(resp.status, STATUS_CODE.NotFound);
assertText(resp);
assertEquals(await resp.text(), "User not found");
Expand All @@ -730,7 +720,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {
const user: User = { ...randomUser(), isSubscribed: true };
await createUser(user);

const constructEventAsyncStub = stub(
using _constructEventAsyncStub = stub(
stripe.webhooks,
"constructEventAsync",
returnsNext([
Expand All @@ -748,14 +738,12 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {
}),
);

constructEventAsyncStub.restore();

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

await test.step("serves bad request response if the event type is not supported", async () => {
const constructEventAsyncStub = stub(
using _constructEventAsyncStub = stub(
stripe.webhooks,
"constructEventAsync",
returnsNext([
Expand All @@ -770,8 +758,6 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {
}),
);

constructEventAsyncStub.restore();

assertEquals(resp.status, STATUS_CODE.BadRequest);
assertText(resp);
assertEquals(await resp.text(), "Event type not supported");
Expand Down Expand Up @@ -849,7 +835,7 @@ Deno.test("[e2e] GET /account/manage", async (test) => {
const session = { url: "https://stubbing-returned-url" } as Stripe.Response<
Stripe.BillingPortal.Session
>;
const sessionsCreateStub = stub(
using _sessionsCreateStub = stub(
stripe.billingPortal.sessions,
"create",
resolvesNext([session]),
Expand All @@ -861,8 +847,6 @@ Deno.test("[e2e] GET /account/manage", async (test) => {
}),
);

sessionsCreateStub.restore();

assertRedirect(resp, session.url);
});
});
Expand All @@ -882,7 +866,7 @@ Deno.test("[e2e] GET /account/upgrade", async (test) => {

await test.step("serves internal server error response if the `STRIPE_PREMIUM_PLAN_PRICE_ID` environment variable is not set", async () => {
// Suppress the error message thrown by the handler
const stubbedError = stub(console, "error");
using _stubbedError = stub(console, "error");

setupEnv(
{ "STRIPE_PREMIUM_PLAN_PRICE_ID": null },
Expand All @@ -896,8 +880,6 @@ Deno.test("[e2e] GET /account/upgrade", async (test) => {

assertEquals(resp.status, STATUS_CODE.InternalServerError);
assertHtml(resp);

stubbedError.restore();
});

await test.step("serves not found response if Stripe is disabled", async () => {
Expand All @@ -921,7 +903,7 @@ Deno.test("[e2e] GET /account/upgrade", async (test) => {
const session = { url: null } as Stripe.Response<
Stripe.Checkout.Session
>;
const sessionsCreateStub = stub(
using _sessionsCreateStub = stub(
stripe.checkout.sessions,
"create",
resolvesNext([session]),
Expand All @@ -933,8 +915,6 @@ Deno.test("[e2e] GET /account/upgrade", async (test) => {
}),
);

sessionsCreateStub.restore();

assertEquals(resp.status, STATUS_CODE.NotFound);
assertHtml(resp);
});
Expand All @@ -948,7 +928,7 @@ Deno.test("[e2e] GET /account/upgrade", async (test) => {
const session = { url: "https://stubbing-returned-url" } as Stripe.Response<
Stripe.Checkout.Session
>;
const sessionsCreateStub = stub(
using _sessionsCreateStub = stub(
stripe.checkout.sessions,
"create",
resolvesNext([session]),
Expand All @@ -960,8 +940,6 @@ Deno.test("[e2e] GET /account/upgrade", async (test) => {
}),
);

sessionsCreateStub.restore();

assertRedirect(resp, session.url!);
});
});
Expand Down
6 changes: 2 additions & 4 deletions utils/github_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BadRequestError } from "@/utils/http.ts";
Deno.test("[plugins] getGitHubUser()", async (test) => {
await test.step("rejects on error message", async () => {
const message = crypto.randomUUID();
const fetchStub = stub(
using _fetchStub = stub(
globalThis,
"fetch",
returnsNext([
Expand All @@ -23,17 +23,15 @@ Deno.test("[plugins] getGitHubUser()", async (test) => {
BadRequestError,
message,
);
fetchStub.restore();
});

await test.step("resolves to a GitHub user object", async () => {
const body = { login: crypto.randomUUID(), email: crypto.randomUUID() };
const fetchStub = stub(
using _fetchStub = stub(
globalThis,
"fetch",
returnsNext([Promise.resolve(Response.json(body))]),
);
assertEquals(await getGitHubUser(crypto.randomUUID()), body);
fetchStub.restore();
});
});
4 changes: 1 addition & 3 deletions utils/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Deno.test("[http] fetchValues()", async () => {
};
const resp2Cursor = crypto.randomUUID();
const resp2 = Promise.resolve(Response.json(resp2Body));
const fetchStub = stub(
using _fetchStub = stub(
globalThis,
"fetch",
returnsNext([resp1, resp2]),
Expand All @@ -56,6 +56,4 @@ Deno.test("[http] fetchValues()", async () => {
await fetchValues<Item>(endpoint + "/api/items", resp2Cursor),
resp2Body,
);

fetchStub.restore();
});
Loading