Skip to content

Commit

Permalink
Test error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Premwoik committed Feb 16, 2022
1 parent 78de8e6 commit c72ae7a
Showing 1 changed file with 77 additions and 29 deletions.
106 changes: 77 additions & 29 deletions big_tests/tests/graphql_muc_light_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
-include_lib("exml/include/exml.hrl").

-define(UNKNOWN_DOMAIN, <<"not-existing-domain">>).
-define(UNKNOWN, <<"not-existing">>).

suite() ->
require_rpc_nodes([mim]) ++ escalus:suite().
Expand All @@ -30,8 +31,10 @@ user_muc_light_handler() ->
admin_muc_light_handler() ->
[admin_create_room,
admin_create_identified_room,
admin_change_room_configuration,
admin_change_room_config,
admin_change_room_config_errors,
admin_invite_user,
admin_invite_user_errors,
admin_delete_room,
admin_kick_user,
admin_send_message_to_room,
Expand Down Expand Up @@ -104,7 +107,7 @@ admin_create_room_story(Config, Alice) ->
<<"participants">> := Participants} = get_ok_value(Path, Res),
?assertMatch(#jid{server = MucServer}, jid:from_binary(JID)),
?assertEqual([#{<<"jid">> => AliceBinLower, <<"affiliance">> => <<"owner">>}], Participants),
% Try with not existing domain
% Try with a non-existing domain
Res2 = execute_auth(admin_create_room_body(?UNKNOWN_DOMAIN, Name, AliceBin, Subject, null), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"does not exist">>)).

Expand All @@ -122,33 +125,53 @@ admin_create_identified_room_story(Config, Alice) ->
Path = [data, muc_light, createRoom],
#{<<"jid">> := JID, <<"name">> := Name, <<"subject">> := Subject} = get_ok_value(Path, Res),
?assertMatch(#jid{user = Id, server = MucServer}, jid:from_binary(JID)),
% Try to create room with existing ID
% Create a room with an existing ID
Res2 = execute_auth(admin_create_room_body(Domain, <<"snd room">>, AliceBin, Subject, Id), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"already exists">>)),
% Try with not existing domain
% Try with a non-existing domain
Res3 = execute_auth(admin_create_room_body(?UNKNOWN_DOMAIN, <<>>, AliceBin, Subject, Id), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res3), <<"does not exist">>)).

admin_change_room_configuration(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}], fun admin_change_room_configuration_story/2).
admin_change_room_config(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}], fun admin_change_room_config_story/2).

admin_change_room_configuration_story(Config, Alice) ->
admin_change_room_config_story(Config, Alice) ->
AliceBin = escalus_client:short_jid(Alice),
Domain = escalus_client:server(Alice),
Name = <<"first room">>,
Subject = <<"testing">>,
Id = atom_to_binary(?FUNCTION_NAME),
%% Create a new room
% Create a new room
execute_auth(admin_create_room_body(Domain, Name, AliceBin, Subject, Id), Config),
%% Try to change the room configuration
% Try to change the room configuration
Name2 = <<"changed room">>,
Subject2 = <<"not testing">>,
Res = execute_auth(admin_change_room_configuration_body(Id, Domain, AliceBin, Name2, Subject2), Config),
Path = [data, muc_light, changeRoomConfiguration],
?assertMatch(#{<<"name">> := Name2, <<"subject">> := Subject2}, get_ok_value(Path, Res)),
% Try with not existing domain
Res2 = execute_auth(admin_change_room_configuration_body(Id, ?UNKNOWN_DOMAIN, AliceBin,Name2, Subject2), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"does not exist">>)).
?assertMatch(#{<<"name">> := Name2, <<"subject">> := Subject2}, get_ok_value(Path, Res)).

admin_change_room_config_errors(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}], fun admin_change_room_config_errors_story/3).

admin_change_room_config_errors_story(Config, Alice, Bob) ->
AliceBin = escalus_client:short_jid(Alice),
BobBin = escalus_client:short_jid(Bob),
Domain = escalus_client:server(Alice),
RoomName = <<"first room">>,
{ok, #{jid := #jid{luser = RoomID}}} = create_room(<<>>, Domain, RoomName, <<>>, AliceBin),
{ok, _} = invite_user(Domain, RoomName, AliceBin, BobBin),
% Try to change the config with a non-existing domain
Res = execute_auth(admin_change_room_configuration_body(RoomID, ?UNKNOWN_DOMAIN, AliceBin, RoomName, <<>>), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res), <<"does not exist">>)),
% Try to change the config of the non-existing room
Res2 = execute_auth(admin_change_room_configuration_body(<<"unknown">>, Domain, AliceBin, RoomName, <<>>), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"does not exist">>)),
% Try to change the config by the non-existing user
Res3 = execute_auth(admin_change_room_configuration_body(RoomID, Domain, <<"wrong-user@wrong-domain">>, RoomName, <<>>), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res3), <<"not room participant">>)),
% Try to change a config by the user without permission
Res4 = execute_auth(admin_change_room_configuration_body(RoomID, Domain, BobBin, RoomName, <<>>), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res4), <<"not permission to change">>)).

