|
| 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("test_sign_up", async (ctx: TestContext) => { |
| 7 | + const username = faker.internet.userName(); |
| 8 | + const password = faker.internet.password(); |
| 9 | + |
| 10 | + // MARK: Should be able to sign up and things should match |
| 11 | + const { token } = await ctx.modules.authUsernamePassword.signUp({ |
| 12 | + username, |
| 13 | + password, |
| 14 | + }); |
| 15 | + |
| 16 | + const { userId } = await ctx.modules.users.authenticateToken({ |
| 17 | + userToken: token.token, |
| 18 | + }); |
| 19 | + |
| 20 | + |
| 21 | + const { users: [user] } = await ctx.modules.users.fetchByUname({ |
| 22 | + usernames: [username], |
| 23 | + }); |
| 24 | + |
| 25 | + assertExists(user); |
| 26 | + assertEquals(user.id, userId); |
| 27 | + |
| 28 | + // MARK: Should not be able to sign up with the same username |
| 29 | + const error = await assertRejects(async () => { |
| 30 | + await ctx.modules.authUsernamePassword.signUp({ |
| 31 | + username, |
| 32 | + password, |
| 33 | + }); |
| 34 | + }, RuntimeError); |
| 35 | + assertEquals(error.code, "user_already_exists"); |
| 36 | +}); |
| 37 | + |
| 38 | +test("test_sign_in", async (ctx: TestContext) => { |
| 39 | + const username = faker.internet.userName(); |
| 40 | + const password = faker.internet.password(); |
| 41 | + |
| 42 | + // MARK: Sign up |
| 43 | + await ctx.modules.authUsernamePassword.signUp({ |
| 44 | + username, |
| 45 | + password, |
| 46 | + }); |
| 47 | + |
| 48 | + // MARK: Can sign in with correct credentials |
| 49 | + const { token } = await ctx.modules.authUsernamePassword.signIn({ |
| 50 | + username, |
| 51 | + password, |
| 52 | + }); |
| 53 | + |
| 54 | + const { userId } = await ctx.modules.users.authenticateToken({ |
| 55 | + userToken: token.token, |
| 56 | + }); |
| 57 | + |
| 58 | + const { users: [user] } = await ctx.modules.users.fetchByUname({ |
| 59 | + usernames: [username], |
| 60 | + }); |
| 61 | + |
| 62 | + assertExists(user); |
| 63 | + assertEquals(user.id, userId); |
| 64 | + assertEquals(user.username, username); |
| 65 | + |
| 66 | + // MARK: Can't sign in with wrong password |
| 67 | + const error = await assertRejects(async () => { |
| 68 | + await ctx.modules.authUsernamePassword.signIn({ |
| 69 | + username, |
| 70 | + password: faker.internet.password(), |
| 71 | + }); |
| 72 | + }, RuntimeError); |
| 73 | + assertEquals(error.code, "invalid_username_or_password"); |
| 74 | +}); |
| 75 | + |
0 commit comments