Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify the admin REST API #3753

Merged
merged 14 commits into from
Sep 15, 2022
22 changes: 22 additions & 0 deletions big_tests/tests/inbox_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ groups() ->
[
timeout_cleaner_flush_all,
rest_api_bin_flush_all,
rest_api_bin_flush_all_errors,
rest_api_bin_flush_user,
rest_api_bin_flush_user_errors,
xmpp_bin_flush,
bin_is_not_included_by_default
]},
Expand Down Expand Up @@ -1325,6 +1327,19 @@ rest_api_bin_flush_user(Config) ->
check_inbox(Bob, [], #{box => bin})
end).

rest_api_bin_flush_user_errors(Config) ->
Config1 = escalus_fresh:create_users(Config, [{alice, 1}]),
User = escalus_users:get_username(Config1, alice),
Domain = escalus_users:get_server(Config1, alice),
{{<<"400">>, <<"Bad Request">>}, <<"Invalid number of days">>} =
rest_helper:delete(admin, <<"/inbox/", Domain/binary, "/", User/binary, "/x/bin">>),
{{<<"400">>, <<"Bad Request">>}, <<"Invalid JID">>} =
rest_helper:delete(admin, <<"/inbox/", Domain/binary, "/@/0/bin">>),
{{<<"404">>, <<"Not Found">>}, <<"Domain not found">>} =
rest_helper:delete(admin, <<"/inbox/baddomain/", User/binary, "/0/bin">>),
{{<<"404">>, <<"Not Found">>}, <<"User baduser@", _/binary>>} =
rest_helper:delete(admin, <<"/inbox/", Domain/binary, "/baduser/0/bin">>).

rest_api_bin_flush_all(Config) ->
escalus:fresh_story(Config, [{alice, 1}, {bob, 1}, {kate, 1}], fun(Alice, Bob, Kate) ->
create_room_and_make_users_leave(Alice, Bob, Kate),
Expand All @@ -1337,6 +1352,13 @@ rest_api_bin_flush_all(Config) ->
check_inbox(Kate, [], #{box => bin})
end).

rest_api_bin_flush_all_errors(_Config) ->
HostTypePath = uri_string:normalize(#{path => domain_helper:host_type()}),
{{<<"400">>, <<"Bad Request">>}, <<"Invalid number of days">>} =
rest_helper:delete(admin, <<"/inbox/", HostTypePath/binary, "/x/bin">>),
{{<<"404">>, <<"Not Found">>}, <<"Host type not found">>} =
rest_helper:delete(admin, <<"/inbox/bad_host_type/0/bin">>).

timeout_cleaner_flush_all(Config) ->
escalus:fresh_story(Config, [{alice, 1}, {bob, 1}, {kate, 1}], fun(Alice, Bob, Kate) ->
create_room_and_make_users_leave(Alice, Bob, Kate),
Expand Down
131 changes: 88 additions & 43 deletions big_tests/tests/muc_http_api_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
-include_lib("eunit/include/eunit.hrl").
-include_lib("exml/include/exml.hrl").

-import(domain_helper, [domain/0]).
-import(domain_helper, [domain/0, secondary_domain/0]).
-import(rest_helper, [post/3, delete/2]).

%%--------------------------------------------------------------------
%% Suite configuration
Expand All @@ -37,9 +38,8 @@ all() ->
{group, negative}].

groups() ->
G = [{positive, [parallel], success_response() ++ complex()},
{negative, [parallel], failure_response()}],
ct_helper:repeat_all_until_all_ok(G).
[{positive, [parallel], success_response() ++ complex()},
{negative, [parallel], failure_response()}].

success_response() ->
[
Expand All @@ -56,8 +56,10 @@ complex() ->
].

failure_response() ->
[failed_invites,
failed_messages].
[room_creation_errors,
invite_errors,
kick_user_errors,
message_errors].

%%--------------------------------------------------------------------
%% Init & teardown
Expand Down Expand Up @@ -127,7 +129,7 @@ invite_online_user_to_room(Config) ->
recipient => escalus_client:short_jid(Bob),
reason => Reason},
{{<<"404">>, _}, <<"Room not found">>} = rest_helper:post(admin, Path, Body),
set_up_room(Config, Alice),
set_up_room(Config, escalus_client:short_jid(Alice)),
{{<<"204">>, _}, <<"">>} = rest_helper:post(admin, Path, Body),
Stanza = escalus:wait_for_stanza(Bob),
is_direct_invitation(Stanza),
Expand Down Expand Up @@ -274,56 +276,99 @@ multiparty_multiprotocol(Config) ->
user_sees_message_from(Alice, Room, 2))
end).

