aboutsummaryrefslogtreecommitdiff
path: root/test/mod_http_api_test.exs
diff options
context:
space:
mode:
authorPaweł Chmielowski <pchmielowski@process-one.net>2016-05-30 23:04:29 +0200
committerPaweł Chmielowski <pchmielowski@process-one.net>2016-05-30 23:06:29 +0200
commitbe0dd51e51ef821f2879be80b7e694e2c749134e (patch)
tree82aeb4585290972d83457fbee2dd244787d317b4 /test/mod_http_api_test.exs
parentMore strict check for commands with policy user (diff)
Fix mod_http_api_test.exs
Diffstat (limited to 'test/mod_http_api_test.exs')
-rw-r--r--test/mod_http_api_test.exs30
1 files changed, 26 insertions, 4 deletions
diff --git a/test/mod_http_api_test.exs b/test/mod_http_api_test.exs
index 9432ae471..99b8d9b28 100644
--- a/test/mod_http_api_test.exs
+++ b/test/mod_http_api_test.exs
@@ -39,6 +39,7 @@ defmodule ModHttpApiTest do
end
test "We can expose several commands to API at a time" do
+ setup_mocks()
:ejabberd_config.add_local_option(:commands, [[{:add_commands, [:open_cmd, :user_cmd]}]])
commands = :ejabberd_commands.get_commands()
assert Enum.member?(commands, :open_cmd)
@@ -46,21 +47,24 @@ defmodule ModHttpApiTest do
end
test "We can call open commands without authentication" do
+ setup_mocks()
:ejabberd_config.add_local_option(:commands, [[{:add_commands, [:open_cmd]}]])
- request = request(method: :POST, data: "[]")
+ request = request(method: :POST, ip: {{127,0,0,1},50000}, 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
+ setup_mocks()
:ejabberd_config.add_local_option(:commands, [])
- request = request(method: :POST, data: "[]")
+ request = request(method: :POST, ip: {{127,0,0,1},50000}, data: "[]")
{401, _, _} = :mod_http_api.process(["open_cmd"], request)
end
test "Call to user, admin or restricted commands without authentication are rejected" do
+ setup_mocks()
:ejabberd_config.add_local_option(:commands, [[{:add_commands, [:user_cmd, :admin_cmd, :restricted]}]])
- request = request(method: :POST, data: "[]")
+ request = request(method: :POST, ip: {{127,0,0,1},50000}, data: "[]")
{401, _, _} = :mod_http_api.process(["user_cmd"], request)
{401, _, _} = :mod_http_api.process(["admin_cmd"], request)
{401, _, _} = :mod_http_api.process(["restricted_cmd"], request)
@@ -68,6 +72,7 @@ defmodule ModHttpApiTest do
@tag pending: true
test "If admin_ip_access is enabled, we can call restricted API without authentication from that IP" do
+ setup_mocks()
end
# Define a set of test commands that we expose through API
@@ -86,10 +91,27 @@ defmodule ModHttpApiTest do
end
def open_cmd, do: :ok
- def user_cmd, do: :ok
+ def user_cmd(_, _), do: :ok
def admin_cmd, do: :ok
def restricted_cmd, do: :ok
+ defp setup_mocks() do
+ :meck.unload
+ mock(:gen_mod, :get_module_opt,
+ fn (_server, :mod_http_api, admin_ip_access, _, _) ->
+ [{:allow, [{:ip, {{127,0,0,2}, 32}}]}]
+ end)
+ end
+
+ defp mock(module, function, fun) do
+ try do
+ :meck.new(module)
+ catch
+ :error, {:already_started, _pid} -> :ok
+ end
+ :meck.expect(module, function, fun)
+ end
+
defp unregister_commands(commands) do
try do
:ejabberd_commands.unregister_commands(commands)