Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test GraphQL CLI for all categories #3710

Merged
merged 14 commits into from
Jul 22, 2022
304 changes: 101 additions & 203 deletions big_tests/tests/graphql_domain_SUITE.erl

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion big_tests/tests/graphql_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
-import(distributed_helper, [mim/0, rpc/4]).

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

-spec execute(atom(), binary(), {binary(), binary()} | undefined) ->
Expand Down Expand Up @@ -115,18 +116,31 @@ get_err_code(Resp) ->
get_err_msg(Resp) ->
get_err_msg(1, Resp).

get_coercion_err_msg({Code, #{<<"errors">> := [Error]}}) ->
assert_response_code(bad_request, Code),
?assertEqual(<<"input_coercion">>, get_value([extensions, code], Error)),
get_value([message], Error).

get_err_msg(N, Resp) ->
get_value([message], get_error(N, Resp)).

get_error(N, {Code, #{<<"errors">> := Errors}}) ->
assert_response_code(error, Code),
lists:nth(N, Errors).

%% Expect both errors and successful responses
get_err_value(Path, {Code, Data}) ->
assert_response_code(error, Code),
get_value(Path, Data).

get_ok_value(Path, {Code, Data}) ->
assert_response_code(ok, Code),
get_value(Path, Data).

assert_response_code(_, {<<"200">>, <<"OK">>}) -> ok;
assert_response_code(bad_request, {<<"400">>, <<"Bad Request">>}) -> ok;
assert_response_code(error, {<<"200">>, <<"OK">>}) -> ok;
assert_response_code(ok, {<<"200">>, <<"OK">>}) -> ok;
assert_response_code(bad_request, {exit_status, 1}) -> ok;
assert_response_code(error, {exit_status, 1}) -> ok;
assert_response_code(ok, {exit_status, 0}) -> ok;
assert_response_code(Type, Code) ->
Expand Down
189 changes: 80 additions & 109 deletions big_tests/tests/graphql_http_upload_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,93 @@

-compile([export_all, nowarn_export_all]).

-import(distributed_helper, [require_rpc_nodes/1]).
-import(distributed_helper, [mim/0, require_rpc_nodes/1]).
-import(domain_helper, [host_type/0, domain/0]).
-import(graphql_helper, [execute_user/3, execute_auth/2, user_to_bin/1]).
-import(graphql_helper, [execute_user_command/5, execute_command/4, get_ok_value/2,
get_err_msg/1, 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").
-include("../../include/mod_roster.hrl").

-define(S3_HOSTNAME, <<"http://bucket.s3-eu-east-25.example.com">>).

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

all() ->
[{group, user_http_upload},
{group, user_http_upload_not_configured},
{group, admin_http_upload},
{group, admin_http_upload_not_configured}].
[{group, user},
{group, admin_http},
{group, admin_cli}].

groups() ->
[{user_http_upload, [], user_http_upload_handler()},
{user_http_upload_not_configured, [], user_http_upload_not_configured_handler()},
{admin_http_upload, [], admin_http_upload_handler()},
{admin_http_upload_not_configured, [], admin_http_upload_not_configured_handler()}].
[{user, [], user_groups()},
{admin_http, [], admin_groups()},
{admin_cli, [], admin_groups()},
{user_http_upload, [], user_http_upload_tests()},
{user_http_upload_not_configured, [], user_http_upload_not_configured_tests()},
{admin_http_upload, [], admin_http_upload_tests()},
{admin_http_upload_not_configured, [], admin_http_upload_not_configured_tests()}].

user_groups() ->
[{group, user_http_upload},
{group, user_http_upload_not_configured}].

user_http_upload_handler() ->
admin_groups() ->
[{group, admin_http_upload},
{group, admin_http_upload_not_configured}].

user_http_upload_tests() ->
[user_get_url_test,
user_get_url_zero_size,
user_get_url_too_large_size,
user_get_url_zero_timeout].

user_http_upload_not_configured_handler() ->
user_http_upload_not_configured_tests() ->
[user_http_upload_not_configured].

admin_http_upload_handler() ->
admin_http_upload_tests() ->
[admin_get_url_test,
admin_get_url_zero_size,
admin_get_url_too_large_size,
admin_get_url_zero_timeout,
admin_get_url_no_domain].

admin_http_upload_not_configured_handler() ->
admin_http_upload_not_configured_tests() ->
[admin_http_upload_not_configured].

init_per_suite(Config) ->
Config1 = dynamic_modules:save_modules(host_type(), Config),
escalus:init_per_suite(Config1).
Config2 = ejabberd_node_utils:init(mim(), Config1),
escalus:init_per_suite(Config2).

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

init_per_group(user, Config) ->
graphql_helper:init_user(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_http_upload, Config) ->
dynamic_modules:ensure_modules(host_type(),
[{mod_http_upload, create_opts(?S3_HOSTNAME, true)}]),
[{schema_endpoint, user} | Config];
Config;
init_per_group(user_http_upload_not_configured, Config) ->
dynamic_modules:ensure_modules(host_type(), [{mod_http_upload, stopped}]),
[{schema_endpoint, user} | Config];
Config;
init_per_group(admin_http_upload, Config) ->
dynamic_modules:ensure_modules(host_type(),
[{mod_http_upload, create_opts(?S3_HOSTNAME, true)}]),
graphql_helper:init_admin_handler(Config);
Config;
init_per_group(admin_http_upload_not_configured, Config) ->
dynamic_modules:ensure_modules(host_type(), [{mod_http_upload, stopped}]),
graphql_helper:init_admin_handler(Config).
Config.

end_per_group(GroupName, _Config) when GroupName =:= user;
GroupName =:= admin_http;
GroupName =:= admin_cli ->
graphql_helper:clean();
end_per_group(_, _Config) ->
escalus_fresh:clean().

Expand Down Expand Up @@ -100,10 +118,8 @@ user_get_url_test(Config) ->
fun user_get_url_test/2).

user_get_url_test(Config, Alice) ->
Vars = #{<<"filename">> => <<"test">>, <<"size">> => 123,
<<"contentType">> => <<"Test">>, <<"timeout">> => 123},
GraphQlRequest = user_send_request(Config, Vars, Alice),
ParsedResult = ok_result(<<"httpUpload">>, <<"getUrl">>, GraphQlRequest),
Result = user_get_url(<<"test">>, 123, <<"Test">>, 123, Alice, Config),
ParsedResult = get_ok_value([data, httpUpload, getUrl], Result),
#{<<"PutUrl">> := PutURL, <<"GetUrl">> := GetURL, <<"Header">> := _Headers} = ParsedResult,
?assertMatch({_, _}, binary:match(PutURL, [?S3_HOSTNAME])),
?assertMatch({_, _}, binary:match(GetURL, [?S3_HOSTNAME])).
Expand All @@ -113,124 +129,79 @@ user_get_url_zero_size(Config) ->
fun user_get_url_zero_size/2).

user_get_url_zero_size(Config, Alice) ->
Vars = #{<<"filename">> => <<"test">>, <<"size">> => 0,
<<"contentType">> => <<"Test">>, <<"timeout">> => 123},
GraphQlRequest = user_send_request(Config, Vars, Alice),
ParsedResult = error_result(<<"extensions">>, <<"code">>, GraphQlRequest),
?assertEqual(<<"size_error">>, ParsedResult).
Result = user_get_url(<<"test">>, 0, <<"Test">>, 123, Alice, Config),
?assertEqual(<<"size_error">>, get_err_code(Result)),
?assertEqual(<<"size must be positive integer">>, get_err_msg(Result)).

user_get_url_too_large_size(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}],
fun user_get_url_too_large_size/2).

