Skip to content

Commit

Permalink
fix: properly handle match error (#1287)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco authored Jan 29, 2025
1 parent f3cf587 commit a3f98f2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ on:
pull_request:
branches:
- main
push:
branches:
- main
jobs:
format:
name: Tests
Expand Down
3 changes: 3 additions & 0 deletions lib/realtime_web/channels/realtime_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ defmodule RealtimeWeb.RealtimeChannel do
{:error, :expected_claims_map} ->
shutdown_response(socket, "Token claims must be a map")

{:error, :unable_to_set_policies, _msg} ->
shutdown_response(socket, "Realtime was unable to connect to the project database")

{:error, error} ->
shutdown_response(socket, inspect(error))
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
def project do
[
app: :realtime,
version: "2.34.14",
version: "2.34.15",
elixir: "~> 1.17.3",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
55 changes: 54 additions & 1 deletion test/integration/rt_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Realtime.Integration.RtChannelTest do
use RealtimeWeb.ConnCase, async: false
import ExUnit.CaptureLog
import Generators
import Mock

require Logger

Expand All @@ -19,6 +20,7 @@ defmodule Realtime.Integration.RtChannelTest do
alias Realtime.Integration.WebsocketClient
alias Realtime.Repo
alias Realtime.Tenants
alias Realtime.Tenants.Authorization
alias Realtime.Tenants.Migrations

@moduletag :capture_log
Expand Down Expand Up @@ -909,7 +911,6 @@ defmodule Realtime.Integration.RtChannelTest do

# token becomes a string in between joins so it needs to be handled by the channel and not the socket
Process.sleep(1000)
realtime_topic = "realtime:#{topic}"
WebsocketClient.join(socket, realtime_topic, %{config: config, access_token: "potato"})

assert_receive %Message{
Expand All @@ -924,6 +925,58 @@ defmodule Realtime.Integration.RtChannelTest do

assert_receive %Message{event: "phx_close"}
end

@tag policies: [
:authenticated_read_broadcast_and_presence,
:authenticated_write_broadcast_and_presence
]
test "handles RPC error on token refreshed", %{topic: topic} do
with_mocks [
{Authorization, [:passthrough], build_authorization_params: &passthrough([&1])},
{Authorization, [:passthrough],
get_read_authorizations: [
in_series([:_, :_, :_], [&passthrough([&1, &2, &3]), {:error, "RPC Error"}])
]}
] do
{socket, access_token} = get_connection("authenticated")
config = %{broadcast: %{self: true}, private: true}
realtime_topic = "realtime:#{topic}"

WebsocketClient.join(socket, realtime_topic, %{config: config, access_token: access_token})

assert_receive %Phoenix.Socket.Message{event: "phx_reply"}, 500
assert_receive %Phoenix.Socket.Message{event: "presence_state"}, 500

# Update token to force update
{:ok, access_token} =
generate_token(%{:exp => System.system_time(:second) + 1000, role: "authenticated"})

log =
capture_log(fn ->
WebsocketClient.send_event(socket, realtime_topic, "access_token", %{
"access_token" => access_token
})

assert_receive %Phoenix.Socket.Message{
event: "system",
payload: %{
"status" => "error",
"extension" => "system",
"message" =>
"Realtime was unable to connect to the project database"
},
topic: ^realtime_topic,
join_ref: nil,
ref: nil
},
500

assert_receive %Phoenix.Socket.Message{event: "phx_close", topic: ^realtime_topic}
end)

assert log =~ "Realtime was unable to connect to the project database"
end
end
end

describe "handle broadcast changes" do
Expand Down
3 changes: 2 additions & 1 deletion test/realtime_web/channels/realtime_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ defmodule RealtimeWeb.RealtimeChannelTest do
import ExUnit.CaptureLog

alias Phoenix.Socket
alias Realtime.Tenants.Authorization
alias RealtimeWeb.ChannelsAuthorization
alias RealtimeWeb.UserSocket
alias RealtimeWeb.Joken.CurrentTime
alias RealtimeWeb.UserSocket

@tenant_external_id "dev_tenant"

Expand Down

0 comments on commit a3f98f2

Please sign in to comment.