diff --git a/big_tests/tests/graphql_token_SUITE.erl b/big_tests/tests/graphql_token_SUITE.erl index fa772b4c7c..fc3641d185 100644 --- a/big_tests/tests/graphql_token_SUITE.erl +++ b/big_tests/tests/graphql_token_SUITE.erl @@ -2,36 +2,36 @@ -compile([export_all, nowarn_export_all]). --import(distributed_helper, [require_rpc_nodes/1]). --import(graphql_helper, [execute_user/3, execute_auth/2, user_to_bin/1]). +-import(distributed_helper, [require_rpc_nodes/1, mim/0]). +-import(graphql_helper, [execute_command/4, execute_user_command/5, user_to_bin/1, + get_ok_value/2, get_err_code/1]). --include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). --include_lib("exml/include/exml.hrl"). --include_lib("escalus/include/escalus.hrl"). suite() -> require_rpc_nodes([mim]) ++ escalus:suite(). all() -> [{group, user}, - {group, admin}]. + {group, admin_http}, + {group, admin_cli}]. groups() -> - [{user, [], user_cases()}, - {admin, [], admin_cases()}]. - -user_cases() -> - [user_request_token, - user_revoke_token_no_token_before, - user_revoke_token]. - -admin_cases() -> - [admin_request_token, - admin_request_token_no_user, - admin_revoke_token_no_user, - admin_revoke_token_no_token, - admin_revoke_token]. + [{user, [], user_tests()}, + {admin_http, [], admin_tests()}, + {admin_cli, [], admin_tests()}]. + +user_tests() -> + [user_request_token_test, + user_revoke_token_no_token_before_test, + user_revoke_token_test]. + +admin_tests() -> + [admin_request_token_test, + admin_request_token_no_user_test, + admin_revoke_token_no_user_test, + admin_revoke_token_no_token_test, + admin_revoke_token_test]. init_per_suite(Config0) -> case mongoose_helper:is_rdbms_enabled(domain_helper:host_type()) of @@ -39,7 +39,8 @@ init_per_suite(Config0) -> HostType = domain_helper:host_type(), Config = dynamic_modules:save_modules(HostType, Config0), dynamic_modules:ensure_modules(HostType, required_modules()), - escalus:init_per_suite(Config); + Config1 = escalus:init_per_suite(Config), + ejabberd_node_utils:init(mim(), Config1); false -> {skip, "RDBMS not available"} end. @@ -60,12 +61,15 @@ auth_token_opts() -> Defaults#{validity_period => #{access => #{value => 60, unit => minutes}, refresh => #{value => 1, unit => days}}}. -init_per_group(admin, Config) -> +init_per_group(admin_http, Config) -> graphql_helper:init_admin_handler(Config); +init_per_group(admin_cli, Config) -> + graphql_helper:init_admin_cli(Config); init_per_group(user, Config) -> - [{schema_endpoint, user} | Config]. + graphql_helper:init_user(Config). end_per_group(_, _Config) -> + graphql_helper:clean(), escalus_fresh:clean(). init_per_testcase(CaseName, Config) -> @@ -76,118 +80,76 @@ end_per_testcase(CaseName, Config) -> % User tests -user_request_token(Config) -> - escalus:fresh_story_with_config(Config, [{alice, 1}], fun user_request_token/2). +user_request_token_test(Config) -> + escalus:fresh_story_with_config(Config, [{alice, 1}], fun user_request_token_test/2). -user_request_token(Config, Alice) -> - Req = #{query => user_request_token_mutation(), operationName => <<"M1">>, - variables => #{}}, - Res = execute_user(Req, Alice, Config), - #{<<"refresh">> := Refresh, <<"access">> := Access} = ok_result(<<"token">>, - <<"requestToken">>, Res), +user_request_token_test(Config, Alice) -> + Res = user_request_token(Alice, Config), + #{<<"refresh">> := Refresh, <<"access">> := Access} = + get_ok_value([data, token, requestToken], Res), ?assert(is_binary(Refresh)), ?assert(is_binary(Access)). -user_revoke_token_no_token_before(Config) -> - escalus:fresh_story_with_config(Config, [{alice, 1}], fun user_revoke_token_no_token_before/2). - -user_revoke_token_no_token_before(Config, Alice) -> - Req = #{query => user_revoke_token_mutation(), operationName => <<"M1">>, - variables => #{}}, - Res = execute_user(Req, Alice, Config), - ParsedRes = error_result(<<"extensions">>, <<"code">>, Res), - ?assertEqual(<<"not_found">>, ParsedRes). - -user_revoke_token(Config) -> - escalus:fresh_story_with_config(Config, [{alice, 1}], fun user_revoke_token/2). - -user_revoke_token(Config, Alice) -> - Req = #{query => user_request_token_mutation(), operationName => <<"M1">>, - variables => #{}}, - execute_user(Req, Alice, Config), - Req2 = #{query => user_revoke_token_mutation(), operationName => <<"M1">>, - variables => #{}}, - Res2 = execute_user(Req2, Alice, Config), - ParsedRes = ok_result(<<"token">>, <<"revokeToken">>, Res2), +user_revoke_token_no_token_before_test(Config) -> + escalus:fresh_story_with_config(Config, [{alice, 1}], fun user_revoke_token_no_token_before_test/2). + +user_revoke_token_no_token_before_test(Config, Alice) -> + Res = user_revoke_token(Alice, Config), + ?assertEqual(<<"not_found">>, get_err_code(Res)). + +user_revoke_token_test(Config) -> + escalus:fresh_story_with_config(Config, [{alice, 1}], fun user_revoke_token_test/2). + +user_revoke_token_test(Config, Alice) -> + user_request_token(Alice, Config), + Res2 = user_revoke_token(Alice, Config), + ParsedRes = get_ok_value([data, token, revokeToken], Res2), ?assertEqual(<<"Revoked.">>, ParsedRes). % Admin tests -admin_request_token(Config) -> - escalus:fresh_story_with_config(Config, [{alice, 1}], fun admin_request_token/2). +admin_request_token_test(Config) -> + escalus:fresh_story_with_config(Config, [{alice, 1}], fun admin_request_token_test/2). -admin_request_token(Config, Alice) -> - Req = #{query => admin_request_token_mutation(), operationName => <<"M1">>, - variables => #{<<"user">> => user_to_bin(Alice)}}, - Res = execute_auth(Req, Config), - #{<<"refresh">> := Refresh, <<"access">> := Access} = ok_result(<<"token">>, - <<"requestToken">>, Res), +admin_request_token_test(Config, Alice) -> + Res = admin_request_token(user_to_bin(Alice), Config), + #{<<"refresh">> := Refresh, <<"access">> := Access} = + get_ok_value([data, token, requestToken], Res), ?assert(is_binary(Refresh)), ?assert(is_binary(Access)). -admin_request_token_no_user(Config) -> - Req = #{query => admin_request_token_mutation(), operationName => <<"M1">>, - variables => #{<<"user">> => <<"AAAA">>}}, - Res = execute_auth(Req, Config), - ParsedRes = error_result(<<"extensions">>, <<"code">>, Res), - ?assertEqual(<<"not_found">>, ParsedRes). - -admin_revoke_token_no_user(Config) -> - Req = #{query => admin_revoke_token_mutation(), operationName => <<"M1">>, - variables => #{<<"user">> => <<"AAAA">>}}, - Res = execute_auth(Req, Config), - ParsedRes = error_result(<<"extensions">>, <<"code">>, Res), - ?assertEqual(<<"not_found">>, ParsedRes). - -admin_revoke_token_no_token(Config) -> - escalus:fresh_story_with_config(Config, [{alice, 1}], fun admin_revoke_token_no_token/2). - -admin_revoke_token_no_token(Config, Alice) -> - Req = #{query => admin_revoke_token_mutation(), operationName => <<"M1">>, - variables => #{<<"user">> => user_to_bin(Alice)}}, - Res = execute_auth(Req, Config), - ParsedRes = error_result(<<"extensions">>, <<"code">>, Res), - ?assertEqual(<<"not_found">>, ParsedRes). - -admin_revoke_token(Config) -> - escalus:fresh_story_with_config(Config, [{alice, 1}], fun admin_revoke_token/2). - -admin_revoke_token(Config, Alice) -> - Req = #{query => admin_request_token_mutation(), operationName => <<"M1">>, - variables => #{<<"user">> => user_to_bin(Alice)}}, - execute_auth(Req, Config), - Req2 = #{query => admin_revoke_token_mutation(), operationName => <<"M1">>, - variables => #{<<"user">> => user_to_bin(Alice)}}, - Res2 = execute_auth(Req2, Config), - ParsedRes = ok_result(<<"token">>, <<"revokeToken">>, Res2), +admin_request_token_no_user_test(Config) -> + Res = admin_request_token(<<"AAAAA">>, Config), + ?assertEqual(<<"not_found">>, get_err_code(Res)). + +admin_revoke_token_no_user_test(Config) -> + Res = admin_revoke_token(<<"AAAAA">>, Config), + ?assertEqual(<<"not_found">>, get_err_code(Res)). + +admin_revoke_token_no_token_test(Config) -> + escalus:fresh_story_with_config(Config, [{alice, 1}], fun admin_revoke_token_no_token_test/2). + +admin_revoke_token_no_token_test(Config, Alice) -> + Res = admin_revoke_token(user_to_bin(Alice), Config), + ?assertEqual(<<"not_found">>, get_err_code(Res)). + +admin_revoke_token_test(Config) -> + escalus:fresh_story_with_config(Config, [{alice, 1}], fun admin_revoke_token_test/2). + +admin_revoke_token_test(Config, Alice) -> + admin_request_token(user_to_bin(Alice), Config), + Res2 = admin_revoke_token(user_to_bin(Alice), Config), + ParsedRes = get_ok_value([data, token, revokeToken], Res2), ?assertEqual(<<"Revoked.">>, ParsedRes). -user_request_token_mutation() -> - <<"mutation M1 - { token { - requestToken {access refresh} - } }">>. - -user_revoke_token_mutation() -> - <<"mutation M1 - { token { - revokeToken - } }">>. - -admin_request_token_mutation() -> - <<"mutation M1($user: JID!) - { token { - requestToken(user: $user) {access refresh} - } }">>. - -admin_revoke_token_mutation() -> - <<"mutation M1($user: JID!) - { token { - revokeToken(user: $user) - } }">>. - -ok_result(What1, What2, {{<<"200">>, <<"OK">>}, #{<<"data">> := Data}}) -> - maps:get(What2, maps:get(What1, Data)). - -error_result(What1, What2, {{<<"200">>, <<"OK">>}, #{<<"errors">> := [Data]}}) -> - maps:get(What2, maps:get(What1, Data)). +user_request_token(User, Config) -> + execute_user_command(<<"token">>, <<"requestToken">>, User, #{}, Config). + +user_revoke_token(User, Config) -> + execute_user_command(<<"token">>, <<"revokeToken">>, User, #{}, Config). + +admin_request_token(User, Config) -> + execute_command(<<"token">>, <<"requestToken">>, #{user => User}, Config). + +admin_revoke_token(User, Config) -> + execute_command(<<"token">>, <<"revokeToken">>, #{user => User}, Config).