product_logo_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::SystemAssets::ProductLogo do
  4. let(:base64) { 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' }
  5. let(:data) { Rails.root.join('test/data/image/1000x1000.png').binread }
  6. let(:raw_logo) do
  7. create(:store,
  8. object: 'System::Logo',
  9. o_id: described_class::PRODUCT_LOGO_RAW,
  10. data: 'example_raw',
  11. filename: 'logo_raw',
  12. preferences: { 'Content-Type' => 'image/png' })
  13. end
  14. let(:resized_logo) do
  15. create(:store,
  16. object: 'System::Logo',
  17. o_id: described_class::PRODUCT_LOGO_RESIZED,
  18. data: 'example',
  19. filename: 'logo',
  20. preferences: { 'Content-Type' => 'image/png' })
  21. end
  22. describe '.sendable_asset' do
  23. it 'returns default logo' do
  24. expect(described_class.sendable_asset)
  25. .to have_attributes(
  26. content: be_present,
  27. filename: 'logo.svg',
  28. type: 'image/svg+xml'
  29. )
  30. end
  31. it 'returns custom logo when present' do
  32. raw_logo
  33. expect(described_class.sendable_asset)
  34. .to have_attributes(
  35. content: 'example_raw',
  36. filename: 'logo_raw',
  37. type: 'image/png'
  38. )
  39. end
  40. it 'returns custom resized logo when present' do
  41. raw_logo
  42. resized_logo
  43. expect(described_class.sendable_asset)
  44. .to have_attributes(
  45. content: 'example',
  46. filename: 'logo',
  47. type: 'image/png'
  48. )
  49. end
  50. end
  51. describe '.store_logo' do
  52. before do
  53. raw_logo
  54. resized_logo
  55. described_class.store_logo(preprocessed)
  56. end
  57. let(:preprocessed) { ImageHelper.data_url_attributes base64 }
  58. it 'sets resized logos to empty' do
  59. expect(Store.list(object: 'System::Logo', o_id: described_class::PRODUCT_LOGO_RESIZED))
  60. .to be_blank
  61. end
  62. it 'stores raw logo' do
  63. stored = Store.list(object: 'System::Logo', o_id: described_class::PRODUCT_LOGO_RAW).first
  64. expect(stored.content).to eq preprocessed[:content]
  65. end
  66. end
  67. describe '.store', aggregate_failures: true do
  68. it 'returns timestamp' do
  69. freeze_time
  70. expect(described_class.store(data)).to eq(Time.current.to_i)
  71. end
  72. it 'returns nil if given invalid data' do
  73. expect(described_class.store('invalid string')).to be_nil
  74. end
  75. it 'stores original logo' do
  76. described_class.store(data)
  77. Store.list(object: 'System::Logo', o_id: described_class::PRODUCT_LOGO_RAW)
  78. expect(Store.list(object: 'System::Logo', o_id: described_class::PRODUCT_LOGO_RAW)).to be_present
  79. end
  80. it 'stores resized logo' do
  81. described_class.store(nil, data)
  82. Store.list(object: 'System::Logo', o_id: described_class::PRODUCT_LOGO_RESIZED)
  83. expect(Store.list(object: 'System::Logo', o_id: described_class::PRODUCT_LOGO_RESIZED)).to be_present
  84. expect(Store.list(object: 'System::Logo', o_id: described_class::PRODUCT_LOGO_RAW)).to be_blank
  85. end
  86. it 'resized original logo and stores as resized' do
  87. described_class.store(data)
  88. stored = Store.list(object: 'System::Logo', o_id: described_class::PRODUCT_LOGO_RESIZED).first
  89. expect(Rszr::Image.load_data(stored.content))
  90. .to have_attributes(width: 100, height: 100)
  91. end
  92. context 'when logos exist beforehand' do
  93. before do
  94. raw_logo
  95. resized_logo
  96. end
  97. it 'stores both logos' do
  98. expect { described_class.store(data) }
  99. .to change { Store.list(object: 'System::Logo', o_id: described_class::PRODUCT_LOGO_RESIZED).first.id }
  100. .and change { Store.list(object: 'System::Logo', o_id: described_class::PRODUCT_LOGO_RAW).first.id }
  101. end
  102. it 'does not change logos if given content is invalid' do
  103. expect(Store.list(object: 'System::Logo', o_id: described_class::PRODUCT_LOGO_RAW)).to be_present
  104. expect(Store.list(object: 'System::Logo', o_id: described_class::PRODUCT_LOGO_RESIZED)).to be_present
  105. end
  106. end
  107. end
  108. end