-
Notifications
You must be signed in to change notification settings - Fork 616
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
msw: Implement
PUT /api/v1/me/crate_owner_invitations/accept/:token
…
… request handler
- Loading branch information
Showing
3 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
packages/crates-io-msw/handlers/invites/redeem-by-token.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
packages/crates-io-msw/handlers/invites/redeem-by-token.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |