notification_preferences_update_spec.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::User::Current::NotificationPreferencesUpdate, :aggregate_failures, type: :graphql do
  4. let(:user) { create(:agent) }
  5. let(:mutation) do
  6. <<~GQL
  7. mutation userCurrentNotificationPreferencesUpdate($groupIds: [ID!], $matrix: UserNotificationMatrixInput!, $sound: UserNotificationSoundInput!) {
  8. userCurrentNotificationPreferencesUpdate(groupIds: $groupIds, matrix: $matrix, sound: $sound) {
  9. user {
  10. personalSettings {
  11. notificationConfig {
  12. groupIds
  13. matrix {
  14. create {
  15. channel {
  16. email
  17. online
  18. }
  19. criteria {
  20. ownedByMe
  21. ownedByNobody
  22. subscribed
  23. no
  24. }
  25. }
  26. update {
  27. channel {
  28. email
  29. online
  30. }
  31. criteria {
  32. ownedByMe
  33. ownedByNobody
  34. subscribed
  35. no
  36. }
  37. }
  38. reminderReached {
  39. channel {
  40. email
  41. online
  42. }
  43. criteria {
  44. ownedByMe
  45. ownedByNobody
  46. subscribed
  47. no
  48. }
  49. }
  50. escalation {
  51. channel {
  52. email
  53. online
  54. }
  55. criteria {
  56. ownedByMe
  57. ownedByNobody
  58. subscribed
  59. no
  60. }
  61. }
  62. }
  63. }
  64. notificationSound {
  65. enabled
  66. file
  67. }
  68. }
  69. }
  70. }
  71. }
  72. GQL
  73. end
  74. let(:matrix_row) do
  75. {
  76. 'channel' => { 'email' => false, 'online' => true },
  77. 'criteria' => { 'ownedByMe' => true, 'ownedByNobody' => false, 'subscribed' => true, 'no' => false },
  78. }
  79. end
  80. let(:matrix) do
  81. {
  82. 'create' => matrix_row.dup,
  83. 'update' => matrix_row.dup,
  84. 'reminderReached' => matrix_row.dup,
  85. 'escalation' => matrix_row.dup,
  86. }
  87. end
  88. let(:group_ids) { nil }
  89. let(:sound) { { 'enabled' => true, 'file' => 'Bell' } }
  90. let(:variables) { { groupIds: group_ids, matrix:, sound: } }
  91. def execute_graphql_query
  92. gql.execute(mutation, variables: variables)
  93. end
  94. context 'when user is not authenticated' do
  95. it 'returns an error' do
  96. expect(execute_graphql_query.error_message).to eq('Authentication required')
  97. end
  98. end
  99. context 'when user is authenticated', authenticated_as: :user do
  100. context 'without sufficient permissions', authenticated_as: :user do
  101. let(:user) do
  102. create(:agent).tap do |user|
  103. user.roles.each { |role| role.permission_revoke('user_preferences') }
  104. end
  105. end
  106. it 'returns an error' do
  107. expect(execute_graphql_query.error_type).to eq(Exceptions::Forbidden)
  108. end
  109. end
  110. context 'with sufficient permissions' do
  111. context 'without group_ids' do
  112. let(:expected_preferences) do
  113. {
  114. 'notificationConfig' => {
  115. 'matrix' => matrix,
  116. 'groupIds' => nil,
  117. },
  118. 'notificationSound' => sound
  119. }
  120. end
  121. it 'updates user profile notification settings' do
  122. execute_graphql_query
  123. expect(gql.result.data[:user][:personalSettings]).to include(expected_preferences)
  124. end
  125. end
  126. context 'with empty groupIds' do
  127. let(:group_ids) { [] }
  128. let(:expected_preferences) do
  129. {
  130. 'notificationConfig' => {
  131. 'matrix' => matrix,
  132. 'groupIds' => nil,
  133. },
  134. 'notificationSound' => sound
  135. }
  136. end
  137. it 'updates user profile notification settings' do
  138. execute_graphql_query
  139. expect(gql.result.data[:user][:personalSettings]).to include(expected_preferences)
  140. end
  141. end
  142. context 'with group_ids' do
  143. let(:group_ids) { groups.map { |group| gql.id(group) } }
  144. let(:groups) do
  145. create_list(:group, 2).tap do |groups|
  146. user.groups << groups
  147. user.save!
  148. end
  149. end
  150. let(:expected_preferences) do
  151. {
  152. 'notificationConfig' => {
  153. 'matrix' => matrix,
  154. 'groupIds' => groups.map(&:id),
  155. },
  156. 'notificationSound' => sound
  157. }
  158. end
  159. it 'updates user profile notification settings' do
  160. execute_graphql_query
  161. expect(gql.result.data[:user][:personalSettings]).to include(expected_preferences)
  162. end
  163. end
  164. end
  165. end
  166. end