discourse/spec/fabricators/upload_fabricator.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

106 lines
2.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
Fabricator(:upload) do
user
sha1 { sequence(:sha1) { |n| Digest::SHA1.hexdigest("#{n}#{Process.pid}") } }
2014-04-14 16:55:57 -04:00
original_filename "logo.png"
filesize 1234
width 100
height 200
thumbnail_width 30
thumbnail_height 60
url do |attrs|
sequence(:url) do |n|
Discourse.store.get_path_for("original", n + 1, attrs[:sha1], ".#{attrs[:extension]}")
end
end
extension "png"
end
2013-07-10 16:59:07 -04:00
Fabricator(:large_image_upload, from: :upload) do
width 2000
height 2000
after_create do |upload, _transients|
file = file_from_fixtures("2000x2000.png")
upload.url = Discourse.store.store_upload(file, upload)
upload.sha1 = Upload.generate_digest(file)
end
end
Fabricator(:image_upload, from: :upload) do
transient color: "white"
transient color_depth: 8
after_create do |upload, transients|
file = Tempfile.new(%w[fabricated .png])
`magick -size #{upload.width}x#{upload.height} -depth #{transients[:color_depth]} xc:#{transients[:color]} "#{file.path}"`
upload.url = Discourse.store.store_upload(file, upload)
upload.sha1 = Upload.generate_digest(file.path)
WebMock.stub_request(:get, "http://#{Discourse.current_hostname}#{upload.url}").to_return(
status: 200,
body: File.new(file.path),
)
end
end
Fabricator(:upload_no_dimensions, from: :upload) do
width nil
height nil
thumbnail_width nil
thumbnail_height nil
end
Fabricator(:video_upload, from: :upload) do
original_filename "video.mp4"
width nil
height nil
thumbnail_width nil
thumbnail_height nil
extension "mp4"
end
FEATURE: Secure media allowing duplicated uploads with category-level privacy and post-based access rules (#8664) ### General Changes and Duplication * We now consider a post `with_secure_media?` if it is in a read-restricted category. * When uploading we now set an upload's secure status straight away. * When uploading if `SiteSetting.secure_media` is enabled, we do not check to see if the upload already exists using the `sha1` digest of the upload. The `sha1` column of the upload is filled with a `SecureRandom.hex(20)` value which is the same length as `Upload::SHA1_LENGTH`. The `original_sha1` column is filled with the _real_ sha1 digest of the file. * Whether an upload `should_be_secure?` is now determined by whether the `access_control_post` is `with_secure_media?` (if there is no access control post then we leave the secure status as is). * When serializing the upload, we now cook the URL if the upload is secure. This is so it shows up correctly in the composer preview, because we set secure status on upload. ### Viewing Secure Media * The secure-media-upload URL will take the post that the upload is attached to into account via `Guardian.can_see?` for access permissions * If there is no `access_control_post` then we just deliver the media. This should be a rare occurrance and shouldn't cause issues as the `access_control_post` is set when `link_post_uploads` is called via `CookedPostProcessor` ### Removed We no longer do any of these because we do not reuse uploads by sha1 if secure media is enabled. * We no longer have a way to prevent cross-posting of a secure upload from a private context to a public context. * We no longer have to set `secure: false` for uploads when uploading for a theme component.
2020-01-15 22:50:27 -05:00
Fabricator(:secure_upload, from: :upload) do
secure true
FEATURE: Secure media allowing duplicated uploads with category-level privacy and post-based access rules (#8664) ### General Changes and Duplication * We now consider a post `with_secure_media?` if it is in a read-restricted category. * When uploading we now set an upload's secure status straight away. * When uploading if `SiteSetting.secure_media` is enabled, we do not check to see if the upload already exists using the `sha1` digest of the upload. The `sha1` column of the upload is filled with a `SecureRandom.hex(20)` value which is the same length as `Upload::SHA1_LENGTH`. The `original_sha1` column is filled with the _real_ sha1 digest of the file. * Whether an upload `should_be_secure?` is now determined by whether the `access_control_post` is `with_secure_media?` (if there is no access control post then we leave the secure status as is). * When serializing the upload, we now cook the URL if the upload is secure. This is so it shows up correctly in the composer preview, because we set secure status on upload. ### Viewing Secure Media * The secure-media-upload URL will take the post that the upload is attached to into account via `Guardian.can_see?` for access permissions * If there is no `access_control_post` then we just deliver the media. This should be a rare occurrance and shouldn't cause issues as the `access_control_post` is set when `link_post_uploads` is called via `CookedPostProcessor` ### Removed We no longer do any of these because we do not reuse uploads by sha1 if secure media is enabled. * We no longer have a way to prevent cross-posting of a secure upload from a private context to a public context. * We no longer have to set `secure: false` for uploads when uploading for a theme component.
2020-01-15 22:50:27 -05:00
sha1 { SecureRandom.hex(20) }
original_sha1 { sequence(:sha1) { |n| Digest::SHA1.hexdigest(n.to_s) } }
end
Fabricator(:upload_s3, from: :upload) do
url do |attrs|
sequence(:url) do |n|
path = +Discourse.store.get_path_for("original", n + 1, attrs[:sha1], ".#{attrs[:extension]}")
path.prepend(File.join(Discourse.store.upload_path, "/")) if Rails.configuration.multisite
File.join(Discourse.store.absolute_base_url, path)
end
end
2013-07-10 16:59:07 -04:00
end
Fabricator(:s3_image_upload, from: :upload_s3) do
after_create do |upload|
file = Tempfile.new(%w[fabricated .png])
`magick -size #{upload.width}x#{upload.height} xc:white "#{file.path}"`
upload.url = Discourse.store.store_upload(file, upload)
upload.sha1 = Upload.generate_digest(file.path)
WebMock.stub_request(:get, upload.url).to_return(status: 200, body: File.new(file.path))
end
end
Fabricator(:secure_upload_s3, from: :upload_s3) do
secure true
sha1 { SecureRandom.hex(20) }
original_sha1 { sequence(:sha1) { |n| Digest::SHA1.hexdigest(n.to_s) } }
end
Fabricator(:upload_reference) do
target
upload
end