update_spec.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # Copyright (C) 2012-2024 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) do
  32. {
  33. "can't be blank" => 'darf nicht leer sein',
  34. 'has already been taken' => 'wird bereits verwendet.',
  35. 'This field %s' => 'Dieses Feld %{message}',
  36. 'This object already exists.' => 'Dieses Objekt existiert bereits.'
  37. }
  38. end
  39. before do
  40. allow(Translation).to receive(:translate) { |_locale, string| custom_translations[string] || string }
  41. gql.execute(query, variables: variables)
  42. end
  43. context 'when updating organization name with empty attributes' do
  44. let(:input_payload) { { name: 'NewName', objectAttributeValues: [] } }
  45. it 'returns updated organization name' do
  46. expect(gql.result.data[:organization]).to include('name' => 'NewName')
  47. end
  48. end
  49. context 'when updating organization with empty name' do
  50. let(:input_payload) { { name: '' } }
  51. it 'returns a user error' do
  52. expect(gql.result.data[:errors].first).to include('field' => 'name', 'message' => 'Dieses Feld darf nicht leer sein')
  53. end
  54. end
  55. context 'when updating organization with name of another organization' do
  56. let(:input_payload) { { name: other_org.name } }
  57. let(:other_org) { create(:organization) }
  58. it 'returns a user error' do
  59. expect(gql.result.data[:errors].first).to include('field' => 'name', 'message' => 'Dieses Feld wird bereits verwendet.')
  60. end
  61. end
  62. context 'when updating organization name without attributes' do
  63. let(:input_payload) { { name: 'NewName' } }
  64. it 'returns updated organization name' do
  65. expect(gql.result.data[:organization]).to include('name' => 'NewName')
  66. end
  67. end
  68. context 'when dealing with object attributes', db_strategy: :reset do
  69. let!(:object_attributes) do
  70. screens = { create: { 'admin.organization': { shown: true, required: false } } }
  71. attribute_text = create(:object_manager_attribute_text, object_name: 'Organization',
  72. screens: screens)
  73. attribute_multiselect = create(:object_manager_attribute_multiselect, object_name: 'Organization',
  74. screens: screens)
  75. attribute_integer = create(:object_manager_attribute_integer, object_name: 'Organization',
  76. screens: screens)
  77. attribute_boolean = create(:object_manager_attribute_boolean, object_name: 'Organization',
  78. screens: screens)
  79. ObjectManager::Attribute.migration_execute
  80. {
  81. text: attribute_text,
  82. multiselect: attribute_multiselect,
  83. integer: attribute_integer,
  84. boolean: attribute_boolean,
  85. }
  86. end
  87. let(:input_payload) do
  88. {
  89. objectAttributeValues: [
  90. {
  91. name: object_attributes[:text].name,
  92. value: 'some test value',
  93. },
  94. {
  95. name: object_attributes[:multiselect].name,
  96. value: %w[key_1 key_2],
  97. },
  98. {
  99. name: object_attributes[:integer].name,
  100. value: 1337,
  101. },
  102. {
  103. name: object_attributes[:boolean].name,
  104. value: true,
  105. },
  106. ]
  107. }
  108. end
  109. it 'returns updated organization object attributes' do
  110. oas = gql.result.data[:organization][:objectAttributeValues]
  111. expect(oas.map { |oa| { oa['attribute']['name'] => oa['value'] } }).to eq(
  112. [
  113. {
  114. object_attributes[:text].name => 'some test value',
  115. },
  116. {
  117. object_attributes[:multiselect].name => %w[key_1 key_2],
  118. },
  119. {
  120. object_attributes[:integer].name => 1337,
  121. },
  122. {
  123. object_attributes[:boolean].name => true,
  124. },
  125. ]
  126. )
  127. end
  128. end
  129. context 'when trying to update without having correct permissions' do
  130. let(:user) { create(:customer) }
  131. it 'raises an error' do
  132. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  133. end
  134. end
  135. end
  136. end