mirror of
https://github.com/processone/ejabberd.git
synced 2024-12-20 17:27:00 +01:00
3dc55c6d47
- add API versionning - changed error handling, based on exception - commands moved/merged from mod_admin_p1 to mod_admin_extra - command bufixes - add some elixir unit test cases Squashed commit of the following: commit dd59855b3486f78a9349756e4f102e79b3accff8 Merge: 14e8ffc 506e08e Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Fri Oct 30 11:43:18 2015 +0100 Merge branch '3.2.x' into api commit 14e8ffce78cbea6c8605371d1fc50a0c1d1e012c Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Tue Oct 27 16:35:17 2015 +0100 Added OAuth tests to ejabberd_commands commit f81c550c14628edfe4861c228576cb767924366a Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Tue Oct 27 16:34:55 2015 +0100 Added some mod_http_api tests commit 6a64578d5b2ba532a2feb6503ed98561e56d5d53 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Mon Oct 26 15:29:36 2015 +0100 Fix get_last command test Previous version won't work with dst. commit 27e0cde9e9c1f001effe68f8424a365ad947c068 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Fri Oct 23 17:59:34 2015 +0200 Add tests on admin command policy commit 19dad8d54f54c9fabd454280483cccfb06c8e78a Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Fri Oct 23 16:49:36 2015 +0200 Added command related tests (http api & user policy) commit e0e596ab4a3f3a70aba5f374f028939ab794de33 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Fri Oct 23 16:49:16 2015 +0200 Fix command call. commit 128cd7d1ede3c47a34f8ec3a750c980ccad2c61d Merge: 60c4c4c 447313c Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Thu Oct 22 14:48:39 2015 +0200 Merge branch '3.2.x' into api commit 60c4c4c0751302524c14219c6bc8c56a6069a689 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Thu Oct 22 14:45:57 2015 +0200 Fix ejabberd_commands spec. commit 8e145c28c5da762c2b93ee32327eff1db94ebfed Merge: 397273a f13dc94 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Wed Oct 21 18:26:07 2015 +0200 Merge branch '3.2.x' into api commit 397273a23ed415feac87aed33da6452229793387 Merge: c30e89b f289e27 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Wed Oct 21 15:27:45 2015 +0200 Merge branch '3.2.x' into api commit c30e89bb8a0013bff37e61e4c6953350c9c1f313 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Wed Oct 21 12:47:02 2015 +0200 Merge mod_http_api commit 7b0db22b4acd48ff6fabce41c1b2525e6580a3c5 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Fri Oct 16 11:55:48 2015 +0200 Fix exunit tests to run with common_test suites commit d8b1a89800ac7379a57a7eb4a09c3c93c3e1e5eb Merge: 2879ae8 63455b3 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Thu Oct 15 11:39:45 2015 +0200 Merge branch '3.2.x' into api commit 2879ae87ff3eee369ef3d780136b96ecff5285d1 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Wed Oct 14 14:53:44 2015 +0200 Fix update_roster command. commit a1d453dd7a3afda9861a8d747494a45057ad574b Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Tue Oct 13 16:14:28 2015 +0200 API commands refactor Moving and/or merging commands from mod_admin_p1 to mod_admin_extra commit b709ed26b0fc0ca4f3bdd5a59fa58ec7e3db97fa Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Wed Oct 7 15:10:01 2015 +0200 Add tests on commands commit 6711687bee9c672cb3d5aed0744e13420ecf6dbd Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Tue Sep 29 15:58:16 2015 +0200 Add ejabberd_commands tests commit df8682f419cf3877e77e36a19bca0fc55dc991f8 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Mon Sep 28 14:54:39 2015 +0200 Added API versioning for ejabberdctl and rest commands commit cd017b0e3aac431bc3ee807ceb7f8641e1523ef5 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Fri Sep 18 11:21:45 2015 +0200 Better error handling of HTTP API commands. commit ca5cb6acd8e4643f9d6c484d2277b0d7e88471e5 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Tue Sep 15 15:03:05 2015 +0200 add commands to mod_admin_extra: - get_offline_count - get_presence - change_password commit 7f583fa099e30ac2b0915669fd8f102ac565b833 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Tue Sep 15 15:02:16 2015 +0200 Improve REST API error handling commit 14753b1c02cdce434a786b7f80f6c09f0d210075 Author: Jerome Sautret <jerome.sautret@process-one.net> Date: Mon Sep 14 10:51:17 2015 +0200 Change REST API return codes for integer type.
204 lines
7.0 KiB
Elixir
204 lines
7.0 KiB
Elixir
# ----------------------------------------------------------------------
|
|
#
|
|
# ejabberd, Copyright (C) 2002-2016 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.
|
|
#
|
|
# ----------------------------------------------------------------------
|
|
|
|
# Notes on the tests:
|
|
#
|
|
# This test suite will print out errors in logs for tests:
|
|
#
|
|
# test "Error in run_fold is ignored"
|
|
# test "Throw in run_fold is ignored"
|
|
# test "Exit in run_fold is ignored"
|
|
#
|
|
# Those tests are not failing and we can safely ignore those errors in
|
|
# log as we are exercising hook handler recovery from that situation.
|
|
|
|
defmodule EjabberdHooksTest do
|
|
use ExUnit.Case, async: false
|
|
|
|
@author "mremond@process-one.net"
|
|
@host <<"domain.net">>
|
|
@self __MODULE__
|
|
|
|
setup_all do
|
|
{:ok, _pid} = :ejabberd_hooks.start_link
|
|
:ok
|
|
end
|
|
|
|
setup do
|
|
:meck.unload
|
|
:true = :ejabberd_hooks.delete_all_hooks
|
|
:ok
|
|
end
|
|
|
|
test "An anonymous function can be added as a hook" do
|
|
hookname = :test_fun_hook
|
|
:ok = :ejabberd_hooks.add(hookname, @host, fn _ -> :ok end, 50)
|
|
[{50, :undefined, _}] = :ejabberd_hooks.get_handlers(hookname, @host)
|
|
end
|
|
|
|
test "A module function can be added as a hook" do
|
|
hookname = :test_mod_hook
|
|
callback = :hook_callback
|
|
:ok = :ejabberd_hooks.add(hookname, @host, @self, callback, 40)
|
|
[{40, @self, _callback}] = :ejabberd_hooks.get_handlers(hookname, @host)
|
|
end
|
|
|
|
test "An anonymous function can be removed from hook handlers" do
|
|
hookname = :test_fun_hook
|
|
anon_fun = fn _ -> :ok end
|
|
:ok = :ejabberd_hooks.add(hookname, @host, anon_fun, 50)
|
|
:ok = :ejabberd_hooks.delete(hookname, @host, anon_fun, 50)
|
|
[] = :ejabberd_hooks.get_handlers(hookname, @host)
|
|
end
|
|
|
|
test "An module function can be removed from hook handlers" do
|
|
hookname = :test_mod_hook
|
|
callback = :hook_callback
|
|
:ok = :ejabberd_hooks.add(hookname, @host, @self, callback, 40)
|
|
:ok = :ejabberd_hooks.delete(hookname, @host, @self, callback, 40)
|
|
[] = :ejabberd_hooks.get_handlers(hookname, @host)
|
|
# TODO: Check that removed function is not call anymore
|
|
end
|
|
|
|
test "'Run hook' call registered handler once" do
|
|
test_result = :hook_result
|
|
run_hook([], fn -> test_result end, test_result)
|
|
end
|
|
|
|
test "'Run hook' can call registered handler with parameters" do
|
|
test_result = :hook_result_with_params
|
|
run_hook([:hook_params], fn _ -> test_result end, test_result)
|
|
end
|
|
|
|
# TODO test "Several handlers are run in order by hook"
|
|
|
|
test "Hook run chain is stopped when handler return 'stop'" do
|
|
# setup test
|
|
hookname = :test_mod_hook
|
|
modulename = :hook_module
|
|
mock(modulename, :hook_callback1, fn _ -> :stop end)
|
|
mock(modulename, :hook_callback2, fn _ -> :end_result end)
|
|
|
|
:ok = :ejabberd_hooks.add(hookname, @host, modulename, :hook_callback1, 40)
|
|
:ok = :ejabberd_hooks.add(hookname, @host, modulename, :hook_callback1, 50)
|
|
|
|
:ok = :ejabberd_hooks.run(hookname, @host, [:hook_params])
|
|
# callback2 is never run:
|
|
[{_pid, {^modulename, _callback, [:hook_params]}, :stop}] = :meck.history(modulename)
|
|
end
|
|
|
|
test "Run fold hooks accumulate state in correct order through handlers" do
|
|
# setup test
|
|
hookname = :test_mod_hook
|
|
modulename = :hook_module
|
|
mock(modulename, :hook_callback1, fn(list, user) -> [user|list] end)
|
|
mock(modulename, :hook_callback2, fn(list, _user) -> ["jid2"|list] end)
|
|
|
|
:ok = :ejabberd_hooks.add(hookname, @host, modulename, :hook_callback1, 40)
|
|
:ok = :ejabberd_hooks.add(hookname, @host, modulename, :hook_callback2, 50)
|
|
|
|
["jid2", "jid1"] = :ejabberd_hooks.run_fold(hookname, @host, [], ["jid1"])
|
|
end
|
|
|
|
test "Hook run_fold are executed based on priority order, not registration order" do
|
|
# setup test
|
|
hookname = :test_mod_hook
|
|
modulename = :hook_module
|
|
mock(modulename, :hook_callback1, fn(_acc) -> :first end)
|
|
mock(modulename, :hook_callback2, fn(_acc) -> :second end)
|
|
|
|
:ok = :ejabberd_hooks.add(hookname, @host, modulename, :hook_callback2, 50)
|
|
:ok = :ejabberd_hooks.add(hookname, @host, modulename, :hook_callback1, 40)
|
|
|
|
:second = :ejabberd_hooks.run_fold(hookname, @host, :started, [])
|
|
# Both module have been called:
|
|
2 = length(:meck.history(modulename))
|
|
end
|
|
|
|
# TODO: Test with ability to stop and return a value
|
|
test "Hook run_fold chain is stopped when handler return 'stop'" do
|
|
# setup test
|
|
hookname = :test_mod_hook
|
|
modulename = :hook_module
|
|
mock(modulename, :hook_callback1, fn(_acc) -> :stop end)
|
|
mock(modulename, :hook_callback2, fn(_acc) -> :executed end)
|
|
|
|
:ok = :ejabberd_hooks.add(hookname, @host, modulename, :hook_callback1, 40)
|
|
:ok = :ejabberd_hooks.add(hookname, @host, modulename, :hook_callback2, 50)
|
|
|
|
:stopped = :ejabberd_hooks.run_fold(hookname, @host, :started, [])
|
|
# Only one module has been called
|
|
[{_pid, {^modulename, :hook_callback1, [:started]}, :stop}] = :meck.history(modulename)
|
|
end
|
|
|
|
test "Error in run_fold is ignored" do
|
|
run_fold_crash(fn(_acc) -> raise "crashed" end)
|
|
end
|
|
|
|
test "Throw in run_fold is ignored" do
|
|
run_fold_crash(fn(_acc) -> throw :crashed end)
|
|
end
|
|
|
|
test "Exit in run_fold is ignored" do
|
|
run_fold_crash(fn(_acc) -> exit :crashed end)
|
|
end
|
|
|
|
# test for run hook with various number of params
|
|
def run_hook(params, fun, result) do
|
|
# setup test
|
|
hookname = :test_mod_hook
|
|
modulename = :hook_module
|
|
callback = :hook_callback
|
|
mock(modulename, callback, fun)
|
|
|
|
# Then check
|
|
:ok = :ejabberd_hooks.add(hookname, @host, modulename, callback, 40)
|
|
:ok = :ejabberd_hooks.run(hookname, @host, params)
|
|
[{_pid, {^modulename, ^callback, ^params}, ^result}] = :meck.history(modulename)
|
|
end
|
|
|
|
def run_fold_crash(crash_fun) do
|
|
# setup test
|
|
hookname = :test_mod_hook
|
|
modulename = :hook_module
|
|
mock(modulename, :hook_callback1, crash_fun)
|
|
mock(modulename, :hook_callback2, fn(_acc) -> :final end)
|
|
|
|
:ok = :ejabberd_hooks.add(hookname, @host, modulename, :hook_callback1, 40)
|
|
:ok = :ejabberd_hooks.add(hookname, @host, modulename, :hook_callback2, 50)
|
|
|
|
:final = :ejabberd_hooks.run_fold(hookname, @host, :started, [])
|
|
# Both handlers were called
|
|
2 = length(:meck.history(modulename))
|
|
end
|
|
|
|
# TODO refactor: Move to ejabberd_test_mock
|
|
def mock(module, function, fun) do
|
|
try do
|
|
:meck.new(module, [:non_strict])
|
|
catch
|
|
:error, {:already_started, _pid} -> :ok
|
|
end
|
|
|
|
:meck.expect(module, function, fun)
|
|
end
|
|
|
|
end
|