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

84 lines
2.0 KiB
TypeScript
Raw Normal View History

import { hash256, SecurePassword } from "blitz";
2021-07-31 14:33:18 +00:00
import db from "../../../db";
import resetPassword from "./reset-password";
2021-07-31 14:33:18 +00:00
beforeEach(async () => {
await db.$reset();
});
2021-07-31 14:33:18 +00:00
const mockCtx: any = {
session: {
$create: jest.fn,
},
};
2021-07-31 14:33:18 +00:00
describe.skip("resetPassword mutation", () => {
2021-07-31 14:33:18 +00:00
it("works correctly", async () => {
expect(true).toBe(true);
2021-07-31 14:33:18 +00:00
// Create test user
const goodToken = "randomPasswordResetToken";
const expiredToken = "expiredRandomPasswordResetToken";
const future = new Date();
future.setHours(future.getHours() + 4);
const past = new Date();
past.setHours(past.getHours() - 4);
2021-07-31 14:33:18 +00:00
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: hash256(expiredToken),
expiresAt: past,
sentTo: "user@example.com",
},
{
type: "RESET_PASSWORD",
hashedToken: hash256(goodToken),
expiresAt: future,
sentTo: "user@example.com",
},
],
},
},
include: { tokens: true },
});
2021-07-31 14:33:18 +00:00
const newPassword = "newPassword";
2021-07-31 14:33:18 +00:00
// Non-existent token
await expect(
resetPassword({ token: "no-token", password: "", passwordConfirmation: "" }, mockCtx)
).rejects.toThrowError();
2021-07-31 14:33:18 +00:00
// Expired token
await expect(
resetPassword(
{ token: expiredToken, password: newPassword, passwordConfirmation: newPassword },
mockCtx
)
).rejects.toThrowError();
2021-07-31 14:33:18 +00:00
// Good token
await resetPassword(
{ token: goodToken, password: newPassword, passwordConfirmation: newPassword },
mockCtx
);
2021-07-31 14:33:18 +00:00
// Delete's the token
const numberOfTokens = await db.token.count({ where: { userId: user.id } });
expect(numberOfTokens).toBe(0);
2021-07-31 14:33:18 +00:00
// Updates user's password
const updatedUser = await db.user.findFirst({ where: { id: user.id } });
2021-07-31 14:33:18 +00:00
expect(await SecurePassword.verify(updatedUser!.hashedPassword, newPassword)).toBe(
SecurePassword.VALID
);
});
});