mirror of
https://github.com/discourse/discourse.git
synced 2024-11-29 19:01:36 +08:00
b2acc416e7
* FIX: Use Category.secured(guardian) for hashtag datasource Follow up to comments in #19219, changing the category hashtag datasource to use Category.secured(guardian) instead of Site.new(guardian).categories here since the latter does more work for not much benefit, and the query time is the same. Also eliminates some Hash -> Model back and forth busywork. Add some more specs too. * FIX: Server-side hashtag lookup cooking user loading When we were using the PrettyText.options.currentUser and parsing back and forth with JSON for the hashtag lookups server-side, we had a bug where the user's secure categories were not loaded since we never actually loaded a User model from the database, only parsed it from JSON. This commit fixes the issue by instead using the PretyText.options.userId and looking up the user directly from the database when calling hashtag_lookup via the PrettyText::Helpers code when cooking server-side. Added the missing spec to check for this as well.
153 lines
5.2 KiB
Ruby
153 lines
5.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe PrettyText::Helpers do
|
|
describe ".lookup_upload_urls" do
|
|
let(:upload) { Fabricate(:upload) }
|
|
|
|
it "should return cdn url if available" do
|
|
short_url = upload.short_url
|
|
result = PrettyText::Helpers.lookup_upload_urls([short_url])
|
|
expect(result[short_url][:url]).to eq(upload.url)
|
|
|
|
set_cdn_url "https://awesome.com"
|
|
|
|
result = PrettyText::Helpers.lookup_upload_urls([short_url])
|
|
expect(result[short_url][:url]).to eq("https://awesome.com#{upload.url}")
|
|
end
|
|
end
|
|
|
|
describe ".category_tag_hashtag_lookup" do
|
|
fab!(:tag) { Fabricate(:tag, name: "somecooltag") }
|
|
fab!(:category) do
|
|
Fabricate(:category, name: "Some Awesome Category", slug: "someawesomecategory")
|
|
end
|
|
|
|
it "handles tags based on slug with TAG_HASHTAG_POSTFIX" do
|
|
expect(
|
|
PrettyText::Helpers.category_tag_hashtag_lookup(
|
|
+"somecooltag#{PrettyText::Helpers::TAG_HASHTAG_POSTFIX}",
|
|
),
|
|
).to eq([tag.url, "somecooltag"])
|
|
end
|
|
|
|
it "handles categories based on slug" do
|
|
expect(PrettyText::Helpers.category_tag_hashtag_lookup("someawesomecategory")).to eq(
|
|
[category.url, "someawesomecategory"],
|
|
)
|
|
end
|
|
|
|
it "handles tags based on slug without TAG_HASHTAG_POSTFIX" do
|
|
expect(PrettyText::Helpers.category_tag_hashtag_lookup(+"somecooltag")).to eq(
|
|
[tag.url, "somecooltag"],
|
|
)
|
|
end
|
|
|
|
it "returns nil when no tag or category that matches exists" do
|
|
expect(PrettyText::Helpers.category_tag_hashtag_lookup("blah")).to eq(nil)
|
|
end
|
|
end
|
|
|
|
describe ".hashtag_lookup" do
|
|
fab!(:tag) { Fabricate(:tag, name: "somecooltag", description: "Coolest things ever") }
|
|
fab!(:category) do
|
|
Fabricate(:category, name: "Some Awesome Category", slug: "someawesomecategory", description: "Really great stuff here")
|
|
end
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
it "handles tags and categories based on slug with type suffix" do
|
|
expect(PrettyText::Helpers.hashtag_lookup("somecooltag::tag", user.id, %w[category tag])).to eq(
|
|
{
|
|
relative_url: tag.url,
|
|
text: "somecooltag",
|
|
description: "Coolest things ever",
|
|
icon: "tag",
|
|
slug: "somecooltag",
|
|
ref: "somecooltag::tag",
|
|
type: "tag",
|
|
},
|
|
)
|
|
expect(PrettyText::Helpers.hashtag_lookup("someawesomecategory::category", user.id, %w[category tag])).to eq(
|
|
{
|
|
relative_url: category.url,
|
|
text: "Some Awesome Category",
|
|
description: "Really great stuff here",
|
|
icon: "folder",
|
|
slug: "someawesomecategory",
|
|
ref: "someawesomecategory::category",
|
|
type: "category",
|
|
},
|
|
)
|
|
end
|
|
|
|
it "handles categories based on slug" do
|
|
expect(
|
|
PrettyText::Helpers.hashtag_lookup("someawesomecategory", user.id, %w[category tag]),
|
|
).to eq(
|
|
{
|
|
relative_url: category.url,
|
|
text: "Some Awesome Category",
|
|
description: "Really great stuff here",
|
|
icon: "folder",
|
|
slug: "someawesomecategory",
|
|
ref: "someawesomecategory",
|
|
type: "category",
|
|
},
|
|
)
|
|
end
|
|
|
|
it "handles tags and categories based on slug without type suffix" do
|
|
expect(PrettyText::Helpers.hashtag_lookup("somecooltag", user.id, %w[category tag])).to eq(
|
|
{
|
|
relative_url: tag.url,
|
|
text: "somecooltag",
|
|
description: "Coolest things ever",
|
|
icon: "tag",
|
|
slug: "somecooltag",
|
|
ref: "somecooltag",
|
|
type: "tag",
|
|
},
|
|
)
|
|
expect(PrettyText::Helpers.hashtag_lookup("someawesomecategory", user.id, %w[category tag])).to eq(
|
|
{
|
|
relative_url: category.url,
|
|
text: "Some Awesome Category",
|
|
description: "Really great stuff here",
|
|
icon: "folder",
|
|
slug: "someawesomecategory",
|
|
ref: "someawesomecategory",
|
|
type: "category",
|
|
},
|
|
)
|
|
end
|
|
|
|
it "does not include categories the cooking user does not have access to" do
|
|
group = Fabricate(:group)
|
|
private_category = Fabricate(:private_category, slug: "secretcategory", name: "Manager Hideout", group: group)
|
|
expect(PrettyText::Helpers.hashtag_lookup("secretcategory", user.id, %w[category tag])).to eq(nil)
|
|
|
|
GroupUser.create(group: group, user: user)
|
|
expect(PrettyText::Helpers.hashtag_lookup("secretcategory", user.id, %w[category tag])).to eq(
|
|
{
|
|
relative_url: private_category.url,
|
|
text: "Manager Hideout",
|
|
description: nil,
|
|
icon: "folder",
|
|
slug: "secretcategory",
|
|
ref: "secretcategory",
|
|
type: "category",
|
|
},
|
|
)
|
|
end
|
|
|
|
it "returns nil when no tag or category that matches exists" do
|
|
expect(PrettyText::Helpers.hashtag_lookup("blah", user.id, %w[category tag])).to eq(nil)
|
|
end
|
|
|
|
it "uses the system user if the cooking_user is nil" do
|
|
guardian_system = Guardian.new(Discourse.system_user)
|
|
Guardian.expects(:new).with(Discourse.system_user).returns(guardian_system)
|
|
PrettyText::Helpers.hashtag_lookup("somecooltag", nil, %w[category tag])
|
|
end
|
|
end
|
|
end
|