avatar_spec.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Avatar, type: :model do
  4. describe '#add' do
  5. context 'when providing urls' do
  6. let(:user) { create(:agent) }
  7. let(:headers) { { 'content-type' => content_type } }
  8. let(:response_body) { 'some sample image data' }
  9. let(:avatar_data) do
  10. {
  11. object: 'User',
  12. o_id: user.id,
  13. default: true,
  14. url: url,
  15. source: 'web',
  16. deletable: true,
  17. updated_by_id: 1,
  18. created_by_id: 1,
  19. }
  20. end
  21. before do
  22. stub_request(:get, url).to_return(status: 200, body: response_body, headers: headers)
  23. # to disable generation of previews in the store
  24. Setting.set('import_mode', true)
  25. end
  26. shared_examples 'successful avatar add' do
  27. it 'creates an avatar' do
  28. expect(Store.find_by(id: described_class.add(avatar_data).store_full_id)[:preferences].to_h).to include('Mime-Type' => content_type)
  29. end
  30. end
  31. shared_examples 'unsuccessful avatar add' do
  32. it 'does not create an avatar' do
  33. expect { described_class.add(avatar_data) }.not_to change(Store, :count)
  34. end
  35. end
  36. context 'when the url does not have a file ending' do
  37. let(:content_type) { 'image/png' }
  38. let(:url) { 'https://zammad.org/avatar' }
  39. include_examples 'successful avatar add'
  40. end
  41. context 'when the url has a file ending' do
  42. let(:content_type) { 'image/jpeg' }
  43. let(:url) { 'https://zammad.org/avatar.jpg' }
  44. include_examples 'successful avatar add'
  45. end
  46. context 'when a not allowed content-type is used' do
  47. let(:content_type) { 'image/tiff' }
  48. let(:url) { 'https://zammad.org/avatar' }
  49. include_examples 'unsuccessful avatar add'
  50. end
  51. end
  52. end
  53. end