FIX: Missing User objects in Utilities

This commit is contained in:
Robin Ward
2019-11-13 15:55:32 -05:00
parent f5ed0dc2e6
commit bc2067898e
12 changed files with 49 additions and 35 deletions

View File

@ -1,6 +1,5 @@
import { escape } from "pretty-text/sanitizer"; import { escape } from "pretty-text/sanitizer";
import toMarkdown from "discourse/lib/to-markdown"; import toMarkdown from "discourse/lib/to-markdown";
import User from "discourse/models/user";
const homepageSelector = "meta[name=discourse_current_homepage]"; const homepageSelector = "meta[name=discourse_current_homepage]";
@ -239,7 +238,7 @@ export function validateUploadedFile(file, opts) {
// check that the uploaded file is authorized // check that the uploaded file is authorized
if (opts.allowStaffToUploadAnyFileInPm && opts.isPrivateMessage) { if (opts.allowStaffToUploadAnyFileInPm && opts.isPrivateMessage) {
if (User.currentProp("staff")) { if (Discourse.User.currentProp("staff")) {
return true; return true;
} }
} }
@ -271,7 +270,7 @@ export function validateUploadedFile(file, opts) {
if (!opts.bypassNewUserRestriction) { if (!opts.bypassNewUserRestriction) {
// ensures that new users can upload a file // ensures that new users can upload a file
if (!User.current().isAllowedToUploadAFile(opts.type)) { if (!Discourse.User.current().isAllowedToUploadAFile(opts.type)) {
bootbox.alert( bootbox.alert(
I18n.t(`post.errors.${opts.type}_upload_not_allowed_for_new_user`) I18n.t(`post.errors.${opts.type}_upload_not_allowed_for_new_user`)
); );
@ -305,7 +304,7 @@ function staffExtensions() {
function imagesExtensions() { function imagesExtensions() {
let exts = extensions().filter(ext => IMAGES_EXTENSIONS_REGEX.test(ext)); let exts = extensions().filter(ext => IMAGES_EXTENSIONS_REGEX.test(ext));
if (User.currentProp("staff")) { if (Discourse.User.currentProp("staff")) {
const staffExts = staffExtensions().filter(ext => const staffExts = staffExtensions().filter(ext =>
IMAGES_EXTENSIONS_REGEX.test(ext) IMAGES_EXTENSIONS_REGEX.test(ext)
); );
@ -327,7 +326,10 @@ function staffExtensionsRegex() {
} }
function isAuthorizedFile(fileName) { function isAuthorizedFile(fileName) {
if (User.currentProp("staff") && staffExtensionsRegex().test(fileName)) { if (
Discourse.User.currentProp("staff") &&
staffExtensionsRegex().test(fileName)
) {
return true; return true;
} }
return extensionsRegex().test(fileName); return extensionsRegex().test(fileName);
@ -338,7 +340,7 @@ function isAuthorizedImage(fileName) {
} }
export function authorizedExtensions() { export function authorizedExtensions() {
const exts = User.currentProp("staff") const exts = Discourse.User.currentProp("staff")
? [...extensions(), ...staffExtensions()] ? [...extensions(), ...staffExtensions()]
: extensions(); : extensions();
return exts.filter(ext => ext.length > 0).join(", "); return exts.filter(ext => ext.length > 0).join(", ");
@ -354,7 +356,7 @@ export function authorizesAllExtensions() {
return ( return (
Discourse.SiteSettings.authorized_extensions.indexOf("*") >= 0 || Discourse.SiteSettings.authorized_extensions.indexOf("*") >= 0 ||
(Discourse.SiteSettings.authorized_extensions_for_staff.indexOf("*") >= 0 && (Discourse.SiteSettings.authorized_extensions_for_staff.indexOf("*") >= 0 &&
User.currentProp("staff")) Discourse.User.currentProp("staff"))
); );
} }

View File

@ -3,6 +3,7 @@ import { next } from "@ember/runloop";
import Topic from "discourse/models/topic"; import Topic from "discourse/models/topic";
import PostStream from "discourse/models/post-stream"; import PostStream from "discourse/models/post-stream";
import { Placeholder } from "discourse/lib/posts-with-placeholders"; import { Placeholder } from "discourse/lib/posts-with-placeholders";
import User from "discourse/models/user";
moduleFor("controller:topic", "controller:topic", { moduleFor("controller:topic", "controller:topic", {
needs: [ needs: [
@ -223,7 +224,7 @@ QUnit.test("canDeleteSelected", function(assert) {
], ],
stream: [1, 2, 3] stream: [1, 2, 3]
}; };
const currentUser = Discourse.User.create({ admin: false }); const currentUser = User.create({ admin: false });
this.registry.register("current-user:main", currentUser, { this.registry.register("current-user:main", currentUser, {
instantiate: false instantiate: false
}); });
@ -316,7 +317,7 @@ QUnit.test("Can split/merge topic", function(assert) {
}); });
QUnit.test("canChangeOwner", function(assert) { QUnit.test("canChangeOwner", function(assert) {
const currentUser = Discourse.User.create({ admin: false }); const currentUser = User.create({ admin: false });
this.registry.register("current-user:main", currentUser, { this.registry.register("current-user:main", currentUser, {
instantiate: false instantiate: false
}); });

View File

@ -2,6 +2,7 @@ import EmberObject from "@ember/object";
import createStore from "helpers/create-store"; import createStore from "helpers/create-store";
import { autoLoadModules } from "discourse/initializers/auto-load-modules"; import { autoLoadModules } from "discourse/initializers/auto-load-modules";
import TopicTrackingState from "discourse/models/topic-tracking-state"; import TopicTrackingState from "discourse/models/topic-tracking-state";
import User from "discourse/models/user";
export default function(name, opts) { export default function(name, opts) {
opts = opts || {}; opts = opts || {};
@ -29,7 +30,7 @@ export default function(name, opts) {
const store = createStore(); const store = createStore();
if (!opts.anonymous) { if (!opts.anonymous) {
const currentUser = Discourse.User.create({ username: "eviltrout" }); const currentUser = User.create({ username: "eviltrout" });
this.currentUser = currentUser; this.currentUser = currentUser;
this.registry.register("current-user:main", this.currentUser, { this.registry.register("current-user:main", this.currentUser, {
instantiate: false instantiate: false

View File

@ -1,6 +1,7 @@
import DiscourseURL from "discourse/lib/url"; import DiscourseURL from "discourse/lib/url";
import ClickTrack from "discourse/lib/click-track"; import ClickTrack from "discourse/lib/click-track";
import { fixture, logIn } from "helpers/qunit-helpers"; import { fixture, logIn } from "helpers/qunit-helpers";
import User from "discourse/models/user";
QUnit.module("lib:click-track-edit-history", { QUnit.module("lib:click-track-edit-history", {
beforeEach() { beforeEach() {
@ -93,7 +94,7 @@ QUnit.skip(
"tracks external URLs when opening in another window", "tracks external URLs when opening in another window",
async assert => { async assert => {
assert.expect(3); assert.expect(3);
Discourse.User.currentProp("external_links_in_new_tab", true); User.currentProp("external_links_in_new_tab", true);
const done = assert.async(); const done = assert.async();
/* global server */ /* global server */

View File

@ -2,6 +2,7 @@ import { later } from "@ember/runloop";
import DiscourseURL from "discourse/lib/url"; import DiscourseURL from "discourse/lib/url";
import ClickTrack from "discourse/lib/click-track"; import ClickTrack from "discourse/lib/click-track";
import { fixture, logIn } from "helpers/qunit-helpers"; import { fixture, logIn } from "helpers/qunit-helpers";
import User from "discourse/models/user";
QUnit.module("lib:click-track", { QUnit.module("lib:click-track", {
beforeEach() { beforeEach() {
@ -104,7 +105,7 @@ QUnit.skip(
"tracks external URLs when opening in another window", "tracks external URLs when opening in another window",
async assert => { async assert => {
assert.expect(3); assert.expect(3);
Discourse.User.currentProp("external_links_in_new_tab", true); User.currentProp("external_links_in_new_tab", true);
const done = assert.async(); const done = assert.async();
/* global server */ /* global server */
@ -140,7 +141,7 @@ QUnit.skip("does not track right clicks inside quotes", async assert => {
}); });
QUnit.skip("does not track clicks links in quotes", async assert => { QUnit.skip("does not track clicks links in quotes", async assert => {
Discourse.User.currentProp("external_links_in_new_tab", true); User.currentProp("external_links_in_new_tab", true);
assert.notOk(track(generateClickEventOn(".quote a:last-child"))); assert.notOk(track(generateClickEventOn(".quote a:last-child")));
assert.ok(window.open.calledWith("https://google.com", "_blank")); assert.ok(window.open.calledWith("https://google.com", "_blank"));
}); });
@ -154,7 +155,7 @@ QUnit.skip("does not track clicks on mailto", async assert => {
}); });
QUnit.skip("removes the href and put it as a data attribute", async assert => { QUnit.skip("removes the href and put it as a data attribute", async assert => {
Discourse.User.currentProp("external_links_in_new_tab", true); User.currentProp("external_links_in_new_tab", true);
assert.notOk(track(generateClickEventOn("a"))); assert.notOk(track(generateClickEventOn("a")));
@ -188,7 +189,7 @@ function badgeClickCount(assert, id, expected) {
QUnit.skip("does not update badge clicks on my own link", async assert => { QUnit.skip("does not update badge clicks on my own link", async assert => {
sandbox sandbox
.stub(Discourse.User, "currentProp") .stub(User, "currentProp")
.withArgs("id") .withArgs("id")
.returns(314); .returns(314);
badgeClickCount(assert, "with-badge", 1); badgeClickCount(assert, "with-badge", 1);
@ -196,7 +197,7 @@ QUnit.skip("does not update badge clicks on my own link", async assert => {
QUnit.skip("does not update badge clicks in my own post", async assert => { QUnit.skip("does not update badge clicks in my own post", async assert => {
sandbox sandbox
.stub(Discourse.User, "currentProp") .stub(User, "currentProp")
.withArgs("id") .withArgs("id")
.returns(3141); .returns(3141);
badgeClickCount(assert, "with-badge-but-not-mine", 1); badgeClickCount(assert, "with-badge-but-not-mine", 1);

View File

@ -17,6 +17,7 @@ import {
setCaretPosition, setCaretPosition,
fillMissingDates fillMissingDates
} from "discourse/lib/utilities"; } from "discourse/lib/utilities";
import User from "discourse/models/user";
import * as Utilities from "discourse/lib/utilities"; import * as Utilities from "discourse/lib/utilities";
QUnit.module("lib:utilities"); QUnit.module("lib:utilities");
@ -72,7 +73,7 @@ QUnit.test("uploading one file", assert => {
QUnit.test("new user cannot upload images", assert => { QUnit.test("new user cannot upload images", assert => {
Discourse.SiteSettings.newuser_max_images = 0; Discourse.SiteSettings.newuser_max_images = 0;
Discourse.User.resetCurrent(Discourse.User.create()); User.resetCurrent(User.create());
sandbox.stub(bootbox, "alert"); sandbox.stub(bootbox, "alert");
assert.not(validUpload([{ name: "image.png" }]), "the upload is not valid"); assert.not(validUpload([{ name: "image.png" }]), "the upload is not valid");
@ -87,7 +88,7 @@ QUnit.test("new user cannot upload images", assert => {
QUnit.test("new user cannot upload attachments", assert => { QUnit.test("new user cannot upload attachments", assert => {
Discourse.SiteSettings.newuser_max_attachments = 0; Discourse.SiteSettings.newuser_max_attachments = 0;
sandbox.stub(bootbox, "alert"); sandbox.stub(bootbox, "alert");
Discourse.User.resetCurrent(Discourse.User.create()); User.resetCurrent(User.create());
assert.not(validUpload([{ name: "roman.txt" }])); assert.not(validUpload([{ name: "roman.txt" }]));
assert.ok( assert.ok(
@ -120,7 +121,7 @@ QUnit.test("skipping validation works", assert => {
QUnit.test("staff can upload anything in PM", assert => { QUnit.test("staff can upload anything in PM", assert => {
const files = [{ name: "some.docx" }]; const files = [{ name: "some.docx" }];
Discourse.SiteSettings.authorized_extensions = "jpeg"; Discourse.SiteSettings.authorized_extensions = "jpeg";
Discourse.User.resetCurrent(Discourse.User.create({ moderator: true })); User.resetCurrent(User.create({ moderator: true }));
sandbox.stub(bootbox, "alert"); sandbox.stub(bootbox, "alert");
@ -151,8 +152,8 @@ var dummyBlob = function() {
}; };
QUnit.test("allows valid uploads to go through", assert => { QUnit.test("allows valid uploads to go through", assert => {
Discourse.User.resetCurrent(Discourse.User.create()); User.resetCurrent(User.create());
Discourse.User.currentProp("trust_level", 1); User.currentProp("trust_level", 1);
sandbox.stub(bootbox, "alert"); sandbox.stub(bootbox, "alert");
// image // image

View File

@ -1,5 +1,6 @@
import Post from "discourse/models/post"; import Post from "discourse/models/post";
import createStore from "helpers/create-store"; import createStore from "helpers/create-store";
import User from "discourse/models/user";
QUnit.module("model:post-stream"); QUnit.module("model:post-stream");
@ -549,7 +550,7 @@ QUnit.test("staging and undoing a new post", assert => {
"the original post is lastAppended" "the original post is lastAppended"
); );
const user = Discourse.User.create({ const user = User.create({
username: "eviltrout", username: "eviltrout",
name: "eviltrout", name: "eviltrout",
id: 321 id: 321
@ -650,7 +651,7 @@ QUnit.test("staging and committing a post", assert => {
"the original post is lastAppended" "the original post is lastAppended"
); );
const user = Discourse.User.create({ const user = User.create({
username: "eviltrout", username: "eviltrout",
name: "eviltrout", name: "eviltrout",
id: 321 id: 321
@ -772,7 +773,7 @@ QUnit.test("comitting and triggerNewPostInStream race condition", assert => {
const store = postStream.store; const store = postStream.store;
postStream.appendPost(store.createRecord("post", { id: 1, post_number: 1 })); postStream.appendPost(store.createRecord("post", { id: 1, post_number: 1 }));
const user = Discourse.User.create({ const user = User.create({
username: "eviltrout", username: "eviltrout",
name: "eviltrout", name: "eviltrout",
id: 321 id: 321
@ -804,8 +805,8 @@ QUnit.test("comitting and triggerNewPostInStream race condition", assert => {
QUnit.test("triggerNewPostInStream for ignored posts", async assert => { QUnit.test("triggerNewPostInStream for ignored posts", async assert => {
const postStream = buildStream(280, [1]); const postStream = buildStream(280, [1]);
const store = postStream.store; const store = postStream.store;
Discourse.User.resetCurrent( User.resetCurrent(
Discourse.User.create({ User.create({
username: "eviltrout", username: "eviltrout",
name: "eviltrout", name: "eviltrout",
id: 321, id: 321,

View File

@ -1,4 +1,5 @@
import Post from "discourse/models/post"; import Post from "discourse/models/post";
import User from "discourse/models/user";
QUnit.module("model: Post"); QUnit.module("model: Post");
@ -56,7 +57,7 @@ QUnit.test("updateFromPost", assert => {
}); });
QUnit.test("destroy by staff", assert => { QUnit.test("destroy by staff", assert => {
var user = Discourse.User.create({ username: "staff", moderator: true }), var user = User.create({ username: "staff", moderator: true }),
post = buildPost({ user: user }); post = buildPost({ user: user });
post.destroy(user); post.destroy(user);
@ -81,7 +82,7 @@ QUnit.test("destroy by staff", assert => {
QUnit.test("destroy by non-staff", assert => { QUnit.test("destroy by non-staff", assert => {
var originalCooked = "this is the original cooked value", var originalCooked = "this is the original cooked value",
user = Discourse.User.create({ username: "evil trout" }), user = User.create({ username: "evil trout" }),
post = buildPost({ user: user, cooked: originalCooked }); post = buildPost({ user: user, cooked: originalCooked });
return post.destroy(user).then(() => { return post.destroy(user).then(() => {

View File

@ -1,3 +1,5 @@
import User from "discourse/models/user";
QUnit.module("model:topic-details"); QUnit.module("model:topic-details");
import Topic from "discourse/models/topic"; import Topic from "discourse/models/topic";
@ -25,5 +27,5 @@ QUnit.test("updateFromJson", assert => {
1, 1,
"it loaded the allowed users" "it loaded the allowed users"
); );
assert.containsInstance(details.get("allowed_users"), Discourse.User); assert.containsInstance(details.get("allowed_users"), User);
}); });

View File

@ -2,6 +2,7 @@ import EmberObject from "@ember/object";
import { IMAGE_VERSION as v } from "pretty-text/emoji/version"; import { IMAGE_VERSION as v } from "pretty-text/emoji/version";
import Category from "discourse/models/category"; import Category from "discourse/models/category";
import Topic from "discourse/models/topic"; import Topic from "discourse/models/topic";
import User from "discourse/models/user";
QUnit.module("model:topic"); QUnit.module("model:topic");
@ -108,7 +109,7 @@ QUnit.test("updateFromJson", assert => {
}); });
QUnit.test("destroy", assert => { QUnit.test("destroy", assert => {
const user = Discourse.User.create({ username: "eviltrout" }); const user = User.create({ username: "eviltrout" });
const topic = Topic.create({ id: 1234 }); const topic = Topic.create({ id: 1234 });
topic.destroy(user); topic.destroy(user);
@ -117,7 +118,7 @@ QUnit.test("destroy", assert => {
}); });
QUnit.test("recover", assert => { QUnit.test("recover", assert => {
const user = Discourse.User.create({ username: "eviltrout" }); const user = User.create({ username: "eviltrout" });
const topic = Topic.create({ const topic = Topic.create({
id: 1234, id: 1234,
deleted_at: new Date(), deleted_at: new Date(),

View File

@ -1,10 +1,11 @@
import UserDraft from "discourse/models/user-draft"; import UserDraft from "discourse/models/user-draft";
import { NEW_TOPIC_KEY } from "discourse/models/composer"; import { NEW_TOPIC_KEY } from "discourse/models/composer";
import User from "discourse/models/user";
QUnit.module("model:user-drafts"); QUnit.module("model:user-drafts");
QUnit.test("stream", assert => { QUnit.test("stream", assert => {
const user = Discourse.User.create({ id: 1, username: "eviltrout" }); const user = User.create({ id: 1, username: "eviltrout" });
const stream = user.get("userDraftsStream"); const stream = user.get("userDraftsStream");
assert.present(stream, "a user has a drafts stream by default"); assert.present(stream, "a user has a drafts stream by default");
assert.equal(stream.get("itemsLoaded"), 0, "no items are loaded by default"); assert.equal(stream.get("itemsLoaded"), 0, "no items are loaded by default");

View File

@ -1,9 +1,10 @@
import UserAction from "discourse/models/user-action"; import UserAction from "discourse/models/user-action";
import User from "discourse/models/user";
QUnit.module("Discourse.UserStream"); QUnit.module("model: UserStream");
QUnit.test("basics", assert => { QUnit.test("basics", assert => {
var user = Discourse.User.create({ id: 1, username: "eviltrout" }); var user = User.create({ id: 1, username: "eviltrout" });
var stream = user.get("stream"); var stream = user.get("stream");
assert.present(stream, "a user has a stream by default"); assert.present(stream, "a user has a stream by default");
assert.equal(stream.get("user"), user, "the stream points back to the user"); assert.equal(stream.get("user"), user, "the stream points back to the user");
@ -16,7 +17,7 @@ QUnit.test("basics", assert => {
}); });
QUnit.test("filterParam", assert => { QUnit.test("filterParam", assert => {
var user = Discourse.User.create({ id: 1, username: "eviltrout" }); var user = User.create({ id: 1, username: "eviltrout" });
var stream = user.get("stream"); var stream = user.get("stream");
// defaults to posts/topics // defaults to posts/topics