Skip to content

Commit

Permalink
port tootsuite#12562 to monsterfork: Fix media attachments without fi…
Browse files Browse the repository at this point in the history
…le being uploadable

Fix mastodon#12554
  • Loading branch information
Gargron authored and multiple creatures committed Feb 21, 2020
1 parent c8159e7 commit e6bb4bd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
3 changes: 2 additions & 1 deletion app/models/media_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ class MediaAttachment < ApplicationRecord
include Attachmentable

validates :account, presence: true
validates :description, length: { maximum: 6666 }, 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 :unattached, -> { where(status_id: nil, scheduled_status_id: nil) }
Expand Down
18 changes: 7 additions & 11 deletions spec/fabricators/media_attachment_fabricator.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
Fabricator(:media_attachment) do
account

file do |attrs|
[
case attrs[:type]
when :gifv
attachment_fixture ['attachment.gif', 'attachment.webm'].sample
when :image
attachment_fixture 'attachment.jpg'
when nil
attachment_fixture ['attachment.gif', 'attachment.jpg', 'attachment.webm'].sample
end,
nil
].sample
case attrs[:type]
when :gifv, :video
attachment_fixture('attachment.webm')
else
attachment_fixture('attachment.jpg')
end
end
end
13 changes: 5 additions & 8 deletions spec/models/media_attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@
context 'file is blank' do
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
let(:remote_url) { 'remote_url' }

Expand Down Expand Up @@ -153,6 +145,11 @@
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
it 'are cut off at 666 characters' do
media = Fabricate(:media_attachment, description: 'foo' * 1000, remote_url: 'http://example.com/blah.jpg')
Expand Down
8 changes: 6 additions & 2 deletions spec/services/post_status_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,18 @@

it 'does not allow attaching both videos and images' do
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
subject.call(
account,
text: "test status update",
media_ids: [
Fabricate(:media_attachment, type: :video, account: account),
Fabricate(:media_attachment, type: :image, account: account),
video,
image,
].map(&:id),
)
end.to raise_error(
Expand Down

0 comments on commit e6bb4bd

Please sign in to comment.