Skip to content

Commit

Permalink
Cover more error cases in rest_SUITE
Browse files Browse the repository at this point in the history
Also: reorganize tests for easier management
  • Loading branch information
Paweł Chrząszcz committed Sep 7, 2022
1 parent 19b5e81 commit a3efc38
Showing 1 changed file with 75 additions and 50 deletions.
125 changes: 75 additions & 50 deletions big_tests/tests/rest_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ groups() ->
[{admin, [parallel], test_cases()},
{auth, [parallel], auth_test_cases()},
{blank_auth, [parallel], blank_auth_testcases()},
{roster, [parallel], [list_contacts,
befriend_and_alienate,
befriend_and_alienate_auto,
invalid_roster_operations]}
{roster, [parallel], roster_test_cases()}
].

auth_test_cases() ->
Expand All @@ -85,6 +82,15 @@ test_cases() ->
password_can_be_changed
].

roster_test_cases() ->
[list_contacts,
befriend_and_alienate,
befriend_and_alienate_auto,
invalid_contacts_get,
invalid_contacts_post,
invalid_contacts_put,
invalid_contacts_delete].

suite() ->
escalus:suite().

Expand Down Expand Up @@ -365,6 +371,7 @@ befriend_and_alienate(Config) ->
check_roster_empty(BobPath),
% adds them to rosters
{?NOCONTENT, _} = post(admin, AlicePath, #{jid => BobJID}),
{?NOCONTENT, _} = post(admin, AlicePath, #{jid => BobJID}), % it is idempotent
{?NOCONTENT, _} = post(admin, BobPath, #{jid => AliceJID}),
check_roster(BobPath, AliceJID, none, none),
check_roster(AlicePath, BobJID, none, none),
Expand Down Expand Up @@ -455,52 +462,70 @@ befriend_and_alienate_auto(Config) ->
),
ok.

invalid_roster_operations(Config) ->
escalus:fresh_story(
Config, [{alice, 1}, {bob, 1}],
fun(Alice, Bob) ->
AliceJID = escalus_utils:jid_to_lower(
escalus_client:short_jid(Alice)),
BobJID = escalus_utils:jid_to_lower(escalus_client:short_jid(Bob)),
AliceS = binary_to_list(AliceJID),
BobS = binary_to_list(BobJID),
AlicePath = lists:flatten(["/contacts/", AliceS]),
% adds them to rosters
{?BAD_REQUEST, <<"Invalid JID">>} = post(admin, AlicePath, #{jid => <<"@invalidjid">>}),
{?BAD_REQUEST, <<"Invalid user JID">>} = post(admin, "/contacts/@invalid_jid", #{jid => BobJID}),
% it is idempotent
{?NOCONTENT, _} = post(admin, AlicePath, #{jid => BobJID}),
{?NOCONTENT, _} = post(admin, AlicePath, #{jid => BobJID}),
PutPathA = lists:flatten([AlicePath, "/@invalid_jid"]),
{?BAD_REQUEST, <<"Invalid contact JID">>} = putt(admin, PutPathA, #{action => <<"subscribe">>}),
PutPathB = lists:flatten(["/contacts/@invalid_jid/", BobS]),
{?BAD_REQUEST, <<"Invalid user JID">>} = putt(admin, PutPathB, #{action => <<"subscribe">>}),
PutPathC = lists:flatten([AlicePath, "/", BobS]),
{?BAD_REQUEST, <<"Invalid action">>} = putt(admin, PutPathC, #{action => <<"something stupid">>}),
ManagePath = lists:flatten(["/contacts/",
AliceS,
"/",
BobS,
"/manage"
]),
{?BAD_REQUEST, <<"Invalid action">>} = putt(admin, ManagePath, #{action => <<"off with his head">>}),
MangePathA = lists:flatten(["/contacts/",
"@invalid",
"/",
BobS,
"/manage"
]),
{?BAD_REQUEST, <<"Invalid user JID">>} = putt(admin, MangePathA, #{action => <<"connect">>}),
MangePathB = lists:flatten(["/contacts/",
AliceS,
"/",
"@bzzz",
"/manage"
]),
{?BAD_REQUEST, <<"Invalid contact JID">>} = putt(admin, MangePathB, #{action => <<"connect">>}),
ok
end
).
invalid_contacts_get(_Config) ->
{?NOT_FOUND, <<"Domain not found">>} = gett(admin, contacts_path("baduser@baddomain")).

invalid_contacts_post(Config) ->
Config1 = escalus_fresh:create_users(Config, [{alice, 1}, {bob, 1}]),
BobJID = escalus_users:get_jid(Config, bob),
AliceS = binary_to_list(escalus_users:get_jid(Config1, alice)),
DomainS = binary_to_list(domain()),
{?BAD_REQUEST, <<"Missing JID">>} =
post(admin, contacts_path(AliceS), #{}),
{?BAD_REQUEST, <<"Invalid JID">>} =
post(admin, contacts_path(AliceS), #{jid => <<"@invalidjid">>}),
{?BAD_REQUEST, <<"Invalid user JID">>} =
post(admin, contacts_path("@invalid_jid"), #{jid => BobJID}),
{?NOT_FOUND, <<"The user baduser@", _/binary>>} =
post(admin, contacts_path("baduser@" ++ DomainS), #{jid => BobJID}),
{?NOT_FOUND, <<"Domain not found">>} =
post(admin, contacts_path("baduser@baddomain"), #{jid => BobJID}).

invalid_contacts_put(Config) ->
Config1 = escalus_fresh:create_users(Config, [{alice, 1}, {bob, 1}]),
AliceS = binary_to_list(escalus_users:get_jid(Config1, alice)),
BobS = binary_to_list(escalus_users:get_jid(Config1, bob)),
DomainS = binary_to_list(domain()),
{?BAD_REQUEST, <<"Invalid contact JID">>} =
putt(admin, contacts_path(AliceS, "@invalid_jid"), #{action => <<"subscribe">>}),
{?BAD_REQUEST, <<"Invalid user JID">>} =
putt(admin, contacts_path("@invalid_jid", BobS), #{action => <<"subscribe">>}),
{?BAD_REQUEST, <<"Missing action">>} =
putt(admin, contacts_path(AliceS, BobS), #{}),
{?BAD_REQUEST, <<"Missing action">>} =
putt(admin, contacts_manage_path(AliceS, BobS), #{}),
{?BAD_REQUEST, <<"Invalid action">>} =
putt(admin, contacts_path(AliceS, BobS), #{action => <<"something stupid">>}),
{?BAD_REQUEST, <<"Invalid action">>} =
putt(admin, contacts_manage_path(AliceS, BobS), #{action => <<"off with his head">>}),
{?BAD_REQUEST, <<"Invalid user JID">>} =
putt(admin, contacts_manage_path("@invalid", BobS), #{action => <<"connect">>}),
{?BAD_REQUEST, <<"Invalid contact JID">>} =
putt(admin, contacts_manage_path(AliceS, "@bzzz"), #{action => <<"connect">>}),
{?NOT_FOUND, <<"The user baduser@baddomain does not exist">>} =
putt(admin, contacts_manage_path(AliceS, "baduser@baddomain"), #{action => <<"connect">>}),
{?NOT_FOUND, <<"Domain not found">>} =
putt(admin, contacts_manage_path("baduser@baddomain", AliceS), #{action => <<"connect">>}),
{?NOT_FOUND, <<"Cannot remove", _/binary>>} =
putt(admin, contacts_manage_path(AliceS, "baduser@" ++ DomainS), #{action => <<"disconnect">>}).

invalid_contacts_delete(Config) ->
Config1 = escalus_fresh:create_users(Config, [{alice, 1}]),
AliceS = binary_to_list(escalus_users:get_jid(Config1, alice)),
DomainS = binary_to_list(domain()),
{?NOT_FOUND, <<"Cannot remove", _/binary>>} =
delete(admin, contacts_path(AliceS, "baduser@" ++ DomainS)),
{?BAD_REQUEST, <<"Missing contact JID">>} =
delete(admin, contacts_path(AliceS)).

contacts_path(UserJID) ->
"/contacts/" ++ UserJID.

contacts_path(UserJID, ContactJID) ->
contacts_path(UserJID) ++ "/" ++ ContactJID.

contacts_manage_path(UserJID, ContactJID) ->
contacts_path(UserJID, ContactJID) ++ "/manage".

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

0 comments on commit a3efc38

Please sign in to comment.