diff options
author | Hubert Chathi <hubert@uhoreg.ca> | 2020-08-16 11:50:29 -0400 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2020-08-16 11:50:29 -0400 |
commit | 81734318fd677fe7061183457c39e09b0763773c (patch) | |
tree | cc72fcbf5e33dda5e2b77fa8725299d7e9995e5e /test/polyjuice | |
parent | allow body to be iolist or a filename, and add media upload/download (diff) |
move some client state to an Agent, and make http_spec take a URI struct
Diffstat (limited to 'test/polyjuice')
-rw-r--r-- | test/polyjuice/client/media_test.exs | 15 | ||||
-rw-r--r-- | test/polyjuice/client/sync_test.exs | 19 | ||||
-rw-r--r-- | test/polyjuice/client/user_test.exs | 105 | ||||
-rw-r--r-- | test/polyjuice/client_test.exs | 189 |
4 files changed, 185 insertions, 143 deletions
diff --git a/test/polyjuice/client/media_test.exs b/test/polyjuice/client/media_test.exs index c9d8df9..b4899ca 100644 --- a/test/polyjuice/client/media_test.exs +++ b/test/polyjuice/client/media_test.exs @@ -86,12 +86,13 @@ defmodule Polyjuice.Client.MediaTest do port = :httpd.info(httpd_pid) |> Keyword.fetch!(:port) - client = %Polyjuice.Client{ - base_url: "http://127.0.0.1:#{port}/Elixir.Polyjuice.Client.MediaTest.Httpd/", - access_token: "an_access_token", - user_id: "@alice:example.org", - test: true - } + client = + Polyjuice.Client.start( + "http://127.0.0.1:#{port}/Elixir.Polyjuice.Client.MediaTest.Httpd", + access_token: "an_access_token", + user_id: "@alice:example.org", + test: true + ) {:ok, url} = Polyjuice.Client.Media.upload(client, {:file, "mix.exs"}) @@ -104,6 +105,8 @@ defmodule Polyjuice.Client.MediaTest do assert content_type == "text/plain" assert Enum.join(body) == "foo" + Polyjuice.Client.stop(client) + :inets.stop(:httpd, httpd_pid) after File.rm_rf(tmpdir) diff --git a/test/polyjuice/client/sync_test.exs b/test/polyjuice/client/sync_test.exs index b1f2752..c1b4f64 100644 --- a/test/polyjuice/client/sync_test.exs +++ b/test/polyjuice/client/sync_test.exs @@ -17,7 +17,7 @@ defmodule Polyjuice.Client.SyncTest do defmodule Httpd do def _matrix(session_id, env, input) do - # FIXME: check authorization header + assert Keyword.get(env, :http_authorization) == 'Bearer an_access_token' # FIXME: check method [path | _] = Keyword.get(env, :path_info) @@ -213,13 +213,14 @@ defmodule Polyjuice.Client.SyncTest do port = :httpd.info(httpd_pid) |> Keyword.fetch!(:port) - client = %Polyjuice.Client{ - base_url: "http://127.0.0.1:#{port}/Elixir.Polyjuice.Client.SyncTest.Httpd", - access_token: "an_access_token", - user_id: "@alice:example.org", - storage: storage, - test: true - } + client = + Polyjuice.Client.start( + "http://127.0.0.1:#{port}/Elixir.Polyjuice.Client.SyncTest.Httpd", + access_token: "an_access_token", + user_id: "@alice:example.org", + storage: storage, + test: true + ) {:ok, sync_pid} = Polyjuice.Client.API.sync_child_spec(client, self(), filter: %{}) @@ -307,6 +308,8 @@ defmodule Polyjuice.Client.SyncTest do Process.unlink(sync_pid) Process.exit(sync_pid, :kill) + Polyjuice.Client.stop(client) + :inets.stop(:httpd, httpd_pid) after Polyjuice.Client.Storage.close(storage) diff --git a/test/polyjuice/client/user_test.exs b/test/polyjuice/client/user_test.exs deleted file mode 100644 index 06b95bd..0000000 --- a/test/polyjuice/client/user_test.exs +++ /dev/null @@ -1,105 +0,0 @@ -# Copyright 2020 Hubert Chathi <hubert@uhoreg.ca> -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -defmodule Polyjuice.Client.UserTest do - use ExUnit.Case - - test "log in" do - with client = %DummyClient{ - response: { - %Polyjuice.Client.Endpoint.PostLogin{ - type: "m.login.password", - identifier: %{ - "type" => "m.id.user", - "user" => "user" - }, - password: "password" - }, - {:ok, %{}} - } - } do - {:ok, %{}} = Polyjuice.Client.User.log_in_with_password(client, "user", "password") - end - - with client = %DummyClient{ - response: { - %Polyjuice.Client.Endpoint.PostLogin{ - type: "m.login.password", - identifier: %{ - "type" => "m.id.thirdparty", - "medium" => "email", - "address" => "user@example.com" - }, - password: "password", - device_id: "device_id", - initial_device_display_name: "Device name" - }, - {:ok, %{}} - } - } do - {:ok, %{}} = - Polyjuice.Client.User.log_in_with_password( - client, - {:email, "user@example.com"}, - "password", - device_id: "device_id", - initial_device_display_name: "Device name" - ) - end - - with client = %DummyClient{ - response: { - %Polyjuice.Client.Endpoint.PostLogin{ - type: "m.login.password", - identifier: %{ - "type" => "m.id.phone", - "country" => "CA", - "phone" => "1234567890" - }, - password: "password" - }, - {:ok, %{}} - } - } do - {:ok, %{}} = - Polyjuice.Client.User.log_in_with_password( - client, - {:phone, "CA", "1234567890"}, - "password" - ) - - {:ok, %{}} = - Polyjuice.Client.User.log_in_with_password( - client, - %{ - "type" => "m.id.phone", - "country" => "CA", - "phone" => "1234567890" - }, - "password" - ) - end - end - - test "log out" do - with client = %DummyClient{ - response: { - %Polyjuice.Client.Endpoint.PostLogout{}, - {:ok} - } - } do - {:ok} = Polyjuice.Client.User.log_out(client) - end - end -end diff --git a/test/polyjuice/client_test.exs b/test/polyjuice/client_test.exs index 8b5b415..0c880c1 100644 --- a/test/polyjuice/client_test.exs +++ b/test/polyjuice/client_test.exs @@ -15,9 +15,67 @@ defmodule Polyjuice.ClientTest do use ExUnit.Case + defmodule Httpd do + # for testing basic calls + def foo(session_id, _env, {_, input}) do + if input == 'foobar' do + :mod_esi.deliver( + session_id, + 'Content-Type: application/json\r\n\r\n{"foo":"bar"}' + ) + else + :mod_esi.deliver( + session_id, + 'Status: 400 Bad Request\r\nContent-Type: application/json\r\n\r\n{"errcode":"M_UNKNOWN","error":"Wrong contents"}' + ) + end + end + + def _matrix(session_id, env, input) do + [path | _] = + Keyword.get(env, :path_info) + |> to_string() + |> String.split("?", parts: 2) + + case path do + "client/r0/login" -> + handle_login(session_id, env, input) + + "client/r0/logout" -> + handle_logout(session_id, env, input) + + _ -> + :mod_esi.deliver( + session_id, + 'Status: 404 Not Found\r\nContent-Type: application/json\r\n\r\n{"errcode":"M_NOT_FOUND","error":"Not found"}' + ) + end + end + + defp handle_login(session_id, _env, {_, input}) do + identifier_type = + to_string(input) |> Jason.decode!() |> Map.get("identifier") |> Map.get("type") + + :mod_esi.deliver( + session_id, + 'Content-Type: application/json\r\n\r\n{"user_id":"@alice:example.org","access_token":"#{ + identifier_type + }_login","device_id":"foo"}' + ) + end + + defp handle_logout(session_id, _env, _input) do + :mod_esi.deliver( + session_id, + 'Content-Type: application/json\r\n\r\n{}' + ) + end + end + test "transaction_id is unique" do client = %Polyjuice.Client{ - base_url: "http://localhost:8008" + base_url: "http://localhost:8008", + pid: nil } # the best that we can do is test that two calls to transaction_id return @@ -28,7 +86,8 @@ defmodule Polyjuice.ClientTest do test "sync child spec" do client = %Polyjuice.Client{ - base_url: "http://localhost:8008" + base_url: "http://localhost:8008", + pid: nil } %{id: Polyjuice.Client.Sync, restart: :permanent, start: start} = @@ -71,22 +130,6 @@ defmodule Polyjuice.ClientTest do end end - defmodule Httpd do - def foo(session_id, _env, {_, input}) do - if input == 'foobar' do - :mod_esi.deliver( - session_id, - 'Content-Type: application/json\r\n\r\n{"foo":"bar"}' - ) - else - :mod_esi.deliver( - session_id, - 'Status: 400 Bad Request\r\nContent-Type: application/json\r\n\r\n{"errcode":"M_UNKNOWN","error":"Wrong contents"}' - ) - end - end - end - test "call" do {:ok, tmpdir} = TestUtil.mktmpdir("sync-") @@ -109,12 +152,13 @@ defmodule Polyjuice.ClientTest do port = :httpd.info(httpd_pid) |> Keyword.fetch!(:port) - client = %Polyjuice.Client{ - base_url: "http://127.0.0.1:#{port}/Elixir.Polyjuice.ClientTest.Httpd/", - access_token: "an_access_token", - user_id: "@alice:example.org", - test: true - } + client = + Polyjuice.Client.start( + "http://127.0.0.1:#{port}/Elixir.Polyjuice.ClientTest.Httpd/", + access_token: "an_access_token", + user_id: "@alice:example.org", + test: true + ) # binary body assert Polyjuice.Client.API.call( @@ -144,6 +188,103 @@ defmodule Polyjuice.ClientTest do } ) == {:ok, %{"foo" => "bar"}} + Polyjuice.Client.stop(client) + + :inets.stop(:httpd, httpd_pid) + after + File.rm_rf(tmpdir) + end + end + + test "login" do + {:ok, tmpdir} = TestUtil.mktmpdir("sync-") + + try do + tmpdir_charlist = to_charlist(tmpdir) + + :inets.start() + + {:ok, httpd_pid} = + :inets.start( + :httpd, + port: 0, + server_name: 'sync.test', + server_root: tmpdir_charlist, + document_root: tmpdir_charlist, + bind_address: {127, 0, 0, 1}, + modules: [:mod_esi], + erl_script_alias: {'', [Polyjuice.ClientTest.Httpd]} + ) + + port = :httpd.info(httpd_pid) |> Keyword.fetch!(:port) + + client = + Polyjuice.Client.start( + "http://127.0.0.1:#{port}/Elixir.Polyjuice.ClientTest.Httpd/", + access_token: nil, + test: true + ) + + Polyjuice.Client.log_in_with_password(client, "@alice:example.org", "password") + + assert Agent.get(client.pid, fn %{ + access_token: access_token, + user_id: user_id, + device_id: device_id + } -> + {access_token, user_id, device_id} + end) == {"m.id.user_login", "@alice:example.org", "foo"} + + Polyjuice.Client.log_out(client) + + assert Agent.get(client.pid, fn %{access_token: access_token} -> access_token end) == nil + + Polyjuice.Client.log_in_with_password( + client, + {:email, "user@example.com"}, + "password" + ) + + assert Agent.get(client.pid, fn %{ + access_token: access_token, + user_id: user_id, + device_id: device_id + } -> + {access_token, user_id, device_id} + end) == {"m.id.thirdparty_login", "@alice:example.org", "foo"} + + Polyjuice.Client.log_in_with_password( + client, + {:phone, "CA", "1234567890"}, + "password" + ) + + assert Agent.get(client.pid, fn %{ + access_token: access_token, + user_id: user_id, + device_id: device_id + } -> + {access_token, user_id, device_id} + end) == {"m.id.phone_login", "@alice:example.org", "foo"} + + Polyjuice.Client.log_in_with_password( + client, + %{ + "type" => "ca.uhoreg.foo" + }, + "password" + ) + + assert Agent.get(client.pid, fn %{ + access_token: access_token, + user_id: user_id, + device_id: device_id + } -> + {access_token, user_id, device_id} + end) == {"ca.uhoreg.foo_login", "@alice:example.org", "foo"} + + Polyjuice.Client.stop(client) + :inets.stop(:httpd, httpd_pid) after File.rm_rf(tmpdir) |