2016-03-31 15:06:41 +02:00
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# ejabberd, Copyright (C) 2002-2015 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.
|
|
|
|
#
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
2016-04-01 11:13:48 +02:00
|
|
|
defmodule ModHttpApiMockTest do
|
2016-03-31 15:06:41 +02:00
|
|
|
use ExUnit.Case, async: false
|
|
|
|
|
|
|
|
@author "jsautret@process-one.net"
|
|
|
|
|
|
|
|
# Admin user
|
|
|
|
@admin "admin"
|
|
|
|
@adminpass "adminpass"
|
|
|
|
# Non admin user
|
|
|
|
@user "user"
|
|
|
|
@userpass "userpass"
|
|
|
|
# XMPP domain
|
|
|
|
@domain "domain"
|
|
|
|
# mocked command
|
|
|
|
@command "command_test"
|
|
|
|
@acommand String.to_atom(@command)
|
|
|
|
# default API version
|
|
|
|
@version 0
|
2016-04-01 11:13:48 +02:00
|
|
|
|
2016-03-31 15:06:41 +02:00
|
|
|
require Record
|
2016-04-01 11:13:48 +02:00
|
|
|
Record.defrecord :request, Record.extract(:request, from_lib: "ejabberd/include/ejabberd_http.hrl")
|
2016-03-31 15:06:41 +02:00
|
|
|
|
|
|
|
setup_all do
|
|
|
|
try do
|
2016-04-01 11:13:48 +02:00
|
|
|
:jid.start
|
|
|
|
:mnesia.start
|
2016-03-31 15:06:41 +02:00
|
|
|
:stringprep.start
|
2016-04-01 11:13:48 +02:00
|
|
|
:ejabberd_config.start([@domain], [])
|
2016-10-05 13:54:29 +02:00
|
|
|
{:ok, _} = :ejabberd_access_permissions.start_link()
|
2016-04-01 11:13:48 +02:00
|
|
|
:ejabberd_commands.init
|
2016-03-31 15:06:41 +02:00
|
|
|
rescue
|
|
|
|
_ -> :ok
|
|
|
|
end
|
|
|
|
:mod_http_api.start(@domain, [])
|
|
|
|
EjabberdOauthMock.init
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
:meck.unload
|
|
|
|
:meck.new :ejabberd_commands
|
2016-07-23 00:17:12 +02:00
|
|
|
:meck.new(:acl, [:passthrough]) # Need to fake acl to allow oauth
|
2016-03-31 15:06:41 +02:00
|
|
|
EjabberdAuthMock.init
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
test "HTTP GET simple command call with Basic Auth" do
|
|
|
|
EjabberdAuthMock.create_user @user, @domain, @userpass
|
|
|
|
|
|
|
|
# Mock a simple command() -> :ok
|
|
|
|
:meck.expect(:ejabberd_commands, :get_command_format,
|
|
|
|
fn (@acommand, {@user, @domain, @userpass, false}, @version) ->
|
|
|
|
{[], {:res, :rescode}}
|
|
|
|
end)
|
2016-07-19 04:27:49 +02:00
|
|
|
:meck.expect(:ejabberd_commands, :get_command_policy_and_scope,
|
|
|
|
fn (@acommand) -> {:ok, :user, [:erlang.atom_to_binary(@acommand,:utf8)]} end)
|
2016-07-26 12:17:37 +02:00
|
|
|
:meck.expect(:ejabberd_commands, :get_exposed_commands,
|
2016-04-01 11:13:48 +02:00
|
|
|
fn () -> [@acommand] end)
|
2016-03-31 15:06:41 +02:00
|
|
|
:meck.expect(:ejabberd_commands, :execute_command,
|
2016-05-31 00:07:26 +02:00
|
|
|
fn (:undefined, {@user, @domain, @userpass, false}, @acommand, [], @version, _) ->
|
2016-03-31 15:06:41 +02:00
|
|
|
:ok
|
|
|
|
end)
|
|
|
|
|
2016-04-01 11:13:48 +02:00
|
|
|
:ejabberd_config.add_local_option(:commands, [[{:add_commands, [@acommand]}]])
|
2016-03-31 15:06:41 +02:00
|
|
|
|
|
|
|
# Correct Basic Auth call
|
|
|
|
req = request(method: :GET,
|
|
|
|
path: ["api", @command],
|
|
|
|
q: [nokey: ""],
|
|
|
|
# Basic auth
|
|
|
|
auth: {@user<>"@"<>@domain, @userpass},
|
|
|
|
ip: {{127,0,0,1},60000},
|
|
|
|
host: @domain)
|
|
|
|
result = :mod_http_api.process([@command], req)
|
2016-04-01 11:13:48 +02:00
|
|
|
|
|
|
|
# history = :meck.history(:ejabberd_commands)
|
|
|
|
|
2016-03-31 15:06:41 +02:00
|
|
|
assert 200 == elem(result, 0) # HTTP code
|
|
|
|
assert "0" == elem(result, 2) # command result
|
|
|
|
|
|
|
|
# Bad password
|
|
|
|
req = request(method: :GET,
|
|
|
|
path: ["api", @command],
|
|
|
|
q: [nokey: ""],
|
|
|
|
# Basic auth
|
|
|
|
auth: {@user<>"@"<>@domain, @userpass<>"bad"},
|
|
|
|
ip: {{127,0,0,1},60000},
|
|
|
|
host: @domain)
|
|
|
|
result = :mod_http_api.process([@command], req)
|
|
|
|
assert 401 == elem(result, 0) # HTTP code
|
|
|
|
|
|
|
|
# Check that the command was executed only once
|
|
|
|
assert 1 ==
|
|
|
|
:meck.num_calls(:ejabberd_commands, :execute_command, :_)
|
|
|
|
|
|
|
|
assert :meck.validate :ejabberd_auth
|
|
|
|
assert :meck.validate :ejabberd_commands
|
|
|
|
end
|
|
|
|
|
|
|
|
test "HTTP GET simple command call with OAuth" do
|
|
|
|
EjabberdAuthMock.create_user @user, @domain, @userpass
|
|
|
|
|
|
|
|
# Mock a simple command() -> :ok
|
|
|
|
:meck.expect(:ejabberd_commands, :get_command_format,
|
|
|
|
fn (@acommand, {@user, @domain, {:oauth, _token}, false}, @version) ->
|
|
|
|
{[], {:res, :rescode}}
|
|
|
|
end)
|
2016-07-19 04:27:49 +02:00
|
|
|
:meck.expect(:ejabberd_commands, :get_command_policy_and_scope,
|
|
|
|
fn (@acommand) -> {:ok, :user, [:erlang.atom_to_binary(@acommand,:utf8), "ejabberd:user"]} end)
|
2016-07-26 12:17:37 +02:00
|
|
|
:meck.expect(:ejabberd_commands, :get_exposed_commands,
|
2016-04-01 11:13:48 +02:00
|
|
|
fn () -> [@acommand] end)
|
2016-03-31 15:06:41 +02:00
|
|
|
:meck.expect(:ejabberd_commands, :execute_command,
|
|
|
|
fn (:undefined, {@user, @domain, {:oauth, _token}, false},
|
2016-05-31 00:07:26 +02:00
|
|
|
@acommand, [], @version, _) ->
|
2016-03-31 15:06:41 +02:00
|
|
|
:ok
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
2016-07-19 04:27:49 +02:00
|
|
|
# Correct OAuth call using specific scope
|
2016-03-31 15:06:41 +02:00
|
|
|
token = EjabberdOauthMock.get_token @user, @domain, @command
|
|
|
|
req = request(method: :GET,
|
|
|
|
path: ["api", @command],
|
|
|
|
q: [nokey: ""],
|
|
|
|
# OAuth
|
|
|
|
auth: {:oauth, token, []},
|
|
|
|
ip: {{127,0,0,1},60000},
|
|
|
|
host: @domain)
|
|
|
|
result = :mod_http_api.process([@command], req)
|
|
|
|
assert 200 == elem(result, 0) # HTTP code
|
|
|
|
assert "0" == elem(result, 2) # command result
|
|
|
|
|
2016-07-19 04:27:49 +02:00
|
|
|
# Correct OAuth call using specific ejabberd:user scope
|
|
|
|
token = EjabberdOauthMock.get_token @user, @domain, "ejabberd:user"
|
|
|
|
req = request(method: :GET,
|
|
|
|
path: ["api", @command],
|
|
|
|
q: [nokey: ""],
|
|
|
|
# OAuth
|
|
|
|
auth: {:oauth, token, []},
|
|
|
|
ip: {{127,0,0,1},60000},
|
|
|
|
host: @domain)
|
|
|
|
result = :mod_http_api.process([@command], req)
|
|
|
|
assert 200 == elem(result, 0) # HTTP code
|
|
|
|
assert "0" == elem(result, 2) # command result
|
|
|
|
|
2016-03-31 15:06:41 +02:00
|
|
|
# Wrong OAuth token
|
|
|
|
req = request(method: :GET,
|
|
|
|
path: ["api", @command],
|
|
|
|
q: [nokey: ""],
|
|
|
|
# OAuth
|
|
|
|
auth: {:oauth, "bad"<>token, []},
|
|
|
|
ip: {{127,0,0,1},60000},
|
|
|
|
host: @domain)
|
|
|
|
result = :mod_http_api.process([@command], req)
|
|
|
|
assert 401 == elem(result, 0) # HTTP code
|
|
|
|
|
|
|
|
# Expired OAuth token
|
|
|
|
token = EjabberdOauthMock.get_token @user, @domain, @command, 1
|
|
|
|
:timer.sleep 1500
|
|
|
|
req = request(method: :GET,
|
|
|
|
path: ["api", @command],
|
|
|
|
q: [nokey: ""],
|
|
|
|
# OAuth
|
|
|
|
auth: {:oauth, token, []},
|
|
|
|
ip: {{127,0,0,1},60000},
|
|
|
|
host: @domain)
|
|
|
|
result = :mod_http_api.process([@command], req)
|
|
|
|
assert 401 == elem(result, 0) # HTTP code
|
|
|
|
|
|
|
|
# Wrong OAuth scope
|
|
|
|
token = EjabberdOauthMock.get_token @user, @domain, "bad_command"
|
|
|
|
:timer.sleep 1500
|
|
|
|
req = request(method: :GET,
|
|
|
|
path: ["api", @command],
|
|
|
|
q: [nokey: ""],
|
|
|
|
# OAuth
|
|
|
|
auth: {:oauth, token, []},
|
|
|
|
ip: {{127,0,0,1},60000},
|
|
|
|
host: @domain)
|
|
|
|
result = :mod_http_api.process([@command], req)
|
|
|
|
assert 401 == elem(result, 0) # HTTP code
|
|
|
|
|
2016-07-19 04:27:49 +02:00
|
|
|
# Check that the command was executed twice
|
|
|
|
assert 2 ==
|
2016-03-31 15:06:41 +02:00
|
|
|
:meck.num_calls(:ejabberd_commands, :execute_command, :_)
|
|
|
|
|
|
|
|
assert :meck.validate :ejabberd_auth
|
|
|
|
assert :meck.validate :ejabberd_commands
|
|
|
|
#assert :ok = :meck.history(:ejabberd_commands)
|
|
|
|
end
|
|
|
|
|
2016-07-23 00:17:12 +02:00
|
|
|
test "Request oauth token, resource owner password credentials" do
|
|
|
|
EjabberdAuthMock.create_user @user, @domain, @userpass
|
|
|
|
:application.set_env(:oauth2, :backend, :ejabberd_oauth)
|
|
|
|
:application.start(:oauth2)
|
|
|
|
|
|
|
|
# Mock a simple command() -> :ok
|
|
|
|
:meck.expect(:ejabberd_commands, :get_command_format,
|
|
|
|
fn (@acommand, {@user, @domain, {:oauth, _token}, false}, @version) ->
|
|
|
|
{[], {:res, :rescode}}
|
|
|
|
end)
|
|
|
|
:meck.expect(:ejabberd_commands, :get_command_policy_and_scope,
|
|
|
|
fn (@acommand) -> {:ok, :user, [:erlang.atom_to_binary(@acommand,:utf8), "ejabberd:user"]} end)
|
2016-07-26 12:17:37 +02:00
|
|
|
:meck.expect(:ejabberd_commands, :get_exposed_commands,
|
2016-07-23 00:17:12 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
# Correct password
|
|
|
|
req = request(method: :POST,
|
|
|
|
path: ["oauth", "token"],
|
|
|
|
q: [{"grant_type", "password"}, {"scope", @command}, {"username", @user<>"@"<>@domain}, {"ttl", "4000"}, {"password", @userpass}],
|
|
|
|
ip: {{127,0,0,1},60000},
|
|
|
|
host: @domain)
|
|
|
|
result = :ejabberd_oauth.process([], req)
|
|
|
|
assert 200 = elem(result, 0) #http code
|
|
|
|
{kv} = :jiffy.decode(elem(result,2))
|
2016-10-05 13:54:29 +02:00
|
|
|
assert {_, "bearer"} = List.keyfind(kv, "token_type", 0)
|
|
|
|
assert {_, @command} = List.keyfind(kv, "scope", 0)
|
|
|
|
assert {_, 4000} = List.keyfind(kv, "expires_in", 0)
|
|
|
|
{"access_token", _token} = List.keyfind(kv, "access_token", 0)
|
2016-07-23 00:17:12 +02:00
|
|
|
|
|
|
|
#missing grant_type
|
|
|
|
req = request(method: :POST,
|
|
|
|
path: ["oauth", "token"],
|
|
|
|
q: [{"scope", @command}, {"username", @user<>"@"<>@domain}, {"password", @userpass}],
|
|
|
|
ip: {{127,0,0,1},60000},
|
|
|
|
host: @domain)
|
|
|
|
result = :ejabberd_oauth.process([], req)
|
|
|
|
assert 400 = elem(result, 0) #http code
|
|
|
|
{kv} = :jiffy.decode(elem(result,2))
|
2016-10-05 13:54:29 +02:00
|
|
|
assert {_, "unsupported_grant_type"} = List.keyfind(kv, "error", 0)
|
2016-07-23 00:17:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
# incorrect user/pass
|
|
|
|
req = request(method: :POST,
|
|
|
|
path: ["oauth", "token"],
|
|
|
|
q: [{"grant_type", "password"}, {"scope", @command}, {"username", @user<>"@"<>@domain}, {"password", @userpass<>"aa"}],
|
|
|
|
ip: {{127,0,0,1},60000},
|
|
|
|
host: @domain)
|
|
|
|
result = :ejabberd_oauth.process([], req)
|
|
|
|
assert 400 = elem(result, 0) #http code
|
|
|
|
{kv} = :jiffy.decode(elem(result,2))
|
2016-10-05 13:54:29 +02:00
|
|
|
assert {_, "invalid_grant"} = List.keyfind(kv, "error", 0)
|
2016-07-23 00:17:12 +02:00
|
|
|
|
|
|
|
assert :meck.validate :ejabberd_auth
|
|
|
|
assert :meck.validate :ejabberd_commands
|
|
|
|
end
|
2016-04-01 11:13:48 +02:00
|
|
|
|
2016-03-31 15:06:41 +02:00
|
|
|
end
|