summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHubert Chathi <hubert@uhoreg.ca>2020-04-21 19:22:56 -0400
committerHubert Chathi <hubert@uhoreg.ca>2020-04-21 19:24:43 -0400
commite54a74a76eed8de5a052da72bb21f6fd13c5cf4b (patch)
tree53aca70302bfbfe3275a29798e48fe314f5febdd
parentfix login endpoint add logout endpoint, and add functions for logging in/out (diff)
add some tests and some cleanups
-rw-r--r--lib/polyjuice/client.ex1
-rw-r--r--lib/polyjuice/client/endpoint.ex2
-rw-r--r--lib/polyjuice/client/endpoint/post_join.ex2
-rw-r--r--lib/polyjuice/client/endpoint/post_logout.ex5
-rw-r--r--lib/polyjuice/client/room.ex2
-rw-r--r--test/polyjuice/client/endpoint/post_logout_test.exs43
-rw-r--r--test/polyjuice/client/room_test.exs38
-rw-r--r--test/polyjuice/client_test.exs105
8 files changed, 192 insertions, 6 deletions
diff --git a/lib/polyjuice/client.ex b/lib/polyjuice/client.ex
index 7de93b1..7433b19 100644
--- a/lib/polyjuice/client.ex
+++ b/lib/polyjuice/client.ex
@@ -128,6 +128,7 @@ defmodule Polyjuice.Client do
if auth_required and access_token == nil do
{:error, :auth_required}
else
+ # FIXME: handle errors
{:ok, status_code, resp_headers, body} =
:hackney.request(
method,
diff --git a/lib/polyjuice/client/endpoint.ex b/lib/polyjuice/client/endpoint.ex
index 82f9e11..97eb277 100644
--- a/lib/polyjuice/client/endpoint.ex
+++ b/lib/polyjuice/client/endpoint.ex
@@ -39,8 +39,8 @@ defmodule Polyjuice.Client.Endpoint do
:method,
:headers,
:url,
- :body,
:transform,
+ body: "",
auth_required: true
]
end
diff --git a/lib/polyjuice/client/endpoint/post_join.ex b/lib/polyjuice/client/endpoint/post_join.ex
index 66007cf..4e967e8 100644
--- a/lib/polyjuice/client/endpoint/post_join.ex
+++ b/lib/polyjuice/client/endpoint/post_join.ex
@@ -61,7 +61,7 @@ defmodule Polyjuice.Client.Endpoint.PostJoin do
if servers == [] do
nil
else
- Enum.join(Enum.map(servers, &"server_name=#{e.(&1)}"), "&")
+ servers |> Enum.map(&{"server_name", &1}) |> URI.encode_query()
end
}
diff --git a/lib/polyjuice/client/endpoint/post_logout.ex b/lib/polyjuice/client/endpoint/post_logout.ex
index d23f725..7523797 100644
--- a/lib/polyjuice/client/endpoint/post_logout.ex
+++ b/lib/polyjuice/client/endpoint/post_logout.ex
@@ -32,7 +32,8 @@ defmodule Polyjuice.Client.Endpoint.PostLogout do
%Polyjuice.Client.Endpoint.HttpSpec{
method: :post,
headers: [
- {"Accept", "application/json"}
+ {"Accept", "application/json"},
+ {"Content-Type", "application/json"}
],
url:
URI.merge(
@@ -40,7 +41,7 @@ defmodule Polyjuice.Client.Endpoint.PostLogout do
"#{Polyjuice.Client.prefix_r0()}/logout"
)
|> to_string(),
- body: "",
+ body: "{}",
transform: &Polyjuice.Client.Endpoint.PostLogout.transform/3
}
end
diff --git a/lib/polyjuice/client/room.ex b/lib/polyjuice/client/room.ex
index 0196571..6d84b13 100644
--- a/lib/polyjuice/client/room.ex
+++ b/lib/polyjuice/client/room.ex
@@ -157,7 +157,7 @@ defmodule Polyjuice.Client.Room do
client_api :: Polyjuice.Client.API.t(),
room :: String.t(),
servers :: list(String.t()),
- third_party_join :: map | nil
+ third_party_signed :: map | nil
) :: Any
def join(client_api, room, servers \\ [], third_party_signed \\ nil)
when is_binary(room) and is_list(servers) and
diff --git a/test/polyjuice/client/endpoint/post_logout_test.exs b/test/polyjuice/client/endpoint/post_logout_test.exs
new file mode 100644
index 0000000..a7a10f5
--- /dev/null
+++ b/test/polyjuice/client/endpoint/post_logout_test.exs
@@ -0,0 +1,43 @@
+# 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.Endpoint.PostLogoutTest do
+ use ExUnit.Case
+
+ test "POST logout" do
+ endpoint = %Polyjuice.Client.Endpoint.PostLogout{}
+
+ http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint, "https://example.com")
+
+ assert %{http_spec | transform: nil} == %Polyjuice.Client.Endpoint.HttpSpec{
+ auth_required: true,
+ body: "{}",
+ headers: [
+ {"Accept", "application/json"},
+ {"Content-Type", "application/json"}
+ ],
+ method: :post,
+ transform: nil,
+ url: "https://example.com/_matrix/client/r0/logout"
+ }
+
+ assert http_spec.transform.(
+ 200,
+ [],
+ "{}"
+ ) == :ok
+
+ assert http_spec.transform.(500, [], "Aaah!") == {:error, 500, "Aaah!"}
+ end
+end
diff --git a/test/polyjuice/client/room_test.exs b/test/polyjuice/client/room_test.exs
index 83885cb..bb86be1 100644
--- a/test/polyjuice/client/room_test.exs
+++ b/test/polyjuice/client/room_test.exs
@@ -1,4 +1,4 @@
-# Copyright 2019 Hubert Chathi <hubert@uhoreg.ca>
+# 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.
@@ -143,6 +143,14 @@ defmodule Polyjuice.Client.RoomTest do
)
assert event_id == "$foo1"
+
+ {:ok, ^event_id} =
+ Polyjuice.Client.Room.send_state_event(
+ client,
+ "!bar",
+ "m.room.name",
+ %{"name" => "foo"}
+ )
end
end
@@ -161,4 +169,32 @@ defmodule Polyjuice.Client.RoomTest do
{:ok} = Polyjuice.Client.Room.update_read_receipt(client, "!room", "$event")
end
end
+
+ test "join room" do
+ with client = %DummyClient{
+ response: {
+ %Polyjuice.Client.Endpoint.PostJoin{
+ room: "!room",
+ servers: [],
+ third_party_signed: nil
+ },
+ {:ok, "!room"}
+ }
+ } do
+ {:ok, "!room"} = Polyjuice.Client.Room.join(client, "!room")
+ end
+
+ with client = %DummyClient{
+ response: {
+ %Polyjuice.Client.Endpoint.PostJoin{
+ room: "!room",
+ servers: ["example.org"],
+ third_party_signed: %{}
+ },
+ {:ok, "!room"}
+ }
+ } do
+ {:ok, "!room"} = Polyjuice.Client.Room.join(client, "!room", ["example.org"], %{})
+ end
+ end
end
diff --git a/test/polyjuice/client_test.exs b/test/polyjuice/client_test.exs
new file mode 100644
index 0000000..d8274af
--- /dev/null
+++ b/test/polyjuice/client_test.exs
@@ -0,0 +1,105 @@
+# 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.ClientTest 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.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.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.log_in_with_password(
+ client,
+ {:phone, "CA", "1234567890"},
+ "password"
+ )
+
+ {:ok, %{}} =
+ Polyjuice.Client.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.log_out(client)
+ end
+ end
+end