user_get_url_too_large_size(Config, Alice) ->
Vars = #{<<"filename">> => <<"test">>, <<"size">> => 100000,
<<"contentType">> => <<"Test">>, <<"timeout">> => 123},
GraphQlRequest = user_send_request(Config, Vars, Alice),
ParsedResult = error_result(<<"extensions">>, <<"code">>, GraphQlRequest),
?assertEqual(<<"file_too_large_error">>, ParsedResult).
Result = user_get_url(<<"test">>, 100000, <<"Test">>, 123, Alice, Config),
?assertEqual(<<"file_too_large_error">>, get_err_code(Result)),
?assertMatch(<<"Declared file size exceeds", _/bits>>, get_err_msg(Result)).

user_get_url_zero_timeout(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}],
fun user_get_url_zero_timeout/2).

user_get_url_zero_timeout(Config, Alice) ->
Vars = #{<<"filename">> => <<"test">>, <<"size">> => 123,
<<"contentType">> => <<"Test">>, <<"timeout">> => 0},
GraphQlRequest = user_send_request(Config, Vars, Alice),
ParsedResult = error_result(<<"extensions">>, <<"code">>, GraphQlRequest),
?assertEqual(<<"timeout_error">>, ParsedResult).
Result = user_get_url(<<"test">>, 123, <<"Test">>, 0, Alice, Config),
?assertEqual(<<"timeout_error">>, get_err_code(Result)),
?assertEqual(<<"timeout must be positive integer">>, get_err_msg(Result)).

