Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix string vs atom keys issue #14

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -37,7 +37,7 @@
end
end

defmodule :"#{mod}.UserSocket" do

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

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.13.4, 25.0.2)

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

channel "room:*", :"#{mod}.RoomChannel"
Expand Down Expand Up @@ -89,7 +89,7 @@
end
end

defmodule :"#{mod}.UserSocket" do

Check warning on line 92 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 Test1093.UserSocket)

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

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.13.4, 25.0.2)

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

Check warning on line 92 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 Test293.UserSocket)
use ChannelSpec.Socket

channel "room:*", :"#{mod}.RoomChannel"
Expand Down Expand Up @@ -122,6 +122,82 @@
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 Test5604.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 Test2885.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 All @@ -148,7 +224,7 @@
event "new_msg", :"#{mod}.RoomChannel.Handler", :msg
end

defmodule :"#{mod}.UserSocket" do

Check warning on line 227 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 Test2276.UserSocket)

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

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.13.4, 25.0.2)

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

Check warning on line 227 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 Test1156.UserSocket)
use ChannelSpec.Socket

channel "room:*", :"#{mod}.RoomChannel"
Expand Down Expand Up @@ -195,7 +271,7 @@
end
end

defmodule :"#{mod}.UserSocket" do

Check warning on line 274 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 Test962.UserSocket)

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

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.13.4, 25.0.2)

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

Check warning on line 274 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 Test3843.UserSocket)
use ChannelSpec.Socket

channel "room:*", :"#{mod}.RoomChannel"
Expand Down Expand Up @@ -240,7 +316,7 @@
end
end

defmodule :"#{mod}.UserSocket" do

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

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.13.4, 25.0.2)

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

channel "room:*", :"#{mod}.RoomChannel"
Expand Down
Loading