Skip to content

Commit

Permalink
Fix string vs atom keys issue (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
doorgan authored Jun 6, 2024
1 parent caafc09 commit 4e7503c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/channel_spec/testing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,16 @@ defmodule ChannelSpec.Testing do
socket = Process.get(unquote(ref))
assert_reply(unquote(ref), unquote(status), reply = unquote(reply))

normalized_reply = reply |> Jason.encode!() |> Jason.decode!()

with true <- function_exported?(socket.handler, :__socket_schemas__, 0),
socket_schema = socket.handler.__socket_schemas__(),
topic = socket.assigns.__channel_topic__,
event = socket.assigns.__event__,
status = to_string(unquote(status)),
%{} = schema <-
socket_schema["channels"][topic]["messages"][event]["replies"][status] do
case Xema.validate(schema, reply) do
case Xema.validate(schema, normalized_reply) do
:ok ->
:ok

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule ChannelSpec.MixProject do
{:mneme, "~> 0.5", only: [:dev, :test]},
{:json_xema, "~> 0.6"},
{:phoenix, "~> 1.7"},
{:xema, "~> 0.17"}
{:xema, "~> 0.17.2"}
]
end

Expand Down
76 changes: 76 additions & 0 deletions test/channel_spec/testing_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,82 @@ defmodule ChannelSpec.TestingTest do
assert error.message =~ "Channel reply doesn't match reply spec for status ok"
end

@tag :capture_log
test "validates atom or string keys in the payload", %{mod: mod} do
defmodule :"#{mod}.ReplySchema" do
def schema() do
%{
type: :object,
properties: %{foo: %{type: :object, properties: %{bar: %{type: :integer}}}}
}
end
end

defmodule :"#{mod}.RoomChannel.Handler" do
use ChannelHandler.Handler
use ChannelSpec.Operations

operation :msg,
payload: %{type: :object, properties: %{body: %{type: :string}}, required: [:body]},
replies: %{
ok: %{"$ref": :"#{mod}.ReplySchema"}
}

def msg(%{"body" => "atom"}, _, socket) do
{:reply, {:ok, %{foo: %{bar: 123}}}, socket}
end

def msg(%{"body" => "string"}, _, socket) do
{:reply, {:ok, %{"foo" => %{"bar" => 123}}}, socket}
end
end

defmodule :"#{mod}.RoomChannel" do
use Phoenix.Channel
use ChannelHandler.Router

def join("room:" <> _, _params, socket) do
{:ok, socket}
end

event "new_msg", :"#{mod}.RoomChannel.Handler", :msg
end

defmodule :"#{mod}.UserSocket" do

Check warning on line 166 in test/channel_spec/testing_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.13.4, 24.3)

function id/1 required by behaviour Phoenix.Socket is not implemented (in module Test2755.UserSocket)

Check warning on line 166 in test/channel_spec/testing_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.14.3, 25.2)

function id/1 required by behaviour Phoenix.Socket is not implemented (in module Test1122.UserSocket)
use ChannelSpec.Socket

channel "room:*", :"#{mod}.RoomChannel"
end

defmodule :"#{mod}.Endpoint" do
use Phoenix.Endpoint, otp_app: :channel_spec

Phoenix.Endpoint.socket("/socket", :"#{mod}.UserSocket")

defoverridable config: 1, config: 2
def config(:pubsub_server), do: __MODULE__.PubSub
def config(which), do: super(which)
def config(which, default), do: super(which, default)
end

start_supervised({Phoenix.PubSub, name: :"#{mod}.Endpoint.PubSub"})

{:ok, _endpoint_pid} = start_supervised(:"#{mod}.Endpoint")

{:ok, _, socket} =
:"#{mod}.UserSocket"
|> build_socket("room:123", %{}, :"#{mod}.Endpoint")
|> subscribe_and_join(:"#{mod}.RoomChannel", "room:123")

ref = push(socket, "new_msg", %{"body" => "atom"})

assert_reply_spec ref, :ok, %{foo: %{bar: 123}}

ref = push(socket, "new_msg", %{"body" => "string"})

assert_reply_spec ref, :ok, %{"foo" => %{"bar" => 123}}
end

@tag :capture_log
test "validates response against the schema in a handler module", %{mod: mod} do
defmodule :"#{mod}.RoomChannel.Handler" do
Expand Down

0 comments on commit 4e7503c

Please sign in to comment.