diff options
Diffstat (limited to 'test/polyjuice/client/room_test.exs')
-rw-r--r-- | test/polyjuice/client/room_test.exs | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/test/polyjuice/client/room_test.exs b/test/polyjuice/client/room_test.exs new file mode 100644 index 0000000..36d80cb --- /dev/null +++ b/test/polyjuice/client/room_test.exs @@ -0,0 +1,137 @@ +# Copyright 2019 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.RoomTest do + use ExUnit.Case + doctest Polyjuice.Client.Room + + test "send message" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PutRoomsSend{ + room: "!bar", + txn_id: "txn_id", + event_type: "m.room.message", + message: %{ + "msgtype" => "m.text", + "body" => "foo" + } + }, + {:ok, "$foo1"} + } + } do + {:ok, event_id} = Polyjuice.Client.Room.send_message(client, "!bar", "foo") + assert event_id == "$foo1" + end + + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PutRoomsSend{ + room: "!bar", + txn_id: "txn_id", + event_type: "m.room.message", + message: %{ + "msgtype" => "m.text", + "formatted_body" => "<i>foo</i>", + "format" => "org.matrix.custom.html", + "body" => "foo" + } + }, + {:ok, "$foo2"} + } + } do + {:ok, event_id} = Polyjuice.Client.Room.send_message(client, "!bar", {"foo", "<i>foo</i>"}) + assert event_id == "$foo2" + end + + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PutRoomsSend{ + room: "!bar", + txn_id: "txn_id", + event_type: "m.room.message", + message: %{ + "msgtype" => "m.notice", + "body" => "foo" + } + }, + {:ok, "$foo3"} + } + } do + {:ok, event_id} = + Polyjuice.Client.Room.send_message(client, "!bar", %{ + "msgtype" => "m.notice", + "body" => "foo" + }) + + assert event_id == "$foo3" + + # trying to send a non-msgdata should error + assert_raise ArgumentError, fn -> + Polyjuice.Client.Room.send_message(client, "!bar", 1) + end + + assert_raise ArgumentError, fn -> + Polyjuice.Client.Room.send_message(client, "!bar", client) + end + + assert_raise FunctionClauseError, fn -> + Polyjuice.Client.Room.send_message(client, "!bar", {"a"}) + end + end + end + + test "send event" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PutRoomsSend{ + room: "!bar", + txn_id: "txn_id", + event_type: "m.room.message", + message: %{ + "msgtype" => "m.text", + "body" => "foo" + } + }, + {:ok, "$foo1"} + } + } do + {:ok, event_id} = + Polyjuice.Client.Room.send_event( + client, + "!bar", + "m.room.message", + %{"msgtype" => "m.text", "body" => "foo"} + ) + + assert event_id == "$foo1" + end + end + + test "update read receipt" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PostRoomsReceipt{ + room: "!room", + event_id: "$event", + receipt_type: "m.read" + }, + {:ok} + } + } do + {:ok} = Polyjuice.Client.Room.update_read_receipt(client, "!room", "$event", "m.read") + {:ok} = Polyjuice.Client.Room.update_read_receipt(client, "!room", "$event") + end + end +end |