From 09e731af58c5e3e35f86e8a4d9636925ec2d979c Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Sat, 12 Jun 2021 21:42:41 +0200 Subject: [PATCH] mirage: Implement generic `PUT /me/crate_owner_invitations/accept/:token` route handler --- mirage/factories/crate-owner-invitation.js | 1 + mirage/route-handlers/me.js | 14 +++++++++++++ mirage/serializers/crate-owner-invitation.js | 1 + tests/acceptance/token-invites-test.js | 22 +++++++------------- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/mirage/factories/crate-owner-invitation.js b/mirage/factories/crate-owner-invitation.js index cfe69687413..cbc4d43ef69 100644 --- a/mirage/factories/crate-owner-invitation.js +++ b/mirage/factories/crate-owner-invitation.js @@ -2,6 +2,7 @@ import { Factory } from 'ember-cli-mirage'; export default Factory.extend({ createdAt: '2016-12-24T12:34:56Z', + token: i => `secret-token-${i}`, afterCreate(invite) { if (!invite.crateId) { diff --git a/mirage/route-handlers/me.js b/mirage/route-handlers/me.js index ba58b138635..b5dde2731ee 100644 --- a/mirage/route-handlers/me.js +++ b/mirage/route-handlers/me.js @@ -130,4 +130,18 @@ export function register(server) { return { crate_owner_invitation: { crate_id: crateId, accepted } }; }); + + server.put('/api/v1/me/crate_owner_invitations/accept/:token', (schema, request) => { + let { token } = request.params; + + let invite = schema.crateOwnerInvitations.findBy({ token }); + if (!invite) { + return new Response(404); + } + + server.create('crate-ownership', { crate: invite.crate, user: invite.invitee }); + invite.destroy(); + + return { crate_owner_invitation: { crate_id: invite.crateId, accepted: true } }; + }); } diff --git a/mirage/serializers/crate-owner-invitation.js b/mirage/serializers/crate-owner-invitation.js index 97b5d92e680..61c13dd4d09 100644 --- a/mirage/serializers/crate-owner-invitation.js +++ b/mirage/serializers/crate-owner-invitation.js @@ -20,6 +20,7 @@ export default BaseSerializer.extend({ _adjust(hash) { delete hash.id; + delete hash.token; let crate = this.schema.crates.find(hash.crate_id); hash.crate_name = crate.name; diff --git a/tests/acceptance/token-invites-test.js b/tests/acceptance/token-invites-test.js index a8eb6f5bc55..448631e89e5 100644 --- a/tests/acceptance/token-invites-test.js +++ b/tests/acceptance/token-invites-test.js @@ -2,7 +2,6 @@ import { currentURL } from '@ember/test-helpers'; import { module, test } from 'qunit'; import percySnapshot from '@percy/ember'; -import Response from 'ember-cli-mirage/response'; import { setupApplicationTest } from 'cargo/tests/helpers'; @@ -24,13 +23,6 @@ module('Acceptance | /accept-invite/:token', function (hooks) { }); test('shows error for unknown token', async function (assert) { - assert.expect(3); - - this.server.put('/api/v1/me/crate_owner_invitations/accept/:token', (schema, request) => { - assert.deepEqual(request.params, { token: 'unknown' }); - return new Response(404); - }); - await visit('/accept-invite/unknown'); assert.equal(currentURL(), '/accept-invite/unknown'); assert.dom('[data-test-error-message]').hasText('You may want to visit crates.io/me/pending-invites to try again.'); @@ -48,15 +40,15 @@ module('Acceptance | /accept-invite/:token', function (hooks) { }); test('shows success for known token', async function (assert) { - assert.expect(3); + let inviter = this.server.create('user'); + let invitee = this.server.create('user'); - this.server.put('/api/v1/me/crate_owner_invitations/accept/:token', (schema, request) => { - assert.deepEqual(request.params, { token: 'ember-rs' }); - return { crate_owner_invitation: { crate_id: 42, accepted: true } }; - }); + let crate = this.server.create('crate', { name: 'nanomsg' }); + this.server.create('version', { crate }); + let invite = this.server.create('crate-owner-invitation', { crate, invitee, inviter }); - await visit('/accept-invite/ember-rs'); - assert.equal(currentURL(), '/accept-invite/ember-rs'); + await visit(`/accept-invite/${invite.token}`); + assert.equal(currentURL(), `/accept-invite/${invite.token}`); assert .dom('[data-test-success-message]') .hasText(