delete_spec.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::User::Current::Avatar::Delete, 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) { { id: gql.id(avatar) } }
  7. let(:execute_query) { true }
  8. let(:avatar) { create(:avatar, o_id: agent.id) }
  9. let(:query) do
  10. <<~QUERY
  11. mutation userCurrentAvatarDelete($id: ID!) {
  12. userCurrentAvatarDelete(id: $id) {
  13. success
  14. errors {
  15. message
  16. field
  17. }
  18. }
  19. }
  20. QUERY
  21. end
  22. before do
  23. next if !execute_query
  24. gql.execute(query, variables: variables)
  25. end
  26. context 'with existing avatar' do
  27. it 'returns success' do
  28. expect(gql.result.data[:success]).to be true
  29. end
  30. it 'does not find the avatar anymore' do
  31. expect { Avatar.find(avatar.id) }.to raise_error(ActiveRecord::RecordNotFound)
  32. end
  33. end
  34. context 'when deleting avatar of another user' do
  35. let(:avatar) { create(:avatar, o_id: 1) }
  36. it 'fails with error message' do
  37. expect(gql.result.error_message).to eq('Avatar could not be found.')
  38. end
  39. end
  40. context 'with not existing avatar' do
  41. let(:variables) { { id: 123_456_789 } }
  42. it 'fails with error message' do
  43. expect(gql.result.error_message).to eq("Could not find Avatar #{variables[:id]}")
  44. end
  45. it 'fails with error type' do
  46. expect(gql.result.error_type).to eq(ActiveRecord::RecordNotFound)
  47. end
  48. end
  49. it_behaves_like 'graphql responds with error if unauthenticated'
  50. end
  51. end