diff options
author | Hubert Chathi <hubert@uhoreg.ca> | 2020-12-26 18:03:58 -0500 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2020-12-26 18:03:58 -0500 |
commit | 2834044f508f3210c43cbc116a96f25d047e5069 (patch) | |
tree | 2acb5a0d7cec7b402c969d17efed10028cbb8953 /test/polyjuice | |
parent | Merge branch 'rooms_read_marker' into 'master' (diff) |
Add methods for creating rooms.
(Manual merge of https://gitlab.com/uhoreg/polyjuice_client/-/merge_requests/3)
Thanks to multi prise.
Diffstat (limited to 'test/polyjuice')
-rw-r--r-- | test/polyjuice/client/endpoint/post_create_room_test.exs | 122 | ||||
-rw-r--r-- | test/polyjuice/client/endpoint/put_room_alias.exs | 52 | ||||
-rw-r--r-- | test/polyjuice/client/room_test.exs | 41 |
3 files changed, 215 insertions, 0 deletions
diff --git a/test/polyjuice/client/endpoint/post_create_room_test.exs b/test/polyjuice/client/endpoint/post_create_room_test.exs new file mode 100644 index 0000000..c42ee25 --- /dev/null +++ b/test/polyjuice/client/endpoint/post_create_room_test.exs @@ -0,0 +1,122 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# 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.PostCreateRoomTest do + use ExUnit.Case + + test "POST /_matrix/client/r0/createRoom with empty variables" do + endpoint = %Polyjuice.Client.Endpoint.PostCreateRoom{} + + http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint) + + assert %{http_spec | body: nil} == %Polyjuice.Client.Endpoint.HttpSpec{ + auth_required: true, + body: nil, + headers: [ + {"Accept", "application/json"}, + {"Content-Type", "application/json"} + ], + method: :post, + path: "_matrix/client/r0/createRoom" + } + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 200, + [{"Content-Type", "application/json"}], + ~s({"room_id":"!room"}) + ) == {:ok, %{"room_id" => "!room"}} + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 500, + [], + "Aaah!" + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} + end + + test "POST /_matrix/client/r0/createRoom with all variables" do + endpoint = %Polyjuice.Client.Endpoint.PostCreateRoom{ + visibility: "public", + room_alias_name: "#foo", + name: "super fofo", + topic: "about foo", + invite: [], + invite_3pid: [], + room_version: "3", + creation_content: %{ + "m.federate": false + }, + initial_state: [], + preset: :private_chat, + is_direct: true, + power_level_content_override: %{ + ban: 50, + events: %{ + "m.room.name": 100, + "m.room.power_levels": 100 + } + } + } + + http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint) + + assert %{http_spec | body: nil} == %Polyjuice.Client.Endpoint.HttpSpec{ + auth_required: true, + body: nil, + headers: [ + {"Accept", "application/json"}, + {"Content-Type", "application/json"} + ], + method: :post, + path: "_matrix/client/r0/createRoom" + } + + assert Jason.decode!(http_spec.body) == %{ + "visibility" => "public", + "room_alias_name" => "#foo", + "name" => "super fofo", + "topic" => "about foo", + "room_version" => "3", + "creation_content" => %{ + "m.federate" => false + }, + "preset" => "private_chat", + "is_direct" => true, + "power_level_content_override" => %{ + "ban" => 50, + "events" => %{ + "m.room.name" => 100, + "m.room.power_levels" => 100 + } + } + } + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 200, + [{"Content-Type", "application/json"}], + ~s({"room_id":"!room"}) + ) == {:ok, %{"room_id" => "!room"}} + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 500, + [], + "Aaah!" + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} + end +end diff --git a/test/polyjuice/client/endpoint/put_room_alias.exs b/test/polyjuice/client/endpoint/put_room_alias.exs new file mode 100644 index 0000000..203a7ab --- /dev/null +++ b/test/polyjuice/client/endpoint/put_room_alias.exs @@ -0,0 +1,52 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# 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.PutRoomAliasTest do + use ExUnit.Case + + test "PUT room/{room_alias}" do + endpoint = %Polyjuice.Client.Endpoint.PutRoomsSend{ + room: "!room_id", + room_alias: "!room_alias" + } + + http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint) + + assert TestUtil.http_spec_body_to_binary(http_spec) == %Polyjuice.Client.Endpoint.HttpSpec{ + auth_required: true, + body: ~s({"body":"Hello World!"}), + headers: [ + {"Accept", "application/json"}, + {"Content-Type", "application/json"} + ], + method: :put, + url: "https://example.com/_matrix/client/r0/room/%21room_alias" + } + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 200, + [{"Content-Type", "application/json"}], + "{\"room_id\": \"$foo1\", \"room_alias\": \"$bar\"}" + ) == {:ok, "$bar:exampke.com"} + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 500, + [], + "Aaah!" + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} + end +end diff --git a/test/polyjuice/client/room_test.exs b/test/polyjuice/client/room_test.exs index 349ac54..115ab30 100644 --- a/test/polyjuice/client/room_test.exs +++ b/test/polyjuice/client/room_test.exs @@ -378,4 +378,45 @@ defmodule Polyjuice.Client.RoomTest do Polyjuice.Client.API.stop(client) end end + + test "create_room" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PostCreateRoom{}, + {:ok, "!room"} + } + } do + {:ok, "!room"} = Polyjuice.Client.Room.create_room(client) + end + end + + test "create alias" do + room_id = "id_roomtest:homeserver" + room_alias = "new_room_name:homeserver" + + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PutRoomAlias{ + room_id: room_id, + room_alias: "#" <> room_alias + }, + {:ok, "#" <> room_alias} + } + } do + {:ok, "#" <> room_alias} = Polyjuice.Client.Room.create_alias(client, room_id, room_alias) + end + + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PutRoomAlias{ + room_id: room_id, + room_alias: "#" <> room_alias + }, + {:ok, "#" <> room_alias} + } + } do + room_alias = "#new_room_name:homeserver" + {:ok, room_alias} = Polyjuice.Client.Room.create_alias(client, room_id, room_alias) + end + end end |