Skip to content

Commit

Permalink
mirage: Implement generic `PUT /me/crate_owner_invitations/accept/:to…
Browse files Browse the repository at this point in the history
…ken` route handler
  • Loading branch information
Turbo87 committed Jun 16, 2021
1 parent e806318 commit 9ab26af
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
1 change: 1 addition & 0 deletions mirage/factories/crate-owner-invitation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions mirage/route-handlers/me.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } };
});
}
1 change: 1 addition & 0 deletions mirage/serializers/crate-owner-invitation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 7 additions & 15 deletions tests/acceptance/token-invites-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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.');
Expand All @@ -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(
Expand Down

0 comments on commit 9ab26af

Please sign in to comment.