Skip to content

Commit

Permalink
Fix webshop tests that assumed pre-existing data in DB
Browse files Browse the repository at this point in the history
Tests broke since there were no access policies in the database
  • Loading branch information
danieladugyan committed Apr 23, 2024
1 parent 04c98a8 commit 2dde863
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 64 deletions.
12 changes: 3 additions & 9 deletions src/lib/server/shop/getTickets.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -10,6 +9,7 @@ import {
removeMockTickets,
removeMockUsers,
} from "./mock";
import apiNames from "$lib/utils/apiNames";
const prisma = new PrismaClient();
const SUITE_PREFIX = "getTickets";

Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down
68 changes: 21 additions & 47 deletions src/lib/server/shop/getTickets.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -13,6 +13,7 @@ import {
type DBShopIdentification,
type ShopIdentification,
} from "./types";
import dayjs from "dayjs";

export type TicketWithMoreInfo = Ticket &
Shoppable & {
Expand All @@ -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<typeof ticketIncludedFields>;
Expand Down Expand Up @@ -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<TicketWithMoreInfo[]> => {
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
Expand Down
9 changes: 3 additions & 6 deletions src/lib/server/shop/payments/purchase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
it,
vi,
} from "vitest";
import { getAccessPolicies } from "../../../../hooks.server.helpers";
import {
MOCK_ACTIVE_TICKET,
MOCK_ACTIVE_TICKET_2,
Expand All @@ -35,6 +34,7 @@ import {
dbIdentification,
type ShopIdentification,
} from "../types";
import apiNames from "$lib/utils/apiNames";

const mockFns = vi.hoisted(() => ({
customers: {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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, {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/server/shop/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export type TransactionClient = Parameters<
export type ShopIdentification =
| {
memberId: string;
externalCode?: undefined;
externalCode?: never;
}
| {
memberId?: undefined;
memberId?: never;
externalCode: string;
};

Expand Down

0 comments on commit 2dde863

Please sign in to comment.