Skip to content

Commit

Permalink
msw: Implement PUT /api/v1/me/crate_owner_invitations/accept/:token
Browse files Browse the repository at this point in the history
… request handler
  • Loading branch information
Turbo87 committed Jan 22, 2025
1 parent b75b6b6 commit 117e636
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/crates-io-msw/handlers/invites.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import legacyListInvites from './invites/legacy-list.js';
import listInvites from './invites/list.js';
import redeemByCrateId from './invites/redeem-by-crate-id.js';
import redeemByToken from './invites/redeem-by-token.js';

export default [listInvites, legacyListInvites, redeemByCrateId];
export default [listInvites, legacyListInvites, redeemByCrateId, redeemByToken];
16 changes: 16 additions & 0 deletions packages/crates-io-msw/handlers/invites/redeem-by-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { http, HttpResponse } from 'msw';

import { db } from '../../index.js';
import { notFound } from '../../utils/handlers.js';

export default http.put('/api/v1/me/crate_owner_invitations/accept/:token', async ({ params }) => {
let { token } = params;

let invite = db.crateOwnerInvitation.findFirst({ where: { token: { equals: token } } });
if (!invite) return notFound();

db.crateOwnership.create({ crate: invite.crate, user: invite.invitee });
db.crateOwnerInvitation.delete({ where: { id: invite.id } });

return HttpResponse.json({ crate_owner_invitation: { crate_id: invite.crate.id, accepted: true } });
});
36 changes: 36 additions & 0 deletions packages/crates-io-msw/handlers/invites/redeem-by-token.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { assert, test } from 'vitest';

import { db } from '../../index.js';

test('can accept an invitation', async function () {
let serde = db.crate.create({ name: 'serde' });
db.version.create({ crate: serde });

let inviter = db.user.create();
let invitee = db.user.create();
db.mswSession.create({ user: invitee });

let invite = db.crateOwnerInvitation.create({ crate: serde, invitee, inviter });

let response = await fetch(`/api/v1/me/crate_owner_invitations/accept/${invite.token}`, { method: 'PUT' });
assert.strictEqual(response.status, 200);
assert.deepEqual(await response.json(), {
crate_owner_invitation: {
accepted: true,
crate_id: serde.id,
},
});

let invites = db.crateOwnerInvitation.findMany({ where: { crate: { id: serde.id }, invitee: { id: invitee.id } } });
assert.strictEqual(invites.length, 0);
let owners = db.crateOwnership.findMany({ where: { crate: { id: serde.id }, user: { id: invitee.id } } });
assert.strictEqual(owners.length, 1);
});

test('returns 404 if invite does not exist', async function () {
let user = db.user.create();
db.mswSession.create({ user });

let response = await fetch('/api/v1/me/crate_owner_invitations/accept/secret-token', { method: 'PUT' });
assert.strictEqual(response.status, 404);
});

0 comments on commit 117e636

Please sign in to comment.