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.

184 lines
5.2 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) }
  49. let(:shortcode) { media_attachment.shortcode }
  50. it 'returns shortcode' do
  51. expect(media_attachment.to_param).to eq shortcode
  52. end
  53. end
  54. describe 'animated gif conversion' do
  55. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture('avatar.gif')) }
  56. it 'sets type to gifv' do
  57. expect(media.type).to eq 'gifv'
  58. end
  59. it 'converts original file to mp4' do
  60. expect(media.file_content_type).to eq 'video/mp4'
  61. end
  62. it 'sets meta' do
  63. expect(media.file.meta["original"]["width"]).to eq 128
  64. expect(media.file.meta["original"]["height"]).to eq 128
  65. end
  66. end
  67. describe 'non-animated gif non-conversion' do
  68. fixtures = [
  69. { filename: 'attachment.gif', width: 600, height: 400, aspect: 1.5 },
  70. { filename: 'mini-static.gif', width: 32, height: 32, aspect: 1.0 },
  71. ]
  72. fixtures.each do |fixture|
  73. context fixture[:filename] do
  74. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture(fixture[:filename])) }
  75. it 'sets type to image' do
  76. expect(media.type).to eq 'image'
  77. end
  78. it 'leaves original file as-is' do
  79. expect(media.file_content_type).to eq 'image/gif'
  80. end
  81. it 'sets meta' do
  82. expect(media.file.meta["original"]["width"]).to eq fixture[:width]
  83. expect(media.file.meta["original"]["height"]).to eq fixture[:height]
  84. expect(media.file.meta["original"]["aspect"]).to eq fixture[:aspect]
  85. end
  86. end
  87. end
  88. end
  89. describe 'ogg with cover art' do
  90. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture('boop.ogg')) }
  91. it 'detects it as an audio file' do
  92. expect(media.type).to eq 'audio'
  93. end
  94. it 'sets meta for the duration' do
  95. expect(media.file.meta['original']['duration']).to be_within(0.05).of(0.235102)
  96. end
  97. it 'extracts thumbnail' do
  98. expect(media.thumbnail.present?).to eq true
  99. end
  100. it 'extracts colors from thumbnail' do
  101. expect(media.file.meta['colors']['background']).to eq '#3088d4'
  102. end
  103. it 'gives the file a random name' do
  104. expect(media.file_file_name).to_not eq 'boop.ogg'
  105. end
  106. end
  107. describe 'jpeg' do
  108. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture('attachment.jpg')) }
  109. it 'sets meta for different style' do
  110. expect(media.file.meta["original"]["width"]).to eq 600
  111. expect(media.file.meta["original"]["height"]).to eq 400
  112. expect(media.file.meta["original"]["aspect"]).to eq 1.5
  113. expect(media.file.meta["small"]["width"]).to eq 490
  114. expect(media.file.meta["small"]["height"]).to eq 327
  115. expect(media.file.meta["small"]["aspect"]).to eq 490.0 / 327
  116. end
  117. it 'gives the file a random name' do
  118. expect(media.file_file_name).to_not eq 'attachment.jpg'
  119. end
  120. end
  121. describe 'base64-encoded jpeg' do
  122. let(:base64_attachment) { "data:image/jpeg;base64,#{Base64.encode64(attachment_fixture('attachment.jpg').read)}" }
  123. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: base64_attachment) }
  124. it 'saves media attachment' do
  125. expect(media.persisted?).to be true
  126. expect(media.file).to_not be_nil
  127. end
  128. it 'gives the file a file name' do
  129. expect(media.file_file_name).to_not be_blank
  130. end
  131. end
  132. it 'is invalid without file' do
  133. media = MediaAttachment.new(account: Fabricate(:account))
  134. expect(media.valid?).to be false
  135. end
  136. describe 'descriptions for remote attachments' do
  137. it 'are cut off at 1500 characters' do
  138. media = Fabricate(:media_attachment, description: 'foo' * 1000, remote_url: 'http://example.com/blah.jpg')
  139. expect(media.description.size).to be <= 1_500
  140. end
  141. end
  142. end