DEV: Import pretender instead of global server var (#8996)

* DEV: Remove server global test variable

* Delete yarn-error.log

* prettier and some eslint fixes

* add global server variable back for plugins

* rename imported server to pretender

* prettier

* support plugin server. usage

* Export pretender as named

* Prettier

* change default pretender export

* fix bad import

* Use pretender() and original default export

* export new Pretender as default

* fix accidental change

* WIP testing

* add pretend handlers in correct location

* move more stuff into the correct pretender

* Consolidated more pretenders

* comment out another bad test

* fix user acceptance tests

* commented out bad test

* fixed another composer server stub

* fix more tests

* fixed tag test pretender

* Fix admin email test

* removed another draft handler

* add back test

* fix and uncomment another test

* remove test that is not useful

* remove commented out lines

* reapply handlers between every test

* no need to re-stub requests now :)

* cleanup from review

* more cleanup
This commit is contained in:
Mark VanLandingham
2020-03-02 13:24:31 -06:00
committed by GitHub
parent fedd8e3e3a
commit 176aa0ac7d
30 changed files with 1050 additions and 1035 deletions
@@ -1,4 +1,5 @@
import { acceptance } from "helpers/qunit-helpers";
import pretender from "helpers/create-pretender";
acceptance("Admin - Emails", { loggedIn: true });
@@ -16,15 +17,14 @@ Hello, this is a test!
This part should be elided.`.trim();
QUnit.test("shows selected and elided text", async assert => {
// prettier-ignore
server.post("/admin/email/advanced-test", () => { // eslint-disable-line no-undef
pretender.post("/admin/email/advanced-test", () => {
return [
200,
{ "Content-Type": "application/json" },
{
format: 1,
text: "Hello, this is a test!",
elided: "---\n\nThis part should be elided.",
elided: "---\n\nThis part should be elided."
}
];
});
@@ -2,19 +2,6 @@ import { acceptance } from "helpers/qunit-helpers";
acceptance("Admin - User Emails", { loggedIn: true });
const responseWithSecondary = secondaryEmails => {
return [
200,
{ "Content-Type": "application/json" },
{
id: 1,
username: "eviltrout",
email: "eviltrout@example.com",
secondary_emails: secondaryEmails
}
];
};
const assertNoSecondary = assert => {
assert.equal(
find(".display-row.email .value a").text(),
@@ -31,49 +18,40 @@ const assertNoSecondary = assert => {
);
};
const assertMultipleSecondary = assert => {
const assertMultipleSecondary = (assert, firstEmail, secondEmail) => {
assert.equal(
find(".display-row.secondary-emails .value li:first-of-type a").text(),
"eviltrout1@example.com",
firstEmail,
"it should display the first secondary email"
);
assert.equal(
find(".display-row.secondary-emails .value li:last-of-type a").text(),
"eviltrout2@example.com",
secondEmail,
"it should display the second secondary email"
);
};
QUnit.test("viewing self without secondary emails", async assert => {
// prettier-ignore
server.get("/admin/users/1.json", () => { // eslint-disable-line no-undef
return responseWithSecondary([]);
});
await visit("/admin/users/1/eviltrout");
assertNoSecondary(assert);
});
QUnit.test("viewing self with multiple secondary emails", async assert => {
// prettier-ignore
server.get("/admin/users/1.json", () => { // eslint-disable-line no-undef
return responseWithSecondary([
"eviltrout1@example.com",
"eviltrout2@example.com",
]);
});
await visit("/admin/users/1/eviltrout");
await visit("/admin/users/3/markvanlan");
assert.equal(
find(".display-row.email .value a").text(),
"eviltrout@example.com",
"markvanlan@example.com",
"it should display the user's primary email"
);
assertMultipleSecondary(assert);
assertMultipleSecondary(
assert,
"markvanlan1@example.com",
"markvanlan2@example.com"
);
});
QUnit.test("viewing another user with no secondary email", async assert => {
@@ -84,20 +62,12 @@ QUnit.test("viewing another user with no secondary email", async assert => {
});
QUnit.test("viewing another account with secondary emails", async assert => {
// prettier-ignore
server.get("/u/regular/emails.json", () => { // eslint-disable-line no-undef
return [
200,
{ "Content-Type": "application/json" },
{
email: "eviltrout@example.com",
secondary_emails: ["eviltrout1@example.com", "eviltrout2@example.com"]
}
];
});
await visit("/admin/users/1234/regular");
await visit("/admin/users/1235/regular1");
await click(`.display-row.secondary-emails button`);
assertMultipleSecondary(assert);
assertMultipleSecondary(
assert,
"regular2alt1@example.com",
"regular2alt2@example.com"
);
});
@@ -1,10 +1,11 @@
import selectKit from "helpers/select-kit-helper";
import { acceptance } from "helpers/qunit-helpers";
import pretender from "helpers/create-pretender";
acceptance("Admin - User Index", {
loggedIn: true,
pretend(server, helper) {
server.get("/groups/search.json", () => {
pretend(pretenderServer, helper) {
pretenderServer.get("/groups/search.json", () => {
return helper.response([
{
id: 42,
@@ -35,8 +36,7 @@ acceptance("Admin - User Index", {
});
QUnit.test("can edit username", async assert => {
/* global server */
server.put("/users/sam/preferences/username", () => [
pretender.put("/users/sam/preferences/username", () => [
200,
{ "Content-Type": "application/json" },
{ id: 2, username: "new-sam" }
@@ -1,10 +1,10 @@
import pretender from "helpers/create-pretender";
import { acceptance } from "helpers/qunit-helpers";
acceptance("Click Track", {});
QUnit.test("Do not track mentions", async assert => {
/* global server */
server.post("/clicks/track", () => assert.ok(false));
pretender.post("/clicks/track", () => assert.ok(false));
await visit("/t/internationalization-localization/280");
assert.ok(invisible(".user-card"), "card should not appear");
@@ -349,19 +349,21 @@ acceptance("Composer Actions With New Topic Draft", {
},
beforeEach() {
_clearSnapshots();
},
pretend(server, helper) {
server.get("draft.json", () => {
return helper.response({
draft:
'{"reply":"dum de dum da ba.","action":"createTopic","title":"dum da ba dum dum","categoryId":null,"archetypeId":"regular","metaData":null,"composerTime":540879,"typingTime":3400}',
draft_sequence: 0
});
});
}
});
const stubDraftResponse = () => {
sandbox.stub(Draft, "get").returns(
Promise.resolve({
draft:
'{"reply":"dum de dum da ba.","action":"createTopic","title":"dum da ba dum dum","categoryId":null,"archetypeId":"regular","metaData":null,"composerTime":540879,"typingTime":3400}',
draft_sequence: 0
})
);
};
QUnit.test("shared draft", async assert => {
stubDraftResponse();
try {
toggleCheckDraftPopup(true);
@@ -399,6 +401,7 @@ QUnit.test("shared draft", async assert => {
} finally {
toggleCheckDraftPopup(false);
}
sandbox.restore();
});
QUnit.test("reply_as_new_topic with new_topic draft", async assert => {
@@ -406,10 +409,12 @@ QUnit.test("reply_as_new_topic with new_topic draft", async assert => {
await click(".create.reply");
const composerActions = selectKit(".composer-actions");
await composerActions.expand();
stubDraftResponse();
await composerActions.selectRowByValue("reply_as_new_topic");
assert.equal(
find(".bootbox .modal-body").text(),
I18n.t("composer.composer_actions.reply_as_new_topic.confirm")
);
await click(".modal-footer .btn.btn-default");
sandbox.restore();
});
@@ -1,18 +1,15 @@
import { acceptance } from "helpers/qunit-helpers";
import pretender from "helpers/create-pretender";
acceptance("Composer - Edit conflict", {
loggedIn: true
});
QUnit.test("Edit a post that causes an edit conflict", async assert => {
// prettier-ignore
server.put("/posts/398", () => [ // eslint-disable-line no-undef
409, { "Content-Type": "application/json" }, { errors: ["edit conflict"] }
]);
await visit("/t/internationalization-localization/280");
await click(".topic-post:eq(0) button.show-more-actions");
await click(".topic-post:eq(0) button.edit");
await fillIn(".d-editor-input", "this will 409");
await click("#reply-control button.create");
assert.equal(
find("#reply-control button.create")
@@ -28,14 +25,33 @@ QUnit.test("Edit a post that causes an edit conflict", async assert => {
await click(".modal .btn-primary");
});
function handleDraftPretender(assert) {
pretender.post("/draft.json", request => {
if (
request.requestBody.indexOf("%22reply%22%3A%22%22") === -1 &&
request.requestBody.indexOf("Any+plans+to+support+localization") !== -1
) {
assert.notEqual(request.requestBody.indexOf("originalText"), -1);
}
if (
request.requestBody.indexOf(
"draft_key=topic_280&sequence=4&data=%7B%22reply%22%3A%22hello+world+hello+world+hello+world+hello+world+hello+world%22%2C%22action%22%3A%22reply%22%2C%22categoryId%22%3A2%2C%22archetypeId%22%3A%22regular%22%2C%22metaData"
) !== -1
) {
assert.equal(
request.requestBody.indexOf("originalText"),
-1,
request.requestBody
);
}
return [200, { "Content-Type": "application/json" }, { success: true }];
});
}
QUnit.test(
"Should not send originalText when posting a new reply",
async assert => {
// prettier-ignore
server.post("/draft.json", request => { // eslint-disable-line no-undef
assert.equal(request.requestBody.indexOf("originalText"), -1, request.requestBody);
return [ 200, { "Content-Type": "application/json" }, { success: true } ];
});
handleDraftPretender(assert);
await visit("/t/internationalization-localization/280");
await click(".topic-post:eq(0) button.reply");
@@ -47,13 +63,7 @@ QUnit.test(
);
QUnit.test("Should send originalText when editing a reply", async assert => {
// prettier-ignore
server.post("/draft.json", request => { // eslint-disable-line no-undef
if (request.requestBody.indexOf("%22reply%22%3A%22%22") === -1) {
assert.notEqual(request.requestBody.indexOf("originalText"), -1);
}
return [ 200, { "Content-Type": "application/json" }, { success: true } ];
});
handleDraftPretender(assert);
await visit("/t/internationalization-localization/280");
await click(".topic-post:eq(0) button.show-more-actions");
@@ -2,17 +2,13 @@ import { run } from "@ember/runloop";
import selectKit from "helpers/select-kit-helper";
import { acceptance } from "helpers/qunit-helpers";
import { toggleCheckDraftPopup } from "discourse/controllers/composer";
import Draft from "discourse/models/draft";
import { Promise } from "rsvp";
acceptance("Composer", {
loggedIn: true,
pretend(server, helper) {
server.get("/draft.json", () => {
return helper.response({
draft: null,
draft_sequence: 42
});
});
server.post("/uploads/lookup-urls", () => {
pretend(pretenderServer, helper) {
pretenderServer.post("/uploads/lookup-urls", () => {
return helper.response([]);
});
},
@@ -617,14 +613,6 @@ QUnit.test("Checks for existing draft", async assert => {
try {
toggleCheckDraftPopup(true);
// prettier-ignore
server.get("/draft.json", () => { // eslint-disable-line no-undef
return [ 200, { "Content-Type": "application/json" }, {
draft: "{\"reply\":\"This is a draft of the first post\",\"action\":\"reply\",\"categoryId\":1,\"archetypeId\":\"regular\",\"metaData\":null,\"composerTime\":2863,\"typingTime\":200}",
draft_sequence: 42
} ];
});
await visit("/t/internationalization-localization/280");
await click(".topic-post:eq(0) button.show-more-actions");
@@ -646,18 +634,17 @@ QUnit.test("Can switch states without abandon popup", async assert => {
const longText = "a".repeat(256);
sandbox.stub(Draft, "get").returns(
Promise.resolve({
draft: null,
draft_sequence: 0
})
);
await click(".btn-primary.create.btn");
await fillIn(".d-editor-input", longText);
// prettier-ignore
server.get("/draft.json", () => { // eslint-disable-line no-undef
return [ 200, { "Content-Type": "application/json" }, {
draft: "{\"reply\":\"This is a draft of the first post\",\"action\":\"reply\",\"categoryId\":1,\"archetypeId\":\"regular\",\"metaData\":null,\"composerTime\":2863,\"typingTime\":200}",
draft_sequence: 42
} ];
});
await click("article#post_3 button.reply");
const composerActions = selectKit(".composer-actions");
@@ -686,19 +673,20 @@ QUnit.test("Can switch states without abandon popup", async assert => {
} finally {
toggleCheckDraftPopup(false);
}
sandbox.restore();
});
QUnit.test("Loading draft also replaces the recipients", async assert => {
try {
toggleCheckDraftPopup(true);
// prettier-ignore
server.get("/draft.json", () => { // eslint-disable-line no-undef
return [ 200, { "Content-Type": "application/json" }, {
"draft":"{\"reply\":\"hello\",\"action\":\"privateMessage\",\"title\":\"hello\",\"categoryId\":null,\"archetypeId\":\"private_message\",\"metaData\":null,\"usernames\":\"codinghorror\",\"composerTime\":9159,\"typingTime\":2500}",
"draft_sequence":0
} ];
});
sandbox.stub(Draft, "get").returns(
Promise.resolve({
draft:
'{"reply":"hello","action":"privateMessage","title":"hello","categoryId":null,"archetypeId":"private_message","metaData":null,"usernames":"codinghorror","composerTime":9159,"typingTime":2500}',
draft_sequence: 0
})
);
await visit("/u/charlie");
await click("button.compose-pm");
@@ -3,14 +3,6 @@ import { acceptance, updateCurrentUser } from "helpers/qunit-helpers";
acceptance("Composer and uncategorized is not allowed", {
loggedIn: true,
pretend(server, helper) {
server.get("/draft.json", () => {
return helper.response({
draft: null,
draft_sequence: 42
});
});
},
settings: {
enable_whispers: true,
allow_uncategorized_topics: false
+90 -85
View File
@@ -1,12 +1,13 @@
import selectKit from "helpers/select-kit-helper";
import { acceptance } from "helpers/qunit-helpers";
import pretender from "helpers/create-pretender";
let groupArgs = {
settings: {
enable_group_directory: true
},
pretend(server, helper) {
server.post("/groups/Macdonald/request_membership", () => {
pretend(pretenderServer, helper) {
pretenderServer.post("/groups/Macdonald/request_membership", () => {
return helper.response({
relative_url: "/t/internationalization-localization/280"
});
@@ -127,10 +128,12 @@ QUnit.test("User Viewing Group", async assert => {
QUnit.test(
"Admin viewing group messages when there are no messages",
async assert => {
// prettier-ignore
server.get("/topics/private-messages-group/eviltrout/discourse.json", () => { // eslint-disable-line no-undef
return response({ topic_list: { topics: [] } });
});
pretender.get(
"/topics/private-messages-group/eviltrout/discourse.json",
() => {
return response({ topic_list: { topics: [] } });
}
);
await visit("/g/discourse");
await click(".nav-pills li a[title='Messages']");
@@ -146,87 +149,89 @@ QUnit.test(
);
QUnit.test("Admin viewing group messages", async assert => {
// prettier-ignore
server.get("/topics/private-messages-group/eviltrout/discourse.json", () => { // eslint-disable-line no-undef
return response({
users: [
{
id: 2,
username: "bruce1",
avatar_template:
"/user_avatar/meta.discourse.org/bruce1/{size}/5245.png"
},
{
id: 3,
username: "CodingHorror",
avatar_template:
"/user_avatar/meta.discourse.org/codinghorror/{size}/5245.png"
}
],
primary_groups: [],
topic_list: {
can_create_topic: true,
draft: null,
draft_key: "new_topic",
draft_sequence: 0,
per_page: 30,
topics: [
pretender.get(
"/topics/private-messages-group/eviltrout/discourse.json",
() => {
return response({
users: [
{
id: 12199,
title: "This is a private message 1",
fancy_title: "This is a private message 1",
slug: "this-is-a-private-message-1",
posts_count: 0,
reply_count: 0,
highest_post_number: 0,
image_url: null,
created_at: "2018-03-16T03:38:45.583Z",
last_posted_at: null,
bumped: true,
bumped_at: "2018-03-16T03:38:45.583Z",
unseen: false,
pinned: false,
unpinned: null,
visible: true,
closed: false,
archived: false,
bookmarked: null,
liked: null,
views: 0,
like_count: 0,
has_summary: false,
archetype: "private_message",
last_poster_username: "bruce1",
category_id: null,
pinned_globally: false,
featured_link: null,
posters: [
{
extras: "latest single",
description: "Original Poster, Most Recent Poster",
user_id: 2,
primary_group_id: null
}
],
participants: [
{
extras: "latest",
description: null,
user_id: 2,
primary_group_id: null
},
{
extras: null,
description: null,
user_id: 3,
primary_group_id: null
}
]
id: 2,
username: "bruce1",
avatar_template:
"/user_avatar/meta.discourse.org/bruce1/{size}/5245.png"
},
{
id: 3,
username: "CodingHorror",
avatar_template:
"/user_avatar/meta.discourse.org/codinghorror/{size}/5245.png"
}
]
}
});
});
],
primary_groups: [],
topic_list: {
can_create_topic: true,
draft: null,
draft_key: "new_topic",
draft_sequence: 0,
per_page: 30,
topics: [
{
id: 12199,
title: "This is a private message 1",
fancy_title: "This is a private message 1",
slug: "this-is-a-private-message-1",
posts_count: 0,
reply_count: 0,
highest_post_number: 0,
image_url: null,
created_at: "2018-03-16T03:38:45.583Z",
last_posted_at: null,
bumped: true,
bumped_at: "2018-03-16T03:38:45.583Z",
unseen: false,
pinned: false,
unpinned: null,
visible: true,
closed: false,
archived: false,
bookmarked: null,
liked: null,
views: 0,
like_count: 0,
has_summary: false,
archetype: "private_message",
last_poster_username: "bruce1",
category_id: null,
pinned_globally: false,
featured_link: null,
posters: [
{
extras: "latest single",
description: "Original Poster, Most Recent Poster",
user_id: 2,
primary_group_id: null
}
],
participants: [
{
extras: "latest",
description: null,
user_id: 2,
primary_group_id: null
},
{
extras: null,
description: null,
user_id: 3,
primary_group_id: null
}
]
}
]
}
});
}
);
await visit("/g/discourse");
await click(".nav-pills li a[title='Messages']");
@@ -1,16 +1,16 @@
import { acceptance } from "helpers/qunit-helpers";
import pretender from "helpers/create-pretender";
acceptance("Keyboard Shortcuts", { loggedIn: true });
test("go to first suggested topic", async assert => {
/* global server */
server.get("/t/27331/4.json", () => [
pretender.get("/t/27331/4.json", () => [
200,
{ "Content-Type": "application/json" },
{}
]);
server.get("/t/27331.json", () => [
pretender.get("/t/27331.json", () => [
200,
{ "Content-Type": "application/json" },
{}
@@ -20,7 +20,7 @@ test("go to first suggested topic", async assert => {
* No suggested topics exist.
*/
server.get("/t/9/last.json", () => [
pretender.get("/t/9/last.json", () => [
200,
{ "Content-Type": "application/json" },
{}
@@ -44,7 +44,7 @@ test("go to first suggested topic", async assert => {
* Suggested topic is returned by server.
*/
server.get("/t/28830/last.json", () => [
pretender.get("/t/28830/last.json", () => [
200,
{ "Content-Type": "application/json" },
{
@@ -1,4 +1,5 @@
import { acceptance } from "helpers/qunit-helpers";
import pretender from "helpers/create-pretender";
acceptance("Login with email - hide email address taken", {
settings: {
@@ -10,8 +11,7 @@ acceptance("Login with email - hide email address taken", {
return [200, { "Content-Type": "application/json" }, object];
};
// prettier-ignore
server.post("/u/email-login", () => { // eslint-disable-line no-undef
pretender.post("/u/email-login", () => {
return response({ success: "OK" });
});
}
+7 -48
View File
@@ -1,4 +1,6 @@
import { updateCurrentUser, acceptance } from "helpers/qunit-helpers";
import pretender from "helpers/create-pretender";
acceptance("Tags", { loggedIn: true });
QUnit.test("list the tags", async assert => {
@@ -19,48 +21,6 @@ acceptance("Tags listed by group", {
});
QUnit.test("list the tags in groups", async assert => {
// prettier-ignore
server.get("/tags", () => { // eslint-disable-line no-undef
return [
200,
{ "Content-Type": "application/json" },
{
tags: [
{ id: "planned", text: "planned", count: 7, pm_count: 0 },
{ id: "private", text: "private", count: 0, pm_count: 7 }
],
extras: {
tag_groups: [
{
id: 2,
name: "Ford Cars",
tags: [
{ id: "Escort", text: "Escort", count: 1, pm_count: 0 },
{ id: "focus", text: "focus", count: 3, pm_count: 0 }
]
},
{
id: 1,
name: "Honda Cars",
tags: [
{ id: "civic", text: "civic", count: 4, pm_count: 0 },
{ id: "accord", text: "accord", count: 2, pm_count: 0 }
]
},
{
id: 1,
name: "Makes",
tags: [
{ id: "ford", text: "ford", count: 5, pm_count: 0 },
{ id: "honda", text: "honda", count: 6, pm_count: 0 }
]
}
]
}
}
];
});
await visit("/tags");
assert.equal(
$(".tag-list").length,
@@ -102,14 +62,13 @@ QUnit.test("list the tags in groups", async assert => {
});
test("new topic button is not available for staff-only tags", async assert => {
/* global server */
server.get("/tag/regular-tag/notifications", () => [
pretender.get("/tag/regular-tag/notifications", () => [
200,
{ "Content-Type": "application/json" },
{ tag_notification: { id: "regular-tag", notification_level: 1 } }
]);
server.get("/tag/regular-tag/l/latest.json", () => [
pretender.get("/tag/regular-tag/l/latest.json", () => [
200,
{ "Content-Type": "application/json" },
{
@@ -133,13 +92,13 @@ test("new topic button is not available for staff-only tags", async assert => {
}
]);
server.get("/tag/staff-only-tag/notifications", () => [
pretender.get("/tag/staff-only-tag/notifications", () => [
200,
{ "Content-Type": "application/json" },
{ tag_notification: { id: "staff-only-tag", notification_level: 1 } }
]);
server.get("/tag/staff-only-tag/l/latest.json", () => [
pretender.get("/tag/staff-only-tag/l/latest.json", () => [
200,
{ "Content-Type": "application/json" },
{
@@ -286,7 +245,7 @@ test("tag info can show synonyms", async assert => {
});
test("admin can manage tags", async assert => {
server.delete("/tag/planters/synonyms/containers", () => [
pretender.delete("/tag/planters/synonyms/containers", () => [
200,
{ "Content-Type": "application/json" },
{ success: true }
+11 -9
View File
@@ -1,10 +1,12 @@
import { acceptance } from "helpers/qunit-helpers";
import pretender from "helpers/create-pretender";
import Draft from "discourse/models/draft";
import { Promise } from "rsvp";
acceptance("User", { loggedIn: true });
QUnit.test("Invalid usernames", async assert => {
// prettier-ignore
server.get("/u/eviltrout%2F..%2F..%2F.json", () => { // eslint-disable-line no-undef
pretender.get("/u/eviltrout%2F..%2F..%2F.json", () => {
return [400, { "Content-Type": "application/json" }, {}];
});
@@ -67,13 +69,12 @@ QUnit.test("Viewing Summary", async assert => {
});
QUnit.test("Viewing Drafts", async assert => {
// prettier-ignore
server.get("/draft.json", () => { // eslint-disable-line no-undef
return [ 200, { "Content-Type": "application/json" }, {
draft: "{\"reply\":\"This is a draft of the first post\",\"action\":\"reply\",\"categoryId\":1,\"archetypeId\":\"regular\",\"metaData\":null,\"composerTime\":2863,\"typingTime\":200}",
draft_sequence: 42
} ];
});
sandbox.stub(Draft, "get").returns(
Promise.resolve({
draft: null,
draft_sequence: 0
})
);
await visit("/u/eviltrout/activity/drafts");
assert.ok(exists(".user-stream"), "has drafts stream");
@@ -87,4 +88,5 @@ QUnit.test("Viewing Drafts", async assert => {
exists(".d-editor-input"),
"composer is visible after resuming a draft"
);
sandbox.restore();
});