Skip to content

Commit

Permalink
Use pre-generated user queries in big tests for GraphQL
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Jul 15, 2022
1 parent cc23340 commit a169204
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 197 deletions.
54 changes: 19 additions & 35 deletions big_tests/tests/graphql_account_SUITE.erl
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
-module(graphql_account_SUITE).

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

-compile([export_all, nowarn_export_all]).

-import(distributed_helper, [mim/0, require_rpc_nodes/1, rpc/4]).
-import(graphql_helper, [execute/3, execute_auth/2, execute_command/4, get_listener_port/1,
-import(graphql_helper, [execute_command/4, execute_user_command/5, get_listener_port/1,
get_listener_config/1, get_ok_value/2, get_err_msg/1]).

-define(NOT_EXISTING_JID, <<"unknown987@unknown">>).
Expand Down Expand Up @@ -57,18 +56,14 @@ init_per_group(admin_account_http, Config) ->
init_per_group(admin_account_cli, Config) ->
graphql_helper:init_admin_cli(init_users(Config));
init_per_group(user_account, Config) ->
[{schema_endpoint, user} | Config];
init_per_group(_, Config) ->
Config.

end_per_group(admin_account_http, Config) ->
clean_users(Config);
end_per_group(admin_account_cli, Config) ->
clean_users(Config);
graphql_helper:init_user(Config).

end_per_group(user_account, _Config) ->
graphql_helper:clean(),
escalus_fresh:clean();
end_per_group(_, _Config) ->
ok.
end_per_group(_GroupName, Config) ->
graphql_helper:clean(),
clean_users(Config).

init_users(Config) ->
escalus:create_users(Config, escalus:get_users([alice])).
Expand Down Expand Up @@ -109,33 +104,24 @@ user_unregister(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}], fun user_unregister_story/2).

user_unregister_story(Config, Alice) ->
Ep = ?config(schema_endpoint, Config),
Password = lists:last(escalus_users:get_usp(Config, alice)),
BinJID = escalus_client:full_jid(Alice),
Creds = {BinJID, Password},
Query = <<"mutation { account { unregister } }">>,

Resp = user_unregister(Alice, Config),
Path = [data, account, unregister],
Resp = execute(Ep, #{query => Query}, Creds),
?assertNotEqual(nomatch, binary:match(get_ok_value(Path, Resp), <<"successfully unregistered">>)),
% Ensure the user is removed
AllUsers = rpc(mim(), mongoose_account_api, list_users, [domain_helper:domain()]),
LAliceJID = jid:to_binary(jid:to_lower((jid:binary_to_bare(BinJID)))),
LAliceJID = jid:to_binary(jid:from_binary(escalus_client:short_jid(Alice))),
?assertNot(lists:member(LAliceJID, AllUsers)).

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

user_change_password_story(Config, Alice) ->
Ep = ?config(schema_endpoint, Config),
Password = lists:last(escalus_users:get_usp(Config, alice)),
Creds = {escalus_client:full_jid(Alice), Password},
% Set an empty password
Resp1 = execute(Ep, user_change_password_body(<<>>), Creds),
Resp1 = user_change_password(Alice, <<>>, Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Resp1), <<"Empty password">>)),
% Set a correct password
Resp2 = user_change_password(Alice, <<"kaczka">>, Config),
Path = [data, account, changePassword],
Resp2 = execute(Ep, user_change_password_body(<<"kaczka">>), Creds),
?assertNotEqual(nomatch, binary:match(get_ok_value(Path, Resp2), <<"Password changed">>)).

admin_list_users(Config) ->
Expand Down Expand Up @@ -282,7 +268,14 @@ get_md5(AccountPass) ->
lists:flatten([io_lib:format("~.16B", [X])
|| X <- binary_to_list(crypto:hash(md5, AccountPass))]).

%% Admin commands
%% Commands

user_unregister(User, Config) ->
execute_user_command(<<"account">>, <<"unregister">>, User, #{}, Config).

user_change_password(User, Password, Config) ->
Vars = #{<<"newPassword">> => Password},
execute_user_command(<<"account">>, <<"changePassword">>, User, Vars, Config).

list_users(Domain, Config) ->
Vars = #{<<"domain">> => Domain},
Expand Down Expand Up @@ -323,12 +316,3 @@ ban_user(JID, Reason, Config) ->
change_user_password(JID, NewPassword, Config) ->
Vars = #{<<"user">> => JID, <<"newPassword">> => NewPassword},
execute_command(<<"account">>, <<"changeUserPassword">>, Vars, Config).

%% Request bodies

user_change_password_body(NewPassword) ->
Query = <<"mutation M1($newPassword: String!)
{ account { changePassword(newPassword: $newPassword) } }">>,
OpName = <<"M1">>,
Vars = #{<<"newPassword">> => NewPassword},
#{query => Query, operationName => OpName, variables => Vars}.
4 changes: 1 addition & 3 deletions big_tests/tests/graphql_metric_SUITE.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
-module(graphql_metric_SUITE).

-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]).

