45 lines
1.5 KiB
Ruby
45 lines
1.5 KiB
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe ApplicationCable::Connection, type: :channel do
|
|
include ActionCable::Connection::TestCase::Behavior
|
|
|
|
let(:user) { User.create!(nickname: "CaptainCook", password: "password123") }
|
|
let(:token) { JwtService.encode(user_id: user.id) }
|
|
|
|
describe "#connect" do
|
|
it "connects successfully with token in query params" do
|
|
connect "/cable", params: { token: token }
|
|
expect(connection.current_user).to eq(user)
|
|
end
|
|
|
|
it "connects successfully with Bearer token in Authorization header" do
|
|
connect "/cable", headers: { "Authorization" => "Bearer #{token}" }
|
|
expect(connection.current_user).to eq(user)
|
|
end
|
|
|
|
it "rejects connection when no token is provided" do
|
|
expect { connect "/cable" }.to have_rejected_connection
|
|
end
|
|
|
|
it "rejects connection when token is invalid" do
|
|
expect {
|
|
connect "/cable", params: { token: "invalid.jwt.token" }
|
|
}.to have_rejected_connection
|
|
end
|
|
|
|
it "rejects connection when token is expired" do
|
|
expired_token = JwtService.encode(user_id: user.id, exp: 1.hour.ago.to_i)
|
|
expect {
|
|
connect "/cable", params: { token: expired_token }
|
|
}.to have_rejected_connection
|
|
end
|
|
|
|
it "rejects connection when user does not exist in the database" do
|
|
orphan_token = JwtService.encode(user_id: 999_999)
|
|
expect {
|
|
connect "/cable", params: { token: orphan_token }
|
|
}.to have_rejected_connection
|
|
end
|
|
end
|
|
end
|