shellphone.app/app/auth/mutations/forgot-password.test.ts

60 lines
1.6 KiB
TypeScript
Raw Normal View History

import { hash256, Ctx } from "blitz";
import previewEmail from "preview-email";
2021-07-31 14:33:18 +00:00
import forgotPassword from "./forgot-password";
import db from "../../../db";
2021-07-31 14:33:18 +00:00
beforeEach(async () => {
await db.$reset();
});
2021-07-31 14:33:18 +00:00
const generatedToken = "plain-token";
2021-07-31 14:33:18 +00:00
jest.mock("blitz", () => ({
...jest.requireActual<object>("blitz")!,
generateToken: () => generatedToken,
}));
jest.mock("preview-email", () => jest.fn());
2021-07-31 14:33:18 +00:00
describe.skip("forgotPassword mutation", () => {
2021-07-31 14:33:18 +00:00
it("does not throw error if user doesn't exist", async () => {
2021-08-01 14:03:49 +00:00
await expect(forgotPassword({ email: "no-user@email.com" }, {} as Ctx)).resolves.not.toThrow();
});
2021-07-31 14:33:18 +00:00
it("works correctly", async () => {
// Create test user
const user = await db.user.create({
data: {
email: "user@example.com",
tokens: {
// Create old token to ensure it's deleted
create: {
type: "RESET_PASSWORD",
hashedToken: "token",
expiresAt: new Date(),
sentTo: "user@example.com",
},
},
},
include: { tokens: true },
});
2021-07-31 14:33:18 +00:00
// Invoke the mutation
await forgotPassword({ email: user.email }, {} as Ctx);
2021-07-31 14:33:18 +00:00
const tokens = await db.token.findMany({ where: { userId: user.id } });
const token = tokens[0];
if (!user.tokens[0]) throw new Error("Missing user token");
if (!token) throw new Error("Missing token");
2021-07-31 14:33:18 +00:00
// delete's existing tokens
expect(tokens.length).toBe(1);
expect(token.id).not.toBe(user.tokens[0].id);
expect(token.type).toBe("RESET_PASSWORD");
expect(token.sentTo).toBe(user.email);
expect(token.hashedToken).toBe(hash256(generatedToken));
expect(token.expiresAt > new Date()).toBe(true);
expect(previewEmail).toBeCalled();
});
});