Skip to content

Commit

Permalink
mirage: Replace GET /me/crate_owner_invitations route handlers with…
Browse files Browse the repository at this point in the history
… generic implementation
  • Loading branch information
Turbo87 committed Jun 16, 2021
1 parent 6241941 commit b3e27ff
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 71 deletions.
17 changes: 17 additions & 0 deletions mirage/factories/crate-owner-invitation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Factory } from 'ember-cli-mirage';

export default Factory.extend({
createdAt: '2016-12-24T12:34:56Z',

afterCreate(invite) {
if (!invite.crateId) {
throw new Error(`Missing \`crate\` relationship on \`crate-owner-invitation:${invite.id}\``);
}
if (!invite.inviteeId) {
throw new Error(`Missing \`invitee\` relationship on \`crate-owner-invitation:${invite.id}\``);
}
if (!invite.inviterId) {
throw new Error(`Missing \`inviter\` relationship on \`crate-owner-invitation:${invite.id}\``);
}
},
});
7 changes: 7 additions & 0 deletions mirage/models/crate-owner-invitation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { belongsTo, Model } from 'ember-cli-mirage';

export default Model.extend({
crate: belongsTo(),
invitee: belongsTo('user'),
inviter: belongsTo('user'),
});
3 changes: 0 additions & 3 deletions mirage/models/crate-owner-invite.js

This file was deleted.

2 changes: 1 addition & 1 deletion mirage/route-handlers/me.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ export function register(server) {
return new Response(403, {}, { errors: [{ detail: 'must be logged in to perform that action' }] });
}

return { crate_owner_invitations: [] };
return schema.crateOwnerInvitations.where({ inviteeId: user.id });
});
}
33 changes: 33 additions & 0 deletions mirage/serializers/crate-owner-invitation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import BaseSerializer from './application';

