taskbar_item_updates_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::User::Current::TaskbarItemUpdates, type: :graphql do
  4. let(:user) { create(:agent) }
  5. let(:app) { 'desktop' }
  6. let(:variables) { { userId: gql.id(user), app: app } }
  7. let(:mock_channel) { build_mock_channel }
  8. let(:subscription) do
  9. <<~QUERY
  10. subscription userCurrentTaskbarItemUpdates($userId: ID!, $app: EnumTaskbarApp!) {
  11. userCurrentTaskbarItemUpdates(userId: $userId, app: $app) {
  12. addItem {
  13. app
  14. key
  15. }
  16. updateItem {
  17. app
  18. key
  19. }
  20. removeItem
  21. }
  22. }
  23. QUERY
  24. end
  25. context 'with not authenticated user' do
  26. it 'does not subscribe to taskbar item updates and returns an authorization error' do
  27. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  28. expect(gql.result.error_type).to eq(Exceptions::NotAuthorized)
  29. end
  30. end
  31. context 'with authenticated user', authenticated_as: :user do
  32. it 'subscribes to taskbar item updates' do
  33. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  34. expect(gql.result.data).not_to be_nil
  35. end
  36. it 'triggers after create' do
  37. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  38. create(:taskbar, user_id: user.id, app: app, key: 'key')
  39. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['userCurrentTaskbarItemUpdates']
  40. expect(result).to eq(
  41. { 'addItem' => { 'app' => app, 'key' => 'key' }, 'removeItem' => nil, 'updateItem' => nil }
  42. )
  43. end
  44. it 'triggers after update' do
  45. item = create(:taskbar, user_id: user.id, app: app, key: 'key')
  46. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  47. item.update!(key: 'new_key')
  48. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['userCurrentTaskbarItemUpdates']
  49. expect(result).to eq(
  50. { 'addItem' => nil, 'removeItem' => nil, 'updateItem' => { 'app' => app, 'key' => 'new_key' } }
  51. )
  52. end
  53. it 'triggers after remove' do
  54. item = create(:taskbar, user_id: user.id, app: app, key: 'key')
  55. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  56. gid = Gql::ZammadSchema.id_from_object(item)
  57. item.destroy!
  58. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['userCurrentTaskbarItemUpdates']
  59. expect(result).to eq(
  60. { 'addItem' => nil, 'removeItem' => gid, 'updateItem' => nil }
  61. )
  62. end
  63. context 'with different target app' do
  64. let(:app) { 'mobile' }
  65. it 'does not trigger' do
  66. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  67. create(:taskbar, user_id: user.id, app: 'desktop', key: 'key')
  68. expect(mock_channel.mock_broadcasted_messages).to be_empty
  69. end
  70. end
  71. context 'with different target user' do
  72. let(:another_user) { create(:agent) }
  73. let(:variables) { { userId: gql.id(another_user), app: app } }
  74. it 'does not subscribe to taskbar item updates and returns a forbidden error' do
  75. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  76. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  77. end
  78. end
  79. end
  80. end