user_http_upload_not_configured(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}],
fun user_http_upload_not_configured/2).

user_http_upload_not_configured(Config, Alice) ->
Vars = #{<<"filename">> => <<"test">>, <<"size">> => 123,
<<"contentType">> => <<"Test">>, <<"timeout">> => 123},
GraphQlRequest = user_send_request(Config, Vars, Alice),
ParsedResult = error_result(<<"extensions">>, <<"code">>, GraphQlRequest),
?assertEqual(<<"module_not_loaded_error">>, ParsedResult).
Result = user_get_url(<<"test">>, 123, <<"Test">>, 123, Alice, Config),
?assertEqual(<<"module_not_loaded_error">>, get_err_code(Result)),
?assertEqual(<<"mod_http_upload is not loaded for this host">>, get_err_msg(Result)).

% Admin test cases

admin_get_url_test(Config) ->
Vars = #{<<"domain">> => domain(), <<"filename">> => <<"test">>,
<<"size">> => 123, <<"contentType">> => <<"Test">>, <<"timeout">> => 123},
GraphQlRequest = admin_send_request(Config, Vars),
ParsedResult = ok_result(<<"httpUpload">>, <<"getUrl">>, GraphQlRequest),
Result = admin_get_url(domain(), <<"test">>, 123, <<"Test">>, 123, Config),
ParsedResult = get_ok_value([data, httpUpload, getUrl], Result),
#{<<"PutUrl">> := PutURL, <<"GetUrl">> := GetURL, <<"Header">> := _Headers} = ParsedResult,
?assertMatch({_, _}, binary:match(PutURL, [?S3_HOSTNAME])),
?assertMatch({_, _}, binary:match(GetURL, [?S3_HOSTNAME])).

admin_get_url_zero_size(Config) ->
Vars = #{<<"domain">> => domain(), <<"filename">> => <<"test">>,
<<"size">> => 0, <<"contentType">> => <<"Test">>, <<"timeout">> => 123},
GraphQlRequest = admin_send_request(Config, Vars),
ParsedResult = error_result(<<"extensions">>, <<"code">>, GraphQlRequest),
?assertEqual(<<"size_error">>, ParsedResult).
Result = admin_get_url(domain(), <<"test">>, 0, <<"Test">>, 123, Config),
?assertEqual(<<"size_error">>, get_err_code(Result)),
?assertEqual(<<"size must be positive integer">>, get_err_msg(Result)).

admin_get_url_too_large_size(Config) ->
Vars = #{<<"domain">> => domain(), <<"filename">> => <<"test">>,
<<"size">> => 1000000, <<"contentType">> => <<"Test">>, <<"timeout">> => 123},
GraphQlRequest = admin_send_request(Config, Vars),
ParsedResult = error_result(<<"extensions">>, <<"code">>, GraphQlRequest),
?assertEqual(<<"file_too_large_error">>, ParsedResult).
Result = admin_get_url(domain(), <<"test">>, 100000, <<"Test">>, 123, Config),
?assertEqual(<<"file_too_large_error">>, get_err_code(Result)),
?assertMatch(<<"Declared file size exceeds", _/bits>>, get_err_msg(Result)).

