seen_spec.rb 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::OnlineNotification::Seen, :aggregate_failures, type: :graphql do
  4. context 'when setting online notifications for different objects to seen' do
  5. let(:agent) { create(:agent) }
  6. let(:notification) { create(:online_notification, user: agent, o: object) }
  7. let(:other_notifications) { create_list(:online_notification, 5, user: create(:agent), o: object) }
  8. let(:user_with_permission) { agent }
  9. let(:user_without_permission) { agent }
  10. let(:query) do
  11. <<~QUERY
  12. mutation onlineNotificationSeen($objectId: ID!) {
  13. onlineNotificationSeen(objectId: $objectId) {
  14. success
  15. }
  16. }
  17. QUERY
  18. end
  19. let(:variables) do
  20. {
  21. objectId: gql.id(object),
  22. }
  23. end
  24. shared_context 'when mobile: notification seen handling' do
  25. context 'with permissions', authenticated_as: :user_with_permission do
  26. before do
  27. object && notification && other_notifications
  28. if object.respond_to?(:group)
  29. agent.groups << object.group
  30. end
  31. gql.execute(query, variables: variables)
  32. end
  33. it 'marks the existing notification as seen' do
  34. expect(gql.result.data[:success]).to be true
  35. expect(notification.reload).to have_attributes(seen: true)
  36. end
  37. it 'does not mark other notifications for the same object as seen' do
  38. expect(other_notifications.map { |x| x.reload.seen }).to all(be(false))
  39. end
  40. end
  41. context 'without permission', authenticated_as: :user_without_permission do
  42. before do
  43. object && notification && other_notifications
  44. gql.execute(query, variables: variables)
  45. end
  46. it 'results in an error' do
  47. expect(gql.result.error_type).to eq Exceptions::Forbidden
  48. expect(gql.result.error_message).to match %r{not allowed to .*Policy#show\? this #{object.class.name}}
  49. end
  50. end
  51. end
  52. context 'with Ticket model' do
  53. let(:group) { create(:group, email_address: nil) }
  54. let(:object) { create(:ticket, group: group) }
  55. include_context 'when mobile: notification seen handling'
  56. end
  57. context 'with User model' do
  58. let(:user_with_permission) { agent }
  59. let(:user_without_permission) { create(:user) }
  60. let(:object) { create(:user) }
  61. include_context 'when mobile: notification seen handling'
  62. end
  63. context 'with Organization model' do
  64. let(:user_with_permission) { agent }
  65. let(:user_without_permission) { create(:user) }
  66. let(:object) { create(:organization) }
  67. include_context 'when mobile: notification seen handling'
  68. end
  69. end
  70. end