media_attachment_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 blank' do
  25. let(:remote_url) { '' }
  26. it 'returns false' do
  27. is_expected.to be false
  28. end
  29. end
  30. context 'remote_url is present' do
  31. let(:remote_url) { 'remote_url' }
  32. it 'returns true' do
  33. is_expected.to be true
  34. end
  35. end
  36. end
  37. context 'file is present' do
  38. let(:file) { attachment_fixture('avatar.gif') }
  39. context 'remote_url is blank' do
  40. let(:remote_url) { '' }
  41. it 'returns false' do
  42. is_expected.to be false
  43. end
  44. end
  45. context 'remote_url is present' do
  46. let(:remote_url) { 'remote_url' }
  47. it 'returns true' do
  48. is_expected.to be false
  49. end
  50. end
  51. end
  52. end
  53. describe '#to_param' do
  54. let(:media_attachment) { Fabricate(:media_attachment) }
  55. let(:shortcode) { media_attachment.shortcode }
  56. it 'returns shortcode' do
  57. expect(media_attachment.to_param).to eq shortcode
  58. end
  59. end
  60. describe 'animated gif conversion' do
  61. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture('avatar.gif')) }
  62. it 'sets type to gifv' do
  63. expect(media.type).to eq 'gifv'
  64. end
  65. it 'converts original file to mp4' do
  66. expect(media.file_content_type).to eq 'video/mp4'
  67. end
  68. it 'sets meta' do
  69. expect(media.file.meta["original"]["width"]).to eq 128
  70. expect(media.file.meta["original"]["height"]).to eq 128
  71. end
  72. end
  73. describe 'non-animated gif non-conversion' do
  74. fixtures = [
  75. { filename: 'attachment.gif', width: 600, height: 400, aspect: 1.5 },
  76. { filename: 'mini-static.gif', width: 32, height: 32, aspect: 1.0 },
  77. ]
  78. fixtures.each do |fixture|
  79. context fixture[:filename] do
  80. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture(fixture[:filename])) }
  81. it 'sets type to image' do
  82. expect(media.type).to eq 'image'
  83. end
  84. it 'leaves original file as-is' do
  85. expect(media.file_content_type).to eq 'image/gif'
  86. end
  87. it 'sets meta' do
  88. expect(media.file.meta["original"]["width"]).to eq fixture[:width]
  89. expect(media.file.meta["original"]["height"]).to eq fixture[:height]
  90. expect(media.file.meta["original"]["aspect"]).to eq fixture[:aspect]
  91. end
  92. end
  93. end
  94. end
  95. describe 'jpeg' do
  96. let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture('attachment.jpg')) }
  97. it 'sets meta for different style' do
  98. expect(media.file.meta["original"]["width"]).to eq 600
  99. expect(media.file.meta["original"]["height"]).to eq 400
  100. expect(media.file.meta["original"]["aspect"]).to eq 1.5
  101. expect(media.file.meta["small"]["width"]).to eq 490
  102. expect(media.file.meta["small"]["height"]).to eq 327
  103. expect(media.file.meta["small"]["aspect"]).to eq 490.0 / 327
  104. end
  105. end
  106. describe 'descriptions for remote attachments' do
  107. it 'are cut off at 140 characters' do
  108. media = Fabricate(:media_attachment, description: 'foo' * 1000, remote_url: 'http://example.com/blah.jpg')
  109. expect(media.description.size).to be <= 420
  110. end
  111. end
  112. end