media_attachment_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 'jpeg' do
  90. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture('attachment.jpg')) }
  91. it 'sets meta for different style' do
  92. expect(media.file.meta["original"]["width"]).to eq 600
  93. expect(media.file.meta["original"]["height"]).to eq 400
  94. expect(media.file.meta["original"]["aspect"]).to eq 1.5
  95. expect(media.file.meta["small"]["width"]).to eq 490
  96. expect(media.file.meta["small"]["height"]).to eq 327
  97. expect(media.file.meta["small"]["aspect"]).to eq 490.0 / 327
  98. end
  99. it 'gives the file a random name' do
  100. expect(media.file_file_name).to_not eq 'attachment.jpg'
  101. end
  102. end
  103. describe 'base64-encoded jpeg' do
  104. let(:base64_attachment) { "data:image/jpeg;base64,#{Base64.encode64(attachment_fixture('attachment.jpg').read)}" }
  105. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: base64_attachment) }
  106. it 'saves media attachment' do
  107. expect(media.persisted?).to be true
  108. expect(media.file).to_not be_nil
  109. end
  110. it 'gives the file a file name' do
  111. expect(media.file_file_name).to_not be_blank
  112. end
  113. end
  114. it 'is invalid without file' do
  115. media = MediaAttachment.new(account: Fabricate(:account))
  116. expect(media.valid?).to be false
  117. end
  118. describe 'descriptions for remote attachments' do
  119. it 'are cut off at 1500 characters' do
  120. media = Fabricate(:media_attachment, description: 'foo' * 1000, remote_url: 'http://example.com/blah.jpg')
  121. expect(media.description.size).to be <= 1_500
  122. end
  123. end
  124. end