connection_spec.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe ApplicationCable::Connection, type: :channel do
  4. context 'when user session is present' do
  5. let(:session_id) { '123_456' }
  6. before do
  7. private_session_id = Rack::Session::SessionId.new(session_id).private_id
  8. create(:active_session, session_id: private_session_id, user: user)
  9. cookies[Zammad::Application::Initializer::SessionStore::SESSION_KEY] = session_id
  10. end
  11. context 'when session contains a user' do
  12. let(:user) { create(:agent) }
  13. it 'sets current user on connecting' do
  14. connect
  15. expect(connection).to have_attributes(current_user: user, sid: session_id)
  16. end
  17. it 'connects but sets no user or sid if user in session no longer exists' do
  18. user.destroy!
  19. connect
  20. expect(connection).to have_attributes(current_user: be_nil, sid: session_id)
  21. end
  22. end
  23. context 'when session contains no user' do
  24. let(:user) { nil }
  25. it 'connects but sets no user or sid if user in session no longer exists' do
  26. connect
  27. expect(connection).to have_attributes(current_user: be_nil, sid: session_id)
  28. end
  29. end
  30. end
  31. context 'when no user session present' do
  32. it 'connects but sets no user or sid' do
  33. connect
  34. expect(connection).to have_attributes(current_user: be_nil, sid: be_nil)
  35. end
  36. end
  37. end