From 3b940602d32c4476a064eda0dd7446ffc2005aea Mon Sep 17 00:00:00 2001 From: Daniel Adu-Gyan <26229521+danieladugyan@users.noreply.github.com> Date: Tue, 23 Apr 2024 02:15:07 +0200 Subject: [PATCH] Fix webshop tests that assumed pre-existing data in DB Tests broke since there were no access policies in the database --- src/lib/server/shop/getTickets.test.ts | 12 +--- src/lib/server/shop/getTickets.ts | 68 ++++++------------- src/lib/server/shop/payments/purchase.test.ts | 9 +-- src/lib/server/shop/types.ts | 4 +- 4 files changed, 29 insertions(+), 64 deletions(-) diff --git a/src/lib/server/shop/getTickets.test.ts b/src/lib/server/shop/getTickets.test.ts index 82b01d6cf..a498eed8e 100644 --- a/src/lib/server/shop/getTickets.test.ts +++ b/src/lib/server/shop/getTickets.test.ts @@ -1,7 +1,6 @@ import { PrismaClient, type Member } from "@prisma/client"; import { enhance } from "@zenstackhq/runtime"; import { afterAll, describe, expect, it } from "vitest"; -import { getAccessPolicies } from "../../../hooks.server.helpers"; import { getTickets } from "./getTickets"; import { addMockTickets, @@ -10,6 +9,7 @@ import { removeMockTickets, removeMockUsers, } from "./mock"; +import apiNames from "$lib/utils/apiNames"; const prisma = new PrismaClient(); const SUITE_PREFIX = "getTickets"; @@ -43,10 +43,7 @@ describe("Get tickets as logged in user", async () => { user: { studentId: users.customerMember.studentId, memberId: users.customerMember.id, - policies: await getAccessPolicies( - prisma, - users.customerMember.studentId!, - ), + policies: [apiNames.EVENT.READ], }, }); afterAll(async () => { @@ -63,10 +60,7 @@ describe("Get tickets as anonymous user", async () => { user: { studentId: users.customerMember.studentId, memberId: users.customerMember.id, - policies: await getAccessPolicies( - prisma, - users.customerMember.studentId!, - ), + policies: [apiNames.EVENT.READ], }, }); afterAll(async () => { diff --git a/src/lib/server/shop/getTickets.ts b/src/lib/server/shop/getTickets.ts index f4eef25c0..18e567f63 100644 --- a/src/lib/server/shop/getTickets.ts +++ b/src/lib/server/shop/getTickets.ts @@ -1,10 +1,10 @@ -import type { - Event, - Prisma, +import { PrismaClient, - Shoppable, - Tag, - Ticket, + type Event, + type Prisma, + type Shoppable, + type Tag, + type Ticket, } from "@prisma/client"; import { removeExpiredConsumables } from "./addToCart/reservations"; import { @@ -13,6 +13,7 @@ import { type DBShopIdentification, type ShopIdentification, } from "./types"; +import dayjs from "dayjs"; export type TicketWithMoreInfo = Ticket & Shoppable & { @@ -32,42 +33,21 @@ const ticketIncludedFields = (id: DBShopIdentification) => ({ consumables: { where: { ...id, - OR: [ - { - expiresAt: { - gt: new Date(), - }, - }, - { - expiresAt: null, - }, - ], - }, - }, - reservations: { - where: { - ...id, + OR: [{ expiresAt: { gt: new Date() } }, { expiresAt: null }], }, }, + reservations: { where: { ...id } }, _count: { select: { // Number of bought tickets consumables: { - where: { - purchasedAt: { - not: null, - }, - }, + where: { purchasedAt: { not: null } }, }, }, }, }, }, - event: { - include: { - tags: true, - }, - }, + event: { include: { tags: true } }, }); type TicketInclude = ReturnType; @@ -123,33 +103,27 @@ export const getTicket = async ( } return formatTicket(ticket); }; + +/** + * Retrieves tickets from the database based on the provided shop identification. + * @param prisma - The Prisma client instance. + * @param identification - Either the user's ID or the user's session ID. + * @returns A promise that resolves to an array of tickets. + */ export const getTickets = async ( prisma: PrismaClient, identification: ShopIdentification, ): Promise => { const dbId = dbIdentification(identification); - const tenDaysAgo = new Date(Date.now() - 10 * 24 * 60 * 60 * 1000); + const tenDaysAgo = dayjs().subtract(10, "days").toDate(); const tickets = await prisma.ticket.findMany({ where: { shoppable: { AND: [ + { OR: [{ removedAt: null }, { removedAt: { lt: new Date() } }] }, { OR: [ - { - removedAt: null, - }, - { - removedAt: { - lt: new Date(), - }, - }, - ], - }, - { - OR: [ - { - availableTo: null, - }, + { availableTo: null }, { availableTo: { gt: tenDaysAgo, // show items which were available in the last 10 days diff --git a/src/lib/server/shop/payments/purchase.test.ts b/src/lib/server/shop/payments/purchase.test.ts index 26658047c..8c21802cd 100644 --- a/src/lib/server/shop/payments/purchase.test.ts +++ b/src/lib/server/shop/payments/purchase.test.ts @@ -20,7 +20,6 @@ import { it, vi, } from "vitest"; -import { getAccessPolicies } from "../../../../hooks.server.helpers"; import { MOCK_ACTIVE_TICKET, MOCK_ACTIVE_TICKET_2, @@ -35,6 +34,7 @@ import { dbIdentification, type ShopIdentification, } from "../types"; +import apiNames from "$lib/utils/apiNames"; const mockFns = vi.hoisted(() => ({ customers: { @@ -497,7 +497,7 @@ const addPurchaseTestForUser = ( describe("stripe customer creation", () => { if (identification.memberId) { - it("creates a stripe customer if no stripe id in db", async () => { + it("creates a stripe customer if no stripe is in db", async () => { await purchaseCart(prismaWithAccess, identification, "idempotency-key"); expect(mockFns.customers.retrieve).not.toHaveBeenCalled(); expect(mockFns.customers.create).toHaveBeenCalledOnce(); @@ -550,10 +550,7 @@ describe("Purchase as logged in user", async () => { user: { studentId: users.customerMember.studentId, memberId: users.customerMember.id, - policies: await getAccessPolicies( - prisma, - users.customerMember.studentId!, - ), + policies: [apiNames.EVENT.READ, apiNames.MEMBER.READ], }, }); addPurchaseTestForUser(prismaWithAccess, users.adminMember, { diff --git a/src/lib/server/shop/types.ts b/src/lib/server/shop/types.ts index 3c0125c42..e0a0dc104 100644 --- a/src/lib/server/shop/types.ts +++ b/src/lib/server/shop/types.ts @@ -7,10 +7,10 @@ export type TransactionClient = Parameters< export type ShopIdentification = | { memberId: string; - externalCode?: undefined; + externalCode?: never; } | { - memberId?: undefined; + memberId?: never; externalCode: string; };