summaryrefslogtreecommitdiff
path: root/test/polyjuice
diff options
context:
space:
mode:
authorHubert Chathi <hubert@uhoreg.ca>2020-08-15 04:19:50 -0400
committerHubert Chathi <hubert@uhoreg.ca>2020-08-15 04:28:16 -0400
commit988bf8af92c6f253ae729a044178a19c76648a71 (patch)
tree04df4a99c2a26a66fd7927a42a25163a6b685171 /test/polyjuice
parents/Any/any/ in typespecs (diff)
allow body to be iolist or a filename, and add media upload/download
Diffstat (limited to 'test/polyjuice')
-rw-r--r--test/polyjuice/client/endpoint/post_user_filter_test.exs2
-rw-r--r--test/polyjuice/client/endpoint/put_rooms_send_test.exs2
-rw-r--r--test/polyjuice/client/endpoint/put_rooms_state_test.exs2
-rw-r--r--test/polyjuice/client/media_test.exs112
-rw-r--r--test/polyjuice/client/sync_test.exs2
-rw-r--r--test/polyjuice/client_test.exs79
6 files changed, 196 insertions, 3 deletions
diff --git a/test/polyjuice/client/endpoint/post_user_filter_test.exs b/test/polyjuice/client/endpoint/post_user_filter_test.exs
index 605834c..947474c 100644
--- a/test/polyjuice/client/endpoint/post_user_filter_test.exs
+++ b/test/polyjuice/client/endpoint/post_user_filter_test.exs
@@ -27,7 +27,7 @@ defmodule Polyjuice.Client.Endpoint.PostUserfilterTest do
http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint, "https://example.com")
- assert http_spec == %Polyjuice.Client.Endpoint.HttpSpec{
+ assert TestUtil.http_spec_body_to_binary(http_spec) == %Polyjuice.Client.Endpoint.HttpSpec{
auth_required: true,
body: ~s({"presence":{"types":[]}}),
headers: [
diff --git a/test/polyjuice/client/endpoint/put_rooms_send_test.exs b/test/polyjuice/client/endpoint/put_rooms_send_test.exs
index 5c3f2ef..eeee870 100644
--- a/test/polyjuice/client/endpoint/put_rooms_send_test.exs
+++ b/test/polyjuice/client/endpoint/put_rooms_send_test.exs
@@ -27,7 +27,7 @@ defmodule Polyjuice.Client.Endpoint.PutRoomsSendTest do
http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint, "https://example.com")
- assert http_spec == %Polyjuice.Client.Endpoint.HttpSpec{
+ assert TestUtil.http_spec_body_to_binary(http_spec) == %Polyjuice.Client.Endpoint.HttpSpec{
auth_required: true,
body: ~s({"body":"Hello World!"}),
headers: [
diff --git a/test/polyjuice/client/endpoint/put_rooms_state_test.exs b/test/polyjuice/client/endpoint/put_rooms_state_test.exs
index 04fa59e..690c885 100644
--- a/test/polyjuice/client/endpoint/put_rooms_state_test.exs
+++ b/test/polyjuice/client/endpoint/put_rooms_state_test.exs
@@ -27,7 +27,7 @@ defmodule Polyjuice.Client.Endpoint.PutRoomsStateTest do
http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint, "https://example.com")
- assert http_spec == %Polyjuice.Client.Endpoint.HttpSpec{
+ assert TestUtil.http_spec_body_to_binary(http_spec) == %Polyjuice.Client.Endpoint.HttpSpec{
auth_required: true,
body: ~s({"name":"foo"}),
headers: [
diff --git a/test/polyjuice/client/media_test.exs b/test/polyjuice/client/media_test.exs
new file mode 100644
index 0000000..c9d8df9
--- /dev/null
+++ b/test/polyjuice/client/media_test.exs
@@ -0,0 +1,112 @@
+# Copyright 2019-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.MediaTest do
+ use ExUnit.Case
+ doctest Polyjuice.Client.Room
+
+ defmodule Httpd do
+ def _matrix(session_id, env, input) do
+ # FIXME: check authorization header
+ # FIXME: check method
+ [path | _] =
+ Keyword.get(env, :path_info)
+ |> to_string()
+ |> String.split("?", parts: 2)
+
+ case path do
+ "media/r0/upload" ->
+ handle_upload(session_id, env, input)
+
+ "media/r0/download/example.org/foo" ->
+ handle_download(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_upload(session_id, _env, {_, input}) do
+ {:ok, file} = File.read("mix.exs")
+
+ if to_string(input) == file do
+ :mod_esi.deliver(
+ session_id,
+ 'Content-Type: application/json\r\n\r\n{"content_uri":"mxc://example.org/abcdefg"}'
+ )
+ 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
+
+ defp handle_download(session_id, _env, _input) do
+ :mod_esi.deliver(
+ session_id,
+ 'Content-Type: text/plain\r\nContent-Disposition: attachment; filename="foo.txt"\r\n\r\nfoo'
+ )
+ end
+ end
+
+ test "upload" 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.Client.MediaTest.Httpd]}
+ )
+
+ 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
+ }
+
+ {:ok, url} = Polyjuice.Client.Media.upload(client, {:file, "mix.exs"})
+
+ assert url == "mxc://example.org/abcdefg"
+
+ {:ok, filename, content_type, body} =
+ Polyjuice.Client.Media.download(client, "mxc://example.org/foo")
+
+ assert filename == "foo.txt"
+ assert content_type == "text/plain"
+ assert Enum.join(body) == "foo"
+
+ :inets.stop(:httpd, httpd_pid)
+ after
+ File.rm_rf(tmpdir)
+ end
+ end
+end
diff --git a/test/polyjuice/client/sync_test.exs b/test/polyjuice/client/sync_test.exs
index f42e135..b1f2752 100644
--- a/test/polyjuice/client/sync_test.exs
+++ b/test/polyjuice/client/sync_test.exs
@@ -306,6 +306,8 @@ defmodule Polyjuice.Client.SyncTest do
Process.unlink(sync_pid)
Process.exit(sync_pid, :kill)
+
+ :inets.stop(:httpd, httpd_pid)
after
Polyjuice.Client.Storage.close(storage)
File.rm_rf(tmpdir)
diff --git a/test/polyjuice/client_test.exs b/test/polyjuice/client_test.exs
index ecb539a..8b5b415 100644
--- a/test/polyjuice/client_test.exs
+++ b/test/polyjuice/client_test.exs
@@ -70,4 +70,83 @@ 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-")
+
+ 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{
+ base_url: "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(
+ client,
+ %DummyEndpoint{
+ http_spec: %Polyjuice.Client.Endpoint.HttpSpec{
+ method: :put,
+ url: "foo",
+ headers: [],
+ body: "foobar",
+ auth_required: false
+ }
+ }
+ ) == {:ok, %{"foo" => "bar"}}
+
+ # iolist body
+ assert Polyjuice.Client.API.call(
+ client,
+ %DummyEndpoint{
+ http_spec: %Polyjuice.Client.Endpoint.HttpSpec{
+ method: :put,
+ url: "foo",
+ headers: [],
+ body: [?f, ["oo"], ["b", [?a | "r"]]],
+ auth_required: false
+ }
+ }
+ ) == {:ok, %{"foo" => "bar"}}
+
+ :inets.stop(:httpd, httpd_pid)
+ after
+ File.rm_rf(tmpdir)
+ end
+ end
end