Skip to content

Commit

Permalink
Adding new tests checking stats returned after adding a user
Browse files Browse the repository at this point in the history
  • Loading branch information
Janusz Jakubiec authored and Janusz Jakubiec committed Jul 8, 2022
1 parent c20f804 commit 3fb89f2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
42 changes: 36 additions & 6 deletions big_tests/tests/graphql_stats_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ all() ->
[{group, admin_stats}].

groups() ->
[{admin_stats, [], admin_stats_handler()}].
[{admin_stats, [sequential], admin_stats_handler()}].

admin_stats_handler() ->
[admin_stats_test,
admin_stats_domain_test].
[admin_stats_global_test,
admin_stats_global_with_users_test,
admin_stats_domain_test,
admin_stats_domain_with_users_test].

init_per_suite(Config) ->
escalus:init_per_suite(Config).
Expand All @@ -40,16 +42,17 @@ end_per_group(_, _Config) ->
escalus_fresh:clean().

init_per_testcase(CaseName, Config) ->
escalus_fresh:clean(),
escalus:init_per_testcase(CaseName, Config).

end_per_testcase(CaseName, Config) ->
escalus:end_per_testcase(CaseName, Config).

% Admin test cases

admin_stats_test(Config) ->
admin_stats_global_test(Config) ->
GraphQlRequest = admin_get_stats(Config, #{}),
Result = ok_result(<<"stats">>, <<"stats">>, GraphQlRequest),
Result = ok_result(<<"stats">>, <<"globalStats">>, GraphQlRequest),
#{<<"uptimeSeconds">> := UptimeSeconds, <<"registeredUsers">> := RegisteredUsers,
<<"onlineUsersNode">> := OnlineUsersNode, <<"onlineUsers">> := OnlineUsers,
<<"incomingS2S">> := IncomingS2S, <<"outgoingS2S">> := OutgoingS2S} = Result,
Expand All @@ -60,6 +63,22 @@ admin_stats_test(Config) ->
?assertEqual(0, IncomingS2S),
?assertEqual(0, OutgoingS2S).

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

admin_stats_global_with_users_test(Config, _Alice) ->
GraphQlRequest = admin_get_stats(Config, #{}),
Result = ok_result(<<"stats">>, <<"globalStats">>, GraphQlRequest),
#{<<"uptimeSeconds">> := UptimeSeconds, <<"registeredUsers">> := RegisteredUsers,
<<"onlineUsersNode">> := OnlineUsersNode, <<"onlineUsers">> := OnlineUsers,
<<"incomingS2S">> := IncomingS2S, <<"outgoingS2S">> := OutgoingS2S} = Result,
?assertEqual(true, is_integer(UptimeSeconds)),
?assertEqual(1, RegisteredUsers),
?assertEqual(1, OnlineUsersNode),
?assertEqual(1, OnlineUsers),
?assertEqual(0, IncomingS2S),
?assertEqual(0, OutgoingS2S).

admin_stats_domain_test(Config) ->
Vars = #{<<"domain">> => domain()},
GraphQlRequest = admin_get_stats_domain(Config, Vars),
Expand All @@ -68,11 +87,22 @@ admin_stats_domain_test(Config) ->
?assertEqual(0, RegisteredUsers),
?assertEqual(0, OnlineUsers).

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

admin_stats_domain_with_users_test(Config, _Alice) ->
Vars = #{<<"domain">> => domain()},
GraphQlRequest = admin_get_stats_domain(Config, Vars),
Result = ok_result(<<"stats">>, <<"domainStats">>, GraphQlRequest),
#{<<"registeredUsers">> := RegisteredUsers, <<"onlineUsers">> := OnlineUsers} = Result,
?assertEqual(1, RegisteredUsers),
?assertEqual(1, OnlineUsers).

% Helpers

admin_get_stats(Config, Vars) ->
Query = <<"query Q1
{stats{stats{uptimeSeconds registeredUsers onlineUsersNode
{stats{globalStats{uptimeSeconds registeredUsers onlineUsersNode
onlineUsers incomingS2S outgoingS2S}}}">>,
admin_send_mutation(Config, Vars, Query).

Expand Down
4 changes: 2 additions & 2 deletions priv/graphql/schemas/admin/stats.gql
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"Allow admin to get statistics"
type StatsAdminQuery @protected{
"allow admin to acquire all nodes' statistics"
stats: Stats
globalStats: GlobalStats
@protected(type: GLOBAL)
"allow admin to acquire domain's statistics"
domainStats(domain: String!): DomainStats
@protected(type: DOMAIN, args: ["domain"])
}

type Stats {
type GlobalStats {
"uptime of the node"
uptimeSeconds: Int
"number of registered users"
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/admin/mongoose_graphql_stats_admin_query.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
-include("mongoose.hrl").
-include("jlib.hrl").

execute(_Ctx, stats, <<"stats">>, _Args) ->
{ok, stats};
execute(_Ctx, stats, <<"globalStats">>, _Args) ->
{ok, globalStats};
execute(_Ctx, stats, <<"domainStats">>, #{<<"domain">> := Domain}) ->
{ok, Domain}.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-module(mongoose_graphql_stats).
-module(mongoose_graphql_stats_global).
-behaviour(mongoose_graphql).

-export([execute/4]).
Expand All @@ -11,15 +11,15 @@
-include("mongoose.hrl").
-include("jlib.hrl").

execute(_Ctx, stats, <<"uptimeSeconds">>, _Args) ->
execute(_Ctx, globalStats, <<"uptimeSeconds">>, _Args) ->
{ok, stats_api:stats(<<"uptimeseconds">>)};
execute(_Ctx, stats, <<"registeredUsers">>, _Args) ->
execute(_Ctx, globalStats, <<"registeredUsers">>, _Args) ->
{ok, stats_api:stats(<<"registeredusers">>)};
execute(_Ctx, stats, <<"onlineUsersNode">>, _Args) ->
execute(_Ctx, globalStats, <<"onlineUsersNode">>, _Args) ->
{ok, stats_api:stats(<<"onlineusersnode">>)};
execute(_Ctx, stats, <<"onlineUsers">>, _Args) ->
execute(_Ctx, globalStats, <<"onlineUsers">>, _Args) ->
{ok, stats_api:stats(<<"onlineusers">>)};
execute(_Ctx, stats, <<"incomingS2S">>, _Args) ->
execute(_Ctx, globalStats, <<"incomingS2S">>, _Args) ->
{ok, stats_api:incoming_s2s_number()};
execute(_Ctx, stats, <<"outgoingS2S">>, _Args) ->
execute(_Ctx, globalStats, <<"outgoingS2S">>, _Args) ->
{ok, stats_api:outgoing_s2s_number()}.
2 changes: 1 addition & 1 deletion src/graphql/mongoose_graphql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ admin_mapping_rules() ->
'SessionAdminQuery' => mongoose_graphql_session_admin_query,
'StanzaAdminMutation' => mongoose_graphql_stanza_admin_mutation,
'StatsAdminQuery' => mongoose_graphql_stats_admin_query,
'Stats' => mongoose_graphql_stats,
'GlobalStats' => mongoose_graphql_stats_global,
'DomainStats' => mongoose_graphql_stats_domain,
'StanzaAdminQuery' => mongoose_graphql_stanza_admin_query,
'LastAdminMutation' => mongoose_graphql_last_admin_mutation,
Expand Down

0 comments on commit 3fb89f2

Please sign in to comment.