Skip to content

Commit

Permalink
Delete database records after now
Browse files Browse the repository at this point in the history
  • Loading branch information
jpraissman committed Oct 16, 2024
1 parent a5a5532 commit 86a308f
Showing 1 changed file with 45 additions and 37 deletions.
82 changes: 45 additions & 37 deletions tests/auth/get-authenticated-user.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeAll, expect, test } from "bun:test";
import { afterAll, beforeAll, expect, test } from "bun:test";

import { prisma } from "@good-dog/db";
import { _trpcCaller } from "@good-dog/trpc/server";
Expand All @@ -7,61 +7,61 @@ import { MockNextCookies } from "../mocks/MockNextCookies";

// Seeds the database before running the tests
beforeAll(async () => {
const alice = await prisma.user.upsert({
where: { email: "alice@prisma.io" },
const person1 = await prisma.user.upsert({
where: { email: "person1@prisma.io" },
update: {},
create: {
email: "alice@prisma.io",
name: "Alice",
password: "alicePasswod",
email: "person1@prisma.io",
name: "Person 1",
password: "person1Password",
},
});
await prisma.session.upsert({
where: { token: "aliceToken" },
where: { token: "person1Token" },
update: {
expiresAt: new Date(
new Date().setFullYear(new Date().getFullYear() + 10),
),
},
create: {
userId: alice.id,
token: "aliceToken",
userId: person1.id,
token: "person1Token",
expiresAt: new Date(
new Date().setFullYear(new Date().getFullYear() + 10),
),
},
});

const bob = await prisma.user.upsert({
where: { email: "bob@gmail.com" },
const person2 = await prisma.user.upsert({
where: { email: "person2@gmail.com" },
update: {},
create: {
email: "bob@gmail.com",
name: "Bob Jones",
password: "bobPassword",
email: "person2@gmail.com",
name: "Person2 Jones",
password: "person2Password",
},
});
await prisma.session.upsert({
where: { token: "bobToken1" },
where: { token: "person2Token1" },
update: {
expiresAt: new Date(new Date().setFullYear(new Date().getFullYear() - 1)),
},
create: {
userId: bob.id,
token: "bobToken1",
userId: person2.id,
token: "person2Token1",
expiresAt: new Date(new Date().setFullYear(new Date().getFullYear() - 1)),
},
});
await prisma.session.upsert({
where: { token: "bobToken2" },
where: { token: "person2Token2" },
update: {
expiresAt: new Date(
new Date().setFullYear(new Date().getFullYear() + 10),
),
},
create: {
userId: bob.id,
token: "bobToken2",
userId: person2.id,
token: "person2Token2",
expiresAt: new Date(
new Date().setFullYear(new Date().getFullYear() + 10),
),
Expand All @@ -72,40 +72,29 @@ beforeAll(async () => {
test("Correct user is returned when they have a valid session.", async () => {
// Set the cookies
const cookies = new MockNextCookies();
cookies.set("sessionToken", "aliceToken");
cookies.set("sessionToken", "person1Token");
await cookies.apply();

const user = await _trpcCaller.user();

expect(user.email).toEqual("alice@prisma.io");
expect(user.email).toEqual("person1@prisma.io");
});

test("Correct user is returned when they have multiple sessions and one is valid.", async () => {
// Set the cookies
const cookies = new MockNextCookies();
cookies.set("sessionToken", "bobToken2");
cookies.set("sessionToken", "person2Token2");
await cookies.apply();

const user = await _trpcCaller.user();

expect(user.email).toEqual("bob@gmail.com");
expect(user.email).toEqual("person2@gmail.com");
});

test("'UNAUTHORIZED' error is thrown when no session is found for the token.", async () => {
// Set the cookies
const cookies = new MockNextCookies();
cookies.set("sessionToken", "testToken");
await cookies.apply();

const getUser = async () => await _trpcCaller.user();

expect(getUser).toThrow("UNAUTHORIZED");
});

test("'UNAUTHORIZED' error is thrown when no session is found for the token.", async () => {
// Set the cookies
const cookies = new MockNextCookies();
cookies.set("sessionToken", "testToken");
cookies.set("sessionToken", "kjhakhouadakjs");
await cookies.apply();

const getUser = async () => await _trpcCaller.user();
Expand All @@ -125,10 +114,29 @@ test("'UNAUTHORIZED' error is thrown when there is no 'sessionToken' cookie.", a
test("'UNAUTHORIZED' error is thrown when session is expired.", async () => {
// Set the cookies
const cookies = new MockNextCookies();
cookies.set("sessionToken", "bobToken1");
cookies.set("sessionToken", "person2Token1");
await cookies.apply();

const getUser = async () => await _trpcCaller.user();

expect(getUser).toThrow("UNAUTHORIZED");
});

// Delete the records created for these tests
afterAll(async () => {
await prisma.session.delete({
where: { token: "person1Token" },
});
await prisma.session.delete({
where: { token: "person2Token1" },
});
await prisma.session.delete({
where: { token: "person2Token2" },
});
await prisma.user.delete({
where: { email: "person1@prisma.io" },
});
await prisma.user.delete({
where: { email: "person2@gmail.com" },
});
});

0 comments on commit 86a308f

Please sign in to comment.