failed_invites(Config) ->
escalus:fresh_story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
Name = set_up_room(Config, Alice),
BAlice = escalus_client:short_jid(Alice),
BBob = escalus_client:short_jid(Bob),
% non-existing room
{{<<"404">>, _}, <<"Room not found">>} = send_invite(<<"thisroomdoesnotexist">>, BAlice, BBob),
% invite with bad jid
{{<<"400">>, _}, <<"Invalid jid:", _/binary>>} = send_invite(Name, BAlice, <<"@badjid">>),
{{<<"400">>, _}, <<"Invalid jid:", _/binary>>} = send_invite(Name, <<"@badjid">>, BBob),
ok
end).

failed_messages(Config) ->
escalus:fresh_story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
Name = set_up_room(Config, Alice),
% non-existing room
BAlice = escalus_client:short_jid(Alice),
BBob = escalus_client:short_jid(Bob),
{{<<"404">>, _}, <<"Room not found">>} = send_invite(<<"thisroomdoesnotexist">>, BAlice, BBob),
% invite with bad jid
{{<<"400">>, _}, <<"Invalid jid:", _/binary>>} = send_invite(Name, BAlice, <<"@badjid">>),
{{<<"400">>, _}, <<"Invalid jid:", _/binary>>} = send_invite(Name, <<"@badjid">>, BBob),
ok
end).

