Commit 4c0fb5e1 authored by Pierre de Lacroix's avatar Pierre de Lacroix
Browse files

Merge branch 'controller_tests' into 'master'

test: add tests for controllers (closes #4)

Closes #4

See merge request !8
parents a7093f02 11864905
Pipeline #204761305 passed with stages
in 5 minutes and 36 seconds
......@@ -8,3 +8,9 @@ config :matrix_app_service, MatrixAppServiceWeb.Endpoint,
# Print only warnings and errors during test
config :logger, level: :warn
config :matrix_app_service,
transaction_adapter: MatrixAppService.TestTransactionAdapter,
room_adapter: MatrixAppService.TestRoomAdapter,
user_adapter: MatrixAppService.TestUserAdapter,
homeserver_token: "homeserver token"
......@@ -21,6 +21,7 @@ defmodule MatrixAppService do
In your Phoenix Router:
```
require MatrixAppServiceWeb.Router
MatrixAppServiceWeb.Router.routes()
```
......@@ -33,20 +34,26 @@ defmodule MatrixAppService do
For instance:
```
@behaviour MatrixAppService.Adapter.Room
@impl MatrixAppService.Adapter.Room
def query_alias(room_alias) do
# Do something with the room alias
# If the room exists, return :ok
end
@behaviour MatrixAppService.Adapter.User
@impl MatrixAppService.Adapter.User
def query_user(user_id) do
# Do something with the user ID
# If the user exists, return :ok
end
@behaviour MatrixAppService.Adapter.Transaction
@impl MatrixAppService.Adapter.Transaction
def new_event(%MatrixAppService.Event{type: type, content: content})
def new_event(%MatrixAppService.Event{type: type, content: content}) do
# Do something with the event
end
```
......
......@@ -18,8 +18,11 @@ defmodule MatrixAppService.Application do
]
children =
if Application.get_env(:matrix_app_service, :standalone, false) do
[MatrixAppServiceWeb.Endpoint | children]
if Mix.env() == :test do
[
MatrixAppServiceWeb.TestEndpoint
| children
]
else
children
end
......
......@@ -11,10 +11,14 @@ defmodule MatrixAppServiceWeb.V1.RoomController do
adapter = Application.fetch_env!(:matrix_app_service, :room_adapter)
with :ok <- adapter.query_alias(room_alias) do
send_resp(conn, 200, "{}")
conn
|> put_status(200)
|> json("{}")
else
_ ->
send_resp(conn, 404, "")
conn
|> put_status(404)
|> json("")
end
end
end
......@@ -11,10 +11,14 @@ defmodule MatrixAppServiceWeb.V1.UserController do
adapter = Application.fetch_env!(:matrix_app_service, :user_adapter)
with :ok <- adapter.query_user(user_id) do
send_resp(conn, 200, "{}")
conn
|> put_status(200)
|> json("{}")
else
_ ->
send_resp(conn, 404, "")
conn
|> put_status(404)
|> json("")
end
end
end
......@@ -17,7 +17,7 @@ defmodule MatrixAppServiceWeb.Router do
path = Application.compile_env(:matrix_app_service, :path, "/")
scope path, MatrixAppServiceWeb.V1 do
scope path, MatrixAppServiceWeb.V1, as: :matrix do
pipe_through :matrix_api
put "/transactions/:txn_id", TransactionController, :push
......
defmodule MatrixAppServiceWeb.RoomControllerTest do
use MatrixAppServiceWeb.ConnCase
describe "query" do
@tag authenticated: true
test "querying an existing room returns 200", %{conn: conn} do
conn = get(conn, Routes.matrix_room_path(conn, :query, "#existing:homeserver"), conn.params)
assert json_response(conn, 200) == "{}"
end
@tag authenticated: true
test "querying an non-existing room returns 404", %{conn: conn} do
conn =
get(conn, Routes.matrix_room_path(conn, :query, "#non-existing:homeserver"), conn.params)
assert json_response(conn, 404) == ""
end
end
end
defmodule MatrixAppServiceWeb.TransactionControllerTest do
use MatrixAppServiceWeb.ConnCase
import ExUnit.CaptureLog
describe "push" do
@tag authenticated: true
test "transactions are pushed", %{conn: conn} do
event = %{
"age" => 42,
"content" => 42,
"event_id" => 42,
"origin_server_ts" => 42,
"room_id" => 42,
"sender" => 42,
"type" => 42,
"unsigned" => 42,
"user_id" => 42
}
assert capture_log(fn ->
put(
conn,
Routes.matrix_transaction_path(conn, :push, 42),
Map.put(conn.params, "events", [event])
)
end) =~
"got an event"
end
end
end
defmodule MatrixAppServiceWeb.UserControllerTest do
use MatrixAppServiceWeb.ConnCase
describe "query" do
@tag authenticated: true
test "querying an existing user returns 200", %{conn: conn} do
conn = get(conn, Routes.matrix_user_path(conn, :query, "@existing:homeserver"), conn.params)
assert json_response(conn, 200) == "{}"
end
@tag authenticated: true
test "querying an non-existing user returns 404", %{conn: conn} do
conn =
get(conn, Routes.matrix_user_path(conn, :query, "@non-existing:homeserver"), conn.params)
assert json_response(conn, 404) == ""
end
end
end
......@@ -24,14 +24,23 @@ defmodule MatrixAppServiceWeb.ConnCase do
import Phoenix.ConnTest
import MatrixAppServiceWeb.ConnCase
alias MatrixAppServiceWeb.Router.Helpers, as: Routes
alias MatrixAppServiceWeb.TestRouter.Helpers, as: Routes
# The default endpoint for testing
@endpoint MatrixAppServiceWeb.Endpoint
@endpoint MatrixAppServiceWeb.TestEndpoint
end
end
setup _tags do
{:ok, conn: Phoenix.ConnTest.build_conn()}
setup tags do
conn =
if tags[:authenticated] do
Phoenix.ConnTest.build_conn(:get, "/", %{
"access_token" => Application.fetch_env!(:matrix_app_service, :homeserver_token)
})
else
Phoenix.ConnTest.build_conn()
end
{:ok, conn: conn}
end
end
defmodule MatrixAppServiceWeb.TestEndpoint do
use Phoenix.Endpoint, otp_app: :matrix_app_service
# The session will be stored in the cookie and signed,
# this means its contents can be read but not tampered with.
# Set :encryption_salt if you would also like to encrypt it.
@session_options [
store: :cookie,
key: "_matrix_app_service_key",
signing_salt: "zE7AHynD"
]
plug Plug.RequestId
# plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Phoenix.json_library()
plug Plug.MethodOverride
plug Plug.Head
plug Plug.Session, @session_options
plug MatrixAppServiceWeb.TestRouter
end
defmodule MatrixAppService.TestRoomAdapter do
@behaviour MatrixAppService.Adapter.Room
@impl MatrixAppService.Adapter.Room
def query_alias("#existing:homeserver") do
:ok
end
def query_alias(_) do
nil
end
end
defmodule MatrixAppServiceWeb.TestRouter do
require MatrixAppServiceWeb.Router
use Phoenix.Router
import Plug.Conn
import Phoenix.Controller
MatrixAppServiceWeb.Router.routes()
end
defmodule MatrixAppService.TestTransactionAdapter do
require Logger
@behaviour MatrixAppService.Adapter.Transaction
@impl MatrixAppService.Adapter.Transaction
def new_event(%MatrixAppService.Event{}) do
Logger.error("got an event")
nil
end
end
defmodule MatrixAppService.TestUserAdapter do
@behaviour MatrixAppService.Adapter.User
@impl MatrixAppService.Adapter.User
def query_user("@existing:homeserver") do
:ok
end
def query_user(_) do
nil
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment