0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 13:08:40 +08:00
discourse/spec/models/theme_setting_spec.rb
Gabriel Grubba e52b385ae1
FIX: type: objects uploads should be stored as IDs (#40178)
Inside `type objects` we accepted upload URLs and converted them into
IDs, but we did not update the original object to have an ID.

Only on consuming should we turn the ID into a URL.

For normal front-end consumers, object upload fields should still be
URLs. Backend consumers should expect upload IDs.

<img width="2295" height="1263" alt="Screenshot 2026-05-20 at 12 19 23"
src="https://github.com/user-attachments/assets/20d411c4-1570-45fc-96c8-02f2b1b3639d"
/>
2026-05-21 13:45:27 -03:00

111 lines
3.4 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe ThemeSetting do
fab!(:theme)
describe "creating upload references for objects settings with upload fields" do
fab!(:upload)
fab!(:upload2, :upload)
let(:objects_setting_yaml) { <<~YAML }
objects_with_upload:
type: objects
default: []
schema:
name: test_object
properties:
name:
type: string
image:
type: upload
YAML
let(:ui_cards_setting_yaml) { <<~YAML }
ui_cards_setting:
type: objects
default:
- title: "Build a community"
schema:
name: card
identifier: title
properties:
title:
type: string
required: true
image:
type: upload
YAML
it "creates upload references for type objects settings with upload fields" do
theme.set_field(target: :settings, name: "yaml", value: objects_setting_yaml)
theme.save!
theme.settings[:objects_with_upload].value = [
{ "name" => "object1", "image" => upload.id },
{ "name" => "object2", "image" => upload2.id },
]
theme_setting = theme.theme_settings.find_by(name: "objects_with_upload")
upload_references = UploadReference.where(target: theme_setting)
expect(upload_references.pluck(:upload_id)).to contain_exactly(upload.id, upload2.id)
end
it "stores object upload fields as upload IDs when set with upload URLs" do
theme.set_field(target: :settings, name: "yaml", value: ui_cards_setting_yaml)
theme.save!
theme.settings[:ui_cards_setting].value = [
{ "title" => "Build a community", "image" => upload.url },
]
theme_setting = theme.theme_settings.find_by(name: "ui_cards_setting")
expect(theme_setting.json_value).to eq(
[{ "title" => "Build a community", "image" => upload.id }],
)
expect(theme.cached_settings[:ui_cards_setting].first["image"]).to eq(
Discourse.store.cdn_url(upload.url),
)
end
it "destroys upload references for type objects setting when the setting is destroyed" do
theme.set_field(target: :settings, name: "yaml", value: objects_setting_yaml)
theme.save!
theme.settings[:objects_with_upload].value = [
{ "name" => "object1", "image" => upload.id },
{ "name" => "object2", "image" => upload2.id },
]
theme_setting = theme.theme_settings.find_by(name: "objects_with_upload")
expect { theme_setting.destroy! }.to change { UploadReference.count }.by(-2)
end
end
context "for validations" do
it "should be invalid when json_value size is greater than the maximum allowed size" do
json_value = { "key" => "value" }
bytesize = json_value.to_json.bytesize
expect(bytesize).to eq(15)
stub_const(ThemeSetting, "MAXIMUM_JSON_VALUE_SIZE_BYTES", bytesize - 1) do
theme_setting =
ThemeSetting.new(
name: "test",
data_type: ThemeSetting.types[:objects],
theme:,
json_value:,
)
expect(theme_setting.valid?).to eq(false)
expect(theme_setting.errors[:json_value]).to contain_exactly(
I18n.t(
"theme_settings.errors.json_value.too_large",
max_size: (bytesize - 1) / 1024 / 1024,
),
)
end
end
end
end