Expand Down Expand Up @@ -51,7 +49,7 @@ init_per_group(metrics_cli, Config) ->
graphql_helper:init_admin_cli(Config).

end_per_group(_GroupName, _Config) ->
ok.
graphql_helper:clean().

init_per_testcase(CaseName, Config) ->
escalus:init_per_testcase(CaseName, Config).
Expand Down
195 changes: 36 additions & 159 deletions big_tests/tests/graphql_vcard_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
-compile([export_all, nowarn_export_all]).

-import(distributed_helper, [require_rpc_nodes/1, mim/0]).
-import(graphql_helper, [execute_user/3, execute_command/4, user_to_bin/1, get_ok_value/2,
skip_null_fields/1, get_err_msg/1]).
-import(graphql_helper, [execute_command/4, execute_user_command/5,
user_to_bin/1, get_ok_value/2, skip_null_fields/1, get_err_msg/1]).

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

suite() ->
require_rpc_nodes([mim]) ++ escalus:suite().
Expand Down Expand Up @@ -58,9 +57,10 @@ init_per_group(admin_vcard_http, Config) ->
init_per_group(admin_vcard_cli, Config) ->
graphql_helper:init_admin_cli(Config);
init_per_group(user_vcard, Config) ->
[{schema_endpoint, user} | Config].
graphql_helper:init_user(Config).

end_per_group(_GroupName, _Config) ->
graphql_helper:clean(),
escalus_fresh:clean().

init_per_testcase(CaseName, Config) ->
Expand All @@ -77,15 +77,12 @@ user_set_vcard(Config) ->

user_set_vcard(Config, Alice) ->
Vcard = complete_vcard_input(),
BodySet = set_vcard_body_user(Vcard),
GraphQlRequestSet = execute_user(BodySet, Alice, Config),
ParsedResultSet = ok_result(<<"vcard">>, <<"setVcard">>, GraphQlRequestSet),
?assertEqual(Vcard, ParsedResultSet),
QueryGet = user_get_full_vcard_as_result_query(),
BodyGet = #{query => QueryGet, operationName => <<"Q1">>, variables => #{}},
GraphQlRequestGet = execute_user(BodyGet, Alice, Config),
ParsedResultGet = ok_result(<<"vcard">>, <<"getVcard">>, GraphQlRequestGet),
?assertEqual(Vcard, ParsedResultGet).
ResultSet = user_set_vcard(Alice, Vcard, Config),
ParsedResultSet = get_ok_value([data, vcard, setVcard], ResultSet),
?assertEqual(Vcard, skip_null_fields(ParsedResultSet)),
ResultGet = user_get_own_vcard(Alice, Config),
ParsedResultGet = get_ok_value([data, vcard, getVcard], ResultGet),
?assertEqual(Vcard, skip_null_fields(ParsedResultGet)).

user_get_their_vcard(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}],
Expand All @@ -99,20 +96,17 @@ user_get_their_vcard(Config, Alice) ->
<<"email">> => [#{<<"userId">> => <<"TESTEMAIL">>, <<"tags">> => [<<"HOME">>, <<"WORK">>]},
#{<<"userId">> => <<"TESTEMAIL2">>, <<"tags">> => [<<"HOME">>]}]},
escalus_client:send_and_wait(Alice, escalus_stanza:vcard_update(Client1Fields)),
Body = #{query => user_get_query(), operationName => <<"Q1">>, variables => #{}},
GraphQlRequest = execute_user(Body, Alice, Config),
ParsedResult = ok_result(<<"vcard">>, <<"getVcard">>, GraphQlRequest),
?assertEqual(ExpectedResult, ParsedResult).
Result = user_get_own_vcard(Alice, Config),
ParsedResult = get_ok_value([data, vcard, getVcard], Result),
?assertEqual(ExpectedResult, skip_null_fields(ParsedResult)).

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

user_get_their_vcard_no_vcard(Config, Alice) ->
Body = #{query => user_get_query(), operationName => <<"Q1">>, variables => #{}},
GraphQlRequest = execute_user(Body, Alice, Config),
ParsedResult = error_result(<<"message">>, GraphQlRequest),
?assertEqual(<<"Vcard for user not found">>, ParsedResult).
Result = user_get_own_vcard(Alice, Config),
?assertEqual(<<"Vcard for user not found">>, get_err_msg(Result)).

user_get_others_vcard(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}],
Expand All @@ -125,34 +119,27 @@ user_get_others_vcard(Config, Alice, Bob) ->
ExpectedResult = #{<<"formattedName">> => <<"TESTNAME">>,
<<"email">> => [#{<<"userId">> => <<"TESTEMAIL">>, <<"tags">> => [<<"HOME">>, <<"WORK">>]},
#{<<"userId">> => <<"TESTEMAIL2">>, <<"tags">> => [<<"HOME">>]}]},
escalus_client:send_and_wait(Bob, escalus_stanza:vcard_update([{<<"VERSION">>, <<"TESTVERSION">>} | Client1Fields])),
Body = #{query => user_get_query(), operationName => <<"Q1">>,
variables => #{user => user_to_bin(Bob)}},
GraphQlRequest = execute_user(Body, Alice, Config),
ParsedResult = ok_result(<<"vcard">>, <<"getVcard">>, GraphQlRequest),
?assertEqual(ExpectedResult, ParsedResult).
escalus_client:send_and_wait(Bob, escalus_stanza:vcard_update([{<<"VERSION">>, <<"TESTVERSION">>}
| Client1Fields])),
Result = user_get_vcard(Alice, user_to_bin(Bob), Config),
ParsedResult = get_ok_value([data, vcard, getVcard], Result),
?assertEqual(ExpectedResult, skip_null_fields(ParsedResult)).

user_get_others_vcard_no_vcard(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}],
fun user_get_others_vcard_no_vcard/3).

