add_spec.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::User::Current::Avatar::Add, type: :graphql do
  4. context 'when creating a new avatar for the logged-in user', authenticated_as: :agent do
  5. let(:agent) { create(:agent) }
  6. let(:variables) { { images: { original: upload, resized: upload } } }
  7. let(:upload) { { name: filename, type: type, content: image_data } }
  8. let(:image_data) { 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' }
  9. let(:type) { 'image/png' }
  10. let(:filename) { 'avatar.png' }
  11. let(:execute_query) { true }
  12. let(:query) do
  13. <<~QUERY
  14. mutation userCurrentAvatarAdd($images: AvatarInput!) {
  15. userCurrentAvatarAdd(images: $images) {
  16. avatar {
  17. id
  18. }
  19. errors {
  20. message
  21. field
  22. }
  23. }
  24. }
  25. QUERY
  26. end
  27. before do
  28. next if !execute_query
  29. gql.execute(query, variables: variables)
  30. end
  31. context 'with valid image' do
  32. it 'returns the newly created avatar' do
  33. expect(gql.result.data[:avatar]).not_to be_nil
  34. end
  35. it 'updates the image for the user' do
  36. avatar = Gql::ZammadSchema.verified_object_from_id(gql.result.data[:avatar][:id], type: Avatar)
  37. expect(agent.reload.image).to eq(avatar.store_hash)
  38. end
  39. context 'when checking the Store' do
  40. let(:execute_query) { false }
  41. it 'increases the amount of records correctly' do
  42. expect { gql.execute(query, variables: variables) }.to change(Store, :count).by(2)
  43. end
  44. end
  45. end
  46. context 'with invalid image' do
  47. let(:image_data) { 'invalid image' }
  48. it 'fails with error message' do
  49. expect(gql.result.error_message).to eq('Variable $images of type AvatarInput! was provided invalid value for original.content (invalid base64), resized.content (invalid base64)')
  50. end
  51. end
  52. context 'with invalid mime-type' do
  53. let(:type) { 'image/tiff' }
  54. it 'fails with error message' do
  55. expect(gql.result.data[:errors][0]).to include('message' => 'The MIME type of the image is invalid.')
  56. end
  57. end
  58. it_behaves_like 'graphql responds with error if unauthenticated'
  59. end
  60. end