DEV: Apply syntax_tree formatting to spec/*

This commit is contained in:
David Taylor
2023-01-09 11:18:21 +00:00
parent 0cf6421716
commit cb932d6ee1
907 changed files with 58693 additions and 45909 deletions

View File

@ -16,7 +16,7 @@ RSpec.describe Upload do
let(:attachment_path) { __FILE__ }
let(:attachment) { File.new(attachment_path) }
describe '.with_no_non_post_relations' do
describe ".with_no_non_post_relations" do
it "does not find non-post related uploads" do
post_upload = Fabricate(:upload)
post = Fabricate(:post, raw: "<img src='#{post_upload.url}'>")
@ -32,13 +32,10 @@ RSpec.describe Upload do
SiteSetting.create!(
name: "logo",
data_type: SiteSettings::TypeSupervisor.types[:upload],
value: site_setting_upload.id
value: site_setting_upload.id,
)
upload_ids = Upload
.by_users
.with_no_non_post_relations
.pluck(:id)
upload_ids = Upload.by_users.with_no_non_post_relations.pluck(:id)
expect(upload_ids).to eq([post_upload.id])
end
end
@ -100,11 +97,13 @@ RSpec.describe Upload do
expect(upload.thumbnail_width).to eq(nil)
end
it 'returns error when image resolution is to big' do
it "returns error when image resolution is to big" do
SiteSetting.max_image_megapixels = 10
upload = UploadCreator.new(huge_image, "image.png").create_for(user_id)
expect(upload.persisted?).to eq(false)
expect(upload.errors.messages[:base].first).to eq(I18n.t("upload.images.larger_than_x_megapixels", max_image_megapixels: 20))
expect(upload.errors.messages[:base].first).to eq(
I18n.t("upload.images.larger_than_x_megapixels", max_image_megapixels: 20),
)
end
it "extracts file extension" do
@ -119,10 +118,10 @@ RSpec.describe Upload do
end
describe ".extract_url" do
let(:url) { 'https://example.com/uploads/default/original/1X/d1c2d40ab994e8410c.png' }
let(:url) { "https://example.com/uploads/default/original/1X/d1c2d40ab994e8410c.png" }
it 'should return the right part of url' do
expect(Upload.extract_url(url).to_s).to eq('/original/1X/d1c2d40ab994e8410c.png')
it "should return the right part of url" do
expect(Upload.extract_url(url).to_s).to eq("/original/1X/d1c2d40ab994e8410c.png")
end
end
@ -134,13 +133,13 @@ RSpec.describe Upload do
expect(Upload.get_from_url(upload.url)).to eq(upload)
end
describe 'for an extensionless url' do
describe "for an extensionless url" do
before do
upload.update!(url: upload.url.sub('.png', ''))
upload.update!(url: upload.url.sub(".png", ""))
upload.reload
end
it 'should return the right upload' do
it "should return the right upload" do
expect(Upload.get_from_url(upload.url)).to eq(upload)
end
end
@ -150,23 +149,18 @@ RSpec.describe Upload do
expect(Upload.get_from_url(upload.url)).to eq(upload)
expect(Upload.get_from_url("/uploads/default/123131/971308e535305c51.png"))
.to eq(nil)
expect(Upload.get_from_url("/uploads/default/123131/971308e535305c51.png")).to eq(nil)
end
describe 'for a url a tree' do
describe "for a url a tree" do
before do
upload.update!(url:
Discourse.store.get_path_for(
"original",
16001,
upload.sha1,
".#{upload.extension}"
)
upload.update!(
url:
Discourse.store.get_path_for("original", 16_001, upload.sha1, ".#{upload.extension}"),
)
end
it 'should return the right upload' do
it "should return the right upload" do
expect(Upload.get_from_url(upload.url)).to eq(upload)
end
end
@ -174,20 +168,18 @@ RSpec.describe Upload do
it "works when using a cdn" do
begin
original_asset_host = Rails.configuration.action_controller.asset_host
Rails.configuration.action_controller.asset_host = 'http://my.cdn.com'
Rails.configuration.action_controller.asset_host = "http://my.cdn.com"
expect(Upload.get_from_url(
URI.join("http://my.cdn.com", upload.url).to_s
)).to eq(upload)
expect(Upload.get_from_url(URI.join("http://my.cdn.com", upload.url).to_s)).to eq(upload)
ensure
Rails.configuration.action_controller.asset_host = original_asset_host
end
end
it "should return the right upload when using the full URL" do
expect(Upload.get_from_url(
URI.join("http://discourse.some.com:3000/", upload.url).to_s
)).to eq(upload)
expect(
Upload.get_from_url(URI.join("http://discourse.some.com:3000/", upload.url).to_s),
).to eq(upload)
end
it "doesn't blow up with an invalid URI" do
@ -198,15 +190,12 @@ RSpec.describe Upload do
describe "s3 store" do
let(:upload) { Fabricate(:upload_s3) }
let(:path) { upload.url.sub(SiteSetting.Upload.s3_base_url, '') }
let(:path) { upload.url.sub(SiteSetting.Upload.s3_base_url, "") }
before do
setup_s3
end
before { setup_s3 }
it "can download an s3 upload" do
stub_request(:get, upload.url)
.to_return(status: 200, body: "hello", headers: {})
stub_request(:get, upload.url).to_return(status: 200, body: "hello", headers: {})
expect(upload.content).to eq("hello")
end
@ -216,22 +205,18 @@ RSpec.describe Upload do
expect(Upload.get_from_url(upload.url)).to eq(upload)
end
describe 'when using a cdn' do
let(:s3_cdn_url) { 'https://mycdn.slowly.net' }
describe "when using a cdn" do
let(:s3_cdn_url) { "https://mycdn.slowly.net" }
before do
SiteSetting.s3_cdn_url = s3_cdn_url
end
before { SiteSetting.s3_cdn_url = s3_cdn_url }
it "should return the right upload" do
upload
expect(Upload.get_from_url(URI.join(s3_cdn_url, path).to_s)).to eq(upload)
end
describe 'when upload bucket contains subfolder' do
before do
SiteSetting.s3_upload_bucket = "s3-upload-bucket/path/path2"
end
describe "when upload bucket contains subfolder" do
before { SiteSetting.s3_upload_bucket = "s3-upload-bucket/path/path2" }
it "should return the right upload" do
upload
@ -243,14 +228,12 @@ RSpec.describe Upload do
it "should return the right upload when using one CDN for both s3 and assets" do
begin
original_asset_host = Rails.configuration.action_controller.asset_host
cdn_url = 'http://my.cdn.com'
cdn_url = "http://my.cdn.com"
Rails.configuration.action_controller.asset_host = cdn_url
SiteSetting.s3_cdn_url = cdn_url
upload
expect(Upload.get_from_url(
URI.join(cdn_url, path).to_s
)).to eq(upload)
expect(Upload.get_from_url(URI.join(cdn_url, path).to_s)).to eq(upload)
ensure
Rails.configuration.action_controller.asset_host = original_asset_host
end
@ -267,7 +250,7 @@ RSpec.describe Upload do
end
it "works for an extensionless URL" do
url = upload.url.sub('.png', '')
url = upload.url.sub(".png", "")
upload.update!(url: url)
expect(Upload.get_from_urls([url])).to contain_exactly(upload)
end
@ -279,74 +262,76 @@ RSpec.describe Upload do
end
it "works with an upload with a URL containing a deep tree" do
upload.update!(url: Discourse.store.get_path_for("original", 16001, upload.sha1, ".#{upload.extension}"))
upload.update!(
url: Discourse.store.get_path_for("original", 16_001, upload.sha1, ".#{upload.extension}"),
)
expect(Upload.get_from_urls([upload.url])).to contain_exactly(upload)
end
it "works when using a CDN" do
begin
original_asset_host = Rails.configuration.action_controller.asset_host
Rails.configuration.action_controller.asset_host = 'http://my.cdn.com'
Rails.configuration.action_controller.asset_host = "http://my.cdn.com"
expect(Upload.get_from_urls([
URI.join("http://my.cdn.com", upload.url).to_s
])).to contain_exactly(upload)
expect(
Upload.get_from_urls([URI.join("http://my.cdn.com", upload.url).to_s]),
).to contain_exactly(upload)
ensure
Rails.configuration.action_controller.asset_host = original_asset_host
end
end
it "works with full URLs" do
expect(Upload.get_from_urls([
URI.join("http://discourse.some.com:3000/", upload.url).to_s
])).to contain_exactly(upload)
expect(
Upload.get_from_urls([URI.join("http://discourse.some.com:3000/", upload.url).to_s]),
).to contain_exactly(upload)
end
it "handles invalid URIs" do
urls = ["http://ip:port/index.html", "mailto:admin%40example.com", "mailto:example"]
urls = %w[http://ip:port/index.html mailto:admin%40example.com mailto:example]
expect { Upload.get_from_urls(urls) }.not_to raise_error
end
end
describe '.generate_digest' do
describe ".generate_digest" do
it "should return the right digest" do
expect(Upload.generate_digest(image.path)).to eq('bc975735dfc6409c1c2aa5ebf2239949bcbdbd65')
expect(Upload.generate_digest(image.path)).to eq("bc975735dfc6409c1c2aa5ebf2239949bcbdbd65")
end
end
describe '.short_url' do
describe ".short_url" do
it "should generate a correct short url" do
upload = Upload.new(sha1: 'bda2c513e1da04f7b4e99230851ea2aafeb8cc4e', extension: 'png')
expect(upload.short_url).to eq('upload://r3AYqESanERjladb4vBB7VsMBm6.png')
upload = Upload.new(sha1: "bda2c513e1da04f7b4e99230851ea2aafeb8cc4e", extension: "png")
expect(upload.short_url).to eq("upload://r3AYqESanERjladb4vBB7VsMBm6.png")
upload.extension = nil
expect(upload.short_url).to eq('upload://r3AYqESanERjladb4vBB7VsMBm6')
expect(upload.short_url).to eq("upload://r3AYqESanERjladb4vBB7VsMBm6")
end
end
describe '.sha1_from_short_url' do
describe ".sha1_from_short_url" do
it "should be able to look up sha1" do
sha1 = 'bda2c513e1da04f7b4e99230851ea2aafeb8cc4e'
sha1 = "bda2c513e1da04f7b4e99230851ea2aafeb8cc4e"
expect(Upload.sha1_from_short_url('upload://r3AYqESanERjladb4vBB7VsMBm6.png')).to eq(sha1)
expect(Upload.sha1_from_short_url('upload://r3AYqESanERjladb4vBB7VsMBm6')).to eq(sha1)
expect(Upload.sha1_from_short_url('r3AYqESanERjladb4vBB7VsMBm6')).to eq(sha1)
expect(Upload.sha1_from_short_url("upload://r3AYqESanERjladb4vBB7VsMBm6.png")).to eq(sha1)
expect(Upload.sha1_from_short_url("upload://r3AYqESanERjladb4vBB7VsMBm6")).to eq(sha1)
expect(Upload.sha1_from_short_url("r3AYqESanERjladb4vBB7VsMBm6")).to eq(sha1)
end
it "should be able to look up sha1 even with leading zeros" do
sha1 = '0000c513e1da04f7b4e99230851ea2aafeb8cc4e'
expect(Upload.sha1_from_short_url('upload://1Eg9p8rrCURq4T3a6iJUk0ri6.png')).to eq(sha1)
sha1 = "0000c513e1da04f7b4e99230851ea2aafeb8cc4e"
expect(Upload.sha1_from_short_url("upload://1Eg9p8rrCURq4T3a6iJUk0ri6.png")).to eq(sha1)
end
end
describe '#base62_sha1' do
it 'should return the right value' do
describe "#base62_sha1" do
it "should return the right value" do
upload.update!(sha1: "0000c513e1da04f7b4e99230851ea2aafeb8cc4e")
expect(upload.base62_sha1).to eq("1Eg9p8rrCURq4T3a6iJUk0ri6")
end
end
describe '.sha1_from_short_path' do
describe ".sha1_from_short_path" do
it "should be able to lookup sha1" do
path = "/uploads/short-url/3UjQ4jHoyeoQndk5y3qHzm3QVTQ.png"
sha1 = "1b6453892473a467d07372d45eb05abc2031647a"
@ -356,22 +341,21 @@ RSpec.describe Upload do
end
end
describe '#to_s' do
it 'should return the right value' do
describe "#to_s" do
it "should return the right value" do
expect(upload.to_s).to eq(upload.url)
end
end
describe '.migrate_to_new_scheme' do
it 'should not migrate system uploads' do
describe ".migrate_to_new_scheme" do
it "should not migrate system uploads" do
SiteSetting.migrate_to_new_scheme = true
expect { Upload.migrate_to_new_scheme }
.to_not change { Upload.pluck(:url) }
expect { Upload.migrate_to_new_scheme }.to_not change { Upload.pluck(:url) }
end
end
describe '.update_secure_status' do
describe ".update_secure_status" do
it "respects the override parameter if provided" do
upload.update!(secure: true)
@ -384,54 +368,48 @@ RSpec.describe Upload do
expect(upload.secure).to eq(false)
end
it 'marks a local upload as not secure with default settings' do
it "marks a local upload as not secure with default settings" do
upload.update!(secure: true)
expect { upload.update_secure_status }
.to change { upload.secure }
expect { upload.update_secure_status }.to change { upload.secure }
expect(upload.secure).to eq(false)
end
context "with local attachment" do
before do
SiteSetting.authorized_extensions = "pdf"
before { SiteSetting.authorized_extensions = "pdf" }
let(:upload) do
Fabricate(:upload, original_filename: "small.pdf", extension: "pdf", secure: true)
end
let(:upload) { Fabricate(:upload, original_filename: "small.pdf", extension: "pdf", secure: true) }
it 'marks a local attachment as secure if secure uploads enabled' do
it "marks a local attachment as secure if secure uploads enabled" do
upload.update!(secure: false, access_control_post: Fabricate(:private_message_post))
enable_secure_uploads
expect { upload.update_secure_status }
.to change { upload.secure }
expect { upload.update_secure_status }.to change { upload.secure }
expect(upload.secure).to eq(true)
end
it 'marks a local attachment as not secure if secure uploads enabled' do
expect { upload.update_secure_status }
.to change { upload.secure }
it "marks a local attachment as not secure if secure uploads enabled" do
expect { upload.update_secure_status }.to change { upload.secure }
expect(upload.secure).to eq(false)
end
end
it 'does not change secure status of a non-attachment when prevent_anons_from_downloading_files is enabled by itself' do
it "does not change secure status of a non-attachment when prevent_anons_from_downloading_files is enabled by itself" do
SiteSetting.prevent_anons_from_downloading_files = true
SiteSetting.authorized_extensions = "mp4"
upload.update!(original_filename: "small.mp4", extension: "mp4")
expect { upload.update_secure_status }
.not_to change { upload.secure }
expect { upload.update_secure_status }.not_to change { upload.secure }
expect(upload.secure).to eq(false)
end
context "with secure uploads enabled" do
before do
enable_secure_uploads
end
before { enable_secure_uploads }
it "does not mark an image upload as not secure when there is no access control post id, to avoid unintentional exposure" do
upload.update!(secure: true)
@ -455,8 +433,7 @@ RSpec.describe Upload do
SiteSetting.login_required = true
upload.update!(secure: false)
expect { upload.update_secure_status }
.to change { upload.secure }
expect { upload.update_secure_status }.to change { upload.secure }
expect(upload.reload.secure).to eq(true)
end
@ -471,7 +448,10 @@ RSpec.describe Upload do
it "does not mark an upload whose origin matches a regular emoji as secure (sometimes emojis are downloaded in pull_hotlinked_images)" do
SiteSetting.login_required = true
falafel = Emoji.all.find { |e| e.url == "/images/emoji/twitter/falafel.png?v=#{Emoji::EMOJI_VERSION}" }
falafel =
Emoji.all.find do |e|
e.url == "/images/emoji/twitter/falafel.png?v=#{Emoji::EMOJI_VERSION}"
end
upload.update!(secure: false, origin: "http://localhost:3000#{falafel.url}")
upload.update_secure_status
expect(upload.reload.secure).to eq(false)
@ -485,9 +465,15 @@ RSpec.describe Upload do
end
it "does not throw an error if the object storage provider does not support ACLs" do
FileStore::S3Store.any_instance.stubs(:update_upload_ACL).raises(
Aws::S3::Errors::NotImplemented.new("A header you provided implies functionality that is not implemented", "")
)
FileStore::S3Store
.any_instance
.stubs(:update_upload_ACL)
.raises(
Aws::S3::Errors::NotImplemented.new(
"A header you provided implies functionality that is not implemented",
"",
),
)
upload.update!(secure: true, access_control_post: Fabricate(:private_message_post))
expect { upload.update_secure_status }.not_to raise_error
end
@ -505,37 +491,38 @@ RSpec.describe Upload do
SiteSetting.login_required = true
SiteSetting.authorized_extensions = ""
expect do
upl = Fabricate(
:upload,
access_control_post: Fabricate(:private_message_post),
security_last_changed_at: Time.zone.now,
security_last_changed_reason: "test",
secure: true
)
upl =
Fabricate(
:upload,
access_control_post: Fabricate(:private_message_post),
security_last_changed_at: Time.zone.now,
security_last_changed_reason: "test",
secure: true,
)
end.to raise_error(ActiveRecord::RecordInvalid)
end
end
end
describe '.extract_upload_ids' do
describe ".extract_upload_ids" do
let(:upload) { Fabricate(:upload) }
it 'works with short URLs' do
it "works with short URLs" do
ids = Upload.extract_upload_ids("This URL #{upload.short_url} is an upload")
expect(ids).to contain_exactly(upload.id)
end
it 'works with SHA1s' do
it "works with SHA1s" do
ids = Upload.extract_upload_ids("This URL /#{upload.sha1} is an upload")
expect(ids).to contain_exactly(upload.id)
end
it 'works with Base62 hashes' do
it "works with Base62 hashes" do
ids = Upload.extract_upload_ids("This URL /#{Upload.base62_sha1(upload.sha1)} is an upload")
expect(ids).to contain_exactly(upload.id)
end
it 'works with shorter base62 hashes (when sha1 has leading 0s)' do
it "works with shorter base62 hashes (when sha1 has leading 0s)" do
upload.update(sha1: "0000c513e1da04f7b4e99230851ea2aafeb8cc4e")
base62 = Upload.base62_sha1(upload.sha1).delete_prefix("0")
ids = Upload.extract_upload_ids("This URL /#{base62} is an upload")
@ -549,14 +536,14 @@ RSpec.describe Upload do
stub_upload(upload)
end
describe '.destroy' do
describe ".destroy" do
it "can correctly clear information when destroying an upload" do
upload = Fabricate(:upload)
user = Fabricate(:user)
user.user_profile.update!(
card_background_upload_id: upload.id,
profile_background_upload_id: upload.id
profile_background_upload_id: upload.id,
)
upload.destroy
@ -584,11 +571,13 @@ RSpec.describe Upload do
describe ".secure_uploads_url?" do
it "works for a secure uploads url with or without schema + host" do
url = "//localhost:3000/secure-uploads/original/2X/f/f62055931bb702c7fd8f552fb901f977e0289a18.png"
url =
"//localhost:3000/secure-uploads/original/2X/f/f62055931bb702c7fd8f552fb901f977e0289a18.png"
expect(Upload.secure_uploads_url?(url)).to eq(true)
url = "/secure-uploads/original/2X/f/f62055931bb702c7fd8f552fb901f977e0289a18.png"
expect(Upload.secure_uploads_url?(url)).to eq(true)
url = "http://localhost:3000/secure-uploads/original/2X/f/f62055931bb702c7fd8f552fb901f977e0289a18.png"
url =
"http://localhost:3000/secure-uploads/original/2X/f/f62055931bb702c7fd8f552fb901f977e0289a18.png"
expect(Upload.secure_uploads_url?(url)).to eq(true)
end
@ -622,26 +611,26 @@ RSpec.describe Upload do
describe "#dominant_color" do
let(:white_image) { Fabricate(:image_upload, color: "white") }
let(:red_image) { Fabricate(:image_upload, color: "red") }
let(:not_an_image) {
let(:not_an_image) do
upload = Fabricate(:upload)
file = Tempfile.new(['invalid', '.txt'])
file = Tempfile.new(%w[invalid .txt])
file << "Not really an image"
file.rewind
upload.update(url: Discourse.store.store_upload(file, upload), extension: "txt")
upload
}
let(:invalid_image) {
end
let(:invalid_image) do
upload = Fabricate(:upload)
file = Tempfile.new(['invalid', '.png'])
file = Tempfile.new(%w[invalid .png])
file << "Not really an image"
file.rewind
upload.update(url: Discourse.store.store_upload(file, upload))
upload
}
end
it "correctly identifies and stores an image's dominant color" do
expect(white_image.dominant_color).to eq(nil)
@ -692,13 +681,13 @@ RSpec.describe Upload do
end
it "correctly handles unparsable ImageMagick output" do
Discourse::Utils.stubs(:execute_command).returns('someinvalidoutput')
Discourse::Utils.stubs(:execute_command).returns("someinvalidoutput")
expect(invalid_image.dominant_color).to eq(nil)
expect {
invalid_image.dominant_color(calculate_if_missing: true)
}.to raise_error(/Calculated dominant color but unable to parse output/)
expect { invalid_image.dominant_color(calculate_if_missing: true) }.to raise_error(
/Calculated dominant color but unable to parse output/,
)
expect(invalid_image.dominant_color).to eq(nil)
end
@ -714,7 +703,11 @@ RSpec.describe Upload do
it "correctly handles error when file has HTTP error" do
white_image.stubs(:local?).returns(false)
FileStore::LocalStore.any_instance.stubs(:download).raises(OpenURI::HTTPError.new("Error", nil)).once
FileStore::LocalStore
.any_instance
.stubs(:download)
.raises(OpenURI::HTTPError.new("Error", nil))
.once
expect(white_image.dominant_color).to eq(nil)
expect(white_image.dominant_color(calculate_if_missing: true)).to eq("")
@ -729,13 +722,11 @@ RSpec.describe Upload do
u.update!(dominant_color: "")
u.update!(dominant_color: "abcdef")
expect {
u.update!(dominant_color: "toomanycharacters")
}.to raise_error(ActiveRecord::RecordInvalid)
expect { u.update!(dominant_color: "toomanycharacters") }.to raise_error(
ActiveRecord::RecordInvalid,
)
expect {
u.update!(dominant_color: "abcd")
}.to raise_error(ActiveRecord::RecordInvalid)
expect { u.update!(dominant_color: "abcd") }.to raise_error(ActiveRecord::RecordInvalid)
end
end
end