user_get_others_vcard_no_vcard(Config, Alice, Bob) ->
Body = #{query => user_get_query(), operationName => <<"Q1">>,
variables => #{user => user_to_bin(Bob)}},
GraphQlRequest = execute_user(Body, Alice, Config),
ParsedResult = error_result(<<"message">>, GraphQlRequest),
?assertEqual(<<"Vcard for user not found">>, ParsedResult).
Result = user_get_vcard(Alice, user_to_bin(Bob), Config),
?assertEqual(<<"Vcard for user not found">>, get_err_msg(Result)).

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

user_get_others_vcard_no_user(Config, Alice) ->
Body = #{query => user_get_query(), operationName => <<"Q1">>,
variables => #{user => <<"AAAAA">>}},
GraphQlRequest = execute_user(Body, Alice, Config),
ParsedResult = error_result(<<"message">>, GraphQlRequest),
?assertEqual(<<"User does not exist">>, ParsedResult).
Result = user_get_vcard(Alice, <<"AAAAA">>, Config),
?assertEqual(<<"User does not exist">>, get_err_msg(Result)).

%% Admin test cases

Expand Down Expand Up @@ -211,7 +198,18 @@ admin_get_vcard_no_user(Config) ->
Result = get_vcard(<<"AAAAA">>, Config),
?assertEqual(<<"User does not exist">>, get_err_msg(Result)).

%% Admin commands
%% Commands

user_set_vcard(User, VCard, Config) ->
Vars = #{vcard => VCard},
execute_user_command(<<"vcard">>, <<"setVcard">>, User, Vars, Config).

user_get_own_vcard(User, Config) ->
execute_user_command(<<"vcard">>, <<"getVcard">>, User, #{}, Config).

user_get_vcard(User, JID, Config) ->
Vars = #{user => JID},
execute_user_command(<<"vcard">>, <<"getVcard">>, User, Vars, Config).

set_vcard(VCard, User, Config) ->
Vars = #{vcard => VCard, user => User},
Expand All @@ -223,16 +221,6 @@ get_vcard(User, Config) ->

%% Helpers

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

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

set_vcard_body_user(Body) ->
QuerySet = user_get_full_vcard_as_result_mutation(),
#{query => QuerySet, operationName => <<"M1">>, variables => #{vcard => Body}}.

address_vcard_input() ->
#{<<"formattedName">> => <<"TestName">>,
<<"nameComponents">> => #{},
Expand Down Expand Up @@ -362,114 +350,3 @@ agent_vcard_input() ->
<<"binValue">> => <<"TestBinaries">>},
#{<<"extValue">> => <<"External Value">>}
]}.

user_get_query() ->
<<"query Q1($user: JID)
{
vcard
{
getVcard(user: $user)
{
formattedName
email
{
userId
tags
}
}
}
}">>.

user_get_full_vcard_as_result_mutation() ->
ResultFormat = get_full_vcard_as_result(),
<<"mutation M1($vcard: VcardInput!){vcard{setVcard(vcard: $vcard)",
ResultFormat/binary, "}}">>.

user_get_full_vcard_as_result_query() ->
ResultFormat = get_full_vcard_as_result(),
<<"query Q1($user: JID){vcard{getVcard(user: $user)",
ResultFormat/binary, "}}">>.

get_full_vcard_as_result() ->
<<"{
formattedName
nameComponents
{ family givenName middleName prefix suffix }
nickname
photo
{
... on ImageData
{ type binValue }
... on External
{ extValue }
}
birthday
address
{ tags pobox extadd street locality region pcode country }
label
{ tags line }
telephone
{ tags number }
email
{ tags userId }
jabberId
mailer
timeZone
geo
{ lat lon }
title
role
logo
{
... on ImageData
{ type binValue }
... on External
{ extValue }
}
agent
{
... on External
{ extValue }
... on AgentVcard
{
vcard
{
formattedName
nameComponents
{ family givenName middleName prefix suffix }
nickname
photo
{
... on ImageData
{ type binValue }
... on External
{ extValue }
}
}
}
}
org
{ orgname orgunit }
categories
{ keyword }
note
prodId
rev
sortString
sound
{
... on External
{ extValue }
... on BinValue
{ binValue }
... on Phonetic
{ phonetic }
}
uid
url
desc
class
{ tags }
key
{ type credential }
}">>.

0 comments on commit a169204

Please sign in to comment.