Fix media attachments without file being uploadable (#12562)

Fix #12554
master
Eugen Rochko 4 years ago committed by GitHub
parent 43daeccccb
commit 81cc86bb1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      app/models/media_attachment.rb
  2. 18
      spec/fabricators/media_attachment_fabricator.rb
  3. 13
      spec/models/media_attachment_spec.rb
  4. 8
      spec/services/post_status_service_spec.rb

@ -142,6 +142,7 @@ class MediaAttachment < ApplicationRecord
validates :account, presence: true validates :account, presence: true
validates :description, length: { maximum: MAX_DESCRIPTION_LENGTH }, if: :local? validates :description, length: { maximum: MAX_DESCRIPTION_LENGTH }, if: :local?
validates :file, presence: true, if: :local?
scope :attached, -> { where.not(status_id: nil).or(where.not(scheduled_status_id: nil)) } scope :attached, -> { where.not(status_id: nil).or(where.not(scheduled_status_id: nil)) }
scope :unattached, -> { where(status_id: nil, scheduled_status_id: nil) } scope :unattached, -> { where(status_id: nil, scheduled_status_id: nil) }

@ -1,16 +1,12 @@
Fabricator(:media_attachment) do Fabricator(:media_attachment) do
account account
file do |attrs| file do |attrs|
[ case attrs[:type]
case attrs[:type] when :gifv, :video
when :gifv attachment_fixture('attachment.webm')
attachment_fixture ['attachment.gif', 'attachment.webm'].sample else
when :image attachment_fixture('attachment.jpg')
attachment_fixture 'attachment.jpg' end
when nil
attachment_fixture ['attachment.gif', 'attachment.jpg', 'attachment.webm'].sample
end,
nil
].sample
end end
end end

@ -31,14 +31,6 @@ RSpec.describe MediaAttachment, type: :model do
context 'file is blank' do context 'file is blank' do
let(:file) { nil } let(:file) { nil }
context 'remote_url is blank' do
let(:remote_url) { '' }
it 'returns false' do
is_expected.to be false
end
end
context 'remote_url is present' do context 'remote_url is present' do
let(:remote_url) { 'remote_url' } let(:remote_url) { 'remote_url' }
@ -153,6 +145,11 @@ RSpec.describe MediaAttachment, type: :model do
end end
end end
it 'is invalid without file' do
media = MediaAttachment.new(account: Fabricate(:account))
expect(media.valid?).to be false
end
describe 'descriptions for remote attachments' do describe 'descriptions for remote attachments' do
it 'are cut off at 1500 characters' do it 'are cut off at 1500 characters' do
media = Fabricate(:media_attachment, description: 'foo' * 1000, remote_url: 'http://example.com/blah.jpg') media = Fabricate(:media_attachment, description: 'foo' * 1000, remote_url: 'http://example.com/blah.jpg')

@ -212,14 +212,18 @@ RSpec.describe PostStatusService, type: :service do
it 'does not allow attaching both videos and images' do it 'does not allow attaching both videos and images' do
account = Fabricate(:account) account = Fabricate(:account)
video = Fabricate(:media_attachment, type: :video, account: account)
image = Fabricate(:media_attachment, type: :image, account: account)
video.update(type: :video)
expect do expect do
subject.call( subject.call(
account, account,
text: "test status update", text: "test status update",
media_ids: [ media_ids: [
Fabricate(:media_attachment, type: :video, account: account), video,
Fabricate(:media_attachment, type: :image, account: account), image,
].map(&:id), ].map(&:id),
) )
end.to raise_error( end.to raise_error(

Loading…
Cancel
Save