mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-20 17:32:15 +03:00
fb549645f8
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
73 lines
1.9 KiB
JavaScript
73 lines
1.9 KiB
JavaScript
import {Response} from 'ember-cli-mirage';
|
|
import {dasherize} from '@ember/string';
|
|
import {isBlank} from '@ember/utils';
|
|
import {paginateModelArray} from '../utils';
|
|
|
|
export default function mockPosts(server) {
|
|
server.post('/posts', function ({posts}) {
|
|
let attrs = this.normalizedRequestAttrs();
|
|
|
|
// mirage expects `author` to be a reference but we only have an ID
|
|
attrs.authorId = attrs.author;
|
|
delete attrs.author;
|
|
|
|
if (isBlank(attrs.slug) && !isBlank(attrs.title)) {
|
|
attrs.slug = dasherize(attrs.title);
|
|
}
|
|
|
|
return posts.create(attrs);
|
|
});
|
|
|
|
// TODO: handle author filter
|
|
server.get('/posts/', function ({posts}, {queryParams}) {
|
|
let page = +queryParams.page || 1;
|
|
let limit = +queryParams.limit || 15;
|
|
let {status, staticPages} = queryParams;
|
|
let query = {};
|
|
let models;
|
|
|
|
if (status && status !== 'all') {
|
|
query.status = status;
|
|
}
|
|
|
|
if (staticPages === 'false') {
|
|
query.page = false;
|
|
}
|
|
|
|
if (staticPages === 'true') {
|
|
query.page = true;
|
|
}
|
|
|
|
models = posts.where(query).models;
|
|
|
|
return paginateModelArray('posts', models, page, limit);
|
|
});
|
|
|
|
server.get('/posts/:id/', function ({posts}, {params}) {
|
|
let {id} = params;
|
|
let post = posts.find(id);
|
|
|
|
return post || new Response(404, {}, {
|
|
errors: [{
|
|
errorType: 'NotFoundError',
|
|
message: 'Post not found.'
|
|
}]
|
|
});
|
|
});
|
|
|
|
// Handle embedded author in post
|
|
server.put('/posts/:id/', function ({posts}, request) {
|
|
let post = this.normalizedRequestAttrs();
|
|
let {author} = post;
|
|
delete post.author;
|
|
|
|
let savedPost = posts.find(request.params.id).update(post);
|
|
savedPost.authorId = author;
|
|
savedPost.save();
|
|
|
|
return savedPost;
|
|
});
|
|
|
|
server.del('/posts/:id/');
|
|
}
|