Ghost/ghost/admin/mirage/config/invites.js
Kevin Ansfield fb549645f8 Fixed normalization of attrs in Mirage config and tests (#872)
no issue

We weren't being consistent in our use of Mirage's `normalizedRequestAttrs()` method which meant that in certain cases Mirage's internal database had duplicated attrs, the original set being `camelCase` and the new/updated set being `underscore_case` which was not only confusing but can lead to errors or unexpected behaviour in tests.

- updated Mirage config to always normalize where necessary
- updated tests to always use `camelCase` attrs
- added `HEAD` route handler for gravatar to avoid unknown route noise in tests
2017-09-28 09:33:00 +07:00

43 lines
1.2 KiB
JavaScript

import moment from 'moment';
import {Response} from 'ember-cli-mirage';
import {paginatedResponse} from '../utils';
export default function mockInvites(server) {
server.get('/invites/', paginatedResponse('invites'));
server.get('/invites/:id', function (schema, request) {
let {id} = request.params;
let invite = schema.invites.find(id);
return invite || new Response(404, {}, {
errors: [{
errorType: 'NotFoundError',
message: 'Invite not found.'
}]
});
});
server.post('/invites/', function ({invites}) {
let attrs = this.normalizedRequestAttrs();
let oldInvite = invites.findBy({email: attrs.email});
if (oldInvite) {
oldInvite.destroy();
}
/* eslint-disable camelcase */
attrs.token = `${invites.all().models.length}-token`;
attrs.expires = moment.utc().add(1, 'day').valueOf();
attrs.createdAt = moment.utc().format();
attrs.createdBy = 1;
attrs.updatedAt = moment.utc().format();
attrs.updatedBy = 1;
attrs.status = 'sent';
/* eslint-enable camelcase */
return invites.create(attrs);
});
server.del('/invites/:id/');
}