1: %%==============================================================================
    2: %% Copyright 2014 Erlang Solutions Ltd.
    3: %%
    4: %% Licensed under the Apache License, Version 2.0 (the "License");
    5: %% you may not use this file except in compliance with the License.
    6: %% You may obtain a copy of the License at
    7: %%
    8: %% http://www.apache.org/licenses/LICENSE-2.0
    9: %%
   10: %% Unless required by applicable law or agreed to in writing, software
   11: %% distributed under the License is distributed on an "AS IS" BASIS,
   12: %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13: %% See the License for the specific language governing permissions and
   14: %% limitations under the License.
   15: %%==============================================================================
   16: 
   17: -module(auth_dummy_SUITE).
   18: -compile([export_all, nowarn_export_all]).
   19: -author('kacper.mentel@erlang-solutions.com').
   20: 
   21: -include_lib("common_test/include/ct.hrl").
   22: 
   23: -define(DOMAIN, <<"localhost">>).
   24: -define(HOST_TYPE, <<"some host type">>).
   25: 
   26: %%--------------------------------------------------------------------
   27: %% Suite configuration
   28: %%--------------------------------------------------------------------
   29: 
   30: all() -> [
   31:     authorize,
   32:     ejabberd_auth_interfaces,
   33:     supports_dynamic_domains
   34: ].
   35: 
   36: init_per_suite(C) ->
   37:     {ok, _} = application:ensure_all_started(jid),
   38:     AuthOpts = #{methods => [dummy],
   39:                  dummy => #{base_time => 5, variance => 10}},
   40:     mongoose_config:set_opts(#{{auth, ?HOST_TYPE} => AuthOpts}),
   41:     C.
   42: 
   43: end_per_suite(_C) ->
   44:     mongoose_config:erase_opts().
   45: 
   46: %%--------------------------------------------------------------------
   47: %% Authentication tests
   48: %%--------------------------------------------------------------------
   49: 
   50: authorize(_Config) ->
   51:     Creds = mongoose_credentials:new(?DOMAIN, ?HOST_TYPE, #{}),
   52:     {ok, Creds2} = ejabberd_auth_dummy:authorize(Creds),
   53:     ejabberd_auth_dummy = mongoose_credentials:get(Creds2, auth_module).
   54: 
   55: ejabberd_auth_interfaces(_Config) ->
   56:     [meck:new(M, Opts) || {M, Opts} <-
   57:         [{mongoose_domain_api, []}, {ejabberd_auth_dummy, [passthrough]},
   58:          {mongoose_metrics, []}]],
   59: 
   60:     meck:expect(mongoose_domain_api, get_domain_host_type,
   61:                 fun(?DOMAIN) -> {ok, ?HOST_TYPE} end),
   62:     meck:expect(mongoose_metrics, update, fun(_, _, _) -> ok end),
   63: 
   64:     Creds = mongoose_credentials:new(?DOMAIN, ?HOST_TYPE, #{}),
   65:     {ok, Creds2} = ejabberd_auth:authorize(Creds),
   66:     ejabberd_auth_dummy = mongoose_credentials:get(Creds2, auth_module),
   67: 
   68:     UserName = <<"any_user">>, Password = <<"any_pasword">>,
   69:     JID = jid:make(UserName, ?DOMAIN, <<"any_resource">>),
   70:     true = ejabberd_auth:check_password(JID, Password),
   71:     Args1 = [?HOST_TYPE, UserName, ?DOMAIN, Password],
   72:     1 = meck:num_calls(ejabberd_auth_dummy, check_password, Args1),
   73: 
   74:     Digest = <<"any_digest">>, DigestGen = fun(_) -> <<"">> end,
   75:     false = ejabberd_auth:check_password(JID, Password, Digest, DigestGen),
   76:     Args2 = [?HOST_TYPE, UserName, ?DOMAIN, Password, Digest, DigestGen],
   77:     1 = meck:num_calls(ejabberd_auth_dummy, check_password, Args2).
   78: 
   79: supports_dynamic_domains(_) ->
   80:     true = ejabberd_auth:does_method_support(dummy, dynamic_domains),
   81:     false = ejabberd_auth:does_method_support(invalid_method, dynamic_domains),
   82:     false = ejabberd_auth:does_method_support(dummy, invalid_feature).