Docs and cleanup
This commit is contained in:
parent
f0cf03141b
commit
22a7fd9c6d
13 changed files with 159 additions and 74 deletions
|
|
@ -4,14 +4,17 @@ defmodule BirdyChatWeb.Api.MessagesTest do
|
|||
setup %{conn: conn} do
|
||||
url = ~p"/api/messages"
|
||||
|
||||
path = Application.app_dir(:birdy_chat, ["priv", "messages", "test1-user.txt"])
|
||||
unique_user_id = System.unique_integer([:positive])
|
||||
username = "test1-user#{unique_user_id}"
|
||||
|
||||
path = Application.app_dir(:birdy_chat, ["priv", "messages", "#{username}.txt"])
|
||||
on_exit(fn -> File.rm(path) end)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|
||||
%{conn: conn, url: url}
|
||||
%{conn: conn, url: url, username: username, path: path}
|
||||
end
|
||||
|
||||
describe "POST /api/messages to other server" do
|
||||
|
|
@ -92,16 +95,30 @@ defmodule BirdyChatWeb.Api.MessagesTest do
|
|||
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"}
|
||||
test "returns error when you post a message to other server", %{conn: conn, url: url} do
|
||||
message = %{"from" => "nothomeserver-user", "to" => "test1-user", "message" => "123"}
|
||||
|
||||
payload = Jason.encode!(message)
|
||||
conn = post(conn, url, payload)
|
||||
assert result = json_response(conn, :unprocessable_entity)
|
||||
|
||||
expected_result = %{
|
||||
"errors" => %{"from" => ["you can only communicate with your home server"]}
|
||||
}
|
||||
|
||||
assert result == expected_result
|
||||
end
|
||||
|
||||
test "returns message and 201 when successful", %{conn: conn, url: url, username: username} do
|
||||
message = %{"from" => "test1-user", "to" => username, "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",
|
||||
"from" => "test1-user",
|
||||
"to" => username,
|
||||
"message" => "123",
|
||||
"routing" => "local",
|
||||
"server" => "test1"
|
||||
|
|
@ -110,15 +127,15 @@ defmodule BirdyChatWeb.Api.MessagesTest do
|
|||
assert result == expected_result
|
||||
end
|
||||
|
||||
test "writes message to file", %{conn: conn, url: url} do
|
||||
message = %{"from" => "test2-user", "to" => "test1-user", "message" => "123"}
|
||||
test "writes message to file", %{conn: conn, url: url, username: username, path: path} do
|
||||
message = %{"from" => "test1-user", "to" => username, "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",
|
||||
"from" => "test1-user",
|
||||
"to" => username,
|
||||
"message" => "123",
|
||||
"routing" => "local",
|
||||
"server" => "test1"
|
||||
|
|
@ -126,24 +143,22 @@ defmodule BirdyChatWeb.Api.MessagesTest do
|
|||
|
||||
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"
|
||||
assert contents == "test1-user: 123\n"
|
||||
end
|
||||
|
||||
test "appends message to file", %{conn: conn, url: url} do
|
||||
message = %{"from" => "test2-user", "to" => "test1-user", "message" => "123"}
|
||||
test "appends message to file", %{conn: conn, url: url, username: username, path: path} do
|
||||
message = %{"from" => "test1-user", "to" => username, "message" => "123"}
|
||||
payload = Jason.encode!(message)
|
||||
post(conn, url, payload)
|
||||
|
||||
message = %{"from" => "test2-user", "to" => "test1-user", "message" => "456"}
|
||||
message = %{"from" => "test1-user", "to" => username, "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"
|
||||
assert contents == "test1-user: 123\ntest1-user: 456\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,34 +0,0 @@
|
|||
defmodule BirdyChatWeb.ChannelCase do
|
||||
@moduledoc """
|
||||
This module defines the test case to be used by
|
||||
channel tests.
|
||||
|
||||
Such tests rely on `Phoenix.ChannelTest` and also
|
||||
import other functionality to make it easier
|
||||
to build common data structures and query the data layer.
|
||||
|
||||
Finally, if the test case interacts with the database,
|
||||
we enable the SQL sandbox, so changes done to the database
|
||||
are reverted at the end of every test. If you are using
|
||||
PostgreSQL, you can even run database tests asynchronously
|
||||
by setting `use BirdyChatWeb.ChannelCase, async: true`, although
|
||||
this option is not recommended for other databases.
|
||||
"""
|
||||
|
||||
use ExUnit.CaseTemplate
|
||||
|
||||
using do
|
||||
quote do
|
||||
# Import conveniences for testing with channels
|
||||
import Phoenix.ChannelTest
|
||||
import BirdyChatWeb.ChannelCase
|
||||
|
||||
# The default endpoint for testing
|
||||
@endpoint BirdyChatWeb.Endpoint
|
||||
end
|
||||
end
|
||||
|
||||
setup _tags do
|
||||
:ok
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue