24
1
mirror of https://github.com/processone/ejabberd.git synced 2024-06-02 21:17:12 +02:00

Check that various type of commands are properly rejected without auth

This commit is contained in:
Mickael Remond 2016-03-31 12:38:53 +02:00
parent 7c2998a55d
commit 3cfcdbb245

View File

@ -37,6 +37,13 @@ defmodule ModHttpApiTest do
on_exit fn -> unregister_commands(cmds) end on_exit fn -> unregister_commands(cmds) end
end end
test "We can expose several commands to API at a time" do
:ejabberd_config.add_local_option(:commands, [[{:add_commands, [:open_cmd, :user_cmd]}]])
commands = :ejabberd_commands.get_commands()
assert Enum.member?(commands, :open_cmd)
assert Enum.member?(commands, :user_cmd)
end
test "We can call open commands without authentication" do test "We can call open commands without authentication" do
:ejabberd_config.add_local_option(:commands, [[{:add_commands, [:open_cmd]}]]) :ejabberd_config.add_local_option(:commands, [[{:add_commands, [:open_cmd]}]])
request = request(method: :POST, data: "[]") request = request(method: :POST, data: "[]")
@ -50,32 +57,33 @@ defmodule ModHttpApiTest do
{401, _, _} = :mod_http_api.process(["open_cmd"], request) {401, _, _} = :mod_http_api.process(["open_cmd"], request)
end end
test "Call to user commands without authentication are rejected" do test "Call to user, admin or restricted commands without authentication are rejected" do
:ejabberd_config.add_local_option(:commands, [[{:add_commands, [:user_cmd]}]]) :ejabberd_config.add_local_option(:commands, [[{:add_commands, [:user_cmd, :admin_cmd, :restricted]}]])
request = request(method: :POST, data: "[]") request = request(method: :POST, data: "[]")
{401, _, _} = :mod_http_api.process(["user_cmd"], request) {401, _, _} = :mod_http_api.process(["user_cmd"], request)
{401, _, _} = :mod_http_api.process(["admin_cmd"], request)
{401, _, _} = :mod_http_api.process(["restricted_cmd"], request)
end end
# Define a set of test commands that we expose through API # Define a set of test commands that we expose through API
# We define one for each policy type
defp cmds do defp cmds do
# TODO Refactor [:open, :user, :admin, :restricted]
[ejabberd_commands(name: :open_cmd, tags: [:test], |> Enum.map(&({&1, String.to_atom(to_string(&1) <> "_cmd")}))
policy: :open, |> Enum.map(fn({cmd_type, cmd}) ->
ejabberd_commands(name: cmd, tags: [:test],
policy: cmd_type,
module: __MODULE__, module: __MODULE__,
function: :open_cmd_fun, function: cmd,
args: [],
result: {:res, :rescode}),
ejabberd_commands(name: :user_cmd, tags: [:test],
policy: :user,
module: __MODULE__,
function: :user_cmd_fun,
args: [], args: [],
result: {:res, :rescode}) result: {:res, :rescode})
] end)
end end
def open_cmd_fun, do: :ok def open_cmd, do: :ok
def user_cmd_fun, do: :ok def user_cmd, do: :ok
def admin_cmd, do: :ok
def restricted_cmd, do: :ok
defp unregister_commands(commands) do defp unregister_commands(commands) do
try do try do