Skip to content

Commit 6996524

Browse files
committed
mirage: Implement generic PUT /me/crate_owner_invitations/:crate_id route handler
1 parent 9e33e58 commit 6996524

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

mirage/route-handlers/me.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,27 @@ export function register(server) {
107107

108108
return schema.crateOwnerInvitations.where({ inviteeId: user.id });
109109
});
110+
111+
server.put('/api/v1/me/crate_owner_invitations/:crate_id', (schema, request) => {
112+
let { user } = getSession(schema);
113+
if (!user) {
114+
return new Response(403, {}, { errors: [{ detail: 'must be logged in to perform that action' }] });
115+
}
116+
117+
let body = JSON.parse(request.requestBody);
118+
let { accepted, crate_id: crateId } = body.crate_owner_invite;
119+
120+
let invite = schema.crateOwnerInvitations.findBy({ crateId, inviteeId: user.id });
121+
if (!invite) {
122+
return new Response(404);
123+
}
124+
125+
if (accepted) {
126+
server.create('crate-ownership', { crate: invite.crate, user });
127+
}
128+
129+
invite.destroy();
130+
131+
return { crate_owner_invitation: { crate_id: crateId, accepted } };
132+
});
110133
}

tests/acceptance/invites-test.js

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module('Acceptance | /me/pending-invites', function (hooks) {
3737

3838
context.authenticateAs(user);
3939

40-
return { nanomsg };
40+
return { nanomsg, user };
4141
}
4242

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

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

86-
let { nanomsg } = prepare(this);
87-
88-
this.server.put('/api/v1/me/crate_owner_invitations/:crate_id', (schema, request) => {
89-
assert.deepEqual(request.params, { crate_id: nanomsg.id });
90-
91-
let body = JSON.parse(request.requestBody);
92-
assert.strictEqual(body.crate_owner_invite.accepted, false);
93-
assert.strictEqual(body.crate_owner_invite.crate_id, nanomsg.id);
94-
95-
return { crate_owner_invitation: { crate_id: 42, accepted: false } };
96-
});
86+
let { crateOwnerInvitations, crateOwnerships } = this.server.schema;
87+
assert.equal(crateOwnerInvitations.where({ crateId: nanomsg.id, inviteeId: user.id }).length, 1);
88+
assert.equal(crateOwnerships.where({ crateId: nanomsg.id, userId: user.id }).length, 0);
9789

9890
await visit('/me/pending-invites');
9991
assert.equal(currentURL(), '/me/pending-invites');
@@ -106,6 +98,9 @@ module('Acceptance | /me/pending-invites', function (hooks) {
10698
.hasText('Declined. You have not been added as an owner of crate nanomsg.');
10799
assert.dom('[data-test-invite="nanomsg"] [data-test-crate-link]').doesNotExist();
108100
assert.dom('[data-test-invite="nanomsg"] [data-test-inviter-link]').doesNotExist();
101+
102+
assert.equal(crateOwnerInvitations.where({ crateId: nanomsg.id, inviteeId: user.id }).length, 0);
103+
assert.equal(crateOwnerships.where({ crateId: nanomsg.id, userId: user.id }).length, 0);
109104
});
110105

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

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

128-
let { nanomsg } = prepare(this);
129-
130-
this.server.put('/api/v1/me/crate_owner_invitations/:crate_id', (schema, request) => {
131-
assert.deepEqual(request.params, { crate_id: nanomsg.id });
132-
133-
let body = JSON.parse(request.requestBody);
134-
assert.strictEqual(body.crate_owner_invite.accepted, true);
135-
assert.strictEqual(body.crate_owner_invite.crate_id, nanomsg.id);
136-
137-
return { crate_owner_invitation: { crate_id: 42, accepted: true } };
138-
});
123+
let { crateOwnerInvitations, crateOwnerships } = this.server.schema;
124+
assert.equal(crateOwnerInvitations.where({ crateId: nanomsg.id, inviteeId: user.id }).length, 1);
125+
assert.equal(crateOwnerships.where({ crateId: nanomsg.id, userId: user.id }).length, 0);
139126

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

152139
await percySnapshot(assert);
140+
141+
assert.equal(crateOwnerInvitations.where({ crateId: nanomsg.id, inviteeId: user.id }).length, 0);
142+
assert.equal(crateOwnerships.where({ crateId: nanomsg.id, userId: user.id }).length, 1);
153143
});
154144

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

0 commit comments

Comments
 (0)