|
| 1 | +import { test, TestContext } from "../module.gen.ts"; |
| 2 | +import { assertExists, assertEquals, assertRejects } from "https://deno.land/std@0.217.0/assert/mod.ts"; |
| 3 | +import { faker } from "https://deno.land/x/deno_faker@v1.0.3/mod.ts"; |
| 4 | +import { RuntimeError } from "../module.gen.ts"; |
| 5 | + |
| 6 | +test("accept_matching_password", async (ctx: TestContext) => { |
| 7 | + const { user } = await ctx.modules.users.create({ |
| 8 | + username: faker.internet.userName(), |
| 9 | + }); |
| 10 | + assertExists(user); |
| 11 | + |
| 12 | + // Register password |
| 13 | + const password = faker.internet.password(); |
| 14 | + await ctx.modules.userPasswords.add({ |
| 15 | + userId: user.id, |
| 16 | + password, |
| 17 | + }); |
| 18 | + |
| 19 | + // Verify password |
| 20 | + await ctx.modules.userPasswords.verify({ |
| 21 | + userId: user.id, |
| 22 | + passGuess: password, |
| 23 | + }); |
| 24 | + |
| 25 | + // Change password |
| 26 | + const newPass = faker.internet.password(); |
| 27 | + await ctx.modules.userPasswords.update({ |
| 28 | + userId: user.id, |
| 29 | + newPassword: newPass, |
| 30 | + }); |
| 31 | + |
| 32 | + // Verify new password |
| 33 | + await ctx.modules.userPasswords.verify({ |
| 34 | + userId: user.id, |
| 35 | + passGuess: newPass, |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | + |
| 40 | +test("reject_different_password", async (ctx: TestContext) => { |
| 41 | + const { user } = await ctx.modules.users.create({ |
| 42 | + username: faker.internet.userName(), |
| 43 | + }); |
| 44 | + assertExists(user); |
| 45 | + |
| 46 | + // Register password |
| 47 | + const password = faker.internet.password(); |
| 48 | + await ctx.modules.userPasswords.add({ |
| 49 | + userId: user.id, |
| 50 | + password, |
| 51 | + }); |
| 52 | + |
| 53 | + const wrongPassword = faker.internet.password(); |
| 54 | + |
| 55 | + // Verify incorrect password |
| 56 | + const error = await assertRejects(async () => { |
| 57 | + await ctx.modules.userPasswords.verify({ |
| 58 | + userId: user.id, |
| 59 | + passGuess: wrongPassword, |
| 60 | + }); |
| 61 | + }, RuntimeError); |
| 62 | + |
| 63 | + // Verify error message |
| 64 | + assertExists(error.message); |
| 65 | + assertEquals(error.code, "password_invalid"); |
| 66 | +}); |
| 67 | + |
| 68 | +test("reject_unregistered", async (ctx: TestContext) => { |
| 69 | + const { user } = await ctx.modules.users.create({ |
| 70 | + username: faker.internet.userName(), |
| 71 | + }); |
| 72 | + assertExists(user); |
| 73 | + |
| 74 | + // Register password |
| 75 | + const password = faker.internet.password(); |
| 76 | + await ctx.modules.userPasswords.add({ |
| 77 | + userId: user.id, |
| 78 | + password, |
| 79 | + }); |
| 80 | + |
| 81 | + const wrongPassword = faker.internet.password(); |
| 82 | + |
| 83 | + // Verify "correct" password with unregistered user |
| 84 | + const error = await assertRejects(async () => { |
| 85 | + await ctx.modules.userPasswords.verify({ |
| 86 | + userId: crypto.randomUUID(), |
| 87 | + passGuess: wrongPassword, |
| 88 | + }); |
| 89 | + }, RuntimeError); |
| 90 | + |
| 91 | + // Verify error message |
| 92 | + assertExists(error.message); |
| 93 | + assertEquals(error.code, "user_does_not_have_password"); |
| 94 | +}); |
0 commit comments