diff --git a/playwright.config.ts b/playwright.config.ts index 4f60067..aab6177 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -16,13 +16,13 @@ const config: PlaywrightTestConfig = { expect: { timeout: 3000 }, - timeout: 10000, + // timeout: 10000, testDir: 'tests', testMatch:'*spec.ts', use: { locale: 'en-US', timezoneId: 'America/Los_Angeles' - } + }, }; export default config; diff --git a/prisma/seed.ts b/prisma/seed.ts index 92c935c..96fa905 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -5,25 +5,26 @@ import SeedUtils from './utils'; const prisma = new PrismaClient(); async function main() { - const utils = new SeedUtils(new Date(), prisma); + console.log("SEEDING") + // const utils = new SeedUtils(new Date(), prisma); - await utils.deleteAllHouseholds(); + // await utils.deleteAllHouseholds(); - await Promise.all([ - utils.deleteAllFriendRequests(), - utils.createExpiredLink(1), - ...USERS_WITH_NOTHING.map((userInd) => utils.createUserWithNothing(userInd)), - ...USERS_WITH_EMPTY_HOUSEHOLD.map((userInd) => utils.createUserWithEmptyHousehold(userInd)), - ...USERS_WITH_ACTIVE_SESSION.map((userInd) => utils.createActiveSession(userInd)), - utils.createUserWithEmptyHousehold(5), - utils.createUserWithKid(6), - ]); + // await Promise.all([ + // utils.deleteAllFriendRequests(), + // utils.createExpiredLink(1), + // ...USERS_WITH_NOTHING.map((userInd) => utils.createUserWithNothing(userInd)), + // ...USERS_WITH_EMPTY_HOUSEHOLD.map((userInd) => utils.createUserWithEmptyHousehold(userInd)), + // ...USERS_WITH_ACTIVE_SESSION.map((userInd) => utils.createActiveSession(userInd)), + // utils.createUserWithEmptyHousehold(5), + // utils.createUserWithKid(6), + // ]); - await Promise.all([ - utils.createFriendRequest(4, 3), - utils.createHouseholdConnection(3, 5), - utils.createHouseholdInvite(5, 2) - ]); + // await Promise.all([ + // utils.createFriendRequest(4, 3), + // utils.createHouseholdConnection(3, 5), + // utils.createHouseholdInvite(5, 2) + // ]); } export async function run() { diff --git a/prisma/utils.ts b/prisma/utils.ts index a5c6d99..8d3591e 100644 --- a/prisma/utils.ts +++ b/prisma/utils.ts @@ -3,15 +3,6 @@ import { Prisma, PrismaClient, Pronoun } from '@prisma/client'; export default class SeedUtils { #now: Date; #prisma: PrismaClient; - PHONES = [ - '+12015550121', - '+12015550122', - '+12015550123', - '+12015550124', - '+12015550125', - '+12015550126', - '+12015550127' - ]; constructor(now: Date, prisma: PrismaClient) { this.#now = now; @@ -40,15 +31,8 @@ export default class SeedUtils { emptyHousehold(ind: number) { return { household: { - connectOrCreate: { - where: { - id: ind - }, - create: { - // phone: phones[ind - 1], - id: ind, - name: `Household ${ind}` - } + create: { + name: `Household ${ind}` } } }; @@ -57,25 +41,13 @@ export default class SeedUtils { householdWithKid(ind: number, kidInd: number) { return { household: { - connectOrCreate: { - where: { - id: ind - }, - create: { - id: ind, - name: `Household ${ind}`, - children: { - connectOrCreate: [ - { - where: { - id: kidInd - }, - create: { - firstName: `User ${ind} Kid ${kidInd}`, - pronouns: Pronoun['HE_HIM_HIS'] - } - } - ] + create: { + id: ind, + name: `Household ${ind}`, + children: { + create: { + firstName: `User ${ind} Kid ${kidInd}`, + pronouns: Pronoun['HE_HIM_HIS'] } } } @@ -95,26 +67,18 @@ export default class SeedUtils { }; } - async deleteAllFriendRequests() { - await this.#prisma.friendRequest - .deleteMany() - .catch(() => console.log('No friend request table to delete')); - } - - async deleteAllHouseholds() { - await this.#prisma.householdChild - .deleteMany() - .catch(() => console.log('No household child table to delete')); - await this.#prisma.household - .deleteMany() - .catch(() => console.log('No household table to delete')); - } - async deleteUserAndHousehold(phone: string) { const user = await this.#prisma.user.findUnique({ where: { phone } }); - if (!user || !user.householdId) return; + if (!user) return; + + if (!user.householdId) { + await this.#prisma.user.delete({ + where: { phone } + }); + return; + } const { householdId } = user; // delete all kids const deleteKids = this.#prisma.householdChild.deleteMany({ @@ -167,47 +131,55 @@ export default class SeedUtils { } }); - // delete all adults - const deleteAdults = this.#prisma.user.deleteMany({ - where: { - householdId - } - }); - - // finally, delete the household - const deleteHousehold = this.#prisma.household.delete({ - where: { id: householdId } - }); - - await this.#prisma.$transaction([ - deleteKids, - deleteHouseholdInvites, - deleteFriendReqs1, - deleteFriendReqs2, - deleteFriends1, - deleteFriends2, - deleteAdults, - deleteHousehold - ]); + try { + await this.#prisma.$transaction([ + deleteKids, + deleteHouseholdInvites, + deleteFriendReqs1, + deleteFriendReqs2, + deleteFriends1, + deleteFriends2 + ]); + // delete all adults + await this.#prisma.user.deleteMany({ + where: { + householdId + } + }); + + // finally, delete the household + await this.#prisma.household.delete({ + where: { id: householdId } + }); + } catch (err) { + console.error(err); + console.error('Failed to delete user and household for:', { phone }); + console.error('Household Invites', await this.#prisma.joinHouseholdRequest.findMany()); + throw new Error("Couldn't delete user and household"); + } } async createExpiredLink(userInd: number) { - const expiredLink = { - token: '3e99472f1003794c', - phone: this.PHONES[userInd - 1], - expires: new Date('8/5/2020') - }; - await this.#prisma.magicLink.upsert({ - where: { - id: userInd - }, - update: expiredLink, - create: expiredLink + let crypto; + try { + crypto = await import('node:crypto'); + } catch (err) { + console.error('crypto support is disabled!'); + return null; + } + const token = crypto.randomBytes(8).toString('hex'); + await this.#prisma.magicLink.create({ + data: { + token, + phone: this.userIndToPhone(userInd), + expires: new Date('8/5/2020') + } }); + return token; } async createUserWithNothing(userInd: number) { - const phone = this.PHONES[userInd - 1]; + const phone = this.userIndToPhone(userInd); await this.#prisma.user.upsert({ where: { @@ -222,7 +194,7 @@ export default class SeedUtils { } async createActiveSession(userInd: number) { - const phone = this.PHONES[userInd - 1]; + const phone = this.userIndToPhone(userInd); const expires = new Date(this.#now); expires.setHours(expires.getHours() + 1); @@ -243,7 +215,7 @@ export default class SeedUtils { } async createUserWithEmptyHousehold(userInd: number) { - const phone = this.PHONES[userInd - 1]; + const phone = this.userIndToPhone(userInd); const user = { ...this.basicUser(userInd), ...this.emptyHousehold(userInd) @@ -261,54 +233,13 @@ export default class SeedUtils { }); } - async createFriendRequest(fromUserInd: number, toUserInd: number) { - const friendReq = { - id: toUserInd, - targetPhone: this.PHONES[toUserInd - 1], - fromHouseholdId: fromUserInd, - fromUserId: fromUserInd - }; - - await this.#prisma.friendRequest.upsert({ - where: { - id: toUserInd - }, - update: friendReq, - create: friendReq - }); - } - - async createHouseholdConnection(hId1: number, hId2: number) { - const householdConnection = { - id: hId1, - householdId: hId1, - friendHouseholdId: hId2 - }; - await this.#prisma.householdConnection.upsert({ - where: { - id: hId1 - }, - update: householdConnection, - create: householdConnection - }); - } - - async createHouseholdInvite(fromUserInd: number, toUserInd: number) { - // household invite from User 5 to User 2 - const householdInvite = { - id: toUserInd, - targetPhone: this.PHONES[toUserInd - 1], - householdId: fromUserInd, - fromUserId: fromUserInd - }; - - await this.#prisma.joinHouseholdRequest.upsert({ - where: { - id: toUserInd - }, - update: householdInvite, - create: householdInvite - }); + userIndToPhone(userInd: number) { + const BASE = '+1201555'; + let suffix = `${userInd}`; + while (suffix.length < 4) { + suffix = `0${suffix}`; + } + return `${BASE}${suffix}`; } async createUserWithKid(userInd: number) { @@ -317,7 +248,7 @@ export default class SeedUtils { ...this.householdWithKid(userInd, 1) }; - const phone = this.PHONES[userInd - 1]; + const phone = this.userIndToPhone(userInd); await this.#prisma.user.upsert({ where: { @@ -330,10 +261,4 @@ export default class SeedUtils { } }); } - - async deleteUsers(where: Prisma.UserWhereUniqueInput) { - await this.#prisma.user.deleteMany({ - where - }); - } } diff --git a/src/lib/server/repository/Household.ts b/src/lib/server/repository/Household.ts index d2e5220..a6a40fa 100644 --- a/src/lib/server/repository/Household.ts +++ b/src/lib/server/repository/Household.ts @@ -1,5 +1,5 @@ import prisma from '$lib/logics/_shared/prisma'; -import type * as Prisma from '@prisma/client'; +import type { Prisma } from '@prisma/client'; export default class HouseholdRepository { static async create( diff --git a/src/routes/db/+server.ts b/src/routes/db/+server.ts index da2e72a..3e6fe38 100644 --- a/src/routes/db/+server.ts +++ b/src/routes/db/+server.ts @@ -42,7 +42,7 @@ export async function POST({ } catch (err) { console.error(`${req.type} for user ${user.id} failed`); console.error(err) - throw error(err.status, err.body.message); + throw error(err?.status || 500, err.body?.message || err); } } diff --git a/tests/calendar.spec.ts b/tests/calendar.spec.ts index 19d8415..f33fc48 100644 --- a/tests/calendar.spec.ts +++ b/tests/calendar.spec.ts @@ -15,4 +15,4 @@ create profile create kid save basic info -*/ \ No newline at end of file +*/ diff --git a/tests/db.spec.ts b/tests/db.spec.ts index 1107b8d..9a65b43 100644 --- a/tests/db.spec.ts +++ b/tests/db.spec.ts @@ -1,5 +1,4 @@ import { expect } from '@playwright/test'; -import SeedUtils from '../prisma/utils'; import { test } from './test'; const host = 'http://localhost:5173'; @@ -32,22 +31,20 @@ that info is derived from the session cookie */ test.describe('Household Invites', () => { - test.beforeAll(async ({ prisma }) => { - const utils = new SeedUtils(new Date(), prisma); - + test.beforeAll(async ({ utils }) => { await Promise.all([ utils.deleteUserAndHousehold('+12015550001'), utils.deleteUserAndHousehold('+12015550002') ]); await Promise.all([ - ...[1].map((userInd) => utils.createUserWithNothing(userInd)), - ...[2].map((userInd) => utils.createUserWithEmptyHousehold(userInd)), - ...[1, 2].map((userInd) => utils.createActiveSession(userInd)) + ...[2, 4].map((userInd) => utils.createUserWithNothing(userInd)), + ...[1, 3].map((userInd) => utils.createUserWithEmptyHousehold(userInd)), + ...[1, 3].map((userInd) => utils.createActiveSession(userInd)) ]); }); - test("User 1 fails to accept household invite on User 2's behalf", async ({ page, context }) => { + test("User 1 fails to accept household invite on User 2's behalf", async ({ context }) => { await context.addCookies([ { name: 'session', @@ -62,20 +59,19 @@ test.describe('Household Invites', () => { }, data: { type: 'acceptHouseholdInvite', - id: 2 + id: 0 } }); const { message } = await res.json(); expect(message).toEqual("You can't accept a household invite that wasn't issued to you"); expect(res.status()).toEqual(401); - await page.close(); }); - test("User 1 fails to decline household invite on User 2's behalf", async ({ page, context }) => { + test("User 3 fails to decline household invite on User 4's behalf", async ({ context }) => { await context.addCookies([ { name: 'session', - value: 'user1session', + value: 'user3session', url: host } ]); @@ -86,38 +82,41 @@ test.describe('Household Invites', () => { }, data: { type: 'rejectHouseholdInvite', - id: 2 + id: 0 } }); const { message } = await res.json(); expect(message).toEqual("You can't delete a household invite that wasn't issued to you"); expect(res.status()).toEqual(401); - await page.close(); + }); + + test.afterAll(async ({ browser }) => { + await browser.close(); }); }); test.describe('Friend Requests', () => { - test.beforeAll(async ({ prisma }) => { - const utils = new SeedUtils(new Date(), prisma); - + test.beforeAll(async ({ utils }) => { await Promise.all([ - utils.deleteUserAndHousehold('+12015550003'), - utils.deleteUserAndHousehold('+12015550004') + utils.deleteUserAndHousehold('+12015550005'), + utils.deleteUserAndHousehold('+12015550006'), + utils.deleteUserAndHousehold('+12015550007'), + utils.deleteUserAndHousehold('+12015550008'), + utils.deleteUserAndHousehold('+12015550009'), + utils.deleteUserAndHousehold('+12015550010') ]); await Promise.all([ - ...[3, 4].map((userInd) => utils.createUserWithEmptyHousehold(userInd)), - ...[4].map((userInd) => utils.createActiveSession(userInd)) + ...[5, 6, 7, 8, 9, 10].map((userInd) => utils.createUserWithEmptyHousehold(userInd)), + ...[5, 7, 9].map((userInd) => utils.createActiveSession(userInd)) ]); - - await Promise.all([utils.createFriendRequest(4, 3)]); }); - test("User 4 fails to accept friend request on User 3's behalf", async ({ page, context }) => { + test("User 5 fails to accept friend request on User 6's behalf", async ({ context }) => { await context.addCookies([ { name: 'session', - value: 'user4session', + value: 'user5session', url: host } ]); @@ -128,20 +127,19 @@ test.describe('Friend Requests', () => { }, data: { type: 'acceptFriendReq', - friendReqId: 3 + friendReqId: 0 } }); const { message } = await res.json(); expect(message).toEqual('No friend request with that id issued to you'); expect(res.status()).toEqual(401); - await page.close(); }); - test("User 4 fails to decline friend request on User 3's behalf", async ({ page, context }) => { + test("User 7 fails to decline friend request on User 8's behalf", async ({ context }) => { await context.addCookies([ { name: 'session', - value: 'user4session', + value: 'user7session', url: host } ]); @@ -152,20 +150,19 @@ test.describe('Friend Requests', () => { }, data: { type: 'rejectFriendReq', - reqId: 3 + reqId: 0 } }); const { message } = await res.json(); expect(message).toEqual("Can't delete friend request not issued to you"); expect(res.status()).toEqual(401); - await page.close(); }); - test("User 4 fails to delete friend on User 3's behalf", async ({ page, context }) => { + test("User 9 fails to delete friend on User 10's behalf", async ({ context }) => { await context.addCookies([ { name: 'session', - value: 'user4session', + value: 'user9session', url: host } ]); @@ -176,13 +173,20 @@ test.describe('Friend Requests', () => { }, data: { type: 'deleteFriend', - connectionId: 3 + connectionId: 0 } }); const { message } = await res.json(); expect(message).toEqual("You can't delete a household connection that you're not a part of"); expect(res.status()).toEqual(401); - await page.close(); + }); + + test.afterAll(async ({ browser }) => { + await browser.close(); }); }); // TODO: Can't create household for someone who's already in a household + +test.afterAll(async ({ browser }) => { + await browser.close(); +}); \ No newline at end of file diff --git a/tests/household.spec.ts b/tests/household.spec.ts index 14a5f68..355d52a 100644 --- a/tests/household.spec.ts +++ b/tests/household.spec.ts @@ -1,30 +1,41 @@ -import { expect, test } from '@playwright/test'; -import { run } from '../prisma/seed'; +import { expect } from '@playwright/test'; +import { test } from './test'; const host = 'http://localhost:5173'; -test.beforeAll(async () => { - await run(); +test.beforeAll(async ({ utils }) => { + await Promise.all([utils.deleteUserAndHousehold('+12015550011')]); + + await Promise.all([ + ...[11].map((userInd) => utils.createUserWithNothing(userInd)), + ...[11].map((userInd) => utils.createActiveSession(userInd)) + ]); }); test('User can create new kid', async ({ page, context }) => { - await context.addCookies([ + await context.addCookies([ { name: 'session', - value: 'user7session', + value: 'user11session', url: host } ]); await page.goto(host); await page.waitForURL(`${host}/household`); - await page.waitForTimeout(2000) - expect(await page.locator('.kid-card').count()).toBe(0) + await page.waitForTimeout(2000); + expect(await page.locator('.kid-card').count()).toBe(0); - await page.locator('input[name="first-name"]').fill('FIRST_NAME') - await page.locator('input[name="last-name"]').fill('LAST_NAME') + await page.locator('input[name="first-name"]').fill('FIRST_NAME'); + await page.locator('input[name="last-name"]').fill('LAST_NAME'); await page.locator('select[name="pronouns"]').selectOption('SHE_HER_HERS'); - await page.getByText('Save Child').click(); - await page.locator('.kid-card').first().waitFor(); - expect(await page.locator('.kid-card').count()).toBe(1) + await page.getByText('Save Child').click(); + await page.screenshot({ path: 'screenshot.png', fullPage: true }); + await page.locator('.kid-card').first().waitFor(); + expect(await page.locator('.kid-card').count()).toBe(1); + await page.close(); +}); + +test.afterAll(async ({ browser }) => { + await browser.close(); }); diff --git a/tests/login.spec.ts b/tests/login.spec.ts index 7e00352..4e594f2 100644 --- a/tests/login.spec.ts +++ b/tests/login.spec.ts @@ -1,15 +1,23 @@ -import { test, expect } from '@playwright/test'; -import { run } from '../prisma/seed'; +import { expect } from '@playwright/test'; +import { test } from './test'; const host = 'http://localhost:5173'; -test.beforeEach(async () => { - await run(); +let token: string | null; +test.beforeEach(async ({ utils }) => { + [token] = await Promise.all([utils.createExpiredLink(18), utils.createUserWithNothing(18)]); }); test('Redirect to login page w/ prefilled phone num on expired magic link', async ({ page }) => { - await page.goto('http://localhost:5173/login/3e99472f1003794c'); + test.skip(!token, "Couldn't generate expired link"); - await page.waitForURL(`${host}?phone=+12015550121&status=403`, { waitUntil: 'networkidle' }); - await expect(page).toHaveURL(`${host}?phone=+12015550121&status=403`); + await page.goto(`http://localhost:5173/login/${token}`); + + await page.waitForURL(`${host}?phone=+12015550018&status=403`, { waitUntil: 'networkidle' }); + await expect(page).toHaveURL(`${host}?phone=+12015550018&status=403`); + await page.close(); +}); + +test.afterAll(async ({ browser }) => { + await browser.close(); }); diff --git a/tests/profile.spec.ts b/tests/profile.spec.ts index 1801b1b..b86b393 100644 --- a/tests/profile.spec.ts +++ b/tests/profile.spec.ts @@ -1,15 +1,11 @@ -import { test, type ConsoleMessage } from '@playwright/test'; -import { PrismaClient } from '@prisma/client'; -import SeedUtils from '../prisma/utils'; +import type { ConsoleMessage } from '@playwright/test'; +import { test } from './test'; const host = 'http://localhost:5173'; -const phone = '+12016660127'; +const phone = '+12015550019'; -test.beforeEach(async () => { - const prisma = new PrismaClient(); - const utils = new SeedUtils(new Date(), prisma); - await utils.deleteUsers({ phone }); - await prisma.$disconnect(); +test.beforeEach(async ({ utils }) => { + await utils.deleteUserAndHousehold(phone); }); test('User can create new profile with valid phone number', async ({ page }) => { @@ -52,4 +48,9 @@ test('User can create new profile with valid phone number', async ({ page }) => await page.getByText('Save').click(); await page.waitForURL(`${host}/household`); + await page.close(); +}); + +test.afterAll(async ({ browser }) => { + await browser.close(); }); diff --git a/tests/test.ts b/tests/test.ts index 04dcf80..7037e1c 100644 --- a/tests/test.ts +++ b/tests/test.ts @@ -1,8 +1,9 @@ import { test as base } from '@playwright/test'; import { PrismaClient } from '@prisma/client'; +import SeedUtils from '../prisma/utils'; // Note that we pass worker fixture types as a second template parameter. -export const test = base.extend<{}, { prisma: PrismaClient }>({ +export const test = base.extend<{}, { prisma: PrismaClient; utils: SeedUtils }>({ prisma: [ async ({}, use) => { const prisma = new PrismaClient(); @@ -10,4 +11,10 @@ export const test = base.extend<{}, { prisma: PrismaClient }>({ }, { scope: 'worker' } ], + utils: [ + async ({ prisma }, use) => { + await use(new SeedUtils(new Date(), prisma)); + }, + { scope: 'worker' } + ] }); diff --git a/tests/twilio.spec.ts b/tests/twilio.spec.ts index cbfb704..2f536cf 100644 --- a/tests/twilio.spec.ts +++ b/tests/twilio.spec.ts @@ -1,143 +1,157 @@ -import { test, expect } from '@playwright/test'; -import { run } from '../prisma/seed'; +import { expect } from '@playwright/test'; +import { test } from './test'; const host = 'http://localhost:5173'; -test.beforeEach(async () => { - await run(); -}); +test.describe('Twilio', () => { + test.beforeAll(async ({ utils }) => { + await Promise.all([ + utils.deleteUserAndHousehold('+12015550012'), + utils.deleteUserAndHousehold('+12015550013'), + utils.deleteUserAndHousehold('+12015550014'), + utils.deleteUserAndHousehold('+12015550015'), + utils.deleteUserAndHousehold('+12015550016'), + utils.deleteUserAndHousehold('+12015550017') + ]); + + await Promise.all([ + ...[12, 15].map((userInd) => utils.createUserWithNothing(userInd)), + ...[13, 14, 16, 17].map((userInd) => utils.createUserWithKid(userInd)), + ...[12, 13, 14, 16].map((userInd) => utils.createActiveSession(userInd)) + ]); + }); -/* + /* Skipped tests b/c the generated SMSes are only ever sent back to the user (low / no-auth) - login link - thanks msg - tip msg */ -test("User can't send circleNotif msg without session cookie", async ({ page, context }) => { - const res = await context.request.fetch(host + '/twilio', { - method: 'post', - headers: { - 'Content-Type': 'application/json' - }, - data: { - phone: '+1xxxxxxxxxx', - type: 'circleNotif', - schedDiffs: '{ some: schedDiff }', - diff: true - } - }); + test("User can't send circleNotif msg without session cookie", async ({ context }) => { + const res = await context.request.fetch(host + '/twilio', { + method: 'post', + headers: { + 'Content-Type': 'application/json' + }, + data: { + phone: '+1xxxxxxxxxx', + type: 'circleNotif', + schedDiffs: '{ some: schedDiff }', + diff: true + } + }); - // should redirect user to home page for logging in - expect(res.url()).toEqual(host + '/'); - await page.close(); -}); + // should redirect user to home page for logging in + expect(res.url()).toEqual(host + '/'); + }); -test("User 2 can't send circleNotif msg (no household)", async ({ page, context }) => { - await context.addCookies([ - { - name: 'session', - value: 'user2session', - url: host - } - ]); - const res = await context.request.fetch(host + '/twilio', { - method: 'post', - headers: { - 'Content-Type': 'application/json', - 'Accept': 'application/json' - }, - data: { - phone: '+1xxxxxxxxxx', - type: 'circleNotif', - schedDiffs: '{ some: schedDiff }', - diff: true - } + test("User 12 can't send circleNotif msg (no household)", async ({ context }) => { + await context.addCookies([ + { + name: 'session', + value: 'user12session', + url: host + } + ]); + const res = await context.request.fetch(host + '/twilio', { + method: 'post', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json' + }, + data: { + phone: '+1xxxxxxxxxx', + type: 'circleNotif', + schedDiffs: '{ some: schedDiff }', + diff: true + } + }); + // endpoint itself has a check for this but gonna get stopped by hooks routing even before reaching that point + expect(res.status()).toEqual(403); }); - // endpoint itself has a check for this but gonna get stopped by hooks routing even before reaching that point - expect(res.status()).toEqual(403); - await page.close(); -}); -test("User 6 can't send circleNotif msg to nonexistent user", async ({ page, context }) => { - await context.addCookies([ - { - name: 'session', - value: 'user6session', - url: host - } - ]); - const res = await context.request.fetch(host + '/twilio', { - method: 'post', - headers: { - 'Content-Type': 'application/json' - }, - data: { - phone: '+1xxxxxxxxxx', - type: 'circleNotif', - schedDiffs: '{ some: schedDiff }', - diff: true - } + test("User 13 can't send circleNotif msg to nonexistent user", async ({ context }) => { + await context.addCookies([ + { + name: 'session', + value: 'user13session', + url: host + } + ]); + const res = await context.request.fetch(host + '/twilio', { + method: 'post', + headers: { + 'Content-Type': 'application/json' + }, + data: { + phone: '+1xxxxxxxxxx', + type: 'circleNotif', + schedDiffs: '{ some: schedDiff }', + diff: true + } + }); + + const { message } = await res.json(); + expect(message).toEqual('Recipient must be one of our users'); + expect(res.status()).toEqual(400); }); - const { message } = await res.json(); - expect(message).toEqual('Recipient must be one of our users'); - expect(res.status()).toEqual(400); - await page.close(); -}); + test("User 14 can't send circleNotif msg to user w/ no household", async ({ context }) => { + await context.addCookies([ + { + name: 'session', + value: 'user14session', + url: host + } + ]); + const res = await context.request.fetch(host + '/twilio', { + method: 'post', + headers: { + 'Content-Type': 'application/json' + }, + data: { + phone: '+12015550015', + type: 'circleNotif', + schedDiffs: '{ some: schedDiff }', + diff: true + } + }); -test("User 6 can't send circleNotif msg to user w/ no household", async ({ page, context }) => { - await context.addCookies([ - { - name: 'session', - value: 'user6session', - url: host - } - ]); - const res = await context.request.fetch(host + '/twilio', { - method: 'post', - headers: { - 'Content-Type': 'application/json' - }, - data: { - phone: '+12015550121', - type: 'circleNotif', - schedDiffs: '{ some: schedDiff }', - diff: true - } + const { message } = await res.json(); + expect(message).toEqual("Can't notify someone who doesn't have a household of sched updates"); + expect(res.status()).toEqual(401); }); - const { message } = await res.json(); - expect(message).toEqual("Can't notify someone who doesn't have a household of sched updates"); - expect(res.status()).toEqual(401); - await page.close(); -}); + test("User 16 can't send circleNotif msg to user outside of circle", async ({ context }) => { + await context.addCookies([ + { + name: 'session', + value: 'user16session', + url: host + } + ]); + const res = await context.request.fetch(host + '/twilio', { + method: 'post', + headers: { + 'Content-Type': 'application/json' + }, + data: { + phone: '+12015550017', + type: 'circleNotif', + schedDiffs: '{ some: schedDiff }', + diff: true + } + }); -test("User 6 can't send circleNotif msg to user outside of circle", async ({ page, context }) => { - await context.addCookies([ - { - name: 'session', - value: 'user6session', - url: host - } - ]); - const res = await context.request.fetch(host + '/twilio', { - method: 'post', - headers: { - 'Content-Type': 'application/json' - }, - data: { - phone: '+12015550123', - type: 'circleNotif', - schedDiffs: '{ some: schedDiff }', - diff: true - } + const { message } = await res.json(); + expect(message).toEqual( + 'You must be friends with a user before notifying them of your updated schedule' + ); + expect(res.status()).toEqual(401); }); - const { message } = await res.json(); - expect(message).toEqual( - 'You must be friends with a user before notifying them of your updated schedule' - ); - expect(res.status()).toEqual(401); - await page.close(); + test.afterAll(async ({ browser }) => { + await browser.close(); + }); });