online_notifications_spec.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::OnlineNotifications, authenticated_as: :user, type: :graphql do
  4. context 'with a notification' do
  5. let(:user) { create(:agent) }
  6. let(:notification) { create(:online_notification, user: user) }
  7. let(:another_user_notification) { create(:online_notification, user: create(:user)) }
  8. let(:query) do
  9. <<~QUERY
  10. query onlineNotifications {
  11. onlineNotifications {
  12. edges {
  13. node {
  14. id
  15. createdAt
  16. createdBy {
  17. id
  18. email
  19. }
  20. typeName
  21. objectName
  22. metaObject {
  23. ... on Ticket {
  24. id
  25. title
  26. }
  27. }
  28. }
  29. }
  30. }
  31. }
  32. QUERY
  33. end
  34. before do
  35. user.groups << Ticket.first.group
  36. notification
  37. another_user_notification
  38. end
  39. it 'contains a notification' do
  40. gql.execute(query)
  41. returned_ids = gql.result.nodes.pluck('id')
  42. expect(returned_ids).to contain_exactly(gql.id(notification))
  43. end
  44. it 'returns meta object' do
  45. gql.execute(query)
  46. expect(gql.result.nodes)
  47. .to include(include('id' => gql.id(notification), 'metaObject' => include('id' => gql.id(Ticket.first))))
  48. end
  49. context 'with notification pointing to inaccessible ticket' do
  50. let(:inaccessible_ticket) { create(:ticket) }
  51. let(:inaccessible_notification) { create(:online_notification, user: user, o: inaccessible_ticket) }
  52. before do
  53. inaccessible_notification
  54. end
  55. it 'returns list' do
  56. gql.execute(query)
  57. returned_ids = gql.result.nodes.pluck('id')
  58. expect(returned_ids).to contain_exactly(gql.id(notification), gql.id(inaccessible_notification))
  59. end
  60. it 'returns inaccessible notification with no meta object' do
  61. gql.execute(query)
  62. expect(gql.result.nodes)
  63. .to include(include('id' => gql.id(inaccessible_notification), 'metaObject' => nil, 'createdBy' => nil))
  64. end
  65. end
  66. context 'with some more notifications' do
  67. let(:notification) { nil }
  68. let(:another_user_notification) { nil }
  69. let(:notifications) { Array.new(10) { create(:online_notification, user: user, created_at: Faker::Date.between(from: 1.year.ago, to: 50.weeks.from_now).to_datetime) } }
  70. it 'returns notifications in correct order' do
  71. notifications
  72. gql.execute(query)
  73. expect(gql.result.nodes.pluck('id')).to eq(notifications.sort_by { |n| n[:created_at] }.reverse.map { |n| gql.id(n) })
  74. end
  75. end
  76. end
  77. end