|
| 1 | +import { test, TestContext, RuntimeError } from "../module.gen.ts"; |
| 2 | +import { assert, assertEquals, assertRejects } from "https://deno.land/[email protected]/assert/mod.ts"; |
| 3 | +import { faker } from "https://deno.land/x/[email protected]/mod.ts"; |
| 4 | + |
| 5 | +test("right_code_first_time", async (ctx: TestContext) => { |
| 6 | + const data = { email: faker.internet.email() }; |
| 7 | + const createdVerification = await ctx.modules.verifications.create({ data }); |
| 8 | + const attemptResult = await ctx.modules.verifications.attempt({ |
| 9 | + token: createdVerification.token, |
| 10 | + code: createdVerification.code, |
| 11 | + }); |
| 12 | + |
| 13 | + assert(attemptResult.succeeded); |
| 14 | + assert(!attemptResult.canTryAgain); |
| 15 | + assertEquals(attemptResult.data, data); |
| 16 | +}); |
| 17 | + |
| 18 | +test("wrong_code_fail", async (ctx: TestContext) => { |
| 19 | + const data = { email: faker.internet.email() }; |
| 20 | + |
| 21 | + const createdVerification = await ctx.modules.verifications.create({ data }); |
| 22 | + const attemptResult = await ctx.modules.verifications.attempt({ |
| 23 | + token: createdVerification.token, |
| 24 | + code: "AAAAAAAA", |
| 25 | + }); |
| 26 | + |
| 27 | + assert(!attemptResult.succeeded, "Verification succeeded when it shouldn't have"); |
| 28 | + assert(attemptResult.canTryAgain, "Verification should have more than 1 try"); |
| 29 | + assertEquals(attemptResult.data, data, "Verification data did not match"); |
| 30 | +}); |
| 31 | + |
| 32 | +test("overattempted", async (ctx: TestContext) => { |
| 33 | + const data = { email: faker.internet.email() }; |
| 34 | + |
| 35 | + const MAX_ATTEMPTS = 5; |
| 36 | + |
| 37 | + const createdVerification = await ctx.modules.verifications.create({ |
| 38 | + data, |
| 39 | + maxAttempts: MAX_ATTEMPTS, |
| 40 | + }); |
| 41 | + |
| 42 | + for (let i = 0; i < MAX_ATTEMPTS - 1; i++) { |
| 43 | + const attemptResult = await ctx.modules.verifications.attempt({ |
| 44 | + token: createdVerification.token, |
| 45 | + code: "AAAAAAAA", |
| 46 | + }); |
| 47 | + |
| 48 | + assert(!attemptResult.succeeded, "Verification succeeded when it shouldn't have"); |
| 49 | + assert(attemptResult.canTryAgain, "Verification ran out of tries earlier than it should have"); |
| 50 | + assertEquals(attemptResult.data, data, "Verification data did not match"); |
| 51 | + } |
| 52 | + |
| 53 | + const attemptResult = await ctx.modules.verifications.attempt({ |
| 54 | + token: createdVerification.token, |
| 55 | + code: "AAAAAAAA", |
| 56 | + }); |
| 57 | + assert(!attemptResult.succeeded, "Verification succeeded when it shouldn't have"); |
| 58 | + assert(!attemptResult.canTryAgain, "Verification should be out of tries"); |
| 59 | + assertEquals(attemptResult.data, data, "Verification data did not match"); |
| 60 | + |
| 61 | + |
| 62 | + const err = await assertRejects(() => ctx.modules.verifications.attempt({ |
| 63 | + token: createdVerification.token, |
| 64 | + code: "AAAAAAAA", |
| 65 | + }), RuntimeError); |
| 66 | + |
| 67 | + assertEquals(err.code, "no_verification_found"); |
| 68 | +}); |
| 69 | + |
| 70 | +test("get_all_methods", async (ctx: TestContext) => { |
| 71 | + const data = { email: faker.internet.email() }; |
| 72 | + |
| 73 | + const createdVerification = await ctx.modules.verifications.create({ data }); |
| 74 | + const getById = await ctx.modules.verifications.get({ |
| 75 | + id: createdVerification.id, |
| 76 | + }); |
| 77 | + const getByToken = await ctx.modules.verifications.get({ |
| 78 | + token: createdVerification.token, |
| 79 | + }); |
| 80 | + const getByData = await ctx.modules.verifications.get({ |
| 81 | + data, |
| 82 | + }); |
| 83 | + |
| 84 | + assertEquals(getById, getByToken); |
| 85 | + assertEquals(getByToken, getByData); |
| 86 | +}); |
0 commit comments