admin_invite_user(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}], fun admin_invite_user_story/3).
Expand All @@ -158,18 +181,37 @@ admin_invite_user_story(Config, Alice, Bob) ->
BobBin = escalus_client:short_jid(Bob),
Domain = escalus_client:server(Alice),
Name = <<"first room">>,
Name2 = <<"second room">>,
{ok, #{jid := RoomJID}} = create_room(<<>>, Domain, Name, <<>>, AliceBin),
{ok, _} = create_room(<<>>, Domain, Name2, <<>>, AliceBin),

Res = execute_auth(admin_invite_user_body(Domain, Name, AliceBin, BobBin), Config),
?assertNotEqual(nomatch, binary:match(get_ok_value([data, muc_light, inviteUser], Res),
<<"successfully">>)),
BobName = escalus_utils:jid_to_lower(escalus_client:username(Bob)),
AliceName = escalus_utils:jid_to_lower(escalus_client:username(Alice)),
ExpectedAff = lists:sort([{{AliceName, Domain}, owner},
{{BobName, Domain}, member}]),
?assertMatch(ExpectedAff, lists:sort(get_room_aff(RoomJID))),
% Try with not existing domain
Res2 = execute_auth(admin_invite_user_body(?UNKNOWN_DOMAIN, Name, AliceBin, BobBin), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"does not exist">>)).
?assertMatch(ExpectedAff, lists:sort(get_room_aff(RoomJID))).

admin_invite_user_errors(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}], fun admin_invite_user_errors_story/3).

admin_invite_user_errors_story(Config, Alice, Bob) ->
AliceBin = escalus_client:short_jid(Alice),
BobBin = escalus_client:short_jid(Bob),
Domain = escalus_client:server(Alice),
Name = <<"first room">>,
{ok, #{jid := _RoomJID}} = create_room(<<>>, Domain, Name, <<>>, AliceBin),
% Try to invite a user to not existing room
Res = execute_auth(admin_invite_user_body(Domain, <<>>, AliceBin, BobBin), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res), <<"does not exist">>)),
% User without rooms tries to invite a user
Res2 = execute_auth(admin_invite_user_body(Domain, <<>>, BobBin, AliceBin), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"does not occupy any room">>)),
% Try with a non-existing domain
Res3 = execute_auth(admin_invite_user_body(?UNKNOWN_DOMAIN, Name, AliceBin, BobBin), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res3), <<"does not exist">>)).

admin_delete_room(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}], fun admin_delete_room_story/2).
Expand All @@ -184,7 +226,7 @@ admin_delete_room_story(Config, Alice) ->
?assertNotEqual(nomatch, binary:match(get_ok_value([data, muc_light, deleteRoom], Res),
<<"successfully">>)),
?assertEqual({error, not_exists}, get_room_info(jid:from_binary(RoomJID))),
% Try with not existing domain
% Try with a non-existing domain
Res2 = execute_auth(admin_delete_room_body(?UNKNOWN_DOMAIN, RoomID), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"does not exist">>)).

Expand All @@ -198,13 +240,13 @@ admin_kick_user_story(Config, Alice, Bob) ->
RoomName = <<"first room">>,
RoomID = <<"kick_user_test_room">>,
{ok, #{jid := RoomJID}} = create_room(RoomID, Domain, RoomName, <<>>, AliceBin),
{ok, _} = invite_user(Domain, RoomName, jid:from_binary(AliceBin), BobBin),
{ok, _} = invite_user(Domain, RoomName, AliceBin, BobBin),
?assertEqual(2, length(get_room_aff(RoomJID))),
Res = execute_auth(admin_kick_user_body(Domain, RoomID, BobBin), Config),
?assertNotEqual(nomatch, binary:match(get_ok_value([data, muc_light, kickUser], Res),
<<"successfully">>)),
?assertEqual(1, length(get_room_aff(RoomJID))),
% Try with not existing domain
% Try with a non-existing domain
Res2 = execute_auth(admin_kick_user_body(?UNKNOWN_DOMAIN, RoomID, BobBin), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"does not exist">>)).