admin_get_url_zero_timeout(Config) ->
Vars = #{<<"domain">> => domain(), <<"filename">> => <<"test">>,
<<"size">> => 123, <<"contentType">> => <<"Test">>, <<"timeout">> => 0},
GraphQlRequest = admin_send_request(Config, Vars),
ParsedResult = error_result(<<"extensions">>, <<"code">>, GraphQlRequest),
?assertEqual(<<"timeout_error">>, ParsedResult).
Result = admin_get_url(domain(), <<"test">>, 123, <<"Test">>, 0, Config),
?assertEqual(<<"timeout_error">>, get_err_code(Result)),
?assertEqual(<<"timeout must be positive integer">>, get_err_msg(Result)).

admin_get_url_no_domain(Config) ->
Vars = #{<<"domain">> => <<"AAAAA">>, <<"filename">> => <<"test">>,
<<"size">> => 123, <<"contentType">> => <<"Test">>, <<"timeout">> => 123},
GraphQlRequest = admin_send_request(Config, Vars),
ParsedResult = error_result(<<"extensions">>, <<"code">>, GraphQlRequest),
?assertEqual(<<"domain_not_found">>, ParsedResult).
Result = admin_get_url(<<"AAAAA">>, <<"test">>, 123, <<"Test">>, 123, Config),
?assertEqual(<<"domain_not_found">>, get_err_code(Result)),
?assertEqual(<<"domain does not exist">>, get_err_msg(Result)).

admin_http_upload_not_configured(Config) ->
Vars = #{<<"domain">> => domain(), <<"filename">> => <<"test">>,
<<"size">> => 123, <<"contentType">> => <<"Test">>, <<"timeout">> => 123},
GraphQlRequest = admin_send_request(Config, Vars),
ParsedResult = error_result(<<"extensions">>, <<"code">>, GraphQlRequest),
?assertEqual(<<"module_not_loaded_error">>, ParsedResult).
Result = admin_get_url(domain(), <<"test">>, 123, <<"Test">>, 123, Config),
?assertEqual(<<"module_not_loaded_error">>, get_err_code(Result)),
?assertEqual(<<"mod_http_upload is not loaded for this host">>, get_err_msg(Result)).

% Helpers

user_get_url() ->
<<"mutation M1($filename: String!, $size: Int!,
$contentType: String!, $timeout: Int!)",
"{httpUpload{getUrl(filename: $filename, size: $size,
contentType: $contentType, timeout: $timeout)
{PutUrl, GetUrl, Header}}}">>.

admin_get_url() ->
<<"mutation M1($domain: String!, $filename: String!, $size: Int!,
$contentType: String!, $timeout: Int!)",
"{httpUpload{getUrl(domain: $domain, filename: $filename,
size: $size, contentType: $contentType, timeout: $timeout)
{PutUrl, GetUrl, Header}}}">>.

user_send_request(Config, Vars, User) ->
Body = #{query => user_get_url(), operationName => <<"M1">>, variables => Vars},
execute_user(Body, User, Config).

admin_send_request(Config, Vars) ->
Body = #{query => admin_get_url(), operationName => <<"M1">>, variables => Vars},
execute_auth(Body, Config).

error_result(What1, What2, {{<<"200">>, <<"OK">>}, #{<<"errors">> := [Data]}}) ->
maps:get(What2, maps:get(What1, Data)).

ok_result(What1, What2, {{<<"200">>, <<"OK">>}, #{<<"data">> := Data}}) ->
maps:get(What2, maps:get(What1, Data)).

url_contains(UrlType, Filename, Result) ->
Url = extract_url(Result, UrlType),
binary:match(Url, Filename) =/= nomatch.
user_get_url(FileName, Size, ContentType, Timeout, User, Config) ->
Vars = #{<<"filename">> => FileName, <<"size">> => Size,
<<"contentType">> => ContentType, <<"timeout">> => Timeout},
execute_user_command(<<"httpUpload">>, <<"getUrl">>, User, Vars, Config).

extract_url(Result, UrlType) ->
exml_query:path(Result, [{element, <<"slot">>}, {element, UrlType}, {attr, <<"url">>}]).
admin_get_url(Domain, FileName, Size, ContentType, Timeout, Config) ->
Vars = #{<<"domain">> => Domain, <<"filename">> => FileName, <<"size">> => Size,
<<"contentType">> => ContentType, <<"timeout">> => Timeout},
execute_command(<<"httpUpload">>, <<"getUrl">>, Vars, Config).
Loading