FIX: Invite acceptance tests were broken in Ember CLI (#12367)

* FIX: Invite acceptance tests were broken in Ember CLI

They relied on old Ember behavior where the app does not boot until
`visit` is called and this is no longer true.

This refactors the test to DRY stuff up a bit, and modify the DOM where
necessary in `needs.hooks.beforeEach`.

* Update app/assets/javascripts/discourse/tests/acceptance/invite-accept-test.js

Co-authored-by: Jarek Radosz <jradosz@gmail.com>

Co-authored-by: Jarek Radosz <jradosz@gmail.com>
This commit is contained in:
Robin Ward 2021-03-12 09:00:39 -05:00 committed by GitHub
parent 1533cbb38b
commit c60cdab1fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 104 additions and 97 deletions

View File

@ -8,6 +8,43 @@ import PreloadStore from "discourse/lib/preload-store";
import I18n from "I18n"; import I18n from "I18n";
import { test } from "qunit"; import { test } from "qunit";
function setAuthenticationData(hooks, json) {
hooks.beforeEach(() => {
const node = document.createElement("meta");
node.dataset.authenticationData = JSON.stringify(json);
node.id = "data-authentication";
document.querySelector("head").appendChild(node);
});
hooks.afterEach(() => {
document
.querySelector("head")
.removeChild(document.getElementById("data-authentication"));
});
}
function preloadInvite({ link = false } = {}) {
const info = {
invited_by: {
id: 123,
username: "foobar",
avatar_template: "/user_avatar/localhost/neil/{size}/25_1.png",
name: "foobar",
title: "team",
},
username: "invited",
};
if (link) {
info.email = "null";
info.is_invite_link = true;
} else {
info.email = "foobar@example.com";
info.is_invite_link = false;
}
PreloadStore.store("invite_info", info);
}
acceptance("Invite accept", function (needs) { acceptance("Invite accept", function (needs) {
needs.settings({ full_name_required: true }); needs.settings({ full_name_required: true });
@ -115,31 +152,8 @@ acceptance("Invite accept", function (needs) {
acceptance("Invite accept when local login is disabled", function (needs) { acceptance("Invite accept when local login is disabled", function (needs) {
needs.settings({ enable_local_logins: false }); needs.settings({ enable_local_logins: false });
const preloadStore = function (isInviteLink) {
const info = {
invited_by: {
id: 123,
username: "foobar",
avatar_template: "/user_avatar/localhost/neil/{size}/25_1.png",
name: "foobar",
title: "team",
},
username: "invited",
};
if (isInviteLink) {
info.email = "null";
info.is_invite_link = true;
} else {
info.email = "foobar@example.com";
info.is_invite_link = false;
}
PreloadStore.store("invite_info", info);
};
test("invite link", async function (assert) { test("invite link", async function (assert) {
preloadStore(true); preloadInvite({ link: true });
await visit("/invites/myvalidinvitetoken"); await visit("/invites/myvalidinvitetoken");
@ -147,20 +161,28 @@ acceptance("Invite accept when local login is disabled", function (needs) {
assert.ok(!exists("form"), "does not display the form"); assert.ok(!exists("form"), "does not display the form");
}); });
test("invite link with authentication data", async function (assert) { test("email invite link", async function (assert) {
preloadStore(true); preloadInvite();
await visit("/invites/myvalidinvitetoken");
// Simulate authticated with Facebook assert.ok(exists(".btn-social.facebook"), "shows Facebook login button");
const node = document.createElement("meta"); assert.ok(!exists("form"), "does not display the form");
node.dataset.authenticationData = JSON.stringify({ });
auth_provider: "facebook", });
email: "blah@example.com",
email_valid: true, acceptance("Invite link with authentication data", function (needs) {
username: "foobar", needs.settings({ enable_local_logins: false });
name: "barfoo",
}); setAuthenticationData(needs.hooks, {
node.id = "data-authentication"; auth_provider: "facebook",
document.querySelector("head").appendChild(node); email: "blah@example.com",
email_valid: true,
username: "foobar",
name: "barfoo",
});
test("form elements and buttons are correct ", async function (assert) {
preloadInvite({ link: true });
await visit("/invites/myvalidinvitetoken"); await visit("/invites/myvalidinvitetoken");
@ -192,35 +214,22 @@ acceptance("Invite accept when local login is disabled", function (needs) {
"barfoo", "barfoo",
"name is prefilled" "name is prefilled"
); );
document
.querySelector("head")
.removeChild(document.getElementById("data-authentication"));
}); });
});
test("email invite link", async function (assert) { acceptance("Email Invite link with authentication data", function (needs) {
preloadStore(false); needs.settings({ enable_local_logins: false });
await visit("/invites/myvalidinvitetoken"); setAuthenticationData(needs.hooks, {
auth_provider: "facebook",
assert.ok(exists(".btn-social.facebook"), "shows Facebook login button"); email: "blah@example.com",
assert.ok(!exists("form"), "does not display the form"); email_valid: true,
username: "foobar",
name: "barfoo",
}); });
test("email invite link with authentication data when email does not match", async function (assert) { test("email invite link with authentication data when email does not match", async function (assert) {
preloadStore(false); preloadInvite();
// Simulate authticated with Facebook
const node = document.createElement("meta");
node.dataset.authenticationData = JSON.stringify({
auth_provider: "facebook",
email: "blah@example.com",
email_valid: true,
username: "foobar",
name: "barfoo",
});
node.id = "data-authentication";
document.querySelector("head").appendChild(node);
await visit("/invites/myvalidinvitetoken"); await visit("/invites/myvalidinvitetoken");
@ -230,56 +239,54 @@ acceptance("Invite accept when local login is disabled", function (needs) {
); );
assert.ok(!exists("form"), "does not display the form"); assert.ok(!exists("form"), "does not display the form");
document
.querySelector("head")
.removeChild(document.getElementById("data-authentication"));
}); });
});
test("email invite link with authentication data", async function (assert) { acceptance(
preloadStore(false); "Email Invite link with valid authentication data",
function (needs) {
needs.settings({ enable_local_logins: false });
// Simulate authticated with Facebook setAuthenticationData(needs.hooks, {
const node = document.createElement("meta");
node.dataset.authenticationData = JSON.stringify({
auth_provider: "facebook", auth_provider: "facebook",
email: "foobar@example.com", email: "foobar@example.com",
email_valid: true, email_valid: true,
username: "foobar", username: "foobar",
name: "barfoo", name: "barfoo",
}); });
node.id = "data-authentication";
document.querySelector("head").appendChild(node);
await visit("/invites/myvalidinvitetoken"); test("confirm form and buttons", async function (assert) {
preloadInvite();
assert.ok( await visit("/invites/myvalidinvitetoken");
!exists(".btn-social.facebook"),
"does not show Facebook login button"
);
assert.ok(!exists("#new-account-password"), "does not show password field"); assert.ok(
assert.ok(!exists("#new-account-email"), "does not show email field"); !exists(".btn-social.facebook"),
"does not show Facebook login button"
);
assert.equal( assert.ok(
queryAll("#account-email-validation").text().trim(), !exists("#new-account-password"),
I18n.t("user.email.authenticated", { provider: "Facebook" }) "does not show password field"
); );
assert.ok(!exists("#new-account-email"), "does not show email field");
assert.equal( assert.equal(
queryAll("#new-account-username").val(), queryAll("#account-email-validation").text().trim(),
"foobar", I18n.t("user.email.authenticated", { provider: "Facebook" })
"username is prefilled" );
);
assert.equal( assert.equal(
queryAll("#new-account-name").val(), queryAll("#new-account-username").val(),
"barfoo", "foobar",
"name is prefilled" "username is prefilled"
); );
document assert.equal(
.querySelector("head") queryAll("#new-account-name").val(),
.removeChild(document.getElementById("data-authentication")); "barfoo",
}); "name is prefilled"
}); );
});
}
);