Skip to content

Commit

Permalink
Merge pull request #3483 from esl/graphql/mu-stanza-api
Browse files Browse the repository at this point in the history
Stanza api for graphql
  • Loading branch information
chrzaszcz authored Jan 14, 2022
2 parents ab4ddf1 + d21a51c commit c468ee6
Show file tree
Hide file tree
Showing 27 changed files with 719 additions and 125 deletions.
1 change: 1 addition & 0 deletions big_tests/default.spec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
{suites, "tests", extdisco_SUITE}.
{suites, "tests", gdpr_SUITE}.
{suites, "tests", graphql_SUITE}.
{suites, "tests", graphql_stanza_SUITE}.
{suites, "tests", inbox_SUITE}.
{suites, "tests", inbox_extensions_SUITE}.
{suites, "tests", jingle_SUITE}.
Expand Down
1 change: 1 addition & 0 deletions big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"at the moment mod_pubsub doesn't support dynamic domains"}.

{suites, "tests", graphql_SUITE}.
{suites, "tests", graphql_stanza_SUITE}.

{suites, "tests", inbox_SUITE}.

Expand Down
32 changes: 8 additions & 24 deletions big_tests/tests/graphql_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("exml/include/exml.hrl").

-compile([export_all, nowarn_export_all]).

-import(distributed_helper, [mim/0, require_rpc_nodes/1, rpc/4]).
-import(graphql_helper, [execute/3, get_listener_port/1, get_listener_config/1]).
-import(graphql_helper, [execute/3]).

-define(assertAdminAuth(Auth, Data), assert_auth(atom_to_binary(Auth), Data)).
-define(assertUserAuth(Username, Auth, Data),
Expand Down Expand Up @@ -41,20 +42,15 @@ common_tests() ->
[can_load_graphiql].

init_per_suite(Config) ->
escalus:init_per_suite(Config).
Config1 = escalus:init_per_suite(Config),
dynamic_modules:save_modules(domain_helper:host_type(), Config1).

end_per_suite(Config) ->
dynamic_modules:restore_modules(Config),
escalus:end_per_suite(Config).

init_per_group(admin_handler, Config) ->
Endpoint = admin,
Opts = get_listener_opts(Endpoint),
case proplists:is_defined(username, Opts) of
true ->
[{schema_endpoint, Endpoint} | Config];
false ->
{skipped, <<"Admin credentials are not defined in config">>}
end;
graphql_helper:init_admin_handler(Config);
init_per_group(user_handler, Config) ->
Config1 = escalus:create_users(Config, escalus:get_users([alice])),
[{schema_endpoint, user} | Config1];
Expand Down Expand Up @@ -110,7 +106,7 @@ admin_checks_auth(Config) ->

auth_admin_checks_auth(Config) ->
Ep = ?config(schema_endpoint, Config),
Opts = get_listener_opts(Ep),
Opts = ?config(listener_opts, Config),
User = proplists:get_value(username, Opts),
Password = proplists:get_value(password, Opts),
Body = #{query => "{ checkAuth }"},
Expand All @@ -127,21 +123,9 @@ user_password(User) ->
[{User, Props}] = escalus:get_users([User]),
proplists:get_value(password, Props).

get_listener_opts(EpName) ->
{_, ejabberd_cowboy, Opts} = get_listener_config(EpName),
{value, {modules, Modules}} = lists:keysearch(modules, 1, Opts),
[Opts2] = lists:filtermap(
fun
({_, _Path, mongoose_graphql_cowboy_handler, Args}) ->
{true, Args};
(_) ->
false
end, Modules),
Opts2.

get_graphiql_website(EpName) ->
Request =
#{port => get_listener_port(EpName),
#{port => graphql_helper:get_listener_port(EpName),
role => {graphql, atom_to_binary(EpName)},
method => <<"GET">>,
headers => [{<<"Accept">>, <<"text/html">>}],
Expand Down
34 changes: 33 additions & 1 deletion big_tests/tests/graphql_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

-import(distributed_helper, [mim/0, rpc/4]).

-export([execute/3, get_listener_port/1, get_listener_config/1]).
-export([execute/3, execute_auth/2, get_listener_port/1, get_listener_config/1]).
-export([init_admin_handler/1]).

-include_lib("common_test/include/ct.hrl").

-spec execute(atom(), binary(), {binary(), binary()} | undefined) ->
{Status :: tuple(), Data :: map()}.
Expand All @@ -17,6 +20,13 @@ execute(EpName, Body, Creds) ->
body => Body},
rest_helper:make_request(Request).

execute_auth(Body, Config) ->
Ep = ?config(schema_endpoint, Config),
Opts = get_listener_opts(Ep),
User = proplists:get_value(username, Opts),
Password = proplists:get_value(password, Opts),
execute(Ep, Body, {User, Password}).

-spec get_listener_port(binary()) -> integer().
get_listener_port(EpName) ->
{PortIpNet, ejabberd_cowboy, _Opts} = get_listener_config(EpName),
Expand All @@ -29,6 +39,28 @@ get_listener_config(EpName) ->
lists:filter(fun(Config) -> is_graphql_config(Config, EpName) end, Listeners),
Config.

init_admin_handler(Config) ->
Endpoint = admin,
Opts = get_listener_opts(Endpoint),
case proplists:is_defined(username, Opts) of
true ->
[{schema_endpoint, Endpoint}, {listener_opts, Opts} | Config];
false ->
ct:fail(<<"Admin credentials are not defined in config">>)
end.

get_listener_opts(EpName) ->
{_, ejabberd_cowboy, Opts} = get_listener_config(EpName),
{value, {modules, Modules}} = lists:keysearch(modules, 1, Opts),
[Opts2] = lists:filtermap(
fun
({_, _Path, mongoose_graphql_cowboy_handler, Args}) ->
{true, Args};
(_) ->
false
end, Modules),
Opts2.

%% Internal

is_graphql_config({_PortIpNet, ejabberd_cowboy, Opts}, EpName) ->
Expand Down
Loading

0 comments on commit c468ee6

Please sign in to comment.