online_notifications_spec.rb 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. # Don't use relative dates here as they disable generation of unique values.
  70. let(:notifications) { Array.new(10) { create(:online_notification, user: user, created_at: Faker::Date.unique.between(from: Date.parse('2022-01-01'), to: Date.parse('2024-01-01')).to_datetime) } }
  71. it 'returns notifications in correct order' do
  72. notifications
  73. gql.execute(query)
  74. expect(gql.result.nodes.pluck('id')).to eq(notifications.sort_by { |n| n[:created_at] }.reverse.map { |n| gql.id(n) })
  75. end
  76. end
  77. end
  78. end