Expand All @@ -220,11 +262,11 @@ admin_send_message_to_room_story(Config, Alice, Bob) ->
RoomID = <<"send_message_test_room">>,
MsgBody = <<"Hello there!">>,
{ok, #{jid := _RoomJID}} = create_room(RoomID, Domain, RoomName, <<>>, AliceBin),
{ok, _} = invite_user(Domain, RoomName, jid:from_binary(AliceBin), BobBin),
{ok, _} = invite_user(Domain, RoomName, AliceBin, BobBin),
Res = execute_auth(admin_send_message_to_room_body(Domain, RoomName, AliceBin, MsgBody), Config),
?assertNotEqual(nomatch, binary:match(get_ok_value([data, muc_light, sendMessageToRoom], Res),
<<"successfully">>)),
% Try with not existing domain
% Try with a non-existing domain
Res2 = execute_auth(admin_send_message_to_room_body(?UNKNOWN_DOMAIN, RoomName, AliceBin, MsgBody), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"does not exist">>)).

Expand All @@ -235,15 +277,17 @@ admin_get_room_messages_story(Config, Alice) ->
AliceBin = escalus_client:short_jid(Alice),
Domain = escalus_client:server(Alice),
RoomName = <<"first room">>,
RoomName2 = <<"second room">>,
RoomID = <<"get_messages_test_room">>,
{ok, #{jid := _RoomJID}} = create_room(RoomID, Domain, RoomName, <<>>, AliceBin),
{ok, _} = create_room(<<>>, Domain, RoomName2, <<>>, AliceBin),
Message = <<"Hello friends">>,
send_message_to_room(Domain, RoomName, jid:from_binary(AliceBin), Message),
mam_helper:maybe_wait_for_archive(Config),
Res = execute_auth(admin_get_room_messages_body(Domain, RoomID), Config),
[#{<<"stanza">> := StanzaXML}] = get_ok_value([data, muc_light, getRoomMessages, stanzas], Res),
?assertMatch({ok, #xmlel{name = <<"message">>}}, exml:parse(StanzaXML)),
% Try with not existing domain
% Try with a non-existing domain
Res2 = execute_auth(admin_get_room_messages_body(?UNKNOWN_DOMAIN, RoomID), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"does not exist">>)).

Expand All @@ -261,10 +305,10 @@ admin_list_user_rooms_story(Config, Alice) ->
Res = execute_auth(admin_list_user_rooms_body(AliceBin), Config),
?assertEqual(lists:sort([jid:to_binary(RoomJID), jid:to_binary(RoomJID2)]),
lists:sort(get_ok_value(Path, Res))),
% Try with not existing user
% Try with a non-existing user
Res2 = execute_auth(admin_list_user_rooms_body(<<"not-exist@", Domain/binary>>), Config),
?assertEqual([], lists:sort(get_ok_value(Path, Res2))),
% Try with not existing domain
% Try with a non-existing domain
Res3 = execute_auth(admin_list_user_rooms_body(<<"not-exist@not-exist">>), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res3), <<"does not exist">>)).

Expand All @@ -280,7 +324,7 @@ admin_list_room_users_story(Config, Alice) ->
Res = execute_auth(admin_list_room_users_body(Domain, RoomID), Config),
?assertEqual([#{<<"jid">> => AliceLower, <<"affiliance">> => <<"owner">>}],
get_ok_value([data, muc_light, listRoomUsers], Res)),
% Try with not existing domain
% Try with a non-existing domain
Res2 = execute_auth(admin_list_room_users_body(?UNKNOWN_DOMAIN, RoomID), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"does not exist">>)).

Expand All @@ -300,9 +344,12 @@ admin_get_room_config_story(Config, Alice) ->
?assertEqual(#{<<"jid">> => RoomJIDBin, <<"subject">> => RoomSubject, <<"name">> => RoomName,
<<"participants">> => [#{<<"jid">> => AliceLower, <<"affiliance">> => <<"owner">>}]},
get_ok_value([data, muc_light, getRoomConfig], Res)),
% Try with not existing domain
% Try with a non-existing domain
Res2 = execute_auth(admin_get_room_config_body(?UNKNOWN_DOMAIN, RoomID), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"does not exist">>)).
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"does not exist">>)),
% Try with a non-existing room
Res3 = execute_auth(admin_get_room_config_body(Domain, ?UNKNOWN), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res3), <<"does not exist">>)).

%% Helpers

Expand All @@ -317,7 +364,8 @@ create_room(Id, Domain, Name, Subject, CreatorBin) ->
CreatorJID = jid:from_binary(CreatorBin),
rpc(mim(), mod_muc_light_api, create_room, [Domain, Id, Name, CreatorJID, Subject]).

invite_user(Domain, RoomName, SenderJID, RecipientBin) ->
invite_user(Domain, RoomName, SenderBin, RecipientBin) ->
SenderJID = jid:from_binary(SenderBin),
RecipientJID = jid:from_binary(RecipientBin),
rpc(mim(), mod_muc_light_api, invite_to_room, [Domain, RoomName, SenderJID, RecipientJID]).

Expand Down

0 comments on commit c72ae7a

Please sign in to comment.