2015-12-01 15:31:15 -05:00
|
|
|
import storePretender from 'helpers/store-pretender';
|
|
|
|
import fixturePretender from 'helpers/fixture-pretender';
|
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
function parsePostData(query) {
|
2015-03-13 12:08:28 -04:00
|
|
|
const result = {};
|
2014-07-31 14:51:10 -04:00
|
|
|
query.split("&").forEach(function(part) {
|
2015-03-13 12:08:28 -04:00
|
|
|
const item = part.split("=");
|
2015-04-09 14:54:17 -04:00
|
|
|
const firstSeg = decodeURIComponent(item[0]);
|
|
|
|
const m = /^([^\[]+)\[([^\]]+)\]/.exec(firstSeg);
|
|
|
|
|
|
|
|
const val = decodeURIComponent(item[1]).replace(/\+/g, ' ');
|
|
|
|
if (m) {
|
|
|
|
result[m[1]] = result[m[1]] || {};
|
|
|
|
result[m[1]][m[2]] = val;
|
|
|
|
} else {
|
|
|
|
result[firstSeg] = val;
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-07-31 17:26:44 -04:00
|
|
|
function response(code, obj) {
|
2014-07-31 14:51:10 -04:00
|
|
|
if (typeof code === "object") {
|
|
|
|
obj = code;
|
|
|
|
code = 200;
|
|
|
|
}
|
2014-07-31 17:26:44 -04:00
|
|
|
return [code, {"Content-Type": "application/json"}, obj];
|
2014-07-31 14:51:10 -04:00
|
|
|
}
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
const success = () => response({ success: true });
|
2015-12-01 15:31:15 -05:00
|
|
|
const loggedIn = () => !!Discourse.User.current();
|
2014-10-07 16:03:51 -04:00
|
|
|
|
2015-08-18 17:15:46 -04:00
|
|
|
|
2015-12-01 15:31:15 -05:00
|
|
|
const helpers = { response, success, parsePostData };
|
2015-04-01 14:18:46 -04:00
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
export default function() {
|
2015-04-01 14:18:46 -04:00
|
|
|
|
2015-03-13 12:08:28 -04:00
|
|
|
const server = new Pretender(function() {
|
2015-12-01 15:31:15 -05:00
|
|
|
storePretender.call(this, helpers);
|
|
|
|
const fixturesByUrl = fixturePretender.call(this, helpers);
|
2014-08-01 17:26:43 -04:00
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
this.get('/admin/plugins', () => response({ plugins: [] }));
|
2015-08-27 16:59:36 -04:00
|
|
|
|
2016-06-06 15:23:19 -04:00
|
|
|
this.get('/composer_messages', () => response({ composer_messages: [] }));
|
2015-04-01 14:18:46 -04:00
|
|
|
|
|
|
|
this.get("/latest.json", () => {
|
|
|
|
const json = fixturesByUrl['/latest.json'];
|
|
|
|
|
|
|
|
if (loggedIn()) {
|
|
|
|
// Stuff to let us post
|
|
|
|
json.topic_list.can_create_topic = true;
|
|
|
|
json.topic_list.draft_key = "new_topic";
|
|
|
|
json.topic_list.draft_sequence = 1;
|
|
|
|
}
|
|
|
|
return response(json);
|
|
|
|
});
|
|
|
|
|
2016-11-10 13:33:31 -05:00
|
|
|
this.get('/tags', () => {
|
|
|
|
return response({ tags: [{
|
|
|
|
id: 'eviltrout',
|
|
|
|
count: 1
|
|
|
|
}] });
|
|
|
|
});
|
|
|
|
|
2016-11-22 14:02:47 -05:00
|
|
|
this.get(`/users/eviltrout/emails.json`, () => {
|
|
|
|
return response({ email: 'eviltrout@example.com' });
|
|
|
|
});
|
|
|
|
|
2015-05-13 10:42:36 -04:00
|
|
|
this.get('/users/eviltrout.json', () => {
|
|
|
|
const json = fixturesByUrl['/users/eviltrout.json'];
|
2016-11-10 14:35:53 -05:00
|
|
|
json.user.can_edit = loggedIn();
|
2015-05-13 10:42:36 -04:00
|
|
|
return response(json);
|
|
|
|
});
|
|
|
|
|
2016-08-02 13:21:06 -04:00
|
|
|
this.get('/users/eviltrout/summary.json', () => {
|
|
|
|
return response({
|
|
|
|
user_summary: {
|
|
|
|
topics: [],
|
|
|
|
topic_ids: [],
|
|
|
|
replies: [],
|
|
|
|
links: []
|
|
|
|
},
|
|
|
|
topics: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-10 13:33:31 -05:00
|
|
|
this.get('/users/eviltrout/invited_count.json', () => {
|
|
|
|
return response({
|
|
|
|
"counts": { "pending": 1, "redeemed": 0, "total": 0 }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/users/eviltrout/invited.json', () => {
|
|
|
|
return response({ "invites": [ {id: 1} ] });
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/topics/private-messages/eviltrout.json', () => {
|
|
|
|
return response({ topic_list: { topics: [] } });
|
|
|
|
});
|
|
|
|
|
2016-06-30 13:55:44 -04:00
|
|
|
this.get('/clicks/track', success);
|
|
|
|
|
2016-08-09 12:16:29 -04:00
|
|
|
this.get('/search', request => {
|
|
|
|
if (request.queryParams.q === 'posts') {
|
|
|
|
return response({
|
|
|
|
posts: [{
|
|
|
|
id: 1234
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return response({});
|
|
|
|
});
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
this.put('/users/eviltrout', () => response({ user: {} }));
|
2015-05-13 10:42:36 -04:00
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
this.get("/t/280.json", () => response(fixturesByUrl['/t/280/1.json']));
|
|
|
|
this.get("/t/28830.json", () => response(fixturesByUrl['/t/28830/1.json']));
|
|
|
|
this.get("/t/9.json", () => response(fixturesByUrl['/t/9/1.json']));
|
2015-08-13 12:49:13 -04:00
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
this.get("/t/id_for/:slug", () => {
|
2014-09-17 11:18:41 -04:00
|
|
|
return response({id: 280, slug: "internationalization-localization", url: "/t/internationalization-localization/280"});
|
|
|
|
});
|
|
|
|
|
2016-06-30 13:55:44 -04:00
|
|
|
this.delete('/t/:id', success);
|
|
|
|
this.put('/t/:id/recover', success);
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
this.get("/404-body", () => {
|
2014-08-01 17:26:43 -04:00
|
|
|
return [200, {"Content-Type": "text/html"}, "<div class='page-not-found'>not found</div>"];
|
|
|
|
});
|
|
|
|
|
2015-04-09 18:33:37 -04:00
|
|
|
this.delete('/draft.json', success);
|
2015-08-13 12:49:13 -04:00
|
|
|
this.post('/draft.json', success);
|
2015-04-09 18:33:37 -04:00
|
|
|
|
2015-04-21 14:36:46 -04:00
|
|
|
this.get('/users/:username/staff-info.json', () => response({}));
|
|
|
|
|
2016-01-04 15:18:09 -05:00
|
|
|
this.get('/post_action_users', () => {
|
|
|
|
return response({
|
|
|
|
post_action_users: [
|
|
|
|
{id: 1, username: 'eviltrout', avatar_template: '/user_avatar/default/eviltrout/{size}/1.png', username_lower: 'eviltrout' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/post_replies', () => {
|
|
|
|
return response({ post_replies: [{ id: 1234, cooked: 'wat' }] });
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/post_reply_histories', () => {
|
|
|
|
return response({ post_reply_histories: [{ id: 1234, cooked: 'wat' }] });
|
|
|
|
});
|
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
this.get('/category_hashtags/check', () => {
|
|
|
|
return response({ valid: [{ slug: "bug", url: '/c/bugs' }] });
|
|
|
|
});
|
|
|
|
|
2016-08-29 16:47:44 -04:00
|
|
|
this.get("/categories_and_latest", () => response(fixturesByUrl["/categories_and_latest.json"]));
|
|
|
|
|
2016-01-04 15:18:09 -05:00
|
|
|
this.put('/categories/:category_id', request => {
|
2016-07-28 11:57:30 -04:00
|
|
|
|
2015-07-02 12:06:24 -04:00
|
|
|
const category = parsePostData(request.requestBody);
|
2016-07-28 11:57:30 -04:00
|
|
|
|
|
|
|
if (category.email_in === "duplicate@example.com") {
|
|
|
|
return response(422, {"errors": ['duplicate email']});
|
|
|
|
}
|
|
|
|
|
2015-07-02 12:06:24 -04:00
|
|
|
return response({category});
|
|
|
|
});
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
this.get('/draft.json', () => response({}));
|
2015-03-13 02:45:55 -04:00
|
|
|
|
2015-04-10 17:00:50 -04:00
|
|
|
this.put('/queued_posts/:queued_post_id', function(request) {
|
|
|
|
return response({ queued_post: {id: request.params.queued_post_id } });
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/queued_posts', function() {
|
|
|
|
return response({
|
2015-05-19 13:00:50 -04:00
|
|
|
queued_posts: [{id: 1, raw: 'queued post text', can_delete_user: true}]
|
2015-04-10 17:00:50 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
this.post('/session', function(request) {
|
2015-03-13 12:08:28 -04:00
|
|
|
const data = parsePostData(request.requestBody);
|
2014-07-31 14:51:10 -04:00
|
|
|
|
|
|
|
if (data.password === 'correct') {
|
2014-07-31 17:26:44 -04:00
|
|
|
return response({username: 'eviltrout'});
|
2014-07-31 14:51:10 -04:00
|
|
|
}
|
2016-08-05 12:01:16 -04:00
|
|
|
|
|
|
|
if (data.password === 'not-activated') {
|
|
|
|
return response({ error: "not active",
|
|
|
|
reason: "not_activated",
|
|
|
|
sent_to_email: '<small>eviltrout@example.com</small>',
|
|
|
|
current_email: '<small>current@example.com</small>' });
|
|
|
|
}
|
|
|
|
|
2014-07-31 17:26:44 -04:00
|
|
|
return response(400, {error: 'invalid login'});
|
2014-07-31 14:51:10 -04:00
|
|
|
});
|
|
|
|
|
2016-08-05 12:01:16 -04:00
|
|
|
this.post('/users/action/send_activation_email', success);
|
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
this.get('/users/hp.json', function() {
|
2014-07-31 17:26:44 -04:00
|
|
|
return response({"value":"32faff1b1ef1ac3","challenge":"61a3de0ccf086fb9604b76e884d75801"});
|
2014-07-31 14:51:10 -04:00
|
|
|
});
|
|
|
|
|
2014-07-31 17:06:00 -04:00
|
|
|
this.get('/session/csrf', function() {
|
2014-07-31 17:26:44 -04:00
|
|
|
return response({"csrf":"mgk906YLagHo2gOgM1ddYjAN4hQolBdJCqlY6jYzAYs="});
|
2014-07-31 17:06:00 -04:00
|
|
|
});
|
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
this.get('/users/check_username', function(request) {
|
|
|
|
if (request.queryParams.username === 'taken') {
|
2014-07-31 17:26:44 -04:00
|
|
|
return response({available: false, suggestion: 'nottaken'});
|
2014-07-31 14:51:10 -04:00
|
|
|
}
|
2014-07-31 17:26:44 -04:00
|
|
|
return response({available: true});
|
2014-07-31 14:51:10 -04:00
|
|
|
});
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
this.post('/users', () => response({success: true}));
|
2014-07-31 17:59:52 -04:00
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
this.get('/login.html', () => [200, {}, 'LOGIN PAGE']);
|
2014-10-07 16:03:51 -04:00
|
|
|
|
|
|
|
this.delete('/posts/:post_id', success);
|
|
|
|
this.put('/posts/:post_id/recover', success);
|
2016-01-04 15:18:09 -05:00
|
|
|
this.get('/posts/:post_id/expand-embed', success);
|
2015-03-06 12:37:24 -05:00
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
this.put('/posts/:post_id', request => {
|
2015-04-09 14:54:17 -04:00
|
|
|
const data = parsePostData(request.requestBody);
|
|
|
|
data.post.id = request.params.post_id;
|
|
|
|
data.post.version = 2;
|
|
|
|
return response(200, data.post);
|
2015-04-01 14:18:46 -04:00
|
|
|
});
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
this.get('/t/403.json', () => response(403, {}));
|
|
|
|
this.get('/t/404.json', () => response(404, "not found"));
|
|
|
|
this.get('/t/500.json', () => response(502, {}));
|
2015-05-27 13:53:49 -04:00
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
this.put('/t/:slug/:id', request => {
|
2015-04-01 14:18:46 -04:00
|
|
|
const data = parsePostData(request.requestBody);
|
|
|
|
|
|
|
|
return response(200, { basic_topic: {id: request.params.id,
|
|
|
|
title: data.title,
|
|
|
|
fancy_title: data.title,
|
2015-04-08 14:17:21 -04:00
|
|
|
slug: request.params.slug } });
|
2015-04-01 14:18:46 -04:00
|
|
|
});
|
|
|
|
|
2016-04-11 13:16:37 -04:00
|
|
|
this.get("/groups/discourse/topics.json", () => {
|
|
|
|
return response(200, fixturesByUrl['/groups/discourse/posts.json']);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get("/groups/discourse/mentions.json", () => {
|
|
|
|
return response(200, fixturesByUrl['/groups/discourse/posts.json']);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get("/groups/discourse/messages.json", () => {
|
|
|
|
return response(200, fixturesByUrl['/groups/discourse/posts.json']);
|
|
|
|
});
|
|
|
|
|
2015-12-01 16:04:13 -05:00
|
|
|
this.get('/t/:topic_id/posts.json', request => {
|
|
|
|
const postIds = request.queryParams.post_ids;
|
|
|
|
const posts = postIds.map(p => ({id: parseInt(p), post_number: parseInt(p) }));
|
|
|
|
return response(200, { post_stream: { posts } });
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/posts/:post_id/reply-history.json', () => {
|
|
|
|
return response(200, [ { id: 2222, post_number: 2222 } ]);
|
|
|
|
});
|
|
|
|
|
2016-06-30 13:55:44 -04:00
|
|
|
this.post('/user_badges', () => response(200, fixturesByUrl['/user_badges']));
|
|
|
|
this.delete('/user_badges/:badge_id', success);
|
|
|
|
|
2015-04-01 14:18:46 -04:00
|
|
|
this.post('/posts', function(request) {
|
|
|
|
const data = parsePostData(request.requestBody);
|
|
|
|
|
|
|
|
if (data.title === "this title triggers an error") {
|
|
|
|
return response(422, {errors: ['That title has already been taken']});
|
|
|
|
}
|
2015-04-09 18:33:37 -04:00
|
|
|
|
|
|
|
if (data.raw === "enqueue this content please") {
|
|
|
|
return response(200, { success: true, action: 'enqueued' });
|
|
|
|
}
|
|
|
|
|
|
|
|
return response(200, {
|
|
|
|
success: true,
|
|
|
|
action: 'create_post',
|
|
|
|
post: {id: 12345, topic_id: 280, topic_slug: 'internationalization-localization'}
|
|
|
|
});
|
2015-04-01 14:18:46 -04:00
|
|
|
});
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
this.post('/topics/timings', () => response(200, {}));
|
|
|
|
|
|
|
|
const siteText = {id: 'site.test', value: 'Test McTest'};
|
2015-11-30 15:22:58 -05:00
|
|
|
const overridden = {id: 'site.overridden', value: 'Overridden', overridden: true };
|
2016-08-05 12:01:16 -04:00
|
|
|
|
|
|
|
this.get('/admin/users/list/active.json', () => {
|
|
|
|
return response(200, [
|
|
|
|
{id: 1, username: 'eviltrout', email: '<small>eviltrout@example.com</small>'}
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2015-11-30 15:22:58 -05:00
|
|
|
this.get('/admin/customize/site_texts', request => {
|
|
|
|
|
|
|
|
if (request.queryParams.overridden) {
|
2015-12-01 15:31:15 -05:00
|
|
|
return response(200, {site_texts: [overridden] });
|
2015-11-30 15:22:58 -05:00
|
|
|
} else {
|
2015-12-01 15:31:15 -05:00
|
|
|
return response(200, {site_texts: [siteText, overridden] });
|
2015-11-30 15:22:58 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
this.get('/admin/customize/site_texts/:key', () => response(200, {site_text: siteText }));
|
|
|
|
this.delete('/admin/customize/site_texts/:key', () => response(200, {site_text: siteText }));
|
|
|
|
|
|
|
|
this.put('/admin/customize/site_texts/:key', request => {
|
|
|
|
const result = parsePostData(request.requestBody);
|
|
|
|
result.id = request.params.key;
|
|
|
|
result.can_revert = true;
|
|
|
|
return response(200, {site_text: result});
|
2015-04-01 14:18:46 -04:00
|
|
|
});
|
2016-07-05 14:58:58 -04:00
|
|
|
|
|
|
|
this.get('/tag_groups', () => response(200, {tag_groups: []}));
|
2016-06-30 13:55:44 -04:00
|
|
|
this.post('/admin/users/:user_id/generate_api_key', success);
|
|
|
|
this.delete('/admin/users/:user_id/revoke_api_key', success);
|
|
|
|
this.post('/admin/badges', success);
|
|
|
|
this.delete('/admin/badges/:id', success);
|
2015-04-01 14:18:46 -04:00
|
|
|
});
|
2014-07-31 17:26:44 -04:00
|
|
|
|
|
|
|
server.prepareBody = function(body){
|
|
|
|
if (body && typeof body === "object") {
|
|
|
|
return JSON.stringify(body);
|
|
|
|
}
|
2014-08-01 17:26:43 -04:00
|
|
|
return body;
|
2014-07-31 17:26:44 -04:00
|
|
|
};
|
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
server.unhandledRequest = function(verb, path) {
|
2015-03-13 12:08:28 -04:00
|
|
|
const error = 'Unhandled request in test environment: ' + path + ' (' + verb + ')';
|
2014-07-31 18:44:32 -04:00
|
|
|
window.console.error(error);
|
|
|
|
throw error;
|
2014-07-31 14:51:10 -04:00
|
|
|
};
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
server.checkPassthrough = request => request.requestHeaders['Discourse-Script'];
|
2014-07-31 14:51:10 -04:00
|
|
|
return server;
|
|
|
|
}
|