diff options
author | Pierre de Lacroix <pierre@pdelacroix.com> | 2020-10-19 18:04:56 +0000 |
---|---|---|
committer | Pierre de Lacroix <pierre@pdelacroix.com> | 2020-10-19 18:04:56 +0000 |
commit | 4c0fb5e1c89eb7d83eb46ce992bbb8b7b5b80130 (patch) | |
tree | 584a3f3100dc76bb9f7a58e4330d5a866c784d68 | |
parent | Merge branch 'test-ci' into 'master' (diff) | |
parent | test: add tests for controllers (diff) |
Merge branch 'controller_tests' into 'master'
test: add tests for controllers (closes #4)
Closes #4
See merge request kazarma/matrix_app_service.ex!8
-rw-r--r-- | config/test.exs | 6 | ||||
-rw-r--r-- | lib/matrix_app_service.ex | 9 | ||||
-rw-r--r-- | lib/matrix_app_service/application.ex | 7 | ||||
-rw-r--r-- | lib/matrix_app_service_web/controllers/v1/room_controller.ex | 8 | ||||
-rw-r--r-- | lib/matrix_app_service_web/controllers/v1/user_controller.ex | 8 | ||||
-rw-r--r-- | lib/matrix_app_service_web/router.ex | 2 | ||||
-rw-r--r-- | test/matrix_app_service_web/controllers/room_controller_test.exs | 16 | ||||
-rw-r--r-- | test/matrix_app_service_web/controllers/third_party_controller_test.exs (renamed from test/matrix_app_service_web/controllers/thir_party_controller_test.exs) | 0 | ||||
-rw-r--r-- | test/matrix_app_service_web/controllers/transaction_controller_test.exs | 28 | ||||
-rw-r--r-- | test/matrix_app_service_web/controllers/user_controller_test.exs | 16 | ||||
-rw-r--r-- | test/support/conn_case.ex | 17 | ||||
-rw-r--r-- | test/support/test_endpoint.ex | 25 | ||||
-rw-r--r-- | test/support/test_room_adapter.ex | 12 | ||||
-rw-r--r-- | test/support/test_router.ex | 10 | ||||
-rw-r--r-- | test/support/test_transaction_adapter.ex | 10 | ||||
-rw-r--r-- | test/support/test_user_adapter.ex | 12 |
16 files changed, 174 insertions, 12 deletions
diff --git a/config/test.exs b/config/test.exs index aa8991c..61436fd 100644 --- a/config/test.exs +++ b/config/test.exs @@ -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" diff --git a/lib/matrix_app_service.ex b/lib/matrix_app_service.ex index 49acd1b..289d276 100644 --- a/lib/matrix_app_service.ex +++ b/lib/matrix_app_service.ex @@ -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 ``` diff --git a/lib/matrix_app_service/application.ex b/lib/matrix_app_service/application.ex index 57ce521..38f65be 100644 --- a/lib/matrix_app_service/application.ex +++ b/lib/matrix_app_service/application.ex @@ -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 diff --git a/lib/matrix_app_service_web/controllers/v1/room_controller.ex b/lib/matrix_app_service_web/controllers/v1/room_controller.ex index 460bcbf..cc84c48 100644 --- a/lib/matrix_app_service_web/controllers/v1/room_controller.ex +++ b/lib/matrix_app_service_web/controllers/v1/room_controller.ex @@ -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 diff --git a/lib/matrix_app_service_web/controllers/v1/user_controller.ex b/lib/matrix_app_service_web/controllers/v1/user_controller.ex index 9d00f63..bf3aa1e 100644 --- a/lib/matrix_app_service_web/controllers/v1/user_controller.ex +++ b/lib/matrix_app_service_web/controllers/v1/user_controller.ex @@ -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 diff --git a/lib/matrix_app_service_web/router.ex b/lib/matrix_app_service_web/router.ex index ca37fe0..2c2f0ca 100644 --- a/lib/matrix_app_service_web/router.ex +++ b/lib/matrix_app_service_web/router.ex @@ -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 diff --git a/test/matrix_app_service_web/controllers/room_controller_test.exs b/test/matrix_app_service_web/controllers/room_controller_test.exs index a8cfabc..27eb48a 100644 --- a/test/matrix_app_service_web/controllers/room_controller_test.exs +++ b/test/matrix_app_service_web/controllers/room_controller_test.exs @@ -1,3 +1,19 @@ 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 diff --git a/test/matrix_app_service_web/controllers/thir_party_controller_test.exs b/test/matrix_app_service_web/controllers/third_party_controller_test.exs index 961e751..961e751 100644 --- a/test/matrix_app_service_web/controllers/thir_party_controller_test.exs +++ b/test/matrix_app_service_web/controllers/third_party_controller_test.exs diff --git a/test/matrix_app_service_web/controllers/transaction_controller_test.exs b/test/matrix_app_service_web/controllers/transaction_controller_test.exs index 639ca62..435a4bc 100644 --- a/test/matrix_app_service_web/controllers/transaction_controller_test.exs +++ b/test/matrix_app_service_web/controllers/transaction_controller_test.exs @@ -1,3 +1,31 @@ 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 diff --git a/test/matrix_app_service_web/controllers/user_controller_test.exs b/test/matrix_app_service_web/controllers/user_controller_test.exs index 6b061c1..cbbd015 100644 --- a/test/matrix_app_service_web/controllers/user_controller_test.exs +++ b/test/matrix_app_service_web/controllers/user_controller_test.exs @@ -1,3 +1,19 @@ 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 diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 478e03f..bc28d10 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -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 diff --git a/test/support/test_endpoint.ex b/test/support/test_endpoint.ex new file mode 100644 index 0000000..5112df8 --- /dev/null +++ b/test/support/test_endpoint.ex @@ -0,0 +1,25 @@ +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 diff --git a/test/support/test_room_adapter.ex b/test/support/test_room_adapter.ex new file mode 100644 index 0000000..d57d420 --- /dev/null +++ b/test/support/test_room_adapter.ex @@ -0,0 +1,12 @@ +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 diff --git a/test/support/test_router.ex b/test/support/test_router.ex new file mode 100644 index 0000000..3bbad03 --- /dev/null +++ b/test/support/test_router.ex @@ -0,0 +1,10 @@ +defmodule MatrixAppServiceWeb.TestRouter do + require MatrixAppServiceWeb.Router + + use Phoenix.Router + + import Plug.Conn + import Phoenix.Controller + + MatrixAppServiceWeb.Router.routes() +end diff --git a/test/support/test_transaction_adapter.ex b/test/support/test_transaction_adapter.ex new file mode 100644 index 0000000..c39371b --- /dev/null +++ b/test/support/test_transaction_adapter.ex @@ -0,0 +1,10 @@ +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 diff --git a/test/support/test_user_adapter.ex b/test/support/test_user_adapter.ex new file mode 100644 index 0000000..c402e0b --- /dev/null +++ b/test/support/test_user_adapter.ex @@ -0,0 +1,12 @@ +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 |