Improve elixir tests
This commit is contained in:
parent
3b0eee785f
commit
eb9faffadd
|
@ -25,12 +25,15 @@ defmodule EjabberdAdminTest do
|
|||
|
||||
setup_all do
|
||||
:mnesia.start
|
||||
:ejabberd_mnesia.start
|
||||
# For some myterious reason, :ejabberd_commands.init mays
|
||||
# sometimes fails if module is not loaded before
|
||||
{:module, :ejabberd_commands} = Code.ensure_loaded(:ejabberd_commands)
|
||||
:ejabberd_hooks.start_link
|
||||
{:ok, _} = :acl.start_link
|
||||
{:ok, _} = :ejabberd_access_permissions.start_link()
|
||||
:ejabberd_commands.init
|
||||
:ejabberd_admin.start
|
||||
:ejabberd_commands.start_link
|
||||
:ejabberd_admin.start_link
|
||||
:ok
|
||||
end
|
||||
|
||||
|
@ -41,40 +44,44 @@ defmodule EjabberdAdminTest do
|
|||
test "Logvel can be set and retrieved" do
|
||||
:ejabberd_logger.start()
|
||||
|
||||
assert :lager == :ejabberd_commands.execute_command(:set_loglevel, [1])
|
||||
assert :lager == call_command(:set_loglevel, [1])
|
||||
assert {1, :critical, 'Critical'} ==
|
||||
:ejabberd_commands.execute_command(:get_loglevel, [])
|
||||
call_command(:get_loglevel, [])
|
||||
|
||||
assert :lager == :ejabberd_commands.execute_command(:set_loglevel, [2])
|
||||
assert :lager == call_command(:set_loglevel, [2])
|
||||
assert {2, :error, 'Error'} ==
|
||||
:ejabberd_commands.execute_command(:get_loglevel, [])
|
||||
call_command(:get_loglevel, [])
|
||||
|
||||
assert :lager == :ejabberd_commands.execute_command(:set_loglevel, [3])
|
||||
assert :lager == call_command(:set_loglevel, [3])
|
||||
assert {3, :warning, 'Warning'} ==
|
||||
:ejabberd_commands.execute_command(:get_loglevel, [])
|
||||
call_command(:get_loglevel, [])
|
||||
|
||||
assert {:wrong_loglevel, 6} ==
|
||||
catch_throw :ejabberd_commands.execute_command(:set_loglevel, [6])
|
||||
catch_throw call_command(:set_loglevel, [6])
|
||||
assert {3, :warning, 'Warning'} ==
|
||||
:ejabberd_commands.execute_command(:get_loglevel, [])
|
||||
call_command(:get_loglevel, [])
|
||||
|
||||
assert :lager == :ejabberd_commands.execute_command(:set_loglevel, [4])
|
||||
assert :lager == call_command(:set_loglevel, [4])
|
||||
assert {4, :info, 'Info'} ==
|
||||
:ejabberd_commands.execute_command(:get_loglevel, [])
|
||||
call_command(:get_loglevel, [])
|
||||
|
||||
assert :lager == :ejabberd_commands.execute_command(:set_loglevel, [5])
|
||||
assert :lager == call_command(:set_loglevel, [5])
|
||||
assert {5, :debug, 'Debug'} ==
|
||||
:ejabberd_commands.execute_command(:get_loglevel, [])
|
||||
call_command(:get_loglevel, [])
|
||||
|
||||
assert :lager == :ejabberd_commands.execute_command(:set_loglevel, [0])
|
||||
assert :lager == call_command(:set_loglevel, [0])
|
||||
assert {0, :no_log, 'No log'} ==
|
||||
:ejabberd_commands.execute_command(:get_loglevel, [])
|
||||
call_command(:get_loglevel, [])
|
||||
|
||||
end
|
||||
|
||||
defp call_command(name, args) do
|
||||
:ejabberd_commands.execute_command2(name, args, %{:caller_module => :ejabberd_ctl})
|
||||
end
|
||||
|
||||
test "command status works with ejabberd stopped" do
|
||||
assert :ejabberd_not_running ==
|
||||
elem(:ejabberd_commands.execute_command(:status, []), 0)
|
||||
elem(call_command(:status, []), 0)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,108 +0,0 @@
|
|||
# ----------------------------------------------------------------------
|
||||
#
|
||||
# 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 EjabberdCommandsTest do
|
||||
@author "mremond@process-one.net"
|
||||
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
require Record
|
||||
Record.defrecord :ejabberd_commands, Record.extract(:ejabberd_commands, from_lib: "ejabberd/include/ejabberd_commands.hrl")
|
||||
|
||||
setup_all do
|
||||
:mnesia.start
|
||||
:ejabberd_mnesia.start
|
||||
:stringprep.start
|
||||
:ok = :ejabberd_config.start(["localhost"], [])
|
||||
{:ok, _} = :ejabberd_access_permissions.start_link()
|
||||
|
||||
:ejabberd_commands.start_link
|
||||
:ok
|
||||
end
|
||||
|
||||
test "Check that we can register a command" do
|
||||
:ok = :ejabberd_commands.register_commands([user_test_command])
|
||||
commands = :ejabberd_commands.list_commands
|
||||
assert Enum.member?(commands, {:test_user, [], "Test user"})
|
||||
end
|
||||
|
||||
test "get_exposed_commands/0 returns registered commands" do
|
||||
commands = [open_test_command]
|
||||
:ok = :ejabberd_commands.register_commands(commands)
|
||||
:ok = :ejabberd_commands.expose_commands(commands)
|
||||
exposed_commands = :ejabberd_commands.get_exposed_commands
|
||||
assert Enum.member?(exposed_commands, :test_open)
|
||||
end
|
||||
|
||||
test "Check that admin commands are rejected with noauth credentials" do
|
||||
:ok = :ejabberd_commands.register_commands([admin_test_command])
|
||||
|
||||
assert catch_throw(:ejabberd_commands.execute_command(:undefined, :noauth, :test_admin, [])) == {:error, :account_unprivileged}
|
||||
|
||||
# Command executed from ejabberdctl passes anyway with access commands trick
|
||||
# TODO: We should refactor to have explicit call when bypassing auth check for command-line
|
||||
:ok = :ejabberd_commands.execute_command([], :noauth, :test_admin, [])
|
||||
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),
|
||||
# ejabberd_commands:check_access_commands(AccessCommands, {<<"test">>,<<"localhost">>, {oauth,<<"MyToken">>}, false}, test_user, Command, []).
|
||||
# end
|
||||
|
||||
defp user_test_command do
|
||||
ejabberd_commands(name: :test_user, tags: [:roster],
|
||||
desc: "Test user",
|
||||
policy: :user,
|
||||
module: __MODULE__,
|
||||
function: :test_user,
|
||||
args: [],
|
||||
result: {:contacts, {:list, {:contact, {:tuple, [
|
||||
{:jid, :string},
|
||||
{:nick, :string}
|
||||
]}}}})
|
||||
end
|
||||
|
||||
defp open_test_command do
|
||||
ejabberd_commands(name: :test_open, tags: [:test],
|
||||
desc: "Test open",
|
||||
policy: :open,
|
||||
module: __MODULE__,
|
||||
function: :test_open,
|
||||
args: [],
|
||||
result: {:res, :rescode})
|
||||
end
|
||||
|
||||
defp admin_test_command do
|
||||
ejabberd_commands(name: :test_admin, tags: [:roster],
|
||||
desc: "Test admin",
|
||||
policy: :restricted,
|
||||
module: __MODULE__,
|
||||
function: :test_admin,
|
||||
args: [],
|
||||
result: {:res, :rescode})
|
||||
end
|
||||
|
||||
def test_admin, do: :ok
|
||||
end
|
|
@ -30,6 +30,7 @@ defmodule EjabberdCyrsaslTest do
|
|||
:ejabberd_mnesia.start
|
||||
:ok = start_module(:stringprep)
|
||||
start_module(:jid)
|
||||
:ejabberd_hooks.start_link
|
||||
:ok = :ejabberd_config.start(["domain1"], [])
|
||||
{:ok, _} = :cyrsasl.start_link
|
||||
cyrstate = :cyrsasl.server_new("domain1", "domain1", "domain1", :ok, &get_password/1,
|
||||
|
|
|
@ -41,16 +41,19 @@ defmodule EjabberdModAdminExtraTest do
|
|||
:jid.start
|
||||
:stringprep.start
|
||||
:mnesia.start
|
||||
:ejabberd_mnesia.start
|
||||
:p1_sha.load_nif
|
||||
:ejabberd_hooks.start_link
|
||||
rescue
|
||||
_ -> :ok
|
||||
end
|
||||
{:ok, _} = :ejabberd_access_permissions.start_link()
|
||||
:ejabberd_commands.init
|
||||
:acl.start_link
|
||||
:ejabberd_access_permissions.start_link()
|
||||
:ejabberd_commands.start_link
|
||||
:ok = :ejabberd_config.start([@domain], [])
|
||||
:mod_admin_extra.start(@domain, [])
|
||||
:sel_application.start_app(:moka)
|
||||
{:ok, _pid} = :ejabberd_hooks.start_link
|
||||
:ejabberd_hooks.start_link
|
||||
:ok
|
||||
end
|
||||
|
||||
|
@ -66,9 +69,9 @@ defmodule EjabberdModAdminExtraTest do
|
|||
test "check_account works" do
|
||||
EjabberdAuthMock.create_user @user, @domain, @password
|
||||
|
||||
assert :ejabberd_commands.execute_command(:check_account, [@user, @domain])
|
||||
refute :ejabberd_commands.execute_command(:check_account, [@user, "bad_domain"])
|
||||
refute :ejabberd_commands.execute_command(:check_account, ["bad_user", @domain])
|
||||
assert call_command(:check_account, [@user, @domain])
|
||||
refute call_command(:check_account, [@user, "bad_domain"])
|
||||
refute call_command(:check_account, ["bad_user", @domain])
|
||||
|
||||
assert :meck.validate :ejabberd_auth
|
||||
end
|
||||
|
@ -77,13 +80,13 @@ defmodule EjabberdModAdminExtraTest do
|
|||
|
||||
EjabberdAuthMock.create_user @user, @domain, @password
|
||||
|
||||
assert :ejabberd_commands.execute_command(:check_password,
|
||||
assert call_command(:check_password,
|
||||
[@user, @domain, @password])
|
||||
refute :ejabberd_commands.execute_command(:check_password,
|
||||
refute call_command(:check_password,
|
||||
[@user, @domain, "bad_password"])
|
||||
refute :ejabberd_commands.execute_command(:check_password,
|
||||
refute call_command(:check_password,
|
||||
[@user, "bad_domain", @password])
|
||||
refute :ejabberd_commands.execute_command(:check_password,
|
||||
refute call_command(:check_password,
|
||||
["bad_user", @domain, @password])
|
||||
|
||||
assert :meck.validate :ejabberd_auth
|
||||
|
@ -95,21 +98,21 @@ defmodule EjabberdModAdminExtraTest do
|
|||
EjabberdAuthMock.create_user @user, @domain, @password
|
||||
hash = "5F4DCC3B5AA765D61D8327DEB882CF99" # echo -n password|md5
|
||||
|
||||
assert :ejabberd_commands.execute_command(:check_password_hash,
|
||||
assert call_command(:check_password_hash,
|
||||
[@user, @domain, hash, "md5"])
|
||||
refute :ejabberd_commands.execute_command(:check_password_hash,
|
||||
refute call_command(:check_password_hash,
|
||||
[@user, @domain, "bad_hash", "md5"])
|
||||
refute :ejabberd_commands.execute_command(:check_password_hash,
|
||||
refute call_command(:check_password_hash,
|
||||
[@user, "bad_domain", hash, "md5"])
|
||||
refute :ejabberd_commands.execute_command(:check_password_hash,
|
||||
refute call_command(:check_password_hash,
|
||||
["bad_user", @domain, hash, "md5"])
|
||||
|
||||
hash = "5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8" # echo -n password|shasum
|
||||
assert :ejabberd_commands.execute_command(:check_password_hash,
|
||||
assert call_command(:check_password_hash,
|
||||
[@user, @domain, hash, "sha"])
|
||||
|
||||
assert :unkown_hash_method ==
|
||||
catch_throw :ejabberd_commands.execute_command(:check_password_hash,
|
||||
catch_throw call_command(:check_password_hash,
|
||||
[@user, @domain, hash, "bad_method"])
|
||||
|
||||
assert :meck.validate :ejabberd_auth
|
||||
|
@ -119,14 +122,14 @@ defmodule EjabberdModAdminExtraTest do
|
|||
test "set_password works" do
|
||||
EjabberdAuthMock.create_user @user, @domain, @password
|
||||
|
||||
assert :ejabberd_commands.execute_command(:change_password,
|
||||
assert call_command(:change_password,
|
||||
[@user, @domain, "new_password"])
|
||||
refute :ejabberd_commands.execute_command(:check_password,
|
||||
refute call_command(:check_password,
|
||||
[@user, @domain, @password])
|
||||
assert :ejabberd_commands.execute_command(:check_password,
|
||||
assert call_command(:check_password,
|
||||
[@user, @domain, "new_password"])
|
||||
assert {:not_found, 'unknown_user'} ==
|
||||
catch_throw :ejabberd_commands.execute_command(:change_password,
|
||||
catch_throw call_command(:change_password,
|
||||
["bad_user", @domain,
|
||||
@password])
|
||||
assert :meck.validate :ejabberd_auth
|
||||
|
@ -135,23 +138,23 @@ defmodule EjabberdModAdminExtraTest do
|
|||
###################### Sessions
|
||||
|
||||
test "num_resources works" do
|
||||
assert 0 == :ejabberd_commands.execute_command(:num_resources,
|
||||
assert 0 == call_command(:num_resources,
|
||||
[@user, @domain])
|
||||
|
||||
EjabberdSmMock.connect_resource @user, @domain, @resource
|
||||
assert 1 == :ejabberd_commands.execute_command(:num_resources,
|
||||
assert 1 == call_command(:num_resources,
|
||||
[@user, @domain])
|
||||
|
||||
EjabberdSmMock.connect_resource @user, @domain, @resource<>"2"
|
||||
assert 2 == :ejabberd_commands.execute_command(:num_resources,
|
||||
assert 2 == call_command(:num_resources,
|
||||
[@user, @domain])
|
||||
|
||||
EjabberdSmMock.connect_resource @user<>"1", @domain, @resource
|
||||
assert 2 == :ejabberd_commands.execute_command(:num_resources,
|
||||
assert 2 == call_command(:num_resources,
|
||||
[@user, @domain])
|
||||
|
||||
EjabberdSmMock.disconnect_resource @user, @domain, @resource
|
||||
assert 1 == :ejabberd_commands.execute_command(:num_resources,
|
||||
assert 1 == call_command(:num_resources,
|
||||
[@user, @domain])
|
||||
|
||||
assert :meck.validate :ejabberd_sm
|
||||
|
@ -163,14 +166,14 @@ defmodule EjabberdModAdminExtraTest do
|
|||
EjabberdSmMock.connect_resource @user, @domain, @resource<>"1"
|
||||
|
||||
assert :bad_argument ==
|
||||
elem(catch_throw(:ejabberd_commands.execute_command(:resource_num,
|
||||
elem(catch_throw(call_command(:resource_num,
|
||||
[@user, @domain, 0])), 0)
|
||||
assert @resource<>"1" ==
|
||||
:ejabberd_commands.execute_command(:resource_num, [@user, @domain, 1])
|
||||
call_command(:resource_num, [@user, @domain, 1])
|
||||
assert @resource<>"3" ==
|
||||
:ejabberd_commands.execute_command(:resource_num, [@user, @domain, 3])
|
||||
call_command(:resource_num, [@user, @domain, 3])
|
||||
assert :bad_argument ==
|
||||
elem(catch_throw(:ejabberd_commands.execute_command(:resource_num,
|
||||
elem(catch_throw(call_command(:resource_num,
|
||||
[@user, @domain, 4])), 0)
|
||||
assert :meck.validate :ejabberd_sm
|
||||
end
|
||||
|
@ -184,7 +187,7 @@ defmodule EjabberdModAdminExtraTest do
|
|||
assert 1 == length EjabberdSmMock.get_session @user, @domain, @resource<>"2"
|
||||
|
||||
assert :ok ==
|
||||
:ejabberd_commands.execute_command(:kick_session,
|
||||
call_command(:kick_session,
|
||||
[@user, @domain,
|
||||
@resource<>"2", "kick"])
|
||||
|
||||
|
@ -199,18 +202,18 @@ defmodule EjabberdModAdminExtraTest do
|
|||
test "get_last works" do
|
||||
|
||||
assert {_, 'NOT FOUND'} =
|
||||
:ejabberd_commands.execute_command(:get_last, [@user, @domain])
|
||||
call_command(:get_last, [@user, @domain])
|
||||
|
||||
EjabberdSmMock.connect_resource @user, @domain, @resource<>"1"
|
||||
EjabberdSmMock.connect_resource @user, @domain, @resource<>"2"
|
||||
|
||||
assert {_, 'ONLINE'} =
|
||||
:ejabberd_commands.execute_command(:get_last, [@user, @domain])
|
||||
call_command(:get_last, [@user, @domain])
|
||||
|
||||
EjabberdSmMock.disconnect_resource @user, @domain, @resource<>"1"
|
||||
|
||||
assert {_, 'ONLINE'} =
|
||||
:ejabberd_commands.execute_command(:get_last, [@user, @domain])
|
||||
call_command(:get_last, [@user, @domain])
|
||||
|
||||
now = {megasecs, secs, _microsecs} = :os.timestamp
|
||||
timestamp = megasecs * 1000000 + secs
|
||||
|
@ -221,7 +224,7 @@ defmodule EjabberdModAdminExtraTest do
|
|||
"~w-~.2.0w-~.2.0wT~.2.0w:~.2.0w:~.2.0wZ",
|
||||
[year, month, day, hour, minute, second]))
|
||||
assert {result, ""} ==
|
||||
:ejabberd_commands.execute_command(:get_last, [@user, @domain])
|
||||
call_command(:get_last, [@user, @domain])
|
||||
|
||||
assert :meck.validate :mod_last
|
||||
end
|
||||
|
@ -238,7 +241,7 @@ defmodule EjabberdModAdminExtraTest do
|
|||
assert [] == ModRosterMock.get_roster(@user, @domain)
|
||||
|
||||
assert :ok ==
|
||||
:ejabberd_commands.execute_command(:add_rosteritem, [@user, @domain,
|
||||
call_command(:add_rosteritem, [@user, @domain,
|
||||
@user<>"1", @domain,
|
||||
"nick1",
|
||||
"group1",
|
||||
|
@ -261,7 +264,7 @@ defmodule EjabberdModAdminExtraTest do
|
|||
{:item, {@user<>"1", @domain, ""}, :both}])
|
||||
|
||||
assert :ok ==
|
||||
:ejabberd_commands.execute_command(:add_rosteritem, [@user, @domain,
|
||||
call_command(:add_rosteritem, [@user, @domain,
|
||||
@user<>"2", @domain,
|
||||
"nick2",
|
||||
"group2",
|
||||
|
@ -278,7 +281,7 @@ defmodule EjabberdModAdminExtraTest do
|
|||
{:item, {@user<>"2", @domain, ""}, :both}])
|
||||
|
||||
|
||||
:ejabberd_commands.execute_command(:delete_rosteritem, [@user, @domain,
|
||||
call_command(:delete_rosteritem, [@user, @domain,
|
||||
@user<>"1", @domain])
|
||||
result = ModRosterMock.get_roster(@user, @domain)
|
||||
assert 1 == length result
|
||||
|
@ -296,7 +299,7 @@ defmodule EjabberdModAdminExtraTest do
|
|||
[jid,
|
||||
{:item, {@user<>"1", @domain, ""}, :none}])
|
||||
|
||||
:ejabberd_commands.execute_command(:delete_rosteritem, [@user, @domain,
|
||||
call_command(:delete_rosteritem, [@user, @domain,
|
||||
@user<>"2", @domain])
|
||||
|
||||
# Check that the item roster user2 was pushed with subscription
|
||||
|
@ -321,39 +324,47 @@ defmodule EjabberdModAdminExtraTest do
|
|||
|
||||
test "get_roster works" do
|
||||
assert [] == ModRosterMock.get_roster(@user, @domain)
|
||||
assert [] == :ejabberd_commands.execute_command(:get_roster, [@user, @domain],
|
||||
assert [] == call_command(:get_roster, [@user, @domain],
|
||||
:admin)
|
||||
|
||||
assert :ok ==
|
||||
:ejabberd_commands.execute_command(:add_rosteritem, [@user, @domain,
|
||||
call_command(:add_rosteritem, [@user, @domain,
|
||||
@user<>"1", @domain,
|
||||
"nick1",
|
||||
"group1",
|
||||
"both"])
|
||||
assert [{@user<>"1@"<>@domain, "", 'both', 'none', "group1"}] ==
|
||||
:ejabberd_commands.execute_command(:get_roster, [@user, @domain], :admin)
|
||||
call_command(:get_roster, [@user, @domain], :admin)
|
||||
assert :ok ==
|
||||
:ejabberd_commands.execute_command(:add_rosteritem, [@user, @domain,
|
||||
call_command(:add_rosteritem, [@user, @domain,
|
||||
@user<>"2", @domain,
|
||||
"nick2",
|
||||
"group2",
|
||||
"none"])
|
||||
result = :ejabberd_commands.execute_command(:get_roster, [@user, @domain], :admin)
|
||||
result = call_command(:get_roster, [@user, @domain], :admin)
|
||||
assert 2 == length result
|
||||
assert Enum.member?(result, {@user<>"1@"<>@domain, "", 'both', 'none', "group1"})
|
||||
assert Enum.member?(result, {@user<>"2@"<>@domain, "", 'none', 'none', "group2"})
|
||||
|
||||
end
|
||||
|
||||
defp call_command(name, args) do
|
||||
:ejabberd_commands.execute_command2(name, args, %{:caller_module => :ejabberd_ctl})
|
||||
end
|
||||
|
||||
defp call_command(name, args, mode) do
|
||||
call_command(name, args)
|
||||
end
|
||||
|
||||
# kick_user command is defined in ejabberd_sm, move to extra?
|
||||
# test "kick_user works" do
|
||||
# assert 0 == :ejabberd_commands.execute_command(:num_resources,
|
||||
# assert 0 == call_command(:num_resources,
|
||||
# [@user, @domain])
|
||||
# EjabberdSmMock.connect_resource(@user, @domain, @resource<>"1")
|
||||
# EjabberdSmMock.connect_resource(@user, @domain, @resource<>"2")
|
||||
# assert 2 ==
|
||||
# :ejabberd_commands.execute_command(:kick_user, [@user, @domain])
|
||||
# assert 0 == :ejabberd_commands.execute_command(:num_resources,
|
||||
# call_command(:kick_user, [@user, @domain])
|
||||
# assert 0 == call_command(:num_resources,
|
||||
# [@user, @domain])
|
||||
# assert :meck.validate :ejabberd_sm
|
||||
# end
|
||||
|
|
|
@ -46,9 +46,10 @@ defmodule ModHttpApiMockTest do
|
|||
:mnesia.start
|
||||
:ejabberd_mnesia.start
|
||||
:stringprep.start
|
||||
:ejabberd_hooks.start_link
|
||||
:ejabberd_config.start([@domain], [])
|
||||
{:ok, _} = :ejabberd_access_permissions.start_link()
|
||||
:ejabberd_commands.init
|
||||
:ejabberd_commands.start_link
|
||||
rescue
|
||||
_ -> :ok
|
||||
end
|
||||
|
@ -79,10 +80,6 @@ defmodule ModHttpApiMockTest do
|
|||
fn (@acommand, [], %{usr: {@user, @domain, _}}, @version) ->
|
||||
:ok
|
||||
end)
|
||||
:meck.expect(:ejabberd_commands, :execute_command,
|
||||
fn (:undefined, {@user, @domain, @userpass, false}, @acommand, [], @version, _) ->
|
||||
:ok
|
||||
end)
|
||||
|
||||
:ejabberd_config.add_local_option(:commands, [[{:add_commands, [@acommand]}]])
|
||||
|
||||
|
@ -138,11 +135,6 @@ defmodule ModHttpApiMockTest do
|
|||
(@acommand, [], %{usr: {@user, @domain, _}, oauth_scope: _}, @version) ->
|
||||
throw({:error, :access_rules_unauthorized})
|
||||
end)
|
||||
:meck.expect(:ejabberd_commands, :execute_command,
|
||||
fn (:undefined, {@user, @domain, {:oauth, _token}, false},
|
||||
@acommand, [], @version, _) ->
|
||||
:ok
|
||||
end)
|
||||
|
||||
|
||||
# Correct OAuth call using specific scope
|
||||
|
@ -229,11 +221,6 @@ defmodule ModHttpApiMockTest do
|
|||
end)
|
||||
:meck.expect(:ejabberd_commands, :get_exposed_commands,
|
||||
fn () -> [@acommand] end)
|
||||
:meck.expect(:ejabberd_commands, :execute_command,
|
||||
fn (:undefined, {@user, @domain, {:oauth, _token}, false},
|
||||
@acommand, [], @version, _) ->
|
||||
:ok
|
||||
end)
|
||||
|
||||
#Mock acl to allow oauth authorizations
|
||||
:meck.expect(:acl, :match_rule, fn(_Server, _Access, _Jid) -> :allow end)
|
||||
|
|
|
@ -31,9 +31,11 @@ defmodule ModHttpApiTest do
|
|||
:ok = :mnesia.start
|
||||
:ejabberd_mnesia.start
|
||||
:stringprep.start
|
||||
:ejabberd_hooks.start_link
|
||||
:ok = :ejabberd_config.start(["localhost"], [])
|
||||
:acl.start_link
|
||||
{:ok, _} = :ejabberd_access_permissions.start_link()
|
||||
:ok = :ejabberd_commands.init
|
||||
{:ok, _} = :ejabberd_commands.start_link
|
||||
:ok = :ejabberd_commands.register_commands(cmds)
|
||||
on_exit fn ->
|
||||
:meck.unload
|
||||
|
@ -42,7 +44,7 @@ defmodule ModHttpApiTest do
|
|||
|
||||
test "We can expose several commands to API at a time" do
|
||||
setup_mocks()
|
||||
:ejabberd_commands.expose_commands([:open_cmd, :user_cmd])
|
||||
assert :ok == :ejabberd_commands.expose_commands([:open_cmd, :user_cmd])
|
||||
commands = :ejabberd_commands.get_exposed_commands()
|
||||
assert Enum.member?(commands, :open_cmd)
|
||||
assert Enum.member?(commands, :user_cmd)
|
||||
|
@ -58,14 +60,14 @@ defmodule ModHttpApiTest do
|
|||
# This related to the commands config file option
|
||||
test "Attempting to access a command that is not exposed as HTTP API returns 403" do
|
||||
setup_mocks()
|
||||
:ejabberd_commands.expose_commands([])
|
||||
assert :ok == :ejabberd_commands.expose_commands([])
|
||||
request = request(method: :POST, ip: {{127,0,0,1},50000}, data: "[]")
|
||||
{403, _, _} = :mod_http_api.process(["open_cmd"], request)
|
||||
end
|
||||
|
||||
test "Call to user, admin or restricted commands without authentication are rejected" do
|
||||
setup_mocks()
|
||||
:ejabberd_commands.expose_commands([:user_cmd, :admin_cmd, :restricted])
|
||||
assert :ok == :ejabberd_commands.expose_commands([:user_cmd, :admin_cmd, :restricted])
|
||||
request = request(method: :POST, ip: {{127,0,0,1},50000}, data: "[]")
|
||||
{403, _, _} = :mod_http_api.process(["user_cmd"], request)
|
||||
{403, _, _} = :mod_http_api.process(["admin_cmd"], request)
|
||||
|
|
|
@ -23,6 +23,7 @@ defmodule ModRosterMock do
|
|||
|
||||
require Record
|
||||
Record.defrecord :roster, Record.extract(:roster, from_lib: "ejabberd/include/mod_roster.hrl")
|
||||
Record.defrecord :roster_version, Record.extract(:roster_version, from_lib: "ejabberd/include/mod_roster.hrl")
|
||||
|
||||
@agent __MODULE__
|
||||
|
||||
|
@ -37,6 +38,13 @@ defmodule ModRosterMock do
|
|||
|
||||
mock_with_moka module
|
||||
|
||||
:ejabberd_mnesia.create(:mod_roster_mnesia, :roster,
|
||||
[ram_copies: [node()],
|
||||
attributes: Keyword.keys(roster(roster())),
|
||||
index: [:us]])
|
||||
:ejabberd_mnesia.create(:mod_roster_mnesia, :roster_version,
|
||||
[ram_copies: [node()],
|
||||
attributes: Keyword.keys(roster_version(roster_version()))])
|
||||
#:mod_roster.stop(domain)
|
||||
:mod_roster.start(domain, [])
|
||||
end
|
||||
|
@ -92,6 +100,11 @@ defmodule ModRosterMock do
|
|||
:moka.load(roster_mock0)
|
||||
|
||||
roster_mock = :moka.start(:mod_roster_mnesia)
|
||||
:moka.replace(roster_mock, :init,
|
||||
fn (_host, _opts) ->
|
||||
:ok
|
||||
end)
|
||||
|
||||
:moka.replace(roster_mock, :gen_mod, :db_type,
|
||||
fn (_host, _opts) ->
|
||||
{:none}
|
||||
|
|
Loading…
Reference in New Issue