summaryrefslogtreecommitdiff
path: root/test/support/dummy_client.ex
blob: 4335282da68fcedb000e69cd75a48fdcba50f6bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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 DummyClient do
  require ExUnit.Assertions
  import ExUnit.Assertions
  defstruct [:response]

  defimpl Polyjuice.Client.API do
    def call(%{response: {request, result}}, endpoint) do
      if request != nil do
        assert endpoint == request
      end

      result
    end

    def room_queue(_client_api, _room_id, func) do
      # this client doesn't have a queue.  Just run the function.
      func.()
    end

    def transaction_id(_), do: "txn_id"

    def get_user_and_device(_) do
      {"@alice:example.org", "DEVICEID"}
    end

    def stop(_, _, _) do
      # don't need to do anything to stop it
      :ok
    end
  end

  defmodule MultiReq do
    require ExUnit.Assertions
    import ExUnit.Assertions
    defstruct [:pid]

    def create(responses) when is_list(responses) do
      {:ok, pid} = Agent.start(fn -> responses end)

      %MultiReq{
        pid: pid
      }
    end

    defimpl Polyjuice.Client.API do
      def call(%{pid: pid}, endpoint) do
        {request, result} =
          Agent.get_and_update(
            pid,
            fn
              [head | rest] -> {head, rest}
              [] -> {nil, nil}
            end
          )

        assert request != nil
        assert endpoint == request

        result
      end

      def room_queue(_client_api, _room_id, func) do
        # this client doesn't have a queue.  Just run the function.
        func.()
      end

      def transaction_id(_), do: "txn_id"

      def get_user_and_device(_) do
        {"@alice:example.org", "DEVICEID"}
      end

      def stop(%{pid: pid}, _, _) do
        # make sure we weren't expecting any more requests
        remaining = Agent.get(pid, & &1)
        assert remaining == []

        Process.exit(pid, :kill)
        :ok
      end
    end
  end
end