Skip to content

Commit

Permalink
mirage: Implement generic PUT /me/crate_owner_invitations/:crate_id
Browse files Browse the repository at this point in the history
… route handler
  • Loading branch information
Turbo87 committed Jun 15, 2021
1 parent 9e33e58 commit 6996524
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
23 changes: 23 additions & 0 deletions mirage/route-handlers/me.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,27 @@ export function register(server) {

return schema.crateOwnerInvitations.where({ inviteeId: user.id });
});

server.put('/api/v1/me/crate_owner_invitations/:crate_id', (schema, request) => {
let { user } = getSession(schema);
if (!user) {
return new Response(403, {}, { errors: [{ detail: 'must be logged in to perform that action' }] });
}

let body = JSON.parse(request.requestBody);
let { accepted, crate_id: crateId } = body.crate_owner_invite;

let invite = schema.crateOwnerInvitations.findBy({ crateId, inviteeId: user.id });
if (!invite) {
return new Response(404);
}

if (accepted) {
server.create('crate-ownership', { crate: invite.crate, user });
}

invite.destroy();

return { crate_owner_invitation: { crate_id: crateId, accepted } };
});
}
40 changes: 15 additions & 25 deletions tests/acceptance/invites-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module('Acceptance | /me/pending-invites', function (hooks) {

context.authenticateAs(user);

return { nanomsg };
return { nanomsg, user };
}

test('redirects to / when not logged in', async function (assert) {
Expand Down Expand Up @@ -81,19 +81,11 @@ module('Acceptance | /me/pending-invites', function (hooks) {
});

test('invites can be declined', async function (assert) {
assert.expect(9);
let { nanomsg, user } = prepare(this);

let { nanomsg } = prepare(this);

this.server.put('/api/v1/me/crate_owner_invitations/:crate_id', (schema, request) => {
assert.deepEqual(request.params, { crate_id: nanomsg.id });

let body = JSON.parse(request.requestBody);
assert.strictEqual(body.crate_owner_invite.accepted, false);
assert.strictEqual(body.crate_owner_invite.crate_id, nanomsg.id);

return { crate_owner_invitation: { crate_id: 42, accepted: false } };
});
let { crateOwnerInvitations, crateOwnerships } = this.server.schema;
assert.equal(crateOwnerInvitations.where({ crateId: nanomsg.id, inviteeId: user.id }).length, 1);
assert.equal(crateOwnerships.where({ crateId: nanomsg.id, userId: user.id }).length, 0);

await visit('/me/pending-invites');
assert.equal(currentURL(), '/me/pending-invites');
Expand All @@ -106,6 +98,9 @@ module('Acceptance | /me/pending-invites', function (hooks) {
.hasText('Declined. You have not been added as an owner of crate nanomsg.');
assert.dom('[data-test-invite="nanomsg"] [data-test-crate-link]').doesNotExist();
assert.dom('[data-test-invite="nanomsg"] [data-test-inviter-link]').doesNotExist();

assert.equal(crateOwnerInvitations.where({ crateId: nanomsg.id, inviteeId: user.id }).length, 0);
assert.equal(crateOwnerships.where({ crateId: nanomsg.id, userId: user.id }).length, 0);
});

test('error message is shown if decline request fails', async function (assert) {
Expand All @@ -123,19 +118,11 @@ module('Acceptance | /me/pending-invites', function (hooks) {
});

test('invites can be accepted', async function (assert) {
assert.expect(9);
let { nanomsg, user } = prepare(this);

let { nanomsg } = prepare(this);

this.server.put('/api/v1/me/crate_owner_invitations/:crate_id', (schema, request) => {
assert.deepEqual(request.params, { crate_id: nanomsg.id });

let body = JSON.parse(request.requestBody);
assert.strictEqual(body.crate_owner_invite.accepted, true);
assert.strictEqual(body.crate_owner_invite.crate_id, nanomsg.id);

return { crate_owner_invitation: { crate_id: 42, accepted: true } };
});
let { crateOwnerInvitations, crateOwnerships } = this.server.schema;
assert.equal(crateOwnerInvitations.where({ crateId: nanomsg.id, inviteeId: user.id }).length, 1);
assert.equal(crateOwnerships.where({ crateId: nanomsg.id, userId: user.id }).length, 0);

await visit('/me/pending-invites');
assert.equal(currentURL(), '/me/pending-invites');
Expand All @@ -150,6 +137,9 @@ module('Acceptance | /me/pending-invites', function (hooks) {
assert.dom('[data-test-invite="nanomsg"] [data-test-inviter-link]').doesNotExist();

await percySnapshot(assert);

assert.equal(crateOwnerInvitations.where({ crateId: nanomsg.id, inviteeId: user.id }).length, 0);
assert.equal(crateOwnerships.where({ crateId: nanomsg.id, userId: user.id }).length, 1);
});

test('error message is shown if accept request fails', async function (assert) {
Expand Down

0 comments on commit 6996524

Please sign in to comment.