birdy_chat/test/birdy_chat_web/api/messages_test.exs
2026-03-01 11:35:32 +02:00

149 lines
4.7 KiB
Elixir

defmodule BirdyChatWeb.Api.MessagesTest do
use BirdyChatWeb.ConnCase, async: true
setup %{conn: conn} do
url = ~p"/api/messages"
path = Application.app_dir(:birdy_chat, ["priv", "messages", "test1-user.txt"])
on_exit(fn -> File.rm(path) end)
conn =
conn
|> put_req_header("content-type", "application/json")
%{conn: conn, url: url}
end
describe "POST /api/messages to other server" do
test "returns error when the peer is down", %{conn: conn, url: url} do
message = %{from: "test1-user", to: "test2-user", message: "123"}
Req.Test.expect(BirdyChat.Dispatcher, &Req.Test.transport_error(&1, :econnrefused))
payload = Jason.encode!(message)
conn = post(conn, url, payload)
assert result = json_response(conn, :unprocessable_entity)
assert result == %{"errors" => %{"general" => "peer test2 is unreachable"}}
end
test "returns error when a peer is unknown", %{conn: conn, url: url} do
message = %{from: "test1-user", to: "fake-user", message: "123"}
payload = Jason.encode!(message)
conn = post(conn, url, payload)
assert result = json_response(conn, :unprocessable_entity)
expected_result = %{"errors" => %{"server" => ["unknown 'to' server"]}}
assert result == expected_result
end
test "send message to the other server via HTTP", %{conn: conn, url: url} do
message = %{from: "test1-user", to: "test2-user", message: "123"}
string_message = %{
"from" => "test1-user",
"message" => "123",
"to" => "test2-user",
"routing" => "remote",
"server" => "test2"
}
Req.Test.expect(BirdyChat.Dispatcher, fn conn ->
expected_body_params = %{
"from" => "test1-user",
"message" => "123",
"routing" => "remote",
"server" => "test2",
"to" => "test2-user"
}
{"authorization", token} =
Enum.find(conn.req_headers, fn {key, _v} -> key == "authorization" end)
{:ok, "test1-user"} =
Phoenix.Token.verify(BirdyChatWeb.Endpoint, "serverAuth", token, max_age: 1200)
assert conn.body_params == expected_body_params
resp = Jason.encode!(expected_body_params)
Plug.Conn.send_resp(conn, :created, resp)
end)
payload = Jason.encode!(message)
conn = post(conn, url, payload)
assert result = json_response(conn, :created)
assert result == string_message
end
end
describe "POST /api/messages" do
test "returns errors for invalid message", %{conn: conn, url: url} do
payload = Jason.encode!(%{})
conn = post(conn, url, payload)
assert result = json_response(conn, :unprocessable_entity)
expected_result = %{
"errors" => %{
"from" => ["can't be blank"],
"message" => ["can't be blank"],
"to" => ["can't be blank"]
}
}
assert result == expected_result
end
test "returns message and 201 when successful", %{conn: conn, url: url} do
message = %{"from" => "test2-user", "to" => "test1-user", "message" => "123"}
payload = Jason.encode!(message)
conn = post(conn, url, payload)
assert result = json_response(conn, :created)
expected_result = %{
"from" => "test2-user",
"to" => "test1-user",
"message" => "123",
"routing" => "local",
"server" => "test1"
}
assert result == expected_result
end
test "writes message to file", %{conn: conn, url: url} do
message = %{"from" => "test2-user", "to" => "test1-user", "message" => "123"}
payload = Jason.encode!(message)
conn = post(conn, url, payload)
assert result = json_response(conn, :created)
expected_result = %{
"from" => "test2-user",
"to" => "test1-user",
"message" => "123",
"routing" => "local",
"server" => "test1"
}
assert result == expected_result
path = Application.app_dir(:birdy_chat, ["priv", "messages", "test1-user.txt"])
contents = File.read!(path)
assert contents == "test2-user: 123\n"
end
test "appends message to file", %{conn: conn, url: url} do
message = %{"from" => "test2-user", "to" => "test1-user", "message" => "123"}
payload = Jason.encode!(message)
post(conn, url, payload)
message = %{"from" => "test2-user", "to" => "test1-user", "message" => "456"}
payload = Jason.encode!(message)
conn = post(conn, url, payload)
assert json_response(conn, :created)
path = Application.app_dir(:birdy_chat, ["priv", "messages", "test1-user.txt"])
contents = File.read!(path)
assert contents == "test2-user: 123\ntest2-user: 456\n"
end
end
end