export default BaseSerializer.extend({
// eslint-disable-next-line ember/avoid-leaking-state-in-ember-objects
include: ['inviter'],

getHashForResource() {
let [hash, addToIncludes] = BaseSerializer.prototype.getHashForResource.apply(this, arguments);

if (Array.isArray(hash)) {
for (let resource of hash) {
this._adjust(resource);
}
} else {
this._adjust(hash);
}

return [hash, addToIncludes];
},

_adjust(hash) {
delete hash.id;

let crate = this.schema.crates.find(hash.crate_id);
hash.crate_name = crate.name;

hash.invitee_id = Number(hash.invitee_id);
hash.inviter_id = Number(hash.inviter_id);

let inviter = this.schema.users.find(hash.inviter_id);
hash.invited_by_username = inviter.login;
},
});
74 changes: 35 additions & 39 deletions tests/acceptance/invites-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,32 @@ module('Acceptance | /me/pending-invites', function (hooks) {
setupApplicationTest(hooks);

function prepare(context) {
let user = context.server.create('user');
context.authenticateAs(user);

let inviter = context.server.create('user', { name: 'janed' });
let inviter2 = context.server.create('user', { name: 'wycats' });
context.server.get('/api/v1/me/crate_owner_invitations', function () {
let users = [this.serialize(inviter, 'user').user, this.serialize(inviter2, 'user').user];

return {
crate_owner_invitations: [
{
invited_by_username: 'janed',
crate_name: 'nanomsg',
crate_id: 42,
created_at: '2016-12-24T12:34:56Z',
invitee_id: parseInt(user.id, 10),
inviter_id: parseInt(inviter.id, 10),
},
{
invited_by_username: 'wycats',
crate_name: 'ember-rs',
crate_id: 1,
created_at: '2020-12-31T12:34:56Z',
invitee_id: parseInt(user.id, 10),
inviter_id: parseInt(inviter2.id, 10),
},
],
users,
};

let user = context.server.create('user');

let nanomsg = context.server.create('crate', { name: 'nanomsg' });
context.server.create('version', { crate: nanomsg });
context.server.create('crate-owner-invitation', {
crate: nanomsg,
createdAt: '2016-12-24T12:34:56Z',
invitee: user,
inviter,
});

let ember = context.server.create('crate', { name: 'ember-rs' });
context.server.create('version', { crate: ember });
context.server.create('crate-owner-invitation', {
crate: ember,
createdAt: '2020-12-31T12:34:56Z',
invitee: user,
inviter: inviter2,
});

context.authenticateAs(user);

return { nanomsg };
}

test('redirects to / when not logged in', async function (assert) {
Expand Down Expand Up @@ -76,7 +72,7 @@ module('Acceptance | /me/pending-invites', function (hooks) {
test('shows empty list message', async function (assert) {
prepare(this);

this.server.get('/api/v1/me/crate_owner_invitations', { crate_owner_invitations: [] });
this.server.schema.crateOwnerInvitations.all().destroy();

await visit('/me/pending-invites');
assert.equal(currentURL(), '/me/pending-invites');
Expand All @@ -87,14 +83,14 @@ module('Acceptance | /me/pending-invites', function (hooks) {
test('invites can be declined', async function (assert) {
assert.expect(9);

prepare(this);
let { nanomsg } = prepare(this);

this.server.put('/api/v1/me/crate_owner_invitations/:crate', (schema, request) => {
assert.deepEqual(request.params, { crate: '42' });
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, 42);
assert.strictEqual(body.crate_owner_invite.crate_id, nanomsg.id);

return { crate_owner_invitation: { crate_id: 42, accepted: false } };
});
Expand All @@ -115,7 +111,7 @@ module('Acceptance | /me/pending-invites', function (hooks) {
test('error message is shown if decline request fails', async function (assert) {
prepare(this);

this.server.put('/api/v1/me/crate_owner_invitations/:crate', () => new Response(500));
this.server.put('/api/v1/me/crate_owner_invitations/:crate_id', () => new Response(500));

await visit('/me/pending-invites');
assert.equal(currentURL(), '/me/pending-invites');
Expand All @@ -129,14 +125,14 @@ module('Acceptance | /me/pending-invites', function (hooks) {
test('invites can be accepted', async function (assert) {
assert.expect(9);

prepare(this);
let { nanomsg } = prepare(this);

this.server.put('/api/v1/me/crate_owner_invitations/:crate', (schema, request) => {
assert.deepEqual(request.params, { crate: '42' });
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, 42);
assert.strictEqual(body.crate_owner_invite.crate_id, nanomsg.id);

return { crate_owner_invitation: { crate_id: 42, accepted: true } };
});
Expand All @@ -159,7 +155,7 @@ module('Acceptance | /me/pending-invites', function (hooks) {
test('error message is shown if accept request fails', async function (assert) {
prepare(this);

this.server.put('/api/v1/me/crate_owner_invitations/:crate', () => new Response(500));
this.server.put('/api/v1/me/crate_owner_invitations/:crate_id', () => new Response(500));

await visit('/me/pending-invites');
assert.equal(currentURL(), '/me/pending-invites');
Expand All @@ -176,7 +172,7 @@ module('Acceptance | /me/pending-invites', function (hooks) {
let errorMessage =
'The invitation to become an owner of the demo_crate crate expired. Please reach out to an owner of the crate to request a new invitation.';
let payload = { errors: [{ detail: errorMessage }] };
this.server.put('/api/v1/me/crate_owner_invitations/:crate', payload, 410);
this.server.put('/api/v1/me/crate_owner_invitations/:crate_id', payload, 410);

await visit('/me/pending-invites');
assert.equal(currentURL(), '/me/pending-invites');
Expand Down
41 changes: 13 additions & 28 deletions tests/mirage/invitations-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { module, test } from 'qunit';

import fetch from 'fetch';
import timekeeper from 'timekeeper';

import { setupTest } from 'cargo/tests/helpers';

Expand All @@ -23,9 +22,7 @@ module('Mirage | Crate Owner Invitations', function (hooks) {
assert.deepEqual(responsePayload, { crate_owner_invitations: [] });
});

test('returns a paginated crates list', async function (assert) {
timekeeper.freeze(new Date('2016-12-24T12:34:56Z'));

test('returns the list of invitations for the authenticated user', async function (assert) {
let nanomsg = this.server.create('crate', { name: 'nanomsg' });
this.server.create('version', { crate: nanomsg });

Expand All @@ -36,31 +33,19 @@ module('Mirage | Crate Owner Invitations', function (hooks) {
this.server.create('mirage-session', { user });

let inviter = this.server.create('user', { name: 'janed' });
this.server.create('crate-owner-invitation', {
crate: nanomsg,
createdAt: '2016-12-24T12:34:56Z',
invitee: user,
inviter,
});

let inviter2 = this.server.create('user', { name: 'wycats' });
this.server.get('/api/v1/me/crate_owner_invitations', function () {
let users = [this.serialize(inviter, 'user').user, this.serialize(inviter2, 'user').user];

return {
crate_owner_invitations: [
{
invited_by_username: 'janed',
crate_name: nanomsg.name,
crate_id: nanomsg.id,
created_at: '2016-12-24T12:34:56Z',
invitee_id: parseInt(user.id, 10),
inviter_id: parseInt(inviter.id, 10),
},
{
invited_by_username: 'wycats',
crate_name: ember.name,
crate_id: ember.id,
created_at: '2020-12-31T12:34:56Z',
invitee_id: parseInt(user.id, 10),
inviter_id: parseInt(inviter2.id, 10),
},
],
users,
};
this.server.create('crate-owner-invitation', {
crate: ember,
createdAt: '2020-12-31T12:34:56Z',
invitee: user,
inviter: inviter2,
});

let response = await fetch('/api/v1/me/crate_owner_invitations');
Expand Down

0 comments on commit b3e27ff

Please sign in to comment.