You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
6.9 KiB

  1. require 'rails_helper'
  2. RSpec.describe MediaAttachment, type: :model do
  3. describe 'local?' do
  4. let(:media_attachment) { Fabricate(:media_attachment, remote_url: remote_url) }
  5. subject { media_attachment.local? }
  6. context 'remote_url is blank' do
  7. let(:remote_url) { '' }
  8. it 'returns true' do
  9. is_expected.to be true
  10. end
  11. end
  12. context 'remote_url is present' do
  13. let(:remote_url) { 'remote_url' }
  14. it 'returns false' do
  15. is_expected.to be false
  16. end
  17. end
  18. end
  19. describe 'needs_redownload?' do
  20. let(:media_attachment) { Fabricate(:media_attachment, remote_url: remote_url, file: file) }
  21. subject { media_attachment.needs_redownload? }
  22. context 'file is blank' do
  23. let(:file) { nil }
  24. context 'remote_url is present' do
  25. let(:remote_url) { 'remote_url' }
  26. it 'returns true' do
  27. is_expected.to be true
  28. end
  29. end
  30. end
  31. context 'file is present' do
  32. let(:file) { attachment_fixture('avatar.gif') }
  33. context 'remote_url is blank' do
  34. let(:remote_url) { '' }
  35. it 'returns false' do
  36. is_expected.to be false
  37. end
  38. end
  39. context 'remote_url is present' do
  40. let(:remote_url) { 'remote_url' }
  41. it 'returns true' do
  42. is_expected.to be false
  43. end
  44. end
  45. end
  46. end
  47. describe '#to_param' do
  48. let(:media_attachment) { Fabricate(:media_attachment, shortcode: shortcode) }
  49. let(:shortcode) { nil }
  50. context 'when media attachment has a shortcode' do
  51. let(:shortcode) { 'foo' }
  52. it 'returns shortcode' do
  53. expect(media_attachment.to_param).to eq shortcode
  54. end
  55. end
  56. context 'when media attachment does not have a shortcode' do
  57. let(:shortcode) { nil }
  58. it 'returns string representation of id' do
  59. expect(media_attachment.to_param).to eq media_attachment.id.to_s
  60. end
  61. end
  62. end
  63. describe 'animated gif conversion' do
  64. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture('avatar.gif')) }
  65. it 'sets type to gifv' do
  66. expect(media.type).to eq 'gifv'
  67. end
  68. it 'converts original file to mp4' do
  69. expect(media.file_content_type).to eq 'video/mp4'
  70. end
  71. it 'sets meta' do
  72. expect(media.file.meta["original"]["width"]).to eq 128
  73. expect(media.file.meta["original"]["height"]).to eq 128
  74. end
  75. end
  76. describe 'non-animated gif non-conversion' do
  77. fixtures = [
  78. { filename: 'attachment.gif', width: 600, height: 400, aspect: 1.5 },
  79. { filename: 'mini-static.gif', width: 32, height: 32, aspect: 1.0 },
  80. ]
  81. fixtures.each do |fixture|
  82. context fixture[:filename] do
  83. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture(fixture[:filename])) }
  84. it 'sets type to image' do
  85. expect(media.type).to eq 'image'
  86. end
  87. it 'leaves original file as-is' do
  88. expect(media.file_content_type).to eq 'image/gif'
  89. end
  90. it 'sets meta' do
  91. expect(media.file.meta["original"]["width"]).to eq fixture[:width]
  92. expect(media.file.meta["original"]["height"]).to eq fixture[:height]
  93. expect(media.file.meta["original"]["aspect"]).to eq fixture[:aspect]
  94. end
  95. end
  96. end
  97. end
  98. describe 'ogg with cover art' do
  99. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture('boop.ogg')) }
  100. it 'detects it as an audio file' do
  101. expect(media.type).to eq 'audio'
  102. end
  103. it 'sets meta for the duration' do
  104. expect(media.file.meta['original']['duration']).to be_within(0.05).of(0.235102)
  105. end
  106. it 'extracts thumbnail' do
  107. expect(media.thumbnail.present?).to eq true
  108. end
  109. it 'extracts colors from thumbnail' do
  110. expect(media.file.meta['colors']['background']).to eq '#3088d4'
  111. end
  112. it 'gives the file a random name' do
  113. expect(media.file_file_name).to_not eq 'boop.ogg'
  114. end
  115. end
  116. describe 'jpeg' do
  117. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture('attachment.jpg')) }
  118. it 'sets meta for different style' do
  119. expect(media.file.meta["original"]["width"]).to eq 600
  120. expect(media.file.meta["original"]["height"]).to eq 400
  121. expect(media.file.meta["original"]["aspect"]).to eq 1.5
  122. expect(media.file.meta["small"]["width"]).to eq 490
  123. expect(media.file.meta["small"]["height"]).to eq 327
  124. expect(media.file.meta["small"]["aspect"]).to eq 490.0 / 327
  125. end
  126. it 'gives the file a random name' do
  127. expect(media.file_file_name).to_not eq 'attachment.jpg'
  128. end
  129. end
  130. describe 'base64-encoded jpeg' do
  131. let(:base64_attachment) { "data:image/jpeg;base64,#{Base64.encode64(attachment_fixture('attachment.jpg').read)}" }
  132. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: base64_attachment) }
  133. it 'saves media attachment' do
  134. expect(media.persisted?).to be true
  135. expect(media.file).to_not be_nil
  136. end
  137. it 'gives the file a file name' do
  138. expect(media.file_file_name).to_not be_blank
  139. end
  140. end
  141. it 'is invalid without file' do
  142. media = MediaAttachment.new(account: Fabricate(:account))
  143. expect(media.valid?).to be false
  144. end
  145. describe 'descriptions for remote attachments' do
  146. it 'are cut off at 1500 characters' do
  147. media = Fabricate(:media_attachment, description: 'foo' * 1000, remote_url: 'http://example.com/blah.jpg')
  148. expect(media.description.size).to be <= 1_500
  149. end
  150. end
  151. describe 'size limit validation' do
  152. it 'rejects video files that are too large' do
  153. stub_const 'MediaAttachment::IMAGE_LIMIT', 100.megabytes
  154. stub_const 'MediaAttachment::VIDEO_LIMIT', 1.kilobyte
  155. expect { MediaAttachment.create!(account: Fabricate(:account), file: attachment_fixture('attachment.webm')) }.to raise_error(ActiveRecord::RecordInvalid)
  156. end
  157. it 'accepts video files that are small enough' do
  158. stub_const 'MediaAttachment::IMAGE_LIMIT', 1.kilobyte
  159. stub_const 'MediaAttachment::VIDEO_LIMIT', 100.megabytes
  160. media = MediaAttachment.create!(account: Fabricate(:account), file: attachment_fixture('attachment.webm'))
  161. expect(media.valid?).to be true
  162. end
  163. it 'rejects image files that are too large' do
  164. stub_const 'MediaAttachment::IMAGE_LIMIT', 1.kilobyte
  165. stub_const 'MediaAttachment::VIDEO_LIMIT', 100.megabytes
  166. expect { MediaAttachment.create!(account: Fabricate(:account), file: attachment_fixture('attachment.jpg')) }.to raise_error(ActiveRecord::RecordInvalid)
  167. end
  168. it 'accepts image files that are small enough' do
  169. stub_const 'MediaAttachment::IMAGE_LIMIT', 100.megabytes
  170. stub_const 'MediaAttachment::VIDEO_LIMIT', 1.kilobyte
  171. media = MediaAttachment.create!(account: Fabricate(:account), file: attachment_fixture('attachment.jpg'))
  172. expect(media.valid?).to be true
  173. end
  174. end
  175. end