Skip to content

Commit

Permalink
twilio tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jho44 committed Aug 3, 2023
1 parent 0540b7c commit 83a7b0d
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 37 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# playdate
[![Run Tests](https://github.com/lilith/playdate/actions/workflows/test.yml/badge.svg)](https://github.com/lilith/playdate/actions/workflows/test.yml)

[![Run Tests](https://github.com/lilith/playdate/actions/workflows/test.yml/badge.svg)](https://github.com/lilith/playdate/actions/workflows/test.yml)

_get texts when your kid's friends are free. broadcast texts to their parents when you're free. save time automatically. schedule easier. build friendships. make memories._

[playdate.help](https://playdate.help) saves time and helps build friendships by coordinating tentative availability between parents for playdates. It's open-source (AGPLv3) and non-commercial.


The [design document draft is here](https://docs.google.com/document/d/18AJJTOX9x-pXl4mSTfKHp_9Op4cszZLhZkb9UiQZbNA/edit?usp=sharing) (comment and edit!)

ideal tech stack: SvelteKit, Twilio, PostgreSQL, Vercel (If too slow, DigitalOcean/Heroku/app platform).
Expand Down
2 changes: 1 addition & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const config: PlaywrightTestConfig = {
expect: {
timeout: 3000
},
timeout: 20000,
timeout: 5000,
testDir: 'tests'
};

Expand Down
78 changes: 77 additions & 1 deletion prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ async function main() {
'+12015550122',
'+12015550123',
'+12015550124',
'+12015550125'
'+12015550125',
'+12015550126'
];
const now = new Date();
const expires = new Date();
Expand Down Expand Up @@ -48,6 +49,35 @@ async function main() {
};
}

function 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']
}
}
]
}
}
}
}
};
}

function basicUser(id: number) {
return {
firstName: `User ${id}`,
Expand All @@ -60,6 +90,8 @@ async function main() {
};
}

await prisma.friendRequest.deleteMany();

// User 1
await prisma.user.upsert({
where: {
Expand Down Expand Up @@ -207,6 +239,50 @@ async function main() {
update: householdInvite,
create: householdInvite
});

// User 6
const user6 = {
...basicUser(6),
...householdWithKid(6, 1)
};

await prisma.user.upsert({
where: {
phone: phones[5]
},
update: user6,
create: {
...user6,
...permsYes(phones[5])
}
});

const kid1 = {
householdId: 6,
firstName: 'User 6 Kid 1',
pronouns: Pronoun['HE_HIM_HIS']
};
await prisma.householdChild.upsert({
where: {
id: 1
},
update: kid1,
create: kid1
});

const user6session = 'user6session';
const session6 = {
token: user6session,
phone: phones[5],
expires
};
await prisma.session.upsert({
where: {
token: user6session
},
update: session6,
create: session6
});
}

export async function run() {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/server/twilio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const msgToSend = async (
}
if (!recipient.householdId) {
throw error(401, {
message: "Can't notify someone who's not in your circle"
message: "Can't notify someone who doesn't have a household of sched updates"
});
}

Expand Down Expand Up @@ -149,9 +149,11 @@ export const sendMsg = async (request: Request, initiator: User | null) => {
}
}

const body = await msgToSend(type, recipient, initiator, { ...rest, phone });

try {
createMessageRequest = {
body: await msgToSend(type, recipient, initiator, { ...rest, phone }),
body,
to: phone,
...(sendAt
? {
Expand Down
31 changes: 0 additions & 31 deletions tests/db.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,6 @@ const host = 'http://localhost:5173';

test.beforeEach(async ({ page }) => {
await run();
// await page.goto('http://localhost:5173');

// await page.waitForTimeout(3000);
// await page.getByRole('textbox').fill('2015550127');
// await page.getByRole('button').click();

// let token: string, phone: string;
// page.on('dialog', async (dialog) => {
// const thing = dialog.message().split(' ');
// phone = thing[0];
// token = thing[1];
// dialog.accept();
// });
// page.on('console', async (msg) => {
// const first = await msg.args()[0]?.jsonValue();
// if (first === 'PHONE_TOKEN') {
// phone = await msg.args()[1].jsonValue();
// token = await msg.args()[2].jsonValue();
// }
// });
// await new Promise<void>((resolve) => {
// let intervalId = setInterval(() => {
// if (phone && token) {
// clearInterval(intervalId);
// resolve();
// }
// }, 100);
// });
// await page.goto(`http://localhost:5173/login/${phone}/${token}`);
// await page.mainFrame().waitForLoadState();
// await expect(page).toHaveURL(host + '/profile');
});

test("User can't save profile without session cookie", async ({ page, context }) => {
Expand Down
137 changes: 137 additions & 0 deletions tests/twilio.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { test, expect } from '@playwright/test';
import { run } from '../prisma/seed';

const host = 'http://localhost:5173';

test.beforeEach(async ({ page }) => {
await run();
});

/*
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 }'
}
});

// should redirect user to home page for logging in
expect(res.url()).toEqual(host + '/');
await page.close();
});

test("User 2 can't send circleNotif msg (no household)", async ({ page, context }) => {
context.addCookies([
{
name: 'session',
value: 'user2session',
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 }'
}
});
// endpoint itself has a check for this but gonna get stopped by hooks routing even before raeching that point
expect(res.status()).not.toEqual(200);
await page.close();
});

test("User 6 can't send circleNotif msg to nonexistent user", async ({ page, context }) => {
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 }'
}
});

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 6 can't send circleNotif msg to user w/ no household", async ({ page, context }) => {
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 }'
}
});

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 6 can't send circleNotif msg to user outside of circle", async ({ page, context }) => {
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 }'
}
});

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();
});

0 comments on commit 83a7b0d

Please sign in to comment.