Skip to content

Commit

Permalink
Test graphql execution from ctl
Browse files Browse the repository at this point in the history
  • Loading branch information
Premwoik committed Dec 7, 2021
1 parent 0a27425 commit 197cf8a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 19 deletions.
18 changes: 1 addition & 17 deletions big_tests/tests/graphql_SUITE.erl
Expand Up @@ -5,6 +5,7 @@

-compile([export_all, nowarn_export_all]).
-import(distributed_helper, [mim/0, require_rpc_nodes/1, rpc/4]).
-import(graphql_helper, [load_test_schema/2]).

suite() ->
require_rpc_nodes([mim]) ++ escalus:suite().
Expand Down Expand Up @@ -145,23 +146,6 @@ user_password(User) ->
[{User, Props}] = escalus:get_users([User]),
proplists:get_value(password, Props).

load_test_schema(Name, Config) ->
Path = filename:join([?config(mim_data_dir, Config), "schema.gql"]),
{ok, SchemaData} = file:read_file(Path),
Ep = rpc(mim(), mongoose_graphql, get_endpoint, [Name]),
ok = rpc(mim(), graphql_schema, reset, [Ep]),
ok = rpc(mim(), graphql, load_schema, [Ep, test_schema_mapping(), SchemaData]),
ok = rpc(mim(), graphql, validate_schema, [Ep]),
ok.

test_schema_mapping() ->
#{objects => #{
'Query' => mongoose_graphql_default,
'Mutation' => mongoose_graphql_default,
default => mongoose_graphql_default
}
}.

get_port(EpName) ->
{PortIpNet, ejabberd_cowboy, _Opts} = get_listener_config(EpName),
element(1, PortIpNet).
Expand Down
24 changes: 24 additions & 0 deletions big_tests/tests/graphql_helper.erl
@@ -0,0 +1,24 @@
-module(graphql_helper).

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

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

-export([load_test_schema/2]).

load_test_schema(Name, Config) ->
Path = filename:join([?config(mim_data_dir, Config), "schema.gql"]),
{ok, SchemaData} = file:read_file(Path),
Ep = rpc(mim(), mongoose_graphql, get_endpoint, [Name]),
ok = rpc(mim(), graphql_schema, reset, [Ep]),
ok = rpc(mim(), graphql, load_schema, [Ep, test_schema_mapping(), SchemaData]),
ok = rpc(mim(), graphql, validate_schema, [Ep]),
ok.

test_schema_mapping() ->
#{objects => #{
'Query' => mongoose_graphql_default,
'Mutation' => mongoose_graphql_default,
default => mongoose_graphql_default
}
}.
35 changes: 33 additions & 2 deletions big_tests/tests/mongooseimctl_SUITE.erl
Expand Up @@ -112,7 +112,8 @@ all() ->
{group, stanza},
{group, stats},
{group, basic},
{group, upload}
{group, upload},
{group, graphql}
].

groups() ->
Expand All @@ -128,7 +129,8 @@ groups() ->
{stats, [sequence], stats()},
{upload, [], upload()},
{upload_with_acl, [], upload_enabled()},
{upload_without_acl, [], upload_enabled()}].
{upload_without_acl, [], upload_enabled()},
{graphql, [], graphql()}].

basic() ->
[simple_register,
Expand Down Expand Up @@ -183,6 +185,10 @@ upload_enabled() ->
real_upload_without_content_type,
real_upload_with_content_type].

graphql() ->
[can_execute_admin_protected_query,
can_execute_admin_unprotected_query].

suite() ->
require_rpc_nodes([mim]) ++ escalus:suite().

Expand Down Expand Up @@ -241,6 +247,10 @@ init_per_group(upload_without_acl, Config) ->
init_per_group(upload_with_acl, Config) ->
dynamic_modules:start(host_type(), mod_http_upload, ?MINIO_OPTS(true)),
[{with_acl, true} | Config];
init_per_group(graphql, Config) ->
% reset admin endpoint and load test schema
ok = graphql_helper:load_test_schema(admin, Config),
Config;
init_per_group(_GroupName, Config) ->
Config.

Expand Down Expand Up @@ -271,6 +281,9 @@ end_per_group(UploadGroup, Config) when UploadGroup =:= upload_without_acl;
UploadGroup =:= upload_with_acl ->
dynamic_modules:stop(host_type(), mod_http_upload),
Config;
end_per_group(graphql, _Config) ->
% reinit endpoints with original schemas
ok = rpc(mim(), mongoose_graphql, init, []);
end_per_group(_GroupName, Config) ->
Config.

Expand Down Expand Up @@ -1093,7 +1106,25 @@ stats_host(Config) ->
{"0\n", 0} = mongooseimctl("stats_host", ["onlineusers", SecDomain], Config)
end).

%%--------------------------------------------------------------------
%% mongoose_graphql tests
%%--------------------------------------------------------------------

can_execute_admin_protected_query(Config) ->
Query = "query { field }",
Res = mongooseimctl("graphql", [Query], Config),
?assertMatch({_, 0}, Res),
Data = element(1, Res),
% We expect resolver to crash, because valid resolver was not defined.
?assertNotEqual(nomatch, string:find(Data, "resolver_crash")).

can_execute_admin_unprotected_query(Config) ->
Query = "mutation { field }",
Res = mongooseimctl("graphql", [Query], Config),
?assertMatch({_, 0}, Res),
Data = element(1, Res),
% We expect resolver to crash, because valid resolver was not defined.
?assertNotEqual(nomatch, string:find(Data, "resolver_crash")).

%%-----------------------------------------------------------------
%% Improve coverage
Expand Down
21 changes: 21 additions & 0 deletions big_tests/tests/mongooseimctl_SUITE_data/schema.gql
@@ -0,0 +1,21 @@
schema{
query: Query,
mutation: Mutation
}

directive @protected on FIELD_DEFINITION | OBJECT

"""
Contains all available queries.
"""
type Query @protected{
field: String
}

"""
Contains all available mutations.
"""
type Mutation{
field: String
id(value: String!): String
}

0 comments on commit 197cf8a

Please sign in to comment.