add_spec.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Account::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: { full: base64_img, resize: base64_img } } }
  7. let(:base64_img) { "data:#{mime_type};base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" }
  8. let(:mime_type) { 'image/png' }
  9. let(:execute_query) { true }
  10. let(:query) do
  11. gql.read_files('apps/mobile/modules/account/avatar/graphql/mutations/add.graphql', 'shared/graphql/fragments/errors.graphql')
  12. end
  13. before do
  14. next if !execute_query
  15. gql.execute(query, variables: variables)
  16. end
  17. context 'with valid image' do
  18. it 'returns the newly created avatar' do
  19. expect(gql.result.data['avatar']).not_to be_nil
  20. end
  21. it 'updates the image for the user' do
  22. avatar = Gql::ZammadSchema.verified_object_from_id(gql.result.data['avatar']['id'], type: Avatar)
  23. expect(agent.reload.image).to eq(avatar.store_hash)
  24. end
  25. context 'when checking the Store' do
  26. let(:execute_query) { false }
  27. it 'increases the amount of records correctly' do
  28. expect { gql.execute(query, variables: variables) }.to change(Store, :count).by(2)
  29. end
  30. end
  31. end
  32. context 'with invalid image' do
  33. let(:base64_img) { 'invalid image' }
  34. it 'fails with error message' do
  35. expect(gql.result.data['errors'][0]).to include('message' => 'The image is invalid.')
  36. end
  37. end
  38. context 'with invalid mime-type' do
  39. let(:mime_type) { 'image/tiff' }
  40. it 'fails with error message' do
  41. expect(gql.result.data['errors'][0]).to include('message' => 'The MIME type of the image is invalid.')
  42. end
  43. end
  44. it_behaves_like 'graphql responds with error if unauthenticated'
  45. end
  46. end