aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMickaël Rémond <mickael.remond@process-one.net>2016-03-31 11:37:14 +0200
committerMickaël Rémond <mickael.remond@process-one.net>2016-03-31 11:37:14 +0200
commit7c2998a55d78d1345b706192c55a9d03c649c38a (patch)
treeb092154b65f17eedc5a90e2e7576ca1d44c09cca /test
parentmix version updated for 16.03 release (diff)
parentFix Dialyzer inconsistency (diff)
Merge pull request #1044 from processone/http-api
Add ability to call open ejabberd commands through ReST API
Diffstat (limited to 'test')
-rw-r--r--test/ejabberd_commands_test.exs4
-rw-r--r--test/elixir_SUITE.erl4
-rw-r--r--test/mod_http_api_test.exs88
3 files changed, 95 insertions, 1 deletions
diff --git a/test/ejabberd_commands_test.exs b/test/ejabberd_commands_test.exs
index 0c06fc2ca..db5b82cfc 100644
--- a/test/ejabberd_commands_test.exs
+++ b/test/ejabberd_commands_test.exs
@@ -36,6 +36,10 @@ defmodule EjabberdCommandsTest do
assert Enum.member?(commands, {:test_user, [], "Test user"})
end
+ # TODO Test that we can add command to list of expose commands
+ # This can be done with:
+ # ejabberd_config:add_local_option(commands, [[{add_commands, [open_cmd]}]]).
+
# test "Check that a user can use a user command" do
# [Command] = ets:lookup(ejabberd_commands, test_user),
# AccessCommands = ejabberd_commands:get_access_commands(undefined),
diff --git a/test/elixir_SUITE.erl b/test/elixir_SUITE.erl
index b9a0b1a23..041d0603e 100644
--- a/test/elixir_SUITE.erl
+++ b/test/elixir_SUITE.erl
@@ -65,7 +65,9 @@ undefined_function(Module, Func, Args) ->
error_handler:undefined_function(Module, Func,Args).
run_elixir_test(Func) ->
- 'Elixir.ExUnit':start([]),
+ %% Elixir tests can be tagged as follow to be ignored (place before test start)
+ %% @tag pending: true
+ 'Elixir.ExUnit':start([{exclude, [{pending, true}]}]),
'Elixir.Code':load_file(list_to_binary(filename:join(test_dir(), atom_to_list(Func)))),
%% I did not use map syntax, so that this file can still be build under R16
ResultMap = 'Elixir.ExUnit':run(),
diff --git a/test/mod_http_api_test.exs b/test/mod_http_api_test.exs
new file mode 100644
index 000000000..cc5aed5a8
--- /dev/null
+++ b/test/mod_http_api_test.exs
@@ -0,0 +1,88 @@
+# ----------------------------------------------------------------------
+#
+# 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.
+#
+# ----------------------------------------------------------------------
+
+defmodule ModHttpApiTest do
+ @author "mremond@process-one.net"
+
+ use ExUnit.Case, async: true
+
+ require Record
+ Record.defrecord :request, Record.extract(:request, from_lib: "ejabberd/include/ejabberd_http.hrl")
+ Record.defrecord :ejabberd_commands, Record.extract(:ejabberd_commands, from_lib: "ejabberd/include/ejabberd_commands.hrl")
+
+ setup_all do
+ :ok = :mnesia.start
+ :ok = :ejabberd_config.start(["localhost"], [])
+
+ :ok = :ejabberd_commands.init
+
+ :ok = :ejabberd_commands.register_commands(cmds)
+ on_exit fn -> unregister_commands(cmds) end
+ end
+
+ test "We can call open commands without authentication" do
+ :ejabberd_config.add_local_option(:commands, [[{:add_commands, [:open_cmd]}]])
+ request = request(method: :POST, data: "[]")
+ {200, _, _} = :mod_http_api.process(["open_cmd"], request)
+ end
+
+ # This related to the commands config file option
+ test "Attempting to access a command that is not exposed as HTTP API returns 401" do
+ :ejabberd_config.add_local_option(:commands, [])
+ request = request(method: :POST, data: "[]")
+ {401, _, _} = :mod_http_api.process(["open_cmd"], request)
+ end
+
+ test "Call to user commands without authentication are rejected" do
+ :ejabberd_config.add_local_option(:commands, [[{:add_commands, [:user_cmd]}]])
+ request = request(method: :POST, data: "[]")
+ {401, _, _} = :mod_http_api.process(["user_cmd"], request)
+ end
+
+ # Define a set of test commands that we expose through API
+ defp cmds do
+ # TODO Refactor
+ [ejabberd_commands(name: :open_cmd, tags: [:test],
+ policy: :open,
+ module: __MODULE__,
+ function: :open_cmd_fun,
+ args: [],
+ result: {:res, :rescode}),
+ ejabberd_commands(name: :user_cmd, tags: [:test],
+ policy: :user,
+ module: __MODULE__,
+ function: :user_cmd_fun,
+ args: [],
+ result: {:res, :rescode})
+ ]
+ end
+
+ def open_cmd_fun, do: :ok
+ def user_cmd_fun, do: :ok
+
+ defp unregister_commands(commands) do
+ try do
+ :ejabberd_commands.unregister_commands(commands)
+ catch
+ _,_ -> :ok
+ end
+ end
+
+end