# Copyright 2019 Hubert Chathi # # 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 doctest Polyjuice.Client 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.send_message(client, "foo", "!bar") 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" => "foo", "format" => "org.matrix.custom.html", "body" => "foo" } }, {:ok, "$foo2"} } } do {:ok, event_id} = Polyjuice.Client.send_message(client, {"foo", "foo"}, "!bar") 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.send_message(client, %{"msgtype" => "m.notice", "body" => "foo"}, "!bar") assert event_id == "$foo3" # trying to send a non-msgdata should error assert_raise ArgumentError, fn -> Polyjuice.Client.send_message(client, 1, "!bar") end assert_raise ArgumentError, fn -> Polyjuice.Client.send_message(client, client, "!bar") end assert_raise FunctionClauseError, fn -> Polyjuice.Client.send_message(client, {"a"}, "!bar") 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.send_event( client, "m.room.message", %{"msgtype" => "m.text", "body" => "foo"}, "!bar" ) 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.update_read_receipt(client, "!room", "$event", "m.read") {:ok} = Polyjuice.Client.update_read_receipt(client, "!room", "$event") end end end