From bc2067898eef094865660e68ddb2525602631ddc Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 13 Nov 2019 15:55:32 -0500 Subject: [PATCH] FIX: Missing User objects in Utilities --- .../javascripts/discourse/lib/utilities.js.es6 | 16 +++++++++------- test/javascripts/controllers/topic-test.js.es6 | 5 +++-- test/javascripts/helpers/component-test.js.es6 | 3 ++- .../lib/click-track-edit-history-test.js.es6 | 3 ++- test/javascripts/lib/click-track-test.js.es6 | 11 ++++++----- test/javascripts/lib/utilities-test.js.es6 | 11 ++++++----- test/javascripts/models/post-stream-test.js.es6 | 11 ++++++----- test/javascripts/models/post-test.js.es6 | 5 +++-- .../javascripts/models/topic-details-test.js.es6 | 4 +++- test/javascripts/models/topic-test.js.es6 | 5 +++-- test/javascripts/models/user-drafts-test.js.es6 | 3 ++- test/javascripts/models/user-stream-test.js.es6 | 7 ++++--- 12 files changed, 49 insertions(+), 35 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/utilities.js.es6 b/app/assets/javascripts/discourse/lib/utilities.js.es6 index bdc7b59d7c6..599c3a4965a 100644 --- a/app/assets/javascripts/discourse/lib/utilities.js.es6 +++ b/app/assets/javascripts/discourse/lib/utilities.js.es6 @@ -1,6 +1,5 @@ import { escape } from "pretty-text/sanitizer"; import toMarkdown from "discourse/lib/to-markdown"; -import User from "discourse/models/user"; const homepageSelector = "meta[name=discourse_current_homepage]"; @@ -239,7 +238,7 @@ export function validateUploadedFile(file, opts) { // check that the uploaded file is authorized if (opts.allowStaffToUploadAnyFileInPm && opts.isPrivateMessage) { - if (User.currentProp("staff")) { + if (Discourse.User.currentProp("staff")) { return true; } } @@ -271,7 +270,7 @@ export function validateUploadedFile(file, opts) { if (!opts.bypassNewUserRestriction) { // ensures that new users can upload a file - if (!User.current().isAllowedToUploadAFile(opts.type)) { + if (!Discourse.User.current().isAllowedToUploadAFile(opts.type)) { bootbox.alert( I18n.t(`post.errors.${opts.type}_upload_not_allowed_for_new_user`) ); @@ -305,7 +304,7 @@ function staffExtensions() { function imagesExtensions() { let exts = extensions().filter(ext => IMAGES_EXTENSIONS_REGEX.test(ext)); - if (User.currentProp("staff")) { + if (Discourse.User.currentProp("staff")) { const staffExts = staffExtensions().filter(ext => IMAGES_EXTENSIONS_REGEX.test(ext) ); @@ -327,7 +326,10 @@ function staffExtensionsRegex() { } function isAuthorizedFile(fileName) { - if (User.currentProp("staff") && staffExtensionsRegex().test(fileName)) { + if ( + Discourse.User.currentProp("staff") && + staffExtensionsRegex().test(fileName) + ) { return true; } return extensionsRegex().test(fileName); @@ -338,7 +340,7 @@ function isAuthorizedImage(fileName) { } export function authorizedExtensions() { - const exts = User.currentProp("staff") + const exts = Discourse.User.currentProp("staff") ? [...extensions(), ...staffExtensions()] : extensions(); return exts.filter(ext => ext.length > 0).join(", "); @@ -354,7 +356,7 @@ export function authorizesAllExtensions() { return ( Discourse.SiteSettings.authorized_extensions.indexOf("*") >= 0 || (Discourse.SiteSettings.authorized_extensions_for_staff.indexOf("*") >= 0 && - User.currentProp("staff")) + Discourse.User.currentProp("staff")) ); } diff --git a/test/javascripts/controllers/topic-test.js.es6 b/test/javascripts/controllers/topic-test.js.es6 index 695a2042911..e48105a67c3 100644 --- a/test/javascripts/controllers/topic-test.js.es6 +++ b/test/javascripts/controllers/topic-test.js.es6 @@ -3,6 +3,7 @@ import { next } from "@ember/runloop"; import Topic from "discourse/models/topic"; import PostStream from "discourse/models/post-stream"; import { Placeholder } from "discourse/lib/posts-with-placeholders"; +import User from "discourse/models/user"; moduleFor("controller:topic", "controller:topic", { needs: [ @@ -223,7 +224,7 @@ QUnit.test("canDeleteSelected", function(assert) { ], stream: [1, 2, 3] }; - const currentUser = Discourse.User.create({ admin: false }); + const currentUser = User.create({ admin: false }); this.registry.register("current-user:main", currentUser, { instantiate: false }); @@ -316,7 +317,7 @@ QUnit.test("Can split/merge topic", 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, { instantiate: false }); diff --git a/test/javascripts/helpers/component-test.js.es6 b/test/javascripts/helpers/component-test.js.es6 index 5a571d45cd8..4d6256a784c 100644 --- a/test/javascripts/helpers/component-test.js.es6 +++ b/test/javascripts/helpers/component-test.js.es6 @@ -2,6 +2,7 @@ import EmberObject from "@ember/object"; import createStore from "helpers/create-store"; import { autoLoadModules } from "discourse/initializers/auto-load-modules"; import TopicTrackingState from "discourse/models/topic-tracking-state"; +import User from "discourse/models/user"; export default function(name, opts) { opts = opts || {}; @@ -29,7 +30,7 @@ export default function(name, opts) { const store = createStore(); if (!opts.anonymous) { - const currentUser = Discourse.User.create({ username: "eviltrout" }); + const currentUser = User.create({ username: "eviltrout" }); this.currentUser = currentUser; this.registry.register("current-user:main", this.currentUser, { instantiate: false diff --git a/test/javascripts/lib/click-track-edit-history-test.js.es6 b/test/javascripts/lib/click-track-edit-history-test.js.es6 index 3212cd13092..745d7d195c1 100644 --- a/test/javascripts/lib/click-track-edit-history-test.js.es6 +++ b/test/javascripts/lib/click-track-edit-history-test.js.es6 @@ -1,6 +1,7 @@ import DiscourseURL from "discourse/lib/url"; import ClickTrack from "discourse/lib/click-track"; import { fixture, logIn } from "helpers/qunit-helpers"; +import User from "discourse/models/user"; QUnit.module("lib:click-track-edit-history", { beforeEach() { @@ -93,7 +94,7 @@ QUnit.skip( "tracks external URLs when opening in another window", async assert => { assert.expect(3); - Discourse.User.currentProp("external_links_in_new_tab", true); + User.currentProp("external_links_in_new_tab", true); const done = assert.async(); /* global server */ diff --git a/test/javascripts/lib/click-track-test.js.es6 b/test/javascripts/lib/click-track-test.js.es6 index a40e803bd2e..7dd0747e723 100644 --- a/test/javascripts/lib/click-track-test.js.es6 +++ b/test/javascripts/lib/click-track-test.js.es6 @@ -2,6 +2,7 @@ import { later } from "@ember/runloop"; import DiscourseURL from "discourse/lib/url"; import ClickTrack from "discourse/lib/click-track"; import { fixture, logIn } from "helpers/qunit-helpers"; +import User from "discourse/models/user"; QUnit.module("lib:click-track", { beforeEach() { @@ -104,7 +105,7 @@ QUnit.skip( "tracks external URLs when opening in another window", async assert => { assert.expect(3); - Discourse.User.currentProp("external_links_in_new_tab", true); + User.currentProp("external_links_in_new_tab", true); const done = assert.async(); /* 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 => { - 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.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 => { - Discourse.User.currentProp("external_links_in_new_tab", true); + User.currentProp("external_links_in_new_tab", true); 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 => { sandbox - .stub(Discourse.User, "currentProp") + .stub(User, "currentProp") .withArgs("id") .returns(314); 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 => { sandbox - .stub(Discourse.User, "currentProp") + .stub(User, "currentProp") .withArgs("id") .returns(3141); badgeClickCount(assert, "with-badge-but-not-mine", 1); diff --git a/test/javascripts/lib/utilities-test.js.es6 b/test/javascripts/lib/utilities-test.js.es6 index a11264c8029..350be246960 100644 --- a/test/javascripts/lib/utilities-test.js.es6 +++ b/test/javascripts/lib/utilities-test.js.es6 @@ -17,6 +17,7 @@ import { setCaretPosition, fillMissingDates } from "discourse/lib/utilities"; +import User from "discourse/models/user"; import * as Utilities from "discourse/lib/utilities"; QUnit.module("lib:utilities"); @@ -72,7 +73,7 @@ QUnit.test("uploading one file", assert => { QUnit.test("new user cannot upload images", assert => { Discourse.SiteSettings.newuser_max_images = 0; - Discourse.User.resetCurrent(Discourse.User.create()); + User.resetCurrent(User.create()); sandbox.stub(bootbox, "alert"); 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 => { Discourse.SiteSettings.newuser_max_attachments = 0; sandbox.stub(bootbox, "alert"); - Discourse.User.resetCurrent(Discourse.User.create()); + User.resetCurrent(User.create()); assert.not(validUpload([{ name: "roman.txt" }])); assert.ok( @@ -120,7 +121,7 @@ QUnit.test("skipping validation works", assert => { QUnit.test("staff can upload anything in PM", assert => { const files = [{ name: "some.docx" }]; Discourse.SiteSettings.authorized_extensions = "jpeg"; - Discourse.User.resetCurrent(Discourse.User.create({ moderator: true })); + User.resetCurrent(User.create({ moderator: true })); sandbox.stub(bootbox, "alert"); @@ -151,8 +152,8 @@ var dummyBlob = function() { }; QUnit.test("allows valid uploads to go through", assert => { - Discourse.User.resetCurrent(Discourse.User.create()); - Discourse.User.currentProp("trust_level", 1); + User.resetCurrent(User.create()); + User.currentProp("trust_level", 1); sandbox.stub(bootbox, "alert"); // image diff --git a/test/javascripts/models/post-stream-test.js.es6 b/test/javascripts/models/post-stream-test.js.es6 index 49b6018e203..fb90fdb8b41 100644 --- a/test/javascripts/models/post-stream-test.js.es6 +++ b/test/javascripts/models/post-stream-test.js.es6 @@ -1,5 +1,6 @@ import Post from "discourse/models/post"; import createStore from "helpers/create-store"; +import User from "discourse/models/user"; QUnit.module("model:post-stream"); @@ -549,7 +550,7 @@ QUnit.test("staging and undoing a new post", assert => { "the original post is lastAppended" ); - const user = Discourse.User.create({ + const user = User.create({ username: "eviltrout", name: "eviltrout", id: 321 @@ -650,7 +651,7 @@ QUnit.test("staging and committing a post", assert => { "the original post is lastAppended" ); - const user = Discourse.User.create({ + const user = User.create({ username: "eviltrout", name: "eviltrout", id: 321 @@ -772,7 +773,7 @@ QUnit.test("comitting and triggerNewPostInStream race condition", assert => { const store = postStream.store; postStream.appendPost(store.createRecord("post", { id: 1, post_number: 1 })); - const user = Discourse.User.create({ + const user = User.create({ username: "eviltrout", name: "eviltrout", id: 321 @@ -804,8 +805,8 @@ QUnit.test("comitting and triggerNewPostInStream race condition", assert => { QUnit.test("triggerNewPostInStream for ignored posts", async assert => { const postStream = buildStream(280, [1]); const store = postStream.store; - Discourse.User.resetCurrent( - Discourse.User.create({ + User.resetCurrent( + User.create({ username: "eviltrout", name: "eviltrout", id: 321, diff --git a/test/javascripts/models/post-test.js.es6 b/test/javascripts/models/post-test.js.es6 index 5a7aa345202..a96c32bb915 100644 --- a/test/javascripts/models/post-test.js.es6 +++ b/test/javascripts/models/post-test.js.es6 @@ -1,4 +1,5 @@ import Post from "discourse/models/post"; +import User from "discourse/models/user"; QUnit.module("model: Post"); @@ -56,7 +57,7 @@ QUnit.test("updateFromPost", 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.destroy(user); @@ -81,7 +82,7 @@ QUnit.test("destroy by staff", assert => { QUnit.test("destroy by non-staff", assert => { 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 }); return post.destroy(user).then(() => { diff --git a/test/javascripts/models/topic-details-test.js.es6 b/test/javascripts/models/topic-details-test.js.es6 index 35534faf696..cdca4e1fc60 100644 --- a/test/javascripts/models/topic-details-test.js.es6 +++ b/test/javascripts/models/topic-details-test.js.es6 @@ -1,3 +1,5 @@ +import User from "discourse/models/user"; + QUnit.module("model:topic-details"); import Topic from "discourse/models/topic"; @@ -25,5 +27,5 @@ QUnit.test("updateFromJson", assert => { 1, "it loaded the allowed users" ); - assert.containsInstance(details.get("allowed_users"), Discourse.User); + assert.containsInstance(details.get("allowed_users"), User); }); diff --git a/test/javascripts/models/topic-test.js.es6 b/test/javascripts/models/topic-test.js.es6 index 40ea25f854a..281d621e702 100644 --- a/test/javascripts/models/topic-test.js.es6 +++ b/test/javascripts/models/topic-test.js.es6 @@ -2,6 +2,7 @@ import EmberObject from "@ember/object"; import { IMAGE_VERSION as v } from "pretty-text/emoji/version"; import Category from "discourse/models/category"; import Topic from "discourse/models/topic"; +import User from "discourse/models/user"; QUnit.module("model:topic"); @@ -108,7 +109,7 @@ QUnit.test("updateFromJson", assert => { }); QUnit.test("destroy", assert => { - const user = Discourse.User.create({ username: "eviltrout" }); + const user = User.create({ username: "eviltrout" }); const topic = Topic.create({ id: 1234 }); topic.destroy(user); @@ -117,7 +118,7 @@ QUnit.test("destroy", assert => { }); QUnit.test("recover", assert => { - const user = Discourse.User.create({ username: "eviltrout" }); + const user = User.create({ username: "eviltrout" }); const topic = Topic.create({ id: 1234, deleted_at: new Date(), diff --git a/test/javascripts/models/user-drafts-test.js.es6 b/test/javascripts/models/user-drafts-test.js.es6 index 895bdce5508..20f68718df2 100644 --- a/test/javascripts/models/user-drafts-test.js.es6 +++ b/test/javascripts/models/user-drafts-test.js.es6 @@ -1,10 +1,11 @@ import UserDraft from "discourse/models/user-draft"; import { NEW_TOPIC_KEY } from "discourse/models/composer"; +import User from "discourse/models/user"; QUnit.module("model:user-drafts"); 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"); assert.present(stream, "a user has a drafts stream by default"); assert.equal(stream.get("itemsLoaded"), 0, "no items are loaded by default"); diff --git a/test/javascripts/models/user-stream-test.js.es6 b/test/javascripts/models/user-stream-test.js.es6 index a0fef540142..58f57b9501f 100644 --- a/test/javascripts/models/user-stream-test.js.es6 +++ b/test/javascripts/models/user-stream-test.js.es6 @@ -1,9 +1,10 @@ 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 => { - var user = Discourse.User.create({ id: 1, username: "eviltrout" }); + var user = User.create({ id: 1, username: "eviltrout" }); var stream = user.get("stream"); assert.present(stream, "a user has a stream by default"); 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 => { - var user = Discourse.User.create({ id: 1, username: "eviltrout" }); + var user = User.create({ id: 1, username: "eviltrout" }); var stream = user.get("stream"); // defaults to posts/topics