2022-06-09 07:24:30 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe UploadReference do
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "badge uploads" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:upload)
|
2022-06-09 07:24:30 +08:00
|
|
|
|
|
|
|
it "creates upload references" do
|
|
|
|
badge = nil
|
|
|
|
expect { badge = Fabricate(:badge, image_upload_id: upload.id) }.to change {
|
|
|
|
UploadReference.count
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
upload_reference = UploadReference.last
|
|
|
|
expect(upload_reference.upload).to eq(upload)
|
|
|
|
expect(upload_reference.target).to eq(badge)
|
|
|
|
|
|
|
|
expect { badge.destroy! }.to change { UploadReference.count }.by(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "category uploads" do
|
2022-06-09 07:24:30 +08:00
|
|
|
fab!(:upload1) { Fabricate(:upload) }
|
|
|
|
fab!(:upload2) { Fabricate(:upload) }
|
2022-10-07 23:00:44 +08:00
|
|
|
fab!(:upload3) { Fabricate(:upload) }
|
2023-10-20 20:48:06 +08:00
|
|
|
fab!(:upload4) { Fabricate(:upload) }
|
2022-06-09 07:24:30 +08:00
|
|
|
|
|
|
|
it "creates upload references" do
|
|
|
|
category = nil
|
2022-10-07 23:00:44 +08:00
|
|
|
expect {
|
|
|
|
category =
|
|
|
|
Fabricate(
|
|
|
|
:category,
|
|
|
|
uploaded_logo_id: upload1.id,
|
|
|
|
uploaded_logo_dark_id: upload2.id,
|
|
|
|
uploaded_background_id: upload3.id,
|
2023-10-20 20:48:06 +08:00
|
|
|
uploaded_background_dark_id: upload4.id,
|
2022-10-07 23:00:44 +08:00
|
|
|
)
|
2023-10-20 20:48:06 +08:00
|
|
|
}.to change { UploadReference.count }.by(4)
|
2022-06-09 07:24:30 +08:00
|
|
|
|
|
|
|
upload_reference = UploadReference.last
|
|
|
|
expect(upload_reference.target).to eq(category)
|
|
|
|
|
2023-10-20 20:48:06 +08:00
|
|
|
expect { category.destroy! }.to change { UploadReference.count }.by(-4)
|
2022-06-09 07:24:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "custom emoji uploads" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:upload)
|
2022-06-09 07:24:30 +08:00
|
|
|
|
|
|
|
it "creates upload references" do
|
|
|
|
custom_emoji = nil
|
|
|
|
expect { custom_emoji = CustomEmoji.create!(name: "emoji", upload_id: upload.id) }.to change {
|
|
|
|
UploadReference.count
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
upload_reference = UploadReference.last
|
|
|
|
expect(upload_reference.target).to eq(custom_emoji)
|
|
|
|
|
|
|
|
expect { custom_emoji.destroy! }.to change { UploadReference.count }.by(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "group uploads" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:upload)
|
2022-06-09 07:24:30 +08:00
|
|
|
|
|
|
|
it "creates upload references" do
|
|
|
|
group = nil
|
|
|
|
expect { group = Fabricate(:group, flair_upload_id: upload.id) }.to change {
|
|
|
|
UploadReference.count
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
upload_reference = UploadReference.last
|
|
|
|
expect(upload_reference.upload).to eq(upload)
|
|
|
|
expect(upload_reference.target).to eq(group)
|
|
|
|
|
|
|
|
expect { group.destroy! }.to change { UploadReference.count }.by(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "post uploads" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:upload)
|
2022-06-09 07:24:30 +08:00
|
|
|
fab!(:post) { Fabricate(:post, raw: "[](#{upload.short_url})") }
|
|
|
|
|
|
|
|
it "creates upload references" do
|
|
|
|
expect { post.link_post_uploads }.to change { UploadReference.count }.by(1)
|
|
|
|
|
|
|
|
upload_reference = UploadReference.last
|
|
|
|
expect(upload_reference.upload).to eq(upload)
|
|
|
|
expect(upload_reference.target).to eq(post)
|
|
|
|
|
|
|
|
expect { post.destroy! }.to change { UploadReference.count }.by(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "site setting uploads" do
|
2022-06-09 07:24:30 +08:00
|
|
|
let(:provider) { SiteSettings::DbProvider.new(SiteSetting) }
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:upload)
|
2022-06-09 07:24:30 +08:00
|
|
|
fab!(:upload2) { Fabricate(:upload) }
|
|
|
|
|
|
|
|
it "creates upload references for uploads" do
|
|
|
|
expect {
|
|
|
|
provider.save("logo", upload.id, SiteSettings::TypeSupervisor.types[:upload])
|
|
|
|
}.to change { UploadReference.count }.by(1)
|
|
|
|
|
|
|
|
upload_reference = UploadReference.last
|
|
|
|
expect(upload_reference.upload).to eq(upload)
|
|
|
|
expect(upload_reference.target).to eq(SiteSetting.find_by(name: "logo"))
|
|
|
|
|
|
|
|
expect { provider.destroy("logo") }.to change { UploadReference.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates upload references for uploaded_image_lists" do
|
|
|
|
expect {
|
|
|
|
provider.save(
|
|
|
|
"selectable_avatars",
|
|
|
|
"#{upload.id}|#{upload2.id}",
|
|
|
|
SiteSettings::TypeSupervisor.types[:uploaded_image_list],
|
|
|
|
)
|
|
|
|
}.to change { UploadReference.count }.by(2)
|
|
|
|
|
|
|
|
upload_references =
|
|
|
|
UploadReference.all.where(target: SiteSetting.find_by(name: "selectable_avatars"))
|
|
|
|
expect(upload_references.pluck(:upload_id)).to contain_exactly(upload.id, upload2.id)
|
|
|
|
|
|
|
|
expect { provider.destroy("selectable_avatars") }.to change { UploadReference.count }.by(-2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "theme field uploads" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:upload)
|
2022-06-09 07:24:30 +08:00
|
|
|
|
2023-06-14 04:02:21 +08:00
|
|
|
it "creates upload references" do
|
2022-06-09 07:24:30 +08:00
|
|
|
theme_field = nil
|
|
|
|
expect do
|
|
|
|
theme_field =
|
|
|
|
ThemeField.create!(
|
|
|
|
theme_id: Fabricate(:theme).id,
|
|
|
|
target_id: 0,
|
|
|
|
name: "field",
|
|
|
|
value: "",
|
|
|
|
upload: upload,
|
|
|
|
type_id: ThemeField.types[:theme_upload_var],
|
|
|
|
)
|
|
|
|
end.to change { UploadReference.count }.by(1)
|
|
|
|
|
|
|
|
upload_reference = UploadReference.last
|
|
|
|
expect(upload_reference.upload).to eq(upload)
|
|
|
|
expect(upload_reference.target).to eq(theme_field)
|
|
|
|
|
|
|
|
expect { theme_field.destroy! }.to change { UploadReference.count }.by(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "theme setting uploads" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:upload)
|
2022-06-09 07:24:30 +08:00
|
|
|
|
2023-06-14 04:02:21 +08:00
|
|
|
it "creates upload references" do
|
2022-06-09 07:24:30 +08:00
|
|
|
theme_setting = nil
|
|
|
|
expect do
|
|
|
|
theme_setting =
|
|
|
|
ThemeSetting.create!(
|
|
|
|
name: "field",
|
|
|
|
data_type: ThemeSetting.types[:upload],
|
|
|
|
value: upload.id,
|
|
|
|
theme_id: Fabricate(:theme).id,
|
|
|
|
)
|
|
|
|
end.to change { UploadReference.count }.by(1)
|
|
|
|
|
|
|
|
upload_reference = UploadReference.last
|
|
|
|
expect(upload_reference.upload).to eq(upload)
|
|
|
|
expect(upload_reference.target).to eq(theme_setting)
|
|
|
|
|
|
|
|
expect { theme_setting.destroy! }.to change { UploadReference.count }.by(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "user uploads" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:upload)
|
2022-06-09 07:24:30 +08:00
|
|
|
|
|
|
|
it "creates upload references" do
|
|
|
|
user = nil
|
|
|
|
expect { user = Fabricate(:user, uploaded_avatar_id: upload.id) }.to change {
|
|
|
|
UploadReference.count
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
upload_reference = UploadReference.last
|
|
|
|
expect(upload_reference.upload).to eq(upload)
|
|
|
|
expect(upload_reference.target).to eq(user)
|
|
|
|
|
|
|
|
expect { user.destroy! }.to change { UploadReference.count }.by(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "user avatar uploads" do
|
2022-06-09 07:24:30 +08:00
|
|
|
fab!(:upload1) { Fabricate(:upload) }
|
|
|
|
fab!(:upload2) { Fabricate(:upload) }
|
|
|
|
|
|
|
|
it "creates upload references" do
|
|
|
|
user_avatar = nil
|
|
|
|
expect {
|
|
|
|
user_avatar =
|
|
|
|
Fabricate(:user_avatar, custom_upload_id: upload1.id, gravatar_upload_id: upload2.id)
|
|
|
|
}.to change { UploadReference.count }.by(2)
|
|
|
|
|
|
|
|
upload_reference = UploadReference.last
|
|
|
|
expect(upload_reference.target).to eq(user_avatar)
|
|
|
|
|
|
|
|
expect { user_avatar.destroy! }.to change { UploadReference.count }.by(-2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "user export uploads" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:upload)
|
2022-06-09 07:24:30 +08:00
|
|
|
|
|
|
|
it "creates upload references" do
|
|
|
|
user_export = nil
|
|
|
|
expect do
|
|
|
|
user_export =
|
|
|
|
UserExport.create!(
|
|
|
|
file_name: "export",
|
|
|
|
user: Fabricate(:user),
|
|
|
|
upload: upload,
|
|
|
|
topic: Fabricate(:topic),
|
|
|
|
)
|
|
|
|
end.to change { UploadReference.count }.by(1)
|
|
|
|
|
|
|
|
upload_reference = UploadReference.last
|
|
|
|
expect(upload_reference.upload).to eq(upload)
|
|
|
|
expect(upload_reference.target).to eq(user_export)
|
|
|
|
|
|
|
|
expect { user_export.destroy! }.to change { UploadReference.count }.by(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "user profile uploads" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
2022-06-09 07:24:30 +08:00
|
|
|
fab!(:upload1) { Fabricate(:upload) }
|
|
|
|
fab!(:upload2) { Fabricate(:upload) }
|
|
|
|
|
|
|
|
it "creates upload references" do
|
|
|
|
user_profile = user.user_profile
|
|
|
|
expect {
|
|
|
|
user_profile.update!(
|
|
|
|
profile_background_upload_id: upload1.id,
|
|
|
|
card_background_upload_id: upload2.id,
|
|
|
|
)
|
|
|
|
}.to change { UploadReference.count }.by(2)
|
|
|
|
|
|
|
|
upload_reference = UploadReference.last
|
|
|
|
expect(upload_reference.target).to eq(user_profile)
|
|
|
|
|
|
|
|
expect { user_profile.destroy! }.to change { UploadReference.count }.by(-2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|