update_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Organization::Update, type: :graphql do
  4. context 'when updating organizations', authenticated_as: :user do
  5. let(:user) { create(:agent, preferences: { locale: 'de-de' }) }
  6. let(:organization) { create(:organization) }
  7. let(:variables) { { id: gql.id(organization), input: input_payload } }
  8. let(:input_payload) { {} }
  9. let(:query) do
  10. <<~QUERY
  11. mutation organizationUpdate($id: ID!, $input: OrganizationInput!) {
  12. organizationUpdate(id: $id, input: $input) {
  13. organization {
  14. id
  15. name
  16. objectAttributeValues {
  17. attribute {
  18. name
  19. }
  20. value
  21. }
  22. }
  23. errors {
  24. message
  25. field
  26. }
  27. }
  28. }
  29. QUERY
  30. end
  31. let(:custom_translations) { { "can't be blank" => 'darf nicht leer sein', 'This field %s' => 'Dieses Feld %{message}', 'This object already exists.' => 'Dieses Objekt existiert bereits.' } } # rubocop:disable Style/FormatStringToken
  32. before do
  33. allow(Translation).to receive(:translate) { |_locale, string| custom_translations[string] || string }
  34. gql.execute(query, variables: variables)
  35. end
  36. context 'when updating organization name with empty attributes' do
  37. let(:input_payload) { { name: 'NewName', objectAttributeValues: [] } }
  38. it 'returns updated organization name' do
  39. expect(gql.result.data['organization']).to include('name' => 'NewName')
  40. end
  41. end
  42. context 'when updating organization with empty name' do
  43. let(:input_payload) { { name: '' } }
  44. it 'returns a user error' do
  45. expect(gql.result.data['errors'].first).to include('field' => 'name', 'message' => 'Dieses Feld darf nicht leer sein')
  46. end
  47. end
  48. context 'when updating organization with name of another organization' do
  49. let(:input_payload) { { name: other_org.name } }
  50. let(:other_org) { create(:organization) }
  51. it 'returns a user error' do
  52. expect(gql.result.data['errors'].first).to include('message' => 'Dieses Objekt existiert bereits.')
  53. end
  54. end
  55. context 'when updating organization name without attributes' do
  56. let(:input_payload) { { name: 'NewName' } }
  57. it 'returns updated organization name' do
  58. expect(gql.result.data['organization']).to include('name' => 'NewName')
  59. end
  60. end
  61. context 'when dealing with object attributes', db_strategy: :reset do
  62. let!(:object_attributes) do
  63. screens = { create: { 'admin.organization': { shown: true, required: false } } }
  64. attribute_text = create(:object_manager_attribute_text, object_name: 'Organization',
  65. screens: screens)
  66. attribute_multiselect = create(:object_manager_attribute_multiselect, object_name: 'Organization',
  67. screens: screens)
  68. attribute_integer = create(:object_manager_attribute_integer, object_name: 'Organization',
  69. screens: screens)
  70. attribute_boolean = create(:object_manager_attribute_boolean, object_name: 'Organization',
  71. screens: screens)
  72. ObjectManager::Attribute.migration_execute
  73. {
  74. text: attribute_text,
  75. multiselect: attribute_multiselect,
  76. integer: attribute_integer,
  77. boolean: attribute_boolean,
  78. }
  79. end
  80. let(:input_payload) do
  81. {
  82. objectAttributeValues: [
  83. {
  84. name: object_attributes[:text].name,
  85. value: 'some test value',
  86. },
  87. {
  88. name: object_attributes[:multiselect].name,
  89. value: %w[key_1 key_2],
  90. },
  91. {
  92. name: object_attributes[:integer].name,
  93. value: 1337,
  94. },
  95. {
  96. name: object_attributes[:boolean].name,
  97. value: true,
  98. },
  99. ]
  100. }
  101. end
  102. it 'returns updated organization object attributes' do
  103. oas = gql.result.data['organization']['objectAttributeValues']
  104. expect(oas.map { |oa| { oa['attribute']['name'] => oa['value'] } }).to eq(
  105. [
  106. {
  107. object_attributes[:text].name => 'some test value',
  108. },
  109. {
  110. object_attributes[:multiselect].name => %w[key_1 key_2],
  111. },
  112. {
  113. object_attributes[:integer].name => 1337,
  114. },
  115. {
  116. object_attributes[:boolean].name => true,
  117. },
  118. ]
  119. )
  120. end
  121. end
  122. context 'when trying to update without having correct permissions' do
  123. let(:user) { create(:customer) }
  124. it 'raises an error' do
  125. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  126. end
  127. end
  128. end
  129. end