room_creation_errors(Config) ->
Config1 = escalus_fresh:create_users(Config, [{alice, 1}]),
AliceJid = escalus_users:get_jid(Config1, alice),
Name = ?config(room_name, Config),
Body = #{name => Name, owner => AliceJid, nick => <<"nick">>},
{{<<"400">>, _}, <<"Missing room name">>} =
post(admin, <<"/mucs/", (domain())/binary>>, maps:remove(name, Body)),
{{<<"400">>, _}, <<"Missing nickname">>} =
post(admin, <<"/mucs/", (domain())/binary>>, maps:remove(nick, Body)),
{{<<"400">>, _}, <<"Missing owner JID">>} =
post(admin, <<"/mucs/", (domain())/binary>>, maps:remove(owner, Body)),
{{<<"400">>, _}, <<"Invalid room name">>} =
post(admin, <<"/mucs/", (domain())/binary>>, Body#{name := <<"@invalid">>}),
{{<<"400">>, _}, <<"Invalid owner JID">>} =
post(admin, <<"/mucs/", (domain())/binary>>, Body#{owner := <<"@invalid">>}),
{{<<"404">>, _}, <<"Given user not found">>} =
post(admin, <<"/mucs/", (domain())/binary>>, Body#{owner := <<"baduser@baddomain">>}).

invite_errors(Config) ->
Config1 = escalus_fresh:create_users(Config, [{alice, 1}, {bob, 1}]),
AliceJid = escalus_users:get_jid(Config1, alice),
BobJid = escalus_users:get_jid(Config1, bob),
Name = set_up_room(Config1, AliceJid),
Path = path([Name, "participants"]),
Body = #{sender => AliceJid, recipient => BobJid, reason => <<"Join this room!">>},
{{<<"400">>, _}, <<"Missing sender JID">>} =
post(admin, Path, maps:remove(sender, Body)),
{{<<"400">>, _}, <<"Missing recipient JID">>} =
post(admin, Path, maps:remove(recipient, Body)),
{{<<"400">>, _}, <<"Missing invite reason">>} =
post(admin, Path, maps:remove(reason, Body)),
{{<<"400">>, _}, <<"Invalid recipient JID">>} =
post(admin, Path, Body#{recipient := <<"@badjid">>}),
{{<<"400">>, _}, <<"Invalid sender JID">>} =
post(admin, Path, Body#{sender := <<"@badjid">>}),
{{<<"404">>, _}, <<"MUC domain not found">>} =
post(admin, <<"/mucs/baddomain/", Name/binary, "/participants">>, Body),
{{<<"404">>, _}, <<"Room not found">>} =
post(admin, path(["thisroomdoesnotexist", "participants"]), Body).

kick_user_errors(Config) ->
Config1 = escalus_fresh:create_users(Config, [{alice, 1}]),
AliceJid = escalus_users:get_jid(Config1, alice),
Name = ?config(room_name, Config1),
{{<<"404">>, _}, <<"Room not found">>} = delete(admin, path([Name, "nick"])),
set_up_room(Config1, AliceJid),
mongoose_helper:wait_until(fun() -> check_if_moderator_not_found(Name) end, ok),
%% Alice sends presence to the room, making her the moderator
{ok, Alice} = escalus_client:start(Config1, alice, <<"res1">>),
escalus:send(Alice, muc_helper:stanza_muc_enter_room(Name, <<"ali">>)),
%% Alice gets her affiliation information and the room's subject line.
escalus:wait_for_stanzas(Alice, 2),
%% Kicking a non-existent nick succeeds in the current implementation
{{<<"204">>, _}, <<>>} = delete(admin, path([Name, "nick"])),
escalus_client:stop(Config, Alice).

%% @doc Check if the sequence below has already happened:
%% 1. Room notification to the owner is bounced back, because the owner is offline
%% 2. The owner is removed from the online users
%% As a result, a request to kick a user returns Error 404
check_if_moderator_not_found(RoomName) ->
case delete(admin, path([RoomName, "nick"])) of
{{<<"404">>, _}, <<"Moderator user not found">>} -> ok;
{{<<"204">>, _}, _} -> not_yet
end.

message_errors(Config) ->
Config1 = escalus_fresh:create_users(Config, [{alice, 1}]),
AliceJid = escalus_users:get_jid(Config1, alice),
Name = set_up_room(Config1, AliceJid),
Path = path([Name, "messages"]),
Body = #{from => AliceJid, body => <<"Greetings!">>},
% Message to a non-existent room succeeds in the current implementation
{{<<"204">>, _}, <<>>} = post(admin, path(["thisroomdoesnotexist", "messages"]), Body),
{{<<"400">>, _}, <<"Missing message body">>} = post(admin, Path, maps:remove(body, Body)),
{{<<"400">>, _}, <<"Missing sender JID">>} = post(admin, Path, maps:remove(from, Body)),
{{<<"400">>, _}, <<"Invalid sender JID">>} = post(admin, Path, Body#{from := <<"@invalid">>}).

%%--------------------------------------------------------------------
%% Ancillary (adapted from the MUC suite)
%%--------------------------------------------------------------------

set_up_room(Config, Alice) ->
set_up_room(Config, OwnerJID) ->
% create a room first
Name = ?config(room_name, Config),
Path = path([]),
Body = #{name => Name,
owner => escalus_client:short_jid(Alice),
owner => OwnerJID,
nick => <<"ali">>},
Res = rest_helper:post(admin, Path, Body),
{{<<"201">>, _}, Name} = Res,
Name.

send_invite(RoomName, BinFrom, BinTo) ->
Path = path([RoomName, "participants"]),
Reason = <<"I think you'll like this room!">>,
Body = #{sender => BinFrom,
recipient => BinTo,
reason => Reason},
rest_helper:post(admin, Path, Body).

make_distinct_name(Prefix) ->
{_, S, US} = os:timestamp(),
L = lists:flatten([integer_to_list(S rem 100), ".", integer_to_list(US)]),
Expand Down
144 changes: 101 additions & 43 deletions big_tests/tests/muc_light_http_api_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
-import(distributed_helper, [subhost_pattern/1]).
-import(domain_helper, [host_type/0, domain/0]).
-import(config_parser_helper, [mod_config/2]).
-import(rest_helper, [putt/3, post/3, delete/2]).

%%--------------------------------------------------------------------
%% Suite configuration
Expand All @@ -52,10 +53,11 @@ success_response() ->
].

negative_response() ->
[delete_non_existent_room,
create_non_unique_room,
create_room_on_non_existing_muc_server
].
[create_room_errors,
create_identifiable_room_errors,
invite_to_room_errors,
send_message_errors,
delete_room_errors].

%%--------------------------------------------------------------------
%% Init & teardown
Expand Down Expand Up @@ -193,44 +195,101 @@ delete_room(Config) ->
Alice, [Bob, Kate])
end).

delete_non_existent_room(Config) ->
RoomID = atom_to_binary(?FUNCTION_NAME),
RoomName = <<"wonderland">>,
escalus:fresh_story(Config,
[{alice, 1}, {bob, 1}, {kate, 1}],
fun(Alice, Bob, Kate)->
{{<<"404">>, _}, <<"Cannot remove not existing room">>} =
check_delete_room(Config, RoomName, RoomID,
<<"some_non_existent_room">>,
Alice, [Bob, Kate])
end).

create_non_unique_room(Config) ->
escalus:fresh_story(Config, [{alice, 1}], fun(Alice) ->
Path = path([muc_light_domain()]),
RandBits = base16:encode(crypto:strong_rand_bytes(5)),
Name = <<"wonderland">>,
RoomID = <<"just_some_id_", RandBits/binary>>,
Body = #{ id => RoomID,
name => Name,
owner => escalus_client:short_jid(Alice),
subject => <<"Lewis Carol">>
},
{{<<"201">>, _}, _RoomJID} = rest_helper:putt(admin, Path, Body),
{{<<"403">>, _}, <<"Room already exists">>} = rest_helper:putt(admin, Path, Body),
ok
end).

