diff --git a/big_tests/dynamic_domains.config b/big_tests/dynamic_domains.config index 6f55ba42706..0204b4ebf50 100644 --- a/big_tests/dynamic_domains.config +++ b/big_tests/dynamic_domains.config @@ -174,5 +174,10 @@ {username, <<"jon">>}, {server, <<"anonymous.example.com">>}, {host, <<"localhost">>}, - {auth_method, <<"SASL-ANON">>}]} + {auth_method, <<"SASL-ANON">>}]}, + {anna, [ + {username, <<"anna">>}, + {password, <<"ignored">>}, + {server, <<"anonymous.example.com">>}, + {host, <<"localhost">>}]} ]}. diff --git a/big_tests/test.config b/big_tests/test.config index c8ebb0558da..cf890598aa6 100644 --- a/big_tests/test.config +++ b/big_tests/test.config @@ -224,8 +224,13 @@ {username, <<"jon">>}, {server, <<"anonymous.localhost">>}, {host, <<"localhost">>}, - {auth_method, <<"SASL-ANON">>}]} - ]}. + {auth_method, <<"SASL-ANON">>}]}, + {anna, [ + {username, <<"anna">>}, + {password, <<"ignored">>}, + {server, <<"anonymous.localhost">>}, + {host, <<"localhost">>}]} +]}. {presets, [{toml, diff --git a/big_tests/tests/anonymous_SUITE.erl b/big_tests/tests/anonymous_SUITE.erl index ad9b6da36cd..9bb57987fa6 100644 --- a/big_tests/tests/anonymous_SUITE.erl +++ b/big_tests/tests/anonymous_SUITE.erl @@ -18,7 +18,7 @@ -compile([export_all, nowarn_export_all]). -include_lib("escalus/include/escalus.hrl"). --include_lib("common_test/include/ct.hrl"). +-include_lib("eunit/include/eunit.hrl"). -import(distributed_helper, [mim/0, rpc/4]). @@ -33,7 +33,8 @@ groups() -> [{anonymous, [sequence], all_tests()}]. all_tests() -> - [connection_is_registered, + [connection_is_registered_with_sasl_anon, + connection_is_registered_with_login, messages_story]. suite() -> @@ -48,14 +49,8 @@ init_per_suite(Config) -> end_per_suite(Config) -> escalus:end_per_suite(Config). -init_per_group(_GroupName, Config) -> - escalus:create_users(Config, escalus:get_users([alice])). - -end_per_group(_GroupName, Config) -> - escalus:delete_users(Config, escalus:get_users([alice])). - init_per_testcase(CaseName, Config0) -> - NewUsers = proplists:get_value(escalus_users, Config0) ++ escalus_ct:get_config(escalus_anon_users), + NewUsers = escalus_ct:get_config(escalus_anon_users), Config = [{escalus_users, NewUsers}] ++ Config0, escalus:init_per_testcase(CaseName, Config). @@ -68,20 +63,33 @@ end_per_testcase(CaseName, Config) -> %% Anonymous tests %%-------------------------------------------------------------------- -connection_is_registered(Config) -> +connection_is_registered_with_sasl_anon(Config) -> escalus:story(Config, [{jon, 1}], fun(Jon) -> JID = jid:from_binary(escalus_client:short_jid(Jon)), + OrigName = escalus_users:get_username(Config, jon), + ?assertNotEqual(OrigName, JID#jid.user), F = fun() -> rpc(mim(), ejabberd_auth, does_user_exist, [JID]) end, true = F(), escalus_connection:kill(Jon), mongoose_helper:wait_until(F, false) end). +connection_is_registered_with_login(Config) -> + escalus:story(Config, [{anna, 1}], fun(Anna) -> + JID = jid:from_binary(escalus_client:short_jid(Anna)), + OrigName = escalus_users:get_username(Config, anna), + ?assertEqual(OrigName, JID#jid.user), + F = fun() -> rpc(mim(), ejabberd_auth, does_user_exist, [JID]) end, + true = F(), + escalus_connection:kill(Anna), + mongoose_helper:wait_until(F, false) + end). + messages_story(Config) -> - escalus:story(Config, [{alice, 1}, {jon, 1}], fun(Alice, Jon) -> + escalus:story(Config, [{anna, 1}, {jon, 1}], fun(Anna, Jon) -> erlang:put(anon_user, escalus_utils:get_jid(Jon)), - escalus_client:send(Jon, escalus_stanza:chat_to(Alice, <<"Hi!">>)), - Stanza = escalus_client:wait_for_stanza(Alice), + escalus_client:send(Jon, escalus_stanza:chat_to(Anna, <<"Hi!">>)), + Stanza = escalus_client:wait_for_stanza(Anna), %% Below's dirty, but there is no other easy way... escalus_assert:is_chat_message(<<"Hi!">>, Stanza) end). diff --git a/src/auth/ejabberd_auth.erl b/src/auth/ejabberd_auth.erl index db2e91c0af7..6044d1b19e6 100644 --- a/src/auth/ejabberd_auth.erl +++ b/src/auth/ejabberd_auth.erl @@ -323,8 +323,8 @@ does_user_exist(Status, _, _, _) -> -spec does_stored_user_exist(mongooseim:host_type(), jid:jid() | error) -> boolean() | {error, any()}. does_stored_user_exist(HostType, #jid{luser = LUser, lserver = LServer}) -> - F = fun(Mod) when Mod =/= ejabberd_auth_anonymous -> - does_user_exist_in_module(HostType, LUser, LServer, Mod) + F = fun(ejabberd_auth_anonymous) -> continue; + (Mod) -> does_user_exist_in_module(HostType, LUser, LServer, Mod) end, call_auth_modules_for_host_type(HostType, F, #{default => false, metric => does_user_exist}); does_stored_user_exist(_HostType, error) -> diff --git a/src/auth/ejabberd_auth_anonymous.erl b/src/auth/ejabberd_auth_anonymous.erl index 59c5e654e11..f30b50794ef 100644 --- a/src/auth/ejabberd_auth_anonymous.erl +++ b/src/auth/ejabberd_auth_anonymous.erl @@ -138,7 +138,9 @@ remove_connection(SID, LUser, LServer) -> JID :: jid:jid(), Info :: ejabberd_sm:info()) -> Acc when Acc :: any(). register_connection(Acc, HostType, SID, #jid{luser = LUser, lserver = LServer}, - #{auth_module := cyrsasl_anonymous}) -> + #{auth_module := AuthModule}) + when AuthModule =:= ejabberd_auth_anonymous; % login_anon + AuthModule =:= cyrsasl_anonymous -> % sasl_anon mongoose_hooks:register_user(HostType, LServer, LUser), US = {LUser, LServer}, mnesia:sync_dirty(fun() -> mnesia:write(#anonymous{us = US, sid = SID}) end), @@ -157,7 +159,6 @@ unregister_connection(Acc, SID, #jid{luser = LUser, lserver = LServer}, _, _) -> remove_connection(SID, LUser, LServer), Acc. - %% @doc Launch the hook to purge user data only for anonymous users. -spec purge_hook(boolean(), mongooseim:host_type(), jid:luser(), jid:lserver()) -> 'ok'. purge_hook(false, _HostType, _LUser, _LServer) ->