1: %%==============================================================================
    2: %% Copyright 2012 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(anonymous_SUITE).
   18: -compile([export_all, nowarn_export_all]).
   19: 
   20: -include_lib("jid/include/jid.hrl").
   21: -include_lib("escalus/include/escalus.hrl").
   22: -include_lib("eunit/include/eunit.hrl").
   23: 
   24: -import(distributed_helper, [mim/0, rpc/4]).
   25: 
   26: %%--------------------------------------------------------------------
   27: %% Suite configuration
   28: %%--------------------------------------------------------------------
   29: 
   30: all() ->
   31:     [{group, anonymous}].
   32: 
   33: groups() ->
   34:     [{anonymous, [sequence], all_tests()}].
   35: 
   36: all_tests() ->
   37:     [connection_is_registered_with_sasl_anon,
   38:      connection_is_registered_with_login,
   39:      messages_story].
   40: 
   41: suite() ->
   42:     escalus:suite().
   43: 
   44: %%--------------------------------------------------------------------
   45: %% Init & teardown
   46: %%--------------------------------------------------------------------
   47: init_per_suite(Config) ->
   48:     escalus:init_per_suite(Config).
   49: 
   50: end_per_suite(Config) ->
   51:     escalus:end_per_suite(Config).
   52: 
   53: init_per_testcase(CaseName, Config0) ->
   54:     NewUsers = escalus_ct:get_config(escalus_anon_users),
   55:     Config = [{escalus_users, NewUsers}] ++ Config0,
   56:     escalus:init_per_testcase(CaseName, Config).
   57: 
   58: end_per_testcase(CaseName, Config) ->
   59:     AnonJID = erlang:get(anon_user),
   60:     mongoose_helper:clear_last_activity(Config, AnonJID),
   61:     escalus:end_per_testcase(CaseName, Config).
   62: 
   63: %%--------------------------------------------------------------------
   64: %% Anonymous tests
   65: %%--------------------------------------------------------------------
   66: 
   67: connection_is_registered_with_sasl_anon(Config) ->
   68:     escalus:story(Config, [{jon, 1}], fun(Jon) ->
   69:         JID = jid:from_binary(escalus_client:short_jid(Jon)),
   70:         OrigName = escalus_users:get_username(Config, jon),
   71:         ?assertNotEqual(OrigName, JID#jid.luser),
   72:         F = fun() -> rpc(mim(), ejabberd_auth, does_user_exist, [JID]) end,
   73:         true = F(),
   74:         escalus_connection:kill(Jon),
   75:         mongoose_helper:wait_until(F, false)
   76:     end).
   77: 
   78: connection_is_registered_with_login(Config) ->
   79:     escalus:story(Config, [{anna, 1}], fun(Anna) ->
   80:         JID = jid:from_binary(escalus_client:short_jid(Anna)),
   81:         OrigName = escalus_users:get_username(Config, anna),
   82:         ?assertEqual(OrigName, JID#jid.luser),
   83:         F = fun() -> rpc(mim(), ejabberd_auth, does_user_exist, [JID]) end,
   84:         true = F(),
   85:         escalus_connection:kill(Anna),
   86:         mongoose_helper:wait_until(F, false)
   87:     end).
   88: 
   89: messages_story(Config) ->
   90:     escalus:story(Config, [{anna, 1}, {jon, 1}], fun(Anna, Jon) ->
   91:         erlang:put(anon_user, escalus_utils:get_jid(Jon)),
   92:         escalus_client:send(Jon, escalus_stanza:chat_to(Anna, <<"Hi!">>)),
   93:         Stanza = escalus_client:wait_for_stanza(Anna),
   94:         %% Below's dirty, but there is no other easy way...
   95:         escalus_assert:is_chat_message(<<"Hi!">>, Stanza)
   96:     end).