create_room_on_non_existing_muc_server(Config) ->
escalus:fresh_story(Config, [{alice, 1}], fun(Alice) ->
Path = path([domain_helper:domain()]),
Name = <<"wonderland">>,
Body = #{ name => Name,
owner => escalus_client:short_jid(Alice),
subject => <<"Lewis Carol">>
},
{{<<"404">>,<<"Not Found">>}, _} = rest_helper:post(admin, Path, Body)
end).
create_room_errors(Config) ->
Config1 = escalus_fresh:create_users(Config, [{alice, 1}]),
AliceJid = escalus_users:get_jid(Config1, alice),
Path = path([muc_light_domain()]),
Body = #{name => <<"Name">>, owner => AliceJid, subject => <<"Lewis Carol">>},
{{<<"400">>, _}, <<"Missing room name">>} =
post(admin, Path, maps:remove(name, Body)),
{{<<"400">>, _}, <<"Missing owner JID">>} =
post(admin, Path, maps:remove(owner, Body)),
{{<<"400">>, _}, <<"Missing room subject">>} =
post(admin, Path, maps:remove(subject, Body)),
{{<<"400">>, _}, <<"Invalid owner JID">>} =
post(admin, Path, Body#{owner := <<"@invalid">>}),
{{<<"404">>, _}, <<"MUC Light server not found">>} =
post(admin, path([domain_helper:domain()]), Body).

create_identifiable_room_errors(Config) ->
Config1 = escalus_fresh:create_users(Config, [{alice, 1}]),
AliceJid = escalus_users:get_jid(Config1, alice),
Path = path([muc_light_domain()]),
Body = #{id => <<"ID">>, name => <<"NameA">>, owner => AliceJid, subject => <<"Lewis Carol">>},
{{<<"201">>, _}, _RoomJID} = putt(admin, Path, Body#{id => <<"ID1">>}),
% Fails to create a room with the same ID
{{<<"400">>, _}, <<"Missing room ID">>} =
putt(admin, Path, maps:remove(id, Body)),
{{<<"400">>, _}, <<"Missing room name">>} =
putt(admin, Path, maps:remove(name, Body)),
{{<<"400">>, _}, <<"Missing owner JID">>} =
putt(admin, Path, maps:remove(owner, Body)),
{{<<"400">>, _}, <<"Missing room subject">>} =
putt(admin, Path, maps:remove(subject, Body)),
{{<<"400">>, _}, <<"Invalid owner JID">>} =
putt(admin, Path, Body#{owner := <<"@invalid">>}),
{{<<"403">>, _}, <<"Room already exists">>} =
putt(admin, Path, Body#{id := <<"ID1">>, name := <<"NameB">>}),
{{<<"404">>, _}, <<"MUC Light server not found">>} =
putt(admin, path([domain_helper:domain()]), Body).

invite_to_room_errors(Config) ->
Config1 = escalus_fresh:create_users(Config, [{alice, 1}, {bob, 1}]),
AliceJid = escalus_users:get_jid(Config1, alice),
BobJid = escalus_users:get_jid(Config1, bob),
Name = jid:nodeprep(<<(escalus_users:get_username(Config1, alice))/binary, "-room">>),
muc_light_helper:create_room(Name, muc_light_domain(), alice, [], Config1, <<"v1">>),
Path = path([muc_light_domain(), Name, "participants"]),
Body = #{sender => AliceJid, recipient => BobJid},
{{<<"400">>, _}, <<"Missing recipient JID">>} =
rest_helper:post(admin, Path, maps:remove(recipient, Body)),
{{<<"400">>, _}, <<"Missing sender JID">>} =
rest_helper:post(admin, Path, maps:remove(sender, Body)),
{{<<"400">>, _}, <<"Invalid recipient JID">>} =
rest_helper:post(admin, Path, Body#{recipient := <<"@invalid">>}),
{{<<"400">>, _}, <<"Invalid sender JID">>} =
rest_helper:post(admin, Path, Body#{sender := <<"@invalid">>}),
{{<<"403">>, _}, <<"Given user does not occupy this room">>} =
rest_helper:post(admin, Path, Body#{sender := BobJid, recipient := AliceJid}),
{{<<"404">>, _}, <<"MUC Light server not found">>} =
rest_helper:post(admin, path([domain(), Name, "participants"]), Body).

send_message_errors(Config) ->
Config1 = escalus_fresh:create_users(Config, [{alice, 1}, {bob, 1}]),
AliceJid = escalus_users:get_jid(Config1, alice),
BobJid = escalus_users:get_jid(Config1, bob),
Name = jid:nodeprep(<<(escalus_users:get_username(Config1, alice))/binary, "-room">>),
muc_light_helper:create_room(Name, muc_light_domain(), alice, [], Config1, <<"v1">>),
Path = path([muc_light_domain(), Name, "messages"]),
Body = #{from => AliceJid, body => <<"hello">>},
{{<<"204">>, _}, <<>>} =
rest_helper:post(admin, Path, Body),
{{<<"400">>, _}, <<"Missing message body">>} =
rest_helper:post(admin, Path, maps:remove(body, Body)),
{{<<"400">>, _}, <<"Missing sender JID">>} =
rest_helper:post(admin, Path, maps:remove(from, Body)),
{{<<"400">>, _}, <<"Invalid sender JID">>} =
rest_helper:post(admin, Path, Body#{from := <<"@invalid">>}),
{{<<"403">>, _}, <<"Given user does not occupy this room">>} =
rest_helper:post(admin, Path, Body#{from := BobJid}),
{{<<"403">>, _}, <<"Given user does not occupy this room">>} =
rest_helper:post(admin, path([muc_light_domain(), "badroom", "messages"]), Body),
{{<<"404">>, _}, <<"MUC Light server not found">>} =
rest_helper:post(admin, path([domain(), Name, "messages"]), Body).

delete_room_errors(_Config) ->
{{<<"400">>, _}, <<"Invalid room ID or domain name">>} =
delete(admin, path([muc_light_domain(), "@badroom", "management"])),
{{<<"404">>, _}, _} =
delete(admin, path([muc_light_domain()])),
{{<<"404">>, _}, _} =
delete(admin, path([muc_light_domain(), "badroom"])),
{{<<"404">>, _}, <<"Cannot remove not existing room">>} =
delete(admin, path([muc_light_domain(), "badroom", "management"])),
{{<<"404">>, _}, <<"MUC Light server not found">>} =
delete(admin, path([domain(), "badroom", "management"])),
{{<<"404">>, _}, <<"MUC Light server not found">>} =
delete(admin, path(["baddomain", "badroom", "management"])).

%%--------------------------------------------------------------------
%% Ancillary (borrowed and adapted from the MUC and MUC Light suites)
Expand Down Expand Up @@ -286,7 +345,6 @@ check_delete_room(_Config, RoomName, RoomIDToCreate, RoomIDToDelete, RoomOwner,
Path = path([muc_light_domain(), RoomIDToDelete, "management"]),
rest_helper:delete(admin, Path).


%%--------------------------------------------------------------------
%% Helpers
%%--------------------------------------------------------------------
Expand Down
Loading