summaryrefslogtreecommitdiff
path: root/test/ejabberd_sm_mock.exs
blob: 9ac739ba54d76f0df6db30ee08ef753962c692e6 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# ----------------------------------------------------------------------
#
# ejabberd, Copyright (C) 2002-2017   ProcessOne
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# ----------------------------------------------------------------------

defmodule EjabberdSmMock do
  @author "jsautret@process-one.net"

	require Record
	Record.defrecord :session, Record.extract(:session, from_lib: "ejabberd/include/ejabberd_sm.hrl")
	Record.defrecord :jid, Record.extract(:jid, from_lib: "xmpp/include/jid.hrl")

	@agent __MODULE__

	def init do
		ModLastMock.init

		try do
			Agent.stop(@agent)
    catch
      :exit, _e -> :ok
    end

		{:ok, _pid} = Agent.start_link(fn -> [] end, name: @agent)

    mock(:ejabberd_sm, :get_user_resources,
			fn (user, domain)  -> for s <- get_sessions(user, domain), do: s.resource end)

    mock(:ejabberd_sm, :route,
      fn (to, {:exit, _reason})  ->
        user = jid(to, :user)
        domain = jid(to, :server)
        resource = jid(to, :resource)
        disconnect_resource(user, domain, resource)
        :ok
        (_, _) -> :ok
      end)

	end

	def connect_resource(user, domain, resource,
											 opts \\ [priority: 1, conn: :c2s]) do
		Agent.update(@agent, fn sessions ->
			session = %{user: user, domain: domain, resource: resource,
									timestamp: :os.timestamp, pid: self, node: node,
									auth_module: :ejabberd_auth, ip: :undefined,
									priority: opts[:priority], conn: opts[:conn]}
			[session | sessions]
		end)
	end

	def disconnect_resource(user, domain, resource) do
		disconnect_resource(user, domain, resource, ModLastMock.now)
	end

	def disconnect_resource(user, domain, resource, timestamp) do
		Agent.update(@agent, fn sessions ->
		for s <- sessions,
			s.user != user or s.domain != domain or s.resource != resource, do: s
		end)
		ModLastMock.set_last user, domain, "", timestamp
	end

	def get_sessions() do
		Agent.get(@agent, fn sessions -> sessions end)
	end

	def get_sessions(user, domain) do
		Agent.get(@agent, fn sessions ->
		for s <- sessions, s.user == user, s.domain == domain, do: s
		end)
	end

	def get_session(user, domain, resource) do
		Agent.get(@agent, fn sessions ->
		for s <- sessions,
			s.user == user, s.domain == domain,	s.resource == resource, do: s
		end)
	end

	def to_record(s) do
		session(usr: {s.user, s.domain, s.ressource},
						us: {s.user, s.domain},
						sid: {s.timestamp, s.pid},
						priority: s.priority,
						info: [conn: s.conn, ip: s.ip, node: s.node,
									 oor: false, auth_module: s.auth_module])
	end

	####################################################################
	#     Helpers
	####################################################################


  # TODO refactor: Move to ejabberd_test_mock
  def mock(module, function, fun) do
    try do
      :meck.new(module)
    catch
      :error, {:already_started, _pid} -> :ok
    end

    :meck.expect(module, function, fun)
  end

end